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