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