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