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