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