ParseTentative.cpp 58 KB

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