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