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