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