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