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