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