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