Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParsedTemplate.h" |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 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 |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 33 | /// [C++0x] static_assert-declaration |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 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 | /// |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 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: |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 59 | // static_assert-declaration |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 60 | case tok::kw_static_assert: |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 61 | return true; |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 62 | // simple-declaration |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 63 | default: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 64 | return isCXXSimpleDeclaration(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// isCXXSimpleDeclaration - C++-specialized function that disambiguates |
| 69 | /// between a simple-declaration or an expression-statement. |
| 70 | /// If during the disambiguation process a parsing error is encountered, |
| 71 | /// the function returns true to let the declaration parsing code handle it. |
| 72 | /// Returns false if the statement is disambiguated as expression. |
| 73 | /// |
| 74 | /// simple-declaration: |
| 75 | /// decl-specifier-seq init-declarator-list[opt] ';' |
| 76 | /// |
| 77 | bool Parser::isCXXSimpleDeclaration() { |
| 78 | // C++ 6.8p1: |
| 79 | // There is an ambiguity in the grammar involving expression-statements and |
| 80 | // declarations: An expression-statement with a function-style explicit type |
| 81 | // conversion (5.2.3) as its leftmost subexpression can be indistinguishable |
| 82 | // from a declaration where the first declarator starts with a '('. In those |
| 83 | // cases the statement is a declaration. [Note: To disambiguate, the whole |
| 84 | // statement might have to be examined to determine if it is an |
| 85 | // expression-statement or a declaration]. |
| 86 | |
| 87 | // C++ 6.8p3: |
| 88 | // The disambiguation is purely syntactic; that is, the meaning of the names |
| 89 | // occurring in such a statement, beyond whether they are type-names or not, |
| 90 | // is not generally used in or changed by the disambiguation. Class |
| 91 | // templates are instantiated as necessary to determine if a qualified name |
| 92 | // is a type-name. Disambiguation precedes parsing, and a statement |
| 93 | // disambiguated as a declaration may be an ill-formed declaration. |
| 94 | |
| 95 | // We don't have to parse all of the decl-specifier-seq part. There's only |
| 96 | // an ambiguity if the first decl-specifier is |
| 97 | // simple-type-specifier/typename-specifier followed by a '(', which may |
| 98 | // indicate a function-style cast expression. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 99 | // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such |
| 100 | // a case. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 101 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 102 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 103 | if (TPR != TPResult::Ambiguous()) |
| 104 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 105 | // TPResult::Error(). |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 106 | |
| 107 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 108 | // and how they were resolved (number of declarations+number of expressions). |
| 109 | |
| 110 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 111 | // We need tentative parsing... |
| 112 | |
| 113 | TentativeParsingAction PA(*this); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 114 | TPR = TryParseSimpleDeclaration(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 115 | PA.Revert(); |
| 116 | |
| 117 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 118 | if (TPR == TPResult::Error()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 119 | return true; |
| 120 | |
| 121 | // Declarations take precedence over expressions. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 122 | if (TPR == TPResult::Ambiguous()) |
| 123 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 124 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 125 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 126 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// simple-declaration: |
| 130 | /// decl-specifier-seq init-declarator-list[opt] ';' |
| 131 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 132 | Parser::TPResult Parser::TryParseSimpleDeclaration() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 133 | // We know that we have a simple-type-specifier/typename-specifier followed |
| 134 | // by a '('. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 135 | assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 136 | |
| 137 | if (Tok.is(tok::kw_typeof)) |
| 138 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 139 | else { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 140 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 141 | |
| 142 | if (getLang().ObjC1 && Tok.is(tok::less)) |
| 143 | TryParseProtocolQualifiers(); |
| 144 | } |
| 145 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 146 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 147 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 148 | TPResult TPR = TryParseInitDeclaratorList(); |
| 149 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 150 | return TPR; |
| 151 | |
| 152 | if (Tok.isNot(tok::semi)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 153 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 154 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 155 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 158 | /// init-declarator-list: |
| 159 | /// init-declarator |
| 160 | /// init-declarator-list ',' init-declarator |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 161 | /// |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 162 | /// init-declarator: |
| 163 | /// declarator initializer[opt] |
| 164 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 165 | /// |
| 166 | /// initializer: |
| 167 | /// '=' initializer-clause |
| 168 | /// '(' expression-list ')' |
| 169 | /// |
| 170 | /// initializer-clause: |
| 171 | /// assignment-expression |
| 172 | /// '{' initializer-list ','[opt] '}' |
| 173 | /// '{' '}' |
| 174 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 175 | Parser::TPResult Parser::TryParseInitDeclaratorList() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 176 | while (1) { |
| 177 | // declarator |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 178 | TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 179 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 180 | return TPR; |
| 181 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 182 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 183 | if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 184 | return TPResult::True(); |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 185 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 186 | // initializer[opt] |
| 187 | if (Tok.is(tok::l_paren)) { |
| 188 | // Parse through the parens. |
| 189 | ConsumeParen(); |
| 190 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 191 | return TPResult::Error(); |
Fariborz Jahanian | f459beb | 2010-08-29 17:20:53 +0000 | [diff] [blame] | 192 | } else if (Tok.is(tok::equal) || isTokIdentifier_in()) { |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 193 | // MSVC and g++ won't examine the rest of declarators if '=' is |
| 194 | // encountered; they just conclude that we have a declaration. |
| 195 | // EDG parses the initializer completely, which is the proper behavior |
| 196 | // for this case. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 197 | // |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 198 | // At present, Clang follows MSVC and g++, since the parser does not have |
| 199 | // the ability to parse an expression fully without recording the |
| 200 | // results of that parse. |
Fariborz Jahanian | f459beb | 2010-08-29 17:20:53 +0000 | [diff] [blame] | 201 | // Also allow 'in' after on objective-c declaration as in: |
| 202 | // for (int (^b)(void) in array). Ideally this should be done in the |
| 203 | // context of parsing for-init-statement of a foreach statement only. But, |
| 204 | // in any other context 'in' is invalid after a declaration and parser |
| 205 | // issues the error regardless of outcome of this decision. |
| 206 | // FIXME. Change if above assumption does not hold. |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 207 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (Tok.isNot(tok::comma)) |
| 211 | break; |
| 212 | ConsumeToken(); // the comma. |
| 213 | } |
| 214 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 215 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 218 | /// isCXXConditionDeclaration - Disambiguates between a declaration or an |
| 219 | /// expression for a condition of a if/switch/while/for statement. |
| 220 | /// If during the disambiguation process a parsing error is encountered, |
| 221 | /// the function returns true to let the declaration parsing code handle it. |
| 222 | /// |
| 223 | /// condition: |
| 224 | /// expression |
| 225 | /// type-specifier-seq declarator '=' assignment-expression |
| 226 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 227 | /// '=' assignment-expression |
| 228 | /// |
| 229 | bool Parser::isCXXConditionDeclaration() { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 230 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 231 | if (TPR != TPResult::Ambiguous()) |
| 232 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 233 | // TPResult::Error(). |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 234 | |
| 235 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 236 | // and how they were resolved (number of declarations+number of expressions). |
| 237 | |
| 238 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 239 | // We need tentative parsing... |
| 240 | |
| 241 | TentativeParsingAction PA(*this); |
| 242 | |
| 243 | // type-specifier-seq |
| 244 | if (Tok.is(tok::kw_typeof)) |
| 245 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 246 | else { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 247 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 248 | |
| 249 | if (getLang().ObjC1 && Tok.is(tok::less)) |
| 250 | TryParseProtocolQualifiers(); |
| 251 | } |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 252 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 253 | |
| 254 | // declarator |
| 255 | TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 256 | |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 257 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 258 | if (TPR == TPResult::Error()) |
| 259 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 260 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 261 | if (TPR == TPResult::Ambiguous()) { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 262 | // '=' |
| 263 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 264 | if (Tok.is(tok::equal) || |
| 265 | Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 266 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 267 | else |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 268 | TPR = TPResult::False(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Argyrios Kyrtzidis | ca35baa | 2008-10-05 15:19:49 +0000 | [diff] [blame] | 271 | PA.Revert(); |
| 272 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 273 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 274 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | /// \brief Determine whether the next set of tokens contains a type-id. |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 278 | /// |
| 279 | /// The context parameter states what context we're parsing right |
| 280 | /// now, which affects how this routine copes with the token |
| 281 | /// following the type-id. If the context is TypeIdInParens, we have |
| 282 | /// already parsed the '(' and we will cease lookahead when we hit |
| 283 | /// the corresponding ')'. If the context is |
| 284 | /// TypeIdAsTemplateArgument, we've already parsed the '<' or ',' |
| 285 | /// before this template argument, and will cease lookahead when we |
| 286 | /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id |
| 287 | /// and false for an expression. If during the disambiguation |
| 288 | /// process a parsing error is encountered, the function returns |
| 289 | /// true to let the declaration parsing code handle it. |
| 290 | /// |
| 291 | /// type-id: |
| 292 | /// type-specifier-seq abstract-declarator[opt] |
| 293 | /// |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 294 | bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 296 | isAmbiguous = false; |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 297 | |
| 298 | // C++ 8.2p2: |
| 299 | // The ambiguity arising from the similarity between a function-style cast and |
| 300 | // a type-id can occur in different contexts. The ambiguity appears as a |
| 301 | // choice between a function-style cast expression and a declaration of a |
| 302 | // type. The resolution is that any construct that could possibly be a type-id |
| 303 | // in its syntactic context shall be considered a type-id. |
| 304 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 305 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 306 | if (TPR != TPResult::Ambiguous()) |
| 307 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 308 | // TPResult::Error(). |
| 309 | |
| 310 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 311 | // and how they were resolved (number of declarations+number of expressions). |
| 312 | |
| 313 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 314 | // We need tentative parsing... |
| 315 | |
| 316 | TentativeParsingAction PA(*this); |
| 317 | |
| 318 | // type-specifier-seq |
| 319 | if (Tok.is(tok::kw_typeof)) |
| 320 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 321 | else { |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 322 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 323 | |
| 324 | if (getLang().ObjC1 && Tok.is(tok::less)) |
| 325 | TryParseProtocolQualifiers(); |
| 326 | } |
| 327 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 328 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 329 | |
| 330 | // declarator |
| 331 | TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); |
| 332 | |
| 333 | // In case of an error, let the declaration parsing code handle it. |
| 334 | if (TPR == TPResult::Error()) |
| 335 | TPR = TPResult::True(); |
| 336 | |
| 337 | if (TPR == TPResult::Ambiguous()) { |
| 338 | // We are supposed to be inside parens, so if after the abstract declarator |
| 339 | // we encounter a ')' this is a type-id, otherwise it's an expression. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 340 | if (Context == TypeIdInParens && Tok.is(tok::r_paren)) { |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 341 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 342 | isAmbiguous = true; |
| 343 | |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 344 | // We are supposed to be inside a template argument, so if after |
| 345 | // the abstract declarator we encounter a '>', '>>' (in C++0x), or |
| 346 | // ',', this is a type-id. Otherwise, it's an expression. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 347 | } else if (Context == TypeIdAsTemplateArgument && |
| 348 | (Tok.is(tok::greater) || Tok.is(tok::comma) || |
| 349 | (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) { |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 350 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 351 | isAmbiguous = true; |
| 352 | |
| 353 | } else |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 354 | TPR = TPResult::False(); |
| 355 | } |
| 356 | |
| 357 | PA.Revert(); |
| 358 | |
| 359 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 360 | return TPR == TPResult::True(); |
| 361 | } |
| 362 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 363 | /// isCXX0XAttributeSpecifier - returns true if this is a C++0x |
| 364 | /// attribute-specifier. By default, unless in Obj-C++, only a cursory check is |
| 365 | /// performed that will simply return true if a [[ is seen. Currently C++ has no |
| 366 | /// syntactical ambiguities from this check, but it may inhibit error recovery. |
| 367 | /// If CheckClosing is true, a check is made for closing ]] brackets. |
| 368 | /// |
| 369 | /// If given, After is set to the token after the attribute-specifier so that |
| 370 | /// appropriate parsing decisions can be made; it is left untouched if false is |
| 371 | /// returned. |
| 372 | /// |
| 373 | /// FIXME: If an error is in the closing ]] brackets, the program assumes |
| 374 | /// the absence of an attribute-specifier, which can cause very yucky errors |
| 375 | /// to occur. |
| 376 | /// |
| 377 | /// [C++0x] attribute-specifier: |
| 378 | /// '[' '[' attribute-list ']' ']' |
| 379 | /// |
| 380 | /// [C++0x] attribute-list: |
| 381 | /// attribute[opt] |
| 382 | /// attribute-list ',' attribute[opt] |
| 383 | /// |
| 384 | /// [C++0x] attribute: |
| 385 | /// attribute-token attribute-argument-clause[opt] |
| 386 | /// |
| 387 | /// [C++0x] attribute-token: |
| 388 | /// identifier |
| 389 | /// attribute-scoped-token |
| 390 | /// |
| 391 | /// [C++0x] attribute-scoped-token: |
| 392 | /// attribute-namespace '::' identifier |
| 393 | /// |
| 394 | /// [C++0x] attribute-namespace: |
| 395 | /// identifier |
| 396 | /// |
| 397 | /// [C++0x] attribute-argument-clause: |
| 398 | /// '(' balanced-token-seq ')' |
| 399 | /// |
| 400 | /// [C++0x] balanced-token-seq: |
| 401 | /// balanced-token |
| 402 | /// balanced-token-seq balanced-token |
| 403 | /// |
| 404 | /// [C++0x] balanced-token: |
| 405 | /// '(' balanced-token-seq ')' |
| 406 | /// '[' balanced-token-seq ']' |
| 407 | /// '{' balanced-token-seq '}' |
| 408 | /// any token but '(', ')', '[', ']', '{', or '}' |
| 409 | bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing, |
| 410 | tok::TokenKind *After) { |
| 411 | if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square)) |
| 412 | return false; |
| 413 | |
| 414 | // No tentative parsing if we don't need to look for ]] |
| 415 | if (!CheckClosing && !getLang().ObjC1) |
| 416 | return true; |
| 417 | |
| 418 | struct TentativeReverter { |
| 419 | TentativeParsingAction PA; |
| 420 | |
| 421 | TentativeReverter (Parser& P) |
| 422 | : PA(P) |
| 423 | {} |
| 424 | ~TentativeReverter () { |
| 425 | PA.Revert(); |
| 426 | } |
| 427 | } R(*this); |
| 428 | |
| 429 | // Opening brackets were checked for above. |
| 430 | ConsumeBracket(); |
| 431 | ConsumeBracket(); |
| 432 | |
| 433 | // SkipUntil will handle balanced tokens, which are guaranteed in attributes. |
| 434 | SkipUntil(tok::r_square, false); |
| 435 | |
| 436 | if (Tok.isNot(tok::r_square)) |
| 437 | return false; |
| 438 | ConsumeBracket(); |
| 439 | |
| 440 | if (After) |
| 441 | *After = Tok.getKind(); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 446 | /// declarator: |
| 447 | /// direct-declarator |
| 448 | /// ptr-operator declarator |
| 449 | /// |
| 450 | /// direct-declarator: |
| 451 | /// declarator-id |
| 452 | /// direct-declarator '(' parameter-declaration-clause ')' |
| 453 | /// cv-qualifier-seq[opt] exception-specification[opt] |
| 454 | /// direct-declarator '[' constant-expression[opt] ']' |
| 455 | /// '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 456 | /// [GNU] '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 457 | /// |
| 458 | /// abstract-declarator: |
| 459 | /// ptr-operator abstract-declarator[opt] |
| 460 | /// direct-abstract-declarator |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 461 | /// ... |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 462 | /// |
| 463 | /// direct-abstract-declarator: |
| 464 | /// direct-abstract-declarator[opt] |
| 465 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 466 | /// exception-specification[opt] |
| 467 | /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 468 | /// '(' abstract-declarator ')' |
| 469 | /// |
| 470 | /// ptr-operator: |
| 471 | /// '*' cv-qualifier-seq[opt] |
| 472 | /// '&' |
| 473 | /// [C++0x] '&&' [TODO] |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 474 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 475 | /// |
| 476 | /// cv-qualifier-seq: |
| 477 | /// cv-qualifier cv-qualifier-seq[opt] |
| 478 | /// |
| 479 | /// cv-qualifier: |
| 480 | /// 'const' |
| 481 | /// 'volatile' |
| 482 | /// |
| 483 | /// declarator-id: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 484 | /// '...'[opt] id-expression |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// id-expression: |
| 487 | /// unqualified-id |
| 488 | /// qualified-id [TODO] |
| 489 | /// |
| 490 | /// unqualified-id: |
| 491 | /// identifier |
| 492 | /// operator-function-id [TODO] |
| 493 | /// conversion-function-id [TODO] |
| 494 | /// '~' class-name [TODO] |
| 495 | /// template-id [TODO] |
| 496 | /// |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 497 | Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, |
| 498 | bool mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 499 | // declarator: |
| 500 | // direct-declarator |
| 501 | // ptr-operator declarator |
| 502 | |
| 503 | while (1) { |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 504 | if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 505 | if (TryAnnotateCXXScopeToken(true)) |
| 506 | return TPResult::Error(); |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 508 | if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) || |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 509 | Tok.is(tok::ampamp) || |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 510 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 511 | // ptr-operator |
| 512 | ConsumeToken(); |
| 513 | while (Tok.is(tok::kw_const) || |
| 514 | Tok.is(tok::kw_volatile) || |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 515 | Tok.is(tok::kw_restrict)) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 516 | ConsumeToken(); |
| 517 | } else { |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // direct-declarator: |
| 523 | // direct-abstract-declarator: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 524 | if (Tok.is(tok::ellipsis)) |
| 525 | ConsumeToken(); |
| 526 | |
Argyrios Kyrtzidis | 1e05421 | 2009-07-21 17:05:03 +0000 | [diff] [blame] | 527 | if ((Tok.is(tok::identifier) || |
| 528 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) && |
| 529 | mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 530 | // declarator-id |
Argyrios Kyrtzidis | 1e05421 | 2009-07-21 17:05:03 +0000 | [diff] [blame] | 531 | if (Tok.is(tok::annot_cxxscope)) |
| 532 | ConsumeToken(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 533 | ConsumeToken(); |
| 534 | } else if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 535 | ConsumeParen(); |
| 536 | if (mayBeAbstract && |
| 537 | (Tok.is(tok::r_paren) || // 'int()' is a function. |
| 538 | Tok.is(tok::ellipsis) || // 'int(...)' is a function. |
| 539 | isDeclarationSpecifier())) { // 'int(int)' is a function. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 540 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 541 | // exception-specification[opt] |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 542 | TPResult TPR = TryParseFunctionDeclarator(); |
| 543 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 544 | return TPR; |
| 545 | } else { |
| 546 | // '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 547 | // '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 548 | // '(' abstract-declarator ')' |
Chris Lattner | 6229c8e | 2010-09-28 23:35:09 +0000 | [diff] [blame] | 549 | if (Tok.is(tok::kw___attribute) || |
| 550 | Tok.is(tok::kw___declspec) || |
| 551 | Tok.is(tok::kw___cdecl) || |
| 552 | Tok.is(tok::kw___stdcall) || |
| 553 | Tok.is(tok::kw___fastcall) || |
| 554 | Tok.is(tok::kw___thiscall)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 555 | return TPResult::True(); // attributes indicate declaration |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 556 | TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 557 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 558 | return TPR; |
| 559 | if (Tok.isNot(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 560 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 561 | ConsumeParen(); |
| 562 | } |
| 563 | } else if (!mayBeAbstract) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 564 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | while (1) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 568 | TPResult TPR(TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 570 | // abstract-declarator: ... |
| 571 | if (Tok.is(tok::ellipsis)) |
| 572 | ConsumeToken(); |
| 573 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 574 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 575 | // Check whether we have a function declarator or a possible ctor-style |
| 576 | // initializer that follows the declarator. Note that ctor-style |
| 577 | // initializers are not possible in contexts where abstract declarators |
| 578 | // are allowed. |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 579 | if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/)) |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 580 | break; |
| 581 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 582 | // direct-declarator '(' parameter-declaration-clause ')' |
| 583 | // cv-qualifier-seq[opt] exception-specification[opt] |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 584 | ConsumeParen(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 585 | TPR = TryParseFunctionDeclarator(); |
| 586 | } else if (Tok.is(tok::l_square)) { |
| 587 | // direct-declarator '[' constant-expression[opt] ']' |
| 588 | // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 589 | TPR = TryParseBracketDeclarator(); |
| 590 | } else { |
| 591 | break; |
| 592 | } |
| 593 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 594 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 595 | return TPR; |
| 596 | } |
| 597 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 598 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 601 | Parser::TPResult |
| 602 | Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) { |
| 603 | switch (Kind) { |
| 604 | // Obviously starts an expression. |
| 605 | case tok::numeric_constant: |
| 606 | case tok::char_constant: |
| 607 | case tok::string_literal: |
| 608 | case tok::wide_string_literal: |
| 609 | case tok::l_square: |
| 610 | case tok::l_paren: |
| 611 | case tok::amp: |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 612 | case tok::ampamp: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 613 | case tok::star: |
| 614 | case tok::plus: |
| 615 | case tok::plusplus: |
| 616 | case tok::minus: |
| 617 | case tok::minusminus: |
| 618 | case tok::tilde: |
| 619 | case tok::exclaim: |
| 620 | case tok::kw_sizeof: |
| 621 | case tok::kw___func__: |
| 622 | case tok::kw_const_cast: |
| 623 | case tok::kw_delete: |
| 624 | case tok::kw_dynamic_cast: |
| 625 | case tok::kw_false: |
| 626 | case tok::kw_new: |
| 627 | case tok::kw_operator: |
| 628 | case tok::kw_reinterpret_cast: |
| 629 | case tok::kw_static_cast: |
| 630 | case tok::kw_this: |
| 631 | case tok::kw_throw: |
| 632 | case tok::kw_true: |
| 633 | case tok::kw_typeid: |
| 634 | case tok::kw_alignof: |
| 635 | case tok::kw_noexcept: |
| 636 | case tok::kw_nullptr: |
| 637 | case tok::kw___null: |
| 638 | case tok::kw___alignof: |
| 639 | case tok::kw___builtin_choose_expr: |
| 640 | case tok::kw___builtin_offsetof: |
| 641 | case tok::kw___builtin_types_compatible_p: |
| 642 | case tok::kw___builtin_va_arg: |
| 643 | case tok::kw___imag: |
| 644 | case tok::kw___real: |
| 645 | case tok::kw___FUNCTION__: |
| 646 | case tok::kw___PRETTY_FUNCTION__: |
| 647 | case tok::kw___has_nothrow_assign: |
| 648 | case tok::kw___has_nothrow_copy: |
| 649 | case tok::kw___has_nothrow_constructor: |
| 650 | case tok::kw___has_trivial_assign: |
| 651 | case tok::kw___has_trivial_copy: |
| 652 | case tok::kw___has_trivial_constructor: |
| 653 | case tok::kw___has_trivial_destructor: |
| 654 | case tok::kw___has_virtual_destructor: |
| 655 | case tok::kw___is_abstract: |
| 656 | case tok::kw___is_base_of: |
| 657 | case tok::kw___is_class: |
| 658 | case tok::kw___is_empty: |
| 659 | case tok::kw___is_enum: |
| 660 | case tok::kw___is_pod: |
| 661 | case tok::kw___is_polymorphic: |
| 662 | case tok::kw___is_union: |
| 663 | case tok::kw___is_literal: |
| 664 | case tok::kw___uuidof: |
| 665 | return TPResult::True(); |
| 666 | |
| 667 | // Obviously starts a type-specifier-seq: |
| 668 | case tok::kw_char: |
| 669 | case tok::kw_const: |
| 670 | case tok::kw_double: |
| 671 | case tok::kw_enum: |
| 672 | case tok::kw_float: |
| 673 | case tok::kw_int: |
| 674 | case tok::kw_long: |
| 675 | case tok::kw_restrict: |
| 676 | case tok::kw_short: |
| 677 | case tok::kw_signed: |
| 678 | case tok::kw_struct: |
| 679 | case tok::kw_union: |
| 680 | case tok::kw_unsigned: |
| 681 | case tok::kw_void: |
| 682 | case tok::kw_volatile: |
| 683 | case tok::kw__Bool: |
| 684 | case tok::kw__Complex: |
| 685 | case tok::kw_class: |
| 686 | case tok::kw_typename: |
| 687 | case tok::kw_wchar_t: |
| 688 | case tok::kw_char16_t: |
| 689 | case tok::kw_char32_t: |
| 690 | case tok::kw_decltype: |
| 691 | case tok::kw_thread_local: |
| 692 | case tok::kw__Decimal32: |
| 693 | case tok::kw__Decimal64: |
| 694 | case tok::kw__Decimal128: |
| 695 | case tok::kw___thread: |
| 696 | case tok::kw_typeof: |
| 697 | case tok::kw___cdecl: |
| 698 | case tok::kw___stdcall: |
| 699 | case tok::kw___fastcall: |
| 700 | case tok::kw___thiscall: |
| 701 | case tok::kw___vector: |
| 702 | case tok::kw___pixel: |
| 703 | return TPResult::False(); |
| 704 | |
| 705 | default: |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | return TPResult::Ambiguous(); |
| 710 | } |
| 711 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 712 | /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration |
| 713 | /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could |
| 714 | /// be either a decl-specifier or a function-style cast, and TPResult::Error() |
| 715 | /// if a parsing error was found and reported. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 716 | /// |
| 717 | /// decl-specifier: |
| 718 | /// storage-class-specifier |
| 719 | /// type-specifier |
| 720 | /// function-specifier |
| 721 | /// 'friend' |
| 722 | /// 'typedef' |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 723 | /// [C++0x] 'constexpr' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 724 | /// [GNU] attributes declaration-specifiers[opt] |
| 725 | /// |
| 726 | /// storage-class-specifier: |
| 727 | /// 'register' |
| 728 | /// 'static' |
| 729 | /// 'extern' |
| 730 | /// 'mutable' |
| 731 | /// 'auto' |
| 732 | /// [GNU] '__thread' |
| 733 | /// |
| 734 | /// function-specifier: |
| 735 | /// 'inline' |
| 736 | /// 'virtual' |
| 737 | /// 'explicit' |
| 738 | /// |
| 739 | /// typedef-name: |
| 740 | /// identifier |
| 741 | /// |
| 742 | /// type-specifier: |
| 743 | /// simple-type-specifier |
| 744 | /// class-specifier |
| 745 | /// enum-specifier |
| 746 | /// elaborated-type-specifier |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 747 | /// typename-specifier |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 748 | /// cv-qualifier |
| 749 | /// |
| 750 | /// simple-type-specifier: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 751 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 752 | /// '::'[opt] nested-name-specifier 'template' |
| 753 | /// simple-template-id [TODO] |
| 754 | /// 'char' |
| 755 | /// 'wchar_t' |
| 756 | /// 'bool' |
| 757 | /// 'short' |
| 758 | /// 'int' |
| 759 | /// 'long' |
| 760 | /// 'signed' |
| 761 | /// 'unsigned' |
| 762 | /// 'float' |
| 763 | /// 'double' |
| 764 | /// 'void' |
| 765 | /// [GNU] typeof-specifier |
| 766 | /// [GNU] '_Complex' |
| 767 | /// [C++0x] 'auto' [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 768 | /// [C++0x] 'decltype' ( expression ) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 769 | /// |
| 770 | /// type-name: |
| 771 | /// class-name |
| 772 | /// enum-name |
| 773 | /// typedef-name |
| 774 | /// |
| 775 | /// elaborated-type-specifier: |
| 776 | /// class-key '::'[opt] nested-name-specifier[opt] identifier |
| 777 | /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] |
| 778 | /// simple-template-id |
| 779 | /// 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 780 | /// |
| 781 | /// enum-name: |
| 782 | /// identifier |
| 783 | /// |
| 784 | /// enum-specifier: |
| 785 | /// 'enum' identifier[opt] '{' enumerator-list[opt] '}' |
| 786 | /// 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 787 | /// |
| 788 | /// class-specifier: |
| 789 | /// class-head '{' member-specification[opt] '}' |
| 790 | /// |
| 791 | /// class-head: |
| 792 | /// class-key identifier[opt] base-clause[opt] |
| 793 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 794 | /// class-key nested-name-specifier[opt] simple-template-id |
| 795 | /// base-clause[opt] |
| 796 | /// |
| 797 | /// class-key: |
| 798 | /// 'class' |
| 799 | /// 'struct' |
| 800 | /// 'union' |
| 801 | /// |
| 802 | /// cv-qualifier: |
| 803 | /// 'const' |
| 804 | /// 'volatile' |
| 805 | /// [GNU] restrict |
| 806 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 807 | Parser::TPResult Parser::isCXXDeclarationSpecifier() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 808 | switch (Tok.getKind()) { |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 809 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 810 | // Check for need to substitute AltiVec __vector keyword |
| 811 | // for "vector" identifier. |
| 812 | if (TryAltiVecVectorToken()) |
| 813 | return TPResult::True(); |
| 814 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 815 | case tok::kw_typename: // typename T::type |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 816 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 817 | // recurse to handle whatever we get. |
| 818 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 819 | return TPResult::Error(); |
| 820 | if (Tok.is(tok::identifier)) |
| 821 | return TPResult::False(); |
| 822 | return isCXXDeclarationSpecifier(); |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 2ee9b40 | 2009-12-19 01:11:05 +0000 | [diff] [blame] | 824 | case tok::coloncolon: { // ::foo::bar |
| 825 | const Token &Next = NextToken(); |
| 826 | if (Next.is(tok::kw_new) || // ::new |
| 827 | Next.is(tok::kw_delete)) // ::delete |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 828 | return TPResult::False(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 830 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 831 | // recurse to handle whatever we get. |
| 832 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 833 | return TPResult::Error(); |
| 834 | return isCXXDeclarationSpecifier(); |
Chris Lattner | 2ee9b40 | 2009-12-19 01:11:05 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 837 | // decl-specifier: |
| 838 | // storage-class-specifier |
| 839 | // type-specifier |
| 840 | // function-specifier |
| 841 | // 'friend' |
| 842 | // 'typedef' |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 843 | // 'constexpr' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 844 | case tok::kw_friend: |
| 845 | case tok::kw_typedef: |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 846 | case tok::kw_constexpr: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 847 | // storage-class-specifier |
| 848 | case tok::kw_register: |
| 849 | case tok::kw_static: |
| 850 | case tok::kw_extern: |
| 851 | case tok::kw_mutable: |
| 852 | case tok::kw_auto: |
| 853 | case tok::kw___thread: |
| 854 | // function-specifier |
| 855 | case tok::kw_inline: |
| 856 | case tok::kw_virtual: |
| 857 | case tok::kw_explicit: |
| 858 | |
| 859 | // type-specifier: |
| 860 | // simple-type-specifier |
| 861 | // class-specifier |
| 862 | // enum-specifier |
| 863 | // elaborated-type-specifier |
| 864 | // typename-specifier |
| 865 | // cv-qualifier |
| 866 | |
| 867 | // class-specifier |
| 868 | // elaborated-type-specifier |
| 869 | case tok::kw_class: |
| 870 | case tok::kw_struct: |
| 871 | case tok::kw_union: |
| 872 | // enum-specifier |
| 873 | case tok::kw_enum: |
| 874 | // cv-qualifier |
| 875 | case tok::kw_const: |
| 876 | case tok::kw_volatile: |
| 877 | |
| 878 | // GNU |
| 879 | case tok::kw_restrict: |
| 880 | case tok::kw__Complex: |
| 881 | case tok::kw___attribute: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 882 | return TPResult::True(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 884 | // Microsoft |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 885 | case tok::kw___declspec: |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 886 | case tok::kw___cdecl: |
| 887 | case tok::kw___stdcall: |
| 888 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 889 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 890 | case tok::kw___w64: |
| 891 | case tok::kw___ptr64: |
| 892 | case tok::kw___forceinline: |
| 893 | return TPResult::True(); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 894 | |
| 895 | // Borland |
| 896 | case tok::kw___pascal: |
| 897 | return TPResult::True(); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 898 | |
| 899 | // AltiVec |
| 900 | case tok::kw___vector: |
| 901 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 902 | |
John McCall | 51e2a5d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 903 | case tok::annot_template_id: { |
| 904 | TemplateIdAnnotation *TemplateId |
| 905 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 906 | if (TemplateId->Kind != TNK_Type_template) |
| 907 | return TPResult::False(); |
| 908 | CXXScopeSpec SS; |
| 909 | AnnotateTemplateIdTokenAsType(&SS); |
| 910 | assert(Tok.is(tok::annot_typename)); |
| 911 | goto case_typename; |
| 912 | } |
| 913 | |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 914 | case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed |
| 915 | // We've already annotated a scope; try to annotate a type. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 916 | if (TryAnnotateTypeOrScopeToken()) |
| 917 | return TPResult::Error(); |
| 918 | if (!Tok.is(tok::annot_typename)) |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 919 | return TPResult::False(); |
| 920 | // If that succeeded, fallthrough into the generic simple-type-id case. |
| 921 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 922 | // The ambiguity resides in a simple-type-specifier/typename-specifier |
| 923 | // followed by a '('. The '(' could either be the start of: |
| 924 | // |
| 925 | // direct-declarator: |
| 926 | // '(' declarator ')' |
| 927 | // |
| 928 | // direct-abstract-declarator: |
| 929 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 930 | // exception-specification[opt] |
| 931 | // '(' abstract-declarator ')' |
| 932 | // |
| 933 | // or part of a function-style cast expression: |
| 934 | // |
| 935 | // simple-type-specifier '(' expression-list[opt] ')' |
| 936 | // |
| 937 | |
| 938 | // simple-type-specifier: |
| 939 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 940 | case tok::annot_typename: |
| 941 | case_typename: |
| 942 | // In Objective-C, we might have a protocol-qualified type. |
| 943 | if (getLang().ObjC1 && NextToken().is(tok::less)) { |
| 944 | // Tentatively parse the |
| 945 | TentativeParsingAction PA(*this); |
| 946 | ConsumeToken(); // The type token |
| 947 | |
| 948 | TPResult TPR = TryParseProtocolQualifiers(); |
| 949 | bool isFollowedByParen = Tok.is(tok::l_paren); |
| 950 | |
| 951 | PA.Revert(); |
| 952 | |
| 953 | if (TPR == TPResult::Error()) |
| 954 | return TPResult::Error(); |
| 955 | |
| 956 | if (isFollowedByParen) |
| 957 | return TPResult::Ambiguous(); |
| 958 | |
| 959 | return TPResult::True(); |
| 960 | } |
| 961 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 962 | case tok::kw_char: |
| 963 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 964 | case tok::kw_char16_t: |
| 965 | case tok::kw_char32_t: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 966 | case tok::kw_bool: |
| 967 | case tok::kw_short: |
| 968 | case tok::kw_int: |
| 969 | case tok::kw_long: |
| 970 | case tok::kw_signed: |
| 971 | case tok::kw_unsigned: |
| 972 | case tok::kw_float: |
| 973 | case tok::kw_double: |
| 974 | case tok::kw_void: |
| 975 | if (NextToken().is(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 976 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 977 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 978 | if (isStartOfObjCClassMessageMissingOpenBracket()) |
| 979 | return TPResult::False(); |
| 980 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 981 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 982 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 983 | // GNU typeof support. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 984 | case tok::kw_typeof: { |
| 985 | if (NextToken().isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 986 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 987 | |
| 988 | TentativeParsingAction PA(*this); |
| 989 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 990 | TPResult TPR = TryParseTypeofSpecifier(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 991 | bool isFollowedByParen = Tok.is(tok::l_paren); |
| 992 | |
| 993 | PA.Revert(); |
| 994 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 995 | if (TPR == TPResult::Error()) |
| 996 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 997 | |
| 998 | if (isFollowedByParen) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 999 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1000 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1001 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1004 | // C++0x decltype support. |
| 1005 | case tok::kw_decltype: |
| 1006 | return TPResult::True(); |
| 1007 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1008 | default: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1009 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | /// [GNU] typeof-specifier: |
| 1014 | /// 'typeof' '(' expressions ')' |
| 1015 | /// 'typeof' '(' type-name ')' |
| 1016 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1017 | Parser::TPResult Parser::TryParseTypeofSpecifier() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1018 | assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); |
| 1019 | ConsumeToken(); |
| 1020 | |
| 1021 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 1022 | // Parse through the parens after 'typeof'. |
| 1023 | ConsumeParen(); |
| 1024 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1025 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1026 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1027 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1030 | /// [ObjC] protocol-qualifiers: |
| 1031 | //// '<' identifier-list '>' |
| 1032 | Parser::TPResult Parser::TryParseProtocolQualifiers() { |
| 1033 | assert(Tok.is(tok::less) && "Expected '<' for qualifier list"); |
| 1034 | ConsumeToken(); |
| 1035 | do { |
| 1036 | if (Tok.isNot(tok::identifier)) |
| 1037 | return TPResult::Error(); |
| 1038 | ConsumeToken(); |
| 1039 | |
| 1040 | if (Tok.is(tok::comma)) { |
| 1041 | ConsumeToken(); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | if (Tok.is(tok::greater)) { |
| 1046 | ConsumeToken(); |
| 1047 | return TPResult::Ambiguous(); |
| 1048 | } |
| 1049 | } while (false); |
| 1050 | |
| 1051 | return TPResult::Error(); |
| 1052 | } |
| 1053 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1054 | Parser::TPResult Parser::TryParseDeclarationSpecifier() { |
| 1055 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 1056 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1057 | return TPR; |
| 1058 | |
| 1059 | if (Tok.is(tok::kw_typeof)) |
| 1060 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1061 | else { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1062 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (getLang().ObjC1 && Tok.is(tok::less)) |
| 1065 | TryParseProtocolQualifiers(); |
| 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1068 | assert(Tok.is(tok::l_paren) && "Expected '('!"); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1069 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /// isCXXFunctionDeclarator - Disambiguates between a function declarator or |
| 1073 | /// a constructor-style initializer, when parsing declaration statements. |
| 1074 | /// Returns true for function declarator and false for constructor-style |
| 1075 | /// initializer. |
| 1076 | /// If during the disambiguation process a parsing error is encountered, |
| 1077 | /// the function returns true to let the declaration parsing code handle it. |
| 1078 | /// |
| 1079 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 1080 | /// exception-specification[opt] |
| 1081 | /// |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 1082 | bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) { |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 1083 | |
| 1084 | // C++ 8.2p1: |
| 1085 | // The ambiguity arising from the similarity between a function-style cast and |
| 1086 | // a declaration mentioned in 6.8 can also occur in the context of a |
| 1087 | // declaration. In that context, the choice is between a function declaration |
| 1088 | // with a redundant set of parentheses around a parameter name and an object |
| 1089 | // declaration with a function-style cast as the initializer. Just as for the |
| 1090 | // ambiguities mentioned in 6.8, the resolution is to consider any construct |
| 1091 | // that could possibly be a declaration a declaration. |
| 1092 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1093 | TentativeParsingAction PA(*this); |
| 1094 | |
| 1095 | ConsumeParen(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1096 | TPResult TPR = TryParseParameterDeclarationClause(); |
| 1097 | if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) |
| 1098 | TPR = TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1099 | |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 1100 | SourceLocation TPLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1101 | PA.Revert(); |
| 1102 | |
| 1103 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1104 | if (TPR == TPResult::Error()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1105 | return true; |
| 1106 | |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 1107 | if (TPR == TPResult::Ambiguous()) { |
| 1108 | // Function declarator has precedence over constructor-style initializer. |
| 1109 | // Emit a warning just in case the author intended a variable definition. |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 1110 | if (warnIfAmbiguous) |
Chris Lattner | ef708fd | 2008-11-18 07:50:21 +0000 | [diff] [blame] | 1111 | Diag(Tok, diag::warn_parens_disambiguated_as_function_decl) |
| 1112 | << SourceRange(Tok.getLocation(), TPLoc); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1113 | return true; |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 1114 | } |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1115 | |
| 1116 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /// parameter-declaration-clause: |
| 1120 | /// parameter-declaration-list[opt] '...'[opt] |
| 1121 | /// parameter-declaration-list ',' '...' |
| 1122 | /// |
| 1123 | /// parameter-declaration-list: |
| 1124 | /// parameter-declaration |
| 1125 | /// parameter-declaration-list ',' parameter-declaration |
| 1126 | /// |
| 1127 | /// parameter-declaration: |
Argyrios Kyrtzidis | 346af03 | 2010-12-08 02:02:46 +0000 | [diff] [blame] | 1128 | /// decl-specifier-seq declarator attributes[opt] |
| 1129 | /// decl-specifier-seq declarator attributes[opt] '=' assignment-expression |
| 1130 | /// decl-specifier-seq abstract-declarator[opt] attributes[opt] |
| 1131 | /// decl-specifier-seq abstract-declarator[opt] attributes[opt] |
| 1132 | /// '=' assignment-expression |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1133 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1134 | Parser::TPResult Parser::TryParseParameterDeclarationClause() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1135 | |
| 1136 | if (Tok.is(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1137 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1138 | |
| 1139 | // parameter-declaration-list[opt] '...'[opt] |
| 1140 | // parameter-declaration-list ',' '...' |
| 1141 | // |
| 1142 | // parameter-declaration-list: |
| 1143 | // parameter-declaration |
| 1144 | // parameter-declaration-list ',' parameter-declaration |
| 1145 | // |
| 1146 | while (1) { |
| 1147 | // '...'[opt] |
| 1148 | if (Tok.is(tok::ellipsis)) { |
| 1149 | ConsumeToken(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1150 | return TPResult::True(); // '...' is a sign of a function declarator. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1153 | ParsedAttributes attrs; |
| 1154 | MaybeParseMicrosoftAttributes(attrs); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 1155 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1156 | // decl-specifier-seq |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1157 | TPResult TPR = TryParseDeclarationSpecifier(); |
| 1158 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1159 | return TPR; |
| 1160 | |
| 1161 | // declarator |
| 1162 | // abstract-declarator[opt] |
| 1163 | TPR = TryParseDeclarator(true/*mayBeAbstract*/); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1164 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1165 | return TPR; |
| 1166 | |
Argyrios Kyrtzidis | 346af03 | 2010-12-08 02:02:46 +0000 | [diff] [blame] | 1167 | // [GNU] attributes[opt] |
| 1168 | if (Tok.is(tok::kw___attribute)) |
| 1169 | return TPResult::True(); |
| 1170 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1171 | if (Tok.is(tok::equal)) { |
| 1172 | // '=' assignment-expression |
| 1173 | // Parse through assignment-expression. |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 1174 | tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren }; |
| 1175 | if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1176 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | if (Tok.is(tok::ellipsis)) { |
| 1180 | ConsumeToken(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1181 | return TPResult::True(); // '...' is a sign of a function declarator. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | if (Tok.isNot(tok::comma)) |
| 1185 | break; |
| 1186 | ConsumeToken(); // the comma. |
| 1187 | } |
| 1188 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1189 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 1192 | /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue |
| 1193 | /// parsing as a function declarator. |
| 1194 | /// If TryParseFunctionDeclarator fully parsed the function declarator, it will |
| 1195 | /// return TPResult::Ambiguous(), otherwise it will return either False() or |
| 1196 | /// Error(). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | /// |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1198 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 1199 | /// exception-specification[opt] |
| 1200 | /// |
| 1201 | /// exception-specification: |
| 1202 | /// 'throw' '(' type-id-list[opt] ')' |
| 1203 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1204 | Parser::TPResult Parser::TryParseFunctionDeclarator() { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 1205 | |
| 1206 | // The '(' is already parsed. |
| 1207 | |
| 1208 | TPResult TPR = TryParseParameterDeclarationClause(); |
| 1209 | if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) |
| 1210 | TPR = TPResult::False(); |
| 1211 | |
| 1212 | if (TPR == TPResult::False() || TPR == TPResult::Error()) |
| 1213 | return TPR; |
| 1214 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1215 | // Parse through the parens. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1216 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1217 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1218 | |
| 1219 | // cv-qualifier-seq |
| 1220 | while (Tok.is(tok::kw_const) || |
| 1221 | Tok.is(tok::kw_volatile) || |
| 1222 | Tok.is(tok::kw_restrict) ) |
| 1223 | ConsumeToken(); |
| 1224 | |
Douglas Gregor | e3c7a7c | 2011-01-26 16:50:54 +0000 | [diff] [blame^] | 1225 | // ref-qualifier[opt] |
| 1226 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) |
| 1227 | ConsumeToken(); |
| 1228 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1229 | // exception-specification |
| 1230 | if (Tok.is(tok::kw_throw)) { |
| 1231 | ConsumeToken(); |
| 1232 | if (Tok.isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1233 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Parse through the parens after 'throw'. |
| 1236 | ConsumeParen(); |
| 1237 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1238 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1241 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /// '[' constant-expression[opt] ']' |
| 1245 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1246 | Parser::TPResult Parser::TryParseBracketDeclarator() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1247 | ConsumeBracket(); |
| 1248 | if (!SkipUntil(tok::r_square)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1249 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1250 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1251 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1252 | } |