File: | build/source/clang/lib/Parse/ParseTemplate.cpp |
Warning: | line 1745, column 51 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- ParseTemplate.cpp - Template Parsing -----------------------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file implements parsing of C++ templates. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "clang/AST/ASTContext.h" | |||
14 | #include "clang/AST/DeclTemplate.h" | |||
15 | #include "clang/AST/ExprCXX.h" | |||
16 | #include "clang/Parse/ParseDiagnostic.h" | |||
17 | #include "clang/Parse/Parser.h" | |||
18 | #include "clang/Parse/RAIIObjectsForParser.h" | |||
19 | #include "clang/Sema/DeclSpec.h" | |||
20 | #include "clang/Sema/ParsedTemplate.h" | |||
21 | #include "clang/Sema/Scope.h" | |||
22 | #include "clang/Sema/SemaDiagnostic.h" | |||
23 | #include "llvm/Support/TimeProfiler.h" | |||
24 | using namespace clang; | |||
25 | ||||
26 | /// Re-enter a possible template scope, creating as many template parameter | |||
27 | /// scopes as necessary. | |||
28 | /// \return The number of template parameter scopes entered. | |||
29 | unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) { | |||
30 | return Actions.ActOnReenterTemplateScope(D, [&] { | |||
31 | S.Enter(Scope::TemplateParamScope); | |||
32 | return Actions.getCurScope(); | |||
33 | }); | |||
34 | } | |||
35 | ||||
36 | /// Parse a template declaration, explicit instantiation, or | |||
37 | /// explicit specialization. | |||
38 | Decl *Parser::ParseDeclarationStartingWithTemplate( | |||
39 | DeclaratorContext Context, SourceLocation &DeclEnd, | |||
40 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { | |||
41 | ObjCDeclContextSwitch ObjCDC(*this); | |||
42 | ||||
43 | if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { | |||
44 | return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(), | |||
45 | DeclEnd, AccessAttrs, AS); | |||
46 | } | |||
47 | return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs, | |||
48 | AS); | |||
49 | } | |||
50 | ||||
51 | /// Parse a template declaration or an explicit specialization. | |||
52 | /// | |||
53 | /// Template declarations include one or more template parameter lists | |||
54 | /// and either the function or class template declaration. Explicit | |||
55 | /// specializations contain one or more 'template < >' prefixes | |||
56 | /// followed by a (possibly templated) declaration. Since the | |||
57 | /// syntactic form of both features is nearly identical, we parse all | |||
58 | /// of the template headers together and let semantic analysis sort | |||
59 | /// the declarations from the explicit specializations. | |||
60 | /// | |||
61 | /// template-declaration: [C++ temp] | |||
62 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration | |||
63 | /// | |||
64 | /// template-declaration: [C++2a] | |||
65 | /// template-head declaration | |||
66 | /// template-head concept-definition | |||
67 | /// | |||
68 | /// TODO: requires-clause | |||
69 | /// template-head: [C++2a] | |||
70 | /// 'template' '<' template-parameter-list '>' | |||
71 | /// requires-clause[opt] | |||
72 | /// | |||
73 | /// explicit-specialization: [ C++ temp.expl.spec] | |||
74 | /// 'template' '<' '>' declaration | |||
75 | Decl *Parser::ParseTemplateDeclarationOrSpecialization( | |||
76 | DeclaratorContext Context, SourceLocation &DeclEnd, | |||
77 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { | |||
78 | assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&(static_cast <bool> (Tok.isOneOf(tok::kw_export, tok::kw_template ) && "Token does not start a template declaration.") ? void (0) : __assert_fail ("Tok.isOneOf(tok::kw_export, tok::kw_template) && \"Token does not start a template declaration.\"" , "clang/lib/Parse/ParseTemplate.cpp", 79, __extension__ __PRETTY_FUNCTION__ )) | |||
79 | "Token does not start a template declaration.")(static_cast <bool> (Tok.isOneOf(tok::kw_export, tok::kw_template ) && "Token does not start a template declaration.") ? void (0) : __assert_fail ("Tok.isOneOf(tok::kw_export, tok::kw_template) && \"Token does not start a template declaration.\"" , "clang/lib/Parse/ParseTemplate.cpp", 79, __extension__ __PRETTY_FUNCTION__ )); | |||
80 | ||||
81 | MultiParseScope TemplateParamScopes(*this); | |||
82 | ||||
83 | // Tell the action that names should be checked in the context of | |||
84 | // the declaration to come. | |||
85 | ParsingDeclRAIIObject | |||
86 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); | |||
87 | ||||
88 | // Parse multiple levels of template headers within this template | |||
89 | // parameter scope, e.g., | |||
90 | // | |||
91 | // template<typename T> | |||
92 | // template<typename U> | |||
93 | // class A<T>::B { ... }; | |||
94 | // | |||
95 | // We parse multiple levels non-recursively so that we can build a | |||
96 | // single data structure containing all of the template parameter | |||
97 | // lists to easily differentiate between the case above and: | |||
98 | // | |||
99 | // template<typename T> | |||
100 | // class A { | |||
101 | // template<typename U> class B; | |||
102 | // }; | |||
103 | // | |||
104 | // In the first case, the action for declaring A<T>::B receives | |||
105 | // both template parameter lists. In the second case, the action for | |||
106 | // defining A<T>::B receives just the inner template parameter list | |||
107 | // (and retrieves the outer template parameter list from its | |||
108 | // context). | |||
109 | bool isSpecialization = true; | |||
110 | bool LastParamListWasEmpty = false; | |||
111 | TemplateParameterLists ParamLists; | |||
112 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); | |||
113 | ||||
114 | do { | |||
115 | // Consume the 'export', if any. | |||
116 | SourceLocation ExportLoc; | |||
117 | TryConsumeToken(tok::kw_export, ExportLoc); | |||
118 | ||||
119 | // Consume the 'template', which should be here. | |||
120 | SourceLocation TemplateLoc; | |||
121 | if (!TryConsumeToken(tok::kw_template, TemplateLoc)) { | |||
122 | Diag(Tok.getLocation(), diag::err_expected_template); | |||
123 | return nullptr; | |||
124 | } | |||
125 | ||||
126 | // Parse the '<' template-parameter-list '>' | |||
127 | SourceLocation LAngleLoc, RAngleLoc; | |||
128 | SmallVector<NamedDecl*, 4> TemplateParams; | |||
129 | if (ParseTemplateParameters(TemplateParamScopes, | |||
130 | CurTemplateDepthTracker.getDepth(), | |||
131 | TemplateParams, LAngleLoc, RAngleLoc)) { | |||
132 | // Skip until the semi-colon or a '}'. | |||
133 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); | |||
134 | TryConsumeToken(tok::semi); | |||
135 | return nullptr; | |||
136 | } | |||
137 | ||||
138 | ExprResult OptionalRequiresClauseConstraintER; | |||
139 | if (!TemplateParams.empty()) { | |||
140 | isSpecialization = false; | |||
141 | ++CurTemplateDepthTracker; | |||
142 | ||||
143 | if (TryConsumeToken(tok::kw_requires)) { | |||
144 | OptionalRequiresClauseConstraintER = | |||
145 | Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression( | |||
146 | /*IsTrailingRequiresClause=*/false)); | |||
147 | if (!OptionalRequiresClauseConstraintER.isUsable()) { | |||
148 | // Skip until the semi-colon or a '}'. | |||
149 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); | |||
150 | TryConsumeToken(tok::semi); | |||
151 | return nullptr; | |||
152 | } | |||
153 | } | |||
154 | } else { | |||
155 | LastParamListWasEmpty = true; | |||
156 | } | |||
157 | ||||
158 | ParamLists.push_back(Actions.ActOnTemplateParameterList( | |||
159 | CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc, | |||
160 | TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get())); | |||
161 | } while (Tok.isOneOf(tok::kw_export, tok::kw_template)); | |||
162 | ||||
163 | // Parse the actual template declaration. | |||
164 | if (Tok.is(tok::kw_concept)) | |||
165 | return ParseConceptDefinition( | |||
166 | ParsedTemplateInfo(&ParamLists, isSpecialization, | |||
167 | LastParamListWasEmpty), | |||
168 | DeclEnd); | |||
169 | ||||
170 | return ParseSingleDeclarationAfterTemplate( | |||
171 | Context, | |||
172 | ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty), | |||
173 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); | |||
174 | } | |||
175 | ||||
176 | /// Parse a single declaration that declares a template, | |||
177 | /// template specialization, or explicit instantiation of a template. | |||
178 | /// | |||
179 | /// \param DeclEnd will receive the source location of the last token | |||
180 | /// within this declaration. | |||
181 | /// | |||
182 | /// \param AS the access specifier associated with this | |||
183 | /// declaration. Will be AS_none for namespace-scope declarations. | |||
184 | /// | |||
185 | /// \returns the new declaration. | |||
186 | Decl *Parser::ParseSingleDeclarationAfterTemplate( | |||
187 | DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, | |||
188 | ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, | |||
189 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { | |||
190 | assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&(static_cast <bool> (TemplateInfo.Kind != ParsedTemplateInfo ::NonTemplate && "Template information required") ? void (0) : __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && \"Template information required\"" , "clang/lib/Parse/ParseTemplate.cpp", 191, __extension__ __PRETTY_FUNCTION__ )) | |||
191 | "Template information required")(static_cast <bool> (TemplateInfo.Kind != ParsedTemplateInfo ::NonTemplate && "Template information required") ? void (0) : __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && \"Template information required\"" , "clang/lib/Parse/ParseTemplate.cpp", 191, __extension__ __PRETTY_FUNCTION__ )); | |||
192 | ||||
193 | if (Tok.is(tok::kw_static_assert)) { | |||
194 | // A static_assert declaration may not be templated. | |||
195 | Diag(Tok.getLocation(), diag::err_templated_invalid_declaration) | |||
196 | << TemplateInfo.getSourceRange(); | |||
197 | // Parse the static_assert declaration to improve error recovery. | |||
198 | return ParseStaticAssertDeclaration(DeclEnd); | |||
199 | } | |||
200 | ||||
201 | if (Context == DeclaratorContext::Member) { | |||
202 | // We are parsing a member template. | |||
203 | DeclGroupPtrTy D = ParseCXXClassMemberDeclaration( | |||
204 | AS, AccessAttrs, TemplateInfo, &DiagsFromTParams); | |||
205 | ||||
206 | if (!D || !D.get().isSingleDecl()) | |||
207 | return nullptr; | |||
208 | return D.get().getSingleDecl(); | |||
209 | } | |||
210 | ||||
211 | ParsedAttributes prefixAttrs(AttrFactory); | |||
212 | MaybeParseCXX11Attributes(prefixAttrs); | |||
213 | ||||
214 | if (Tok.is(tok::kw_using)) { | |||
215 | auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, | |||
216 | prefixAttrs); | |||
217 | if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl()) | |||
218 | return nullptr; | |||
219 | return usingDeclPtr.get().getSingleDecl(); | |||
220 | } | |||
221 | ||||
222 | // Parse the declaration specifiers, stealing any diagnostics from | |||
223 | // the template parameters. | |||
224 | ParsingDeclSpec DS(*this, &DiagsFromTParams); | |||
225 | ||||
226 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, | |||
227 | getDeclSpecContextFromDeclaratorContext(Context)); | |||
228 | ||||
229 | if (Tok.is(tok::semi)) { | |||
230 | ProhibitAttributes(prefixAttrs); | |||
231 | DeclEnd = ConsumeToken(); | |||
232 | RecordDecl *AnonRecord = nullptr; | |||
233 | Decl *Decl = Actions.ParsedFreeStandingDeclSpec( | |||
234 | getCurScope(), AS, DS, ParsedAttributesView::none(), | |||
235 | TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams | |||
236 | : MultiTemplateParamsArg(), | |||
237 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation, | |||
238 | AnonRecord); | |||
239 | assert(!AnonRecord &&(static_cast <bool> (!AnonRecord && "Anonymous unions/structs should not be valid with template" ) ? void (0) : __assert_fail ("!AnonRecord && \"Anonymous unions/structs should not be valid with template\"" , "clang/lib/Parse/ParseTemplate.cpp", 240, __extension__ __PRETTY_FUNCTION__ )) | |||
240 | "Anonymous unions/structs should not be valid with template")(static_cast <bool> (!AnonRecord && "Anonymous unions/structs should not be valid with template" ) ? void (0) : __assert_fail ("!AnonRecord && \"Anonymous unions/structs should not be valid with template\"" , "clang/lib/Parse/ParseTemplate.cpp", 240, __extension__ __PRETTY_FUNCTION__ )); | |||
241 | DS.complete(Decl); | |||
242 | return Decl; | |||
243 | } | |||
244 | ||||
245 | // Move the attributes from the prefix into the DS. | |||
246 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) | |||
247 | ProhibitAttributes(prefixAttrs); | |||
248 | ||||
249 | // Parse the declarator. | |||
250 | ParsingDeclarator DeclaratorInfo(*this, DS, prefixAttrs, | |||
251 | (DeclaratorContext)Context); | |||
252 | if (TemplateInfo.TemplateParams) | |||
253 | DeclaratorInfo.setTemplateParameterLists(*TemplateInfo.TemplateParams); | |||
254 | ||||
255 | // Turn off usual access checking for template specializations and | |||
256 | // instantiations. | |||
257 | // C++20 [temp.spec] 13.9/6. | |||
258 | // This disables the access checking rules for function template explicit | |||
259 | // instantiation and explicit specialization: | |||
260 | // - parameter-list; | |||
261 | // - template-argument-list; | |||
262 | // - noexcept-specifier; | |||
263 | // - dynamic-exception-specifications (deprecated in C++11, removed since | |||
264 | // C++17). | |||
265 | bool IsTemplateSpecOrInst = | |||
266 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || | |||
267 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); | |||
268 | SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst); | |||
269 | ||||
270 | ParseDeclarator(DeclaratorInfo); | |||
271 | ||||
272 | if (IsTemplateSpecOrInst) | |||
273 | SAC.done(); | |||
274 | ||||
275 | // Error parsing the declarator? | |||
276 | if (!DeclaratorInfo.hasName()) { | |||
277 | // If so, skip until the semi-colon or a }. | |||
278 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); | |||
279 | if (Tok.is(tok::semi)) | |||
280 | ConsumeToken(); | |||
281 | return nullptr; | |||
282 | } | |||
283 | ||||
284 | llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() { | |||
285 | return std::string(DeclaratorInfo.getIdentifier() != nullptr | |||
286 | ? DeclaratorInfo.getIdentifier()->getName() | |||
287 | : "<unknown>"); | |||
288 | }); | |||
289 | ||||
290 | LateParsedAttrList LateParsedAttrs(true); | |||
291 | if (DeclaratorInfo.isFunctionDeclarator()) { | |||
292 | if (Tok.is(tok::kw_requires)) { | |||
293 | CXXScopeSpec &ScopeSpec = DeclaratorInfo.getCXXScopeSpec(); | |||
294 | DeclaratorScopeObj DeclScopeObj(*this, ScopeSpec); | |||
295 | if (ScopeSpec.isValid() && | |||
296 | Actions.ShouldEnterDeclaratorScope(getCurScope(), ScopeSpec)) | |||
297 | DeclScopeObj.EnterDeclaratorScope(); | |||
298 | ParseTrailingRequiresClause(DeclaratorInfo); | |||
299 | } | |||
300 | ||||
301 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); | |||
302 | } | |||
303 | ||||
304 | if (DeclaratorInfo.isFunctionDeclarator() && | |||
305 | isStartOfFunctionDefinition(DeclaratorInfo)) { | |||
306 | ||||
307 | // Function definitions are only allowed at file scope and in C++ classes. | |||
308 | // The C++ inline method definition case is handled elsewhere, so we only | |||
309 | // need to handle the file scope definition case. | |||
310 | if (Context != DeclaratorContext::File) { | |||
311 | Diag(Tok, diag::err_function_definition_not_allowed); | |||
312 | SkipMalformedDecl(); | |||
313 | return nullptr; | |||
314 | } | |||
315 | ||||
316 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { | |||
317 | // Recover by ignoring the 'typedef'. This was probably supposed to be | |||
318 | // the 'typename' keyword, which we should have already suggested adding | |||
319 | // if it's appropriate. | |||
320 | Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) | |||
321 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | |||
322 | DS.ClearStorageClassSpecs(); | |||
323 | } | |||
324 | ||||
325 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { | |||
326 | if (DeclaratorInfo.getName().getKind() != | |||
327 | UnqualifiedIdKind::IK_TemplateId) { | |||
328 | // If the declarator-id is not a template-id, issue a diagnostic and | |||
329 | // recover by ignoring the 'template' keyword. | |||
330 | Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; | |||
331 | return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), | |||
332 | &LateParsedAttrs); | |||
333 | } else { | |||
334 | SourceLocation LAngleLoc | |||
335 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); | |||
336 | Diag(DeclaratorInfo.getIdentifierLoc(), | |||
337 | diag::err_explicit_instantiation_with_definition) | |||
338 | << SourceRange(TemplateInfo.TemplateLoc) | |||
339 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); | |||
340 | ||||
341 | // Recover as if it were an explicit specialization. | |||
342 | TemplateParameterLists FakedParamLists; | |||
343 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( | |||
344 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, | |||
345 | std::nullopt, LAngleLoc, nullptr)); | |||
346 | ||||
347 | return ParseFunctionDefinition( | |||
348 | DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, | |||
349 | /*isSpecialization=*/true, | |||
350 | /*lastParameterListWasEmpty=*/true), | |||
351 | &LateParsedAttrs); | |||
352 | } | |||
353 | } | |||
354 | return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, | |||
355 | &LateParsedAttrs); | |||
356 | } | |||
357 | ||||
358 | // Parse this declaration. | |||
359 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, | |||
360 | TemplateInfo); | |||
361 | ||||
362 | if (Tok.is(tok::comma)) { | |||
363 | Diag(Tok, diag::err_multiple_template_declarators) | |||
364 | << (int)TemplateInfo.Kind; | |||
365 | SkipUntil(tok::semi); | |||
366 | return ThisDecl; | |||
367 | } | |||
368 | ||||
369 | // Eat the semi colon after the declaration. | |||
370 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); | |||
371 | if (LateParsedAttrs.size() > 0) | |||
372 | ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); | |||
373 | DeclaratorInfo.complete(ThisDecl); | |||
374 | return ThisDecl; | |||
375 | } | |||
376 | ||||
377 | /// \brief Parse a single declaration that declares a concept. | |||
378 | /// | |||
379 | /// \param DeclEnd will receive the source location of the last token | |||
380 | /// within this declaration. | |||
381 | /// | |||
382 | /// \returns the new declaration. | |||
383 | Decl * | |||
384 | Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo, | |||
385 | SourceLocation &DeclEnd) { | |||
386 | assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&(static_cast <bool> (TemplateInfo.Kind != ParsedTemplateInfo ::NonTemplate && "Template information required") ? void (0) : __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && \"Template information required\"" , "clang/lib/Parse/ParseTemplate.cpp", 387, __extension__ __PRETTY_FUNCTION__ )) | |||
387 | "Template information required")(static_cast <bool> (TemplateInfo.Kind != ParsedTemplateInfo ::NonTemplate && "Template information required") ? void (0) : __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && \"Template information required\"" , "clang/lib/Parse/ParseTemplate.cpp", 387, __extension__ __PRETTY_FUNCTION__ )); | |||
388 | assert(Tok.is(tok::kw_concept) &&(static_cast <bool> (Tok.is(tok::kw_concept) && "ParseConceptDefinition must be called when at a 'concept' keyword" ) ? void (0) : __assert_fail ("Tok.is(tok::kw_concept) && \"ParseConceptDefinition must be called when at a 'concept' keyword\"" , "clang/lib/Parse/ParseTemplate.cpp", 389, __extension__ __PRETTY_FUNCTION__ )) | |||
389 | "ParseConceptDefinition must be called when at a 'concept' keyword")(static_cast <bool> (Tok.is(tok::kw_concept) && "ParseConceptDefinition must be called when at a 'concept' keyword" ) ? void (0) : __assert_fail ("Tok.is(tok::kw_concept) && \"ParseConceptDefinition must be called when at a 'concept' keyword\"" , "clang/lib/Parse/ParseTemplate.cpp", 389, __extension__ __PRETTY_FUNCTION__ )); | |||
390 | ||||
391 | ConsumeToken(); // Consume 'concept' | |||
392 | ||||
393 | SourceLocation BoolKWLoc; | |||
394 | if (TryConsumeToken(tok::kw_bool, BoolKWLoc)) | |||
395 | Diag(Tok.getLocation(), diag::err_concept_legacy_bool_keyword) << | |||
396 | FixItHint::CreateRemoval(SourceLocation(BoolKWLoc)); | |||
397 | ||||
398 | DiagnoseAndSkipCXX11Attributes(); | |||
399 | ||||
400 | CXXScopeSpec SS; | |||
401 | if (ParseOptionalCXXScopeSpecifier( | |||
402 | SS, /*ObjectType=*/nullptr, | |||
403 | /*ObjectHasErrors=*/false, /*EnteringContext=*/false, | |||
404 | /*MayBePseudoDestructor=*/nullptr, | |||
405 | /*IsTypename=*/false, /*LastII=*/nullptr, /*OnlyNamespace=*/true) || | |||
406 | SS.isInvalid()) { | |||
407 | SkipUntil(tok::semi); | |||
408 | return nullptr; | |||
409 | } | |||
410 | ||||
411 | if (SS.isNotEmpty()) | |||
412 | Diag(SS.getBeginLoc(), | |||
413 | diag::err_concept_definition_not_identifier); | |||
414 | ||||
415 | UnqualifiedId Result; | |||
416 | if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, | |||
417 | /*ObjectHadErrors=*/false, /*EnteringContext=*/false, | |||
418 | /*AllowDestructorName=*/false, | |||
419 | /*AllowConstructorName=*/false, | |||
420 | /*AllowDeductionGuide=*/false, | |||
421 | /*TemplateKWLoc=*/nullptr, Result)) { | |||
422 | SkipUntil(tok::semi); | |||
423 | return nullptr; | |||
424 | } | |||
425 | ||||
426 | if (Result.getKind() != UnqualifiedIdKind::IK_Identifier) { | |||
427 | Diag(Result.getBeginLoc(), diag::err_concept_definition_not_identifier); | |||
428 | SkipUntil(tok::semi); | |||
429 | return nullptr; | |||
430 | } | |||
431 | ||||
432 | IdentifierInfo *Id = Result.Identifier; | |||
433 | SourceLocation IdLoc = Result.getBeginLoc(); | |||
434 | ||||
435 | DiagnoseAndSkipCXX11Attributes(); | |||
436 | ||||
437 | if (!TryConsumeToken(tok::equal)) { | |||
438 | Diag(Tok.getLocation(), diag::err_expected) << tok::equal; | |||
439 | SkipUntil(tok::semi); | |||
440 | return nullptr; | |||
441 | } | |||
442 | ||||
443 | ExprResult ConstraintExprResult = | |||
444 | Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression()); | |||
445 | if (ConstraintExprResult.isInvalid()) { | |||
446 | SkipUntil(tok::semi); | |||
447 | return nullptr; | |||
448 | } | |||
449 | ||||
450 | DeclEnd = Tok.getLocation(); | |||
451 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); | |||
452 | Expr *ConstraintExpr = ConstraintExprResult.get(); | |||
453 | return Actions.ActOnConceptDefinition(getCurScope(), | |||
454 | *TemplateInfo.TemplateParams, | |||
455 | Id, IdLoc, ConstraintExpr); | |||
456 | } | |||
457 | ||||
458 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in | |||
459 | /// angle brackets. Depth is the depth of this template-parameter-list, which | |||
460 | /// is the number of template headers directly enclosing this template header. | |||
461 | /// TemplateParams is the current list of template parameters we're building. | |||
462 | /// The template parameter we parse will be added to this list. LAngleLoc and | |||
463 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, | |||
464 | /// that enclose this template parameter list. | |||
465 | /// | |||
466 | /// \returns true if an error occurred, false otherwise. | |||
467 | bool Parser::ParseTemplateParameters( | |||
468 | MultiParseScope &TemplateScopes, unsigned Depth, | |||
469 | SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc, | |||
470 | SourceLocation &RAngleLoc) { | |||
471 | // Get the template parameter list. | |||
472 | if (!TryConsumeToken(tok::less, LAngleLoc)) { | |||
473 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; | |||
474 | return true; | |||
475 | } | |||
476 | ||||
477 | // Try to parse the template parameter list. | |||
478 | bool Failed = false; | |||
479 | // FIXME: Missing greatergreatergreater support. | |||
480 | if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) { | |||
481 | TemplateScopes.Enter(Scope::TemplateParamScope); | |||
482 | Failed = ParseTemplateParameterList(Depth, TemplateParams); | |||
483 | } | |||
484 | ||||
485 | if (Tok.is(tok::greatergreater)) { | |||
486 | // No diagnostic required here: a template-parameter-list can only be | |||
487 | // followed by a declaration or, for a template template parameter, the | |||
488 | // 'class' keyword. Therefore, the second '>' will be diagnosed later. | |||
489 | // This matters for elegant diagnosis of: | |||
490 | // template<template<typename>> struct S; | |||
491 | Tok.setKind(tok::greater); | |||
492 | RAngleLoc = Tok.getLocation(); | |||
493 | Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); | |||
494 | } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) { | |||
495 | Diag(Tok.getLocation(), diag::err_expected) << tok::greater; | |||
496 | return true; | |||
497 | } | |||
498 | return false; | |||
499 | } | |||
500 | ||||
501 | /// ParseTemplateParameterList - Parse a template parameter list. If | |||
502 | /// the parsing fails badly (i.e., closing bracket was left out), this | |||
503 | /// will try to put the token stream in a reasonable position (closing | |||
504 | /// a statement, etc.) and return false. | |||
505 | /// | |||
506 | /// template-parameter-list: [C++ temp] | |||
507 | /// template-parameter | |||
508 | /// template-parameter-list ',' template-parameter | |||
509 | bool | |||
510 | Parser::ParseTemplateParameterList(const unsigned Depth, | |||
511 | SmallVectorImpl<NamedDecl*> &TemplateParams) { | |||
512 | while (true) { | |||
513 | ||||
514 | if (NamedDecl *TmpParam | |||
515 | = ParseTemplateParameter(Depth, TemplateParams.size())) { | |||
516 | TemplateParams.push_back(TmpParam); | |||
517 | } else { | |||
518 | // If we failed to parse a template parameter, skip until we find | |||
519 | // a comma or closing brace. | |||
520 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, | |||
521 | StopAtSemi | StopBeforeMatch); | |||
522 | } | |||
523 | ||||
524 | // Did we find a comma or the end of the template parameter list? | |||
525 | if (Tok.is(tok::comma)) { | |||
526 | ConsumeToken(); | |||
527 | } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) { | |||
528 | // Don't consume this... that's done by template parser. | |||
529 | break; | |||
530 | } else { | |||
531 | // Somebody probably forgot to close the template. Skip ahead and | |||
532 | // try to get out of the expression. This error is currently | |||
533 | // subsumed by whatever goes on in ParseTemplateParameter. | |||
534 | Diag(Tok.getLocation(), diag::err_expected_comma_greater); | |||
535 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, | |||
536 | StopAtSemi | StopBeforeMatch); | |||
537 | return false; | |||
538 | } | |||
539 | } | |||
540 | return true; | |||
541 | } | |||
542 | ||||
543 | /// Determine whether the parser is at the start of a template | |||
544 | /// type parameter. | |||
545 | Parser::TPResult Parser::isStartOfTemplateTypeParameter() { | |||
546 | if (Tok.is(tok::kw_class)) { | |||
547 | // "class" may be the start of an elaborated-type-specifier or a | |||
548 | // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. | |||
549 | switch (NextToken().getKind()) { | |||
550 | case tok::equal: | |||
551 | case tok::comma: | |||
552 | case tok::greater: | |||
553 | case tok::greatergreater: | |||
554 | case tok::ellipsis: | |||
555 | return TPResult::True; | |||
556 | ||||
557 | case tok::identifier: | |||
558 | // This may be either a type-parameter or an elaborated-type-specifier. | |||
559 | // We have to look further. | |||
560 | break; | |||
561 | ||||
562 | default: | |||
563 | return TPResult::False; | |||
564 | } | |||
565 | ||||
566 | switch (GetLookAheadToken(2).getKind()) { | |||
567 | case tok::equal: | |||
568 | case tok::comma: | |||
569 | case tok::greater: | |||
570 | case tok::greatergreater: | |||
571 | return TPResult::True; | |||
572 | ||||
573 | default: | |||
574 | return TPResult::False; | |||
575 | } | |||
576 | } | |||
577 | ||||
578 | if (TryAnnotateTypeConstraint()) | |||
579 | return TPResult::Error; | |||
580 | ||||
581 | if (isTypeConstraintAnnotation() && | |||
582 | // Next token might be 'auto' or 'decltype', indicating that this | |||
583 | // type-constraint is in fact part of a placeholder-type-specifier of a | |||
584 | // non-type template parameter. | |||
585 | !GetLookAheadToken(Tok.is(tok::annot_cxxscope) ? 2 : 1) | |||
586 | .isOneOf(tok::kw_auto, tok::kw_decltype)) | |||
587 | return TPResult::True; | |||
588 | ||||
589 | // 'typedef' is a reasonably-common typo/thinko for 'typename', and is | |||
590 | // ill-formed otherwise. | |||
591 | if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef)) | |||
592 | return TPResult::False; | |||
593 | ||||
594 | // C++ [temp.param]p2: | |||
595 | // There is no semantic difference between class and typename in a | |||
596 | // template-parameter. typename followed by an unqualified-id | |||
597 | // names a template type parameter. typename followed by a | |||
598 | // qualified-id denotes the type in a non-type | |||
599 | // parameter-declaration. | |||
600 | Token Next = NextToken(); | |||
601 | ||||
602 | // If we have an identifier, skip over it. | |||
603 | if (Next.getKind() == tok::identifier) | |||
604 | Next = GetLookAheadToken(2); | |||
605 | ||||
606 | switch (Next.getKind()) { | |||
607 | case tok::equal: | |||
608 | case tok::comma: | |||
609 | case tok::greater: | |||
610 | case tok::greatergreater: | |||
611 | case tok::ellipsis: | |||
612 | return TPResult::True; | |||
613 | ||||
614 | case tok::kw_typename: | |||
615 | case tok::kw_typedef: | |||
616 | case tok::kw_class: | |||
617 | // These indicate that a comma was missed after a type parameter, not that | |||
618 | // we have found a non-type parameter. | |||
619 | return TPResult::True; | |||
620 | ||||
621 | default: | |||
622 | return TPResult::False; | |||
623 | } | |||
624 | } | |||
625 | ||||
626 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). | |||
627 | /// | |||
628 | /// template-parameter: [C++ temp.param] | |||
629 | /// type-parameter | |||
630 | /// parameter-declaration | |||
631 | /// | |||
632 | /// type-parameter: (See below) | |||
633 | /// type-parameter-key ...[opt] identifier[opt] | |||
634 | /// type-parameter-key identifier[opt] = type-id | |||
635 | /// (C++2a) type-constraint ...[opt] identifier[opt] | |||
636 | /// (C++2a) type-constraint identifier[opt] = type-id | |||
637 | /// 'template' '<' template-parameter-list '>' type-parameter-key | |||
638 | /// ...[opt] identifier[opt] | |||
639 | /// 'template' '<' template-parameter-list '>' type-parameter-key | |||
640 | /// identifier[opt] '=' id-expression | |||
641 | /// | |||
642 | /// type-parameter-key: | |||
643 | /// class | |||
644 | /// typename | |||
645 | /// | |||
646 | NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { | |||
647 | ||||
648 | switch (isStartOfTemplateTypeParameter()) { | |||
649 | case TPResult::True: | |||
650 | // Is there just a typo in the input code? ('typedef' instead of | |||
651 | // 'typename') | |||
652 | if (Tok.is(tok::kw_typedef)) { | |||
653 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); | |||
654 | ||||
655 | Diag(Tok.getLocation(), diag::note_meant_to_use_typename) | |||
656 | << FixItHint::CreateReplacement(CharSourceRange::getCharRange( | |||
657 | Tok.getLocation(), | |||
658 | Tok.getEndLoc()), | |||
659 | "typename"); | |||
660 | ||||
661 | Tok.setKind(tok::kw_typename); | |||
662 | } | |||
663 | ||||
664 | return ParseTypeParameter(Depth, Position); | |||
665 | case TPResult::False: | |||
666 | break; | |||
667 | ||||
668 | case TPResult::Error: { | |||
669 | // We return an invalid parameter as opposed to null to avoid having bogus | |||
670 | // diagnostics about an empty template parameter list. | |||
671 | // FIXME: Fix ParseTemplateParameterList to better handle nullptr results | |||
672 | // from here. | |||
673 | // Return a NTTP as if there was an error in a scope specifier, the user | |||
674 | // probably meant to write the type of a NTTP. | |||
675 | DeclSpec DS(getAttrFactory()); | |||
676 | DS.SetTypeSpecError(); | |||
677 | Declarator D(DS, ParsedAttributesView::none(), | |||
678 | DeclaratorContext::TemplateParam); | |||
679 | D.SetIdentifier(nullptr, Tok.getLocation()); | |||
680 | D.setInvalidType(true); | |||
681 | NamedDecl *ErrorParam = Actions.ActOnNonTypeTemplateParameter( | |||
682 | getCurScope(), D, Depth, Position, /*EqualLoc=*/SourceLocation(), | |||
683 | /*DefaultArg=*/nullptr); | |||
684 | ErrorParam->setInvalidDecl(true); | |||
685 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, | |||
686 | StopAtSemi | StopBeforeMatch); | |||
687 | return ErrorParam; | |||
688 | } | |||
689 | ||||
690 | case TPResult::Ambiguous: | |||
691 | llvm_unreachable("template param classification can't be ambiguous")::llvm::llvm_unreachable_internal("template param classification can't be ambiguous" , "clang/lib/Parse/ParseTemplate.cpp", 691); | |||
692 | } | |||
693 | ||||
694 | if (Tok.is(tok::kw_template)) | |||
695 | return ParseTemplateTemplateParameter(Depth, Position); | |||
696 | ||||
697 | // If it's none of the above, then it must be a parameter declaration. | |||
698 | // NOTE: This will pick up errors in the closure of the template parameter | |||
699 | // list (e.g., template < ; Check here to implement >> style closures. | |||
700 | return ParseNonTypeTemplateParameter(Depth, Position); | |||
701 | } | |||
702 | ||||
703 | /// Check whether the current token is a template-id annotation denoting a | |||
704 | /// type-constraint. | |||
705 | bool Parser::isTypeConstraintAnnotation() { | |||
706 | const Token &T = Tok.is(tok::annot_cxxscope) ? NextToken() : Tok; | |||
707 | if (T.isNot(tok::annot_template_id)) | |||
708 | return false; | |||
709 | const auto *ExistingAnnot = | |||
710 | static_cast<TemplateIdAnnotation *>(T.getAnnotationValue()); | |||
711 | return ExistingAnnot->Kind == TNK_Concept_template; | |||
712 | } | |||
713 | ||||
714 | /// Try parsing a type-constraint at the current location. | |||
715 | /// | |||
716 | /// type-constraint: | |||
717 | /// nested-name-specifier[opt] concept-name | |||
718 | /// nested-name-specifier[opt] concept-name | |||
719 | /// '<' template-argument-list[opt] '>'[opt] | |||
720 | /// | |||
721 | /// \returns true if an error occurred, and false otherwise. | |||
722 | bool Parser::TryAnnotateTypeConstraint() { | |||
723 | if (!getLangOpts().CPlusPlus20) | |||
724 | return false; | |||
725 | CXXScopeSpec SS; | |||
726 | bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); | |||
727 | if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, | |||
728 | /*ObjectHasErrors=*/false, | |||
729 | /*EnteringContext=*/false, | |||
730 | /*MayBePseudoDestructor=*/nullptr, | |||
731 | // If this is not a type-constraint, then | |||
732 | // this scope-spec is part of the typename | |||
733 | // of a non-type template parameter | |||
734 | /*IsTypename=*/true, /*LastII=*/nullptr, | |||
735 | // We won't find concepts in | |||
736 | // non-namespaces anyway, so might as well | |||
737 | // parse this correctly for possible type | |||
738 | // names. | |||
739 | /*OnlyNamespace=*/false)) | |||
740 | return true; | |||
741 | ||||
742 | if (Tok.is(tok::identifier)) { | |||
743 | UnqualifiedId PossibleConceptName; | |||
744 | PossibleConceptName.setIdentifier(Tok.getIdentifierInfo(), | |||
745 | Tok.getLocation()); | |||
746 | ||||
747 | TemplateTy PossibleConcept; | |||
748 | bool MemberOfUnknownSpecialization = false; | |||
749 | auto TNK = Actions.isTemplateName(getCurScope(), SS, | |||
750 | /*hasTemplateKeyword=*/false, | |||
751 | PossibleConceptName, | |||
752 | /*ObjectType=*/ParsedType(), | |||
753 | /*EnteringContext=*/false, | |||
754 | PossibleConcept, | |||
755 | MemberOfUnknownSpecialization, | |||
756 | /*Disambiguation=*/true); | |||
757 | if (MemberOfUnknownSpecialization || !PossibleConcept || | |||
758 | TNK != TNK_Concept_template) { | |||
759 | if (SS.isNotEmpty()) | |||
760 | AnnotateScopeToken(SS, !WasScopeAnnotation); | |||
761 | return false; | |||
762 | } | |||
763 | ||||
764 | // At this point we're sure we're dealing with a constrained parameter. It | |||
765 | // may or may not have a template parameter list following the concept | |||
766 | // name. | |||
767 | if (AnnotateTemplateIdToken(PossibleConcept, TNK, SS, | |||
768 | /*TemplateKWLoc=*/SourceLocation(), | |||
769 | PossibleConceptName, | |||
770 | /*AllowTypeAnnotation=*/false, | |||
771 | /*TypeConstraint=*/true)) | |||
772 | return true; | |||
773 | } | |||
774 | ||||
775 | if (SS.isNotEmpty()) | |||
776 | AnnotateScopeToken(SS, !WasScopeAnnotation); | |||
777 | return false; | |||
778 | } | |||
779 | ||||
780 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). | |||
781 | /// Other kinds of template parameters are parsed in | |||
782 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. | |||
783 | /// | |||
784 | /// type-parameter: [C++ temp.param] | |||
785 | /// 'class' ...[opt][C++0x] identifier[opt] | |||
786 | /// 'class' identifier[opt] '=' type-id | |||
787 | /// 'typename' ...[opt][C++0x] identifier[opt] | |||
788 | /// 'typename' identifier[opt] '=' type-id | |||
789 | NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { | |||
790 | assert((Tok.isOneOf(tok::kw_class, tok::kw_typename) ||(static_cast <bool> ((Tok.isOneOf(tok::kw_class, tok::kw_typename ) || isTypeConstraintAnnotation()) && "A type-parameter starts with 'class', 'typename' or a " "type-constraint") ? void (0) : __assert_fail ("(Tok.isOneOf(tok::kw_class, tok::kw_typename) || isTypeConstraintAnnotation()) && \"A type-parameter starts with 'class', 'typename' or a \" \"type-constraint\"" , "clang/lib/Parse/ParseTemplate.cpp", 793, __extension__ __PRETTY_FUNCTION__ )) | |||
791 | isTypeConstraintAnnotation()) &&(static_cast <bool> ((Tok.isOneOf(tok::kw_class, tok::kw_typename ) || isTypeConstraintAnnotation()) && "A type-parameter starts with 'class', 'typename' or a " "type-constraint") ? void (0) : __assert_fail ("(Tok.isOneOf(tok::kw_class, tok::kw_typename) || isTypeConstraintAnnotation()) && \"A type-parameter starts with 'class', 'typename' or a \" \"type-constraint\"" , "clang/lib/Parse/ParseTemplate.cpp", 793, __extension__ __PRETTY_FUNCTION__ )) | |||
792 | "A type-parameter starts with 'class', 'typename' or a "(static_cast <bool> ((Tok.isOneOf(tok::kw_class, tok::kw_typename ) || isTypeConstraintAnnotation()) && "A type-parameter starts with 'class', 'typename' or a " "type-constraint") ? void (0) : __assert_fail ("(Tok.isOneOf(tok::kw_class, tok::kw_typename) || isTypeConstraintAnnotation()) && \"A type-parameter starts with 'class', 'typename' or a \" \"type-constraint\"" , "clang/lib/Parse/ParseTemplate.cpp", 793, __extension__ __PRETTY_FUNCTION__ )) | |||
793 | "type-constraint")(static_cast <bool> ((Tok.isOneOf(tok::kw_class, tok::kw_typename ) || isTypeConstraintAnnotation()) && "A type-parameter starts with 'class', 'typename' or a " "type-constraint") ? void (0) : __assert_fail ("(Tok.isOneOf(tok::kw_class, tok::kw_typename) || isTypeConstraintAnnotation()) && \"A type-parameter starts with 'class', 'typename' or a \" \"type-constraint\"" , "clang/lib/Parse/ParseTemplate.cpp", 793, __extension__ __PRETTY_FUNCTION__ )); | |||
794 | ||||
795 | CXXScopeSpec TypeConstraintSS; | |||
796 | TemplateIdAnnotation *TypeConstraint = nullptr; | |||
797 | bool TypenameKeyword = false; | |||
798 | SourceLocation KeyLoc; | |||
799 | ParseOptionalCXXScopeSpecifier(TypeConstraintSS, /*ObjectType=*/nullptr, | |||
800 | /*ObjectHasErrors=*/false, | |||
801 | /*EnteringContext*/ false); | |||
802 | if (Tok.is(tok::annot_template_id)) { | |||
803 | // Consume the 'type-constraint'. | |||
804 | TypeConstraint = | |||
805 | static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); | |||
806 | assert(TypeConstraint->Kind == TNK_Concept_template &&(static_cast <bool> (TypeConstraint->Kind == TNK_Concept_template && "stray non-concept template-id annotation") ? void (0) : __assert_fail ("TypeConstraint->Kind == TNK_Concept_template && \"stray non-concept template-id annotation\"" , "clang/lib/Parse/ParseTemplate.cpp", 807, __extension__ __PRETTY_FUNCTION__ )) | |||
807 | "stray non-concept template-id annotation")(static_cast <bool> (TypeConstraint->Kind == TNK_Concept_template && "stray non-concept template-id annotation") ? void (0) : __assert_fail ("TypeConstraint->Kind == TNK_Concept_template && \"stray non-concept template-id annotation\"" , "clang/lib/Parse/ParseTemplate.cpp", 807, __extension__ __PRETTY_FUNCTION__ )); | |||
808 | KeyLoc = ConsumeAnnotationToken(); | |||
809 | } else { | |||
810 | assert(TypeConstraintSS.isEmpty() &&(static_cast <bool> (TypeConstraintSS.isEmpty() && "expected type constraint after scope specifier") ? void (0) : __assert_fail ("TypeConstraintSS.isEmpty() && \"expected type constraint after scope specifier\"" , "clang/lib/Parse/ParseTemplate.cpp", 811, __extension__ __PRETTY_FUNCTION__ )) | |||
811 | "expected type constraint after scope specifier")(static_cast <bool> (TypeConstraintSS.isEmpty() && "expected type constraint after scope specifier") ? void (0) : __assert_fail ("TypeConstraintSS.isEmpty() && \"expected type constraint after scope specifier\"" , "clang/lib/Parse/ParseTemplate.cpp", 811, __extension__ __PRETTY_FUNCTION__ )); | |||
812 | ||||
813 | // Consume the 'class' or 'typename' keyword. | |||
814 | TypenameKeyword = Tok.is(tok::kw_typename); | |||
815 | KeyLoc = ConsumeToken(); | |||
816 | } | |||
817 | ||||
818 | // Grab the ellipsis (if given). | |||
819 | SourceLocation EllipsisLoc; | |||
820 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { | |||
821 | Diag(EllipsisLoc, | |||
822 | getLangOpts().CPlusPlus11 | |||
823 | ? diag::warn_cxx98_compat_variadic_templates | |||
824 | : diag::ext_variadic_templates); | |||
825 | } | |||
826 | ||||
827 | // Grab the template parameter name (if given) | |||
828 | SourceLocation NameLoc = Tok.getLocation(); | |||
829 | IdentifierInfo *ParamName = nullptr; | |||
830 | if (Tok.is(tok::identifier)) { | |||
831 | ParamName = Tok.getIdentifierInfo(); | |||
832 | ConsumeToken(); | |||
833 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, | |||
834 | tok::greatergreater)) { | |||
835 | // Unnamed template parameter. Don't have to do anything here, just | |||
836 | // don't consume this token. | |||
837 | } else { | |||
838 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; | |||
839 | return nullptr; | |||
840 | } | |||
841 | ||||
842 | // Recover from misplaced ellipsis. | |||
843 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); | |||
844 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) | |||
845 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); | |||
846 | ||||
847 | // Grab a default argument (if available). | |||
848 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before | |||
849 | // we introduce the type parameter into the local scope. | |||
850 | SourceLocation EqualLoc; | |||
851 | ParsedType DefaultArg; | |||
852 | if (TryConsumeToken(tok::equal, EqualLoc)) | |||
853 | DefaultArg = | |||
854 | ParseTypeName(/*Range=*/nullptr, DeclaratorContext::TemplateTypeArg) | |||
855 | .get(); | |||
856 | ||||
857 | NamedDecl *NewDecl = Actions.ActOnTypeParameter(getCurScope(), | |||
858 | TypenameKeyword, EllipsisLoc, | |||
859 | KeyLoc, ParamName, NameLoc, | |||
860 | Depth, Position, EqualLoc, | |||
861 | DefaultArg, | |||
862 | TypeConstraint != nullptr); | |||
863 | ||||
864 | if (TypeConstraint) { | |||
865 | Actions.ActOnTypeConstraint(TypeConstraintSS, TypeConstraint, | |||
866 | cast<TemplateTypeParmDecl>(NewDecl), | |||
867 | EllipsisLoc); | |||
868 | } | |||
869 | ||||
870 | return NewDecl; | |||
871 | } | |||
872 | ||||
873 | /// ParseTemplateTemplateParameter - Handle the parsing of template | |||
874 | /// template parameters. | |||
875 | /// | |||
876 | /// type-parameter: [C++ temp.param] | |||
877 | /// template-head type-parameter-key ...[opt] identifier[opt] | |||
878 | /// template-head type-parameter-key identifier[opt] = id-expression | |||
879 | /// type-parameter-key: | |||
880 | /// 'class' | |||
881 | /// 'typename' [C++1z] | |||
882 | /// template-head: [C++2a] | |||
883 | /// 'template' '<' template-parameter-list '>' | |||
884 | /// requires-clause[opt] | |||
885 | NamedDecl *Parser::ParseTemplateTemplateParameter(unsigned Depth, | |||
886 | unsigned Position) { | |||
887 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword")(static_cast <bool> (Tok.is(tok::kw_template) && "Expected 'template' keyword") ? void (0) : __assert_fail ("Tok.is(tok::kw_template) && \"Expected 'template' keyword\"" , "clang/lib/Parse/ParseTemplate.cpp", 887, __extension__ __PRETTY_FUNCTION__ )); | |||
888 | ||||
889 | // Handle the template <...> part. | |||
890 | SourceLocation TemplateLoc = ConsumeToken(); | |||
891 | SmallVector<NamedDecl*,8> TemplateParams; | |||
892 | SourceLocation LAngleLoc, RAngleLoc; | |||
893 | ExprResult OptionalRequiresClauseConstraintER; | |||
894 | { | |||
895 | MultiParseScope TemplateParmScope(*this); | |||
896 | if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams, | |||
897 | LAngleLoc, RAngleLoc)) { | |||
898 | return nullptr; | |||
899 | } | |||
900 | if (TryConsumeToken(tok::kw_requires)) { | |||
901 | OptionalRequiresClauseConstraintER = | |||
902 | Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression( | |||
903 | /*IsTrailingRequiresClause=*/false)); | |||
904 | if (!OptionalRequiresClauseConstraintER.isUsable()) { | |||
905 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, | |||
906 | StopAtSemi | StopBeforeMatch); | |||
907 | return nullptr; | |||
908 | } | |||
909 | } | |||
910 | } | |||
911 | ||||
912 | // Provide an ExtWarn if the C++1z feature of using 'typename' here is used. | |||
913 | // Generate a meaningful error if the user forgot to put class before the | |||
914 | // identifier, comma, or greater. Provide a fixit if the identifier, comma, | |||
915 | // or greater appear immediately or after 'struct'. In the latter case, | |||
916 | // replace the keyword with 'class'. | |||
917 | if (!TryConsumeToken(tok::kw_class)) { | |||
918 | bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct); | |||
919 | const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok; | |||
920 | if (Tok.is(tok::kw_typename)) { | |||
921 | Diag(Tok.getLocation(), | |||
922 | getLangOpts().CPlusPlus17 | |||
923 | ? diag::warn_cxx14_compat_template_template_param_typename | |||
924 | : diag::ext_template_template_param_typename) | |||
925 | << (!getLangOpts().CPlusPlus17 | |||
926 | ? FixItHint::CreateReplacement(Tok.getLocation(), "class") | |||
927 | : FixItHint()); | |||
928 | } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater, | |||
929 | tok::greatergreater, tok::ellipsis)) { | |||
930 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param) | |||
931 | << getLangOpts().CPlusPlus17 | |||
932 | << (Replace | |||
933 | ? FixItHint::CreateReplacement(Tok.getLocation(), "class") | |||
934 | : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); | |||
935 | } else | |||
936 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param) | |||
937 | << getLangOpts().CPlusPlus17; | |||
938 | ||||
939 | if (Replace) | |||
940 | ConsumeToken(); | |||
941 | } | |||
942 | ||||
943 | // Parse the ellipsis, if given. | |||
944 | SourceLocation EllipsisLoc; | |||
945 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) | |||
946 | Diag(EllipsisLoc, | |||
947 | getLangOpts().CPlusPlus11 | |||
948 | ? diag::warn_cxx98_compat_variadic_templates | |||
949 | : diag::ext_variadic_templates); | |||
950 | ||||
951 | // Get the identifier, if given. | |||
952 | SourceLocation NameLoc = Tok.getLocation(); | |||
953 | IdentifierInfo *ParamName = nullptr; | |||
954 | if (Tok.is(tok::identifier)) { | |||
955 | ParamName = Tok.getIdentifierInfo(); | |||
956 | ConsumeToken(); | |||
957 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, | |||
958 | tok::greatergreater)) { | |||
959 | // Unnamed template parameter. Don't have to do anything here, just | |||
960 | // don't consume this token. | |||
961 | } else { | |||
962 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; | |||
963 | return nullptr; | |||
964 | } | |||
965 | ||||
966 | // Recover from misplaced ellipsis. | |||
967 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); | |||
968 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) | |||
969 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); | |||
970 | ||||
971 | TemplateParameterList *ParamList = Actions.ActOnTemplateParameterList( | |||
972 | Depth, SourceLocation(), TemplateLoc, LAngleLoc, TemplateParams, | |||
973 | RAngleLoc, OptionalRequiresClauseConstraintER.get()); | |||
974 | ||||
975 | // Grab a default argument (if available). | |||
976 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before | |||
977 | // we introduce the template parameter into the local scope. | |||
978 | SourceLocation EqualLoc; | |||
979 | ParsedTemplateArgument DefaultArg; | |||
980 | if (TryConsumeToken(tok::equal, EqualLoc)) { | |||
981 | DefaultArg = ParseTemplateTemplateArgument(); | |||
982 | if (DefaultArg.isInvalid()) { | |||
983 | Diag(Tok.getLocation(), | |||
984 | diag::err_default_template_template_parameter_not_template); | |||
985 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, | |||
986 | StopAtSemi | StopBeforeMatch); | |||
987 | } | |||
988 | } | |||
989 | ||||
990 | return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, | |||
991 | ParamList, EllipsisLoc, | |||
992 | ParamName, NameLoc, Depth, | |||
993 | Position, EqualLoc, DefaultArg); | |||
994 | } | |||
995 | ||||
996 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type | |||
997 | /// template parameters (e.g., in "template<int Size> class array;"). | |||
998 | /// | |||
999 | /// template-parameter: | |||
1000 | /// ... | |||
1001 | /// parameter-declaration | |||
1002 | NamedDecl * | |||
1003 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { | |||
1004 | // Parse the declaration-specifiers (i.e., the type). | |||
1005 | // FIXME: The type should probably be restricted in some way... Not all | |||
1006 | // declarators (parts of declarators?) are accepted for parameters. | |||
1007 | DeclSpec DS(AttrFactory); | |||
1008 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, | |||
1009 | DeclSpecContext::DSC_template_param); | |||
1010 | ||||
1011 | // Parse this as a typename. | |||
1012 | Declarator ParamDecl(DS, ParsedAttributesView::none(), | |||
1013 | DeclaratorContext::TemplateParam); | |||
1014 | ParseDeclarator(ParamDecl); | |||
1015 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { | |||
1016 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); | |||
1017 | return nullptr; | |||
1018 | } | |||
1019 | ||||
1020 | // Recover from misplaced ellipsis. | |||
1021 | SourceLocation EllipsisLoc; | |||
1022 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) | |||
1023 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl); | |||
1024 | ||||
1025 | // If there is a default value, parse it. | |||
1026 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before | |||
1027 | // we introduce the template parameter into the local scope. | |||
1028 | SourceLocation EqualLoc; | |||
1029 | ExprResult DefaultArg; | |||
1030 | if (TryConsumeToken(tok::equal, EqualLoc)) { | |||
1031 | if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) { | |||
1032 | Diag(Tok.getLocation(), diag::err_stmt_expr_in_default_arg) << 1; | |||
1033 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); | |||
1034 | } else { | |||
1035 | // C++ [temp.param]p15: | |||
1036 | // When parsing a default template-argument for a non-type | |||
1037 | // template-parameter, the first non-nested > is taken as the | |||
1038 | // end of the template-parameter-list rather than a greater-than | |||
1039 | // operator. | |||
1040 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); | |||
1041 | EnterExpressionEvaluationContext ConstantEvaluated( | |||
1042 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); | |||
1043 | DefaultArg = | |||
1044 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); | |||
1045 | if (DefaultArg.isInvalid()) | |||
1046 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); | |||
1047 | } | |||
1048 | } | |||
1049 | ||||
1050 | // Create the parameter. | |||
1051 | return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, | |||
1052 | Depth, Position, EqualLoc, | |||
1053 | DefaultArg.get()); | |||
1054 | } | |||
1055 | ||||
1056 | void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc, | |||
1057 | SourceLocation CorrectLoc, | |||
1058 | bool AlreadyHasEllipsis, | |||
1059 | bool IdentifierHasName) { | |||
1060 | FixItHint Insertion; | |||
1061 | if (!AlreadyHasEllipsis) | |||
1062 | Insertion = FixItHint::CreateInsertion(CorrectLoc, "..."); | |||
1063 | Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) | |||
1064 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion | |||
1065 | << !IdentifierHasName; | |||
1066 | } | |||
1067 | ||||
1068 | void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc, | |||
1069 | Declarator &D) { | |||
1070 | assert(EllipsisLoc.isValid())(static_cast <bool> (EllipsisLoc.isValid()) ? void (0) : __assert_fail ("EllipsisLoc.isValid()", "clang/lib/Parse/ParseTemplate.cpp" , 1070, __extension__ __PRETTY_FUNCTION__)); | |||
1071 | bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid(); | |||
1072 | if (!AlreadyHasEllipsis) | |||
1073 | D.setEllipsisLoc(EllipsisLoc); | |||
1074 | DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(), | |||
1075 | AlreadyHasEllipsis, D.hasName()); | |||
1076 | } | |||
1077 | ||||
1078 | /// Parses a '>' at the end of a template list. | |||
1079 | /// | |||
1080 | /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries | |||
1081 | /// to determine if these tokens were supposed to be a '>' followed by | |||
1082 | /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. | |||
1083 | /// | |||
1084 | /// \param RAngleLoc the location of the consumed '>'. | |||
1085 | /// | |||
1086 | /// \param ConsumeLastToken if true, the '>' is consumed. | |||
1087 | /// | |||
1088 | /// \param ObjCGenericList if true, this is the '>' closing an Objective-C | |||
1089 | /// type parameter or type argument list, rather than a C++ template parameter | |||
1090 | /// or argument list. | |||
1091 | /// | |||
1092 | /// \returns true, if current token does not start with '>', false otherwise. | |||
1093 | bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc, | |||
1094 | SourceLocation &RAngleLoc, | |||
1095 | bool ConsumeLastToken, | |||
1096 | bool ObjCGenericList) { | |||
1097 | // What will be left once we've consumed the '>'. | |||
1098 | tok::TokenKind RemainingToken; | |||
1099 | const char *ReplacementStr = "> >"; | |||
1100 | bool MergeWithNextToken = false; | |||
1101 | ||||
1102 | switch (Tok.getKind()) { | |||
1103 | default: | |||
1104 | Diag(getEndOfPreviousToken(), diag::err_expected) << tok::greater; | |||
1105 | Diag(LAngleLoc, diag::note_matching) << tok::less; | |||
1106 | return true; | |||
1107 | ||||
1108 | case tok::greater: | |||
1109 | // Determine the location of the '>' token. Only consume this token | |||
1110 | // if the caller asked us to. | |||
1111 | RAngleLoc = Tok.getLocation(); | |||
1112 | if (ConsumeLastToken) | |||
1113 | ConsumeToken(); | |||
1114 | return false; | |||
1115 | ||||
1116 | case tok::greatergreater: | |||
1117 | RemainingToken = tok::greater; | |||
1118 | break; | |||
1119 | ||||
1120 | case tok::greatergreatergreater: | |||
1121 | RemainingToken = tok::greatergreater; | |||
1122 | break; | |||
1123 | ||||
1124 | case tok::greaterequal: | |||
1125 | RemainingToken = tok::equal; | |||
1126 | ReplacementStr = "> ="; | |||
1127 | ||||
1128 | // Join two adjacent '=' tokens into one, for cases like: | |||
1129 | // void (*p)() = f<int>; | |||
1130 | // return f<int>==p; | |||
1131 | if (NextToken().is(tok::equal) && | |||
1132 | areTokensAdjacent(Tok, NextToken())) { | |||
1133 | RemainingToken = tok::equalequal; | |||
1134 | MergeWithNextToken = true; | |||
1135 | } | |||
1136 | break; | |||
1137 | ||||
1138 | case tok::greatergreaterequal: | |||
1139 | RemainingToken = tok::greaterequal; | |||
1140 | break; | |||
1141 | } | |||
1142 | ||||
1143 | // This template-id is terminated by a token that starts with a '>'. | |||
1144 | // Outside C++11 and Objective-C, this is now error recovery. | |||
1145 | // | |||
1146 | // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we | |||
1147 | // extend that treatment to also apply to the '>>>' token. | |||
1148 | // | |||
1149 | // Objective-C allows this in its type parameter / argument lists. | |||
1150 | ||||
1151 | SourceLocation TokBeforeGreaterLoc = PrevTokLocation; | |||
1152 | SourceLocation TokLoc = Tok.getLocation(); | |||
1153 | Token Next = NextToken(); | |||
1154 | ||||
1155 | // Whether splitting the current token after the '>' would undesirably result | |||
1156 | // in the remaining token pasting with the token after it. This excludes the | |||
1157 | // MergeWithNextToken cases, which we've already handled. | |||
1158 | bool PreventMergeWithNextToken = | |||
1159 | (RemainingToken == tok::greater || | |||
1160 | RemainingToken == tok::greatergreater) && | |||
1161 | (Next.isOneOf(tok::greater, tok::greatergreater, | |||
1162 | tok::greatergreatergreater, tok::equal, tok::greaterequal, | |||
1163 | tok::greatergreaterequal, tok::equalequal)) && | |||
1164 | areTokensAdjacent(Tok, Next); | |||
1165 | ||||
1166 | // Diagnose this situation as appropriate. | |||
1167 | if (!ObjCGenericList) { | |||
1168 | // The source range of the replaced token(s). | |||
1169 | CharSourceRange ReplacementRange = CharSourceRange::getCharRange( | |||
1170 | TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(), | |||
1171 | getLangOpts())); | |||
1172 | ||||
1173 | // A hint to put a space between the '>>'s. In order to make the hint as | |||
1174 | // clear as possible, we include the characters either side of the space in | |||
1175 | // the replacement, rather than just inserting a space at SecondCharLoc. | |||
1176 | FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, | |||
1177 | ReplacementStr); | |||
1178 | ||||
1179 | // A hint to put another space after the token, if it would otherwise be | |||
1180 | // lexed differently. | |||
1181 | FixItHint Hint2; | |||
1182 | if (PreventMergeWithNextToken) | |||
1183 | Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); | |||
1184 | ||||
1185 | unsigned DiagId = diag::err_two_right_angle_brackets_need_space; | |||
1186 | if (getLangOpts().CPlusPlus11 && | |||
1187 | (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater))) | |||
1188 | DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; | |||
1189 | else if (Tok.is(tok::greaterequal)) | |||
1190 | DiagId = diag::err_right_angle_bracket_equal_needs_space; | |||
1191 | Diag(TokLoc, DiagId) << Hint1 << Hint2; | |||
1192 | } | |||
1193 | ||||
1194 | // Find the "length" of the resulting '>' token. This is not always 1, as it | |||
1195 | // can contain escaped newlines. | |||
1196 | unsigned GreaterLength = Lexer::getTokenPrefixLength( | |||
1197 | TokLoc, 1, PP.getSourceManager(), getLangOpts()); | |||
1198 | ||||
1199 | // Annotate the source buffer to indicate that we split the token after the | |||
1200 | // '>'. This allows us to properly find the end of, and extract the spelling | |||
1201 | // of, the '>' token later. | |||
1202 | RAngleLoc = PP.SplitToken(TokLoc, GreaterLength); | |||
1203 | ||||
1204 | // Strip the initial '>' from the token. | |||
1205 | bool CachingTokens = PP.IsPreviousCachedToken(Tok); | |||
1206 | ||||
1207 | Token Greater = Tok; | |||
1208 | Greater.setLocation(RAngleLoc); | |||
1209 | Greater.setKind(tok::greater); | |||
1210 | Greater.setLength(GreaterLength); | |||
1211 | ||||
1212 | unsigned OldLength = Tok.getLength(); | |||
1213 | if (MergeWithNextToken) { | |||
1214 | ConsumeToken(); | |||
1215 | OldLength += Tok.getLength(); | |||
1216 | } | |||
1217 | ||||
1218 | Tok.setKind(RemainingToken); | |||
1219 | Tok.setLength(OldLength - GreaterLength); | |||
1220 | ||||
1221 | // Split the second token if lexing it normally would lex a different token | |||
1222 | // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>'). | |||
1223 | SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength); | |||
1224 | if (PreventMergeWithNextToken) | |||
1225 | AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength()); | |||
1226 | Tok.setLocation(AfterGreaterLoc); | |||
1227 | ||||
1228 | // Update the token cache to match what we just did if necessary. | |||
1229 | if (CachingTokens) { | |||
1230 | // If the previous cached token is being merged, delete it. | |||
1231 | if (MergeWithNextToken) | |||
1232 | PP.ReplacePreviousCachedToken({}); | |||
1233 | ||||
1234 | if (ConsumeLastToken) | |||
1235 | PP.ReplacePreviousCachedToken({Greater, Tok}); | |||
1236 | else | |||
1237 | PP.ReplacePreviousCachedToken({Greater}); | |||
1238 | } | |||
1239 | ||||
1240 | if (ConsumeLastToken) { | |||
1241 | PrevTokLocation = RAngleLoc; | |||
1242 | } else { | |||
1243 | PrevTokLocation = TokBeforeGreaterLoc; | |||
1244 | PP.EnterToken(Tok, /*IsReinject=*/true); | |||
1245 | Tok = Greater; | |||
1246 | } | |||
1247 | ||||
1248 | return false; | |||
1249 | } | |||
1250 | ||||
1251 | /// Parses a template-id that after the template name has | |||
1252 | /// already been parsed. | |||
1253 | /// | |||
1254 | /// This routine takes care of parsing the enclosed template argument | |||
1255 | /// list ('<' template-parameter-list [opt] '>') and placing the | |||
1256 | /// results into a form that can be transferred to semantic analysis. | |||
1257 | /// | |||
1258 | /// \param ConsumeLastToken if true, then we will consume the last | |||
1259 | /// token that forms the template-id. Otherwise, we will leave the | |||
1260 | /// last token in the stream (e.g., so that it can be replaced with an | |||
1261 | /// annotation token). | |||
1262 | bool Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken, | |||
1263 | SourceLocation &LAngleLoc, | |||
1264 | TemplateArgList &TemplateArgs, | |||
1265 | SourceLocation &RAngleLoc, | |||
1266 | TemplateTy Template) { | |||
1267 | assert(Tok.is(tok::less) && "Must have already parsed the template-name")(static_cast <bool> (Tok.is(tok::less) && "Must have already parsed the template-name" ) ? void (0) : __assert_fail ("Tok.is(tok::less) && \"Must have already parsed the template-name\"" , "clang/lib/Parse/ParseTemplate.cpp", 1267, __extension__ __PRETTY_FUNCTION__ )); | |||
1268 | ||||
1269 | // Consume the '<'. | |||
1270 | LAngleLoc = ConsumeToken(); | |||
1271 | ||||
1272 | // Parse the optional template-argument-list. | |||
1273 | bool Invalid = false; | |||
1274 | { | |||
1275 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); | |||
1276 | if (!Tok.isOneOf(tok::greater, tok::greatergreater, | |||
1277 | tok::greatergreatergreater, tok::greaterequal, | |||
1278 | tok::greatergreaterequal)) | |||
1279 | Invalid = ParseTemplateArgumentList(TemplateArgs, Template, LAngleLoc); | |||
1280 | ||||
1281 | if (Invalid) { | |||
1282 | // Try to find the closing '>'. | |||
1283 | if (getLangOpts().CPlusPlus11) | |||
1284 | SkipUntil(tok::greater, tok::greatergreater, | |||
1285 | tok::greatergreatergreater, StopAtSemi | StopBeforeMatch); | |||
1286 | else | |||
1287 | SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); | |||
1288 | } | |||
1289 | } | |||
1290 | ||||
1291 | return ParseGreaterThanInTemplateList(LAngleLoc, RAngleLoc, ConsumeLastToken, | |||
1292 | /*ObjCGenericList=*/false) || | |||
1293 | Invalid; | |||
1294 | } | |||
1295 | ||||
1296 | /// Replace the tokens that form a simple-template-id with an | |||
1297 | /// annotation token containing the complete template-id. | |||
1298 | /// | |||
1299 | /// The first token in the stream must be the name of a template that | |||
1300 | /// is followed by a '<'. This routine will parse the complete | |||
1301 | /// simple-template-id and replace the tokens with a single annotation | |||
1302 | /// token with one of two different kinds: if the template-id names a | |||
1303 | /// type (and \p AllowTypeAnnotation is true), the annotation token is | |||
1304 | /// a type annotation that includes the optional nested-name-specifier | |||
1305 | /// (\p SS). Otherwise, the annotation token is a template-id | |||
1306 | /// annotation that does not include the optional | |||
1307 | /// nested-name-specifier. | |||
1308 | /// | |||
1309 | /// \param Template the declaration of the template named by the first | |||
1310 | /// token (an identifier), as returned from \c Action::isTemplateName(). | |||
1311 | /// | |||
1312 | /// \param TNK the kind of template that \p Template | |||
1313 | /// refers to, as returned from \c Action::isTemplateName(). | |||
1314 | /// | |||
1315 | /// \param SS if non-NULL, the nested-name-specifier that precedes | |||
1316 | /// this template name. | |||
1317 | /// | |||
1318 | /// \param TemplateKWLoc if valid, specifies that this template-id | |||
1319 | /// annotation was preceded by the 'template' keyword and gives the | |||
1320 | /// location of that keyword. If invalid (the default), then this | |||
1321 | /// template-id was not preceded by a 'template' keyword. | |||
1322 | /// | |||
1323 | /// \param AllowTypeAnnotation if true (the default), then a | |||
1324 | /// simple-template-id that refers to a class template, template | |||
1325 | /// template parameter, or other template that produces a type will be | |||
1326 | /// replaced with a type annotation token. Otherwise, the | |||
1327 | /// simple-template-id is always replaced with a template-id | |||
1328 | /// annotation token. | |||
1329 | /// | |||
1330 | /// \param TypeConstraint if true, then this is actually a type-constraint, | |||
1331 | /// meaning that the template argument list can be omitted (and the template in | |||
1332 | /// question must be a concept). | |||
1333 | /// | |||
1334 | /// If an unrecoverable parse error occurs and no annotation token can be | |||
1335 | /// formed, this function returns true. | |||
1336 | /// | |||
1337 | bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, | |||
1338 | CXXScopeSpec &SS, | |||
1339 | SourceLocation TemplateKWLoc, | |||
1340 | UnqualifiedId &TemplateName, | |||
1341 | bool AllowTypeAnnotation, | |||
1342 | bool TypeConstraint) { | |||
1343 | assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++")(static_cast <bool> (getLangOpts().CPlusPlus && "Can only annotate template-ids in C++") ? void (0) : __assert_fail ("getLangOpts().CPlusPlus && \"Can only annotate template-ids in C++\"" , "clang/lib/Parse/ParseTemplate.cpp", 1343, __extension__ __PRETTY_FUNCTION__ )); | |||
1344 | assert((Tok.is(tok::less) || TypeConstraint) &&(static_cast <bool> ((Tok.is(tok::less) || TypeConstraint ) && "Parser isn't at the beginning of a template-id" ) ? void (0) : __assert_fail ("(Tok.is(tok::less) || TypeConstraint) && \"Parser isn't at the beginning of a template-id\"" , "clang/lib/Parse/ParseTemplate.cpp", 1345, __extension__ __PRETTY_FUNCTION__ )) | |||
1345 | "Parser isn't at the beginning of a template-id")(static_cast <bool> ((Tok.is(tok::less) || TypeConstraint ) && "Parser isn't at the beginning of a template-id" ) ? void (0) : __assert_fail ("(Tok.is(tok::less) || TypeConstraint) && \"Parser isn't at the beginning of a template-id\"" , "clang/lib/Parse/ParseTemplate.cpp", 1345, __extension__ __PRETTY_FUNCTION__ )); | |||
1346 | assert(!(TypeConstraint && AllowTypeAnnotation) && "type-constraint can't be "(static_cast <bool> (!(TypeConstraint && AllowTypeAnnotation ) && "type-constraint can't be " "a type annotation") ? void (0) : __assert_fail ("!(TypeConstraint && AllowTypeAnnotation) && \"type-constraint can't be \" \"a type annotation\"" , "clang/lib/Parse/ParseTemplate.cpp", 1347, __extension__ __PRETTY_FUNCTION__ )) | |||
1347 | "a type annotation")(static_cast <bool> (!(TypeConstraint && AllowTypeAnnotation ) && "type-constraint can't be " "a type annotation") ? void (0) : __assert_fail ("!(TypeConstraint && AllowTypeAnnotation) && \"type-constraint can't be \" \"a type annotation\"" , "clang/lib/Parse/ParseTemplate.cpp", 1347, __extension__ __PRETTY_FUNCTION__ )); | |||
1348 | assert((!TypeConstraint || TNK == TNK_Concept_template) && "type-constraint "(static_cast <bool> ((!TypeConstraint || TNK == TNK_Concept_template ) && "type-constraint " "must accompany a concept name" ) ? void (0) : __assert_fail ("(!TypeConstraint || TNK == TNK_Concept_template) && \"type-constraint \" \"must accompany a concept name\"" , "clang/lib/Parse/ParseTemplate.cpp", 1349, __extension__ __PRETTY_FUNCTION__ )) | |||
1349 | "must accompany a concept name")(static_cast <bool> ((!TypeConstraint || TNK == TNK_Concept_template ) && "type-constraint " "must accompany a concept name" ) ? void (0) : __assert_fail ("(!TypeConstraint || TNK == TNK_Concept_template) && \"type-constraint \" \"must accompany a concept name\"" , "clang/lib/Parse/ParseTemplate.cpp", 1349, __extension__ __PRETTY_FUNCTION__ )); | |||
1350 | assert((Template || TNK == TNK_Non_template) && "missing template name")(static_cast <bool> ((Template || TNK == TNK_Non_template ) && "missing template name") ? void (0) : __assert_fail ("(Template || TNK == TNK_Non_template) && \"missing template name\"" , "clang/lib/Parse/ParseTemplate.cpp", 1350, __extension__ __PRETTY_FUNCTION__ )); | |||
1351 | ||||
1352 | // Consume the template-name. | |||
1353 | SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); | |||
1354 | ||||
1355 | // Parse the enclosed template argument list. | |||
1356 | SourceLocation LAngleLoc, RAngleLoc; | |||
1357 | TemplateArgList TemplateArgs; | |||
1358 | bool ArgsInvalid = false; | |||
1359 | if (!TypeConstraint || Tok.is(tok::less)) { | |||
1360 | ArgsInvalid = ParseTemplateIdAfterTemplateName( | |||
1361 | false, LAngleLoc, TemplateArgs, RAngleLoc, Template); | |||
1362 | // If we couldn't recover from invalid arguments, don't form an annotation | |||
1363 | // token -- we don't know how much to annotate. | |||
1364 | // FIXME: This can lead to duplicate diagnostics if we retry parsing this | |||
1365 | // template-id in another context. Try to annotate anyway? | |||
1366 | if (RAngleLoc.isInvalid()) | |||
1367 | return true; | |||
1368 | } | |||
1369 | ||||
1370 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); | |||
1371 | ||||
1372 | // Build the annotation token. | |||
1373 | if (TNK == TNK_Type_template && AllowTypeAnnotation) { | |||
1374 | TypeResult Type = ArgsInvalid | |||
1375 | ? TypeError() | |||
1376 | : Actions.ActOnTemplateIdType( | |||
1377 | getCurScope(), SS, TemplateKWLoc, Template, | |||
1378 | TemplateName.Identifier, TemplateNameLoc, | |||
1379 | LAngleLoc, TemplateArgsPtr, RAngleLoc); | |||
1380 | ||||
1381 | Tok.setKind(tok::annot_typename); | |||
1382 | setTypeAnnotation(Tok, Type); | |||
1383 | if (SS.isNotEmpty()) | |||
1384 | Tok.setLocation(SS.getBeginLoc()); | |||
1385 | else if (TemplateKWLoc.isValid()) | |||
1386 | Tok.setLocation(TemplateKWLoc); | |||
1387 | else | |||
1388 | Tok.setLocation(TemplateNameLoc); | |||
1389 | } else { | |||
1390 | // Build a template-id annotation token that can be processed | |||
1391 | // later. | |||
1392 | Tok.setKind(tok::annot_template_id); | |||
1393 | ||||
1394 | IdentifierInfo *TemplateII = | |||
1395 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier | |||
1396 | ? TemplateName.Identifier | |||
1397 | : nullptr; | |||
1398 | ||||
1399 | OverloadedOperatorKind OpKind = | |||
1400 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier | |||
1401 | ? OO_None | |||
1402 | : TemplateName.OperatorFunctionId.Operator; | |||
1403 | ||||
1404 | TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( | |||
1405 | TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK, | |||
1406 | LAngleLoc, RAngleLoc, TemplateArgs, ArgsInvalid, TemplateIds); | |||
1407 | ||||
1408 | Tok.setAnnotationValue(TemplateId); | |||
1409 | if (TemplateKWLoc.isValid()) | |||
1410 | Tok.setLocation(TemplateKWLoc); | |||
1411 | else | |||
1412 | Tok.setLocation(TemplateNameLoc); | |||
1413 | } | |||
1414 | ||||
1415 | // Common fields for the annotation token | |||
1416 | Tok.setAnnotationEndLoc(RAngleLoc); | |||
1417 | ||||
1418 | // In case the tokens were cached, have Preprocessor replace them with the | |||
1419 | // annotation token. | |||
1420 | PP.AnnotateCachedTokens(Tok); | |||
1421 | return false; | |||
1422 | } | |||
1423 | ||||
1424 | /// Replaces a template-id annotation token with a type | |||
1425 | /// annotation token. | |||
1426 | /// | |||
1427 | /// If there was a failure when forming the type from the template-id, | |||
1428 | /// a type annotation token will still be created, but will have a | |||
1429 | /// NULL type pointer to signify an error. | |||
1430 | /// | |||
1431 | /// \param SS The scope specifier appearing before the template-id, if any. | |||
1432 | /// | |||
1433 | /// \param AllowImplicitTypename whether this is a context where T::type | |||
1434 | /// denotes a dependent type. | |||
1435 | /// \param IsClassName Is this template-id appearing in a context where we | |||
1436 | /// know it names a class, such as in an elaborated-type-specifier or | |||
1437 | /// base-specifier? ('typename' and 'template' are unneeded and disallowed | |||
1438 | /// in those contexts.) | |||
1439 | void Parser::AnnotateTemplateIdTokenAsType( | |||
1440 | CXXScopeSpec &SS, ImplicitTypenameContext AllowImplicitTypename, | |||
1441 | bool IsClassName) { | |||
1442 | assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens")(static_cast <bool> (Tok.is(tok::annot_template_id) && "Requires template-id tokens") ? void (0) : __assert_fail ("Tok.is(tok::annot_template_id) && \"Requires template-id tokens\"" , "clang/lib/Parse/ParseTemplate.cpp", 1442, __extension__ __PRETTY_FUNCTION__ )); | |||
1443 | ||||
1444 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); | |||
1445 | assert(TemplateId->mightBeType() &&(static_cast <bool> (TemplateId->mightBeType() && "Only works for type and dependent templates") ? void (0) : __assert_fail ("TemplateId->mightBeType() && \"Only works for type and dependent templates\"" , "clang/lib/Parse/ParseTemplate.cpp", 1446, __extension__ __PRETTY_FUNCTION__ )) | |||
1446 | "Only works for type and dependent templates")(static_cast <bool> (TemplateId->mightBeType() && "Only works for type and dependent templates") ? void (0) : __assert_fail ("TemplateId->mightBeType() && \"Only works for type and dependent templates\"" , "clang/lib/Parse/ParseTemplate.cpp", 1446, __extension__ __PRETTY_FUNCTION__ )); | |||
1447 | ||||
1448 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), | |||
1449 | TemplateId->NumArgs); | |||
1450 | ||||
1451 | TypeResult Type = | |||
1452 | TemplateId->isInvalid() | |||
1453 | ? TypeError() | |||
1454 | : Actions.ActOnTemplateIdType( | |||
1455 | getCurScope(), SS, TemplateId->TemplateKWLoc, | |||
1456 | TemplateId->Template, TemplateId->Name, | |||
1457 | TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, | |||
1458 | TemplateArgsPtr, TemplateId->RAngleLoc, | |||
1459 | /*IsCtorOrDtorName=*/false, IsClassName, AllowImplicitTypename); | |||
1460 | // Create the new "type" annotation token. | |||
1461 | Tok.setKind(tok::annot_typename); | |||
1462 | setTypeAnnotation(Tok, Type); | |||
1463 | if (SS.isNotEmpty()) // it was a C++ qualified type name. | |||
1464 | Tok.setLocation(SS.getBeginLoc()); | |||
1465 | // End location stays the same | |||
1466 | ||||
1467 | // Replace the template-id annotation token, and possible the scope-specifier | |||
1468 | // that precedes it, with the typename annotation token. | |||
1469 | PP.AnnotateCachedTokens(Tok); | |||
1470 | } | |||
1471 | ||||
1472 | /// Determine whether the given token can end a template argument. | |||
1473 | static bool isEndOfTemplateArgument(Token Tok) { | |||
1474 | // FIXME: Handle '>>>'. | |||
1475 | return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater, | |||
1476 | tok::greatergreatergreater); | |||
1477 | } | |||
1478 | ||||
1479 | /// Parse a C++ template template argument. | |||
1480 | ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { | |||
1481 | if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && | |||
1482 | !Tok.is(tok::annot_cxxscope)) | |||
1483 | return ParsedTemplateArgument(); | |||
1484 | ||||
1485 | // C++0x [temp.arg.template]p1: | |||
1486 | // A template-argument for a template template-parameter shall be the name | |||
1487 | // of a class template or an alias template, expressed as id-expression. | |||
1488 | // | |||
1489 | // We parse an id-expression that refers to a class template or alias | |||
1490 | // template. The grammar we parse is: | |||
1491 | // | |||
1492 | // nested-name-specifier[opt] template[opt] identifier ...[opt] | |||
1493 | // | |||
1494 | // followed by a token that terminates a template argument, such as ',', | |||
1495 | // '>', or (in some cases) '>>'. | |||
1496 | CXXScopeSpec SS; // nested-name-specifier, if present | |||
1497 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, | |||
1498 | /*ObjectHasErrors=*/false, | |||
1499 | /*EnteringContext=*/false); | |||
1500 | ||||
1501 | ParsedTemplateArgument Result; | |||
1502 | SourceLocation EllipsisLoc; | |||
1503 | if (SS.isSet() && Tok.is(tok::kw_template)) { | |||
1504 | // Parse the optional 'template' keyword following the | |||
1505 | // nested-name-specifier. | |||
1506 | SourceLocation TemplateKWLoc = ConsumeToken(); | |||
1507 | ||||
1508 | if (Tok.is(tok::identifier)) { | |||
1509 | // We appear to have a dependent template name. | |||
1510 | UnqualifiedId Name; | |||
1511 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); | |||
1512 | ConsumeToken(); // the identifier | |||
1513 | ||||
1514 | TryConsumeToken(tok::ellipsis, EllipsisLoc); | |||
1515 | ||||
1516 | // If the next token signals the end of a template argument, then we have | |||
1517 | // a (possibly-dependent) template name that could be a template template | |||
1518 | // argument. | |||
1519 | TemplateTy Template; | |||
1520 | if (isEndOfTemplateArgument(Tok) && | |||
1521 | Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Name, | |||
1522 | /*ObjectType=*/nullptr, | |||
1523 | /*EnteringContext=*/false, Template)) | |||
1524 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); | |||
1525 | } | |||
1526 | } else if (Tok.is(tok::identifier)) { | |||
1527 | // We may have a (non-dependent) template name. | |||
1528 | TemplateTy Template; | |||
1529 | UnqualifiedId Name; | |||
1530 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); | |||
1531 | ConsumeToken(); // the identifier | |||
1532 | ||||
1533 | TryConsumeToken(tok::ellipsis, EllipsisLoc); | |||
1534 | ||||
1535 | if (isEndOfTemplateArgument(Tok)) { | |||
1536 | bool MemberOfUnknownSpecialization; | |||
1537 | TemplateNameKind TNK = Actions.isTemplateName( | |||
1538 | getCurScope(), SS, | |||
1539 | /*hasTemplateKeyword=*/false, Name, | |||
1540 | /*ObjectType=*/nullptr, | |||
1541 | /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization); | |||
1542 | if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { | |||
1543 | // We have an id-expression that refers to a class template or | |||
1544 | // (C++0x) alias template. | |||
1545 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); | |||
1546 | } | |||
1547 | } | |||
1548 | } | |||
1549 | ||||
1550 | // If this is a pack expansion, build it as such. | |||
1551 | if (EllipsisLoc.isValid() && !Result.isInvalid()) | |||
1552 | Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); | |||
1553 | ||||
1554 | return Result; | |||
1555 | } | |||
1556 | ||||
1557 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). | |||
1558 | /// | |||
1559 | /// template-argument: [C++ 14.2] | |||
1560 | /// constant-expression | |||
1561 | /// type-id | |||
1562 | /// id-expression | |||
1563 | ParsedTemplateArgument Parser::ParseTemplateArgument() { | |||
1564 | // C++ [temp.arg]p2: | |||
1565 | // In a template-argument, an ambiguity between a type-id and an | |||
1566 | // expression is resolved to a type-id, regardless of the form of | |||
1567 | // the corresponding template-parameter. | |||
1568 | // | |||
1569 | // Therefore, we initially try to parse a type-id - and isCXXTypeId might look | |||
1570 | // up and annotate an identifier as an id-expression during disambiguation, | |||
1571 | // so enter the appropriate context for a constant expression template | |||
1572 | // argument before trying to disambiguate. | |||
1573 | ||||
1574 | EnterExpressionEvaluationContext EnterConstantEvaluated( | |||
1575 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated, | |||
1576 | /*LambdaContextDecl=*/nullptr, | |||
1577 | /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument); | |||
1578 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { | |||
1579 | TypeResult TypeArg = ParseTypeName( | |||
1580 | /*Range=*/nullptr, DeclaratorContext::TemplateArg); | |||
1581 | return Actions.ActOnTemplateTypeArgument(TypeArg); | |||
1582 | } | |||
1583 | ||||
1584 | // Try to parse a template template argument. | |||
1585 | { | |||
1586 | TentativeParsingAction TPA(*this); | |||
1587 | ||||
1588 | ParsedTemplateArgument TemplateTemplateArgument | |||
1589 | = ParseTemplateTemplateArgument(); | |||
1590 | if (!TemplateTemplateArgument.isInvalid()) { | |||
1591 | TPA.Commit(); | |||
1592 | return TemplateTemplateArgument; | |||
1593 | } | |||
1594 | ||||
1595 | // Revert this tentative parse to parse a non-type template argument. | |||
1596 | TPA.Revert(); | |||
1597 | } | |||
1598 | ||||
1599 | // Parse a non-type template argument. | |||
1600 | SourceLocation Loc = Tok.getLocation(); | |||
1601 | ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast); | |||
1602 | if (ExprArg.isInvalid() || !ExprArg.get()) { | |||
1603 | return ParsedTemplateArgument(); | |||
1604 | } | |||
1605 | ||||
1606 | return ParsedTemplateArgument(ParsedTemplateArgument::NonType, | |||
1607 | ExprArg.get(), Loc); | |||
1608 | } | |||
1609 | ||||
1610 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list | |||
1611 | /// (C++ [temp.names]). Returns true if there was an error. | |||
1612 | /// | |||
1613 | /// template-argument-list: [C++ 14.2] | |||
1614 | /// template-argument | |||
1615 | /// template-argument-list ',' template-argument | |||
1616 | /// | |||
1617 | /// \param Template is only used for code completion, and may be null. | |||
1618 | bool Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, | |||
1619 | TemplateTy Template, | |||
1620 | SourceLocation OpenLoc) { | |||
1621 | ||||
1622 | ColonProtectionRAIIObject ColonProtection(*this, false); | |||
1623 | ||||
1624 | auto RunSignatureHelp = [&] { | |||
1625 | if (!Template) | |||
1626 | return QualType(); | |||
1627 | CalledSignatureHelp = true; | |||
1628 | return Actions.ProduceTemplateArgumentSignatureHelp(Template, TemplateArgs, | |||
1629 | OpenLoc); | |||
1630 | }; | |||
1631 | ||||
1632 | do { | |||
1633 | PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp); | |||
1634 | ParsedTemplateArgument Arg = ParseTemplateArgument(); | |||
1635 | SourceLocation EllipsisLoc; | |||
1636 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) | |||
1637 | Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); | |||
1638 | ||||
1639 | if (Arg.isInvalid()) { | |||
1640 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) | |||
1641 | RunSignatureHelp(); | |||
1642 | return true; | |||
1643 | } | |||
1644 | ||||
1645 | // Save this template argument. | |||
1646 | TemplateArgs.push_back(Arg); | |||
1647 | ||||
1648 | // If the next token is a comma, consume it and keep reading | |||
1649 | // arguments. | |||
1650 | } while (TryConsumeToken(tok::comma)); | |||
1651 | ||||
1652 | return false; | |||
1653 | } | |||
1654 | ||||
1655 | /// Parse a C++ explicit template instantiation | |||
1656 | /// (C++ [temp.explicit]). | |||
1657 | /// | |||
1658 | /// explicit-instantiation: | |||
1659 | /// 'extern' [opt] 'template' declaration | |||
1660 | /// | |||
1661 | /// Note that the 'extern' is a GNU extension and C++11 feature. | |||
1662 | Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context, | |||
1663 | SourceLocation ExternLoc, | |||
1664 | SourceLocation TemplateLoc, | |||
1665 | SourceLocation &DeclEnd, | |||
1666 | ParsedAttributes &AccessAttrs, | |||
1667 | AccessSpecifier AS) { | |||
1668 | // This isn't really required here. | |||
1669 | ParsingDeclRAIIObject | |||
1670 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); | |||
1671 | ||||
1672 | return ParseSingleDeclarationAfterTemplate( | |||
1673 | Context, ParsedTemplateInfo(ExternLoc, TemplateLoc), | |||
1674 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); | |||
1675 | } | |||
1676 | ||||
1677 | SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { | |||
1678 | if (TemplateParams) | |||
1679 | return getTemplateParamsRange(TemplateParams->data(), | |||
1680 | TemplateParams->size()); | |||
1681 | ||||
1682 | SourceRange R(TemplateLoc); | |||
1683 | if (ExternLoc.isValid()) | |||
1684 | R.setBegin(ExternLoc); | |||
1685 | return R; | |||
1686 | } | |||
1687 | ||||
1688 | void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { | |||
1689 | ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); | |||
| ||||
1690 | } | |||
1691 | ||||
1692 | /// Late parse a C++ function template in Microsoft mode. | |||
1693 | void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { | |||
1694 | if (!LPT.D) | |||
1695 | return; | |||
1696 | ||||
1697 | // Destroy TemplateIdAnnotations when we're done, if possible. | |||
1698 | DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); | |||
1699 | ||||
1700 | // Get the FunctionDecl. | |||
1701 | FunctionDecl *FunD = LPT.D->getAsFunction(); | |||
1702 | // Track template parameter depth. | |||
1703 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); | |||
1704 | ||||
1705 | // To restore the context after late parsing. | |||
1706 | Sema::ContextRAII GlobalSavedContext( | |||
1707 | Actions, Actions.Context.getTranslationUnitDecl()); | |||
1708 | ||||
1709 | MultiParseScope Scopes(*this); | |||
1710 | ||||
1711 | // Get the list of DeclContexts to reenter. | |||
1712 | SmallVector<DeclContext*, 4> DeclContextsToReenter; | |||
1713 | for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit(); | |||
1714 | DC = DC->getLexicalParent()) | |||
1715 | DeclContextsToReenter.push_back(DC); | |||
1716 | ||||
1717 | // Reenter scopes from outermost to innermost. | |||
1718 | for (DeclContext *DC : reverse(DeclContextsToReenter)) { | |||
1719 | CurTemplateDepthTracker.addDepth( | |||
1720 | ReenterTemplateScopes(Scopes, cast<Decl>(DC))); | |||
1721 | Scopes.Enter(Scope::DeclScope); | |||
1722 | // We'll reenter the function context itself below. | |||
1723 | if (DC != FunD) | |||
1724 | Actions.PushDeclContext(Actions.getCurScope(), DC); | |||
1725 | } | |||
1726 | ||||
1727 | assert(!LPT.Toks.empty() && "Empty body!")(static_cast <bool> (!LPT.Toks.empty() && "Empty body!" ) ? void (0) : __assert_fail ("!LPT.Toks.empty() && \"Empty body!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1727, __extension__ __PRETTY_FUNCTION__ )); | |||
1728 | ||||
1729 | // Append the current token at the end of the new token stream so that it | |||
1730 | // doesn't get lost. | |||
1731 | LPT.Toks.push_back(Tok); | |||
1732 | PP.EnterTokenStream(LPT.Toks, true, /*IsReinject*/true); | |||
1733 | ||||
1734 | // Consume the previously pushed token. | |||
1735 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); | |||
1736 | assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&(static_cast <bool> (Tok.isOneOf(tok::l_brace, tok::colon , tok::kw_try) && "Inline method not starting with '{', ':' or 'try'" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) && \"Inline method not starting with '{', ':' or 'try'\"" , "clang/lib/Parse/ParseTemplate.cpp", 1737, __extension__ __PRETTY_FUNCTION__ )) | |||
1737 | "Inline method not starting with '{', ':' or 'try'")(static_cast <bool> (Tok.isOneOf(tok::l_brace, tok::colon , tok::kw_try) && "Inline method not starting with '{', ':' or 'try'" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) && \"Inline method not starting with '{', ':' or 'try'\"" , "clang/lib/Parse/ParseTemplate.cpp", 1737, __extension__ __PRETTY_FUNCTION__ )); | |||
1738 | ||||
1739 | // Parse the method body. Function body parsing code is similar enough | |||
1740 | // to be re-used for method bodies as well. | |||
1741 | ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | | |||
1742 | Scope::CompoundStmtScope); | |||
1743 | ||||
1744 | // Recreate the containing function DeclContext. | |||
1745 | Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent()); | |||
| ||||
1746 | ||||
1747 | Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); | |||
1748 | ||||
1749 | if (Tok.is(tok::kw_try)) { | |||
1750 | ParseFunctionTryBlock(LPT.D, FnScope); | |||
1751 | } else { | |||
1752 | if (Tok.is(tok::colon)) | |||
1753 | ParseConstructorInitializer(LPT.D); | |||
1754 | else | |||
1755 | Actions.ActOnDefaultCtorInitializers(LPT.D); | |||
1756 | ||||
1757 | if (Tok.is(tok::l_brace)) { | |||
1758 | assert((!isa<FunctionTemplateDecl>(LPT.D) ||(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )) | |||
1759 | cast<FunctionTemplateDecl>(LPT.D)(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )) | |||
1760 | ->getTemplateParameters()(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )) | |||
1761 | ->getDepth() == TemplateParameterDepth - 1) &&(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )) | |||
1762 | "TemplateParameterDepth should be greater than the depth of "(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )) | |||
1763 | "current template being instantiated!")(static_cast <bool> ((!isa<FunctionTemplateDecl>( LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters () ->getDepth() == TemplateParameterDepth - 1) && "TemplateParameterDepth should be greater than the depth of " "current template being instantiated!") ? void (0) : __assert_fail ("(!isa<FunctionTemplateDecl>(LPT.D) || cast<FunctionTemplateDecl>(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"" , "clang/lib/Parse/ParseTemplate.cpp", 1763, __extension__ __PRETTY_FUNCTION__ )); | |||
1764 | ParseFunctionStatementBody(LPT.D, FnScope); | |||
1765 | Actions.UnmarkAsLateParsedTemplate(FunD); | |||
1766 | } else | |||
1767 | Actions.ActOnFinishFunctionBody(LPT.D, nullptr); | |||
1768 | } | |||
1769 | } | |||
1770 | ||||
1771 | /// Lex a delayed template function for late parsing. | |||
1772 | void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { | |||
1773 | tok::TokenKind kind = Tok.getKind(); | |||
1774 | if (!ConsumeAndStoreFunctionPrologue(Toks)) { | |||
1775 | // Consume everything up to (and including) the matching right brace. | |||
1776 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); | |||
1777 | } | |||
1778 | ||||
1779 | // If we're in a function-try-block, we need to store all the catch blocks. | |||
1780 | if (kind == tok::kw_try) { | |||
1781 | while (Tok.is(tok::kw_catch)) { | |||
1782 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); | |||
1783 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); | |||
1784 | } | |||
1785 | } | |||
1786 | } | |||
1787 | ||||
1788 | /// We've parsed something that could plausibly be intended to be a template | |||
1789 | /// name (\p LHS) followed by a '<' token, and the following code can't possibly | |||
1790 | /// be an expression. Determine if this is likely to be a template-id and if so, | |||
1791 | /// diagnose it. | |||
1792 | bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) { | |||
1793 | TentativeParsingAction TPA(*this); | |||
1794 | // FIXME: We could look at the token sequence in a lot more detail here. | |||
1795 | if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater, | |||
1796 | StopAtSemi | StopBeforeMatch)) { | |||
1797 | TPA.Commit(); | |||
1798 | ||||
1799 | SourceLocation Greater; | |||
1800 | ParseGreaterThanInTemplateList(Less, Greater, true, false); | |||
1801 | Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS, | |||
1802 | Less, Greater); | |||
1803 | return true; | |||
1804 | } | |||
1805 | ||||
1806 | // There's no matching '>' token, this probably isn't supposed to be | |||
1807 | // interpreted as a template-id. Parse it as an (ill-formed) comparison. | |||
1808 | TPA.Revert(); | |||
1809 | return false; | |||
1810 | } | |||
1811 | ||||
1812 | void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) { | |||
1813 | assert(Tok.is(tok::less) && "not at a potential angle bracket")(static_cast <bool> (Tok.is(tok::less) && "not at a potential angle bracket" ) ? void (0) : __assert_fail ("Tok.is(tok::less) && \"not at a potential angle bracket\"" , "clang/lib/Parse/ParseTemplate.cpp", 1813, __extension__ __PRETTY_FUNCTION__ )); | |||
1814 | ||||
1815 | bool DependentTemplateName = false; | |||
1816 | if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName, | |||
1817 | DependentTemplateName)) | |||
1818 | return; | |||
1819 | ||||
1820 | // OK, this might be a name that the user intended to be parsed as a | |||
1821 | // template-name, followed by a '<' token. Check for some easy cases. | |||
1822 | ||||
1823 | // If we have potential_template<>, then it's supposed to be a template-name. | |||
1824 | if (NextToken().is(tok::greater) || | |||
1825 | (getLangOpts().CPlusPlus11 && | |||
1826 | NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) { | |||
1827 | SourceLocation Less = ConsumeToken(); | |||
1828 | SourceLocation Greater; | |||
1829 | ParseGreaterThanInTemplateList(Less, Greater, true, false); | |||
1830 | Actions.diagnoseExprIntendedAsTemplateName( | |||
1831 | getCurScope(), PotentialTemplateName, Less, Greater); | |||
1832 | // FIXME: Perform error recovery. | |||
1833 | PotentialTemplateName = ExprError(); | |||
1834 | return; | |||
1835 | } | |||
1836 | ||||
1837 | // If we have 'potential_template<type-id', assume it's supposed to be a | |||
1838 | // template-name if there's a matching '>' later on. | |||
1839 | { | |||
1840 | // FIXME: Avoid the tentative parse when NextToken() can't begin a type. | |||
1841 | TentativeParsingAction TPA(*this); | |||
1842 | SourceLocation Less = ConsumeToken(); | |||
1843 | if (isTypeIdUnambiguously() && | |||
1844 | diagnoseUnknownTemplateId(PotentialTemplateName, Less)) { | |||
1845 | TPA.Commit(); | |||
1846 | // FIXME: Perform error recovery. | |||
1847 | PotentialTemplateName = ExprError(); | |||
1848 | return; | |||
1849 | } | |||
1850 | TPA.Revert(); | |||
1851 | } | |||
1852 | ||||
1853 | // Otherwise, remember that we saw this in case we see a potentially-matching | |||
1854 | // '>' token later on. | |||
1855 | AngleBracketTracker::Priority Priority = | |||
1856 | (DependentTemplateName ? AngleBracketTracker::DependentName | |||
1857 | : AngleBracketTracker::PotentialTypo) | | |||
1858 | (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess | |||
1859 | : AngleBracketTracker::NoSpaceBeforeLess); | |||
1860 | AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(), | |||
1861 | Priority); | |||
1862 | } | |||
1863 | ||||
1864 | bool Parser::checkPotentialAngleBracketDelimiter( | |||
1865 | const AngleBracketTracker::Loc &LAngle, const Token &OpToken) { | |||
1866 | // If a comma in an expression context is followed by a type that can be a | |||
1867 | // template argument and cannot be an expression, then this is ill-formed, | |||
1868 | // but might be intended to be part of a template-id. | |||
1869 | if (OpToken.is(tok::comma) && isTypeIdUnambiguously() && | |||
1870 | diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) { | |||
1871 | AngleBrackets.clear(*this); | |||
1872 | return true; | |||
1873 | } | |||
1874 | ||||
1875 | // If a context that looks like a template-id is followed by '()', then | |||
1876 | // this is ill-formed, but might be intended to be a template-id | |||
1877 | // followed by '()'. | |||
1878 | if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) && | |||
1879 | NextToken().is(tok::r_paren)) { | |||
1880 | Actions.diagnoseExprIntendedAsTemplateName( | |||
1881 | getCurScope(), LAngle.TemplateName, LAngle.LessLoc, | |||
1882 | OpToken.getLocation()); | |||
1883 | AngleBrackets.clear(*this); | |||
1884 | return true; | |||
1885 | } | |||
1886 | ||||
1887 | // After a '>' (etc), we're no longer potentially in a construct that's | |||
1888 | // intended to be treated as a template-id. | |||
1889 | if (OpToken.is(tok::greater) || | |||
1890 | (getLangOpts().CPlusPlus11 && | |||
1891 | OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater))) | |||
1892 | AngleBrackets.clear(*this); | |||
1893 | return false; | |||
1894 | } |