1*67e74705SXin Li //===--- ParseCXXInlineMethods.cpp - C++ class inline methods 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 for C++ class inline methods.
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/DeclTemplate.h"
17*67e74705SXin Li #include "clang/Parse/ParseDiagnostic.h"
18*67e74705SXin Li #include "clang/Sema/DeclSpec.h"
19*67e74705SXin Li #include "clang/Sema/Scope.h"
20*67e74705SXin Li using namespace clang;
21*67e74705SXin Li
22*67e74705SXin Li /// ParseCXXInlineMethodDef - We parsed and verified that the specified
23*67e74705SXin Li /// Declarator is a well formed C++ inline method definition. Now lex its body
24*67e74705SXin Li /// and store its tokens for parsing after the C++ class is complete.
ParseCXXInlineMethodDef(AccessSpecifier AS,AttributeList * AccessAttrs,ParsingDeclarator & D,const ParsedTemplateInfo & TemplateInfo,const VirtSpecifiers & VS,SourceLocation PureSpecLoc)25*67e74705SXin Li NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
26*67e74705SXin Li AttributeList *AccessAttrs,
27*67e74705SXin Li ParsingDeclarator &D,
28*67e74705SXin Li const ParsedTemplateInfo &TemplateInfo,
29*67e74705SXin Li const VirtSpecifiers& VS,
30*67e74705SXin Li SourceLocation PureSpecLoc) {
31*67e74705SXin Li assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
32*67e74705SXin Li assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
33*67e74705SXin Li "Current token not a '{', ':', '=', or 'try'!");
34*67e74705SXin Li
35*67e74705SXin Li MultiTemplateParamsArg TemplateParams(
36*67e74705SXin Li TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
37*67e74705SXin Li : nullptr,
38*67e74705SXin Li TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
39*67e74705SXin Li
40*67e74705SXin Li NamedDecl *FnD;
41*67e74705SXin Li if (D.getDeclSpec().isFriendSpecified())
42*67e74705SXin Li FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
43*67e74705SXin Li TemplateParams);
44*67e74705SXin Li else {
45*67e74705SXin Li FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
46*67e74705SXin Li TemplateParams, nullptr,
47*67e74705SXin Li VS, ICIS_NoInit);
48*67e74705SXin Li if (FnD) {
49*67e74705SXin Li Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
50*67e74705SXin Li if (PureSpecLoc.isValid())
51*67e74705SXin Li Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
52*67e74705SXin Li }
53*67e74705SXin Li }
54*67e74705SXin Li
55*67e74705SXin Li if (FnD)
56*67e74705SXin Li HandleMemberFunctionDeclDelays(D, FnD);
57*67e74705SXin Li
58*67e74705SXin Li D.complete(FnD);
59*67e74705SXin Li
60*67e74705SXin Li if (TryConsumeToken(tok::equal)) {
61*67e74705SXin Li if (!FnD) {
62*67e74705SXin Li SkipUntil(tok::semi);
63*67e74705SXin Li return nullptr;
64*67e74705SXin Li }
65*67e74705SXin Li
66*67e74705SXin Li bool Delete = false;
67*67e74705SXin Li SourceLocation KWLoc;
68*67e74705SXin Li SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
69*67e74705SXin Li if (TryConsumeToken(tok::kw_delete, KWLoc)) {
70*67e74705SXin Li Diag(KWLoc, getLangOpts().CPlusPlus11
71*67e74705SXin Li ? diag::warn_cxx98_compat_defaulted_deleted_function
72*67e74705SXin Li : diag::ext_defaulted_deleted_function)
73*67e74705SXin Li << 1 /* deleted */;
74*67e74705SXin Li Actions.SetDeclDeleted(FnD, KWLoc);
75*67e74705SXin Li Delete = true;
76*67e74705SXin Li if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
77*67e74705SXin Li DeclAsFunction->setRangeEnd(KWEndLoc);
78*67e74705SXin Li }
79*67e74705SXin Li } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
80*67e74705SXin Li Diag(KWLoc, getLangOpts().CPlusPlus11
81*67e74705SXin Li ? diag::warn_cxx98_compat_defaulted_deleted_function
82*67e74705SXin Li : diag::ext_defaulted_deleted_function)
83*67e74705SXin Li << 0 /* defaulted */;
84*67e74705SXin Li Actions.SetDeclDefaulted(FnD, KWLoc);
85*67e74705SXin Li if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
86*67e74705SXin Li DeclAsFunction->setRangeEnd(KWEndLoc);
87*67e74705SXin Li }
88*67e74705SXin Li } else {
89*67e74705SXin Li llvm_unreachable("function definition after = not 'delete' or 'default'");
90*67e74705SXin Li }
91*67e74705SXin Li
92*67e74705SXin Li if (Tok.is(tok::comma)) {
93*67e74705SXin Li Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
94*67e74705SXin Li << Delete;
95*67e74705SXin Li SkipUntil(tok::semi);
96*67e74705SXin Li } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
97*67e74705SXin Li Delete ? "delete" : "default")) {
98*67e74705SXin Li SkipUntil(tok::semi);
99*67e74705SXin Li }
100*67e74705SXin Li
101*67e74705SXin Li return FnD;
102*67e74705SXin Li }
103*67e74705SXin Li
104*67e74705SXin Li if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
105*67e74705SXin Li trySkippingFunctionBody()) {
106*67e74705SXin Li Actions.ActOnSkippedFunctionBody(FnD);
107*67e74705SXin Li return FnD;
108*67e74705SXin Li }
109*67e74705SXin Li
110*67e74705SXin Li // In delayed template parsing mode, if we are within a class template
111*67e74705SXin Li // or if we are about to parse function member template then consume
112*67e74705SXin Li // the tokens and store them for parsing at the end of the translation unit.
113*67e74705SXin Li if (getLangOpts().DelayedTemplateParsing &&
114*67e74705SXin Li D.getFunctionDefinitionKind() == FDK_Definition &&
115*67e74705SXin Li !D.getDeclSpec().isConstexprSpecified() &&
116*67e74705SXin Li !(FnD && FnD->getAsFunction() &&
117*67e74705SXin Li FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
118*67e74705SXin Li ((Actions.CurContext->isDependentContext() ||
119*67e74705SXin Li (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
120*67e74705SXin Li TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
121*67e74705SXin Li !Actions.IsInsideALocalClassWithinATemplateFunction())) {
122*67e74705SXin Li
123*67e74705SXin Li CachedTokens Toks;
124*67e74705SXin Li LexTemplateFunctionForLateParsing(Toks);
125*67e74705SXin Li
126*67e74705SXin Li if (FnD) {
127*67e74705SXin Li FunctionDecl *FD = FnD->getAsFunction();
128*67e74705SXin Li Actions.CheckForFunctionRedefinition(FD);
129*67e74705SXin Li Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
130*67e74705SXin Li }
131*67e74705SXin Li
132*67e74705SXin Li return FnD;
133*67e74705SXin Li }
134*67e74705SXin Li
135*67e74705SXin Li // Consume the tokens and store them for later parsing.
136*67e74705SXin Li
137*67e74705SXin Li LexedMethod* LM = new LexedMethod(this, FnD);
138*67e74705SXin Li getCurrentClass().LateParsedDeclarations.push_back(LM);
139*67e74705SXin Li LM->TemplateScope = getCurScope()->isTemplateParamScope();
140*67e74705SXin Li CachedTokens &Toks = LM->Toks;
141*67e74705SXin Li
142*67e74705SXin Li tok::TokenKind kind = Tok.getKind();
143*67e74705SXin Li // Consume everything up to (and including) the left brace of the
144*67e74705SXin Li // function body.
145*67e74705SXin Li if (ConsumeAndStoreFunctionPrologue(Toks)) {
146*67e74705SXin Li // We didn't find the left-brace we expected after the
147*67e74705SXin Li // constructor initializer; we already printed an error, and it's likely
148*67e74705SXin Li // impossible to recover, so don't try to parse this method later.
149*67e74705SXin Li // Skip over the rest of the decl and back to somewhere that looks
150*67e74705SXin Li // reasonable.
151*67e74705SXin Li SkipMalformedDecl();
152*67e74705SXin Li delete getCurrentClass().LateParsedDeclarations.back();
153*67e74705SXin Li getCurrentClass().LateParsedDeclarations.pop_back();
154*67e74705SXin Li return FnD;
155*67e74705SXin Li } else {
156*67e74705SXin Li // Consume everything up to (and including) the matching right brace.
157*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
158*67e74705SXin Li }
159*67e74705SXin Li
160*67e74705SXin Li // If we're in a function-try-block, we need to store all the catch blocks.
161*67e74705SXin Li if (kind == tok::kw_try) {
162*67e74705SXin Li while (Tok.is(tok::kw_catch)) {
163*67e74705SXin Li ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
164*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
165*67e74705SXin Li }
166*67e74705SXin Li }
167*67e74705SXin Li
168*67e74705SXin Li if (FnD) {
169*67e74705SXin Li // If this is a friend function, mark that it's late-parsed so that
170*67e74705SXin Li // it's still known to be a definition even before we attach the
171*67e74705SXin Li // parsed body. Sema needs to treat friend function definitions
172*67e74705SXin Li // differently during template instantiation, and it's possible for
173*67e74705SXin Li // the containing class to be instantiated before all its member
174*67e74705SXin Li // function definitions are parsed.
175*67e74705SXin Li //
176*67e74705SXin Li // If you remove this, you can remove the code that clears the flag
177*67e74705SXin Li // after parsing the member.
178*67e74705SXin Li if (D.getDeclSpec().isFriendSpecified()) {
179*67e74705SXin Li FunctionDecl *FD = FnD->getAsFunction();
180*67e74705SXin Li Actions.CheckForFunctionRedefinition(FD);
181*67e74705SXin Li FD->setLateTemplateParsed(true);
182*67e74705SXin Li }
183*67e74705SXin Li } else {
184*67e74705SXin Li // If semantic analysis could not build a function declaration,
185*67e74705SXin Li // just throw away the late-parsed declaration.
186*67e74705SXin Li delete getCurrentClass().LateParsedDeclarations.back();
187*67e74705SXin Li getCurrentClass().LateParsedDeclarations.pop_back();
188*67e74705SXin Li }
189*67e74705SXin Li
190*67e74705SXin Li return FnD;
191*67e74705SXin Li }
192*67e74705SXin Li
193*67e74705SXin Li /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
194*67e74705SXin Li /// specified Declarator is a well formed C++ non-static data member
195*67e74705SXin Li /// declaration. Now lex its initializer and store its tokens for parsing
196*67e74705SXin Li /// after the class is complete.
ParseCXXNonStaticMemberInitializer(Decl * VarD)197*67e74705SXin Li void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
198*67e74705SXin Li assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
199*67e74705SXin Li "Current token not a '{' or '='!");
200*67e74705SXin Li
201*67e74705SXin Li LateParsedMemberInitializer *MI =
202*67e74705SXin Li new LateParsedMemberInitializer(this, VarD);
203*67e74705SXin Li getCurrentClass().LateParsedDeclarations.push_back(MI);
204*67e74705SXin Li CachedTokens &Toks = MI->Toks;
205*67e74705SXin Li
206*67e74705SXin Li tok::TokenKind kind = Tok.getKind();
207*67e74705SXin Li if (kind == tok::equal) {
208*67e74705SXin Li Toks.push_back(Tok);
209*67e74705SXin Li ConsumeToken();
210*67e74705SXin Li }
211*67e74705SXin Li
212*67e74705SXin Li if (kind == tok::l_brace) {
213*67e74705SXin Li // Begin by storing the '{' token.
214*67e74705SXin Li Toks.push_back(Tok);
215*67e74705SXin Li ConsumeBrace();
216*67e74705SXin Li
217*67e74705SXin Li // Consume everything up to (and including) the matching right brace.
218*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
219*67e74705SXin Li } else {
220*67e74705SXin Li // Consume everything up to (but excluding) the comma or semicolon.
221*67e74705SXin Li ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
222*67e74705SXin Li }
223*67e74705SXin Li
224*67e74705SXin Li // Store an artificial EOF token to ensure that we don't run off the end of
225*67e74705SXin Li // the initializer when we come to parse it.
226*67e74705SXin Li Token Eof;
227*67e74705SXin Li Eof.startToken();
228*67e74705SXin Li Eof.setKind(tok::eof);
229*67e74705SXin Li Eof.setLocation(Tok.getLocation());
230*67e74705SXin Li Eof.setEofData(VarD);
231*67e74705SXin Li Toks.push_back(Eof);
232*67e74705SXin Li }
233*67e74705SXin Li
~LateParsedDeclaration()234*67e74705SXin Li Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
ParseLexedMethodDeclarations()235*67e74705SXin Li void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
ParseLexedMemberInitializers()236*67e74705SXin Li void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
ParseLexedMethodDefs()237*67e74705SXin Li void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
238*67e74705SXin Li
LateParsedClass(Parser * P,ParsingClass * C)239*67e74705SXin Li Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
240*67e74705SXin Li : Self(P), Class(C) {}
241*67e74705SXin Li
~LateParsedClass()242*67e74705SXin Li Parser::LateParsedClass::~LateParsedClass() {
243*67e74705SXin Li Self->DeallocateParsedClasses(Class);
244*67e74705SXin Li }
245*67e74705SXin Li
ParseLexedMethodDeclarations()246*67e74705SXin Li void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
247*67e74705SXin Li Self->ParseLexedMethodDeclarations(*Class);
248*67e74705SXin Li }
249*67e74705SXin Li
ParseLexedMemberInitializers()250*67e74705SXin Li void Parser::LateParsedClass::ParseLexedMemberInitializers() {
251*67e74705SXin Li Self->ParseLexedMemberInitializers(*Class);
252*67e74705SXin Li }
253*67e74705SXin Li
ParseLexedMethodDefs()254*67e74705SXin Li void Parser::LateParsedClass::ParseLexedMethodDefs() {
255*67e74705SXin Li Self->ParseLexedMethodDefs(*Class);
256*67e74705SXin Li }
257*67e74705SXin Li
ParseLexedMethodDeclarations()258*67e74705SXin Li void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
259*67e74705SXin Li Self->ParseLexedMethodDeclaration(*this);
260*67e74705SXin Li }
261*67e74705SXin Li
ParseLexedMethodDefs()262*67e74705SXin Li void Parser::LexedMethod::ParseLexedMethodDefs() {
263*67e74705SXin Li Self->ParseLexedMethodDef(*this);
264*67e74705SXin Li }
265*67e74705SXin Li
ParseLexedMemberInitializers()266*67e74705SXin Li void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
267*67e74705SXin Li Self->ParseLexedMemberInitializer(*this);
268*67e74705SXin Li }
269*67e74705SXin Li
270*67e74705SXin Li /// ParseLexedMethodDeclarations - We finished parsing the member
271*67e74705SXin Li /// specification of a top (non-nested) C++ class. Now go over the
272*67e74705SXin Li /// stack of method declarations with some parts for which parsing was
273*67e74705SXin Li /// delayed (such as default arguments) and parse them.
ParseLexedMethodDeclarations(ParsingClass & Class)274*67e74705SXin Li void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
275*67e74705SXin Li bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
276*67e74705SXin Li ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
277*67e74705SXin Li HasTemplateScope);
278*67e74705SXin Li TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
279*67e74705SXin Li if (HasTemplateScope) {
280*67e74705SXin Li Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
281*67e74705SXin Li ++CurTemplateDepthTracker;
282*67e74705SXin Li }
283*67e74705SXin Li
284*67e74705SXin Li // The current scope is still active if we're the top-level class.
285*67e74705SXin Li // Otherwise we'll need to push and enter a new scope.
286*67e74705SXin Li bool HasClassScope = !Class.TopLevelClass;
287*67e74705SXin Li ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
288*67e74705SXin Li HasClassScope);
289*67e74705SXin Li if (HasClassScope)
290*67e74705SXin Li Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
291*67e74705SXin Li Class.TagOrTemplate);
292*67e74705SXin Li
293*67e74705SXin Li for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
294*67e74705SXin Li Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
295*67e74705SXin Li }
296*67e74705SXin Li
297*67e74705SXin Li if (HasClassScope)
298*67e74705SXin Li Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
299*67e74705SXin Li Class.TagOrTemplate);
300*67e74705SXin Li }
301*67e74705SXin Li
ParseLexedMethodDeclaration(LateParsedMethodDeclaration & LM)302*67e74705SXin Li void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
303*67e74705SXin Li // If this is a member template, introduce the template parameter scope.
304*67e74705SXin Li ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
305*67e74705SXin Li TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
306*67e74705SXin Li if (LM.TemplateScope) {
307*67e74705SXin Li Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
308*67e74705SXin Li ++CurTemplateDepthTracker;
309*67e74705SXin Li }
310*67e74705SXin Li // Start the delayed C++ method declaration
311*67e74705SXin Li Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
312*67e74705SXin Li
313*67e74705SXin Li // Introduce the parameters into scope and parse their default
314*67e74705SXin Li // arguments.
315*67e74705SXin Li ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
316*67e74705SXin Li Scope::FunctionDeclarationScope | Scope::DeclScope);
317*67e74705SXin Li for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
318*67e74705SXin Li auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
319*67e74705SXin Li // Introduce the parameter into scope.
320*67e74705SXin Li bool HasUnparsed = Param->hasUnparsedDefaultArg();
321*67e74705SXin Li Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
322*67e74705SXin Li if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
323*67e74705SXin Li // Mark the end of the default argument so that we know when to stop when
324*67e74705SXin Li // we parse it later on.
325*67e74705SXin Li Token LastDefaultArgToken = Toks->back();
326*67e74705SXin Li Token DefArgEnd;
327*67e74705SXin Li DefArgEnd.startToken();
328*67e74705SXin Li DefArgEnd.setKind(tok::eof);
329*67e74705SXin Li DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
330*67e74705SXin Li DefArgEnd.setEofData(Param);
331*67e74705SXin Li Toks->push_back(DefArgEnd);
332*67e74705SXin Li
333*67e74705SXin Li // Parse the default argument from its saved token stream.
334*67e74705SXin Li Toks->push_back(Tok); // So that the current token doesn't get lost
335*67e74705SXin Li PP.EnterTokenStream(*Toks, true);
336*67e74705SXin Li
337*67e74705SXin Li // Consume the previously-pushed token.
338*67e74705SXin Li ConsumeAnyToken();
339*67e74705SXin Li
340*67e74705SXin Li // Consume the '='.
341*67e74705SXin Li assert(Tok.is(tok::equal) && "Default argument not starting with '='");
342*67e74705SXin Li SourceLocation EqualLoc = ConsumeToken();
343*67e74705SXin Li
344*67e74705SXin Li // The argument isn't actually potentially evaluated unless it is
345*67e74705SXin Li // used.
346*67e74705SXin Li EnterExpressionEvaluationContext Eval(Actions,
347*67e74705SXin Li Sema::PotentiallyEvaluatedIfUsed,
348*67e74705SXin Li Param);
349*67e74705SXin Li
350*67e74705SXin Li ExprResult DefArgResult;
351*67e74705SXin Li if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
352*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
353*67e74705SXin Li DefArgResult = ParseBraceInitializer();
354*67e74705SXin Li } else
355*67e74705SXin Li DefArgResult = ParseAssignmentExpression();
356*67e74705SXin Li DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
357*67e74705SXin Li if (DefArgResult.isInvalid()) {
358*67e74705SXin Li Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
359*67e74705SXin Li } else {
360*67e74705SXin Li if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
361*67e74705SXin Li // The last two tokens are the terminator and the saved value of
362*67e74705SXin Li // Tok; the last token in the default argument is the one before
363*67e74705SXin Li // those.
364*67e74705SXin Li assert(Toks->size() >= 3 && "expected a token in default arg");
365*67e74705SXin Li Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
366*67e74705SXin Li << SourceRange(Tok.getLocation(),
367*67e74705SXin Li (*Toks)[Toks->size() - 3].getLocation());
368*67e74705SXin Li }
369*67e74705SXin Li Actions.ActOnParamDefaultArgument(Param, EqualLoc,
370*67e74705SXin Li DefArgResult.get());
371*67e74705SXin Li }
372*67e74705SXin Li
373*67e74705SXin Li // There could be leftover tokens (e.g. because of an error).
374*67e74705SXin Li // Skip through until we reach the 'end of default argument' token.
375*67e74705SXin Li while (Tok.isNot(tok::eof))
376*67e74705SXin Li ConsumeAnyToken();
377*67e74705SXin Li
378*67e74705SXin Li if (Tok.is(tok::eof) && Tok.getEofData() == Param)
379*67e74705SXin Li ConsumeAnyToken();
380*67e74705SXin Li
381*67e74705SXin Li delete Toks;
382*67e74705SXin Li LM.DefaultArgs[I].Toks = nullptr;
383*67e74705SXin Li } else if (HasUnparsed) {
384*67e74705SXin Li assert(Param->hasInheritedDefaultArg());
385*67e74705SXin Li FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
386*67e74705SXin Li ParmVarDecl *OldParam = Old->getParamDecl(I);
387*67e74705SXin Li assert (!OldParam->hasUnparsedDefaultArg());
388*67e74705SXin Li if (OldParam->hasUninstantiatedDefaultArg())
389*67e74705SXin Li Param->setUninstantiatedDefaultArg(
390*67e74705SXin Li OldParam->getUninstantiatedDefaultArg());
391*67e74705SXin Li else
392*67e74705SXin Li Param->setDefaultArg(OldParam->getInit());
393*67e74705SXin Li }
394*67e74705SXin Li }
395*67e74705SXin Li
396*67e74705SXin Li // Parse a delayed exception-specification, if there is one.
397*67e74705SXin Li if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
398*67e74705SXin Li // Add the 'stop' token.
399*67e74705SXin Li Token LastExceptionSpecToken = Toks->back();
400*67e74705SXin Li Token ExceptionSpecEnd;
401*67e74705SXin Li ExceptionSpecEnd.startToken();
402*67e74705SXin Li ExceptionSpecEnd.setKind(tok::eof);
403*67e74705SXin Li ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
404*67e74705SXin Li ExceptionSpecEnd.setEofData(LM.Method);
405*67e74705SXin Li Toks->push_back(ExceptionSpecEnd);
406*67e74705SXin Li
407*67e74705SXin Li // Parse the default argument from its saved token stream.
408*67e74705SXin Li Toks->push_back(Tok); // So that the current token doesn't get lost
409*67e74705SXin Li PP.EnterTokenStream(*Toks, true);
410*67e74705SXin Li
411*67e74705SXin Li // Consume the previously-pushed token.
412*67e74705SXin Li ConsumeAnyToken();
413*67e74705SXin Li
414*67e74705SXin Li // C++11 [expr.prim.general]p3:
415*67e74705SXin Li // If a declaration declares a member function or member function
416*67e74705SXin Li // template of a class X, the expression this is a prvalue of type
417*67e74705SXin Li // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
418*67e74705SXin Li // and the end of the function-definition, member-declarator, or
419*67e74705SXin Li // declarator.
420*67e74705SXin Li CXXMethodDecl *Method;
421*67e74705SXin Li if (FunctionTemplateDecl *FunTmpl
422*67e74705SXin Li = dyn_cast<FunctionTemplateDecl>(LM.Method))
423*67e74705SXin Li Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
424*67e74705SXin Li else
425*67e74705SXin Li Method = cast<CXXMethodDecl>(LM.Method);
426*67e74705SXin Li
427*67e74705SXin Li Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
428*67e74705SXin Li Method->getTypeQualifiers(),
429*67e74705SXin Li getLangOpts().CPlusPlus11);
430*67e74705SXin Li
431*67e74705SXin Li // Parse the exception-specification.
432*67e74705SXin Li SourceRange SpecificationRange;
433*67e74705SXin Li SmallVector<ParsedType, 4> DynamicExceptions;
434*67e74705SXin Li SmallVector<SourceRange, 4> DynamicExceptionRanges;
435*67e74705SXin Li ExprResult NoexceptExpr;
436*67e74705SXin Li CachedTokens *ExceptionSpecTokens;
437*67e74705SXin Li
438*67e74705SXin Li ExceptionSpecificationType EST
439*67e74705SXin Li = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
440*67e74705SXin Li DynamicExceptions,
441*67e74705SXin Li DynamicExceptionRanges, NoexceptExpr,
442*67e74705SXin Li ExceptionSpecTokens);
443*67e74705SXin Li
444*67e74705SXin Li if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
445*67e74705SXin Li Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
446*67e74705SXin Li
447*67e74705SXin Li // Attach the exception-specification to the method.
448*67e74705SXin Li Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
449*67e74705SXin Li SpecificationRange,
450*67e74705SXin Li DynamicExceptions,
451*67e74705SXin Li DynamicExceptionRanges,
452*67e74705SXin Li NoexceptExpr.isUsable()?
453*67e74705SXin Li NoexceptExpr.get() : nullptr);
454*67e74705SXin Li
455*67e74705SXin Li // There could be leftover tokens (e.g. because of an error).
456*67e74705SXin Li // Skip through until we reach the original token position.
457*67e74705SXin Li while (Tok.isNot(tok::eof))
458*67e74705SXin Li ConsumeAnyToken();
459*67e74705SXin Li
460*67e74705SXin Li // Clean up the remaining EOF token.
461*67e74705SXin Li if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
462*67e74705SXin Li ConsumeAnyToken();
463*67e74705SXin Li
464*67e74705SXin Li delete Toks;
465*67e74705SXin Li LM.ExceptionSpecTokens = nullptr;
466*67e74705SXin Li }
467*67e74705SXin Li
468*67e74705SXin Li PrototypeScope.Exit();
469*67e74705SXin Li
470*67e74705SXin Li // Finish the delayed C++ method declaration.
471*67e74705SXin Li Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
472*67e74705SXin Li }
473*67e74705SXin Li
474*67e74705SXin Li /// ParseLexedMethodDefs - We finished parsing the member specification of a top
475*67e74705SXin Li /// (non-nested) C++ class. Now go over the stack of lexed methods that were
476*67e74705SXin Li /// collected during its parsing and parse them all.
ParseLexedMethodDefs(ParsingClass & Class)477*67e74705SXin Li void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
478*67e74705SXin Li bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
479*67e74705SXin Li ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
480*67e74705SXin Li TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
481*67e74705SXin Li if (HasTemplateScope) {
482*67e74705SXin Li Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
483*67e74705SXin Li ++CurTemplateDepthTracker;
484*67e74705SXin Li }
485*67e74705SXin Li bool HasClassScope = !Class.TopLevelClass;
486*67e74705SXin Li ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
487*67e74705SXin Li HasClassScope);
488*67e74705SXin Li
489*67e74705SXin Li for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
490*67e74705SXin Li Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
491*67e74705SXin Li }
492*67e74705SXin Li }
493*67e74705SXin Li
ParseLexedMethodDef(LexedMethod & LM)494*67e74705SXin Li void Parser::ParseLexedMethodDef(LexedMethod &LM) {
495*67e74705SXin Li // If this is a member template, introduce the template parameter scope.
496*67e74705SXin Li ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
497*67e74705SXin Li TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
498*67e74705SXin Li if (LM.TemplateScope) {
499*67e74705SXin Li Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
500*67e74705SXin Li ++CurTemplateDepthTracker;
501*67e74705SXin Li }
502*67e74705SXin Li
503*67e74705SXin Li assert(!LM.Toks.empty() && "Empty body!");
504*67e74705SXin Li Token LastBodyToken = LM.Toks.back();
505*67e74705SXin Li Token BodyEnd;
506*67e74705SXin Li BodyEnd.startToken();
507*67e74705SXin Li BodyEnd.setKind(tok::eof);
508*67e74705SXin Li BodyEnd.setLocation(LastBodyToken.getEndLoc());
509*67e74705SXin Li BodyEnd.setEofData(LM.D);
510*67e74705SXin Li LM.Toks.push_back(BodyEnd);
511*67e74705SXin Li // Append the current token at the end of the new token stream so that it
512*67e74705SXin Li // doesn't get lost.
513*67e74705SXin Li LM.Toks.push_back(Tok);
514*67e74705SXin Li PP.EnterTokenStream(LM.Toks, true);
515*67e74705SXin Li
516*67e74705SXin Li // Consume the previously pushed token.
517*67e74705SXin Li ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
518*67e74705SXin Li assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
519*67e74705SXin Li && "Inline method not starting with '{', ':' or 'try'");
520*67e74705SXin Li
521*67e74705SXin Li // Parse the method body. Function body parsing code is similar enough
522*67e74705SXin Li // to be re-used for method bodies as well.
523*67e74705SXin Li ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
524*67e74705SXin Li Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
525*67e74705SXin Li
526*67e74705SXin Li if (Tok.is(tok::kw_try)) {
527*67e74705SXin Li ParseFunctionTryBlock(LM.D, FnScope);
528*67e74705SXin Li
529*67e74705SXin Li while (Tok.isNot(tok::eof))
530*67e74705SXin Li ConsumeAnyToken();
531*67e74705SXin Li
532*67e74705SXin Li if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
533*67e74705SXin Li ConsumeAnyToken();
534*67e74705SXin Li return;
535*67e74705SXin Li }
536*67e74705SXin Li if (Tok.is(tok::colon)) {
537*67e74705SXin Li ParseConstructorInitializer(LM.D);
538*67e74705SXin Li
539*67e74705SXin Li // Error recovery.
540*67e74705SXin Li if (!Tok.is(tok::l_brace)) {
541*67e74705SXin Li FnScope.Exit();
542*67e74705SXin Li Actions.ActOnFinishFunctionBody(LM.D, nullptr);
543*67e74705SXin Li
544*67e74705SXin Li while (Tok.isNot(tok::eof))
545*67e74705SXin Li ConsumeAnyToken();
546*67e74705SXin Li
547*67e74705SXin Li if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
548*67e74705SXin Li ConsumeAnyToken();
549*67e74705SXin Li return;
550*67e74705SXin Li }
551*67e74705SXin Li } else
552*67e74705SXin Li Actions.ActOnDefaultCtorInitializers(LM.D);
553*67e74705SXin Li
554*67e74705SXin Li assert((Actions.getDiagnostics().hasErrorOccurred() ||
555*67e74705SXin Li !isa<FunctionTemplateDecl>(LM.D) ||
556*67e74705SXin Li cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
557*67e74705SXin Li < TemplateParameterDepth) &&
558*67e74705SXin Li "TemplateParameterDepth should be greater than the depth of "
559*67e74705SXin Li "current template being instantiated!");
560*67e74705SXin Li
561*67e74705SXin Li ParseFunctionStatementBody(LM.D, FnScope);
562*67e74705SXin Li
563*67e74705SXin Li // Clear the late-template-parsed bit if we set it before.
564*67e74705SXin Li if (LM.D)
565*67e74705SXin Li LM.D->getAsFunction()->setLateTemplateParsed(false);
566*67e74705SXin Li
567*67e74705SXin Li while (Tok.isNot(tok::eof))
568*67e74705SXin Li ConsumeAnyToken();
569*67e74705SXin Li
570*67e74705SXin Li if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
571*67e74705SXin Li ConsumeAnyToken();
572*67e74705SXin Li
573*67e74705SXin Li if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
574*67e74705SXin Li if (isa<CXXMethodDecl>(FD) ||
575*67e74705SXin Li FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
576*67e74705SXin Li Actions.ActOnFinishInlineFunctionDef(FD);
577*67e74705SXin Li }
578*67e74705SXin Li
579*67e74705SXin Li /// ParseLexedMemberInitializers - We finished parsing the member specification
580*67e74705SXin Li /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
581*67e74705SXin Li /// initializers that were collected during its parsing and parse them all.
ParseLexedMemberInitializers(ParsingClass & Class)582*67e74705SXin Li void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
583*67e74705SXin Li bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
584*67e74705SXin Li ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
585*67e74705SXin Li HasTemplateScope);
586*67e74705SXin Li TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
587*67e74705SXin Li if (HasTemplateScope) {
588*67e74705SXin Li Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
589*67e74705SXin Li ++CurTemplateDepthTracker;
590*67e74705SXin Li }
591*67e74705SXin Li // Set or update the scope flags.
592*67e74705SXin Li bool AlreadyHasClassScope = Class.TopLevelClass;
593*67e74705SXin Li unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
594*67e74705SXin Li ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
595*67e74705SXin Li ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
596*67e74705SXin Li
597*67e74705SXin Li if (!AlreadyHasClassScope)
598*67e74705SXin Li Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
599*67e74705SXin Li Class.TagOrTemplate);
600*67e74705SXin Li
601*67e74705SXin Li if (!Class.LateParsedDeclarations.empty()) {
602*67e74705SXin Li // C++11 [expr.prim.general]p4:
603*67e74705SXin Li // Otherwise, if a member-declarator declares a non-static data member
604*67e74705SXin Li // (9.2) of a class X, the expression this is a prvalue of type "pointer
605*67e74705SXin Li // to X" within the optional brace-or-equal-initializer. It shall not
606*67e74705SXin Li // appear elsewhere in the member-declarator.
607*67e74705SXin Li Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
608*67e74705SXin Li /*TypeQuals=*/(unsigned)0);
609*67e74705SXin Li
610*67e74705SXin Li for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
611*67e74705SXin Li Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
612*67e74705SXin Li }
613*67e74705SXin Li }
614*67e74705SXin Li
615*67e74705SXin Li if (!AlreadyHasClassScope)
616*67e74705SXin Li Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
617*67e74705SXin Li Class.TagOrTemplate);
618*67e74705SXin Li
619*67e74705SXin Li Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
620*67e74705SXin Li }
621*67e74705SXin Li
ParseLexedMemberInitializer(LateParsedMemberInitializer & MI)622*67e74705SXin Li void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
623*67e74705SXin Li if (!MI.Field || MI.Field->isInvalidDecl())
624*67e74705SXin Li return;
625*67e74705SXin Li
626*67e74705SXin Li // Append the current token at the end of the new token stream so that it
627*67e74705SXin Li // doesn't get lost.
628*67e74705SXin Li MI.Toks.push_back(Tok);
629*67e74705SXin Li PP.EnterTokenStream(MI.Toks, true);
630*67e74705SXin Li
631*67e74705SXin Li // Consume the previously pushed token.
632*67e74705SXin Li ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
633*67e74705SXin Li
634*67e74705SXin Li SourceLocation EqualLoc;
635*67e74705SXin Li
636*67e74705SXin Li Actions.ActOnStartCXXInClassMemberInitializer();
637*67e74705SXin Li
638*67e74705SXin Li ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
639*67e74705SXin Li EqualLoc);
640*67e74705SXin Li
641*67e74705SXin Li Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
642*67e74705SXin Li Init.get());
643*67e74705SXin Li
644*67e74705SXin Li // The next token should be our artificial terminating EOF token.
645*67e74705SXin Li if (Tok.isNot(tok::eof)) {
646*67e74705SXin Li if (!Init.isInvalid()) {
647*67e74705SXin Li SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
648*67e74705SXin Li if (!EndLoc.isValid())
649*67e74705SXin Li EndLoc = Tok.getLocation();
650*67e74705SXin Li // No fixit; we can't recover as if there were a semicolon here.
651*67e74705SXin Li Diag(EndLoc, diag::err_expected_semi_decl_list);
652*67e74705SXin Li }
653*67e74705SXin Li
654*67e74705SXin Li // Consume tokens until we hit the artificial EOF.
655*67e74705SXin Li while (Tok.isNot(tok::eof))
656*67e74705SXin Li ConsumeAnyToken();
657*67e74705SXin Li }
658*67e74705SXin Li // Make sure this is *our* artificial EOF token.
659*67e74705SXin Li if (Tok.getEofData() == MI.Field)
660*67e74705SXin Li ConsumeAnyToken();
661*67e74705SXin Li }
662*67e74705SXin Li
663*67e74705SXin Li /// ConsumeAndStoreUntil - Consume and store the token at the passed token
664*67e74705SXin Li /// container until the token 'T' is reached (which gets
665*67e74705SXin Li /// consumed/stored too, if ConsumeFinalToken).
666*67e74705SXin Li /// If StopAtSemi is true, then we will stop early at a ';' character.
667*67e74705SXin Li /// Returns true if token 'T1' or 'T2' was found.
668*67e74705SXin Li /// NOTE: This is a specialized version of Parser::SkipUntil.
ConsumeAndStoreUntil(tok::TokenKind T1,tok::TokenKind T2,CachedTokens & Toks,bool StopAtSemi,bool ConsumeFinalToken)669*67e74705SXin Li bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
670*67e74705SXin Li CachedTokens &Toks,
671*67e74705SXin Li bool StopAtSemi, bool ConsumeFinalToken) {
672*67e74705SXin Li // We always want this function to consume at least one token if the first
673*67e74705SXin Li // token isn't T and if not at EOF.
674*67e74705SXin Li bool isFirstTokenConsumed = true;
675*67e74705SXin Li while (1) {
676*67e74705SXin Li // If we found one of the tokens, stop and return true.
677*67e74705SXin Li if (Tok.is(T1) || Tok.is(T2)) {
678*67e74705SXin Li if (ConsumeFinalToken) {
679*67e74705SXin Li Toks.push_back(Tok);
680*67e74705SXin Li ConsumeAnyToken();
681*67e74705SXin Li }
682*67e74705SXin Li return true;
683*67e74705SXin Li }
684*67e74705SXin Li
685*67e74705SXin Li switch (Tok.getKind()) {
686*67e74705SXin Li case tok::eof:
687*67e74705SXin Li case tok::annot_module_begin:
688*67e74705SXin Li case tok::annot_module_end:
689*67e74705SXin Li case tok::annot_module_include:
690*67e74705SXin Li // Ran out of tokens.
691*67e74705SXin Li return false;
692*67e74705SXin Li
693*67e74705SXin Li case tok::l_paren:
694*67e74705SXin Li // Recursively consume properly-nested parens.
695*67e74705SXin Li Toks.push_back(Tok);
696*67e74705SXin Li ConsumeParen();
697*67e74705SXin Li ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
698*67e74705SXin Li break;
699*67e74705SXin Li case tok::l_square:
700*67e74705SXin Li // Recursively consume properly-nested square brackets.
701*67e74705SXin Li Toks.push_back(Tok);
702*67e74705SXin Li ConsumeBracket();
703*67e74705SXin Li ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
704*67e74705SXin Li break;
705*67e74705SXin Li case tok::l_brace:
706*67e74705SXin Li // Recursively consume properly-nested braces.
707*67e74705SXin Li Toks.push_back(Tok);
708*67e74705SXin Li ConsumeBrace();
709*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
710*67e74705SXin Li break;
711*67e74705SXin Li
712*67e74705SXin Li // Okay, we found a ']' or '}' or ')', which we think should be balanced.
713*67e74705SXin Li // Since the user wasn't looking for this token (if they were, it would
714*67e74705SXin Li // already be handled), this isn't balanced. If there is a LHS token at a
715*67e74705SXin Li // higher level, we will assume that this matches the unbalanced token
716*67e74705SXin Li // and return it. Otherwise, this is a spurious RHS token, which we skip.
717*67e74705SXin Li case tok::r_paren:
718*67e74705SXin Li if (ParenCount && !isFirstTokenConsumed)
719*67e74705SXin Li return false; // Matches something.
720*67e74705SXin Li Toks.push_back(Tok);
721*67e74705SXin Li ConsumeParen();
722*67e74705SXin Li break;
723*67e74705SXin Li case tok::r_square:
724*67e74705SXin Li if (BracketCount && !isFirstTokenConsumed)
725*67e74705SXin Li return false; // Matches something.
726*67e74705SXin Li Toks.push_back(Tok);
727*67e74705SXin Li ConsumeBracket();
728*67e74705SXin Li break;
729*67e74705SXin Li case tok::r_brace:
730*67e74705SXin Li if (BraceCount && !isFirstTokenConsumed)
731*67e74705SXin Li return false; // Matches something.
732*67e74705SXin Li Toks.push_back(Tok);
733*67e74705SXin Li ConsumeBrace();
734*67e74705SXin Li break;
735*67e74705SXin Li
736*67e74705SXin Li case tok::code_completion:
737*67e74705SXin Li Toks.push_back(Tok);
738*67e74705SXin Li ConsumeCodeCompletionToken();
739*67e74705SXin Li break;
740*67e74705SXin Li
741*67e74705SXin Li case tok::string_literal:
742*67e74705SXin Li case tok::wide_string_literal:
743*67e74705SXin Li case tok::utf8_string_literal:
744*67e74705SXin Li case tok::utf16_string_literal:
745*67e74705SXin Li case tok::utf32_string_literal:
746*67e74705SXin Li Toks.push_back(Tok);
747*67e74705SXin Li ConsumeStringToken();
748*67e74705SXin Li break;
749*67e74705SXin Li case tok::semi:
750*67e74705SXin Li if (StopAtSemi)
751*67e74705SXin Li return false;
752*67e74705SXin Li // FALL THROUGH.
753*67e74705SXin Li default:
754*67e74705SXin Li // consume this token.
755*67e74705SXin Li Toks.push_back(Tok);
756*67e74705SXin Li ConsumeToken();
757*67e74705SXin Li break;
758*67e74705SXin Li }
759*67e74705SXin Li isFirstTokenConsumed = false;
760*67e74705SXin Li }
761*67e74705SXin Li }
762*67e74705SXin Li
763*67e74705SXin Li /// \brief Consume tokens and store them in the passed token container until
764*67e74705SXin Li /// we've passed the try keyword and constructor initializers and have consumed
765*67e74705SXin Li /// the opening brace of the function body. The opening brace will be consumed
766*67e74705SXin Li /// if and only if there was no error.
767*67e74705SXin Li ///
768*67e74705SXin Li /// \return True on error.
ConsumeAndStoreFunctionPrologue(CachedTokens & Toks)769*67e74705SXin Li bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
770*67e74705SXin Li if (Tok.is(tok::kw_try)) {
771*67e74705SXin Li Toks.push_back(Tok);
772*67e74705SXin Li ConsumeToken();
773*67e74705SXin Li }
774*67e74705SXin Li
775*67e74705SXin Li if (Tok.isNot(tok::colon)) {
776*67e74705SXin Li // Easy case, just a function body.
777*67e74705SXin Li
778*67e74705SXin Li // Grab any remaining garbage to be diagnosed later. We stop when we reach a
779*67e74705SXin Li // brace: an opening one is the function body, while a closing one probably
780*67e74705SXin Li // means we've reached the end of the class.
781*67e74705SXin Li ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
782*67e74705SXin Li /*StopAtSemi=*/true,
783*67e74705SXin Li /*ConsumeFinalToken=*/false);
784*67e74705SXin Li if (Tok.isNot(tok::l_brace))
785*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
786*67e74705SXin Li
787*67e74705SXin Li Toks.push_back(Tok);
788*67e74705SXin Li ConsumeBrace();
789*67e74705SXin Li return false;
790*67e74705SXin Li }
791*67e74705SXin Li
792*67e74705SXin Li Toks.push_back(Tok);
793*67e74705SXin Li ConsumeToken();
794*67e74705SXin Li
795*67e74705SXin Li // We can't reliably skip over a mem-initializer-id, because it could be
796*67e74705SXin Li // a template-id involving not-yet-declared names. Given:
797*67e74705SXin Li //
798*67e74705SXin Li // S ( ) : a < b < c > ( e )
799*67e74705SXin Li //
800*67e74705SXin Li // 'e' might be an initializer or part of a template argument, depending
801*67e74705SXin Li // on whether 'b' is a template.
802*67e74705SXin Li
803*67e74705SXin Li // Track whether we might be inside a template argument. We can give
804*67e74705SXin Li // significantly better diagnostics if we know that we're not.
805*67e74705SXin Li bool MightBeTemplateArgument = false;
806*67e74705SXin Li
807*67e74705SXin Li while (true) {
808*67e74705SXin Li // Skip over the mem-initializer-id, if possible.
809*67e74705SXin Li if (Tok.is(tok::kw_decltype)) {
810*67e74705SXin Li Toks.push_back(Tok);
811*67e74705SXin Li SourceLocation OpenLoc = ConsumeToken();
812*67e74705SXin Li if (Tok.isNot(tok::l_paren))
813*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
814*67e74705SXin Li << "decltype";
815*67e74705SXin Li Toks.push_back(Tok);
816*67e74705SXin Li ConsumeParen();
817*67e74705SXin Li if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
818*67e74705SXin Li Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
819*67e74705SXin Li Diag(OpenLoc, diag::note_matching) << tok::l_paren;
820*67e74705SXin Li return true;
821*67e74705SXin Li }
822*67e74705SXin Li }
823*67e74705SXin Li do {
824*67e74705SXin Li // Walk over a component of a nested-name-specifier.
825*67e74705SXin Li if (Tok.is(tok::coloncolon)) {
826*67e74705SXin Li Toks.push_back(Tok);
827*67e74705SXin Li ConsumeToken();
828*67e74705SXin Li
829*67e74705SXin Li if (Tok.is(tok::kw_template)) {
830*67e74705SXin Li Toks.push_back(Tok);
831*67e74705SXin Li ConsumeToken();
832*67e74705SXin Li }
833*67e74705SXin Li }
834*67e74705SXin Li
835*67e74705SXin Li if (Tok.isOneOf(tok::identifier, tok::kw_template)) {
836*67e74705SXin Li Toks.push_back(Tok);
837*67e74705SXin Li ConsumeToken();
838*67e74705SXin Li } else if (Tok.is(tok::code_completion)) {
839*67e74705SXin Li Toks.push_back(Tok);
840*67e74705SXin Li ConsumeCodeCompletionToken();
841*67e74705SXin Li // Consume the rest of the initializers permissively.
842*67e74705SXin Li // FIXME: We should be able to perform code-completion here even if
843*67e74705SXin Li // there isn't a subsequent '{' token.
844*67e74705SXin Li MightBeTemplateArgument = true;
845*67e74705SXin Li break;
846*67e74705SXin Li } else {
847*67e74705SXin Li break;
848*67e74705SXin Li }
849*67e74705SXin Li } while (Tok.is(tok::coloncolon));
850*67e74705SXin Li
851*67e74705SXin Li if (Tok.is(tok::less))
852*67e74705SXin Li MightBeTemplateArgument = true;
853*67e74705SXin Li
854*67e74705SXin Li if (MightBeTemplateArgument) {
855*67e74705SXin Li // We may be inside a template argument list. Grab up to the start of the
856*67e74705SXin Li // next parenthesized initializer or braced-init-list. This *might* be the
857*67e74705SXin Li // initializer, or it might be a subexpression in the template argument
858*67e74705SXin Li // list.
859*67e74705SXin Li // FIXME: Count angle brackets, and clear MightBeTemplateArgument
860*67e74705SXin Li // if all angles are closed.
861*67e74705SXin Li if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
862*67e74705SXin Li /*StopAtSemi=*/true,
863*67e74705SXin Li /*ConsumeFinalToken=*/false)) {
864*67e74705SXin Li // We're not just missing the initializer, we're also missing the
865*67e74705SXin Li // function body!
866*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
867*67e74705SXin Li }
868*67e74705SXin Li } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
869*67e74705SXin Li // We found something weird in a mem-initializer-id.
870*67e74705SXin Li if (getLangOpts().CPlusPlus11)
871*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected_either)
872*67e74705SXin Li << tok::l_paren << tok::l_brace;
873*67e74705SXin Li else
874*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
875*67e74705SXin Li }
876*67e74705SXin Li
877*67e74705SXin Li tok::TokenKind kind = Tok.getKind();
878*67e74705SXin Li Toks.push_back(Tok);
879*67e74705SXin Li bool IsLParen = (kind == tok::l_paren);
880*67e74705SXin Li SourceLocation OpenLoc = Tok.getLocation();
881*67e74705SXin Li
882*67e74705SXin Li if (IsLParen) {
883*67e74705SXin Li ConsumeParen();
884*67e74705SXin Li } else {
885*67e74705SXin Li assert(kind == tok::l_brace && "Must be left paren or brace here.");
886*67e74705SXin Li ConsumeBrace();
887*67e74705SXin Li // In C++03, this has to be the start of the function body, which
888*67e74705SXin Li // means the initializer is malformed; we'll diagnose it later.
889*67e74705SXin Li if (!getLangOpts().CPlusPlus11)
890*67e74705SXin Li return false;
891*67e74705SXin Li }
892*67e74705SXin Li
893*67e74705SXin Li // Grab the initializer (or the subexpression of the template argument).
894*67e74705SXin Li // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
895*67e74705SXin Li // if we might be inside the braces of a lambda-expression.
896*67e74705SXin Li tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
897*67e74705SXin Li if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
898*67e74705SXin Li Diag(Tok, diag::err_expected) << CloseKind;
899*67e74705SXin Li Diag(OpenLoc, diag::note_matching) << kind;
900*67e74705SXin Li return true;
901*67e74705SXin Li }
902*67e74705SXin Li
903*67e74705SXin Li // Grab pack ellipsis, if present.
904*67e74705SXin Li if (Tok.is(tok::ellipsis)) {
905*67e74705SXin Li Toks.push_back(Tok);
906*67e74705SXin Li ConsumeToken();
907*67e74705SXin Li }
908*67e74705SXin Li
909*67e74705SXin Li // If we know we just consumed a mem-initializer, we must have ',' or '{'
910*67e74705SXin Li // next.
911*67e74705SXin Li if (Tok.is(tok::comma)) {
912*67e74705SXin Li Toks.push_back(Tok);
913*67e74705SXin Li ConsumeToken();
914*67e74705SXin Li } else if (Tok.is(tok::l_brace)) {
915*67e74705SXin Li // This is the function body if the ')' or '}' is immediately followed by
916*67e74705SXin Li // a '{'. That cannot happen within a template argument, apart from the
917*67e74705SXin Li // case where a template argument contains a compound literal:
918*67e74705SXin Li //
919*67e74705SXin Li // S ( ) : a < b < c > ( d ) { }
920*67e74705SXin Li // // End of declaration, or still inside the template argument?
921*67e74705SXin Li //
922*67e74705SXin Li // ... and the case where the template argument contains a lambda:
923*67e74705SXin Li //
924*67e74705SXin Li // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
925*67e74705SXin Li // ( ) > ( ) { }
926*67e74705SXin Li //
927*67e74705SXin Li // FIXME: Disambiguate these cases. Note that the latter case is probably
928*67e74705SXin Li // going to be made ill-formed by core issue 1607.
929*67e74705SXin Li Toks.push_back(Tok);
930*67e74705SXin Li ConsumeBrace();
931*67e74705SXin Li return false;
932*67e74705SXin Li } else if (!MightBeTemplateArgument) {
933*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
934*67e74705SXin Li << tok::comma;
935*67e74705SXin Li }
936*67e74705SXin Li }
937*67e74705SXin Li }
938*67e74705SXin Li
939*67e74705SXin Li /// \brief Consume and store tokens from the '?' to the ':' in a conditional
940*67e74705SXin Li /// expression.
ConsumeAndStoreConditional(CachedTokens & Toks)941*67e74705SXin Li bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
942*67e74705SXin Li // Consume '?'.
943*67e74705SXin Li assert(Tok.is(tok::question));
944*67e74705SXin Li Toks.push_back(Tok);
945*67e74705SXin Li ConsumeToken();
946*67e74705SXin Li
947*67e74705SXin Li while (Tok.isNot(tok::colon)) {
948*67e74705SXin Li if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
949*67e74705SXin Li /*StopAtSemi=*/true,
950*67e74705SXin Li /*ConsumeFinalToken=*/false))
951*67e74705SXin Li return false;
952*67e74705SXin Li
953*67e74705SXin Li // If we found a nested conditional, consume it.
954*67e74705SXin Li if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
955*67e74705SXin Li return false;
956*67e74705SXin Li }
957*67e74705SXin Li
958*67e74705SXin Li // Consume ':'.
959*67e74705SXin Li Toks.push_back(Tok);
960*67e74705SXin Li ConsumeToken();
961*67e74705SXin Li return true;
962*67e74705SXin Li }
963*67e74705SXin Li
964*67e74705SXin Li /// \brief A tentative parsing action that can also revert token annotations.
965*67e74705SXin Li class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
966*67e74705SXin Li public:
UnannotatedTentativeParsingAction(Parser & Self,tok::TokenKind EndKind)967*67e74705SXin Li explicit UnannotatedTentativeParsingAction(Parser &Self,
968*67e74705SXin Li tok::TokenKind EndKind)
969*67e74705SXin Li : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
970*67e74705SXin Li // Stash away the old token stream, so we can restore it once the
971*67e74705SXin Li // tentative parse is complete.
972*67e74705SXin Li TentativeParsingAction Inner(Self);
973*67e74705SXin Li Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
974*67e74705SXin Li Inner.Revert();
975*67e74705SXin Li }
976*67e74705SXin Li
RevertAnnotations()977*67e74705SXin Li void RevertAnnotations() {
978*67e74705SXin Li Revert();
979*67e74705SXin Li
980*67e74705SXin Li // Put back the original tokens.
981*67e74705SXin Li Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
982*67e74705SXin Li if (Toks.size()) {
983*67e74705SXin Li auto Buffer = llvm::make_unique<Token[]>(Toks.size());
984*67e74705SXin Li std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
985*67e74705SXin Li Buffer[Toks.size() - 1] = Self.Tok;
986*67e74705SXin Li Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true);
987*67e74705SXin Li
988*67e74705SXin Li Self.Tok = Toks.front();
989*67e74705SXin Li }
990*67e74705SXin Li }
991*67e74705SXin Li
992*67e74705SXin Li private:
993*67e74705SXin Li Parser &Self;
994*67e74705SXin Li CachedTokens Toks;
995*67e74705SXin Li tok::TokenKind EndKind;
996*67e74705SXin Li };
997*67e74705SXin Li
998*67e74705SXin Li /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
999*67e74705SXin Li /// container until the end of the current initializer expression (either a
1000*67e74705SXin Li /// default argument or an in-class initializer for a non-static data member).
1001*67e74705SXin Li ///
1002*67e74705SXin Li /// Returns \c true if we reached the end of something initializer-shaped,
1003*67e74705SXin Li /// \c false if we bailed out.
ConsumeAndStoreInitializer(CachedTokens & Toks,CachedInitKind CIK)1004*67e74705SXin Li bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1005*67e74705SXin Li CachedInitKind CIK) {
1006*67e74705SXin Li // We always want this function to consume at least one token if not at EOF.
1007*67e74705SXin Li bool IsFirstToken = true;
1008*67e74705SXin Li
1009*67e74705SXin Li // Number of possible unclosed <s we've seen so far. These might be templates,
1010*67e74705SXin Li // and might not, but if there were none of them (or we know for sure that
1011*67e74705SXin Li // we're within a template), we can avoid a tentative parse.
1012*67e74705SXin Li unsigned AngleCount = 0;
1013*67e74705SXin Li unsigned KnownTemplateCount = 0;
1014*67e74705SXin Li
1015*67e74705SXin Li while (1) {
1016*67e74705SXin Li switch (Tok.getKind()) {
1017*67e74705SXin Li case tok::comma:
1018*67e74705SXin Li // If we might be in a template, perform a tentative parse to check.
1019*67e74705SXin Li if (!AngleCount)
1020*67e74705SXin Li // Not a template argument: this is the end of the initializer.
1021*67e74705SXin Li return true;
1022*67e74705SXin Li if (KnownTemplateCount)
1023*67e74705SXin Li goto consume_token;
1024*67e74705SXin Li
1025*67e74705SXin Li // We hit a comma inside angle brackets. This is the hard case. The
1026*67e74705SXin Li // rule we follow is:
1027*67e74705SXin Li // * For a default argument, if the tokens after the comma form a
1028*67e74705SXin Li // syntactically-valid parameter-declaration-clause, in which each
1029*67e74705SXin Li // parameter has an initializer, then this comma ends the default
1030*67e74705SXin Li // argument.
1031*67e74705SXin Li // * For a default initializer, if the tokens after the comma form a
1032*67e74705SXin Li // syntactically-valid init-declarator-list, then this comma ends
1033*67e74705SXin Li // the default initializer.
1034*67e74705SXin Li {
1035*67e74705SXin Li UnannotatedTentativeParsingAction PA(*this,
1036*67e74705SXin Li CIK == CIK_DefaultInitializer
1037*67e74705SXin Li ? tok::semi : tok::r_paren);
1038*67e74705SXin Li Sema::TentativeAnalysisScope Scope(Actions);
1039*67e74705SXin Li
1040*67e74705SXin Li TPResult Result = TPResult::Error;
1041*67e74705SXin Li ConsumeToken();
1042*67e74705SXin Li switch (CIK) {
1043*67e74705SXin Li case CIK_DefaultInitializer:
1044*67e74705SXin Li Result = TryParseInitDeclaratorList();
1045*67e74705SXin Li // If we parsed a complete, ambiguous init-declarator-list, this
1046*67e74705SXin Li // is only syntactically-valid if it's followed by a semicolon.
1047*67e74705SXin Li if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1048*67e74705SXin Li Result = TPResult::False;
1049*67e74705SXin Li break;
1050*67e74705SXin Li
1051*67e74705SXin Li case CIK_DefaultArgument:
1052*67e74705SXin Li bool InvalidAsDeclaration = false;
1053*67e74705SXin Li Result = TryParseParameterDeclarationClause(
1054*67e74705SXin Li &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
1055*67e74705SXin Li // If this is an expression or a declaration with a missing
1056*67e74705SXin Li // 'typename', assume it's not a declaration.
1057*67e74705SXin Li if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1058*67e74705SXin Li Result = TPResult::False;
1059*67e74705SXin Li break;
1060*67e74705SXin Li }
1061*67e74705SXin Li
1062*67e74705SXin Li // If what follows could be a declaration, it is a declaration.
1063*67e74705SXin Li if (Result != TPResult::False && Result != TPResult::Error) {
1064*67e74705SXin Li PA.Revert();
1065*67e74705SXin Li return true;
1066*67e74705SXin Li }
1067*67e74705SXin Li
1068*67e74705SXin Li // In the uncommon case that we decide the following tokens are part
1069*67e74705SXin Li // of a template argument, revert any annotations we've performed in
1070*67e74705SXin Li // those tokens. We're not going to look them up until we've parsed
1071*67e74705SXin Li // the rest of the class, and that might add more declarations.
1072*67e74705SXin Li PA.RevertAnnotations();
1073*67e74705SXin Li }
1074*67e74705SXin Li
1075*67e74705SXin Li // Keep going. We know we're inside a template argument list now.
1076*67e74705SXin Li ++KnownTemplateCount;
1077*67e74705SXin Li goto consume_token;
1078*67e74705SXin Li
1079*67e74705SXin Li case tok::eof:
1080*67e74705SXin Li case tok::annot_module_begin:
1081*67e74705SXin Li case tok::annot_module_end:
1082*67e74705SXin Li case tok::annot_module_include:
1083*67e74705SXin Li // Ran out of tokens.
1084*67e74705SXin Li return false;
1085*67e74705SXin Li
1086*67e74705SXin Li case tok::less:
1087*67e74705SXin Li // FIXME: A '<' can only start a template-id if it's preceded by an
1088*67e74705SXin Li // identifier, an operator-function-id, or a literal-operator-id.
1089*67e74705SXin Li ++AngleCount;
1090*67e74705SXin Li goto consume_token;
1091*67e74705SXin Li
1092*67e74705SXin Li case tok::question:
1093*67e74705SXin Li // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1094*67e74705SXin Li // that is *never* the end of the initializer. Skip to the ':'.
1095*67e74705SXin Li if (!ConsumeAndStoreConditional(Toks))
1096*67e74705SXin Li return false;
1097*67e74705SXin Li break;
1098*67e74705SXin Li
1099*67e74705SXin Li case tok::greatergreatergreater:
1100*67e74705SXin Li if (!getLangOpts().CPlusPlus11)
1101*67e74705SXin Li goto consume_token;
1102*67e74705SXin Li if (AngleCount) --AngleCount;
1103*67e74705SXin Li if (KnownTemplateCount) --KnownTemplateCount;
1104*67e74705SXin Li // Fall through.
1105*67e74705SXin Li case tok::greatergreater:
1106*67e74705SXin Li if (!getLangOpts().CPlusPlus11)
1107*67e74705SXin Li goto consume_token;
1108*67e74705SXin Li if (AngleCount) --AngleCount;
1109*67e74705SXin Li if (KnownTemplateCount) --KnownTemplateCount;
1110*67e74705SXin Li // Fall through.
1111*67e74705SXin Li case tok::greater:
1112*67e74705SXin Li if (AngleCount) --AngleCount;
1113*67e74705SXin Li if (KnownTemplateCount) --KnownTemplateCount;
1114*67e74705SXin Li goto consume_token;
1115*67e74705SXin Li
1116*67e74705SXin Li case tok::kw_template:
1117*67e74705SXin Li // 'template' identifier '<' is known to start a template argument list,
1118*67e74705SXin Li // and can be used to disambiguate the parse.
1119*67e74705SXin Li // FIXME: Support all forms of 'template' unqualified-id '<'.
1120*67e74705SXin Li Toks.push_back(Tok);
1121*67e74705SXin Li ConsumeToken();
1122*67e74705SXin Li if (Tok.is(tok::identifier)) {
1123*67e74705SXin Li Toks.push_back(Tok);
1124*67e74705SXin Li ConsumeToken();
1125*67e74705SXin Li if (Tok.is(tok::less)) {
1126*67e74705SXin Li ++AngleCount;
1127*67e74705SXin Li ++KnownTemplateCount;
1128*67e74705SXin Li Toks.push_back(Tok);
1129*67e74705SXin Li ConsumeToken();
1130*67e74705SXin Li }
1131*67e74705SXin Li }
1132*67e74705SXin Li break;
1133*67e74705SXin Li
1134*67e74705SXin Li case tok::kw_operator:
1135*67e74705SXin Li // If 'operator' precedes other punctuation, that punctuation loses
1136*67e74705SXin Li // its special behavior.
1137*67e74705SXin Li Toks.push_back(Tok);
1138*67e74705SXin Li ConsumeToken();
1139*67e74705SXin Li switch (Tok.getKind()) {
1140*67e74705SXin Li case tok::comma:
1141*67e74705SXin Li case tok::greatergreatergreater:
1142*67e74705SXin Li case tok::greatergreater:
1143*67e74705SXin Li case tok::greater:
1144*67e74705SXin Li case tok::less:
1145*67e74705SXin Li Toks.push_back(Tok);
1146*67e74705SXin Li ConsumeToken();
1147*67e74705SXin Li break;
1148*67e74705SXin Li default:
1149*67e74705SXin Li break;
1150*67e74705SXin Li }
1151*67e74705SXin Li break;
1152*67e74705SXin Li
1153*67e74705SXin Li case tok::l_paren:
1154*67e74705SXin Li // Recursively consume properly-nested parens.
1155*67e74705SXin Li Toks.push_back(Tok);
1156*67e74705SXin Li ConsumeParen();
1157*67e74705SXin Li ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1158*67e74705SXin Li break;
1159*67e74705SXin Li case tok::l_square:
1160*67e74705SXin Li // Recursively consume properly-nested square brackets.
1161*67e74705SXin Li Toks.push_back(Tok);
1162*67e74705SXin Li ConsumeBracket();
1163*67e74705SXin Li ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1164*67e74705SXin Li break;
1165*67e74705SXin Li case tok::l_brace:
1166*67e74705SXin Li // Recursively consume properly-nested braces.
1167*67e74705SXin Li Toks.push_back(Tok);
1168*67e74705SXin Li ConsumeBrace();
1169*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1170*67e74705SXin Li break;
1171*67e74705SXin Li
1172*67e74705SXin Li // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1173*67e74705SXin Li // Since the user wasn't looking for this token (if they were, it would
1174*67e74705SXin Li // already be handled), this isn't balanced. If there is a LHS token at a
1175*67e74705SXin Li // higher level, we will assume that this matches the unbalanced token
1176*67e74705SXin Li // and return it. Otherwise, this is a spurious RHS token, which we
1177*67e74705SXin Li // consume and pass on to downstream code to diagnose.
1178*67e74705SXin Li case tok::r_paren:
1179*67e74705SXin Li if (CIK == CIK_DefaultArgument)
1180*67e74705SXin Li return true; // End of the default argument.
1181*67e74705SXin Li if (ParenCount && !IsFirstToken)
1182*67e74705SXin Li return false;
1183*67e74705SXin Li Toks.push_back(Tok);
1184*67e74705SXin Li ConsumeParen();
1185*67e74705SXin Li continue;
1186*67e74705SXin Li case tok::r_square:
1187*67e74705SXin Li if (BracketCount && !IsFirstToken)
1188*67e74705SXin Li return false;
1189*67e74705SXin Li Toks.push_back(Tok);
1190*67e74705SXin Li ConsumeBracket();
1191*67e74705SXin Li continue;
1192*67e74705SXin Li case tok::r_brace:
1193*67e74705SXin Li if (BraceCount && !IsFirstToken)
1194*67e74705SXin Li return false;
1195*67e74705SXin Li Toks.push_back(Tok);
1196*67e74705SXin Li ConsumeBrace();
1197*67e74705SXin Li continue;
1198*67e74705SXin Li
1199*67e74705SXin Li case tok::code_completion:
1200*67e74705SXin Li Toks.push_back(Tok);
1201*67e74705SXin Li ConsumeCodeCompletionToken();
1202*67e74705SXin Li break;
1203*67e74705SXin Li
1204*67e74705SXin Li case tok::string_literal:
1205*67e74705SXin Li case tok::wide_string_literal:
1206*67e74705SXin Li case tok::utf8_string_literal:
1207*67e74705SXin Li case tok::utf16_string_literal:
1208*67e74705SXin Li case tok::utf32_string_literal:
1209*67e74705SXin Li Toks.push_back(Tok);
1210*67e74705SXin Li ConsumeStringToken();
1211*67e74705SXin Li break;
1212*67e74705SXin Li case tok::semi:
1213*67e74705SXin Li if (CIK == CIK_DefaultInitializer)
1214*67e74705SXin Li return true; // End of the default initializer.
1215*67e74705SXin Li // FALL THROUGH.
1216*67e74705SXin Li default:
1217*67e74705SXin Li consume_token:
1218*67e74705SXin Li Toks.push_back(Tok);
1219*67e74705SXin Li ConsumeToken();
1220*67e74705SXin Li break;
1221*67e74705SXin Li }
1222*67e74705SXin Li IsFirstToken = false;
1223*67e74705SXin Li }
1224*67e74705SXin Li }
1225