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: |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 65 | return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 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 | /// |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 78 | /// (if AllowForRangeDecl specified) |
| 79 | /// for ( for-range-declaration : for-range-initializer ) statement |
| 80 | /// for-range-declaration: |
| 81 | /// attribute-specifier-seqopt type-specifier-seq declarator |
| 82 | bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 83 | // C++ 6.8p1: |
| 84 | // There is an ambiguity in the grammar involving expression-statements and |
| 85 | // declarations: An expression-statement with a function-style explicit type |
| 86 | // conversion (5.2.3) as its leftmost subexpression can be indistinguishable |
| 87 | // from a declaration where the first declarator starts with a '('. In those |
| 88 | // cases the statement is a declaration. [Note: To disambiguate, the whole |
| 89 | // statement might have to be examined to determine if it is an |
| 90 | // expression-statement or a declaration]. |
| 91 | |
| 92 | // C++ 6.8p3: |
| 93 | // The disambiguation is purely syntactic; that is, the meaning of the names |
| 94 | // occurring in such a statement, beyond whether they are type-names or not, |
| 95 | // is not generally used in or changed by the disambiguation. Class |
| 96 | // templates are instantiated as necessary to determine if a qualified name |
| 97 | // is a type-name. Disambiguation precedes parsing, and a statement |
| 98 | // disambiguated as a declaration may be an ill-formed declaration. |
| 99 | |
| 100 | // We don't have to parse all of the decl-specifier-seq part. There's only |
| 101 | // an ambiguity if the first decl-specifier is |
| 102 | // simple-type-specifier/typename-specifier followed by a '(', which may |
| 103 | // indicate a function-style cast expression. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 104 | // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such |
| 105 | // a case. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 107 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 108 | if (TPR != TPResult::Ambiguous()) |
| 109 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 110 | // TPResult::Error(). |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 111 | |
| 112 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 113 | // and how they were resolved (number of declarations+number of expressions). |
| 114 | |
| 115 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 116 | // We need tentative parsing... |
| 117 | |
| 118 | TentativeParsingAction PA(*this); |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 119 | TPR = TryParseSimpleDeclaration(AllowForRangeDecl); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 120 | PA.Revert(); |
| 121 | |
| 122 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 123 | if (TPR == TPResult::Error()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 124 | return true; |
| 125 | |
| 126 | // Declarations take precedence over expressions. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 127 | if (TPR == TPResult::Ambiguous()) |
| 128 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 129 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 130 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 131 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /// simple-declaration: |
| 135 | /// decl-specifier-seq init-declarator-list[opt] ';' |
| 136 | /// |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 137 | /// (if AllowForRangeDecl specified) |
| 138 | /// for ( for-range-declaration : for-range-initializer ) statement |
| 139 | /// for-range-declaration: |
| 140 | /// attribute-specifier-seqopt type-specifier-seq declarator |
| 141 | /// |
| 142 | Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 143 | // We know that we have a simple-type-specifier/typename-specifier followed |
| 144 | // by a '('. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 145 | assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 146 | |
| 147 | if (Tok.is(tok::kw_typeof)) |
| 148 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 149 | else { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 150 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 151 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 152 | if (getLangOpts().ObjC1 && Tok.is(tok::less)) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 153 | TryParseProtocolQualifiers(); |
| 154 | } |
| 155 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 156 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 157 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 158 | TPResult TPR = TryParseInitDeclaratorList(); |
| 159 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 160 | return TPR; |
| 161 | |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 162 | if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon))) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 163 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 164 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 165 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 168 | /// init-declarator-list: |
| 169 | /// init-declarator |
| 170 | /// init-declarator-list ',' init-declarator |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 171 | /// |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 172 | /// init-declarator: |
| 173 | /// declarator initializer[opt] |
| 174 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 175 | /// |
| 176 | /// initializer: |
| 177 | /// '=' initializer-clause |
| 178 | /// '(' expression-list ')' |
| 179 | /// |
| 180 | /// initializer-clause: |
| 181 | /// assignment-expression |
| 182 | /// '{' initializer-list ','[opt] '}' |
| 183 | /// '{' '}' |
| 184 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 185 | Parser::TPResult Parser::TryParseInitDeclaratorList() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 186 | while (1) { |
| 187 | // declarator |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 188 | TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 189 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 190 | return TPR; |
| 191 | |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 192 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 193 | if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 194 | return TPResult::True(); |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 195 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 196 | // initializer[opt] |
| 197 | if (Tok.is(tok::l_paren)) { |
| 198 | // Parse through the parens. |
| 199 | ConsumeParen(); |
| 200 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 201 | return TPResult::Error(); |
Fariborz Jahanian | f459beb | 2010-08-29 17:20:53 +0000 | [diff] [blame] | 202 | } else if (Tok.is(tok::equal) || isTokIdentifier_in()) { |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 203 | // MSVC and g++ won't examine the rest of declarators if '=' is |
| 204 | // encountered; they just conclude that we have a declaration. |
| 205 | // EDG parses the initializer completely, which is the proper behavior |
| 206 | // for this case. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 207 | // |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 208 | // At present, Clang follows MSVC and g++, since the parser does not have |
| 209 | // the ability to parse an expression fully without recording the |
| 210 | // results of that parse. |
Fariborz Jahanian | f459beb | 2010-08-29 17:20:53 +0000 | [diff] [blame] | 211 | // Also allow 'in' after on objective-c declaration as in: |
| 212 | // for (int (^b)(void) in array). Ideally this should be done in the |
| 213 | // context of parsing for-init-statement of a foreach statement only. But, |
| 214 | // in any other context 'in' is invalid after a declaration and parser |
| 215 | // issues the error regardless of outcome of this decision. |
| 216 | // FIXME. Change if above assumption does not hold. |
Douglas Gregor | 06b7080 | 2010-07-15 21:05:01 +0000 | [diff] [blame] | 217 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (Tok.isNot(tok::comma)) |
| 221 | break; |
| 222 | ConsumeToken(); // the comma. |
| 223 | } |
| 224 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 225 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 228 | /// isCXXConditionDeclaration - Disambiguates between a declaration or an |
| 229 | /// expression for a condition of a if/switch/while/for statement. |
| 230 | /// If during the disambiguation process a parsing error is encountered, |
| 231 | /// the function returns true to let the declaration parsing code handle it. |
| 232 | /// |
| 233 | /// condition: |
| 234 | /// expression |
| 235 | /// type-specifier-seq declarator '=' assignment-expression |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 236 | /// [C++11] type-specifier-seq declarator '=' initializer-clause |
| 237 | /// [C++11] type-specifier-seq declarator braced-init-list |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 238 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 239 | /// '=' assignment-expression |
| 240 | /// |
| 241 | bool Parser::isCXXConditionDeclaration() { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 242 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 243 | if (TPR != TPResult::Ambiguous()) |
| 244 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 245 | // TPResult::Error(). |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 246 | |
| 247 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 248 | // and how they were resolved (number of declarations+number of expressions). |
| 249 | |
| 250 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 251 | // We need tentative parsing... |
| 252 | |
| 253 | TentativeParsingAction PA(*this); |
| 254 | |
| 255 | // type-specifier-seq |
| 256 | if (Tok.is(tok::kw_typeof)) |
| 257 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 258 | else { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 259 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 260 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 261 | if (getLangOpts().ObjC1 && Tok.is(tok::less)) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 262 | TryParseProtocolQualifiers(); |
| 263 | } |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 264 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 265 | |
| 266 | // declarator |
| 267 | TPR = TryParseDeclarator(false/*mayBeAbstract*/); |
| 268 | |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 269 | // In case of an error, let the declaration parsing code handle it. |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 270 | if (TPR == TPResult::Error()) |
| 271 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 272 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 273 | if (TPR == TPResult::Ambiguous()) { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 274 | // '=' |
| 275 | // [GNU] simple-asm-expr[opt] attributes[opt] |
| 276 | if (Tok.is(tok::equal) || |
| 277 | Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 278 | TPR = TPResult::True(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 279 | else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 280 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 281 | else |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 282 | TPR = TPResult::False(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Argyrios Kyrtzidis | ca35baa | 2008-10-05 15:19:49 +0000 | [diff] [blame] | 285 | PA.Revert(); |
| 286 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 287 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 288 | return TPR == TPResult::True(); |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | /// \brief Determine whether the next set of tokens contains a type-id. |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 292 | /// |
| 293 | /// The context parameter states what context we're parsing right |
| 294 | /// now, which affects how this routine copes with the token |
| 295 | /// following the type-id. If the context is TypeIdInParens, we have |
| 296 | /// already parsed the '(' and we will cease lookahead when we hit |
| 297 | /// the corresponding ')'. If the context is |
| 298 | /// TypeIdAsTemplateArgument, we've already parsed the '<' or ',' |
| 299 | /// before this template argument, and will cease lookahead when we |
| 300 | /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id |
| 301 | /// and false for an expression. If during the disambiguation |
| 302 | /// process a parsing error is encountered, the function returns |
| 303 | /// true to let the declaration parsing code handle it. |
| 304 | /// |
| 305 | /// type-id: |
| 306 | /// type-specifier-seq abstract-declarator[opt] |
| 307 | /// |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 308 | bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 310 | isAmbiguous = false; |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 311 | |
| 312 | // C++ 8.2p2: |
| 313 | // The ambiguity arising from the similarity between a function-style cast and |
| 314 | // a type-id can occur in different contexts. The ambiguity appears as a |
| 315 | // choice between a function-style cast expression and a declaration of a |
| 316 | // type. The resolution is that any construct that could possibly be a type-id |
| 317 | // in its syntactic context shall be considered a type-id. |
| 318 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 319 | TPResult TPR = isCXXDeclarationSpecifier(); |
| 320 | if (TPR != TPResult::Ambiguous()) |
| 321 | return TPR != TPResult::False(); // Returns true for TPResult::True() or |
| 322 | // TPResult::Error(). |
| 323 | |
| 324 | // FIXME: Add statistics about the number of ambiguous statements encountered |
| 325 | // and how they were resolved (number of declarations+number of expressions). |
| 326 | |
| 327 | // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. |
| 328 | // We need tentative parsing... |
| 329 | |
| 330 | TentativeParsingAction PA(*this); |
| 331 | |
| 332 | // type-specifier-seq |
| 333 | if (Tok.is(tok::kw_typeof)) |
| 334 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 335 | else { |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 336 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 337 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 338 | if (getLangOpts().ObjC1 && Tok.is(tok::less)) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 339 | TryParseProtocolQualifiers(); |
| 340 | } |
| 341 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 342 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 343 | |
| 344 | // declarator |
| 345 | TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); |
| 346 | |
| 347 | // In case of an error, let the declaration parsing code handle it. |
| 348 | if (TPR == TPResult::Error()) |
| 349 | TPR = TPResult::True(); |
| 350 | |
| 351 | if (TPR == TPResult::Ambiguous()) { |
| 352 | // We are supposed to be inside parens, so if after the abstract declarator |
| 353 | // 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] | 354 | if (Context == TypeIdInParens && Tok.is(tok::r_paren)) { |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 355 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 356 | isAmbiguous = true; |
| 357 | |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 358 | // We are supposed to be inside a template argument, so if after |
| 359 | // the abstract declarator we encounter a '>', '>>' (in C++0x), or |
| 360 | // ',', this is a type-id. Otherwise, it's an expression. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 361 | } else if (Context == TypeIdAsTemplateArgument && |
| 362 | (Tok.is(tok::greater) || Tok.is(tok::comma) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 363 | (getLangOpts().CPlusPlus0x && Tok.is(tok::greatergreater)))) { |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 364 | TPR = TPResult::True(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 365 | isAmbiguous = true; |
| 366 | |
| 367 | } else |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 368 | TPR = TPResult::False(); |
| 369 | } |
| 370 | |
| 371 | PA.Revert(); |
| 372 | |
| 373 | assert(TPR == TPResult::True() || TPR == TPResult::False()); |
| 374 | return TPR == TPResult::True(); |
| 375 | } |
| 376 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 377 | /// \brief Returns true if this is a C++11 attribute-specifier. Per |
| 378 | /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens |
| 379 | /// always introduce an attribute. In Objective-C++11, this rule does not |
| 380 | /// apply if either '[' begins a message-send. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 381 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 382 | /// If Disambiguate is true, we try harder to determine whether a '[[' starts |
| 383 | /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 384 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 385 | /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an |
| 386 | /// Obj-C message send or the start of an attribute. Otherwise, we assume it |
| 387 | /// is not an Obj-C message send. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 388 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 389 | /// C++11 [dcl.attr.grammar]: |
| 390 | /// |
| 391 | /// attribute-specifier: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 392 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 393 | /// alignment-specifier |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 394 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 395 | /// attribute-list: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 396 | /// attribute[opt] |
| 397 | /// attribute-list ',' attribute[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 398 | /// attribute '...' |
| 399 | /// attribute-list ',' attribute '...' |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 400 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 401 | /// attribute: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 402 | /// attribute-token attribute-argument-clause[opt] |
| 403 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 404 | /// attribute-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 405 | /// identifier |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 406 | /// identifier '::' identifier |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 407 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 408 | /// attribute-argument-clause: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 409 | /// '(' balanced-token-seq ')' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 410 | Parser::CXX11AttributeKind |
| 411 | Parser::isCXX11AttributeSpecifier(bool Disambiguate, |
| 412 | bool OuterMightBeMessageSend) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 413 | if (Tok.is(tok::kw_alignas)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 414 | return CAK_AttributeSpecifier; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 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)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 417 | return CAK_NotAttributeSpecifier; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 418 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 419 | // No tentative parsing if we don't need to look for ']]' or a lambda. |
| 420 | if (!Disambiguate && !getLangOpts().ObjC1) |
| 421 | return CAK_AttributeSpecifier; |
| 422 | |
| 423 | TentativeParsingAction PA(*this); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 424 | |
| 425 | // Opening brackets were checked for above. |
| 426 | ConsumeBracket(); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 427 | |
| 428 | // Outside Obj-C++11, treat anything with a matching ']]' as an attribute. |
| 429 | if (!getLangOpts().ObjC1) { |
| 430 | ConsumeBracket(); |
| 431 | |
| 432 | bool IsAttribute = SkipUntil(tok::r_square, false); |
| 433 | IsAttribute &= Tok.is(tok::r_square); |
| 434 | |
| 435 | PA.Revert(); |
| 436 | |
| 437 | return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier; |
| 438 | } |
| 439 | |
| 440 | // In Obj-C++11, we need to distinguish four situations: |
| 441 | // 1a) int x[[attr]]; C++11 attribute. |
| 442 | // 1b) [[attr]]; C++11 statement attribute. |
| 443 | // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index. |
| 444 | // 3a) int x[[obj get]]; Message send in array size/index. |
| 445 | // 3b) [[Class alloc] init]; Message send in message send. |
| 446 | // 4) [[obj]{ return self; }() doStuff]; Lambda in message send. |
| 447 | // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted. |
| 448 | |
| 449 | // If we have a lambda-introducer, then this is definitely not a message send. |
| 450 | // FIXME: If this disambiguation is too slow, fold the tentative lambda parse |
| 451 | // into the tentative attribute parse below. |
| 452 | LambdaIntroducer Intro; |
| 453 | if (!TryParseLambdaIntroducer(Intro)) { |
| 454 | // A lambda cannot end with ']]', and an attribute must. |
| 455 | bool IsAttribute = Tok.is(tok::r_square); |
| 456 | |
| 457 | PA.Revert(); |
| 458 | |
| 459 | if (IsAttribute) |
| 460 | // Case 1: C++11 attribute. |
| 461 | return CAK_AttributeSpecifier; |
| 462 | |
| 463 | if (OuterMightBeMessageSend) |
| 464 | // Case 4: Lambda in message send. |
| 465 | return CAK_NotAttributeSpecifier; |
| 466 | |
| 467 | // Case 2: Lambda in array size / index. |
| 468 | return CAK_InvalidAttributeSpecifier; |
| 469 | } |
| 470 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 471 | ConsumeBracket(); |
| 472 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 473 | // If we don't have a lambda-introducer, then we have an attribute or a |
| 474 | // message-send. |
| 475 | bool IsAttribute = true; |
| 476 | while (Tok.isNot(tok::r_square)) { |
| 477 | if (Tok.is(tok::comma)) { |
| 478 | // Case 1: Stray commas can only occur in attributes. |
| 479 | PA.Revert(); |
| 480 | return CAK_AttributeSpecifier; |
| 481 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 482 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 483 | // Parse the attribute-token, if present. |
| 484 | // C++11 [dcl.attr.grammar]: |
| 485 | // If a keyword or an alternative token that satisfies the syntactic |
| 486 | // requirements of an identifier is contained in an attribute-token, |
| 487 | // it is considered an identifier. |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 488 | SourceLocation Loc; |
| 489 | if (!TryParseCXX11AttributeIdentifier(Loc)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 490 | IsAttribute = false; |
| 491 | break; |
| 492 | } |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 493 | if (Tok.is(tok::coloncolon)) { |
| 494 | ConsumeToken(); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 495 | if (!TryParseCXX11AttributeIdentifier(Loc)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 496 | IsAttribute = false; |
| 497 | break; |
| 498 | } |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 499 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 500 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 501 | // Parse the attribute-argument-clause, if present. |
| 502 | if (Tok.is(tok::l_paren)) { |
| 503 | ConsumeParen(); |
| 504 | if (!SkipUntil(tok::r_paren, false)) { |
| 505 | IsAttribute = false; |
| 506 | break; |
| 507 | } |
| 508 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 509 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 510 | if (Tok.is(tok::ellipsis)) |
| 511 | ConsumeToken(); |
| 512 | |
| 513 | if (Tok.isNot(tok::comma)) |
| 514 | break; |
| 515 | |
| 516 | ConsumeToken(); |
| 517 | } |
| 518 | |
| 519 | // An attribute must end ']]'. |
| 520 | if (IsAttribute) { |
| 521 | if (Tok.is(tok::r_square)) { |
| 522 | ConsumeBracket(); |
| 523 | IsAttribute = Tok.is(tok::r_square); |
| 524 | } else { |
| 525 | IsAttribute = false; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | PA.Revert(); |
| 530 | |
| 531 | if (IsAttribute) |
| 532 | // Case 1: C++11 statement attribute. |
| 533 | return CAK_AttributeSpecifier; |
| 534 | |
| 535 | // Case 3: Message send. |
| 536 | return CAK_NotAttributeSpecifier; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 539 | /// declarator: |
| 540 | /// direct-declarator |
| 541 | /// ptr-operator declarator |
| 542 | /// |
| 543 | /// direct-declarator: |
| 544 | /// declarator-id |
| 545 | /// direct-declarator '(' parameter-declaration-clause ')' |
| 546 | /// cv-qualifier-seq[opt] exception-specification[opt] |
| 547 | /// direct-declarator '[' constant-expression[opt] ']' |
| 548 | /// '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 549 | /// [GNU] '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 550 | /// |
| 551 | /// abstract-declarator: |
| 552 | /// ptr-operator abstract-declarator[opt] |
| 553 | /// direct-abstract-declarator |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 554 | /// ... |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 555 | /// |
| 556 | /// direct-abstract-declarator: |
| 557 | /// direct-abstract-declarator[opt] |
| 558 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 559 | /// exception-specification[opt] |
| 560 | /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 561 | /// '(' abstract-declarator ')' |
| 562 | /// |
| 563 | /// ptr-operator: |
| 564 | /// '*' cv-qualifier-seq[opt] |
| 565 | /// '&' |
| 566 | /// [C++0x] '&&' [TODO] |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 567 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 568 | /// |
| 569 | /// cv-qualifier-seq: |
| 570 | /// cv-qualifier cv-qualifier-seq[opt] |
| 571 | /// |
| 572 | /// cv-qualifier: |
| 573 | /// 'const' |
| 574 | /// 'volatile' |
| 575 | /// |
| 576 | /// declarator-id: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 577 | /// '...'[opt] id-expression |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 578 | /// |
| 579 | /// id-expression: |
| 580 | /// unqualified-id |
| 581 | /// qualified-id [TODO] |
| 582 | /// |
| 583 | /// unqualified-id: |
| 584 | /// identifier |
| 585 | /// operator-function-id [TODO] |
| 586 | /// conversion-function-id [TODO] |
| 587 | /// '~' class-name [TODO] |
| 588 | /// template-id [TODO] |
| 589 | /// |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 590 | Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, |
| 591 | bool mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 592 | // declarator: |
| 593 | // direct-declarator |
| 594 | // ptr-operator declarator |
| 595 | |
| 596 | while (1) { |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 597 | if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 598 | if (TryAnnotateCXXScopeToken(true)) |
| 599 | return TPResult::Error(); |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 601 | 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] | 602 | Tok.is(tok::ampamp) || |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 603 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 604 | // ptr-operator |
| 605 | ConsumeToken(); |
| 606 | while (Tok.is(tok::kw_const) || |
| 607 | Tok.is(tok::kw_volatile) || |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 608 | Tok.is(tok::kw_restrict)) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 609 | ConsumeToken(); |
| 610 | } else { |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // direct-declarator: |
| 616 | // direct-abstract-declarator: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 617 | if (Tok.is(tok::ellipsis)) |
| 618 | ConsumeToken(); |
| 619 | |
Argyrios Kyrtzidis | 1e05421 | 2009-07-21 17:05:03 +0000 | [diff] [blame] | 620 | if ((Tok.is(tok::identifier) || |
| 621 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) && |
| 622 | mayHaveIdentifier) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 623 | // declarator-id |
Argyrios Kyrtzidis | 1e05421 | 2009-07-21 17:05:03 +0000 | [diff] [blame] | 624 | if (Tok.is(tok::annot_cxxscope)) |
| 625 | ConsumeToken(); |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 626 | else |
| 627 | TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 628 | ConsumeToken(); |
| 629 | } else if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 630 | ConsumeParen(); |
| 631 | if (mayBeAbstract && |
| 632 | (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 633 | // 'int(...)' is a function. |
| 634 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) || |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 635 | isDeclarationSpecifier())) { // 'int(int)' is a function. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 636 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 637 | // exception-specification[opt] |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 638 | TPResult TPR = TryParseFunctionDeclarator(); |
| 639 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 640 | return TPR; |
| 641 | } else { |
| 642 | // '(' declarator ')' |
Argyrios Kyrtzidis | 1ee2c43 | 2008-10-05 14:27:18 +0000 | [diff] [blame] | 643 | // '(' attributes declarator ')' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 644 | // '(' abstract-declarator ')' |
Chris Lattner | 6229c8e | 2010-09-28 23:35:09 +0000 | [diff] [blame] | 645 | if (Tok.is(tok::kw___attribute) || |
| 646 | Tok.is(tok::kw___declspec) || |
| 647 | Tok.is(tok::kw___cdecl) || |
| 648 | Tok.is(tok::kw___stdcall) || |
| 649 | Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 650 | Tok.is(tok::kw___thiscall) || |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 651 | Tok.is(tok::kw___unaligned)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 652 | return TPResult::True(); // attributes indicate declaration |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 653 | TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 654 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 655 | return TPR; |
| 656 | if (Tok.isNot(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 657 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 658 | ConsumeParen(); |
| 659 | } |
| 660 | } else if (!mayBeAbstract) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 661 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | while (1) { |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 665 | TPResult TPR(TPResult::Ambiguous()); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 666 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 667 | // abstract-declarator: ... |
| 668 | if (Tok.is(tok::ellipsis)) |
| 669 | ConsumeToken(); |
| 670 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 671 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 672 | // Check whether we have a function declarator or a possible ctor-style |
| 673 | // initializer that follows the declarator. Note that ctor-style |
| 674 | // initializers are not possible in contexts where abstract declarators |
| 675 | // are allowed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 676 | if (!mayBeAbstract && !isCXXFunctionDeclarator()) |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 677 | break; |
| 678 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 679 | // direct-declarator '(' parameter-declaration-clause ')' |
| 680 | // cv-qualifier-seq[opt] exception-specification[opt] |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 681 | ConsumeParen(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 682 | TPR = TryParseFunctionDeclarator(); |
| 683 | } else if (Tok.is(tok::l_square)) { |
| 684 | // direct-declarator '[' constant-expression[opt] ']' |
| 685 | // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' |
| 686 | TPR = TryParseBracketDeclarator(); |
| 687 | } else { |
| 688 | break; |
| 689 | } |
| 690 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 691 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 692 | return TPR; |
| 693 | } |
| 694 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 695 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 698 | Parser::TPResult |
| 699 | Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) { |
| 700 | switch (Kind) { |
| 701 | // Obviously starts an expression. |
| 702 | case tok::numeric_constant: |
| 703 | case tok::char_constant: |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 704 | case tok::wide_char_constant: |
| 705 | case tok::utf16_char_constant: |
| 706 | case tok::utf32_char_constant: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 707 | case tok::string_literal: |
| 708 | case tok::wide_string_literal: |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 709 | case tok::utf8_string_literal: |
| 710 | case tok::utf16_string_literal: |
| 711 | case tok::utf32_string_literal: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 712 | case tok::l_square: |
| 713 | case tok::l_paren: |
| 714 | case tok::amp: |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 715 | case tok::ampamp: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 716 | case tok::star: |
| 717 | case tok::plus: |
| 718 | case tok::plusplus: |
| 719 | case tok::minus: |
| 720 | case tok::minusminus: |
| 721 | case tok::tilde: |
| 722 | case tok::exclaim: |
| 723 | case tok::kw_sizeof: |
| 724 | case tok::kw___func__: |
| 725 | case tok::kw_const_cast: |
| 726 | case tok::kw_delete: |
| 727 | case tok::kw_dynamic_cast: |
| 728 | case tok::kw_false: |
| 729 | case tok::kw_new: |
| 730 | case tok::kw_operator: |
| 731 | case tok::kw_reinterpret_cast: |
| 732 | case tok::kw_static_cast: |
| 733 | case tok::kw_this: |
| 734 | case tok::kw_throw: |
| 735 | case tok::kw_true: |
| 736 | case tok::kw_typeid: |
| 737 | case tok::kw_alignof: |
| 738 | case tok::kw_noexcept: |
| 739 | case tok::kw_nullptr: |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 740 | case tok::kw__Alignof: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 741 | case tok::kw___null: |
| 742 | case tok::kw___alignof: |
| 743 | case tok::kw___builtin_choose_expr: |
| 744 | case tok::kw___builtin_offsetof: |
| 745 | case tok::kw___builtin_types_compatible_p: |
| 746 | case tok::kw___builtin_va_arg: |
| 747 | case tok::kw___imag: |
| 748 | case tok::kw___real: |
| 749 | case tok::kw___FUNCTION__: |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 750 | case tok::kw_L__FUNCTION__: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 751 | case tok::kw___PRETTY_FUNCTION__: |
| 752 | case tok::kw___has_nothrow_assign: |
| 753 | case tok::kw___has_nothrow_copy: |
| 754 | case tok::kw___has_nothrow_constructor: |
| 755 | case tok::kw___has_trivial_assign: |
| 756 | case tok::kw___has_trivial_copy: |
| 757 | case tok::kw___has_trivial_constructor: |
| 758 | case tok::kw___has_trivial_destructor: |
| 759 | case tok::kw___has_virtual_destructor: |
| 760 | case tok::kw___is_abstract: |
| 761 | case tok::kw___is_base_of: |
| 762 | case tok::kw___is_class: |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 763 | case tok::kw___is_convertible_to: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 764 | case tok::kw___is_empty: |
| 765 | case tok::kw___is_enum: |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 766 | case tok::kw___is_final: |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 767 | case tok::kw___is_literal: |
Chandler Carruth | 3840281 | 2011-04-24 02:49:28 +0000 | [diff] [blame] | 768 | case tok::kw___is_literal_type: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 769 | case tok::kw___is_pod: |
| 770 | case tok::kw___is_polymorphic: |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 771 | case tok::kw___is_trivial: |
Douglas Gregor | 25d0a0f | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 772 | case tok::kw___is_trivially_assignable: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 773 | case tok::kw___is_trivially_constructible: |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 774 | case tok::kw___is_trivially_copyable: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 775 | case tok::kw___is_union: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 776 | case tok::kw___uuidof: |
| 777 | return TPResult::True(); |
| 778 | |
| 779 | // Obviously starts a type-specifier-seq: |
| 780 | case tok::kw_char: |
| 781 | case tok::kw_const: |
| 782 | case tok::kw_double: |
| 783 | case tok::kw_enum: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 784 | case tok::kw_half: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 785 | case tok::kw_float: |
| 786 | case tok::kw_int: |
| 787 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 788 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 789 | case tok::kw___int128: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 790 | case tok::kw_restrict: |
| 791 | case tok::kw_short: |
| 792 | case tok::kw_signed: |
| 793 | case tok::kw_struct: |
| 794 | case tok::kw_union: |
| 795 | case tok::kw_unsigned: |
| 796 | case tok::kw_void: |
| 797 | case tok::kw_volatile: |
| 798 | case tok::kw__Bool: |
| 799 | case tok::kw__Complex: |
| 800 | case tok::kw_class: |
| 801 | case tok::kw_typename: |
| 802 | case tok::kw_wchar_t: |
| 803 | case tok::kw_char16_t: |
| 804 | case tok::kw_char32_t: |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 805 | case tok::kw___underlying_type: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 806 | case tok::kw_thread_local: |
| 807 | case tok::kw__Decimal32: |
| 808 | case tok::kw__Decimal64: |
| 809 | case tok::kw__Decimal128: |
| 810 | case tok::kw___thread: |
| 811 | case tok::kw_typeof: |
| 812 | case tok::kw___cdecl: |
| 813 | case tok::kw___stdcall: |
| 814 | case tok::kw___fastcall: |
| 815 | case tok::kw___thiscall: |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 816 | case tok::kw___unaligned: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 817 | case tok::kw___vector: |
| 818 | case tok::kw___pixel: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 819 | case tok::kw__Atomic: |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 820 | return TPResult::False(); |
| 821 | |
| 822 | default: |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | return TPResult::Ambiguous(); |
| 827 | } |
| 828 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 829 | bool Parser::isTentativelyDeclared(IdentifierInfo *II) { |
| 830 | return std::find(TentativelyDeclaredIdentifiers.begin(), |
| 831 | TentativelyDeclaredIdentifiers.end(), II) |
| 832 | != TentativelyDeclaredIdentifiers.end(); |
| 833 | } |
| 834 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 835 | /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration |
| 836 | /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could |
| 837 | /// be either a decl-specifier or a function-style cast, and TPResult::Error() |
| 838 | /// if a parsing error was found and reported. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 839 | /// |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 840 | /// If HasMissingTypename is provided, a name with a dependent scope specifier |
| 841 | /// will be treated as ambiguous if the 'typename' keyword is missing. If this |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 842 | /// happens, *HasMissingTypename will be set to 'true'. This will also be used |
| 843 | /// as an indicator that undeclared identifiers (which will trigger a later |
| 844 | /// parse error) should be treated as types. Returns TPResult::Ambiguous() in |
| 845 | /// such cases. |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 846 | /// |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 847 | /// decl-specifier: |
| 848 | /// storage-class-specifier |
| 849 | /// type-specifier |
| 850 | /// function-specifier |
| 851 | /// 'friend' |
| 852 | /// 'typedef' |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 853 | /// [C++0x] 'constexpr' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 854 | /// [GNU] attributes declaration-specifiers[opt] |
| 855 | /// |
| 856 | /// storage-class-specifier: |
| 857 | /// 'register' |
| 858 | /// 'static' |
| 859 | /// 'extern' |
| 860 | /// 'mutable' |
| 861 | /// 'auto' |
| 862 | /// [GNU] '__thread' |
| 863 | /// |
| 864 | /// function-specifier: |
| 865 | /// 'inline' |
| 866 | /// 'virtual' |
| 867 | /// 'explicit' |
| 868 | /// |
| 869 | /// typedef-name: |
| 870 | /// identifier |
| 871 | /// |
| 872 | /// type-specifier: |
| 873 | /// simple-type-specifier |
| 874 | /// class-specifier |
| 875 | /// enum-specifier |
| 876 | /// elaborated-type-specifier |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 877 | /// typename-specifier |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 878 | /// cv-qualifier |
| 879 | /// |
| 880 | /// simple-type-specifier: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 881 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 882 | /// '::'[opt] nested-name-specifier 'template' |
| 883 | /// simple-template-id [TODO] |
| 884 | /// 'char' |
| 885 | /// 'wchar_t' |
| 886 | /// 'bool' |
| 887 | /// 'short' |
| 888 | /// 'int' |
| 889 | /// 'long' |
| 890 | /// 'signed' |
| 891 | /// 'unsigned' |
| 892 | /// 'float' |
| 893 | /// 'double' |
| 894 | /// 'void' |
| 895 | /// [GNU] typeof-specifier |
| 896 | /// [GNU] '_Complex' |
| 897 | /// [C++0x] 'auto' [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 898 | /// [C++0x] 'decltype' ( expression ) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 899 | /// |
| 900 | /// type-name: |
| 901 | /// class-name |
| 902 | /// enum-name |
| 903 | /// typedef-name |
| 904 | /// |
| 905 | /// elaborated-type-specifier: |
| 906 | /// class-key '::'[opt] nested-name-specifier[opt] identifier |
| 907 | /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] |
| 908 | /// simple-template-id |
| 909 | /// 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 910 | /// |
| 911 | /// enum-name: |
| 912 | /// identifier |
| 913 | /// |
| 914 | /// enum-specifier: |
| 915 | /// 'enum' identifier[opt] '{' enumerator-list[opt] '}' |
| 916 | /// 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 917 | /// |
| 918 | /// class-specifier: |
| 919 | /// class-head '{' member-specification[opt] '}' |
| 920 | /// |
| 921 | /// class-head: |
| 922 | /// class-key identifier[opt] base-clause[opt] |
| 923 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 924 | /// class-key nested-name-specifier[opt] simple-template-id |
| 925 | /// base-clause[opt] |
| 926 | /// |
| 927 | /// class-key: |
| 928 | /// 'class' |
| 929 | /// 'struct' |
| 930 | /// 'union' |
| 931 | /// |
| 932 | /// cv-qualifier: |
| 933 | /// 'const' |
| 934 | /// 'volatile' |
| 935 | /// [GNU] restrict |
| 936 | /// |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 937 | Parser::TPResult |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 938 | Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult, |
| 939 | bool *HasMissingTypename) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 940 | switch (Tok.getKind()) { |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 941 | case tok::identifier: { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 942 | // Check for need to substitute AltiVec __vector keyword |
| 943 | // for "vector" identifier. |
| 944 | if (TryAltiVecVectorToken()) |
| 945 | return TPResult::True(); |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 946 | |
| 947 | const Token &Next = NextToken(); |
| 948 | // In 'foo bar', 'foo' is always a type name outside of Objective-C. |
| 949 | if (!getLangOpts().ObjC1 && Next.is(tok::identifier)) |
| 950 | return TPResult::True(); |
| 951 | |
| 952 | if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) { |
| 953 | // Determine whether this is a valid expression. If not, we will hit |
| 954 | // a parse error one way or another. In that case, tell the caller that |
| 955 | // this is ambiguous. Typo-correct to type and expression keywords and |
| 956 | // to types and identifiers, in order to try to recover from errors. |
| 957 | CorrectionCandidateCallback TypoCorrection; |
| 958 | TypoCorrection.WantRemainingKeywords = false; |
| 959 | switch (TryAnnotateName(false /* no nested name specifier */, |
| 960 | &TypoCorrection)) { |
| 961 | case ANK_Error: |
| 962 | return TPResult::Error(); |
| 963 | case ANK_TentativeDecl: |
| 964 | return TPResult::False(); |
| 965 | case ANK_TemplateName: |
| 966 | // A bare type template-name which can't be a template template |
| 967 | // argument is an error, and was probably intended to be a type. |
| 968 | return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); |
| 969 | case ANK_Unresolved: |
| 970 | return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False(); |
| 971 | case ANK_Success: |
| 972 | break; |
| 973 | } |
| 974 | assert(Tok.isNot(tok::identifier) && |
| 975 | "TryAnnotateName succeeded without producing an annotation"); |
| 976 | } else { |
| 977 | // This might possibly be a type with a dependent scope specifier and |
| 978 | // a missing 'typename' keyword. Don't use TryAnnotateName in this case, |
| 979 | // since it will annotate as a primary expression, and we want to use the |
| 980 | // "missing 'typename'" logic. |
| 981 | if (TryAnnotateTypeOrScopeToken()) |
| 982 | return TPResult::Error(); |
| 983 | // If annotation failed, assume it's a non-type. |
| 984 | // FIXME: If this happens due to an undeclared identifier, treat it as |
| 985 | // ambiguous. |
| 986 | if (Tok.is(tok::identifier)) |
| 987 | return TPResult::False(); |
| 988 | } |
| 989 | |
| 990 | // We annotated this token as something. Recurse to handle whatever we got. |
| 991 | return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); |
| 992 | } |
| 993 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 994 | case tok::kw_typename: // typename T::type |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 995 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 996 | // recurse to handle whatever we get. |
| 997 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 998 | return TPResult::Error(); |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 999 | return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 2ee9b40 | 2009-12-19 01:11:05 +0000 | [diff] [blame] | 1001 | case tok::coloncolon: { // ::foo::bar |
| 1002 | const Token &Next = NextToken(); |
| 1003 | if (Next.is(tok::kw_new) || // ::new |
| 1004 | Next.is(tok::kw_delete)) // ::delete |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 1005 | return TPResult::False(); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1006 | } |
| 1007 | // Fall through. |
| 1008 | case tok::kw_decltype: |
Chris Lattner | e584926 | 2009-01-04 23:33:56 +0000 | [diff] [blame] | 1009 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1010 | // recurse to handle whatever we get. |
| 1011 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1012 | return TPResult::Error(); |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1013 | return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); |
Nick Lewycky | 443aac9 | 2012-01-25 01:19:14 +0000 | [diff] [blame] | 1014 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1015 | // decl-specifier: |
| 1016 | // storage-class-specifier |
| 1017 | // type-specifier |
| 1018 | // function-specifier |
| 1019 | // 'friend' |
| 1020 | // 'typedef' |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1021 | // 'constexpr' |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1022 | case tok::kw_friend: |
| 1023 | case tok::kw_typedef: |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1024 | case tok::kw_constexpr: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1025 | // storage-class-specifier |
| 1026 | case tok::kw_register: |
| 1027 | case tok::kw_static: |
| 1028 | case tok::kw_extern: |
| 1029 | case tok::kw_mutable: |
| 1030 | case tok::kw_auto: |
| 1031 | case tok::kw___thread: |
| 1032 | // function-specifier |
| 1033 | case tok::kw_inline: |
| 1034 | case tok::kw_virtual: |
| 1035 | case tok::kw_explicit: |
| 1036 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1037 | // Modules |
| 1038 | case tok::kw___module_private__: |
| 1039 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1040 | // type-specifier: |
| 1041 | // simple-type-specifier |
| 1042 | // class-specifier |
| 1043 | // enum-specifier |
| 1044 | // elaborated-type-specifier |
| 1045 | // typename-specifier |
| 1046 | // cv-qualifier |
| 1047 | |
| 1048 | // class-specifier |
| 1049 | // elaborated-type-specifier |
| 1050 | case tok::kw_class: |
| 1051 | case tok::kw_struct: |
| 1052 | case tok::kw_union: |
| 1053 | // enum-specifier |
| 1054 | case tok::kw_enum: |
| 1055 | // cv-qualifier |
| 1056 | case tok::kw_const: |
| 1057 | case tok::kw_volatile: |
| 1058 | |
| 1059 | // GNU |
| 1060 | case tok::kw_restrict: |
| 1061 | case tok::kw__Complex: |
| 1062 | case tok::kw___attribute: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1063 | return TPResult::True(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 1065 | // Microsoft |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 1066 | case tok::kw___declspec: |
Steve Naroff | 7ec5658 | 2009-01-06 17:40:00 +0000 | [diff] [blame] | 1067 | case tok::kw___cdecl: |
| 1068 | case tok::kw___stdcall: |
| 1069 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1070 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1071 | case tok::kw___w64: |
| 1072 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 1073 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1074 | case tok::kw___forceinline: |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1075 | case tok::kw___unaligned: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1076 | return TPResult::True(); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Borland |
| 1079 | case tok::kw___pascal: |
| 1080 | return TPResult::True(); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1081 | |
| 1082 | // AltiVec |
| 1083 | case tok::kw___vector: |
| 1084 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1085 | |
John McCall | 51e2a5d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 1086 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1087 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
John McCall | 51e2a5d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 1088 | if (TemplateId->Kind != TNK_Type_template) |
| 1089 | return TPResult::False(); |
| 1090 | CXXScopeSpec SS; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1091 | AnnotateTemplateIdTokenAsType(); |
John McCall | 51e2a5d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 1092 | assert(Tok.is(tok::annot_typename)); |
| 1093 | goto case_typename; |
| 1094 | } |
| 1095 | |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 1096 | case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed |
| 1097 | // We've already annotated a scope; try to annotate a type. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1098 | if (TryAnnotateTypeOrScopeToken()) |
| 1099 | return TPResult::Error(); |
Nick Lewycky | 443aac9 | 2012-01-25 01:19:14 +0000 | [diff] [blame] | 1100 | if (!Tok.is(tok::annot_typename)) { |
| 1101 | // If the next token is an identifier or a type qualifier, then this |
| 1102 | // can't possibly be a valid expression either. |
| 1103 | if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) { |
| 1104 | CXXScopeSpec SS; |
| 1105 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1106 | Tok.getAnnotationRange(), |
| 1107 | SS); |
| 1108 | if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) { |
| 1109 | TentativeParsingAction PA(*this); |
| 1110 | ConsumeToken(); |
| 1111 | ConsumeToken(); |
| 1112 | bool isIdentifier = Tok.is(tok::identifier); |
| 1113 | TPResult TPR = TPResult::False(); |
| 1114 | if (!isIdentifier) |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1115 | TPR = isCXXDeclarationSpecifier(BracedCastResult, |
| 1116 | HasMissingTypename); |
Nick Lewycky | 443aac9 | 2012-01-25 01:19:14 +0000 | [diff] [blame] | 1117 | PA.Revert(); |
| 1118 | |
| 1119 | if (isIdentifier || |
| 1120 | TPR == TPResult::True() || TPR == TPResult::Error()) |
| 1121 | return TPResult::Error(); |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (HasMissingTypename) { |
| 1124 | // We can't tell whether this is a missing 'typename' or a valid |
| 1125 | // expression. |
| 1126 | *HasMissingTypename = true; |
| 1127 | return TPResult::Ambiguous(); |
| 1128 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame^] | 1129 | } else { |
| 1130 | // Try to resolve the name. If it doesn't exist, assume it was |
| 1131 | // intended to name a type and keep disambiguating. |
| 1132 | switch (TryAnnotateName(false /* SS is not dependent */)) { |
| 1133 | case ANK_Error: |
| 1134 | return TPResult::Error(); |
| 1135 | case ANK_TentativeDecl: |
| 1136 | return TPResult::False(); |
| 1137 | case ANK_TemplateName: |
| 1138 | // A bare type template-name which can't be a template template |
| 1139 | // argument is an error, and was probably intended to be a type. |
| 1140 | return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); |
| 1141 | case ANK_Unresolved: |
| 1142 | return HasMissingTypename ? TPResult::Ambiguous() |
| 1143 | : TPResult::False(); |
| 1144 | case ANK_Success: |
| 1145 | // Annotated it, check again. |
| 1146 | assert(Tok.isNot(tok::annot_cxxscope) || |
| 1147 | NextToken().isNot(tok::identifier)); |
| 1148 | return isCXXDeclarationSpecifier(BracedCastResult, |
| 1149 | HasMissingTypename); |
| 1150 | } |
Nick Lewycky | 443aac9 | 2012-01-25 01:19:14 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 1153 | return TPResult::False(); |
Nick Lewycky | 443aac9 | 2012-01-25 01:19:14 +0000 | [diff] [blame] | 1154 | } |
John McCall | ae03cb5 | 2009-12-19 00:35:18 +0000 | [diff] [blame] | 1155 | // If that succeeded, fallthrough into the generic simple-type-id case. |
| 1156 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1157 | // The ambiguity resides in a simple-type-specifier/typename-specifier |
| 1158 | // followed by a '('. The '(' could either be the start of: |
| 1159 | // |
| 1160 | // direct-declarator: |
| 1161 | // '(' declarator ')' |
| 1162 | // |
| 1163 | // direct-abstract-declarator: |
| 1164 | // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 1165 | // exception-specification[opt] |
| 1166 | // '(' abstract-declarator ')' |
| 1167 | // |
| 1168 | // or part of a function-style cast expression: |
| 1169 | // |
| 1170 | // simple-type-specifier '(' expression-list[opt] ')' |
| 1171 | // |
| 1172 | |
| 1173 | // simple-type-specifier: |
| 1174 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1175 | case tok::annot_typename: |
| 1176 | case_typename: |
| 1177 | // In Objective-C, we might have a protocol-qualified type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1178 | if (getLangOpts().ObjC1 && NextToken().is(tok::less)) { |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1179 | // Tentatively parse the |
| 1180 | TentativeParsingAction PA(*this); |
| 1181 | ConsumeToken(); // The type token |
| 1182 | |
| 1183 | TPResult TPR = TryParseProtocolQualifiers(); |
| 1184 | bool isFollowedByParen = Tok.is(tok::l_paren); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1185 | bool isFollowedByBrace = Tok.is(tok::l_brace); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1186 | |
| 1187 | PA.Revert(); |
| 1188 | |
| 1189 | if (TPR == TPResult::Error()) |
| 1190 | return TPResult::Error(); |
| 1191 | |
| 1192 | if (isFollowedByParen) |
| 1193 | return TPResult::Ambiguous(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1194 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1195 | if (getLangOpts().CPlusPlus0x && isFollowedByBrace) |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1196 | return BracedCastResult; |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1197 | |
| 1198 | return TPResult::True(); |
| 1199 | } |
| 1200 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1201 | case tok::kw_char: |
| 1202 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1203 | case tok::kw_char16_t: |
| 1204 | case tok::kw_char32_t: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1205 | case tok::kw_bool: |
| 1206 | case tok::kw_short: |
| 1207 | case tok::kw_int: |
| 1208 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 1209 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 1210 | case tok::kw___int128: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1211 | case tok::kw_signed: |
| 1212 | case tok::kw_unsigned: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1213 | case tok::kw_half: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1214 | case tok::kw_float: |
| 1215 | case tok::kw_double: |
| 1216 | case tok::kw_void: |
David Blaikie | 5e089fe | 2012-01-24 05:47:35 +0000 | [diff] [blame] | 1217 | case tok::annot_decltype: |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1218 | if (NextToken().is(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1219 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1220 | |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1221 | // This is a function-style cast in all cases we disambiguate other than |
| 1222 | // one: |
| 1223 | // struct S { |
| 1224 | // enum E : int { a = 4 }; // enum |
| 1225 | // enum E : int { 4 }; // bit-field |
| 1226 | // }; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1227 | if (getLangOpts().CPlusPlus0x && NextToken().is(tok::l_brace)) |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1228 | return BracedCastResult; |
| 1229 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 1230 | if (isStartOfObjCClassMessageMissingOpenBracket()) |
| 1231 | return TPResult::False(); |
| 1232 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1233 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1234 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1235 | // GNU typeof support. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1236 | case tok::kw_typeof: { |
| 1237 | if (NextToken().isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1238 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1239 | |
| 1240 | TentativeParsingAction PA(*this); |
| 1241 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1242 | TPResult TPR = TryParseTypeofSpecifier(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1243 | bool isFollowedByParen = Tok.is(tok::l_paren); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1244 | bool isFollowedByBrace = Tok.is(tok::l_brace); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1245 | |
| 1246 | PA.Revert(); |
| 1247 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1248 | if (TPR == TPResult::Error()) |
| 1249 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1250 | |
| 1251 | if (isFollowedByParen) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1252 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1253 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1254 | if (getLangOpts().CPlusPlus0x && isFollowedByBrace) |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1255 | return BracedCastResult; |
| 1256 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1257 | return TPResult::True(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1260 | // C++0x type traits support |
| 1261 | case tok::kw___underlying_type: |
| 1262 | return TPResult::True(); |
| 1263 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1264 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1265 | case tok::kw__Atomic: |
| 1266 | return TPResult::True(); |
| 1267 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1268 | default: |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1269 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /// [GNU] typeof-specifier: |
| 1274 | /// 'typeof' '(' expressions ')' |
| 1275 | /// 'typeof' '(' type-name ')' |
| 1276 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1277 | Parser::TPResult Parser::TryParseTypeofSpecifier() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1278 | assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); |
| 1279 | ConsumeToken(); |
| 1280 | |
| 1281 | assert(Tok.is(tok::l_paren) && "Expected '('"); |
| 1282 | // Parse through the parens after 'typeof'. |
| 1283 | ConsumeParen(); |
| 1284 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1285 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1286 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1287 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1290 | /// [ObjC] protocol-qualifiers: |
| 1291 | //// '<' identifier-list '>' |
| 1292 | Parser::TPResult Parser::TryParseProtocolQualifiers() { |
| 1293 | assert(Tok.is(tok::less) && "Expected '<' for qualifier list"); |
| 1294 | ConsumeToken(); |
| 1295 | do { |
| 1296 | if (Tok.isNot(tok::identifier)) |
| 1297 | return TPResult::Error(); |
| 1298 | ConsumeToken(); |
| 1299 | |
| 1300 | if (Tok.is(tok::comma)) { |
| 1301 | ConsumeToken(); |
| 1302 | continue; |
| 1303 | } |
| 1304 | |
| 1305 | if (Tok.is(tok::greater)) { |
| 1306 | ConsumeToken(); |
| 1307 | return TPResult::Ambiguous(); |
| 1308 | } |
| 1309 | } while (false); |
| 1310 | |
| 1311 | return TPResult::Error(); |
| 1312 | } |
| 1313 | |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1314 | Parser::TPResult |
| 1315 | Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) { |
| 1316 | TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(), |
| 1317 | HasMissingTypename); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1318 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1319 | return TPR; |
| 1320 | |
| 1321 | if (Tok.is(tok::kw_typeof)) |
| 1322 | TryParseTypeofSpecifier(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1323 | else { |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1324 | if (Tok.is(tok::annot_cxxscope)) |
| 1325 | ConsumeToken(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1326 | ConsumeToken(); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1327 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1328 | if (getLangOpts().ObjC1 && Tok.is(tok::less)) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1329 | TryParseProtocolQualifiers(); |
| 1330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1332 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /// isCXXFunctionDeclarator - Disambiguates between a function declarator or |
| 1336 | /// a constructor-style initializer, when parsing declaration statements. |
| 1337 | /// Returns true for function declarator and false for constructor-style |
| 1338 | /// initializer. |
| 1339 | /// If during the disambiguation process a parsing error is encountered, |
| 1340 | /// the function returns true to let the declaration parsing code handle it. |
| 1341 | /// |
| 1342 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 1343 | /// exception-specification[opt] |
| 1344 | /// |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 1345 | bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) { |
Argyrios Kyrtzidis | d3dbbb6 | 2008-10-05 21:10:08 +0000 | [diff] [blame] | 1346 | |
| 1347 | // C++ 8.2p1: |
| 1348 | // The ambiguity arising from the similarity between a function-style cast and |
| 1349 | // a declaration mentioned in 6.8 can also occur in the context of a |
| 1350 | // declaration. In that context, the choice is between a function declaration |
| 1351 | // with a redundant set of parentheses around a parameter name and an object |
| 1352 | // declaration with a function-style cast as the initializer. Just as for the |
| 1353 | // ambiguities mentioned in 6.8, the resolution is to consider any construct |
| 1354 | // that could possibly be a declaration a declaration. |
| 1355 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1356 | TentativeParsingAction PA(*this); |
| 1357 | |
| 1358 | ConsumeParen(); |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1359 | bool InvalidAsDeclaration = false; |
| 1360 | TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration); |
| 1361 | if (TPR == TPResult::Ambiguous()) { |
| 1362 | if (Tok.isNot(tok::r_paren)) |
| 1363 | TPR = TPResult::False(); |
| 1364 | else { |
| 1365 | const Token &Next = NextToken(); |
| 1366 | if (Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 1367 | Next.is(tok::kw_const) || Next.is(tok::kw_volatile) || |
| 1368 | Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) || |
| 1369 | Next.is(tok::l_square) || isCXX0XVirtSpecifier(Next) || |
| 1370 | Next.is(tok::l_brace) || Next.is(tok::kw_try) || |
Richard Smith | 5969a5f | 2012-07-23 21:41:30 +0000 | [diff] [blame] | 1371 | Next.is(tok::equal) || Next.is(tok::arrow)) |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1372 | // The next token cannot appear after a constructor-style initializer, |
| 1373 | // and can appear next in a function definition. This must be a function |
| 1374 | // declarator. |
| 1375 | TPR = TPResult::True(); |
| 1376 | else if (InvalidAsDeclaration) |
| 1377 | // Use the absence of 'typename' as a tie-breaker. |
| 1378 | TPR = TPResult::False(); |
| 1379 | } |
| 1380 | } |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1381 | |
| 1382 | PA.Revert(); |
| 1383 | |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 1384 | if (IsAmbiguous && TPR == TPResult::Ambiguous()) |
| 1385 | *IsAmbiguous = true; |
| 1386 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1387 | // In case of an error, let the declaration parsing code handle it. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 1388 | return TPR != TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /// parameter-declaration-clause: |
| 1392 | /// parameter-declaration-list[opt] '...'[opt] |
| 1393 | /// parameter-declaration-list ',' '...' |
| 1394 | /// |
| 1395 | /// parameter-declaration-list: |
| 1396 | /// parameter-declaration |
| 1397 | /// parameter-declaration-list ',' parameter-declaration |
| 1398 | /// |
| 1399 | /// parameter-declaration: |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1400 | /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] |
| 1401 | /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] |
Argyrios Kyrtzidis | 346af03 | 2010-12-08 02:02:46 +0000 | [diff] [blame] | 1402 | /// '=' assignment-expression |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1403 | /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] |
| 1404 | /// attributes[opt] |
| 1405 | /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] |
| 1406 | /// attributes[opt] '=' assignment-expression |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1407 | /// |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1408 | Parser::TPResult |
| 1409 | Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1410 | |
| 1411 | if (Tok.is(tok::r_paren)) |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 1412 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1413 | |
| 1414 | // parameter-declaration-list[opt] '...'[opt] |
| 1415 | // parameter-declaration-list ',' '...' |
| 1416 | // |
| 1417 | // parameter-declaration-list: |
| 1418 | // parameter-declaration |
| 1419 | // parameter-declaration-list ',' parameter-declaration |
| 1420 | // |
| 1421 | while (1) { |
| 1422 | // '...'[opt] |
| 1423 | if (Tok.is(tok::ellipsis)) { |
| 1424 | ConsumeToken(); |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 1425 | if (Tok.is(tok::r_paren)) |
| 1426 | return TPResult::True(); // '...)' is a sign of a function declarator. |
| 1427 | else |
| 1428 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 1431 | // An attribute-specifier-seq here is a sign of a function declarator. |
| 1432 | if (isCXX11AttributeSpecifier(/*Disambiguate*/false, |
| 1433 | /*OuterMightBeMessageSend*/true)) |
| 1434 | return TPResult::True(); |
| 1435 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1436 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1437 | MaybeParseMicrosoftAttributes(attrs); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 1438 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1439 | // decl-specifier-seq |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1440 | // A parameter-declaration's initializer must be preceded by an '=', so |
| 1441 | // decl-specifier-seq '{' is not a parameter in C++11. |
Richard Smith | 25582fc | 2012-05-16 23:40:17 +0000 | [diff] [blame] | 1442 | TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1443 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1444 | return TPR; |
| 1445 | |
| 1446 | // declarator |
| 1447 | // abstract-declarator[opt] |
| 1448 | TPR = TryParseDeclarator(true/*mayBeAbstract*/); |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1449 | if (TPR != TPResult::Ambiguous()) |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1450 | return TPR; |
| 1451 | |
Argyrios Kyrtzidis | 346af03 | 2010-12-08 02:02:46 +0000 | [diff] [blame] | 1452 | // [GNU] attributes[opt] |
| 1453 | if (Tok.is(tok::kw___attribute)) |
| 1454 | return TPResult::True(); |
| 1455 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1456 | if (Tok.is(tok::equal)) { |
| 1457 | // '=' assignment-expression |
| 1458 | // Parse through assignment-expression. |
David Blaikie | eb52f86a | 2012-04-09 16:37:11 +0000 | [diff] [blame] | 1459 | if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/, |
| 1460 | true/*DontConsume*/)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1461 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | if (Tok.is(tok::ellipsis)) { |
| 1465 | ConsumeToken(); |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 1466 | if (Tok.is(tok::r_paren)) |
| 1467 | return TPResult::True(); // '...)' is a sign of a function declarator. |
| 1468 | else |
| 1469 | return TPResult::False(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | if (Tok.isNot(tok::comma)) |
| 1473 | break; |
| 1474 | ConsumeToken(); // the comma. |
| 1475 | } |
| 1476 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1477 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 1480 | /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue |
| 1481 | /// parsing as a function declarator. |
| 1482 | /// If TryParseFunctionDeclarator fully parsed the function declarator, it will |
| 1483 | /// return TPResult::Ambiguous(), otherwise it will return either False() or |
| 1484 | /// Error(). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | /// |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1486 | /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] |
| 1487 | /// exception-specification[opt] |
| 1488 | /// |
| 1489 | /// exception-specification: |
| 1490 | /// 'throw' '(' type-id-list[opt] ')' |
| 1491 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1492 | Parser::TPResult Parser::TryParseFunctionDeclarator() { |
Argyrios Kyrtzidis | d3616a8 | 2008-10-05 23:15:41 +0000 | [diff] [blame] | 1493 | |
| 1494 | // The '(' is already parsed. |
| 1495 | |
| 1496 | TPResult TPR = TryParseParameterDeclarationClause(); |
| 1497 | if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) |
| 1498 | TPR = TPResult::False(); |
| 1499 | |
| 1500 | if (TPR == TPResult::False() || TPR == TPResult::Error()) |
| 1501 | return TPR; |
| 1502 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1503 | // Parse through the parens. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1504 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1505 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1506 | |
| 1507 | // cv-qualifier-seq |
| 1508 | while (Tok.is(tok::kw_const) || |
| 1509 | Tok.is(tok::kw_volatile) || |
| 1510 | Tok.is(tok::kw_restrict) ) |
| 1511 | ConsumeToken(); |
| 1512 | |
Douglas Gregor | e3c7a7c | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1513 | // ref-qualifier[opt] |
| 1514 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) |
| 1515 | ConsumeToken(); |
| 1516 | |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1517 | // exception-specification |
| 1518 | if (Tok.is(tok::kw_throw)) { |
| 1519 | ConsumeToken(); |
| 1520 | if (Tok.isNot(tok::l_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1521 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1522 | |
| 1523 | // Parse through the parens after 'throw'. |
| 1524 | ConsumeParen(); |
| 1525 | if (!SkipUntil(tok::r_paren)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1526 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1527 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1528 | if (Tok.is(tok::kw_noexcept)) { |
| 1529 | ConsumeToken(); |
| 1530 | // Possibly an expression as well. |
| 1531 | if (Tok.is(tok::l_paren)) { |
| 1532 | // Find the matching rparen. |
| 1533 | ConsumeParen(); |
| 1534 | if (!SkipUntil(tok::r_paren)) |
| 1535 | return TPResult::Error(); |
| 1536 | } |
| 1537 | } |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1538 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1539 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /// '[' constant-expression[opt] ']' |
| 1543 | /// |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1544 | Parser::TPResult Parser::TryParseBracketDeclarator() { |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1545 | ConsumeBracket(); |
| 1546 | if (!SkipUntil(tok::r_square)) |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1547 | return TPResult::Error(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1548 | |
Argyrios Kyrtzidis | b9f3419 | 2008-10-05 18:52:21 +0000 | [diff] [blame] | 1549 | return TPResult::Ambiguous(); |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1550 | } |