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