Parser.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  1. //===--- Parser.cpp - C Language Family Parser ----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the Parser interfaces.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Parse/Parser.h"
  13. #include "clang/AST/ASTConsumer.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/DeclTemplate.h"
  16. #include "clang/Basic/FileManager.h"
  17. #include "clang/Parse/ParseDiagnostic.h"
  18. #include "clang/Parse/RAIIObjectsForParser.h"
  19. #include "clang/Sema/DeclSpec.h"
  20. #include "clang/Sema/ParsedTemplate.h"
  21. #include "clang/Sema/Scope.h"
  22. #include "llvm/Support/Path.h"
  23. using namespace clang;
  24. namespace {
  25. /// 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. bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
  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), PreferredType(pp.isCodeCompletionEnabled()), Actions(actions),
  45. Diags(PP.getDiagnostics()), GreaterThanIsOperator(true),
  46. ColonIsSacred(false), InMessageExpression(false),
  47. TemplateParameterDepth(0), ParsingInObjCContainer(false) {
  48. SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
  49. Tok.startToken();
  50. Tok.setKind(tok::eof);
  51. Actions.CurScope = nullptr;
  52. NumCachedScopes = 0;
  53. CurParsedObjCImpl = nullptr;
  54. // Add #pragma handlers. These are removed and destroyed in the
  55. // destructor.
  56. initializePragmaHandlers();
  57. CommentSemaHandler.reset(new ActionCommentHandler(actions));
  58. PP.addCommentHandler(CommentSemaHandler.get());
  59. PP.setCodeCompletionHandler(*this);
  60. }
  61. DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
  62. return Diags.Report(Loc, DiagID);
  63. }
  64. DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
  65. return Diag(Tok.getLocation(), DiagID);
  66. }
  67. /// Emits a diagnostic suggesting parentheses surrounding a
  68. /// given range.
  69. ///
  70. /// \param Loc The location where we'll emit the diagnostic.
  71. /// \param DK The kind of diagnostic to emit.
  72. /// \param ParenRange Source range enclosing code that should be parenthesized.
  73. void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
  74. SourceRange ParenRange) {
  75. SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
  76. if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
  77. // We can't display the parentheses, so just dig the
  78. // warning/error and return.
  79. Diag(Loc, DK);
  80. return;
  81. }
  82. Diag(Loc, DK)
  83. << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
  84. << FixItHint::CreateInsertion(EndLoc, ")");
  85. }
  86. static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
  87. switch (ExpectedTok) {
  88. case tok::semi:
  89. return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
  90. default: return false;
  91. }
  92. }
  93. bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
  94. StringRef Msg) {
  95. if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
  96. ConsumeAnyToken();
  97. return false;
  98. }
  99. // Detect common single-character typos and resume.
  100. if (IsCommonTypo(ExpectedTok, Tok)) {
  101. SourceLocation Loc = Tok.getLocation();
  102. {
  103. DiagnosticBuilder DB = Diag(Loc, DiagID);
  104. DB << FixItHint::CreateReplacement(
  105. SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
  106. if (DiagID == diag::err_expected)
  107. DB << ExpectedTok;
  108. else if (DiagID == diag::err_expected_after)
  109. DB << Msg << ExpectedTok;
  110. else
  111. DB << Msg;
  112. }
  113. // Pretend there wasn't a problem.
  114. ConsumeAnyToken();
  115. return false;
  116. }
  117. SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
  118. const char *Spelling = nullptr;
  119. if (EndLoc.isValid())
  120. Spelling = tok::getPunctuatorSpelling(ExpectedTok);
  121. DiagnosticBuilder DB =
  122. Spelling
  123. ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling)
  124. : Diag(Tok, DiagID);
  125. if (DiagID == diag::err_expected)
  126. DB << ExpectedTok;
  127. else if (DiagID == diag::err_expected_after)
  128. DB << Msg << ExpectedTok;
  129. else
  130. DB << Msg;
  131. return true;
  132. }
  133. bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
  134. if (TryConsumeToken(tok::semi))
  135. return false;
  136. if (Tok.is(tok::code_completion)) {
  137. handleUnexpectedCodeCompletionToken();
  138. return false;
  139. }
  140. if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
  141. NextToken().is(tok::semi)) {
  142. Diag(Tok, diag::err_extraneous_token_before_semi)
  143. << PP.getSpelling(Tok)
  144. << FixItHint::CreateRemoval(Tok.getLocation());
  145. ConsumeAnyToken(); // The ')' or ']'.
  146. ConsumeToken(); // The ';'.
  147. return false;
  148. }
  149. return ExpectAndConsume(tok::semi, DiagID);
  150. }
  151. void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
  152. if (!Tok.is(tok::semi)) return;
  153. bool HadMultipleSemis = false;
  154. SourceLocation StartLoc = Tok.getLocation();
  155. SourceLocation EndLoc = Tok.getLocation();
  156. ConsumeToken();
  157. while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
  158. HadMultipleSemis = true;
  159. EndLoc = Tok.getLocation();
  160. ConsumeToken();
  161. }
  162. // C++11 allows extra semicolons at namespace scope, but not in any of the
  163. // other contexts.
  164. if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
  165. if (getLangOpts().CPlusPlus11)
  166. Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
  167. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  168. else
  169. Diag(StartLoc, diag::ext_extra_semi_cxx11)
  170. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  171. return;
  172. }
  173. if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
  174. Diag(StartLoc, diag::ext_extra_semi)
  175. << Kind << DeclSpec::getSpecifierName(TST,
  176. Actions.getASTContext().getPrintingPolicy())
  177. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  178. else
  179. // A single semicolon is valid after a member function definition.
  180. Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
  181. << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
  182. }
  183. bool Parser::expectIdentifier() {
  184. if (Tok.is(tok::identifier))
  185. return false;
  186. if (const auto *II = Tok.getIdentifierInfo()) {
  187. if (II->isCPlusPlusKeyword(getLangOpts())) {
  188. Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword)
  189. << tok::identifier << Tok.getIdentifierInfo();
  190. // Objective-C++: Recover by treating this keyword as a valid identifier.
  191. return false;
  192. }
  193. }
  194. Diag(Tok, diag::err_expected) << tok::identifier;
  195. return true;
  196. }
  197. void Parser::checkCompoundToken(SourceLocation FirstTokLoc,
  198. tok::TokenKind FirstTokKind, CompoundToken Op) {
  199. if (FirstTokLoc.isInvalid())
  200. return;
  201. SourceLocation SecondTokLoc = Tok.getLocation();
  202. // If either token is in a macro, we expect both tokens to come from the same
  203. // macro expansion.
  204. if ((FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID()) &&
  205. PP.getSourceManager().getFileID(FirstTokLoc) !=
  206. PP.getSourceManager().getFileID(SecondTokLoc)) {
  207. Diag(FirstTokLoc, diag::warn_compound_token_split_by_macro)
  208. << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
  209. << static_cast<int>(Op) << SourceRange(FirstTokLoc);
  210. Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here)
  211. << (FirstTokKind == Tok.getKind()) << Tok.getKind()
  212. << SourceRange(SecondTokLoc);
  213. return;
  214. }
  215. // We expect the tokens to abut.
  216. if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
  217. SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc);
  218. if (SpaceLoc.isInvalid())
  219. SpaceLoc = FirstTokLoc;
  220. Diag(SpaceLoc, diag::warn_compound_token_split_by_whitespace)
  221. << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
  222. << static_cast<int>(Op) << SourceRange(FirstTokLoc, SecondTokLoc);
  223. return;
  224. }
  225. }
  226. //===----------------------------------------------------------------------===//
  227. // Error recovery.
  228. //===----------------------------------------------------------------------===//
  229. static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {
  230. return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
  231. }
  232. /// SkipUntil - Read tokens until we get to the specified token, then consume
  233. /// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the
  234. /// token will ever occur, this skips to the next token, or to some likely
  235. /// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
  236. /// character.
  237. ///
  238. /// If SkipUntil finds the specified token, it returns true, otherwise it
  239. /// returns false.
  240. bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) {
  241. // We always want this function to skip at least one token if the first token
  242. // isn't T and if not at EOF.
  243. bool isFirstTokenSkipped = true;
  244. while (true) {
  245. // If we found one of the tokens, stop and return true.
  246. for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
  247. if (Tok.is(Toks[i])) {
  248. if (HasFlagsSet(Flags, StopBeforeMatch)) {
  249. // Noop, don't consume the token.
  250. } else {
  251. ConsumeAnyToken();
  252. }
  253. return true;
  254. }
  255. }
  256. // Important special case: The caller has given up and just wants us to
  257. // skip the rest of the file. Do this without recursing, since we can
  258. // get here precisely because the caller detected too much recursion.
  259. if (Toks.size() == 1 && Toks[0] == tok::eof &&
  260. !HasFlagsSet(Flags, StopAtSemi) &&
  261. !HasFlagsSet(Flags, StopAtCodeCompletion)) {
  262. while (Tok.isNot(tok::eof))
  263. ConsumeAnyToken();
  264. return true;
  265. }
  266. switch (Tok.getKind()) {
  267. case tok::eof:
  268. // Ran out of tokens.
  269. return false;
  270. case tok::annot_pragma_openmp:
  271. case tok::annot_attr_openmp:
  272. case tok::annot_pragma_openmp_end:
  273. // Stop before an OpenMP pragma boundary.
  274. if (OpenMPDirectiveParsing)
  275. return false;
  276. ConsumeAnnotationToken();
  277. break;
  278. case tok::annot_module_begin:
  279. case tok::annot_module_end:
  280. case tok::annot_module_include:
  281. // Stop before we change submodules. They generally indicate a "good"
  282. // place to pick up parsing again (except in the special case where
  283. // we're trying to skip to EOF).
  284. return false;
  285. case tok::code_completion:
  286. if (!HasFlagsSet(Flags, StopAtCodeCompletion))
  287. handleUnexpectedCodeCompletionToken();
  288. return false;
  289. case tok::l_paren:
  290. // Recursively skip properly-nested parens.
  291. ConsumeParen();
  292. if (HasFlagsSet(Flags, StopAtCodeCompletion))
  293. SkipUntil(tok::r_paren, StopAtCodeCompletion);
  294. else
  295. SkipUntil(tok::r_paren);
  296. break;
  297. case tok::l_square:
  298. // Recursively skip properly-nested square brackets.
  299. ConsumeBracket();
  300. if (HasFlagsSet(Flags, StopAtCodeCompletion))
  301. SkipUntil(tok::r_square, StopAtCodeCompletion);
  302. else
  303. SkipUntil(tok::r_square);
  304. break;
  305. case tok::l_brace:
  306. // Recursively skip properly-nested braces.
  307. ConsumeBrace();
  308. if (HasFlagsSet(Flags, StopAtCodeCompletion))
  309. SkipUntil(tok::r_brace, StopAtCodeCompletion);
  310. else
  311. SkipUntil(tok::r_brace);
  312. break;
  313. case tok::question:
  314. // Recursively skip ? ... : pairs; these function as brackets. But
  315. // still stop at a semicolon if requested.
  316. ConsumeToken();
  317. SkipUntil(tok::colon,
  318. SkipUntilFlags(unsigned(Flags) &
  319. unsigned(StopAtCodeCompletion | StopAtSemi)));
  320. break;
  321. // Okay, we found a ']' or '}' or ')', which we think should be balanced.
  322. // Since the user wasn't looking for this token (if they were, it would
  323. // already be handled), this isn't balanced. If there is a LHS token at a
  324. // higher level, we will assume that this matches the unbalanced token
  325. // and return it. Otherwise, this is a spurious RHS token, which we skip.
  326. case tok::r_paren:
  327. if (ParenCount && !isFirstTokenSkipped)
  328. return false; // Matches something.
  329. ConsumeParen();
  330. break;
  331. case tok::r_square:
  332. if (BracketCount && !isFirstTokenSkipped)
  333. return false; // Matches something.
  334. ConsumeBracket();
  335. break;
  336. case tok::r_brace:
  337. if (BraceCount && !isFirstTokenSkipped)
  338. return false; // Matches something.
  339. ConsumeBrace();
  340. break;
  341. case tok::semi:
  342. if (HasFlagsSet(Flags, StopAtSemi))
  343. return false;
  344. LLVM_FALLTHROUGH;
  345. default:
  346. // Skip this token.
  347. ConsumeAnyToken();
  348. break;
  349. }
  350. isFirstTokenSkipped = false;
  351. }
  352. }
  353. //===----------------------------------------------------------------------===//
  354. // Scope manipulation
  355. //===----------------------------------------------------------------------===//
  356. /// EnterScope - Start a new scope.
  357. void Parser::EnterScope(unsigned ScopeFlags) {
  358. if (NumCachedScopes) {
  359. Scope *N = ScopeCache[--NumCachedScopes];
  360. N->Init(getCurScope(), ScopeFlags);
  361. Actions.CurScope = N;
  362. } else {
  363. Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
  364. }
  365. }
  366. /// ExitScope - Pop a scope off the scope stack.
  367. void Parser::ExitScope() {
  368. assert(getCurScope() && "Scope imbalance!");
  369. // Inform the actions module that this scope is going away if there are any
  370. // decls in it.
  371. Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
  372. Scope *OldScope = getCurScope();
  373. Actions.CurScope = OldScope->getParent();
  374. if (NumCachedScopes == ScopeCacheSize)
  375. delete OldScope;
  376. else
  377. ScopeCache[NumCachedScopes++] = OldScope;
  378. }
  379. /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
  380. /// this object does nothing.
  381. Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
  382. bool ManageFlags)
  383. : CurScope(ManageFlags ? Self->getCurScope() : nullptr) {
  384. if (CurScope) {
  385. OldFlags = CurScope->getFlags();
  386. CurScope->setFlags(ScopeFlags);
  387. }
  388. }
  389. /// Restore the flags for the current scope to what they were before this
  390. /// object overrode them.
  391. Parser::ParseScopeFlags::~ParseScopeFlags() {
  392. if (CurScope)
  393. CurScope->setFlags(OldFlags);
  394. }
  395. //===----------------------------------------------------------------------===//
  396. // C99 6.9: External Definitions.
  397. //===----------------------------------------------------------------------===//
  398. Parser::~Parser() {
  399. // If we still have scopes active, delete the scope tree.
  400. delete getCurScope();
  401. Actions.CurScope = nullptr;
  402. // Free the scope cache.
  403. for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
  404. delete ScopeCache[i];
  405. resetPragmaHandlers();
  406. PP.removeCommentHandler(CommentSemaHandler.get());
  407. PP.clearCodeCompletionHandler();
  408. DestroyTemplateIds();
  409. }
  410. /// Initialize - Warm up the parser.
  411. ///
  412. void Parser::Initialize() {
  413. // Create the translation unit scope. Install it as the current scope.
  414. assert(getCurScope() == nullptr && "A scope is already active?");
  415. EnterScope(Scope::DeclScope);
  416. Actions.ActOnTranslationUnitScope(getCurScope());
  417. // Initialization for Objective-C context sensitive keywords recognition.
  418. // Referenced in Parser::ParseObjCTypeQualifierList.
  419. if (getLangOpts().ObjC) {
  420. ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
  421. ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
  422. ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
  423. ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
  424. ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
  425. ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
  426. ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull");
  427. ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable");
  428. ObjCTypeQuals[objc_null_unspecified]
  429. = &PP.getIdentifierTable().get("null_unspecified");
  430. }
  431. Ident_instancetype = nullptr;
  432. Ident_final = nullptr;
  433. Ident_sealed = nullptr;
  434. Ident_abstract = nullptr;
  435. Ident_override = nullptr;
  436. Ident_GNU_final = nullptr;
  437. Ident_import = nullptr;
  438. Ident_module = nullptr;
  439. Ident_super = &PP.getIdentifierTable().get("super");
  440. Ident_vector = nullptr;
  441. Ident_bool = nullptr;
  442. Ident_Bool = nullptr;
  443. Ident_pixel = nullptr;
  444. if (getLangOpts().AltiVec || getLangOpts().ZVector) {
  445. Ident_vector = &PP.getIdentifierTable().get("vector");
  446. Ident_bool = &PP.getIdentifierTable().get("bool");
  447. Ident_Bool = &PP.getIdentifierTable().get("_Bool");
  448. }
  449. if (getLangOpts().AltiVec)
  450. Ident_pixel = &PP.getIdentifierTable().get("pixel");
  451. Ident_introduced = nullptr;
  452. Ident_deprecated = nullptr;
  453. Ident_obsoleted = nullptr;
  454. Ident_unavailable = nullptr;
  455. Ident_strict = nullptr;
  456. Ident_replacement = nullptr;
  457. Ident_language = Ident_defined_in = Ident_generated_declaration = nullptr;
  458. Ident__except = nullptr;
  459. Ident__exception_code = Ident__exception_info = nullptr;
  460. Ident__abnormal_termination = Ident___exception_code = nullptr;
  461. Ident___exception_info = Ident___abnormal_termination = nullptr;
  462. Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr;
  463. Ident_AbnormalTermination = nullptr;
  464. if(getLangOpts().Borland) {
  465. Ident__exception_info = PP.getIdentifierInfo("_exception_info");
  466. Ident___exception_info = PP.getIdentifierInfo("__exception_info");
  467. Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
  468. Ident__exception_code = PP.getIdentifierInfo("_exception_code");
  469. Ident___exception_code = PP.getIdentifierInfo("__exception_code");
  470. Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
  471. Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
  472. Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
  473. Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
  474. PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
  475. PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
  476. PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
  477. PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
  478. PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
  479. PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
  480. PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
  481. PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
  482. PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
  483. }
  484. if (getLangOpts().CPlusPlusModules) {
  485. Ident_import = PP.getIdentifierInfo("import");
  486. Ident_module = PP.getIdentifierInfo("module");
  487. }
  488. Actions.Initialize();
  489. // Prime the lexer look-ahead.
  490. ConsumeToken();
  491. }
  492. void Parser::DestroyTemplateIds() {
  493. for (TemplateIdAnnotation *Id : TemplateIds)
  494. Id->Destroy();
  495. TemplateIds.clear();
  496. }
  497. /// Parse the first top-level declaration in a translation unit.
  498. ///
  499. /// translation-unit:
  500. /// [C] external-declaration
  501. /// [C] translation-unit external-declaration
  502. /// [C++] top-level-declaration-seq[opt]
  503. /// [C++20] global-module-fragment[opt] module-declaration
  504. /// top-level-declaration-seq[opt] private-module-fragment[opt]
  505. ///
  506. /// Note that in C, it is an error if there is no first declaration.
  507. bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result) {
  508. Actions.ActOnStartOfTranslationUnit();
  509. // C11 6.9p1 says translation units must have at least one top-level
  510. // declaration. C++ doesn't have this restriction. We also don't want to
  511. // complain if we have a precompiled header, although technically if the PCH
  512. // is empty we should still emit the (pedantic) diagnostic.
  513. // If the main file is a header, we're only pretending it's a TU; don't warn.
  514. bool NoTopLevelDecls = ParseTopLevelDecl(Result, true);
  515. if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() &&
  516. !getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile)
  517. Diag(diag::ext_empty_translation_unit);
  518. return NoTopLevelDecls;
  519. }
  520. /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
  521. /// action tells us to. This returns true if the EOF was encountered.
  522. ///
  523. /// top-level-declaration:
  524. /// declaration
  525. /// [C++20] module-import-declaration
  526. bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, bool IsFirstDecl) {
  527. DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
  528. // Skip over the EOF token, flagging end of previous input for incremental
  529. // processing
  530. if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
  531. ConsumeToken();
  532. Result = nullptr;
  533. switch (Tok.getKind()) {
  534. case tok::annot_pragma_unused:
  535. HandlePragmaUnused();
  536. return false;
  537. case tok::kw_export:
  538. switch (NextToken().getKind()) {
  539. case tok::kw_module:
  540. goto module_decl;
  541. // Note: no need to handle kw_import here. We only form kw_import under
  542. // the Modules TS, and in that case 'export import' is parsed as an
  543. // export-declaration containing an import-declaration.
  544. // Recognize context-sensitive C++20 'export module' and 'export import'
  545. // declarations.
  546. case tok::identifier: {
  547. IdentifierInfo *II = NextToken().getIdentifierInfo();
  548. if ((II == Ident_module || II == Ident_import) &&
  549. GetLookAheadToken(2).isNot(tok::coloncolon)) {
  550. if (II == Ident_module)
  551. goto module_decl;
  552. else
  553. goto import_decl;
  554. }
  555. break;
  556. }
  557. default:
  558. break;
  559. }
  560. break;
  561. case tok::kw_module:
  562. module_decl:
  563. Result = ParseModuleDecl(IsFirstDecl);
  564. return false;
  565. // tok::kw_import is handled by ParseExternalDeclaration. (Under the Modules
  566. // TS, an import can occur within an export block.)
  567. import_decl: {
  568. Decl *ImportDecl = ParseModuleImport(SourceLocation());
  569. Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
  570. return false;
  571. }
  572. case tok::annot_module_include:
  573. Actions.ActOnModuleInclude(Tok.getLocation(),
  574. reinterpret_cast<Module *>(
  575. Tok.getAnnotationValue()));
  576. ConsumeAnnotationToken();
  577. return false;
  578. case tok::annot_module_begin:
  579. Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>(
  580. Tok.getAnnotationValue()));
  581. ConsumeAnnotationToken();
  582. return false;
  583. case tok::annot_module_end:
  584. Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>(
  585. Tok.getAnnotationValue()));
  586. ConsumeAnnotationToken();
  587. return false;
  588. case tok::eof:
  589. // Check whether -fmax-tokens= was reached.
  590. if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) {
  591. PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total)
  592. << PP.getTokenCount() << PP.getMaxTokens();
  593. SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc();
  594. if (OverrideLoc.isValid()) {
  595. PP.Diag(OverrideLoc, diag::note_max_tokens_total_override);
  596. }
  597. }
  598. // Late template parsing can begin.
  599. Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr, this);
  600. if (!PP.isIncrementalProcessingEnabled())
  601. Actions.ActOnEndOfTranslationUnit();
  602. //else don't tell Sema that we ended parsing: more input might come.
  603. return true;
  604. case tok::identifier:
  605. // C++2a [basic.link]p3:
  606. // A token sequence beginning with 'export[opt] module' or
  607. // 'export[opt] import' and not immediately followed by '::'
  608. // is never interpreted as the declaration of a top-level-declaration.
  609. if ((Tok.getIdentifierInfo() == Ident_module ||
  610. Tok.getIdentifierInfo() == Ident_import) &&
  611. NextToken().isNot(tok::coloncolon)) {
  612. if (Tok.getIdentifierInfo() == Ident_module)
  613. goto module_decl;
  614. else
  615. goto import_decl;
  616. }
  617. break;
  618. default:
  619. break;
  620. }
  621. ParsedAttributesWithRange attrs(AttrFactory);
  622. MaybeParseCXX11Attributes(attrs);
  623. Result = ParseExternalDeclaration(attrs);
  624. return false;
  625. }
  626. /// ParseExternalDeclaration:
  627. ///
  628. /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
  629. /// function-definition
  630. /// declaration
  631. /// [GNU] asm-definition
  632. /// [GNU] __extension__ external-declaration
  633. /// [OBJC] objc-class-definition
  634. /// [OBJC] objc-class-declaration
  635. /// [OBJC] objc-alias-declaration
  636. /// [OBJC] objc-protocol-definition
  637. /// [OBJC] objc-method-definition
  638. /// [OBJC] @end
  639. /// [C++] linkage-specification
  640. /// [GNU] asm-definition:
  641. /// simple-asm-expr ';'
  642. /// [C++11] empty-declaration
  643. /// [C++11] attribute-declaration
  644. ///
  645. /// [C++11] empty-declaration:
  646. /// ';'
  647. ///
  648. /// [C++0x/GNU] 'extern' 'template' declaration
  649. ///
  650. /// [Modules-TS] module-import-declaration
  651. ///
  652. Parser::DeclGroupPtrTy
  653. Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
  654. ParsingDeclSpec *DS) {
  655. DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
  656. ParenBraceBracketBalancer BalancerRAIIObj(*this);
  657. if (PP.isCodeCompletionReached()) {
  658. cutOffParsing();
  659. return nullptr;
  660. }
  661. Decl *SingleDecl = nullptr;
  662. switch (Tok.getKind()) {
  663. case tok::annot_pragma_vis:
  664. HandlePragmaVisibility();
  665. return nullptr;
  666. case tok::annot_pragma_pack:
  667. HandlePragmaPack();
  668. return nullptr;
  669. case tok::annot_pragma_msstruct:
  670. HandlePragmaMSStruct();
  671. return nullptr;
  672. case tok::annot_pragma_align:
  673. HandlePragmaAlign();
  674. return nullptr;
  675. case tok::annot_pragma_weak:
  676. HandlePragmaWeak();
  677. return nullptr;
  678. case tok::annot_pragma_weakalias:
  679. HandlePragmaWeakAlias();
  680. return nullptr;
  681. case tok::annot_pragma_redefine_extname:
  682. HandlePragmaRedefineExtname();
  683. return nullptr;
  684. case tok::annot_pragma_fp_contract:
  685. HandlePragmaFPContract();
  686. return nullptr;
  687. case tok::annot_pragma_fenv_access:
  688. case tok::annot_pragma_fenv_access_ms:
  689. HandlePragmaFEnvAccess();
  690. return nullptr;
  691. case tok::annot_pragma_fenv_round:
  692. HandlePragmaFEnvRound();
  693. return nullptr;
  694. case tok::annot_pragma_float_control:
  695. HandlePragmaFloatControl();
  696. return nullptr;
  697. case tok::annot_pragma_fp:
  698. HandlePragmaFP();
  699. break;
  700. case tok::annot_pragma_opencl_extension:
  701. HandlePragmaOpenCLExtension();
  702. return nullptr;
  703. case tok::annot_attr_openmp:
  704. case tok::annot_pragma_openmp: {
  705. AccessSpecifier AS = AS_none;
  706. return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, attrs);
  707. }
  708. case tok::annot_pragma_ms_pointers_to_members:
  709. HandlePragmaMSPointersToMembers();
  710. return nullptr;
  711. case tok::annot_pragma_ms_vtordisp:
  712. HandlePragmaMSVtorDisp();
  713. return nullptr;
  714. case tok::annot_pragma_ms_pragma:
  715. HandlePragmaMSPragma();
  716. return nullptr;
  717. case tok::annot_pragma_dump:
  718. HandlePragmaDump();
  719. return nullptr;
  720. case tok::annot_pragma_attribute:
  721. HandlePragmaAttribute();
  722. return nullptr;
  723. case tok::semi:
  724. // Either a C++11 empty-declaration or attribute-declaration.
  725. SingleDecl =
  726. Actions.ActOnEmptyDeclaration(getCurScope(), attrs, Tok.getLocation());
  727. ConsumeExtraSemi(OutsideFunction);
  728. break;
  729. case tok::r_brace:
  730. Diag(Tok, diag::err_extraneous_closing_brace);
  731. ConsumeBrace();
  732. return nullptr;
  733. case tok::eof:
  734. Diag(Tok, diag::err_expected_external_declaration);
  735. return nullptr;
  736. case tok::kw___extension__: {
  737. // __extension__ silences extension warnings in the subexpression.
  738. ExtensionRAIIObject O(Diags); // Use RAII to do this.
  739. ConsumeToken();
  740. return ParseExternalDeclaration(attrs);
  741. }
  742. case tok::kw_asm: {
  743. ProhibitAttributes(attrs);
  744. SourceLocation StartLoc = Tok.getLocation();
  745. SourceLocation EndLoc;
  746. ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc));
  747. // Check if GNU-style InlineAsm is disabled.
  748. // Empty asm string is allowed because it will not introduce
  749. // any assembly code.
  750. if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
  751. const auto *SL = cast<StringLiteral>(Result.get());
  752. if (!SL->getString().trim().empty())
  753. Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
  754. }
  755. ExpectAndConsume(tok::semi, diag::err_expected_after,
  756. "top-level asm block");
  757. if (Result.isInvalid())
  758. return nullptr;
  759. SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
  760. break;
  761. }
  762. case tok::at:
  763. return ParseObjCAtDirectives(attrs);
  764. case tok::minus:
  765. case tok::plus:
  766. if (!getLangOpts().ObjC) {
  767. Diag(Tok, diag::err_expected_external_declaration);
  768. ConsumeToken();
  769. return nullptr;
  770. }
  771. SingleDecl = ParseObjCMethodDefinition();
  772. break;
  773. case tok::code_completion:
  774. cutOffParsing();
  775. if (CurParsedObjCImpl) {
  776. // Code-complete Objective-C methods even without leading '-'/'+' prefix.
  777. Actions.CodeCompleteObjCMethodDecl(getCurScope(),
  778. /*IsInstanceMethod=*/None,
  779. /*ReturnType=*/nullptr);
  780. }
  781. Actions.CodeCompleteOrdinaryName(
  782. getCurScope(),
  783. CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace);
  784. return nullptr;
  785. case tok::kw_import:
  786. SingleDecl = ParseModuleImport(SourceLocation());
  787. break;
  788. case tok::kw_export:
  789. if (getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS) {
  790. SingleDecl = ParseExportDeclaration();
  791. break;
  792. }
  793. // This must be 'export template'. Parse it so we can diagnose our lack
  794. // of support.
  795. LLVM_FALLTHROUGH;
  796. case tok::kw_using:
  797. case tok::kw_namespace:
  798. case tok::kw_typedef:
  799. case tok::kw_template:
  800. case tok::kw_static_assert:
  801. case tok::kw__Static_assert:
  802. // A function definition cannot start with any of these keywords.
  803. {
  804. SourceLocation DeclEnd;
  805. return ParseDeclaration(DeclaratorContext::File, DeclEnd, attrs);
  806. }
  807. case tok::kw_static:
  808. // Parse (then ignore) 'static' prior to a template instantiation. This is
  809. // a GCC extension that we intentionally do not support.
  810. if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
  811. Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
  812. << 0;
  813. SourceLocation DeclEnd;
  814. return ParseDeclaration(DeclaratorContext::File, DeclEnd, attrs);
  815. }
  816. goto dont_know;
  817. case tok::kw_inline:
  818. if (getLangOpts().CPlusPlus) {
  819. tok::TokenKind NextKind = NextToken().getKind();
  820. // Inline namespaces. Allowed as an extension even in C++03.
  821. if (NextKind == tok::kw_namespace) {
  822. SourceLocation DeclEnd;
  823. return ParseDeclaration(DeclaratorContext::File, DeclEnd, attrs);
  824. }
  825. // Parse (then ignore) 'inline' prior to a template instantiation. This is
  826. // a GCC extension that we intentionally do not support.
  827. if (NextKind == tok::kw_template) {
  828. Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
  829. << 1;
  830. SourceLocation DeclEnd;
  831. return ParseDeclaration(DeclaratorContext::File, DeclEnd, attrs);
  832. }
  833. }
  834. goto dont_know;
  835. case tok::kw_extern:
  836. if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
  837. // Extern templates
  838. SourceLocation ExternLoc = ConsumeToken();
  839. SourceLocation TemplateLoc = ConsumeToken();
  840. Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
  841. diag::warn_cxx98_compat_extern_template :
  842. diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
  843. SourceLocation DeclEnd;
  844. return Actions.ConvertDeclToDeclGroup(ParseExplicitInstantiation(
  845. DeclaratorContext::File, ExternLoc, TemplateLoc, DeclEnd, attrs));
  846. }
  847. goto dont_know;
  848. case tok::kw___if_exists:
  849. case tok::kw___if_not_exists:
  850. ParseMicrosoftIfExistsExternalDeclaration();
  851. return nullptr;
  852. case tok::kw_module:
  853. Diag(Tok, diag::err_unexpected_module_decl);
  854. SkipUntil(tok::semi);
  855. return nullptr;
  856. default:
  857. dont_know:
  858. if (Tok.isEditorPlaceholder()) {
  859. ConsumeToken();
  860. return nullptr;
  861. }
  862. // We can't tell whether this is a function-definition or declaration yet.
  863. return ParseDeclarationOrFunctionDefinition(attrs, DS);
  864. }
  865. // This routine returns a DeclGroup, if the thing we parsed only contains a
  866. // single decl, convert it now.
  867. return Actions.ConvertDeclToDeclGroup(SingleDecl);
  868. }
  869. /// Determine whether the current token, if it occurs after a
  870. /// declarator, continues a declaration or declaration list.
  871. bool Parser::isDeclarationAfterDeclarator() {
  872. // Check for '= delete' or '= default'
  873. if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
  874. const Token &KW = NextToken();
  875. if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
  876. return false;
  877. }
  878. return Tok.is(tok::equal) || // int X()= -> not a function def
  879. Tok.is(tok::comma) || // int X(), -> not a function def
  880. Tok.is(tok::semi) || // int X(); -> not a function def
  881. Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
  882. Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
  883. (getLangOpts().CPlusPlus &&
  884. Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
  885. }
  886. /// Determine whether the current token, if it occurs after a
  887. /// declarator, indicates the start of a function definition.
  888. bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
  889. assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
  890. if (Tok.is(tok::l_brace)) // int X() {}
  891. return true;
  892. // Handle K&R C argument lists: int X(f) int f; {}
  893. if (!getLangOpts().CPlusPlus &&
  894. Declarator.getFunctionTypeInfo().isKNRPrototype())
  895. return isDeclarationSpecifier();
  896. if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
  897. const Token &KW = NextToken();
  898. return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
  899. }
  900. return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
  901. Tok.is(tok::kw_try); // X() try { ... }
  902. }
  903. /// Parse either a function-definition or a declaration. We can't tell which
  904. /// we have until we read up to the compound-statement in function-definition.
  905. /// TemplateParams, if non-NULL, provides the template parameters when we're
  906. /// parsing a C++ template-declaration.
  907. ///
  908. /// function-definition: [C99 6.9.1]
  909. /// decl-specs declarator declaration-list[opt] compound-statement
  910. /// [C90] function-definition: [C99 6.7.1] - implicit int result
  911. /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
  912. ///
  913. /// declaration: [C99 6.7]
  914. /// declaration-specifiers init-declarator-list[opt] ';'
  915. /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
  916. /// [OMP] threadprivate-directive
  917. /// [OMP] allocate-directive [TODO]
  918. ///
  919. Parser::DeclGroupPtrTy
  920. Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
  921. ParsingDeclSpec &DS,
  922. AccessSpecifier AS) {
  923. MaybeParseMicrosoftAttributes(DS.getAttributes());
  924. // Parse the common declaration-specifiers piece.
  925. ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS,
  926. DeclSpecContext::DSC_top_level);
  927. // If we had a free-standing type definition with a missing semicolon, we
  928. // may get this far before the problem becomes obvious.
  929. if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition(
  930. DS, AS, DeclSpecContext::DSC_top_level))
  931. return nullptr;
  932. // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
  933. // declaration-specifiers init-declarator-list[opt] ';'
  934. if (Tok.is(tok::semi)) {
  935. auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
  936. assert(DeclSpec::isDeclRep(TKind));
  937. switch(TKind) {
  938. case DeclSpec::TST_class:
  939. return 5;
  940. case DeclSpec::TST_struct:
  941. return 6;
  942. case DeclSpec::TST_union:
  943. return 5;
  944. case DeclSpec::TST_enum:
  945. return 4;
  946. case DeclSpec::TST_interface:
  947. return 9;
  948. default:
  949. llvm_unreachable("we only expect to get the length of the class/struct/union/enum");
  950. }
  951. };
  952. // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'
  953. SourceLocation CorrectLocationForAttributes =
  954. DeclSpec::isDeclRep(DS.getTypeSpecType())
  955. ? DS.getTypeSpecTypeLoc().getLocWithOffset(
  956. LengthOfTSTToken(DS.getTypeSpecType()))
  957. : SourceLocation();
  958. ProhibitAttributes(attrs, CorrectLocationForAttributes);
  959. ConsumeToken();
  960. RecordDecl *AnonRecord = nullptr;
  961. Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
  962. DS, AnonRecord);
  963. DS.complete(TheDecl);
  964. if (AnonRecord) {
  965. Decl* decls[] = {AnonRecord, TheDecl};
  966. return Actions.BuildDeclaratorGroup(decls);
  967. }
  968. return Actions.ConvertDeclToDeclGroup(TheDecl);
  969. }
  970. DS.takeAttributesFrom(attrs);
  971. // ObjC2 allows prefix attributes on class interfaces and protocols.
  972. // FIXME: This still needs better diagnostics. We should only accept
  973. // attributes here, no types, etc.
  974. if (getLangOpts().ObjC && Tok.is(tok::at)) {
  975. SourceLocation AtLoc = ConsumeToken(); // the "@"
  976. if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
  977. !Tok.isObjCAtKeyword(tok::objc_protocol) &&
  978. !Tok.isObjCAtKeyword(tok::objc_implementation)) {
  979. Diag(Tok, diag::err_objc_unexpected_attr);
  980. SkipUntil(tok::semi);
  981. return nullptr;
  982. }
  983. DS.abort();
  984. const char *PrevSpec = nullptr;
  985. unsigned DiagID;
  986. if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
  987. Actions.getASTContext().getPrintingPolicy()))
  988. Diag(AtLoc, DiagID) << PrevSpec;
  989. if (Tok.isObjCAtKeyword(tok::objc_protocol))
  990. return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
  991. if (Tok.isObjCAtKeyword(tok::objc_implementation))
  992. return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes());
  993. return Actions.ConvertDeclToDeclGroup(
  994. ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
  995. }
  996. // If the declspec consisted only of 'extern' and we have a string
  997. // literal following it, this must be a C++ linkage specifier like
  998. // 'extern "C"'.
  999. if (getLangOpts().CPlusPlus && isTokenStringLiteral() &&
  1000. DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
  1001. DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
  1002. Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::File);
  1003. return Actions.ConvertDeclToDeclGroup(TheDecl);
  1004. }
  1005. return ParseDeclGroup(DS, DeclaratorContext::File);
  1006. }
  1007. Parser::DeclGroupPtrTy
  1008. Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
  1009. ParsingDeclSpec *DS,
  1010. AccessSpecifier AS) {
  1011. if (DS) {
  1012. return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
  1013. } else {
  1014. ParsingDeclSpec PDS(*this);
  1015. // Must temporarily exit the objective-c container scope for
  1016. // parsing c constructs and re-enter objc container scope
  1017. // afterwards.
  1018. ObjCDeclContextSwitch ObjCDC(*this);
  1019. return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
  1020. }
  1021. }
  1022. /// ParseFunctionDefinition - We parsed and verified that the specified
  1023. /// Declarator is well formed. If this is a K&R-style function, read the
  1024. /// parameters declaration-list, then start the compound-statement.
  1025. ///
  1026. /// function-definition: [C99 6.9.1]
  1027. /// decl-specs declarator declaration-list[opt] compound-statement
  1028. /// [C90] function-definition: [C99 6.7.1] - implicit int result
  1029. /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
  1030. /// [C++] function-definition: [C++ 8.4]
  1031. /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
  1032. /// function-body
  1033. /// [C++] function-definition: [C++ 8.4]
  1034. /// decl-specifier-seq[opt] declarator function-try-block
  1035. ///
  1036. Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
  1037. const ParsedTemplateInfo &TemplateInfo,
  1038. LateParsedAttrList *LateParsedAttrs) {
  1039. // Poison SEH identifiers so they are flagged as illegal in function bodies.
  1040. PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
  1041. const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
  1042. TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
  1043. // If this is C90 and the declspecs were completely missing, fudge in an
  1044. // implicit int. We do this here because this is the only place where
  1045. // declaration-specifiers are completely optional in the grammar.
  1046. if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
  1047. const char *PrevSpec;
  1048. unsigned DiagID;
  1049. const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
  1050. D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
  1051. D.getIdentifierLoc(),
  1052. PrevSpec, DiagID,
  1053. Policy);
  1054. D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
  1055. }
  1056. // If this declaration was formed with a K&R-style identifier list for the
  1057. // arguments, parse declarations for all of the args next.
  1058. // int foo(a,b) int a; float b; {}
  1059. if (FTI.isKNRPrototype())
  1060. ParseKNRParamDeclarations(D);
  1061. // We should have either an opening brace or, in a C++ constructor,
  1062. // we may have a colon.
  1063. if (Tok.isNot(tok::l_brace) &&
  1064. (!getLangOpts().CPlusPlus ||
  1065. (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
  1066. Tok.isNot(tok::equal)))) {
  1067. Diag(Tok, diag::err_expected_fn_body);
  1068. // Skip over garbage, until we get to '{'. Don't eat the '{'.
  1069. SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
  1070. // If we didn't find the '{', bail out.
  1071. if (Tok.isNot(tok::l_brace))
  1072. return nullptr;
  1073. }
  1074. // Check to make sure that any normal attributes are allowed to be on
  1075. // a definition. Late parsed attributes are checked at the end.
  1076. if (Tok.isNot(tok::equal)) {
  1077. for (const ParsedAttr &AL : D.getAttributes())
  1078. if (AL.isKnownToGCC() && !AL.isStandardAttributeSyntax())
  1079. Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL;
  1080. }
  1081. // In delayed template parsing mode, for function template we consume the
  1082. // tokens and store them for late parsing at the end of the translation unit.
  1083. if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
  1084. TemplateInfo.Kind == ParsedTemplateInfo::Template &&
  1085. Actions.canDelayFunctionBody(D)) {
  1086. MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
  1087. ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
  1088. Scope::CompoundStmtScope);
  1089. Scope *ParentScope = getCurScope()->getParent();
  1090. D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
  1091. Decl *DP = Actions.HandleDeclarator(ParentScope, D,
  1092. TemplateParameterLists);
  1093. D.complete(DP);
  1094. D.getMutableDeclSpec().abort();
  1095. if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
  1096. trySkippingFunctionBody()) {
  1097. BodyScope.Exit();
  1098. return Actions.ActOnSkippedFunctionBody(DP);
  1099. }
  1100. CachedTokens Toks;
  1101. LexTemplateFunctionForLateParsing(Toks);
  1102. if (DP) {
  1103. FunctionDecl *FnD = DP->getAsFunction();
  1104. Actions.CheckForFunctionRedefinition(FnD);
  1105. Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
  1106. }
  1107. return DP;
  1108. }
  1109. else if (CurParsedObjCImpl &&
  1110. !TemplateInfo.TemplateParams &&
  1111. (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
  1112. Tok.is(tok::colon)) &&
  1113. Actions.CurContext->isTranslationUnit()) {
  1114. ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
  1115. Scope::CompoundStmtScope);
  1116. Scope *ParentScope = getCurScope()->getParent();
  1117. D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
  1118. Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
  1119. MultiTemplateParamsArg());
  1120. D.complete(FuncDecl);
  1121. D.getMutableDeclSpec().abort();
  1122. if (FuncDecl) {
  1123. // Consume the tokens and store them for later parsing.
  1124. StashAwayMethodOrFunctionBodyTokens(FuncDecl);
  1125. CurParsedObjCImpl->HasCFunction = true;
  1126. return FuncDecl;
  1127. }
  1128. // FIXME: Should we really fall through here?
  1129. }
  1130. // Enter a scope for the function body.
  1131. ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
  1132. Scope::CompoundStmtScope);
  1133. // Tell the actions module that we have entered a function definition with the
  1134. // specified Declarator for the function.
  1135. Sema::SkipBodyInfo SkipBody;
  1136. Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
  1137. TemplateInfo.TemplateParams
  1138. ? *TemplateInfo.TemplateParams
  1139. : MultiTemplateParamsArg(),
  1140. &SkipBody);
  1141. if (SkipBody.ShouldSkip) {
  1142. SkipFunctionBody();
  1143. return Res;
  1144. }
  1145. // Break out of the ParsingDeclarator context before we parse the body.
  1146. D.complete(Res);
  1147. // Break out of the ParsingDeclSpec context, too. This const_cast is
  1148. // safe because we're always the sole owner.
  1149. D.getMutableDeclSpec().abort();
  1150. // With abbreviated function templates - we need to explicitly add depth to
  1151. // account for the implicit template parameter list induced by the template.
  1152. if (auto *Template = dyn_cast_or_null<FunctionTemplateDecl>(Res))
  1153. if (Template->isAbbreviated() &&
  1154. Template->getTemplateParameters()->getParam(0)->isImplicit())
  1155. // First template parameter is implicit - meaning no explicit template
  1156. // parameter list was specified.
  1157. CurTemplateDepthTracker.addDepth(1);
  1158. if (TryConsumeToken(tok::equal)) {
  1159. assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
  1160. bool Delete = false;
  1161. SourceLocation KWLoc;
  1162. if (TryConsumeToken(tok::kw_delete, KWLoc)) {
  1163. Diag(KWLoc, getLangOpts().CPlusPlus11
  1164. ? diag::warn_cxx98_compat_defaulted_deleted_function
  1165. : diag::ext_defaulted_deleted_function)
  1166. << 1 /* deleted */;
  1167. Actions.SetDeclDeleted(Res, KWLoc);
  1168. Delete = true;
  1169. } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
  1170. Diag(KWLoc, getLangOpts().CPlusPlus11
  1171. ? diag::warn_cxx98_compat_defaulted_deleted_function
  1172. : diag::ext_defaulted_deleted_function)
  1173. << 0 /* defaulted */;
  1174. Actions.SetDeclDefaulted(Res, KWLoc);
  1175. } else {
  1176. llvm_unreachable("function definition after = not 'delete' or 'default'");
  1177. }
  1178. if (Tok.is(tok::comma)) {
  1179. Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
  1180. << Delete;
  1181. SkipUntil(tok::semi);
  1182. } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
  1183. Delete ? "delete" : "default")) {
  1184. SkipUntil(tok::semi);
  1185. }
  1186. Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
  1187. Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
  1188. return Res;
  1189. }
  1190. if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
  1191. trySkippingFunctionBody()) {
  1192. BodyScope.Exit();
  1193. Actions.ActOnSkippedFunctionBody(Res);
  1194. return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
  1195. }
  1196. if (Tok.is(tok::kw_try))
  1197. return ParseFunctionTryBlock(Res, BodyScope);
  1198. // If we have a colon, then we're probably parsing a C++
  1199. // ctor-initializer.
  1200. if (Tok.is(tok::colon)) {
  1201. ParseConstructorInitializer(Res);
  1202. // Recover from error.
  1203. if (!Tok.is(tok::l_brace)) {
  1204. BodyScope.Exit();
  1205. Actions.ActOnFinishFunctionBody(Res, nullptr);
  1206. return Res;
  1207. }
  1208. } else
  1209. Actions.ActOnDefaultCtorInitializers(Res);
  1210. // Late attributes are parsed in the same scope as the function body.
  1211. if (LateParsedAttrs)
  1212. ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
  1213. return ParseFunctionStatementBody(Res, BodyScope);
  1214. }
  1215. void Parser::SkipFunctionBody() {
  1216. if (Tok.is(tok::equal)) {
  1217. SkipUntil(tok::semi);
  1218. return;
  1219. }
  1220. bool IsFunctionTryBlock = Tok.is(tok::kw_try);
  1221. if (IsFunctionTryBlock)
  1222. ConsumeToken();
  1223. CachedTokens Skipped;
  1224. if (ConsumeAndStoreFunctionPrologue(Skipped))
  1225. SkipMalformedDecl();
  1226. else {
  1227. SkipUntil(tok::r_brace);
  1228. while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
  1229. SkipUntil(tok::l_brace);
  1230. SkipUntil(tok::r_brace);
  1231. }
  1232. }
  1233. }
  1234. /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
  1235. /// types for a function with a K&R-style identifier list for arguments.
  1236. void Parser::ParseKNRParamDeclarations(Declarator &D) {
  1237. // We know that the top-level of this declarator is a function.
  1238. DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
  1239. // Enter function-declaration scope, limiting any declarators to the
  1240. // function prototype scope, including parameter declarators.
  1241. ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
  1242. Scope::FunctionDeclarationScope | Scope::DeclScope);
  1243. // Read all the argument declarations.
  1244. while (isDeclarationSpecifier()) {
  1245. SourceLocation DSStart = Tok.getLocation();
  1246. // Parse the common declaration-specifiers piece.
  1247. DeclSpec DS(AttrFactory);
  1248. ParseDeclarationSpecifiers(DS);
  1249. // C99 6.9.1p6: 'each declaration in the declaration list shall have at
  1250. // least one declarator'.
  1251. // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
  1252. // the declarations though. It's trivial to ignore them, really hard to do
  1253. // anything else with them.
  1254. if (TryConsumeToken(tok::semi)) {
  1255. Diag(DSStart, diag::err_declaration_does_not_declare_param);
  1256. continue;
  1257. }
  1258. // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
  1259. // than register.
  1260. if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
  1261. DS.getStorageClassSpec() != DeclSpec::SCS_register) {
  1262. Diag(DS.getStorageClassSpecLoc(),
  1263. diag::err_invalid_storage_class_in_func_decl);
  1264. DS.ClearStorageClassSpecs();
  1265. }
  1266. if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) {
  1267. Diag(DS.getThreadStorageClassSpecLoc(),
  1268. diag::err_invalid_storage_class_in_func_decl);
  1269. DS.ClearStorageClassSpecs();
  1270. }
  1271. // Parse the first declarator attached to this declspec.
  1272. Declarator ParmDeclarator(DS, DeclaratorContext::KNRTypeList);
  1273. ParseDeclarator(ParmDeclarator);
  1274. // Handle the full declarator list.
  1275. while (true) {
  1276. // If attributes are present, parse them.
  1277. MaybeParseGNUAttributes(ParmDeclarator);
  1278. // Ask the actions module to compute the type for this declarator.
  1279. Decl *Param =
  1280. Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
  1281. if (Param &&
  1282. // A missing identifier has already been diagnosed.
  1283. ParmDeclarator.getIdentifier()) {
  1284. // Scan the argument list looking for the correct param to apply this
  1285. // type.
  1286. for (unsigned i = 0; ; ++i) {
  1287. // C99 6.9.1p6: those declarators shall declare only identifiers from
  1288. // the identifier list.
  1289. if (i == FTI.NumParams) {
  1290. Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
  1291. << ParmDeclarator.getIdentifier();
  1292. break;
  1293. }
  1294. if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
  1295. // Reject redefinitions of parameters.
  1296. if (FTI.Params[i].Param) {
  1297. Diag(ParmDeclarator.getIdentifierLoc(),
  1298. diag::err_param_redefinition)
  1299. << ParmDeclarator.getIdentifier();
  1300. } else {
  1301. FTI.Params[i].Param = Param;
  1302. }
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. // If we don't have a comma, it is either the end of the list (a ';') or
  1308. // an error, bail out.
  1309. if (Tok.isNot(tok::comma))
  1310. break;
  1311. ParmDeclarator.clear();
  1312. // Consume the comma.
  1313. ParmDeclarator.setCommaLoc(ConsumeToken());
  1314. // Parse the next declarator.
  1315. ParseDeclarator(ParmDeclarator);
  1316. }
  1317. // Consume ';' and continue parsing.
  1318. if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
  1319. continue;
  1320. // Otherwise recover by skipping to next semi or mandatory function body.
  1321. if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
  1322. break;
  1323. TryConsumeToken(tok::semi);
  1324. }
  1325. // The actions module must verify that all arguments were declared.
  1326. Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
  1327. }
  1328. /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
  1329. /// allowed to be a wide string, and is not subject to character translation.
  1330. /// Unlike GCC, we also diagnose an empty string literal when parsing for an
  1331. /// asm label as opposed to an asm statement, because such a construct does not
  1332. /// behave well.
  1333. ///
  1334. /// [GNU] asm-string-literal:
  1335. /// string-literal
  1336. ///
  1337. ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
  1338. if (!isTokenStringLiteral()) {
  1339. Diag(Tok, diag::err_expected_string_literal)
  1340. << /*Source='in...'*/0 << "'asm'";
  1341. return ExprError();
  1342. }
  1343. ExprResult AsmString(ParseStringLiteralExpression());
  1344. if (!AsmString.isInvalid()) {
  1345. const auto *SL = cast<StringLiteral>(AsmString.get());
  1346. if (!SL->isAscii()) {
  1347. Diag(Tok, diag::err_asm_operand_wide_string_literal)
  1348. << SL->isWide()
  1349. << SL->getSourceRange();
  1350. return ExprError();
  1351. }
  1352. if (ForAsmLabel && SL->getString().empty()) {
  1353. Diag(Tok, diag::err_asm_operand_wide_string_literal)
  1354. << 2 /* an empty */ << SL->getSourceRange();
  1355. return ExprError();
  1356. }
  1357. }
  1358. return AsmString;
  1359. }
  1360. /// ParseSimpleAsm
  1361. ///
  1362. /// [GNU] simple-asm-expr:
  1363. /// 'asm' '(' asm-string-literal ')'
  1364. ///
  1365. ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) {
  1366. assert(Tok.is(tok::kw_asm) && "Not an asm!");
  1367. SourceLocation Loc = ConsumeToken();
  1368. if (isGNUAsmQualifier(Tok)) {
  1369. // Remove from the end of 'asm' to the end of the asm qualifier.
  1370. SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
  1371. PP.getLocForEndOfToken(Tok.getLocation()));
  1372. Diag(Tok, diag::err_global_asm_qualifier_ignored)
  1373. << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok))
  1374. << FixItHint::CreateRemoval(RemovalRange);
  1375. ConsumeToken();
  1376. }
  1377. BalancedDelimiterTracker T(*this, tok::l_paren);
  1378. if (T.consumeOpen()) {
  1379. Diag(Tok, diag::err_expected_lparen_after) << "asm";
  1380. return ExprError();
  1381. }
  1382. ExprResult Result(ParseAsmStringLiteral(ForAsmLabel));
  1383. if (!Result.isInvalid()) {
  1384. // Close the paren and get the location of the end bracket
  1385. T.consumeClose();
  1386. if (EndLoc)
  1387. *EndLoc = T.getCloseLocation();
  1388. } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
  1389. if (EndLoc)
  1390. *EndLoc = Tok.getLocation();
  1391. ConsumeParen();
  1392. }
  1393. return Result;
  1394. }
  1395. /// Get the TemplateIdAnnotation from the token and put it in the
  1396. /// cleanup pool so that it gets destroyed when parsing the current top level
  1397. /// declaration is finished.
  1398. TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
  1399. assert(tok.is(tok::annot_template_id) && "Expected template-id token");
  1400. TemplateIdAnnotation *
  1401. Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
  1402. return Id;
  1403. }
  1404. void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
  1405. // Push the current token back into the token stream (or revert it if it is
  1406. // cached) and use an annotation scope token for current token.
  1407. if (PP.isBacktrackEnabled())
  1408. PP.RevertCachedTokens(1);
  1409. else
  1410. PP.EnterToken(Tok, /*IsReinject=*/true);
  1411. Tok.setKind(tok::annot_cxxscope);
  1412. Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
  1413. Tok.setAnnotationRange(SS.getRange());
  1414. // In case the tokens were cached, have Preprocessor replace them
  1415. // with the annotation token. We don't need to do this if we've
  1416. // just reverted back to a prior state.
  1417. if (IsNewAnnotation)
  1418. PP.AnnotateCachedTokens(Tok);
  1419. }
  1420. /// Attempt to classify the name at the current token position. This may
  1421. /// form a type, scope or primary expression annotation, or replace the token
  1422. /// with a typo-corrected keyword. This is only appropriate when the current
  1423. /// name must refer to an entity which has already been declared.
  1424. ///
  1425. /// \param CCC Indicates how to perform typo-correction for this name. If NULL,
  1426. /// no typo correction will be performed.
  1427. Parser::AnnotatedNameKind
  1428. Parser::TryAnnotateName(CorrectionCandidateCallback *CCC) {
  1429. assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
  1430. const bool EnteringContext = false;
  1431. const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
  1432. CXXScopeSpec SS;
  1433. if (getLangOpts().CPlusPlus &&
  1434. ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
  1435. /*ObjectHasErrors=*/false,
  1436. EnteringContext))
  1437. return ANK_Error;
  1438. if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
  1439. if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
  1440. return ANK_Error;
  1441. return ANK_Unresolved;
  1442. }
  1443. IdentifierInfo *Name = Tok.getIdentifierInfo();
  1444. SourceLocation NameLoc = Tok.getLocation();
  1445. // FIXME: Move the tentative declaration logic into ClassifyName so we can
  1446. // typo-correct to tentatively-declared identifiers.
  1447. if (isTentativelyDeclared(Name)) {
  1448. // Identifier has been tentatively declared, and thus cannot be resolved as
  1449. // an expression. Fall back to annotating it as a type.
  1450. if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
  1451. return ANK_Error;
  1452. return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
  1453. }
  1454. Token Next = NextToken();
  1455. // Look up and classify the identifier. We don't perform any typo-correction
  1456. // after a scope specifier, because in general we can't recover from typos
  1457. // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to
  1458. // jump back into scope specifier parsing).
  1459. Sema::NameClassification Classification = Actions.ClassifyName(
  1460. getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr);
  1461. // If name lookup found nothing and we guessed that this was a template name,
  1462. // double-check before committing to that interpretation. C++20 requires that
  1463. // we interpret this as a template-id if it can be, but if it can't be, then
  1464. // this is an error recovery case.
  1465. if (Classification.getKind() == Sema::NC_UndeclaredTemplate &&
  1466. isTemplateArgumentList(1) == TPResult::False) {
  1467. // It's not a template-id; re-classify without the '<' as a hint.
  1468. Token FakeNext = Next;
  1469. FakeNext.setKind(tok::unknown);
  1470. Classification =
  1471. Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext,
  1472. SS.isEmpty() ? CCC : nullptr);
  1473. }
  1474. switch (Classification.getKind()) {
  1475. case Sema::NC_Error:
  1476. return ANK_Error;
  1477. case Sema::NC_Keyword:
  1478. // The identifier was typo-corrected to a keyword.
  1479. Tok.setIdentifierInfo(Name);
  1480. Tok.setKind(Name->getTokenID());
  1481. PP.TypoCorrectToken(Tok);
  1482. if (SS.isNotEmpty())
  1483. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1484. // We've "annotated" this as a keyword.
  1485. return ANK_Success;
  1486. case Sema::NC_Unknown:
  1487. // It's not something we know about. Leave it unannotated.
  1488. break;
  1489. case Sema::NC_Type: {
  1490. if (TryAltiVecVectorToken())
  1491. // vector has been found as a type id when altivec is enabled but
  1492. // this is followed by a declaration specifier so this is really the
  1493. // altivec vector token. Leave it unannotated.
  1494. break;
  1495. SourceLocation BeginLoc = NameLoc;
  1496. if (SS.isNotEmpty())
  1497. BeginLoc = SS.getBeginLoc();
  1498. /// An Objective-C object type followed by '<' is a specialization of
  1499. /// a parameterized class type or a protocol-qualified type.
  1500. ParsedType Ty = Classification.getType();
  1501. if (getLangOpts().ObjC && NextToken().is(tok::less) &&
  1502. (Ty.get()->isObjCObjectType() ||
  1503. Ty.get()->isObjCObjectPointerType())) {
  1504. // Consume the name.
  1505. SourceLocation IdentifierLoc = ConsumeToken();
  1506. SourceLocation NewEndLoc;
  1507. TypeResult NewType
  1508. = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
  1509. /*consumeLastToken=*/false,
  1510. NewEndLoc);
  1511. if (NewType.isUsable())
  1512. Ty = NewType.get();
  1513. else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
  1514. return ANK_Error;
  1515. }
  1516. Tok.setKind(tok::annot_typename);
  1517. setTypeAnnotation(Tok, Ty);
  1518. Tok.setAnnotationEndLoc(Tok.getLocation());
  1519. Tok.setLocation(BeginLoc);
  1520. PP.AnnotateCachedTokens(Tok);
  1521. return ANK_Success;
  1522. }
  1523. case Sema::NC_OverloadSet:
  1524. Tok.setKind(tok::annot_overload_set);
  1525. setExprAnnotation(Tok, Classification.getExpression());
  1526. Tok.setAnnotationEndLoc(NameLoc);
  1527. if (SS.isNotEmpty())
  1528. Tok.setLocation(SS.getBeginLoc());
  1529. PP.AnnotateCachedTokens(Tok);
  1530. return ANK_Success;
  1531. case Sema::NC_NonType:
  1532. if (TryAltiVecVectorToken())
  1533. // vector has been found as a non-type id when altivec is enabled but
  1534. // this is followed by a declaration specifier so this is really the
  1535. // altivec vector token. Leave it unannotated.
  1536. break;
  1537. Tok.setKind(tok::annot_non_type);
  1538. setNonTypeAnnotation(Tok, Classification.getNonTypeDecl());
  1539. Tok.setLocation(NameLoc);
  1540. Tok.setAnnotationEndLoc(NameLoc);
  1541. PP.AnnotateCachedTokens(Tok);
  1542. if (SS.isNotEmpty())
  1543. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1544. return ANK_Success;
  1545. case Sema::NC_UndeclaredNonType:
  1546. case Sema::NC_DependentNonType:
  1547. Tok.setKind(Classification.getKind() == Sema::NC_UndeclaredNonType
  1548. ? tok::annot_non_type_undeclared
  1549. : tok::annot_non_type_dependent);
  1550. setIdentifierAnnotation(Tok, Name);
  1551. Tok.setLocation(NameLoc);
  1552. Tok.setAnnotationEndLoc(NameLoc);
  1553. PP.AnnotateCachedTokens(Tok);
  1554. if (SS.isNotEmpty())
  1555. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1556. return ANK_Success;
  1557. case Sema::NC_TypeTemplate:
  1558. if (Next.isNot(tok::less)) {
  1559. // This may be a type template being used as a template template argument.
  1560. if (SS.isNotEmpty())
  1561. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1562. return ANK_TemplateName;
  1563. }
  1564. LLVM_FALLTHROUGH;
  1565. case Sema::NC_VarTemplate:
  1566. case Sema::NC_FunctionTemplate:
  1567. case Sema::NC_UndeclaredTemplate: {
  1568. // We have a type, variable or function template followed by '<'.
  1569. ConsumeToken();
  1570. UnqualifiedId Id;
  1571. Id.setIdentifier(Name, NameLoc);
  1572. if (AnnotateTemplateIdToken(
  1573. TemplateTy::make(Classification.getTemplateName()),
  1574. Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
  1575. return ANK_Error;
  1576. return ANK_Success;
  1577. }
  1578. case Sema::NC_Concept: {
  1579. UnqualifiedId Id;
  1580. Id.setIdentifier(Name, NameLoc);
  1581. if (Next.is(tok::less))
  1582. // We have a concept name followed by '<'. Consume the identifier token so
  1583. // we reach the '<' and annotate it.
  1584. ConsumeToken();
  1585. if (AnnotateTemplateIdToken(
  1586. TemplateTy::make(Classification.getTemplateName()),
  1587. Classification.getTemplateNameKind(), SS, SourceLocation(), Id,
  1588. /*AllowTypeAnnotation=*/false, /*TypeConstraint=*/true))
  1589. return ANK_Error;
  1590. return ANK_Success;
  1591. }
  1592. }
  1593. // Unable to classify the name, but maybe we can annotate a scope specifier.
  1594. if (SS.isNotEmpty())
  1595. AnnotateScopeToken(SS, !WasScopeAnnotation);
  1596. return ANK_Unresolved;
  1597. }
  1598. bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
  1599. assert(Tok.isNot(tok::identifier));
  1600. Diag(Tok, diag::ext_keyword_as_ident)
  1601. << PP.getSpelling(Tok)
  1602. << DisableKeyword;
  1603. if (DisableKeyword)
  1604. Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
  1605. Tok.setKind(tok::identifier);
  1606. return true;
  1607. }
  1608. /// TryAnnotateTypeOrScopeToken - If the current token position is on a
  1609. /// typename (possibly qualified in C++) or a C++ scope specifier not followed
  1610. /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
  1611. /// with a single annotation token representing the typename or C++ scope
  1612. /// respectively.
  1613. /// This simplifies handling of C++ scope specifiers and allows efficient
  1614. /// backtracking without the need to re-parse and resolve nested-names and
  1615. /// typenames.
  1616. /// It will mainly be called when we expect to treat identifiers as typenames
  1617. /// (if they are typenames). For example, in C we do not expect identifiers
  1618. /// inside expressions to be treated as typenames so it will not be called
  1619. /// for expressions in C.
  1620. /// The benefit for C/ObjC is that a typename will be annotated and
  1621. /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
  1622. /// will not be called twice, once to check whether we have a declaration
  1623. /// specifier, and another one to get the actual type inside
  1624. /// ParseDeclarationSpecifiers).
  1625. ///
  1626. /// This returns true if an error occurred.
  1627. ///
  1628. /// Note that this routine emits an error if you call it with ::new or ::delete
  1629. /// as the current tokens, so only call it in contexts where these are invalid.
  1630. bool Parser::TryAnnotateTypeOrScopeToken() {
  1631. assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
  1632. Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
  1633. Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
  1634. Tok.is(tok::kw___super)) &&
  1635. "Cannot be a type or scope token!");
  1636. if (Tok.is(tok::kw_typename)) {
  1637. // MSVC lets you do stuff like:
  1638. // typename typedef T_::D D;
  1639. //
  1640. // We will consume the typedef token here and put it back after we have
  1641. // parsed the first identifier, transforming it into something more like:
  1642. // typename T_::D typedef D;
  1643. if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
  1644. Token TypedefToken;
  1645. PP.Lex(TypedefToken);
  1646. bool Result = TryAnnotateTypeOrScopeToken();
  1647. PP.EnterToken(Tok, /*IsReinject=*/true);
  1648. Tok = TypedefToken;
  1649. if (!Result)
  1650. Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
  1651. return Result;
  1652. }
  1653. // Parse a C++ typename-specifier, e.g., "typename T::type".
  1654. //
  1655. // typename-specifier:
  1656. // 'typename' '::' [opt] nested-name-specifier identifier
  1657. // 'typename' '::' [opt] nested-name-specifier template [opt]
  1658. // simple-template-id
  1659. SourceLocation TypenameLoc = ConsumeToken();
  1660. CXXScopeSpec SS;
  1661. if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
  1662. /*ObjectHasErrors=*/false,
  1663. /*EnteringContext=*/false, nullptr,
  1664. /*IsTypename*/ true))
  1665. return true;
  1666. if (SS.isEmpty()) {
  1667. if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
  1668. Tok.is(tok::annot_decltype)) {
  1669. // Attempt to recover by skipping the invalid 'typename'
  1670. if (Tok.is(tok::annot_decltype) ||
  1671. (!TryAnnotateTypeOrScopeToken() && Tok.isAnnotation())) {
  1672. unsigned DiagID = diag::err_expected_qualified_after_typename;
  1673. // MS compatibility: MSVC permits using known types with typename.
  1674. // e.g. "typedef typename T* pointer_type"
  1675. if (getLangOpts().MicrosoftExt)
  1676. DiagID = diag::warn_expected_qualified_after_typename;
  1677. Diag(Tok.getLocation(), DiagID);
  1678. return false;
  1679. }
  1680. }
  1681. if (Tok.isEditorPlaceholder())
  1682. return true;
  1683. Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
  1684. return true;
  1685. }
  1686. TypeResult Ty;
  1687. if (Tok.is(tok::identifier)) {
  1688. // FIXME: check whether the next token is '<', first!
  1689. Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
  1690. *Tok.getIdentifierInfo(),
  1691. Tok.getLocation());
  1692. } else if (Tok.is(tok::annot_template_id)) {
  1693. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1694. if (!TemplateId->mightBeType()) {
  1695. Diag(Tok, diag::err_typename_refers_to_non_type_template)
  1696. << Tok.getAnnotationRange();
  1697. return true;
  1698. }
  1699. ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
  1700. TemplateId->NumArgs);
  1701. Ty = TemplateId->isInvalid()
  1702. ? TypeError()
  1703. : Actions.ActOnTypenameType(
  1704. getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc,
  1705. TemplateId->Template, TemplateId->Name,
  1706. TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
  1707. TemplateArgsPtr, TemplateId->RAngleLoc);
  1708. } else {
  1709. Diag(Tok, diag::err_expected_type_name_after_typename)
  1710. << SS.getRange();
  1711. return true;
  1712. }
  1713. SourceLocation EndLoc = Tok.getLastLoc();
  1714. Tok.setKind(tok::annot_typename);
  1715. setTypeAnnotation(Tok, Ty);
  1716. Tok.setAnnotationEndLoc(EndLoc);
  1717. Tok.setLocation(TypenameLoc);
  1718. PP.AnnotateCachedTokens(Tok);
  1719. return false;
  1720. }
  1721. // Remembers whether the token was originally a scope annotation.
  1722. bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
  1723. CXXScopeSpec SS;
  1724. if (getLangOpts().CPlusPlus)
  1725. if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
  1726. /*ObjectHasErrors=*/false,
  1727. /*EnteringContext*/ false))
  1728. return true;
  1729. return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation);
  1730. }
  1731. /// Try to annotate a type or scope token, having already parsed an
  1732. /// optional scope specifier. \p IsNewScope should be \c true unless the scope
  1733. /// specifier was extracted from an existing tok::annot_cxxscope annotation.
  1734. bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(CXXScopeSpec &SS,
  1735. bool IsNewScope) {
  1736. if (Tok.is(tok::identifier)) {
  1737. // Determine whether the identifier is a type name.
  1738. if (ParsedType Ty = Actions.getTypeName(
  1739. *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
  1740. false, NextToken().is(tok::period), nullptr,
  1741. /*IsCtorOrDtorName=*/false,
  1742. /*NonTrivialTypeSourceInfo*/true,
  1743. /*IsClassTemplateDeductionContext*/true)) {
  1744. SourceLocation BeginLoc = Tok.getLocation();
  1745. if (SS.isNotEmpty()) // it was a C++ qualified type name.
  1746. BeginLoc = SS.getBeginLoc();
  1747. /// An Objective-C object type followed by '<' is a specialization of
  1748. /// a parameterized class type or a protocol-qualified type.
  1749. if (getLangOpts().ObjC && NextToken().is(tok::less) &&
  1750. (Ty.get()->isObjCObjectType() ||
  1751. Ty.get()->isObjCObjectPointerType())) {
  1752. // Consume the name.
  1753. SourceLocation IdentifierLoc = ConsumeToken();
  1754. SourceLocation NewEndLoc;
  1755. TypeResult NewType
  1756. = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
  1757. /*consumeLastToken=*/false,
  1758. NewEndLoc);
  1759. if (NewType.isUsable())
  1760. Ty = NewType.get();
  1761. else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
  1762. return false;
  1763. }
  1764. // This is a typename. Replace the current token in-place with an
  1765. // annotation type token.
  1766. Tok.setKind(tok::annot_typename);
  1767. setTypeAnnotation(Tok, Ty);
  1768. Tok.setAnnotationEndLoc(Tok.getLocation());
  1769. Tok.setLocation(BeginLoc);
  1770. // In case the tokens were cached, have Preprocessor replace
  1771. // them with the annotation token.
  1772. PP.AnnotateCachedTokens(Tok);
  1773. return false;
  1774. }
  1775. if (!getLangOpts().CPlusPlus) {
  1776. // If we're in C, we can't have :: tokens at all (the lexer won't return
  1777. // them). If the identifier is not a type, then it can't be scope either,
  1778. // just early exit.
  1779. return false;
  1780. }
  1781. // If this is a template-id, annotate with a template-id or type token.
  1782. // FIXME: This appears to be dead code. We already have formed template-id
  1783. // tokens when parsing the scope specifier; this can never form a new one.
  1784. if (NextToken().is(tok::less)) {
  1785. TemplateTy Template;
  1786. UnqualifiedId TemplateName;
  1787. TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
  1788. bool MemberOfUnknownSpecialization;
  1789. if (TemplateNameKind TNK = Actions.isTemplateName(
  1790. getCurScope(), SS,
  1791. /*hasTemplateKeyword=*/false, TemplateName,
  1792. /*ObjectType=*/nullptr, /*EnteringContext*/false, Template,
  1793. MemberOfUnknownSpecialization)) {
  1794. // Only annotate an undeclared template name as a template-id if the
  1795. // following tokens have the form of a template argument list.
  1796. if (TNK != TNK_Undeclared_template ||
  1797. isTemplateArgumentList(1) != TPResult::False) {
  1798. // Consume the identifier.
  1799. ConsumeToken();
  1800. if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
  1801. TemplateName)) {
  1802. // If an unrecoverable error occurred, we need to return true here,
  1803. // because the token stream is in a damaged state. We may not
  1804. // return a valid identifier.
  1805. return true;
  1806. }
  1807. }
  1808. }
  1809. }
  1810. // The current token, which is either an identifier or a
  1811. // template-id, is not part of the annotation. Fall through to
  1812. // push that token back into the stream and complete the C++ scope
  1813. // specifier annotation.
  1814. }
  1815. if (Tok.is(tok::annot_template_id)) {
  1816. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1817. if (TemplateId->Kind == TNK_Type_template) {
  1818. // A template-id that refers to a type was parsed into a
  1819. // template-id annotation in a context where we weren't allowed
  1820. // to produce a type annotation token. Update the template-id
  1821. // annotation token to a type annotation token now.
  1822. AnnotateTemplateIdTokenAsType(SS);
  1823. return false;
  1824. }
  1825. }
  1826. if (SS.isEmpty())
  1827. return false;
  1828. // A C++ scope specifier that isn't followed by a typename.
  1829. AnnotateScopeToken(SS, IsNewScope);
  1830. return false;
  1831. }
  1832. /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
  1833. /// annotates C++ scope specifiers and template-ids. This returns
  1834. /// true if there was an error that could not be recovered from.
  1835. ///
  1836. /// Note that this routine emits an error if you call it with ::new or ::delete
  1837. /// as the current tokens, so only call it in contexts where these are invalid.
  1838. bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
  1839. assert(getLangOpts().CPlusPlus &&
  1840. "Call sites of this function should be guarded by checking for C++");
  1841. assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");
  1842. CXXScopeSpec SS;
  1843. if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
  1844. /*ObjectHasErrors=*/false,
  1845. EnteringContext))
  1846. return true;
  1847. if (SS.isEmpty())
  1848. return false;
  1849. AnnotateScopeToken(SS, true);
  1850. return false;
  1851. }
  1852. bool Parser::isTokenEqualOrEqualTypo() {
  1853. tok::TokenKind Kind = Tok.getKind();
  1854. switch (Kind) {
  1855. default:
  1856. return false;
  1857. case tok::ampequal: // &=
  1858. case tok::starequal: // *=
  1859. case tok::plusequal: // +=
  1860. case tok::minusequal: // -=
  1861. case tok::exclaimequal: // !=
  1862. case tok::slashequal: // /=
  1863. case tok::percentequal: // %=
  1864. case tok::lessequal: // <=
  1865. case tok::lesslessequal: // <<=
  1866. case tok::greaterequal: // >=
  1867. case tok::greatergreaterequal: // >>=
  1868. case tok::caretequal: // ^=
  1869. case tok::pipeequal: // |=
  1870. case tok::equalequal: // ==
  1871. Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
  1872. << Kind
  1873. << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
  1874. LLVM_FALLTHROUGH;
  1875. case tok::equal:
  1876. return true;
  1877. }
  1878. }
  1879. SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
  1880. assert(Tok.is(tok::code_completion));
  1881. PrevTokLocation = Tok.getLocation();
  1882. for (Scope *S = getCurScope(); S; S = S->getParent()) {
  1883. if (S->getFlags() & Scope::FnScope) {
  1884. cutOffParsing();
  1885. Actions.CodeCompleteOrdinaryName(getCurScope(),
  1886. Sema::PCC_RecoveryInFunction);
  1887. return PrevTokLocation;
  1888. }
  1889. if (S->getFlags() & Scope::ClassScope) {
  1890. cutOffParsing();
  1891. Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
  1892. return PrevTokLocation;
  1893. }
  1894. }
  1895. cutOffParsing();
  1896. Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
  1897. return PrevTokLocation;
  1898. }
  1899. // Code-completion pass-through functions
  1900. void Parser::CodeCompleteDirective(bool InConditional) {
  1901. Actions.CodeCompletePreprocessorDirective(InConditional);
  1902. }
  1903. void Parser::CodeCompleteInConditionalExclusion() {
  1904. Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
  1905. }
  1906. void Parser::CodeCompleteMacroName(bool IsDefinition) {
  1907. Actions.CodeCompletePreprocessorMacroName(IsDefinition);
  1908. }
  1909. void Parser::CodeCompletePreprocessorExpression() {
  1910. Actions.CodeCompletePreprocessorExpression();
  1911. }
  1912. void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
  1913. MacroInfo *MacroInfo,
  1914. unsigned ArgumentIndex) {
  1915. Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
  1916. ArgumentIndex);
  1917. }
  1918. void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {
  1919. Actions.CodeCompleteIncludedFile(Dir, IsAngled);
  1920. }
  1921. void Parser::CodeCompleteNaturalLanguage() {
  1922. Actions.CodeCompleteNaturalLanguage();
  1923. }
  1924. bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
  1925. assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
  1926. "Expected '__if_exists' or '__if_not_exists'");
  1927. Result.IsIfExists = Tok.is(tok::kw___if_exists);
  1928. Result.KeywordLoc = ConsumeToken();
  1929. BalancedDelimiterTracker T(*this, tok::l_paren);
  1930. if (T.consumeOpen()) {
  1931. Diag(Tok, diag::err_expected_lparen_after)
  1932. << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
  1933. return true;
  1934. }
  1935. // Parse nested-name-specifier.
  1936. if (getLangOpts().CPlusPlus)
  1937. ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,
  1938. /*ObjectHasErrors=*/false,
  1939. /*EnteringContext=*/false);
  1940. // Check nested-name specifier.
  1941. if (Result.SS.isInvalid()) {
  1942. T.skipToEnd();
  1943. return true;
  1944. }
  1945. // Parse the unqualified-id.
  1946. SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
  1947. if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,
  1948. /*ObjectHadErrors=*/false, /*EnteringContext*/ false,
  1949. /*AllowDestructorName*/ true,
  1950. /*AllowConstructorName*/ true,
  1951. /*AllowDeductionGuide*/ false, &TemplateKWLoc,
  1952. Result.Name)) {
  1953. T.skipToEnd();
  1954. return true;
  1955. }
  1956. if (T.consumeClose())
  1957. return true;
  1958. // Check if the symbol exists.
  1959. switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
  1960. Result.IsIfExists, Result.SS,
  1961. Result.Name)) {
  1962. case Sema::IER_Exists:
  1963. Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
  1964. break;
  1965. case Sema::IER_DoesNotExist:
  1966. Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
  1967. break;
  1968. case Sema::IER_Dependent:
  1969. Result.Behavior = IEB_Dependent;
  1970. break;
  1971. case Sema::IER_Error:
  1972. return true;
  1973. }
  1974. return false;
  1975. }
  1976. void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
  1977. IfExistsCondition Result;
  1978. if (ParseMicrosoftIfExistsCondition(Result))
  1979. return;
  1980. BalancedDelimiterTracker Braces(*this, tok::l_brace);
  1981. if (Braces.consumeOpen()) {
  1982. Diag(Tok, diag::err_expected) << tok::l_brace;
  1983. return;
  1984. }
  1985. switch (Result.Behavior) {
  1986. case IEB_Parse:
  1987. // Parse declarations below.
  1988. break;
  1989. case IEB_Dependent:
  1990. llvm_unreachable("Cannot have a dependent external declaration");
  1991. case IEB_Skip:
  1992. Braces.skipToEnd();
  1993. return;
  1994. }
  1995. // Parse the declarations.
  1996. // FIXME: Support module import within __if_exists?
  1997. while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
  1998. ParsedAttributesWithRange attrs(AttrFactory);
  1999. MaybeParseCXX11Attributes(attrs);
  2000. DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
  2001. if (Result && !getCurScope()->getParent())
  2002. Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
  2003. }
  2004. Braces.consumeClose();
  2005. }
  2006. /// Parse a declaration beginning with the 'module' keyword or C++20
  2007. /// context-sensitive keyword (optionally preceded by 'export').
  2008. ///
  2009. /// module-declaration: [Modules TS + P0629R0]
  2010. /// 'export'[opt] 'module' module-name attribute-specifier-seq[opt] ';'
  2011. ///
  2012. /// global-module-fragment: [C++2a]
  2013. /// 'module' ';' top-level-declaration-seq[opt]
  2014. /// module-declaration: [C++2a]
  2015. /// 'export'[opt] 'module' module-name module-partition[opt]
  2016. /// attribute-specifier-seq[opt] ';'
  2017. /// private-module-fragment: [C++2a]
  2018. /// 'module' ':' 'private' ';' top-level-declaration-seq[opt]
  2019. Parser::DeclGroupPtrTy Parser::ParseModuleDecl(bool IsFirstDecl) {
  2020. SourceLocation StartLoc = Tok.getLocation();
  2021. Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)
  2022. ? Sema::ModuleDeclKind::Interface
  2023. : Sema::ModuleDeclKind::Implementation;
  2024. assert(
  2025. (Tok.is(tok::kw_module) ||
  2026. (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&
  2027. "not a module declaration");
  2028. SourceLocation ModuleLoc = ConsumeToken();
  2029. // Attributes appear after the module name, not before.
  2030. // FIXME: Suggest moving the attributes later with a fixit.
  2031. DiagnoseAndSkipCXX11Attributes();
  2032. // Parse a global-module-fragment, if present.
  2033. if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {
  2034. SourceLocation SemiLoc = ConsumeToken();
  2035. if (!IsFirstDecl) {
  2036. Diag(StartLoc, diag::err_global_module_introducer_not_at_start)
  2037. << SourceRange(StartLoc, SemiLoc);
  2038. return nullptr;
  2039. }
  2040. if (MDK == Sema::ModuleDeclKind::Interface) {
  2041. Diag(StartLoc, diag::err_module_fragment_exported)
  2042. << /*global*/0 << FixItHint::CreateRemoval(StartLoc);
  2043. }
  2044. return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);
  2045. }
  2046. // Parse a private-module-fragment, if present.
  2047. if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&
  2048. NextToken().is(tok::kw_private)) {
  2049. if (MDK == Sema::ModuleDeclKind::Interface) {
  2050. Diag(StartLoc, diag::err_module_fragment_exported)
  2051. << /*private*/1 << FixItHint::CreateRemoval(StartLoc);
  2052. }
  2053. ConsumeToken();
  2054. SourceLocation PrivateLoc = ConsumeToken();
  2055. DiagnoseAndSkipCXX11Attributes();
  2056. ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);
  2057. return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);
  2058. }
  2059. SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
  2060. if (ParseModuleName(ModuleLoc, Path, /*IsImport*/false))
  2061. return nullptr;
  2062. // Parse the optional module-partition.
  2063. if (Tok.is(tok::colon)) {
  2064. SourceLocation ColonLoc = ConsumeToken();
  2065. SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
  2066. if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/false))
  2067. return nullptr;
  2068. // FIXME: Support module partition declarations.
  2069. Diag(ColonLoc, diag::err_unsupported_module_partition)
  2070. << SourceRange(ColonLoc, Partition.back().second);
  2071. // Recover by parsing as a non-partition.
  2072. }
  2073. // We don't support any module attributes yet; just parse them and diagnose.
  2074. ParsedAttributesWithRange Attrs(AttrFactory);
  2075. MaybeParseCXX11Attributes(Attrs);
  2076. ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr);
  2077. ExpectAndConsumeSemi(diag::err_module_expected_semi);
  2078. return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, IsFirstDecl);
  2079. }
  2080. /// Parse a module import declaration. This is essentially the same for
  2081. /// Objective-C and the C++ Modules TS, except for the leading '@' (in ObjC)
  2082. /// and the trailing optional attributes (in C++).
  2083. ///
  2084. /// [ObjC] @import declaration:
  2085. /// '@' 'import' module-name ';'
  2086. /// [ModTS] module-import-declaration:
  2087. /// 'import' module-name attribute-specifier-seq[opt] ';'
  2088. /// [C++2a] module-import-declaration:
  2089. /// 'export'[opt] 'import' module-name
  2090. /// attribute-specifier-seq[opt] ';'
  2091. /// 'export'[opt] 'import' module-partition
  2092. /// attribute-specifier-seq[opt] ';'
  2093. /// 'export'[opt] 'import' header-name
  2094. /// attribute-specifier-seq[opt] ';'
  2095. Decl *Parser::ParseModuleImport(SourceLocation AtLoc) {
  2096. SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;
  2097. SourceLocation ExportLoc;
  2098. TryConsumeToken(tok::kw_export, ExportLoc);
  2099. assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)
  2100. : Tok.isObjCAtKeyword(tok::objc_import)) &&
  2101. "Improper start to module import");
  2102. bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
  2103. SourceLocation ImportLoc = ConsumeToken();
  2104. SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
  2105. Module *HeaderUnit = nullptr;
  2106. if (Tok.is(tok::header_name)) {
  2107. // This is a header import that the preprocessor decided we should skip
  2108. // because it was malformed in some way. Parse and ignore it; it's already
  2109. // been diagnosed.
  2110. ConsumeToken();
  2111. } else if (Tok.is(tok::annot_header_unit)) {
  2112. // This is a header import that the preprocessor mapped to a module import.
  2113. HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());
  2114. ConsumeAnnotationToken();
  2115. } else if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon)) {
  2116. SourceLocation ColonLoc = ConsumeToken();
  2117. if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
  2118. return nullptr;
  2119. // FIXME: Support module partition import.
  2120. Diag(ColonLoc, diag::err_unsupported_module_partition)
  2121. << SourceRange(ColonLoc, Path.back().second);
  2122. return nullptr;
  2123. } else {
  2124. if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
  2125. return nullptr;
  2126. }
  2127. ParsedAttributesWithRange Attrs(AttrFactory);
  2128. MaybeParseCXX11Attributes(Attrs);
  2129. // We don't support any module import attributes yet.
  2130. ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr);
  2131. if (PP.hadModuleLoaderFatalFailure()) {
  2132. // With a fatal failure in the module loader, we abort parsing.
  2133. cutOffParsing();
  2134. return nullptr;
  2135. }
  2136. DeclResult Import;
  2137. if (HeaderUnit)
  2138. Import =
  2139. Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
  2140. else if (!Path.empty())
  2141. Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path);
  2142. ExpectAndConsumeSemi(diag::err_module_expected_semi);
  2143. if (Import.isInvalid())
  2144. return nullptr;
  2145. // Using '@import' in framework headers requires modules to be enabled so that
  2146. // the header is parseable. Emit a warning to make the user aware.
  2147. if (IsObjCAtImport && AtLoc.isValid()) {
  2148. auto &SrcMgr = PP.getSourceManager();
  2149. auto *FE = SrcMgr.getFileEntryForID(SrcMgr.getFileID(AtLoc));
  2150. if (FE && llvm::sys::path::parent_path(FE->getDir()->getName())
  2151. .endswith(".framework"))
  2152. Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);
  2153. }
  2154. return Import.get();
  2155. }
  2156. /// Parse a C++ Modules TS / Objective-C module name (both forms use the same
  2157. /// grammar).
  2158. ///
  2159. /// module-name:
  2160. /// module-name-qualifier[opt] identifier
  2161. /// module-name-qualifier:
  2162. /// module-name-qualifier[opt] identifier '.'
  2163. bool Parser::ParseModuleName(
  2164. SourceLocation UseLoc,
  2165. SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
  2166. bool IsImport) {
  2167. // Parse the module path.
  2168. while (true) {
  2169. if (!Tok.is(tok::identifier)) {
  2170. if (Tok.is(tok::code_completion)) {
  2171. cutOffParsing();
  2172. Actions.CodeCompleteModuleImport(UseLoc, Path);
  2173. return true;
  2174. }
  2175. Diag(Tok, diag::err_module_expected_ident) << IsImport;
  2176. SkipUntil(tok::semi);
  2177. return true;
  2178. }
  2179. // Record this part of the module path.
  2180. Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
  2181. ConsumeToken();
  2182. if (Tok.isNot(tok::period))
  2183. return false;
  2184. ConsumeToken();
  2185. }
  2186. }
  2187. /// Try recover parser when module annotation appears where it must not
  2188. /// be found.
  2189. /// \returns false if the recover was successful and parsing may be continued, or
  2190. /// true if parser must bail out to top level and handle the token there.
  2191. bool Parser::parseMisplacedModuleImport() {
  2192. while (true) {
  2193. switch (Tok.getKind()) {
  2194. case tok::annot_module_end:
  2195. // If we recovered from a misplaced module begin, we expect to hit a
  2196. // misplaced module end too. Stay in the current context when this
  2197. // happens.
  2198. if (MisplacedModuleBeginCount) {
  2199. --MisplacedModuleBeginCount;
  2200. Actions.ActOnModuleEnd(Tok.getLocation(),
  2201. reinterpret_cast<Module *>(
  2202. Tok.getAnnotationValue()));
  2203. ConsumeAnnotationToken();
  2204. continue;
  2205. }
  2206. // Inform caller that recovery failed, the error must be handled at upper
  2207. // level. This will generate the desired "missing '}' at end of module"
  2208. // diagnostics on the way out.
  2209. return true;
  2210. case tok::annot_module_begin:
  2211. // Recover by entering the module (Sema will diagnose).
  2212. Actions.ActOnModuleBegin(Tok.getLocation(),
  2213. reinterpret_cast<Module *>(
  2214. Tok.getAnnotationValue()));
  2215. ConsumeAnnotationToken();
  2216. ++MisplacedModuleBeginCount;
  2217. continue;
  2218. case tok::annot_module_include:
  2219. // Module import found where it should not be, for instance, inside a
  2220. // namespace. Recover by importing the module.
  2221. Actions.ActOnModuleInclude(Tok.getLocation(),
  2222. reinterpret_cast<Module *>(
  2223. Tok.getAnnotationValue()));
  2224. ConsumeAnnotationToken();
  2225. // If there is another module import, process it.
  2226. continue;
  2227. default:
  2228. return false;
  2229. }
  2230. }
  2231. return false;
  2232. }
  2233. bool BalancedDelimiterTracker::diagnoseOverflow() {
  2234. P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
  2235. << P.getLangOpts().BracketDepth;
  2236. P.Diag(P.Tok, diag::note_bracket_depth);
  2237. P.cutOffParsing();
  2238. return true;
  2239. }
  2240. bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
  2241. const char *Msg,
  2242. tok::TokenKind SkipToTok) {
  2243. LOpen = P.Tok.getLocation();
  2244. if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
  2245. if (SkipToTok != tok::unknown)
  2246. P.SkipUntil(SkipToTok, Parser::StopAtSemi);
  2247. return true;
  2248. }
  2249. if (getDepth() < P.getLangOpts().BracketDepth)
  2250. return false;
  2251. return diagnoseOverflow();
  2252. }
  2253. bool BalancedDelimiterTracker::diagnoseMissingClose() {
  2254. assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
  2255. if (P.Tok.is(tok::annot_module_end))
  2256. P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
  2257. else
  2258. P.Diag(P.Tok, diag::err_expected) << Close;
  2259. P.Diag(LOpen, diag::note_matching) << Kind;
  2260. // If we're not already at some kind of closing bracket, skip to our closing
  2261. // token.
  2262. if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
  2263. P.Tok.isNot(tok::r_square) &&
  2264. P.SkipUntil(Close, FinalToken,
  2265. Parser::StopAtSemi | Parser::StopBeforeMatch) &&
  2266. P.Tok.is(Close))
  2267. LClose = P.ConsumeAnyToken();
  2268. return true;
  2269. }
  2270. void BalancedDelimiterTracker::skipToEnd() {
  2271. P.SkipUntil(Close, Parser::StopBeforeMatch);
  2272. consumeClose();
  2273. }