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