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