1 //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements parsing for C++ class inline methods. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Parse/Parser.h" 14 #include "clang/AST/DeclTemplate.h" 15 #include "clang/Parse/ParseDiagnostic.h" 16 #include "clang/Parse/RAIIObjectsForParser.h" 17 #include "clang/Sema/DeclSpec.h" 18 #include "clang/Sema/Scope.h" 19 using namespace clang; 20 21 /// ParseCXXInlineMethodDef - We parsed and verified that the specified 22 /// Declarator is a well formed C++ inline method definition. Now lex its body 23 /// and store its tokens for parsing after the C++ class is complete. 24 NamedDecl *Parser::ParseCXXInlineMethodDef( 25 AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D, 26 const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS, 27 SourceLocation PureSpecLoc) { 28 assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); 29 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) && 30 "Current token not a '{', ':', '=', or 'try'!"); 31 32 MultiTemplateParamsArg TemplateParams( 33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() 34 : nullptr, 35 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 36 37 NamedDecl *FnD; 38 if (D.getDeclSpec().isFriendSpecified()) 39 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, 40 TemplateParams); 41 else { 42 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, 43 TemplateParams, nullptr, 44 VS, ICIS_NoInit); 45 if (FnD) { 46 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs); 47 if (PureSpecLoc.isValid()) 48 Actions.ActOnPureSpecifier(FnD, PureSpecLoc); 49 } 50 } 51 52 if (FnD) 53 HandleMemberFunctionDeclDelays(D, FnD); 54 55 D.complete(FnD); 56 57 if (TryConsumeToken(tok::equal)) { 58 if (!FnD) { 59 SkipUntil(tok::semi); 60 return nullptr; 61 } 62 63 bool Delete = false; 64 SourceLocation KWLoc; 65 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1); 66 if (TryConsumeToken(tok::kw_delete, KWLoc)) { 67 Diag(KWLoc, getLangOpts().CPlusPlus11 68 ? diag::warn_cxx98_compat_defaulted_deleted_function 69 : diag::ext_defaulted_deleted_function) 70 << 1 /* deleted */; 71 Actions.SetDeclDeleted(FnD, KWLoc); 72 Delete = true; 73 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) { 74 DeclAsFunction->setRangeEnd(KWEndLoc); 75 } 76 } else if (TryConsumeToken(tok::kw_default, KWLoc)) { 77 Diag(KWLoc, getLangOpts().CPlusPlus11 78 ? diag::warn_cxx98_compat_defaulted_deleted_function 79 : diag::ext_defaulted_deleted_function) 80 << 0 /* defaulted */; 81 Actions.SetDeclDefaulted(FnD, KWLoc); 82 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) { 83 DeclAsFunction->setRangeEnd(KWEndLoc); 84 } 85 } else { 86 llvm_unreachable("function definition after = not 'delete' or 'default'"); 87 } 88 89 if (Tok.is(tok::comma)) { 90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 91 << Delete; 92 SkipUntil(tok::semi); 93 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, 94 Delete ? "delete" : "default")) { 95 SkipUntil(tok::semi); 96 } 97 98 return FnD; 99 } 100 101 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) && 102 trySkippingFunctionBody()) { 103 Actions.ActOnSkippedFunctionBody(FnD); 104 return FnD; 105 } 106 107 // In delayed template parsing mode, if we are within a class template 108 // or if we are about to parse function member template then consume 109 // the tokens and store them for parsing at the end of the translation unit. 110 if (getLangOpts().DelayedTemplateParsing && 111 D.getFunctionDefinitionKind() == FDK_Definition && 112 !D.getDeclSpec().hasConstexprSpecifier() && 113 !(FnD && FnD->getAsFunction() && 114 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) && 115 ((Actions.CurContext->isDependentContext() || 116 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 117 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) && 118 !Actions.IsInsideALocalClassWithinATemplateFunction())) { 119 120 CachedTokens Toks; 121 LexTemplateFunctionForLateParsing(Toks); 122 123 if (FnD) { 124 FunctionDecl *FD = FnD->getAsFunction(); 125 Actions.CheckForFunctionRedefinition(FD); 126 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); 127 } 128 129 return FnD; 130 } 131 132 // Consume the tokens and store them for later parsing. 133 134 LexedMethod* LM = new LexedMethod(this, FnD); 135 getCurrentClass().LateParsedDeclarations.push_back(LM); 136 CachedTokens &Toks = LM->Toks; 137 138 tok::TokenKind kind = Tok.getKind(); 139 // Consume everything up to (and including) the left brace of the 140 // function body. 141 if (ConsumeAndStoreFunctionPrologue(Toks)) { 142 // We didn't find the left-brace we expected after the 143 // constructor initializer; we already printed an error, and it's likely 144 // impossible to recover, so don't try to parse this method later. 145 // Skip over the rest of the decl and back to somewhere that looks 146 // reasonable. 147 SkipMalformedDecl(); 148 delete getCurrentClass().LateParsedDeclarations.back(); 149 getCurrentClass().LateParsedDeclarations.pop_back(); 150 return FnD; 151 } else { 152 // Consume everything up to (and including) the matching right brace. 153 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 154 } 155 156 // If we're in a function-try-block, we need to store all the catch blocks. 157 if (kind == tok::kw_try) { 158 while (Tok.is(tok::kw_catch)) { 159 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 161 } 162 } 163 164 if (FnD) { 165 FunctionDecl *FD = FnD->getAsFunction(); 166 // Track that this function will eventually have a body; Sema needs 167 // to know this. 168 Actions.CheckForFunctionRedefinition(FD); 169 FD->setWillHaveBody(true); 170 } else { 171 // If semantic analysis could not build a function declaration, 172 // just throw away the late-parsed declaration. 173 delete getCurrentClass().LateParsedDeclarations.back(); 174 getCurrentClass().LateParsedDeclarations.pop_back(); 175 } 176 177 return FnD; 178 } 179 180 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the 181 /// specified Declarator is a well formed C++ non-static data member 182 /// declaration. Now lex its initializer and store its tokens for parsing 183 /// after the class is complete. 184 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { 185 assert(Tok.isOneOf(tok::l_brace, tok::equal) && 186 "Current token not a '{' or '='!"); 187 188 LateParsedMemberInitializer *MI = 189 new LateParsedMemberInitializer(this, VarD); 190 getCurrentClass().LateParsedDeclarations.push_back(MI); 191 CachedTokens &Toks = MI->Toks; 192 193 tok::TokenKind kind = Tok.getKind(); 194 if (kind == tok::equal) { 195 Toks.push_back(Tok); 196 ConsumeToken(); 197 } 198 199 if (kind == tok::l_brace) { 200 // Begin by storing the '{' token. 201 Toks.push_back(Tok); 202 ConsumeBrace(); 203 204 // Consume everything up to (and including) the matching right brace. 205 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); 206 } else { 207 // Consume everything up to (but excluding) the comma or semicolon. 208 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer); 209 } 210 211 // Store an artificial EOF token to ensure that we don't run off the end of 212 // the initializer when we come to parse it. 213 Token Eof; 214 Eof.startToken(); 215 Eof.setKind(tok::eof); 216 Eof.setLocation(Tok.getLocation()); 217 Eof.setEofData(VarD); 218 Toks.push_back(Eof); 219 } 220 221 Parser::LateParsedDeclaration::~LateParsedDeclaration() {} 222 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} 223 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} 224 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} 225 void Parser::LateParsedDeclaration::ParseLexedAttributes() {} 226 void Parser::LateParsedDeclaration::ParseLexedPragmas() {} 227 228 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) 229 : Self(P), Class(C) {} 230 231 Parser::LateParsedClass::~LateParsedClass() { 232 Self->DeallocateParsedClasses(Class); 233 } 234 235 void Parser::LateParsedClass::ParseLexedMethodDeclarations() { 236 Self->ParseLexedMethodDeclarations(*Class); 237 } 238 239 void Parser::LateParsedClass::ParseLexedMemberInitializers() { 240 Self->ParseLexedMemberInitializers(*Class); 241 } 242 243 void Parser::LateParsedClass::ParseLexedMethodDefs() { 244 Self->ParseLexedMethodDefs(*Class); 245 } 246 247 void Parser::LateParsedClass::ParseLexedAttributes() { 248 Self->ParseLexedAttributes(*Class); 249 } 250 251 void Parser::LateParsedClass::ParseLexedPragmas() { 252 Self->ParseLexedPragmas(*Class); 253 } 254 255 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { 256 Self->ParseLexedMethodDeclaration(*this); 257 } 258 259 void Parser::LexedMethod::ParseLexedMethodDefs() { 260 Self->ParseLexedMethodDef(*this); 261 } 262 263 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { 264 Self->ParseLexedMemberInitializer(*this); 265 } 266 267 void Parser::LateParsedAttribute::ParseLexedAttributes() { 268 Self->ParseLexedAttribute(*this, true, false); 269 } 270 271 void Parser::LateParsedPragma::ParseLexedPragmas() { 272 Self->ParseLexedPragma(*this); 273 } 274 275 /// Utility to re-enter a possibly-templated scope while parsing its 276 /// late-parsed components. 277 struct Parser::ReenterTemplateScopeRAII { 278 Parser &P; 279 MultiParseScope Scopes; 280 TemplateParameterDepthRAII CurTemplateDepthTracker; 281 282 ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true) 283 : P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) { 284 if (Enter) { 285 CurTemplateDepthTracker.addDepth( 286 P.ReenterTemplateScopes(Scopes, MaybeTemplated)); 287 } 288 } 289 }; 290 291 /// Utility to re-enter a class scope while parsing its late-parsed components. 292 struct Parser::ReenterClassScopeRAII : ReenterTemplateScopeRAII { 293 ParsingClass &Class; 294 295 ReenterClassScopeRAII(Parser &P, ParsingClass &Class) 296 : ReenterTemplateScopeRAII(P, Class.TagOrTemplate, 297 /*Enter=*/!Class.TopLevelClass), 298 Class(Class) { 299 // If this is the top-level class, we're still within its scope. 300 if (Class.TopLevelClass) 301 return; 302 303 // Re-enter the class scope itself. 304 Scopes.Enter(Scope::ClassScope|Scope::DeclScope); 305 P.Actions.ActOnStartDelayedMemberDeclarations(P.getCurScope(), 306 Class.TagOrTemplate); 307 } 308 ~ReenterClassScopeRAII() { 309 if (Class.TopLevelClass) 310 return; 311 312 P.Actions.ActOnFinishDelayedMemberDeclarations(P.getCurScope(), 313 Class.TagOrTemplate); 314 } 315 }; 316 317 /// ParseLexedMethodDeclarations - We finished parsing the member 318 /// specification of a top (non-nested) C++ class. Now go over the 319 /// stack of method declarations with some parts for which parsing was 320 /// delayed (such as default arguments) and parse them. 321 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { 322 ReenterClassScopeRAII InClassScope(*this, Class); 323 324 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations) 325 LateD->ParseLexedMethodDeclarations(); 326 } 327 328 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { 329 // If this is a member template, introduce the template parameter scope. 330 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.Method); 331 332 // Start the delayed C++ method declaration 333 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 334 335 // Introduce the parameters into scope and parse their default 336 // arguments. 337 InFunctionTemplateScope.Scopes.Enter(Scope::FunctionPrototypeScope | 338 Scope::FunctionDeclarationScope | 339 Scope::DeclScope); 340 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { 341 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param); 342 // Introduce the parameter into scope. 343 bool HasUnparsed = Param->hasUnparsedDefaultArg(); 344 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param); 345 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks); 346 if (Toks) { 347 ParenBraceBracketBalancer BalancerRAIIObj(*this); 348 349 // Mark the end of the default argument so that we know when to stop when 350 // we parse it later on. 351 Token LastDefaultArgToken = Toks->back(); 352 Token DefArgEnd; 353 DefArgEnd.startToken(); 354 DefArgEnd.setKind(tok::eof); 355 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc()); 356 DefArgEnd.setEofData(Param); 357 Toks->push_back(DefArgEnd); 358 359 // Parse the default argument from its saved token stream. 360 Toks->push_back(Tok); // So that the current token doesn't get lost 361 PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true); 362 363 // Consume the previously-pushed token. 364 ConsumeAnyToken(); 365 366 // Consume the '='. 367 assert(Tok.is(tok::equal) && "Default argument not starting with '='"); 368 SourceLocation EqualLoc = ConsumeToken(); 369 370 // The argument isn't actually potentially evaluated unless it is 371 // used. 372 EnterExpressionEvaluationContext Eval( 373 Actions, 374 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param); 375 376 ExprResult DefArgResult; 377 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 378 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 379 DefArgResult = ParseBraceInitializer(); 380 } else 381 DefArgResult = ParseAssignmentExpression(); 382 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); 383 if (DefArgResult.isInvalid()) { 384 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); 385 } else { 386 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) { 387 // The last two tokens are the terminator and the saved value of 388 // Tok; the last token in the default argument is the one before 389 // those. 390 assert(Toks->size() >= 3 && "expected a token in default arg"); 391 Diag(Tok.getLocation(), diag::err_default_arg_unparsed) 392 << SourceRange(Tok.getLocation(), 393 (*Toks)[Toks->size() - 3].getLocation()); 394 } 395 Actions.ActOnParamDefaultArgument(Param, EqualLoc, 396 DefArgResult.get()); 397 } 398 399 // There could be leftover tokens (e.g. because of an error). 400 // Skip through until we reach the 'end of default argument' token. 401 while (Tok.isNot(tok::eof)) 402 ConsumeAnyToken(); 403 404 if (Tok.is(tok::eof) && Tok.getEofData() == Param) 405 ConsumeAnyToken(); 406 } else if (HasUnparsed) { 407 assert(Param->hasInheritedDefaultArg()); 408 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl(); 409 ParmVarDecl *OldParam = Old->getParamDecl(I); 410 assert (!OldParam->hasUnparsedDefaultArg()); 411 if (OldParam->hasUninstantiatedDefaultArg()) 412 Param->setUninstantiatedDefaultArg( 413 OldParam->getUninstantiatedDefaultArg()); 414 else 415 Param->setDefaultArg(OldParam->getInit()); 416 } 417 } 418 419 // Parse a delayed exception-specification, if there is one. 420 if (CachedTokens *Toks = LM.ExceptionSpecTokens) { 421 ParenBraceBracketBalancer BalancerRAIIObj(*this); 422 423 // Add the 'stop' token. 424 Token LastExceptionSpecToken = Toks->back(); 425 Token ExceptionSpecEnd; 426 ExceptionSpecEnd.startToken(); 427 ExceptionSpecEnd.setKind(tok::eof); 428 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc()); 429 ExceptionSpecEnd.setEofData(LM.Method); 430 Toks->push_back(ExceptionSpecEnd); 431 432 // Parse the default argument from its saved token stream. 433 Toks->push_back(Tok); // So that the current token doesn't get lost 434 PP.EnterTokenStream(*Toks, true, /*IsReinject*/true); 435 436 // Consume the previously-pushed token. 437 ConsumeAnyToken(); 438 439 // C++11 [expr.prim.general]p3: 440 // If a declaration declares a member function or member function 441 // template of a class X, the expression this is a prvalue of type 442 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq 443 // and the end of the function-definition, member-declarator, or 444 // declarator. 445 CXXMethodDecl *Method; 446 if (FunctionTemplateDecl *FunTmpl 447 = dyn_cast<FunctionTemplateDecl>(LM.Method)) 448 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 449 else 450 Method = cast<CXXMethodDecl>(LM.Method); 451 452 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(), 453 Method->getMethodQualifiers(), 454 getLangOpts().CPlusPlus11); 455 456 // Parse the exception-specification. 457 SourceRange SpecificationRange; 458 SmallVector<ParsedType, 4> DynamicExceptions; 459 SmallVector<SourceRange, 4> DynamicExceptionRanges; 460 ExprResult NoexceptExpr; 461 CachedTokens *ExceptionSpecTokens; 462 463 ExceptionSpecificationType EST 464 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange, 465 DynamicExceptions, 466 DynamicExceptionRanges, NoexceptExpr, 467 ExceptionSpecTokens); 468 469 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method) 470 Diag(Tok.getLocation(), diag::err_except_spec_unparsed); 471 472 // Attach the exception-specification to the method. 473 Actions.actOnDelayedExceptionSpecification(LM.Method, EST, 474 SpecificationRange, 475 DynamicExceptions, 476 DynamicExceptionRanges, 477 NoexceptExpr.isUsable()? 478 NoexceptExpr.get() : nullptr); 479 480 // There could be leftover tokens (e.g. because of an error). 481 // Skip through until we reach the original token position. 482 while (Tok.isNot(tok::eof)) 483 ConsumeAnyToken(); 484 485 // Clean up the remaining EOF token. 486 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method) 487 ConsumeAnyToken(); 488 489 delete Toks; 490 LM.ExceptionSpecTokens = nullptr; 491 } 492 493 InFunctionTemplateScope.Scopes.Exit(); 494 495 // Finish the delayed C++ method declaration. 496 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 497 } 498 499 /// ParseLexedMethodDefs - We finished parsing the member specification of a top 500 /// (non-nested) C++ class. Now go over the stack of lexed methods that were 501 /// collected during its parsing and parse them all. 502 void Parser::ParseLexedMethodDefs(ParsingClass &Class) { 503 ReenterClassScopeRAII InClassScope(*this, Class); 504 505 for (LateParsedDeclaration *D : Class.LateParsedDeclarations) 506 D->ParseLexedMethodDefs(); 507 } 508 509 void Parser::ParseLexedMethodDef(LexedMethod &LM) { 510 // If this is a member template, introduce the template parameter scope. 511 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.D); 512 513 ParenBraceBracketBalancer BalancerRAIIObj(*this); 514 515 assert(!LM.Toks.empty() && "Empty body!"); 516 Token LastBodyToken = LM.Toks.back(); 517 Token BodyEnd; 518 BodyEnd.startToken(); 519 BodyEnd.setKind(tok::eof); 520 BodyEnd.setLocation(LastBodyToken.getEndLoc()); 521 BodyEnd.setEofData(LM.D); 522 LM.Toks.push_back(BodyEnd); 523 // Append the current token at the end of the new token stream so that it 524 // doesn't get lost. 525 LM.Toks.push_back(Tok); 526 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true); 527 528 // Consume the previously pushed token. 529 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 530 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) 531 && "Inline method not starting with '{', ':' or 'try'"); 532 533 // Parse the method body. Function body parsing code is similar enough 534 // to be re-used for method bodies as well. 535 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | 536 Scope::CompoundStmtScope); 537 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); 538 539 if (Tok.is(tok::kw_try)) { 540 ParseFunctionTryBlock(LM.D, FnScope); 541 542 while (Tok.isNot(tok::eof)) 543 ConsumeAnyToken(); 544 545 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 546 ConsumeAnyToken(); 547 return; 548 } 549 if (Tok.is(tok::colon)) { 550 ParseConstructorInitializer(LM.D); 551 552 // Error recovery. 553 if (!Tok.is(tok::l_brace)) { 554 FnScope.Exit(); 555 Actions.ActOnFinishFunctionBody(LM.D, nullptr); 556 557 while (Tok.isNot(tok::eof)) 558 ConsumeAnyToken(); 559 560 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 561 ConsumeAnyToken(); 562 return; 563 } 564 } else 565 Actions.ActOnDefaultCtorInitializers(LM.D); 566 567 assert((Actions.getDiagnostics().hasErrorOccurred() || 568 !isa<FunctionTemplateDecl>(LM.D) || 569 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() 570 < TemplateParameterDepth) && 571 "TemplateParameterDepth should be greater than the depth of " 572 "current template being instantiated!"); 573 574 ParseFunctionStatementBody(LM.D, FnScope); 575 576 while (Tok.isNot(tok::eof)) 577 ConsumeAnyToken(); 578 579 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 580 ConsumeAnyToken(); 581 582 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D)) 583 if (isa<CXXMethodDecl>(FD) || 584 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend)) 585 Actions.ActOnFinishInlineFunctionDef(FD); 586 } 587 588 /// ParseLexedMemberInitializers - We finished parsing the member specification 589 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member 590 /// initializers that were collected during its parsing and parse them all. 591 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { 592 ReenterClassScopeRAII InClassScope(*this, Class); 593 594 if (!Class.LateParsedDeclarations.empty()) { 595 // C++11 [expr.prim.general]p4: 596 // Otherwise, if a member-declarator declares a non-static data member 597 // (9.2) of a class X, the expression this is a prvalue of type "pointer 598 // to X" within the optional brace-or-equal-initializer. It shall not 599 // appear elsewhere in the member-declarator. 600 // FIXME: This should be done in ParseLexedMemberInitializer, not here. 601 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, 602 Qualifiers()); 603 604 for (LateParsedDeclaration *D : Class.LateParsedDeclarations) 605 D->ParseLexedMemberInitializers(); 606 } 607 608 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); 609 } 610 611 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { 612 if (!MI.Field || MI.Field->isInvalidDecl()) 613 return; 614 615 ParenBraceBracketBalancer BalancerRAIIObj(*this); 616 617 // Append the current token at the end of the new token stream so that it 618 // doesn't get lost. 619 MI.Toks.push_back(Tok); 620 PP.EnterTokenStream(MI.Toks, true, /*IsReinject*/true); 621 622 // Consume the previously pushed token. 623 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 624 625 SourceLocation EqualLoc; 626 627 Actions.ActOnStartCXXInClassMemberInitializer(); 628 629 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 630 EqualLoc); 631 632 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc, 633 Init.get()); 634 635 // The next token should be our artificial terminating EOF token. 636 if (Tok.isNot(tok::eof)) { 637 if (!Init.isInvalid()) { 638 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 639 if (!EndLoc.isValid()) 640 EndLoc = Tok.getLocation(); 641 // No fixit; we can't recover as if there were a semicolon here. 642 Diag(EndLoc, diag::err_expected_semi_decl_list); 643 } 644 645 // Consume tokens until we hit the artificial EOF. 646 while (Tok.isNot(tok::eof)) 647 ConsumeAnyToken(); 648 } 649 // Make sure this is *our* artificial EOF token. 650 if (Tok.getEofData() == MI.Field) 651 ConsumeAnyToken(); 652 } 653 654 /// Wrapper class which calls ParseLexedAttribute, after setting up the 655 /// scope appropriately. 656 void Parser::ParseLexedAttributes(ParsingClass &Class) { 657 ReenterClassScopeRAII InClassScope(*this, Class); 658 659 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations) 660 LateD->ParseLexedAttributes(); 661 } 662 663 /// Parse all attributes in LAs, and attach them to Decl D. 664 void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, 665 bool EnterScope, bool OnDefinition) { 666 assert(LAs.parseSoon() && 667 "Attribute list should be marked for immediate parsing."); 668 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { 669 if (D) 670 LAs[i]->addDecl(D); 671 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); 672 delete LAs[i]; 673 } 674 LAs.clear(); 675 } 676 677 /// Finish parsing an attribute for which parsing was delayed. 678 /// This will be called at the end of parsing a class declaration 679 /// for each LateParsedAttribute. We consume the saved tokens and 680 /// create an attribute with the arguments filled in. We add this 681 /// to the Attribute list for the decl. 682 void Parser::ParseLexedAttribute(LateParsedAttribute &LA, 683 bool EnterScope, bool OnDefinition) { 684 // Create a fake EOF so that attribute parsing won't go off the end of the 685 // attribute. 686 Token AttrEnd; 687 AttrEnd.startToken(); 688 AttrEnd.setKind(tok::eof); 689 AttrEnd.setLocation(Tok.getLocation()); 690 AttrEnd.setEofData(LA.Toks.data()); 691 LA.Toks.push_back(AttrEnd); 692 693 // Append the current token at the end of the new token stream so that it 694 // doesn't get lost. 695 LA.Toks.push_back(Tok); 696 PP.EnterTokenStream(LA.Toks, true, /*IsReinject=*/true); 697 // Consume the previously pushed token. 698 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 699 700 ParsedAttributes Attrs(AttrFactory); 701 SourceLocation endLoc; 702 703 if (LA.Decls.size() > 0) { 704 Decl *D = LA.Decls[0]; 705 NamedDecl *ND = dyn_cast<NamedDecl>(D); 706 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); 707 708 // Allow 'this' within late-parsed attributes. 709 Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(), 710 ND && ND->isCXXInstanceMember()); 711 712 if (LA.Decls.size() == 1) { 713 // If the Decl is templatized, add template parameters to scope. 714 ReenterTemplateScopeRAII InDeclScope(*this, D, EnterScope); 715 716 // If the Decl is on a function, add function parameters to the scope. 717 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); 718 if (HasFunScope) { 719 InDeclScope.Scopes.Enter(Scope::FnScope | Scope::DeclScope | 720 Scope::CompoundStmtScope); 721 Actions.ActOnReenterFunctionContext(Actions.CurScope, D); 722 } 723 724 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, 725 nullptr, SourceLocation(), ParsedAttr::AS_GNU, 726 nullptr); 727 728 if (HasFunScope) 729 Actions.ActOnExitFunctionContext(); 730 } else { 731 // If there are multiple decls, then the decl cannot be within the 732 // function scope. 733 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, 734 nullptr, SourceLocation(), ParsedAttr::AS_GNU, 735 nullptr); 736 } 737 } else { 738 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); 739 } 740 741 if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() && 742 Attrs.begin()->isKnownToGCC()) 743 Diag(Tok, diag::warn_attribute_on_function_definition) 744 << &LA.AttrName; 745 746 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) 747 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); 748 749 // Due to a parsing error, we either went over the cached tokens or 750 // there are still cached tokens left, so we skip the leftover tokens. 751 while (Tok.isNot(tok::eof)) 752 ConsumeAnyToken(); 753 754 if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()) 755 ConsumeAnyToken(); 756 } 757 758 void Parser::ParseLexedPragmas(ParsingClass &Class) { 759 ReenterClassScopeRAII InClassScope(*this, Class); 760 761 for (LateParsedDeclaration *D : Class.LateParsedDeclarations) 762 D->ParseLexedPragmas(); 763 } 764 765 void Parser::ParseLexedPragma(LateParsedPragma &LP) { 766 PP.EnterToken(Tok, /*IsReinject=*/true); 767 PP.EnterTokenStream(LP.toks(), /*DisableMacroExpansion=*/true, 768 /*IsReinject=*/true); 769 770 // Consume the previously pushed token. 771 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 772 assert(Tok.isAnnotation() && "Expected annotation token."); 773 switch (Tok.getKind()) { 774 case tok::annot_pragma_openmp: { 775 AccessSpecifier AS = LP.getAccessSpecifier(); 776 ParsedAttributesWithRange Attrs(AttrFactory); 777 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); 778 break; 779 } 780 default: 781 llvm_unreachable("Unexpected token."); 782 } 783 } 784 785 /// ConsumeAndStoreUntil - Consume and store the token at the passed token 786 /// container until the token 'T' is reached (which gets 787 /// consumed/stored too, if ConsumeFinalToken). 788 /// If StopAtSemi is true, then we will stop early at a ';' character. 789 /// Returns true if token 'T1' or 'T2' was found. 790 /// NOTE: This is a specialized version of Parser::SkipUntil. 791 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, 792 CachedTokens &Toks, 793 bool StopAtSemi, bool ConsumeFinalToken) { 794 // We always want this function to consume at least one token if the first 795 // token isn't T and if not at EOF. 796 bool isFirstTokenConsumed = true; 797 while (1) { 798 // If we found one of the tokens, stop and return true. 799 if (Tok.is(T1) || Tok.is(T2)) { 800 if (ConsumeFinalToken) { 801 Toks.push_back(Tok); 802 ConsumeAnyToken(); 803 } 804 return true; 805 } 806 807 switch (Tok.getKind()) { 808 case tok::eof: 809 case tok::annot_module_begin: 810 case tok::annot_module_end: 811 case tok::annot_module_include: 812 // Ran out of tokens. 813 return false; 814 815 case tok::l_paren: 816 // Recursively consume properly-nested parens. 817 Toks.push_back(Tok); 818 ConsumeParen(); 819 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 820 break; 821 case tok::l_square: 822 // Recursively consume properly-nested square brackets. 823 Toks.push_back(Tok); 824 ConsumeBracket(); 825 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 826 break; 827 case tok::l_brace: 828 // Recursively consume properly-nested braces. 829 Toks.push_back(Tok); 830 ConsumeBrace(); 831 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 832 break; 833 834 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 835 // Since the user wasn't looking for this token (if they were, it would 836 // already be handled), this isn't balanced. If there is a LHS token at a 837 // higher level, we will assume that this matches the unbalanced token 838 // and return it. Otherwise, this is a spurious RHS token, which we skip. 839 case tok::r_paren: 840 if (ParenCount && !isFirstTokenConsumed) 841 return false; // Matches something. 842 Toks.push_back(Tok); 843 ConsumeParen(); 844 break; 845 case tok::r_square: 846 if (BracketCount && !isFirstTokenConsumed) 847 return false; // Matches something. 848 Toks.push_back(Tok); 849 ConsumeBracket(); 850 break; 851 case tok::r_brace: 852 if (BraceCount && !isFirstTokenConsumed) 853 return false; // Matches something. 854 Toks.push_back(Tok); 855 ConsumeBrace(); 856 break; 857 858 case tok::semi: 859 if (StopAtSemi) 860 return false; 861 LLVM_FALLTHROUGH; 862 default: 863 // consume this token. 864 Toks.push_back(Tok); 865 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true); 866 break; 867 } 868 isFirstTokenConsumed = false; 869 } 870 } 871 872 /// Consume tokens and store them in the passed token container until 873 /// we've passed the try keyword and constructor initializers and have consumed 874 /// the opening brace of the function body. The opening brace will be consumed 875 /// if and only if there was no error. 876 /// 877 /// \return True on error. 878 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { 879 if (Tok.is(tok::kw_try)) { 880 Toks.push_back(Tok); 881 ConsumeToken(); 882 } 883 884 if (Tok.isNot(tok::colon)) { 885 // Easy case, just a function body. 886 887 // Grab any remaining garbage to be diagnosed later. We stop when we reach a 888 // brace: an opening one is the function body, while a closing one probably 889 // means we've reached the end of the class. 890 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, 891 /*StopAtSemi=*/true, 892 /*ConsumeFinalToken=*/false); 893 if (Tok.isNot(tok::l_brace)) 894 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; 895 896 Toks.push_back(Tok); 897 ConsumeBrace(); 898 return false; 899 } 900 901 Toks.push_back(Tok); 902 ConsumeToken(); 903 904 // We can't reliably skip over a mem-initializer-id, because it could be 905 // a template-id involving not-yet-declared names. Given: 906 // 907 // S ( ) : a < b < c > ( e ) 908 // 909 // 'e' might be an initializer or part of a template argument, depending 910 // on whether 'b' is a template. 911 912 // Track whether we might be inside a template argument. We can give 913 // significantly better diagnostics if we know that we're not. 914 bool MightBeTemplateArgument = false; 915 916 while (true) { 917 // Skip over the mem-initializer-id, if possible. 918 if (Tok.is(tok::kw_decltype)) { 919 Toks.push_back(Tok); 920 SourceLocation OpenLoc = ConsumeToken(); 921 if (Tok.isNot(tok::l_paren)) 922 return Diag(Tok.getLocation(), diag::err_expected_lparen_after) 923 << "decltype"; 924 Toks.push_back(Tok); 925 ConsumeParen(); 926 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) { 927 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; 928 Diag(OpenLoc, diag::note_matching) << tok::l_paren; 929 return true; 930 } 931 } 932 do { 933 // Walk over a component of a nested-name-specifier. 934 if (Tok.is(tok::coloncolon)) { 935 Toks.push_back(Tok); 936 ConsumeToken(); 937 938 if (Tok.is(tok::kw_template)) { 939 Toks.push_back(Tok); 940 ConsumeToken(); 941 } 942 } 943 944 if (Tok.is(tok::identifier)) { 945 Toks.push_back(Tok); 946 ConsumeToken(); 947 } else { 948 break; 949 } 950 } while (Tok.is(tok::coloncolon)); 951 952 if (Tok.is(tok::code_completion)) { 953 Toks.push_back(Tok); 954 ConsumeCodeCompletionToken(); 955 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) { 956 // Could be the start of another member initializer (the ',' has not 957 // been written yet) 958 continue; 959 } 960 } 961 962 if (Tok.is(tok::comma)) { 963 // The initialization is missing, we'll diagnose it later. 964 Toks.push_back(Tok); 965 ConsumeToken(); 966 continue; 967 } 968 if (Tok.is(tok::less)) 969 MightBeTemplateArgument = true; 970 971 if (MightBeTemplateArgument) { 972 // We may be inside a template argument list. Grab up to the start of the 973 // next parenthesized initializer or braced-init-list. This *might* be the 974 // initializer, or it might be a subexpression in the template argument 975 // list. 976 // FIXME: Count angle brackets, and clear MightBeTemplateArgument 977 // if all angles are closed. 978 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, 979 /*StopAtSemi=*/true, 980 /*ConsumeFinalToken=*/false)) { 981 // We're not just missing the initializer, we're also missing the 982 // function body! 983 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; 984 } 985 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) { 986 // We found something weird in a mem-initializer-id. 987 if (getLangOpts().CPlusPlus11) 988 return Diag(Tok.getLocation(), diag::err_expected_either) 989 << tok::l_paren << tok::l_brace; 990 else 991 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; 992 } 993 994 tok::TokenKind kind = Tok.getKind(); 995 Toks.push_back(Tok); 996 bool IsLParen = (kind == tok::l_paren); 997 SourceLocation OpenLoc = Tok.getLocation(); 998 999 if (IsLParen) { 1000 ConsumeParen(); 1001 } else { 1002 assert(kind == tok::l_brace && "Must be left paren or brace here."); 1003 ConsumeBrace(); 1004 // In C++03, this has to be the start of the function body, which 1005 // means the initializer is malformed; we'll diagnose it later. 1006 if (!getLangOpts().CPlusPlus11) 1007 return false; 1008 1009 const Token &PreviousToken = Toks[Toks.size() - 2]; 1010 if (!MightBeTemplateArgument && 1011 !PreviousToken.isOneOf(tok::identifier, tok::greater, 1012 tok::greatergreater)) { 1013 // If the opening brace is not preceded by one of these tokens, we are 1014 // missing the mem-initializer-id. In order to recover better, we need 1015 // to use heuristics to determine if this '{' is most likely the 1016 // beginning of a brace-init-list or the function body. 1017 // Check the token after the corresponding '}'. 1018 TentativeParsingAction PA(*this); 1019 if (SkipUntil(tok::r_brace) && 1020 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) { 1021 // Consider there was a malformed initializer and this is the start 1022 // of the function body. We'll diagnose it later. 1023 PA.Revert(); 1024 return false; 1025 } 1026 PA.Revert(); 1027 } 1028 } 1029 1030 // Grab the initializer (or the subexpression of the template argument). 1031 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false 1032 // if we might be inside the braces of a lambda-expression. 1033 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace; 1034 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) { 1035 Diag(Tok, diag::err_expected) << CloseKind; 1036 Diag(OpenLoc, diag::note_matching) << kind; 1037 return true; 1038 } 1039 1040 // Grab pack ellipsis, if present. 1041 if (Tok.is(tok::ellipsis)) { 1042 Toks.push_back(Tok); 1043 ConsumeToken(); 1044 } 1045 1046 // If we know we just consumed a mem-initializer, we must have ',' or '{' 1047 // next. 1048 if (Tok.is(tok::comma)) { 1049 Toks.push_back(Tok); 1050 ConsumeToken(); 1051 } else if (Tok.is(tok::l_brace)) { 1052 // This is the function body if the ')' or '}' is immediately followed by 1053 // a '{'. That cannot happen within a template argument, apart from the 1054 // case where a template argument contains a compound literal: 1055 // 1056 // S ( ) : a < b < c > ( d ) { } 1057 // // End of declaration, or still inside the template argument? 1058 // 1059 // ... and the case where the template argument contains a lambda: 1060 // 1061 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } 1062 // ( ) > ( ) { } 1063 // 1064 // FIXME: Disambiguate these cases. Note that the latter case is probably 1065 // going to be made ill-formed by core issue 1607. 1066 Toks.push_back(Tok); 1067 ConsumeBrace(); 1068 return false; 1069 } else if (!MightBeTemplateArgument) { 1070 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace 1071 << tok::comma; 1072 } 1073 } 1074 } 1075 1076 /// Consume and store tokens from the '?' to the ':' in a conditional 1077 /// expression. 1078 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) { 1079 // Consume '?'. 1080 assert(Tok.is(tok::question)); 1081 Toks.push_back(Tok); 1082 ConsumeToken(); 1083 1084 while (Tok.isNot(tok::colon)) { 1085 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks, 1086 /*StopAtSemi=*/true, 1087 /*ConsumeFinalToken=*/false)) 1088 return false; 1089 1090 // If we found a nested conditional, consume it. 1091 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks)) 1092 return false; 1093 } 1094 1095 // Consume ':'. 1096 Toks.push_back(Tok); 1097 ConsumeToken(); 1098 return true; 1099 } 1100 1101 /// A tentative parsing action that can also revert token annotations. 1102 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction { 1103 public: 1104 explicit UnannotatedTentativeParsingAction(Parser &Self, 1105 tok::TokenKind EndKind) 1106 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) { 1107 // Stash away the old token stream, so we can restore it once the 1108 // tentative parse is complete. 1109 TentativeParsingAction Inner(Self); 1110 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false); 1111 Inner.Revert(); 1112 } 1113 1114 void RevertAnnotations() { 1115 Revert(); 1116 1117 // Put back the original tokens. 1118 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch); 1119 if (Toks.size()) { 1120 auto Buffer = std::make_unique<Token[]>(Toks.size()); 1121 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get()); 1122 Buffer[Toks.size() - 1] = Self.Tok; 1123 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true, 1124 /*IsReinject*/ true); 1125 1126 Self.Tok = Toks.front(); 1127 } 1128 } 1129 1130 private: 1131 Parser &Self; 1132 CachedTokens Toks; 1133 tok::TokenKind EndKind; 1134 }; 1135 1136 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token 1137 /// container until the end of the current initializer expression (either a 1138 /// default argument or an in-class initializer for a non-static data member). 1139 /// 1140 /// Returns \c true if we reached the end of something initializer-shaped, 1141 /// \c false if we bailed out. 1142 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks, 1143 CachedInitKind CIK) { 1144 // We always want this function to consume at least one token if not at EOF. 1145 bool IsFirstToken = true; 1146 1147 // Number of possible unclosed <s we've seen so far. These might be templates, 1148 // and might not, but if there were none of them (or we know for sure that 1149 // we're within a template), we can avoid a tentative parse. 1150 unsigned AngleCount = 0; 1151 unsigned KnownTemplateCount = 0; 1152 1153 while (1) { 1154 switch (Tok.getKind()) { 1155 case tok::comma: 1156 // If we might be in a template, perform a tentative parse to check. 1157 if (!AngleCount) 1158 // Not a template argument: this is the end of the initializer. 1159 return true; 1160 if (KnownTemplateCount) 1161 goto consume_token; 1162 1163 // We hit a comma inside angle brackets. This is the hard case. The 1164 // rule we follow is: 1165 // * For a default argument, if the tokens after the comma form a 1166 // syntactically-valid parameter-declaration-clause, in which each 1167 // parameter has an initializer, then this comma ends the default 1168 // argument. 1169 // * For a default initializer, if the tokens after the comma form a 1170 // syntactically-valid init-declarator-list, then this comma ends 1171 // the default initializer. 1172 { 1173 UnannotatedTentativeParsingAction PA(*this, 1174 CIK == CIK_DefaultInitializer 1175 ? tok::semi : tok::r_paren); 1176 Sema::TentativeAnalysisScope Scope(Actions); 1177 1178 TPResult Result = TPResult::Error; 1179 ConsumeToken(); 1180 switch (CIK) { 1181 case CIK_DefaultInitializer: 1182 Result = TryParseInitDeclaratorList(); 1183 // If we parsed a complete, ambiguous init-declarator-list, this 1184 // is only syntactically-valid if it's followed by a semicolon. 1185 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi)) 1186 Result = TPResult::False; 1187 break; 1188 1189 case CIK_DefaultArgument: 1190 bool InvalidAsDeclaration = false; 1191 Result = TryParseParameterDeclarationClause( 1192 &InvalidAsDeclaration, /*VersusTemplateArg=*/true); 1193 // If this is an expression or a declaration with a missing 1194 // 'typename', assume it's not a declaration. 1195 if (Result == TPResult::Ambiguous && InvalidAsDeclaration) 1196 Result = TPResult::False; 1197 break; 1198 } 1199 1200 // Put the token stream back and undo any annotations we performed 1201 // after the comma. They may reflect a different parse than the one 1202 // we will actually perform at the end of the class. 1203 PA.RevertAnnotations(); 1204 1205 // If what follows could be a declaration, it is a declaration. 1206 if (Result != TPResult::False && Result != TPResult::Error) 1207 return true; 1208 } 1209 1210 // Keep going. We know we're inside a template argument list now. 1211 ++KnownTemplateCount; 1212 goto consume_token; 1213 1214 case tok::eof: 1215 case tok::annot_module_begin: 1216 case tok::annot_module_end: 1217 case tok::annot_module_include: 1218 // Ran out of tokens. 1219 return false; 1220 1221 case tok::less: 1222 // FIXME: A '<' can only start a template-id if it's preceded by an 1223 // identifier, an operator-function-id, or a literal-operator-id. 1224 ++AngleCount; 1225 goto consume_token; 1226 1227 case tok::question: 1228 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does, 1229 // that is *never* the end of the initializer. Skip to the ':'. 1230 if (!ConsumeAndStoreConditional(Toks)) 1231 return false; 1232 break; 1233 1234 case tok::greatergreatergreater: 1235 if (!getLangOpts().CPlusPlus11) 1236 goto consume_token; 1237 if (AngleCount) --AngleCount; 1238 if (KnownTemplateCount) --KnownTemplateCount; 1239 LLVM_FALLTHROUGH; 1240 case tok::greatergreater: 1241 if (!getLangOpts().CPlusPlus11) 1242 goto consume_token; 1243 if (AngleCount) --AngleCount; 1244 if (KnownTemplateCount) --KnownTemplateCount; 1245 LLVM_FALLTHROUGH; 1246 case tok::greater: 1247 if (AngleCount) --AngleCount; 1248 if (KnownTemplateCount) --KnownTemplateCount; 1249 goto consume_token; 1250 1251 case tok::kw_template: 1252 // 'template' identifier '<' is known to start a template argument list, 1253 // and can be used to disambiguate the parse. 1254 // FIXME: Support all forms of 'template' unqualified-id '<'. 1255 Toks.push_back(Tok); 1256 ConsumeToken(); 1257 if (Tok.is(tok::identifier)) { 1258 Toks.push_back(Tok); 1259 ConsumeToken(); 1260 if (Tok.is(tok::less)) { 1261 ++AngleCount; 1262 ++KnownTemplateCount; 1263 Toks.push_back(Tok); 1264 ConsumeToken(); 1265 } 1266 } 1267 break; 1268 1269 case tok::kw_operator: 1270 // If 'operator' precedes other punctuation, that punctuation loses 1271 // its special behavior. 1272 Toks.push_back(Tok); 1273 ConsumeToken(); 1274 switch (Tok.getKind()) { 1275 case tok::comma: 1276 case tok::greatergreatergreater: 1277 case tok::greatergreater: 1278 case tok::greater: 1279 case tok::less: 1280 Toks.push_back(Tok); 1281 ConsumeToken(); 1282 break; 1283 default: 1284 break; 1285 } 1286 break; 1287 1288 case tok::l_paren: 1289 // Recursively consume properly-nested parens. 1290 Toks.push_back(Tok); 1291 ConsumeParen(); 1292 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 1293 break; 1294 case tok::l_square: 1295 // Recursively consume properly-nested square brackets. 1296 Toks.push_back(Tok); 1297 ConsumeBracket(); 1298 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 1299 break; 1300 case tok::l_brace: 1301 // Recursively consume properly-nested braces. 1302 Toks.push_back(Tok); 1303 ConsumeBrace(); 1304 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1305 break; 1306 1307 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 1308 // Since the user wasn't looking for this token (if they were, it would 1309 // already be handled), this isn't balanced. If there is a LHS token at a 1310 // higher level, we will assume that this matches the unbalanced token 1311 // and return it. Otherwise, this is a spurious RHS token, which we 1312 // consume and pass on to downstream code to diagnose. 1313 case tok::r_paren: 1314 if (CIK == CIK_DefaultArgument) 1315 return true; // End of the default argument. 1316 if (ParenCount && !IsFirstToken) 1317 return false; 1318 Toks.push_back(Tok); 1319 ConsumeParen(); 1320 continue; 1321 case tok::r_square: 1322 if (BracketCount && !IsFirstToken) 1323 return false; 1324 Toks.push_back(Tok); 1325 ConsumeBracket(); 1326 continue; 1327 case tok::r_brace: 1328 if (BraceCount && !IsFirstToken) 1329 return false; 1330 Toks.push_back(Tok); 1331 ConsumeBrace(); 1332 continue; 1333 1334 case tok::code_completion: 1335 Toks.push_back(Tok); 1336 ConsumeCodeCompletionToken(); 1337 break; 1338 1339 case tok::string_literal: 1340 case tok::wide_string_literal: 1341 case tok::utf8_string_literal: 1342 case tok::utf16_string_literal: 1343 case tok::utf32_string_literal: 1344 Toks.push_back(Tok); 1345 ConsumeStringToken(); 1346 break; 1347 case tok::semi: 1348 if (CIK == CIK_DefaultInitializer) 1349 return true; // End of the default initializer. 1350 LLVM_FALLTHROUGH; 1351 default: 1352 consume_token: 1353 Toks.push_back(Tok); 1354 ConsumeToken(); 1355 break; 1356 } 1357 IsFirstToken = false; 1358 } 1359 } 1360