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 /// Get the FunctionDecl for a function or function template decl.
     23 static FunctionDecl *getFunctionDecl(Decl *D) {
     24   if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D))
     25     return fn;
     26   return cast<FunctionTemplateDecl>(D)->getTemplatedDecl();
     27 }
     28 
     29 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
     30 /// Declarator is a well formed C++ inline method definition. Now lex its body
     31 /// and store its tokens for parsing after the C++ class is complete.
     32 NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
     33                                       AttributeList *AccessAttrs,
     34                                       ParsingDeclarator &D,
     35                                       const ParsedTemplateInfo &TemplateInfo,
     36                                       const VirtSpecifiers& VS,
     37                                       FunctionDefinitionKind DefinitionKind,
     38                                       ExprResult& Init) {
     39   assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
     40   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
     41           Tok.is(tok::equal)) &&
     42          "Current token not a '{', ':', '=', or 'try'!");
     43 
     44   MultiTemplateParamsArg TemplateParams(
     45           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
     46           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
     47 
     48   NamedDecl *FnD;
     49   D.setFunctionDefinitionKind(DefinitionKind);
     50   if (D.getDeclSpec().isFriendSpecified())
     51     FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
     52                                           TemplateParams);
     53   else {
     54     FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
     55                                            TemplateParams, 0,
     56                                            VS, ICIS_NoInit);
     57     if (FnD) {
     58       Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
     59                                        false, true);
     60       bool TypeSpecContainsAuto
     61         = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
     62       if (Init.isUsable())
     63         Actions.AddInitializerToDecl(FnD, Init.get(), false,
     64                                      TypeSpecContainsAuto);
     65       else
     66         Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
     67     }
     68   }
     69 
     70   HandleMemberFunctionDeclDelays(D, FnD);
     71 
     72   D.complete(FnD);
     73 
     74   if (Tok.is(tok::equal)) {
     75     ConsumeToken();
     76 
     77     if (!FnD) {
     78       SkipUntil(tok::semi);
     79       return 0;
     80     }
     81 
     82     bool Delete = false;
     83     SourceLocation KWLoc;
     84     if (Tok.is(tok::kw_delete)) {
     85       Diag(Tok, getLangOpts().CPlusPlus11 ?
     86            diag::warn_cxx98_compat_deleted_function :
     87            diag::ext_deleted_function);
     88 
     89       KWLoc = ConsumeToken();
     90       Actions.SetDeclDeleted(FnD, KWLoc);
     91       Delete = true;
     92     } else if (Tok.is(tok::kw_default)) {
     93       Diag(Tok, getLangOpts().CPlusPlus11 ?
     94            diag::warn_cxx98_compat_defaulted_function :
     95            diag::ext_defaulted_function);
     96 
     97       KWLoc = ConsumeToken();
     98       Actions.SetDeclDefaulted(FnD, KWLoc);
     99     } else {
    100       llvm_unreachable("function definition after = not 'delete' or 'default'");
    101     }
    102 
    103     if (Tok.is(tok::comma)) {
    104       Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
    105         << Delete;
    106       SkipUntil(tok::semi);
    107     } else {
    108       ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
    109                        Delete ? "delete" : "default", tok::semi);
    110     }
    111 
    112     return FnD;
    113   }
    114 
    115   // In delayed template parsing mode, if we are within a class template
    116   // or if we are about to parse function member template then consume
    117   // the tokens and store them for parsing at the end of the translation unit.
    118   if (getLangOpts().DelayedTemplateParsing &&
    119       DefinitionKind == FDK_Definition &&
    120       ((Actions.CurContext->isDependentContext() ||
    121         TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
    122         !Actions.IsInsideALocalClassWithinATemplateFunction())) {
    123 
    124     if (FnD) {
    125       LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
    126 
    127       FunctionDecl *FD = getFunctionDecl(FnD);
    128       Actions.CheckForFunctionRedefinition(FD);
    129 
    130       LateParsedTemplateMap[FD] = LPT;
    131       Actions.MarkAsLateParsedTemplate(FD);
    132       LexTemplateFunctionForLateParsing(LPT->Toks);
    133     } else {
    134       CachedTokens Toks;
    135       LexTemplateFunctionForLateParsing(Toks);
    136     }
    137 
    138     return FnD;
    139   }
    140 
    141   // Consume the tokens and store them for later parsing.
    142 
    143   LexedMethod* LM = new LexedMethod(this, FnD);
    144   getCurrentClass().LateParsedDeclarations.push_back(LM);
    145   LM->TemplateScope = getCurScope()->isTemplateParamScope();
    146   CachedTokens &Toks = LM->Toks;
    147 
    148   tok::TokenKind kind = Tok.getKind();
    149   // Consume everything up to (and including) the left brace of the
    150   // function body.
    151   if (ConsumeAndStoreFunctionPrologue(Toks)) {
    152     // We didn't find the left-brace we expected after the
    153     // constructor initializer; we already printed an error, and it's likely
    154     // impossible to recover, so don't try to parse this method later.
    155     // If we stopped at a semicolon, consume it to avoid an extra warning.
    156      if (Tok.is(tok::semi))
    157       ConsumeToken();
    158     delete getCurrentClass().LateParsedDeclarations.back();
    159     getCurrentClass().LateParsedDeclarations.pop_back();
    160     return FnD;
    161   } else {
    162     // Consume everything up to (and including) the matching right brace.
    163     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
    164   }
    165 
    166   // If we're in a function-try-block, we need to store all the catch blocks.
    167   if (kind == tok::kw_try) {
    168     while (Tok.is(tok::kw_catch)) {
    169       ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
    170       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
    171     }
    172   }
    173 
    174 
    175   if (!FnD) {
    176     // If semantic analysis could not build a function declaration,
    177     // just throw away the late-parsed declaration.
    178     delete getCurrentClass().LateParsedDeclarations.back();
    179     getCurrentClass().LateParsedDeclarations.pop_back();
    180   }
    181 
    182   // If this is a friend function, mark that it's late-parsed so that
    183   // it's still known to be a definition even before we attach the
    184   // parsed body.  Sema needs to treat friend function definitions
    185   // differently during template instantiation, and it's possible for
    186   // the containing class to be instantiated before all its member
    187   // function definitions are parsed.
    188   //
    189   // If you remove this, you can remove the code that clears the flag
    190   // after parsing the member.
    191   if (D.getDeclSpec().isFriendSpecified()) {
    192     getFunctionDecl(FnD)->setLateTemplateParsed(true);
    193   }
    194 
    195   return FnD;
    196 }
    197 
    198 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
    199 /// specified Declarator is a well formed C++ non-static data member
    200 /// declaration. Now lex its initializer and store its tokens for parsing
    201 /// after the class is complete.
    202 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
    203   assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
    204          "Current token not a '{' or '='!");
    205 
    206   LateParsedMemberInitializer *MI =
    207     new LateParsedMemberInitializer(this, VarD);
    208   getCurrentClass().LateParsedDeclarations.push_back(MI);
    209   CachedTokens &Toks = MI->Toks;
    210 
    211   tok::TokenKind kind = Tok.getKind();
    212   if (kind == tok::equal) {
    213     Toks.push_back(Tok);
    214     ConsumeToken();
    215   }
    216 
    217   if (kind == tok::l_brace) {
    218     // Begin by storing the '{' token.
    219     Toks.push_back(Tok);
    220     ConsumeBrace();
    221 
    222     // Consume everything up to (and including) the matching right brace.
    223     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
    224   } else {
    225     // Consume everything up to (but excluding) the comma or semicolon.
    226     ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
    227                          /*ConsumeFinalToken=*/false);
    228   }
    229 
    230   // Store an artificial EOF token to ensure that we don't run off the end of
    231   // the initializer when we come to parse it.
    232   Token Eof;
    233   Eof.startToken();
    234   Eof.setKind(tok::eof);
    235   Eof.setLocation(Tok.getLocation());
    236   Toks.push_back(Eof);
    237 }
    238 
    239 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
    240 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
    241 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
    242 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
    243 
    244 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
    245   : Self(P), Class(C) {}
    246 
    247 Parser::LateParsedClass::~LateParsedClass() {
    248   Self->DeallocateParsedClasses(Class);
    249 }
    250 
    251 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
    252   Self->ParseLexedMethodDeclarations(*Class);
    253 }
    254 
    255 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
    256   Self->ParseLexedMemberInitializers(*Class);
    257 }
    258 
    259 void Parser::LateParsedClass::ParseLexedMethodDefs() {
    260   Self->ParseLexedMethodDefs(*Class);
    261 }
    262 
    263 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
    264   Self->ParseLexedMethodDeclaration(*this);
    265 }
    266 
    267 void Parser::LexedMethod::ParseLexedMethodDefs() {
    268   Self->ParseLexedMethodDef(*this);
    269 }
    270 
    271 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
    272   Self->ParseLexedMemberInitializer(*this);
    273 }
    274 
    275 /// ParseLexedMethodDeclarations - We finished parsing the member
    276 /// specification of a top (non-nested) C++ class. Now go over the
    277 /// stack of method declarations with some parts for which parsing was
    278 /// delayed (such as default arguments) and parse them.
    279 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
    280   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
    281   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
    282   if (HasTemplateScope)
    283     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
    284 
    285   // The current scope is still active if we're the top-level class.
    286   // Otherwise we'll need to push and enter a new scope.
    287   bool HasClassScope = !Class.TopLevelClass;
    288   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
    289                         HasClassScope);
    290   if (HasClassScope)
    291     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
    292 
    293   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
    294     Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
    295   }
    296 
    297   if (HasClassScope)
    298     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
    299 }
    300 
    301 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
    302   // If this is a member template, introduce the template parameter scope.
    303   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
    304   if (LM.TemplateScope)
    305     Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
    306 
    307   // Start the delayed C++ method declaration
    308   Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
    309 
    310   // Introduce the parameters into scope and parse their default
    311   // arguments.
    312   ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
    313                             Scope::FunctionDeclarationScope | Scope::DeclScope);
    314   for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
    315     // Introduce the parameter into scope.
    316     Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
    317                                            LM.DefaultArgs[I].Param);
    318 
    319     if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
    320       // Save the current token position.
    321       SourceLocation origLoc = Tok.getLocation();
    322 
    323       // Parse the default argument from its saved token stream.
    324       Toks->push_back(Tok); // So that the current token doesn't get lost
    325       PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
    326 
    327       // Consume the previously-pushed token.
    328       ConsumeAnyToken();
    329 
    330       // Consume the '='.
    331       assert(Tok.is(tok::equal) && "Default argument not starting with '='");
    332       SourceLocation EqualLoc = ConsumeToken();
    333 
    334       // The argument isn't actually potentially evaluated unless it is
    335       // used.
    336       EnterExpressionEvaluationContext Eval(Actions,
    337                                             Sema::PotentiallyEvaluatedIfUsed,
    338                                             LM.DefaultArgs[I].Param);
    339 
    340       ExprResult DefArgResult;
    341       if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
    342         Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
    343         DefArgResult = ParseBraceInitializer();
    344       } else
    345         DefArgResult = ParseAssignmentExpression();
    346       if (DefArgResult.isInvalid())
    347         Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
    348       else {
    349         if (Tok.is(tok::cxx_defaultarg_end))
    350           ConsumeToken();
    351         else
    352           Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
    353         Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
    354                                           DefArgResult.take());
    355       }
    356 
    357       assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
    358                                                          Tok.getLocation()) &&
    359              "ParseAssignmentExpression went over the default arg tokens!");
    360       // There could be leftover tokens (e.g. because of an error).
    361       // Skip through until we reach the original token position.
    362       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
    363         ConsumeAnyToken();
    364 
    365       delete Toks;
    366       LM.DefaultArgs[I].Toks = 0;
    367     }
    368   }
    369 
    370   PrototypeScope.Exit();
    371 
    372   // Finish the delayed C++ method declaration.
    373   Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
    374 }
    375 
    376 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
    377 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
    378 /// collected during its parsing and parse them all.
    379 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
    380   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
    381   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
    382   if (HasTemplateScope)
    383     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
    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   if (LM.TemplateScope)
    398     Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
    399 
    400   // Save the current token position.
    401   SourceLocation origLoc = Tok.getLocation();
    402 
    403   assert(!LM.Toks.empty() && "Empty body!");
    404   // Append the current token at the end of the new token stream so that it
    405   // doesn't get lost.
    406   LM.Toks.push_back(Tok);
    407   PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
    408 
    409   // Consume the previously pushed token.
    410   ConsumeAnyToken();
    411   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
    412          && "Inline method not starting with '{', ':' or 'try'");
    413 
    414   // Parse the method body. Function body parsing code is similar enough
    415   // to be re-used for method bodies as well.
    416   ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
    417   Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
    418 
    419   if (Tok.is(tok::kw_try)) {
    420     ParseFunctionTryBlock(LM.D, FnScope);
    421     assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
    422                                                          Tok.getLocation()) &&
    423            "ParseFunctionTryBlock went over the cached tokens!");
    424     // There could be leftover tokens (e.g. because of an error).
    425     // Skip through until we reach the original token position.
    426     while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
    427       ConsumeAnyToken();
    428     return;
    429   }
    430   if (Tok.is(tok::colon)) {
    431     ParseConstructorInitializer(LM.D);
    432 
    433     // Error recovery.
    434     if (!Tok.is(tok::l_brace)) {
    435       FnScope.Exit();
    436       Actions.ActOnFinishFunctionBody(LM.D, 0);
    437       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
    438         ConsumeAnyToken();
    439       return;
    440     }
    441   } else
    442     Actions.ActOnDefaultCtorInitializers(LM.D);
    443 
    444   ParseFunctionStatementBody(LM.D, FnScope);
    445 
    446   // Clear the late-template-parsed bit if we set it before.
    447   if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false);
    448 
    449   if (Tok.getLocation() != origLoc) {
    450     // Due to parsing error, we either went over the cached tokens or
    451     // there are still cached tokens left. If it's the latter case skip the
    452     // leftover tokens.
    453     // Since this is an uncommon situation that should be avoided, use the
    454     // expensive isBeforeInTranslationUnit call.
    455     if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
    456                                                         origLoc))
    457       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
    458         ConsumeAnyToken();
    459   }
    460 }
    461 
    462 /// ParseLexedMemberInitializers - We finished parsing the member specification
    463 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
    464 /// initializers that were collected during its parsing and parse them all.
    465 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
    466   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
    467   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
    468                                 HasTemplateScope);
    469   if (HasTemplateScope)
    470     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
    471 
    472   // Set or update the scope flags.
    473   bool AlreadyHasClassScope = Class.TopLevelClass;
    474   unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
    475   ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
    476   ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
    477 
    478   if (!AlreadyHasClassScope)
    479     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
    480                                                 Class.TagOrTemplate);
    481 
    482   if (!Class.LateParsedDeclarations.empty()) {
    483     // C++11 [expr.prim.general]p4:
    484     //   Otherwise, if a member-declarator declares a non-static data member
    485     //  (9.2) of a class X, the expression this is a prvalue of type "pointer
    486     //  to X" within the optional brace-or-equal-initializer. It shall not
    487     //  appear elsewhere in the member-declarator.
    488     Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
    489                                      /*TypeQuals=*/(unsigned)0);
    490 
    491     for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
    492       Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
    493     }
    494   }
    495 
    496   if (!AlreadyHasClassScope)
    497     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
    498                                                  Class.TagOrTemplate);
    499 
    500   Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
    501 }
    502 
    503 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
    504   if (!MI.Field || MI.Field->isInvalidDecl())
    505     return;
    506 
    507   // Append the current token at the end of the new token stream so that it
    508   // doesn't get lost.
    509   MI.Toks.push_back(Tok);
    510   PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
    511 
    512   // Consume the previously pushed token.
    513   ConsumeAnyToken();
    514 
    515   SourceLocation EqualLoc;
    516 
    517   ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
    518                                               EqualLoc);
    519 
    520   Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
    521 
    522   // The next token should be our artificial terminating EOF token.
    523   if (Tok.isNot(tok::eof)) {
    524     SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
    525     if (!EndLoc.isValid())
    526       EndLoc = Tok.getLocation();
    527     // No fixit; we can't recover as if there were a semicolon here.
    528     Diag(EndLoc, diag::err_expected_semi_decl_list);
    529 
    530     // Consume tokens until we hit the artificial EOF.
    531     while (Tok.isNot(tok::eof))
    532       ConsumeAnyToken();
    533   }
    534   ConsumeAnyToken();
    535 }
    536 
    537 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
    538 /// container until the token 'T' is reached (which gets
    539 /// consumed/stored too, if ConsumeFinalToken).
    540 /// If StopAtSemi is true, then we will stop early at a ';' character.
    541 /// Returns true if token 'T1' or 'T2' was found.
    542 /// NOTE: This is a specialized version of Parser::SkipUntil.
    543 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
    544                                   CachedTokens &Toks,
    545                                   bool StopAtSemi, bool ConsumeFinalToken) {
    546   // We always want this function to consume at least one token if the first
    547   // token isn't T and if not at EOF.
    548   bool isFirstTokenConsumed = true;
    549   while (1) {
    550     // If we found one of the tokens, stop and return true.
    551     if (Tok.is(T1) || Tok.is(T2)) {
    552       if (ConsumeFinalToken) {
    553         Toks.push_back(Tok);
    554         ConsumeAnyToken();
    555       }
    556       return true;
    557     }
    558 
    559     switch (Tok.getKind()) {
    560     case tok::eof:
    561       // Ran out of tokens.
    562       return false;
    563 
    564     case tok::l_paren:
    565       // Recursively consume properly-nested parens.
    566       Toks.push_back(Tok);
    567       ConsumeParen();
    568       ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
    569       break;
    570     case tok::l_square:
    571       // Recursively consume properly-nested square brackets.
    572       Toks.push_back(Tok);
    573       ConsumeBracket();
    574       ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
    575       break;
    576     case tok::l_brace:
    577       // Recursively consume properly-nested braces.
    578       Toks.push_back(Tok);
    579       ConsumeBrace();
    580       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
    581       break;
    582 
    583     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
    584     // Since the user wasn't looking for this token (if they were, it would
    585     // already be handled), this isn't balanced.  If there is a LHS token at a
    586     // higher level, we will assume that this matches the unbalanced token
    587     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
    588     case tok::r_paren:
    589       if (ParenCount && !isFirstTokenConsumed)
    590         return false;  // Matches something.
    591       Toks.push_back(Tok);
    592       ConsumeParen();
    593       break;
    594     case tok::r_square:
    595       if (BracketCount && !isFirstTokenConsumed)
    596         return false;  // Matches something.
    597       Toks.push_back(Tok);
    598       ConsumeBracket();
    599       break;
    600     case tok::r_brace:
    601       if (BraceCount && !isFirstTokenConsumed)
    602         return false;  // Matches something.
    603       Toks.push_back(Tok);
    604       ConsumeBrace();
    605       break;
    606 
    607     case tok::code_completion:
    608       Toks.push_back(Tok);
    609       ConsumeCodeCompletionToken();
    610       break;
    611 
    612     case tok::string_literal:
    613     case tok::wide_string_literal:
    614     case tok::utf8_string_literal:
    615     case tok::utf16_string_literal:
    616     case tok::utf32_string_literal:
    617       Toks.push_back(Tok);
    618       ConsumeStringToken();
    619       break;
    620     case tok::semi:
    621       if (StopAtSemi)
    622         return false;
    623       // FALL THROUGH.
    624     default:
    625       // consume this token.
    626       Toks.push_back(Tok);
    627       ConsumeToken();
    628       break;
    629     }
    630     isFirstTokenConsumed = false;
    631   }
    632 }
    633 
    634 /// \brief Consume tokens and store them in the passed token container until
    635 /// we've passed the try keyword and constructor initializers and have consumed
    636 /// the opening brace of the function body. The opening brace will be consumed
    637 /// if and only if there was no error.
    638 ///
    639 /// \return True on error.
    640 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
    641   if (Tok.is(tok::kw_try)) {
    642     Toks.push_back(Tok);
    643     ConsumeToken();
    644   }
    645   bool ReadInitializer = false;
    646   if (Tok.is(tok::colon)) {
    647     // Initializers can contain braces too.
    648     Toks.push_back(Tok);
    649     ConsumeToken();
    650 
    651     while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
    652       if (Tok.is(tok::eof) || Tok.is(tok::semi))
    653         return Diag(Tok.getLocation(), diag::err_expected_lbrace);
    654 
    655       // Grab the identifier.
    656       if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
    657                                 /*StopAtSemi=*/true,
    658                                 /*ConsumeFinalToken=*/false))
    659         return Diag(Tok.getLocation(), diag::err_expected_lparen);
    660 
    661       tok::TokenKind kind = Tok.getKind();
    662       Toks.push_back(Tok);
    663       bool IsLParen = (kind == tok::l_paren);
    664       SourceLocation LOpen = Tok.getLocation();
    665 
    666       if (IsLParen) {
    667         ConsumeParen();
    668       } else {
    669         assert(kind == tok::l_brace && "Must be left paren or brace here.");
    670         ConsumeBrace();
    671         // In C++03, this has to be the start of the function body, which
    672         // means the initializer is malformed; we'll diagnose it later.
    673         if (!getLangOpts().CPlusPlus11)
    674           return false;
    675       }
    676 
    677       // Grab the initializer
    678       if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
    679                                 Toks, /*StopAtSemi=*/true)) {
    680         Diag(Tok, IsLParen ? diag::err_expected_rparen :
    681                              diag::err_expected_rbrace);
    682         Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
    683         return true;
    684       }
    685 
    686       // Grab pack ellipsis, if present
    687       if (Tok.is(tok::ellipsis)) {
    688         Toks.push_back(Tok);
    689         ConsumeToken();
    690       }
    691 
    692       // Grab the separating comma, if any.
    693       if (Tok.is(tok::comma)) {
    694         Toks.push_back(Tok);
    695         ConsumeToken();
    696       } else if (Tok.isNot(tok::l_brace)) {
    697         ReadInitializer = true;
    698         break;
    699       }
    700     }
    701   }
    702 
    703   // Grab any remaining garbage to be diagnosed later. We stop when we reach a
    704   // brace: an opening one is the function body, while a closing one probably
    705   // means we've reached the end of the class.
    706   ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
    707                        /*StopAtSemi=*/true,
    708                        /*ConsumeFinalToken=*/false);
    709   if (Tok.isNot(tok::l_brace)) {
    710     if (ReadInitializer)
    711       return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
    712     return Diag(Tok.getLocation(), diag::err_expected_lbrace);
    713   }
    714 
    715   Toks.push_back(Tok);
    716   ConsumeBrace();
    717   return false;
    718 }
    719