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