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