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