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