ParseTentative.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
  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 tentative parsing portions of the Parser
  10. // interfaces, for ambiguity resolution.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Parse/Parser.h"
  14. #include "clang/Parse/ParseDiagnostic.h"
  15. #include "clang/Sema/ParsedTemplate.h"
  16. using namespace clang;
  17. /// isCXXDeclarationStatement - C++-specialized function that disambiguates
  18. /// between a declaration or an expression statement, when parsing function
  19. /// bodies. Returns true for declaration, false for expression.
  20. ///
  21. /// declaration-statement:
  22. /// block-declaration
  23. ///
  24. /// block-declaration:
  25. /// simple-declaration
  26. /// asm-definition
  27. /// namespace-alias-definition
  28. /// using-declaration
  29. /// using-directive
  30. /// [C++0x] static_assert-declaration
  31. ///
  32. /// asm-definition:
  33. /// 'asm' '(' string-literal ')' ';'
  34. ///
  35. /// namespace-alias-definition:
  36. /// 'namespace' identifier = qualified-namespace-specifier ';'
  37. ///
  38. /// using-declaration:
  39. /// 'using' typename[opt] '::'[opt] nested-name-specifier
  40. /// unqualified-id ';'
  41. /// 'using' '::' unqualified-id ;
  42. ///
  43. /// using-directive:
  44. /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
  45. /// namespace-name ';'
  46. ///
  47. bool Parser::isCXXDeclarationStatement() {
  48. switch (Tok.getKind()) {
  49. // asm-definition
  50. case tok::kw_asm:
  51. // namespace-alias-definition
  52. case tok::kw_namespace:
  53. // using-declaration
  54. // using-directive
  55. case tok::kw_using:
  56. // static_assert-declaration
  57. case tok::kw_static_assert:
  58. case tok::kw__Static_assert:
  59. return true;
  60. // simple-declaration
  61. default:
  62. return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
  63. }
  64. }
  65. /// isCXXSimpleDeclaration - C++-specialized function that disambiguates
  66. /// between a simple-declaration or an expression-statement.
  67. /// If during the disambiguation process a parsing error is encountered,
  68. /// the function returns true to let the declaration parsing code handle it.
  69. /// Returns false if the statement is disambiguated as expression.
  70. ///
  71. /// simple-declaration:
  72. /// decl-specifier-seq init-declarator-list[opt] ';'
  73. /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
  74. /// brace-or-equal-initializer ';' [C++17]
  75. ///
  76. /// (if AllowForRangeDecl specified)
  77. /// for ( for-range-declaration : for-range-initializer ) statement
  78. ///
  79. /// for-range-declaration:
  80. /// decl-specifier-seq declarator
  81. /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
  82. ///
  83. /// In any of the above cases there can be a preceding attribute-specifier-seq,
  84. /// but the caller is expected to handle that.
  85. bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
  86. // C++ 6.8p1:
  87. // There is an ambiguity in the grammar involving expression-statements and
  88. // declarations: An expression-statement with a function-style explicit type
  89. // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
  90. // from a declaration where the first declarator starts with a '('. In those
  91. // cases the statement is a declaration. [Note: To disambiguate, the whole
  92. // statement might have to be examined to determine if it is an
  93. // expression-statement or a declaration].
  94. // C++ 6.8p3:
  95. // The disambiguation is purely syntactic; that is, the meaning of the names
  96. // occurring in such a statement, beyond whether they are type-names or not,
  97. // is not generally used in or changed by the disambiguation. Class
  98. // templates are instantiated as necessary to determine if a qualified name
  99. // is a type-name. Disambiguation precedes parsing, and a statement
  100. // disambiguated as a declaration may be an ill-formed declaration.
  101. // We don't have to parse all of the decl-specifier-seq part. There's only
  102. // an ambiguity if the first decl-specifier is
  103. // simple-type-specifier/typename-specifier followed by a '(', which may
  104. // indicate a function-style cast expression.
  105. // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
  106. // a case.
  107. bool InvalidAsDeclaration = false;
  108. TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
  109. &InvalidAsDeclaration);
  110. if (TPR != TPResult::Ambiguous)
  111. return TPR != TPResult::False; // Returns true for TPResult::True or
  112. // TPResult::Error.
  113. // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
  114. // and so gets some cases wrong. We can't carry on if we've already seen
  115. // something which makes this statement invalid as a declaration in this case,
  116. // since it can cause us to misparse valid code. Revisit this once
  117. // TryParseInitDeclaratorList is fixed.
  118. if (InvalidAsDeclaration)
  119. return false;
  120. // FIXME: Add statistics about the number of ambiguous statements encountered
  121. // and how they were resolved (number of declarations+number of expressions).
  122. // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
  123. // or an identifier which doesn't resolve as anything. We need tentative
  124. // parsing...
  125. {
  126. RevertingTentativeParsingAction PA(*this);
  127. TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
  128. }
  129. // In case of an error, let the declaration parsing code handle it.
  130. if (TPR == TPResult::Error)
  131. return true;
  132. // Declarations take precedence over expressions.
  133. if (TPR == TPResult::Ambiguous)
  134. TPR = TPResult::True;
  135. assert(TPR == TPResult::True || TPR == TPResult::False);
  136. return TPR == TPResult::True;
  137. }
  138. /// Try to consume a token sequence that we've already identified as
  139. /// (potentially) starting a decl-specifier.
  140. Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
  141. switch (Tok.getKind()) {
  142. case tok::kw__Atomic:
  143. if (NextToken().isNot(tok::l_paren)) {
  144. ConsumeToken();
  145. break;
  146. }
  147. LLVM_FALLTHROUGH;
  148. case tok::kw_typeof:
  149. case tok::kw___attribute:
  150. case tok::kw___underlying_type: {
  151. ConsumeToken();
  152. if (Tok.isNot(tok::l_paren))
  153. return TPResult::Error;
  154. ConsumeParen();
  155. if (!SkipUntil(tok::r_paren))
  156. return TPResult::Error;
  157. break;
  158. }
  159. case tok::kw_class:
  160. case tok::kw_struct:
  161. case tok::kw_union:
  162. case tok::kw___interface:
  163. case tok::kw_enum:
  164. // elaborated-type-specifier:
  165. // class-key attribute-specifier-seq[opt]
  166. // nested-name-specifier[opt] identifier
  167. // class-key nested-name-specifier[opt] template[opt] simple-template-id
  168. // enum nested-name-specifier[opt] identifier
  169. //
  170. // FIXME: We don't support class-specifiers nor enum-specifiers here.
  171. ConsumeToken();
  172. // Skip attributes.
  173. while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
  174. tok::kw_alignas)) {
  175. if (Tok.is(tok::l_square)) {
  176. ConsumeBracket();
  177. if (!SkipUntil(tok::r_square))
  178. return TPResult::Error;
  179. } else {
  180. ConsumeToken();
  181. if (Tok.isNot(tok::l_paren))
  182. return TPResult::Error;
  183. ConsumeParen();
  184. if (!SkipUntil(tok::r_paren))
  185. return TPResult::Error;
  186. }
  187. }
  188. if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
  189. tok::annot_template_id) &&
  190. TryAnnotateCXXScopeToken())
  191. return TPResult::Error;
  192. if (Tok.is(tok::annot_cxxscope))
  193. ConsumeAnnotationToken();
  194. if (Tok.is(tok::identifier))
  195. ConsumeToken();
  196. else if (Tok.is(tok::annot_template_id))
  197. ConsumeAnnotationToken();
  198. else
  199. return TPResult::Error;
  200. break;
  201. case tok::annot_cxxscope:
  202. ConsumeAnnotationToken();
  203. LLVM_FALLTHROUGH;
  204. default:
  205. ConsumeAnyToken();
  206. if (getLangOpts().ObjC && Tok.is(tok::less))
  207. return TryParseProtocolQualifiers();
  208. break;
  209. }
  210. return TPResult::Ambiguous;
  211. }
  212. /// simple-declaration:
  213. /// decl-specifier-seq init-declarator-list[opt] ';'
  214. ///
  215. /// (if AllowForRangeDecl specified)
  216. /// for ( for-range-declaration : for-range-initializer ) statement
  217. /// for-range-declaration:
  218. /// attribute-specifier-seqopt type-specifier-seq declarator
  219. ///
  220. Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
  221. if (TryConsumeDeclarationSpecifier() == TPResult::Error)
  222. return TPResult::Error;
  223. // Two decl-specifiers in a row conclusively disambiguate this as being a
  224. // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
  225. // overwhelmingly common case that the next token is a '('.
  226. if (Tok.isNot(tok::l_paren)) {
  227. TPResult TPR = isCXXDeclarationSpecifier();
  228. if (TPR == TPResult::Ambiguous)
  229. return TPResult::True;
  230. if (TPR == TPResult::True || TPR == TPResult::Error)
  231. return TPR;
  232. assert(TPR == TPResult::False);
  233. }
  234. TPResult TPR = TryParseInitDeclaratorList();
  235. if (TPR != TPResult::Ambiguous)
  236. return TPR;
  237. if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
  238. return TPResult::False;
  239. return TPResult::Ambiguous;
  240. }
  241. /// Tentatively parse an init-declarator-list in order to disambiguate it from
  242. /// an expression.
  243. ///
  244. /// init-declarator-list:
  245. /// init-declarator
  246. /// init-declarator-list ',' init-declarator
  247. ///
  248. /// init-declarator:
  249. /// declarator initializer[opt]
  250. /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
  251. ///
  252. /// initializer:
  253. /// brace-or-equal-initializer
  254. /// '(' expression-list ')'
  255. ///
  256. /// brace-or-equal-initializer:
  257. /// '=' initializer-clause
  258. /// [C++11] braced-init-list
  259. ///
  260. /// initializer-clause:
  261. /// assignment-expression
  262. /// braced-init-list
  263. ///
  264. /// braced-init-list:
  265. /// '{' initializer-list ','[opt] '}'
  266. /// '{' '}'
  267. ///
  268. Parser::TPResult Parser::TryParseInitDeclaratorList() {
  269. while (1) {
  270. // declarator
  271. TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
  272. if (TPR != TPResult::Ambiguous)
  273. return TPR;
  274. // [GNU] simple-asm-expr[opt] attributes[opt]
  275. if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
  276. return TPResult::True;
  277. // initializer[opt]
  278. if (Tok.is(tok::l_paren)) {
  279. // Parse through the parens.
  280. ConsumeParen();
  281. if (!SkipUntil(tok::r_paren, StopAtSemi))
  282. return TPResult::Error;
  283. } else if (Tok.is(tok::l_brace)) {
  284. // A left-brace here is sufficient to disambiguate the parse; an
  285. // expression can never be followed directly by a braced-init-list.
  286. return TPResult::True;
  287. } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
  288. // MSVC and g++ won't examine the rest of declarators if '=' is
  289. // encountered; they just conclude that we have a declaration.
  290. // EDG parses the initializer completely, which is the proper behavior
  291. // for this case.
  292. //
  293. // At present, Clang follows MSVC and g++, since the parser does not have
  294. // the ability to parse an expression fully without recording the
  295. // results of that parse.
  296. // FIXME: Handle this case correctly.
  297. //
  298. // Also allow 'in' after an Objective-C declaration as in:
  299. // for (int (^b)(void) in array). Ideally this should be done in the
  300. // context of parsing for-init-statement of a foreach statement only. But,
  301. // in any other context 'in' is invalid after a declaration and parser
  302. // issues the error regardless of outcome of this decision.
  303. // FIXME: Change if above assumption does not hold.
  304. return TPResult::True;
  305. }
  306. if (!TryConsumeToken(tok::comma))
  307. break;
  308. }
  309. return TPResult::Ambiguous;
  310. }
  311. struct Parser::ConditionDeclarationOrInitStatementState {
  312. Parser &P;
  313. bool CanBeExpression = true;
  314. bool CanBeCondition = true;
  315. bool CanBeInitStatement;
  316. bool CanBeForRangeDecl;
  317. ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
  318. bool CanBeForRangeDecl)
  319. : P(P), CanBeInitStatement(CanBeInitStatement),
  320. CanBeForRangeDecl(CanBeForRangeDecl) {}
  321. bool resolved() {
  322. return CanBeExpression + CanBeCondition + CanBeInitStatement +
  323. CanBeForRangeDecl < 2;
  324. }
  325. void markNotExpression() {
  326. CanBeExpression = false;
  327. if (!resolved()) {
  328. // FIXME: Unify the parsing codepaths for condition variables and
  329. // simple-declarations so that we don't need to eagerly figure out which
  330. // kind we have here. (Just parse init-declarators until we reach a
  331. // semicolon or right paren.)
  332. RevertingTentativeParsingAction PA(P);
  333. if (CanBeForRangeDecl) {
  334. // Skip until we hit a ')', ';', or a ':' with no matching '?'.
  335. // The final case is a for range declaration, the rest are not.
  336. while (true) {
  337. unsigned QuestionColonDepth = 0;
  338. P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
  339. StopBeforeMatch);
  340. if (P.Tok.is(tok::question))
  341. ++QuestionColonDepth;
  342. else if (P.Tok.is(tok::colon)) {
  343. if (QuestionColonDepth)
  344. --QuestionColonDepth;
  345. else {
  346. CanBeCondition = CanBeInitStatement = false;
  347. return;
  348. }
  349. } else {
  350. CanBeForRangeDecl = false;
  351. break;
  352. }
  353. P.ConsumeToken();
  354. }
  355. } else {
  356. // Just skip until we hit a ')' or ';'.
  357. P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
  358. }
  359. if (P.Tok.isNot(tok::r_paren))
  360. CanBeCondition = CanBeForRangeDecl = false;
  361. if (P.Tok.isNot(tok::semi))
  362. CanBeInitStatement = false;
  363. }
  364. }
  365. bool markNotCondition() {
  366. CanBeCondition = false;
  367. return resolved();
  368. }
  369. bool markNotForRangeDecl() {
  370. CanBeForRangeDecl = false;
  371. return resolved();
  372. }
  373. bool update(TPResult IsDecl) {
  374. switch (IsDecl) {
  375. case TPResult::True:
  376. markNotExpression();
  377. assert(resolved() && "can't continue after tentative parsing bails out");
  378. break;
  379. case TPResult::False:
  380. CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
  381. break;
  382. case TPResult::Ambiguous:
  383. break;
  384. case TPResult::Error:
  385. CanBeExpression = CanBeCondition = CanBeInitStatement =
  386. CanBeForRangeDecl = false;
  387. break;
  388. }
  389. return resolved();
  390. }
  391. ConditionOrInitStatement result() const {
  392. assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
  393. CanBeForRangeDecl < 2 &&
  394. "result called but not yet resolved");
  395. if (CanBeExpression)
  396. return ConditionOrInitStatement::Expression;
  397. if (CanBeCondition)
  398. return ConditionOrInitStatement::ConditionDecl;
  399. if (CanBeInitStatement)
  400. return ConditionOrInitStatement::InitStmtDecl;
  401. if (CanBeForRangeDecl)
  402. return ConditionOrInitStatement::ForRangeDecl;
  403. return ConditionOrInitStatement::Error;
  404. }
  405. };
  406. /// Disambiguates between a declaration in a condition, a
  407. /// simple-declaration in an init-statement, and an expression for
  408. /// a condition of a if/switch statement.
  409. ///
  410. /// condition:
  411. /// expression
  412. /// type-specifier-seq declarator '=' assignment-expression
  413. /// [C++11] type-specifier-seq declarator '=' initializer-clause
  414. /// [C++11] type-specifier-seq declarator braced-init-list
  415. /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
  416. /// '=' assignment-expression
  417. /// simple-declaration:
  418. /// decl-specifier-seq init-declarator-list[opt] ';'
  419. ///
  420. /// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
  421. /// to the ';' to disambiguate cases like 'int(x))' (an expression) from
  422. /// 'int(x);' (a simple-declaration in an init-statement).
  423. Parser::ConditionOrInitStatement
  424. Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
  425. bool CanBeForRangeDecl) {
  426. ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
  427. CanBeForRangeDecl);
  428. if (State.update(isCXXDeclarationSpecifier()))
  429. return State.result();
  430. // It might be a declaration; we need tentative parsing.
  431. RevertingTentativeParsingAction PA(*this);
  432. // FIXME: A tag definition unambiguously tells us this is an init-statement.
  433. if (State.update(TryConsumeDeclarationSpecifier()))
  434. return State.result();
  435. assert(Tok.is(tok::l_paren) && "Expected '('");
  436. while (true) {
  437. // Consume a declarator.
  438. if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
  439. return State.result();
  440. // Attributes, asm label, or an initializer imply this is not an expression.
  441. // FIXME: Disambiguate properly after an = instead of assuming that it's a
  442. // valid declaration.
  443. if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
  444. (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
  445. State.markNotExpression();
  446. return State.result();
  447. }
  448. // A colon here identifies a for-range declaration.
  449. if (State.CanBeForRangeDecl && Tok.is(tok::colon))
  450. return ConditionOrInitStatement::ForRangeDecl;
  451. // At this point, it can't be a condition any more, because a condition
  452. // must have a brace-or-equal-initializer.
  453. if (State.markNotCondition())
  454. return State.result();
  455. // Likewise, it can't be a for-range declaration any more.
  456. if (State.markNotForRangeDecl())
  457. return State.result();
  458. // A parenthesized initializer could be part of an expression or a
  459. // simple-declaration.
  460. if (Tok.is(tok::l_paren)) {
  461. ConsumeParen();
  462. SkipUntil(tok::r_paren, StopAtSemi);
  463. }
  464. if (!TryConsumeToken(tok::comma))
  465. break;
  466. }
  467. // We reached the end. If it can now be some kind of decl, then it is.
  468. if (State.CanBeCondition && Tok.is(tok::r_paren))
  469. return ConditionOrInitStatement::ConditionDecl;
  470. else if (State.CanBeInitStatement && Tok.is(tok::semi))
  471. return ConditionOrInitStatement::InitStmtDecl;
  472. else
  473. return ConditionOrInitStatement::Expression;
  474. }
  475. /// Determine whether the next set of tokens contains a type-id.
  476. ///
  477. /// The context parameter states what context we're parsing right
  478. /// now, which affects how this routine copes with the token
  479. /// following the type-id. If the context is TypeIdInParens, we have
  480. /// already parsed the '(' and we will cease lookahead when we hit
  481. /// the corresponding ')'. If the context is
  482. /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
  483. /// before this template argument, and will cease lookahead when we
  484. /// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
  485. /// preceding such. Returns true for a type-id and false for an expression.
  486. /// If during the disambiguation process a parsing error is encountered,
  487. /// the function returns true to let the declaration parsing code handle it.
  488. ///
  489. /// type-id:
  490. /// type-specifier-seq abstract-declarator[opt]
  491. ///
  492. bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
  493. isAmbiguous = false;
  494. // C++ 8.2p2:
  495. // The ambiguity arising from the similarity between a function-style cast and
  496. // a type-id can occur in different contexts. The ambiguity appears as a
  497. // choice between a function-style cast expression and a declaration of a
  498. // type. The resolution is that any construct that could possibly be a type-id
  499. // in its syntactic context shall be considered a type-id.
  500. TPResult TPR = isCXXDeclarationSpecifier();
  501. if (TPR != TPResult::Ambiguous)
  502. return TPR != TPResult::False; // Returns true for TPResult::True or
  503. // TPResult::Error.
  504. // FIXME: Add statistics about the number of ambiguous statements encountered
  505. // and how they were resolved (number of declarations+number of expressions).
  506. // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
  507. // We need tentative parsing...
  508. RevertingTentativeParsingAction PA(*this);
  509. // type-specifier-seq
  510. TryConsumeDeclarationSpecifier();
  511. assert(Tok.is(tok::l_paren) && "Expected '('");
  512. // declarator
  513. TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
  514. // In case of an error, let the declaration parsing code handle it.
  515. if (TPR == TPResult::Error)
  516. TPR = TPResult::True;
  517. if (TPR == TPResult::Ambiguous) {
  518. // We are supposed to be inside parens, so if after the abstract declarator
  519. // we encounter a ')' this is a type-id, otherwise it's an expression.
  520. if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
  521. TPR = TPResult::True;
  522. isAmbiguous = true;
  523. // We are supposed to be inside a template argument, so if after
  524. // the abstract declarator we encounter a '>', '>>' (in C++0x), or
  525. // ','; or, in C++0x, an ellipsis immediately preceding such, this
  526. // is a type-id. Otherwise, it's an expression.
  527. } else if (Context == TypeIdAsTemplateArgument &&
  528. (Tok.isOneOf(tok::greater, tok::comma) ||
  529. (getLangOpts().CPlusPlus11 &&
  530. (Tok.isOneOf(tok::greatergreater,
  531. tok::greatergreatergreater) ||
  532. (Tok.is(tok::ellipsis) &&
  533. NextToken().isOneOf(tok::greater, tok::greatergreater,
  534. tok::greatergreatergreater,
  535. tok::comma)))))) {
  536. TPR = TPResult::True;
  537. isAmbiguous = true;
  538. } else
  539. TPR = TPResult::False;
  540. }
  541. assert(TPR == TPResult::True || TPR == TPResult::False);
  542. return TPR == TPResult::True;
  543. }
  544. /// Returns true if this is a C++11 attribute-specifier. Per
  545. /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
  546. /// always introduce an attribute. In Objective-C++11, this rule does not
  547. /// apply if either '[' begins a message-send.
  548. ///
  549. /// If Disambiguate is true, we try harder to determine whether a '[[' starts
  550. /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
  551. ///
  552. /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
  553. /// Obj-C message send or the start of an attribute. Otherwise, we assume it
  554. /// is not an Obj-C message send.
  555. ///
  556. /// C++11 [dcl.attr.grammar]:
  557. ///
  558. /// attribute-specifier:
  559. /// '[' '[' attribute-list ']' ']'
  560. /// alignment-specifier
  561. ///
  562. /// attribute-list:
  563. /// attribute[opt]
  564. /// attribute-list ',' attribute[opt]
  565. /// attribute '...'
  566. /// attribute-list ',' attribute '...'
  567. ///
  568. /// attribute:
  569. /// attribute-token attribute-argument-clause[opt]
  570. ///
  571. /// attribute-token:
  572. /// identifier
  573. /// identifier '::' identifier
  574. ///
  575. /// attribute-argument-clause:
  576. /// '(' balanced-token-seq ')'
  577. Parser::CXX11AttributeKind
  578. Parser::isCXX11AttributeSpecifier(bool Disambiguate,
  579. bool OuterMightBeMessageSend) {
  580. if (Tok.is(tok::kw_alignas))
  581. return CAK_AttributeSpecifier;
  582. if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
  583. return CAK_NotAttributeSpecifier;
  584. // No tentative parsing if we don't need to look for ']]' or a lambda.
  585. if (!Disambiguate && !getLangOpts().ObjC)
  586. return CAK_AttributeSpecifier;
  587. // '[[using ns: ...]]' is an attribute.
  588. if (GetLookAheadToken(2).is(tok::kw_using))
  589. return CAK_AttributeSpecifier;
  590. RevertingTentativeParsingAction PA(*this);
  591. // Opening brackets were checked for above.
  592. ConsumeBracket();
  593. if (!getLangOpts().ObjC) {
  594. ConsumeBracket();
  595. bool IsAttribute = SkipUntil(tok::r_square);
  596. IsAttribute &= Tok.is(tok::r_square);
  597. return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
  598. }
  599. // In Obj-C++11, we need to distinguish four situations:
  600. // 1a) int x[[attr]]; C++11 attribute.
  601. // 1b) [[attr]]; C++11 statement attribute.
  602. // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
  603. // 3a) int x[[obj get]]; Message send in array size/index.
  604. // 3b) [[Class alloc] init]; Message send in message send.
  605. // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
  606. // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
  607. // Check to see if this is a lambda-expression.
  608. // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
  609. // into the tentative attribute parse below.
  610. {
  611. RevertingTentativeParsingAction LambdaTPA(*this);
  612. LambdaIntroducer Intro;
  613. LambdaIntroducerTentativeParse Tentative;
  614. if (ParseLambdaIntroducer(Intro, &Tentative)) {
  615. // We hit a hard error after deciding this was not an attribute.
  616. // FIXME: Don't parse and annotate expressions when disambiguating
  617. // against an attribute.
  618. return CAK_NotAttributeSpecifier;
  619. }
  620. switch (Tentative) {
  621. case LambdaIntroducerTentativeParse::MessageSend:
  622. // Case 3: The inner construct is definitely a message send, so the
  623. // outer construct is definitely not an attribute.
  624. return CAK_NotAttributeSpecifier;
  625. case LambdaIntroducerTentativeParse::Success:
  626. case LambdaIntroducerTentativeParse::Incomplete:
  627. // This is a lambda-introducer or attribute-specifier.
  628. if (Tok.is(tok::r_square))
  629. // Case 1: C++11 attribute.
  630. return CAK_AttributeSpecifier;
  631. if (OuterMightBeMessageSend)
  632. // Case 4: Lambda in message send.
  633. return CAK_NotAttributeSpecifier;
  634. // Case 2: Lambda in array size / index.
  635. return CAK_InvalidAttributeSpecifier;
  636. case LambdaIntroducerTentativeParse::Invalid:
  637. // No idea what this is; we couldn't parse it as a lambda-introducer.
  638. // Might still be an attribute-specifier or a message send.
  639. break;
  640. }
  641. }
  642. ConsumeBracket();
  643. // If we don't have a lambda-introducer, then we have an attribute or a
  644. // message-send.
  645. bool IsAttribute = true;
  646. while (Tok.isNot(tok::r_square)) {
  647. if (Tok.is(tok::comma)) {
  648. // Case 1: Stray commas can only occur in attributes.
  649. return CAK_AttributeSpecifier;
  650. }
  651. // Parse the attribute-token, if present.
  652. // C++11 [dcl.attr.grammar]:
  653. // If a keyword or an alternative token that satisfies the syntactic
  654. // requirements of an identifier is contained in an attribute-token,
  655. // it is considered an identifier.
  656. SourceLocation Loc;
  657. if (!TryParseCXX11AttributeIdentifier(Loc)) {
  658. IsAttribute = false;
  659. break;
  660. }
  661. if (Tok.is(tok::coloncolon)) {
  662. ConsumeToken();
  663. if (!TryParseCXX11AttributeIdentifier(Loc)) {
  664. IsAttribute = false;
  665. break;
  666. }
  667. }
  668. // Parse the attribute-argument-clause, if present.
  669. if (Tok.is(tok::l_paren)) {
  670. ConsumeParen();
  671. if (!SkipUntil(tok::r_paren)) {
  672. IsAttribute = false;
  673. break;
  674. }
  675. }
  676. TryConsumeToken(tok::ellipsis);
  677. if (!TryConsumeToken(tok::comma))
  678. break;
  679. }
  680. // An attribute must end ']]'.
  681. if (IsAttribute) {
  682. if (Tok.is(tok::r_square)) {
  683. ConsumeBracket();
  684. IsAttribute = Tok.is(tok::r_square);
  685. } else {
  686. IsAttribute = false;
  687. }
  688. }
  689. if (IsAttribute)
  690. // Case 1: C++11 statement attribute.
  691. return CAK_AttributeSpecifier;
  692. // Case 3: Message send.
  693. return CAK_NotAttributeSpecifier;
  694. }
  695. Parser::TPResult Parser::TryParsePtrOperatorSeq() {
  696. while (true) {
  697. if (Tok.isOneOf(tok::coloncolon, tok::identifier))
  698. if (TryAnnotateCXXScopeToken(true))
  699. return TPResult::Error;
  700. if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
  701. (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
  702. // ptr-operator
  703. ConsumeAnyToken();
  704. while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
  705. tok::kw__Nonnull, tok::kw__Nullable,
  706. tok::kw__Null_unspecified))
  707. ConsumeToken();
  708. } else {
  709. return TPResult::True;
  710. }
  711. }
  712. }
  713. /// operator-function-id:
  714. /// 'operator' operator
  715. ///
  716. /// operator: one of
  717. /// new delete new[] delete[] + - * / % ^ [...]
  718. ///
  719. /// conversion-function-id:
  720. /// 'operator' conversion-type-id
  721. ///
  722. /// conversion-type-id:
  723. /// type-specifier-seq conversion-declarator[opt]
  724. ///
  725. /// conversion-declarator:
  726. /// ptr-operator conversion-declarator[opt]
  727. ///
  728. /// literal-operator-id:
  729. /// 'operator' string-literal identifier
  730. /// 'operator' user-defined-string-literal
  731. Parser::TPResult Parser::TryParseOperatorId() {
  732. assert(Tok.is(tok::kw_operator));
  733. ConsumeToken();
  734. // Maybe this is an operator-function-id.
  735. switch (Tok.getKind()) {
  736. case tok::kw_new: case tok::kw_delete:
  737. ConsumeToken();
  738. if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
  739. ConsumeBracket();
  740. ConsumeBracket();
  741. }
  742. return TPResult::True;
  743. #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
  744. case tok::Token:
  745. #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
  746. #include "clang/Basic/OperatorKinds.def"
  747. ConsumeToken();
  748. return TPResult::True;
  749. case tok::l_square:
  750. if (NextToken().is(tok::r_square)) {
  751. ConsumeBracket();
  752. ConsumeBracket();
  753. return TPResult::True;
  754. }
  755. break;
  756. case tok::l_paren:
  757. if (NextToken().is(tok::r_paren)) {
  758. ConsumeParen();
  759. ConsumeParen();
  760. return TPResult::True;
  761. }
  762. break;
  763. default:
  764. break;
  765. }
  766. // Maybe this is a literal-operator-id.
  767. if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
  768. bool FoundUDSuffix = false;
  769. do {
  770. FoundUDSuffix |= Tok.hasUDSuffix();
  771. ConsumeStringToken();
  772. } while (isTokenStringLiteral());
  773. if (!FoundUDSuffix) {
  774. if (Tok.is(tok::identifier))
  775. ConsumeToken();
  776. else
  777. return TPResult::Error;
  778. }
  779. return TPResult::True;
  780. }
  781. // Maybe this is a conversion-function-id.
  782. bool AnyDeclSpecifiers = false;
  783. while (true) {
  784. TPResult TPR = isCXXDeclarationSpecifier();
  785. if (TPR == TPResult::Error)
  786. return TPR;
  787. if (TPR == TPResult::False) {
  788. if (!AnyDeclSpecifiers)
  789. return TPResult::Error;
  790. break;
  791. }
  792. if (TryConsumeDeclarationSpecifier() == TPResult::Error)
  793. return TPResult::Error;
  794. AnyDeclSpecifiers = true;
  795. }
  796. return TryParsePtrOperatorSeq();
  797. }
  798. /// declarator:
  799. /// direct-declarator
  800. /// ptr-operator declarator
  801. ///
  802. /// direct-declarator:
  803. /// declarator-id
  804. /// direct-declarator '(' parameter-declaration-clause ')'
  805. /// cv-qualifier-seq[opt] exception-specification[opt]
  806. /// direct-declarator '[' constant-expression[opt] ']'
  807. /// '(' declarator ')'
  808. /// [GNU] '(' attributes declarator ')'
  809. ///
  810. /// abstract-declarator:
  811. /// ptr-operator abstract-declarator[opt]
  812. /// direct-abstract-declarator
  813. ///
  814. /// direct-abstract-declarator:
  815. /// direct-abstract-declarator[opt]
  816. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  817. /// exception-specification[opt]
  818. /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
  819. /// '(' abstract-declarator ')'
  820. /// [C++0x] ...
  821. ///
  822. /// ptr-operator:
  823. /// '*' cv-qualifier-seq[opt]
  824. /// '&'
  825. /// [C++0x] '&&' [TODO]
  826. /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
  827. ///
  828. /// cv-qualifier-seq:
  829. /// cv-qualifier cv-qualifier-seq[opt]
  830. ///
  831. /// cv-qualifier:
  832. /// 'const'
  833. /// 'volatile'
  834. ///
  835. /// declarator-id:
  836. /// '...'[opt] id-expression
  837. ///
  838. /// id-expression:
  839. /// unqualified-id
  840. /// qualified-id [TODO]
  841. ///
  842. /// unqualified-id:
  843. /// identifier
  844. /// operator-function-id
  845. /// conversion-function-id
  846. /// literal-operator-id
  847. /// '~' class-name [TODO]
  848. /// '~' decltype-specifier [TODO]
  849. /// template-id [TODO]
  850. ///
  851. Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
  852. bool mayHaveIdentifier,
  853. bool mayHaveDirectInit) {
  854. // declarator:
  855. // direct-declarator
  856. // ptr-operator declarator
  857. if (TryParsePtrOperatorSeq() == TPResult::Error)
  858. return TPResult::Error;
  859. // direct-declarator:
  860. // direct-abstract-declarator:
  861. if (Tok.is(tok::ellipsis))
  862. ConsumeToken();
  863. if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
  864. (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
  865. NextToken().is(tok::kw_operator)))) &&
  866. mayHaveIdentifier) {
  867. // declarator-id
  868. if (Tok.is(tok::annot_cxxscope))
  869. ConsumeAnnotationToken();
  870. else if (Tok.is(tok::identifier))
  871. TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
  872. if (Tok.is(tok::kw_operator)) {
  873. if (TryParseOperatorId() == TPResult::Error)
  874. return TPResult::Error;
  875. } else
  876. ConsumeToken();
  877. } else if (Tok.is(tok::l_paren)) {
  878. ConsumeParen();
  879. if (mayBeAbstract &&
  880. (Tok.is(tok::r_paren) || // 'int()' is a function.
  881. // 'int(...)' is a function.
  882. (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
  883. isDeclarationSpecifier())) { // 'int(int)' is a function.
  884. // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  885. // exception-specification[opt]
  886. TPResult TPR = TryParseFunctionDeclarator();
  887. if (TPR != TPResult::Ambiguous)
  888. return TPR;
  889. } else {
  890. // '(' declarator ')'
  891. // '(' attributes declarator ')'
  892. // '(' abstract-declarator ')'
  893. if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
  894. tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
  895. tok::kw___regcall, tok::kw___vectorcall))
  896. return TPResult::True; // attributes indicate declaration
  897. TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
  898. if (TPR != TPResult::Ambiguous)
  899. return TPR;
  900. if (Tok.isNot(tok::r_paren))
  901. return TPResult::False;
  902. ConsumeParen();
  903. }
  904. } else if (!mayBeAbstract) {
  905. return TPResult::False;
  906. }
  907. if (mayHaveDirectInit)
  908. return TPResult::Ambiguous;
  909. while (1) {
  910. TPResult TPR(TPResult::Ambiguous);
  911. if (Tok.is(tok::l_paren)) {
  912. // Check whether we have a function declarator or a possible ctor-style
  913. // initializer that follows the declarator. Note that ctor-style
  914. // initializers are not possible in contexts where abstract declarators
  915. // are allowed.
  916. if (!mayBeAbstract && !isCXXFunctionDeclarator())
  917. break;
  918. // direct-declarator '(' parameter-declaration-clause ')'
  919. // cv-qualifier-seq[opt] exception-specification[opt]
  920. ConsumeParen();
  921. TPR = TryParseFunctionDeclarator();
  922. } else if (Tok.is(tok::l_square)) {
  923. // direct-declarator '[' constant-expression[opt] ']'
  924. // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
  925. TPR = TryParseBracketDeclarator();
  926. } else {
  927. break;
  928. }
  929. if (TPR != TPResult::Ambiguous)
  930. return TPR;
  931. }
  932. return TPResult::Ambiguous;
  933. }
  934. Parser::TPResult
  935. Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
  936. switch (Kind) {
  937. // Obviously starts an expression.
  938. case tok::numeric_constant:
  939. case tok::char_constant:
  940. case tok::wide_char_constant:
  941. case tok::utf8_char_constant:
  942. case tok::utf16_char_constant:
  943. case tok::utf32_char_constant:
  944. case tok::string_literal:
  945. case tok::wide_string_literal:
  946. case tok::utf8_string_literal:
  947. case tok::utf16_string_literal:
  948. case tok::utf32_string_literal:
  949. case tok::l_square:
  950. case tok::l_paren:
  951. case tok::amp:
  952. case tok::ampamp:
  953. case tok::star:
  954. case tok::plus:
  955. case tok::plusplus:
  956. case tok::minus:
  957. case tok::minusminus:
  958. case tok::tilde:
  959. case tok::exclaim:
  960. case tok::kw_sizeof:
  961. case tok::kw___func__:
  962. case tok::kw_const_cast:
  963. case tok::kw_delete:
  964. case tok::kw_dynamic_cast:
  965. case tok::kw_false:
  966. case tok::kw_new:
  967. case tok::kw_operator:
  968. case tok::kw_reinterpret_cast:
  969. case tok::kw_static_cast:
  970. case tok::kw_this:
  971. case tok::kw_throw:
  972. case tok::kw_true:
  973. case tok::kw_typeid:
  974. case tok::kw_alignof:
  975. case tok::kw_noexcept:
  976. case tok::kw_nullptr:
  977. case tok::kw__Alignof:
  978. case tok::kw___null:
  979. case tok::kw___alignof:
  980. case tok::kw___builtin_choose_expr:
  981. case tok::kw___builtin_offsetof:
  982. case tok::kw___builtin_va_arg:
  983. case tok::kw___imag:
  984. case tok::kw___real:
  985. case tok::kw___FUNCTION__:
  986. case tok::kw___FUNCDNAME__:
  987. case tok::kw___FUNCSIG__:
  988. case tok::kw_L__FUNCTION__:
  989. case tok::kw_L__FUNCSIG__:
  990. case tok::kw___PRETTY_FUNCTION__:
  991. case tok::kw___uuidof:
  992. #define TYPE_TRAIT(N,Spelling,K) \
  993. case tok::kw_##Spelling:
  994. #include "clang/Basic/TokenKinds.def"
  995. return TPResult::True;
  996. // Obviously starts a type-specifier-seq:
  997. case tok::kw_char:
  998. case tok::kw_const:
  999. case tok::kw_double:
  1000. case tok::kw__Float16:
  1001. case tok::kw___float128:
  1002. case tok::kw_enum:
  1003. case tok::kw_half:
  1004. case tok::kw_float:
  1005. case tok::kw_int:
  1006. case tok::kw_long:
  1007. case tok::kw___int64:
  1008. case tok::kw___int128:
  1009. case tok::kw_restrict:
  1010. case tok::kw_short:
  1011. case tok::kw_signed:
  1012. case tok::kw_struct:
  1013. case tok::kw_union:
  1014. case tok::kw_unsigned:
  1015. case tok::kw_void:
  1016. case tok::kw_volatile:
  1017. case tok::kw__Bool:
  1018. case tok::kw__Complex:
  1019. case tok::kw_class:
  1020. case tok::kw_typename:
  1021. case tok::kw_wchar_t:
  1022. case tok::kw_char8_t:
  1023. case tok::kw_char16_t:
  1024. case tok::kw_char32_t:
  1025. case tok::kw__Decimal32:
  1026. case tok::kw__Decimal64:
  1027. case tok::kw__Decimal128:
  1028. case tok::kw___interface:
  1029. case tok::kw___thread:
  1030. case tok::kw_thread_local:
  1031. case tok::kw__Thread_local:
  1032. case tok::kw_typeof:
  1033. case tok::kw___underlying_type:
  1034. case tok::kw___cdecl:
  1035. case tok::kw___stdcall:
  1036. case tok::kw___fastcall:
  1037. case tok::kw___thiscall:
  1038. case tok::kw___regcall:
  1039. case tok::kw___vectorcall:
  1040. case tok::kw___unaligned:
  1041. case tok::kw___vector:
  1042. case tok::kw___pixel:
  1043. case tok::kw___bool:
  1044. case tok::kw__Atomic:
  1045. #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
  1046. #include "clang/Basic/OpenCLImageTypes.def"
  1047. case tok::kw___unknown_anytype:
  1048. return TPResult::False;
  1049. default:
  1050. break;
  1051. }
  1052. return TPResult::Ambiguous;
  1053. }
  1054. bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
  1055. return std::find(TentativelyDeclaredIdentifiers.begin(),
  1056. TentativelyDeclaredIdentifiers.end(), II)
  1057. != TentativelyDeclaredIdentifiers.end();
  1058. }
  1059. namespace {
  1060. class TentativeParseCCC final : public CorrectionCandidateCallback {
  1061. public:
  1062. TentativeParseCCC(const Token &Next) {
  1063. WantRemainingKeywords = false;
  1064. WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
  1065. tok::l_brace, tok::identifier);
  1066. }
  1067. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  1068. // Reject any candidate that only resolves to instance members since they
  1069. // aren't viable as standalone identifiers instead of member references.
  1070. if (Candidate.isResolved() && !Candidate.isKeyword() &&
  1071. llvm::all_of(Candidate,
  1072. [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
  1073. return false;
  1074. return CorrectionCandidateCallback::ValidateCandidate(Candidate);
  1075. }
  1076. std::unique_ptr<CorrectionCandidateCallback> clone() override {
  1077. return std::make_unique<TentativeParseCCC>(*this);
  1078. }
  1079. };
  1080. }
  1081. /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
  1082. /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
  1083. /// be either a decl-specifier or a function-style cast, and TPResult::Error
  1084. /// if a parsing error was found and reported.
  1085. ///
  1086. /// If InvalidAsDeclSpec is not null, some cases that would be ill-formed as
  1087. /// declaration specifiers but possibly valid as some other kind of construct
  1088. /// return TPResult::Ambiguous instead of TPResult::False. When this happens,
  1089. /// the intent is to keep trying to disambiguate, on the basis that we might
  1090. /// find a better reason to treat this construct as a declaration later on.
  1091. /// When this happens and the name could possibly be valid in some other
  1092. /// syntactic context, *InvalidAsDeclSpec is set to 'true'. The current cases
  1093. /// that trigger this are:
  1094. ///
  1095. /// * When parsing X::Y (with no 'typename') where X is dependent
  1096. /// * When parsing X<Y> where X is undeclared
  1097. ///
  1098. /// decl-specifier:
  1099. /// storage-class-specifier
  1100. /// type-specifier
  1101. /// function-specifier
  1102. /// 'friend'
  1103. /// 'typedef'
  1104. /// [C++11] 'constexpr'
  1105. /// [C++20] 'consteval'
  1106. /// [GNU] attributes declaration-specifiers[opt]
  1107. ///
  1108. /// storage-class-specifier:
  1109. /// 'register'
  1110. /// 'static'
  1111. /// 'extern'
  1112. /// 'mutable'
  1113. /// 'auto'
  1114. /// [GNU] '__thread'
  1115. /// [C++11] 'thread_local'
  1116. /// [C11] '_Thread_local'
  1117. ///
  1118. /// function-specifier:
  1119. /// 'inline'
  1120. /// 'virtual'
  1121. /// 'explicit'
  1122. ///
  1123. /// typedef-name:
  1124. /// identifier
  1125. ///
  1126. /// type-specifier:
  1127. /// simple-type-specifier
  1128. /// class-specifier
  1129. /// enum-specifier
  1130. /// elaborated-type-specifier
  1131. /// typename-specifier
  1132. /// cv-qualifier
  1133. ///
  1134. /// simple-type-specifier:
  1135. /// '::'[opt] nested-name-specifier[opt] type-name
  1136. /// '::'[opt] nested-name-specifier 'template'
  1137. /// simple-template-id [TODO]
  1138. /// 'char'
  1139. /// 'wchar_t'
  1140. /// 'bool'
  1141. /// 'short'
  1142. /// 'int'
  1143. /// 'long'
  1144. /// 'signed'
  1145. /// 'unsigned'
  1146. /// 'float'
  1147. /// 'double'
  1148. /// 'void'
  1149. /// [GNU] typeof-specifier
  1150. /// [GNU] '_Complex'
  1151. /// [C++11] 'auto'
  1152. /// [GNU] '__auto_type'
  1153. /// [C++11] 'decltype' ( expression )
  1154. /// [C++1y] 'decltype' ( 'auto' )
  1155. ///
  1156. /// type-name:
  1157. /// class-name
  1158. /// enum-name
  1159. /// typedef-name
  1160. ///
  1161. /// elaborated-type-specifier:
  1162. /// class-key '::'[opt] nested-name-specifier[opt] identifier
  1163. /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
  1164. /// simple-template-id
  1165. /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
  1166. ///
  1167. /// enum-name:
  1168. /// identifier
  1169. ///
  1170. /// enum-specifier:
  1171. /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
  1172. /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
  1173. ///
  1174. /// class-specifier:
  1175. /// class-head '{' member-specification[opt] '}'
  1176. ///
  1177. /// class-head:
  1178. /// class-key identifier[opt] base-clause[opt]
  1179. /// class-key nested-name-specifier identifier base-clause[opt]
  1180. /// class-key nested-name-specifier[opt] simple-template-id
  1181. /// base-clause[opt]
  1182. ///
  1183. /// class-key:
  1184. /// 'class'
  1185. /// 'struct'
  1186. /// 'union'
  1187. ///
  1188. /// cv-qualifier:
  1189. /// 'const'
  1190. /// 'volatile'
  1191. /// [GNU] restrict
  1192. ///
  1193. Parser::TPResult
  1194. Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
  1195. bool *InvalidAsDeclSpec) {
  1196. switch (Tok.getKind()) {
  1197. case tok::identifier: {
  1198. // Check for need to substitute AltiVec __vector keyword
  1199. // for "vector" identifier.
  1200. if (TryAltiVecVectorToken())
  1201. return TPResult::True;
  1202. const Token &Next = NextToken();
  1203. // In 'foo bar', 'foo' is always a type name outside of Objective-C.
  1204. if (!getLangOpts().ObjC && Next.is(tok::identifier))
  1205. return TPResult::True;
  1206. if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
  1207. // Determine whether this is a valid expression. If not, we will hit
  1208. // a parse error one way or another. In that case, tell the caller that
  1209. // this is ambiguous. Typo-correct to type and expression keywords and
  1210. // to types and identifiers, in order to try to recover from errors.
  1211. TentativeParseCCC CCC(Next);
  1212. switch (TryAnnotateName(&CCC)) {
  1213. case ANK_Error:
  1214. return TPResult::Error;
  1215. case ANK_TentativeDecl:
  1216. return TPResult::False;
  1217. case ANK_TemplateName:
  1218. // In C++17, this could be a type template for class template argument
  1219. // deduction. Try to form a type annotation for it. If we're in a
  1220. // template template argument, we'll undo this when checking the
  1221. // validity of the argument.
  1222. if (getLangOpts().CPlusPlus17) {
  1223. if (TryAnnotateTypeOrScopeToken())
  1224. return TPResult::Error;
  1225. if (Tok.isNot(tok::identifier))
  1226. break;
  1227. }
  1228. // A bare type template-name which can't be a template template
  1229. // argument is an error, and was probably intended to be a type.
  1230. return GreaterThanIsOperator ? TPResult::True : TPResult::False;
  1231. case ANK_Unresolved:
  1232. return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
  1233. case ANK_Success:
  1234. break;
  1235. }
  1236. assert(Tok.isNot(tok::identifier) &&
  1237. "TryAnnotateName succeeded without producing an annotation");
  1238. } else {
  1239. // This might possibly be a type with a dependent scope specifier and
  1240. // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
  1241. // since it will annotate as a primary expression, and we want to use the
  1242. // "missing 'typename'" logic.
  1243. if (TryAnnotateTypeOrScopeToken())
  1244. return TPResult::Error;
  1245. // If annotation failed, assume it's a non-type.
  1246. // FIXME: If this happens due to an undeclared identifier, treat it as
  1247. // ambiguous.
  1248. if (Tok.is(tok::identifier))
  1249. return TPResult::False;
  1250. }
  1251. // We annotated this token as something. Recurse to handle whatever we got.
  1252. return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
  1253. }
  1254. case tok::kw_typename: // typename T::type
  1255. // Annotate typenames and C++ scope specifiers. If we get one, just
  1256. // recurse to handle whatever we get.
  1257. if (TryAnnotateTypeOrScopeToken())
  1258. return TPResult::Error;
  1259. return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
  1260. case tok::coloncolon: { // ::foo::bar
  1261. const Token &Next = NextToken();
  1262. if (Next.isOneOf(tok::kw_new, // ::new
  1263. tok::kw_delete)) // ::delete
  1264. return TPResult::False;
  1265. LLVM_FALLTHROUGH;
  1266. }
  1267. case tok::kw___super:
  1268. case tok::kw_decltype:
  1269. // Annotate typenames and C++ scope specifiers. If we get one, just
  1270. // recurse to handle whatever we get.
  1271. if (TryAnnotateTypeOrScopeToken())
  1272. return TPResult::Error;
  1273. return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
  1274. // decl-specifier:
  1275. // storage-class-specifier
  1276. // type-specifier
  1277. // function-specifier
  1278. // 'friend'
  1279. // 'typedef'
  1280. // 'constexpr'
  1281. case tok::kw_friend:
  1282. case tok::kw_typedef:
  1283. case tok::kw_constexpr:
  1284. case tok::kw_consteval:
  1285. case tok::kw_constinit:
  1286. // storage-class-specifier
  1287. case tok::kw_register:
  1288. case tok::kw_static:
  1289. case tok::kw_extern:
  1290. case tok::kw_mutable:
  1291. case tok::kw_auto:
  1292. case tok::kw___thread:
  1293. case tok::kw_thread_local:
  1294. case tok::kw__Thread_local:
  1295. // function-specifier
  1296. case tok::kw_inline:
  1297. case tok::kw_virtual:
  1298. case tok::kw_explicit:
  1299. // Modules
  1300. case tok::kw___module_private__:
  1301. // Debugger support
  1302. case tok::kw___unknown_anytype:
  1303. // type-specifier:
  1304. // simple-type-specifier
  1305. // class-specifier
  1306. // enum-specifier
  1307. // elaborated-type-specifier
  1308. // typename-specifier
  1309. // cv-qualifier
  1310. // class-specifier
  1311. // elaborated-type-specifier
  1312. case tok::kw_class:
  1313. case tok::kw_struct:
  1314. case tok::kw_union:
  1315. case tok::kw___interface:
  1316. // enum-specifier
  1317. case tok::kw_enum:
  1318. // cv-qualifier
  1319. case tok::kw_const:
  1320. case tok::kw_volatile:
  1321. return TPResult::True;
  1322. // OpenCL address space qualifiers
  1323. case tok::kw_private:
  1324. if (!getLangOpts().OpenCL)
  1325. return TPResult::False;
  1326. LLVM_FALLTHROUGH;
  1327. case tok::kw___private:
  1328. case tok::kw___local:
  1329. case tok::kw___global:
  1330. case tok::kw___constant:
  1331. case tok::kw___generic:
  1332. // OpenCL access qualifiers
  1333. case tok::kw___read_only:
  1334. case tok::kw___write_only:
  1335. case tok::kw___read_write:
  1336. // OpenCL pipe
  1337. case tok::kw_pipe:
  1338. // GNU
  1339. case tok::kw_restrict:
  1340. case tok::kw__Complex:
  1341. case tok::kw___attribute:
  1342. case tok::kw___auto_type:
  1343. return TPResult::True;
  1344. // Microsoft
  1345. case tok::kw___declspec:
  1346. case tok::kw___cdecl:
  1347. case tok::kw___stdcall:
  1348. case tok::kw___fastcall:
  1349. case tok::kw___thiscall:
  1350. case tok::kw___regcall:
  1351. case tok::kw___vectorcall:
  1352. case tok::kw___w64:
  1353. case tok::kw___sptr:
  1354. case tok::kw___uptr:
  1355. case tok::kw___ptr64:
  1356. case tok::kw___ptr32:
  1357. case tok::kw___forceinline:
  1358. case tok::kw___unaligned:
  1359. case tok::kw__Nonnull:
  1360. case tok::kw__Nullable:
  1361. case tok::kw__Null_unspecified:
  1362. case tok::kw___kindof:
  1363. return TPResult::True;
  1364. // Borland
  1365. case tok::kw___pascal:
  1366. return TPResult::True;
  1367. // AltiVec
  1368. case tok::kw___vector:
  1369. return TPResult::True;
  1370. case tok::annot_template_id: {
  1371. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1372. // If lookup for the template-name found nothing, don't assume we have a
  1373. // definitive disambiguation result yet.
  1374. if (TemplateId->Kind == TNK_Undeclared_template && InvalidAsDeclSpec) {
  1375. // 'template-id(' can be a valid expression but not a valid decl spec if
  1376. // the template-name is not declared, but we don't consider this to be a
  1377. // definitive disambiguation. In any other context, it's an error either
  1378. // way.
  1379. *InvalidAsDeclSpec = NextToken().is(tok::l_paren);
  1380. return TPResult::Ambiguous;
  1381. }
  1382. if (TemplateId->Kind != TNK_Type_template)
  1383. return TPResult::False;
  1384. CXXScopeSpec SS;
  1385. AnnotateTemplateIdTokenAsType();
  1386. assert(Tok.is(tok::annot_typename));
  1387. goto case_typename;
  1388. }
  1389. case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
  1390. // We've already annotated a scope; try to annotate a type.
  1391. if (TryAnnotateTypeOrScopeToken())
  1392. return TPResult::Error;
  1393. if (!Tok.is(tok::annot_typename)) {
  1394. // If the next token is an identifier or a type qualifier, then this
  1395. // can't possibly be a valid expression either.
  1396. if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
  1397. CXXScopeSpec SS;
  1398. Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
  1399. Tok.getAnnotationRange(),
  1400. SS);
  1401. if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
  1402. RevertingTentativeParsingAction PA(*this);
  1403. ConsumeAnnotationToken();
  1404. ConsumeToken();
  1405. bool isIdentifier = Tok.is(tok::identifier);
  1406. TPResult TPR = TPResult::False;
  1407. if (!isIdentifier)
  1408. TPR = isCXXDeclarationSpecifier(BracedCastResult,
  1409. InvalidAsDeclSpec);
  1410. if (isIdentifier ||
  1411. TPR == TPResult::True || TPR == TPResult::Error)
  1412. return TPResult::Error;
  1413. if (InvalidAsDeclSpec) {
  1414. // We can't tell whether this is a missing 'typename' or a valid
  1415. // expression.
  1416. *InvalidAsDeclSpec = true;
  1417. return TPResult::Ambiguous;
  1418. } else {
  1419. // In MS mode, if InvalidAsDeclSpec is not provided, and the tokens
  1420. // are or the form *) or &) *> or &> &&>, this can't be an expression.
  1421. // The typename must be missing.
  1422. if (getLangOpts().MSVCCompat) {
  1423. if (((Tok.is(tok::amp) || Tok.is(tok::star)) &&
  1424. (NextToken().is(tok::r_paren) ||
  1425. NextToken().is(tok::greater))) ||
  1426. (Tok.is(tok::ampamp) && NextToken().is(tok::greater)))
  1427. return TPResult::True;
  1428. }
  1429. }
  1430. } else {
  1431. // Try to resolve the name. If it doesn't exist, assume it was
  1432. // intended to name a type and keep disambiguating.
  1433. switch (TryAnnotateName()) {
  1434. case ANK_Error:
  1435. return TPResult::Error;
  1436. case ANK_TentativeDecl:
  1437. return TPResult::False;
  1438. case ANK_TemplateName:
  1439. // In C++17, this could be a type template for class template
  1440. // argument deduction.
  1441. if (getLangOpts().CPlusPlus17) {
  1442. if (TryAnnotateTypeOrScopeToken())
  1443. return TPResult::Error;
  1444. if (Tok.isNot(tok::identifier))
  1445. break;
  1446. }
  1447. // A bare type template-name which can't be a template template
  1448. // argument is an error, and was probably intended to be a type.
  1449. // In C++17, this could be class template argument deduction.
  1450. return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
  1451. ? TPResult::True
  1452. : TPResult::False;
  1453. case ANK_Unresolved:
  1454. return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
  1455. case ANK_Success:
  1456. break;
  1457. }
  1458. // Annotated it, check again.
  1459. assert(Tok.isNot(tok::annot_cxxscope) ||
  1460. NextToken().isNot(tok::identifier));
  1461. return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
  1462. }
  1463. }
  1464. return TPResult::False;
  1465. }
  1466. // If that succeeded, fallthrough into the generic simple-type-id case.
  1467. LLVM_FALLTHROUGH;
  1468. // The ambiguity resides in a simple-type-specifier/typename-specifier
  1469. // followed by a '('. The '(' could either be the start of:
  1470. //
  1471. // direct-declarator:
  1472. // '(' declarator ')'
  1473. //
  1474. // direct-abstract-declarator:
  1475. // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1476. // exception-specification[opt]
  1477. // '(' abstract-declarator ')'
  1478. //
  1479. // or part of a function-style cast expression:
  1480. //
  1481. // simple-type-specifier '(' expression-list[opt] ')'
  1482. //
  1483. // simple-type-specifier:
  1484. case tok::annot_typename:
  1485. case_typename:
  1486. // In Objective-C, we might have a protocol-qualified type.
  1487. if (getLangOpts().ObjC && NextToken().is(tok::less)) {
  1488. // Tentatively parse the protocol qualifiers.
  1489. RevertingTentativeParsingAction PA(*this);
  1490. ConsumeAnyToken(); // The type token
  1491. TPResult TPR = TryParseProtocolQualifiers();
  1492. bool isFollowedByParen = Tok.is(tok::l_paren);
  1493. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1494. if (TPR == TPResult::Error)
  1495. return TPResult::Error;
  1496. if (isFollowedByParen)
  1497. return TPResult::Ambiguous;
  1498. if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
  1499. return BracedCastResult;
  1500. return TPResult::True;
  1501. }
  1502. LLVM_FALLTHROUGH;
  1503. case tok::kw_char:
  1504. case tok::kw_wchar_t:
  1505. case tok::kw_char8_t:
  1506. case tok::kw_char16_t:
  1507. case tok::kw_char32_t:
  1508. case tok::kw_bool:
  1509. case tok::kw_short:
  1510. case tok::kw_int:
  1511. case tok::kw_long:
  1512. case tok::kw___int64:
  1513. case tok::kw___int128:
  1514. case tok::kw_signed:
  1515. case tok::kw_unsigned:
  1516. case tok::kw_half:
  1517. case tok::kw_float:
  1518. case tok::kw_double:
  1519. case tok::kw__Float16:
  1520. case tok::kw___float128:
  1521. case tok::kw_void:
  1522. case tok::annot_decltype:
  1523. #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
  1524. #include "clang/Basic/OpenCLImageTypes.def"
  1525. if (NextToken().is(tok::l_paren))
  1526. return TPResult::Ambiguous;
  1527. // This is a function-style cast in all cases we disambiguate other than
  1528. // one:
  1529. // struct S {
  1530. // enum E : int { a = 4 }; // enum
  1531. // enum E : int { 4 }; // bit-field
  1532. // };
  1533. if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
  1534. return BracedCastResult;
  1535. if (isStartOfObjCClassMessageMissingOpenBracket())
  1536. return TPResult::False;
  1537. return TPResult::True;
  1538. // GNU typeof support.
  1539. case tok::kw_typeof: {
  1540. if (NextToken().isNot(tok::l_paren))
  1541. return TPResult::True;
  1542. RevertingTentativeParsingAction PA(*this);
  1543. TPResult TPR = TryParseTypeofSpecifier();
  1544. bool isFollowedByParen = Tok.is(tok::l_paren);
  1545. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1546. if (TPR == TPResult::Error)
  1547. return TPResult::Error;
  1548. if (isFollowedByParen)
  1549. return TPResult::Ambiguous;
  1550. if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
  1551. return BracedCastResult;
  1552. return TPResult::True;
  1553. }
  1554. // C++0x type traits support
  1555. case tok::kw___underlying_type:
  1556. return TPResult::True;
  1557. // C11 _Atomic
  1558. case tok::kw__Atomic:
  1559. return TPResult::True;
  1560. default:
  1561. return TPResult::False;
  1562. }
  1563. }
  1564. bool Parser::isCXXDeclarationSpecifierAType() {
  1565. switch (Tok.getKind()) {
  1566. // typename-specifier
  1567. case tok::annot_decltype:
  1568. case tok::annot_template_id:
  1569. case tok::annot_typename:
  1570. case tok::kw_typeof:
  1571. case tok::kw___underlying_type:
  1572. return true;
  1573. // elaborated-type-specifier
  1574. case tok::kw_class:
  1575. case tok::kw_struct:
  1576. case tok::kw_union:
  1577. case tok::kw___interface:
  1578. case tok::kw_enum:
  1579. return true;
  1580. // simple-type-specifier
  1581. case tok::kw_char:
  1582. case tok::kw_wchar_t:
  1583. case tok::kw_char8_t:
  1584. case tok::kw_char16_t:
  1585. case tok::kw_char32_t:
  1586. case tok::kw_bool:
  1587. case tok::kw_short:
  1588. case tok::kw_int:
  1589. case tok::kw_long:
  1590. case tok::kw___int64:
  1591. case tok::kw___int128:
  1592. case tok::kw_signed:
  1593. case tok::kw_unsigned:
  1594. case tok::kw_half:
  1595. case tok::kw_float:
  1596. case tok::kw_double:
  1597. case tok::kw__Float16:
  1598. case tok::kw___float128:
  1599. case tok::kw_void:
  1600. case tok::kw___unknown_anytype:
  1601. case tok::kw___auto_type:
  1602. #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
  1603. #include "clang/Basic/OpenCLImageTypes.def"
  1604. return true;
  1605. case tok::kw_auto:
  1606. return getLangOpts().CPlusPlus11;
  1607. case tok::kw__Atomic:
  1608. // "_Atomic foo"
  1609. return NextToken().is(tok::l_paren);
  1610. default:
  1611. return false;
  1612. }
  1613. }
  1614. /// [GNU] typeof-specifier:
  1615. /// 'typeof' '(' expressions ')'
  1616. /// 'typeof' '(' type-name ')'
  1617. ///
  1618. Parser::TPResult Parser::TryParseTypeofSpecifier() {
  1619. assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
  1620. ConsumeToken();
  1621. assert(Tok.is(tok::l_paren) && "Expected '('");
  1622. // Parse through the parens after 'typeof'.
  1623. ConsumeParen();
  1624. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1625. return TPResult::Error;
  1626. return TPResult::Ambiguous;
  1627. }
  1628. /// [ObjC] protocol-qualifiers:
  1629. //// '<' identifier-list '>'
  1630. Parser::TPResult Parser::TryParseProtocolQualifiers() {
  1631. assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
  1632. ConsumeToken();
  1633. do {
  1634. if (Tok.isNot(tok::identifier))
  1635. return TPResult::Error;
  1636. ConsumeToken();
  1637. if (Tok.is(tok::comma)) {
  1638. ConsumeToken();
  1639. continue;
  1640. }
  1641. if (Tok.is(tok::greater)) {
  1642. ConsumeToken();
  1643. return TPResult::Ambiguous;
  1644. }
  1645. } while (false);
  1646. return TPResult::Error;
  1647. }
  1648. /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
  1649. /// a constructor-style initializer, when parsing declaration statements.
  1650. /// Returns true for function declarator and false for constructor-style
  1651. /// initializer.
  1652. /// If during the disambiguation process a parsing error is encountered,
  1653. /// the function returns true to let the declaration parsing code handle it.
  1654. ///
  1655. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1656. /// exception-specification[opt]
  1657. ///
  1658. bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
  1659. // C++ 8.2p1:
  1660. // The ambiguity arising from the similarity between a function-style cast and
  1661. // a declaration mentioned in 6.8 can also occur in the context of a
  1662. // declaration. In that context, the choice is between a function declaration
  1663. // with a redundant set of parentheses around a parameter name and an object
  1664. // declaration with a function-style cast as the initializer. Just as for the
  1665. // ambiguities mentioned in 6.8, the resolution is to consider any construct
  1666. // that could possibly be a declaration a declaration.
  1667. RevertingTentativeParsingAction PA(*this);
  1668. ConsumeParen();
  1669. bool InvalidAsDeclaration = false;
  1670. TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
  1671. if (TPR == TPResult::Ambiguous) {
  1672. if (Tok.isNot(tok::r_paren))
  1673. TPR = TPResult::False;
  1674. else {
  1675. const Token &Next = NextToken();
  1676. if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
  1677. tok::kw_throw, tok::kw_noexcept, tok::l_square,
  1678. tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
  1679. isCXX11VirtSpecifier(Next))
  1680. // The next token cannot appear after a constructor-style initializer,
  1681. // and can appear next in a function definition. This must be a function
  1682. // declarator.
  1683. TPR = TPResult::True;
  1684. else if (InvalidAsDeclaration)
  1685. // Use the absence of 'typename' as a tie-breaker.
  1686. TPR = TPResult::False;
  1687. }
  1688. }
  1689. if (IsAmbiguous && TPR == TPResult::Ambiguous)
  1690. *IsAmbiguous = true;
  1691. // In case of an error, let the declaration parsing code handle it.
  1692. return TPR != TPResult::False;
  1693. }
  1694. /// parameter-declaration-clause:
  1695. /// parameter-declaration-list[opt] '...'[opt]
  1696. /// parameter-declaration-list ',' '...'
  1697. ///
  1698. /// parameter-declaration-list:
  1699. /// parameter-declaration
  1700. /// parameter-declaration-list ',' parameter-declaration
  1701. ///
  1702. /// parameter-declaration:
  1703. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1704. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1705. /// '=' assignment-expression
  1706. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1707. /// attributes[opt]
  1708. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1709. /// attributes[opt] '=' assignment-expression
  1710. ///
  1711. Parser::TPResult
  1712. Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
  1713. bool VersusTemplateArgument) {
  1714. if (Tok.is(tok::r_paren))
  1715. return TPResult::Ambiguous;
  1716. // parameter-declaration-list[opt] '...'[opt]
  1717. // parameter-declaration-list ',' '...'
  1718. //
  1719. // parameter-declaration-list:
  1720. // parameter-declaration
  1721. // parameter-declaration-list ',' parameter-declaration
  1722. //
  1723. while (1) {
  1724. // '...'[opt]
  1725. if (Tok.is(tok::ellipsis)) {
  1726. ConsumeToken();
  1727. if (Tok.is(tok::r_paren))
  1728. return TPResult::True; // '...)' is a sign of a function declarator.
  1729. else
  1730. return TPResult::False;
  1731. }
  1732. // An attribute-specifier-seq here is a sign of a function declarator.
  1733. if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
  1734. /*OuterMightBeMessageSend*/true))
  1735. return TPResult::True;
  1736. ParsedAttributes attrs(AttrFactory);
  1737. MaybeParseMicrosoftAttributes(attrs);
  1738. // decl-specifier-seq
  1739. // A parameter-declaration's initializer must be preceded by an '=', so
  1740. // decl-specifier-seq '{' is not a parameter in C++11.
  1741. TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
  1742. InvalidAsDeclaration);
  1743. // A declaration-specifier (not followed by '(' or '{') means this can't be
  1744. // an expression, but it could still be a template argument.
  1745. if (TPR != TPResult::Ambiguous &&
  1746. !(VersusTemplateArgument && TPR == TPResult::True))
  1747. return TPR;
  1748. bool SeenType = false;
  1749. do {
  1750. SeenType |= isCXXDeclarationSpecifierAType();
  1751. if (TryConsumeDeclarationSpecifier() == TPResult::Error)
  1752. return TPResult::Error;
  1753. // If we see a parameter name, this can't be a template argument.
  1754. if (SeenType && Tok.is(tok::identifier))
  1755. return TPResult::True;
  1756. TPR = isCXXDeclarationSpecifier(TPResult::False,
  1757. InvalidAsDeclaration);
  1758. if (TPR == TPResult::Error)
  1759. return TPR;
  1760. // Two declaration-specifiers means this can't be an expression.
  1761. if (TPR == TPResult::True && !VersusTemplateArgument)
  1762. return TPR;
  1763. } while (TPR != TPResult::False);
  1764. // declarator
  1765. // abstract-declarator[opt]
  1766. TPR = TryParseDeclarator(true/*mayBeAbstract*/);
  1767. if (TPR != TPResult::Ambiguous)
  1768. return TPR;
  1769. // [GNU] attributes[opt]
  1770. if (Tok.is(tok::kw___attribute))
  1771. return TPResult::True;
  1772. // If we're disambiguating a template argument in a default argument in
  1773. // a class definition versus a parameter declaration, an '=' here
  1774. // disambiguates the parse one way or the other.
  1775. // If this is a parameter, it must have a default argument because
  1776. // (a) the previous parameter did, and
  1777. // (b) this must be the first declaration of the function, so we can't
  1778. // inherit any default arguments from elsewhere.
  1779. // If we see an ')', then we've reached the end of a
  1780. // parameter-declaration-clause, and the last param is missing its default
  1781. // argument.
  1782. if (VersusTemplateArgument)
  1783. return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
  1784. : TPResult::False;
  1785. if (Tok.is(tok::equal)) {
  1786. // '=' assignment-expression
  1787. // Parse through assignment-expression.
  1788. // FIXME: assignment-expression may contain an unparenthesized comma.
  1789. if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
  1790. return TPResult::Error;
  1791. }
  1792. if (Tok.is(tok::ellipsis)) {
  1793. ConsumeToken();
  1794. if (Tok.is(tok::r_paren))
  1795. return TPResult::True; // '...)' is a sign of a function declarator.
  1796. else
  1797. return TPResult::False;
  1798. }
  1799. if (!TryConsumeToken(tok::comma))
  1800. break;
  1801. }
  1802. return TPResult::Ambiguous;
  1803. }
  1804. /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
  1805. /// parsing as a function declarator.
  1806. /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
  1807. /// return TPResult::Ambiguous, otherwise it will return either False() or
  1808. /// Error().
  1809. ///
  1810. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1811. /// exception-specification[opt]
  1812. ///
  1813. /// exception-specification:
  1814. /// 'throw' '(' type-id-list[opt] ')'
  1815. ///
  1816. Parser::TPResult Parser::TryParseFunctionDeclarator() {
  1817. // The '(' is already parsed.
  1818. TPResult TPR = TryParseParameterDeclarationClause();
  1819. if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
  1820. TPR = TPResult::False;
  1821. if (TPR == TPResult::False || TPR == TPResult::Error)
  1822. return TPR;
  1823. // Parse through the parens.
  1824. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1825. return TPResult::Error;
  1826. // cv-qualifier-seq
  1827. while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
  1828. tok::kw_restrict))
  1829. ConsumeToken();
  1830. // ref-qualifier[opt]
  1831. if (Tok.isOneOf(tok::amp, tok::ampamp))
  1832. ConsumeToken();
  1833. // exception-specification
  1834. if (Tok.is(tok::kw_throw)) {
  1835. ConsumeToken();
  1836. if (Tok.isNot(tok::l_paren))
  1837. return TPResult::Error;
  1838. // Parse through the parens after 'throw'.
  1839. ConsumeParen();
  1840. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1841. return TPResult::Error;
  1842. }
  1843. if (Tok.is(tok::kw_noexcept)) {
  1844. ConsumeToken();
  1845. // Possibly an expression as well.
  1846. if (Tok.is(tok::l_paren)) {
  1847. // Find the matching rparen.
  1848. ConsumeParen();
  1849. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1850. return TPResult::Error;
  1851. }
  1852. }
  1853. return TPResult::Ambiguous;
  1854. }
  1855. /// '[' constant-expression[opt] ']'
  1856. ///
  1857. Parser::TPResult Parser::TryParseBracketDeclarator() {
  1858. ConsumeBracket();
  1859. if (!SkipUntil(tok::r_square, StopAtSemi))
  1860. return TPResult::Error;
  1861. return TPResult::Ambiguous;
  1862. }
  1863. /// Determine whether we might be looking at the '<' template-argument-list '>'
  1864. /// of a template-id or simple-template-id, rather than a less-than comparison.
  1865. /// This will often fail and produce an ambiguity, but should never be wrong
  1866. /// if it returns True or False.
  1867. Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
  1868. if (!TokensToSkip) {
  1869. if (Tok.isNot(tok::less))
  1870. return TPResult::False;
  1871. if (NextToken().is(tok::greater))
  1872. return TPResult::True;
  1873. }
  1874. RevertingTentativeParsingAction PA(*this);
  1875. while (TokensToSkip) {
  1876. ConsumeAnyToken();
  1877. --TokensToSkip;
  1878. }
  1879. if (!TryConsumeToken(tok::less))
  1880. return TPResult::False;
  1881. // We can't do much to tell an expression apart from a template-argument,
  1882. // but one good distinguishing factor is that a "decl-specifier" not
  1883. // followed by '(' or '{' can't appear in an expression.
  1884. bool InvalidAsTemplateArgumentList = false;
  1885. if (isCXXDeclarationSpecifier(TPResult::False,
  1886. &InvalidAsTemplateArgumentList) ==
  1887. TPResult::True)
  1888. return TPResult::True;
  1889. if (InvalidAsTemplateArgumentList)
  1890. return TPResult::False;
  1891. // FIXME: In many contexts, X<thing1, Type> can only be a
  1892. // template-argument-list. But that's not true in general:
  1893. //
  1894. // using b = int;
  1895. // void f() {
  1896. // int a = A<B, b, c = C>D; // OK, declares b, not a template-id.
  1897. //
  1898. // X<Y<0, int> // ', int>' might be end of X's template argument list
  1899. //
  1900. // We might be able to disambiguate a few more cases if we're careful.
  1901. // A template-argument-list must be terminated by a '>'.
  1902. if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
  1903. StopAtSemi | StopBeforeMatch))
  1904. return TPResult::Ambiguous;
  1905. return TPResult::False;
  1906. }