Parser.cpp 68 KB

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