ParseTentative.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. /// simple-declaration:
  132. /// decl-specifier-seq init-declarator-list[opt] ';'
  133. ///
  134. /// (if AllowForRangeDecl specified)
  135. /// for ( for-range-declaration : for-range-initializer ) statement
  136. /// for-range-declaration:
  137. /// attribute-specifier-seqopt type-specifier-seq declarator
  138. ///
  139. Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
  140. if (Tok.is(tok::kw_typeof))
  141. TryParseTypeofSpecifier();
  142. else {
  143. if (Tok.is(tok::annot_cxxscope))
  144. ConsumeToken();
  145. ConsumeToken();
  146. if (getLangOpts().ObjC1 && Tok.is(tok::less))
  147. TryParseProtocolQualifiers();
  148. }
  149. // Two decl-specifiers in a row conclusively disambiguate this as being a
  150. // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
  151. // overwhelmingly common case that the next token is a '('.
  152. if (Tok.isNot(tok::l_paren)) {
  153. TPResult TPR = isCXXDeclarationSpecifier();
  154. if (TPR == TPResult::Ambiguous())
  155. return TPResult::True();
  156. if (TPR == TPResult::True() || TPR == TPResult::Error())
  157. return TPR;
  158. assert(TPR == TPResult::False());
  159. }
  160. TPResult TPR = TryParseInitDeclaratorList();
  161. if (TPR != TPResult::Ambiguous())
  162. return TPR;
  163. if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
  164. return TPResult::False();
  165. return TPResult::Ambiguous();
  166. }
  167. /// init-declarator-list:
  168. /// init-declarator
  169. /// init-declarator-list ',' init-declarator
  170. ///
  171. /// init-declarator:
  172. /// declarator initializer[opt]
  173. /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
  174. ///
  175. /// initializer:
  176. /// '=' initializer-clause
  177. /// '(' expression-list ')'
  178. ///
  179. /// initializer-clause:
  180. /// assignment-expression
  181. /// '{' initializer-list ','[opt] '}'
  182. /// '{' '}'
  183. ///
  184. Parser::TPResult Parser::TryParseInitDeclaratorList() {
  185. while (1) {
  186. // declarator
  187. TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
  188. if (TPR != TPResult::Ambiguous())
  189. return TPR;
  190. // [GNU] simple-asm-expr[opt] attributes[opt]
  191. if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
  192. return TPResult::True();
  193. // initializer[opt]
  194. if (Tok.is(tok::l_paren)) {
  195. // Parse through the parens.
  196. ConsumeParen();
  197. if (!SkipUntil(tok::r_paren))
  198. return TPResult::Error();
  199. } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
  200. // MSVC and g++ won't examine the rest of declarators if '=' is
  201. // encountered; they just conclude that we have a declaration.
  202. // EDG parses the initializer completely, which is the proper behavior
  203. // for this case.
  204. //
  205. // At present, Clang follows MSVC and g++, since the parser does not have
  206. // the ability to parse an expression fully without recording the
  207. // results of that parse.
  208. // Also allow 'in' after on objective-c declaration as in:
  209. // for (int (^b)(void) in array). Ideally this should be done in the
  210. // context of parsing for-init-statement of a foreach statement only. But,
  211. // in any other context 'in' is invalid after a declaration and parser
  212. // issues the error regardless of outcome of this decision.
  213. // FIXME. Change if above assumption does not hold.
  214. return TPResult::True();
  215. }
  216. if (Tok.isNot(tok::comma))
  217. break;
  218. ConsumeToken(); // the comma.
  219. }
  220. return TPResult::Ambiguous();
  221. }
  222. /// isCXXConditionDeclaration - Disambiguates between a declaration or an
  223. /// expression for a condition of a if/switch/while/for statement.
  224. /// If during the disambiguation process a parsing error is encountered,
  225. /// the function returns true to let the declaration parsing code handle it.
  226. ///
  227. /// condition:
  228. /// expression
  229. /// type-specifier-seq declarator '=' assignment-expression
  230. /// [C++11] type-specifier-seq declarator '=' initializer-clause
  231. /// [C++11] type-specifier-seq declarator braced-init-list
  232. /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
  233. /// '=' assignment-expression
  234. ///
  235. bool Parser::isCXXConditionDeclaration() {
  236. TPResult TPR = isCXXDeclarationSpecifier();
  237. if (TPR != TPResult::Ambiguous())
  238. return TPR != TPResult::False(); // Returns true for TPResult::True() or
  239. // TPResult::Error().
  240. // FIXME: Add statistics about the number of ambiguous statements encountered
  241. // and how they were resolved (number of declarations+number of expressions).
  242. // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
  243. // We need tentative parsing...
  244. TentativeParsingAction PA(*this);
  245. // type-specifier-seq
  246. if (Tok.is(tok::kw_typeof))
  247. TryParseTypeofSpecifier();
  248. else {
  249. ConsumeToken();
  250. if (getLangOpts().ObjC1 && Tok.is(tok::less))
  251. TryParseProtocolQualifiers();
  252. }
  253. assert(Tok.is(tok::l_paren) && "Expected '('");
  254. // declarator
  255. TPR = TryParseDeclarator(false/*mayBeAbstract*/);
  256. // In case of an error, let the declaration parsing code handle it.
  257. if (TPR == TPResult::Error())
  258. TPR = TPResult::True();
  259. if (TPR == TPResult::Ambiguous()) {
  260. // '='
  261. // [GNU] simple-asm-expr[opt] attributes[opt]
  262. if (Tok.is(tok::equal) ||
  263. Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
  264. TPR = TPResult::True();
  265. else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace))
  266. TPR = TPResult::True();
  267. else
  268. TPR = TPResult::False();
  269. }
  270. PA.Revert();
  271. assert(TPR == TPResult::True() || TPR == TPResult::False());
  272. return TPR == TPResult::True();
  273. }
  274. /// \brief Determine whether the next set of tokens contains a type-id.
  275. ///
  276. /// The context parameter states what context we're parsing right
  277. /// now, which affects how this routine copes with the token
  278. /// following the type-id. If the context is TypeIdInParens, we have
  279. /// already parsed the '(' and we will cease lookahead when we hit
  280. /// the corresponding ')'. If the context is
  281. /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
  282. /// before this template argument, and will cease lookahead when we
  283. /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
  284. /// and false for an expression. If during the disambiguation
  285. /// process a parsing error is encountered, the function returns
  286. /// true to let the declaration parsing code handle it.
  287. ///
  288. /// type-id:
  289. /// type-specifier-seq abstract-declarator[opt]
  290. ///
  291. bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
  292. isAmbiguous = false;
  293. // C++ 8.2p2:
  294. // The ambiguity arising from the similarity between a function-style cast and
  295. // a type-id can occur in different contexts. The ambiguity appears as a
  296. // choice between a function-style cast expression and a declaration of a
  297. // type. The resolution is that any construct that could possibly be a type-id
  298. // in its syntactic context shall be considered a type-id.
  299. TPResult TPR = isCXXDeclarationSpecifier();
  300. if (TPR != TPResult::Ambiguous())
  301. return TPR != TPResult::False(); // Returns true for TPResult::True() or
  302. // TPResult::Error().
  303. // FIXME: Add statistics about the number of ambiguous statements encountered
  304. // and how they were resolved (number of declarations+number of expressions).
  305. // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
  306. // We need tentative parsing...
  307. TentativeParsingAction PA(*this);
  308. // type-specifier-seq
  309. if (Tok.is(tok::kw_typeof))
  310. TryParseTypeofSpecifier();
  311. else {
  312. ConsumeToken();
  313. if (getLangOpts().ObjC1 && Tok.is(tok::less))
  314. TryParseProtocolQualifiers();
  315. }
  316. assert(Tok.is(tok::l_paren) && "Expected '('");
  317. // declarator
  318. TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
  319. // In case of an error, let the declaration parsing code handle it.
  320. if (TPR == TPResult::Error())
  321. TPR = TPResult::True();
  322. if (TPR == TPResult::Ambiguous()) {
  323. // We are supposed to be inside parens, so if after the abstract declarator
  324. // we encounter a ')' this is a type-id, otherwise it's an expression.
  325. if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
  326. TPR = TPResult::True();
  327. isAmbiguous = true;
  328. // We are supposed to be inside a template argument, so if after
  329. // the abstract declarator we encounter a '>', '>>' (in C++0x), or
  330. // ',', this is a type-id. Otherwise, it's an expression.
  331. } else if (Context == TypeIdAsTemplateArgument &&
  332. (Tok.is(tok::greater) || Tok.is(tok::comma) ||
  333. (getLangOpts().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
  334. TPR = TPResult::True();
  335. isAmbiguous = true;
  336. } else
  337. TPR = TPResult::False();
  338. }
  339. PA.Revert();
  340. assert(TPR == TPResult::True() || TPR == TPResult::False());
  341. return TPR == TPResult::True();
  342. }
  343. /// \brief Returns true if this is a C++11 attribute-specifier. Per
  344. /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
  345. /// always introduce an attribute. In Objective-C++11, this rule does not
  346. /// apply if either '[' begins a message-send.
  347. ///
  348. /// If Disambiguate is true, we try harder to determine whether a '[[' starts
  349. /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
  350. ///
  351. /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
  352. /// Obj-C message send or the start of an attribute. Otherwise, we assume it
  353. /// is not an Obj-C message send.
  354. ///
  355. /// C++11 [dcl.attr.grammar]:
  356. ///
  357. /// attribute-specifier:
  358. /// '[' '[' attribute-list ']' ']'
  359. /// alignment-specifier
  360. ///
  361. /// attribute-list:
  362. /// attribute[opt]
  363. /// attribute-list ',' attribute[opt]
  364. /// attribute '...'
  365. /// attribute-list ',' attribute '...'
  366. ///
  367. /// attribute:
  368. /// attribute-token attribute-argument-clause[opt]
  369. ///
  370. /// attribute-token:
  371. /// identifier
  372. /// identifier '::' identifier
  373. ///
  374. /// attribute-argument-clause:
  375. /// '(' balanced-token-seq ')'
  376. Parser::CXX11AttributeKind
  377. Parser::isCXX11AttributeSpecifier(bool Disambiguate,
  378. bool OuterMightBeMessageSend) {
  379. if (Tok.is(tok::kw_alignas))
  380. return CAK_AttributeSpecifier;
  381. if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
  382. return CAK_NotAttributeSpecifier;
  383. // No tentative parsing if we don't need to look for ']]' or a lambda.
  384. if (!Disambiguate && !getLangOpts().ObjC1)
  385. return CAK_AttributeSpecifier;
  386. TentativeParsingAction PA(*this);
  387. // Opening brackets were checked for above.
  388. ConsumeBracket();
  389. // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
  390. if (!getLangOpts().ObjC1) {
  391. ConsumeBracket();
  392. bool IsAttribute = SkipUntil(tok::r_square, false);
  393. IsAttribute &= Tok.is(tok::r_square);
  394. PA.Revert();
  395. return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
  396. }
  397. // In Obj-C++11, we need to distinguish four situations:
  398. // 1a) int x[[attr]]; C++11 attribute.
  399. // 1b) [[attr]]; C++11 statement attribute.
  400. // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
  401. // 3a) int x[[obj get]]; Message send in array size/index.
  402. // 3b) [[Class alloc] init]; Message send in message send.
  403. // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
  404. // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
  405. // If we have a lambda-introducer, then this is definitely not a message send.
  406. // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
  407. // into the tentative attribute parse below.
  408. LambdaIntroducer Intro;
  409. if (!TryParseLambdaIntroducer(Intro)) {
  410. // A lambda cannot end with ']]', and an attribute must.
  411. bool IsAttribute = Tok.is(tok::r_square);
  412. PA.Revert();
  413. if (IsAttribute)
  414. // Case 1: C++11 attribute.
  415. return CAK_AttributeSpecifier;
  416. if (OuterMightBeMessageSend)
  417. // Case 4: Lambda in message send.
  418. return CAK_NotAttributeSpecifier;
  419. // Case 2: Lambda in array size / index.
  420. return CAK_InvalidAttributeSpecifier;
  421. }
  422. ConsumeBracket();
  423. // If we don't have a lambda-introducer, then we have an attribute or a
  424. // message-send.
  425. bool IsAttribute = true;
  426. while (Tok.isNot(tok::r_square)) {
  427. if (Tok.is(tok::comma)) {
  428. // Case 1: Stray commas can only occur in attributes.
  429. PA.Revert();
  430. return CAK_AttributeSpecifier;
  431. }
  432. // Parse the attribute-token, if present.
  433. // C++11 [dcl.attr.grammar]:
  434. // If a keyword or an alternative token that satisfies the syntactic
  435. // requirements of an identifier is contained in an attribute-token,
  436. // it is considered an identifier.
  437. SourceLocation Loc;
  438. if (!TryParseCXX11AttributeIdentifier(Loc)) {
  439. IsAttribute = false;
  440. break;
  441. }
  442. if (Tok.is(tok::coloncolon)) {
  443. ConsumeToken();
  444. if (!TryParseCXX11AttributeIdentifier(Loc)) {
  445. IsAttribute = false;
  446. break;
  447. }
  448. }
  449. // Parse the attribute-argument-clause, if present.
  450. if (Tok.is(tok::l_paren)) {
  451. ConsumeParen();
  452. if (!SkipUntil(tok::r_paren, false)) {
  453. IsAttribute = false;
  454. break;
  455. }
  456. }
  457. if (Tok.is(tok::ellipsis))
  458. ConsumeToken();
  459. if (Tok.isNot(tok::comma))
  460. break;
  461. ConsumeToken();
  462. }
  463. // An attribute must end ']]'.
  464. if (IsAttribute) {
  465. if (Tok.is(tok::r_square)) {
  466. ConsumeBracket();
  467. IsAttribute = Tok.is(tok::r_square);
  468. } else {
  469. IsAttribute = false;
  470. }
  471. }
  472. PA.Revert();
  473. if (IsAttribute)
  474. // Case 1: C++11 statement attribute.
  475. return CAK_AttributeSpecifier;
  476. // Case 3: Message send.
  477. return CAK_NotAttributeSpecifier;
  478. }
  479. /// declarator:
  480. /// direct-declarator
  481. /// ptr-operator declarator
  482. ///
  483. /// direct-declarator:
  484. /// declarator-id
  485. /// direct-declarator '(' parameter-declaration-clause ')'
  486. /// cv-qualifier-seq[opt] exception-specification[opt]
  487. /// direct-declarator '[' constant-expression[opt] ']'
  488. /// '(' declarator ')'
  489. /// [GNU] '(' attributes declarator ')'
  490. ///
  491. /// abstract-declarator:
  492. /// ptr-operator abstract-declarator[opt]
  493. /// direct-abstract-declarator
  494. /// ...
  495. ///
  496. /// direct-abstract-declarator:
  497. /// direct-abstract-declarator[opt]
  498. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  499. /// exception-specification[opt]
  500. /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
  501. /// '(' abstract-declarator ')'
  502. ///
  503. /// ptr-operator:
  504. /// '*' cv-qualifier-seq[opt]
  505. /// '&'
  506. /// [C++0x] '&&' [TODO]
  507. /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
  508. ///
  509. /// cv-qualifier-seq:
  510. /// cv-qualifier cv-qualifier-seq[opt]
  511. ///
  512. /// cv-qualifier:
  513. /// 'const'
  514. /// 'volatile'
  515. ///
  516. /// declarator-id:
  517. /// '...'[opt] id-expression
  518. ///
  519. /// id-expression:
  520. /// unqualified-id
  521. /// qualified-id [TODO]
  522. ///
  523. /// unqualified-id:
  524. /// identifier
  525. /// operator-function-id [TODO]
  526. /// conversion-function-id [TODO]
  527. /// '~' class-name [TODO]
  528. /// template-id [TODO]
  529. ///
  530. Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
  531. bool mayHaveIdentifier) {
  532. // declarator:
  533. // direct-declarator
  534. // ptr-operator declarator
  535. while (1) {
  536. if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
  537. if (TryAnnotateCXXScopeToken(true))
  538. return TPResult::Error();
  539. if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
  540. Tok.is(tok::ampamp) ||
  541. (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
  542. // ptr-operator
  543. ConsumeToken();
  544. while (Tok.is(tok::kw_const) ||
  545. Tok.is(tok::kw_volatile) ||
  546. Tok.is(tok::kw_restrict))
  547. ConsumeToken();
  548. } else {
  549. break;
  550. }
  551. }
  552. // direct-declarator:
  553. // direct-abstract-declarator:
  554. if (Tok.is(tok::ellipsis))
  555. ConsumeToken();
  556. if ((Tok.is(tok::identifier) ||
  557. (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
  558. mayHaveIdentifier) {
  559. // declarator-id
  560. if (Tok.is(tok::annot_cxxscope))
  561. ConsumeToken();
  562. else
  563. TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
  564. ConsumeToken();
  565. } else if (Tok.is(tok::l_paren)) {
  566. ConsumeParen();
  567. if (mayBeAbstract &&
  568. (Tok.is(tok::r_paren) || // 'int()' is a function.
  569. // 'int(...)' is a function.
  570. (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
  571. isDeclarationSpecifier())) { // 'int(int)' is a function.
  572. // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  573. // exception-specification[opt]
  574. TPResult TPR = TryParseFunctionDeclarator();
  575. if (TPR != TPResult::Ambiguous())
  576. return TPR;
  577. } else {
  578. // '(' declarator ')'
  579. // '(' attributes declarator ')'
  580. // '(' abstract-declarator ')'
  581. if (Tok.is(tok::kw___attribute) ||
  582. Tok.is(tok::kw___declspec) ||
  583. Tok.is(tok::kw___cdecl) ||
  584. Tok.is(tok::kw___stdcall) ||
  585. Tok.is(tok::kw___fastcall) ||
  586. Tok.is(tok::kw___thiscall) ||
  587. Tok.is(tok::kw___unaligned))
  588. return TPResult::True(); // attributes indicate declaration
  589. TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
  590. if (TPR != TPResult::Ambiguous())
  591. return TPR;
  592. if (Tok.isNot(tok::r_paren))
  593. return TPResult::False();
  594. ConsumeParen();
  595. }
  596. } else if (!mayBeAbstract) {
  597. return TPResult::False();
  598. }
  599. while (1) {
  600. TPResult TPR(TPResult::Ambiguous());
  601. // abstract-declarator: ...
  602. if (Tok.is(tok::ellipsis))
  603. ConsumeToken();
  604. if (Tok.is(tok::l_paren)) {
  605. // Check whether we have a function declarator or a possible ctor-style
  606. // initializer that follows the declarator. Note that ctor-style
  607. // initializers are not possible in contexts where abstract declarators
  608. // are allowed.
  609. if (!mayBeAbstract && !isCXXFunctionDeclarator())
  610. break;
  611. // direct-declarator '(' parameter-declaration-clause ')'
  612. // cv-qualifier-seq[opt] exception-specification[opt]
  613. ConsumeParen();
  614. TPR = TryParseFunctionDeclarator();
  615. } else if (Tok.is(tok::l_square)) {
  616. // direct-declarator '[' constant-expression[opt] ']'
  617. // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
  618. TPR = TryParseBracketDeclarator();
  619. } else {
  620. break;
  621. }
  622. if (TPR != TPResult::Ambiguous())
  623. return TPR;
  624. }
  625. return TPResult::Ambiguous();
  626. }
  627. Parser::TPResult
  628. Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
  629. switch (Kind) {
  630. // Obviously starts an expression.
  631. case tok::numeric_constant:
  632. case tok::char_constant:
  633. case tok::wide_char_constant:
  634. case tok::utf16_char_constant:
  635. case tok::utf32_char_constant:
  636. case tok::string_literal:
  637. case tok::wide_string_literal:
  638. case tok::utf8_string_literal:
  639. case tok::utf16_string_literal:
  640. case tok::utf32_string_literal:
  641. case tok::l_square:
  642. case tok::l_paren:
  643. case tok::amp:
  644. case tok::ampamp:
  645. case tok::star:
  646. case tok::plus:
  647. case tok::plusplus:
  648. case tok::minus:
  649. case tok::minusminus:
  650. case tok::tilde:
  651. case tok::exclaim:
  652. case tok::kw_sizeof:
  653. case tok::kw___func__:
  654. case tok::kw_const_cast:
  655. case tok::kw_delete:
  656. case tok::kw_dynamic_cast:
  657. case tok::kw_false:
  658. case tok::kw_new:
  659. case tok::kw_operator:
  660. case tok::kw_reinterpret_cast:
  661. case tok::kw_static_cast:
  662. case tok::kw_this:
  663. case tok::kw_throw:
  664. case tok::kw_true:
  665. case tok::kw_typeid:
  666. case tok::kw_alignof:
  667. case tok::kw_noexcept:
  668. case tok::kw_nullptr:
  669. case tok::kw__Alignof:
  670. case tok::kw___null:
  671. case tok::kw___alignof:
  672. case tok::kw___builtin_choose_expr:
  673. case tok::kw___builtin_offsetof:
  674. case tok::kw___builtin_types_compatible_p:
  675. case tok::kw___builtin_va_arg:
  676. case tok::kw___imag:
  677. case tok::kw___real:
  678. case tok::kw___FUNCTION__:
  679. case tok::kw_L__FUNCTION__:
  680. case tok::kw___PRETTY_FUNCTION__:
  681. case tok::kw___has_nothrow_assign:
  682. case tok::kw___has_nothrow_copy:
  683. case tok::kw___has_nothrow_constructor:
  684. case tok::kw___has_trivial_assign:
  685. case tok::kw___has_trivial_copy:
  686. case tok::kw___has_trivial_constructor:
  687. case tok::kw___has_trivial_destructor:
  688. case tok::kw___has_virtual_destructor:
  689. case tok::kw___is_abstract:
  690. case tok::kw___is_base_of:
  691. case tok::kw___is_class:
  692. case tok::kw___is_convertible_to:
  693. case tok::kw___is_empty:
  694. case tok::kw___is_enum:
  695. case tok::kw___is_final:
  696. case tok::kw___is_literal:
  697. case tok::kw___is_literal_type:
  698. case tok::kw___is_pod:
  699. case tok::kw___is_polymorphic:
  700. case tok::kw___is_trivial:
  701. case tok::kw___is_trivially_assignable:
  702. case tok::kw___is_trivially_constructible:
  703. case tok::kw___is_trivially_copyable:
  704. case tok::kw___is_union:
  705. case tok::kw___uuidof:
  706. return TPResult::True();
  707. // Obviously starts a type-specifier-seq:
  708. case tok::kw_char:
  709. case tok::kw_const:
  710. case tok::kw_double:
  711. case tok::kw_enum:
  712. case tok::kw_half:
  713. case tok::kw_float:
  714. case tok::kw_int:
  715. case tok::kw_long:
  716. case tok::kw___int64:
  717. case tok::kw___int128:
  718. case tok::kw_restrict:
  719. case tok::kw_short:
  720. case tok::kw_signed:
  721. case tok::kw_struct:
  722. case tok::kw_union:
  723. case tok::kw_unsigned:
  724. case tok::kw_void:
  725. case tok::kw_volatile:
  726. case tok::kw__Bool:
  727. case tok::kw__Complex:
  728. case tok::kw_class:
  729. case tok::kw_typename:
  730. case tok::kw_wchar_t:
  731. case tok::kw_char16_t:
  732. case tok::kw_char32_t:
  733. case tok::kw___underlying_type:
  734. case tok::kw_thread_local:
  735. case tok::kw__Decimal32:
  736. case tok::kw__Decimal64:
  737. case tok::kw__Decimal128:
  738. case tok::kw___thread:
  739. case tok::kw_typeof:
  740. case tok::kw___cdecl:
  741. case tok::kw___stdcall:
  742. case tok::kw___fastcall:
  743. case tok::kw___thiscall:
  744. case tok::kw___unaligned:
  745. case tok::kw___vector:
  746. case tok::kw___pixel:
  747. case tok::kw__Atomic:
  748. return TPResult::False();
  749. default:
  750. break;
  751. }
  752. return TPResult::Ambiguous();
  753. }
  754. bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
  755. return std::find(TentativelyDeclaredIdentifiers.begin(),
  756. TentativelyDeclaredIdentifiers.end(), II)
  757. != TentativelyDeclaredIdentifiers.end();
  758. }
  759. /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
  760. /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
  761. /// be either a decl-specifier or a function-style cast, and TPResult::Error()
  762. /// if a parsing error was found and reported.
  763. ///
  764. /// If HasMissingTypename is provided, a name with a dependent scope specifier
  765. /// will be treated as ambiguous if the 'typename' keyword is missing. If this
  766. /// happens, *HasMissingTypename will be set to 'true'. This will also be used
  767. /// as an indicator that undeclared identifiers (which will trigger a later
  768. /// parse error) should be treated as types. Returns TPResult::Ambiguous() in
  769. /// such cases.
  770. ///
  771. /// decl-specifier:
  772. /// storage-class-specifier
  773. /// type-specifier
  774. /// function-specifier
  775. /// 'friend'
  776. /// 'typedef'
  777. /// [C++0x] 'constexpr'
  778. /// [GNU] attributes declaration-specifiers[opt]
  779. ///
  780. /// storage-class-specifier:
  781. /// 'register'
  782. /// 'static'
  783. /// 'extern'
  784. /// 'mutable'
  785. /// 'auto'
  786. /// [GNU] '__thread'
  787. ///
  788. /// function-specifier:
  789. /// 'inline'
  790. /// 'virtual'
  791. /// 'explicit'
  792. ///
  793. /// typedef-name:
  794. /// identifier
  795. ///
  796. /// type-specifier:
  797. /// simple-type-specifier
  798. /// class-specifier
  799. /// enum-specifier
  800. /// elaborated-type-specifier
  801. /// typename-specifier
  802. /// cv-qualifier
  803. ///
  804. /// simple-type-specifier:
  805. /// '::'[opt] nested-name-specifier[opt] type-name
  806. /// '::'[opt] nested-name-specifier 'template'
  807. /// simple-template-id [TODO]
  808. /// 'char'
  809. /// 'wchar_t'
  810. /// 'bool'
  811. /// 'short'
  812. /// 'int'
  813. /// 'long'
  814. /// 'signed'
  815. /// 'unsigned'
  816. /// 'float'
  817. /// 'double'
  818. /// 'void'
  819. /// [GNU] typeof-specifier
  820. /// [GNU] '_Complex'
  821. /// [C++0x] 'auto' [TODO]
  822. /// [C++0x] 'decltype' ( expression )
  823. ///
  824. /// type-name:
  825. /// class-name
  826. /// enum-name
  827. /// typedef-name
  828. ///
  829. /// elaborated-type-specifier:
  830. /// class-key '::'[opt] nested-name-specifier[opt] identifier
  831. /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
  832. /// simple-template-id
  833. /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
  834. ///
  835. /// enum-name:
  836. /// identifier
  837. ///
  838. /// enum-specifier:
  839. /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
  840. /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
  841. ///
  842. /// class-specifier:
  843. /// class-head '{' member-specification[opt] '}'
  844. ///
  845. /// class-head:
  846. /// class-key identifier[opt] base-clause[opt]
  847. /// class-key nested-name-specifier identifier base-clause[opt]
  848. /// class-key nested-name-specifier[opt] simple-template-id
  849. /// base-clause[opt]
  850. ///
  851. /// class-key:
  852. /// 'class'
  853. /// 'struct'
  854. /// 'union'
  855. ///
  856. /// cv-qualifier:
  857. /// 'const'
  858. /// 'volatile'
  859. /// [GNU] restrict
  860. ///
  861. Parser::TPResult
  862. Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
  863. bool *HasMissingTypename) {
  864. switch (Tok.getKind()) {
  865. case tok::identifier: {
  866. // Check for need to substitute AltiVec __vector keyword
  867. // for "vector" identifier.
  868. if (TryAltiVecVectorToken())
  869. return TPResult::True();
  870. const Token &Next = NextToken();
  871. // In 'foo bar', 'foo' is always a type name outside of Objective-C.
  872. if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
  873. return TPResult::True();
  874. if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
  875. // Determine whether this is a valid expression. If not, we will hit
  876. // a parse error one way or another. In that case, tell the caller that
  877. // this is ambiguous. Typo-correct to type and expression keywords and
  878. // to types and identifiers, in order to try to recover from errors.
  879. CorrectionCandidateCallback TypoCorrection;
  880. TypoCorrection.WantRemainingKeywords = false;
  881. switch (TryAnnotateName(false /* no nested name specifier */,
  882. &TypoCorrection)) {
  883. case ANK_Error:
  884. return TPResult::Error();
  885. case ANK_TentativeDecl:
  886. return TPResult::False();
  887. case ANK_TemplateName:
  888. // A bare type template-name which can't be a template template
  889. // argument is an error, and was probably intended to be a type.
  890. return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
  891. case ANK_Unresolved:
  892. return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False();
  893. case ANK_Success:
  894. break;
  895. }
  896. assert(Tok.isNot(tok::identifier) &&
  897. "TryAnnotateName succeeded without producing an annotation");
  898. } else {
  899. // This might possibly be a type with a dependent scope specifier and
  900. // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
  901. // since it will annotate as a primary expression, and we want to use the
  902. // "missing 'typename'" logic.
  903. if (TryAnnotateTypeOrScopeToken())
  904. return TPResult::Error();
  905. // If annotation failed, assume it's a non-type.
  906. // FIXME: If this happens due to an undeclared identifier, treat it as
  907. // ambiguous.
  908. if (Tok.is(tok::identifier))
  909. return TPResult::False();
  910. }
  911. // We annotated this token as something. Recurse to handle whatever we got.
  912. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  913. }
  914. case tok::kw_typename: // typename T::type
  915. // Annotate typenames and C++ scope specifiers. If we get one, just
  916. // recurse to handle whatever we get.
  917. if (TryAnnotateTypeOrScopeToken())
  918. return TPResult::Error();
  919. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  920. case tok::coloncolon: { // ::foo::bar
  921. const Token &Next = NextToken();
  922. if (Next.is(tok::kw_new) || // ::new
  923. Next.is(tok::kw_delete)) // ::delete
  924. return TPResult::False();
  925. }
  926. // Fall through.
  927. case tok::kw_decltype:
  928. // Annotate typenames and C++ scope specifiers. If we get one, just
  929. // recurse to handle whatever we get.
  930. if (TryAnnotateTypeOrScopeToken())
  931. return TPResult::Error();
  932. return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
  933. // decl-specifier:
  934. // storage-class-specifier
  935. // type-specifier
  936. // function-specifier
  937. // 'friend'
  938. // 'typedef'
  939. // 'constexpr'
  940. case tok::kw_friend:
  941. case tok::kw_typedef:
  942. case tok::kw_constexpr:
  943. // storage-class-specifier
  944. case tok::kw_register:
  945. case tok::kw_static:
  946. case tok::kw_extern:
  947. case tok::kw_mutable:
  948. case tok::kw_auto:
  949. case tok::kw___thread:
  950. // function-specifier
  951. case tok::kw_inline:
  952. case tok::kw_virtual:
  953. case tok::kw_explicit:
  954. // Modules
  955. case tok::kw___module_private__:
  956. // type-specifier:
  957. // simple-type-specifier
  958. // class-specifier
  959. // enum-specifier
  960. // elaborated-type-specifier
  961. // typename-specifier
  962. // cv-qualifier
  963. // class-specifier
  964. // elaborated-type-specifier
  965. case tok::kw_class:
  966. case tok::kw_struct:
  967. case tok::kw_union:
  968. // enum-specifier
  969. case tok::kw_enum:
  970. // cv-qualifier
  971. case tok::kw_const:
  972. case tok::kw_volatile:
  973. // GNU
  974. case tok::kw_restrict:
  975. case tok::kw__Complex:
  976. case tok::kw___attribute:
  977. return TPResult::True();
  978. // Microsoft
  979. case tok::kw___declspec:
  980. case tok::kw___cdecl:
  981. case tok::kw___stdcall:
  982. case tok::kw___fastcall:
  983. case tok::kw___thiscall:
  984. case tok::kw___w64:
  985. case tok::kw___ptr64:
  986. case tok::kw___ptr32:
  987. case tok::kw___forceinline:
  988. case tok::kw___unaligned:
  989. return TPResult::True();
  990. // Borland
  991. case tok::kw___pascal:
  992. return TPResult::True();
  993. // AltiVec
  994. case tok::kw___vector:
  995. return TPResult::True();
  996. case tok::annot_template_id: {
  997. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  998. if (TemplateId->Kind != TNK_Type_template)
  999. return TPResult::False();
  1000. CXXScopeSpec SS;
  1001. AnnotateTemplateIdTokenAsType();
  1002. assert(Tok.is(tok::annot_typename));
  1003. goto case_typename;
  1004. }
  1005. case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
  1006. // We've already annotated a scope; try to annotate a type.
  1007. if (TryAnnotateTypeOrScopeToken())
  1008. return TPResult::Error();
  1009. if (!Tok.is(tok::annot_typename)) {
  1010. // If the next token is an identifier or a type qualifier, then this
  1011. // can't possibly be a valid expression either.
  1012. if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
  1013. CXXScopeSpec SS;
  1014. Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
  1015. Tok.getAnnotationRange(),
  1016. SS);
  1017. if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
  1018. TentativeParsingAction PA(*this);
  1019. ConsumeToken();
  1020. ConsumeToken();
  1021. bool isIdentifier = Tok.is(tok::identifier);
  1022. TPResult TPR = TPResult::False();
  1023. if (!isIdentifier)
  1024. TPR = isCXXDeclarationSpecifier(BracedCastResult,
  1025. HasMissingTypename);
  1026. PA.Revert();
  1027. if (isIdentifier ||
  1028. TPR == TPResult::True() || TPR == TPResult::Error())
  1029. return TPResult::Error();
  1030. if (HasMissingTypename) {
  1031. // We can't tell whether this is a missing 'typename' or a valid
  1032. // expression.
  1033. *HasMissingTypename = true;
  1034. return TPResult::Ambiguous();
  1035. }
  1036. } else {
  1037. // Try to resolve the name. If it doesn't exist, assume it was
  1038. // intended to name a type and keep disambiguating.
  1039. switch (TryAnnotateName(false /* SS is not dependent */)) {
  1040. case ANK_Error:
  1041. return TPResult::Error();
  1042. case ANK_TentativeDecl:
  1043. return TPResult::False();
  1044. case ANK_TemplateName:
  1045. // A bare type template-name which can't be a template template
  1046. // argument is an error, and was probably intended to be a type.
  1047. return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
  1048. case ANK_Unresolved:
  1049. return HasMissingTypename ? TPResult::Ambiguous()
  1050. : TPResult::False();
  1051. case ANK_Success:
  1052. // Annotated it, check again.
  1053. assert(Tok.isNot(tok::annot_cxxscope) ||
  1054. NextToken().isNot(tok::identifier));
  1055. return isCXXDeclarationSpecifier(BracedCastResult,
  1056. HasMissingTypename);
  1057. }
  1058. }
  1059. }
  1060. return TPResult::False();
  1061. }
  1062. // If that succeeded, fallthrough into the generic simple-type-id case.
  1063. // The ambiguity resides in a simple-type-specifier/typename-specifier
  1064. // followed by a '('. The '(' could either be the start of:
  1065. //
  1066. // direct-declarator:
  1067. // '(' declarator ')'
  1068. //
  1069. // direct-abstract-declarator:
  1070. // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1071. // exception-specification[opt]
  1072. // '(' abstract-declarator ')'
  1073. //
  1074. // or part of a function-style cast expression:
  1075. //
  1076. // simple-type-specifier '(' expression-list[opt] ')'
  1077. //
  1078. // simple-type-specifier:
  1079. case tok::annot_typename:
  1080. case_typename:
  1081. // In Objective-C, we might have a protocol-qualified type.
  1082. if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
  1083. // Tentatively parse the
  1084. TentativeParsingAction PA(*this);
  1085. ConsumeToken(); // The type token
  1086. TPResult TPR = TryParseProtocolQualifiers();
  1087. bool isFollowedByParen = Tok.is(tok::l_paren);
  1088. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1089. PA.Revert();
  1090. if (TPR == TPResult::Error())
  1091. return TPResult::Error();
  1092. if (isFollowedByParen)
  1093. return TPResult::Ambiguous();
  1094. if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
  1095. return BracedCastResult;
  1096. return TPResult::True();
  1097. }
  1098. case tok::kw_char:
  1099. case tok::kw_wchar_t:
  1100. case tok::kw_char16_t:
  1101. case tok::kw_char32_t:
  1102. case tok::kw_bool:
  1103. case tok::kw_short:
  1104. case tok::kw_int:
  1105. case tok::kw_long:
  1106. case tok::kw___int64:
  1107. case tok::kw___int128:
  1108. case tok::kw_signed:
  1109. case tok::kw_unsigned:
  1110. case tok::kw_half:
  1111. case tok::kw_float:
  1112. case tok::kw_double:
  1113. case tok::kw_void:
  1114. case tok::annot_decltype:
  1115. if (NextToken().is(tok::l_paren))
  1116. return TPResult::Ambiguous();
  1117. // This is a function-style cast in all cases we disambiguate other than
  1118. // one:
  1119. // struct S {
  1120. // enum E : int { a = 4 }; // enum
  1121. // enum E : int { 4 }; // bit-field
  1122. // };
  1123. if (getLangOpts().CPlusPlus0x && NextToken().is(tok::l_brace))
  1124. return BracedCastResult;
  1125. if (isStartOfObjCClassMessageMissingOpenBracket())
  1126. return TPResult::False();
  1127. return TPResult::True();
  1128. // GNU typeof support.
  1129. case tok::kw_typeof: {
  1130. if (NextToken().isNot(tok::l_paren))
  1131. return TPResult::True();
  1132. TentativeParsingAction PA(*this);
  1133. TPResult TPR = TryParseTypeofSpecifier();
  1134. bool isFollowedByParen = Tok.is(tok::l_paren);
  1135. bool isFollowedByBrace = Tok.is(tok::l_brace);
  1136. PA.Revert();
  1137. if (TPR == TPResult::Error())
  1138. return TPResult::Error();
  1139. if (isFollowedByParen)
  1140. return TPResult::Ambiguous();
  1141. if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
  1142. return BracedCastResult;
  1143. return TPResult::True();
  1144. }
  1145. // C++0x type traits support
  1146. case tok::kw___underlying_type:
  1147. return TPResult::True();
  1148. // C11 _Atomic
  1149. case tok::kw__Atomic:
  1150. return TPResult::True();
  1151. default:
  1152. return TPResult::False();
  1153. }
  1154. }
  1155. /// [GNU] typeof-specifier:
  1156. /// 'typeof' '(' expressions ')'
  1157. /// 'typeof' '(' type-name ')'
  1158. ///
  1159. Parser::TPResult Parser::TryParseTypeofSpecifier() {
  1160. assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
  1161. ConsumeToken();
  1162. assert(Tok.is(tok::l_paren) && "Expected '('");
  1163. // Parse through the parens after 'typeof'.
  1164. ConsumeParen();
  1165. if (!SkipUntil(tok::r_paren))
  1166. return TPResult::Error();
  1167. return TPResult::Ambiguous();
  1168. }
  1169. /// [ObjC] protocol-qualifiers:
  1170. //// '<' identifier-list '>'
  1171. Parser::TPResult Parser::TryParseProtocolQualifiers() {
  1172. assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
  1173. ConsumeToken();
  1174. do {
  1175. if (Tok.isNot(tok::identifier))
  1176. return TPResult::Error();
  1177. ConsumeToken();
  1178. if (Tok.is(tok::comma)) {
  1179. ConsumeToken();
  1180. continue;
  1181. }
  1182. if (Tok.is(tok::greater)) {
  1183. ConsumeToken();
  1184. return TPResult::Ambiguous();
  1185. }
  1186. } while (false);
  1187. return TPResult::Error();
  1188. }
  1189. Parser::TPResult
  1190. Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) {
  1191. TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
  1192. HasMissingTypename);
  1193. if (TPR != TPResult::Ambiguous())
  1194. return TPR;
  1195. if (Tok.is(tok::kw_typeof))
  1196. TryParseTypeofSpecifier();
  1197. else {
  1198. if (Tok.is(tok::annot_cxxscope))
  1199. ConsumeToken();
  1200. ConsumeToken();
  1201. if (getLangOpts().ObjC1 && Tok.is(tok::less))
  1202. TryParseProtocolQualifiers();
  1203. }
  1204. return TPResult::Ambiguous();
  1205. }
  1206. /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
  1207. /// a constructor-style initializer, when parsing declaration statements.
  1208. /// Returns true for function declarator and false for constructor-style
  1209. /// initializer.
  1210. /// If during the disambiguation process a parsing error is encountered,
  1211. /// the function returns true to let the declaration parsing code handle it.
  1212. ///
  1213. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1214. /// exception-specification[opt]
  1215. ///
  1216. bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
  1217. // C++ 8.2p1:
  1218. // The ambiguity arising from the similarity between a function-style cast and
  1219. // a declaration mentioned in 6.8 can also occur in the context of a
  1220. // declaration. In that context, the choice is between a function declaration
  1221. // with a redundant set of parentheses around a parameter name and an object
  1222. // declaration with a function-style cast as the initializer. Just as for the
  1223. // ambiguities mentioned in 6.8, the resolution is to consider any construct
  1224. // that could possibly be a declaration a declaration.
  1225. TentativeParsingAction PA(*this);
  1226. ConsumeParen();
  1227. bool InvalidAsDeclaration = false;
  1228. TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
  1229. if (TPR == TPResult::Ambiguous()) {
  1230. if (Tok.isNot(tok::r_paren))
  1231. TPR = TPResult::False();
  1232. else {
  1233. const Token &Next = NextToken();
  1234. if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
  1235. Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
  1236. Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
  1237. Next.is(tok::l_square) || isCXX0XVirtSpecifier(Next) ||
  1238. Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
  1239. Next.is(tok::equal) || Next.is(tok::arrow))
  1240. // The next token cannot appear after a constructor-style initializer,
  1241. // and can appear next in a function definition. This must be a function
  1242. // declarator.
  1243. TPR = TPResult::True();
  1244. else if (InvalidAsDeclaration)
  1245. // Use the absence of 'typename' as a tie-breaker.
  1246. TPR = TPResult::False();
  1247. }
  1248. }
  1249. PA.Revert();
  1250. if (IsAmbiguous && TPR == TPResult::Ambiguous())
  1251. *IsAmbiguous = true;
  1252. // In case of an error, let the declaration parsing code handle it.
  1253. return TPR != TPResult::False();
  1254. }
  1255. /// parameter-declaration-clause:
  1256. /// parameter-declaration-list[opt] '...'[opt]
  1257. /// parameter-declaration-list ',' '...'
  1258. ///
  1259. /// parameter-declaration-list:
  1260. /// parameter-declaration
  1261. /// parameter-declaration-list ',' parameter-declaration
  1262. ///
  1263. /// parameter-declaration:
  1264. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1265. /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
  1266. /// '=' assignment-expression
  1267. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1268. /// attributes[opt]
  1269. /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
  1270. /// attributes[opt] '=' assignment-expression
  1271. ///
  1272. Parser::TPResult
  1273. Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) {
  1274. if (Tok.is(tok::r_paren))
  1275. return TPResult::Ambiguous();
  1276. // parameter-declaration-list[opt] '...'[opt]
  1277. // parameter-declaration-list ',' '...'
  1278. //
  1279. // parameter-declaration-list:
  1280. // parameter-declaration
  1281. // parameter-declaration-list ',' parameter-declaration
  1282. //
  1283. while (1) {
  1284. // '...'[opt]
  1285. if (Tok.is(tok::ellipsis)) {
  1286. ConsumeToken();
  1287. if (Tok.is(tok::r_paren))
  1288. return TPResult::True(); // '...)' is a sign of a function declarator.
  1289. else
  1290. return TPResult::False();
  1291. }
  1292. // An attribute-specifier-seq here is a sign of a function declarator.
  1293. if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
  1294. /*OuterMightBeMessageSend*/true))
  1295. return TPResult::True();
  1296. ParsedAttributes attrs(AttrFactory);
  1297. MaybeParseMicrosoftAttributes(attrs);
  1298. // decl-specifier-seq
  1299. // A parameter-declaration's initializer must be preceded by an '=', so
  1300. // decl-specifier-seq '{' is not a parameter in C++11.
  1301. TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration);
  1302. if (TPR != TPResult::Ambiguous())
  1303. return TPR;
  1304. // declarator
  1305. // abstract-declarator[opt]
  1306. TPR = TryParseDeclarator(true/*mayBeAbstract*/);
  1307. if (TPR != TPResult::Ambiguous())
  1308. return TPR;
  1309. // [GNU] attributes[opt]
  1310. if (Tok.is(tok::kw___attribute))
  1311. return TPResult::True();
  1312. if (Tok.is(tok::equal)) {
  1313. // '=' assignment-expression
  1314. // Parse through assignment-expression.
  1315. if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/,
  1316. true/*DontConsume*/))
  1317. return TPResult::Error();
  1318. }
  1319. if (Tok.is(tok::ellipsis)) {
  1320. ConsumeToken();
  1321. if (Tok.is(tok::r_paren))
  1322. return TPResult::True(); // '...)' is a sign of a function declarator.
  1323. else
  1324. return TPResult::False();
  1325. }
  1326. if (Tok.isNot(tok::comma))
  1327. break;
  1328. ConsumeToken(); // the comma.
  1329. }
  1330. return TPResult::Ambiguous();
  1331. }
  1332. /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
  1333. /// parsing as a function declarator.
  1334. /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
  1335. /// return TPResult::Ambiguous(), otherwise it will return either False() or
  1336. /// Error().
  1337. ///
  1338. /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
  1339. /// exception-specification[opt]
  1340. ///
  1341. /// exception-specification:
  1342. /// 'throw' '(' type-id-list[opt] ')'
  1343. ///
  1344. Parser::TPResult Parser::TryParseFunctionDeclarator() {
  1345. // The '(' is already parsed.
  1346. TPResult TPR = TryParseParameterDeclarationClause();
  1347. if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
  1348. TPR = TPResult::False();
  1349. if (TPR == TPResult::False() || TPR == TPResult::Error())
  1350. return TPR;
  1351. // Parse through the parens.
  1352. if (!SkipUntil(tok::r_paren))
  1353. return TPResult::Error();
  1354. // cv-qualifier-seq
  1355. while (Tok.is(tok::kw_const) ||
  1356. Tok.is(tok::kw_volatile) ||
  1357. Tok.is(tok::kw_restrict) )
  1358. ConsumeToken();
  1359. // ref-qualifier[opt]
  1360. if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
  1361. ConsumeToken();
  1362. // exception-specification
  1363. if (Tok.is(tok::kw_throw)) {
  1364. ConsumeToken();
  1365. if (Tok.isNot(tok::l_paren))
  1366. return TPResult::Error();
  1367. // Parse through the parens after 'throw'.
  1368. ConsumeParen();
  1369. if (!SkipUntil(tok::r_paren))
  1370. return TPResult::Error();
  1371. }
  1372. if (Tok.is(tok::kw_noexcept)) {
  1373. ConsumeToken();
  1374. // Possibly an expression as well.
  1375. if (Tok.is(tok::l_paren)) {
  1376. // Find the matching rparen.
  1377. ConsumeParen();
  1378. if (!SkipUntil(tok::r_paren))
  1379. return TPResult::Error();
  1380. }
  1381. }
  1382. return TPResult::Ambiguous();
  1383. }
  1384. /// '[' constant-expression[opt] ']'
  1385. ///
  1386. Parser::TPResult Parser::TryParseBracketDeclarator() {
  1387. ConsumeBracket();
  1388. if (!SkipUntil(tok::r_square))
  1389. return TPResult::Error();
  1390. return TPResult::Ambiguous();
  1391. }