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