1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672 |
- //===--- Parser.cpp - C Language Family Parser ----------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This file implements the Parser interfaces.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Parse/Parser.h"
- #include "clang/Parse/ParseDiagnostic.h"
- #include "clang/Sema/DeclSpec.h"
- #include "clang/Sema/Scope.h"
- #include "clang/Sema/ParsedTemplate.h"
- #include "llvm/Support/raw_ostream.h"
- #include "RAIIObjectsForParser.h"
- #include "ParsePragma.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/AST/ASTConsumer.h"
- using namespace clang;
- IdentifierInfo *Parser::getSEHExceptKeyword() {
- // __except is accepted as a (contextual) keyword
- if (!Ident__except && (getLang().MicrosoftExt || getLang().Borland))
- Ident__except = PP.getIdentifierInfo("__except");
- return Ident__except;
- }
- Parser::Parser(Preprocessor &pp, Sema &actions)
- : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
- GreaterThanIsOperator(true), ColonIsSacred(false),
- InMessageExpression(false), TemplateParameterDepth(0) {
- Tok.setKind(tok::eof);
- Actions.CurScope = 0;
- NumCachedScopes = 0;
- ParenCount = BracketCount = BraceCount = 0;
- ObjCImpDecl = 0;
- // Add #pragma handlers. These are removed and destroyed in the
- // destructor.
- AlignHandler.reset(new PragmaAlignHandler(actions));
- PP.AddPragmaHandler(AlignHandler.get());
- GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
- PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
- OptionsHandler.reset(new PragmaOptionsHandler(actions));
- PP.AddPragmaHandler(OptionsHandler.get());
- PackHandler.reset(new PragmaPackHandler(actions));
- PP.AddPragmaHandler(PackHandler.get());
-
- MSStructHandler.reset(new PragmaMSStructHandler(actions));
- PP.AddPragmaHandler(MSStructHandler.get());
- UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
- PP.AddPragmaHandler(UnusedHandler.get());
- WeakHandler.reset(new PragmaWeakHandler(actions));
- PP.AddPragmaHandler(WeakHandler.get());
- FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
- PP.AddPragmaHandler("STDC", FPContractHandler.get());
- if (getLang().OpenCL) {
- OpenCLExtensionHandler.reset(
- new PragmaOpenCLExtensionHandler(actions, *this));
- PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
- PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
- }
-
- PP.setCodeCompletionHandler(*this);
- }
- /// If a crash happens while the parser is active, print out a line indicating
- /// what the current token is.
- void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
- const Token &Tok = P.getCurToken();
- if (Tok.is(tok::eof)) {
- OS << "<eof> parser at end of file\n";
- return;
- }
- if (Tok.getLocation().isInvalid()) {
- OS << "<unknown> parser at unknown location\n";
- return;
- }
- const Preprocessor &PP = P.getPreprocessor();
- Tok.getLocation().print(OS, PP.getSourceManager());
- if (Tok.isAnnotation())
- OS << ": at annotation token \n";
- else
- OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
- }
- DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
- return Diags.Report(Loc, DiagID);
- }
- DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
- return Diag(Tok.getLocation(), DiagID);
- }
- /// \brief Emits a diagnostic suggesting parentheses surrounding a
- /// given range.
- ///
- /// \param Loc The location where we'll emit the diagnostic.
- /// \param Loc The kind of diagnostic to emit.
- /// \param ParenRange Source range enclosing code that should be parenthesized.
- void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
- SourceRange ParenRange) {
- SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
- if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
- // We can't display the parentheses, so just dig the
- // warning/error and return.
- Diag(Loc, DK);
- return;
- }
- Diag(Loc, DK)
- << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
- << FixItHint::CreateInsertion(EndLoc, ")");
- }
- static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
- switch (ExpectedTok) {
- case tok::semi: return Tok.is(tok::colon); // : for ;
- default: return false;
- }
- }
- /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
- /// input. If so, it is consumed and false is returned.
- ///
- /// If the input is malformed, this emits the specified diagnostic. Next, if
- /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
- /// returned.
- bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
- const char *Msg, tok::TokenKind SkipToTok) {
- if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
- ConsumeAnyToken();
- return false;
- }
- // Detect common single-character typos and resume.
- if (IsCommonTypo(ExpectedTok, Tok)) {
- SourceLocation Loc = Tok.getLocation();
- Diag(Loc, DiagID)
- << Msg
- << FixItHint::CreateReplacement(SourceRange(Loc),
- getTokenSimpleSpelling(ExpectedTok));
- ConsumeAnyToken();
- // Pretend there wasn't a problem.
- return false;
- }
- const char *Spelling = 0;
- SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
- if (EndLoc.isValid() &&
- (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
- // Show what code to insert to fix this problem.
- Diag(EndLoc, DiagID)
- << Msg
- << FixItHint::CreateInsertion(EndLoc, Spelling);
- } else
- Diag(Tok, DiagID) << Msg;
- if (SkipToTok != tok::unknown)
- SkipUntil(SkipToTok);
- return true;
- }
- bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
- if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
- ConsumeAnyToken();
- return false;
- }
-
- if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
- NextToken().is(tok::semi)) {
- Diag(Tok, diag::err_extraneous_token_before_semi)
- << PP.getSpelling(Tok)
- << FixItHint::CreateRemoval(Tok.getLocation());
- ConsumeAnyToken(); // The ')' or ']'.
- ConsumeToken(); // The ';'.
- return false;
- }
-
- return ExpectAndConsume(tok::semi, DiagID);
- }
- //===----------------------------------------------------------------------===//
- // Error recovery.
- //===----------------------------------------------------------------------===//
- /// SkipUntil - Read tokens until we get to the specified token, then consume
- /// it (unless DontConsume is true). Because we cannot guarantee that the
- /// token will ever occur, this skips to the next token, or to some likely
- /// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
- /// character.
- ///
- /// If SkipUntil finds the specified token, it returns true, otherwise it
- /// returns false.
- bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
- bool StopAtSemi, bool DontConsume,
- bool StopAtCodeCompletion) {
- // We always want this function to skip at least one token if the first token
- // isn't T and if not at EOF.
- bool isFirstTokenSkipped = true;
- while (1) {
- // If we found one of the tokens, stop and return true.
- for (unsigned i = 0; i != NumToks; ++i) {
- if (Tok.is(Toks[i])) {
- if (DontConsume) {
- // Noop, don't consume the token.
- } else {
- ConsumeAnyToken();
- }
- return true;
- }
- }
- switch (Tok.getKind()) {
- case tok::eof:
- // Ran out of tokens.
- return false;
-
- case tok::code_completion:
- if (!StopAtCodeCompletion)
- ConsumeToken();
- return false;
-
- case tok::l_paren:
- // Recursively skip properly-nested parens.
- ConsumeParen();
- SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
- break;
- case tok::l_square:
- // Recursively skip properly-nested square brackets.
- ConsumeBracket();
- SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
- break;
- case tok::l_brace:
- // Recursively skip properly-nested braces.
- ConsumeBrace();
- SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
- break;
- // Okay, we found a ']' or '}' or ')', which we think should be balanced.
- // Since the user wasn't looking for this token (if they were, it would
- // already be handled), this isn't balanced. If there is a LHS token at a
- // higher level, we will assume that this matches the unbalanced token
- // and return it. Otherwise, this is a spurious RHS token, which we skip.
- case tok::r_paren:
- if (ParenCount && !isFirstTokenSkipped)
- return false; // Matches something.
- ConsumeParen();
- break;
- case tok::r_square:
- if (BracketCount && !isFirstTokenSkipped)
- return false; // Matches something.
- ConsumeBracket();
- break;
- case tok::r_brace:
- if (BraceCount && !isFirstTokenSkipped)
- return false; // Matches something.
- ConsumeBrace();
- break;
- case tok::string_literal:
- case tok::wide_string_literal:
- case tok::utf8_string_literal:
- case tok::utf16_string_literal:
- case tok::utf32_string_literal:
- ConsumeStringToken();
- break;
-
- case tok::semi:
- if (StopAtSemi)
- return false;
- // FALL THROUGH.
- default:
- // Skip this token.
- ConsumeToken();
- break;
- }
- isFirstTokenSkipped = false;
- }
- }
- //===----------------------------------------------------------------------===//
- // Scope manipulation
- //===----------------------------------------------------------------------===//
- /// EnterScope - Start a new scope.
- void Parser::EnterScope(unsigned ScopeFlags) {
- if (NumCachedScopes) {
- Scope *N = ScopeCache[--NumCachedScopes];
- N->Init(getCurScope(), ScopeFlags);
- Actions.CurScope = N;
- } else {
- Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
- }
- }
- /// ExitScope - Pop a scope off the scope stack.
- void Parser::ExitScope() {
- assert(getCurScope() && "Scope imbalance!");
- // Inform the actions module that this scope is going away if there are any
- // decls in it.
- if (!getCurScope()->decl_empty())
- Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
- Scope *OldScope = getCurScope();
- Actions.CurScope = OldScope->getParent();
- if (NumCachedScopes == ScopeCacheSize)
- delete OldScope;
- else
- ScopeCache[NumCachedScopes++] = OldScope;
- }
- /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
- /// this object does nothing.
- Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
- bool ManageFlags)
- : CurScope(ManageFlags ? Self->getCurScope() : 0) {
- if (CurScope) {
- OldFlags = CurScope->getFlags();
- CurScope->setFlags(ScopeFlags);
- }
- }
- /// Restore the flags for the current scope to what they were before this
- /// object overrode them.
- Parser::ParseScopeFlags::~ParseScopeFlags() {
- if (CurScope)
- CurScope->setFlags(OldFlags);
- }
- //===----------------------------------------------------------------------===//
- // C99 6.9: External Definitions.
- //===----------------------------------------------------------------------===//
- Parser::~Parser() {
- // If we still have scopes active, delete the scope tree.
- delete getCurScope();
- Actions.CurScope = 0;
-
- // Free the scope cache.
- for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
- delete ScopeCache[i];
- // Free LateParsedTemplatedFunction nodes.
- for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
- it != LateParsedTemplateMap.end(); ++it)
- delete it->second;
- clearLateParsedObjCMethods();
- // Remove the pragma handlers we installed.
- PP.RemovePragmaHandler(AlignHandler.get());
- AlignHandler.reset();
- PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
- GCCVisibilityHandler.reset();
- PP.RemovePragmaHandler(OptionsHandler.get());
- OptionsHandler.reset();
- PP.RemovePragmaHandler(PackHandler.get());
- PackHandler.reset();
- PP.RemovePragmaHandler(MSStructHandler.get());
- MSStructHandler.reset();
- PP.RemovePragmaHandler(UnusedHandler.get());
- UnusedHandler.reset();
- PP.RemovePragmaHandler(WeakHandler.get());
- WeakHandler.reset();
- if (getLang().OpenCL) {
- PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
- OpenCLExtensionHandler.reset();
- PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
- }
- PP.RemovePragmaHandler("STDC", FPContractHandler.get());
- FPContractHandler.reset();
- PP.clearCodeCompletionHandler();
- }
- /// Initialize - Warm up the parser.
- ///
- void Parser::Initialize() {
- // Create the translation unit scope. Install it as the current scope.
- assert(getCurScope() == 0 && "A scope is already active?");
- EnterScope(Scope::DeclScope);
- Actions.ActOnTranslationUnitScope(getCurScope());
- // Prime the lexer look-ahead.
- ConsumeToken();
- if (Tok.is(tok::eof) &&
- !getLang().CPlusPlus) // Empty source file is an extension in C
- Diag(Tok, diag::ext_empty_source_file);
- // Initialization for Objective-C context sensitive keywords recognition.
- // Referenced in Parser::ParseObjCTypeQualifierList.
- if (getLang().ObjC1) {
- ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
- ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
- ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
- ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
- ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
- ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
- }
- Ident_instancetype = 0;
- Ident_final = 0;
- Ident_override = 0;
- Ident_super = &PP.getIdentifierTable().get("super");
- if (getLang().AltiVec) {
- Ident_vector = &PP.getIdentifierTable().get("vector");
- Ident_pixel = &PP.getIdentifierTable().get("pixel");
- }
- Ident_introduced = 0;
- Ident_deprecated = 0;
- Ident_obsoleted = 0;
- Ident_unavailable = 0;
- Ident__except = 0;
-
- Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
- Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
- Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
- if(getLang().Borland) {
- Ident__exception_info = PP.getIdentifierInfo("_exception_info");
- Ident___exception_info = PP.getIdentifierInfo("__exception_info");
- Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
- Ident__exception_code = PP.getIdentifierInfo("_exception_code");
- Ident___exception_code = PP.getIdentifierInfo("__exception_code");
- Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
- Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
- Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
- Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
- PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
- PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
- PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
- PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
- PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
- PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
- PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
- PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
- PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
- }
- }
- /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
- /// action tells us to. This returns true if the EOF was encountered.
- bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
- DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
- while (Tok.is(tok::annot_pragma_unused))
- HandlePragmaUnused();
- Result = DeclGroupPtrTy();
- if (Tok.is(tok::eof)) {
- // Late template parsing can begin.
- if (getLang().DelayedTemplateParsing)
- Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
- Actions.ActOnEndOfTranslationUnit();
- return true;
- }
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX0XAttributes(attrs);
- MaybeParseMicrosoftAttributes(attrs);
-
- Result = ParseExternalDeclaration(attrs);
- return false;
- }
- /// ParseTranslationUnit:
- /// translation-unit: [C99 6.9]
- /// external-declaration
- /// translation-unit external-declaration
- void Parser::ParseTranslationUnit() {
- Initialize();
- DeclGroupPtrTy Res;
- while (!ParseTopLevelDecl(Res))
- /*parse them all*/;
- ExitScope();
- assert(getCurScope() == 0 && "Scope imbalance!");
- }
- /// ParseExternalDeclaration:
- ///
- /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
- /// function-definition
- /// declaration
- /// [C++0x] empty-declaration
- /// [GNU] asm-definition
- /// [GNU] __extension__ external-declaration
- /// [OBJC] objc-class-definition
- /// [OBJC] objc-class-declaration
- /// [OBJC] objc-alias-declaration
- /// [OBJC] objc-protocol-definition
- /// [OBJC] objc-method-definition
- /// [OBJC] @end
- /// [C++] linkage-specification
- /// [GNU] asm-definition:
- /// simple-asm-expr ';'
- ///
- /// [C++0x] empty-declaration:
- /// ';'
- ///
- /// [C++0x/GNU] 'extern' 'template' declaration
- Parser::DeclGroupPtrTy
- Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
- ParsingDeclSpec *DS) {
- DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
- ParenBraceBracketBalancer BalancerRAIIObj(*this);
- if (PP.isCodeCompletionReached()) {
- cutOffParsing();
- return DeclGroupPtrTy();
- }
- Decl *SingleDecl = 0;
- switch (Tok.getKind()) {
- case tok::semi:
- Diag(Tok, getLang().CPlusPlus0x ?
- diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)
- << FixItHint::CreateRemoval(Tok.getLocation());
- ConsumeToken();
- // TODO: Invoke action for top-level semicolon.
- return DeclGroupPtrTy();
- case tok::r_brace:
- Diag(Tok, diag::err_expected_external_declaration);
- ConsumeBrace();
- return DeclGroupPtrTy();
- case tok::eof:
- Diag(Tok, diag::err_expected_external_declaration);
- return DeclGroupPtrTy();
- case tok::kw___extension__: {
- // __extension__ silences extension warnings in the subexpression.
- ExtensionRAIIObject O(Diags); // Use RAII to do this.
- ConsumeToken();
- return ParseExternalDeclaration(attrs);
- }
- case tok::kw_asm: {
- ProhibitAttributes(attrs);
- SourceLocation StartLoc = Tok.getLocation();
- SourceLocation EndLoc;
- ExprResult Result(ParseSimpleAsm(&EndLoc));
- ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
- "top-level asm block");
- if (Result.isInvalid())
- return DeclGroupPtrTy();
- SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
- break;
- }
- case tok::at:
- return ParseObjCAtDirectives();
- break;
- case tok::minus:
- case tok::plus:
- if (!getLang().ObjC1) {
- Diag(Tok, diag::err_expected_external_declaration);
- ConsumeToken();
- return DeclGroupPtrTy();
- }
- SingleDecl = ParseObjCMethodDefinition();
- break;
- case tok::code_completion:
- Actions.CodeCompleteOrdinaryName(getCurScope(),
- ObjCImpDecl? Sema::PCC_ObjCImplementation
- : Sema::PCC_Namespace);
- cutOffParsing();
- return DeclGroupPtrTy();
- case tok::kw_using:
- case tok::kw_namespace:
- case tok::kw_typedef:
- case tok::kw_template:
- case tok::kw_export: // As in 'export template'
- case tok::kw_static_assert:
- case tok::kw__Static_assert:
- // A function definition cannot start with a these keywords.
- {
- SourceLocation DeclEnd;
- StmtVector Stmts(Actions);
- return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
- }
- case tok::kw_static:
- // Parse (then ignore) 'static' prior to a template instantiation. This is
- // a GCC extension that we intentionally do not support.
- if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
- Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
- << 0;
- SourceLocation DeclEnd;
- StmtVector Stmts(Actions);
- return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
- }
- goto dont_know;
-
- case tok::kw_inline:
- if (getLang().CPlusPlus) {
- tok::TokenKind NextKind = NextToken().getKind();
-
- // Inline namespaces. Allowed as an extension even in C++03.
- if (NextKind == tok::kw_namespace) {
- SourceLocation DeclEnd;
- StmtVector Stmts(Actions);
- return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
- }
-
- // Parse (then ignore) 'inline' prior to a template instantiation. This is
- // a GCC extension that we intentionally do not support.
- if (NextKind == tok::kw_template) {
- Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
- << 1;
- SourceLocation DeclEnd;
- StmtVector Stmts(Actions);
- return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
- }
- }
- goto dont_know;
- case tok::kw_extern:
- if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
- // Extern templates
- SourceLocation ExternLoc = ConsumeToken();
- SourceLocation TemplateLoc = ConsumeToken();
- Diag(ExternLoc, getLang().CPlusPlus0x ?
- diag::warn_cxx98_compat_extern_template :
- diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
- SourceLocation DeclEnd;
- return Actions.ConvertDeclToDeclGroup(
- ParseExplicitInstantiation(Declarator::FileContext,
- ExternLoc, TemplateLoc, DeclEnd));
- }
- // FIXME: Detect C++ linkage specifications here?
- goto dont_know;
- case tok::kw___if_exists:
- case tok::kw___if_not_exists:
- ParseMicrosoftIfExistsExternalDeclaration();
- return DeclGroupPtrTy();
- case tok::kw___import_module__:
- return ParseModuleImport();
-
- default:
- dont_know:
- // We can't tell whether this is a function-definition or declaration yet.
- if (DS) {
- DS->takeAttributesFrom(attrs);
- return ParseDeclarationOrFunctionDefinition(*DS);
- } else {
- return ParseDeclarationOrFunctionDefinition(attrs);
- }
- }
- // This routine returns a DeclGroup, if the thing we parsed only contains a
- // single decl, convert it now.
- return Actions.ConvertDeclToDeclGroup(SingleDecl);
- }
- /// \brief Determine whether the current token, if it occurs after a
- /// declarator, continues a declaration or declaration list.
- bool Parser::isDeclarationAfterDeclarator() {
- // Check for '= delete' or '= default'
- if (getLang().CPlusPlus && Tok.is(tok::equal)) {
- const Token &KW = NextToken();
- if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
- return false;
- }
- return Tok.is(tok::equal) || // int X()= -> not a function def
- Tok.is(tok::comma) || // int X(), -> not a function def
- Tok.is(tok::semi) || // int X(); -> not a function def
- Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
- Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
- (getLang().CPlusPlus &&
- Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
- }
- /// \brief Determine whether the current token, if it occurs after a
- /// declarator, indicates the start of a function definition.
- bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
- assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
- if (Tok.is(tok::l_brace)) // int X() {}
- return true;
-
- // Handle K&R C argument lists: int X(f) int f; {}
- if (!getLang().CPlusPlus &&
- Declarator.getFunctionTypeInfo().isKNRPrototype())
- return isDeclarationSpecifier();
- if (getLang().CPlusPlus && Tok.is(tok::equal)) {
- const Token &KW = NextToken();
- return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
- }
-
- return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
- Tok.is(tok::kw_try); // X() try { ... }
- }
- /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
- /// a declaration. We can't tell which we have until we read up to the
- /// compound-statement in function-definition. TemplateParams, if
- /// non-NULL, provides the template parameters when we're parsing a
- /// C++ template-declaration.
- ///
- /// function-definition: [C99 6.9.1]
- /// decl-specs declarator declaration-list[opt] compound-statement
- /// [C90] function-definition: [C99 6.7.1] - implicit int result
- /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
- ///
- /// declaration: [C99 6.7]
- /// declaration-specifiers init-declarator-list[opt] ';'
- /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
- /// [OMP] threadprivate-directive [TODO]
- ///
- Parser::DeclGroupPtrTy
- Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
- AccessSpecifier AS) {
- // Parse the common declaration-specifiers piece.
- ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
- // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
- // declaration-specifiers init-declarator-list[opt] ';'
- if (Tok.is(tok::semi)) {
- ConsumeToken();
- Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
- DS.complete(TheDecl);
- return Actions.ConvertDeclToDeclGroup(TheDecl);
- }
- // ObjC2 allows prefix attributes on class interfaces and protocols.
- // FIXME: This still needs better diagnostics. We should only accept
- // attributes here, no types, etc.
- if (getLang().ObjC2 && Tok.is(tok::at)) {
- SourceLocation AtLoc = ConsumeToken(); // the "@"
- if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
- !Tok.isObjCAtKeyword(tok::objc_protocol)) {
- Diag(Tok, diag::err_objc_unexpected_attr);
- SkipUntil(tok::semi); // FIXME: better skip?
- return DeclGroupPtrTy();
- }
- DS.abort();
- const char *PrevSpec = 0;
- unsigned DiagID;
- if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
- Diag(AtLoc, DiagID) << PrevSpec;
- Decl *TheDecl = 0;
- if (Tok.isObjCAtKeyword(tok::objc_protocol))
- TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
- else
- TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
- return Actions.ConvertDeclToDeclGroup(TheDecl);
- }
- // If the declspec consisted only of 'extern' and we have a string
- // literal following it, this must be a C++ linkage specifier like
- // 'extern "C"'.
- if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
- DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
- DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
- Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
- return Actions.ConvertDeclToDeclGroup(TheDecl);
- }
- return ParseDeclGroup(DS, Declarator::FileContext, true);
- }
- Parser::DeclGroupPtrTy
- Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
- AccessSpecifier AS) {
- ParsingDeclSpec DS(*this);
- DS.takeAttributesFrom(attrs);
- // Must temporarily exit the objective-c container scope for
- // parsing c constructs and re-enter objc container scope
- // afterwards.
- ObjCDeclContextSwitch ObjCDC(*this);
-
- return ParseDeclarationOrFunctionDefinition(DS, AS);
- }
- /// ParseFunctionDefinition - We parsed and verified that the specified
- /// Declarator is well formed. If this is a K&R-style function, read the
- /// parameters declaration-list, then start the compound-statement.
- ///
- /// function-definition: [C99 6.9.1]
- /// decl-specs declarator declaration-list[opt] compound-statement
- /// [C90] function-definition: [C99 6.7.1] - implicit int result
- /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
- /// [C++] function-definition: [C++ 8.4]
- /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
- /// function-body
- /// [C++] function-definition: [C++ 8.4]
- /// decl-specifier-seq[opt] declarator function-try-block
- ///
- Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
- const ParsedTemplateInfo &TemplateInfo) {
- // Poison the SEH identifiers so they are flagged as illegal in function bodies
- PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
- const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
- // If this is C90 and the declspecs were completely missing, fudge in an
- // implicit int. We do this here because this is the only place where
- // declaration-specifiers are completely optional in the grammar.
- if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
- const char *PrevSpec;
- unsigned DiagID;
- D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
- D.getIdentifierLoc(),
- PrevSpec, DiagID);
- D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
- }
- // If this declaration was formed with a K&R-style identifier list for the
- // arguments, parse declarations for all of the args next.
- // int foo(a,b) int a; float b; {}
- if (FTI.isKNRPrototype())
- ParseKNRParamDeclarations(D);
- // We should have either an opening brace or, in a C++ constructor,
- // we may have a colon.
- if (Tok.isNot(tok::l_brace) &&
- (!getLang().CPlusPlus ||
- (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
- Tok.isNot(tok::equal)))) {
- Diag(Tok, diag::err_expected_fn_body);
- // Skip over garbage, until we get to '{'. Don't eat the '{'.
- SkipUntil(tok::l_brace, true, true);
- // If we didn't find the '{', bail out.
- if (Tok.isNot(tok::l_brace))
- return 0;
- }
- // In delayed template parsing mode, for function template we consume the
- // tokens and store them for late parsing at the end of the translation unit.
- if (getLang().DelayedTemplateParsing &&
- TemplateInfo.Kind == ParsedTemplateInfo::Template) {
- MultiTemplateParamsArg TemplateParameterLists(Actions,
- TemplateInfo.TemplateParams->data(),
- TemplateInfo.TemplateParams->size());
-
- ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
- Scope *ParentScope = getCurScope()->getParent();
- D.setFunctionDefinitionKind(FDK_Definition);
- Decl *DP = Actions.HandleDeclarator(ParentScope, D,
- move(TemplateParameterLists));
- D.complete(DP);
- D.getMutableDeclSpec().abort();
- if (DP) {
- LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
- FunctionDecl *FnD = 0;
- if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
- FnD = FunTmpl->getTemplatedDecl();
- else
- FnD = cast<FunctionDecl>(DP);
- Actions.CheckForFunctionRedefinition(FnD);
- LateParsedTemplateMap[FnD] = LPT;
- Actions.MarkAsLateParsedTemplate(FnD);
- LexTemplateFunctionForLateParsing(LPT->Toks);
- } else {
- CachedTokens Toks;
- LexTemplateFunctionForLateParsing(Toks);
- }
- return DP;
- }
- // Enter a scope for the function body.
- ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
- // Tell the actions module that we have entered a function definition with the
- // specified Declarator for the function.
- Decl *Res = TemplateInfo.TemplateParams?
- Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
- MultiTemplateParamsArg(Actions,
- TemplateInfo.TemplateParams->data(),
- TemplateInfo.TemplateParams->size()),
- D)
- : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
- // Break out of the ParsingDeclarator context before we parse the body.
- D.complete(Res);
-
- // Break out of the ParsingDeclSpec context, too. This const_cast is
- // safe because we're always the sole owner.
- D.getMutableDeclSpec().abort();
- if (Tok.is(tok::equal)) {
- assert(getLang().CPlusPlus && "Only C++ function definitions have '='");
- ConsumeToken();
- Actions.ActOnFinishFunctionBody(Res, 0, false);
-
- bool Delete = false;
- SourceLocation KWLoc;
- if (Tok.is(tok::kw_delete)) {
- Diag(Tok, getLang().CPlusPlus0x ?
- diag::warn_cxx98_compat_deleted_function :
- diag::ext_deleted_function);
- KWLoc = ConsumeToken();
- Actions.SetDeclDeleted(Res, KWLoc);
- Delete = true;
- } else if (Tok.is(tok::kw_default)) {
- Diag(Tok, getLang().CPlusPlus0x ?
- diag::warn_cxx98_compat_defaulted_function :
- diag::ext_defaulted_function);
- KWLoc = ConsumeToken();
- Actions.SetDeclDefaulted(Res, KWLoc);
- } else {
- llvm_unreachable("function definition after = not 'delete' or 'default'");
- }
- if (Tok.is(tok::comma)) {
- Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
- << Delete;
- SkipUntil(tok::semi);
- } else {
- ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
- Delete ? "delete" : "default", tok::semi);
- }
- return Res;
- }
- if (Tok.is(tok::kw_try))
- return ParseFunctionTryBlock(Res, BodyScope);
- // If we have a colon, then we're probably parsing a C++
- // ctor-initializer.
- if (Tok.is(tok::colon)) {
- ParseConstructorInitializer(Res);
- // Recover from error.
- if (!Tok.is(tok::l_brace)) {
- BodyScope.Exit();
- Actions.ActOnFinishFunctionBody(Res, 0);
- return Res;
- }
- } else
- Actions.ActOnDefaultCtorInitializers(Res);
- return ParseFunctionStatementBody(Res, BodyScope);
- }
- /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
- /// types for a function with a K&R-style identifier list for arguments.
- void Parser::ParseKNRParamDeclarations(Declarator &D) {
- // We know that the top-level of this declarator is a function.
- DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
- // Enter function-declaration scope, limiting any declarators to the
- // function prototype scope, including parameter declarators.
- ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
- // Read all the argument declarations.
- while (isDeclarationSpecifier()) {
- SourceLocation DSStart = Tok.getLocation();
- // Parse the common declaration-specifiers piece.
- DeclSpec DS(AttrFactory);
- ParseDeclarationSpecifiers(DS);
- // C99 6.9.1p6: 'each declaration in the declaration list shall have at
- // least one declarator'.
- // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
- // the declarations though. It's trivial to ignore them, really hard to do
- // anything else with them.
- if (Tok.is(tok::semi)) {
- Diag(DSStart, diag::err_declaration_does_not_declare_param);
- ConsumeToken();
- continue;
- }
- // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
- // than register.
- if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
- DS.getStorageClassSpec() != DeclSpec::SCS_register) {
- Diag(DS.getStorageClassSpecLoc(),
- diag::err_invalid_storage_class_in_func_decl);
- DS.ClearStorageClassSpecs();
- }
- if (DS.isThreadSpecified()) {
- Diag(DS.getThreadSpecLoc(),
- diag::err_invalid_storage_class_in_func_decl);
- DS.ClearStorageClassSpecs();
- }
- // Parse the first declarator attached to this declspec.
- Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
- ParseDeclarator(ParmDeclarator);
- // Handle the full declarator list.
- while (1) {
- // If attributes are present, parse them.
- MaybeParseGNUAttributes(ParmDeclarator);
- // Ask the actions module to compute the type for this declarator.
- Decl *Param =
- Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
- if (Param &&
- // A missing identifier has already been diagnosed.
- ParmDeclarator.getIdentifier()) {
- // Scan the argument list looking for the correct param to apply this
- // type.
- for (unsigned i = 0; ; ++i) {
- // C99 6.9.1p6: those declarators shall declare only identifiers from
- // the identifier list.
- if (i == FTI.NumArgs) {
- Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
- << ParmDeclarator.getIdentifier();
- break;
- }
- if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
- // Reject redefinitions of parameters.
- if (FTI.ArgInfo[i].Param) {
- Diag(ParmDeclarator.getIdentifierLoc(),
- diag::err_param_redefinition)
- << ParmDeclarator.getIdentifier();
- } else {
- FTI.ArgInfo[i].Param = Param;
- }
- break;
- }
- }
- }
- // If we don't have a comma, it is either the end of the list (a ';') or
- // an error, bail out.
- if (Tok.isNot(tok::comma))
- break;
- // Consume the comma.
- ConsumeToken();
- // Parse the next declarator.
- ParmDeclarator.clear();
- ParseDeclarator(ParmDeclarator);
- }
- if (Tok.is(tok::semi)) {
- ConsumeToken();
- } else {
- Diag(Tok, diag::err_parse_error);
- // Skip to end of block or statement
- SkipUntil(tok::semi, true);
- if (Tok.is(tok::semi))
- ConsumeToken();
- }
- }
- // The actions module must verify that all arguments were declared.
- Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
- }
- /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
- /// allowed to be a wide string, and is not subject to character translation.
- ///
- /// [GNU] asm-string-literal:
- /// string-literal
- ///
- Parser::ExprResult Parser::ParseAsmStringLiteral() {
- switch (Tok.getKind()) {
- case tok::string_literal:
- break;
- case tok::wide_string_literal: {
- SourceLocation L = Tok.getLocation();
- Diag(Tok, diag::err_asm_operand_wide_string_literal)
- << SourceRange(L, L);
- return ExprError();
- }
- default:
- Diag(Tok, diag::err_expected_string_literal);
- return ExprError();
- }
- ExprResult Res(ParseStringLiteralExpression());
- if (Res.isInvalid()) return move(Res);
- return move(Res);
- }
- /// ParseSimpleAsm
- ///
- /// [GNU] simple-asm-expr:
- /// 'asm' '(' asm-string-literal ')'
- ///
- Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
- assert(Tok.is(tok::kw_asm) && "Not an asm!");
- SourceLocation Loc = ConsumeToken();
- if (Tok.is(tok::kw_volatile)) {
- // Remove from the end of 'asm' to the end of 'volatile'.
- SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
- PP.getLocForEndOfToken(Tok.getLocation()));
- Diag(Tok, diag::warn_file_asm_volatile)
- << FixItHint::CreateRemoval(RemovalRange);
- ConsumeToken();
- }
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected_lparen_after) << "asm";
- return ExprError();
- }
- ExprResult Result(ParseAsmStringLiteral());
- if (Result.isInvalid()) {
- SkipUntil(tok::r_paren, true, true);
- if (EndLoc)
- *EndLoc = Tok.getLocation();
- ConsumeAnyToken();
- } else {
- // Close the paren and get the location of the end bracket
- T.consumeClose();
- if (EndLoc)
- *EndLoc = T.getCloseLocation();
- }
- return move(Result);
- }
- /// \brief Get the TemplateIdAnnotation from the token and put it in the
- /// cleanup pool so that it gets destroyed when parsing the current top level
- /// declaration is finished.
- TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
- assert(tok.is(tok::annot_template_id) && "Expected template-id token");
- TemplateIdAnnotation *
- Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
- TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
- &TemplateIdAnnotation::Destroy>(Id);
- return Id;
- }
- /// TryAnnotateTypeOrScopeToken - If the current token position is on a
- /// typename (possibly qualified in C++) or a C++ scope specifier not followed
- /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
- /// with a single annotation token representing the typename or C++ scope
- /// respectively.
- /// This simplifies handling of C++ scope specifiers and allows efficient
- /// backtracking without the need to re-parse and resolve nested-names and
- /// typenames.
- /// It will mainly be called when we expect to treat identifiers as typenames
- /// (if they are typenames). For example, in C we do not expect identifiers
- /// inside expressions to be treated as typenames so it will not be called
- /// for expressions in C.
- /// The benefit for C/ObjC is that a typename will be annotated and
- /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
- /// will not be called twice, once to check whether we have a declaration
- /// specifier, and another one to get the actual type inside
- /// ParseDeclarationSpecifiers).
- ///
- /// This returns true if an error occurred.
- ///
- /// Note that this routine emits an error if you call it with ::new or ::delete
- /// as the current tokens, so only call it in contexts where these are invalid.
- bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
- assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
- || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
- || Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
- if (Tok.is(tok::kw_typename)) {
- // Parse a C++ typename-specifier, e.g., "typename T::type".
- //
- // typename-specifier:
- // 'typename' '::' [opt] nested-name-specifier identifier
- // 'typename' '::' [opt] nested-name-specifier template [opt]
- // simple-template-id
- SourceLocation TypenameLoc = ConsumeToken();
- CXXScopeSpec SS;
- if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
- /*EnteringContext=*/false,
- 0, /*IsTypename*/true))
- return true;
- if (!SS.isSet()) {
- if (getLang().MicrosoftExt)
- Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
- else
- Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
- return true;
- }
- TypeResult Ty;
- if (Tok.is(tok::identifier)) {
- // FIXME: check whether the next token is '<', first!
- Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
- *Tok.getIdentifierInfo(),
- Tok.getLocation());
- } else if (Tok.is(tok::annot_template_id)) {
- TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
- if (TemplateId->Kind == TNK_Function_template) {
- Diag(Tok, diag::err_typename_refers_to_non_type_template)
- << Tok.getAnnotationRange();
- return true;
- }
- ASTTemplateArgsPtr TemplateArgsPtr(Actions,
- TemplateId->getTemplateArgs(),
- TemplateId->NumArgs);
-
- Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
- /*FIXME:*/SourceLocation(),
- TemplateId->Template,
- TemplateId->TemplateNameLoc,
- TemplateId->LAngleLoc,
- TemplateArgsPtr,
- TemplateId->RAngleLoc);
- } else {
- Diag(Tok, diag::err_expected_type_name_after_typename)
- << SS.getRange();
- return true;
- }
- SourceLocation EndLoc = Tok.getLastLoc();
- Tok.setKind(tok::annot_typename);
- setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
- Tok.setAnnotationEndLoc(EndLoc);
- Tok.setLocation(TypenameLoc);
- PP.AnnotateCachedTokens(Tok);
- return false;
- }
- // Remembers whether the token was originally a scope annotation.
- bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
- CXXScopeSpec SS;
- if (getLang().CPlusPlus)
- if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
- return true;
- if (Tok.is(tok::identifier)) {
- IdentifierInfo *CorrectedII = 0;
- // Determine whether the identifier is a type name.
- if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
- Tok.getLocation(), getCurScope(),
- &SS, false,
- NextToken().is(tok::period),
- ParsedType(),
- /*NonTrivialTypeSourceInfo*/true,
- NeedType ? &CorrectedII : NULL)) {
- // A FixIt was applied as a result of typo correction
- if (CorrectedII)
- Tok.setIdentifierInfo(CorrectedII);
- // This is a typename. Replace the current token in-place with an
- // annotation type token.
- Tok.setKind(tok::annot_typename);
- setTypeAnnotation(Tok, Ty);
- Tok.setAnnotationEndLoc(Tok.getLocation());
- if (SS.isNotEmpty()) // it was a C++ qualified type name.
- Tok.setLocation(SS.getBeginLoc());
- // In case the tokens were cached, have Preprocessor replace
- // them with the annotation token.
- PP.AnnotateCachedTokens(Tok);
- return false;
- }
- if (!getLang().CPlusPlus) {
- // If we're in C, we can't have :: tokens at all (the lexer won't return
- // them). If the identifier is not a type, then it can't be scope either,
- // just early exit.
- return false;
- }
- // If this is a template-id, annotate with a template-id or type token.
- if (NextToken().is(tok::less)) {
- TemplateTy Template;
- UnqualifiedId TemplateName;
- TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
- bool MemberOfUnknownSpecialization;
- if (TemplateNameKind TNK
- = Actions.isTemplateName(getCurScope(), SS,
- /*hasTemplateKeyword=*/false, TemplateName,
- /*ObjectType=*/ ParsedType(),
- EnteringContext,
- Template, MemberOfUnknownSpecialization)) {
- // Consume the identifier.
- ConsumeToken();
- if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName)) {
- // If an unrecoverable error occurred, we need to return true here,
- // because the token stream is in a damaged state. We may not return
- // a valid identifier.
- return true;
- }
- }
- }
- // The current token, which is either an identifier or a
- // template-id, is not part of the annotation. Fall through to
- // push that token back into the stream and complete the C++ scope
- // specifier annotation.
- }
- if (Tok.is(tok::annot_template_id)) {
- TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
- if (TemplateId->Kind == TNK_Type_template) {
- // A template-id that refers to a type was parsed into a
- // template-id annotation in a context where we weren't allowed
- // to produce a type annotation token. Update the template-id
- // annotation token to a type annotation token now.
- AnnotateTemplateIdTokenAsType();
- return false;
- }
- }
- if (SS.isEmpty())
- return false;
- // A C++ scope specifier that isn't followed by a typename.
- // Push the current token back into the token stream (or revert it if it is
- // cached) and use an annotation scope token for current token.
- if (PP.isBacktrackEnabled())
- PP.RevertCachedTokens(1);
- else
- PP.EnterToken(Tok);
- Tok.setKind(tok::annot_cxxscope);
- Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
- Tok.setAnnotationRange(SS.getRange());
- // In case the tokens were cached, have Preprocessor replace them
- // with the annotation token. We don't need to do this if we've
- // just reverted back to the state we were in before being called.
- if (!wasScopeAnnotation)
- PP.AnnotateCachedTokens(Tok);
- return false;
- }
- /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
- /// annotates C++ scope specifiers and template-ids. This returns
- /// true if the token was annotated or there was an error that could not be
- /// recovered from.
- ///
- /// Note that this routine emits an error if you call it with ::new or ::delete
- /// as the current tokens, so only call it in contexts where these are invalid.
- bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
- assert(getLang().CPlusPlus &&
- "Call sites of this function should be guarded by checking for C++");
- assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
- (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
- Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
- CXXScopeSpec SS;
- if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
- return true;
- if (SS.isEmpty())
- return false;
- // Push the current token back into the token stream (or revert it if it is
- // cached) and use an annotation scope token for current token.
- if (PP.isBacktrackEnabled())
- PP.RevertCachedTokens(1);
- else
- PP.EnterToken(Tok);
- Tok.setKind(tok::annot_cxxscope);
- Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
- Tok.setAnnotationRange(SS.getRange());
- // In case the tokens were cached, have Preprocessor replace them with the
- // annotation token.
- PP.AnnotateCachedTokens(Tok);
- return false;
- }
- bool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) {
- if (Tok.is(tok::equalequal)) {
- // We have '==' in a context that we would expect a '='.
- // The user probably made a typo, intending to type '='. Emit diagnostic,
- // fixit hint to turn '==' -> '=' and continue as if the user typed '='.
- Diag(Tok, DiagID)
- << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()),
- getTokenSimpleSpelling(tok::equal));
- return true;
- }
- return Tok.is(tok::equal);
- }
- SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
- assert(Tok.is(tok::code_completion));
- PrevTokLocation = Tok.getLocation();
- for (Scope *S = getCurScope(); S; S = S->getParent()) {
- if (S->getFlags() & Scope::FnScope) {
- Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
- cutOffParsing();
- return PrevTokLocation;
- }
-
- if (S->getFlags() & Scope::ClassScope) {
- Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
- cutOffParsing();
- return PrevTokLocation;
- }
- }
-
- Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
- cutOffParsing();
- return PrevTokLocation;
- }
- // Anchor the Parser::FieldCallback vtable to this translation unit.
- // We use a spurious method instead of the destructor because
- // destroying FieldCallbacks can actually be slightly
- // performance-sensitive.
- void Parser::FieldCallback::_anchor() {
- }
- // Code-completion pass-through functions
- void Parser::CodeCompleteDirective(bool InConditional) {
- Actions.CodeCompletePreprocessorDirective(InConditional);
- }
- void Parser::CodeCompleteInConditionalExclusion() {
- Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
- }
- void Parser::CodeCompleteMacroName(bool IsDefinition) {
- Actions.CodeCompletePreprocessorMacroName(IsDefinition);
- }
- void Parser::CodeCompletePreprocessorExpression() {
- Actions.CodeCompletePreprocessorExpression();
- }
- void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
- MacroInfo *MacroInfo,
- unsigned ArgumentIndex) {
- Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
- ArgumentIndex);
- }
- void Parser::CodeCompleteNaturalLanguage() {
- Actions.CodeCompleteNaturalLanguage();
- }
- bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
- assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
- "Expected '__if_exists' or '__if_not_exists'");
- Result.IsIfExists = Tok.is(tok::kw___if_exists);
- Result.KeywordLoc = ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected_lparen_after)
- << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
- return true;
- }
-
- // Parse nested-name-specifier.
- ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
- /*EnteringContext=*/false);
- // Check nested-name specifier.
- if (Result.SS.isInvalid()) {
- T.skipToEnd();
- return true;
- }
- // Parse the unqualified-id.
- if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
- Result.Name)) {
- T.skipToEnd();
- return true;
- }
- if (T.consumeClose())
- return true;
-
- // Check if the symbol exists.
- switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
- Result.IsIfExists, Result.SS,
- Result.Name)) {
- case Sema::IER_Exists:
- Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
- break;
- case Sema::IER_DoesNotExist:
- Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
- break;
- case Sema::IER_Dependent:
- Result.Behavior = IEB_Dependent;
- break;
-
- case Sema::IER_Error:
- return true;
- }
- return false;
- }
- void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
- IfExistsCondition Result;
- if (ParseMicrosoftIfExistsCondition(Result))
- return;
-
- BalancedDelimiterTracker Braces(*this, tok::l_brace);
- if (Braces.consumeOpen()) {
- Diag(Tok, diag::err_expected_lbrace);
- return;
- }
- switch (Result.Behavior) {
- case IEB_Parse:
- // Parse declarations below.
- break;
-
- case IEB_Dependent:
- llvm_unreachable("Cannot have a dependent external declaration");
-
- case IEB_Skip:
- Braces.skipToEnd();
- return;
- }
- // Parse the declarations.
- while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX0XAttributes(attrs);
- MaybeParseMicrosoftAttributes(attrs);
- DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
- if (Result && !getCurScope()->getParent())
- Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
- }
- Braces.consumeClose();
- }
- Parser::DeclGroupPtrTy Parser::ParseModuleImport() {
- assert(Tok.is(tok::kw___import_module__) &&
- "Improper start to module import");
- SourceLocation ImportLoc = ConsumeToken();
-
- llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
-
- // Parse the module path.
- do {
- if (!Tok.is(tok::identifier)) {
- Diag(Tok, diag::err_module_expected_ident);
- SkipUntil(tok::semi);
- return DeclGroupPtrTy();
- }
-
- // Record this part of the module path.
- Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
- ConsumeToken();
-
- if (Tok.is(tok::period)) {
- ConsumeToken();
- continue;
- }
-
- break;
- } while (true);
-
- DeclResult Import = Actions.ActOnModuleImport(ImportLoc, Path);
- ExpectAndConsumeSemi(diag::err_module_expected_semi);
- if (Import.isInvalid())
- return DeclGroupPtrTy();
-
- return Actions.ConvertDeclToDeclGroup(Import.get());
- }
- bool Parser::BalancedDelimiterTracker::consumeOpen() {
- // Try to consume the token we are holding
- if (P.Tok.is(Kind)) {
- P.QuantityTracker.push(Kind);
- Cleanup = true;
- if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
- LOpen = P.ConsumeAnyToken();
- return false;
- } else {
- P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
- P.SkipUntil(tok::eof);
- }
- }
- return true;
- }
- bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
- const char *Msg,
- tok::TokenKind SkipToToc ) {
- LOpen = P.Tok.getLocation();
- if (!P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc)) {
- P.QuantityTracker.push(Kind);
- Cleanup = true;
- if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
- return false;
- } else {
- P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
- P.SkipUntil(tok::eof);
- }
- }
- return true;
- }
- bool Parser::BalancedDelimiterTracker::consumeClose() {
- if (P.Tok.is(Close)) {
- LClose = P.ConsumeAnyToken();
- if (Cleanup)
- P.QuantityTracker.pop(Kind);
- Cleanup = false;
- return false;
- } else {
- const char *LHSName = "unknown";
- diag::kind DID = diag::err_parse_error;
- switch (Close) {
- default: break;
- case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
- case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
- case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
- case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
- case tok::greatergreatergreater:
- LHSName = "<<<"; DID = diag::err_expected_ggg; break;
- }
- P.Diag(P.Tok, DID);
- P.Diag(LOpen, diag::note_matching) << LHSName;
- if (P.SkipUntil(Close))
- LClose = P.Tok.getLocation();
- }
- return true;
- }
- void Parser::BalancedDelimiterTracker::skipToEnd() {
- P.SkipUntil(Close, false);
- Cleanup = false;
- }
|