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