10b57cec5SDimitry Andric //===--- Parser.cpp - C Language Family Parser ----------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Parser interfaces. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/Parse/Parser.h" 140b57cec5SDimitry Andric #include "clang/AST/ASTConsumer.h" 150b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 1606c3fb27SDimitry Andric #include "clang/AST/ASTLambda.h" 175f757f3fSDimitry Andric #include "clang/AST/DeclTemplate.h" 185ffd83dbSDimitry Andric #include "clang/Basic/FileManager.h" 190b57cec5SDimitry Andric #include "clang/Parse/ParseDiagnostic.h" 200b57cec5SDimitry Andric #include "clang/Parse/RAIIObjectsForParser.h" 210b57cec5SDimitry Andric #include "clang/Sema/DeclSpec.h" 220b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h" 230b57cec5SDimitry Andric #include "clang/Sema/Scope.h" 240b57cec5SDimitry Andric #include "llvm/Support/Path.h" 255f757f3fSDimitry Andric #include "llvm/Support/TimeProfiler.h" 260b57cec5SDimitry Andric using namespace clang; 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric namespace { 300b57cec5SDimitry Andric /// A comment handler that passes comments found by the preprocessor 310b57cec5SDimitry Andric /// to the parser action. 320b57cec5SDimitry Andric class ActionCommentHandler : public CommentHandler { 330b57cec5SDimitry Andric Sema &S; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric public: 360b57cec5SDimitry Andric explicit ActionCommentHandler(Sema &S) : S(S) { } 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric bool HandleComment(Preprocessor &PP, SourceRange Comment) override { 390b57cec5SDimitry Andric S.ActOnComment(Comment); 400b57cec5SDimitry Andric return false; 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric }; 430b57cec5SDimitry Andric } // end anonymous namespace 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric IdentifierInfo *Parser::getSEHExceptKeyword() { 460b57cec5SDimitry Andric // __except is accepted as a (contextual) keyword 470b57cec5SDimitry Andric if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland)) 480b57cec5SDimitry Andric Ident__except = PP.getIdentifierInfo("__except"); 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric return Ident__except; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies) 54fe6060f1SDimitry Andric : PP(pp), PreferredType(pp.isCodeCompletionEnabled()), Actions(actions), 55fe6060f1SDimitry Andric Diags(PP.getDiagnostics()), GreaterThanIsOperator(true), 56fe6060f1SDimitry Andric ColonIsSacred(false), InMessageExpression(false), 57fe6060f1SDimitry Andric TemplateParameterDepth(0), ParsingInObjCContainer(false) { 580b57cec5SDimitry Andric SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies; 590b57cec5SDimitry Andric Tok.startToken(); 600b57cec5SDimitry Andric Tok.setKind(tok::eof); 610b57cec5SDimitry Andric Actions.CurScope = nullptr; 620b57cec5SDimitry Andric NumCachedScopes = 0; 630b57cec5SDimitry Andric CurParsedObjCImpl = nullptr; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric // Add #pragma handlers. These are removed and destroyed in the 660b57cec5SDimitry Andric // destructor. 670b57cec5SDimitry Andric initializePragmaHandlers(); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric CommentSemaHandler.reset(new ActionCommentHandler(actions)); 700b57cec5SDimitry Andric PP.addCommentHandler(CommentSemaHandler.get()); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric PP.setCodeCompletionHandler(*this); 73*7a6dacacSDimitry Andric 74*7a6dacacSDimitry Andric Actions.ParseTypeFromStringCallback = 75*7a6dacacSDimitry Andric [this](StringRef TypeStr, StringRef Context, SourceLocation IncludeLoc) { 76*7a6dacacSDimitry Andric return this->ParseTypeFromString(TypeStr, Context, IncludeLoc); 77*7a6dacacSDimitry Andric }; 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 810b57cec5SDimitry Andric return Diags.Report(Loc, DiagID); 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 850b57cec5SDimitry Andric return Diag(Tok.getLocation(), DiagID); 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric /// Emits a diagnostic suggesting parentheses surrounding a 890b57cec5SDimitry Andric /// given range. 900b57cec5SDimitry Andric /// 910b57cec5SDimitry Andric /// \param Loc The location where we'll emit the diagnostic. 920b57cec5SDimitry Andric /// \param DK The kind of diagnostic to emit. 930b57cec5SDimitry Andric /// \param ParenRange Source range enclosing code that should be parenthesized. 940b57cec5SDimitry Andric void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 950b57cec5SDimitry Andric SourceRange ParenRange) { 960b57cec5SDimitry Andric SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 970b57cec5SDimitry Andric if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 980b57cec5SDimitry Andric // We can't display the parentheses, so just dig the 990b57cec5SDimitry Andric // warning/error and return. 1000b57cec5SDimitry Andric Diag(Loc, DK); 1010b57cec5SDimitry Andric return; 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric Diag(Loc, DK) 1050b57cec5SDimitry Andric << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 1060b57cec5SDimitry Andric << FixItHint::CreateInsertion(EndLoc, ")"); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) { 1100b57cec5SDimitry Andric switch (ExpectedTok) { 1110b57cec5SDimitry Andric case tok::semi: 1120b57cec5SDimitry Andric return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ; 1130b57cec5SDimitry Andric default: return false; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 1180b57cec5SDimitry Andric StringRef Msg) { 1190b57cec5SDimitry Andric if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 1200b57cec5SDimitry Andric ConsumeAnyToken(); 1210b57cec5SDimitry Andric return false; 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric // Detect common single-character typos and resume. 1250b57cec5SDimitry Andric if (IsCommonTypo(ExpectedTok, Tok)) { 1260b57cec5SDimitry Andric SourceLocation Loc = Tok.getLocation(); 1270b57cec5SDimitry Andric { 1280b57cec5SDimitry Andric DiagnosticBuilder DB = Diag(Loc, DiagID); 1290b57cec5SDimitry Andric DB << FixItHint::CreateReplacement( 1300b57cec5SDimitry Andric SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok)); 1310b57cec5SDimitry Andric if (DiagID == diag::err_expected) 1320b57cec5SDimitry Andric DB << ExpectedTok; 1330b57cec5SDimitry Andric else if (DiagID == diag::err_expected_after) 1340b57cec5SDimitry Andric DB << Msg << ExpectedTok; 1350b57cec5SDimitry Andric else 1360b57cec5SDimitry Andric DB << Msg; 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric // Pretend there wasn't a problem. 1400b57cec5SDimitry Andric ConsumeAnyToken(); 1410b57cec5SDimitry Andric return false; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 1450b57cec5SDimitry Andric const char *Spelling = nullptr; 1460b57cec5SDimitry Andric if (EndLoc.isValid()) 1470b57cec5SDimitry Andric Spelling = tok::getPunctuatorSpelling(ExpectedTok); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric DiagnosticBuilder DB = 1500b57cec5SDimitry Andric Spelling 1510b57cec5SDimitry Andric ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling) 1520b57cec5SDimitry Andric : Diag(Tok, DiagID); 1530b57cec5SDimitry Andric if (DiagID == diag::err_expected) 1540b57cec5SDimitry Andric DB << ExpectedTok; 1550b57cec5SDimitry Andric else if (DiagID == diag::err_expected_after) 1560b57cec5SDimitry Andric DB << Msg << ExpectedTok; 1570b57cec5SDimitry Andric else 1580b57cec5SDimitry Andric DB << Msg; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric return true; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 163972a253aSDimitry Andric bool Parser::ExpectAndConsumeSemi(unsigned DiagID, StringRef TokenUsed) { 1640b57cec5SDimitry Andric if (TryConsumeToken(tok::semi)) 1650b57cec5SDimitry Andric return false; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 1680b57cec5SDimitry Andric handleUnexpectedCodeCompletionToken(); 1690b57cec5SDimitry Andric return false; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 1730b57cec5SDimitry Andric NextToken().is(tok::semi)) { 1740b57cec5SDimitry Andric Diag(Tok, diag::err_extraneous_token_before_semi) 1750b57cec5SDimitry Andric << PP.getSpelling(Tok) 1760b57cec5SDimitry Andric << FixItHint::CreateRemoval(Tok.getLocation()); 1770b57cec5SDimitry Andric ConsumeAnyToken(); // The ')' or ']'. 1780b57cec5SDimitry Andric ConsumeToken(); // The ';'. 1790b57cec5SDimitry Andric return false; 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 182972a253aSDimitry Andric return ExpectAndConsume(tok::semi, DiagID , TokenUsed); 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric 185a7dea167SDimitry Andric void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) { 1860b57cec5SDimitry Andric if (!Tok.is(tok::semi)) return; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric bool HadMultipleSemis = false; 1890b57cec5SDimitry Andric SourceLocation StartLoc = Tok.getLocation(); 1900b57cec5SDimitry Andric SourceLocation EndLoc = Tok.getLocation(); 1910b57cec5SDimitry Andric ConsumeToken(); 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) { 1940b57cec5SDimitry Andric HadMultipleSemis = true; 1950b57cec5SDimitry Andric EndLoc = Tok.getLocation(); 1960b57cec5SDimitry Andric ConsumeToken(); 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // C++11 allows extra semicolons at namespace scope, but not in any of the 2000b57cec5SDimitry Andric // other contexts. 2010b57cec5SDimitry Andric if (Kind == OutsideFunction && getLangOpts().CPlusPlus) { 2020b57cec5SDimitry Andric if (getLangOpts().CPlusPlus11) 2030b57cec5SDimitry Andric Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi) 2040b57cec5SDimitry Andric << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2050b57cec5SDimitry Andric else 2060b57cec5SDimitry Andric Diag(StartLoc, diag::ext_extra_semi_cxx11) 2070b57cec5SDimitry Andric << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2080b57cec5SDimitry Andric return; 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis) 2120b57cec5SDimitry Andric Diag(StartLoc, diag::ext_extra_semi) 213a7dea167SDimitry Andric << Kind << DeclSpec::getSpecifierName(TST, 2140b57cec5SDimitry Andric Actions.getASTContext().getPrintingPolicy()) 2150b57cec5SDimitry Andric << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2160b57cec5SDimitry Andric else 2170b57cec5SDimitry Andric // A single semicolon is valid after a member function definition. 2180b57cec5SDimitry Andric Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def) 2190b57cec5SDimitry Andric << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric bool Parser::expectIdentifier() { 2230b57cec5SDimitry Andric if (Tok.is(tok::identifier)) 2240b57cec5SDimitry Andric return false; 2250b57cec5SDimitry Andric if (const auto *II = Tok.getIdentifierInfo()) { 2260b57cec5SDimitry Andric if (II->isCPlusPlusKeyword(getLangOpts())) { 2270b57cec5SDimitry Andric Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword) 2280b57cec5SDimitry Andric << tok::identifier << Tok.getIdentifierInfo(); 2290b57cec5SDimitry Andric // Objective-C++: Recover by treating this keyword as a valid identifier. 2300b57cec5SDimitry Andric return false; 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric Diag(Tok, diag::err_expected) << tok::identifier; 2340b57cec5SDimitry Andric return true; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 237e8d8bef9SDimitry Andric void Parser::checkCompoundToken(SourceLocation FirstTokLoc, 238e8d8bef9SDimitry Andric tok::TokenKind FirstTokKind, CompoundToken Op) { 239e8d8bef9SDimitry Andric if (FirstTokLoc.isInvalid()) 240e8d8bef9SDimitry Andric return; 241e8d8bef9SDimitry Andric SourceLocation SecondTokLoc = Tok.getLocation(); 242e8d8bef9SDimitry Andric 243e8d8bef9SDimitry Andric // If either token is in a macro, we expect both tokens to come from the same 244e8d8bef9SDimitry Andric // macro expansion. 245e8d8bef9SDimitry Andric if ((FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID()) && 246e8d8bef9SDimitry Andric PP.getSourceManager().getFileID(FirstTokLoc) != 247e8d8bef9SDimitry Andric PP.getSourceManager().getFileID(SecondTokLoc)) { 248e8d8bef9SDimitry Andric Diag(FirstTokLoc, diag::warn_compound_token_split_by_macro) 249e8d8bef9SDimitry Andric << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind() 250e8d8bef9SDimitry Andric << static_cast<int>(Op) << SourceRange(FirstTokLoc); 251e8d8bef9SDimitry Andric Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here) 252e8d8bef9SDimitry Andric << (FirstTokKind == Tok.getKind()) << Tok.getKind() 253e8d8bef9SDimitry Andric << SourceRange(SecondTokLoc); 254e8d8bef9SDimitry Andric return; 255e8d8bef9SDimitry Andric } 256e8d8bef9SDimitry Andric 257e8d8bef9SDimitry Andric // We expect the tokens to abut. 258e8d8bef9SDimitry Andric if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { 259e8d8bef9SDimitry Andric SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc); 260e8d8bef9SDimitry Andric if (SpaceLoc.isInvalid()) 261e8d8bef9SDimitry Andric SpaceLoc = FirstTokLoc; 262e8d8bef9SDimitry Andric Diag(SpaceLoc, diag::warn_compound_token_split_by_whitespace) 263e8d8bef9SDimitry Andric << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind() 264e8d8bef9SDimitry Andric << static_cast<int>(Op) << SourceRange(FirstTokLoc, SecondTokLoc); 265e8d8bef9SDimitry Andric return; 266e8d8bef9SDimitry Andric } 267e8d8bef9SDimitry Andric } 268e8d8bef9SDimitry Andric 2690b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2700b57cec5SDimitry Andric // Error recovery. 2710b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) { 2740b57cec5SDimitry Andric return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric /// SkipUntil - Read tokens until we get to the specified token, then consume 2780b57cec5SDimitry Andric /// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the 2790b57cec5SDimitry Andric /// token will ever occur, this skips to the next token, or to some likely 2800b57cec5SDimitry Andric /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' 2810b57cec5SDimitry Andric /// character. 2820b57cec5SDimitry Andric /// 2830b57cec5SDimitry Andric /// If SkipUntil finds the specified token, it returns true, otherwise it 2840b57cec5SDimitry Andric /// returns false. 2850b57cec5SDimitry Andric bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) { 2860b57cec5SDimitry Andric // We always want this function to skip at least one token if the first token 2870b57cec5SDimitry Andric // isn't T and if not at EOF. 2880b57cec5SDimitry Andric bool isFirstTokenSkipped = true; 28904eeddc0SDimitry Andric while (true) { 2900b57cec5SDimitry Andric // If we found one of the tokens, stop and return true. 2910b57cec5SDimitry Andric for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) { 2920b57cec5SDimitry Andric if (Tok.is(Toks[i])) { 2930b57cec5SDimitry Andric if (HasFlagsSet(Flags, StopBeforeMatch)) { 2940b57cec5SDimitry Andric // Noop, don't consume the token. 2950b57cec5SDimitry Andric } else { 2960b57cec5SDimitry Andric ConsumeAnyToken(); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric return true; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric // Important special case: The caller has given up and just wants us to 3030b57cec5SDimitry Andric // skip the rest of the file. Do this without recursing, since we can 3040b57cec5SDimitry Andric // get here precisely because the caller detected too much recursion. 3050b57cec5SDimitry Andric if (Toks.size() == 1 && Toks[0] == tok::eof && 3060b57cec5SDimitry Andric !HasFlagsSet(Flags, StopAtSemi) && 3070b57cec5SDimitry Andric !HasFlagsSet(Flags, StopAtCodeCompletion)) { 3080b57cec5SDimitry Andric while (Tok.isNot(tok::eof)) 3090b57cec5SDimitry Andric ConsumeAnyToken(); 3100b57cec5SDimitry Andric return true; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric switch (Tok.getKind()) { 3140b57cec5SDimitry Andric case tok::eof: 3150b57cec5SDimitry Andric // Ran out of tokens. 3160b57cec5SDimitry Andric return false; 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric case tok::annot_pragma_openmp: 319fe6060f1SDimitry Andric case tok::annot_attr_openmp: 3200b57cec5SDimitry Andric case tok::annot_pragma_openmp_end: 3210b57cec5SDimitry Andric // Stop before an OpenMP pragma boundary. 322480093f4SDimitry Andric if (OpenMPDirectiveParsing) 323480093f4SDimitry Andric return false; 324480093f4SDimitry Andric ConsumeAnnotationToken(); 325480093f4SDimitry Andric break; 3265f757f3fSDimitry Andric case tok::annot_pragma_openacc: 3275f757f3fSDimitry Andric case tok::annot_pragma_openacc_end: 3285f757f3fSDimitry Andric // Stop before an OpenACC pragma boundary. 3295f757f3fSDimitry Andric if (OpenACCDirectiveParsing) 3305f757f3fSDimitry Andric return false; 3315f757f3fSDimitry Andric ConsumeAnnotationToken(); 3325f757f3fSDimitry Andric break; 3330b57cec5SDimitry Andric case tok::annot_module_begin: 3340b57cec5SDimitry Andric case tok::annot_module_end: 3350b57cec5SDimitry Andric case tok::annot_module_include: 33606c3fb27SDimitry Andric case tok::annot_repl_input_end: 3370b57cec5SDimitry Andric // Stop before we change submodules. They generally indicate a "good" 3380b57cec5SDimitry Andric // place to pick up parsing again (except in the special case where 3390b57cec5SDimitry Andric // we're trying to skip to EOF). 3400b57cec5SDimitry Andric return false; 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric case tok::code_completion: 3430b57cec5SDimitry Andric if (!HasFlagsSet(Flags, StopAtCodeCompletion)) 3440b57cec5SDimitry Andric handleUnexpectedCodeCompletionToken(); 3450b57cec5SDimitry Andric return false; 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric case tok::l_paren: 3480b57cec5SDimitry Andric // Recursively skip properly-nested parens. 3490b57cec5SDimitry Andric ConsumeParen(); 3500b57cec5SDimitry Andric if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3510b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtCodeCompletion); 3520b57cec5SDimitry Andric else 3530b57cec5SDimitry Andric SkipUntil(tok::r_paren); 3540b57cec5SDimitry Andric break; 3550b57cec5SDimitry Andric case tok::l_square: 3560b57cec5SDimitry Andric // Recursively skip properly-nested square brackets. 3570b57cec5SDimitry Andric ConsumeBracket(); 3580b57cec5SDimitry Andric if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3590b57cec5SDimitry Andric SkipUntil(tok::r_square, StopAtCodeCompletion); 3600b57cec5SDimitry Andric else 3610b57cec5SDimitry Andric SkipUntil(tok::r_square); 3620b57cec5SDimitry Andric break; 3630b57cec5SDimitry Andric case tok::l_brace: 3640b57cec5SDimitry Andric // Recursively skip properly-nested braces. 3650b57cec5SDimitry Andric ConsumeBrace(); 3660b57cec5SDimitry Andric if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3670b57cec5SDimitry Andric SkipUntil(tok::r_brace, StopAtCodeCompletion); 3680b57cec5SDimitry Andric else 3690b57cec5SDimitry Andric SkipUntil(tok::r_brace); 3700b57cec5SDimitry Andric break; 3710b57cec5SDimitry Andric case tok::question: 3720b57cec5SDimitry Andric // Recursively skip ? ... : pairs; these function as brackets. But 3730b57cec5SDimitry Andric // still stop at a semicolon if requested. 3740b57cec5SDimitry Andric ConsumeToken(); 3750b57cec5SDimitry Andric SkipUntil(tok::colon, 3760b57cec5SDimitry Andric SkipUntilFlags(unsigned(Flags) & 3770b57cec5SDimitry Andric unsigned(StopAtCodeCompletion | StopAtSemi))); 3780b57cec5SDimitry Andric break; 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // Okay, we found a ']' or '}' or ')', which we think should be balanced. 3810b57cec5SDimitry Andric // Since the user wasn't looking for this token (if they were, it would 3820b57cec5SDimitry Andric // already be handled), this isn't balanced. If there is a LHS token at a 3830b57cec5SDimitry Andric // higher level, we will assume that this matches the unbalanced token 3840b57cec5SDimitry Andric // and return it. Otherwise, this is a spurious RHS token, which we skip. 3850b57cec5SDimitry Andric case tok::r_paren: 3860b57cec5SDimitry Andric if (ParenCount && !isFirstTokenSkipped) 3870b57cec5SDimitry Andric return false; // Matches something. 3880b57cec5SDimitry Andric ConsumeParen(); 3890b57cec5SDimitry Andric break; 3900b57cec5SDimitry Andric case tok::r_square: 3910b57cec5SDimitry Andric if (BracketCount && !isFirstTokenSkipped) 3920b57cec5SDimitry Andric return false; // Matches something. 3930b57cec5SDimitry Andric ConsumeBracket(); 3940b57cec5SDimitry Andric break; 3950b57cec5SDimitry Andric case tok::r_brace: 3960b57cec5SDimitry Andric if (BraceCount && !isFirstTokenSkipped) 3970b57cec5SDimitry Andric return false; // Matches something. 3980b57cec5SDimitry Andric ConsumeBrace(); 3990b57cec5SDimitry Andric break; 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric case tok::semi: 4020b57cec5SDimitry Andric if (HasFlagsSet(Flags, StopAtSemi)) 4030b57cec5SDimitry Andric return false; 404bdd1243dSDimitry Andric [[fallthrough]]; 4050b57cec5SDimitry Andric default: 4060b57cec5SDimitry Andric // Skip this token. 4070b57cec5SDimitry Andric ConsumeAnyToken(); 4080b57cec5SDimitry Andric break; 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric isFirstTokenSkipped = false; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4150b57cec5SDimitry Andric // Scope manipulation 4160b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric /// EnterScope - Start a new scope. 4190b57cec5SDimitry Andric void Parser::EnterScope(unsigned ScopeFlags) { 4200b57cec5SDimitry Andric if (NumCachedScopes) { 4210b57cec5SDimitry Andric Scope *N = ScopeCache[--NumCachedScopes]; 4220b57cec5SDimitry Andric N->Init(getCurScope(), ScopeFlags); 4230b57cec5SDimitry Andric Actions.CurScope = N; 4240b57cec5SDimitry Andric } else { 4250b57cec5SDimitry Andric Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags); 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric /// ExitScope - Pop a scope off the scope stack. 4300b57cec5SDimitry Andric void Parser::ExitScope() { 4310b57cec5SDimitry Andric assert(getCurScope() && "Scope imbalance!"); 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric // Inform the actions module that this scope is going away if there are any 4340b57cec5SDimitry Andric // decls in it. 4350b57cec5SDimitry Andric Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric Scope *OldScope = getCurScope(); 4380b57cec5SDimitry Andric Actions.CurScope = OldScope->getParent(); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric if (NumCachedScopes == ScopeCacheSize) 4410b57cec5SDimitry Andric delete OldScope; 4420b57cec5SDimitry Andric else 4430b57cec5SDimitry Andric ScopeCache[NumCachedScopes++] = OldScope; 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false, 4470b57cec5SDimitry Andric /// this object does nothing. 4480b57cec5SDimitry Andric Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags, 4490b57cec5SDimitry Andric bool ManageFlags) 4500b57cec5SDimitry Andric : CurScope(ManageFlags ? Self->getCurScope() : nullptr) { 4510b57cec5SDimitry Andric if (CurScope) { 4520b57cec5SDimitry Andric OldFlags = CurScope->getFlags(); 4530b57cec5SDimitry Andric CurScope->setFlags(ScopeFlags); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric /// Restore the flags for the current scope to what they were before this 4580b57cec5SDimitry Andric /// object overrode them. 4590b57cec5SDimitry Andric Parser::ParseScopeFlags::~ParseScopeFlags() { 4600b57cec5SDimitry Andric if (CurScope) 4610b57cec5SDimitry Andric CurScope->setFlags(OldFlags); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4660b57cec5SDimitry Andric // C99 6.9: External Definitions. 4670b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric Parser::~Parser() { 4700b57cec5SDimitry Andric // If we still have scopes active, delete the scope tree. 4710b57cec5SDimitry Andric delete getCurScope(); 4720b57cec5SDimitry Andric Actions.CurScope = nullptr; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric // Free the scope cache. 4750b57cec5SDimitry Andric for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 4760b57cec5SDimitry Andric delete ScopeCache[i]; 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric resetPragmaHandlers(); 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric PP.removeCommentHandler(CommentSemaHandler.get()); 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric PP.clearCodeCompletionHandler(); 4830b57cec5SDimitry Andric 4845ffd83dbSDimitry Andric DestroyTemplateIds(); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric /// Initialize - Warm up the parser. 4880b57cec5SDimitry Andric /// 4890b57cec5SDimitry Andric void Parser::Initialize() { 4900b57cec5SDimitry Andric // Create the translation unit scope. Install it as the current scope. 4910b57cec5SDimitry Andric assert(getCurScope() == nullptr && "A scope is already active?"); 4920b57cec5SDimitry Andric EnterScope(Scope::DeclScope); 4930b57cec5SDimitry Andric Actions.ActOnTranslationUnitScope(getCurScope()); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric // Initialization for Objective-C context sensitive keywords recognition. 4960b57cec5SDimitry Andric // Referenced in Parser::ParseObjCTypeQualifierList. 4970b57cec5SDimitry Andric if (getLangOpts().ObjC) { 4980b57cec5SDimitry Andric ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); 4990b57cec5SDimitry Andric ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); 5000b57cec5SDimitry Andric ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); 5010b57cec5SDimitry Andric ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); 5020b57cec5SDimitry Andric ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); 5030b57cec5SDimitry Andric ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); 5040b57cec5SDimitry Andric ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull"); 5050b57cec5SDimitry Andric ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable"); 5060b57cec5SDimitry Andric ObjCTypeQuals[objc_null_unspecified] 5070b57cec5SDimitry Andric = &PP.getIdentifierTable().get("null_unspecified"); 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric Ident_instancetype = nullptr; 5110b57cec5SDimitry Andric Ident_final = nullptr; 5120b57cec5SDimitry Andric Ident_sealed = nullptr; 513fe6060f1SDimitry Andric Ident_abstract = nullptr; 5140b57cec5SDimitry Andric Ident_override = nullptr; 5150b57cec5SDimitry Andric Ident_GNU_final = nullptr; 5160b57cec5SDimitry Andric Ident_import = nullptr; 5170b57cec5SDimitry Andric Ident_module = nullptr; 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric Ident_super = &PP.getIdentifierTable().get("super"); 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric Ident_vector = nullptr; 5220b57cec5SDimitry Andric Ident_bool = nullptr; 523fe6060f1SDimitry Andric Ident_Bool = nullptr; 5240b57cec5SDimitry Andric Ident_pixel = nullptr; 5250b57cec5SDimitry Andric if (getLangOpts().AltiVec || getLangOpts().ZVector) { 5260b57cec5SDimitry Andric Ident_vector = &PP.getIdentifierTable().get("vector"); 5270b57cec5SDimitry Andric Ident_bool = &PP.getIdentifierTable().get("bool"); 528fe6060f1SDimitry Andric Ident_Bool = &PP.getIdentifierTable().get("_Bool"); 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric if (getLangOpts().AltiVec) 5310b57cec5SDimitry Andric Ident_pixel = &PP.getIdentifierTable().get("pixel"); 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric Ident_introduced = nullptr; 5340b57cec5SDimitry Andric Ident_deprecated = nullptr; 5350b57cec5SDimitry Andric Ident_obsoleted = nullptr; 5360b57cec5SDimitry Andric Ident_unavailable = nullptr; 5370b57cec5SDimitry Andric Ident_strict = nullptr; 5380b57cec5SDimitry Andric Ident_replacement = nullptr; 5390b57cec5SDimitry Andric 54006c3fb27SDimitry Andric Ident_language = Ident_defined_in = Ident_generated_declaration = Ident_USR = 54106c3fb27SDimitry Andric nullptr; 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric Ident__except = nullptr; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric Ident__exception_code = Ident__exception_info = nullptr; 5460b57cec5SDimitry Andric Ident__abnormal_termination = Ident___exception_code = nullptr; 5470b57cec5SDimitry Andric Ident___exception_info = Ident___abnormal_termination = nullptr; 5480b57cec5SDimitry Andric Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr; 5490b57cec5SDimitry Andric Ident_AbnormalTermination = nullptr; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric if(getLangOpts().Borland) { 5520b57cec5SDimitry Andric Ident__exception_info = PP.getIdentifierInfo("_exception_info"); 5530b57cec5SDimitry Andric Ident___exception_info = PP.getIdentifierInfo("__exception_info"); 5540b57cec5SDimitry Andric Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation"); 5550b57cec5SDimitry Andric Ident__exception_code = PP.getIdentifierInfo("_exception_code"); 5560b57cec5SDimitry Andric Ident___exception_code = PP.getIdentifierInfo("__exception_code"); 5570b57cec5SDimitry Andric Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); 5580b57cec5SDimitry Andric Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); 5590b57cec5SDimitry Andric Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); 5600b57cec5SDimitry Andric Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); 5630b57cec5SDimitry Andric PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); 5640b57cec5SDimitry Andric PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block); 5650b57cec5SDimitry Andric PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); 5660b57cec5SDimitry Andric PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); 5670b57cec5SDimitry Andric PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); 5680b57cec5SDimitry Andric PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); 5690b57cec5SDimitry Andric PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); 5700b57cec5SDimitry Andric PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric if (getLangOpts().CPlusPlusModules) { 5740b57cec5SDimitry Andric Ident_import = PP.getIdentifierInfo("import"); 5750b57cec5SDimitry Andric Ident_module = PP.getIdentifierInfo("module"); 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric Actions.Initialize(); 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric // Prime the lexer look-ahead. 5810b57cec5SDimitry Andric ConsumeToken(); 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5845ffd83dbSDimitry Andric void Parser::DestroyTemplateIds() { 5855ffd83dbSDimitry Andric for (TemplateIdAnnotation *Id : TemplateIds) 5865ffd83dbSDimitry Andric Id->Destroy(); 5875ffd83dbSDimitry Andric TemplateIds.clear(); 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric /// Parse the first top-level declaration in a translation unit. 5910b57cec5SDimitry Andric /// 5920b57cec5SDimitry Andric /// translation-unit: 5930b57cec5SDimitry Andric /// [C] external-declaration 5940b57cec5SDimitry Andric /// [C] translation-unit external-declaration 5950b57cec5SDimitry Andric /// [C++] top-level-declaration-seq[opt] 5960b57cec5SDimitry Andric /// [C++20] global-module-fragment[opt] module-declaration 5970b57cec5SDimitry Andric /// top-level-declaration-seq[opt] private-module-fragment[opt] 5980b57cec5SDimitry Andric /// 5990b57cec5SDimitry Andric /// Note that in C, it is an error if there is no first declaration. 60081ad6265SDimitry Andric bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result, 60181ad6265SDimitry Andric Sema::ModuleImportState &ImportState) { 6020b57cec5SDimitry Andric Actions.ActOnStartOfTranslationUnit(); 6030b57cec5SDimitry Andric 60481ad6265SDimitry Andric // For C++20 modules, a module decl must be the first in the TU. We also 60581ad6265SDimitry Andric // need to track module imports. 60681ad6265SDimitry Andric ImportState = Sema::ModuleImportState::FirstDecl; 60781ad6265SDimitry Andric bool NoTopLevelDecls = ParseTopLevelDecl(Result, ImportState); 60881ad6265SDimitry Andric 6090b57cec5SDimitry Andric // C11 6.9p1 says translation units must have at least one top-level 6100b57cec5SDimitry Andric // declaration. C++ doesn't have this restriction. We also don't want to 6110b57cec5SDimitry Andric // complain if we have a precompiled header, although technically if the PCH 6120b57cec5SDimitry Andric // is empty we should still emit the (pedantic) diagnostic. 613e8d8bef9SDimitry Andric // If the main file is a header, we're only pretending it's a TU; don't warn. 6140b57cec5SDimitry Andric if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() && 615e8d8bef9SDimitry Andric !getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile) 6160b57cec5SDimitry Andric Diag(diag::ext_empty_translation_unit); 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric return NoTopLevelDecls; 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the 6220b57cec5SDimitry Andric /// action tells us to. This returns true if the EOF was encountered. 6230b57cec5SDimitry Andric /// 6240b57cec5SDimitry Andric /// top-level-declaration: 6250b57cec5SDimitry Andric /// declaration 6260b57cec5SDimitry Andric /// [C++20] module-import-declaration 62781ad6265SDimitry Andric bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, 62881ad6265SDimitry Andric Sema::ModuleImportState &ImportState) { 6295ffd83dbSDimitry Andric DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); 6300b57cec5SDimitry Andric 6315f757f3fSDimitry Andric // Skip over the EOF token, flagging end of previous input for incremental 6325f757f3fSDimitry Andric // processing 6335f757f3fSDimitry Andric if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof)) 6345f757f3fSDimitry Andric ConsumeToken(); 6355f757f3fSDimitry Andric 6360b57cec5SDimitry Andric Result = nullptr; 6370b57cec5SDimitry Andric switch (Tok.getKind()) { 6380b57cec5SDimitry Andric case tok::annot_pragma_unused: 6390b57cec5SDimitry Andric HandlePragmaUnused(); 6400b57cec5SDimitry Andric return false; 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric case tok::kw_export: 6430b57cec5SDimitry Andric switch (NextToken().getKind()) { 6440b57cec5SDimitry Andric case tok::kw_module: 6450b57cec5SDimitry Andric goto module_decl; 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric // Note: no need to handle kw_import here. We only form kw_import under 64806c3fb27SDimitry Andric // the Standard C++ Modules, and in that case 'export import' is parsed as 64906c3fb27SDimitry Andric // an export-declaration containing an import-declaration. 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric // Recognize context-sensitive C++20 'export module' and 'export import' 6520b57cec5SDimitry Andric // declarations. 6530b57cec5SDimitry Andric case tok::identifier: { 6540b57cec5SDimitry Andric IdentifierInfo *II = NextToken().getIdentifierInfo(); 6550b57cec5SDimitry Andric if ((II == Ident_module || II == Ident_import) && 6560b57cec5SDimitry Andric GetLookAheadToken(2).isNot(tok::coloncolon)) { 6570b57cec5SDimitry Andric if (II == Ident_module) 6580b57cec5SDimitry Andric goto module_decl; 6590b57cec5SDimitry Andric else 6600b57cec5SDimitry Andric goto import_decl; 6610b57cec5SDimitry Andric } 6620b57cec5SDimitry Andric break; 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric default: 6660b57cec5SDimitry Andric break; 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric break; 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric case tok::kw_module: 6710b57cec5SDimitry Andric module_decl: 67281ad6265SDimitry Andric Result = ParseModuleDecl(ImportState); 6730b57cec5SDimitry Andric return false; 6740b57cec5SDimitry Andric 67581ad6265SDimitry Andric case tok::kw_import: 6760b57cec5SDimitry Andric import_decl: { 67781ad6265SDimitry Andric Decl *ImportDecl = ParseModuleImport(SourceLocation(), ImportState); 6780b57cec5SDimitry Andric Result = Actions.ConvertDeclToDeclGroup(ImportDecl); 6790b57cec5SDimitry Andric return false; 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 682753f127fSDimitry Andric case tok::annot_module_include: { 683753f127fSDimitry Andric auto Loc = Tok.getLocation(); 684753f127fSDimitry Andric Module *Mod = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 685753f127fSDimitry Andric // FIXME: We need a better way to disambiguate C++ clang modules and 686753f127fSDimitry Andric // standard C++ modules. 687753f127fSDimitry Andric if (!getLangOpts().CPlusPlusModules || !Mod->isHeaderUnit()) 688753f127fSDimitry Andric Actions.ActOnModuleInclude(Loc, Mod); 689753f127fSDimitry Andric else { 690753f127fSDimitry Andric DeclResult Import = 691753f127fSDimitry Andric Actions.ActOnModuleImport(Loc, SourceLocation(), Loc, Mod); 692753f127fSDimitry Andric Decl *ImportDecl = Import.isInvalid() ? nullptr : Import.get(); 693753f127fSDimitry Andric Result = Actions.ConvertDeclToDeclGroup(ImportDecl); 694753f127fSDimitry Andric } 6950b57cec5SDimitry Andric ConsumeAnnotationToken(); 6960b57cec5SDimitry Andric return false; 697753f127fSDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric case tok::annot_module_begin: 7000b57cec5SDimitry Andric Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>( 7010b57cec5SDimitry Andric Tok.getAnnotationValue())); 7020b57cec5SDimitry Andric ConsumeAnnotationToken(); 70381ad6265SDimitry Andric ImportState = Sema::ModuleImportState::NotACXX20Module; 7040b57cec5SDimitry Andric return false; 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric case tok::annot_module_end: 7070b57cec5SDimitry Andric Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>( 7080b57cec5SDimitry Andric Tok.getAnnotationValue())); 7090b57cec5SDimitry Andric ConsumeAnnotationToken(); 71081ad6265SDimitry Andric ImportState = Sema::ModuleImportState::NotACXX20Module; 7110b57cec5SDimitry Andric return false; 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric case tok::eof: 71406c3fb27SDimitry Andric case tok::annot_repl_input_end: 7155ffd83dbSDimitry Andric // Check whether -fmax-tokens= was reached. 7165ffd83dbSDimitry Andric if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) { 7175ffd83dbSDimitry Andric PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total) 7185ffd83dbSDimitry Andric << PP.getTokenCount() << PP.getMaxTokens(); 7195ffd83dbSDimitry Andric SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc(); 7205ffd83dbSDimitry Andric if (OverrideLoc.isValid()) { 7215ffd83dbSDimitry Andric PP.Diag(OverrideLoc, diag::note_max_tokens_total_override); 7225ffd83dbSDimitry Andric } 7235ffd83dbSDimitry Andric } 7245ffd83dbSDimitry Andric 7250b57cec5SDimitry Andric // Late template parsing can begin. 7265ffd83dbSDimitry Andric Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr, this); 7270b57cec5SDimitry Andric Actions.ActOnEndOfTranslationUnit(); 7280b57cec5SDimitry Andric //else don't tell Sema that we ended parsing: more input might come. 7290b57cec5SDimitry Andric return true; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric case tok::identifier: 7320b57cec5SDimitry Andric // C++2a [basic.link]p3: 7330b57cec5SDimitry Andric // A token sequence beginning with 'export[opt] module' or 7340b57cec5SDimitry Andric // 'export[opt] import' and not immediately followed by '::' 7350b57cec5SDimitry Andric // is never interpreted as the declaration of a top-level-declaration. 7360b57cec5SDimitry Andric if ((Tok.getIdentifierInfo() == Ident_module || 7370b57cec5SDimitry Andric Tok.getIdentifierInfo() == Ident_import) && 7380b57cec5SDimitry Andric NextToken().isNot(tok::coloncolon)) { 7390b57cec5SDimitry Andric if (Tok.getIdentifierInfo() == Ident_module) 7400b57cec5SDimitry Andric goto module_decl; 7410b57cec5SDimitry Andric else 7420b57cec5SDimitry Andric goto import_decl; 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric break; 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric default: 7470b57cec5SDimitry Andric break; 7480b57cec5SDimitry Andric } 7490b57cec5SDimitry Andric 750bdd1243dSDimitry Andric ParsedAttributes DeclAttrs(AttrFactory); 751bdd1243dSDimitry Andric ParsedAttributes DeclSpecAttrs(AttrFactory); 752bdd1243dSDimitry Andric // GNU attributes are applied to the declaration specification while the 753bdd1243dSDimitry Andric // standard attributes are applied to the declaration. We parse the two 754bdd1243dSDimitry Andric // attribute sets into different containters so we can apply them during 755bdd1243dSDimitry Andric // the regular parsing process. 756bdd1243dSDimitry Andric while (MaybeParseCXX11Attributes(DeclAttrs) || 757bdd1243dSDimitry Andric MaybeParseGNUAttributes(DeclSpecAttrs)) 758bdd1243dSDimitry Andric ; 7590b57cec5SDimitry Andric 760bdd1243dSDimitry Andric Result = ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs); 76181ad6265SDimitry Andric // An empty Result might mean a line with ';' or some parsing error, ignore 76281ad6265SDimitry Andric // it. 76381ad6265SDimitry Andric if (Result) { 76481ad6265SDimitry Andric if (ImportState == Sema::ModuleImportState::FirstDecl) 76581ad6265SDimitry Andric // First decl was not modular. 76681ad6265SDimitry Andric ImportState = Sema::ModuleImportState::NotACXX20Module; 76781ad6265SDimitry Andric else if (ImportState == Sema::ModuleImportState::ImportAllowed) 76881ad6265SDimitry Andric // Non-imports disallow further imports. 76981ad6265SDimitry Andric ImportState = Sema::ModuleImportState::ImportFinished; 770bdd1243dSDimitry Andric else if (ImportState == 771bdd1243dSDimitry Andric Sema::ModuleImportState::PrivateFragmentImportAllowed) 772bdd1243dSDimitry Andric // Non-imports disallow further imports. 773bdd1243dSDimitry Andric ImportState = Sema::ModuleImportState::PrivateFragmentImportFinished; 77481ad6265SDimitry Andric } 7750b57cec5SDimitry Andric return false; 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric /// ParseExternalDeclaration: 7790b57cec5SDimitry Andric /// 78081ad6265SDimitry Andric /// The `Attrs` that are passed in are C++11 attributes and appertain to the 78181ad6265SDimitry Andric /// declaration. 78281ad6265SDimitry Andric /// 7830b57cec5SDimitry Andric /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] 7840b57cec5SDimitry Andric /// function-definition 7850b57cec5SDimitry Andric /// declaration 7860b57cec5SDimitry Andric /// [GNU] asm-definition 7870b57cec5SDimitry Andric /// [GNU] __extension__ external-declaration 7880b57cec5SDimitry Andric /// [OBJC] objc-class-definition 7890b57cec5SDimitry Andric /// [OBJC] objc-class-declaration 7900b57cec5SDimitry Andric /// [OBJC] objc-alias-declaration 7910b57cec5SDimitry Andric /// [OBJC] objc-protocol-definition 7920b57cec5SDimitry Andric /// [OBJC] objc-method-definition 7930b57cec5SDimitry Andric /// [OBJC] @end 7940b57cec5SDimitry Andric /// [C++] linkage-specification 7950b57cec5SDimitry Andric /// [GNU] asm-definition: 7960b57cec5SDimitry Andric /// simple-asm-expr ';' 7970b57cec5SDimitry Andric /// [C++11] empty-declaration 7980b57cec5SDimitry Andric /// [C++11] attribute-declaration 7990b57cec5SDimitry Andric /// 8000b57cec5SDimitry Andric /// [C++11] empty-declaration: 8010b57cec5SDimitry Andric /// ';' 8020b57cec5SDimitry Andric /// 8030b57cec5SDimitry Andric /// [C++0x/GNU] 'extern' 'template' declaration 8040b57cec5SDimitry Andric /// 80506c3fb27SDimitry Andric /// [C++20] module-import-declaration 8060b57cec5SDimitry Andric /// 807bdd1243dSDimitry Andric Parser::DeclGroupPtrTy 808bdd1243dSDimitry Andric Parser::ParseExternalDeclaration(ParsedAttributes &Attrs, 809bdd1243dSDimitry Andric ParsedAttributes &DeclSpecAttrs, 8100b57cec5SDimitry Andric ParsingDeclSpec *DS) { 8115ffd83dbSDimitry Andric DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); 8120b57cec5SDimitry Andric ParenBraceBracketBalancer BalancerRAIIObj(*this); 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric if (PP.isCodeCompletionReached()) { 8150b57cec5SDimitry Andric cutOffParsing(); 8160b57cec5SDimitry Andric return nullptr; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric Decl *SingleDecl = nullptr; 8200b57cec5SDimitry Andric switch (Tok.getKind()) { 8210b57cec5SDimitry Andric case tok::annot_pragma_vis: 8220b57cec5SDimitry Andric HandlePragmaVisibility(); 8230b57cec5SDimitry Andric return nullptr; 8240b57cec5SDimitry Andric case tok::annot_pragma_pack: 8250b57cec5SDimitry Andric HandlePragmaPack(); 8260b57cec5SDimitry Andric return nullptr; 8270b57cec5SDimitry Andric case tok::annot_pragma_msstruct: 8280b57cec5SDimitry Andric HandlePragmaMSStruct(); 8290b57cec5SDimitry Andric return nullptr; 8300b57cec5SDimitry Andric case tok::annot_pragma_align: 8310b57cec5SDimitry Andric HandlePragmaAlign(); 8320b57cec5SDimitry Andric return nullptr; 8330b57cec5SDimitry Andric case tok::annot_pragma_weak: 8340b57cec5SDimitry Andric HandlePragmaWeak(); 8350b57cec5SDimitry Andric return nullptr; 8360b57cec5SDimitry Andric case tok::annot_pragma_weakalias: 8370b57cec5SDimitry Andric HandlePragmaWeakAlias(); 8380b57cec5SDimitry Andric return nullptr; 8390b57cec5SDimitry Andric case tok::annot_pragma_redefine_extname: 8400b57cec5SDimitry Andric HandlePragmaRedefineExtname(); 8410b57cec5SDimitry Andric return nullptr; 8420b57cec5SDimitry Andric case tok::annot_pragma_fp_contract: 8430b57cec5SDimitry Andric HandlePragmaFPContract(); 8440b57cec5SDimitry Andric return nullptr; 8450b57cec5SDimitry Andric case tok::annot_pragma_fenv_access: 846349cc55cSDimitry Andric case tok::annot_pragma_fenv_access_ms: 8470b57cec5SDimitry Andric HandlePragmaFEnvAccess(); 8480b57cec5SDimitry Andric return nullptr; 849e8d8bef9SDimitry Andric case tok::annot_pragma_fenv_round: 850e8d8bef9SDimitry Andric HandlePragmaFEnvRound(); 851e8d8bef9SDimitry Andric return nullptr; 8525f757f3fSDimitry Andric case tok::annot_pragma_cx_limited_range: 8535f757f3fSDimitry Andric HandlePragmaCXLimitedRange(); 8545f757f3fSDimitry Andric return nullptr; 8555ffd83dbSDimitry Andric case tok::annot_pragma_float_control: 8565ffd83dbSDimitry Andric HandlePragmaFloatControl(); 8575ffd83dbSDimitry Andric return nullptr; 8580b57cec5SDimitry Andric case tok::annot_pragma_fp: 8590b57cec5SDimitry Andric HandlePragmaFP(); 8600b57cec5SDimitry Andric break; 8610b57cec5SDimitry Andric case tok::annot_pragma_opencl_extension: 8620b57cec5SDimitry Andric HandlePragmaOpenCLExtension(); 8630b57cec5SDimitry Andric return nullptr; 864fe6060f1SDimitry Andric case tok::annot_attr_openmp: 8650b57cec5SDimitry Andric case tok::annot_pragma_openmp: { 8660b57cec5SDimitry Andric AccessSpecifier AS = AS_none; 86781ad6265SDimitry Andric return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); 8680b57cec5SDimitry Andric } 8695f757f3fSDimitry Andric case tok::annot_pragma_openacc: 8705f757f3fSDimitry Andric return ParseOpenACCDirectiveDecl(); 8710b57cec5SDimitry Andric case tok::annot_pragma_ms_pointers_to_members: 8720b57cec5SDimitry Andric HandlePragmaMSPointersToMembers(); 8730b57cec5SDimitry Andric return nullptr; 8740b57cec5SDimitry Andric case tok::annot_pragma_ms_vtordisp: 8750b57cec5SDimitry Andric HandlePragmaMSVtorDisp(); 8760b57cec5SDimitry Andric return nullptr; 8770b57cec5SDimitry Andric case tok::annot_pragma_ms_pragma: 8780b57cec5SDimitry Andric HandlePragmaMSPragma(); 8790b57cec5SDimitry Andric return nullptr; 8800b57cec5SDimitry Andric case tok::annot_pragma_dump: 8810b57cec5SDimitry Andric HandlePragmaDump(); 8820b57cec5SDimitry Andric return nullptr; 8830b57cec5SDimitry Andric case tok::annot_pragma_attribute: 8840b57cec5SDimitry Andric HandlePragmaAttribute(); 8850b57cec5SDimitry Andric return nullptr; 8860b57cec5SDimitry Andric case tok::semi: 8870b57cec5SDimitry Andric // Either a C++11 empty-declaration or attribute-declaration. 8880b57cec5SDimitry Andric SingleDecl = 88981ad6265SDimitry Andric Actions.ActOnEmptyDeclaration(getCurScope(), Attrs, Tok.getLocation()); 8900b57cec5SDimitry Andric ConsumeExtraSemi(OutsideFunction); 8910b57cec5SDimitry Andric break; 8920b57cec5SDimitry Andric case tok::r_brace: 8930b57cec5SDimitry Andric Diag(Tok, diag::err_extraneous_closing_brace); 8940b57cec5SDimitry Andric ConsumeBrace(); 8950b57cec5SDimitry Andric return nullptr; 8960b57cec5SDimitry Andric case tok::eof: 8970b57cec5SDimitry Andric Diag(Tok, diag::err_expected_external_declaration); 8980b57cec5SDimitry Andric return nullptr; 8990b57cec5SDimitry Andric case tok::kw___extension__: { 9000b57cec5SDimitry Andric // __extension__ silences extension warnings in the subexpression. 9010b57cec5SDimitry Andric ExtensionRAIIObject O(Diags); // Use RAII to do this. 9020b57cec5SDimitry Andric ConsumeToken(); 903bdd1243dSDimitry Andric return ParseExternalDeclaration(Attrs, DeclSpecAttrs); 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric case tok::kw_asm: { 90681ad6265SDimitry Andric ProhibitAttributes(Attrs); 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric SourceLocation StartLoc = Tok.getLocation(); 9090b57cec5SDimitry Andric SourceLocation EndLoc; 9100b57cec5SDimitry Andric 911480093f4SDimitry Andric ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc)); 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric // Check if GNU-style InlineAsm is disabled. 9140b57cec5SDimitry Andric // Empty asm string is allowed because it will not introduce 9150b57cec5SDimitry Andric // any assembly code. 9160b57cec5SDimitry Andric if (!(getLangOpts().GNUAsm || Result.isInvalid())) { 9170b57cec5SDimitry Andric const auto *SL = cast<StringLiteral>(Result.get()); 9180b57cec5SDimitry Andric if (!SL->getString().trim().empty()) 9190b57cec5SDimitry Andric Diag(StartLoc, diag::err_gnu_inline_asm_disabled); 9200b57cec5SDimitry Andric } 9210b57cec5SDimitry Andric 9220b57cec5SDimitry Andric ExpectAndConsume(tok::semi, diag::err_expected_after, 9230b57cec5SDimitry Andric "top-level asm block"); 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric if (Result.isInvalid()) 9260b57cec5SDimitry Andric return nullptr; 9270b57cec5SDimitry Andric SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc); 9280b57cec5SDimitry Andric break; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric case tok::at: 931bdd1243dSDimitry Andric return ParseObjCAtDirectives(Attrs, DeclSpecAttrs); 9320b57cec5SDimitry Andric case tok::minus: 9330b57cec5SDimitry Andric case tok::plus: 9340b57cec5SDimitry Andric if (!getLangOpts().ObjC) { 9350b57cec5SDimitry Andric Diag(Tok, diag::err_expected_external_declaration); 9360b57cec5SDimitry Andric ConsumeToken(); 9370b57cec5SDimitry Andric return nullptr; 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric SingleDecl = ParseObjCMethodDefinition(); 9400b57cec5SDimitry Andric break; 9410b57cec5SDimitry Andric case tok::code_completion: 942fe6060f1SDimitry Andric cutOffParsing(); 9430b57cec5SDimitry Andric if (CurParsedObjCImpl) { 9440b57cec5SDimitry Andric // Code-complete Objective-C methods even without leading '-'/'+' prefix. 9450b57cec5SDimitry Andric Actions.CodeCompleteObjCMethodDecl(getCurScope(), 946bdd1243dSDimitry Andric /*IsInstanceMethod=*/std::nullopt, 9470b57cec5SDimitry Andric /*ReturnType=*/nullptr); 9480b57cec5SDimitry Andric } 9495f757f3fSDimitry Andric 9505f757f3fSDimitry Andric Sema::ParserCompletionContext PCC; 9515f757f3fSDimitry Andric if (CurParsedObjCImpl) { 9525f757f3fSDimitry Andric PCC = Sema::PCC_ObjCImplementation; 9535f757f3fSDimitry Andric } else if (PP.isIncrementalProcessingEnabled()) { 9545f757f3fSDimitry Andric PCC = Sema::PCC_TopLevelOrExpression; 9555f757f3fSDimitry Andric } else { 9565f757f3fSDimitry Andric PCC = Sema::PCC_Namespace; 9575f757f3fSDimitry Andric }; 9585f757f3fSDimitry Andric Actions.CodeCompleteOrdinaryName(getCurScope(), PCC); 9590b57cec5SDimitry Andric return nullptr; 96081ad6265SDimitry Andric case tok::kw_import: { 96181ad6265SDimitry Andric Sema::ModuleImportState IS = Sema::ModuleImportState::NotACXX20Module; 96281ad6265SDimitry Andric if (getLangOpts().CPlusPlusModules) { 96381ad6265SDimitry Andric llvm_unreachable("not expecting a c++20 import here"); 96481ad6265SDimitry Andric ProhibitAttributes(Attrs); 96581ad6265SDimitry Andric } 96681ad6265SDimitry Andric SingleDecl = ParseModuleImport(SourceLocation(), IS); 96781ad6265SDimitry Andric } break; 9680b57cec5SDimitry Andric case tok::kw_export: 96906c3fb27SDimitry Andric if (getLangOpts().CPlusPlusModules) { 97081ad6265SDimitry Andric ProhibitAttributes(Attrs); 9710b57cec5SDimitry Andric SingleDecl = ParseExportDeclaration(); 9720b57cec5SDimitry Andric break; 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric // This must be 'export template'. Parse it so we can diagnose our lack 9750b57cec5SDimitry Andric // of support. 976bdd1243dSDimitry Andric [[fallthrough]]; 9770b57cec5SDimitry Andric case tok::kw_using: 9780b57cec5SDimitry Andric case tok::kw_namespace: 9790b57cec5SDimitry Andric case tok::kw_typedef: 9800b57cec5SDimitry Andric case tok::kw_template: 9810b57cec5SDimitry Andric case tok::kw_static_assert: 9820b57cec5SDimitry Andric case tok::kw__Static_assert: 9830b57cec5SDimitry Andric // A function definition cannot start with any of these keywords. 9840b57cec5SDimitry Andric { 9850b57cec5SDimitry Andric SourceLocation DeclEnd; 98681ad6265SDimitry Andric return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 987bdd1243dSDimitry Andric DeclSpecAttrs); 9880b57cec5SDimitry Andric } 9890b57cec5SDimitry Andric 990bdd1243dSDimitry Andric case tok::kw_cbuffer: 991bdd1243dSDimitry Andric case tok::kw_tbuffer: 992bdd1243dSDimitry Andric if (getLangOpts().HLSL) { 993bdd1243dSDimitry Andric SourceLocation DeclEnd; 994bdd1243dSDimitry Andric return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 995bdd1243dSDimitry Andric DeclSpecAttrs); 996bdd1243dSDimitry Andric } 997bdd1243dSDimitry Andric goto dont_know; 998bdd1243dSDimitry Andric 9990b57cec5SDimitry Andric case tok::kw_static: 10000b57cec5SDimitry Andric // Parse (then ignore) 'static' prior to a template instantiation. This is 10010b57cec5SDimitry Andric // a GCC extension that we intentionally do not support. 10020b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 10030b57cec5SDimitry Andric Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 10040b57cec5SDimitry Andric << 0; 10050b57cec5SDimitry Andric SourceLocation DeclEnd; 100681ad6265SDimitry Andric return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 1007bdd1243dSDimitry Andric DeclSpecAttrs); 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric goto dont_know; 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric case tok::kw_inline: 10120b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) { 10130b57cec5SDimitry Andric tok::TokenKind NextKind = NextToken().getKind(); 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric // Inline namespaces. Allowed as an extension even in C++03. 10160b57cec5SDimitry Andric if (NextKind == tok::kw_namespace) { 10170b57cec5SDimitry Andric SourceLocation DeclEnd; 101881ad6265SDimitry Andric return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 1019bdd1243dSDimitry Andric DeclSpecAttrs); 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric // Parse (then ignore) 'inline' prior to a template instantiation. This is 10230b57cec5SDimitry Andric // a GCC extension that we intentionally do not support. 10240b57cec5SDimitry Andric if (NextKind == tok::kw_template) { 10250b57cec5SDimitry Andric Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 10260b57cec5SDimitry Andric << 1; 10270b57cec5SDimitry Andric SourceLocation DeclEnd; 102881ad6265SDimitry Andric return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs, 1029bdd1243dSDimitry Andric DeclSpecAttrs); 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric } 10320b57cec5SDimitry Andric goto dont_know; 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andric case tok::kw_extern: 10350b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 10360b57cec5SDimitry Andric // Extern templates 10370b57cec5SDimitry Andric SourceLocation ExternLoc = ConsumeToken(); 10380b57cec5SDimitry Andric SourceLocation TemplateLoc = ConsumeToken(); 10390b57cec5SDimitry Andric Diag(ExternLoc, getLangOpts().CPlusPlus11 ? 10400b57cec5SDimitry Andric diag::warn_cxx98_compat_extern_template : 10410b57cec5SDimitry Andric diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc); 10420b57cec5SDimitry Andric SourceLocation DeclEnd; 1043e8d8bef9SDimitry Andric return Actions.ConvertDeclToDeclGroup(ParseExplicitInstantiation( 104481ad6265SDimitry Andric DeclaratorContext::File, ExternLoc, TemplateLoc, DeclEnd, Attrs)); 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric goto dont_know; 10470b57cec5SDimitry Andric 10480b57cec5SDimitry Andric case tok::kw___if_exists: 10490b57cec5SDimitry Andric case tok::kw___if_not_exists: 10500b57cec5SDimitry Andric ParseMicrosoftIfExistsExternalDeclaration(); 10510b57cec5SDimitry Andric return nullptr; 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric case tok::kw_module: 10540b57cec5SDimitry Andric Diag(Tok, diag::err_unexpected_module_decl); 10550b57cec5SDimitry Andric SkipUntil(tok::semi); 10560b57cec5SDimitry Andric return nullptr; 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric default: 10590b57cec5SDimitry Andric dont_know: 10600b57cec5SDimitry Andric if (Tok.isEditorPlaceholder()) { 10610b57cec5SDimitry Andric ConsumeToken(); 10620b57cec5SDimitry Andric return nullptr; 10630b57cec5SDimitry Andric } 10645f757f3fSDimitry Andric if (getLangOpts().IncrementalExtensions && 1065bdd1243dSDimitry Andric !isDeclarationStatement(/*DisambiguatingWithExpression=*/true)) 1066bdd1243dSDimitry Andric return ParseTopLevelStmtDecl(); 1067bdd1243dSDimitry Andric 10680b57cec5SDimitry Andric // We can't tell whether this is a function-definition or declaration yet. 1069bdd1243dSDimitry Andric if (!SingleDecl) 1070bdd1243dSDimitry Andric return ParseDeclarationOrFunctionDefinition(Attrs, DeclSpecAttrs, DS); 10710b57cec5SDimitry Andric } 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric // This routine returns a DeclGroup, if the thing we parsed only contains a 10740b57cec5SDimitry Andric // single decl, convert it now. 10750b57cec5SDimitry Andric return Actions.ConvertDeclToDeclGroup(SingleDecl); 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric 10780b57cec5SDimitry Andric /// Determine whether the current token, if it occurs after a 10790b57cec5SDimitry Andric /// declarator, continues a declaration or declaration list. 10800b57cec5SDimitry Andric bool Parser::isDeclarationAfterDeclarator() { 10810b57cec5SDimitry Andric // Check for '= delete' or '= default' 10820b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 10830b57cec5SDimitry Andric const Token &KW = NextToken(); 10840b57cec5SDimitry Andric if (KW.is(tok::kw_default) || KW.is(tok::kw_delete)) 10850b57cec5SDimitry Andric return false; 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric return Tok.is(tok::equal) || // int X()= -> not a function def 10890b57cec5SDimitry Andric Tok.is(tok::comma) || // int X(), -> not a function def 10900b57cec5SDimitry Andric Tok.is(tok::semi) || // int X(); -> not a function def 10910b57cec5SDimitry Andric Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 10920b57cec5SDimitry Andric Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 10930b57cec5SDimitry Andric (getLangOpts().CPlusPlus && 10940b57cec5SDimitry Andric Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 10950b57cec5SDimitry Andric } 10960b57cec5SDimitry Andric 10970b57cec5SDimitry Andric /// Determine whether the current token, if it occurs after a 10980b57cec5SDimitry Andric /// declarator, indicates the start of a function definition. 10990b57cec5SDimitry Andric bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 11000b57cec5SDimitry Andric assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator"); 11010b57cec5SDimitry Andric if (Tok.is(tok::l_brace)) // int X() {} 11020b57cec5SDimitry Andric return true; 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric // Handle K&R C argument lists: int X(f) int f; {} 11050b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus && 11060b57cec5SDimitry Andric Declarator.getFunctionTypeInfo().isKNRPrototype()) 1107bdd1243dSDimitry Andric return isDeclarationSpecifier(ImplicitTypenameContext::No); 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 11100b57cec5SDimitry Andric const Token &KW = NextToken(); 11110b57cec5SDimitry Andric return KW.is(tok::kw_default) || KW.is(tok::kw_delete); 11120b57cec5SDimitry Andric } 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 11150b57cec5SDimitry Andric Tok.is(tok::kw_try); // X() try { ... } 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric /// Parse either a function-definition or a declaration. We can't tell which 11190b57cec5SDimitry Andric /// we have until we read up to the compound-statement in function-definition. 11200b57cec5SDimitry Andric /// TemplateParams, if non-NULL, provides the template parameters when we're 11210b57cec5SDimitry Andric /// parsing a C++ template-declaration. 11220b57cec5SDimitry Andric /// 11230b57cec5SDimitry Andric /// function-definition: [C99 6.9.1] 11240b57cec5SDimitry Andric /// decl-specs declarator declaration-list[opt] compound-statement 11250b57cec5SDimitry Andric /// [C90] function-definition: [C99 6.7.1] - implicit int result 11260b57cec5SDimitry Andric /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 11270b57cec5SDimitry Andric /// 11280b57cec5SDimitry Andric /// declaration: [C99 6.7] 11290b57cec5SDimitry Andric /// declaration-specifiers init-declarator-list[opt] ';' 11300b57cec5SDimitry Andric /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] 11310b57cec5SDimitry Andric /// [OMP] threadprivate-directive 11320b57cec5SDimitry Andric /// [OMP] allocate-directive [TODO] 11330b57cec5SDimitry Andric /// 113481ad6265SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal( 1135bdd1243dSDimitry Andric ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs, 1136bdd1243dSDimitry Andric ParsingDeclSpec &DS, AccessSpecifier AS) { 1137bdd1243dSDimitry Andric // Because we assume that the DeclSpec has not yet been initialised, we simply 1138bdd1243dSDimitry Andric // overwrite the source range and attribute the provided leading declspec 1139bdd1243dSDimitry Andric // attributes. 1140bdd1243dSDimitry Andric assert(DS.getSourceRange().isInvalid() && 1141bdd1243dSDimitry Andric "expected uninitialised source range"); 1142bdd1243dSDimitry Andric DS.SetRangeStart(DeclSpecAttrs.Range.getBegin()); 1143bdd1243dSDimitry Andric DS.SetRangeEnd(DeclSpecAttrs.Range.getEnd()); 1144bdd1243dSDimitry Andric DS.takeAttributesFrom(DeclSpecAttrs); 1145bdd1243dSDimitry Andric 11460b57cec5SDimitry Andric MaybeParseMicrosoftAttributes(DS.getAttributes()); 11470b57cec5SDimitry Andric // Parse the common declaration-specifiers piece. 11480b57cec5SDimitry Andric ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, 11490b57cec5SDimitry Andric DeclSpecContext::DSC_top_level); 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric // If we had a free-standing type definition with a missing semicolon, we 11520b57cec5SDimitry Andric // may get this far before the problem becomes obvious. 11530b57cec5SDimitry Andric if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition( 11540b57cec5SDimitry Andric DS, AS, DeclSpecContext::DSC_top_level)) 11550b57cec5SDimitry Andric return nullptr; 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 11580b57cec5SDimitry Andric // declaration-specifiers init-declarator-list[opt] ';' 11590b57cec5SDimitry Andric if (Tok.is(tok::semi)) { 11600b57cec5SDimitry Andric auto LengthOfTSTToken = [](DeclSpec::TST TKind) { 11610b57cec5SDimitry Andric assert(DeclSpec::isDeclRep(TKind)); 11620b57cec5SDimitry Andric switch(TKind) { 11630b57cec5SDimitry Andric case DeclSpec::TST_class: 11640b57cec5SDimitry Andric return 5; 11650b57cec5SDimitry Andric case DeclSpec::TST_struct: 11660b57cec5SDimitry Andric return 6; 11670b57cec5SDimitry Andric case DeclSpec::TST_union: 11680b57cec5SDimitry Andric return 5; 11690b57cec5SDimitry Andric case DeclSpec::TST_enum: 11700b57cec5SDimitry Andric return 4; 11710b57cec5SDimitry Andric case DeclSpec::TST_interface: 11720b57cec5SDimitry Andric return 9; 11730b57cec5SDimitry Andric default: 11740b57cec5SDimitry Andric llvm_unreachable("we only expect to get the length of the class/struct/union/enum"); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric }; 11780b57cec5SDimitry Andric // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]' 11790b57cec5SDimitry Andric SourceLocation CorrectLocationForAttributes = 11800b57cec5SDimitry Andric DeclSpec::isDeclRep(DS.getTypeSpecType()) 11810b57cec5SDimitry Andric ? DS.getTypeSpecTypeLoc().getLocWithOffset( 11820b57cec5SDimitry Andric LengthOfTSTToken(DS.getTypeSpecType())) 11830b57cec5SDimitry Andric : SourceLocation(); 118481ad6265SDimitry Andric ProhibitAttributes(Attrs, CorrectLocationForAttributes); 11850b57cec5SDimitry Andric ConsumeToken(); 11860b57cec5SDimitry Andric RecordDecl *AnonRecord = nullptr; 118781ad6265SDimitry Andric Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( 118881ad6265SDimitry Andric getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord); 11890b57cec5SDimitry Andric DS.complete(TheDecl); 11905f757f3fSDimitry Andric Actions.ActOnDefinedDeclarationSpecifier(TheDecl); 11910b57cec5SDimitry Andric if (AnonRecord) { 11920b57cec5SDimitry Andric Decl* decls[] = {AnonRecord, TheDecl}; 11930b57cec5SDimitry Andric return Actions.BuildDeclaratorGroup(decls); 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric return Actions.ConvertDeclToDeclGroup(TheDecl); 11960b57cec5SDimitry Andric } 11970b57cec5SDimitry Andric 11985f757f3fSDimitry Andric if (DS.hasTagDefinition()) 11995f757f3fSDimitry Andric Actions.ActOnDefinedDeclarationSpecifier(DS.getRepAsDecl()); 12005f757f3fSDimitry Andric 12010b57cec5SDimitry Andric // ObjC2 allows prefix attributes on class interfaces and protocols. 12020b57cec5SDimitry Andric // FIXME: This still needs better diagnostics. We should only accept 12030b57cec5SDimitry Andric // attributes here, no types, etc. 12040b57cec5SDimitry Andric if (getLangOpts().ObjC && Tok.is(tok::at)) { 12050b57cec5SDimitry Andric SourceLocation AtLoc = ConsumeToken(); // the "@" 12060b57cec5SDimitry Andric if (!Tok.isObjCAtKeyword(tok::objc_interface) && 12070b57cec5SDimitry Andric !Tok.isObjCAtKeyword(tok::objc_protocol) && 12080b57cec5SDimitry Andric !Tok.isObjCAtKeyword(tok::objc_implementation)) { 12090b57cec5SDimitry Andric Diag(Tok, diag::err_objc_unexpected_attr); 12100b57cec5SDimitry Andric SkipUntil(tok::semi); 12110b57cec5SDimitry Andric return nullptr; 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric DS.abort(); 121581ad6265SDimitry Andric DS.takeAttributesFrom(Attrs); 12160b57cec5SDimitry Andric 12170b57cec5SDimitry Andric const char *PrevSpec = nullptr; 12180b57cec5SDimitry Andric unsigned DiagID; 12190b57cec5SDimitry Andric if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID, 12200b57cec5SDimitry Andric Actions.getASTContext().getPrintingPolicy())) 12210b57cec5SDimitry Andric Diag(AtLoc, DiagID) << PrevSpec; 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric if (Tok.isObjCAtKeyword(tok::objc_protocol)) 12240b57cec5SDimitry Andric return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric if (Tok.isObjCAtKeyword(tok::objc_implementation)) 12270b57cec5SDimitry Andric return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes()); 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric return Actions.ConvertDeclToDeclGroup( 12300b57cec5SDimitry Andric ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes())); 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric // If the declspec consisted only of 'extern' and we have a string 12340b57cec5SDimitry Andric // literal following it, this must be a C++ linkage specifier like 12350b57cec5SDimitry Andric // 'extern "C"'. 12360b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && isTokenStringLiteral() && 12370b57cec5SDimitry Andric DS.getStorageClassSpec() == DeclSpec::SCS_extern && 12380b57cec5SDimitry Andric DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 123981ad6265SDimitry Andric ProhibitAttributes(Attrs); 1240e8d8bef9SDimitry Andric Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::File); 12410b57cec5SDimitry Andric return Actions.ConvertDeclToDeclGroup(TheDecl); 12420b57cec5SDimitry Andric } 12430b57cec5SDimitry Andric 124481ad6265SDimitry Andric return ParseDeclGroup(DS, DeclaratorContext::File, Attrs); 12450b57cec5SDimitry Andric } 12460b57cec5SDimitry Andric 124781ad6265SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition( 1248bdd1243dSDimitry Andric ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs, 1249bdd1243dSDimitry Andric ParsingDeclSpec *DS, AccessSpecifier AS) { 12505f757f3fSDimitry Andric // Add an enclosing time trace scope for a bunch of small scopes with 12515f757f3fSDimitry Andric // "EvaluateAsConstExpr". 12525f757f3fSDimitry Andric llvm::TimeTraceScope TimeScope("ParseDeclarationOrFunctionDefinition", [&]() { 12535f757f3fSDimitry Andric return Tok.getLocation().printToString( 12545f757f3fSDimitry Andric Actions.getASTContext().getSourceManager()); 12555f757f3fSDimitry Andric }); 12565f757f3fSDimitry Andric 12570b57cec5SDimitry Andric if (DS) { 1258bdd1243dSDimitry Andric return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS); 12590b57cec5SDimitry Andric } else { 12600b57cec5SDimitry Andric ParsingDeclSpec PDS(*this); 12610b57cec5SDimitry Andric // Must temporarily exit the objective-c container scope for 12620b57cec5SDimitry Andric // parsing c constructs and re-enter objc container scope 12630b57cec5SDimitry Andric // afterwards. 12640b57cec5SDimitry Andric ObjCDeclContextSwitch ObjCDC(*this); 12650b57cec5SDimitry Andric 1266bdd1243dSDimitry Andric return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, PDS, AS); 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric 12700b57cec5SDimitry Andric /// ParseFunctionDefinition - We parsed and verified that the specified 12710b57cec5SDimitry Andric /// Declarator is well formed. If this is a K&R-style function, read the 12720b57cec5SDimitry Andric /// parameters declaration-list, then start the compound-statement. 12730b57cec5SDimitry Andric /// 12740b57cec5SDimitry Andric /// function-definition: [C99 6.9.1] 12750b57cec5SDimitry Andric /// decl-specs declarator declaration-list[opt] compound-statement 12760b57cec5SDimitry Andric /// [C90] function-definition: [C99 6.7.1] - implicit int result 12770b57cec5SDimitry Andric /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 12780b57cec5SDimitry Andric /// [C++] function-definition: [C++ 8.4] 12790b57cec5SDimitry Andric /// decl-specifier-seq[opt] declarator ctor-initializer[opt] 12800b57cec5SDimitry Andric /// function-body 12810b57cec5SDimitry Andric /// [C++] function-definition: [C++ 8.4] 12820b57cec5SDimitry Andric /// decl-specifier-seq[opt] declarator function-try-block 12830b57cec5SDimitry Andric /// 12840b57cec5SDimitry Andric Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 12850b57cec5SDimitry Andric const ParsedTemplateInfo &TemplateInfo, 12860b57cec5SDimitry Andric LateParsedAttrList *LateParsedAttrs) { 12875f757f3fSDimitry Andric llvm::TimeTraceScope TimeScope("ParseFunctionDefinition", [&]() { 12885f757f3fSDimitry Andric return Actions.GetNameForDeclarator(D).getName().getAsString(); 12895f757f3fSDimitry Andric }); 12905f757f3fSDimitry Andric 12910b57cec5SDimitry Andric // Poison SEH identifiers so they are flagged as illegal in function bodies. 12920b57cec5SDimitry Andric PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 12930b57cec5SDimitry Andric const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 129455e4f9d5SDimitry Andric TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 12950b57cec5SDimitry Andric 129681ad6265SDimitry Andric // If this is C89 and the declspecs were completely missing, fudge in an 12970b57cec5SDimitry Andric // implicit int. We do this here because this is the only place where 12980b57cec5SDimitry Andric // declaration-specifiers are completely optional in the grammar. 129981ad6265SDimitry Andric if (getLangOpts().isImplicitIntRequired() && D.getDeclSpec().isEmpty()) { 130081ad6265SDimitry Andric Diag(D.getIdentifierLoc(), diag::warn_missing_type_specifier) 130181ad6265SDimitry Andric << D.getDeclSpec().getSourceRange(); 13020b57cec5SDimitry Andric const char *PrevSpec; 13030b57cec5SDimitry Andric unsigned DiagID; 13040b57cec5SDimitry Andric const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); 13050b57cec5SDimitry Andric D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 13060b57cec5SDimitry Andric D.getIdentifierLoc(), 13070b57cec5SDimitry Andric PrevSpec, DiagID, 13080b57cec5SDimitry Andric Policy); 13090b57cec5SDimitry Andric D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 13100b57cec5SDimitry Andric } 13110b57cec5SDimitry Andric 13120b57cec5SDimitry Andric // If this declaration was formed with a K&R-style identifier list for the 13130b57cec5SDimitry Andric // arguments, parse declarations for all of the args next. 13140b57cec5SDimitry Andric // int foo(a,b) int a; float b; {} 13150b57cec5SDimitry Andric if (FTI.isKNRPrototype()) 13160b57cec5SDimitry Andric ParseKNRParamDeclarations(D); 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric // We should have either an opening brace or, in a C++ constructor, 13190b57cec5SDimitry Andric // we may have a colon. 13200b57cec5SDimitry Andric if (Tok.isNot(tok::l_brace) && 13210b57cec5SDimitry Andric (!getLangOpts().CPlusPlus || 13220b57cec5SDimitry Andric (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) && 13230b57cec5SDimitry Andric Tok.isNot(tok::equal)))) { 13240b57cec5SDimitry Andric Diag(Tok, diag::err_expected_fn_body); 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric // Skip over garbage, until we get to '{'. Don't eat the '{'. 13270b57cec5SDimitry Andric SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric // If we didn't find the '{', bail out. 13300b57cec5SDimitry Andric if (Tok.isNot(tok::l_brace)) 13310b57cec5SDimitry Andric return nullptr; 13320b57cec5SDimitry Andric } 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric // Check to make sure that any normal attributes are allowed to be on 13350b57cec5SDimitry Andric // a definition. Late parsed attributes are checked at the end. 13360b57cec5SDimitry Andric if (Tok.isNot(tok::equal)) { 13370b57cec5SDimitry Andric for (const ParsedAttr &AL : D.getAttributes()) 1338fe6060f1SDimitry Andric if (AL.isKnownToGCC() && !AL.isStandardAttributeSyntax()) 1339a7dea167SDimitry Andric Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL; 13400b57cec5SDimitry Andric } 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric // In delayed template parsing mode, for function template we consume the 13430b57cec5SDimitry Andric // tokens and store them for late parsing at the end of the translation unit. 13440b57cec5SDimitry Andric if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) && 13450b57cec5SDimitry Andric TemplateInfo.Kind == ParsedTemplateInfo::Template && 13460b57cec5SDimitry Andric Actions.canDelayFunctionBody(D)) { 13470b57cec5SDimitry Andric MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams); 13480b57cec5SDimitry Andric 13490b57cec5SDimitry Andric ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 13500b57cec5SDimitry Andric Scope::CompoundStmtScope); 13510b57cec5SDimitry Andric Scope *ParentScope = getCurScope()->getParent(); 13520b57cec5SDimitry Andric 1353e8d8bef9SDimitry Andric D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition); 13540b57cec5SDimitry Andric Decl *DP = Actions.HandleDeclarator(ParentScope, D, 13550b57cec5SDimitry Andric TemplateParameterLists); 13560b57cec5SDimitry Andric D.complete(DP); 13570b57cec5SDimitry Andric D.getMutableDeclSpec().abort(); 13580b57cec5SDimitry Andric 13590b57cec5SDimitry Andric if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) && 13600b57cec5SDimitry Andric trySkippingFunctionBody()) { 13610b57cec5SDimitry Andric BodyScope.Exit(); 13620b57cec5SDimitry Andric return Actions.ActOnSkippedFunctionBody(DP); 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric CachedTokens Toks; 13660b57cec5SDimitry Andric LexTemplateFunctionForLateParsing(Toks); 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric if (DP) { 13690b57cec5SDimitry Andric FunctionDecl *FnD = DP->getAsFunction(); 13700b57cec5SDimitry Andric Actions.CheckForFunctionRedefinition(FnD); 13710b57cec5SDimitry Andric Actions.MarkAsLateParsedTemplate(FnD, DP, Toks); 13720b57cec5SDimitry Andric } 13730b57cec5SDimitry Andric return DP; 13740b57cec5SDimitry Andric } 13750b57cec5SDimitry Andric else if (CurParsedObjCImpl && 13760b57cec5SDimitry Andric !TemplateInfo.TemplateParams && 13770b57cec5SDimitry Andric (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || 13780b57cec5SDimitry Andric Tok.is(tok::colon)) && 13790b57cec5SDimitry Andric Actions.CurContext->isTranslationUnit()) { 13800b57cec5SDimitry Andric ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 13810b57cec5SDimitry Andric Scope::CompoundStmtScope); 13820b57cec5SDimitry Andric Scope *ParentScope = getCurScope()->getParent(); 13830b57cec5SDimitry Andric 1384e8d8bef9SDimitry Andric D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition); 13850b57cec5SDimitry Andric Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D, 13860b57cec5SDimitry Andric MultiTemplateParamsArg()); 13870b57cec5SDimitry Andric D.complete(FuncDecl); 13880b57cec5SDimitry Andric D.getMutableDeclSpec().abort(); 13890b57cec5SDimitry Andric if (FuncDecl) { 13900b57cec5SDimitry Andric // Consume the tokens and store them for later parsing. 13910b57cec5SDimitry Andric StashAwayMethodOrFunctionBodyTokens(FuncDecl); 13920b57cec5SDimitry Andric CurParsedObjCImpl->HasCFunction = true; 13930b57cec5SDimitry Andric return FuncDecl; 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric // FIXME: Should we really fall through here? 13960b57cec5SDimitry Andric } 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric // Enter a scope for the function body. 13990b57cec5SDimitry Andric ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope | 14000b57cec5SDimitry Andric Scope::CompoundStmtScope); 14010b57cec5SDimitry Andric 140281ad6265SDimitry Andric // Parse function body eagerly if it is either '= delete;' or '= default;' as 140381ad6265SDimitry Andric // ActOnStartOfFunctionDef needs to know whether the function is deleted. 140481ad6265SDimitry Andric Sema::FnBodyKind BodyKind = Sema::FnBodyKind::Other; 140581ad6265SDimitry Andric SourceLocation KWLoc; 140681ad6265SDimitry Andric if (TryConsumeToken(tok::equal)) { 140781ad6265SDimitry Andric assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='"); 140881ad6265SDimitry Andric 140981ad6265SDimitry Andric if (TryConsumeToken(tok::kw_delete, KWLoc)) { 141081ad6265SDimitry Andric Diag(KWLoc, getLangOpts().CPlusPlus11 141181ad6265SDimitry Andric ? diag::warn_cxx98_compat_defaulted_deleted_function 141281ad6265SDimitry Andric : diag::ext_defaulted_deleted_function) 141381ad6265SDimitry Andric << 1 /* deleted */; 141481ad6265SDimitry Andric BodyKind = Sema::FnBodyKind::Delete; 141581ad6265SDimitry Andric } else if (TryConsumeToken(tok::kw_default, KWLoc)) { 141681ad6265SDimitry Andric Diag(KWLoc, getLangOpts().CPlusPlus11 141781ad6265SDimitry Andric ? diag::warn_cxx98_compat_defaulted_deleted_function 141881ad6265SDimitry Andric : diag::ext_defaulted_deleted_function) 141981ad6265SDimitry Andric << 0 /* defaulted */; 142081ad6265SDimitry Andric BodyKind = Sema::FnBodyKind::Default; 142181ad6265SDimitry Andric } else { 142281ad6265SDimitry Andric llvm_unreachable("function definition after = not 'delete' or 'default'"); 142381ad6265SDimitry Andric } 142481ad6265SDimitry Andric 142581ad6265SDimitry Andric if (Tok.is(tok::comma)) { 142681ad6265SDimitry Andric Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 142781ad6265SDimitry Andric << (BodyKind == Sema::FnBodyKind::Delete); 142881ad6265SDimitry Andric SkipUntil(tok::semi); 142981ad6265SDimitry Andric } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, 143081ad6265SDimitry Andric BodyKind == Sema::FnBodyKind::Delete 143181ad6265SDimitry Andric ? "delete" 143281ad6265SDimitry Andric : "default")) { 143381ad6265SDimitry Andric SkipUntil(tok::semi); 143481ad6265SDimitry Andric } 143581ad6265SDimitry Andric } 143681ad6265SDimitry Andric 14370b57cec5SDimitry Andric // Tell the actions module that we have entered a function definition with the 14380b57cec5SDimitry Andric // specified Declarator for the function. 14390b57cec5SDimitry Andric Sema::SkipBodyInfo SkipBody; 14400b57cec5SDimitry Andric Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D, 14410b57cec5SDimitry Andric TemplateInfo.TemplateParams 14420b57cec5SDimitry Andric ? *TemplateInfo.TemplateParams 14430b57cec5SDimitry Andric : MultiTemplateParamsArg(), 144481ad6265SDimitry Andric &SkipBody, BodyKind); 14450b57cec5SDimitry Andric 14460b57cec5SDimitry Andric if (SkipBody.ShouldSkip) { 144781ad6265SDimitry Andric // Do NOT enter SkipFunctionBody if we already consumed the tokens. 144881ad6265SDimitry Andric if (BodyKind == Sema::FnBodyKind::Other) 14490b57cec5SDimitry Andric SkipFunctionBody(); 145081ad6265SDimitry Andric 145106c3fb27SDimitry Andric // ExpressionEvaluationContext is pushed in ActOnStartOfFunctionDef 145206c3fb27SDimitry Andric // and it would be popped in ActOnFinishFunctionBody. 145306c3fb27SDimitry Andric // We pop it explcitly here since ActOnFinishFunctionBody won't get called. 145406c3fb27SDimitry Andric // 145506c3fb27SDimitry Andric // Do not call PopExpressionEvaluationContext() if it is a lambda because 145606c3fb27SDimitry Andric // one is already popped when finishing the lambda in BuildLambdaExpr(). 145706c3fb27SDimitry Andric // 145806c3fb27SDimitry Andric // FIXME: It looks not easy to balance PushExpressionEvaluationContext() 145906c3fb27SDimitry Andric // and PopExpressionEvaluationContext(). 146006c3fb27SDimitry Andric if (!isLambdaCallOperator(dyn_cast_if_present<FunctionDecl>(Res))) 146106c3fb27SDimitry Andric Actions.PopExpressionEvaluationContext(); 14620b57cec5SDimitry Andric return Res; 14630b57cec5SDimitry Andric } 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric // Break out of the ParsingDeclarator context before we parse the body. 14660b57cec5SDimitry Andric D.complete(Res); 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andric // Break out of the ParsingDeclSpec context, too. This const_cast is 14690b57cec5SDimitry Andric // safe because we're always the sole owner. 14700b57cec5SDimitry Andric D.getMutableDeclSpec().abort(); 14710b57cec5SDimitry Andric 147281ad6265SDimitry Andric if (BodyKind != Sema::FnBodyKind::Other) { 147381ad6265SDimitry Andric Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind); 147481ad6265SDimitry Andric Stmt *GeneratedBody = Res ? Res->getBody() : nullptr; 147581ad6265SDimitry Andric Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false); 147681ad6265SDimitry Andric return Res; 147781ad6265SDimitry Andric } 147881ad6265SDimitry Andric 147955e4f9d5SDimitry Andric // With abbreviated function templates - we need to explicitly add depth to 148055e4f9d5SDimitry Andric // account for the implicit template parameter list induced by the template. 14815f757f3fSDimitry Andric if (const auto *Template = dyn_cast_if_present<FunctionTemplateDecl>(Res); 14825f757f3fSDimitry Andric Template && Template->isAbbreviated() && 148355e4f9d5SDimitry Andric Template->getTemplateParameters()->getParam(0)->isImplicit()) 148455e4f9d5SDimitry Andric // First template parameter is implicit - meaning no explicit template 148555e4f9d5SDimitry Andric // parameter list was specified. 148655e4f9d5SDimitry Andric CurTemplateDepthTracker.addDepth(1); 148755e4f9d5SDimitry Andric 14880b57cec5SDimitry Andric if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) && 14890b57cec5SDimitry Andric trySkippingFunctionBody()) { 14900b57cec5SDimitry Andric BodyScope.Exit(); 14910b57cec5SDimitry Andric Actions.ActOnSkippedFunctionBody(Res); 14920b57cec5SDimitry Andric return Actions.ActOnFinishFunctionBody(Res, nullptr, false); 14930b57cec5SDimitry Andric } 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric if (Tok.is(tok::kw_try)) 14960b57cec5SDimitry Andric return ParseFunctionTryBlock(Res, BodyScope); 14970b57cec5SDimitry Andric 14980b57cec5SDimitry Andric // If we have a colon, then we're probably parsing a C++ 14990b57cec5SDimitry Andric // ctor-initializer. 15000b57cec5SDimitry Andric if (Tok.is(tok::colon)) { 15010b57cec5SDimitry Andric ParseConstructorInitializer(Res); 15020b57cec5SDimitry Andric 15030b57cec5SDimitry Andric // Recover from error. 15040b57cec5SDimitry Andric if (!Tok.is(tok::l_brace)) { 15050b57cec5SDimitry Andric BodyScope.Exit(); 15060b57cec5SDimitry Andric Actions.ActOnFinishFunctionBody(Res, nullptr); 15070b57cec5SDimitry Andric return Res; 15080b57cec5SDimitry Andric } 15090b57cec5SDimitry Andric } else 15100b57cec5SDimitry Andric Actions.ActOnDefaultCtorInitializers(Res); 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric // Late attributes are parsed in the same scope as the function body. 15130b57cec5SDimitry Andric if (LateParsedAttrs) 15140b57cec5SDimitry Andric ParseLexedAttributeList(*LateParsedAttrs, Res, false, true); 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric return ParseFunctionStatementBody(Res, BodyScope); 15170b57cec5SDimitry Andric } 15180b57cec5SDimitry Andric 15190b57cec5SDimitry Andric void Parser::SkipFunctionBody() { 15200b57cec5SDimitry Andric if (Tok.is(tok::equal)) { 15210b57cec5SDimitry Andric SkipUntil(tok::semi); 15220b57cec5SDimitry Andric return; 15230b57cec5SDimitry Andric } 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric bool IsFunctionTryBlock = Tok.is(tok::kw_try); 15260b57cec5SDimitry Andric if (IsFunctionTryBlock) 15270b57cec5SDimitry Andric ConsumeToken(); 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric CachedTokens Skipped; 15300b57cec5SDimitry Andric if (ConsumeAndStoreFunctionPrologue(Skipped)) 15310b57cec5SDimitry Andric SkipMalformedDecl(); 15320b57cec5SDimitry Andric else { 15330b57cec5SDimitry Andric SkipUntil(tok::r_brace); 15340b57cec5SDimitry Andric while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) { 15350b57cec5SDimitry Andric SkipUntil(tok::l_brace); 15360b57cec5SDimitry Andric SkipUntil(tok::r_brace); 15370b57cec5SDimitry Andric } 15380b57cec5SDimitry Andric } 15390b57cec5SDimitry Andric } 15400b57cec5SDimitry Andric 15410b57cec5SDimitry Andric /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides 15420b57cec5SDimitry Andric /// types for a function with a K&R-style identifier list for arguments. 15430b57cec5SDimitry Andric void Parser::ParseKNRParamDeclarations(Declarator &D) { 15440b57cec5SDimitry Andric // We know that the top-level of this declarator is a function. 15450b57cec5SDimitry Andric DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric // Enter function-declaration scope, limiting any declarators to the 15480b57cec5SDimitry Andric // function prototype scope, including parameter declarators. 15490b57cec5SDimitry Andric ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 15500b57cec5SDimitry Andric Scope::FunctionDeclarationScope | Scope::DeclScope); 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric // Read all the argument declarations. 1553bdd1243dSDimitry Andric while (isDeclarationSpecifier(ImplicitTypenameContext::No)) { 15540b57cec5SDimitry Andric SourceLocation DSStart = Tok.getLocation(); 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric // Parse the common declaration-specifiers piece. 15570b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 15580b57cec5SDimitry Andric ParseDeclarationSpecifiers(DS); 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric // C99 6.9.1p6: 'each declaration in the declaration list shall have at 15610b57cec5SDimitry Andric // least one declarator'. 15620b57cec5SDimitry Andric // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 15630b57cec5SDimitry Andric // the declarations though. It's trivial to ignore them, really hard to do 15640b57cec5SDimitry Andric // anything else with them. 15650b57cec5SDimitry Andric if (TryConsumeToken(tok::semi)) { 15660b57cec5SDimitry Andric Diag(DSStart, diag::err_declaration_does_not_declare_param); 15670b57cec5SDimitry Andric continue; 15680b57cec5SDimitry Andric } 15690b57cec5SDimitry Andric 15700b57cec5SDimitry Andric // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 15710b57cec5SDimitry Andric // than register. 15720b57cec5SDimitry Andric if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 15730b57cec5SDimitry Andric DS.getStorageClassSpec() != DeclSpec::SCS_register) { 15740b57cec5SDimitry Andric Diag(DS.getStorageClassSpecLoc(), 15750b57cec5SDimitry Andric diag::err_invalid_storage_class_in_func_decl); 15760b57cec5SDimitry Andric DS.ClearStorageClassSpecs(); 15770b57cec5SDimitry Andric } 15780b57cec5SDimitry Andric if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) { 15790b57cec5SDimitry Andric Diag(DS.getThreadStorageClassSpecLoc(), 15800b57cec5SDimitry Andric diag::err_invalid_storage_class_in_func_decl); 15810b57cec5SDimitry Andric DS.ClearStorageClassSpecs(); 15820b57cec5SDimitry Andric } 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric // Parse the first declarator attached to this declspec. 158581ad6265SDimitry Andric Declarator ParmDeclarator(DS, ParsedAttributesView::none(), 158681ad6265SDimitry Andric DeclaratorContext::KNRTypeList); 15870b57cec5SDimitry Andric ParseDeclarator(ParmDeclarator); 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric // Handle the full declarator list. 159004eeddc0SDimitry Andric while (true) { 15910b57cec5SDimitry Andric // If attributes are present, parse them. 15920b57cec5SDimitry Andric MaybeParseGNUAttributes(ParmDeclarator); 15930b57cec5SDimitry Andric 15940b57cec5SDimitry Andric // Ask the actions module to compute the type for this declarator. 15950b57cec5SDimitry Andric Decl *Param = 15960b57cec5SDimitry Andric Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 15970b57cec5SDimitry Andric 15980b57cec5SDimitry Andric if (Param && 15990b57cec5SDimitry Andric // A missing identifier has already been diagnosed. 16000b57cec5SDimitry Andric ParmDeclarator.getIdentifier()) { 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric // Scan the argument list looking for the correct param to apply this 16030b57cec5SDimitry Andric // type. 16040b57cec5SDimitry Andric for (unsigned i = 0; ; ++i) { 16050b57cec5SDimitry Andric // C99 6.9.1p6: those declarators shall declare only identifiers from 16060b57cec5SDimitry Andric // the identifier list. 16070b57cec5SDimitry Andric if (i == FTI.NumParams) { 16080b57cec5SDimitry Andric Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 16090b57cec5SDimitry Andric << ParmDeclarator.getIdentifier(); 16100b57cec5SDimitry Andric break; 16110b57cec5SDimitry Andric } 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) { 16140b57cec5SDimitry Andric // Reject redefinitions of parameters. 16150b57cec5SDimitry Andric if (FTI.Params[i].Param) { 16160b57cec5SDimitry Andric Diag(ParmDeclarator.getIdentifierLoc(), 16170b57cec5SDimitry Andric diag::err_param_redefinition) 16180b57cec5SDimitry Andric << ParmDeclarator.getIdentifier(); 16190b57cec5SDimitry Andric } else { 16200b57cec5SDimitry Andric FTI.Params[i].Param = Param; 16210b57cec5SDimitry Andric } 16220b57cec5SDimitry Andric break; 16230b57cec5SDimitry Andric } 16240b57cec5SDimitry Andric } 16250b57cec5SDimitry Andric } 16260b57cec5SDimitry Andric 16270b57cec5SDimitry Andric // If we don't have a comma, it is either the end of the list (a ';') or 16280b57cec5SDimitry Andric // an error, bail out. 16290b57cec5SDimitry Andric if (Tok.isNot(tok::comma)) 16300b57cec5SDimitry Andric break; 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric ParmDeclarator.clear(); 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric // Consume the comma. 16350b57cec5SDimitry Andric ParmDeclarator.setCommaLoc(ConsumeToken()); 16360b57cec5SDimitry Andric 16370b57cec5SDimitry Andric // Parse the next declarator. 16380b57cec5SDimitry Andric ParseDeclarator(ParmDeclarator); 16390b57cec5SDimitry Andric } 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric // Consume ';' and continue parsing. 16420b57cec5SDimitry Andric if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) 16430b57cec5SDimitry Andric continue; 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric // Otherwise recover by skipping to next semi or mandatory function body. 16460b57cec5SDimitry Andric if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch)) 16470b57cec5SDimitry Andric break; 16480b57cec5SDimitry Andric TryConsumeToken(tok::semi); 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andric // The actions module must verify that all arguments were declared. 16520b57cec5SDimitry Andric Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 16530b57cec5SDimitry Andric } 16540b57cec5SDimitry Andric 16550b57cec5SDimitry Andric 16560b57cec5SDimitry Andric /// ParseAsmStringLiteral - This is just a normal string-literal, but is not 16570b57cec5SDimitry Andric /// allowed to be a wide string, and is not subject to character translation. 1658480093f4SDimitry Andric /// Unlike GCC, we also diagnose an empty string literal when parsing for an 1659480093f4SDimitry Andric /// asm label as opposed to an asm statement, because such a construct does not 1660480093f4SDimitry Andric /// behave well. 16610b57cec5SDimitry Andric /// 16620b57cec5SDimitry Andric /// [GNU] asm-string-literal: 16630b57cec5SDimitry Andric /// string-literal 16640b57cec5SDimitry Andric /// 1665480093f4SDimitry Andric ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) { 16660b57cec5SDimitry Andric if (!isTokenStringLiteral()) { 16670b57cec5SDimitry Andric Diag(Tok, diag::err_expected_string_literal) 16680b57cec5SDimitry Andric << /*Source='in...'*/0 << "'asm'"; 16690b57cec5SDimitry Andric return ExprError(); 16700b57cec5SDimitry Andric } 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric ExprResult AsmString(ParseStringLiteralExpression()); 16730b57cec5SDimitry Andric if (!AsmString.isInvalid()) { 16740b57cec5SDimitry Andric const auto *SL = cast<StringLiteral>(AsmString.get()); 167581ad6265SDimitry Andric if (!SL->isOrdinary()) { 16760b57cec5SDimitry Andric Diag(Tok, diag::err_asm_operand_wide_string_literal) 16770b57cec5SDimitry Andric << SL->isWide() 16780b57cec5SDimitry Andric << SL->getSourceRange(); 16790b57cec5SDimitry Andric return ExprError(); 16800b57cec5SDimitry Andric } 1681480093f4SDimitry Andric if (ForAsmLabel && SL->getString().empty()) { 1682480093f4SDimitry Andric Diag(Tok, diag::err_asm_operand_wide_string_literal) 1683480093f4SDimitry Andric << 2 /* an empty */ << SL->getSourceRange(); 1684480093f4SDimitry Andric return ExprError(); 1685480093f4SDimitry Andric } 16860b57cec5SDimitry Andric } 16870b57cec5SDimitry Andric return AsmString; 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric /// ParseSimpleAsm 16910b57cec5SDimitry Andric /// 16920b57cec5SDimitry Andric /// [GNU] simple-asm-expr: 16930b57cec5SDimitry Andric /// 'asm' '(' asm-string-literal ')' 16940b57cec5SDimitry Andric /// 1695480093f4SDimitry Andric ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) { 16960b57cec5SDimitry Andric assert(Tok.is(tok::kw_asm) && "Not an asm!"); 16970b57cec5SDimitry Andric SourceLocation Loc = ConsumeToken(); 16980b57cec5SDimitry Andric 16995ffd83dbSDimitry Andric if (isGNUAsmQualifier(Tok)) { 17005ffd83dbSDimitry Andric // Remove from the end of 'asm' to the end of the asm qualifier. 17010b57cec5SDimitry Andric SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 17020b57cec5SDimitry Andric PP.getLocForEndOfToken(Tok.getLocation())); 17035ffd83dbSDimitry Andric Diag(Tok, diag::err_global_asm_qualifier_ignored) 17045ffd83dbSDimitry Andric << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok)) 17050b57cec5SDimitry Andric << FixItHint::CreateRemoval(RemovalRange); 17060b57cec5SDimitry Andric ConsumeToken(); 17070b57cec5SDimitry Andric } 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 17100b57cec5SDimitry Andric if (T.consumeOpen()) { 17110b57cec5SDimitry Andric Diag(Tok, diag::err_expected_lparen_after) << "asm"; 17120b57cec5SDimitry Andric return ExprError(); 17130b57cec5SDimitry Andric } 17140b57cec5SDimitry Andric 1715480093f4SDimitry Andric ExprResult Result(ParseAsmStringLiteral(ForAsmLabel)); 17160b57cec5SDimitry Andric 17170b57cec5SDimitry Andric if (!Result.isInvalid()) { 17180b57cec5SDimitry Andric // Close the paren and get the location of the end bracket 17190b57cec5SDimitry Andric T.consumeClose(); 17200b57cec5SDimitry Andric if (EndLoc) 17210b57cec5SDimitry Andric *EndLoc = T.getCloseLocation(); 17220b57cec5SDimitry Andric } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { 17230b57cec5SDimitry Andric if (EndLoc) 17240b57cec5SDimitry Andric *EndLoc = Tok.getLocation(); 17250b57cec5SDimitry Andric ConsumeParen(); 17260b57cec5SDimitry Andric } 17270b57cec5SDimitry Andric 17280b57cec5SDimitry Andric return Result; 17290b57cec5SDimitry Andric } 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric /// Get the TemplateIdAnnotation from the token and put it in the 17320b57cec5SDimitry Andric /// cleanup pool so that it gets destroyed when parsing the current top level 17330b57cec5SDimitry Andric /// declaration is finished. 17340b57cec5SDimitry Andric TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) { 17350b57cec5SDimitry Andric assert(tok.is(tok::annot_template_id) && "Expected template-id token"); 17360b57cec5SDimitry Andric TemplateIdAnnotation * 17370b57cec5SDimitry Andric Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue()); 17380b57cec5SDimitry Andric return Id; 17390b57cec5SDimitry Andric } 17400b57cec5SDimitry Andric 17410b57cec5SDimitry Andric void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) { 17420b57cec5SDimitry Andric // Push the current token back into the token stream (or revert it if it is 17430b57cec5SDimitry Andric // cached) and use an annotation scope token for current token. 17440b57cec5SDimitry Andric if (PP.isBacktrackEnabled()) 17450b57cec5SDimitry Andric PP.RevertCachedTokens(1); 17460b57cec5SDimitry Andric else 17470b57cec5SDimitry Andric PP.EnterToken(Tok, /*IsReinject=*/true); 17480b57cec5SDimitry Andric Tok.setKind(tok::annot_cxxscope); 17490b57cec5SDimitry Andric Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 17500b57cec5SDimitry Andric Tok.setAnnotationRange(SS.getRange()); 17510b57cec5SDimitry Andric 17520b57cec5SDimitry Andric // In case the tokens were cached, have Preprocessor replace them 17530b57cec5SDimitry Andric // with the annotation token. We don't need to do this if we've 17540b57cec5SDimitry Andric // just reverted back to a prior state. 17550b57cec5SDimitry Andric if (IsNewAnnotation) 17560b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 17570b57cec5SDimitry Andric } 17580b57cec5SDimitry Andric 17590b57cec5SDimitry Andric /// Attempt to classify the name at the current token position. This may 17600b57cec5SDimitry Andric /// form a type, scope or primary expression annotation, or replace the token 17610b57cec5SDimitry Andric /// with a typo-corrected keyword. This is only appropriate when the current 17620b57cec5SDimitry Andric /// name must refer to an entity which has already been declared. 17630b57cec5SDimitry Andric /// 17640b57cec5SDimitry Andric /// \param CCC Indicates how to perform typo-correction for this name. If NULL, 17650b57cec5SDimitry Andric /// no typo correction will be performed. 1766bdd1243dSDimitry Andric /// \param AllowImplicitTypename Whether we are in a context where a dependent 1767bdd1243dSDimitry Andric /// nested-name-specifier without typename is treated as a type (e.g. 1768bdd1243dSDimitry Andric /// T::type). 17690b57cec5SDimitry Andric Parser::AnnotatedNameKind 1770bdd1243dSDimitry Andric Parser::TryAnnotateName(CorrectionCandidateCallback *CCC, 1771bdd1243dSDimitry Andric ImplicitTypenameContext AllowImplicitTypename) { 17720b57cec5SDimitry Andric assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope)); 17730b57cec5SDimitry Andric 17740b57cec5SDimitry Andric const bool EnteringContext = false; 17750b57cec5SDimitry Andric const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric CXXScopeSpec SS; 17780b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 17795ffd83dbSDimitry Andric ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 178004eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 17815ffd83dbSDimitry Andric EnteringContext)) 17820b57cec5SDimitry Andric return ANK_Error; 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric if (Tok.isNot(tok::identifier) || SS.isInvalid()) { 1785bdd1243dSDimitry Andric if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 1786bdd1243dSDimitry Andric AllowImplicitTypename)) 17870b57cec5SDimitry Andric return ANK_Error; 17880b57cec5SDimitry Andric return ANK_Unresolved; 17890b57cec5SDimitry Andric } 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric IdentifierInfo *Name = Tok.getIdentifierInfo(); 17920b57cec5SDimitry Andric SourceLocation NameLoc = Tok.getLocation(); 17930b57cec5SDimitry Andric 17940b57cec5SDimitry Andric // FIXME: Move the tentative declaration logic into ClassifyName so we can 17950b57cec5SDimitry Andric // typo-correct to tentatively-declared identifiers. 1796bdd1243dSDimitry Andric if (isTentativelyDeclared(Name) && SS.isEmpty()) { 17970b57cec5SDimitry Andric // Identifier has been tentatively declared, and thus cannot be resolved as 17980b57cec5SDimitry Andric // an expression. Fall back to annotating it as a type. 1799bdd1243dSDimitry Andric if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 1800bdd1243dSDimitry Andric AllowImplicitTypename)) 18010b57cec5SDimitry Andric return ANK_Error; 18020b57cec5SDimitry Andric return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl; 18030b57cec5SDimitry Andric } 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric Token Next = NextToken(); 18060b57cec5SDimitry Andric 18070b57cec5SDimitry Andric // Look up and classify the identifier. We don't perform any typo-correction 18080b57cec5SDimitry Andric // after a scope specifier, because in general we can't recover from typos 18090b57cec5SDimitry Andric // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to 18100b57cec5SDimitry Andric // jump back into scope specifier parsing). 1811a7dea167SDimitry Andric Sema::NameClassification Classification = Actions.ClassifyName( 1812a7dea167SDimitry Andric getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr); 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric // If name lookup found nothing and we guessed that this was a template name, 18150b57cec5SDimitry Andric // double-check before committing to that interpretation. C++20 requires that 18160b57cec5SDimitry Andric // we interpret this as a template-id if it can be, but if it can't be, then 18170b57cec5SDimitry Andric // this is an error recovery case. 18180b57cec5SDimitry Andric if (Classification.getKind() == Sema::NC_UndeclaredTemplate && 18190b57cec5SDimitry Andric isTemplateArgumentList(1) == TPResult::False) { 18200b57cec5SDimitry Andric // It's not a template-id; re-classify without the '<' as a hint. 18210b57cec5SDimitry Andric Token FakeNext = Next; 18220b57cec5SDimitry Andric FakeNext.setKind(tok::unknown); 18230b57cec5SDimitry Andric Classification = 18240b57cec5SDimitry Andric Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext, 1825a7dea167SDimitry Andric SS.isEmpty() ? CCC : nullptr); 18260b57cec5SDimitry Andric } 18270b57cec5SDimitry Andric 18280b57cec5SDimitry Andric switch (Classification.getKind()) { 18290b57cec5SDimitry Andric case Sema::NC_Error: 18300b57cec5SDimitry Andric return ANK_Error; 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric case Sema::NC_Keyword: 18330b57cec5SDimitry Andric // The identifier was typo-corrected to a keyword. 18340b57cec5SDimitry Andric Tok.setIdentifierInfo(Name); 18350b57cec5SDimitry Andric Tok.setKind(Name->getTokenID()); 18360b57cec5SDimitry Andric PP.TypoCorrectToken(Tok); 18370b57cec5SDimitry Andric if (SS.isNotEmpty()) 18380b57cec5SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 18390b57cec5SDimitry Andric // We've "annotated" this as a keyword. 18400b57cec5SDimitry Andric return ANK_Success; 18410b57cec5SDimitry Andric 18420b57cec5SDimitry Andric case Sema::NC_Unknown: 18430b57cec5SDimitry Andric // It's not something we know about. Leave it unannotated. 18440b57cec5SDimitry Andric break; 18450b57cec5SDimitry Andric 18460b57cec5SDimitry Andric case Sema::NC_Type: { 1847fe6060f1SDimitry Andric if (TryAltiVecVectorToken()) 1848fe6060f1SDimitry Andric // vector has been found as a type id when altivec is enabled but 1849fe6060f1SDimitry Andric // this is followed by a declaration specifier so this is really the 1850fe6060f1SDimitry Andric // altivec vector token. Leave it unannotated. 1851fe6060f1SDimitry Andric break; 18520b57cec5SDimitry Andric SourceLocation BeginLoc = NameLoc; 18530b57cec5SDimitry Andric if (SS.isNotEmpty()) 18540b57cec5SDimitry Andric BeginLoc = SS.getBeginLoc(); 18550b57cec5SDimitry Andric 18560b57cec5SDimitry Andric /// An Objective-C object type followed by '<' is a specialization of 18570b57cec5SDimitry Andric /// a parameterized class type or a protocol-qualified type. 18580b57cec5SDimitry Andric ParsedType Ty = Classification.getType(); 18590b57cec5SDimitry Andric if (getLangOpts().ObjC && NextToken().is(tok::less) && 18600b57cec5SDimitry Andric (Ty.get()->isObjCObjectType() || 18610b57cec5SDimitry Andric Ty.get()->isObjCObjectPointerType())) { 18620b57cec5SDimitry Andric // Consume the name. 18630b57cec5SDimitry Andric SourceLocation IdentifierLoc = ConsumeToken(); 18640b57cec5SDimitry Andric SourceLocation NewEndLoc; 18650b57cec5SDimitry Andric TypeResult NewType 18660b57cec5SDimitry Andric = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty, 18670b57cec5SDimitry Andric /*consumeLastToken=*/false, 18680b57cec5SDimitry Andric NewEndLoc); 18690b57cec5SDimitry Andric if (NewType.isUsable()) 18700b57cec5SDimitry Andric Ty = NewType.get(); 18710b57cec5SDimitry Andric else if (Tok.is(tok::eof)) // Nothing to do here, bail out... 18720b57cec5SDimitry Andric return ANK_Error; 18730b57cec5SDimitry Andric } 18740b57cec5SDimitry Andric 18750b57cec5SDimitry Andric Tok.setKind(tok::annot_typename); 18760b57cec5SDimitry Andric setTypeAnnotation(Tok, Ty); 18770b57cec5SDimitry Andric Tok.setAnnotationEndLoc(Tok.getLocation()); 18780b57cec5SDimitry Andric Tok.setLocation(BeginLoc); 18790b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 18800b57cec5SDimitry Andric return ANK_Success; 18810b57cec5SDimitry Andric } 18820b57cec5SDimitry Andric 1883e8d8bef9SDimitry Andric case Sema::NC_OverloadSet: 1884e8d8bef9SDimitry Andric Tok.setKind(tok::annot_overload_set); 18850b57cec5SDimitry Andric setExprAnnotation(Tok, Classification.getExpression()); 18860b57cec5SDimitry Andric Tok.setAnnotationEndLoc(NameLoc); 18870b57cec5SDimitry Andric if (SS.isNotEmpty()) 18880b57cec5SDimitry Andric Tok.setLocation(SS.getBeginLoc()); 18890b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 18900b57cec5SDimitry Andric return ANK_Success; 18910b57cec5SDimitry Andric 1892a7dea167SDimitry Andric case Sema::NC_NonType: 1893fe6060f1SDimitry Andric if (TryAltiVecVectorToken()) 1894fe6060f1SDimitry Andric // vector has been found as a non-type id when altivec is enabled but 1895fe6060f1SDimitry Andric // this is followed by a declaration specifier so this is really the 1896fe6060f1SDimitry Andric // altivec vector token. Leave it unannotated. 1897fe6060f1SDimitry Andric break; 1898a7dea167SDimitry Andric Tok.setKind(tok::annot_non_type); 1899a7dea167SDimitry Andric setNonTypeAnnotation(Tok, Classification.getNonTypeDecl()); 1900a7dea167SDimitry Andric Tok.setLocation(NameLoc); 1901a7dea167SDimitry Andric Tok.setAnnotationEndLoc(NameLoc); 1902a7dea167SDimitry Andric PP.AnnotateCachedTokens(Tok); 1903a7dea167SDimitry Andric if (SS.isNotEmpty()) 1904a7dea167SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 1905a7dea167SDimitry Andric return ANK_Success; 1906a7dea167SDimitry Andric 1907a7dea167SDimitry Andric case Sema::NC_UndeclaredNonType: 1908a7dea167SDimitry Andric case Sema::NC_DependentNonType: 1909a7dea167SDimitry Andric Tok.setKind(Classification.getKind() == Sema::NC_UndeclaredNonType 1910a7dea167SDimitry Andric ? tok::annot_non_type_undeclared 1911a7dea167SDimitry Andric : tok::annot_non_type_dependent); 1912a7dea167SDimitry Andric setIdentifierAnnotation(Tok, Name); 1913a7dea167SDimitry Andric Tok.setLocation(NameLoc); 1914a7dea167SDimitry Andric Tok.setAnnotationEndLoc(NameLoc); 1915a7dea167SDimitry Andric PP.AnnotateCachedTokens(Tok); 1916a7dea167SDimitry Andric if (SS.isNotEmpty()) 1917a7dea167SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 1918a7dea167SDimitry Andric return ANK_Success; 1919a7dea167SDimitry Andric 19200b57cec5SDimitry Andric case Sema::NC_TypeTemplate: 19210b57cec5SDimitry Andric if (Next.isNot(tok::less)) { 19220b57cec5SDimitry Andric // This may be a type template being used as a template template argument. 19230b57cec5SDimitry Andric if (SS.isNotEmpty()) 19240b57cec5SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 19250b57cec5SDimitry Andric return ANK_TemplateName; 19260b57cec5SDimitry Andric } 1927bdd1243dSDimitry Andric [[fallthrough]]; 192806c3fb27SDimitry Andric case Sema::NC_Concept: 19290b57cec5SDimitry Andric case Sema::NC_VarTemplate: 19300b57cec5SDimitry Andric case Sema::NC_FunctionTemplate: 19310b57cec5SDimitry Andric case Sema::NC_UndeclaredTemplate: { 193206c3fb27SDimitry Andric bool IsConceptName = Classification.getKind() == Sema::NC_Concept; 193306c3fb27SDimitry Andric // We have a template name followed by '<'. Consume the identifier token so 193455e4f9d5SDimitry Andric // we reach the '<' and annotate it. 193506c3fb27SDimitry Andric if (Next.is(tok::less)) 193655e4f9d5SDimitry Andric ConsumeToken(); 193706c3fb27SDimitry Andric UnqualifiedId Id; 193806c3fb27SDimitry Andric Id.setIdentifier(Name, NameLoc); 193955e4f9d5SDimitry Andric if (AnnotateTemplateIdToken( 194055e4f9d5SDimitry Andric TemplateTy::make(Classification.getTemplateName()), 194155e4f9d5SDimitry Andric Classification.getTemplateNameKind(), SS, SourceLocation(), Id, 194206c3fb27SDimitry Andric /*AllowTypeAnnotation=*/!IsConceptName, 194306c3fb27SDimitry Andric /*TypeConstraint=*/IsConceptName)) 194455e4f9d5SDimitry Andric return ANK_Error; 194506c3fb27SDimitry Andric if (SS.isNotEmpty()) 194606c3fb27SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 194755e4f9d5SDimitry Andric return ANK_Success; 194855e4f9d5SDimitry Andric } 19490b57cec5SDimitry Andric } 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric // Unable to classify the name, but maybe we can annotate a scope specifier. 19520b57cec5SDimitry Andric if (SS.isNotEmpty()) 19530b57cec5SDimitry Andric AnnotateScopeToken(SS, !WasScopeAnnotation); 19540b57cec5SDimitry Andric return ANK_Unresolved; 19550b57cec5SDimitry Andric } 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric bool Parser::TryKeywordIdentFallback(bool DisableKeyword) { 19580b57cec5SDimitry Andric assert(Tok.isNot(tok::identifier)); 19590b57cec5SDimitry Andric Diag(Tok, diag::ext_keyword_as_ident) 19600b57cec5SDimitry Andric << PP.getSpelling(Tok) 19610b57cec5SDimitry Andric << DisableKeyword; 19620b57cec5SDimitry Andric if (DisableKeyword) 19630b57cec5SDimitry Andric Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); 19640b57cec5SDimitry Andric Tok.setKind(tok::identifier); 19650b57cec5SDimitry Andric return true; 19660b57cec5SDimitry Andric } 19670b57cec5SDimitry Andric 19680b57cec5SDimitry Andric /// TryAnnotateTypeOrScopeToken - If the current token position is on a 19690b57cec5SDimitry Andric /// typename (possibly qualified in C++) or a C++ scope specifier not followed 19700b57cec5SDimitry Andric /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens 19710b57cec5SDimitry Andric /// with a single annotation token representing the typename or C++ scope 19720b57cec5SDimitry Andric /// respectively. 19730b57cec5SDimitry Andric /// This simplifies handling of C++ scope specifiers and allows efficient 19740b57cec5SDimitry Andric /// backtracking without the need to re-parse and resolve nested-names and 19750b57cec5SDimitry Andric /// typenames. 19760b57cec5SDimitry Andric /// It will mainly be called when we expect to treat identifiers as typenames 19770b57cec5SDimitry Andric /// (if they are typenames). For example, in C we do not expect identifiers 19780b57cec5SDimitry Andric /// inside expressions to be treated as typenames so it will not be called 19790b57cec5SDimitry Andric /// for expressions in C. 19800b57cec5SDimitry Andric /// The benefit for C/ObjC is that a typename will be annotated and 19810b57cec5SDimitry Andric /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName 19820b57cec5SDimitry Andric /// will not be called twice, once to check whether we have a declaration 19830b57cec5SDimitry Andric /// specifier, and another one to get the actual type inside 19840b57cec5SDimitry Andric /// ParseDeclarationSpecifiers). 19850b57cec5SDimitry Andric /// 19860b57cec5SDimitry Andric /// This returns true if an error occurred. 19870b57cec5SDimitry Andric /// 19880b57cec5SDimitry Andric /// Note that this routine emits an error if you call it with ::new or ::delete 19890b57cec5SDimitry Andric /// as the current tokens, so only call it in contexts where these are invalid. 1990bdd1243dSDimitry Andric bool Parser::TryAnnotateTypeOrScopeToken( 1991bdd1243dSDimitry Andric ImplicitTypenameContext AllowImplicitTypename) { 19920b57cec5SDimitry Andric assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 19930b57cec5SDimitry Andric Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) || 19940b57cec5SDimitry Andric Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) || 199506c3fb27SDimitry Andric Tok.is(tok::kw___super) || Tok.is(tok::kw_auto)) && 19960b57cec5SDimitry Andric "Cannot be a type or scope token!"); 19970b57cec5SDimitry Andric 19980b57cec5SDimitry Andric if (Tok.is(tok::kw_typename)) { 19990b57cec5SDimitry Andric // MSVC lets you do stuff like: 20000b57cec5SDimitry Andric // typename typedef T_::D D; 20010b57cec5SDimitry Andric // 20020b57cec5SDimitry Andric // We will consume the typedef token here and put it back after we have 20030b57cec5SDimitry Andric // parsed the first identifier, transforming it into something more like: 20040b57cec5SDimitry Andric // typename T_::D typedef D; 20050b57cec5SDimitry Andric if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) { 20060b57cec5SDimitry Andric Token TypedefToken; 20070b57cec5SDimitry Andric PP.Lex(TypedefToken); 2008bdd1243dSDimitry Andric bool Result = TryAnnotateTypeOrScopeToken(AllowImplicitTypename); 20090b57cec5SDimitry Andric PP.EnterToken(Tok, /*IsReinject=*/true); 20100b57cec5SDimitry Andric Tok = TypedefToken; 20110b57cec5SDimitry Andric if (!Result) 20120b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename); 20130b57cec5SDimitry Andric return Result; 20140b57cec5SDimitry Andric } 20150b57cec5SDimitry Andric 20160b57cec5SDimitry Andric // Parse a C++ typename-specifier, e.g., "typename T::type". 20170b57cec5SDimitry Andric // 20180b57cec5SDimitry Andric // typename-specifier: 20190b57cec5SDimitry Andric // 'typename' '::' [opt] nested-name-specifier identifier 20200b57cec5SDimitry Andric // 'typename' '::' [opt] nested-name-specifier template [opt] 20210b57cec5SDimitry Andric // simple-template-id 20220b57cec5SDimitry Andric SourceLocation TypenameLoc = ConsumeToken(); 20230b57cec5SDimitry Andric CXXScopeSpec SS; 20240b57cec5SDimitry Andric if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 202504eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 20260b57cec5SDimitry Andric /*EnteringContext=*/false, nullptr, 20270b57cec5SDimitry Andric /*IsTypename*/ true)) 20280b57cec5SDimitry Andric return true; 202955e4f9d5SDimitry Andric if (SS.isEmpty()) { 20300b57cec5SDimitry Andric if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) || 20310b57cec5SDimitry Andric Tok.is(tok::annot_decltype)) { 20320b57cec5SDimitry Andric // Attempt to recover by skipping the invalid 'typename' 20330b57cec5SDimitry Andric if (Tok.is(tok::annot_decltype) || 2034bdd1243dSDimitry Andric (!TryAnnotateTypeOrScopeToken(AllowImplicitTypename) && 2035bdd1243dSDimitry Andric Tok.isAnnotation())) { 20360b57cec5SDimitry Andric unsigned DiagID = diag::err_expected_qualified_after_typename; 20370b57cec5SDimitry Andric // MS compatibility: MSVC permits using known types with typename. 20380b57cec5SDimitry Andric // e.g. "typedef typename T* pointer_type" 20390b57cec5SDimitry Andric if (getLangOpts().MicrosoftExt) 20400b57cec5SDimitry Andric DiagID = diag::warn_expected_qualified_after_typename; 20410b57cec5SDimitry Andric Diag(Tok.getLocation(), DiagID); 20420b57cec5SDimitry Andric return false; 20430b57cec5SDimitry Andric } 20440b57cec5SDimitry Andric } 20450b57cec5SDimitry Andric if (Tok.isEditorPlaceholder()) 20460b57cec5SDimitry Andric return true; 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 20490b57cec5SDimitry Andric return true; 20500b57cec5SDimitry Andric } 20510b57cec5SDimitry Andric 20520b57cec5SDimitry Andric TypeResult Ty; 20530b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 20540b57cec5SDimitry Andric // FIXME: check whether the next token is '<', first! 20550b57cec5SDimitry Andric Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 20560b57cec5SDimitry Andric *Tok.getIdentifierInfo(), 20570b57cec5SDimitry Andric Tok.getLocation()); 20580b57cec5SDimitry Andric } else if (Tok.is(tok::annot_template_id)) { 20590b57cec5SDimitry Andric TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 20605ffd83dbSDimitry Andric if (!TemplateId->mightBeType()) { 20610b57cec5SDimitry Andric Diag(Tok, diag::err_typename_refers_to_non_type_template) 20620b57cec5SDimitry Andric << Tok.getAnnotationRange(); 20630b57cec5SDimitry Andric return true; 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 20670b57cec5SDimitry Andric TemplateId->NumArgs); 20680b57cec5SDimitry Andric 20695ffd83dbSDimitry Andric Ty = TemplateId->isInvalid() 20705ffd83dbSDimitry Andric ? TypeError() 20715ffd83dbSDimitry Andric : Actions.ActOnTypenameType( 20725ffd83dbSDimitry Andric getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc, 20735ffd83dbSDimitry Andric TemplateId->Template, TemplateId->Name, 20745ffd83dbSDimitry Andric TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, 20755ffd83dbSDimitry Andric TemplateArgsPtr, TemplateId->RAngleLoc); 20760b57cec5SDimitry Andric } else { 20770b57cec5SDimitry Andric Diag(Tok, diag::err_expected_type_name_after_typename) 20780b57cec5SDimitry Andric << SS.getRange(); 20790b57cec5SDimitry Andric return true; 20800b57cec5SDimitry Andric } 20810b57cec5SDimitry Andric 20820b57cec5SDimitry Andric SourceLocation EndLoc = Tok.getLastLoc(); 20830b57cec5SDimitry Andric Tok.setKind(tok::annot_typename); 20845ffd83dbSDimitry Andric setTypeAnnotation(Tok, Ty); 20850b57cec5SDimitry Andric Tok.setAnnotationEndLoc(EndLoc); 20860b57cec5SDimitry Andric Tok.setLocation(TypenameLoc); 20870b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 20880b57cec5SDimitry Andric return false; 20890b57cec5SDimitry Andric } 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric // Remembers whether the token was originally a scope annotation. 20920b57cec5SDimitry Andric bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 20930b57cec5SDimitry Andric 20940b57cec5SDimitry Andric CXXScopeSpec SS; 20950b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 20965ffd83dbSDimitry Andric if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 209704eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 20985ffd83dbSDimitry Andric /*EnteringContext*/ false)) 20990b57cec5SDimitry Andric return true; 21000b57cec5SDimitry Andric 2101bdd1243dSDimitry Andric return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation, 2102bdd1243dSDimitry Andric AllowImplicitTypename); 21030b57cec5SDimitry Andric } 21040b57cec5SDimitry Andric 21050b57cec5SDimitry Andric /// Try to annotate a type or scope token, having already parsed an 21060b57cec5SDimitry Andric /// optional scope specifier. \p IsNewScope should be \c true unless the scope 21070b57cec5SDimitry Andric /// specifier was extracted from an existing tok::annot_cxxscope annotation. 2108bdd1243dSDimitry Andric bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec( 2109bdd1243dSDimitry Andric CXXScopeSpec &SS, bool IsNewScope, 2110bdd1243dSDimitry Andric ImplicitTypenameContext AllowImplicitTypename) { 21110b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 21120b57cec5SDimitry Andric // Determine whether the identifier is a type name. 21130b57cec5SDimitry Andric if (ParsedType Ty = Actions.getTypeName( 21140b57cec5SDimitry Andric *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS, 21150b57cec5SDimitry Andric false, NextToken().is(tok::period), nullptr, 21160b57cec5SDimitry Andric /*IsCtorOrDtorName=*/false, 2117bdd1243dSDimitry Andric /*NonTrivialTypeSourceInfo=*/true, 2118bdd1243dSDimitry Andric /*IsClassTemplateDeductionContext=*/true, AllowImplicitTypename)) { 21190b57cec5SDimitry Andric SourceLocation BeginLoc = Tok.getLocation(); 21200b57cec5SDimitry Andric if (SS.isNotEmpty()) // it was a C++ qualified type name. 21210b57cec5SDimitry Andric BeginLoc = SS.getBeginLoc(); 21220b57cec5SDimitry Andric 21230b57cec5SDimitry Andric /// An Objective-C object type followed by '<' is a specialization of 21240b57cec5SDimitry Andric /// a parameterized class type or a protocol-qualified type. 21250b57cec5SDimitry Andric if (getLangOpts().ObjC && NextToken().is(tok::less) && 21260b57cec5SDimitry Andric (Ty.get()->isObjCObjectType() || 21270b57cec5SDimitry Andric Ty.get()->isObjCObjectPointerType())) { 21280b57cec5SDimitry Andric // Consume the name. 21290b57cec5SDimitry Andric SourceLocation IdentifierLoc = ConsumeToken(); 21300b57cec5SDimitry Andric SourceLocation NewEndLoc; 21310b57cec5SDimitry Andric TypeResult NewType 21320b57cec5SDimitry Andric = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty, 21330b57cec5SDimitry Andric /*consumeLastToken=*/false, 21340b57cec5SDimitry Andric NewEndLoc); 21350b57cec5SDimitry Andric if (NewType.isUsable()) 21360b57cec5SDimitry Andric Ty = NewType.get(); 21370b57cec5SDimitry Andric else if (Tok.is(tok::eof)) // Nothing to do here, bail out... 21380b57cec5SDimitry Andric return false; 21390b57cec5SDimitry Andric } 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric // This is a typename. Replace the current token in-place with an 21420b57cec5SDimitry Andric // annotation type token. 21430b57cec5SDimitry Andric Tok.setKind(tok::annot_typename); 21440b57cec5SDimitry Andric setTypeAnnotation(Tok, Ty); 21450b57cec5SDimitry Andric Tok.setAnnotationEndLoc(Tok.getLocation()); 21460b57cec5SDimitry Andric Tok.setLocation(BeginLoc); 21470b57cec5SDimitry Andric 21480b57cec5SDimitry Andric // In case the tokens were cached, have Preprocessor replace 21490b57cec5SDimitry Andric // them with the annotation token. 21500b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 21510b57cec5SDimitry Andric return false; 21520b57cec5SDimitry Andric } 21530b57cec5SDimitry Andric 21540b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus) { 21555f757f3fSDimitry Andric // If we're in C, the only place we can have :: tokens is C23 2156bdd1243dSDimitry Andric // attribute which is parsed elsewhere. If the identifier is not a type, 2157bdd1243dSDimitry Andric // then it can't be scope either, just early exit. 21580b57cec5SDimitry Andric return false; 21590b57cec5SDimitry Andric } 21600b57cec5SDimitry Andric 21610b57cec5SDimitry Andric // If this is a template-id, annotate with a template-id or type token. 21620b57cec5SDimitry Andric // FIXME: This appears to be dead code. We already have formed template-id 21630b57cec5SDimitry Andric // tokens when parsing the scope specifier; this can never form a new one. 21640b57cec5SDimitry Andric if (NextToken().is(tok::less)) { 21650b57cec5SDimitry Andric TemplateTy Template; 21660b57cec5SDimitry Andric UnqualifiedId TemplateName; 21670b57cec5SDimitry Andric TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 21680b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 21690b57cec5SDimitry Andric if (TemplateNameKind TNK = Actions.isTemplateName( 21700b57cec5SDimitry Andric getCurScope(), SS, 21710b57cec5SDimitry Andric /*hasTemplateKeyword=*/false, TemplateName, 21720b57cec5SDimitry Andric /*ObjectType=*/nullptr, /*EnteringContext*/false, Template, 21730b57cec5SDimitry Andric MemberOfUnknownSpecialization)) { 21740b57cec5SDimitry Andric // Only annotate an undeclared template name as a template-id if the 21750b57cec5SDimitry Andric // following tokens have the form of a template argument list. 21760b57cec5SDimitry Andric if (TNK != TNK_Undeclared_template || 21770b57cec5SDimitry Andric isTemplateArgumentList(1) != TPResult::False) { 21780b57cec5SDimitry Andric // Consume the identifier. 21790b57cec5SDimitry Andric ConsumeToken(); 21800b57cec5SDimitry Andric if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 21810b57cec5SDimitry Andric TemplateName)) { 21820b57cec5SDimitry Andric // If an unrecoverable error occurred, we need to return true here, 21830b57cec5SDimitry Andric // because the token stream is in a damaged state. We may not 21840b57cec5SDimitry Andric // return a valid identifier. 21850b57cec5SDimitry Andric return true; 21860b57cec5SDimitry Andric } 21870b57cec5SDimitry Andric } 21880b57cec5SDimitry Andric } 21890b57cec5SDimitry Andric } 21900b57cec5SDimitry Andric 21910b57cec5SDimitry Andric // The current token, which is either an identifier or a 21920b57cec5SDimitry Andric // template-id, is not part of the annotation. Fall through to 21930b57cec5SDimitry Andric // push that token back into the stream and complete the C++ scope 21940b57cec5SDimitry Andric // specifier annotation. 21950b57cec5SDimitry Andric } 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric if (Tok.is(tok::annot_template_id)) { 21980b57cec5SDimitry Andric TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 21990b57cec5SDimitry Andric if (TemplateId->Kind == TNK_Type_template) { 22000b57cec5SDimitry Andric // A template-id that refers to a type was parsed into a 22010b57cec5SDimitry Andric // template-id annotation in a context where we weren't allowed 22020b57cec5SDimitry Andric // to produce a type annotation token. Update the template-id 22030b57cec5SDimitry Andric // annotation token to a type annotation token now. 2204bdd1243dSDimitry Andric AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename); 22050b57cec5SDimitry Andric return false; 22060b57cec5SDimitry Andric } 22070b57cec5SDimitry Andric } 22080b57cec5SDimitry Andric 22090b57cec5SDimitry Andric if (SS.isEmpty()) 22100b57cec5SDimitry Andric return false; 22110b57cec5SDimitry Andric 22120b57cec5SDimitry Andric // A C++ scope specifier that isn't followed by a typename. 22130b57cec5SDimitry Andric AnnotateScopeToken(SS, IsNewScope); 22140b57cec5SDimitry Andric return false; 22150b57cec5SDimitry Andric } 22160b57cec5SDimitry Andric 22170b57cec5SDimitry Andric /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only 22180b57cec5SDimitry Andric /// annotates C++ scope specifiers and template-ids. This returns 22190b57cec5SDimitry Andric /// true if there was an error that could not be recovered from. 22200b57cec5SDimitry Andric /// 22210b57cec5SDimitry Andric /// Note that this routine emits an error if you call it with ::new or ::delete 22220b57cec5SDimitry Andric /// as the current tokens, so only call it in contexts where these are invalid. 22230b57cec5SDimitry Andric bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 22240b57cec5SDimitry Andric assert(getLangOpts().CPlusPlus && 22250b57cec5SDimitry Andric "Call sites of this function should be guarded by checking for C++"); 222655e4f9d5SDimitry Andric assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!"); 22270b57cec5SDimitry Andric 22280b57cec5SDimitry Andric CXXScopeSpec SS; 22295ffd83dbSDimitry Andric if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 223004eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 22315ffd83dbSDimitry Andric EnteringContext)) 22320b57cec5SDimitry Andric return true; 22330b57cec5SDimitry Andric if (SS.isEmpty()) 22340b57cec5SDimitry Andric return false; 22350b57cec5SDimitry Andric 22360b57cec5SDimitry Andric AnnotateScopeToken(SS, true); 22370b57cec5SDimitry Andric return false; 22380b57cec5SDimitry Andric } 22390b57cec5SDimitry Andric 22400b57cec5SDimitry Andric bool Parser::isTokenEqualOrEqualTypo() { 22410b57cec5SDimitry Andric tok::TokenKind Kind = Tok.getKind(); 22420b57cec5SDimitry Andric switch (Kind) { 22430b57cec5SDimitry Andric default: 22440b57cec5SDimitry Andric return false; 22450b57cec5SDimitry Andric case tok::ampequal: // &= 22460b57cec5SDimitry Andric case tok::starequal: // *= 22470b57cec5SDimitry Andric case tok::plusequal: // += 22480b57cec5SDimitry Andric case tok::minusequal: // -= 22490b57cec5SDimitry Andric case tok::exclaimequal: // != 22500b57cec5SDimitry Andric case tok::slashequal: // /= 22510b57cec5SDimitry Andric case tok::percentequal: // %= 22520b57cec5SDimitry Andric case tok::lessequal: // <= 22530b57cec5SDimitry Andric case tok::lesslessequal: // <<= 22540b57cec5SDimitry Andric case tok::greaterequal: // >= 22550b57cec5SDimitry Andric case tok::greatergreaterequal: // >>= 22560b57cec5SDimitry Andric case tok::caretequal: // ^= 22570b57cec5SDimitry Andric case tok::pipeequal: // |= 22580b57cec5SDimitry Andric case tok::equalequal: // == 22590b57cec5SDimitry Andric Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal) 22600b57cec5SDimitry Andric << Kind 22610b57cec5SDimitry Andric << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "="); 2262bdd1243dSDimitry Andric [[fallthrough]]; 22630b57cec5SDimitry Andric case tok::equal: 22640b57cec5SDimitry Andric return true; 22650b57cec5SDimitry Andric } 22660b57cec5SDimitry Andric } 22670b57cec5SDimitry Andric 22680b57cec5SDimitry Andric SourceLocation Parser::handleUnexpectedCodeCompletionToken() { 22690b57cec5SDimitry Andric assert(Tok.is(tok::code_completion)); 22700b57cec5SDimitry Andric PrevTokLocation = Tok.getLocation(); 22710b57cec5SDimitry Andric 22720b57cec5SDimitry Andric for (Scope *S = getCurScope(); S; S = S->getParent()) { 227381ad6265SDimitry Andric if (S->isFunctionScope()) { 2274fe6060f1SDimitry Andric cutOffParsing(); 22750b57cec5SDimitry Andric Actions.CodeCompleteOrdinaryName(getCurScope(), 22760b57cec5SDimitry Andric Sema::PCC_RecoveryInFunction); 22770b57cec5SDimitry Andric return PrevTokLocation; 22780b57cec5SDimitry Andric } 22790b57cec5SDimitry Andric 228081ad6265SDimitry Andric if (S->isClassScope()) { 22810b57cec5SDimitry Andric cutOffParsing(); 2282fe6060f1SDimitry Andric Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class); 22830b57cec5SDimitry Andric return PrevTokLocation; 22840b57cec5SDimitry Andric } 22850b57cec5SDimitry Andric } 22860b57cec5SDimitry Andric 22870b57cec5SDimitry Andric cutOffParsing(); 2288fe6060f1SDimitry Andric Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace); 22890b57cec5SDimitry Andric return PrevTokLocation; 22900b57cec5SDimitry Andric } 22910b57cec5SDimitry Andric 22920b57cec5SDimitry Andric // Code-completion pass-through functions 22930b57cec5SDimitry Andric 22940b57cec5SDimitry Andric void Parser::CodeCompleteDirective(bool InConditional) { 22950b57cec5SDimitry Andric Actions.CodeCompletePreprocessorDirective(InConditional); 22960b57cec5SDimitry Andric } 22970b57cec5SDimitry Andric 22980b57cec5SDimitry Andric void Parser::CodeCompleteInConditionalExclusion() { 22990b57cec5SDimitry Andric Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope()); 23000b57cec5SDimitry Andric } 23010b57cec5SDimitry Andric 23020b57cec5SDimitry Andric void Parser::CodeCompleteMacroName(bool IsDefinition) { 23030b57cec5SDimitry Andric Actions.CodeCompletePreprocessorMacroName(IsDefinition); 23040b57cec5SDimitry Andric } 23050b57cec5SDimitry Andric 23060b57cec5SDimitry Andric void Parser::CodeCompletePreprocessorExpression() { 23070b57cec5SDimitry Andric Actions.CodeCompletePreprocessorExpression(); 23080b57cec5SDimitry Andric } 23090b57cec5SDimitry Andric 23100b57cec5SDimitry Andric void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 23110b57cec5SDimitry Andric MacroInfo *MacroInfo, 23120b57cec5SDimitry Andric unsigned ArgumentIndex) { 23130b57cec5SDimitry Andric Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo, 23140b57cec5SDimitry Andric ArgumentIndex); 23150b57cec5SDimitry Andric } 23160b57cec5SDimitry Andric 23170b57cec5SDimitry Andric void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) { 23180b57cec5SDimitry Andric Actions.CodeCompleteIncludedFile(Dir, IsAngled); 23190b57cec5SDimitry Andric } 23200b57cec5SDimitry Andric 23210b57cec5SDimitry Andric void Parser::CodeCompleteNaturalLanguage() { 23220b57cec5SDimitry Andric Actions.CodeCompleteNaturalLanguage(); 23230b57cec5SDimitry Andric } 23240b57cec5SDimitry Andric 23250b57cec5SDimitry Andric bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) { 23260b57cec5SDimitry Andric assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) && 23270b57cec5SDimitry Andric "Expected '__if_exists' or '__if_not_exists'"); 23280b57cec5SDimitry Andric Result.IsIfExists = Tok.is(tok::kw___if_exists); 23290b57cec5SDimitry Andric Result.KeywordLoc = ConsumeToken(); 23300b57cec5SDimitry Andric 23310b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 23320b57cec5SDimitry Andric if (T.consumeOpen()) { 23330b57cec5SDimitry Andric Diag(Tok, diag::err_expected_lparen_after) 23340b57cec5SDimitry Andric << (Result.IsIfExists? "__if_exists" : "__if_not_exists"); 23350b57cec5SDimitry Andric return true; 23360b57cec5SDimitry Andric } 23370b57cec5SDimitry Andric 23380b57cec5SDimitry Andric // Parse nested-name-specifier. 23390b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 23405ffd83dbSDimitry Andric ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr, 234104eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 23420b57cec5SDimitry Andric /*EnteringContext=*/false); 23430b57cec5SDimitry Andric 23440b57cec5SDimitry Andric // Check nested-name specifier. 23450b57cec5SDimitry Andric if (Result.SS.isInvalid()) { 23460b57cec5SDimitry Andric T.skipToEnd(); 23470b57cec5SDimitry Andric return true; 23480b57cec5SDimitry Andric } 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric // Parse the unqualified-id. 23510b57cec5SDimitry Andric SourceLocation TemplateKWLoc; // FIXME: parsed, but unused. 23525ffd83dbSDimitry Andric if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr, 23535ffd83dbSDimitry Andric /*ObjectHadErrors=*/false, /*EnteringContext*/ false, 23545ffd83dbSDimitry Andric /*AllowDestructorName*/ true, 23555ffd83dbSDimitry Andric /*AllowConstructorName*/ true, 23565ffd83dbSDimitry Andric /*AllowDeductionGuide*/ false, &TemplateKWLoc, 23575ffd83dbSDimitry Andric Result.Name)) { 23580b57cec5SDimitry Andric T.skipToEnd(); 23590b57cec5SDimitry Andric return true; 23600b57cec5SDimitry Andric } 23610b57cec5SDimitry Andric 23620b57cec5SDimitry Andric if (T.consumeClose()) 23630b57cec5SDimitry Andric return true; 23640b57cec5SDimitry Andric 23650b57cec5SDimitry Andric // Check if the symbol exists. 23660b57cec5SDimitry Andric switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc, 23670b57cec5SDimitry Andric Result.IsIfExists, Result.SS, 23680b57cec5SDimitry Andric Result.Name)) { 23690b57cec5SDimitry Andric case Sema::IER_Exists: 23700b57cec5SDimitry Andric Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip; 23710b57cec5SDimitry Andric break; 23720b57cec5SDimitry Andric 23730b57cec5SDimitry Andric case Sema::IER_DoesNotExist: 23740b57cec5SDimitry Andric Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip; 23750b57cec5SDimitry Andric break; 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric case Sema::IER_Dependent: 23780b57cec5SDimitry Andric Result.Behavior = IEB_Dependent; 23790b57cec5SDimitry Andric break; 23800b57cec5SDimitry Andric 23810b57cec5SDimitry Andric case Sema::IER_Error: 23820b57cec5SDimitry Andric return true; 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric return false; 23860b57cec5SDimitry Andric } 23870b57cec5SDimitry Andric 23880b57cec5SDimitry Andric void Parser::ParseMicrosoftIfExistsExternalDeclaration() { 23890b57cec5SDimitry Andric IfExistsCondition Result; 23900b57cec5SDimitry Andric if (ParseMicrosoftIfExistsCondition(Result)) 23910b57cec5SDimitry Andric return; 23920b57cec5SDimitry Andric 23930b57cec5SDimitry Andric BalancedDelimiterTracker Braces(*this, tok::l_brace); 23940b57cec5SDimitry Andric if (Braces.consumeOpen()) { 23950b57cec5SDimitry Andric Diag(Tok, diag::err_expected) << tok::l_brace; 23960b57cec5SDimitry Andric return; 23970b57cec5SDimitry Andric } 23980b57cec5SDimitry Andric 23990b57cec5SDimitry Andric switch (Result.Behavior) { 24000b57cec5SDimitry Andric case IEB_Parse: 24010b57cec5SDimitry Andric // Parse declarations below. 24020b57cec5SDimitry Andric break; 24030b57cec5SDimitry Andric 24040b57cec5SDimitry Andric case IEB_Dependent: 24050b57cec5SDimitry Andric llvm_unreachable("Cannot have a dependent external declaration"); 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric case IEB_Skip: 24080b57cec5SDimitry Andric Braces.skipToEnd(); 24090b57cec5SDimitry Andric return; 24100b57cec5SDimitry Andric } 24110b57cec5SDimitry Andric 24120b57cec5SDimitry Andric // Parse the declarations. 24130b57cec5SDimitry Andric // FIXME: Support module import within __if_exists? 24140b57cec5SDimitry Andric while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 241581ad6265SDimitry Andric ParsedAttributes Attrs(AttrFactory); 241681ad6265SDimitry Andric MaybeParseCXX11Attributes(Attrs); 2417bdd1243dSDimitry Andric ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); 2418bdd1243dSDimitry Andric DeclGroupPtrTy Result = ParseExternalDeclaration(Attrs, EmptyDeclSpecAttrs); 24190b57cec5SDimitry Andric if (Result && !getCurScope()->getParent()) 24200b57cec5SDimitry Andric Actions.getASTConsumer().HandleTopLevelDecl(Result.get()); 24210b57cec5SDimitry Andric } 24220b57cec5SDimitry Andric Braces.consumeClose(); 24230b57cec5SDimitry Andric } 24240b57cec5SDimitry Andric 24250b57cec5SDimitry Andric /// Parse a declaration beginning with the 'module' keyword or C++20 24260b57cec5SDimitry Andric /// context-sensitive keyword (optionally preceded by 'export'). 24270b57cec5SDimitry Andric /// 242806c3fb27SDimitry Andric /// module-declaration: [C++20] 24290b57cec5SDimitry Andric /// 'export'[opt] 'module' module-name attribute-specifier-seq[opt] ';' 24300b57cec5SDimitry Andric /// 24310b57cec5SDimitry Andric /// global-module-fragment: [C++2a] 24320b57cec5SDimitry Andric /// 'module' ';' top-level-declaration-seq[opt] 24330b57cec5SDimitry Andric /// module-declaration: [C++2a] 24340b57cec5SDimitry Andric /// 'export'[opt] 'module' module-name module-partition[opt] 24350b57cec5SDimitry Andric /// attribute-specifier-seq[opt] ';' 24360b57cec5SDimitry Andric /// private-module-fragment: [C++2a] 24370b57cec5SDimitry Andric /// 'module' ':' 'private' ';' top-level-declaration-seq[opt] 243881ad6265SDimitry Andric Parser::DeclGroupPtrTy 243981ad6265SDimitry Andric Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) { 24400b57cec5SDimitry Andric SourceLocation StartLoc = Tok.getLocation(); 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export) 24430b57cec5SDimitry Andric ? Sema::ModuleDeclKind::Interface 24440b57cec5SDimitry Andric : Sema::ModuleDeclKind::Implementation; 24450b57cec5SDimitry Andric 24460b57cec5SDimitry Andric assert( 24470b57cec5SDimitry Andric (Tok.is(tok::kw_module) || 24480b57cec5SDimitry Andric (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) && 24490b57cec5SDimitry Andric "not a module declaration"); 24500b57cec5SDimitry Andric SourceLocation ModuleLoc = ConsumeToken(); 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric // Attributes appear after the module name, not before. 24530b57cec5SDimitry Andric // FIXME: Suggest moving the attributes later with a fixit. 24540b57cec5SDimitry Andric DiagnoseAndSkipCXX11Attributes(); 24550b57cec5SDimitry Andric 24560b57cec5SDimitry Andric // Parse a global-module-fragment, if present. 24570b57cec5SDimitry Andric if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) { 24580b57cec5SDimitry Andric SourceLocation SemiLoc = ConsumeToken(); 245981ad6265SDimitry Andric if (ImportState != Sema::ModuleImportState::FirstDecl) { 24600b57cec5SDimitry Andric Diag(StartLoc, diag::err_global_module_introducer_not_at_start) 24610b57cec5SDimitry Andric << SourceRange(StartLoc, SemiLoc); 24620b57cec5SDimitry Andric return nullptr; 24630b57cec5SDimitry Andric } 24640b57cec5SDimitry Andric if (MDK == Sema::ModuleDeclKind::Interface) { 24650b57cec5SDimitry Andric Diag(StartLoc, diag::err_module_fragment_exported) 24660b57cec5SDimitry Andric << /*global*/0 << FixItHint::CreateRemoval(StartLoc); 24670b57cec5SDimitry Andric } 246881ad6265SDimitry Andric ImportState = Sema::ModuleImportState::GlobalFragment; 24690b57cec5SDimitry Andric return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc); 24700b57cec5SDimitry Andric } 24710b57cec5SDimitry Andric 24720b57cec5SDimitry Andric // Parse a private-module-fragment, if present. 24730b57cec5SDimitry Andric if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) && 24740b57cec5SDimitry Andric NextToken().is(tok::kw_private)) { 24750b57cec5SDimitry Andric if (MDK == Sema::ModuleDeclKind::Interface) { 24760b57cec5SDimitry Andric Diag(StartLoc, diag::err_module_fragment_exported) 24770b57cec5SDimitry Andric << /*private*/1 << FixItHint::CreateRemoval(StartLoc); 24780b57cec5SDimitry Andric } 24790b57cec5SDimitry Andric ConsumeToken(); 24800b57cec5SDimitry Andric SourceLocation PrivateLoc = ConsumeToken(); 24810b57cec5SDimitry Andric DiagnoseAndSkipCXX11Attributes(); 24820b57cec5SDimitry Andric ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi); 2483bdd1243dSDimitry Andric ImportState = ImportState == Sema::ModuleImportState::ImportAllowed 2484bdd1243dSDimitry Andric ? Sema::ModuleImportState::PrivateFragmentImportAllowed 2485bdd1243dSDimitry Andric : Sema::ModuleImportState::PrivateFragmentImportFinished; 24860b57cec5SDimitry Andric return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc); 24870b57cec5SDimitry Andric } 24880b57cec5SDimitry Andric 24890b57cec5SDimitry Andric SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 24900b57cec5SDimitry Andric if (ParseModuleName(ModuleLoc, Path, /*IsImport*/ false)) 24910b57cec5SDimitry Andric return nullptr; 24920b57cec5SDimitry Andric 24930b57cec5SDimitry Andric // Parse the optional module-partition. 249481ad6265SDimitry Andric SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition; 24950b57cec5SDimitry Andric if (Tok.is(tok::colon)) { 24960b57cec5SDimitry Andric SourceLocation ColonLoc = ConsumeToken(); 249781ad6265SDimitry Andric if (!getLangOpts().CPlusPlusModules) 24980b57cec5SDimitry Andric Diag(ColonLoc, diag::err_unsupported_module_partition) 24990b57cec5SDimitry Andric << SourceRange(ColonLoc, Partition.back().second); 250081ad6265SDimitry Andric // Recover by ignoring the partition name. 250181ad6265SDimitry Andric else if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/ false)) 250281ad6265SDimitry Andric return nullptr; 25030b57cec5SDimitry Andric } 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric // We don't support any module attributes yet; just parse them and diagnose. 250681ad6265SDimitry Andric ParsedAttributes Attrs(AttrFactory); 25070b57cec5SDimitry Andric MaybeParseCXX11Attributes(Attrs); 250881ad6265SDimitry Andric ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr, 250906c3fb27SDimitry Andric diag::err_keyword_not_module_attr, 251081ad6265SDimitry Andric /*DiagnoseEmptyAttrs=*/false, 251181ad6265SDimitry Andric /*WarnOnUnknownAttrs=*/true); 25120b57cec5SDimitry Andric 25130b57cec5SDimitry Andric ExpectAndConsumeSemi(diag::err_module_expected_semi); 25140b57cec5SDimitry Andric 251581ad6265SDimitry Andric return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, Partition, 251681ad6265SDimitry Andric ImportState); 25170b57cec5SDimitry Andric } 25180b57cec5SDimitry Andric 25190b57cec5SDimitry Andric /// Parse a module import declaration. This is essentially the same for 252081ad6265SDimitry Andric /// Objective-C and C++20 except for the leading '@' (in ObjC) and the 252181ad6265SDimitry Andric /// trailing optional attributes (in C++). 25220b57cec5SDimitry Andric /// 25230b57cec5SDimitry Andric /// [ObjC] @import declaration: 25240b57cec5SDimitry Andric /// '@' 'import' module-name ';' 25250b57cec5SDimitry Andric /// [ModTS] module-import-declaration: 25260b57cec5SDimitry Andric /// 'import' module-name attribute-specifier-seq[opt] ';' 252781ad6265SDimitry Andric /// [C++20] module-import-declaration: 25280b57cec5SDimitry Andric /// 'export'[opt] 'import' module-name 25290b57cec5SDimitry Andric /// attribute-specifier-seq[opt] ';' 25300b57cec5SDimitry Andric /// 'export'[opt] 'import' module-partition 25310b57cec5SDimitry Andric /// attribute-specifier-seq[opt] ';' 25320b57cec5SDimitry Andric /// 'export'[opt] 'import' header-name 25330b57cec5SDimitry Andric /// attribute-specifier-seq[opt] ';' 253481ad6265SDimitry Andric Decl *Parser::ParseModuleImport(SourceLocation AtLoc, 253581ad6265SDimitry Andric Sema::ModuleImportState &ImportState) { 25360b57cec5SDimitry Andric SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc; 25370b57cec5SDimitry Andric 25380b57cec5SDimitry Andric SourceLocation ExportLoc; 25390b57cec5SDimitry Andric TryConsumeToken(tok::kw_export, ExportLoc); 25400b57cec5SDimitry Andric 25410b57cec5SDimitry Andric assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier) 25420b57cec5SDimitry Andric : Tok.isObjCAtKeyword(tok::objc_import)) && 25430b57cec5SDimitry Andric "Improper start to module import"); 25440b57cec5SDimitry Andric bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import); 25450b57cec5SDimitry Andric SourceLocation ImportLoc = ConsumeToken(); 25460b57cec5SDimitry Andric 254781ad6265SDimitry Andric // For C++20 modules, we can have "name" or ":Partition name" as valid input. 25480b57cec5SDimitry Andric SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 254981ad6265SDimitry Andric bool IsPartition = false; 25500b57cec5SDimitry Andric Module *HeaderUnit = nullptr; 25510b57cec5SDimitry Andric if (Tok.is(tok::header_name)) { 25520b57cec5SDimitry Andric // This is a header import that the preprocessor decided we should skip 25530b57cec5SDimitry Andric // because it was malformed in some way. Parse and ignore it; it's already 25540b57cec5SDimitry Andric // been diagnosed. 25550b57cec5SDimitry Andric ConsumeToken(); 25560b57cec5SDimitry Andric } else if (Tok.is(tok::annot_header_unit)) { 25570b57cec5SDimitry Andric // This is a header import that the preprocessor mapped to a module import. 25580b57cec5SDimitry Andric HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 25590b57cec5SDimitry Andric ConsumeAnnotationToken(); 256081ad6265SDimitry Andric } else if (Tok.is(tok::colon)) { 25610b57cec5SDimitry Andric SourceLocation ColonLoc = ConsumeToken(); 256281ad6265SDimitry Andric if (!getLangOpts().CPlusPlusModules) 25630b57cec5SDimitry Andric Diag(ColonLoc, diag::err_unsupported_module_partition) 25640b57cec5SDimitry Andric << SourceRange(ColonLoc, Path.back().second); 256581ad6265SDimitry Andric // Recover by leaving partition empty. 256681ad6265SDimitry Andric else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true)) 25670b57cec5SDimitry Andric return nullptr; 256881ad6265SDimitry Andric else 256981ad6265SDimitry Andric IsPartition = true; 25700b57cec5SDimitry Andric } else { 25710b57cec5SDimitry Andric if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true)) 25720b57cec5SDimitry Andric return nullptr; 25730b57cec5SDimitry Andric } 25740b57cec5SDimitry Andric 257581ad6265SDimitry Andric ParsedAttributes Attrs(AttrFactory); 25760b57cec5SDimitry Andric MaybeParseCXX11Attributes(Attrs); 25770b57cec5SDimitry Andric // We don't support any module import attributes yet. 257881ad6265SDimitry Andric ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr, 257906c3fb27SDimitry Andric diag::err_keyword_not_import_attr, 258081ad6265SDimitry Andric /*DiagnoseEmptyAttrs=*/false, 258181ad6265SDimitry Andric /*WarnOnUnknownAttrs=*/true); 25820b57cec5SDimitry Andric 25830b57cec5SDimitry Andric if (PP.hadModuleLoaderFatalFailure()) { 25840b57cec5SDimitry Andric // With a fatal failure in the module loader, we abort parsing. 25850b57cec5SDimitry Andric cutOffParsing(); 25860b57cec5SDimitry Andric return nullptr; 25870b57cec5SDimitry Andric } 25880b57cec5SDimitry Andric 258981ad6265SDimitry Andric // Diagnose mis-imports. 259081ad6265SDimitry Andric bool SeenError = true; 259181ad6265SDimitry Andric switch (ImportState) { 259281ad6265SDimitry Andric case Sema::ModuleImportState::ImportAllowed: 259381ad6265SDimitry Andric SeenError = false; 259481ad6265SDimitry Andric break; 259581ad6265SDimitry Andric case Sema::ModuleImportState::FirstDecl: 25965f757f3fSDimitry Andric // If we found an import decl as the first declaration, we must be not in 25975f757f3fSDimitry Andric // a C++20 module unit or we are in an invalid state. 25985f757f3fSDimitry Andric ImportState = Sema::ModuleImportState::NotACXX20Module; 25995f757f3fSDimitry Andric [[fallthrough]]; 260081ad6265SDimitry Andric case Sema::ModuleImportState::NotACXX20Module: 260181ad6265SDimitry Andric // We can only import a partition within a module purview. 260281ad6265SDimitry Andric if (IsPartition) 260381ad6265SDimitry Andric Diag(ImportLoc, diag::err_partition_import_outside_module); 260481ad6265SDimitry Andric else 260581ad6265SDimitry Andric SeenError = false; 260681ad6265SDimitry Andric break; 260781ad6265SDimitry Andric case Sema::ModuleImportState::GlobalFragment: 2608bdd1243dSDimitry Andric case Sema::ModuleImportState::PrivateFragmentImportAllowed: 2609bdd1243dSDimitry Andric // We can only have pre-processor directives in the global module fragment 2610bdd1243dSDimitry Andric // which allows pp-import, but not of a partition (since the global module 2611bdd1243dSDimitry Andric // does not have partitions). 2612bdd1243dSDimitry Andric // We cannot import a partition into a private module fragment, since 2613bdd1243dSDimitry Andric // [module.private.frag]/1 disallows private module fragments in a multi- 2614bdd1243dSDimitry Andric // TU module. 2615bdd1243dSDimitry Andric if (IsPartition || (HeaderUnit && HeaderUnit->Kind != 2616bdd1243dSDimitry Andric Module::ModuleKind::ModuleHeaderUnit)) 2617bdd1243dSDimitry Andric Diag(ImportLoc, diag::err_import_in_wrong_fragment) 2618bdd1243dSDimitry Andric << IsPartition 2619bdd1243dSDimitry Andric << (ImportState == Sema::ModuleImportState::GlobalFragment ? 0 : 1); 262081ad6265SDimitry Andric else 262181ad6265SDimitry Andric SeenError = false; 262281ad6265SDimitry Andric break; 262381ad6265SDimitry Andric case Sema::ModuleImportState::ImportFinished: 2624bdd1243dSDimitry Andric case Sema::ModuleImportState::PrivateFragmentImportFinished: 262581ad6265SDimitry Andric if (getLangOpts().CPlusPlusModules) 262681ad6265SDimitry Andric Diag(ImportLoc, diag::err_import_not_allowed_here); 262781ad6265SDimitry Andric else 262881ad6265SDimitry Andric SeenError = false; 262981ad6265SDimitry Andric break; 263081ad6265SDimitry Andric } 263181ad6265SDimitry Andric if (SeenError) { 263281ad6265SDimitry Andric ExpectAndConsumeSemi(diag::err_module_expected_semi); 263381ad6265SDimitry Andric return nullptr; 263481ad6265SDimitry Andric } 263581ad6265SDimitry Andric 26360b57cec5SDimitry Andric DeclResult Import; 26370b57cec5SDimitry Andric if (HeaderUnit) 26380b57cec5SDimitry Andric Import = 26390b57cec5SDimitry Andric Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit); 26400b57cec5SDimitry Andric else if (!Path.empty()) 264181ad6265SDimitry Andric Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path, 264281ad6265SDimitry Andric IsPartition); 26430b57cec5SDimitry Andric ExpectAndConsumeSemi(diag::err_module_expected_semi); 26440b57cec5SDimitry Andric if (Import.isInvalid()) 26450b57cec5SDimitry Andric return nullptr; 26460b57cec5SDimitry Andric 26470b57cec5SDimitry Andric // Using '@import' in framework headers requires modules to be enabled so that 26480b57cec5SDimitry Andric // the header is parseable. Emit a warning to make the user aware. 26490b57cec5SDimitry Andric if (IsObjCAtImport && AtLoc.isValid()) { 26500b57cec5SDimitry Andric auto &SrcMgr = PP.getSourceManager(); 265181ad6265SDimitry Andric auto FE = SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(AtLoc)); 265281ad6265SDimitry Andric if (FE && llvm::sys::path::parent_path(FE->getDir().getName()) 26535f757f3fSDimitry Andric .ends_with(".framework")) 26540b57cec5SDimitry Andric Diags.Report(AtLoc, diag::warn_atimport_in_framework_header); 26550b57cec5SDimitry Andric } 26560b57cec5SDimitry Andric 26570b57cec5SDimitry Andric return Import.get(); 26580b57cec5SDimitry Andric } 26590b57cec5SDimitry Andric 266006c3fb27SDimitry Andric /// Parse a C++ / Objective-C module name (both forms use the same 26610b57cec5SDimitry Andric /// grammar). 26620b57cec5SDimitry Andric /// 26630b57cec5SDimitry Andric /// module-name: 26640b57cec5SDimitry Andric /// module-name-qualifier[opt] identifier 26650b57cec5SDimitry Andric /// module-name-qualifier: 26660b57cec5SDimitry Andric /// module-name-qualifier[opt] identifier '.' 26670b57cec5SDimitry Andric bool Parser::ParseModuleName( 26680b57cec5SDimitry Andric SourceLocation UseLoc, 26690b57cec5SDimitry Andric SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path, 26700b57cec5SDimitry Andric bool IsImport) { 26710b57cec5SDimitry Andric // Parse the module path. 26720b57cec5SDimitry Andric while (true) { 26730b57cec5SDimitry Andric if (!Tok.is(tok::identifier)) { 26740b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 26750b57cec5SDimitry Andric cutOffParsing(); 2676fe6060f1SDimitry Andric Actions.CodeCompleteModuleImport(UseLoc, Path); 26770b57cec5SDimitry Andric return true; 26780b57cec5SDimitry Andric } 26790b57cec5SDimitry Andric 26800b57cec5SDimitry Andric Diag(Tok, diag::err_module_expected_ident) << IsImport; 26810b57cec5SDimitry Andric SkipUntil(tok::semi); 26820b57cec5SDimitry Andric return true; 26830b57cec5SDimitry Andric } 26840b57cec5SDimitry Andric 26850b57cec5SDimitry Andric // Record this part of the module path. 26860b57cec5SDimitry Andric Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation())); 26870b57cec5SDimitry Andric ConsumeToken(); 26880b57cec5SDimitry Andric 26890b57cec5SDimitry Andric if (Tok.isNot(tok::period)) 26900b57cec5SDimitry Andric return false; 26910b57cec5SDimitry Andric 26920b57cec5SDimitry Andric ConsumeToken(); 26930b57cec5SDimitry Andric } 26940b57cec5SDimitry Andric } 26950b57cec5SDimitry Andric 26960b57cec5SDimitry Andric /// Try recover parser when module annotation appears where it must not 26970b57cec5SDimitry Andric /// be found. 26980b57cec5SDimitry Andric /// \returns false if the recover was successful and parsing may be continued, or 26990b57cec5SDimitry Andric /// true if parser must bail out to top level and handle the token there. 27000b57cec5SDimitry Andric bool Parser::parseMisplacedModuleImport() { 27010b57cec5SDimitry Andric while (true) { 27020b57cec5SDimitry Andric switch (Tok.getKind()) { 27030b57cec5SDimitry Andric case tok::annot_module_end: 27040b57cec5SDimitry Andric // If we recovered from a misplaced module begin, we expect to hit a 27050b57cec5SDimitry Andric // misplaced module end too. Stay in the current context when this 27060b57cec5SDimitry Andric // happens. 27070b57cec5SDimitry Andric if (MisplacedModuleBeginCount) { 27080b57cec5SDimitry Andric --MisplacedModuleBeginCount; 27090b57cec5SDimitry Andric Actions.ActOnModuleEnd(Tok.getLocation(), 27100b57cec5SDimitry Andric reinterpret_cast<Module *>( 27110b57cec5SDimitry Andric Tok.getAnnotationValue())); 27120b57cec5SDimitry Andric ConsumeAnnotationToken(); 27130b57cec5SDimitry Andric continue; 27140b57cec5SDimitry Andric } 27150b57cec5SDimitry Andric // Inform caller that recovery failed, the error must be handled at upper 27160b57cec5SDimitry Andric // level. This will generate the desired "missing '}' at end of module" 27170b57cec5SDimitry Andric // diagnostics on the way out. 27180b57cec5SDimitry Andric return true; 27190b57cec5SDimitry Andric case tok::annot_module_begin: 27200b57cec5SDimitry Andric // Recover by entering the module (Sema will diagnose). 27210b57cec5SDimitry Andric Actions.ActOnModuleBegin(Tok.getLocation(), 27220b57cec5SDimitry Andric reinterpret_cast<Module *>( 27230b57cec5SDimitry Andric Tok.getAnnotationValue())); 27240b57cec5SDimitry Andric ConsumeAnnotationToken(); 27250b57cec5SDimitry Andric ++MisplacedModuleBeginCount; 27260b57cec5SDimitry Andric continue; 27270b57cec5SDimitry Andric case tok::annot_module_include: 27280b57cec5SDimitry Andric // Module import found where it should not be, for instance, inside a 27290b57cec5SDimitry Andric // namespace. Recover by importing the module. 27300b57cec5SDimitry Andric Actions.ActOnModuleInclude(Tok.getLocation(), 27310b57cec5SDimitry Andric reinterpret_cast<Module *>( 27320b57cec5SDimitry Andric Tok.getAnnotationValue())); 27330b57cec5SDimitry Andric ConsumeAnnotationToken(); 27340b57cec5SDimitry Andric // If there is another module import, process it. 27350b57cec5SDimitry Andric continue; 27360b57cec5SDimitry Andric default: 27370b57cec5SDimitry Andric return false; 27380b57cec5SDimitry Andric } 27390b57cec5SDimitry Andric } 27400b57cec5SDimitry Andric return false; 27410b57cec5SDimitry Andric } 27420b57cec5SDimitry Andric 27430b57cec5SDimitry Andric bool BalancedDelimiterTracker::diagnoseOverflow() { 27440b57cec5SDimitry Andric P.Diag(P.Tok, diag::err_bracket_depth_exceeded) 27450b57cec5SDimitry Andric << P.getLangOpts().BracketDepth; 27460b57cec5SDimitry Andric P.Diag(P.Tok, diag::note_bracket_depth); 27470b57cec5SDimitry Andric P.cutOffParsing(); 27480b57cec5SDimitry Andric return true; 27490b57cec5SDimitry Andric } 27500b57cec5SDimitry Andric 27510b57cec5SDimitry Andric bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID, 27520b57cec5SDimitry Andric const char *Msg, 27530b57cec5SDimitry Andric tok::TokenKind SkipToTok) { 27540b57cec5SDimitry Andric LOpen = P.Tok.getLocation(); 27550b57cec5SDimitry Andric if (P.ExpectAndConsume(Kind, DiagID, Msg)) { 27560b57cec5SDimitry Andric if (SkipToTok != tok::unknown) 27570b57cec5SDimitry Andric P.SkipUntil(SkipToTok, Parser::StopAtSemi); 27580b57cec5SDimitry Andric return true; 27590b57cec5SDimitry Andric } 27600b57cec5SDimitry Andric 27610b57cec5SDimitry Andric if (getDepth() < P.getLangOpts().BracketDepth) 27620b57cec5SDimitry Andric return false; 27630b57cec5SDimitry Andric 27640b57cec5SDimitry Andric return diagnoseOverflow(); 27650b57cec5SDimitry Andric } 27660b57cec5SDimitry Andric 27670b57cec5SDimitry Andric bool BalancedDelimiterTracker::diagnoseMissingClose() { 27680b57cec5SDimitry Andric assert(!P.Tok.is(Close) && "Should have consumed closing delimiter"); 27690b57cec5SDimitry Andric 27700b57cec5SDimitry Andric if (P.Tok.is(tok::annot_module_end)) 27710b57cec5SDimitry Andric P.Diag(P.Tok, diag::err_missing_before_module_end) << Close; 27720b57cec5SDimitry Andric else 27730b57cec5SDimitry Andric P.Diag(P.Tok, diag::err_expected) << Close; 27740b57cec5SDimitry Andric P.Diag(LOpen, diag::note_matching) << Kind; 27750b57cec5SDimitry Andric 27760b57cec5SDimitry Andric // If we're not already at some kind of closing bracket, skip to our closing 27770b57cec5SDimitry Andric // token. 27780b57cec5SDimitry Andric if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) && 27790b57cec5SDimitry Andric P.Tok.isNot(tok::r_square) && 27800b57cec5SDimitry Andric P.SkipUntil(Close, FinalToken, 27810b57cec5SDimitry Andric Parser::StopAtSemi | Parser::StopBeforeMatch) && 27820b57cec5SDimitry Andric P.Tok.is(Close)) 27830b57cec5SDimitry Andric LClose = P.ConsumeAnyToken(); 27840b57cec5SDimitry Andric return true; 27850b57cec5SDimitry Andric } 27860b57cec5SDimitry Andric 27870b57cec5SDimitry Andric void BalancedDelimiterTracker::skipToEnd() { 27880b57cec5SDimitry Andric P.SkipUntil(Close, Parser::StopBeforeMatch); 27890b57cec5SDimitry Andric consumeClose(); 27900b57cec5SDimitry Andric } 2791