Clang Project

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