Parser.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. //===--- Parser.cpp - C Language Family Parser ----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Parser interfaces.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Parse/Parser.h"
  14. #include "clang/Parse/ParseDiagnostic.h"
  15. #include "clang/Sema/DeclSpec.h"
  16. #include "clang/Sema/Scope.h"
  17. #include "clang/Sema/ParsedTemplate.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "RAIIObjectsForParser.h"
  20. #include "ParsePragma.h"
  21. #include "clang/AST/DeclTemplate.h"
  22. #include "clang/AST/ASTConsumer.h"
  23. using namespace clang;
  24. namespace {
  25. /// \brief A comment handler that passes comments found by the preprocessor
  26. /// to the parser action.
  27. class ActionCommentHandler : public CommentHandler {
  28. Sema &S;
  29. public:
  30. explicit ActionCommentHandler(Sema &S) : S(S) { }
  31. virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) {
  32. S.ActOnComment(Comment);
  33. return false;
  34. }
  35. };
  36. } // end anonymous namespace
  37. IdentifierInfo *Parser::getSEHExceptKeyword() {
  38. // __except is accepted as a (contextual) keyword
  39. if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
  40. Ident__except = PP.getIdentifierInfo("__except");
  41. return Ident__except;
  42. }
  43. Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
  44. : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
  45. GreaterThanIsOperator(true), ColonIsSacred(false),
  46. InMessageExpression(false), TemplateParameterDepth(0),
  47. ParsingInObjCContainer(false) {
  48. SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
  49. Tok.startToken();
  50. Tok.setKind(tok::eof);
  51. Actions.CurScope = 0;
  52. NumCachedScopes = 0;
  53. ParenCount = BracketCount = BraceCount = 0;
  54. CurParsedObjCImpl = 0;
  55. // Add #pragma handlers. These are removed and destroyed in the
  56. // destructor.
  57. AlignHandler.reset(new PragmaAlignHandler());
  58. PP.AddPragmaHandler(AlignHandler.get());
  59. GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
  60. PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
  61. OptionsHandler.reset(new PragmaOptionsHandler());
  62. PP.AddPragmaHandler(OptionsHandler.get());
  63. PackHandler.reset(new PragmaPackHandler());
  64. PP.AddPragmaHandler(PackHandler.get());
  65. MSStructHandler.reset(new PragmaMSStructHandler());
  66. PP.AddPragmaHandler(MSStructHandler.get());
  67. UnusedHandler.reset(new PragmaUnusedHandler());
  68. PP.AddPragmaHandler(UnusedHandler.get());
  69. WeakHandler.reset(new PragmaWeakHandler());
  70. PP.AddPragmaHandler(WeakHandler.get());
  71. RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
  72. PP.AddPragmaHandler(RedefineExtnameHandler.get());
  73. FPContractHandler.reset(new PragmaFPContractHandler());
  74. PP.AddPragmaHandler("STDC", FPContractHandler.get());
  75. if (getLangOpts().OpenCL) {
  76. OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
  77. PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
  78. PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
  79. }
  80. CommentSemaHandler.reset(new ActionCommentHandler(actions));
  81. PP.addCommentHandler(CommentSemaHandler.get());
  82. PP.setCodeCompletionHandler(*this);
  83. }
  84. /// If a crash happens while the parser is active, print out a line indicating
  85. /// what the current token is.
  86. void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
  87. const Token &Tok = P.getCurToken();
  88. if (Tok.is(tok::eof)) {
  89. OS << "<eof> parser at end of file\n";
  90. return;
  91. }
  92. if (Tok.getLocation().isInvalid()) {
  93. OS << "<unknown> parser at unknown location\n";
  94. return;
  95. }
  96. const Preprocessor &PP = P.getPreprocessor();
  97. Tok.getLocation().print(OS, PP.getSourceManager());
  98. if (Tok.isAnnotation())
  99. OS << ": at annotation token \n";
  100. else
  101. OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
  102. }
  103. DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
  104. return Diags.Report(Loc, DiagID);
  105. }
  106. DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
  107. return Diag(Tok.getLocation(), DiagID);
  108. }
  109. /// \brief Emits a diagnostic suggesting parentheses surrounding a
  110. /// given range.
  111. ///
  112. /// \param Loc The location where we'll emit the diagnostic.
  113. /// \param DK The kind of diagnostic to emit.
  114. /// \param ParenRange Source range enclosing code that should be parenthesized.
  115. void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
  116. SourceRange ParenRange) {
  117. SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
  118. if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
  119. // We can't display the parentheses, so just dig the
  120. // warning/error and return.
  121. Diag(Loc, DK);
  122. return;
  123. }
  124. Diag(Loc, DK)
  125. << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
  126. << FixItHint::CreateInsertion(EndLoc, ")");
  127. }
  128. static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
  129. switch (ExpectedTok) {
  130. case tok::semi:
  131. return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
  132. default: return false;
  133. }
  134. }
  135. /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
  136. /// input. If so, it is consumed and false is returned.
  137. ///
  138. /// If the input is malformed, this emits the specified diagnostic. Next, if
  139. /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
  140. /// returned.
  141. bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
  142. const char *Msg, tok::TokenKind SkipToTok) {
  143. if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
  144. ConsumeAnyToken();
  145. return false;
  146. }
  147. // Detect common single-character typos and resume.
  148. if (IsCommonTypo(ExpectedTok, Tok)) {
  149. SourceLocation Loc = Tok.getLocation();
  150. Diag(Loc, DiagID)
  151. << Msg
  152. << FixItHint::CreateReplacement(SourceRange(Loc),
  153. getTokenSimpleSpelling(ExpectedTok));
  154. ConsumeAnyToken();
  155. // Pretend there wasn't a problem.
  156. return false;
  157. }
  158. const char *Spelling = 0;
  159. SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
  160. if (EndLoc.isValid() &&
  161. (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
  162. // Show what code to insert to fix this problem.
  163. Diag(EndLoc, DiagID)
  164. << Msg
  165. << FixItHint::CreateInsertion(EndLoc, Spelling);
  166. } else
  167. Diag(Tok, DiagID) << Msg;
  168. if (SkipToTok != tok::unknown)
  169. SkipUntil(SkipToTok);
  170. return true;
  171. }
  172. bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
  173. if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
  174. ConsumeToken();
  175. return false;
  176. }
  177. if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
  178. NextToken().is(tok::semi)) {
  179. Diag(Tok, diag::err_extraneous_token_before_semi)
  180. << PP.getSpelling(Tok)
  181. << FixItHint::CreateRemoval(Tok.getLocation());
  182. ConsumeAnyToken(); // The ')' or ']'.
  183. ConsumeToken(); // The ';'.
  184. return false;
  185. }
  186. return ExpectAndConsume(tok::semi, DiagID);
  187. }
  188. void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) {
  189. if (!Tok.is(tok::semi)) return;
  190. bool HadMultipleSemis = false;
  191. SourceLocation StartLoc = Tok.getLocation();
  192. SourceLocation EndLoc = Tok.getLocation();
  193. ConsumeToken();
  194. while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
  195. HadMultipleSemis = true;
  196. EndLoc = Tok.getLocation();
  197. ConsumeToken();
  198. }
  199. // C++11 allows extra semicolons at namespace scope, but not in any of the
  200. // other contexts.
  201. if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
  202. if (getLangOpts().CPlusPlus0x)
  203. Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
  204. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  205. else
  206. Diag(StartLoc, diag::ext_extra_semi_cxx11)
  207. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  208. return;
  209. }
  210. if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
  211. Diag(StartLoc, diag::ext_extra_semi)
  212. << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST)
  213. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  214. else
  215. // A single semicolon is valid after a member function definition.
  216. Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
  217. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  218. }
  219. //===----------------------------------------------------------------------===//
  220. // Error recovery.
  221. //===----------------------------------------------------------------------===//
  222. /// SkipUntil - Read tokens until we get to the specified token, then consume
  223. /// it (unless DontConsume is true). Because we cannot guarantee that the
  224. /// token will ever occur, this skips to the next token, or to some likely
  225. /// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
  226. /// character.
  227. ///
  228. /// If SkipUntil finds the specified token, it returns true, otherwise it
  229. /// returns false.
  230. bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, bool StopAtSemi,
  231. bool DontConsume, bool StopAtCodeCompletion) {
  232. // We always want this function to skip at least one token if the first token
  233. // isn't T and if not at EOF.
  234. bool isFirstTokenSkipped = true;
  235. while (1) {
  236. // If we found one of the tokens, stop and return true.
  237. for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
  238. if (Tok.is(Toks[i])) {
  239. if (DontConsume) {
  240. // Noop, don't consume the token.
  241. } else {
  242. ConsumeAnyToken();
  243. }
  244. return true;
  245. }
  246. }
  247. switch (Tok.getKind()) {
  248. case tok::eof:
  249. // Ran out of tokens.
  250. return false;
  251. case tok::code_completion:
  252. if (!StopAtCodeCompletion)
  253. ConsumeToken();
  254. return false;
  255. case tok::l_paren:
  256. // Recursively skip properly-nested parens.
  257. ConsumeParen();
  258. SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
  259. break;
  260. case tok::l_square:
  261. // Recursively skip properly-nested square brackets.
  262. ConsumeBracket();
  263. SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
  264. break;
  265. case tok::l_brace:
  266. // Recursively skip properly-nested braces.
  267. ConsumeBrace();
  268. SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
  269. break;
  270. // Okay, we found a ']' or '}' or ')', which we think should be balanced.
  271. // Since the user wasn't looking for this token (if they were, it would
  272. // already be handled), this isn't balanced. If there is a LHS token at a
  273. // higher level, we will assume that this matches the unbalanced token
  274. // and return it. Otherwise, this is a spurious RHS token, which we skip.
  275. case tok::r_paren:
  276. if (ParenCount && !isFirstTokenSkipped)
  277. return false; // Matches something.
  278. ConsumeParen();
  279. break;
  280. case tok::r_square:
  281. if (BracketCount && !isFirstTokenSkipped)
  282. return false; // Matches something.
  283. ConsumeBracket();
  284. break;
  285. case tok::r_brace:
  286. if (BraceCount && !isFirstTokenSkipped)
  287. return false; // Matches something.
  288. ConsumeBrace();
  289. break;
  290. case tok::string_literal:
  291. case tok::wide_string_literal:
  292. case tok::utf8_string_literal:
  293. case tok::utf16_string_literal:
  294. case tok::utf32_string_literal:
  295. ConsumeStringToken();
  296. break;
  297. case tok::semi:
  298. if (StopAtSemi)
  299. return false;
  300. // FALL THROUGH.
  301. default:
  302. // Skip this token.
  303. ConsumeToken();
  304. break;
  305. }
  306. isFirstTokenSkipped = false;
  307. }
  308. }
  309. //===----------------------------------------------------------------------===//
  310. // Scope manipulation
  311. //===----------------------------------------------------------------------===//
  312. /// EnterScope - Start a new scope.
  313. void Parser::EnterScope(unsigned ScopeFlags) {
  314. if (NumCachedScopes) {
  315. Scope *N = ScopeCache[--NumCachedScopes];
  316. N->Init(getCurScope(), ScopeFlags);
  317. Actions.CurScope = N;
  318. } else {
  319. Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
  320. }
  321. }
  322. /// ExitScope - Pop a scope off the scope stack.
  323. void Parser::ExitScope() {
  324. assert(getCurScope() && "Scope imbalance!");
  325. // Inform the actions module that this scope is going away if there are any
  326. // decls in it.
  327. if (!getCurScope()->decl_empty())
  328. Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
  329. Scope *OldScope = getCurScope();
  330. Actions.CurScope = OldScope->getParent();
  331. if (NumCachedScopes == ScopeCacheSize)
  332. delete OldScope;
  333. else
  334. ScopeCache[NumCachedScopes++] = OldScope;
  335. }
  336. /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
  337. /// this object does nothing.
  338. Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
  339. bool ManageFlags)
  340. : CurScope(ManageFlags ? Self->getCurScope() : 0) {
  341. if (CurScope) {
  342. OldFlags = CurScope->getFlags();
  343. CurScope->setFlags(ScopeFlags);
  344. }
  345. }
  346. /// Restore the flags for the current scope to what they were before this
  347. /// object overrode them.
  348. Parser::ParseScopeFlags::~ParseScopeFlags() {
  349. if (CurScope)
  350. CurScope->setFlags(OldFlags);
  351. }
  352. //===----------------------------------------------------------------------===//
  353. // C99 6.9: External Definitions.
  354. //===----------------------------------------------------------------------===//
  355. Parser::~Parser() {
  356. // If we still have scopes active, delete the scope tree.
  357. delete getCurScope();
  358. Actions.CurScope = 0;
  359. // Free the scope cache.
  360. for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
  361. delete ScopeCache[i];
  362. // Free LateParsedTemplatedFunction nodes.
  363. for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
  364. it != LateParsedTemplateMap.end(); ++it)
  365. delete it->second;
  366. // Remove the pragma handlers we installed.
  367. PP.RemovePragmaHandler(AlignHandler.get());
  368. AlignHandler.reset();
  369. PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
  370. GCCVisibilityHandler.reset();
  371. PP.RemovePragmaHandler(OptionsHandler.get());
  372. OptionsHandler.reset();
  373. PP.RemovePragmaHandler(PackHandler.get());
  374. PackHandler.reset();
  375. PP.RemovePragmaHandler(MSStructHandler.get());
  376. MSStructHandler.reset();
  377. PP.RemovePragmaHandler(UnusedHandler.get());
  378. UnusedHandler.reset();
  379. PP.RemovePragmaHandler(WeakHandler.get());
  380. WeakHandler.reset();
  381. PP.RemovePragmaHandler(RedefineExtnameHandler.get());
  382. RedefineExtnameHandler.reset();
  383. if (getLangOpts().OpenCL) {
  384. PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
  385. OpenCLExtensionHandler.reset();
  386. PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
  387. }
  388. PP.RemovePragmaHandler("STDC", FPContractHandler.get());
  389. FPContractHandler.reset();
  390. PP.removeCommentHandler(CommentSemaHandler.get());
  391. PP.clearCodeCompletionHandler();
  392. assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?");
  393. }
  394. /// Initialize - Warm up the parser.
  395. ///
  396. void Parser::Initialize() {
  397. // Create the translation unit scope. Install it as the current scope.
  398. assert(getCurScope() == 0 && "A scope is already active?");
  399. EnterScope(Scope::DeclScope);
  400. Actions.ActOnTranslationUnitScope(getCurScope());
  401. // Initialization for Objective-C context sensitive keywords recognition.
  402. // Referenced in Parser::ParseObjCTypeQualifierList.
  403. if (getLangOpts().ObjC1) {
  404. ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
  405. ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
  406. ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
  407. ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
  408. ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
  409. ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
  410. }
  411. Ident_instancetype = 0;
  412. Ident_final = 0;
  413. Ident_override = 0;
  414. Ident_super = &PP.getIdentifierTable().get("super");
  415. if (getLangOpts().AltiVec) {
  416. Ident_vector = &PP.getIdentifierTable().get("vector");
  417. Ident_pixel = &PP.getIdentifierTable().get("pixel");
  418. }
  419. Ident_introduced = 0;
  420. Ident_deprecated = 0;
  421. Ident_obsoleted = 0;
  422. Ident_unavailable = 0;
  423. Ident__except = 0;
  424. Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
  425. Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
  426. Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
  427. if(getLangOpts().Borland) {
  428. Ident__exception_info = PP.getIdentifierInfo("_exception_info");
  429. Ident___exception_info = PP.getIdentifierInfo("__exception_info");
  430. Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
  431. Ident__exception_code = PP.getIdentifierInfo("_exception_code");
  432. Ident___exception_code = PP.getIdentifierInfo("__exception_code");
  433. Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
  434. Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
  435. Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
  436. Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
  437. PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
  438. PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
  439. PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
  440. PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
  441. PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
  442. PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
  443. PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
  444. PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
  445. PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
  446. }
  447. Actions.Initialize();
  448. // Prime the lexer look-ahead.
  449. ConsumeToken();
  450. }
  451. namespace {
  452. /// \brief RAIIObject to destroy the contents of a SmallVector of
  453. /// TemplateIdAnnotation pointers and clear the vector.
  454. class DestroyTemplateIdAnnotationsRAIIObj {
  455. SmallVectorImpl<TemplateIdAnnotation *> &Container;
  456. public:
  457. DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *>
  458. &Container)
  459. : Container(Container) {}
  460. ~DestroyTemplateIdAnnotationsRAIIObj() {
  461. for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
  462. Container.begin(), E = Container.end();
  463. I != E; ++I)
  464. (*I)->Destroy();
  465. Container.clear();
  466. }
  467. };
  468. }
  469. /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
  470. /// action tells us to. This returns true if the EOF was encountered.
  471. bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
  472. DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
  473. // Skip over the EOF token, flagging end of previous input for incremental
  474. // processing
  475. if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
  476. ConsumeToken();
  477. while (Tok.is(tok::annot_pragma_unused))
  478. HandlePragmaUnused();
  479. Result = DeclGroupPtrTy();
  480. if (Tok.is(tok::eof)) {
  481. // Late template parsing can begin.
  482. if (getLangOpts().DelayedTemplateParsing)
  483. Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
  484. if (!PP.isIncrementalProcessingEnabled())
  485. Actions.ActOnEndOfTranslationUnit();
  486. //else don't tell Sema that we ended parsing: more input might come.
  487. return true;
  488. }
  489. ParsedAttributesWithRange attrs(AttrFactory);
  490. MaybeParseCXX0XAttributes(attrs);
  491. MaybeParseMicrosoftAttributes(attrs);
  492. Result = ParseExternalDeclaration(attrs);
  493. return false;
  494. }
  495. /// ParseTranslationUnit:
  496. /// translation-unit: [C99 6.9]
  497. /// external-declaration
  498. /// translation-unit external-declaration
  499. void Parser::ParseTranslationUnit() {
  500. Initialize();
  501. DeclGroupPtrTy Res;
  502. while (!ParseTopLevelDecl(Res))
  503. /*parse them all*/;
  504. ExitScope();
  505. assert(getCurScope() == 0 && "Scope imbalance!");
  506. }
  507. /// ParseExternalDeclaration:
  508. ///
  509. /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
  510. /// function-definition
  511. /// declaration
  512. /// [C++0x] empty-declaration
  513. /// [GNU] asm-definition
  514. /// [GNU] __extension__ external-declaration
  515. /// [OBJC] objc-class-definition
  516. /// [OBJC] objc-class-declaration
  517. /// [OBJC] objc-alias-declaration
  518. /// [OBJC] objc-protocol-definition
  519. /// [OBJC] objc-method-definition
  520. /// [OBJC] @end
  521. /// [C++] linkage-specification
  522. /// [GNU] asm-definition:
  523. /// simple-asm-expr ';'
  524. ///
  525. /// [C++0x] empty-declaration:
  526. /// ';'
  527. ///
  528. /// [C++0x/GNU] 'extern' 'template' declaration
  529. Parser::DeclGroupPtrTy
  530. Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
  531. ParsingDeclSpec *DS) {
  532. DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
  533. ParenBraceBracketBalancer BalancerRAIIObj(*this);
  534. if (PP.isCodeCompletionReached()) {
  535. cutOffParsing();
  536. return DeclGroupPtrTy();
  537. }
  538. Decl *SingleDecl = 0;
  539. switch (Tok.getKind()) {
  540. case tok::annot_pragma_vis:
  541. HandlePragmaVisibility();
  542. return DeclGroupPtrTy();
  543. case tok::annot_pragma_pack:
  544. HandlePragmaPack();
  545. return DeclGroupPtrTy();
  546. case tok::annot_pragma_msstruct:
  547. HandlePragmaMSStruct();
  548. return DeclGroupPtrTy();
  549. case tok::annot_pragma_align:
  550. HandlePragmaAlign();
  551. return DeclGroupPtrTy();
  552. case tok::annot_pragma_weak:
  553. HandlePragmaWeak();
  554. return DeclGroupPtrTy();
  555. case tok::annot_pragma_weakalias:
  556. HandlePragmaWeakAlias();
  557. return DeclGroupPtrTy();
  558. case tok::annot_pragma_redefine_extname:
  559. HandlePragmaRedefineExtname();
  560. return DeclGroupPtrTy();
  561. case tok::annot_pragma_fp_contract:
  562. HandlePragmaFPContract();
  563. return DeclGroupPtrTy();
  564. case tok::annot_pragma_opencl_extension:
  565. HandlePragmaOpenCLExtension();
  566. return DeclGroupPtrTy();
  567. case tok::semi:
  568. ConsumeExtraSemi(OutsideFunction);
  569. // TODO: Invoke action for top-level semicolon.
  570. return DeclGroupPtrTy();
  571. case tok::r_brace:
  572. Diag(Tok, diag::err_extraneous_closing_brace);
  573. ConsumeBrace();
  574. return DeclGroupPtrTy();
  575. case tok::eof:
  576. Diag(Tok, diag::err_expected_external_declaration);
  577. return DeclGroupPtrTy();
  578. case tok::kw___extension__: {
  579. // __extension__ silences extension warnings in the subexpression.
  580. ExtensionRAIIObject O(Diags); // Use RAII to do this.
  581. ConsumeToken();
  582. return ParseExternalDeclaration(attrs);
  583. }
  584. case tok::kw_asm: {
  585. ProhibitAttributes(attrs);
  586. SourceLocation StartLoc = Tok.getLocation();
  587. SourceLocation EndLoc;
  588. ExprResult Result(ParseSimpleAsm(&EndLoc));
  589. ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
  590. "top-level asm block");
  591. if (Result.isInvalid())
  592. return DeclGroupPtrTy();
  593. SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
  594. break;
  595. }
  596. case tok::at:
  597. return ParseObjCAtDirectives();
  598. case tok::minus:
  599. case tok::plus:
  600. if (!getLangOpts().ObjC1) {
  601. Diag(Tok, diag::err_expected_external_declaration);
  602. ConsumeToken();
  603. return DeclGroupPtrTy();
  604. }
  605. SingleDecl = ParseObjCMethodDefinition();
  606. break;
  607. case tok::code_completion:
  608. Actions.CodeCompleteOrdinaryName(getCurScope(),
  609. CurParsedObjCImpl? Sema::PCC_ObjCImplementation
  610. : Sema::PCC_Namespace);
  611. cutOffParsing();
  612. return DeclGroupPtrTy();
  613. case tok::kw_using:
  614. case tok::kw_namespace:
  615. case tok::kw_typedef:
  616. case tok::kw_template:
  617. case tok::kw_export: // As in 'export template'
  618. case tok::kw_static_assert:
  619. case tok::kw__Static_assert:
  620. // A function definition cannot start with any of these keywords.
  621. {
  622. SourceLocation DeclEnd;
  623. StmtVector Stmts;
  624. return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
  625. }
  626. case tok::kw_static:
  627. // Parse (then ignore) 'static' prior to a template instantiation. This is
  628. // a GCC extension that we intentionally do not support.
  629. if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
  630. Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
  631. << 0;
  632. SourceLocation DeclEnd;
  633. StmtVector Stmts;
  634. return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
  635. }
  636. goto dont_know;
  637. case tok::kw_inline:
  638. if (getLangOpts().CPlusPlus) {
  639. tok::TokenKind NextKind = NextToken().getKind();
  640. // Inline namespaces. Allowed as an extension even in C++03.
  641. if (NextKind == tok::kw_namespace) {
  642. SourceLocation DeclEnd;
  643. StmtVector Stmts;
  644. return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
  645. }
  646. // Parse (then ignore) 'inline' prior to a template instantiation. This is
  647. // a GCC extension that we intentionally do not support.
  648. if (NextKind == tok::kw_template) {
  649. Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
  650. << 1;
  651. SourceLocation DeclEnd;
  652. StmtVector Stmts;
  653. return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
  654. }
  655. }
  656. goto dont_know;
  657. case tok::kw_extern:
  658. if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
  659. // Extern templates
  660. SourceLocation ExternLoc = ConsumeToken();
  661. SourceLocation TemplateLoc = ConsumeToken();
  662. Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
  663. diag::warn_cxx98_compat_extern_template :
  664. diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
  665. SourceLocation DeclEnd;
  666. return Actions.ConvertDeclToDeclGroup(
  667. ParseExplicitInstantiation(Declarator::FileContext,
  668. ExternLoc, TemplateLoc, DeclEnd));
  669. }
  670. // FIXME: Detect C++ linkage specifications here?
  671. goto dont_know;
  672. case tok::kw___if_exists:
  673. case tok::kw___if_not_exists:
  674. ParseMicrosoftIfExistsExternalDeclaration();
  675. return DeclGroupPtrTy();
  676. default:
  677. dont_know:
  678. // We can't tell whether this is a function-definition or declaration yet.
  679. if (DS) {
  680. return ParseDeclarationOrFunctionDefinition(attrs, DS);
  681. } else {
  682. return ParseDeclarationOrFunctionDefinition(attrs);
  683. }
  684. }
  685. // This routine returns a DeclGroup, if the thing we parsed only contains a
  686. // single decl, convert it now.
  687. return Actions.ConvertDeclToDeclGroup(SingleDecl);
  688. }
  689. /// \brief Determine whether the current token, if it occurs after a
  690. /// declarator, continues a declaration or declaration list.
  691. bool Parser::isDeclarationAfterDeclarator() {
  692. // Check for '= delete' or '= default'
  693. if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
  694. const Token &KW = NextToken();
  695. if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
  696. return false;
  697. }
  698. return Tok.is(tok::equal) || // int X()= -> not a function def
  699. Tok.is(tok::comma) || // int X(), -> not a function def
  700. Tok.is(tok::semi) || // int X(); -> not a function def
  701. Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
  702. Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
  703. (getLangOpts().CPlusPlus &&
  704. Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
  705. }
  706. /// \brief Determine whether the current token, if it occurs after a
  707. /// declarator, indicates the start of a function definition.
  708. bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
  709. assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
  710. if (Tok.is(tok::l_brace)) // int X() {}
  711. return true;
  712. // Handle K&R C argument lists: int X(f) int f; {}
  713. if (!getLangOpts().CPlusPlus &&
  714. Declarator.getFunctionTypeInfo().isKNRPrototype())
  715. return isDeclarationSpecifier();
  716. if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
  717. const Token &KW = NextToken();
  718. return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
  719. }
  720. return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
  721. Tok.is(tok::kw_try); // X() try { ... }
  722. }
  723. /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
  724. /// a declaration. We can't tell which we have until we read up to the
  725. /// compound-statement in function-definition. TemplateParams, if
  726. /// non-NULL, provides the template parameters when we're parsing a
  727. /// C++ template-declaration.
  728. ///
  729. /// function-definition: [C99 6.9.1]
  730. /// decl-specs declarator declaration-list[opt] compound-statement
  731. /// [C90] function-definition: [C99 6.7.1] - implicit int result
  732. /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
  733. ///
  734. /// declaration: [C99 6.7]
  735. /// declaration-specifiers init-declarator-list[opt] ';'
  736. /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
  737. /// [OMP] threadprivate-directive [TODO]
  738. ///
  739. Parser::DeclGroupPtrTy
  740. Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
  741. ParsingDeclSpec &DS,
  742. AccessSpecifier AS) {
  743. // Parse the common declaration-specifiers piece.
  744. ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
  745. // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
  746. // declaration-specifiers init-declarator-list[opt] ';'
  747. if (Tok.is(tok::semi)) {
  748. ProhibitAttributes(attrs);
  749. ConsumeToken();
  750. Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
  751. DS.complete(TheDecl);
  752. return Actions.ConvertDeclToDeclGroup(TheDecl);
  753. }
  754. DS.takeAttributesFrom(attrs);
  755. // ObjC2 allows prefix attributes on class interfaces and protocols.
  756. // FIXME: This still needs better diagnostics. We should only accept
  757. // attributes here, no types, etc.
  758. if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
  759. SourceLocation AtLoc = ConsumeToken(); // the "@"
  760. if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
  761. !Tok.isObjCAtKeyword(tok::objc_protocol)) {
  762. Diag(Tok, diag::err_objc_unexpected_attr);
  763. SkipUntil(tok::semi); // FIXME: better skip?
  764. return DeclGroupPtrTy();
  765. }
  766. DS.abort();
  767. const char *PrevSpec = 0;
  768. unsigned DiagID;
  769. if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
  770. Diag(AtLoc, DiagID) << PrevSpec;
  771. if (Tok.isObjCAtKeyword(tok::objc_protocol))
  772. return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
  773. return Actions.ConvertDeclToDeclGroup(
  774. ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
  775. }
  776. // If the declspec consisted only of 'extern' and we have a string
  777. // literal following it, this must be a C++ linkage specifier like
  778. // 'extern "C"'.
  779. if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
  780. DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
  781. DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
  782. Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
  783. return Actions.ConvertDeclToDeclGroup(TheDecl);
  784. }
  785. return ParseDeclGroup(DS, Declarator::FileContext, true);
  786. }
  787. Parser::DeclGroupPtrTy
  788. Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
  789. ParsingDeclSpec *DS,
  790. AccessSpecifier AS) {
  791. if (DS) {
  792. return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
  793. } else {
  794. ParsingDeclSpec PDS(*this);
  795. // Must temporarily exit the objective-c container scope for
  796. // parsing c constructs and re-enter objc container scope
  797. // afterwards.
  798. ObjCDeclContextSwitch ObjCDC(*this);
  799. return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
  800. }
  801. }
  802. /// ParseFunctionDefinition - We parsed and verified that the specified
  803. /// Declarator is well formed. If this is a K&R-style function, read the
  804. /// parameters declaration-list, then start the compound-statement.
  805. ///
  806. /// function-definition: [C99 6.9.1]
  807. /// decl-specs declarator declaration-list[opt] compound-statement
  808. /// [C90] function-definition: [C99 6.7.1] - implicit int result
  809. /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
  810. /// [C++] function-definition: [C++ 8.4]
  811. /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
  812. /// function-body
  813. /// [C++] function-definition: [C++ 8.4]
  814. /// decl-specifier-seq[opt] declarator function-try-block
  815. ///
  816. Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
  817. const ParsedTemplateInfo &TemplateInfo,
  818. LateParsedAttrList *LateParsedAttrs) {
  819. // Poison the SEH identifiers so they are flagged as illegal in function bodies
  820. PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
  821. const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
  822. // If this is C90 and the declspecs were completely missing, fudge in an
  823. // implicit int. We do this here because this is the only place where
  824. // declaration-specifiers are completely optional in the grammar.
  825. if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
  826. const char *PrevSpec;
  827. unsigned DiagID;
  828. D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
  829. D.getIdentifierLoc(),
  830. PrevSpec, DiagID);
  831. D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
  832. }
  833. // If this declaration was formed with a K&R-style identifier list for the
  834. // arguments, parse declarations for all of the args next.
  835. // int foo(a,b) int a; float b; {}
  836. if (FTI.isKNRPrototype())
  837. ParseKNRParamDeclarations(D);
  838. // We should have either an opening brace or, in a C++ constructor,
  839. // we may have a colon.
  840. if (Tok.isNot(tok::l_brace) &&
  841. (!getLangOpts().CPlusPlus ||
  842. (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
  843. Tok.isNot(tok::equal)))) {
  844. Diag(Tok, diag::err_expected_fn_body);
  845. // Skip over garbage, until we get to '{'. Don't eat the '{'.
  846. SkipUntil(tok::l_brace, true, true);
  847. // If we didn't find the '{', bail out.
  848. if (Tok.isNot(tok::l_brace))
  849. return 0;
  850. }
  851. // Check to make sure that any normal attributes are allowed to be on
  852. // a definition. Late parsed attributes are checked at the end.
  853. if (Tok.isNot(tok::equal)) {
  854. AttributeList *DtorAttrs = D.getAttributes();
  855. while (DtorAttrs) {
  856. if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
  857. Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
  858. << DtorAttrs->getName()->getName();
  859. }
  860. DtorAttrs = DtorAttrs->getNext();
  861. }
  862. }
  863. // In delayed template parsing mode, for function template we consume the
  864. // tokens and store them for late parsing at the end of the translation unit.
  865. if (getLangOpts().DelayedTemplateParsing &&
  866. Tok.isNot(tok::equal) &&
  867. TemplateInfo.Kind == ParsedTemplateInfo::Template) {
  868. MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
  869. ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
  870. Scope *ParentScope = getCurScope()->getParent();
  871. D.setFunctionDefinitionKind(FDK_Definition);
  872. Decl *DP = Actions.HandleDeclarator(ParentScope, D,
  873. TemplateParameterLists);
  874. D.complete(DP);
  875. D.getMutableDeclSpec().abort();
  876. if (DP) {
  877. LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
  878. FunctionDecl *FnD = 0;
  879. if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
  880. FnD = FunTmpl->getTemplatedDecl();
  881. else
  882. FnD = cast<FunctionDecl>(DP);
  883. Actions.CheckForFunctionRedefinition(FnD);
  884. LateParsedTemplateMap[FnD] = LPT;
  885. Actions.MarkAsLateParsedTemplate(FnD);
  886. LexTemplateFunctionForLateParsing(LPT->Toks);
  887. } else {
  888. CachedTokens Toks;
  889. LexTemplateFunctionForLateParsing(Toks);
  890. }
  891. return DP;
  892. }
  893. else if (CurParsedObjCImpl &&
  894. !TemplateInfo.TemplateParams &&
  895. (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
  896. Tok.is(tok::colon)) &&
  897. Actions.CurContext->isTranslationUnit()) {
  898. ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
  899. Scope *ParentScope = getCurScope()->getParent();
  900. D.setFunctionDefinitionKind(FDK_Definition);
  901. Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
  902. MultiTemplateParamsArg());
  903. D.complete(FuncDecl);
  904. D.getMutableDeclSpec().abort();
  905. if (FuncDecl) {
  906. // Consume the tokens and store them for later parsing.
  907. StashAwayMethodOrFunctionBodyTokens(FuncDecl);
  908. CurParsedObjCImpl->HasCFunction = true;
  909. return FuncDecl;
  910. }
  911. }
  912. // Enter a scope for the function body.
  913. ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
  914. // Tell the actions module that we have entered a function definition with the
  915. // specified Declarator for the function.
  916. Decl *Res = TemplateInfo.TemplateParams?
  917. Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
  918. *TemplateInfo.TemplateParams, D)
  919. : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
  920. // Break out of the ParsingDeclarator context before we parse the body.
  921. D.complete(Res);
  922. // Break out of the ParsingDeclSpec context, too. This const_cast is
  923. // safe because we're always the sole owner.
  924. D.getMutableDeclSpec().abort();
  925. if (Tok.is(tok::equal)) {
  926. assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
  927. ConsumeToken();
  928. Actions.ActOnFinishFunctionBody(Res, 0, false);
  929. bool Delete = false;
  930. SourceLocation KWLoc;
  931. if (Tok.is(tok::kw_delete)) {
  932. Diag(Tok, getLangOpts().CPlusPlus0x ?
  933. diag::warn_cxx98_compat_deleted_function :
  934. diag::ext_deleted_function);
  935. KWLoc = ConsumeToken();
  936. Actions.SetDeclDeleted(Res, KWLoc);
  937. Delete = true;
  938. } else if (Tok.is(tok::kw_default)) {
  939. Diag(Tok, getLangOpts().CPlusPlus0x ?
  940. diag::warn_cxx98_compat_defaulted_function :
  941. diag::ext_defaulted_function);
  942. KWLoc = ConsumeToken();
  943. Actions.SetDeclDefaulted(Res, KWLoc);
  944. } else {
  945. llvm_unreachable("function definition after = not 'delete' or 'default'");
  946. }
  947. if (Tok.is(tok::comma)) {
  948. Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
  949. << Delete;
  950. SkipUntil(tok::semi);
  951. } else {
  952. ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
  953. Delete ? "delete" : "default", tok::semi);
  954. }
  955. return Res;
  956. }
  957. if (Tok.is(tok::kw_try))
  958. return ParseFunctionTryBlock(Res, BodyScope);
  959. // If we have a colon, then we're probably parsing a C++
  960. // ctor-initializer.
  961. if (Tok.is(tok::colon)) {
  962. ParseConstructorInitializer(Res);
  963. // Recover from error.
  964. if (!Tok.is(tok::l_brace)) {
  965. BodyScope.Exit();
  966. Actions.ActOnFinishFunctionBody(Res, 0);
  967. return Res;
  968. }
  969. } else
  970. Actions.ActOnDefaultCtorInitializers(Res);
  971. // Late attributes are parsed in the same scope as the function body.
  972. if (LateParsedAttrs)
  973. ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
  974. return ParseFunctionStatementBody(Res, BodyScope);
  975. }
  976. /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
  977. /// types for a function with a K&R-style identifier list for arguments.
  978. void Parser::ParseKNRParamDeclarations(Declarator &D) {
  979. // We know that the top-level of this declarator is a function.
  980. DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
  981. // Enter function-declaration scope, limiting any declarators to the
  982. // function prototype scope, including parameter declarators.
  983. ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
  984. // Read all the argument declarations.
  985. while (isDeclarationSpecifier()) {
  986. SourceLocation DSStart = Tok.getLocation();
  987. // Parse the common declaration-specifiers piece.
  988. DeclSpec DS(AttrFactory);
  989. ParseDeclarationSpecifiers(DS);
  990. // C99 6.9.1p6: 'each declaration in the declaration list shall have at
  991. // least one declarator'.
  992. // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
  993. // the declarations though. It's trivial to ignore them, really hard to do
  994. // anything else with them.
  995. if (Tok.is(tok::semi)) {
  996. Diag(DSStart, diag::err_declaration_does_not_declare_param);
  997. ConsumeToken();
  998. continue;
  999. }
  1000. // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
  1001. // than register.
  1002. if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
  1003. DS.getStorageClassSpec() != DeclSpec::SCS_register) {
  1004. Diag(DS.getStorageClassSpecLoc(),
  1005. diag::err_invalid_storage_class_in_func_decl);
  1006. DS.ClearStorageClassSpecs();
  1007. }
  1008. if (DS.isThreadSpecified()) {
  1009. Diag(DS.getThreadSpecLoc(),
  1010. diag::err_invalid_storage_class_in_func_decl);
  1011. DS.ClearStorageClassSpecs();
  1012. }
  1013. // Parse the first declarator attached to this declspec.
  1014. Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
  1015. ParseDeclarator(ParmDeclarator);
  1016. // Handle the full declarator list.
  1017. while (1) {
  1018. // If attributes are present, parse them.
  1019. MaybeParseGNUAttributes(ParmDeclarator);
  1020. // Ask the actions module to compute the type for this declarator.
  1021. Decl *Param =
  1022. Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
  1023. if (Param &&
  1024. // A missing identifier has already been diagnosed.
  1025. ParmDeclarator.getIdentifier()) {
  1026. // Scan the argument list looking for the correct param to apply this
  1027. // type.
  1028. for (unsigned i = 0; ; ++i) {
  1029. // C99 6.9.1p6: those declarators shall declare only identifiers from
  1030. // the identifier list.
  1031. if (i == FTI.NumArgs) {
  1032. Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
  1033. << ParmDeclarator.getIdentifier();
  1034. break;
  1035. }
  1036. if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
  1037. // Reject redefinitions of parameters.
  1038. if (FTI.ArgInfo[i].Param) {
  1039. Diag(ParmDeclarator.getIdentifierLoc(),
  1040. diag::err_param_redefinition)
  1041. << ParmDeclarator.getIdentifier();
  1042. } else {
  1043. FTI.ArgInfo[i].Param = Param;
  1044. }
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. // If we don't have a comma, it is either the end of the list (a ';') or
  1050. // an error, bail out.
  1051. if (Tok.isNot(tok::comma))
  1052. break;
  1053. ParmDeclarator.clear();
  1054. // Consume the comma.
  1055. ParmDeclarator.setCommaLoc(ConsumeToken());
  1056. // Parse the next declarator.
  1057. ParseDeclarator(ParmDeclarator);
  1058. }
  1059. if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) {
  1060. // Skip to end of block or statement
  1061. SkipUntil(tok::semi, true);
  1062. if (Tok.is(tok::semi))
  1063. ConsumeToken();
  1064. }
  1065. }
  1066. // The actions module must verify that all arguments were declared.
  1067. Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
  1068. }
  1069. /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
  1070. /// allowed to be a wide string, and is not subject to character translation.
  1071. ///
  1072. /// [GNU] asm-string-literal:
  1073. /// string-literal
  1074. ///
  1075. Parser::ExprResult Parser::ParseAsmStringLiteral() {
  1076. switch (Tok.getKind()) {
  1077. case tok::string_literal:
  1078. break;
  1079. case tok::utf8_string_literal:
  1080. case tok::utf16_string_literal:
  1081. case tok::utf32_string_literal:
  1082. case tok::wide_string_literal: {
  1083. SourceLocation L = Tok.getLocation();
  1084. Diag(Tok, diag::err_asm_operand_wide_string_literal)
  1085. << (Tok.getKind() == tok::wide_string_literal)
  1086. << SourceRange(L, L);
  1087. return ExprError();
  1088. }
  1089. default:
  1090. Diag(Tok, diag::err_expected_string_literal);
  1091. return ExprError();
  1092. }
  1093. return ParseStringLiteralExpression();
  1094. }
  1095. /// ParseSimpleAsm
  1096. ///
  1097. /// [GNU] simple-asm-expr:
  1098. /// 'asm' '(' asm-string-literal ')'
  1099. ///
  1100. Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
  1101. assert(Tok.is(tok::kw_asm) && "Not an asm!");
  1102. SourceLocation Loc = ConsumeToken();
  1103. if (Tok.is(tok::kw_volatile)) {
  1104. // Remove from the end of 'asm' to the end of 'volatile'.
  1105. SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
  1106. PP.getLocForEndOfToken(Tok.getLocation()));
  1107. Diag(Tok, diag::warn_file_asm_volatile)
  1108. << FixItHint::CreateRemoval(RemovalRange);
  1109. ConsumeToken();
  1110. }
  1111. BalancedDelimiterTracker T(*this, tok::l_paren);
  1112. if (T.consumeOpen()) {
  1113. Diag(Tok, diag::err_expected_lparen_after) << "asm";
  1114. return ExprError();
  1115. }
  1116. ExprResult Result(ParseAsmStringLiteral());
  1117. if (Result.isInvalid()) {
  1118. SkipUntil(tok::r_paren, true, true);
  1119. if (EndLoc)
  1120. *EndLoc = Tok.getLocation();
  1121. ConsumeAnyToken();
  1122. } else {
  1123. // Close the paren and get the location of the end bracket
  1124. T.consumeClose();
  1125. if (EndLoc)
  1126. *EndLoc = T.getCloseLocation();
  1127. }
  1128. return Result;
  1129. }
  1130. /// \brief Get the TemplateIdAnnotation from the token and put it in the
  1131. /// cleanup pool so that it gets destroyed when parsing the current top level
  1132. /// declaration is finished.
  1133. TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
  1134. assert(tok.is(tok::annot_template_id) && "Expected template-id token");
  1135. TemplateIdAnnotation *
  1136. Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
  1137. return Id;
  1138. }
  1139. void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
  1140. // Push the current token back into the token stream (or revert it if it is
  1141. // cached) and use an annotation scope token for current token.
  1142. if (PP.isBacktrackEnabled())
  1143. PP.RevertCachedTokens(1);
  1144. else
  1145. PP.EnterToken(Tok);
  1146. Tok.setKind(tok::annot_cxxscope);
  1147. Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
  1148. Tok.setAnnotationRange(SS.getRange());
  1149. // In case the tokens were cached, have Preprocessor replace them
  1150. // with the annotation token. We don't need to do this if we've
  1151. // just reverted back to a prior state.
  1152. if (IsNewAnnotation)
  1153. PP.AnnotateCachedTokens(Tok);
  1154. }
  1155. /// \brief Attempt to classify the name at the current token position. This may
  1156. /// form a type, scope or primary expression annotation, or replace the token
  1157. /// with a typo-corrected keyword. This is only appropriate when the current
  1158. /// name must refer to an entity which has already been declared.
  1159. ///
  1160. /// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
  1161. /// and might possibly have a dependent nested name specifier.
  1162. /// \param CCC Indicates how to perform typo-correction for this name. If NULL,
  1163. /// no typo correction will be performed.
  1164. Parser::AnnotatedNameKind
  1165. Parser::TryAnnotateName(bool IsAddressOfOperand,
  1166. CorrectionCandidateCallback *CCC) {
  1167. assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
  1168. const bool EnteringContext = false;
  1169. const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
  1170. CXXScopeSpec SS;
  1171. if (getLangOpts().CPlusPlus &&
  1172. ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
  1173. return ANK_Error;
  1174. if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
  1175. if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
  1176. !WasScopeAnnotation))
  1177. return ANK_Error;
  1178. return ANK_Unresolved;
  1179. }
  1180. IdentifierInfo *Name = Tok.getIdentifierInfo();
  1181. SourceLocation NameLoc = Tok.getLocation();
  1182. // FIXME: Move the tentative declaration logic into ClassifyName so we can
  1183. // typo-correct to tentatively-declared identifiers.
  1184. if (isTentativelyDeclared(Name)) {
  1185. // Identifier has been tentatively declared, and thus cannot be resolved as
  1186. // an expression. Fall back to annotating it as a type.
  1187. if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
  1188. !WasScopeAnnotation))
  1189. return ANK_Error;
  1190. return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
  1191. }
  1192. Token Next = NextToken();
  1193. // Look up and classify the identifier. We don't perform any typo-correction
  1194. // after a scope specifier, because in general we can't recover from typos
  1195. // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump
  1196. // back into scope specifier parsing).
  1197. Sema::NameClassification Classification
  1198. = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next,
  1199. IsAddressOfOperand, SS.isEmpty() ? CCC : 0);
  1200. switch (Classification.getKind()) {
  1201. case Sema::NC_Error:
  1202. return ANK_Error;
  1203. case Sema::NC_Keyword:
  1204. // The identifier was typo-corrected to a keyword.
  1205. Tok.setIdentifierInfo(Name);
  1206. Tok.setKind(Name->getTokenID());
  1207. PP.TypoCorrectToken(Tok);
  1208. if (SS.isNotEmpty())
  1209. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1210. // We've "annotated" this as a keyword.
  1211. return ANK_Success;
  1212. case Sema::NC_Unknown:
  1213. // It's not something we know about. Leave it unannotated.
  1214. break;
  1215. case Sema::NC_Type:
  1216. Tok.setKind(tok::annot_typename);
  1217. setTypeAnnotation(Tok, Classification.getType());
  1218. Tok.setAnnotationEndLoc(NameLoc);
  1219. if (SS.isNotEmpty())
  1220. Tok.setLocation(SS.getBeginLoc());
  1221. PP.AnnotateCachedTokens(Tok);
  1222. return ANK_Success;
  1223. case Sema::NC_Expression:
  1224. Tok.setKind(tok::annot_primary_expr);
  1225. setExprAnnotation(Tok, Classification.getExpression());
  1226. Tok.setAnnotationEndLoc(NameLoc);
  1227. if (SS.isNotEmpty())
  1228. Tok.setLocation(SS.getBeginLoc());
  1229. PP.AnnotateCachedTokens(Tok);
  1230. return ANK_Success;
  1231. case Sema::NC_TypeTemplate:
  1232. if (Next.isNot(tok::less)) {
  1233. // This may be a type template being used as a template template argument.
  1234. if (SS.isNotEmpty())
  1235. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1236. return ANK_TemplateName;
  1237. }
  1238. // Fall through.
  1239. case Sema::NC_FunctionTemplate: {
  1240. // We have a type or function template followed by '<'.
  1241. ConsumeToken();
  1242. UnqualifiedId Id;
  1243. Id.setIdentifier(Name, NameLoc);
  1244. if (AnnotateTemplateIdToken(
  1245. TemplateTy::make(Classification.getTemplateName()),
  1246. Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
  1247. return ANK_Error;
  1248. return ANK_Success;
  1249. }
  1250. case Sema::NC_NestedNameSpecifier:
  1251. llvm_unreachable("already parsed nested name specifier");
  1252. }
  1253. // Unable to classify the name, but maybe we can annotate a scope specifier.
  1254. if (SS.isNotEmpty())
  1255. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1256. return ANK_Unresolved;
  1257. }
  1258. /// TryAnnotateTypeOrScopeToken - If the current token position is on a
  1259. /// typename (possibly qualified in C++) or a C++ scope specifier not followed
  1260. /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
  1261. /// with a single annotation token representing the typename or C++ scope
  1262. /// respectively.
  1263. /// This simplifies handling of C++ scope specifiers and allows efficient
  1264. /// backtracking without the need to re-parse and resolve nested-names and
  1265. /// typenames.
  1266. /// It will mainly be called when we expect to treat identifiers as typenames
  1267. /// (if they are typenames). For example, in C we do not expect identifiers
  1268. /// inside expressions to be treated as typenames so it will not be called
  1269. /// for expressions in C.
  1270. /// The benefit for C/ObjC is that a typename will be annotated and
  1271. /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
  1272. /// will not be called twice, once to check whether we have a declaration
  1273. /// specifier, and another one to get the actual type inside
  1274. /// ParseDeclarationSpecifiers).
  1275. ///
  1276. /// This returns true if an error occurred.
  1277. ///
  1278. /// Note that this routine emits an error if you call it with ::new or ::delete
  1279. /// as the current tokens, so only call it in contexts where these are invalid.
  1280. bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
  1281. assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
  1282. || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
  1283. || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id))
  1284. && "Cannot be a type or scope token!");
  1285. if (Tok.is(tok::kw_typename)) {
  1286. // Parse a C++ typename-specifier, e.g., "typename T::type".
  1287. //
  1288. // typename-specifier:
  1289. // 'typename' '::' [opt] nested-name-specifier identifier
  1290. // 'typename' '::' [opt] nested-name-specifier template [opt]
  1291. // simple-template-id
  1292. SourceLocation TypenameLoc = ConsumeToken();
  1293. CXXScopeSpec SS;
  1294. if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
  1295. /*EnteringContext=*/false,
  1296. 0, /*IsTypename*/true))
  1297. return true;
  1298. if (!SS.isSet()) {
  1299. if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
  1300. Tok.is(tok::annot_decltype)) {
  1301. // Attempt to recover by skipping the invalid 'typename'
  1302. if (Tok.is(tok::annot_decltype) ||
  1303. (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
  1304. Tok.isAnnotation())) {
  1305. unsigned DiagID = diag::err_expected_qualified_after_typename;
  1306. // MS compatibility: MSVC permits using known types with typename.
  1307. // e.g. "typedef typename T* pointer_type"
  1308. if (getLangOpts().MicrosoftExt)
  1309. DiagID = diag::warn_expected_qualified_after_typename;
  1310. Diag(Tok.getLocation(), DiagID);
  1311. return false;
  1312. }
  1313. }
  1314. Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
  1315. return true;
  1316. }
  1317. TypeResult Ty;
  1318. if (Tok.is(tok::identifier)) {
  1319. // FIXME: check whether the next token is '<', first!
  1320. Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
  1321. *Tok.getIdentifierInfo(),
  1322. Tok.getLocation());
  1323. } else if (Tok.is(tok::annot_template_id)) {
  1324. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1325. if (TemplateId->Kind == TNK_Function_template) {
  1326. Diag(Tok, diag::err_typename_refers_to_non_type_template)
  1327. << Tok.getAnnotationRange();
  1328. return true;
  1329. }
  1330. ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
  1331. TemplateId->NumArgs);
  1332. Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
  1333. TemplateId->TemplateKWLoc,
  1334. TemplateId->Template,
  1335. TemplateId->TemplateNameLoc,
  1336. TemplateId->LAngleLoc,
  1337. TemplateArgsPtr,
  1338. TemplateId->RAngleLoc);
  1339. } else {
  1340. Diag(Tok, diag::err_expected_type_name_after_typename)
  1341. << SS.getRange();
  1342. return true;
  1343. }
  1344. SourceLocation EndLoc = Tok.getLastLoc();
  1345. Tok.setKind(tok::annot_typename);
  1346. setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
  1347. Tok.setAnnotationEndLoc(EndLoc);
  1348. Tok.setLocation(TypenameLoc);
  1349. PP.AnnotateCachedTokens(Tok);
  1350. return false;
  1351. }
  1352. // Remembers whether the token was originally a scope annotation.
  1353. bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
  1354. CXXScopeSpec SS;
  1355. if (getLangOpts().CPlusPlus)
  1356. if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
  1357. return true;
  1358. return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
  1359. SS, !WasScopeAnnotation);
  1360. }
  1361. /// \brief Try to annotate a type or scope token, having already parsed an
  1362. /// optional scope specifier. \p IsNewScope should be \c true unless the scope
  1363. /// specifier was extracted from an existing tok::annot_cxxscope annotation.
  1364. bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext,
  1365. bool NeedType,
  1366. CXXScopeSpec &SS,
  1367. bool IsNewScope) {
  1368. if (Tok.is(tok::identifier)) {
  1369. IdentifierInfo *CorrectedII = 0;
  1370. // Determine whether the identifier is a type name.
  1371. if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
  1372. Tok.getLocation(), getCurScope(),
  1373. &SS, false,
  1374. NextToken().is(tok::period),
  1375. ParsedType(),
  1376. /*IsCtorOrDtorName=*/false,
  1377. /*NonTrivialTypeSourceInfo*/true,
  1378. NeedType ? &CorrectedII : NULL)) {
  1379. // A FixIt was applied as a result of typo correction
  1380. if (CorrectedII)
  1381. Tok.setIdentifierInfo(CorrectedII);
  1382. // This is a typename. Replace the current token in-place with an
  1383. // annotation type token.
  1384. Tok.setKind(tok::annot_typename);
  1385. setTypeAnnotation(Tok, Ty);
  1386. Tok.setAnnotationEndLoc(Tok.getLocation());
  1387. if (SS.isNotEmpty()) // it was a C++ qualified type name.
  1388. Tok.setLocation(SS.getBeginLoc());
  1389. // In case the tokens were cached, have Preprocessor replace
  1390. // them with the annotation token.
  1391. PP.AnnotateCachedTokens(Tok);
  1392. return false;
  1393. }
  1394. if (!getLangOpts().CPlusPlus) {
  1395. // If we're in C, we can't have :: tokens at all (the lexer won't return
  1396. // them). If the identifier is not a type, then it can't be scope either,
  1397. // just early exit.
  1398. return false;
  1399. }
  1400. // If this is a template-id, annotate with a template-id or type token.
  1401. if (NextToken().is(tok::less)) {
  1402. TemplateTy Template;
  1403. UnqualifiedId TemplateName;
  1404. TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
  1405. bool MemberOfUnknownSpecialization;
  1406. if (TemplateNameKind TNK
  1407. = Actions.isTemplateName(getCurScope(), SS,
  1408. /*hasTemplateKeyword=*/false, TemplateName,
  1409. /*ObjectType=*/ ParsedType(),
  1410. EnteringContext,
  1411. Template, MemberOfUnknownSpecialization)) {
  1412. // Consume the identifier.
  1413. ConsumeToken();
  1414. if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
  1415. TemplateName)) {
  1416. // If an unrecoverable error occurred, we need to return true here,
  1417. // because the token stream is in a damaged state. We may not return
  1418. // a valid identifier.
  1419. return true;
  1420. }
  1421. }
  1422. }
  1423. // The current token, which is either an identifier or a
  1424. // template-id, is not part of the annotation. Fall through to
  1425. // push that token back into the stream and complete the C++ scope
  1426. // specifier annotation.
  1427. }
  1428. if (Tok.is(tok::annot_template_id)) {
  1429. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1430. if (TemplateId->Kind == TNK_Type_template) {
  1431. // A template-id that refers to a type was parsed into a
  1432. // template-id annotation in a context where we weren't allowed
  1433. // to produce a type annotation token. Update the template-id
  1434. // annotation token to a type annotation token now.
  1435. AnnotateTemplateIdTokenAsType();
  1436. return false;
  1437. }
  1438. }
  1439. if (SS.isEmpty())
  1440. return false;
  1441. // A C++ scope specifier that isn't followed by a typename.
  1442. AnnotateScopeToken(SS, IsNewScope);
  1443. return false;
  1444. }
  1445. /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
  1446. /// annotates C++ scope specifiers and template-ids. This returns
  1447. /// true if there was an error that could not be recovered from.
  1448. ///
  1449. /// Note that this routine emits an error if you call it with ::new or ::delete
  1450. /// as the current tokens, so only call it in contexts where these are invalid.
  1451. bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
  1452. assert(getLangOpts().CPlusPlus &&
  1453. "Call sites of this function should be guarded by checking for C++");
  1454. assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
  1455. (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
  1456. Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
  1457. CXXScopeSpec SS;
  1458. if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
  1459. return true;
  1460. if (SS.isEmpty())
  1461. return false;
  1462. AnnotateScopeToken(SS, true);
  1463. return false;
  1464. }
  1465. bool Parser::isTokenEqualOrEqualTypo() {
  1466. tok::TokenKind Kind = Tok.getKind();
  1467. switch (Kind) {
  1468. default:
  1469. return false;
  1470. case tok::ampequal: // &=
  1471. case tok::starequal: // *=
  1472. case tok::plusequal: // +=
  1473. case tok::minusequal: // -=
  1474. case tok::exclaimequal: // !=
  1475. case tok::slashequal: // /=
  1476. case tok::percentequal: // %=
  1477. case tok::lessequal: // <=
  1478. case tok::lesslessequal: // <<=
  1479. case tok::greaterequal: // >=
  1480. case tok::greatergreaterequal: // >>=
  1481. case tok::caretequal: // ^=
  1482. case tok::pipeequal: // |=
  1483. case tok::equalequal: // ==
  1484. Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
  1485. << getTokenSimpleSpelling(Kind)
  1486. << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
  1487. case tok::equal:
  1488. return true;
  1489. }
  1490. }
  1491. SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
  1492. assert(Tok.is(tok::code_completion));
  1493. PrevTokLocation = Tok.getLocation();
  1494. for (Scope *S = getCurScope(); S; S = S->getParent()) {
  1495. if (S->getFlags() & Scope::FnScope) {
  1496. Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
  1497. cutOffParsing();
  1498. return PrevTokLocation;
  1499. }
  1500. if (S->getFlags() & Scope::ClassScope) {
  1501. Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
  1502. cutOffParsing();
  1503. return PrevTokLocation;
  1504. }
  1505. }
  1506. Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
  1507. cutOffParsing();
  1508. return PrevTokLocation;
  1509. }
  1510. // Anchor the Parser::FieldCallback vtable to this translation unit.
  1511. // We use a spurious method instead of the destructor because
  1512. // destroying FieldCallbacks can actually be slightly
  1513. // performance-sensitive.
  1514. void Parser::FieldCallback::_anchor() {
  1515. }
  1516. // Code-completion pass-through functions
  1517. void Parser::CodeCompleteDirective(bool InConditional) {
  1518. Actions.CodeCompletePreprocessorDirective(InConditional);
  1519. }
  1520. void Parser::CodeCompleteInConditionalExclusion() {
  1521. Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
  1522. }
  1523. void Parser::CodeCompleteMacroName(bool IsDefinition) {
  1524. Actions.CodeCompletePreprocessorMacroName(IsDefinition);
  1525. }
  1526. void Parser::CodeCompletePreprocessorExpression() {
  1527. Actions.CodeCompletePreprocessorExpression();
  1528. }
  1529. void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
  1530. MacroInfo *MacroInfo,
  1531. unsigned ArgumentIndex) {
  1532. Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
  1533. ArgumentIndex);
  1534. }
  1535. void Parser::CodeCompleteNaturalLanguage() {
  1536. Actions.CodeCompleteNaturalLanguage();
  1537. }
  1538. bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
  1539. assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
  1540. "Expected '__if_exists' or '__if_not_exists'");
  1541. Result.IsIfExists = Tok.is(tok::kw___if_exists);
  1542. Result.KeywordLoc = ConsumeToken();
  1543. BalancedDelimiterTracker T(*this, tok::l_paren);
  1544. if (T.consumeOpen()) {
  1545. Diag(Tok, diag::err_expected_lparen_after)
  1546. << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
  1547. return true;
  1548. }
  1549. // Parse nested-name-specifier.
  1550. ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
  1551. /*EnteringContext=*/false);
  1552. // Check nested-name specifier.
  1553. if (Result.SS.isInvalid()) {
  1554. T.skipToEnd();
  1555. return true;
  1556. }
  1557. // Parse the unqualified-id.
  1558. SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
  1559. if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
  1560. TemplateKWLoc, Result.Name)) {
  1561. T.skipToEnd();
  1562. return true;
  1563. }
  1564. if (T.consumeClose())
  1565. return true;
  1566. // Check if the symbol exists.
  1567. switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
  1568. Result.IsIfExists, Result.SS,
  1569. Result.Name)) {
  1570. case Sema::IER_Exists:
  1571. Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
  1572. break;
  1573. case Sema::IER_DoesNotExist:
  1574. Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
  1575. break;
  1576. case Sema::IER_Dependent:
  1577. Result.Behavior = IEB_Dependent;
  1578. break;
  1579. case Sema::IER_Error:
  1580. return true;
  1581. }
  1582. return false;
  1583. }
  1584. void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
  1585. IfExistsCondition Result;
  1586. if (ParseMicrosoftIfExistsCondition(Result))
  1587. return;
  1588. BalancedDelimiterTracker Braces(*this, tok::l_brace);
  1589. if (Braces.consumeOpen()) {
  1590. Diag(Tok, diag::err_expected_lbrace);
  1591. return;
  1592. }
  1593. switch (Result.Behavior) {
  1594. case IEB_Parse:
  1595. // Parse declarations below.
  1596. break;
  1597. case IEB_Dependent:
  1598. llvm_unreachable("Cannot have a dependent external declaration");
  1599. case IEB_Skip:
  1600. Braces.skipToEnd();
  1601. return;
  1602. }
  1603. // Parse the declarations.
  1604. while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
  1605. ParsedAttributesWithRange attrs(AttrFactory);
  1606. MaybeParseCXX0XAttributes(attrs);
  1607. MaybeParseMicrosoftAttributes(attrs);
  1608. DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
  1609. if (Result && !getCurScope()->getParent())
  1610. Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
  1611. }
  1612. Braces.consumeClose();
  1613. }
  1614. Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
  1615. assert(Tok.isObjCAtKeyword(tok::objc___experimental_modules_import) &&
  1616. "Improper start to module import");
  1617. SourceLocation ImportLoc = ConsumeToken();
  1618. llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
  1619. // Parse the module path.
  1620. do {
  1621. if (!Tok.is(tok::identifier)) {
  1622. if (Tok.is(tok::code_completion)) {
  1623. Actions.CodeCompleteModuleImport(ImportLoc, Path);
  1624. ConsumeCodeCompletionToken();
  1625. SkipUntil(tok::semi);
  1626. return DeclGroupPtrTy();
  1627. }
  1628. Diag(Tok, diag::err_module_expected_ident);
  1629. SkipUntil(tok::semi);
  1630. return DeclGroupPtrTy();
  1631. }
  1632. // Record this part of the module path.
  1633. Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
  1634. ConsumeToken();
  1635. if (Tok.is(tok::period)) {
  1636. ConsumeToken();
  1637. continue;
  1638. }
  1639. break;
  1640. } while (true);
  1641. DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
  1642. ExpectAndConsumeSemi(diag::err_module_expected_semi);
  1643. if (Import.isInvalid())
  1644. return DeclGroupPtrTy();
  1645. return Actions.ConvertDeclToDeclGroup(Import.get());
  1646. }
  1647. bool BalancedDelimiterTracker::diagnoseOverflow() {
  1648. P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
  1649. P.SkipUntil(tok::eof);
  1650. return true;
  1651. }
  1652. bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
  1653. const char *Msg,
  1654. tok::TokenKind SkipToToc ) {
  1655. LOpen = P.Tok.getLocation();
  1656. if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
  1657. return true;
  1658. if (getDepth() < MaxDepth)
  1659. return false;
  1660. return diagnoseOverflow();
  1661. }
  1662. bool BalancedDelimiterTracker::diagnoseMissingClose() {
  1663. assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
  1664. const char *LHSName = "unknown";
  1665. diag::kind DID;
  1666. switch (Close) {
  1667. default: llvm_unreachable("Unexpected balanced token");
  1668. case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
  1669. case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
  1670. case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
  1671. }
  1672. P.Diag(P.Tok, DID);
  1673. P.Diag(LOpen, diag::note_matching) << LHSName;
  1674. if (P.SkipUntil(Close))
  1675. LClose = P.Tok.getLocation();
  1676. return true;
  1677. }
  1678. void BalancedDelimiterTracker::skipToEnd() {
  1679. P.SkipUntil(Close, false);
  1680. }