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" |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | using namespace clang; |
| 18 | |
| 19 | /// isCXXDeclarationStatement - C++-specialized function that disambiguates |
| 20 | /// between a declaration or an expression statement, when parsing function |
| 21 | /// bodies. Returns true for declaration, false for expression. |
| 22 | /// |
| 23 | /// declaration-statement: |
| 24 | /// block-declaration |
| 25 | /// |
| 26 | /// block-declaration: |
| 27 | /// simple-declaration |
| 28 | /// asm-definition |
| 29 | /// namespace-alias-definition |
| 30 | /// using-declaration |
| 31 | /// using-directive |
| 32 | /// [C++0x] static_assert-declaration [TODO] |
| 33 | /// |
| 34 | /// asm-definition: |
| 35 | /// 'asm' '(' string-literal ')' ';' |
| 36 | /// |
| 37 | /// namespace-alias-definition: |
| 38 | /// 'namespace' identifier = qualified-namespace-specifier ';' |
| 39 | /// |
| 40 | /// using-declaration: |
| 41 | /// 'using' typename[opt] '::'[opt] nested-name-specifier |
| 42 | /// unqualified-id ';' |
| 43 | /// 'using' '::' unqualified-id ; |
| 44 | /// |
| 45 | /// using-directive: |
| 46 | /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt] |
| 47 | /// namespace-name ';' |
| 48 | /// |
| 49 | /// [C++0x] static_assert-declaration: [TODO] |
| 50 | /// [C++0x] static_assert '(' constant-expression ',' string-literal ')' ';' |
| 51 | /// |
| 52 | bool Parser::isCXXDeclarationStatement() { |
| 53 | switch (Tok.getKind()) { |
| 54 | // asm-definition |
| 55 | case tok::kw_asm: |
| 56 | // namespace-alias-definition |
| 57 | case tok::kw_namespace: |
| 58 | // using-declaration |
| 59 | // using-directive |
| 60 | case tok::kw_using: |
| 61 | return true; |
| 62 | default: |
| 63 | // simple-declaration |
| 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); |
| 114 | |
| 115 | TPR = TryParseSimpleDeclaration(); |
| 116 | SourceLocation TentativeParseLoc = Tok.getLocation(); |
| 117 | |
| 118 | PA.Revert(); |
| 119 | |
| 120 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 121 | if (TPR == TPResult::Error()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 122 | return true; |
| 123 | |
| 124 | // Declarations take precedence over expressions. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 125 | if (TPR == TPResult::Ambiguous()) |
| 126 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 127 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 128 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 129 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// simple-declaration: |
| 133 | /// decl-specifier-seq init-declarator-list[opt] ';' |
| 134 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 135 | Parser::TPResult Parser::TryParseSimpleDeclaration() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 136 | // We know that we have a simple-type-specifier/typename-specifier followed |
| 137 | // by a '('. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 138 | assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 139 | |
| 140 | if (Tok.is(tok::kw_typeof)) |
| 141 | TryParseTypeofSpecifier(); |
| 142 | else |
| 143 | ConsumeToken(); |
| 144 | |
| 145 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 146 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 147 | TPResult TPR = TryParseInitDeclaratorList(); |
| 148 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 149 | return TPR; |
| 150 | |
| 151 | if (Tok.isNot(tok::semi)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 152 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 153 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 154 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 157 | /// init-declarator-list: |
| 158 | /// init-declarator |
| 159 | /// init-declarator-list ',' init-declarator |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 160 | /// |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 161 | /// init-declarator: |
| 162 | /// declarator initializer[opt] |
| 163 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 164 | /// |
| 165 | /// initializer: |
| 166 | /// '=' initializer-clause |
| 167 | /// '(' expression-list ')' |
| 168 | /// |
| 169 | /// initializer-clause: |
| 170 | /// assignment-expression |
| 171 | /// '{' initializer-list ','[opt] '}' |
| 172 | /// '{' '}' |
| 173 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 174 | Parser::TPResult Parser::TryParseInitDeclaratorList() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 175 | // GCC only examines the first declarator for disambiguation: |
| 176 | // i.e: |
| 177 | // int(x), ++x; // GCC regards it as ill-formed declaration. |
| 178 | // |
| 179 | // Comeau and MSVC will regard the above statement as correct expression. |
| 180 | // Clang examines all of the declarators and also regards the above statement |
| 181 | // as correct expression. |
| 182 | |
| 183 | while (1) { |
| 184 | // declarator |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 185 | TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 186 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 187 | return TPR; |
| 188 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 189 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 190 | if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 191 | return TPResult::True(); |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 192 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 193 | // initializer[opt] |
| 194 | if (Tok.is(tok::l_paren)) { |
| 195 | // Parse through the parens. |
| 196 | ConsumeParen(); |
| 197 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 198 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 199 | } else if (Tok.is(tok::equal)) { |
| 200 | // MSVC won't examine the rest of declarators if '=' is encountered, it |
| 201 | // will conclude that it is a declaration. |
| 202 | // Comeau and Clang will examine the rest of declarators. |
| 203 | // Note that "int(x) = {0}, ++x;" will be interpreted as ill-formed |
| 204 | // expression. |
| 205 | // |
| 206 | // Parse through the initializer-clause. |
| 207 | SkipUntil(tok::comma, true/*StopAtSemi*/, true/*DontConsume*/); |
| 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(); |
| 246 | else |
| 247 | ConsumeToken(); |
| 248 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 249 | |
| 250 | // declarator |
| 251 | TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 252 | |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 253 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 254 | if (TPR == TPResult::Error()) |
| 255 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 256 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 257 | if (TPR == TPResult::Ambiguous()) { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 258 | // '=' |
| 259 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 260 | if (Tok.is(tok::equal) || |
| 261 | Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 262 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 263 | else |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 264 | TPR = TPResult::False(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Argyrios Kyrtzidis | ca35baa | 2008-10-05 15:19:49 +0000 | [diff] [blame] | 267 | PA.Revert(); |
| 268 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 269 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 270 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 273 | /// isCXXTypeIdInParens - Assumes that a '(' was parsed and now we want to |
| 274 | /// know whether the parens contain an expression or a type-id. |
| 275 | /// Returns true for a type-id and false for an expression. |
| 276 | /// If during the disambiguation process a parsing error is encountered, |
| 277 | /// the function returns true to let the declaration parsing code handle it. |
| 278 | /// |
| 279 | /// type-id: |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 280 | /// type-specifier-seq abstract-declarator[opt] |
| 281 | /// |
| 282 | bool Parser::isCXXTypeIdInParens() { |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 283 | |
| 284 | // C++ 8.2p2: |
| 285 | // The ambiguity arising from the similarity between a function-style cast and |
| 286 | // a type-id can occur in different contexts. The ambiguity appears as a |
| 287 | // choice between a function-style cast expression and a declaration of a |
| 288 | // type. The resolution is that any construct that could possibly be a type-id |
| 289 | // in its syntactic context shall be considered a type-id. |
| 290 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 291 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 292 | if (TPR != TPResult::Ambiguous()) |
| 293 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 294 | // TPResult::Error(). |
| 295 | |
| 296 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 297 | // and how they were resolved (number of declarations+number of expressions). |
| 298 | |
| 299 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 300 | // We need tentative parsing... |
| 301 | |
| 302 | TentativeParsingAction PA(*this); |
| 303 | |
| 304 | // type-specifier-seq |
| 305 | if (Tok.is(tok::kw_typeof)) |
| 306 | TryParseTypeofSpecifier(); |
| 307 | else |
| 308 | ConsumeToken(); |
| 309 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 310 | |
| 311 | // declarator |
| 312 | TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); |
| 313 | |
| 314 | // In case of an error, let the declaration parsing code handle it. |
| 315 | if (TPR == TPResult::Error()) |
| 316 | TPR = TPResult::True(); |
| 317 | |
| 318 | if (TPR == TPResult::Ambiguous()) { |
| 319 | // We are supposed to be inside parens, so if after the abstract declarator |
| 320 | // we encounter a ')' this is a type-id, otherwise it's an expression. |
| 321 | if (Tok.is(tok::r_paren)) |
| 322 | TPR = TPResult::True(); |
| 323 | else |
| 324 | TPR = TPResult::False(); |
| 325 | } |
| 326 | |
| 327 | PA.Revert(); |
| 328 | |
| 329 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 330 | return TPR == TPResult::True(); |
| 331 | } |
| 332 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 333 | /// declarator: |
| 334 | /// direct-declarator |
| 335 | /// ptr-operator declarator |
| 336 | /// |
| 337 | /// direct-declarator: |
| 338 | /// declarator-id |
| 339 | /// direct-declarator '(' parameter-declaration-clause ')' |
| 340 | /// cv-qualifier-seq[opt] exception-specification[opt] |
| 341 | /// direct-declarator '[' constant-expression[opt] ']' |
| 342 | /// '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 343 | /// [GNU] '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 344 | /// |
| 345 | /// abstract-declarator: |
| 346 | /// ptr-operator abstract-declarator[opt] |
| 347 | /// direct-abstract-declarator |
| 348 | /// |
| 349 | /// direct-abstract-declarator: |
| 350 | /// direct-abstract-declarator[opt] |
| 351 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 352 | /// exception-specification[opt] |
| 353 | /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 354 | /// '(' abstract-declarator ')' |
| 355 | /// |
| 356 | /// ptr-operator: |
| 357 | /// '*' cv-qualifier-seq[opt] |
| 358 | /// '&' |
| 359 | /// [C++0x] '&&' [TODO] |
| 360 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO] |
| 361 | /// |
| 362 | /// cv-qualifier-seq: |
| 363 | /// cv-qualifier cv-qualifier-seq[opt] |
| 364 | /// |
| 365 | /// cv-qualifier: |
| 366 | /// 'const' |
| 367 | /// 'volatile' |
| 368 | /// |
| 369 | /// declarator-id: |
| 370 | /// id-expression |
| 371 | /// |
| 372 | /// id-expression: |
| 373 | /// unqualified-id |
| 374 | /// qualified-id [TODO] |
| 375 | /// |
| 376 | /// unqualified-id: |
| 377 | /// identifier |
| 378 | /// operator-function-id [TODO] |
| 379 | /// conversion-function-id [TODO] |
| 380 | /// '~' class-name [TODO] |
| 381 | /// template-id [TODO] |
| 382 | /// |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 383 | Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, |
| 384 | bool mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 385 | // declarator: |
| 386 | // direct-declarator |
| 387 | // ptr-operator declarator |
| 388 | |
| 389 | while (1) { |
Chris Lattner | a4f34ea | 2008-11-22 01:15:33 +0000 | [diff] [blame] | 390 | if (Tok.is(tok::star) || Tok.is(tok::amp) || |
| 391 | (Tok.is(tok::caret) && getLang().Blocks)) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 392 | // ptr-operator |
| 393 | ConsumeToken(); |
| 394 | while (Tok.is(tok::kw_const) || |
| 395 | Tok.is(tok::kw_volatile) || |
| 396 | Tok.is(tok::kw_restrict) ) |
| 397 | ConsumeToken(); |
| 398 | } else { |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // direct-declarator: |
| 404 | // direct-abstract-declarator: |
| 405 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 406 | if (Tok.is(tok::identifier) && mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 407 | // declarator-id |
| 408 | ConsumeToken(); |
| 409 | } else if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 410 | ConsumeParen(); |
| 411 | if (mayBeAbstract && |
| 412 | (Tok.is(tok::r_paren) || // 'int()' is a function. |
| 413 | Tok.is(tok::ellipsis) || // 'int(...)' is a function. |
| 414 | isDeclarationSpecifier())) { // 'int(int)' is a function. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 415 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 416 | // exception-specification[opt] |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 417 | TPResult TPR = TryParseFunctionDeclarator(); |
| 418 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 419 | return TPR; |
| 420 | } else { |
| 421 | // '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 422 | // '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 423 | // '(' abstract-declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 424 | if (Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 425 | return TPResult::True(); // attributes indicate declaration |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 426 | TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 427 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 428 | return TPR; |
| 429 | if (Tok.isNot(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 430 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 431 | ConsumeParen(); |
| 432 | } |
| 433 | } else if (!mayBeAbstract) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 434 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | while (1) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 438 | TPResult TPR(TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 439 | |
| 440 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 441 | // Check whether we have a function declarator or a possible ctor-style |
| 442 | // initializer that follows the declarator. Note that ctor-style |
| 443 | // initializers are not possible in contexts where abstract declarators |
| 444 | // are allowed. |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 445 | if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/)) |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 446 | break; |
| 447 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 448 | // direct-declarator '(' parameter-declaration-clause ')' |
| 449 | // cv-qualifier-seq[opt] exception-specification[opt] |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 450 | ConsumeParen(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 451 | TPR = TryParseFunctionDeclarator(); |
| 452 | } else if (Tok.is(tok::l_square)) { |
| 453 | // direct-declarator '[' constant-expression[opt] ']' |
| 454 | // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 455 | TPR = TryParseBracketDeclarator(); |
| 456 | } else { |
| 457 | break; |
| 458 | } |
| 459 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 460 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 461 | return TPR; |
| 462 | } |
| 463 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 464 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 467 | /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration |
| 468 | /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could |
| 469 | /// be either a decl-specifier or a function-style cast, and TPResult::Error() |
| 470 | /// if a parsing error was found and reported. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 471 | /// |
| 472 | /// decl-specifier: |
| 473 | /// storage-class-specifier |
| 474 | /// type-specifier |
| 475 | /// function-specifier |
| 476 | /// 'friend' |
| 477 | /// 'typedef' |
| 478 | /// [GNU] attributes declaration-specifiers[opt] |
| 479 | /// |
| 480 | /// storage-class-specifier: |
| 481 | /// 'register' |
| 482 | /// 'static' |
| 483 | /// 'extern' |
| 484 | /// 'mutable' |
| 485 | /// 'auto' |
| 486 | /// [GNU] '__thread' |
| 487 | /// |
| 488 | /// function-specifier: |
| 489 | /// 'inline' |
| 490 | /// 'virtual' |
| 491 | /// 'explicit' |
| 492 | /// |
| 493 | /// typedef-name: |
| 494 | /// identifier |
| 495 | /// |
| 496 | /// type-specifier: |
| 497 | /// simple-type-specifier |
| 498 | /// class-specifier |
| 499 | /// enum-specifier |
| 500 | /// elaborated-type-specifier |
| 501 | /// typename-specifier [TODO] |
| 502 | /// cv-qualifier |
| 503 | /// |
| 504 | /// simple-type-specifier: |
| 505 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 506 | /// '::'[opt] nested-name-specifier 'template' |
| 507 | /// simple-template-id [TODO] |
| 508 | /// 'char' |
| 509 | /// 'wchar_t' |
| 510 | /// 'bool' |
| 511 | /// 'short' |
| 512 | /// 'int' |
| 513 | /// 'long' |
| 514 | /// 'signed' |
| 515 | /// 'unsigned' |
| 516 | /// 'float' |
| 517 | /// 'double' |
| 518 | /// 'void' |
| 519 | /// [GNU] typeof-specifier |
| 520 | /// [GNU] '_Complex' |
| 521 | /// [C++0x] 'auto' [TODO] |
| 522 | /// |
| 523 | /// type-name: |
| 524 | /// class-name |
| 525 | /// enum-name |
| 526 | /// typedef-name |
| 527 | /// |
| 528 | /// elaborated-type-specifier: |
| 529 | /// class-key '::'[opt] nested-name-specifier[opt] identifier |
| 530 | /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] |
| 531 | /// simple-template-id |
| 532 | /// 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 533 | /// |
| 534 | /// enum-name: |
| 535 | /// identifier |
| 536 | /// |
| 537 | /// enum-specifier: |
| 538 | /// 'enum' identifier[opt] '{' enumerator-list[opt] '}' |
| 539 | /// 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 540 | /// |
| 541 | /// class-specifier: |
| 542 | /// class-head '{' member-specification[opt] '}' |
| 543 | /// |
| 544 | /// class-head: |
| 545 | /// class-key identifier[opt] base-clause[opt] |
| 546 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 547 | /// class-key nested-name-specifier[opt] simple-template-id |
| 548 | /// base-clause[opt] |
| 549 | /// |
| 550 | /// class-key: |
| 551 | /// 'class' |
| 552 | /// 'struct' |
| 553 | /// 'union' |
| 554 | /// |
| 555 | /// cv-qualifier: |
| 556 | /// 'const' |
| 557 | /// 'volatile' |
| 558 | /// [GNU] restrict |
| 559 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 560 | Parser::TPResult Parser::isCXXDeclarationSpecifier() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 561 | switch (Tok.getKind()) { |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 562 | case tok::identifier: // foo::bar |
| 563 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 564 | // recurse to handle whatever we get. |
| 565 | if (TryAnnotateTypeOrScopeToken()) |
| 566 | return isCXXDeclarationSpecifier(); |
| 567 | // Otherwise, not a typename. |
| 568 | return TPResult::False(); |
| 569 | |
| 570 | case tok::coloncolon: // ::foo::bar |
| 571 | if (NextToken().is(tok::kw_new) || // ::new |
| 572 | NextToken().is(tok::kw_delete)) // ::delete |
| 573 | return TPResult::False(); |
| 574 | |
| 575 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 576 | // recurse to handle whatever we get. |
| 577 | if (TryAnnotateTypeOrScopeToken()) |
| 578 | return isCXXDeclarationSpecifier(); |
| 579 | // Otherwise, not a typename. |
| 580 | return TPResult::False(); |
| 581 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 582 | // decl-specifier: |
| 583 | // storage-class-specifier |
| 584 | // type-specifier |
| 585 | // function-specifier |
| 586 | // 'friend' |
| 587 | // 'typedef' |
| 588 | |
| 589 | case tok::kw_friend: |
| 590 | case tok::kw_typedef: |
| 591 | // storage-class-specifier |
| 592 | case tok::kw_register: |
| 593 | case tok::kw_static: |
| 594 | case tok::kw_extern: |
| 595 | case tok::kw_mutable: |
| 596 | case tok::kw_auto: |
| 597 | case tok::kw___thread: |
| 598 | // function-specifier |
| 599 | case tok::kw_inline: |
| 600 | case tok::kw_virtual: |
| 601 | case tok::kw_explicit: |
| 602 | |
| 603 | // type-specifier: |
| 604 | // simple-type-specifier |
| 605 | // class-specifier |
| 606 | // enum-specifier |
| 607 | // elaborated-type-specifier |
| 608 | // typename-specifier |
| 609 | // cv-qualifier |
| 610 | |
| 611 | // class-specifier |
| 612 | // elaborated-type-specifier |
| 613 | case tok::kw_class: |
| 614 | case tok::kw_struct: |
| 615 | case tok::kw_union: |
| 616 | // enum-specifier |
| 617 | case tok::kw_enum: |
| 618 | // cv-qualifier |
| 619 | case tok::kw_const: |
| 620 | case tok::kw_volatile: |
| 621 | |
| 622 | // GNU |
| 623 | case tok::kw_restrict: |
| 624 | case tok::kw__Complex: |
| 625 | case tok::kw___attribute: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 626 | return TPResult::True(); |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 627 | |
| 628 | // Microsoft |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame^] | 629 | case tok::kw___declspec: |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 630 | case tok::kw___cdecl: |
| 631 | case tok::kw___stdcall: |
| 632 | case tok::kw___fastcall: |
| 633 | return PP.getLangOptions().Microsoft ? TPResult::True() : TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 634 | |
| 635 | // The ambiguity resides in a simple-type-specifier/typename-specifier |
| 636 | // followed by a '('. The '(' could either be the start of: |
| 637 | // |
| 638 | // direct-declarator: |
| 639 | // '(' declarator ')' |
| 640 | // |
| 641 | // direct-abstract-declarator: |
| 642 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 643 | // exception-specification[opt] |
| 644 | // '(' abstract-declarator ')' |
| 645 | // |
| 646 | // or part of a function-style cast expression: |
| 647 | // |
| 648 | // simple-type-specifier '(' expression-list[opt] ')' |
| 649 | // |
| 650 | |
| 651 | // simple-type-specifier: |
| 652 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 653 | case tok::kw_char: |
| 654 | case tok::kw_wchar_t: |
| 655 | case tok::kw_bool: |
| 656 | case tok::kw_short: |
| 657 | case tok::kw_int: |
| 658 | case tok::kw_long: |
| 659 | case tok::kw_signed: |
| 660 | case tok::kw_unsigned: |
| 661 | case tok::kw_float: |
| 662 | case tok::kw_double: |
| 663 | case tok::kw_void: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 664 | case tok::annot_typename: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 665 | if (NextToken().is(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 666 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 667 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 668 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 669 | |
| 670 | // GNU typeof support. |
| 671 | case tok::kw_typeof: { |
| 672 | if (NextToken().isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 673 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 674 | |
| 675 | TentativeParsingAction PA(*this); |
| 676 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 677 | TPResult TPR = TryParseTypeofSpecifier(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 678 | bool isFollowedByParen = Tok.is(tok::l_paren); |
| 679 | |
| 680 | PA.Revert(); |
| 681 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 682 | if (TPR == TPResult::Error()) |
| 683 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 684 | |
| 685 | if (isFollowedByParen) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 686 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 687 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 688 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | default: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 692 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | /// [GNU] typeof-specifier: |
| 697 | /// 'typeof' '(' expressions ')' |
| 698 | /// 'typeof' '(' type-name ')' |
| 699 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 700 | Parser::TPResult Parser::TryParseTypeofSpecifier() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 701 | assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); |
| 702 | ConsumeToken(); |
| 703 | |
| 704 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 705 | // Parse through the parens after 'typeof'. |
| 706 | ConsumeParen(); |
| 707 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 708 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 709 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 710 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 713 | Parser::TPResult Parser::TryParseDeclarationSpecifier() { |
| 714 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 715 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 716 | return TPR; |
| 717 | |
| 718 | if (Tok.is(tok::kw_typeof)) |
| 719 | TryParseTypeofSpecifier(); |
| 720 | else |
| 721 | ConsumeToken(); |
| 722 | |
| 723 | assert(Tok.is(tok::l_paren) && "Expected '('!"); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 724 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /// isCXXFunctionDeclarator - Disambiguates between a function declarator or |
| 728 | /// a constructor-style initializer, when parsing declaration statements. |
| 729 | /// Returns true for function declarator and false for constructor-style |
| 730 | /// initializer. |
| 731 | /// If during the disambiguation process a parsing error is encountered, |
| 732 | /// the function returns true to let the declaration parsing code handle it. |
| 733 | /// |
| 734 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 735 | /// exception-specification[opt] |
| 736 | /// |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 737 | bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) { |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 738 | |
| 739 | // C++ 8.2p1: |
| 740 | // The ambiguity arising from the similarity between a function-style cast and |
| 741 | // a declaration mentioned in 6.8 can also occur in the context of a |
| 742 | // declaration. In that context, the choice is between a function declaration |
| 743 | // with a redundant set of parentheses around a parameter name and an object |
| 744 | // declaration with a function-style cast as the initializer. Just as for the |
| 745 | // ambiguities mentioned in 6.8, the resolution is to consider any construct |
| 746 | // that could possibly be a declaration a declaration. |
| 747 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 748 | TentativeParsingAction PA(*this); |
| 749 | |
| 750 | ConsumeParen(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 751 | TPResult TPR = TryParseParameterDeclarationClause(); |
| 752 | if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) |
| 753 | TPR = TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 754 | |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 755 | SourceLocation TPLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 756 | PA.Revert(); |
| 757 | |
| 758 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 759 | if (TPR == TPResult::Error()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 760 | return true; |
| 761 | |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 762 | if (TPR == TPResult::Ambiguous()) { |
| 763 | // Function declarator has precedence over constructor-style initializer. |
| 764 | // Emit a warning just in case the author intended a variable definition. |
Argyrios Kyrtzidis | e75d849 | 2008-10-17 23:23:35 +0000 | [diff] [blame] | 765 | if (warnIfAmbiguous) |
Chris Lattner | ef708fd | 2008-11-18 07:50:21 +0000 | [diff] [blame] | 766 | Diag(Tok, diag::warn_parens_disambiguated_as_function_decl) |
| 767 | << SourceRange(Tok.getLocation(), TPLoc); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 768 | return true; |
Argyrios Kyrtzidis | 259b0d9 | 2008-10-15 23:21:32 +0000 | [diff] [blame] | 769 | } |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 770 | |
| 771 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /// parameter-declaration-clause: |
| 775 | /// parameter-declaration-list[opt] '...'[opt] |
| 776 | /// parameter-declaration-list ',' '...' |
| 777 | /// |
| 778 | /// parameter-declaration-list: |
| 779 | /// parameter-declaration |
| 780 | /// parameter-declaration-list ',' parameter-declaration |
| 781 | /// |
| 782 | /// parameter-declaration: |
| 783 | /// decl-specifier-seq declarator |
| 784 | /// decl-specifier-seq declarator '=' assignment-expression |
| 785 | /// decl-specifier-seq abstract-declarator[opt] |
| 786 | /// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression |
| 787 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 788 | Parser::TPResult Parser::TryParseParameterDeclarationClause() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 789 | |
| 790 | if (Tok.is(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 791 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 792 | |
| 793 | // parameter-declaration-list[opt] '...'[opt] |
| 794 | // parameter-declaration-list ',' '...' |
| 795 | // |
| 796 | // parameter-declaration-list: |
| 797 | // parameter-declaration |
| 798 | // parameter-declaration-list ',' parameter-declaration |
| 799 | // |
| 800 | while (1) { |
| 801 | // '...'[opt] |
| 802 | if (Tok.is(tok::ellipsis)) { |
| 803 | ConsumeToken(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 804 | return TPResult::True(); // '...' is a sign of a function declarator. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // decl-specifier-seq |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 808 | TPResult TPR = TryParseDeclarationSpecifier(); |
| 809 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 810 | return TPR; |
| 811 | |
| 812 | // declarator |
| 813 | // abstract-declarator[opt] |
| 814 | TPR = TryParseDeclarator(true/*mayBeAbstract*/); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 815 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 816 | return TPR; |
| 817 | |
| 818 | if (Tok.is(tok::equal)) { |
| 819 | // '=' assignment-expression |
| 820 | // Parse through assignment-expression. |
| 821 | tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren }; |
| 822 | if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 823 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | if (Tok.is(tok::ellipsis)) { |
| 827 | ConsumeToken(); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 828 | return TPResult::True(); // '...' is a sign of a function declarator. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | if (Tok.isNot(tok::comma)) |
| 832 | break; |
| 833 | ConsumeToken(); // the comma. |
| 834 | } |
| 835 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 836 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 839 | /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue |
| 840 | /// parsing as a function declarator. |
| 841 | /// If TryParseFunctionDeclarator fully parsed the function declarator, it will |
| 842 | /// return TPResult::Ambiguous(), otherwise it will return either False() or |
| 843 | /// Error(). |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 844 | /// |
| 845 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 846 | /// exception-specification[opt] |
| 847 | /// |
| 848 | /// exception-specification: |
| 849 | /// 'throw' '(' type-id-list[opt] ')' |
| 850 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 851 | Parser::TPResult Parser::TryParseFunctionDeclarator() { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 852 | |
| 853 | // The '(' is already parsed. |
| 854 | |
| 855 | TPResult TPR = TryParseParameterDeclarationClause(); |
| 856 | if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) |
| 857 | TPR = TPResult::False(); |
| 858 | |
| 859 | if (TPR == TPResult::False() || TPR == TPResult::Error()) |
| 860 | return TPR; |
| 861 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 862 | // Parse through the parens. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 863 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 864 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 865 | |
| 866 | // cv-qualifier-seq |
| 867 | while (Tok.is(tok::kw_const) || |
| 868 | Tok.is(tok::kw_volatile) || |
| 869 | Tok.is(tok::kw_restrict) ) |
| 870 | ConsumeToken(); |
| 871 | |
| 872 | // exception-specification |
| 873 | if (Tok.is(tok::kw_throw)) { |
| 874 | ConsumeToken(); |
| 875 | if (Tok.isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 876 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 877 | |
| 878 | // Parse through the parens after 'throw'. |
| 879 | ConsumeParen(); |
| 880 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 881 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 884 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /// '[' constant-expression[opt] ']' |
| 888 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 889 | Parser::TPResult Parser::TryParseBracketDeclarator() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 890 | ConsumeBracket(); |
| 891 | if (!SkipUntil(tok::r_square)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 892 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 893 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 894 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 895 | } |