clang API Documentation

ParseCXXInlineMethods.cpp
Go to the documentation of this file.
00001 //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements parsing for C++ class inline methods.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Parse/Parser.h"
00015 #include "RAIIObjectsForParser.h"
00016 #include "clang/AST/DeclTemplate.h"
00017 #include "clang/Parse/ParseDiagnostic.h"
00018 #include "clang/Sema/DeclSpec.h"
00019 #include "clang/Sema/Scope.h"
00020 using namespace clang;
00021 
00022 /// Get the FunctionDecl for a function or function template decl.
00023 static FunctionDecl *getFunctionDecl(Decl *D) {
00024   if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D))
00025     return fn;
00026   return cast<FunctionTemplateDecl>(D)->getTemplatedDecl();
00027 }
00028 
00029 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
00030 /// Declarator is a well formed C++ inline method definition. Now lex its body
00031 /// and store its tokens for parsing after the C++ class is complete.
00032 NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
00033                                       AttributeList *AccessAttrs,
00034                                       ParsingDeclarator &D,
00035                                       const ParsedTemplateInfo &TemplateInfo,
00036                                       const VirtSpecifiers& VS, 
00037                                       FunctionDefinitionKind DefinitionKind,
00038                                       ExprResult& Init) {
00039   assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
00040   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
00041           Tok.is(tok::equal)) &&
00042          "Current token not a '{', ':', '=', or 'try'!");
00043 
00044   MultiTemplateParamsArg TemplateParams(
00045           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
00046           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
00047 
00048   NamedDecl *FnD;
00049   D.setFunctionDefinitionKind(DefinitionKind);
00050   if (D.getDeclSpec().isFriendSpecified())
00051     FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
00052                                           TemplateParams);
00053   else {
00054     FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
00055                                            TemplateParams, 0,
00056                                            VS, ICIS_NoInit);
00057     if (FnD) {
00058       Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
00059                                        false, true);
00060       bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
00061       if (Init.isUsable())
00062         Actions.AddInitializerToDecl(FnD, Init.get(), false, 
00063                                      TypeSpecContainsAuto);
00064       else
00065         Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
00066     }
00067   }
00068 
00069   HandleMemberFunctionDeclDelays(D, FnD);
00070 
00071   D.complete(FnD);
00072 
00073   if (Tok.is(tok::equal)) {
00074     ConsumeToken();
00075 
00076     if (!FnD) {
00077       SkipUntil(tok::semi);
00078       return 0;
00079     }
00080 
00081     bool Delete = false;
00082     SourceLocation KWLoc;
00083     if (Tok.is(tok::kw_delete)) {
00084       Diag(Tok, getLangOpts().CPlusPlus11 ?
00085            diag::warn_cxx98_compat_deleted_function :
00086            diag::ext_deleted_function);
00087 
00088       KWLoc = ConsumeToken();
00089       Actions.SetDeclDeleted(FnD, KWLoc);
00090       Delete = true;
00091     } else if (Tok.is(tok::kw_default)) {
00092       Diag(Tok, getLangOpts().CPlusPlus11 ?
00093            diag::warn_cxx98_compat_defaulted_function :
00094            diag::ext_defaulted_function);
00095 
00096       KWLoc = ConsumeToken();
00097       Actions.SetDeclDefaulted(FnD, KWLoc);
00098     } else {
00099       llvm_unreachable("function definition after = not 'delete' or 'default'");
00100     }
00101 
00102     if (Tok.is(tok::comma)) {
00103       Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
00104         << Delete;
00105       SkipUntil(tok::semi);
00106     } else {
00107       ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
00108                        Delete ? "delete" : "default", tok::semi);
00109     }
00110 
00111     return FnD;
00112   }
00113 
00114   // In delayed template parsing mode, if we are within a class template
00115   // or if we are about to parse function member template then consume
00116   // the tokens and store them for parsing at the end of the translation unit.
00117   if (getLangOpts().DelayedTemplateParsing && 
00118       DefinitionKind == FDK_Definition && 
00119       ((Actions.CurContext->isDependentContext() ||
00120         TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && 
00121         !Actions.IsInsideALocalClassWithinATemplateFunction())) {
00122 
00123     if (FnD) {
00124       LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
00125 
00126       FunctionDecl *FD = getFunctionDecl(FnD);
00127       Actions.CheckForFunctionRedefinition(FD);
00128 
00129       LateParsedTemplateMap[FD] = LPT;
00130       Actions.MarkAsLateParsedTemplate(FD);
00131       LexTemplateFunctionForLateParsing(LPT->Toks);
00132     } else {
00133       CachedTokens Toks;
00134       LexTemplateFunctionForLateParsing(Toks);
00135     }
00136 
00137     return FnD;
00138   }
00139 
00140   // Consume the tokens and store them for later parsing.
00141 
00142   LexedMethod* LM = new LexedMethod(this, FnD);
00143   getCurrentClass().LateParsedDeclarations.push_back(LM);
00144   LM->TemplateScope = getCurScope()->isTemplateParamScope();
00145   CachedTokens &Toks = LM->Toks;
00146 
00147   tok::TokenKind kind = Tok.getKind();
00148   // Consume everything up to (and including) the left brace of the
00149   // function body.
00150   if (ConsumeAndStoreFunctionPrologue(Toks)) {
00151     // We didn't find the left-brace we expected after the
00152     // constructor initializer; we already printed an error, and it's likely
00153     // impossible to recover, so don't try to parse this method later.
00154     // If we stopped at a semicolon, consume it to avoid an extra warning.
00155      if (Tok.is(tok::semi))
00156       ConsumeToken();
00157     delete getCurrentClass().LateParsedDeclarations.back();
00158     getCurrentClass().LateParsedDeclarations.pop_back();
00159     return FnD;
00160   } else {
00161     // Consume everything up to (and including) the matching right brace.
00162     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00163   }
00164 
00165   // If we're in a function-try-block, we need to store all the catch blocks.
00166   if (kind == tok::kw_try) {
00167     while (Tok.is(tok::kw_catch)) {
00168       ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
00169       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00170     }
00171   }
00172 
00173 
00174   if (!FnD) {
00175     // If semantic analysis could not build a function declaration,
00176     // just throw away the late-parsed declaration.
00177     delete getCurrentClass().LateParsedDeclarations.back();
00178     getCurrentClass().LateParsedDeclarations.pop_back();
00179   }
00180 
00181   // If this is a friend function, mark that it's late-parsed so that
00182   // it's still known to be a definition even before we attach the
00183   // parsed body.  Sema needs to treat friend function definitions
00184   // differently during template instantiation, and it's possible for
00185   // the containing class to be instantiated before all its member
00186   // function definitions are parsed.
00187   //
00188   // If you remove this, you can remove the code that clears the flag
00189   // after parsing the member.
00190   if (D.getDeclSpec().isFriendSpecified()) {
00191     getFunctionDecl(FnD)->setLateTemplateParsed(true);
00192   }
00193 
00194   return FnD;
00195 }
00196 
00197 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
00198 /// specified Declarator is a well formed C++ non-static data member
00199 /// declaration. Now lex its initializer and store its tokens for parsing
00200 /// after the class is complete.
00201 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
00202   assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
00203          "Current token not a '{' or '='!");
00204 
00205   LateParsedMemberInitializer *MI =
00206     new LateParsedMemberInitializer(this, VarD);
00207   getCurrentClass().LateParsedDeclarations.push_back(MI);
00208   CachedTokens &Toks = MI->Toks;
00209 
00210   tok::TokenKind kind = Tok.getKind();
00211   if (kind == tok::equal) {
00212     Toks.push_back(Tok);
00213     ConsumeToken();
00214   }
00215 
00216   if (kind == tok::l_brace) {
00217     // Begin by storing the '{' token.
00218     Toks.push_back(Tok);
00219     ConsumeBrace();
00220 
00221     // Consume everything up to (and including) the matching right brace.
00222     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
00223   } else {
00224     // Consume everything up to (but excluding) the comma or semicolon.
00225     ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
00226                          /*ConsumeFinalToken=*/false);
00227   }
00228 
00229   // Store an artificial EOF token to ensure that we don't run off the end of
00230   // the initializer when we come to parse it.
00231   Token Eof;
00232   Eof.startToken();
00233   Eof.setKind(tok::eof);
00234   Eof.setLocation(Tok.getLocation());
00235   Toks.push_back(Eof);
00236 }
00237 
00238 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
00239 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
00240 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
00241 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
00242 
00243 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
00244   : Self(P), Class(C) {}
00245 
00246 Parser::LateParsedClass::~LateParsedClass() {
00247   Self->DeallocateParsedClasses(Class);
00248 }
00249 
00250 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
00251   Self->ParseLexedMethodDeclarations(*Class);
00252 }
00253 
00254 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
00255   Self->ParseLexedMemberInitializers(*Class);
00256 }
00257 
00258 void Parser::LateParsedClass::ParseLexedMethodDefs() {
00259   Self->ParseLexedMethodDefs(*Class);
00260 }
00261 
00262 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
00263   Self->ParseLexedMethodDeclaration(*this);
00264 }
00265 
00266 void Parser::LexedMethod::ParseLexedMethodDefs() {
00267   Self->ParseLexedMethodDef(*this);
00268 }
00269 
00270 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
00271   Self->ParseLexedMemberInitializer(*this);
00272 }
00273 
00274 /// ParseLexedMethodDeclarations - We finished parsing the member
00275 /// specification of a top (non-nested) C++ class. Now go over the
00276 /// stack of method declarations with some parts for which parsing was
00277 /// delayed (such as default arguments) and parse them.
00278 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
00279   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00280   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
00281   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00282   if (HasTemplateScope) {
00283     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00284     ++CurTemplateDepthTracker;
00285   }
00286 
00287   // The current scope is still active if we're the top-level class.
00288   // Otherwise we'll need to push and enter a new scope.
00289   bool HasClassScope = !Class.TopLevelClass;
00290   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
00291                         HasClassScope);
00292   if (HasClassScope)
00293     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
00294 
00295   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00296     Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
00297   }
00298 
00299   if (HasClassScope)
00300     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
00301 }
00302 
00303 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
00304   // If this is a member template, introduce the template parameter scope.
00305   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
00306   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00307   if (LM.TemplateScope) {
00308     Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
00309     ++CurTemplateDepthTracker;
00310   }
00311   // Start the delayed C++ method declaration
00312   Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
00313 
00314   // Introduce the parameters into scope and parse their default
00315   // arguments.
00316   ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
00317                             Scope::FunctionDeclarationScope | Scope::DeclScope);
00318   for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
00319     // Introduce the parameter into scope.
00320     Actions.ActOnDelayedCXXMethodParameter(getCurScope(), 
00321                                            LM.DefaultArgs[I].Param);
00322 
00323     if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
00324       // Save the current token position.
00325       SourceLocation origLoc = Tok.getLocation();
00326 
00327       // Parse the default argument from its saved token stream.
00328       Toks->push_back(Tok); // So that the current token doesn't get lost
00329       PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
00330 
00331       // Consume the previously-pushed token.
00332       ConsumeAnyToken();
00333 
00334       // Consume the '='.
00335       assert(Tok.is(tok::equal) && "Default argument not starting with '='");
00336       SourceLocation EqualLoc = ConsumeToken();
00337 
00338       // The argument isn't actually potentially evaluated unless it is
00339       // used.
00340       EnterExpressionEvaluationContext Eval(Actions,
00341                                             Sema::PotentiallyEvaluatedIfUsed,
00342                                             LM.DefaultArgs[I].Param);
00343 
00344       ExprResult DefArgResult;
00345       if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
00346         Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
00347         DefArgResult = ParseBraceInitializer();
00348       } else
00349         DefArgResult = ParseAssignmentExpression();
00350       if (DefArgResult.isInvalid())
00351         Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
00352       else {
00353         if (Tok.is(tok::cxx_defaultarg_end))
00354           ConsumeToken();
00355         else
00356           Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
00357         Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
00358                                           DefArgResult.take());
00359       }
00360 
00361       assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
00362                                                          Tok.getLocation()) &&
00363              "ParseAssignmentExpression went over the default arg tokens!");
00364       // There could be leftover tokens (e.g. because of an error).
00365       // Skip through until we reach the original token position.
00366       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00367         ConsumeAnyToken();
00368 
00369       delete Toks;
00370       LM.DefaultArgs[I].Toks = 0;
00371     }
00372   }
00373 
00374   PrototypeScope.Exit();
00375 
00376   // Finish the delayed C++ method declaration.
00377   Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
00378 }
00379 
00380 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
00381 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
00382 /// collected during its parsing and parse them all.
00383 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
00384   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00385   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
00386   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00387   if (HasTemplateScope) {
00388     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00389     ++CurTemplateDepthTracker;
00390   }
00391   bool HasClassScope = !Class.TopLevelClass;
00392   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
00393                         HasClassScope);
00394 
00395   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00396     Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
00397   }
00398 }
00399 
00400 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
00401   // If this is a member template, introduce the template parameter scope.
00402   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
00403   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00404   if (LM.TemplateScope) {
00405     Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
00406     ++CurTemplateDepthTracker;
00407   }
00408   // Save the current token position.
00409   SourceLocation origLoc = Tok.getLocation();
00410 
00411   assert(!LM.Toks.empty() && "Empty body!");
00412   // Append the current token at the end of the new token stream so that it
00413   // doesn't get lost.
00414   LM.Toks.push_back(Tok);
00415   PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
00416 
00417   // Consume the previously pushed token.
00418   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
00419   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
00420          && "Inline method not starting with '{', ':' or 'try'");
00421 
00422   // Parse the method body. Function body parsing code is similar enough
00423   // to be re-used for method bodies as well.
00424   ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
00425   Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
00426 
00427   if (Tok.is(tok::kw_try)) {
00428     ParseFunctionTryBlock(LM.D, FnScope);
00429     assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
00430                                                          Tok.getLocation()) &&
00431            "ParseFunctionTryBlock went over the cached tokens!");
00432     // There could be leftover tokens (e.g. because of an error).
00433     // Skip through until we reach the original token position.
00434     while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00435       ConsumeAnyToken();
00436     return;
00437   }
00438   if (Tok.is(tok::colon)) {
00439     ParseConstructorInitializer(LM.D);
00440 
00441     // Error recovery.
00442     if (!Tok.is(tok::l_brace)) {
00443       FnScope.Exit();
00444       Actions.ActOnFinishFunctionBody(LM.D, 0);
00445       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00446         ConsumeAnyToken();
00447       return;
00448     }
00449   } else
00450     Actions.ActOnDefaultCtorInitializers(LM.D);
00451 
00452   assert((Actions.getDiagnostics().hasErrorOccurred() ||
00453           !isa<FunctionTemplateDecl>(LM.D) ||
00454           cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
00455             < TemplateParameterDepth) &&
00456          "TemplateParameterDepth should be greater than the depth of "
00457          "current template being instantiated!");
00458 
00459   ParseFunctionStatementBody(LM.D, FnScope);
00460 
00461   // Clear the late-template-parsed bit if we set it before.
00462   if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false);
00463 
00464   if (Tok.getLocation() != origLoc) {
00465     // Due to parsing error, we either went over the cached tokens or
00466     // there are still cached tokens left. If it's the latter case skip the
00467     // leftover tokens.
00468     // Since this is an uncommon situation that should be avoided, use the
00469     // expensive isBeforeInTranslationUnit call.
00470     if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
00471                                                         origLoc))
00472       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00473         ConsumeAnyToken();
00474   }
00475 }
00476 
00477 /// ParseLexedMemberInitializers - We finished parsing the member specification
00478 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
00479 /// initializers that were collected during its parsing and parse them all.
00480 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
00481   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00482   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
00483                                 HasTemplateScope);
00484   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00485   if (HasTemplateScope) {
00486     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00487     ++CurTemplateDepthTracker;
00488   }
00489   // Set or update the scope flags.
00490   bool AlreadyHasClassScope = Class.TopLevelClass;
00491   unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
00492   ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
00493   ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
00494 
00495   if (!AlreadyHasClassScope)
00496     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
00497                                                 Class.TagOrTemplate);
00498 
00499   if (!Class.LateParsedDeclarations.empty()) {
00500     // C++11 [expr.prim.general]p4:
00501     //   Otherwise, if a member-declarator declares a non-static data member 
00502     //  (9.2) of a class X, the expression this is a prvalue of type "pointer
00503     //  to X" within the optional brace-or-equal-initializer. It shall not 
00504     //  appear elsewhere in the member-declarator.
00505     Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
00506                                      /*TypeQuals=*/(unsigned)0);
00507 
00508     for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00509       Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
00510     }
00511   }
00512   
00513   if (!AlreadyHasClassScope)
00514     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
00515                                                  Class.TagOrTemplate);
00516 
00517   Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
00518 }
00519 
00520 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
00521   if (!MI.Field || MI.Field->isInvalidDecl())
00522     return;
00523 
00524   // Append the current token at the end of the new token stream so that it
00525   // doesn't get lost.
00526   MI.Toks.push_back(Tok);
00527   PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
00528 
00529   // Consume the previously pushed token.
00530   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
00531 
00532   SourceLocation EqualLoc;
00533 
00534   ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 
00535                                               EqualLoc);
00536 
00537   Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
00538 
00539   // The next token should be our artificial terminating EOF token.
00540   if (Tok.isNot(tok::eof)) {
00541     SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
00542     if (!EndLoc.isValid())
00543       EndLoc = Tok.getLocation();
00544     // No fixit; we can't recover as if there were a semicolon here.
00545     Diag(EndLoc, diag::err_expected_semi_decl_list);
00546 
00547     // Consume tokens until we hit the artificial EOF.
00548     while (Tok.isNot(tok::eof))
00549       ConsumeAnyToken();
00550   }
00551   ConsumeAnyToken();
00552 }
00553 
00554 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
00555 /// container until the token 'T' is reached (which gets
00556 /// consumed/stored too, if ConsumeFinalToken).
00557 /// If StopAtSemi is true, then we will stop early at a ';' character.
00558 /// Returns true if token 'T1' or 'T2' was found.
00559 /// NOTE: This is a specialized version of Parser::SkipUntil.
00560 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
00561                                   CachedTokens &Toks,
00562                                   bool StopAtSemi, bool ConsumeFinalToken) {
00563   // We always want this function to consume at least one token if the first
00564   // token isn't T and if not at EOF.
00565   bool isFirstTokenConsumed = true;
00566   while (1) {
00567     // If we found one of the tokens, stop and return true.
00568     if (Tok.is(T1) || Tok.is(T2)) {
00569       if (ConsumeFinalToken) {
00570         Toks.push_back(Tok);
00571         ConsumeAnyToken();
00572       }
00573       return true;
00574     }
00575 
00576     switch (Tok.getKind()) {
00577     case tok::eof:
00578       // Ran out of tokens.
00579       return false;
00580 
00581     case tok::l_paren:
00582       // Recursively consume properly-nested parens.
00583       Toks.push_back(Tok);
00584       ConsumeParen();
00585       ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
00586       break;
00587     case tok::l_square:
00588       // Recursively consume properly-nested square brackets.
00589       Toks.push_back(Tok);
00590       ConsumeBracket();
00591       ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
00592       break;
00593     case tok::l_brace:
00594       // Recursively consume properly-nested braces.
00595       Toks.push_back(Tok);
00596       ConsumeBrace();
00597       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00598       break;
00599 
00600     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
00601     // Since the user wasn't looking for this token (if they were, it would
00602     // already be handled), this isn't balanced.  If there is a LHS token at a
00603     // higher level, we will assume that this matches the unbalanced token
00604     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
00605     case tok::r_paren:
00606       if (ParenCount && !isFirstTokenConsumed)
00607         return false;  // Matches something.
00608       Toks.push_back(Tok);
00609       ConsumeParen();
00610       break;
00611     case tok::r_square:
00612       if (BracketCount && !isFirstTokenConsumed)
00613         return false;  // Matches something.
00614       Toks.push_back(Tok);
00615       ConsumeBracket();
00616       break;
00617     case tok::r_brace:
00618       if (BraceCount && !isFirstTokenConsumed)
00619         return false;  // Matches something.
00620       Toks.push_back(Tok);
00621       ConsumeBrace();
00622       break;
00623 
00624     case tok::code_completion:
00625       Toks.push_back(Tok);
00626       ConsumeCodeCompletionToken();
00627       break;
00628 
00629     case tok::string_literal:
00630     case tok::wide_string_literal:
00631     case tok::utf8_string_literal:
00632     case tok::utf16_string_literal:
00633     case tok::utf32_string_literal:
00634       Toks.push_back(Tok);
00635       ConsumeStringToken();
00636       break;
00637     case tok::semi:
00638       if (StopAtSemi)
00639         return false;
00640       // FALL THROUGH.
00641     default:
00642       // consume this token.
00643       Toks.push_back(Tok);
00644       ConsumeToken();
00645       break;
00646     }
00647     isFirstTokenConsumed = false;
00648   }
00649 }
00650 
00651 /// \brief Consume tokens and store them in the passed token container until
00652 /// we've passed the try keyword and constructor initializers and have consumed
00653 /// the opening brace of the function body. The opening brace will be consumed
00654 /// if and only if there was no error.
00655 ///
00656 /// \return True on error. 
00657 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
00658   if (Tok.is(tok::kw_try)) {
00659     Toks.push_back(Tok);
00660     ConsumeToken();
00661   }
00662   bool ReadInitializer = false;
00663   if (Tok.is(tok::colon)) {
00664     // Initializers can contain braces too.
00665     Toks.push_back(Tok);
00666     ConsumeToken();
00667 
00668     while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
00669       if (Tok.is(tok::eof) || Tok.is(tok::semi))
00670         return Diag(Tok.getLocation(), diag::err_expected_lbrace);
00671 
00672       // Grab the identifier.
00673       if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
00674                                 /*StopAtSemi=*/true,
00675                                 /*ConsumeFinalToken=*/false))
00676         return Diag(Tok.getLocation(), diag::err_expected_lparen);
00677 
00678       tok::TokenKind kind = Tok.getKind();
00679       Toks.push_back(Tok);
00680       bool IsLParen = (kind == tok::l_paren);
00681       SourceLocation LOpen = Tok.getLocation();
00682 
00683       if (IsLParen) {
00684         ConsumeParen();
00685       } else {
00686         assert(kind == tok::l_brace && "Must be left paren or brace here.");
00687         ConsumeBrace();
00688         // In C++03, this has to be the start of the function body, which
00689         // means the initializer is malformed; we'll diagnose it later.
00690         if (!getLangOpts().CPlusPlus11)
00691           return false;
00692       }
00693 
00694       // Grab the initializer
00695       if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
00696                                 Toks, /*StopAtSemi=*/true)) {
00697         Diag(Tok, IsLParen ? diag::err_expected_rparen :
00698                              diag::err_expected_rbrace);
00699         Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
00700         return true;
00701       }
00702 
00703       // Grab pack ellipsis, if present
00704       if (Tok.is(tok::ellipsis)) {
00705         Toks.push_back(Tok);
00706         ConsumeToken();
00707       }
00708 
00709       // Grab the separating comma, if any.
00710       if (Tok.is(tok::comma)) {
00711         Toks.push_back(Tok);
00712         ConsumeToken();
00713       } else if (Tok.isNot(tok::l_brace)) {
00714         ReadInitializer = true;
00715         break;
00716       }
00717     }
00718   }
00719 
00720   // Grab any remaining garbage to be diagnosed later. We stop when we reach a
00721   // brace: an opening one is the function body, while a closing one probably
00722   // means we've reached the end of the class.
00723   ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
00724                        /*StopAtSemi=*/true,
00725                        /*ConsumeFinalToken=*/false);
00726   if (Tok.isNot(tok::l_brace)) {
00727     if (ReadInitializer)
00728       return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
00729     return Diag(Tok.getLocation(), diag::err_expected_lbrace);
00730   }
00731 
00732   Toks.push_back(Tok);
00733   ConsumeBrace();
00734   return false;
00735 }