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