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