ParseTentative.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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. // HLSL Change Starts
  855. case tok::kw_column_major:
  856. case tok::kw_row_major:
  857. case tok::kw_snorm:
  858. case tok::kw_unorm:
  859. // HLSL Change Ends
  860. case tok::kw_restrict:
  861. case tok::kw_short:
  862. case tok::kw_signed:
  863. case tok::kw_struct:
  864. case tok::kw_union:
  865. case tok::kw_unsigned:
  866. case tok::kw_void:
  867. case tok::kw_volatile:
  868. case tok::kw__Bool:
  869. case tok::kw__Complex:
  870. case tok::kw_class:
  871. case tok::kw_typename:
  872. case tok::kw_wchar_t:
  873. case tok::kw_char16_t:
  874. case tok::kw_char32_t:
  875. case tok::kw__Decimal32:
  876. case tok::kw__Decimal64:
  877. case tok::kw__Decimal128:
  878. case tok::kw___interface:
  879. case tok::kw___thread:
  880. case tok::kw_thread_local:
  881. case tok::kw__Thread_local:
  882. case tok::kw_typeof:
  883. case tok::kw___underlying_type:
  884. case tok::kw___cdecl:
  885. case tok::kw___stdcall:
  886. case tok::kw___fastcall:
  887. case tok::kw___thiscall:
  888. case tok::kw___vectorcall:
  889. case tok::kw___unaligned:
  890. case tok::kw___vector:
  891. case tok::kw___pixel:
  892. case tok::kw___bool:
  893. case tok::kw__Atomic:
  894. case tok::kw___unknown_anytype:
  895. return TPResult::False;
  896. default:
  897. break;
  898. }
  899. return TPResult::Ambiguous;
  900. }
  901. bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
  902. return std::find(TentativelyDeclaredIdentifiers.begin(),
  903. TentativelyDeclaredIdentifiers.end(), II)
  904. != TentativelyDeclaredIdentifiers.end();
  905. }
  906. namespace {
  907. class TentativeParseCCC : public CorrectionCandidateCallback {
  908. public:
  909. TentativeParseCCC(const Token &Next) {
  910. WantRemainingKeywords = false;
  911. WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
  912. tok::l_brace, tok::identifier);
  913. }
  914. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  915. // Reject any candidate that only resolves to instance members since they
  916. // aren't viable as standalone identifiers instead of member references.
  917. if (Candidate.isResolved() && !Candidate.isKeyword() &&
  918. std::all_of(Candidate.begin(), Candidate.end(),
  919. [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
  920. return false;
  921. return CorrectionCandidateCallback::ValidateCandidate(Candidate);
  922. }
  923. };
  924. }
  925. /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
  926. /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
  927. /// be either a decl-specifier or a function-style cast, and TPResult::Error
  928. /// if a parsing error was found and reported.
  929. ///
  930. /// If HasMissingTypename is provided, a name with a dependent scope specifier
  931. /// will be treated as ambiguous if the 'typename' keyword is missing. If this
  932. /// happens, *HasMissingTypename will be set to 'true'. This will also be used
  933. /// as an indicator that undeclared identifiers (which will trigger a later
  934. /// parse error) should be treated as types. Returns TPResult::Ambiguous in
  935. /// such cases.
  936. ///
  937. /// decl-specifier:
  938. /// storage-class-specifier
  939. /// type-specifier
  940. /// function-specifier
  941. /// 'friend'
  942. /// 'typedef'
  943. /// [C++11] 'constexpr'
  944. /// [GNU] attributes declaration-specifiers[opt]
  945. ///
  946. /// storage-class-specifier:
  947. /// 'register'
  948. /// 'static'
  949. /// 'extern'
  950. /// 'mutable'
  951. /// 'auto'
  952. /// [GNU] '__thread'
  953. /// [C++11] 'thread_local'
  954. /// [C11] '_Thread_local'
  955. ///
  956. /// function-specifier:
  957. /// 'inline'
  958. /// 'virtual'
  959. /// 'explicit'
  960. ///
  961. /// typedef-name:
  962. /// identifier
  963. ///
  964. /// type-specifier:
  965. /// simple-type-specifier
  966. /// class-specifier
  967. /// enum-specifier
  968. /// elaborated-type-specifier
  969. /// typename-specifier
  970. /// cv-qualifier
  971. ///
  972. /// simple-type-specifier:
  973. /// '::'[opt] nested-name-specifier[opt] type-name
  974. /// '::'[opt] nested-name-specifier 'template'
  975. /// simple-template-id [TODO]
  976. /// 'char'
  977. /// 'wchar_t'
  978. /// 'bool'
  979. /// 'short'
  980. /// 'int'
  981. /// 'long'
  982. /// 'signed'
  983. /// 'unsigned'
  984. /// 'float'
  985. /// 'double'
  986. /// 'void'
  987. /// [GNU] typeof-specifier
  988. /// [GNU] '_Complex'
  989. /// [C++11] 'auto'
  990. /// [C++11] 'decltype' ( expression )
  991. /// [C++1y] 'decltype' ( 'auto' )
  992. ///
  993. /// type-name:
  994. /// class-name
  995. /// enum-name
  996. /// typedef-name
  997. ///
  998. /// elaborated-type-specifier:
  999. /// class-key '::'[opt] nested-name-specifier[opt] identifier
  1000. /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
  1001. /// simple-template-id
  1002. /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
  1003. ///
  1004. /// enum-name:
  1005. /// identifier
  1006. ///
  1007. /// enum-specifier:
  1008. /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
  1009. /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
  1010. ///
  1011. /// class-specifier:
  1012. /// class-head '{' member-specification[opt] '}'
  1013. ///
  1014. /// class-head:
  1015. /// class-key identifier[opt] base-clause[opt]
  1016. /// class-key nested-name-specifier identifier base-clause[opt]
  1017. /// class-key nested-name-specifier[opt] simple-template-id
  1018. /// base-clause[opt]
  1019. ///
  1020. /// class-key:
  1021. /// 'class'
  1022. /// 'struct'
  1023. /// 'union'
  1024. ///
  1025. /// cv-qualifier:
  1026. /// 'const'
  1027. /// 'volatile'
  1028. /// [GNU] restrict
  1029. ///
  1030. Parser::TPResult
  1031. Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
  1032. bool *HasMissingTypename) {
  1033. switch (Tok.getKind()) {
  1034. case tok::identifier: {
  1035. // Check for need to substitute AltiVec __vector keyword
  1036. // for "vector" identifier.
  1037. if (TryAltiVecVectorToken())
  1038. return TPResult::True;
  1039. const Token &Next = NextToken();
  1040. // In 'foo bar', 'foo' is always a type name outside of Objective-C.
  1041. if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
  1042. return TPResult::True;
  1043. if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
  1044. // Determine whether this is a valid expression. If not, we will hit
  1045. // a parse error one way or another. In that case, tell the caller that
  1046. // this is ambiguous. Typo-correct to type and expression keywords and
  1047. // to types and identifiers, in order to try to recover from errors.
  1048. switch (TryAnnotateName(false /* no nested name specifier */,
  1049. llvm::make_unique<TentativeParseCCC>(Next))) {
  1050. case ANK_Error:
  1051. return TPResult::Error;
  1052. case ANK_TentativeDecl:
  1053. return TPResult::False;
  1054. case ANK_TemplateName:
  1055. // A bare type template-name which can't be a template template
  1056. // argument is an error, and was probably intended to be a type.
  1057. return GreaterThanIsOperator ? TPResult::True : TPResult::False;
  1058. case ANK_Unresolved:
  1059. return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
  1060. case ANK_Success:
  1061. break;
  1062. }
  1063. assert(Tok.isNot(tok::identifier) &&
  1064. "TryAnnotateName succeeded without producing an annotation");
  1065. } else {
  1066. // This might possibly be a type with a dependent scope specifier and
  1067. // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
  1068. // since it will annotate as a primary expression, and we want to use the
  1069. // "missing 'typename'" logic.
  1070. if (TryAnnotateTypeOrScopeToken())
  1071. return TPResult::Error;
  1072. // If annotation failed, assume it's a non-type.
  1073. // FIXME: If this happens due to an undeclared identifier, treat it as
  1074. // ambiguous.
  1075. if (Tok.is(tok::identifier))
  1076. return TPResult::False;
  1077. }
  1078. // We annotated this token as something. Recurse to handle whatever we got.
  1079. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  1080. }
  1081. case tok::kw_typename: // typename T::type
  1082. // Annotate typenames and C++ scope specifiers. If we get one, just
  1083. // recurse to handle whatever we get.
  1084. if (TryAnnotateTypeOrScopeToken())
  1085. return TPResult::Error;
  1086. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  1087. case tok::coloncolon: { // ::foo::bar
  1088. const Token &Next = NextToken();
  1089. if (Next.isOneOf(tok::kw_new, // ::new
  1090. tok::kw_delete)) // ::delete
  1091. return TPResult::False;
  1092. }
  1093. // Fall through.
  1094. case tok::kw___super:
  1095. case tok::kw_decltype:
  1096. // Annotate typenames and C++ scope specifiers. If we get one, just
  1097. // recurse to handle whatever we get.
  1098. if (TryAnnotateTypeOrScopeToken())
  1099. return TPResult::Error;
  1100. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  1101. // decl-specifier:
  1102. // storage-class-specifier
  1103. // type-specifier
  1104. // function-specifier
  1105. // 'friend'
  1106. // 'typedef'
  1107. // 'constexpr'
  1108. // 'concept'
  1109. case tok::kw_friend:
  1110. case tok::kw_typedef:
  1111. case tok::kw_constexpr:
  1112. case tok::kw_concept:
  1113. // storage-class-specifier
  1114. case tok::kw_register:
  1115. case tok::kw_static:
  1116. case tok::kw_extern:
  1117. case tok::kw_mutable:
  1118. case tok::kw_auto:
  1119. case tok::kw___thread:
  1120. case tok::kw_thread_local:
  1121. case tok::kw__Thread_local:
  1122. // function-specifier
  1123. case tok::kw_inline:
  1124. case tok::kw_virtual:
  1125. case tok::kw_explicit:
  1126. // Modules
  1127. case tok::kw___module_private__:
  1128. // Debugger support
  1129. case tok::kw___unknown_anytype:
  1130. // type-specifier:
  1131. // simple-type-specifier
  1132. // class-specifier
  1133. // enum-specifier
  1134. // elaborated-type-specifier
  1135. // typename-specifier
  1136. // cv-qualifier
  1137. // class-specifier
  1138. // elaborated-type-specifier
  1139. case tok::kw_class:
  1140. case tok::kw_struct:
  1141. case tok::kw_union:
  1142. case tok::kw___interface:
  1143. // enum-specifier
  1144. case tok::kw_enum:
  1145. // cv-qualifier
  1146. case tok::kw_const:
  1147. case tok::kw_volatile:
  1148. // GNU
  1149. case tok::kw_restrict:
  1150. case tok::kw__Complex:
  1151. case tok::kw___attribute:
  1152. return TPResult::True;
  1153. // HLSL Change Starts
  1154. case tok::kw_in:
  1155. case tok::kw_inout:
  1156. case tok::kw_out:
  1157. case tok::kw_linear:
  1158. case tok::kw_centroid:
  1159. case tok::kw_nointerpolation:
  1160. case tok::kw_noperspective:
  1161. case tok::kw_sample:
  1162. case tok::kw_precise:
  1163. case tok::kw_shared:
  1164. case tok::kw_groupshared:
  1165. case tok::kw_globallycoherent:
  1166. case tok::kw_uniform:
  1167. case tok::kw_row_major:
  1168. case tok::kw_column_major:
  1169. case tok::kw_snorm:
  1170. case tok::kw_unorm:
  1171. case tok::kw_point:
  1172. case tok::kw_line:
  1173. case tok::kw_lineadj:
  1174. case tok::kw_triangle:
  1175. case tok::kw_triangleadj:
  1176. return TPResult::True;
  1177. // HLSL Change Ends
  1178. // Microsoft
  1179. case tok::kw___declspec:
  1180. case tok::kw___cdecl:
  1181. case tok::kw___stdcall:
  1182. case tok::kw___fastcall:
  1183. case tok::kw___thiscall:
  1184. case tok::kw___vectorcall:
  1185. case tok::kw___w64:
  1186. case tok::kw___sptr:
  1187. case tok::kw___uptr:
  1188. case tok::kw___ptr64:
  1189. case tok::kw___ptr32:
  1190. case tok::kw___forceinline:
  1191. case tok::kw___unaligned:
  1192. case tok::kw__Nonnull:
  1193. case tok::kw__Nullable:
  1194. case tok::kw__Null_unspecified:
  1195. case tok::kw___kindof:
  1196. return TPResult::True;
  1197. // Borland
  1198. case tok::kw___pascal:
  1199. return TPResult::True;
  1200. // AltiVec
  1201. case tok::kw___vector:
  1202. return TPResult::True;
  1203. case tok::annot_template_id: {
  1204. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1205. if (TemplateId->Kind != TNK_Type_template)
  1206. return TPResult::False;
  1207. CXXScopeSpec SS;
  1208. AnnotateTemplateIdTokenAsType();
  1209. assert(Tok.is(tok::annot_typename));
  1210. goto case_typename;
  1211. }
  1212. case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
  1213. // We've already annotated a scope; try to annotate a type.
  1214. if (TryAnnotateTypeOrScopeToken())
  1215. return TPResult::Error;
  1216. if (!Tok.is(tok::annot_typename)) {
  1217. // If the next token is an identifier or a type qualifier, then this
  1218. // can't possibly be a valid expression either.
  1219. if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
  1220. CXXScopeSpec SS;
  1221. Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
  1222. Tok.getAnnotationRange(),
  1223. SS);
  1224. if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
  1225. TentativeParsingAction PA(*this);
  1226. ConsumeToken();
  1227. ConsumeToken();
  1228. bool isIdentifier = Tok.is(tok::identifier);
  1229. TPResult TPR = TPResult::False;
  1230. if (!isIdentifier)
  1231. TPR = isCXXDeclarationSpecifier(BracedCastResult,
  1232. HasMissingTypename);
  1233. PA.Revert();
  1234. if (isIdentifier ||
  1235. TPR == TPResult::True || TPR == TPResult::Error)
  1236. return TPResult::Error;
  1237. if (HasMissingTypename) {
  1238. // We can't tell whether this is a missing 'typename' or a valid
  1239. // expression.
  1240. *HasMissingTypename = true;
  1241. return TPResult::Ambiguous;
  1242. }
  1243. } else {
  1244. // Try to resolve the name. If it doesn't exist, assume it was
  1245. // intended to name a type and keep disambiguating.
  1246. switch (TryAnnotateName(false /* SS is not dependent */)) {
  1247. case ANK_Error:
  1248. return TPResult::Error;
  1249. case ANK_TentativeDecl:
  1250. return TPResult::False;
  1251. case ANK_TemplateName:
  1252. // A bare type template-name which can't be a template template
  1253. // argument is an error, and was probably intended to be a type.
  1254. return GreaterThanIsOperator ? TPResult::True : TPResult::False;
  1255. case ANK_Unresolved:
  1256. return HasMissingTypename ? TPResult::Ambiguous
  1257. : TPResult::False;
  1258. case ANK_Success:
  1259. // Annotated it, check again.
  1260. assert(Tok.isNot(tok::annot_cxxscope) ||
  1261. NextToken().isNot(tok::identifier));
  1262. return isCXXDeclarationSpecifier(BracedCastResult,
  1263. HasMissingTypename);
  1264. }
  1265. }
  1266. }
  1267. return TPResult::False;
  1268. }
  1269. // If that succeeded, fallthrough into the generic simple-type-id case.
  1270. // The ambiguity resides in a simple-type-specifier/typename-specifier
  1271. // followed by a '('. The '(' could either be the start of:
  1272. //
  1273. // direct-declarator:
  1274. // '(' declarator ')'
  1275. //
  1276. // direct-abstract-declarator:
  1277. // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1278. // exception-specification[opt]
  1279. // '(' abstract-declarator ')'
  1280. //
  1281. // or part of a function-style cast expression:
  1282. //
  1283. // simple-type-specifier '(' expression-list[opt] ')'
  1284. //
  1285. // simple-type-specifier:
  1286. case tok::annot_typename:
  1287. case_typename:
  1288. // In Objective-C, we might have a protocol-qualified type.
  1289. if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
  1290. // Tentatively parse the protocol qualifiers.
  1291. TentativeParsingAction PA(*this);
  1292. ConsumeToken(); // The type token
  1293. TPResult TPR = TryParseProtocolQualifiers();
  1294. bool isFollowedByParen = Tok.is(tok::l_paren);
  1295. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1296. PA.Revert();
  1297. if (TPR == TPResult::Error)
  1298. return TPResult::Error;
  1299. if (isFollowedByParen)
  1300. return TPResult::Ambiguous;
  1301. if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
  1302. return BracedCastResult;
  1303. return TPResult::True;
  1304. }
  1305. case tok::kw_char:
  1306. case tok::kw_wchar_t:
  1307. case tok::kw_char16_t:
  1308. case tok::kw_char32_t:
  1309. case tok::kw_bool:
  1310. case tok::kw_short:
  1311. case tok::kw_int:
  1312. case tok::kw_long:
  1313. case tok::kw___int64:
  1314. case tok::kw___int128:
  1315. case tok::kw_signed:
  1316. case tok::kw_unsigned:
  1317. case tok::kw_half:
  1318. case tok::kw_float:
  1319. case tok::kw_double:
  1320. case tok::kw_void:
  1321. case tok::annot_decltype:
  1322. if (NextToken().is(tok::l_paren))
  1323. return TPResult::Ambiguous;
  1324. // This is a function-style cast in all cases we disambiguate other than
  1325. // one:
  1326. // struct S {
  1327. // enum E : int { a = 4 }; // enum
  1328. // enum E : int { 4 }; // bit-field
  1329. // };
  1330. if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
  1331. return BracedCastResult;
  1332. if (isStartOfObjCClassMessageMissingOpenBracket())
  1333. return TPResult::False;
  1334. return TPResult::True;
  1335. // GNU typeof support.
  1336. case tok::kw_typeof: {
  1337. if (NextToken().isNot(tok::l_paren))
  1338. return TPResult::True;
  1339. TentativeParsingAction PA(*this);
  1340. TPResult TPR = TryParseTypeofSpecifier();
  1341. bool isFollowedByParen = Tok.is(tok::l_paren);
  1342. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1343. PA.Revert();
  1344. if (TPR == TPResult::Error)
  1345. return TPResult::Error;
  1346. if (isFollowedByParen)
  1347. return TPResult::Ambiguous;
  1348. if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
  1349. return BracedCastResult;
  1350. return TPResult::True;
  1351. }
  1352. // C++0x type traits support
  1353. case tok::kw___underlying_type:
  1354. return TPResult::True;
  1355. // C11 _Atomic
  1356. case tok::kw__Atomic:
  1357. return TPResult::True;
  1358. default:
  1359. return TPResult::False;
  1360. }
  1361. }
  1362. bool Parser::isCXXDeclarationSpecifierAType() {
  1363. switch (Tok.getKind()) {
  1364. // typename-specifier
  1365. case tok::annot_decltype:
  1366. case tok::annot_template_id:
  1367. case tok::annot_typename:
  1368. case tok::kw_typeof:
  1369. case tok::kw___underlying_type:
  1370. return true;
  1371. // elaborated-type-specifier
  1372. case tok::kw_class:
  1373. case tok::kw_struct:
  1374. case tok::kw_union:
  1375. case tok::kw___interface:
  1376. case tok::kw_enum:
  1377. return true;
  1378. // simple-type-specifier
  1379. case tok::kw_char:
  1380. case tok::kw_wchar_t:
  1381. case tok::kw_char16_t:
  1382. case tok::kw_char32_t:
  1383. case tok::kw_bool:
  1384. case tok::kw_short:
  1385. case tok::kw_int:
  1386. case tok::kw_long:
  1387. case tok::kw___int64:
  1388. case tok::kw___int128:
  1389. case tok::kw_signed:
  1390. case tok::kw_unsigned:
  1391. case tok::kw_half:
  1392. case tok::kw_float:
  1393. case tok::kw_double:
  1394. case tok::kw_void:
  1395. case tok::kw___unknown_anytype:
  1396. return true;
  1397. case tok::kw_auto:
  1398. return getLangOpts().CPlusPlus11;
  1399. case tok::kw__Atomic:
  1400. // "_Atomic foo"
  1401. return NextToken().is(tok::l_paren);
  1402. default:
  1403. return false;
  1404. }
  1405. }
  1406. /// [GNU] typeof-specifier:
  1407. /// 'typeof' '(' expressions ')'
  1408. /// 'typeof' '(' type-name ')'
  1409. ///
  1410. Parser::TPResult Parser::TryParseTypeofSpecifier() {
  1411. assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
  1412. ConsumeToken();
  1413. assert(Tok.is(tok::l_paren) && "Expected '('");
  1414. // Parse through the parens after 'typeof'.
  1415. ConsumeParen();
  1416. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1417. return TPResult::Error;
  1418. return TPResult::Ambiguous;
  1419. }
  1420. /// [ObjC] protocol-qualifiers:
  1421. //// '<' identifier-list '>'
  1422. Parser::TPResult Parser::TryParseProtocolQualifiers() {
  1423. assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
  1424. ConsumeToken();
  1425. do {
  1426. if (Tok.isNot(tok::identifier))
  1427. return TPResult::Error;
  1428. ConsumeToken();
  1429. if (Tok.is(tok::comma)) {
  1430. ConsumeToken();
  1431. continue;
  1432. }
  1433. if (Tok.is(tok::greater)) {
  1434. ConsumeToken();
  1435. return TPResult::Ambiguous;
  1436. }
  1437. } while (false);
  1438. return TPResult::Error;
  1439. }
  1440. /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
  1441. /// a constructor-style initializer, when parsing declaration statements.
  1442. /// Returns true for function declarator and false for constructor-style
  1443. /// initializer.
  1444. /// If during the disambiguation process a parsing error is encountered,
  1445. /// the function returns true to let the declaration parsing code handle it.
  1446. ///
  1447. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1448. /// exception-specification[opt]
  1449. ///
  1450. bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
  1451. // C++ 8.2p1:
  1452. // The ambiguity arising from the similarity between a function-style cast and
  1453. // a declaration mentioned in 6.8 can also occur in the context of a
  1454. // declaration. In that context, the choice is between a function declaration
  1455. // with a redundant set of parentheses around a parameter name and an object
  1456. // declaration with a function-style cast as the initializer. Just as for the
  1457. // ambiguities mentioned in 6.8, the resolution is to consider any construct
  1458. // that could possibly be a declaration a declaration.
  1459. TentativeParsingAction PA(*this);
  1460. ConsumeParen();
  1461. bool InvalidAsDeclaration = false;
  1462. TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
  1463. if (TPR == TPResult::Ambiguous) {
  1464. if (Tok.isNot(tok::r_paren))
  1465. TPR = TPResult::False;
  1466. else {
  1467. const Token &Next = NextToken();
  1468. if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
  1469. tok::kw_throw, tok::kw_noexcept, tok::l_square,
  1470. tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
  1471. isCXX11VirtSpecifier(Next))
  1472. // The next token cannot appear after a constructor-style initializer,
  1473. // and can appear next in a function definition. This must be a function
  1474. // declarator.
  1475. TPR = TPResult::True;
  1476. else if (InvalidAsDeclaration)
  1477. // Use the absence of 'typename' as a tie-breaker.
  1478. TPR = TPResult::False;
  1479. }
  1480. }
  1481. PA.Revert();
  1482. if (IsAmbiguous && TPR == TPResult::Ambiguous)
  1483. *IsAmbiguous = true;
  1484. // In case of an error, let the declaration parsing code handle it.
  1485. return TPR != TPResult::False;
  1486. }
  1487. /// parameter-declaration-clause:
  1488. /// parameter-declaration-list[opt] '...'[opt]
  1489. /// parameter-declaration-list ',' '...'
  1490. ///
  1491. /// parameter-declaration-list:
  1492. /// parameter-declaration
  1493. /// parameter-declaration-list ',' parameter-declaration
  1494. ///
  1495. /// parameter-declaration:
  1496. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1497. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1498. /// '=' assignment-expression
  1499. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1500. /// attributes[opt]
  1501. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1502. /// attributes[opt] '=' assignment-expression
  1503. ///
  1504. Parser::TPResult
  1505. Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
  1506. bool VersusTemplateArgument) {
  1507. if (Tok.is(tok::r_paren))
  1508. return TPResult::Ambiguous;
  1509. // parameter-declaration-list[opt] '...'[opt]
  1510. // parameter-declaration-list ',' '...'
  1511. //
  1512. // parameter-declaration-list:
  1513. // parameter-declaration
  1514. // parameter-declaration-list ',' parameter-declaration
  1515. //
  1516. while (1) {
  1517. // '...'[opt]
  1518. if (Tok.is(tok::ellipsis)) {
  1519. ConsumeToken();
  1520. if (Tok.is(tok::r_paren))
  1521. return TPResult::True; // '...)' is a sign of a function declarator.
  1522. else
  1523. return TPResult::False;
  1524. }
  1525. // An attribute-specifier-seq here is a sign of a function declarator.
  1526. if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
  1527. /*OuterMightBeMessageSend*/true))
  1528. return TPResult::True;
  1529. ParsedAttributes attrs(AttrFactory);
  1530. MaybeParseMicrosoftAttributes(attrs);
  1531. // decl-specifier-seq
  1532. // A parameter-declaration's initializer must be preceded by an '=', so
  1533. // decl-specifier-seq '{' is not a parameter in C++11.
  1534. TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
  1535. InvalidAsDeclaration);
  1536. if (VersusTemplateArgument && TPR == TPResult::True) {
  1537. // Consume the decl-specifier-seq. We have to look past it, since a
  1538. // type-id might appear here in a template argument.
  1539. bool SeenType = false;
  1540. do {
  1541. SeenType |= isCXXDeclarationSpecifierAType();
  1542. if (TryConsumeDeclarationSpecifier() == TPResult::Error)
  1543. return TPResult::Error;
  1544. // If we see a parameter name, this can't be a template argument.
  1545. if (SeenType && Tok.is(tok::identifier))
  1546. return TPResult::True;
  1547. TPR = isCXXDeclarationSpecifier(TPResult::False,
  1548. InvalidAsDeclaration);
  1549. if (TPR == TPResult::Error)
  1550. return TPR;
  1551. } while (TPR != TPResult::False);
  1552. } else if (TPR == TPResult::Ambiguous) {
  1553. // Disambiguate what follows the decl-specifier.
  1554. if (TryConsumeDeclarationSpecifier() == TPResult::Error)
  1555. return TPResult::Error;
  1556. } else
  1557. return TPR;
  1558. // declarator
  1559. // abstract-declarator[opt]
  1560. TPR = TryParseDeclarator(true/*mayBeAbstract*/);
  1561. if (TPR != TPResult::Ambiguous)
  1562. return TPR;
  1563. // [GNU] attributes[opt]
  1564. if (Tok.is(tok::kw___attribute))
  1565. return TPResult::True;
  1566. // If we're disambiguating a template argument in a default argument in
  1567. // a class definition versus a parameter declaration, an '=' here
  1568. // disambiguates the parse one way or the other.
  1569. // If this is a parameter, it must have a default argument because
  1570. // (a) the previous parameter did, and
  1571. // (b) this must be the first declaration of the function, so we can't
  1572. // inherit any default arguments from elsewhere.
  1573. // If we see an ')', then we've reached the end of a
  1574. // parameter-declaration-clause, and the last param is missing its default
  1575. // argument.
  1576. if (VersusTemplateArgument)
  1577. return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
  1578. : TPResult::False;
  1579. if (Tok.is(tok::equal)) {
  1580. // '=' assignment-expression
  1581. // Parse through assignment-expression.
  1582. // FIXME: assignment-expression may contain an unparenthesized comma.
  1583. if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
  1584. return TPResult::Error;
  1585. }
  1586. if (Tok.is(tok::ellipsis)) {
  1587. ConsumeToken();
  1588. if (Tok.is(tok::r_paren))
  1589. return TPResult::True; // '...)' is a sign of a function declarator.
  1590. else
  1591. return TPResult::False;
  1592. }
  1593. if (!TryConsumeToken(tok::comma))
  1594. break;
  1595. }
  1596. return TPResult::Ambiguous;
  1597. }
  1598. /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
  1599. /// parsing as a function declarator.
  1600. /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
  1601. /// return TPResult::Ambiguous, otherwise it will return either False() or
  1602. /// Error().
  1603. ///
  1604. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1605. /// exception-specification[opt]
  1606. ///
  1607. /// exception-specification:
  1608. /// 'throw' '(' type-id-list[opt] ')'
  1609. ///
  1610. Parser::TPResult Parser::TryParseFunctionDeclarator() {
  1611. // The '(' is already parsed.
  1612. TPResult TPR = TryParseParameterDeclarationClause();
  1613. if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
  1614. TPR = TPResult::False;
  1615. if (TPR == TPResult::False || TPR == TPResult::Error)
  1616. return TPR;
  1617. // Parse through the parens.
  1618. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1619. return TPResult::Error;
  1620. // cv-qualifier-seq
  1621. while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
  1622. ConsumeToken();
  1623. // ref-qualifier[opt]
  1624. if (Tok.isOneOf(tok::amp, tok::ampamp))
  1625. ConsumeToken();
  1626. // exception-specification
  1627. if (Tok.is(tok::kw_throw)) {
  1628. ConsumeToken();
  1629. if (Tok.isNot(tok::l_paren))
  1630. return TPResult::Error;
  1631. // Parse through the parens after 'throw'.
  1632. ConsumeParen();
  1633. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1634. return TPResult::Error;
  1635. }
  1636. if (Tok.is(tok::kw_noexcept)) {
  1637. ConsumeToken();
  1638. // Possibly an expression as well.
  1639. if (Tok.is(tok::l_paren)) {
  1640. // Find the matching rparen.
  1641. ConsumeParen();
  1642. if (!SkipUntil(tok::r_paren, StopAtSemi))
  1643. return TPResult::Error;
  1644. }
  1645. }
  1646. return TPResult::Ambiguous;
  1647. }
  1648. /// '[' constant-expression[opt] ']'
  1649. ///
  1650. Parser::TPResult Parser::TryParseBracketDeclarator() {
  1651. ConsumeBracket();
  1652. if (!SkipUntil(tok::r_square, StopAtSemi))
  1653. return TPResult::Error;
  1654. return TPResult::Ambiguous;
  1655. }