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