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