2681 lines · cpp
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-exception6//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"29using namespace clang;30 31 32namespace {33/// A comment handler that passes comments found by the preprocessor34/// to the parser action.35class ActionCommentHandler : public CommentHandler {36 Sema &S;37 38public: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 namespace47 48IdentifierInfo *Parser::getSEHExceptKeyword() {49 // __except is accepted as a (contextual) keyword50 if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))51 Ident__except = PP.getIdentifierInfo("__except");52 53 return Ident__except;54}55 56Parser::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 the71 // 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 85DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {86 return Diags.Report(Loc, DiagID);87}88 89DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {90 return Diag(Tok.getLocation(), DiagID);91}92 93DiagnosticBuilder Parser::DiagCompat(SourceLocation Loc,94 unsigned CompatDiagId) {95 return Diag(Loc,96 DiagnosticIDs::getCXXCompatDiagId(getLangOpts(), CompatDiagId));97}98 99DiagnosticBuilder Parser::DiagCompat(const Token &Tok, unsigned CompatDiagId) {100 return DiagCompat(Tok.getLocation(), CompatDiagId);101}102 103void 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 the108 // 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 118static 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 126bool 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 else145 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 Spelling160 ? 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 else167 DB << Msg;168 169 return true;170}171 172bool 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 194void 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 the209 // 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 else215 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 << Kind223 << DeclSpec::getSpecifierName(224 TST, Actions.getASTContext().getPrintingPolicy())225 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));226 else227 // 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 232bool 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 247void 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 same254 // 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 283static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {284 return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;285}286 287bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) {288 // We always want this function to skip at least one token if the first token289 // 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 to305 // skip the rest of the file. Do this without recursing, since we can306 // 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 where341 // 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 else355 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 else363 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 else371 SkipUntil(tok::r_brace);372 break;373 case tok::question:374 // Recursively skip ? ... : pairs; these function as brackets. But375 // 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 would384 // already be handled), this isn't balanced. If there is a LHS token at a385 // higher level, we will assume that this matches the unbalanced token386 // 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 manipulation418//===----------------------------------------------------------------------===//419 420void 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 430void Parser::ExitScope() {431 assert(getCurScope() && "Scope imbalance!");432 433 // Inform the actions module that this scope is going away if there are any434 // 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 else443 ScopeCache[NumCachedScopes++] = OldScope;444}445 446Parser::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 455Parser::ParseScopeFlags::~ParseScopeFlags() {456 if (CurScope)457 CurScope->setFlags(OldFlags);458}459 460 461//===----------------------------------------------------------------------===//462// C99 6.9: External Definitions.463//===----------------------------------------------------------------------===//464 465Parser::~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 483void 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 588void Parser::DestroyTemplateIds() {589 for (TemplateIdAnnotation *Id : TemplateIds)590 Id->Destroy();591 TemplateIds.clear();592}593 594bool 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 also599 // 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-level604 // declaration. C++ doesn't have this restriction. We also don't want to605 // complain if we have a precompiled header, although technically if the PCH606 // 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 615bool 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 under631 // the Standard C++ Modules, and in that case 'export import' is parsed as632 // 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 else643 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 and669 // 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, 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' or719 // '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 else727 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 the738 // standard attributes are applied to the declaration. We parse the two739 // attribute sets into different containters so we can apply them during740 // 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, ignore747 // 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 763Parser::DeclGroupPtrTy764Parser::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 introduce874 // 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 lack935 // 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 is961 // 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 is983 // 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 templates999 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 a1036 // single decl, convert it now.1037 return Actions.ConvertDeclToDeclGroup(SingleDecl);1038}1039 1040bool 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 def1049 Tok.is(tok::comma) || // int X(), -> not a function def1050 Tok.is(tok::semi) || // int X(); -> not a function def1051 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def1052 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def1053 (getLangOpts().CPlusPlus &&1054 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]1055}1056 1057bool 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 1076Parser::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 simply1080 // overwrite the source range and attribute the provided leading declspec1081 // 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.takeAttributesAppendingingFrom(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, we1095 // 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 accept1146 // 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.takeAttributesAppendingingFrom(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 string1177 // literal following it, this must be a C++ linkage specifier like1178 // '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 1190Parser::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 with1194 // "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 for1205 // parsing c constructs and re-enter objc container scope1206 // afterwards.1207 ObjCDeclContextSwitch ObjCDC(*this);1208 1209 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, PDS, AS);1210 }1211}1212 1213Decl *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 an1226 // implicit int. We do this here because this is the only place where1227 // 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 the1242 // 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 on1264 // 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 the1272 // 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 LateParsedAttrs->empty() && 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 if (CurParsedObjCImpl && !TemplateInfo.TemplateParams &&1305 (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || Tok.is(tok::colon)) &&1306 Actions.CurContext->isTranslationUnit()) {1307 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |1308 Scope::CompoundStmtScope);1309 Scope *ParentScope = getCurScope()->getParent();1310 1311 D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);1312 Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,1313 MultiTemplateParamsArg());1314 D.complete(FuncDecl);1315 D.getMutableDeclSpec().abort();1316 if (FuncDecl) {1317 // Consume the tokens and store them for later parsing.1318 StashAwayMethodOrFunctionBodyTokens(FuncDecl);1319 CurParsedObjCImpl->HasCFunction = true;1320 return FuncDecl;1321 }1322 // FIXME: Should we really fall through here?1323 }1324 1325 // Enter a scope for the function body.1326 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |1327 Scope::CompoundStmtScope);1328 1329 // Parse function body eagerly if it is either '= delete;' or '= default;' as1330 // ActOnStartOfFunctionDef needs to know whether the function is deleted.1331 StringLiteral *DeletedMessage = nullptr;1332 Sema::FnBodyKind BodyKind = Sema::FnBodyKind::Other;1333 SourceLocation KWLoc;1334 if (TryConsumeToken(tok::equal)) {1335 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");1336 1337 if (TryConsumeToken(tok::kw_delete, KWLoc)) {1338 Diag(KWLoc, getLangOpts().CPlusPlus111339 ? diag::warn_cxx98_compat_defaulted_deleted_function1340 : diag::ext_defaulted_deleted_function)1341 << 1 /* deleted */;1342 BodyKind = Sema::FnBodyKind::Delete;1343 DeletedMessage = ParseCXXDeletedFunctionMessage();1344 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {1345 Diag(KWLoc, getLangOpts().CPlusPlus111346 ? diag::warn_cxx98_compat_defaulted_deleted_function1347 : diag::ext_defaulted_deleted_function)1348 << 0 /* defaulted */;1349 BodyKind = Sema::FnBodyKind::Default;1350 } else {1351 llvm_unreachable("function definition after = not 'delete' or 'default'");1352 }1353 1354 if (Tok.is(tok::comma)) {1355 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)1356 << (BodyKind == Sema::FnBodyKind::Delete);1357 SkipUntil(tok::semi);1358 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,1359 BodyKind == Sema::FnBodyKind::Delete1360 ? "delete"1361 : "default")) {1362 SkipUntil(tok::semi);1363 }1364 }1365 1366 Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);1367 1368 // Tell the actions module that we have entered a function definition with the1369 // specified Declarator for the function.1370 SkipBodyInfo SkipBody;1371 Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,1372 TemplateInfo.TemplateParams1373 ? *TemplateInfo.TemplateParams1374 : MultiTemplateParamsArg(),1375 &SkipBody, BodyKind);1376 1377 if (SkipBody.ShouldSkip) {1378 // Do NOT enter SkipFunctionBody if we already consumed the tokens.1379 if (BodyKind == Sema::FnBodyKind::Other)1380 SkipFunctionBody();1381 1382 // ExpressionEvaluationContext is pushed in ActOnStartOfFunctionDef1383 // and it would be popped in ActOnFinishFunctionBody.1384 // We pop it explcitly here since ActOnFinishFunctionBody won't get called.1385 //1386 // Do not call PopExpressionEvaluationContext() if it is a lambda because1387 // one is already popped when finishing the lambda in BuildLambdaExpr().1388 //1389 // FIXME: It looks not easy to balance PushExpressionEvaluationContext()1390 // and PopExpressionEvaluationContext().1391 if (!isLambdaCallOperator(dyn_cast_if_present<FunctionDecl>(Res)))1392 Actions.PopExpressionEvaluationContext();1393 return Res;1394 }1395 1396 // Break out of the ParsingDeclarator context before we parse the body.1397 D.complete(Res);1398 1399 // Break out of the ParsingDeclSpec context, too. This const_cast is1400 // safe because we're always the sole owner.1401 D.getMutableDeclSpec().abort();1402 1403 if (BodyKind != Sema::FnBodyKind::Other) {1404 Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage);1405 Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;1406 Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);1407 return Res;1408 }1409 1410 // With abbreviated function templates - we need to explicitly add depth to1411 // account for the implicit template parameter list induced by the template.1412 if (const auto *Template = dyn_cast_if_present<FunctionTemplateDecl>(Res);1413 Template && Template->isAbbreviated() &&1414 Template->getTemplateParameters()->getParam(0)->isImplicit())1415 // First template parameter is implicit - meaning no explicit template1416 // parameter list was specified.1417 CurTemplateDepthTracker.addDepth(1);1418 1419 // Late attributes are parsed in the same scope as the function body.1420 if (LateParsedAttrs)1421 ParseLexedAttributeList(*LateParsedAttrs, Res, /*EnterScope=*/false,1422 /*OnDefinition=*/true);1423 1424 if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&1425 trySkippingFunctionBody()) {1426 BodyScope.Exit();1427 Actions.ActOnSkippedFunctionBody(Res);1428 return Actions.ActOnFinishFunctionBody(Res, nullptr, false);1429 }1430 1431 if (Tok.is(tok::kw_try))1432 return ParseFunctionTryBlock(Res, BodyScope);1433 1434 // If we have a colon, then we're probably parsing a C++1435 // ctor-initializer.1436 if (Tok.is(tok::colon)) {1437 ParseConstructorInitializer(Res);1438 1439 // Recover from error.1440 if (!Tok.is(tok::l_brace)) {1441 BodyScope.Exit();1442 Actions.ActOnFinishFunctionBody(Res, nullptr);1443 return Res;1444 }1445 } else1446 Actions.ActOnDefaultCtorInitializers(Res);1447 1448 return ParseFunctionStatementBody(Res, BodyScope);1449}1450 1451void Parser::SkipFunctionBody() {1452 if (Tok.is(tok::equal)) {1453 SkipUntil(tok::semi);1454 return;1455 }1456 1457 bool IsFunctionTryBlock = Tok.is(tok::kw_try);1458 if (IsFunctionTryBlock)1459 ConsumeToken();1460 1461 CachedTokens Skipped;1462 if (ConsumeAndStoreFunctionPrologue(Skipped))1463 SkipMalformedDecl();1464 else {1465 SkipUntil(tok::r_brace);1466 while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {1467 SkipUntil(tok::l_brace);1468 SkipUntil(tok::r_brace);1469 }1470 }1471}1472 1473void Parser::ParseKNRParamDeclarations(Declarator &D) {1474 // We know that the top-level of this declarator is a function.1475 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();1476 1477 // Enter function-declaration scope, limiting any declarators to the1478 // function prototype scope, including parameter declarators.1479 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |1480 Scope::FunctionDeclarationScope | Scope::DeclScope);1481 1482 // Read all the argument declarations.1483 while (isDeclarationSpecifier(ImplicitTypenameContext::No)) {1484 SourceLocation DSStart = Tok.getLocation();1485 1486 // Parse the common declaration-specifiers piece.1487 DeclSpec DS(AttrFactory);1488 ParsedTemplateInfo TemplateInfo;1489 ParseDeclarationSpecifiers(DS, TemplateInfo);1490 1491 // C99 6.9.1p6: 'each declaration in the declaration list shall have at1492 // least one declarator'.1493 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with1494 // the declarations though. It's trivial to ignore them, really hard to do1495 // anything else with them.1496 if (TryConsumeToken(tok::semi)) {1497 Diag(DSStart, diag::err_declaration_does_not_declare_param);1498 continue;1499 }1500 1501 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other1502 // than register.1503 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&1504 DS.getStorageClassSpec() != DeclSpec::SCS_register) {1505 Diag(DS.getStorageClassSpecLoc(),1506 diag::err_invalid_storage_class_in_func_decl);1507 DS.ClearStorageClassSpecs();1508 }1509 if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) {1510 Diag(DS.getThreadStorageClassSpecLoc(),1511 diag::err_invalid_storage_class_in_func_decl);1512 DS.ClearStorageClassSpecs();1513 }1514 1515 // Parse the first declarator attached to this declspec.1516 Declarator ParmDeclarator(DS, ParsedAttributesView::none(),1517 DeclaratorContext::KNRTypeList);1518 ParseDeclarator(ParmDeclarator);1519 1520 // Handle the full declarator list.1521 while (true) {1522 // If attributes are present, parse them.1523 MaybeParseGNUAttributes(ParmDeclarator);1524 1525 // Ask the actions module to compute the type for this declarator.1526 Decl *Param =1527 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);1528 1529 if (Param &&1530 // A missing identifier has already been diagnosed.1531 ParmDeclarator.getIdentifier()) {1532 1533 // Scan the argument list looking for the correct param to apply this1534 // type.1535 for (unsigned i = 0; ; ++i) {1536 // C99 6.9.1p6: those declarators shall declare only identifiers from1537 // the identifier list.1538 if (i == FTI.NumParams) {1539 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)1540 << ParmDeclarator.getIdentifier();1541 break;1542 }1543 1544 if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {1545 // Reject redefinitions of parameters.1546 if (FTI.Params[i].Param) {1547 Diag(ParmDeclarator.getIdentifierLoc(),1548 diag::err_param_redefinition)1549 << ParmDeclarator.getIdentifier();1550 } else {1551 FTI.Params[i].Param = Param;1552 }1553 break;1554 }1555 }1556 }1557 1558 // If we don't have a comma, it is either the end of the list (a ';') or1559 // an error, bail out.1560 if (Tok.isNot(tok::comma))1561 break;1562 1563 ParmDeclarator.clear();1564 1565 // Consume the comma.1566 ParmDeclarator.setCommaLoc(ConsumeToken());1567 1568 // Parse the next declarator.1569 ParseDeclarator(ParmDeclarator);1570 }1571 1572 // Consume ';' and continue parsing.1573 if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))1574 continue;1575 1576 // Otherwise recover by skipping to next semi or mandatory function body.1577 if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))1578 break;1579 TryConsumeToken(tok::semi);1580 }1581 1582 // The actions module must verify that all arguments were declared.1583 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());1584}1585 1586ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {1587 1588 ExprResult AsmString;1589 if (isTokenStringLiteral()) {1590 AsmString = ParseStringLiteralExpression();1591 if (AsmString.isInvalid())1592 return AsmString;1593 1594 const auto *SL = cast<StringLiteral>(AsmString.get());1595 if (!SL->isOrdinary()) {1596 Diag(Tok, diag::err_asm_operand_wide_string_literal)1597 << SL->isWide() << SL->getSourceRange();1598 return ExprError();1599 }1600 } else if (!ForAsmLabel && getLangOpts().CPlusPlus11 &&1601 Tok.is(tok::l_paren)) {1602 ParenParseOption ExprType = ParenParseOption::SimpleExpr;1603 SourceLocation RParenLoc;1604 ParsedType CastTy;1605 1606 EnterExpressionEvaluationContext ConstantEvaluated(1607 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);1608 AsmString = ParseParenExpression(1609 ExprType, /*StopIfCastExr=*/true, ParenExprKind::Unknown,1610 TypoCorrectionTypeBehavior::AllowBoth, CastTy, RParenLoc);1611 if (!AsmString.isInvalid())1612 AsmString = Actions.ActOnConstantExpression(AsmString);1613 1614 if (AsmString.isInvalid())1615 return ExprError();1616 } else {1617 Diag(Tok, diag::err_asm_expected_string) << /*and expression=*/(1618 (getLangOpts().CPlusPlus11 && !ForAsmLabel) ? 0 : 1);1619 }1620 1621 return Actions.ActOnGCCAsmStmtString(AsmString.get(), ForAsmLabel);1622}1623 1624ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) {1625 assert(Tok.is(tok::kw_asm) && "Not an asm!");1626 SourceLocation Loc = ConsumeToken();1627 1628 if (isGNUAsmQualifier(Tok)) {1629 // Remove from the end of 'asm' to the end of the asm qualifier.1630 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),1631 PP.getLocForEndOfToken(Tok.getLocation()));1632 Diag(Tok, diag::err_global_asm_qualifier_ignored)1633 << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok))1634 << FixItHint::CreateRemoval(RemovalRange);1635 ConsumeToken();1636 }1637 1638 BalancedDelimiterTracker T(*this, tok::l_paren);1639 if (T.consumeOpen()) {1640 Diag(Tok, diag::err_expected_lparen_after) << "asm";1641 return ExprError();1642 }1643 1644 ExprResult Result(ParseAsmStringLiteral(ForAsmLabel));1645 1646 if (!Result.isInvalid()) {1647 // Close the paren and get the location of the end bracket1648 T.consumeClose();1649 if (EndLoc)1650 *EndLoc = T.getCloseLocation();1651 } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {1652 if (EndLoc)1653 *EndLoc = Tok.getLocation();1654 ConsumeParen();1655 }1656 1657 return Result;1658}1659 1660TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {1661 assert(tok.is(tok::annot_template_id) && "Expected template-id token");1662 TemplateIdAnnotation *1663 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());1664 return Id;1665}1666 1667void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {1668 // Push the current token back into the token stream (or revert it if it is1669 // cached) and use an annotation scope token for current token.1670 if (PP.isBacktrackEnabled())1671 PP.RevertCachedTokens(1);1672 else1673 PP.EnterToken(Tok, /*IsReinject=*/true);1674 Tok.setKind(tok::annot_cxxscope);1675 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));1676 Tok.setAnnotationRange(SS.getRange());1677 1678 // In case the tokens were cached, have Preprocessor replace them1679 // with the annotation token. We don't need to do this if we've1680 // just reverted back to a prior state.1681 if (IsNewAnnotation)1682 PP.AnnotateCachedTokens(Tok);1683}1684 1685AnnotatedNameKind1686Parser::TryAnnotateName(CorrectionCandidateCallback *CCC,1687 ImplicitTypenameContext AllowImplicitTypename) {1688 assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));1689 1690 const bool EnteringContext = false;1691 const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);1692 1693 CXXScopeSpec SS;1694 if (getLangOpts().CPlusPlus &&1695 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,1696 /*ObjectHasErrors=*/false,1697 EnteringContext))1698 return AnnotatedNameKind::Error;1699 1700 if (Tok.isNot(tok::identifier) || SS.isInvalid()) {1701 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,1702 AllowImplicitTypename))1703 return AnnotatedNameKind::Error;1704 return AnnotatedNameKind::Unresolved;1705 }1706 1707 IdentifierInfo *Name = Tok.getIdentifierInfo();1708 SourceLocation NameLoc = Tok.getLocation();1709 1710 // FIXME: Move the tentative declaration logic into ClassifyName so we can1711 // typo-correct to tentatively-declared identifiers.1712 if (isTentativelyDeclared(Name) && SS.isEmpty()) {1713 // Identifier has been tentatively declared, and thus cannot be resolved as1714 // an expression. Fall back to annotating it as a type.1715 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,1716 AllowImplicitTypename))1717 return AnnotatedNameKind::Error;1718 return Tok.is(tok::annot_typename) ? AnnotatedNameKind::Success1719 : AnnotatedNameKind::TentativeDecl;1720 }1721 1722 Token Next = NextToken();1723 1724 // Look up and classify the identifier. We don't perform any typo-correction1725 // after a scope specifier, because in general we can't recover from typos1726 // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to1727 // jump back into scope specifier parsing).1728 Sema::NameClassification Classification = Actions.ClassifyName(1729 getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr);1730 1731 // If name lookup found nothing and we guessed that this was a template name,1732 // double-check before committing to that interpretation. C++20 requires that1733 // we interpret this as a template-id if it can be, but if it can't be, then1734 // this is an error recovery case.1735 if (Classification.getKind() == NameClassificationKind::UndeclaredTemplate &&1736 isTemplateArgumentList(1) == TPResult::False) {1737 // It's not a template-id; re-classify without the '<' as a hint.1738 Token FakeNext = Next;1739 FakeNext.setKind(tok::unknown);1740 Classification =1741 Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext,1742 SS.isEmpty() ? CCC : nullptr);1743 }1744 1745 switch (Classification.getKind()) {1746 case NameClassificationKind::Error:1747 return AnnotatedNameKind::Error;1748 1749 case NameClassificationKind::Keyword:1750 // The identifier was typo-corrected to a keyword.1751 Tok.setIdentifierInfo(Name);1752 Tok.setKind(Name->getTokenID());1753 PP.TypoCorrectToken(Tok);1754 if (SS.isNotEmpty())1755 AnnotateScopeToken(SS, !WasScopeAnnotation);1756 // We've "annotated" this as a keyword.1757 return AnnotatedNameKind::Success;1758 1759 case NameClassificationKind::Unknown:1760 // It's not something we know about. Leave it unannotated.1761 break;1762 1763 case NameClassificationKind::Type: {1764 if (TryAltiVecVectorToken())1765 // vector has been found as a type id when altivec is enabled but1766 // this is followed by a declaration specifier so this is really the1767 // altivec vector token. Leave it unannotated.1768 break;1769 SourceLocation BeginLoc = NameLoc;1770 if (SS.isNotEmpty())1771 BeginLoc = SS.getBeginLoc();1772 1773 /// An Objective-C object type followed by '<' is a specialization of1774 /// a parameterized class type or a protocol-qualified type.1775 ParsedType Ty = Classification.getType();1776 QualType T = Actions.GetTypeFromParser(Ty);1777 if (getLangOpts().ObjC && NextToken().is(tok::less) &&1778 (T->isObjCObjectType() || T->isObjCObjectPointerType())) {1779 // Consume the name.1780 SourceLocation IdentifierLoc = ConsumeToken();1781 SourceLocation NewEndLoc;1782 TypeResult NewType1783 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,1784 /*consumeLastToken=*/false,1785 NewEndLoc);1786 if (NewType.isUsable())1787 Ty = NewType.get();1788 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...1789 return AnnotatedNameKind::Error;1790 }1791 1792 Tok.setKind(tok::annot_typename);1793 setTypeAnnotation(Tok, Ty);1794 Tok.setAnnotationEndLoc(Tok.getLocation());1795 Tok.setLocation(BeginLoc);1796 PP.AnnotateCachedTokens(Tok);1797 return AnnotatedNameKind::Success;1798 }1799 1800 case NameClassificationKind::OverloadSet:1801 Tok.setKind(tok::annot_overload_set);1802 setExprAnnotation(Tok, Classification.getExpression());1803 Tok.setAnnotationEndLoc(NameLoc);1804 if (SS.isNotEmpty())1805 Tok.setLocation(SS.getBeginLoc());1806 PP.AnnotateCachedTokens(Tok);1807 return AnnotatedNameKind::Success;1808 1809 case NameClassificationKind::NonType:1810 if (TryAltiVecVectorToken())1811 // vector has been found as a non-type id when altivec is enabled but1812 // this is followed by a declaration specifier so this is really the1813 // altivec vector token. Leave it unannotated.1814 break;1815 Tok.setKind(tok::annot_non_type);1816 setNonTypeAnnotation(Tok, Classification.getNonTypeDecl());1817 Tok.setLocation(NameLoc);1818 Tok.setAnnotationEndLoc(NameLoc);1819 PP.AnnotateCachedTokens(Tok);1820 if (SS.isNotEmpty())1821 AnnotateScopeToken(SS, !WasScopeAnnotation);1822 return AnnotatedNameKind::Success;1823 1824 case NameClassificationKind::UndeclaredNonType:1825 case NameClassificationKind::DependentNonType:1826 Tok.setKind(Classification.getKind() ==1827 NameClassificationKind::UndeclaredNonType1828 ? tok::annot_non_type_undeclared1829 : tok::annot_non_type_dependent);1830 setIdentifierAnnotation(Tok, Name);1831 Tok.setLocation(NameLoc);1832 Tok.setAnnotationEndLoc(NameLoc);1833 PP.AnnotateCachedTokens(Tok);1834 if (SS.isNotEmpty())1835 AnnotateScopeToken(SS, !WasScopeAnnotation);1836 return AnnotatedNameKind::Success;1837 1838 case NameClassificationKind::TypeTemplate:1839 if (Next.isNot(tok::less)) {1840 // This may be a type or variable template being used as a template1841 // 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 so1854 // we reach the '<' and annotate it.1855 UnqualifiedId Id;1856 Id.setIdentifier(Name, NameLoc);1857 if (Next.is(tok::less))1858 ConsumeToken();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 1877SourceLocation Parser::getEndOfPreviousToken() const {1878 SourceLocation TokenEndLoc = PP.getLocForEndOfToken(PrevTokLocation);1879 return TokenEndLoc.isValid() ? TokenEndLoc : Tok.getLocation();1880}1881 1882bool 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 1893bool 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 have1907 // 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 identifier1924 // 'typename' '::' [opt] nested-name-specifier template [opt]1925 // simple-template-id1926 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 2019bool 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 QualType T = Actions.GetTypeFromParser(Ty);2035 2036 /// An Objective-C object type followed by '<' is a specialization of2037 /// a parameterized class type or a protocol-qualified type.2038 if (getLangOpts().ObjC && NextToken().is(tok::less) &&2039 (T->isObjCObjectType() || T->isObjCObjectPointerType())) {2040 // Consume the name.2041 SourceLocation IdentifierLoc = ConsumeToken();2042 SourceLocation NewEndLoc;2043 TypeResult NewType2044 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,2045 /*consumeLastToken=*/false,2046 NewEndLoc);2047 if (NewType.isUsable())2048 Ty = NewType.get();2049 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...2050 return false;2051 }2052 2053 // This is a typename. Replace the current token in-place with an2054 // annotation type token.2055 Tok.setKind(tok::annot_typename);2056 setTypeAnnotation(Tok, Ty);2057 Tok.setAnnotationEndLoc(Tok.getLocation());2058 Tok.setLocation(BeginLoc);2059 2060 // In case the tokens were cached, have Preprocessor replace2061 // them with the annotation token.2062 PP.AnnotateCachedTokens(Tok);2063 return false;2064 }2065 2066 if (!getLangOpts().CPlusPlus) {2067 // If we're in C, the only place we can have :: tokens is C232068 // attribute which is parsed elsewhere. If the identifier is not a type,2069 // then it can't be scope either, just early exit.2070 return false;2071 }2072 2073 // If this is a template-id, annotate with a template-id or type token.2074 // FIXME: This appears to be dead code. We already have formed template-id2075 // tokens when parsing the scope specifier; this can never form a new one.2076 if (NextToken().is(tok::less)) {2077 TemplateTy Template;2078 UnqualifiedId TemplateName;2079 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());2080 bool MemberOfUnknownSpecialization;2081 if (TemplateNameKind TNK = Actions.isTemplateName(2082 getCurScope(), SS,2083 /*hasTemplateKeyword=*/false, TemplateName,2084 /*ObjectType=*/nullptr, /*EnteringContext*/false, Template,2085 MemberOfUnknownSpecialization)) {2086 // Only annotate an undeclared template name as a template-id if the2087 // following tokens have the form of a template argument list.2088 if (TNK != TNK_Undeclared_template ||2089 isTemplateArgumentList(1) != TPResult::False) {2090 // Consume the identifier.2091 ConsumeToken();2092 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),2093 TemplateName)) {2094 // If an unrecoverable error occurred, we need to return true here,2095 // because the token stream is in a damaged state. We may not2096 // return a valid identifier.2097 return true;2098 }2099 }2100 }2101 }2102 2103 // The current token, which is either an identifier or a2104 // template-id, is not part of the annotation. Fall through to2105 // push that token back into the stream and complete the C++ scope2106 // specifier annotation.2107 }2108 2109 if (Tok.is(tok::annot_template_id)) {2110 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);2111 if (TemplateId->Kind == TNK_Type_template) {2112 // A template-id that refers to a type was parsed into a2113 // template-id annotation in a context where we weren't allowed2114 // to produce a type annotation token. Update the template-id2115 // annotation token to a type annotation token now.2116 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);2117 return false;2118 }2119 }2120 2121 if (SS.isEmpty()) {2122 if (getLangOpts().ObjC && !getLangOpts().CPlusPlus &&2123 Tok.is(tok::coloncolon)) {2124 // ObjectiveC does not allow :: as as a scope token.2125 Diag(ConsumeToken(), diag::err_expected_type);2126 return true;2127 }2128 return false;2129 }2130 2131 // A C++ scope specifier that isn't followed by a typename.2132 AnnotateScopeToken(SS, IsNewScope);2133 return false;2134}2135 2136bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {2137 assert(getLangOpts().CPlusPlus &&2138 "Call sites of this function should be guarded by checking for C++");2139 assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");2140 2141 CXXScopeSpec SS;2142 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,2143 /*ObjectHasErrors=*/false,2144 EnteringContext))2145 return true;2146 if (SS.isEmpty())2147 return false;2148 2149 AnnotateScopeToken(SS, true);2150 return false;2151}2152 2153bool Parser::isTokenEqualOrEqualTypo() {2154 tok::TokenKind Kind = Tok.getKind();2155 switch (Kind) {2156 default:2157 return false;2158 case tok::ampequal: // &=2159 case tok::starequal: // *=2160 case tok::plusequal: // +=2161 case tok::minusequal: // -=2162 case tok::exclaimequal: // !=2163 case tok::slashequal: // /=2164 case tok::percentequal: // %=2165 case tok::lessequal: // <=2166 case tok::lesslessequal: // <<=2167 case tok::greaterequal: // >=2168 case tok::greatergreaterequal: // >>=2169 case tok::caretequal: // ^=2170 case tok::pipeequal: // |=2171 case tok::equalequal: // ==2172 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)2173 << Kind2174 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");2175 [[fallthrough]];2176 case tok::equal:2177 return true;2178 }2179}2180 2181SourceLocation Parser::handleUnexpectedCodeCompletionToken() {2182 assert(Tok.is(tok::code_completion));2183 PrevTokLocation = Tok.getLocation();2184 2185 for (Scope *S = getCurScope(); S; S = S->getParent()) {2186 if (S->isFunctionScope()) {2187 cutOffParsing();2188 Actions.CodeCompletion().CodeCompleteOrdinaryName(2189 getCurScope(), SemaCodeCompletion::PCC_RecoveryInFunction);2190 return PrevTokLocation;2191 }2192 2193 if (S->isClassScope()) {2194 cutOffParsing();2195 Actions.CodeCompletion().CodeCompleteOrdinaryName(2196 getCurScope(), SemaCodeCompletion::PCC_Class);2197 return PrevTokLocation;2198 }2199 }2200 2201 cutOffParsing();2202 Actions.CodeCompletion().CodeCompleteOrdinaryName(2203 getCurScope(), SemaCodeCompletion::PCC_Namespace);2204 return PrevTokLocation;2205}2206 2207// Code-completion pass-through functions2208 2209void Parser::CodeCompleteDirective(bool InConditional) {2210 Actions.CodeCompletion().CodeCompletePreprocessorDirective(InConditional);2211}2212 2213void Parser::CodeCompleteInConditionalExclusion() {2214 Actions.CodeCompletion().CodeCompleteInPreprocessorConditionalExclusion(2215 getCurScope());2216}2217 2218void Parser::CodeCompleteMacroName(bool IsDefinition) {2219 Actions.CodeCompletion().CodeCompletePreprocessorMacroName(IsDefinition);2220}2221 2222void Parser::CodeCompletePreprocessorExpression() {2223 Actions.CodeCompletion().CodeCompletePreprocessorExpression();2224}2225 2226void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,2227 MacroInfo *MacroInfo,2228 unsigned ArgumentIndex) {2229 Actions.CodeCompletion().CodeCompletePreprocessorMacroArgument(2230 getCurScope(), Macro, MacroInfo, ArgumentIndex);2231}2232 2233void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {2234 Actions.CodeCompletion().CodeCompleteIncludedFile(Dir, IsAngled);2235}2236 2237void Parser::CodeCompleteNaturalLanguage() {2238 Actions.CodeCompletion().CodeCompleteNaturalLanguage();2239}2240 2241bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {2242 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&2243 "Expected '__if_exists' or '__if_not_exists'");2244 Result.IsIfExists = Tok.is(tok::kw___if_exists);2245 Result.KeywordLoc = ConsumeToken();2246 2247 BalancedDelimiterTracker T(*this, tok::l_paren);2248 if (T.consumeOpen()) {2249 Diag(Tok, diag::err_expected_lparen_after)2250 << (Result.IsIfExists? "__if_exists" : "__if_not_exists");2251 return true;2252 }2253 2254 // Parse nested-name-specifier.2255 if (getLangOpts().CPlusPlus)2256 ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,2257 /*ObjectHasErrors=*/false,2258 /*EnteringContext=*/false);2259 2260 // Check nested-name specifier.2261 if (Result.SS.isInvalid()) {2262 T.skipToEnd();2263 return true;2264 }2265 2266 // Parse the unqualified-id.2267 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.2268 if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,2269 /*ObjectHadErrors=*/false, /*EnteringContext*/ false,2270 /*AllowDestructorName*/ true,2271 /*AllowConstructorName*/ true,2272 /*AllowDeductionGuide*/ false, &TemplateKWLoc,2273 Result.Name)) {2274 T.skipToEnd();2275 return true;2276 }2277 2278 if (T.consumeClose())2279 return true;2280 2281 // Check if the symbol exists.2282 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,2283 Result.IsIfExists, Result.SS,2284 Result.Name)) {2285 case IfExistsResult::Exists:2286 Result.Behavior =2287 Result.IsIfExists ? IfExistsBehavior::Parse : IfExistsBehavior::Skip;2288 break;2289 2290 case IfExistsResult::DoesNotExist:2291 Result.Behavior =2292 !Result.IsIfExists ? IfExistsBehavior::Parse : IfExistsBehavior::Skip;2293 break;2294 2295 case IfExistsResult::Dependent:2296 Result.Behavior = IfExistsBehavior::Dependent;2297 break;2298 2299 case IfExistsResult::Error:2300 return true;2301 }2302 2303 return false;2304}2305 2306void Parser::ParseMicrosoftIfExistsExternalDeclaration() {2307 IfExistsCondition Result;2308 if (ParseMicrosoftIfExistsCondition(Result))2309 return;2310 2311 BalancedDelimiterTracker Braces(*this, tok::l_brace);2312 if (Braces.consumeOpen()) {2313 Diag(Tok, diag::err_expected) << tok::l_brace;2314 return;2315 }2316 2317 switch (Result.Behavior) {2318 case IfExistsBehavior::Parse:2319 // Parse declarations below.2320 break;2321 2322 case IfExistsBehavior::Dependent:2323 llvm_unreachable("Cannot have a dependent external declaration");2324 2325 case IfExistsBehavior::Skip:2326 Braces.skipToEnd();2327 return;2328 }2329 2330 // Parse the declarations.2331 // FIXME: Support module import within __if_exists?2332 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {2333 ParsedAttributes Attrs(AttrFactory);2334 MaybeParseCXX11Attributes(Attrs);2335 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);2336 DeclGroupPtrTy Result = ParseExternalDeclaration(Attrs, EmptyDeclSpecAttrs);2337 if (Result && !getCurScope()->getParent())2338 Actions.getASTConsumer().HandleTopLevelDecl(Result.get());2339 }2340 Braces.consumeClose();2341}2342 2343Parser::DeclGroupPtrTy2344Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {2345 Token Introducer = Tok;2346 SourceLocation StartLoc = Introducer.getLocation();2347 2348 Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)2349 ? Sema::ModuleDeclKind::Interface2350 : Sema::ModuleDeclKind::Implementation;2351 2352 assert(2353 (Tok.is(tok::kw_module) ||2354 (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&2355 "not a module declaration");2356 SourceLocation ModuleLoc = ConsumeToken();2357 2358 // Attributes appear after the module name, not before.2359 // FIXME: Suggest moving the attributes later with a fixit.2360 DiagnoseAndSkipCXX11Attributes();2361 2362 // Parse a global-module-fragment, if present.2363 if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {2364 SourceLocation SemiLoc = ConsumeToken();2365 if (ImportState != Sema::ModuleImportState::FirstDecl ||2366 Introducer.hasSeenNoTrivialPPDirective()) {2367 Diag(StartLoc, diag::err_global_module_introducer_not_at_start)2368 << SourceRange(StartLoc, SemiLoc);2369 return nullptr;2370 }2371 if (MDK == Sema::ModuleDeclKind::Interface) {2372 Diag(StartLoc, diag::err_module_fragment_exported)2373 << /*global*/0 << FixItHint::CreateRemoval(StartLoc);2374 }2375 ImportState = Sema::ModuleImportState::GlobalFragment;2376 return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);2377 }2378 2379 // Parse a private-module-fragment, if present.2380 if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&2381 NextToken().is(tok::kw_private)) {2382 if (MDK == Sema::ModuleDeclKind::Interface) {2383 Diag(StartLoc, diag::err_module_fragment_exported)2384 << /*private*/1 << FixItHint::CreateRemoval(StartLoc);2385 }2386 ConsumeToken();2387 SourceLocation PrivateLoc = ConsumeToken();2388 DiagnoseAndSkipCXX11Attributes();2389 ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);2390 ImportState = ImportState == Sema::ModuleImportState::ImportAllowed2391 ? Sema::ModuleImportState::PrivateFragmentImportAllowed2392 : Sema::ModuleImportState::PrivateFragmentImportFinished;2393 return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);2394 }2395 2396 SmallVector<IdentifierLoc, 2> Path;2397 if (ParseModuleName(ModuleLoc, Path, /*IsImport*/ false))2398 return nullptr;2399 2400 // Parse the optional module-partition.2401 SmallVector<IdentifierLoc, 2> Partition;2402 if (Tok.is(tok::colon)) {2403 SourceLocation ColonLoc = ConsumeToken();2404 if (!getLangOpts().CPlusPlusModules)2405 Diag(ColonLoc, diag::err_unsupported_module_partition)2406 << SourceRange(ColonLoc, Partition.back().getLoc());2407 // Recover by ignoring the partition name.2408 else if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/ false))2409 return nullptr;2410 }2411 2412 // We don't support any module attributes yet; just parse them and diagnose.2413 ParsedAttributes Attrs(AttrFactory);2414 MaybeParseCXX11Attributes(Attrs);2415 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr,2416 diag::err_keyword_not_module_attr,2417 /*DiagnoseEmptyAttrs=*/false,2418 /*WarnOnUnknownAttrs=*/true);2419 2420 ExpectAndConsumeSemi(diag::err_module_expected_semi);2421 2422 return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, Partition,2423 ImportState,2424 Introducer.hasSeenNoTrivialPPDirective());2425}2426 2427Decl *Parser::ParseModuleImport(SourceLocation AtLoc,2428 Sema::ModuleImportState &ImportState) {2429 SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;2430 2431 SourceLocation ExportLoc;2432 TryConsumeToken(tok::kw_export, ExportLoc);2433 2434 assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)2435 : Tok.isObjCAtKeyword(tok::objc_import)) &&2436 "Improper start to module import");2437 bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);2438 SourceLocation ImportLoc = ConsumeToken();2439 2440 // For C++20 modules, we can have "name" or ":Partition name" as valid input.2441 SmallVector<IdentifierLoc, 2> Path;2442 bool IsPartition = false;2443 Module *HeaderUnit = nullptr;2444 if (Tok.is(tok::header_name)) {2445 // This is a header import that the preprocessor decided we should skip2446 // because it was malformed in some way. Parse and ignore it; it's already2447 // been diagnosed.2448 ConsumeToken();2449 } else if (Tok.is(tok::annot_header_unit)) {2450 // This is a header import that the preprocessor mapped to a module import.2451 HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());2452 ConsumeAnnotationToken();2453 } else if (Tok.is(tok::colon)) {2454 SourceLocation ColonLoc = ConsumeToken();2455 if (!getLangOpts().CPlusPlusModules)2456 Diag(ColonLoc, diag::err_unsupported_module_partition)2457 << SourceRange(ColonLoc, Path.back().getLoc());2458 // Recover by leaving partition empty.2459 else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true))2460 return nullptr;2461 else2462 IsPartition = true;2463 } else {2464 if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true))2465 return nullptr;2466 }2467 2468 ParsedAttributes Attrs(AttrFactory);2469 MaybeParseCXX11Attributes(Attrs);2470 // We don't support any module import attributes yet.2471 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr,2472 diag::err_keyword_not_import_attr,2473 /*DiagnoseEmptyAttrs=*/false,2474 /*WarnOnUnknownAttrs=*/true);2475 2476 if (PP.hadModuleLoaderFatalFailure()) {2477 // With a fatal failure in the module loader, we abort parsing.2478 cutOffParsing();2479 return nullptr;2480 }2481 2482 // Diagnose mis-imports.2483 bool SeenError = true;2484 switch (ImportState) {2485 case Sema::ModuleImportState::ImportAllowed:2486 SeenError = false;2487 break;2488 case Sema::ModuleImportState::FirstDecl:2489 // If we found an import decl as the first declaration, we must be not in2490 // a C++20 module unit or we are in an invalid state.2491 ImportState = Sema::ModuleImportState::NotACXX20Module;2492 [[fallthrough]];2493 case Sema::ModuleImportState::NotACXX20Module:2494 // We can only import a partition within a module purview.2495 if (IsPartition)2496 Diag(ImportLoc, diag::err_partition_import_outside_module);2497 else2498 SeenError = false;2499 break;2500 case Sema::ModuleImportState::GlobalFragment:2501 case Sema::ModuleImportState::PrivateFragmentImportAllowed:2502 // We can only have pre-processor directives in the global module fragment2503 // which allows pp-import, but not of a partition (since the global module2504 // does not have partitions).2505 // We cannot import a partition into a private module fragment, since2506 // [module.private.frag]/1 disallows private module fragments in a multi-2507 // TU module.2508 if (IsPartition || (HeaderUnit && HeaderUnit->Kind !=2509 Module::ModuleKind::ModuleHeaderUnit))2510 Diag(ImportLoc, diag::err_import_in_wrong_fragment)2511 << IsPartition2512 << (ImportState == Sema::ModuleImportState::GlobalFragment ? 0 : 1);2513 else2514 SeenError = false;2515 break;2516 case Sema::ModuleImportState::ImportFinished:2517 case Sema::ModuleImportState::PrivateFragmentImportFinished:2518 if (getLangOpts().CPlusPlusModules)2519 Diag(ImportLoc, diag::err_import_not_allowed_here);2520 else2521 SeenError = false;2522 break;2523 }2524 ExpectAndConsumeSemi(diag::err_module_expected_semi);2525 TryConsumeToken(tok::eod);2526 2527 if (SeenError)2528 return nullptr;2529 2530 DeclResult Import;2531 if (HeaderUnit)2532 Import =2533 Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);2534 else if (!Path.empty())2535 Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path,2536 IsPartition);2537 if (Import.isInvalid())2538 return nullptr;2539 2540 // Using '@import' in framework headers requires modules to be enabled so that2541 // the header is parseable. Emit a warning to make the user aware.2542 if (IsObjCAtImport && AtLoc.isValid()) {2543 auto &SrcMgr = PP.getSourceManager();2544 auto FE = SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(AtLoc));2545 if (FE && llvm::sys::path::parent_path(FE->getDir().getName())2546 .ends_with(".framework"))2547 Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);2548 }2549 2550 return Import.get();2551}2552 2553bool Parser::ParseModuleName(SourceLocation UseLoc,2554 SmallVectorImpl<IdentifierLoc> &Path,2555 bool IsImport) {2556 // Parse the module path.2557 while (true) {2558 if (!Tok.is(tok::identifier)) {2559 if (Tok.is(tok::code_completion)) {2560 cutOffParsing();2561 Actions.CodeCompletion().CodeCompleteModuleImport(UseLoc, Path);2562 return true;2563 }2564 2565 Diag(Tok, diag::err_module_expected_ident) << IsImport;2566 SkipUntil(tok::semi);2567 return true;2568 }2569 2570 // Record this part of the module path.2571 Path.emplace_back(Tok.getLocation(), Tok.getIdentifierInfo());2572 ConsumeToken();2573 2574 if (Tok.isNot(tok::period))2575 return false;2576 2577 ConsumeToken();2578 }2579}2580 2581bool Parser::parseMisplacedModuleImport() {2582 while (true) {2583 switch (Tok.getKind()) {2584 case tok::annot_module_end:2585 // If we recovered from a misplaced module begin, we expect to hit a2586 // misplaced module end too. Stay in the current context when this2587 // happens.2588 if (MisplacedModuleBeginCount) {2589 --MisplacedModuleBeginCount;2590 Actions.ActOnAnnotModuleEnd(2591 Tok.getLocation(),2592 reinterpret_cast<Module *>(Tok.getAnnotationValue()));2593 ConsumeAnnotationToken();2594 continue;2595 }2596 // Inform caller that recovery failed, the error must be handled at upper2597 // level. This will generate the desired "missing '}' at end of module"2598 // diagnostics on the way out.2599 return true;2600 case tok::annot_module_begin:2601 // Recover by entering the module (Sema will diagnose).2602 Actions.ActOnAnnotModuleBegin(2603 Tok.getLocation(),2604 reinterpret_cast<Module *>(Tok.getAnnotationValue()));2605 ConsumeAnnotationToken();2606 ++MisplacedModuleBeginCount;2607 continue;2608 case tok::annot_module_include:2609 // Module import found where it should not be, for instance, inside a2610 // namespace. Recover by importing the module.2611 Actions.ActOnAnnotModuleInclude(2612 Tok.getLocation(),2613 reinterpret_cast<Module *>(Tok.getAnnotationValue()));2614 ConsumeAnnotationToken();2615 // If there is another module import, process it.2616 continue;2617 default:2618 return false;2619 }2620 }2621 return false;2622}2623 2624void Parser::diagnoseUseOfC11Keyword(const Token &Tok) {2625 // Warn that this is a C11 extension if in an older mode or if in C++.2626 // Otherwise, warn that it is incompatible with standards before C11 if in2627 // C11 or later.2628 Diag(Tok, getLangOpts().C11 ? diag::warn_c11_compat_keyword2629 : diag::ext_c11_feature)2630 << Tok.getName();2631}2632 2633bool BalancedDelimiterTracker::diagnoseOverflow() {2634 P.Diag(P.Tok, diag::err_bracket_depth_exceeded)2635 << P.getLangOpts().BracketDepth;2636 P.Diag(P.Tok, diag::note_bracket_depth);2637 P.cutOffParsing();2638 return true;2639}2640 2641bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,2642 const char *Msg,2643 tok::TokenKind SkipToTok) {2644 LOpen = P.Tok.getLocation();2645 if (P.ExpectAndConsume(Kind, DiagID, Msg)) {2646 if (SkipToTok != tok::unknown)2647 P.SkipUntil(SkipToTok, Parser::StopAtSemi);2648 return true;2649 }2650 2651 if (getDepth() < P.getLangOpts().BracketDepth)2652 return false;2653 2654 return diagnoseOverflow();2655}2656 2657bool BalancedDelimiterTracker::diagnoseMissingClose() {2658 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");2659 2660 if (P.Tok.is(tok::annot_module_end))2661 P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;2662 else2663 P.Diag(P.Tok, diag::err_expected) << Close;2664 P.Diag(LOpen, diag::note_matching) << Kind;2665 2666 // If we're not already at some kind of closing bracket, skip to our closing2667 // token.2668 if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&2669 P.Tok.isNot(tok::r_square) &&2670 P.SkipUntil(Close, FinalToken,2671 Parser::StopAtSemi | Parser::StopBeforeMatch) &&2672 P.Tok.is(Close))2673 LClose = P.ConsumeAnyToken();2674 return true;2675}2676 2677void BalancedDelimiterTracker::skipToEnd() {2678 P.SkipUntil(Close, Parser::StopBeforeMatch);2679 consumeClose();2680}2681