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 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
00023 /// Declarator is a well formed C++ inline method definition. Now lex its body
00024 /// and store its tokens for parsing after the C++ class is complete.
00025 NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
00026                                       AttributeList *AccessAttrs,
00027                                       ParsingDeclarator &D,
00028                                       const ParsedTemplateInfo &TemplateInfo,
00029                                       const VirtSpecifiers& VS, 
00030                                       FunctionDefinitionKind DefinitionKind,
00031                                       ExprResult& Init) {
00032   assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
00033   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
00034           Tok.is(tok::equal)) &&
00035          "Current token not a '{', ':', '=', or 'try'!");
00036 
00037   MultiTemplateParamsArg TemplateParams(
00038           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
00039           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
00040 
00041   NamedDecl *FnD;
00042   D.setFunctionDefinitionKind(DefinitionKind);
00043   if (D.getDeclSpec().isFriendSpecified())
00044     FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
00045                                           TemplateParams);
00046   else {
00047     FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
00048                                            TemplateParams, 0,
00049                                            VS, ICIS_NoInit);
00050     if (FnD) {
00051       Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
00052       bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
00053       if (Init.isUsable())
00054         Actions.AddInitializerToDecl(FnD, Init.get(), false,
00055                                      TypeSpecContainsAuto);
00056       else
00057         Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
00058     }
00059   }
00060 
00061   HandleMemberFunctionDeclDelays(D, FnD);
00062 
00063   D.complete(FnD);
00064 
00065   if (TryConsumeToken(tok::equal)) {
00066     if (!FnD) {
00067       SkipUntil(tok::semi);
00068       return 0;
00069     }
00070 
00071     bool Delete = false;
00072     SourceLocation KWLoc;
00073     if (TryConsumeToken(tok::kw_delete, KWLoc)) {
00074       Diag(KWLoc, getLangOpts().CPlusPlus11
00075                       ? diag::warn_cxx98_compat_deleted_function
00076                       : diag::ext_deleted_function);
00077       Actions.SetDeclDeleted(FnD, KWLoc);
00078       Delete = true;
00079     } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
00080       Diag(KWLoc, getLangOpts().CPlusPlus11
00081                       ? diag::warn_cxx98_compat_defaulted_function
00082                       : diag::ext_defaulted_function);
00083       Actions.SetDeclDefaulted(FnD, KWLoc);
00084     } else {
00085       llvm_unreachable("function definition after = not 'delete' or 'default'");
00086     }
00087 
00088     if (Tok.is(tok::comma)) {
00089       Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
00090         << Delete;
00091       SkipUntil(tok::semi);
00092     } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
00093                                 Delete ? "delete" : "default")) {
00094       SkipUntil(tok::semi);
00095     }
00096 
00097     return FnD;
00098   }
00099   
00100   // In delayed template parsing mode, if we are within a class template
00101   // or if we are about to parse function member template then consume
00102   // the tokens and store them for parsing at the end of the translation unit.
00103   if (getLangOpts().DelayedTemplateParsing &&
00104       DefinitionKind == FDK_Definition &&
00105       !D.getDeclSpec().isConstexprSpecified() &&
00106       !(FnD && FnD->getAsFunction() &&
00107         FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
00108       ((Actions.CurContext->isDependentContext() ||
00109         (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
00110          TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
00111        !Actions.IsInsideALocalClassWithinATemplateFunction())) {
00112 
00113     CachedTokens Toks;
00114     LexTemplateFunctionForLateParsing(Toks);
00115 
00116     if (FnD) {
00117       FunctionDecl *FD = FnD->getAsFunction();
00118       Actions.CheckForFunctionRedefinition(FD);
00119       Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
00120     }
00121 
00122     return FnD;
00123   }
00124 
00125   // Consume the tokens and store them for later parsing.
00126 
00127   LexedMethod* LM = new LexedMethod(this, FnD);
00128   getCurrentClass().LateParsedDeclarations.push_back(LM);
00129   LM->TemplateScope = getCurScope()->isTemplateParamScope();
00130   CachedTokens &Toks = LM->Toks;
00131 
00132   tok::TokenKind kind = Tok.getKind();
00133   // Consume everything up to (and including) the left brace of the
00134   // function body.
00135   if (ConsumeAndStoreFunctionPrologue(Toks)) {
00136     // We didn't find the left-brace we expected after the
00137     // constructor initializer; we already printed an error, and it's likely
00138     // impossible to recover, so don't try to parse this method later.
00139     // Skip over the rest of the decl and back to somewhere that looks
00140     // reasonable.
00141     SkipMalformedDecl();
00142     delete getCurrentClass().LateParsedDeclarations.back();
00143     getCurrentClass().LateParsedDeclarations.pop_back();
00144     return FnD;
00145   } else {
00146     // Consume everything up to (and including) the matching right brace.
00147     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00148   }
00149 
00150   // If we're in a function-try-block, we need to store all the catch blocks.
00151   if (kind == tok::kw_try) {
00152     while (Tok.is(tok::kw_catch)) {
00153       ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
00154       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00155     }
00156   }
00157 
00158   if (FnD) {
00159     // If this is a friend function, mark that it's late-parsed so that
00160     // it's still known to be a definition even before we attach the
00161     // parsed body.  Sema needs to treat friend function definitions
00162     // differently during template instantiation, and it's possible for
00163     // the containing class to be instantiated before all its member
00164     // function definitions are parsed.
00165     //
00166     // If you remove this, you can remove the code that clears the flag
00167     // after parsing the member.
00168     if (D.getDeclSpec().isFriendSpecified()) {
00169       FunctionDecl *FD = FnD->getAsFunction();
00170       Actions.CheckForFunctionRedefinition(FD);
00171       FD->setLateTemplateParsed(true);
00172     }
00173   } else {
00174     // If semantic analysis could not build a function declaration,
00175     // just throw away the late-parsed declaration.
00176     delete getCurrentClass().LateParsedDeclarations.back();
00177     getCurrentClass().LateParsedDeclarations.pop_back();
00178   }
00179 
00180   return FnD;
00181 }
00182 
00183 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
00184 /// specified Declarator is a well formed C++ non-static data member
00185 /// declaration. Now lex its initializer and store its tokens for parsing
00186 /// after the class is complete.
00187 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
00188   assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
00189          "Current token not a '{' or '='!");
00190 
00191   LateParsedMemberInitializer *MI =
00192     new LateParsedMemberInitializer(this, VarD);
00193   getCurrentClass().LateParsedDeclarations.push_back(MI);
00194   CachedTokens &Toks = MI->Toks;
00195 
00196   tok::TokenKind kind = Tok.getKind();
00197   if (kind == tok::equal) {
00198     Toks.push_back(Tok);
00199     ConsumeToken();
00200   }
00201 
00202   if (kind == tok::l_brace) {
00203     // Begin by storing the '{' token.
00204     Toks.push_back(Tok);
00205     ConsumeBrace();
00206 
00207     // Consume everything up to (and including) the matching right brace.
00208     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
00209   } else {
00210     // Consume everything up to (but excluding) the comma or semicolon.
00211     ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
00212   }
00213 
00214   // Store an artificial EOF token to ensure that we don't run off the end of
00215   // the initializer when we come to parse it.
00216   Token Eof;
00217   Eof.startToken();
00218   Eof.setKind(tok::eof);
00219   Eof.setLocation(Tok.getLocation());
00220   Toks.push_back(Eof);
00221 }
00222 
00223 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
00224 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
00225 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
00226 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
00227 
00228 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
00229   : Self(P), Class(C) {}
00230 
00231 Parser::LateParsedClass::~LateParsedClass() {
00232   Self->DeallocateParsedClasses(Class);
00233 }
00234 
00235 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
00236   Self->ParseLexedMethodDeclarations(*Class);
00237 }
00238 
00239 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
00240   Self->ParseLexedMemberInitializers(*Class);
00241 }
00242 
00243 void Parser::LateParsedClass::ParseLexedMethodDefs() {
00244   Self->ParseLexedMethodDefs(*Class);
00245 }
00246 
00247 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
00248   Self->ParseLexedMethodDeclaration(*this);
00249 }
00250 
00251 void Parser::LexedMethod::ParseLexedMethodDefs() {
00252   Self->ParseLexedMethodDef(*this);
00253 }
00254 
00255 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
00256   Self->ParseLexedMemberInitializer(*this);
00257 }
00258 
00259 /// ParseLexedMethodDeclarations - We finished parsing the member
00260 /// specification of a top (non-nested) C++ class. Now go over the
00261 /// stack of method declarations with some parts for which parsing was
00262 /// delayed (such as default arguments) and parse them.
00263 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
00264   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00265   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
00266   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00267   if (HasTemplateScope) {
00268     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00269     ++CurTemplateDepthTracker;
00270   }
00271 
00272   // The current scope is still active if we're the top-level class.
00273   // Otherwise we'll need to push and enter a new scope.
00274   bool HasClassScope = !Class.TopLevelClass;
00275   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
00276                         HasClassScope);
00277   if (HasClassScope)
00278     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
00279 
00280   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00281     Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
00282   }
00283 
00284   if (HasClassScope)
00285     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
00286 }
00287 
00288 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
00289   // If this is a member template, introduce the template parameter scope.
00290   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
00291   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00292   if (LM.TemplateScope) {
00293     Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
00294     ++CurTemplateDepthTracker;
00295   }
00296   // Start the delayed C++ method declaration
00297   Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
00298 
00299   // Introduce the parameters into scope and parse their default
00300   // arguments.
00301   ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
00302                             Scope::FunctionDeclarationScope | Scope::DeclScope);
00303   for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
00304     // Introduce the parameter into scope.
00305     Actions.ActOnDelayedCXXMethodParameter(getCurScope(), 
00306                                            LM.DefaultArgs[I].Param);
00307 
00308     if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
00309       // Save the current token position.
00310       SourceLocation origLoc = Tok.getLocation();
00311 
00312       // Parse the default argument from its saved token stream.
00313       Toks->push_back(Tok); // So that the current token doesn't get lost
00314       PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
00315 
00316       // Consume the previously-pushed token.
00317       ConsumeAnyToken();
00318 
00319       // Consume the '='.
00320       assert(Tok.is(tok::equal) && "Default argument not starting with '='");
00321       SourceLocation EqualLoc = ConsumeToken();
00322 
00323       // The argument isn't actually potentially evaluated unless it is
00324       // used.
00325       EnterExpressionEvaluationContext Eval(Actions,
00326                                             Sema::PotentiallyEvaluatedIfUsed,
00327                                             LM.DefaultArgs[I].Param);
00328 
00329       ExprResult DefArgResult;
00330       if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
00331         Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
00332         DefArgResult = ParseBraceInitializer();
00333       } else
00334         DefArgResult = ParseAssignmentExpression();
00335       if (DefArgResult.isInvalid())
00336         Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
00337       else {
00338         if (!TryConsumeToken(tok::cxx_defaultarg_end)) {
00339           // The last two tokens are the terminator and the saved value of
00340           // Tok; the last token in the default argument is the one before
00341           // those.
00342           assert(Toks->size() >= 3 && "expected a token in default arg");
00343           Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
00344             << SourceRange(Tok.getLocation(),
00345                            (*Toks)[Toks->size() - 3].getLocation());
00346         }
00347         Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
00348                                           DefArgResult.take());
00349       }
00350 
00351       assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
00352                                                          Tok.getLocation()) &&
00353              "ParseAssignmentExpression went over the default arg tokens!");
00354       // There could be leftover tokens (e.g. because of an error).
00355       // Skip through until we reach the original token position.
00356       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00357         ConsumeAnyToken();
00358 
00359       delete Toks;
00360       LM.DefaultArgs[I].Toks = 0;
00361     }
00362   }
00363 
00364   PrototypeScope.Exit();
00365 
00366   // Finish the delayed C++ method declaration.
00367   Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
00368 }
00369 
00370 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
00371 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
00372 /// collected during its parsing and parse them all.
00373 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
00374   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00375   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
00376   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00377   if (HasTemplateScope) {
00378     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00379     ++CurTemplateDepthTracker;
00380   }
00381   bool HasClassScope = !Class.TopLevelClass;
00382   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
00383                         HasClassScope);
00384 
00385   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00386     Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
00387   }
00388 }
00389 
00390 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
00391   // If this is a member template, introduce the template parameter scope.
00392   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
00393   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00394   if (LM.TemplateScope) {
00395     Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
00396     ++CurTemplateDepthTracker;
00397   }
00398   // Save the current token position.
00399   SourceLocation origLoc = Tok.getLocation();
00400 
00401   assert(!LM.Toks.empty() && "Empty body!");
00402   // Append the current token at the end of the new token stream so that it
00403   // doesn't get lost.
00404   LM.Toks.push_back(Tok);
00405   PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
00406 
00407   // Consume the previously pushed token.
00408   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
00409   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
00410          && "Inline method not starting with '{', ':' or 'try'");
00411 
00412   // Parse the method body. Function body parsing code is similar enough
00413   // to be re-used for method bodies as well.
00414   ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
00415   Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
00416 
00417   if (Tok.is(tok::kw_try)) {
00418     ParseFunctionTryBlock(LM.D, FnScope);
00419     assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
00420                                                          Tok.getLocation()) &&
00421            "ParseFunctionTryBlock went over the cached tokens!");
00422     // There could be leftover tokens (e.g. because of an error).
00423     // Skip through until we reach the original token position.
00424     while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00425       ConsumeAnyToken();
00426     return;
00427   }
00428   if (Tok.is(tok::colon)) {
00429     ParseConstructorInitializer(LM.D);
00430 
00431     // Error recovery.
00432     if (!Tok.is(tok::l_brace)) {
00433       FnScope.Exit();
00434       Actions.ActOnFinishFunctionBody(LM.D, 0);
00435       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00436         ConsumeAnyToken();
00437       return;
00438     }
00439   } else
00440     Actions.ActOnDefaultCtorInitializers(LM.D);
00441 
00442   assert((Actions.getDiagnostics().hasErrorOccurred() ||
00443           !isa<FunctionTemplateDecl>(LM.D) ||
00444           cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
00445             < TemplateParameterDepth) &&
00446          "TemplateParameterDepth should be greater than the depth of "
00447          "current template being instantiated!");
00448 
00449   ParseFunctionStatementBody(LM.D, FnScope);
00450 
00451   // Clear the late-template-parsed bit if we set it before.
00452   if (LM.D)
00453     LM.D->getAsFunction()->setLateTemplateParsed(false);
00454 
00455   if (Tok.getLocation() != origLoc) {
00456     // Due to parsing error, we either went over the cached tokens or
00457     // there are still cached tokens left. If it's the latter case skip the
00458     // leftover tokens.
00459     // Since this is an uncommon situation that should be avoided, use the
00460     // expensive isBeforeInTranslationUnit call.
00461     if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
00462                                                         origLoc))
00463       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
00464         ConsumeAnyToken();
00465   }
00466 }
00467 
00468 /// ParseLexedMemberInitializers - We finished parsing the member specification
00469 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
00470 /// initializers that were collected during its parsing and parse them all.
00471 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
00472   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
00473   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
00474                                 HasTemplateScope);
00475   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
00476   if (HasTemplateScope) {
00477     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
00478     ++CurTemplateDepthTracker;
00479   }
00480   // Set or update the scope flags.
00481   bool AlreadyHasClassScope = Class.TopLevelClass;
00482   unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
00483   ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
00484   ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
00485 
00486   if (!AlreadyHasClassScope)
00487     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
00488                                                 Class.TagOrTemplate);
00489 
00490   if (!Class.LateParsedDeclarations.empty()) {
00491     // C++11 [expr.prim.general]p4:
00492     //   Otherwise, if a member-declarator declares a non-static data member 
00493     //  (9.2) of a class X, the expression this is a prvalue of type "pointer
00494     //  to X" within the optional brace-or-equal-initializer. It shall not 
00495     //  appear elsewhere in the member-declarator.
00496     Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
00497                                      /*TypeQuals=*/(unsigned)0);
00498 
00499     for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
00500       Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
00501     }
00502   }
00503   
00504   if (!AlreadyHasClassScope)
00505     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
00506                                                  Class.TagOrTemplate);
00507 
00508   Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
00509 }
00510 
00511 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
00512   if (!MI.Field || MI.Field->isInvalidDecl())
00513     return;
00514 
00515   // Append the current token at the end of the new token stream so that it
00516   // doesn't get lost.
00517   MI.Toks.push_back(Tok);
00518   PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
00519 
00520   // Consume the previously pushed token.
00521   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
00522 
00523   SourceLocation EqualLoc;
00524 
00525   Actions.ActOnStartCXXInClassMemberInitializer();
00526 
00527   ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 
00528                                               EqualLoc);
00529 
00530   Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
00531                                                  Init.release());
00532 
00533   // The next token should be our artificial terminating EOF token.
00534   if (Tok.isNot(tok::eof)) {
00535     SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
00536     if (!EndLoc.isValid())
00537       EndLoc = Tok.getLocation();
00538     // No fixit; we can't recover as if there were a semicolon here.
00539     Diag(EndLoc, diag::err_expected_semi_decl_list);
00540 
00541     // Consume tokens until we hit the artificial EOF.
00542     while (Tok.isNot(tok::eof))
00543       ConsumeAnyToken();
00544   }
00545   ConsumeAnyToken();
00546 }
00547 
00548 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
00549 /// container until the token 'T' is reached (which gets
00550 /// consumed/stored too, if ConsumeFinalToken).
00551 /// If StopAtSemi is true, then we will stop early at a ';' character.
00552 /// Returns true if token 'T1' or 'T2' was found.
00553 /// NOTE: This is a specialized version of Parser::SkipUntil.
00554 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
00555                                   CachedTokens &Toks,
00556                                   bool StopAtSemi, bool ConsumeFinalToken) {
00557   // We always want this function to consume at least one token if the first
00558   // token isn't T and if not at EOF.
00559   bool isFirstTokenConsumed = true;
00560   while (1) {
00561     // If we found one of the tokens, stop and return true.
00562     if (Tok.is(T1) || Tok.is(T2)) {
00563       if (ConsumeFinalToken) {
00564         Toks.push_back(Tok);
00565         ConsumeAnyToken();
00566       }
00567       return true;
00568     }
00569 
00570     switch (Tok.getKind()) {
00571     case tok::eof:
00572     case tok::annot_module_begin:
00573     case tok::annot_module_end:
00574     case tok::annot_module_include:
00575       // Ran out of tokens.
00576       return false;
00577 
00578     case tok::l_paren:
00579       // Recursively consume properly-nested parens.
00580       Toks.push_back(Tok);
00581       ConsumeParen();
00582       ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
00583       break;
00584     case tok::l_square:
00585       // Recursively consume properly-nested square brackets.
00586       Toks.push_back(Tok);
00587       ConsumeBracket();
00588       ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
00589       break;
00590     case tok::l_brace:
00591       // Recursively consume properly-nested braces.
00592       Toks.push_back(Tok);
00593       ConsumeBrace();
00594       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
00595       break;
00596 
00597     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
00598     // Since the user wasn't looking for this token (if they were, it would
00599     // already be handled), this isn't balanced.  If there is a LHS token at a
00600     // higher level, we will assume that this matches the unbalanced token
00601     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
00602     case tok::r_paren:
00603       if (ParenCount && !isFirstTokenConsumed)
00604         return false;  // Matches something.
00605       Toks.push_back(Tok);
00606       ConsumeParen();
00607       break;
00608     case tok::r_square:
00609       if (BracketCount && !isFirstTokenConsumed)
00610         return false;  // Matches something.
00611       Toks.push_back(Tok);
00612       ConsumeBracket();
00613       break;
00614     case tok::r_brace:
00615       if (BraceCount && !isFirstTokenConsumed)
00616         return false;  // Matches something.
00617       Toks.push_back(Tok);
00618       ConsumeBrace();
00619       break;
00620 
00621     case tok::code_completion:
00622       Toks.push_back(Tok);
00623       ConsumeCodeCompletionToken();
00624       break;
00625 
00626     case tok::string_literal:
00627     case tok::wide_string_literal:
00628     case tok::utf8_string_literal:
00629     case tok::utf16_string_literal:
00630     case tok::utf32_string_literal:
00631       Toks.push_back(Tok);
00632       ConsumeStringToken();
00633       break;
00634     case tok::semi:
00635       if (StopAtSemi)
00636         return false;
00637       // FALL THROUGH.
00638     default:
00639       // consume this token.
00640       Toks.push_back(Tok);
00641       ConsumeToken();
00642       break;
00643     }
00644     isFirstTokenConsumed = false;
00645   }
00646 }
00647 
00648 /// \brief Consume tokens and store them in the passed token container until
00649 /// we've passed the try keyword and constructor initializers and have consumed
00650 /// the opening brace of the function body. The opening brace will be consumed
00651 /// if and only if there was no error.
00652 ///
00653 /// \return True on error.
00654 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
00655   if (Tok.is(tok::kw_try)) {
00656     Toks.push_back(Tok);
00657     ConsumeToken();
00658   }
00659 
00660   if (Tok.isNot(tok::colon)) {
00661     // Easy case, just a function body.
00662 
00663     // Grab any remaining garbage to be diagnosed later. We stop when we reach a
00664     // brace: an opening one is the function body, while a closing one probably
00665     // means we've reached the end of the class.
00666     ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
00667                          /*StopAtSemi=*/true,
00668                          /*ConsumeFinalToken=*/false);
00669     if (Tok.isNot(tok::l_brace))
00670       return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
00671 
00672     Toks.push_back(Tok);
00673     ConsumeBrace();
00674     return false;
00675   }
00676 
00677   Toks.push_back(Tok);
00678   ConsumeToken();
00679 
00680   // We can't reliably skip over a mem-initializer-id, because it could be
00681   // a template-id involving not-yet-declared names. Given:
00682   //
00683   //   S ( ) : a < b < c > ( e )
00684   //
00685   // 'e' might be an initializer or part of a template argument, depending
00686   // on whether 'b' is a template.
00687 
00688   // Track whether we might be inside a template argument. We can give
00689   // significantly better diagnostics if we know that we're not.
00690   bool MightBeTemplateArgument = false;
00691 
00692   while (true) {
00693     // Skip over the mem-initializer-id, if possible.
00694     if (Tok.is(tok::kw_decltype)) {
00695       Toks.push_back(Tok);
00696       SourceLocation OpenLoc = ConsumeToken();
00697       if (Tok.isNot(tok::l_paren))
00698         return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
00699                  << "decltype";
00700       Toks.push_back(Tok);
00701       ConsumeParen();
00702       if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
00703         Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
00704         Diag(OpenLoc, diag::note_matching) << tok::l_paren;
00705         return true;
00706       }
00707     }
00708     do {
00709       // Walk over a component of a nested-name-specifier.
00710       if (Tok.is(tok::coloncolon)) {
00711         Toks.push_back(Tok);
00712         ConsumeToken();
00713 
00714         if (Tok.is(tok::kw_template)) {
00715           Toks.push_back(Tok);
00716           ConsumeToken();
00717         }
00718       }
00719 
00720       if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) {
00721         Toks.push_back(Tok);
00722         ConsumeToken();
00723       } else if (Tok.is(tok::code_completion)) {
00724         Toks.push_back(Tok);
00725         ConsumeCodeCompletionToken();
00726         // Consume the rest of the initializers permissively.
00727         // FIXME: We should be able to perform code-completion here even if
00728         //        there isn't a subsequent '{' token.
00729         MightBeTemplateArgument = true;
00730         break;
00731       } else {
00732         break;
00733       }
00734     } while (Tok.is(tok::coloncolon));
00735 
00736     if (Tok.is(tok::less))
00737       MightBeTemplateArgument = true;
00738 
00739     if (MightBeTemplateArgument) {
00740       // We may be inside a template argument list. Grab up to the start of the
00741       // next parenthesized initializer or braced-init-list. This *might* be the
00742       // initializer, or it might be a subexpression in the template argument
00743       // list.
00744       // FIXME: Count angle brackets, and clear MightBeTemplateArgument
00745       //        if all angles are closed.
00746       if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
00747                                 /*StopAtSemi=*/true,
00748                                 /*ConsumeFinalToken=*/false)) {
00749         // We're not just missing the initializer, we're also missing the
00750         // function body!
00751         return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
00752       }
00753     } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
00754       // We found something weird in a mem-initializer-id.
00755       if (getLangOpts().CPlusPlus11)
00756         return Diag(Tok.getLocation(), diag::err_expected_either)
00757                << tok::l_paren << tok::l_brace;
00758       else
00759         return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
00760     }
00761 
00762     tok::TokenKind kind = Tok.getKind();
00763     Toks.push_back(Tok);
00764     bool IsLParen = (kind == tok::l_paren);
00765     SourceLocation OpenLoc = Tok.getLocation();
00766 
00767     if (IsLParen) {
00768       ConsumeParen();
00769     } else {
00770       assert(kind == tok::l_brace && "Must be left paren or brace here.");
00771       ConsumeBrace();
00772       // In C++03, this has to be the start of the function body, which
00773       // means the initializer is malformed; we'll diagnose it later.
00774       if (!getLangOpts().CPlusPlus11)
00775         return false;
00776     }
00777 
00778     // Grab the initializer (or the subexpression of the template argument).
00779     // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
00780     //        if we might be inside the braces of a lambda-expression.
00781     tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
00782     if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
00783       Diag(Tok, diag::err_expected) << CloseKind;
00784       Diag(OpenLoc, diag::note_matching) << kind;
00785       return true;
00786     }
00787 
00788     // Grab pack ellipsis, if present.
00789     if (Tok.is(tok::ellipsis)) {
00790       Toks.push_back(Tok);
00791       ConsumeToken();
00792     }
00793 
00794     // If we know we just consumed a mem-initializer, we must have ',' or '{'
00795     // next.
00796     if (Tok.is(tok::comma)) {
00797       Toks.push_back(Tok);
00798       ConsumeToken();
00799     } else if (Tok.is(tok::l_brace)) {
00800       // This is the function body if the ')' or '}' is immediately followed by
00801       // a '{'. That cannot happen within a template argument, apart from the
00802       // case where a template argument contains a compound literal:
00803       //
00804       //   S ( ) : a < b < c > ( d ) { }
00805       //   // End of declaration, or still inside the template argument?
00806       //
00807       // ... and the case where the template argument contains a lambda:
00808       //
00809       //   S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
00810       //     ( ) > ( ) { }
00811       //
00812       // FIXME: Disambiguate these cases. Note that the latter case is probably
00813       //        going to be made ill-formed by core issue 1607.
00814       Toks.push_back(Tok);
00815       ConsumeBrace();
00816       return false;
00817     } else if (!MightBeTemplateArgument) {
00818       return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
00819                                                                 << tok::comma;
00820     }
00821   }
00822 }
00823 
00824 /// \brief Consume and store tokens from the '?' to the ':' in a conditional
00825 /// expression.
00826 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
00827   // Consume '?'.
00828   assert(Tok.is(tok::question));
00829   Toks.push_back(Tok);
00830   ConsumeToken();
00831 
00832   while (Tok.isNot(tok::colon)) {
00833     if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks, /*StopAtSemi*/true,
00834                               /*ConsumeFinalToken*/false))
00835       return false;
00836 
00837     // If we found a nested conditional, consume it.
00838     if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
00839       return false;
00840   }
00841 
00842   // Consume ':'.
00843   Toks.push_back(Tok);
00844   ConsumeToken();
00845   return true;
00846 }
00847 
00848 /// \brief A tentative parsing action that can also revert token annotations.
00849 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
00850 public:
00851   explicit UnannotatedTentativeParsingAction(Parser &Self,
00852                                              tok::TokenKind EndKind)
00853       : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
00854     // Stash away the old token stream, so we can restore it once the
00855     // tentative parse is complete.
00856     TentativeParsingAction Inner(Self);
00857     Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
00858     Inner.Revert();
00859   }
00860 
00861   void RevertAnnotations() {
00862     Revert();
00863 
00864     // Put back the original tokens.
00865     Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
00866     if (Toks.size()) {
00867       Token *Buffer = new Token[Toks.size()];
00868       std::copy(Toks.begin() + 1, Toks.end(), Buffer);
00869       Buffer[Toks.size() - 1] = Self.Tok;
00870       Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true);
00871 
00872       Self.Tok = Toks.front();
00873     }
00874   }
00875 
00876 private:
00877   Parser &Self;
00878   CachedTokens Toks;
00879   tok::TokenKind EndKind;
00880 };
00881 
00882 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
00883 /// container until the end of the current initializer expression (either a
00884 /// default argument or an in-class initializer for a non-static data member).
00885 /// The final token is not consumed.
00886 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
00887                                         CachedInitKind CIK) {
00888   // We always want this function to consume at least one token if not at EOF.
00889   bool IsFirstTokenConsumed = true;
00890 
00891   // Number of possible unclosed <s we've seen so far. These might be templates,
00892   // and might not, but if there were none of them (or we know for sure that
00893   // we're within a template), we can avoid a tentative parse.
00894   unsigned AngleCount = 0;
00895   unsigned KnownTemplateCount = 0;
00896 
00897   while (1) {
00898     switch (Tok.getKind()) {
00899     case tok::comma:
00900       // If we might be in a template, perform a tentative parse to check.
00901       if (!AngleCount)
00902         // Not a template argument: this is the end of the initializer.
00903         return true;
00904       if (KnownTemplateCount)
00905         goto consume_token;
00906 
00907       // We hit a comma inside angle brackets. This is the hard case. The
00908       // rule we follow is:
00909       //  * For a default argument, if the tokens after the comma form a
00910       //    syntactically-valid parameter-declaration-clause, in which each
00911       //    parameter has an initializer, then this comma ends the default
00912       //    argument.
00913       //  * For a default initializer, if the tokens after the comma form a
00914       //    syntactically-valid init-declarator-list, then this comma ends
00915       //    the default initializer.
00916       {
00917         UnannotatedTentativeParsingAction PA(*this,
00918                                              CIK == CIK_DefaultInitializer
00919                                                ? tok::semi : tok::r_paren);
00920         Sema::TentativeAnalysisScope Scope(Actions);
00921 
00922         TPResult Result = TPResult::Error();
00923         ConsumeToken();
00924         switch (CIK) {
00925         case CIK_DefaultInitializer:
00926           Result = TryParseInitDeclaratorList();
00927           // If we parsed a complete, ambiguous init-declarator-list, this
00928           // is only syntactically-valid if it's followed by a semicolon.
00929           if (Result == TPResult::Ambiguous() && Tok.isNot(tok::semi))
00930             Result = TPResult::False();
00931           break;
00932 
00933         case CIK_DefaultArgument:
00934           bool InvalidAsDeclaration = false;
00935           Result = TryParseParameterDeclarationClause(
00936               &InvalidAsDeclaration, /*VersusTemplateArgument*/true);
00937           // If this is an expression or a declaration with a missing
00938           // 'typename', assume it's not a declaration.
00939           if (Result == TPResult::Ambiguous() && InvalidAsDeclaration)
00940             Result = TPResult::False();
00941           break;
00942         }
00943 
00944         // If what follows could be a declaration, it is a declaration.
00945         if (Result != TPResult::False() && Result != TPResult::Error()) {
00946           PA.Revert();
00947           return true;
00948         }
00949 
00950         // In the uncommon case that we decide the following tokens are part
00951         // of a template argument, revert any annotations we've performed in
00952         // those tokens. We're not going to look them up until we've parsed
00953         // the rest of the class, and that might add more declarations.
00954         PA.RevertAnnotations();
00955       }
00956 
00957       // Keep going. We know we're inside a template argument list now.
00958       ++KnownTemplateCount;
00959       goto consume_token;
00960 
00961     case tok::eof:
00962     case tok::annot_module_begin:
00963     case tok::annot_module_end:
00964     case tok::annot_module_include:
00965       // Ran out of tokens.
00966       return false;
00967 
00968     case tok::less:
00969       // FIXME: A '<' can only start a template-id if it's preceded by an
00970       // identifier, an operator-function-id, or a literal-operator-id.
00971       ++AngleCount;
00972       goto consume_token;
00973 
00974     case tok::question:
00975       // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
00976       // that is *never* the end of the initializer. Skip to the ':'.
00977       if (!ConsumeAndStoreConditional(Toks))
00978         return false;
00979       break;
00980 
00981     case tok::greatergreatergreater:
00982       if (!getLangOpts().CPlusPlus11)
00983         goto consume_token;
00984       if (AngleCount) --AngleCount;
00985       if (KnownTemplateCount) --KnownTemplateCount;
00986       // Fall through.
00987     case tok::greatergreater:
00988       if (!getLangOpts().CPlusPlus11)
00989         goto consume_token;
00990       if (AngleCount) --AngleCount;
00991       if (KnownTemplateCount) --KnownTemplateCount;
00992       // Fall through.
00993     case tok::greater:
00994       if (AngleCount) --AngleCount;
00995       if (KnownTemplateCount) --KnownTemplateCount;
00996       goto consume_token;
00997 
00998     case tok::kw_template:
00999       // 'template' identifier '<' is known to start a template argument list,
01000       // and can be used to disambiguate the parse.
01001       // FIXME: Support all forms of 'template' unqualified-id '<'.
01002       Toks.push_back(Tok);
01003       ConsumeToken();
01004       if (Tok.is(tok::identifier)) {
01005         Toks.push_back(Tok);
01006         ConsumeToken();
01007         if (Tok.is(tok::less)) {
01008           ++KnownTemplateCount;
01009           Toks.push_back(Tok);
01010           ConsumeToken();
01011         }
01012       }
01013       break;
01014 
01015     case tok::kw_operator:
01016       // If 'operator' precedes other punctuation, that punctuation loses
01017       // its special behavior.
01018       Toks.push_back(Tok);
01019       ConsumeToken();
01020       switch (Tok.getKind()) {
01021       case tok::comma:
01022       case tok::greatergreatergreater:
01023       case tok::greatergreater:
01024       case tok::greater:
01025       case tok::less:
01026         Toks.push_back(Tok);
01027         ConsumeToken();
01028         break;
01029       default:
01030         break;
01031       }
01032       break;
01033 
01034     case tok::l_paren:
01035       // Recursively consume properly-nested parens.
01036       Toks.push_back(Tok);
01037       ConsumeParen();
01038       ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
01039       break;
01040     case tok::l_square:
01041       // Recursively consume properly-nested square brackets.
01042       Toks.push_back(Tok);
01043       ConsumeBracket();
01044       ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
01045       break;
01046     case tok::l_brace:
01047       // Recursively consume properly-nested braces.
01048       Toks.push_back(Tok);
01049       ConsumeBrace();
01050       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
01051       break;
01052 
01053     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
01054     // Since the user wasn't looking for this token (if they were, it would
01055     // already be handled), this isn't balanced.  If there is a LHS token at a
01056     // higher level, we will assume that this matches the unbalanced token
01057     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
01058     case tok::r_paren:
01059       if (CIK == CIK_DefaultArgument)
01060         return true; // End of the default argument.
01061       if (ParenCount && !IsFirstTokenConsumed)
01062         return false;  // Matches something.
01063       goto consume_token;
01064     case tok::r_square:
01065       if (BracketCount && !IsFirstTokenConsumed)
01066         return false;  // Matches something.
01067       goto consume_token;
01068     case tok::r_brace:
01069       if (BraceCount && !IsFirstTokenConsumed)
01070         return false;  // Matches something.
01071       goto consume_token;
01072 
01073     case tok::code_completion:
01074       Toks.push_back(Tok);
01075       ConsumeCodeCompletionToken();
01076       break;
01077 
01078     case tok::string_literal:
01079     case tok::wide_string_literal:
01080     case tok::utf8_string_literal:
01081     case tok::utf16_string_literal:
01082     case tok::utf32_string_literal:
01083       Toks.push_back(Tok);
01084       ConsumeStringToken();
01085       break;
01086     case tok::semi:
01087       if (CIK == CIK_DefaultInitializer)
01088         return true; // End of the default initializer.
01089       // FALL THROUGH.
01090     default:
01091     consume_token:
01092       Toks.push_back(Tok);
01093       ConsumeToken();
01094       break;
01095     }
01096     IsFirstTokenConsumed = false;
01097   }
01098 }