1 //===--- Parser.cpp - C Language Family Parser ----------------------------===// 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 the Parser interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Parse/Parser.h" 14 #include "clang/AST/ASTConsumer.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTLambda.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/Basic/DiagnosticParse.h" 19 #include "clang/Basic/StackExhaustionHandler.h" 20 #include "clang/Parse/RAIIObjectsForParser.h" 21 #include "clang/Sema/DeclSpec.h" 22 #include "clang/Sema/EnterExpressionEvaluationContext.h" 23 #include "clang/Sema/ParsedTemplate.h" 24 #include "clang/Sema/Scope.h" 25 #include "clang/Sema/SemaCodeCompletion.h" 26 #include "llvm/ADT/STLForwardCompat.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/TimeProfiler.h" 29 using namespace clang; 30 31 32 namespace { 33 /// A comment handler that passes comments found by the preprocessor 34 /// to the parser action. 35 class ActionCommentHandler : public CommentHandler { 36 Sema &S; 37 38 public: 39 explicit ActionCommentHandler(Sema &S) : S(S) { } 40 41 bool HandleComment(Preprocessor &PP, SourceRange Comment) override { 42 S.ActOnComment(Comment); 43 return false; 44 } 45 }; 46 } // end anonymous namespace 47 48 IdentifierInfo *Parser::getSEHExceptKeyword() { 49 // __except is accepted as a (contextual) keyword 50 if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland)) 51 Ident__except = PP.getIdentifierInfo("__except"); 52 53 return Ident__except; 54 } 55 56 Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies) 57 : PP(pp), 58 PreferredType(&actions.getASTContext(), pp.isCodeCompletionEnabled()), 59 Actions(actions), Diags(PP.getDiagnostics()), StackHandler(Diags), 60 GreaterThanIsOperator(true), ColonIsSacred(false), 61 InMessageExpression(false), ParsingInObjCContainer(false), 62 TemplateParameterDepth(0) { 63 SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies; 64 Tok.startToken(); 65 Tok.setKind(tok::eof); 66 Actions.CurScope = nullptr; 67 NumCachedScopes = 0; 68 CurParsedObjCImpl = nullptr; 69 70 // Add #pragma handlers. These are removed and destroyed in the 71 // destructor. 72 initializePragmaHandlers(); 73 74 CommentSemaHandler.reset(new ActionCommentHandler(actions)); 75 PP.addCommentHandler(CommentSemaHandler.get()); 76 77 PP.setCodeCompletionHandler(*this); 78 79 Actions.ParseTypeFromStringCallback = 80 [this](StringRef TypeStr, StringRef Context, SourceLocation IncludeLoc) { 81 return this->ParseTypeFromString(TypeStr, Context, IncludeLoc); 82 }; 83 } 84 85 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 86 return Diags.Report(Loc, DiagID); 87 } 88 89 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 90 return Diag(Tok.getLocation(), DiagID); 91 } 92 93 DiagnosticBuilder Parser::DiagCompat(SourceLocation Loc, 94 unsigned CompatDiagId) { 95 return Diag(Loc, 96 DiagnosticIDs::getCXXCompatDiagId(getLangOpts(), CompatDiagId)); 97 } 98 99 DiagnosticBuilder Parser::DiagCompat(const Token &Tok, unsigned CompatDiagId) { 100 return DiagCompat(Tok.getLocation(), CompatDiagId); 101 } 102 103 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 104 SourceRange ParenRange) { 105 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 106 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 107 // We can't display the parentheses, so just dig the 108 // warning/error and return. 109 Diag(Loc, DK); 110 return; 111 } 112 113 Diag(Loc, DK) 114 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 115 << FixItHint::CreateInsertion(EndLoc, ")"); 116 } 117 118 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) { 119 switch (ExpectedTok) { 120 case tok::semi: 121 return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ; 122 default: return false; 123 } 124 } 125 126 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 127 StringRef Msg) { 128 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 129 ConsumeAnyToken(); 130 return false; 131 } 132 133 // Detect common single-character typos and resume. 134 if (IsCommonTypo(ExpectedTok, Tok)) { 135 SourceLocation Loc = Tok.getLocation(); 136 { 137 DiagnosticBuilder DB = Diag(Loc, DiagID); 138 DB << FixItHint::CreateReplacement( 139 SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok)); 140 if (DiagID == diag::err_expected) 141 DB << ExpectedTok; 142 else if (DiagID == diag::err_expected_after) 143 DB << Msg << ExpectedTok; 144 else 145 DB << Msg; 146 } 147 148 // Pretend there wasn't a problem. 149 ConsumeAnyToken(); 150 return false; 151 } 152 153 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 154 const char *Spelling = nullptr; 155 if (EndLoc.isValid()) 156 Spelling = tok::getPunctuatorSpelling(ExpectedTok); 157 158 DiagnosticBuilder DB = 159 Spelling 160 ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling) 161 : Diag(Tok, DiagID); 162 if (DiagID == diag::err_expected) 163 DB << ExpectedTok; 164 else if (DiagID == diag::err_expected_after) 165 DB << Msg << ExpectedTok; 166 else 167 DB << Msg; 168 169 return true; 170 } 171 172 bool Parser::ExpectAndConsumeSemi(unsigned DiagID, StringRef TokenUsed) { 173 if (TryConsumeToken(tok::semi)) 174 return false; 175 176 if (Tok.is(tok::code_completion)) { 177 handleUnexpectedCodeCompletionToken(); 178 return false; 179 } 180 181 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 182 NextToken().is(tok::semi)) { 183 Diag(Tok, diag::err_extraneous_token_before_semi) 184 << PP.getSpelling(Tok) 185 << FixItHint::CreateRemoval(Tok.getLocation()); 186 ConsumeAnyToken(); // The ')' or ']'. 187 ConsumeToken(); // The ';'. 188 return false; 189 } 190 191 return ExpectAndConsume(tok::semi, DiagID , TokenUsed); 192 } 193 194 void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) { 195 if (!Tok.is(tok::semi)) return; 196 197 bool HadMultipleSemis = false; 198 SourceLocation StartLoc = Tok.getLocation(); 199 SourceLocation EndLoc = Tok.getLocation(); 200 ConsumeToken(); 201 202 while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) { 203 HadMultipleSemis = true; 204 EndLoc = Tok.getLocation(); 205 ConsumeToken(); 206 } 207 208 // C++11 allows extra semicolons at namespace scope, but not in any of the 209 // other contexts. 210 if (Kind == ExtraSemiKind::OutsideFunction && getLangOpts().CPlusPlus) { 211 if (getLangOpts().CPlusPlus11) 212 Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi) 213 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 214 else 215 Diag(StartLoc, diag::ext_extra_semi_cxx11) 216 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 217 return; 218 } 219 220 if (Kind != ExtraSemiKind::AfterMemberFunctionDefinition || HadMultipleSemis) 221 Diag(StartLoc, diag::ext_extra_semi) 222 << Kind 223 << DeclSpec::getSpecifierName( 224 TST, Actions.getASTContext().getPrintingPolicy()) 225 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 226 else 227 // A single semicolon is valid after a member function definition. 228 Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def) 229 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 230 } 231 232 bool Parser::expectIdentifier() { 233 if (Tok.is(tok::identifier)) 234 return false; 235 if (const auto *II = Tok.getIdentifierInfo()) { 236 if (II->isCPlusPlusKeyword(getLangOpts())) { 237 Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword) 238 << tok::identifier << Tok.getIdentifierInfo(); 239 // Objective-C++: Recover by treating this keyword as a valid identifier. 240 return false; 241 } 242 } 243 Diag(Tok, diag::err_expected) << tok::identifier; 244 return true; 245 } 246 247 void Parser::checkCompoundToken(SourceLocation FirstTokLoc, 248 tok::TokenKind FirstTokKind, CompoundToken Op) { 249 if (FirstTokLoc.isInvalid()) 250 return; 251 SourceLocation SecondTokLoc = Tok.getLocation(); 252 253 // If either token is in a macro, we expect both tokens to come from the same 254 // macro expansion. 255 if ((FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID()) && 256 PP.getSourceManager().getFileID(FirstTokLoc) != 257 PP.getSourceManager().getFileID(SecondTokLoc)) { 258 Diag(FirstTokLoc, diag::warn_compound_token_split_by_macro) 259 << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind() 260 << static_cast<int>(Op) << SourceRange(FirstTokLoc); 261 Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here) 262 << (FirstTokKind == Tok.getKind()) << Tok.getKind() 263 << SourceRange(SecondTokLoc); 264 return; 265 } 266 267 // We expect the tokens to abut. 268 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { 269 SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc); 270 if (SpaceLoc.isInvalid()) 271 SpaceLoc = FirstTokLoc; 272 Diag(SpaceLoc, diag::warn_compound_token_split_by_whitespace) 273 << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind() 274 << static_cast<int>(Op) << SourceRange(FirstTokLoc, SecondTokLoc); 275 return; 276 } 277 } 278 279 //===----------------------------------------------------------------------===// 280 // Error recovery. 281 //===----------------------------------------------------------------------===// 282 283 static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) { 284 return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0; 285 } 286 287 bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) { 288 // We always want this function to skip at least one token if the first token 289 // isn't T and if not at EOF. 290 bool isFirstTokenSkipped = true; 291 while (true) { 292 // If we found one of the tokens, stop and return true. 293 for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) { 294 if (Tok.is(Toks[i])) { 295 if (HasFlagsSet(Flags, StopBeforeMatch)) { 296 // Noop, don't consume the token. 297 } else { 298 ConsumeAnyToken(); 299 } 300 return true; 301 } 302 } 303 304 // Important special case: The caller has given up and just wants us to 305 // skip the rest of the file. Do this without recursing, since we can 306 // get here precisely because the caller detected too much recursion. 307 if (Toks.size() == 1 && Toks[0] == tok::eof && 308 !HasFlagsSet(Flags, StopAtSemi) && 309 !HasFlagsSet(Flags, StopAtCodeCompletion)) { 310 while (Tok.isNot(tok::eof)) 311 ConsumeAnyToken(); 312 return true; 313 } 314 315 switch (Tok.getKind()) { 316 case tok::eof: 317 // Ran out of tokens. 318 return false; 319 320 case tok::annot_pragma_openmp: 321 case tok::annot_attr_openmp: 322 case tok::annot_pragma_openmp_end: 323 // Stop before an OpenMP pragma boundary. 324 if (OpenMPDirectiveParsing) 325 return false; 326 ConsumeAnnotationToken(); 327 break; 328 case tok::annot_pragma_openacc: 329 case tok::annot_pragma_openacc_end: 330 // Stop before an OpenACC pragma boundary. 331 if (OpenACCDirectiveParsing) 332 return false; 333 ConsumeAnnotationToken(); 334 break; 335 case tok::annot_module_begin: 336 case tok::annot_module_end: 337 case tok::annot_module_include: 338 case tok::annot_repl_input_end: 339 // Stop before we change submodules. They generally indicate a "good" 340 // place to pick up parsing again (except in the special case where 341 // we're trying to skip to EOF). 342 return false; 343 344 case tok::code_completion: 345 if (!HasFlagsSet(Flags, StopAtCodeCompletion)) 346 handleUnexpectedCodeCompletionToken(); 347 return false; 348 349 case tok::l_paren: 350 // Recursively skip properly-nested parens. 351 ConsumeParen(); 352 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 353 SkipUntil(tok::r_paren, StopAtCodeCompletion); 354 else 355 SkipUntil(tok::r_paren); 356 break; 357 case tok::l_square: 358 // Recursively skip properly-nested square brackets. 359 ConsumeBracket(); 360 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 361 SkipUntil(tok::r_square, StopAtCodeCompletion); 362 else 363 SkipUntil(tok::r_square); 364 break; 365 case tok::l_brace: 366 // Recursively skip properly-nested braces. 367 ConsumeBrace(); 368 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 369 SkipUntil(tok::r_brace, StopAtCodeCompletion); 370 else 371 SkipUntil(tok::r_brace); 372 break; 373 case tok::question: 374 // Recursively skip ? ... : pairs; these function as brackets. But 375 // still stop at a semicolon if requested. 376 ConsumeToken(); 377 SkipUntil(tok::colon, 378 SkipUntilFlags(unsigned(Flags) & 379 unsigned(StopAtCodeCompletion | StopAtSemi))); 380 break; 381 382 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 383 // Since the user wasn't looking for this token (if they were, it would 384 // already be handled), this isn't balanced. If there is a LHS token at a 385 // higher level, we will assume that this matches the unbalanced token 386 // and return it. Otherwise, this is a spurious RHS token, which we skip. 387 case tok::r_paren: 388 if (ParenCount && !isFirstTokenSkipped) 389 return false; // Matches something. 390 ConsumeParen(); 391 break; 392 case tok::r_square: 393 if (BracketCount && !isFirstTokenSkipped) 394 return false; // Matches something. 395 ConsumeBracket(); 396 break; 397 case tok::r_brace: 398 if (BraceCount && !isFirstTokenSkipped) 399 return false; // Matches something. 400 ConsumeBrace(); 401 break; 402 403 case tok::semi: 404 if (HasFlagsSet(Flags, StopAtSemi)) 405 return false; 406 [[fallthrough]]; 407 default: 408 // Skip this token. 409 ConsumeAnyToken(); 410 break; 411 } 412 isFirstTokenSkipped = false; 413 } 414 } 415 416 //===----------------------------------------------------------------------===// 417 // Scope manipulation 418 //===----------------------------------------------------------------------===// 419 420 void Parser::EnterScope(unsigned ScopeFlags) { 421 if (NumCachedScopes) { 422 Scope *N = ScopeCache[--NumCachedScopes]; 423 N->Init(getCurScope(), ScopeFlags); 424 Actions.CurScope = N; 425 } else { 426 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags); 427 } 428 } 429 430 void Parser::ExitScope() { 431 assert(getCurScope() && "Scope imbalance!"); 432 433 // Inform the actions module that this scope is going away if there are any 434 // decls in it. 435 Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 436 437 Scope *OldScope = getCurScope(); 438 Actions.CurScope = OldScope->getParent(); 439 440 if (NumCachedScopes == ScopeCacheSize) 441 delete OldScope; 442 else 443 ScopeCache[NumCachedScopes++] = OldScope; 444 } 445 446 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags, 447 bool ManageFlags) 448 : CurScope(ManageFlags ? Self->getCurScope() : nullptr) { 449 if (CurScope) { 450 OldFlags = CurScope->getFlags(); 451 CurScope->setFlags(ScopeFlags); 452 } 453 } 454 455 Parser::ParseScopeFlags::~ParseScopeFlags() { 456 if (CurScope) 457 CurScope->setFlags(OldFlags); 458 } 459 460 461 //===----------------------------------------------------------------------===// 462 // C99 6.9: External Definitions. 463 //===----------------------------------------------------------------------===// 464 465 Parser::~Parser() { 466 // If we still have scopes active, delete the scope tree. 467 delete getCurScope(); 468 Actions.CurScope = nullptr; 469 470 // Free the scope cache. 471 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 472 delete ScopeCache[i]; 473 474 resetPragmaHandlers(); 475 476 PP.removeCommentHandler(CommentSemaHandler.get()); 477 478 PP.clearCodeCompletionHandler(); 479 480 DestroyTemplateIds(); 481 } 482 483 void Parser::Initialize() { 484 // Create the translation unit scope. Install it as the current scope. 485 assert(getCurScope() == nullptr && "A scope is already active?"); 486 EnterScope(Scope::DeclScope); 487 Actions.ActOnTranslationUnitScope(getCurScope()); 488 489 // Initialization for Objective-C context sensitive keywords recognition. 490 // Referenced in Parser::ParseObjCTypeQualifierList. 491 if (getLangOpts().ObjC) { 492 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::in)] = 493 &PP.getIdentifierTable().get("in"); 494 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::out)] = 495 &PP.getIdentifierTable().get("out"); 496 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::inout)] = 497 &PP.getIdentifierTable().get("inout"); 498 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::oneway)] = 499 &PP.getIdentifierTable().get("oneway"); 500 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::bycopy)] = 501 &PP.getIdentifierTable().get("bycopy"); 502 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::byref)] = 503 &PP.getIdentifierTable().get("byref"); 504 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::nonnull)] = 505 &PP.getIdentifierTable().get("nonnull"); 506 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::nullable)] = 507 &PP.getIdentifierTable().get("nullable"); 508 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::null_unspecified)] = 509 &PP.getIdentifierTable().get("null_unspecified"); 510 } 511 512 Ident_instancetype = nullptr; 513 Ident_final = nullptr; 514 Ident_sealed = nullptr; 515 Ident_abstract = nullptr; 516 Ident_override = nullptr; 517 Ident_trivially_relocatable_if_eligible = nullptr; 518 Ident_replaceable_if_eligible = nullptr; 519 Ident_GNU_final = nullptr; 520 Ident_import = nullptr; 521 Ident_module = nullptr; 522 523 Ident_super = &PP.getIdentifierTable().get("super"); 524 525 Ident_vector = nullptr; 526 Ident_bool = nullptr; 527 Ident_Bool = nullptr; 528 Ident_pixel = nullptr; 529 if (getLangOpts().AltiVec || getLangOpts().ZVector) { 530 Ident_vector = &PP.getIdentifierTable().get("vector"); 531 Ident_bool = &PP.getIdentifierTable().get("bool"); 532 Ident_Bool = &PP.getIdentifierTable().get("_Bool"); 533 } 534 if (getLangOpts().AltiVec) 535 Ident_pixel = &PP.getIdentifierTable().get("pixel"); 536 537 Ident_introduced = nullptr; 538 Ident_deprecated = nullptr; 539 Ident_obsoleted = nullptr; 540 Ident_unavailable = nullptr; 541 Ident_strict = nullptr; 542 Ident_replacement = nullptr; 543 544 Ident_language = Ident_defined_in = Ident_generated_declaration = Ident_USR = 545 nullptr; 546 547 Ident__except = nullptr; 548 549 Ident__exception_code = Ident__exception_info = nullptr; 550 Ident__abnormal_termination = Ident___exception_code = nullptr; 551 Ident___exception_info = Ident___abnormal_termination = nullptr; 552 Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr; 553 Ident_AbnormalTermination = nullptr; 554 555 if(getLangOpts().Borland) { 556 Ident__exception_info = PP.getIdentifierInfo("_exception_info"); 557 Ident___exception_info = PP.getIdentifierInfo("__exception_info"); 558 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation"); 559 Ident__exception_code = PP.getIdentifierInfo("_exception_code"); 560 Ident___exception_code = PP.getIdentifierInfo("__exception_code"); 561 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); 562 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); 563 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); 564 Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); 565 566 PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); 567 PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); 568 PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block); 569 PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); 570 PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); 571 PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); 572 PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); 573 PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); 574 PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); 575 } 576 577 if (getLangOpts().CPlusPlusModules) { 578 Ident_import = PP.getIdentifierInfo("import"); 579 Ident_module = PP.getIdentifierInfo("module"); 580 } 581 582 Actions.Initialize(); 583 584 // Prime the lexer look-ahead. 585 ConsumeToken(); 586 } 587 588 void Parser::DestroyTemplateIds() { 589 for (TemplateIdAnnotation *Id : TemplateIds) 590 Id->Destroy(); 591 TemplateIds.clear(); 592 } 593 594 bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result, 595 Sema::ModuleImportState &ImportState) { 596 Actions.ActOnStartOfTranslationUnit(); 597 598 // For C++20 modules, a module decl must be the first in the TU. We also 599 // need to track module imports. 600 ImportState = Sema::ModuleImportState::FirstDecl; 601 bool NoTopLevelDecls = ParseTopLevelDecl(Result, ImportState); 602 603 // C11 6.9p1 says translation units must have at least one top-level 604 // declaration. C++ doesn't have this restriction. We also don't want to 605 // complain if we have a precompiled header, although technically if the PCH 606 // is empty we should still emit the (pedantic) diagnostic. 607 // If the main file is a header, we're only pretending it's a TU; don't warn. 608 if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() && 609 !getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile) 610 Diag(diag::ext_empty_translation_unit); 611 612 return NoTopLevelDecls; 613 } 614 615 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, 616 Sema::ModuleImportState &ImportState) { 617 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); 618 619 Result = nullptr; 620 switch (Tok.getKind()) { 621 case tok::annot_pragma_unused: 622 HandlePragmaUnused(); 623 return false; 624 625 case tok::kw_export: 626 switch (NextToken().getKind()) { 627 case tok::kw_module: 628 goto module_decl; 629 630 // Note: no need to handle kw_import here. We only form kw_import under 631 // the Standard C++ Modules, and in that case 'export import' is parsed as 632 // an export-declaration containing an import-declaration. 633 634 // Recognize context-sensitive C++20 'export module' and 'export import' 635 // declarations. 636 case tok::identifier: { 637 IdentifierInfo *II = NextToken().getIdentifierInfo(); 638 if ((II == Ident_module || II == Ident_import) && 639 GetLookAheadToken(2).isNot(tok::coloncolon)) { 640 if (II == Ident_module) 641 goto module_decl; 642 else 643 goto import_decl; 644 } 645 break; 646 } 647 648 default: 649 break; 650 } 651 break; 652 653 case tok::kw_module: 654 module_decl: 655 Result = ParseModuleDecl(ImportState); 656 return false; 657 658 case tok::kw_import: 659 import_decl: { 660 Decl *ImportDecl = ParseModuleImport(SourceLocation(), ImportState); 661 Result = Actions.ConvertDeclToDeclGroup(ImportDecl); 662 return false; 663 } 664 665 case tok::annot_module_include: { 666 auto Loc = Tok.getLocation(); 667 Module *Mod = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 668 // FIXME: We need a better way to disambiguate C++ clang modules and 669 // standard C++ modules. 670 if (!getLangOpts().CPlusPlusModules || !Mod->isHeaderUnit()) 671 Actions.ActOnAnnotModuleInclude(Loc, Mod); 672 else { 673 DeclResult Import = 674 Actions.ActOnModuleImport(Loc, SourceLocation(), Loc, Mod); 675 Decl *ImportDecl = Import.isInvalid() ? nullptr : Import.get(); 676 Result = Actions.ConvertDeclToDeclGroup(ImportDecl); 677 } 678 ConsumeAnnotationToken(); 679 return false; 680 } 681 682 case tok::annot_module_begin: 683 Actions.ActOnAnnotModuleBegin( 684 Tok.getLocation(), 685 reinterpret_cast<Module *>(Tok.getAnnotationValue())); 686 ConsumeAnnotationToken(); 687 ImportState = Sema::ModuleImportState::NotACXX20Module; 688 return false; 689 690 case tok::annot_module_end: 691 Actions.ActOnAnnotModuleEnd( 692 Tok.getLocation(), 693 reinterpret_cast<Module *>(Tok.getAnnotationValue())); 694 ConsumeAnnotationToken(); 695 ImportState = Sema::ModuleImportState::NotACXX20Module; 696 return false; 697 698 case tok::eof: 699 case tok::annot_repl_input_end: 700 // Check whether -fmax-tokens= was reached. 701 if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) { 702 PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total) 703 << PP.getTokenCount() << PP.getMaxTokens(); 704 SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc(); 705 if (OverrideLoc.isValid()) { 706 PP.Diag(OverrideLoc, diag::note_max_tokens_total_override); 707 } 708 } 709 710 // Late template parsing can begin. 711 Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr, this); 712 Actions.ActOnEndOfTranslationUnit(); 713 //else don't tell Sema that we ended parsing: more input might come. 714 return true; 715 716 case tok::identifier: 717 // C++2a [basic.link]p3: 718 // A token sequence beginning with 'export[opt] module' or 719 // 'export[opt] import' and not immediately followed by '::' 720 // is never interpreted as the declaration of a top-level-declaration. 721 if ((Tok.getIdentifierInfo() == Ident_module || 722 Tok.getIdentifierInfo() == Ident_import) && 723 NextToken().isNot(tok::coloncolon)) { 724 if (Tok.getIdentifierInfo() == Ident_module) 725 goto module_decl; 726 else 727 goto import_decl; 728 } 729 break; 730 731 default: 732 break; 733 } 734 735 ParsedAttributes DeclAttrs(AttrFactory); 736 ParsedAttributes DeclSpecAttrs(AttrFactory); 737 // GNU attributes are applied to the declaration specification while the 738 // standard attributes are applied to the declaration. We parse the two 739 // attribute sets into different containters so we can apply them during 740 // the regular parsing process. 741 while (MaybeParseCXX11Attributes(DeclAttrs) || 742 MaybeParseGNUAttributes(DeclSpecAttrs)) 743 ; 744 745 Result = ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs); 746 // An empty Result might mean a line with ';' or some parsing error, ignore 747 // it. 748 if (Result) { 749 if (ImportState == Sema::ModuleImportState::FirstDecl) 750 // First decl was not modular. 751 ImportState = Sema::ModuleImportState::NotACXX20Module; 752 else if (ImportState == Sema::ModuleImportState::ImportAllowed) 753 // Non-imports disallow further imports. 754 ImportState = Sema::ModuleImportState::ImportFinished; 755 else if (ImportState == 756 Sema::ModuleImportState::PrivateFragmentImportAllowed) 757 // Non-imports disallow further imports. 758 ImportState = Sema::ModuleImportState::PrivateFragmentImportFinished; 759 } 760 return false; 761 } 762 763 Parser::DeclGroupPtrTy 764 Parser::ParseExternalDeclaration(ParsedAttributes &Attrs, 765 ParsedAttributes &DeclSpecAttrs, 766 ParsingDeclSpec *DS) { 767 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); 768 ParenBraceBracketBalancer BalancerRAIIObj(*this); 769 770 if (PP.isCodeCompletionReached()) { 771 cutOffParsing(); 772 return nullptr; 773 } 774 775 Decl *SingleDecl = nullptr; 776 switch (Tok.getKind()) { 777 case tok::annot_pragma_vis: 778 HandlePragmaVisibility(); 779 return nullptr; 780 case tok::annot_pragma_pack: 781 HandlePragmaPack(); 782 return nullptr; 783 case tok::annot_pragma_msstruct: 784 HandlePragmaMSStruct(); 785 return nullptr; 786 case tok::annot_pragma_align: 787 HandlePragmaAlign(); 788 return nullptr; 789 case tok::annot_pragma_weak: 790 HandlePragmaWeak(); 791 return nullptr; 792 case tok::annot_pragma_weakalias: 793 HandlePragmaWeakAlias(); 794 return nullptr; 795 case tok::annot_pragma_redefine_extname: 796 HandlePragmaRedefineExtname(); 797 return nullptr; 798 case tok::annot_pragma_fp_contract: 799 HandlePragmaFPContract(); 800 return nullptr; 801 case tok::annot_pragma_fenv_access: 802 case tok::annot_pragma_fenv_access_ms: 803 HandlePragmaFEnvAccess(); 804 return nullptr; 805 case tok::annot_pragma_fenv_round: 806 HandlePragmaFEnvRound(); 807 return nullptr; 808 case tok::annot_pragma_cx_limited_range: 809 HandlePragmaCXLimitedRange(); 810 return nullptr; 811 case tok::annot_pragma_float_control: 812 HandlePragmaFloatControl(); 813 return nullptr; 814 case tok::annot_pragma_fp: 815 HandlePragmaFP(); 816 break; 817 case tok::annot_pragma_opencl_extension: 818 HandlePragmaOpenCLExtension(); 819 return nullptr; 820 case tok::annot_attr_openmp: 821 case tok::annot_pragma_openmp: { 822 AccessSpecifier AS = AS_none; 823 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); 824 } 825 case tok::annot_pragma_openacc: { 826 AccessSpecifier AS = AS_none; 827 return ParseOpenACCDirectiveDecl(AS, Attrs, DeclSpec::TST_unspecified, 828 /*TagDecl=*/nullptr); 829 } 830 case tok::annot_pragma_ms_pointers_to_members: 831 HandlePragmaMSPointersToMembers(); 832 return nullptr; 833 case tok::annot_pragma_ms_vtordisp: 834 HandlePragmaMSVtorDisp(); 835 return nullptr; 836 case tok::annot_pragma_ms_pragma: 837 HandlePragmaMSPragma(); 838 return nullptr; 839 case tok::annot_pragma_dump: 840 HandlePragmaDump(); 841 return nullptr; 842 case tok::annot_pragma_attribute: 843 HandlePragmaAttribute(); 844 return nullptr; 845 case tok::semi: 846 // Either a C++11 empty-declaration or attribute-declaration. 847 SingleDecl = 848 Actions.ActOnEmptyDeclaration(getCurScope(), Attrs, Tok.getLocation()); 849 ConsumeExtraSemi(ExtraSemiKind::OutsideFunction); 850 break; 851 case tok::r_brace: 852 Diag(Tok, diag::err_extraneous_closing_brace); 853 ConsumeBrace(); 854 return nullptr; 855 case tok::eof: 856 Diag(Tok, diag::err_expected_external_declaration); 857 return nullptr; 858 case tok::kw___extension__: { 859 // __extension__ silences extension warnings in the subexpression. 860 ExtensionRAIIObject O(Diags); // Use RAII to do this. 861 ConsumeToken(); 862 return ParseExternalDeclaration(Attrs, DeclSpecAttrs); 863 } 864 case tok::kw_asm: { 865 ProhibitAttributes(Attrs); 866 867 SourceLocation StartLoc = Tok.getLocation(); 868 SourceLocation EndLoc; 869 870 ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc)); 871 872 // Check if GNU-style InlineAsm is disabled. 873 // Empty asm string is allowed because it will not introduce 874 // any assembly code. 875 if (!(getLangOpts().GNUAsm || Result.isInvalid())) { 876 const auto *SL = cast<StringLiteral>(Result.get()); 877 if (!SL->getString().trim().empty()) 878 Diag(StartLoc, diag::err_gnu_inline_asm_disabled); 879 } 880 881 ExpectAndConsume(tok::semi, diag::err_expected_after, 882 "top-level asm block"); 883 884 if (Result.isInvalid()) 885 return nullptr; 886 SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc); 887 break; 888 } 889 case tok::at: 890 return ParseObjCAtDirectives(Attrs, DeclSpecAttrs); 891 case tok::minus: 892 case tok::plus: 893 if (!getLangOpts().ObjC) { 894 Diag(Tok, diag::err_expected_external_declaration); 895 ConsumeToken(); 896 return nullptr; 897 } 898 SingleDecl = ParseObjCMethodDefinition(); 899 break; 900 case tok::code_completion: 901 cutOffParsing(); 902 if (CurParsedObjCImpl) { 903 // Code-complete Objective-C methods even without leading '-'/'+' prefix. 904 Actions.CodeCompletion().CodeCompleteObjCMethodDecl( 905 getCurScope(), 906 /*IsInstanceMethod=*/std::nullopt, 907 /*ReturnType=*/nullptr); 908 } 909 910 SemaCodeCompletion::ParserCompletionContext PCC; 911 if (CurParsedObjCImpl) { 912 PCC = SemaCodeCompletion::PCC_ObjCImplementation; 913 } else if (PP.isIncrementalProcessingEnabled()) { 914 PCC = SemaCodeCompletion::PCC_TopLevelOrExpression; 915 } else { 916 PCC = SemaCodeCompletion::PCC_Namespace; 917 }; 918 Actions.CodeCompletion().CodeCompleteOrdinaryName(getCurScope(), PCC); 919 return nullptr; 920 case tok::kw_import: { 921 Sema::ModuleImportState IS = Sema::ModuleImportState::NotACXX20Module; 922 if (getLangOpts().CPlusPlusModules) { 923 llvm_unreachable("not expecting a c++20 import here"); 924 ProhibitAttributes(Attrs); 925 } 926 SingleDecl = ParseModuleImport(SourceLocation(), IS); 927 } break; 928 case tok::kw_export: 929 if (getLangOpts().CPlusPlusModules || getLangOpts().HLSL) { 930 ProhibitAttributes(Attrs); 931 SingleDecl = ParseExportDeclaration(); 932 break; 933 } 934 // This must be 'export template'. Parse it so we can diagnose our lack 935 // of support. 936 [[fallthrough]]; 937 case tok::kw_using: 938 case tok::kw_namespace: 939 case tok::kw_typedef: 940 case tok::kw_template: 941 case tok::kw_static_assert: 942 case tok::kw__Static_assert: 943 // A function definition cannot start with any of these keywords. 944 { 945 SourceLocation DeclEnd; 946 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 947 DeclSpecAttrs); 948 } 949 950 case tok::kw_cbuffer: 951 case tok::kw_tbuffer: 952 if (getLangOpts().HLSL) { 953 SourceLocation DeclEnd; 954 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 955 DeclSpecAttrs); 956 } 957 goto dont_know; 958 959 case tok::kw_static: 960 // Parse (then ignore) 'static' prior to a template instantiation. This is 961 // a GCC extension that we intentionally do not support. 962 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 963 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 964 << 0; 965 SourceLocation DeclEnd; 966 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 967 DeclSpecAttrs); 968 } 969 goto dont_know; 970 971 case tok::kw_inline: 972 if (getLangOpts().CPlusPlus) { 973 tok::TokenKind NextKind = NextToken().getKind(); 974 975 // Inline namespaces. Allowed as an extension even in C++03. 976 if (NextKind == tok::kw_namespace) { 977 SourceLocation DeclEnd; 978 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 979 DeclSpecAttrs); 980 } 981 982 // Parse (then ignore) 'inline' prior to a template instantiation. This is 983 // a GCC extension that we intentionally do not support. 984 if (NextKind == tok::kw_template) { 985 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 986 << 1; 987 SourceLocation DeclEnd; 988 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 989 DeclSpecAttrs); 990 } 991 } 992 goto dont_know; 993 994 case tok::kw_extern: 995 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 996 ProhibitAttributes(Attrs); 997 ProhibitAttributes(DeclSpecAttrs); 998 // Extern templates 999 SourceLocation ExternLoc = ConsumeToken(); 1000 SourceLocation TemplateLoc = ConsumeToken(); 1001 Diag(ExternLoc, getLangOpts().CPlusPlus11 ? 1002 diag::warn_cxx98_compat_extern_template : 1003 diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc); 1004 SourceLocation DeclEnd; 1005 return ParseExplicitInstantiation(DeclaratorContext::File, ExternLoc, 1006 TemplateLoc, DeclEnd, Attrs); 1007 } 1008 goto dont_know; 1009 1010 case tok::kw___if_exists: 1011 case tok::kw___if_not_exists: 1012 ParseMicrosoftIfExistsExternalDeclaration(); 1013 return nullptr; 1014 1015 case tok::kw_module: 1016 Diag(Tok, diag::err_unexpected_module_decl); 1017 SkipUntil(tok::semi); 1018 return nullptr; 1019 1020 default: 1021 dont_know: 1022 if (Tok.isEditorPlaceholder()) { 1023 ConsumeToken(); 1024 return nullptr; 1025 } 1026 if (getLangOpts().IncrementalExtensions && 1027 !isDeclarationStatement(/*DisambiguatingWithExpression=*/true)) 1028 return ParseTopLevelStmtDecl(); 1029 1030 // We can't tell whether this is a function-definition or declaration yet. 1031 if (!SingleDecl) 1032 return ParseDeclarationOrFunctionDefinition(Attrs, DeclSpecAttrs, DS); 1033 } 1034 1035 // This routine returns a DeclGroup, if the thing we parsed only contains a 1036 // single decl, convert it now. 1037 return Actions.ConvertDeclToDeclGroup(SingleDecl); 1038 } 1039 1040 bool Parser::isDeclarationAfterDeclarator() { 1041 // Check for '= delete' or '= default' 1042 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 1043 const Token &KW = NextToken(); 1044 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete)) 1045 return false; 1046 } 1047 1048 return Tok.is(tok::equal) || // int X()= -> not a function def 1049 Tok.is(tok::comma) || // int X(), -> not a function def 1050 Tok.is(tok::semi) || // int X(); -> not a function def 1051 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 1052 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 1053 (getLangOpts().CPlusPlus && 1054 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 1055 } 1056 1057 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 1058 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator"); 1059 if (Tok.is(tok::l_brace)) // int X() {} 1060 return true; 1061 1062 // Handle K&R C argument lists: int X(f) int f; {} 1063 if (!getLangOpts().CPlusPlus && 1064 Declarator.getFunctionTypeInfo().isKNRPrototype()) 1065 return isDeclarationSpecifier(ImplicitTypenameContext::No); 1066 1067 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 1068 const Token &KW = NextToken(); 1069 return KW.is(tok::kw_default) || KW.is(tok::kw_delete); 1070 } 1071 1072 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 1073 Tok.is(tok::kw_try); // X() try { ... } 1074 } 1075 1076 Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal( 1077 ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs, 1078 ParsingDeclSpec &DS, AccessSpecifier AS) { 1079 // Because we assume that the DeclSpec has not yet been initialised, we simply 1080 // overwrite the source range and attribute the provided leading declspec 1081 // attributes. 1082 assert(DS.getSourceRange().isInvalid() && 1083 "expected uninitialised source range"); 1084 DS.SetRangeStart(DeclSpecAttrs.Range.getBegin()); 1085 DS.SetRangeEnd(DeclSpecAttrs.Range.getEnd()); 1086 DS.takeAttributesFrom(DeclSpecAttrs); 1087 1088 ParsedTemplateInfo TemplateInfo; 1089 MaybeParseMicrosoftAttributes(DS.getAttributes()); 1090 // Parse the common declaration-specifiers piece. 1091 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 1092 DeclSpecContext::DSC_top_level); 1093 1094 // If we had a free-standing type definition with a missing semicolon, we 1095 // may get this far before the problem becomes obvious. 1096 if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition( 1097 DS, AS, DeclSpecContext::DSC_top_level)) 1098 return nullptr; 1099 1100 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 1101 // declaration-specifiers init-declarator-list[opt] ';' 1102 if (Tok.is(tok::semi)) { 1103 auto LengthOfTSTToken = [](DeclSpec::TST TKind) { 1104 assert(DeclSpec::isDeclRep(TKind)); 1105 switch(TKind) { 1106 case DeclSpec::TST_class: 1107 return 5; 1108 case DeclSpec::TST_struct: 1109 return 6; 1110 case DeclSpec::TST_union: 1111 return 5; 1112 case DeclSpec::TST_enum: 1113 return 4; 1114 case DeclSpec::TST_interface: 1115 return 9; 1116 default: 1117 llvm_unreachable("we only expect to get the length of the class/struct/union/enum"); 1118 } 1119 1120 }; 1121 // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]' 1122 SourceLocation CorrectLocationForAttributes = 1123 DeclSpec::isDeclRep(DS.getTypeSpecType()) 1124 ? DS.getTypeSpecTypeLoc().getLocWithOffset( 1125 LengthOfTSTToken(DS.getTypeSpecType())) 1126 : SourceLocation(); 1127 ProhibitAttributes(Attrs, CorrectLocationForAttributes); 1128 ConsumeToken(); 1129 RecordDecl *AnonRecord = nullptr; 1130 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( 1131 getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord); 1132 DS.complete(TheDecl); 1133 Actions.ActOnDefinedDeclarationSpecifier(TheDecl); 1134 if (AnonRecord) { 1135 Decl* decls[] = {AnonRecord, TheDecl}; 1136 return Actions.BuildDeclaratorGroup(decls); 1137 } 1138 return Actions.ConvertDeclToDeclGroup(TheDecl); 1139 } 1140 1141 if (DS.hasTagDefinition()) 1142 Actions.ActOnDefinedDeclarationSpecifier(DS.getRepAsDecl()); 1143 1144 // ObjC2 allows prefix attributes on class interfaces and protocols. 1145 // FIXME: This still needs better diagnostics. We should only accept 1146 // attributes here, no types, etc. 1147 if (getLangOpts().ObjC && Tok.is(tok::at)) { 1148 SourceLocation AtLoc = ConsumeToken(); // the "@" 1149 if (!Tok.isObjCAtKeyword(tok::objc_interface) && 1150 !Tok.isObjCAtKeyword(tok::objc_protocol) && 1151 !Tok.isObjCAtKeyword(tok::objc_implementation)) { 1152 Diag(Tok, diag::err_objc_unexpected_attr); 1153 SkipUntil(tok::semi); 1154 return nullptr; 1155 } 1156 1157 DS.abort(); 1158 DS.takeAttributesFrom(Attrs); 1159 1160 const char *PrevSpec = nullptr; 1161 unsigned DiagID; 1162 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID, 1163 Actions.getASTContext().getPrintingPolicy())) 1164 Diag(AtLoc, DiagID) << PrevSpec; 1165 1166 if (Tok.isObjCAtKeyword(tok::objc_protocol)) 1167 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 1168 1169 if (Tok.isObjCAtKeyword(tok::objc_implementation)) 1170 return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes()); 1171 1172 return Actions.ConvertDeclToDeclGroup( 1173 ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes())); 1174 } 1175 1176 // If the declspec consisted only of 'extern' and we have a string 1177 // literal following it, this must be a C++ linkage specifier like 1178 // 'extern "C"'. 1179 if (getLangOpts().CPlusPlus && isTokenStringLiteral() && 1180 DS.getStorageClassSpec() == DeclSpec::SCS_extern && 1181 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 1182 ProhibitAttributes(Attrs); 1183 Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::File); 1184 return Actions.ConvertDeclToDeclGroup(TheDecl); 1185 } 1186 1187 return ParseDeclGroup(DS, DeclaratorContext::File, Attrs, TemplateInfo); 1188 } 1189 1190 Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition( 1191 ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs, 1192 ParsingDeclSpec *DS, AccessSpecifier AS) { 1193 // Add an enclosing time trace scope for a bunch of small scopes with 1194 // "EvaluateAsConstExpr". 1195 llvm::TimeTraceScope TimeScope("ParseDeclarationOrFunctionDefinition", [&]() { 1196 return Tok.getLocation().printToString( 1197 Actions.getASTContext().getSourceManager()); 1198 }); 1199 1200 if (DS) { 1201 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS); 1202 } else { 1203 ParsingDeclSpec PDS(*this); 1204 // Must temporarily exit the objective-c container scope for 1205 // parsing c constructs and re-enter objc container scope 1206 // afterwards. 1207 ObjCDeclContextSwitch ObjCDC(*this); 1208 1209 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, PDS, AS); 1210 } 1211 } 1212 1213 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 1214 const ParsedTemplateInfo &TemplateInfo, 1215 LateParsedAttrList *LateParsedAttrs) { 1216 llvm::TimeTraceScope TimeScope("ParseFunctionDefinition", [&]() { 1217 return Actions.GetNameForDeclarator(D).getName().getAsString(); 1218 }); 1219 1220 // Poison SEH identifiers so they are flagged as illegal in function bodies. 1221 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 1222 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 1223 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 1224 1225 // If this is C89 and the declspecs were completely missing, fudge in an 1226 // implicit int. We do this here because this is the only place where 1227 // declaration-specifiers are completely optional in the grammar. 1228 if (getLangOpts().isImplicitIntRequired() && D.getDeclSpec().isEmpty()) { 1229 Diag(D.getIdentifierLoc(), diag::warn_missing_type_specifier) 1230 << D.getDeclSpec().getSourceRange(); 1231 const char *PrevSpec; 1232 unsigned DiagID; 1233 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); 1234 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 1235 D.getIdentifierLoc(), 1236 PrevSpec, DiagID, 1237 Policy); 1238 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 1239 } 1240 1241 // If this declaration was formed with a K&R-style identifier list for the 1242 // arguments, parse declarations for all of the args next. 1243 // int foo(a,b) int a; float b; {} 1244 if (FTI.isKNRPrototype()) 1245 ParseKNRParamDeclarations(D); 1246 1247 // We should have either an opening brace or, in a C++ constructor, 1248 // we may have a colon. 1249 if (Tok.isNot(tok::l_brace) && 1250 (!getLangOpts().CPlusPlus || 1251 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) && 1252 Tok.isNot(tok::equal)))) { 1253 Diag(Tok, diag::err_expected_fn_body); 1254 1255 // Skip over garbage, until we get to '{'. Don't eat the '{'. 1256 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); 1257 1258 // If we didn't find the '{', bail out. 1259 if (Tok.isNot(tok::l_brace)) 1260 return nullptr; 1261 } 1262 1263 // Check to make sure that any normal attributes are allowed to be on 1264 // a definition. Late parsed attributes are checked at the end. 1265 if (Tok.isNot(tok::equal)) { 1266 for (const ParsedAttr &AL : D.getAttributes()) 1267 if (AL.isKnownToGCC() && !AL.isStandardAttributeSyntax()) 1268 Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL; 1269 } 1270 1271 // In delayed template parsing mode, for function template we consume the 1272 // tokens and store them for late parsing at the end of the translation unit. 1273 if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) && 1274 TemplateInfo.Kind == ParsedTemplateKind::Template && 1275 Actions.canDelayFunctionBody(D)) { 1276 MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams); 1277 1278 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 1279 Scope::CompoundStmtScope); 1280 Scope *ParentScope = getCurScope()->getParent(); 1281 1282 D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition); 1283 Decl *DP = Actions.HandleDeclarator(ParentScope, D, 1284 TemplateParameterLists); 1285 D.complete(DP); 1286 D.getMutableDeclSpec().abort(); 1287 1288 if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) && 1289 trySkippingFunctionBody()) { 1290 BodyScope.Exit(); 1291 return Actions.ActOnSkippedFunctionBody(DP); 1292 } 1293 1294 CachedTokens Toks; 1295 LexTemplateFunctionForLateParsing(Toks); 1296 1297 if (DP) { 1298 FunctionDecl *FnD = DP->getAsFunction(); 1299 Actions.CheckForFunctionRedefinition(FnD); 1300 Actions.MarkAsLateParsedTemplate(FnD, DP, Toks); 1301 } 1302 return DP; 1303 } 1304 else if (CurParsedObjCImpl && 1305 !TemplateInfo.TemplateParams && 1306 (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || 1307 Tok.is(tok::colon)) && 1308 Actions.CurContext->isTranslationUnit()) { 1309 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 1310 Scope::CompoundStmtScope); 1311 Scope *ParentScope = getCurScope()->getParent(); 1312 1313 D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition); 1314 Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D, 1315 MultiTemplateParamsArg()); 1316 D.complete(FuncDecl); 1317 D.getMutableDeclSpec().abort(); 1318 if (FuncDecl) { 1319 // Consume the tokens and store them for later parsing. 1320 StashAwayMethodOrFunctionBodyTokens(FuncDecl); 1321 CurParsedObjCImpl->HasCFunction = true; 1322 return FuncDecl; 1323 } 1324 // FIXME: Should we really fall through here? 1325 } 1326 1327 // Enter a scope for the function body. 1328 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 1329 Scope::CompoundStmtScope); 1330 1331 // Parse function body eagerly if it is either '= delete;' or '= default;' as 1332 // ActOnStartOfFunctionDef needs to know whether the function is deleted. 1333 StringLiteral *DeletedMessage = nullptr; 1334 Sema::FnBodyKind BodyKind = Sema::FnBodyKind::Other; 1335 SourceLocation KWLoc; 1336 if (TryConsumeToken(tok::equal)) { 1337 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='"); 1338 1339 if (TryConsumeToken(tok::kw_delete, KWLoc)) { 1340 Diag(KWLoc, getLangOpts().CPlusPlus11 1341 ? diag::warn_cxx98_compat_defaulted_deleted_function 1342 : diag::ext_defaulted_deleted_function) 1343 << 1 /* deleted */; 1344 BodyKind = Sema::FnBodyKind::Delete; 1345 DeletedMessage = ParseCXXDeletedFunctionMessage(); 1346 } else if (TryConsumeToken(tok::kw_default, KWLoc)) { 1347 Diag(KWLoc, getLangOpts().CPlusPlus11 1348 ? diag::warn_cxx98_compat_defaulted_deleted_function 1349 : diag::ext_defaulted_deleted_function) 1350 << 0 /* defaulted */; 1351 BodyKind = Sema::FnBodyKind::Default; 1352 } else { 1353 llvm_unreachable("function definition after = not 'delete' or 'default'"); 1354 } 1355 1356 if (Tok.is(tok::comma)) { 1357 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 1358 << (BodyKind == Sema::FnBodyKind::Delete); 1359 SkipUntil(tok::semi); 1360 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, 1361 BodyKind == Sema::FnBodyKind::Delete 1362 ? "delete" 1363 : "default")) { 1364 SkipUntil(tok::semi); 1365 } 1366 } 1367 1368 Sema::FPFeaturesStateRAII SaveFPFeatures(Actions); 1369 1370 // Tell the actions module that we have entered a function definition with the 1371 // specified Declarator for the function. 1372 SkipBodyInfo SkipBody; 1373 Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D, 1374 TemplateInfo.TemplateParams 1375 ? *TemplateInfo.TemplateParams 1376 : MultiTemplateParamsArg(), 1377 &SkipBody, BodyKind); 1378 1379 if (SkipBody.ShouldSkip) { 1380 // Do NOT enter SkipFunctionBody if we already consumed the tokens. 1381 if (BodyKind == Sema::FnBodyKind::Other) 1382 SkipFunctionBody(); 1383 1384 // ExpressionEvaluationContext is pushed in ActOnStartOfFunctionDef 1385 // and it would be popped in ActOnFinishFunctionBody. 1386 // We pop it explcitly here since ActOnFinishFunctionBody won't get called. 1387 // 1388 // Do not call PopExpressionEvaluationContext() if it is a lambda because 1389 // one is already popped when finishing the lambda in BuildLambdaExpr(). 1390 // 1391 // FIXME: It looks not easy to balance PushExpressionEvaluationContext() 1392 // and PopExpressionEvaluationContext(). 1393 if (!isLambdaCallOperator(dyn_cast_if_present<FunctionDecl>(Res))) 1394 Actions.PopExpressionEvaluationContext(); 1395 return Res; 1396 } 1397 1398 // Break out of the ParsingDeclarator context before we parse the body. 1399 D.complete(Res); 1400 1401 // Break out of the ParsingDeclSpec context, too. This const_cast is 1402 // safe because we're always the sole owner. 1403 D.getMutableDeclSpec().abort(); 1404 1405 if (BodyKind != Sema::FnBodyKind::Other) { 1406 Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage); 1407 Stmt *GeneratedBody = Res ? Res->getBody() : nullptr; 1408 Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false); 1409 return Res; 1410 } 1411 1412 // With abbreviated function templates - we need to explicitly add depth to 1413 // account for the implicit template parameter list induced by the template. 1414 if (const auto *Template = dyn_cast_if_present<FunctionTemplateDecl>(Res); 1415 Template && Template->isAbbreviated() && 1416 Template->getTemplateParameters()->getParam(0)->isImplicit()) 1417 // First template parameter is implicit - meaning no explicit template 1418 // parameter list was specified. 1419 CurTemplateDepthTracker.addDepth(1); 1420 1421 if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) && 1422 trySkippingFunctionBody()) { 1423 BodyScope.Exit(); 1424 Actions.ActOnSkippedFunctionBody(Res); 1425 return Actions.ActOnFinishFunctionBody(Res, nullptr, false); 1426 } 1427 1428 if (Tok.is(tok::kw_try)) 1429 return ParseFunctionTryBlock(Res, BodyScope); 1430 1431 // If we have a colon, then we're probably parsing a C++ 1432 // ctor-initializer. 1433 if (Tok.is(tok::colon)) { 1434 ParseConstructorInitializer(Res); 1435 1436 // Recover from error. 1437 if (!Tok.is(tok::l_brace)) { 1438 BodyScope.Exit(); 1439 Actions.ActOnFinishFunctionBody(Res, nullptr); 1440 return Res; 1441 } 1442 } else 1443 Actions.ActOnDefaultCtorInitializers(Res); 1444 1445 // Late attributes are parsed in the same scope as the function body. 1446 if (LateParsedAttrs) 1447 ParseLexedAttributeList(*LateParsedAttrs, Res, false, true); 1448 1449 return ParseFunctionStatementBody(Res, BodyScope); 1450 } 1451 1452 void Parser::SkipFunctionBody() { 1453 if (Tok.is(tok::equal)) { 1454 SkipUntil(tok::semi); 1455 return; 1456 } 1457 1458 bool IsFunctionTryBlock = Tok.is(tok::kw_try); 1459 if (IsFunctionTryBlock) 1460 ConsumeToken(); 1461 1462 CachedTokens Skipped; 1463 if (ConsumeAndStoreFunctionPrologue(Skipped)) 1464 SkipMalformedDecl(); 1465 else { 1466 SkipUntil(tok::r_brace); 1467 while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) { 1468 SkipUntil(tok::l_brace); 1469 SkipUntil(tok::r_brace); 1470 } 1471 } 1472 } 1473 1474 void Parser::ParseKNRParamDeclarations(Declarator &D) { 1475 // We know that the top-level of this declarator is a function. 1476 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 1477 1478 // Enter function-declaration scope, limiting any declarators to the 1479 // function prototype scope, including parameter declarators. 1480 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 1481 Scope::FunctionDeclarationScope | Scope::DeclScope); 1482 1483 // Read all the argument declarations. 1484 while (isDeclarationSpecifier(ImplicitTypenameContext::No)) { 1485 SourceLocation DSStart = Tok.getLocation(); 1486 1487 // Parse the common declaration-specifiers piece. 1488 DeclSpec DS(AttrFactory); 1489 ParsedTemplateInfo TemplateInfo; 1490 ParseDeclarationSpecifiers(DS, TemplateInfo); 1491 1492 // C99 6.9.1p6: 'each declaration in the declaration list shall have at 1493 // least one declarator'. 1494 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 1495 // the declarations though. It's trivial to ignore them, really hard to do 1496 // anything else with them. 1497 if (TryConsumeToken(tok::semi)) { 1498 Diag(DSStart, diag::err_declaration_does_not_declare_param); 1499 continue; 1500 } 1501 1502 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 1503 // than register. 1504 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 1505 DS.getStorageClassSpec() != DeclSpec::SCS_register) { 1506 Diag(DS.getStorageClassSpecLoc(), 1507 diag::err_invalid_storage_class_in_func_decl); 1508 DS.ClearStorageClassSpecs(); 1509 } 1510 if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) { 1511 Diag(DS.getThreadStorageClassSpecLoc(), 1512 diag::err_invalid_storage_class_in_func_decl); 1513 DS.ClearStorageClassSpecs(); 1514 } 1515 1516 // Parse the first declarator attached to this declspec. 1517 Declarator ParmDeclarator(DS, ParsedAttributesView::none(), 1518 DeclaratorContext::KNRTypeList); 1519 ParseDeclarator(ParmDeclarator); 1520 1521 // Handle the full declarator list. 1522 while (true) { 1523 // If attributes are present, parse them. 1524 MaybeParseGNUAttributes(ParmDeclarator); 1525 1526 // Ask the actions module to compute the type for this declarator. 1527 Decl *Param = 1528 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 1529 1530 if (Param && 1531 // A missing identifier has already been diagnosed. 1532 ParmDeclarator.getIdentifier()) { 1533 1534 // Scan the argument list looking for the correct param to apply this 1535 // type. 1536 for (unsigned i = 0; ; ++i) { 1537 // C99 6.9.1p6: those declarators shall declare only identifiers from 1538 // the identifier list. 1539 if (i == FTI.NumParams) { 1540 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 1541 << ParmDeclarator.getIdentifier(); 1542 break; 1543 } 1544 1545 if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) { 1546 // Reject redefinitions of parameters. 1547 if (FTI.Params[i].Param) { 1548 Diag(ParmDeclarator.getIdentifierLoc(), 1549 diag::err_param_redefinition) 1550 << ParmDeclarator.getIdentifier(); 1551 } else { 1552 FTI.Params[i].Param = Param; 1553 } 1554 break; 1555 } 1556 } 1557 } 1558 1559 // If we don't have a comma, it is either the end of the list (a ';') or 1560 // an error, bail out. 1561 if (Tok.isNot(tok::comma)) 1562 break; 1563 1564 ParmDeclarator.clear(); 1565 1566 // Consume the comma. 1567 ParmDeclarator.setCommaLoc(ConsumeToken()); 1568 1569 // Parse the next declarator. 1570 ParseDeclarator(ParmDeclarator); 1571 } 1572 1573 // Consume ';' and continue parsing. 1574 if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) 1575 continue; 1576 1577 // Otherwise recover by skipping to next semi or mandatory function body. 1578 if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch)) 1579 break; 1580 TryConsumeToken(tok::semi); 1581 } 1582 1583 // The actions module must verify that all arguments were declared. 1584 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 1585 } 1586 1587 ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) { 1588 1589 ExprResult AsmString; 1590 if (isTokenStringLiteral()) { 1591 AsmString = ParseStringLiteralExpression(); 1592 if (AsmString.isInvalid()) 1593 return AsmString; 1594 1595 const auto *SL = cast<StringLiteral>(AsmString.get()); 1596 if (!SL->isOrdinary()) { 1597 Diag(Tok, diag::err_asm_operand_wide_string_literal) 1598 << SL->isWide() << SL->getSourceRange(); 1599 return ExprError(); 1600 } 1601 } else if (!ForAsmLabel && getLangOpts().CPlusPlus11 && 1602 Tok.is(tok::l_paren)) { 1603 ParenParseOption ExprType = ParenParseOption::SimpleExpr; 1604 SourceLocation RParenLoc; 1605 ParsedType CastTy; 1606 1607 EnterExpressionEvaluationContext ConstantEvaluated( 1608 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); 1609 AsmString = ParseParenExpression( 1610 ExprType, /*StopIfCastExr=*/true, ParenExprKind::Unknown, 1611 TypoCorrectionTypeBehavior::AllowBoth, CastTy, RParenLoc); 1612 if (!AsmString.isInvalid()) 1613 AsmString = Actions.ActOnConstantExpression(AsmString); 1614 1615 if (AsmString.isInvalid()) 1616 return ExprError(); 1617 } else { 1618 Diag(Tok, diag::err_asm_expected_string) << /*and expression=*/( 1619 (getLangOpts().CPlusPlus11 && !ForAsmLabel) ? 0 : 1); 1620 } 1621 1622 return Actions.ActOnGCCAsmStmtString(AsmString.get(), ForAsmLabel); 1623 } 1624 1625 ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) { 1626 assert(Tok.is(tok::kw_asm) && "Not an asm!"); 1627 SourceLocation Loc = ConsumeToken(); 1628 1629 if (isGNUAsmQualifier(Tok)) { 1630 // Remove from the end of 'asm' to the end of the asm qualifier. 1631 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 1632 PP.getLocForEndOfToken(Tok.getLocation())); 1633 Diag(Tok, diag::err_global_asm_qualifier_ignored) 1634 << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok)) 1635 << FixItHint::CreateRemoval(RemovalRange); 1636 ConsumeToken(); 1637 } 1638 1639 BalancedDelimiterTracker T(*this, tok::l_paren); 1640 if (T.consumeOpen()) { 1641 Diag(Tok, diag::err_expected_lparen_after) << "asm"; 1642 return ExprError(); 1643 } 1644 1645 ExprResult Result(ParseAsmStringLiteral(ForAsmLabel)); 1646 1647 if (!Result.isInvalid()) { 1648 // Close the paren and get the location of the end bracket 1649 T.consumeClose(); 1650 if (EndLoc) 1651 *EndLoc = T.getCloseLocation(); 1652 } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { 1653 if (EndLoc) 1654 *EndLoc = Tok.getLocation(); 1655 ConsumeParen(); 1656 } 1657 1658 return Result; 1659 } 1660 1661 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) { 1662 assert(tok.is(tok::annot_template_id) && "Expected template-id token"); 1663 TemplateIdAnnotation * 1664 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue()); 1665 return Id; 1666 } 1667 1668 void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) { 1669 // Push the current token back into the token stream (or revert it if it is 1670 // cached) and use an annotation scope token for current token. 1671 if (PP.isBacktrackEnabled()) 1672 PP.RevertCachedTokens(1); 1673 else 1674 PP.EnterToken(Tok, /*IsReinject=*/true); 1675 Tok.setKind(tok::annot_cxxscope); 1676 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 1677 Tok.setAnnotationRange(SS.getRange()); 1678 1679 // In case the tokens were cached, have Preprocessor replace them 1680 // with the annotation token. We don't need to do this if we've 1681 // just reverted back to a prior state. 1682 if (IsNewAnnotation) 1683 PP.AnnotateCachedTokens(Tok); 1684 } 1685 1686 AnnotatedNameKind 1687 Parser::TryAnnotateName(CorrectionCandidateCallback *CCC, 1688 ImplicitTypenameContext AllowImplicitTypename) { 1689 assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope)); 1690 1691 const bool EnteringContext = false; 1692 const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1693 1694 CXXScopeSpec SS; 1695 if (getLangOpts().CPlusPlus && 1696 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 1697 /*ObjectHasErrors=*/false, 1698 EnteringContext)) 1699 return AnnotatedNameKind::Error; 1700 1701 if (Tok.isNot(tok::identifier) || SS.isInvalid()) { 1702 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 1703 AllowImplicitTypename)) 1704 return AnnotatedNameKind::Error; 1705 return AnnotatedNameKind::Unresolved; 1706 } 1707 1708 IdentifierInfo *Name = Tok.getIdentifierInfo(); 1709 SourceLocation NameLoc = Tok.getLocation(); 1710 1711 // FIXME: Move the tentative declaration logic into ClassifyName so we can 1712 // typo-correct to tentatively-declared identifiers. 1713 if (isTentativelyDeclared(Name) && SS.isEmpty()) { 1714 // Identifier has been tentatively declared, and thus cannot be resolved as 1715 // an expression. Fall back to annotating it as a type. 1716 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 1717 AllowImplicitTypename)) 1718 return AnnotatedNameKind::Error; 1719 return Tok.is(tok::annot_typename) ? AnnotatedNameKind::Success 1720 : AnnotatedNameKind::TentativeDecl; 1721 } 1722 1723 Token Next = NextToken(); 1724 1725 // Look up and classify the identifier. We don't perform any typo-correction 1726 // after a scope specifier, because in general we can't recover from typos 1727 // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to 1728 // jump back into scope specifier parsing). 1729 Sema::NameClassification Classification = Actions.ClassifyName( 1730 getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr); 1731 1732 // If name lookup found nothing and we guessed that this was a template name, 1733 // double-check before committing to that interpretation. C++20 requires that 1734 // we interpret this as a template-id if it can be, but if it can't be, then 1735 // this is an error recovery case. 1736 if (Classification.getKind() == NameClassificationKind::UndeclaredTemplate && 1737 isTemplateArgumentList(1) == TPResult::False) { 1738 // It's not a template-id; re-classify without the '<' as a hint. 1739 Token FakeNext = Next; 1740 FakeNext.setKind(tok::unknown); 1741 Classification = 1742 Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext, 1743 SS.isEmpty() ? CCC : nullptr); 1744 } 1745 1746 switch (Classification.getKind()) { 1747 case NameClassificationKind::Error: 1748 return AnnotatedNameKind::Error; 1749 1750 case NameClassificationKind::Keyword: 1751 // The identifier was typo-corrected to a keyword. 1752 Tok.setIdentifierInfo(Name); 1753 Tok.setKind(Name->getTokenID()); 1754 PP.TypoCorrectToken(Tok); 1755 if (SS.isNotEmpty()) 1756 AnnotateScopeToken(SS, !WasScopeAnnotation); 1757 // We've "annotated" this as a keyword. 1758 return AnnotatedNameKind::Success; 1759 1760 case NameClassificationKind::Unknown: 1761 // It's not something we know about. Leave it unannotated. 1762 break; 1763 1764 case NameClassificationKind::Type: { 1765 if (TryAltiVecVectorToken()) 1766 // vector has been found as a type id when altivec is enabled but 1767 // this is followed by a declaration specifier so this is really the 1768 // altivec vector token. Leave it unannotated. 1769 break; 1770 SourceLocation BeginLoc = NameLoc; 1771 if (SS.isNotEmpty()) 1772 BeginLoc = SS.getBeginLoc(); 1773 1774 /// An Objective-C object type followed by '<' is a specialization of 1775 /// a parameterized class type or a protocol-qualified type. 1776 ParsedType Ty = Classification.getType(); 1777 if (getLangOpts().ObjC && NextToken().is(tok::less) && 1778 (Ty.get()->isObjCObjectType() || 1779 Ty.get()->isObjCObjectPointerType())) { 1780 // Consume the name. 1781 SourceLocation IdentifierLoc = ConsumeToken(); 1782 SourceLocation NewEndLoc; 1783 TypeResult NewType 1784 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty, 1785 /*consumeLastToken=*/false, 1786 NewEndLoc); 1787 if (NewType.isUsable()) 1788 Ty = NewType.get(); 1789 else if (Tok.is(tok::eof)) // Nothing to do here, bail out... 1790 return AnnotatedNameKind::Error; 1791 } 1792 1793 Tok.setKind(tok::annot_typename); 1794 setTypeAnnotation(Tok, Ty); 1795 Tok.setAnnotationEndLoc(Tok.getLocation()); 1796 Tok.setLocation(BeginLoc); 1797 PP.AnnotateCachedTokens(Tok); 1798 return AnnotatedNameKind::Success; 1799 } 1800 1801 case NameClassificationKind::OverloadSet: 1802 Tok.setKind(tok::annot_overload_set); 1803 setExprAnnotation(Tok, Classification.getExpression()); 1804 Tok.setAnnotationEndLoc(NameLoc); 1805 if (SS.isNotEmpty()) 1806 Tok.setLocation(SS.getBeginLoc()); 1807 PP.AnnotateCachedTokens(Tok); 1808 return AnnotatedNameKind::Success; 1809 1810 case NameClassificationKind::NonType: 1811 if (TryAltiVecVectorToken()) 1812 // vector has been found as a non-type id when altivec is enabled but 1813 // this is followed by a declaration specifier so this is really the 1814 // altivec vector token. Leave it unannotated. 1815 break; 1816 Tok.setKind(tok::annot_non_type); 1817 setNonTypeAnnotation(Tok, Classification.getNonTypeDecl()); 1818 Tok.setLocation(NameLoc); 1819 Tok.setAnnotationEndLoc(NameLoc); 1820 PP.AnnotateCachedTokens(Tok); 1821 if (SS.isNotEmpty()) 1822 AnnotateScopeToken(SS, !WasScopeAnnotation); 1823 return AnnotatedNameKind::Success; 1824 1825 case NameClassificationKind::UndeclaredNonType: 1826 case NameClassificationKind::DependentNonType: 1827 Tok.setKind(Classification.getKind() == 1828 NameClassificationKind::UndeclaredNonType 1829 ? tok::annot_non_type_undeclared 1830 : tok::annot_non_type_dependent); 1831 setIdentifierAnnotation(Tok, Name); 1832 Tok.setLocation(NameLoc); 1833 Tok.setAnnotationEndLoc(NameLoc); 1834 PP.AnnotateCachedTokens(Tok); 1835 if (SS.isNotEmpty()) 1836 AnnotateScopeToken(SS, !WasScopeAnnotation); 1837 return AnnotatedNameKind::Success; 1838 1839 case NameClassificationKind::TypeTemplate: 1840 if (Next.isNot(tok::less)) { 1841 // This may be a type template being used as a template template argument. 1842 if (SS.isNotEmpty()) 1843 AnnotateScopeToken(SS, !WasScopeAnnotation); 1844 return AnnotatedNameKind::TemplateName; 1845 } 1846 [[fallthrough]]; 1847 case NameClassificationKind::Concept: 1848 case NameClassificationKind::VarTemplate: 1849 case NameClassificationKind::FunctionTemplate: 1850 case NameClassificationKind::UndeclaredTemplate: { 1851 bool IsConceptName = 1852 Classification.getKind() == NameClassificationKind::Concept; 1853 // We have a template name followed by '<'. Consume the identifier token so 1854 // we reach the '<' and annotate it. 1855 if (Next.is(tok::less)) 1856 ConsumeToken(); 1857 UnqualifiedId Id; 1858 Id.setIdentifier(Name, NameLoc); 1859 if (AnnotateTemplateIdToken( 1860 TemplateTy::make(Classification.getTemplateName()), 1861 Classification.getTemplateNameKind(), SS, SourceLocation(), Id, 1862 /*AllowTypeAnnotation=*/!IsConceptName, 1863 /*TypeConstraint=*/IsConceptName)) 1864 return AnnotatedNameKind::Error; 1865 if (SS.isNotEmpty()) 1866 AnnotateScopeToken(SS, !WasScopeAnnotation); 1867 return AnnotatedNameKind::Success; 1868 } 1869 } 1870 1871 // Unable to classify the name, but maybe we can annotate a scope specifier. 1872 if (SS.isNotEmpty()) 1873 AnnotateScopeToken(SS, !WasScopeAnnotation); 1874 return AnnotatedNameKind::Unresolved; 1875 } 1876 1877 SourceLocation Parser::getEndOfPreviousToken() const { 1878 SourceLocation TokenEndLoc = PP.getLocForEndOfToken(PrevTokLocation); 1879 return TokenEndLoc.isValid() ? TokenEndLoc : Tok.getLocation(); 1880 } 1881 1882 bool Parser::TryKeywordIdentFallback(bool DisableKeyword) { 1883 assert(Tok.isNot(tok::identifier)); 1884 Diag(Tok, diag::ext_keyword_as_ident) 1885 << PP.getSpelling(Tok) 1886 << DisableKeyword; 1887 if (DisableKeyword) 1888 Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); 1889 Tok.setKind(tok::identifier); 1890 return true; 1891 } 1892 1893 bool Parser::TryAnnotateTypeOrScopeToken( 1894 ImplicitTypenameContext AllowImplicitTypename) { 1895 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 1896 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) || 1897 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) || 1898 Tok.is(tok::kw___super) || Tok.is(tok::kw_auto) || 1899 Tok.is(tok::annot_pack_indexing_type)) && 1900 "Cannot be a type or scope token!"); 1901 1902 if (Tok.is(tok::kw_typename)) { 1903 // MSVC lets you do stuff like: 1904 // typename typedef T_::D D; 1905 // 1906 // We will consume the typedef token here and put it back after we have 1907 // parsed the first identifier, transforming it into something more like: 1908 // typename T_::D typedef D; 1909 if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) { 1910 Token TypedefToken; 1911 PP.Lex(TypedefToken); 1912 bool Result = TryAnnotateTypeOrScopeToken(AllowImplicitTypename); 1913 PP.EnterToken(Tok, /*IsReinject=*/true); 1914 Tok = TypedefToken; 1915 if (!Result) 1916 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename); 1917 return Result; 1918 } 1919 1920 // Parse a C++ typename-specifier, e.g., "typename T::type". 1921 // 1922 // typename-specifier: 1923 // 'typename' '::' [opt] nested-name-specifier identifier 1924 // 'typename' '::' [opt] nested-name-specifier template [opt] 1925 // simple-template-id 1926 SourceLocation TypenameLoc = ConsumeToken(); 1927 CXXScopeSpec SS; 1928 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 1929 /*ObjectHasErrors=*/false, 1930 /*EnteringContext=*/false, nullptr, 1931 /*IsTypename*/ true)) 1932 return true; 1933 if (SS.isEmpty()) { 1934 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) || 1935 Tok.is(tok::annot_decltype)) { 1936 // Attempt to recover by skipping the invalid 'typename' 1937 if (Tok.is(tok::annot_decltype) || 1938 (!TryAnnotateTypeOrScopeToken(AllowImplicitTypename) && 1939 Tok.isAnnotation())) { 1940 unsigned DiagID = diag::err_expected_qualified_after_typename; 1941 // MS compatibility: MSVC permits using known types with typename. 1942 // e.g. "typedef typename T* pointer_type" 1943 if (getLangOpts().MicrosoftExt) 1944 DiagID = diag::warn_expected_qualified_after_typename; 1945 Diag(Tok.getLocation(), DiagID); 1946 return false; 1947 } 1948 } 1949 if (Tok.isEditorPlaceholder()) 1950 return true; 1951 1952 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 1953 return true; 1954 } 1955 1956 bool TemplateKWPresent = false; 1957 if (Tok.is(tok::kw_template)) { 1958 ConsumeToken(); 1959 TemplateKWPresent = true; 1960 } 1961 1962 TypeResult Ty; 1963 if (Tok.is(tok::identifier)) { 1964 if (TemplateKWPresent && NextToken().isNot(tok::less)) { 1965 Diag(Tok.getLocation(), 1966 diag::missing_template_arg_list_after_template_kw); 1967 return true; 1968 } 1969 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 1970 *Tok.getIdentifierInfo(), 1971 Tok.getLocation()); 1972 } else if (Tok.is(tok::annot_template_id)) { 1973 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1974 if (!TemplateId->mightBeType()) { 1975 Diag(Tok, diag::err_typename_refers_to_non_type_template) 1976 << Tok.getAnnotationRange(); 1977 return true; 1978 } 1979 1980 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 1981 TemplateId->NumArgs); 1982 1983 Ty = TemplateId->isInvalid() 1984 ? TypeError() 1985 : Actions.ActOnTypenameType( 1986 getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc, 1987 TemplateId->Template, TemplateId->Name, 1988 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, 1989 TemplateArgsPtr, TemplateId->RAngleLoc); 1990 } else { 1991 Diag(Tok, diag::err_expected_type_name_after_typename) 1992 << SS.getRange(); 1993 return true; 1994 } 1995 1996 SourceLocation EndLoc = Tok.getLastLoc(); 1997 Tok.setKind(tok::annot_typename); 1998 setTypeAnnotation(Tok, Ty); 1999 Tok.setAnnotationEndLoc(EndLoc); 2000 Tok.setLocation(TypenameLoc); 2001 PP.AnnotateCachedTokens(Tok); 2002 return false; 2003 } 2004 2005 // Remembers whether the token was originally a scope annotation. 2006 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 2007 2008 CXXScopeSpec SS; 2009 if (getLangOpts().CPlusPlus) 2010 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 2011 /*ObjectHasErrors=*/false, 2012 /*EnteringContext*/ false)) 2013 return true; 2014 2015 return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 2016 AllowImplicitTypename); 2017 } 2018 2019 bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec( 2020 CXXScopeSpec &SS, bool IsNewScope, 2021 ImplicitTypenameContext AllowImplicitTypename) { 2022 if (Tok.is(tok::identifier)) { 2023 // Determine whether the identifier is a type name. 2024 if (ParsedType Ty = Actions.getTypeName( 2025 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS, 2026 false, NextToken().is(tok::period), nullptr, 2027 /*IsCtorOrDtorName=*/false, 2028 /*NonTrivialTypeSourceInfo=*/true, 2029 /*IsClassTemplateDeductionContext=*/true, AllowImplicitTypename)) { 2030 SourceLocation BeginLoc = Tok.getLocation(); 2031 if (SS.isNotEmpty()) // it was a C++ qualified type name. 2032 BeginLoc = SS.getBeginLoc(); 2033 2034 /// An Objective-C object type followed by '<' is a specialization of 2035 /// a parameterized class type or a protocol-qualified type. 2036 if (getLangOpts().ObjC && NextToken().is(tok::less) && 2037 (Ty.get()->isObjCObjectType() || 2038 Ty.get()->isObjCObjectPointerType())) { 2039 // Consume the name. 2040 SourceLocation IdentifierLoc = ConsumeToken(); 2041 SourceLocation NewEndLoc; 2042 TypeResult NewType 2043 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty, 2044 /*consumeLastToken=*/false, 2045 NewEndLoc); 2046 if (NewType.isUsable()) 2047 Ty = NewType.get(); 2048 else if (Tok.is(tok::eof)) // Nothing to do here, bail out... 2049 return false; 2050 } 2051 2052 // This is a typename. Replace the current token in-place with an 2053 // annotation type token. 2054 Tok.setKind(tok::annot_typename); 2055 setTypeAnnotation(Tok, Ty); 2056 Tok.setAnnotationEndLoc(Tok.getLocation()); 2057 Tok.setLocation(BeginLoc); 2058 2059 // In case the tokens were cached, have Preprocessor replace 2060 // them with the annotation token. 2061 PP.AnnotateCachedTokens(Tok); 2062 return false; 2063 } 2064 2065 if (!getLangOpts().CPlusPlus) { 2066 // If we're in C, the only place we can have :: tokens is C23 2067 // attribute which is parsed elsewhere. If the identifier is not a type, 2068 // then it can't be scope either, just early exit. 2069 return false; 2070 } 2071 2072 // If this is a template-id, annotate with a template-id or type token. 2073 // FIXME: This appears to be dead code. We already have formed template-id 2074 // tokens when parsing the scope specifier; this can never form a new one. 2075 if (NextToken().is(tok::less)) { 2076 TemplateTy Template; 2077 UnqualifiedId TemplateName; 2078 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2079 bool MemberOfUnknownSpecialization; 2080 if (TemplateNameKind TNK = Actions.isTemplateName( 2081 getCurScope(), SS, 2082 /*hasTemplateKeyword=*/false, TemplateName, 2083 /*ObjectType=*/nullptr, /*EnteringContext*/false, Template, 2084 MemberOfUnknownSpecialization)) { 2085 // Only annotate an undeclared template name as a template-id if the 2086 // following tokens have the form of a template argument list. 2087 if (TNK != TNK_Undeclared_template || 2088 isTemplateArgumentList(1) != TPResult::False) { 2089 // Consume the identifier. 2090 ConsumeToken(); 2091 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 2092 TemplateName)) { 2093 // If an unrecoverable error occurred, we need to return true here, 2094 // because the token stream is in a damaged state. We may not 2095 // return a valid identifier. 2096 return true; 2097 } 2098 } 2099 } 2100 } 2101 2102 // The current token, which is either an identifier or a 2103 // template-id, is not part of the annotation. Fall through to 2104 // push that token back into the stream and complete the C++ scope 2105 // specifier annotation. 2106 } 2107 2108 if (Tok.is(tok::annot_template_id)) { 2109 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 2110 if (TemplateId->Kind == TNK_Type_template) { 2111 // A template-id that refers to a type was parsed into a 2112 // template-id annotation in a context where we weren't allowed 2113 // to produce a type annotation token. Update the template-id 2114 // annotation token to a type annotation token now. 2115 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename); 2116 return false; 2117 } 2118 } 2119 2120 if (SS.isEmpty()) { 2121 if (getLangOpts().ObjC && !getLangOpts().CPlusPlus && 2122 Tok.is(tok::coloncolon)) { 2123 // ObjectiveC does not allow :: as as a scope token. 2124 Diag(ConsumeToken(), diag::err_expected_type); 2125 return true; 2126 } 2127 return false; 2128 } 2129 2130 // A C++ scope specifier that isn't followed by a typename. 2131 AnnotateScopeToken(SS, IsNewScope); 2132 return false; 2133 } 2134 2135 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 2136 assert(getLangOpts().CPlusPlus && 2137 "Call sites of this function should be guarded by checking for C++"); 2138 assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!"); 2139 2140 CXXScopeSpec SS; 2141 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 2142 /*ObjectHasErrors=*/false, 2143 EnteringContext)) 2144 return true; 2145 if (SS.isEmpty()) 2146 return false; 2147 2148 AnnotateScopeToken(SS, true); 2149 return false; 2150 } 2151 2152 bool Parser::isTokenEqualOrEqualTypo() { 2153 tok::TokenKind Kind = Tok.getKind(); 2154 switch (Kind) { 2155 default: 2156 return false; 2157 case tok::ampequal: // &= 2158 case tok::starequal: // *= 2159 case tok::plusequal: // += 2160 case tok::minusequal: // -= 2161 case tok::exclaimequal: // != 2162 case tok::slashequal: // /= 2163 case tok::percentequal: // %= 2164 case tok::lessequal: // <= 2165 case tok::lesslessequal: // <<= 2166 case tok::greaterequal: // >= 2167 case tok::greatergreaterequal: // >>= 2168 case tok::caretequal: // ^= 2169 case tok::pipeequal: // |= 2170 case tok::equalequal: // == 2171 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal) 2172 << Kind 2173 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "="); 2174 [[fallthrough]]; 2175 case tok::equal: 2176 return true; 2177 } 2178 } 2179 2180 SourceLocation Parser::handleUnexpectedCodeCompletionToken() { 2181 assert(Tok.is(tok::code_completion)); 2182 PrevTokLocation = Tok.getLocation(); 2183 2184 for (Scope *S = getCurScope(); S; S = S->getParent()) { 2185 if (S->isFunctionScope()) { 2186 cutOffParsing(); 2187 Actions.CodeCompletion().CodeCompleteOrdinaryName( 2188 getCurScope(), SemaCodeCompletion::PCC_RecoveryInFunction); 2189 return PrevTokLocation; 2190 } 2191 2192 if (S->isClassScope()) { 2193 cutOffParsing(); 2194 Actions.CodeCompletion().CodeCompleteOrdinaryName( 2195 getCurScope(), SemaCodeCompletion::PCC_Class); 2196 return PrevTokLocation; 2197 } 2198 } 2199 2200 cutOffParsing(); 2201 Actions.CodeCompletion().CodeCompleteOrdinaryName( 2202 getCurScope(), SemaCodeCompletion::PCC_Namespace); 2203 return PrevTokLocation; 2204 } 2205 2206 // Code-completion pass-through functions 2207 2208 void Parser::CodeCompleteDirective(bool InConditional) { 2209 Actions.CodeCompletion().CodeCompletePreprocessorDirective(InConditional); 2210 } 2211 2212 void Parser::CodeCompleteInConditionalExclusion() { 2213 Actions.CodeCompletion().CodeCompleteInPreprocessorConditionalExclusion( 2214 getCurScope()); 2215 } 2216 2217 void Parser::CodeCompleteMacroName(bool IsDefinition) { 2218 Actions.CodeCompletion().CodeCompletePreprocessorMacroName(IsDefinition); 2219 } 2220 2221 void Parser::CodeCompletePreprocessorExpression() { 2222 Actions.CodeCompletion().CodeCompletePreprocessorExpression(); 2223 } 2224 2225 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 2226 MacroInfo *MacroInfo, 2227 unsigned ArgumentIndex) { 2228 Actions.CodeCompletion().CodeCompletePreprocessorMacroArgument( 2229 getCurScope(), Macro, MacroInfo, ArgumentIndex); 2230 } 2231 2232 void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) { 2233 Actions.CodeCompletion().CodeCompleteIncludedFile(Dir, IsAngled); 2234 } 2235 2236 void Parser::CodeCompleteNaturalLanguage() { 2237 Actions.CodeCompletion().CodeCompleteNaturalLanguage(); 2238 } 2239 2240 bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) { 2241 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) && 2242 "Expected '__if_exists' or '__if_not_exists'"); 2243 Result.IsIfExists = Tok.is(tok::kw___if_exists); 2244 Result.KeywordLoc = ConsumeToken(); 2245 2246 BalancedDelimiterTracker T(*this, tok::l_paren); 2247 if (T.consumeOpen()) { 2248 Diag(Tok, diag::err_expected_lparen_after) 2249 << (Result.IsIfExists? "__if_exists" : "__if_not_exists"); 2250 return true; 2251 } 2252 2253 // Parse nested-name-specifier. 2254 if (getLangOpts().CPlusPlus) 2255 ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr, 2256 /*ObjectHasErrors=*/false, 2257 /*EnteringContext=*/false); 2258 2259 // Check nested-name specifier. 2260 if (Result.SS.isInvalid()) { 2261 T.skipToEnd(); 2262 return true; 2263 } 2264 2265 // Parse the unqualified-id. 2266 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused. 2267 if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr, 2268 /*ObjectHadErrors=*/false, /*EnteringContext*/ false, 2269 /*AllowDestructorName*/ true, 2270 /*AllowConstructorName*/ true, 2271 /*AllowDeductionGuide*/ false, &TemplateKWLoc, 2272 Result.Name)) { 2273 T.skipToEnd(); 2274 return true; 2275 } 2276 2277 if (T.consumeClose()) 2278 return true; 2279 2280 // Check if the symbol exists. 2281 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc, 2282 Result.IsIfExists, Result.SS, 2283 Result.Name)) { 2284 case IfExistsResult::Exists: 2285 Result.Behavior = 2286 Result.IsIfExists ? IfExistsBehavior::Parse : IfExistsBehavior::Skip; 2287 break; 2288 2289 case IfExistsResult::DoesNotExist: 2290 Result.Behavior = 2291 !Result.IsIfExists ? IfExistsBehavior::Parse : IfExistsBehavior::Skip; 2292 break; 2293 2294 case IfExistsResult::Dependent: 2295 Result.Behavior = IfExistsBehavior::Dependent; 2296 break; 2297 2298 case IfExistsResult::Error: 2299 return true; 2300 } 2301 2302 return false; 2303 } 2304 2305 void Parser::ParseMicrosoftIfExistsExternalDeclaration() { 2306 IfExistsCondition Result; 2307 if (ParseMicrosoftIfExistsCondition(Result)) 2308 return; 2309 2310 BalancedDelimiterTracker Braces(*this, tok::l_brace); 2311 if (Braces.consumeOpen()) { 2312 Diag(Tok, diag::err_expected) << tok::l_brace; 2313 return; 2314 } 2315 2316 switch (Result.Behavior) { 2317 case IfExistsBehavior::Parse: 2318 // Parse declarations below. 2319 break; 2320 2321 case IfExistsBehavior::Dependent: 2322 llvm_unreachable("Cannot have a dependent external declaration"); 2323 2324 case IfExistsBehavior::Skip: 2325 Braces.skipToEnd(); 2326 return; 2327 } 2328 2329 // Parse the declarations. 2330 // FIXME: Support module import within __if_exists? 2331 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 2332 ParsedAttributes Attrs(AttrFactory); 2333 MaybeParseCXX11Attributes(Attrs); 2334 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); 2335 DeclGroupPtrTy Result = ParseExternalDeclaration(Attrs, EmptyDeclSpecAttrs); 2336 if (Result && !getCurScope()->getParent()) 2337 Actions.getASTConsumer().HandleTopLevelDecl(Result.get()); 2338 } 2339 Braces.consumeClose(); 2340 } 2341 2342 Parser::DeclGroupPtrTy 2343 Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) { 2344 Token Introducer = Tok; 2345 SourceLocation StartLoc = Introducer.getLocation(); 2346 2347 Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export) 2348 ? Sema::ModuleDeclKind::Interface 2349 : Sema::ModuleDeclKind::Implementation; 2350 2351 assert( 2352 (Tok.is(tok::kw_module) || 2353 (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) && 2354 "not a module declaration"); 2355 SourceLocation ModuleLoc = ConsumeToken(); 2356 2357 // Attributes appear after the module name, not before. 2358 // FIXME: Suggest moving the attributes later with a fixit. 2359 DiagnoseAndSkipCXX11Attributes(); 2360 2361 // Parse a global-module-fragment, if present. 2362 if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) { 2363 SourceLocation SemiLoc = ConsumeToken(); 2364 if (ImportState != Sema::ModuleImportState::FirstDecl || 2365 Introducer.hasSeenNoTrivialPPDirective()) { 2366 Diag(StartLoc, diag::err_global_module_introducer_not_at_start) 2367 << SourceRange(StartLoc, SemiLoc); 2368 return nullptr; 2369 } 2370 if (MDK == Sema::ModuleDeclKind::Interface) { 2371 Diag(StartLoc, diag::err_module_fragment_exported) 2372 << /*global*/0 << FixItHint::CreateRemoval(StartLoc); 2373 } 2374 ImportState = Sema::ModuleImportState::GlobalFragment; 2375 return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc); 2376 } 2377 2378 // Parse a private-module-fragment, if present. 2379 if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) && 2380 NextToken().is(tok::kw_private)) { 2381 if (MDK == Sema::ModuleDeclKind::Interface) { 2382 Diag(StartLoc, diag::err_module_fragment_exported) 2383 << /*private*/1 << FixItHint::CreateRemoval(StartLoc); 2384 } 2385 ConsumeToken(); 2386 SourceLocation PrivateLoc = ConsumeToken(); 2387 DiagnoseAndSkipCXX11Attributes(); 2388 ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi); 2389 ImportState = ImportState == Sema::ModuleImportState::ImportAllowed 2390 ? Sema::ModuleImportState::PrivateFragmentImportAllowed 2391 : Sema::ModuleImportState::PrivateFragmentImportFinished; 2392 return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc); 2393 } 2394 2395 SmallVector<IdentifierLoc, 2> Path; 2396 if (ParseModuleName(ModuleLoc, Path, /*IsImport*/ false)) 2397 return nullptr; 2398 2399 // Parse the optional module-partition. 2400 SmallVector<IdentifierLoc, 2> Partition; 2401 if (Tok.is(tok::colon)) { 2402 SourceLocation ColonLoc = ConsumeToken(); 2403 if (!getLangOpts().CPlusPlusModules) 2404 Diag(ColonLoc, diag::err_unsupported_module_partition) 2405 << SourceRange(ColonLoc, Partition.back().getLoc()); 2406 // Recover by ignoring the partition name. 2407 else if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/ false)) 2408 return nullptr; 2409 } 2410 2411 // We don't support any module attributes yet; just parse them and diagnose. 2412 ParsedAttributes Attrs(AttrFactory); 2413 MaybeParseCXX11Attributes(Attrs); 2414 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr, 2415 diag::err_keyword_not_module_attr, 2416 /*DiagnoseEmptyAttrs=*/false, 2417 /*WarnOnUnknownAttrs=*/true); 2418 2419 ExpectAndConsumeSemi(diag::err_module_expected_semi); 2420 2421 return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, Partition, 2422 ImportState, 2423 Introducer.hasSeenNoTrivialPPDirective()); 2424 } 2425 2426 Decl *Parser::ParseModuleImport(SourceLocation AtLoc, 2427 Sema::ModuleImportState &ImportState) { 2428 SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc; 2429 2430 SourceLocation ExportLoc; 2431 TryConsumeToken(tok::kw_export, ExportLoc); 2432 2433 assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier) 2434 : Tok.isObjCAtKeyword(tok::objc_import)) && 2435 "Improper start to module import"); 2436 bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import); 2437 SourceLocation ImportLoc = ConsumeToken(); 2438 2439 // For C++20 modules, we can have "name" or ":Partition name" as valid input. 2440 SmallVector<IdentifierLoc, 2> Path; 2441 bool IsPartition = false; 2442 Module *HeaderUnit = nullptr; 2443 if (Tok.is(tok::header_name)) { 2444 // This is a header import that the preprocessor decided we should skip 2445 // because it was malformed in some way. Parse and ignore it; it's already 2446 // been diagnosed. 2447 ConsumeToken(); 2448 } else if (Tok.is(tok::annot_header_unit)) { 2449 // This is a header import that the preprocessor mapped to a module import. 2450 HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 2451 ConsumeAnnotationToken(); 2452 } else if (Tok.is(tok::colon)) { 2453 SourceLocation ColonLoc = ConsumeToken(); 2454 if (!getLangOpts().CPlusPlusModules) 2455 Diag(ColonLoc, diag::err_unsupported_module_partition) 2456 << SourceRange(ColonLoc, Path.back().getLoc()); 2457 // Recover by leaving partition empty. 2458 else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true)) 2459 return nullptr; 2460 else 2461 IsPartition = true; 2462 } else { 2463 if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true)) 2464 return nullptr; 2465 } 2466 2467 ParsedAttributes Attrs(AttrFactory); 2468 MaybeParseCXX11Attributes(Attrs); 2469 // We don't support any module import attributes yet. 2470 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr, 2471 diag::err_keyword_not_import_attr, 2472 /*DiagnoseEmptyAttrs=*/false, 2473 /*WarnOnUnknownAttrs=*/true); 2474 2475 if (PP.hadModuleLoaderFatalFailure()) { 2476 // With a fatal failure in the module loader, we abort parsing. 2477 cutOffParsing(); 2478 return nullptr; 2479 } 2480 2481 // Diagnose mis-imports. 2482 bool SeenError = true; 2483 switch (ImportState) { 2484 case Sema::ModuleImportState::ImportAllowed: 2485 SeenError = false; 2486 break; 2487 case Sema::ModuleImportState::FirstDecl: 2488 // If we found an import decl as the first declaration, we must be not in 2489 // a C++20 module unit or we are in an invalid state. 2490 ImportState = Sema::ModuleImportState::NotACXX20Module; 2491 [[fallthrough]]; 2492 case Sema::ModuleImportState::NotACXX20Module: 2493 // We can only import a partition within a module purview. 2494 if (IsPartition) 2495 Diag(ImportLoc, diag::err_partition_import_outside_module); 2496 else 2497 SeenError = false; 2498 break; 2499 case Sema::ModuleImportState::GlobalFragment: 2500 case Sema::ModuleImportState::PrivateFragmentImportAllowed: 2501 // We can only have pre-processor directives in the global module fragment 2502 // which allows pp-import, but not of a partition (since the global module 2503 // does not have partitions). 2504 // We cannot import a partition into a private module fragment, since 2505 // [module.private.frag]/1 disallows private module fragments in a multi- 2506 // TU module. 2507 if (IsPartition || (HeaderUnit && HeaderUnit->Kind != 2508 Module::ModuleKind::ModuleHeaderUnit)) 2509 Diag(ImportLoc, diag::err_import_in_wrong_fragment) 2510 << IsPartition 2511 << (ImportState == Sema::ModuleImportState::GlobalFragment ? 0 : 1); 2512 else 2513 SeenError = false; 2514 break; 2515 case Sema::ModuleImportState::ImportFinished: 2516 case Sema::ModuleImportState::PrivateFragmentImportFinished: 2517 if (getLangOpts().CPlusPlusModules) 2518 Diag(ImportLoc, diag::err_import_not_allowed_here); 2519 else 2520 SeenError = false; 2521 break; 2522 } 2523 ExpectAndConsumeSemi(diag::err_module_expected_semi); 2524 2525 if (SeenError) 2526 return nullptr; 2527 2528 DeclResult Import; 2529 if (HeaderUnit) 2530 Import = 2531 Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit); 2532 else if (!Path.empty()) 2533 Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path, 2534 IsPartition); 2535 if (Import.isInvalid()) 2536 return nullptr; 2537 2538 // Using '@import' in framework headers requires modules to be enabled so that 2539 // the header is parseable. Emit a warning to make the user aware. 2540 if (IsObjCAtImport && AtLoc.isValid()) { 2541 auto &SrcMgr = PP.getSourceManager(); 2542 auto FE = SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(AtLoc)); 2543 if (FE && llvm::sys::path::parent_path(FE->getDir().getName()) 2544 .ends_with(".framework")) 2545 Diags.Report(AtLoc, diag::warn_atimport_in_framework_header); 2546 } 2547 2548 return Import.get(); 2549 } 2550 2551 bool Parser::ParseModuleName(SourceLocation UseLoc, 2552 SmallVectorImpl<IdentifierLoc> &Path, 2553 bool IsImport) { 2554 // Parse the module path. 2555 while (true) { 2556 if (!Tok.is(tok::identifier)) { 2557 if (Tok.is(tok::code_completion)) { 2558 cutOffParsing(); 2559 Actions.CodeCompletion().CodeCompleteModuleImport(UseLoc, Path); 2560 return true; 2561 } 2562 2563 Diag(Tok, diag::err_module_expected_ident) << IsImport; 2564 SkipUntil(tok::semi); 2565 return true; 2566 } 2567 2568 // Record this part of the module path. 2569 Path.emplace_back(Tok.getLocation(), Tok.getIdentifierInfo()); 2570 ConsumeToken(); 2571 2572 if (Tok.isNot(tok::period)) 2573 return false; 2574 2575 ConsumeToken(); 2576 } 2577 } 2578 2579 bool Parser::parseMisplacedModuleImport() { 2580 while (true) { 2581 switch (Tok.getKind()) { 2582 case tok::annot_module_end: 2583 // If we recovered from a misplaced module begin, we expect to hit a 2584 // misplaced module end too. Stay in the current context when this 2585 // happens. 2586 if (MisplacedModuleBeginCount) { 2587 --MisplacedModuleBeginCount; 2588 Actions.ActOnAnnotModuleEnd( 2589 Tok.getLocation(), 2590 reinterpret_cast<Module *>(Tok.getAnnotationValue())); 2591 ConsumeAnnotationToken(); 2592 continue; 2593 } 2594 // Inform caller that recovery failed, the error must be handled at upper 2595 // level. This will generate the desired "missing '}' at end of module" 2596 // diagnostics on the way out. 2597 return true; 2598 case tok::annot_module_begin: 2599 // Recover by entering the module (Sema will diagnose). 2600 Actions.ActOnAnnotModuleBegin( 2601 Tok.getLocation(), 2602 reinterpret_cast<Module *>(Tok.getAnnotationValue())); 2603 ConsumeAnnotationToken(); 2604 ++MisplacedModuleBeginCount; 2605 continue; 2606 case tok::annot_module_include: 2607 // Module import found where it should not be, for instance, inside a 2608 // namespace. Recover by importing the module. 2609 Actions.ActOnAnnotModuleInclude( 2610 Tok.getLocation(), 2611 reinterpret_cast<Module *>(Tok.getAnnotationValue())); 2612 ConsumeAnnotationToken(); 2613 // If there is another module import, process it. 2614 continue; 2615 default: 2616 return false; 2617 } 2618 } 2619 return false; 2620 } 2621 2622 void Parser::diagnoseUseOfC11Keyword(const Token &Tok) { 2623 // Warn that this is a C11 extension if in an older mode or if in C++. 2624 // Otherwise, warn that it is incompatible with standards before C11 if in 2625 // C11 or later. 2626 Diag(Tok, getLangOpts().C11 ? diag::warn_c11_compat_keyword 2627 : diag::ext_c11_feature) 2628 << Tok.getName(); 2629 } 2630 2631 bool BalancedDelimiterTracker::diagnoseOverflow() { 2632 P.Diag(P.Tok, diag::err_bracket_depth_exceeded) 2633 << P.getLangOpts().BracketDepth; 2634 P.Diag(P.Tok, diag::note_bracket_depth); 2635 P.cutOffParsing(); 2636 return true; 2637 } 2638 2639 bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID, 2640 const char *Msg, 2641 tok::TokenKind SkipToTok) { 2642 LOpen = P.Tok.getLocation(); 2643 if (P.ExpectAndConsume(Kind, DiagID, Msg)) { 2644 if (SkipToTok != tok::unknown) 2645 P.SkipUntil(SkipToTok, Parser::StopAtSemi); 2646 return true; 2647 } 2648 2649 if (getDepth() < P.getLangOpts().BracketDepth) 2650 return false; 2651 2652 return diagnoseOverflow(); 2653 } 2654 2655 bool BalancedDelimiterTracker::diagnoseMissingClose() { 2656 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter"); 2657 2658 if (P.Tok.is(tok::annot_module_end)) 2659 P.Diag(P.Tok, diag::err_missing_before_module_end) << Close; 2660 else 2661 P.Diag(P.Tok, diag::err_expected) << Close; 2662 P.Diag(LOpen, diag::note_matching) << Kind; 2663 2664 // If we're not already at some kind of closing bracket, skip to our closing 2665 // token. 2666 if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) && 2667 P.Tok.isNot(tok::r_square) && 2668 P.SkipUntil(Close, FinalToken, 2669 Parser::StopAtSemi | Parser::StopBeforeMatch) && 2670 P.Tok.is(Close)) 2671 LClose = P.ConsumeAnyToken(); 2672 return true; 2673 } 2674 2675 void BalancedDelimiterTracker::skipToEnd() { 2676 P.SkipUntil(Close, Parser::StopBeforeMatch); 2677 consumeClose(); 2678 } 2679