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