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