1193326Sed//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements the tentative parsing portions of the Parser 11193326Sed// interfaces, for ambiguity resolution. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15193326Sed#include "clang/Parse/Parser.h" 16193326Sed#include "clang/Parse/ParseDiagnostic.h" 17212904Sdim#include "clang/Sema/ParsedTemplate.h" 18193326Sedusing namespace clang; 19193326Sed 20193326Sed/// isCXXDeclarationStatement - C++-specialized function that disambiguates 21193326Sed/// between a declaration or an expression statement, when parsing function 22193326Sed/// bodies. Returns true for declaration, false for expression. 23193326Sed/// 24193326Sed/// declaration-statement: 25193326Sed/// block-declaration 26193326Sed/// 27193326Sed/// block-declaration: 28193326Sed/// simple-declaration 29193326Sed/// asm-definition 30193326Sed/// namespace-alias-definition 31193326Sed/// using-declaration 32193326Sed/// using-directive 33193326Sed/// [C++0x] static_assert-declaration 34193326Sed/// 35193326Sed/// asm-definition: 36193326Sed/// 'asm' '(' string-literal ')' ';' 37193326Sed/// 38193326Sed/// namespace-alias-definition: 39193326Sed/// 'namespace' identifier = qualified-namespace-specifier ';' 40193326Sed/// 41193326Sed/// using-declaration: 42193326Sed/// 'using' typename[opt] '::'[opt] nested-name-specifier 43193326Sed/// unqualified-id ';' 44193326Sed/// 'using' '::' unqualified-id ; 45193326Sed/// 46193326Sed/// using-directive: 47193326Sed/// 'using' 'namespace' '::'[opt] nested-name-specifier[opt] 48193326Sed/// namespace-name ';' 49193326Sed/// 50193326Sedbool Parser::isCXXDeclarationStatement() { 51193326Sed switch (Tok.getKind()) { 52193326Sed // asm-definition 53193326Sed case tok::kw_asm: 54193326Sed // namespace-alias-definition 55193326Sed case tok::kw_namespace: 56193326Sed // using-declaration 57193326Sed // using-directive 58193326Sed case tok::kw_using: 59199990Srdivacky // static_assert-declaration 60193326Sed case tok::kw_static_assert: 61221345Sdim case tok::kw__Static_assert: 62193326Sed return true; 63199990Srdivacky // simple-declaration 64193326Sed default: 65234353Sdim return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); 66193326Sed } 67193326Sed} 68193326Sed 69193326Sed/// isCXXSimpleDeclaration - C++-specialized function that disambiguates 70193326Sed/// between a simple-declaration or an expression-statement. 71193326Sed/// If during the disambiguation process a parsing error is encountered, 72193326Sed/// the function returns true to let the declaration parsing code handle it. 73193326Sed/// Returns false if the statement is disambiguated as expression. 74193326Sed/// 75193326Sed/// simple-declaration: 76193326Sed/// decl-specifier-seq init-declarator-list[opt] ';' 77193326Sed/// 78234353Sdim/// (if AllowForRangeDecl specified) 79234353Sdim/// for ( for-range-declaration : for-range-initializer ) statement 80234353Sdim/// for-range-declaration: 81234353Sdim/// attribute-specifier-seqopt type-specifier-seq declarator 82234353Sdimbool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) { 83193326Sed // C++ 6.8p1: 84193326Sed // There is an ambiguity in the grammar involving expression-statements and 85193326Sed // declarations: An expression-statement with a function-style explicit type 86193326Sed // conversion (5.2.3) as its leftmost subexpression can be indistinguishable 87193326Sed // from a declaration where the first declarator starts with a '('. In those 88193326Sed // cases the statement is a declaration. [Note: To disambiguate, the whole 89193326Sed // statement might have to be examined to determine if it is an 90193326Sed // expression-statement or a declaration]. 91193326Sed 92193326Sed // C++ 6.8p3: 93193326Sed // The disambiguation is purely syntactic; that is, the meaning of the names 94193326Sed // occurring in such a statement, beyond whether they are type-names or not, 95193326Sed // is not generally used in or changed by the disambiguation. Class 96193326Sed // templates are instantiated as necessary to determine if a qualified name 97193326Sed // is a type-name. Disambiguation precedes parsing, and a statement 98193326Sed // disambiguated as a declaration may be an ill-formed declaration. 99193326Sed 100193326Sed // We don't have to parse all of the decl-specifier-seq part. There's only 101193326Sed // an ambiguity if the first decl-specifier is 102193326Sed // simple-type-specifier/typename-specifier followed by a '(', which may 103193326Sed // indicate a function-style cast expression. 104193326Sed // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such 105193326Sed // a case. 106193326Sed 107243830Sdim bool InvalidAsDeclaration = false; 108243830Sdim TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(), 109243830Sdim &InvalidAsDeclaration); 110193326Sed if (TPR != TPResult::Ambiguous()) 111193326Sed return TPR != TPResult::False(); // Returns true for TPResult::True() or 112193326Sed // TPResult::Error(). 113193326Sed 114243830Sdim // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer, 115243830Sdim // and so gets some cases wrong. We can't carry on if we've already seen 116243830Sdim // something which makes this statement invalid as a declaration in this case, 117243830Sdim // since it can cause us to misparse valid code. Revisit this once 118243830Sdim // TryParseInitDeclaratorList is fixed. 119243830Sdim if (InvalidAsDeclaration) 120243830Sdim return false; 121243830Sdim 122193326Sed // FIXME: Add statistics about the number of ambiguous statements encountered 123193326Sed // and how they were resolved (number of declarations+number of expressions). 124193326Sed 125243830Sdim // Ok, we have a simple-type-specifier/typename-specifier followed by a '(', 126243830Sdim // or an identifier which doesn't resolve as anything. We need tentative 127243830Sdim // parsing... 128193326Sed 129193326Sed TentativeParsingAction PA(*this); 130234353Sdim TPR = TryParseSimpleDeclaration(AllowForRangeDecl); 131193326Sed PA.Revert(); 132193326Sed 133193326Sed // In case of an error, let the declaration parsing code handle it. 134193326Sed if (TPR == TPResult::Error()) 135193326Sed return true; 136193326Sed 137193326Sed // Declarations take precedence over expressions. 138193326Sed if (TPR == TPResult::Ambiguous()) 139193326Sed TPR = TPResult::True(); 140193326Sed 141193326Sed assert(TPR == TPResult::True() || TPR == TPResult::False()); 142193326Sed return TPR == TPResult::True(); 143193326Sed} 144193326Sed 145263508Sdim/// Try to consume a token sequence that we've already identified as 146263508Sdim/// (potentially) starting a decl-specifier. 147263508SdimParser::TPResult Parser::TryConsumeDeclarationSpecifier() { 148263508Sdim switch (Tok.getKind()) { 149263508Sdim case tok::kw__Atomic: 150263508Sdim if (NextToken().isNot(tok::l_paren)) { 151263508Sdim ConsumeToken(); 152263508Sdim break; 153263508Sdim } 154263508Sdim // Fall through. 155263508Sdim case tok::kw_typeof: 156263508Sdim case tok::kw___attribute: 157263508Sdim case tok::kw___underlying_type: { 158263508Sdim ConsumeToken(); 159263508Sdim if (Tok.isNot(tok::l_paren)) 160263508Sdim return TPResult::Error(); 161263508Sdim ConsumeParen(); 162263508Sdim if (!SkipUntil(tok::r_paren)) 163263508Sdim return TPResult::Error(); 164263508Sdim break; 165263508Sdim } 166263508Sdim 167263508Sdim case tok::kw_class: 168263508Sdim case tok::kw_struct: 169263508Sdim case tok::kw_union: 170263508Sdim case tok::kw___interface: 171263508Sdim case tok::kw_enum: 172263508Sdim // elaborated-type-specifier: 173263508Sdim // class-key attribute-specifier-seq[opt] 174263508Sdim // nested-name-specifier[opt] identifier 175263508Sdim // class-key nested-name-specifier[opt] template[opt] simple-template-id 176263508Sdim // enum nested-name-specifier[opt] identifier 177263508Sdim // 178263508Sdim // FIXME: We don't support class-specifiers nor enum-specifiers here. 179263508Sdim ConsumeToken(); 180263508Sdim 181263508Sdim // Skip attributes. 182263508Sdim while (Tok.is(tok::l_square) || Tok.is(tok::kw___attribute) || 183263508Sdim Tok.is(tok::kw___declspec) || Tok.is(tok::kw_alignas)) { 184263508Sdim if (Tok.is(tok::l_square)) { 185263508Sdim ConsumeBracket(); 186263508Sdim if (!SkipUntil(tok::r_square)) 187263508Sdim return TPResult::Error(); 188263508Sdim } else { 189263508Sdim ConsumeToken(); 190263508Sdim if (Tok.isNot(tok::l_paren)) 191263508Sdim return TPResult::Error(); 192263508Sdim ConsumeParen(); 193263508Sdim if (!SkipUntil(tok::r_paren)) 194263508Sdim return TPResult::Error(); 195263508Sdim } 196263508Sdim } 197263508Sdim 198263508Sdim if (TryAnnotateCXXScopeToken()) 199263508Sdim return TPResult::Error(); 200263508Sdim if (Tok.is(tok::annot_cxxscope)) 201263508Sdim ConsumeToken(); 202263508Sdim if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) 203263508Sdim return TPResult::Error(); 204263508Sdim ConsumeToken(); 205263508Sdim break; 206263508Sdim 207263508Sdim case tok::annot_cxxscope: 208263508Sdim ConsumeToken(); 209263508Sdim // Fall through. 210263508Sdim default: 211263508Sdim ConsumeToken(); 212263508Sdim 213263508Sdim if (getLangOpts().ObjC1 && Tok.is(tok::less)) 214263508Sdim return TryParseProtocolQualifiers(); 215263508Sdim break; 216263508Sdim } 217263508Sdim 218263508Sdim return TPResult::Ambiguous(); 219263508Sdim} 220263508Sdim 221193326Sed/// simple-declaration: 222193326Sed/// decl-specifier-seq init-declarator-list[opt] ';' 223193326Sed/// 224234353Sdim/// (if AllowForRangeDecl specified) 225234353Sdim/// for ( for-range-declaration : for-range-initializer ) statement 226234353Sdim/// for-range-declaration: 227234353Sdim/// attribute-specifier-seqopt type-specifier-seq declarator 228234353Sdim/// 229234353SdimParser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) { 230263508Sdim if (TryConsumeDeclarationSpecifier() == TPResult::Error()) 231263508Sdim return TPResult::Error(); 232243830Sdim 233243830Sdim // Two decl-specifiers in a row conclusively disambiguate this as being a 234243830Sdim // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the 235243830Sdim // overwhelmingly common case that the next token is a '('. 236243830Sdim if (Tok.isNot(tok::l_paren)) { 237243830Sdim TPResult TPR = isCXXDeclarationSpecifier(); 238243830Sdim if (TPR == TPResult::Ambiguous()) 239243830Sdim return TPResult::True(); 240243830Sdim if (TPR == TPResult::True() || TPR == TPResult::Error()) 241243830Sdim return TPR; 242243830Sdim assert(TPR == TPResult::False()); 243243830Sdim } 244243830Sdim 245193326Sed TPResult TPR = TryParseInitDeclaratorList(); 246193326Sed if (TPR != TPResult::Ambiguous()) 247193326Sed return TPR; 248193326Sed 249234353Sdim if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon))) 250193326Sed return TPResult::False(); 251193326Sed 252193326Sed return TPResult::Ambiguous(); 253193326Sed} 254193326Sed 255249423Sdim/// Tentatively parse an init-declarator-list in order to disambiguate it from 256249423Sdim/// an expression. 257249423Sdim/// 258193326Sed/// init-declarator-list: 259193326Sed/// init-declarator 260193326Sed/// init-declarator-list ',' init-declarator 261193326Sed/// 262193326Sed/// init-declarator: 263193326Sed/// declarator initializer[opt] 264193326Sed/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] 265193326Sed/// 266249423Sdim/// initializer: 267249423Sdim/// brace-or-equal-initializer 268249423Sdim/// '(' expression-list ')' 269193326Sed/// 270249423Sdim/// brace-or-equal-initializer: 271249423Sdim/// '=' initializer-clause 272249423Sdim/// [C++11] braced-init-list 273193326Sed/// 274249423Sdim/// initializer-clause: 275249423Sdim/// assignment-expression 276249423Sdim/// braced-init-list 277249423Sdim/// 278249423Sdim/// braced-init-list: 279249423Sdim/// '{' initializer-list ','[opt] '}' 280249423Sdim/// '{' '}' 281249423Sdim/// 282193326SedParser::TPResult Parser::TryParseInitDeclaratorList() { 283193326Sed while (1) { 284193326Sed // declarator 285193326Sed TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); 286193326Sed if (TPR != TPResult::Ambiguous()) 287193326Sed return TPR; 288193326Sed 289193326Sed // [GNU] simple-asm-expr[opt] attributes[opt] 290193326Sed if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) 291193326Sed return TPResult::True(); 292193326Sed 293193326Sed // initializer[opt] 294193326Sed if (Tok.is(tok::l_paren)) { 295193326Sed // Parse through the parens. 296193326Sed ConsumeParen(); 297263508Sdim if (!SkipUntil(tok::r_paren, StopAtSemi)) 298193326Sed return TPResult::Error(); 299249423Sdim } else if (Tok.is(tok::l_brace)) { 300249423Sdim // A left-brace here is sufficient to disambiguate the parse; an 301249423Sdim // expression can never be followed directly by a braced-init-list. 302249423Sdim return TPResult::True(); 303212904Sdim } else if (Tok.is(tok::equal) || isTokIdentifier_in()) { 304263508Sdim // MSVC and g++ won't examine the rest of declarators if '=' is 305212904Sdim // encountered; they just conclude that we have a declaration. 306212904Sdim // EDG parses the initializer completely, which is the proper behavior 307212904Sdim // for this case. 308193326Sed // 309212904Sdim // At present, Clang follows MSVC and g++, since the parser does not have 310212904Sdim // the ability to parse an expression fully without recording the 311212904Sdim // results of that parse. 312263508Sdim // FIXME: Handle this case correctly. 313263508Sdim // 314263508Sdim // Also allow 'in' after an Objective-C declaration as in: 315263508Sdim // for (int (^b)(void) in array). Ideally this should be done in the 316212904Sdim // context of parsing for-init-statement of a foreach statement only. But, 317212904Sdim // in any other context 'in' is invalid after a declaration and parser 318212904Sdim // issues the error regardless of outcome of this decision. 319263508Sdim // FIXME: Change if above assumption does not hold. 320212904Sdim return TPResult::True(); 321193326Sed } 322193326Sed 323193326Sed if (Tok.isNot(tok::comma)) 324193326Sed break; 325193326Sed ConsumeToken(); // the comma. 326193326Sed } 327193326Sed 328193326Sed return TPResult::Ambiguous(); 329193326Sed} 330193326Sed 331193326Sed/// isCXXConditionDeclaration - Disambiguates between a declaration or an 332193326Sed/// expression for a condition of a if/switch/while/for statement. 333193326Sed/// If during the disambiguation process a parsing error is encountered, 334193326Sed/// the function returns true to let the declaration parsing code handle it. 335193326Sed/// 336193326Sed/// condition: 337193326Sed/// expression 338193326Sed/// type-specifier-seq declarator '=' assignment-expression 339234353Sdim/// [C++11] type-specifier-seq declarator '=' initializer-clause 340234353Sdim/// [C++11] type-specifier-seq declarator braced-init-list 341193326Sed/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 342193326Sed/// '=' assignment-expression 343193326Sed/// 344193326Sedbool Parser::isCXXConditionDeclaration() { 345193326Sed TPResult TPR = isCXXDeclarationSpecifier(); 346193326Sed if (TPR != TPResult::Ambiguous()) 347193326Sed return TPR != TPResult::False(); // Returns true for TPResult::True() or 348193326Sed // TPResult::Error(). 349193326Sed 350193326Sed // FIXME: Add statistics about the number of ambiguous statements encountered 351193326Sed // and how they were resolved (number of declarations+number of expressions). 352193326Sed 353193326Sed // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. 354193326Sed // We need tentative parsing... 355193326Sed 356193326Sed TentativeParsingAction PA(*this); 357193326Sed 358193326Sed // type-specifier-seq 359263508Sdim TryConsumeDeclarationSpecifier(); 360193326Sed assert(Tok.is(tok::l_paren) && "Expected '('"); 361193326Sed 362193326Sed // declarator 363193326Sed TPR = TryParseDeclarator(false/*mayBeAbstract*/); 364193326Sed 365193326Sed // In case of an error, let the declaration parsing code handle it. 366193326Sed if (TPR == TPResult::Error()) 367193326Sed TPR = TPResult::True(); 368193326Sed 369193326Sed if (TPR == TPResult::Ambiguous()) { 370193326Sed // '=' 371193326Sed // [GNU] simple-asm-expr[opt] attributes[opt] 372193326Sed if (Tok.is(tok::equal) || 373193326Sed Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) 374193326Sed TPR = TPResult::True(); 375249423Sdim else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) 376234353Sdim TPR = TPResult::True(); 377193326Sed else 378193326Sed TPR = TPResult::False(); 379193326Sed } 380193326Sed 381193326Sed PA.Revert(); 382193326Sed 383193326Sed assert(TPR == TPResult::True() || TPR == TPResult::False()); 384193326Sed return TPR == TPResult::True(); 385193326Sed} 386193326Sed 387198092Srdivacky /// \brief Determine whether the next set of tokens contains a type-id. 388193326Sed /// 389193326Sed /// The context parameter states what context we're parsing right 390193326Sed /// now, which affects how this routine copes with the token 391193326Sed /// following the type-id. If the context is TypeIdInParens, we have 392193326Sed /// already parsed the '(' and we will cease lookahead when we hit 393193326Sed /// the corresponding ')'. If the context is 394193326Sed /// TypeIdAsTemplateArgument, we've already parsed the '<' or ',' 395193326Sed /// before this template argument, and will cease lookahead when we 396193326Sed /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id 397193326Sed /// and false for an expression. If during the disambiguation 398193326Sed /// process a parsing error is encountered, the function returns 399193326Sed /// true to let the declaration parsing code handle it. 400193326Sed /// 401193326Sed /// type-id: 402193326Sed /// type-specifier-seq abstract-declarator[opt] 403193326Sed /// 404193326Sedbool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) { 405198092Srdivacky 406193326Sed isAmbiguous = false; 407193326Sed 408193326Sed // C++ 8.2p2: 409193326Sed // The ambiguity arising from the similarity between a function-style cast and 410193326Sed // a type-id can occur in different contexts. The ambiguity appears as a 411193326Sed // choice between a function-style cast expression and a declaration of a 412193326Sed // type. The resolution is that any construct that could possibly be a type-id 413193326Sed // in its syntactic context shall be considered a type-id. 414193326Sed 415193326Sed TPResult TPR = isCXXDeclarationSpecifier(); 416193326Sed if (TPR != TPResult::Ambiguous()) 417193326Sed return TPR != TPResult::False(); // Returns true for TPResult::True() or 418193326Sed // TPResult::Error(). 419193326Sed 420193326Sed // FIXME: Add statistics about the number of ambiguous statements encountered 421193326Sed // and how they were resolved (number of declarations+number of expressions). 422193326Sed 423193326Sed // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. 424193326Sed // We need tentative parsing... 425193326Sed 426193326Sed TentativeParsingAction PA(*this); 427193326Sed 428193326Sed // type-specifier-seq 429263508Sdim TryConsumeDeclarationSpecifier(); 430193326Sed assert(Tok.is(tok::l_paren) && "Expected '('"); 431193326Sed 432193326Sed // declarator 433193326Sed TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); 434193326Sed 435193326Sed // In case of an error, let the declaration parsing code handle it. 436193326Sed if (TPR == TPResult::Error()) 437193326Sed TPR = TPResult::True(); 438193326Sed 439193326Sed if (TPR == TPResult::Ambiguous()) { 440193326Sed // We are supposed to be inside parens, so if after the abstract declarator 441193326Sed // we encounter a ')' this is a type-id, otherwise it's an expression. 442193326Sed if (Context == TypeIdInParens && Tok.is(tok::r_paren)) { 443193326Sed TPR = TPResult::True(); 444193326Sed isAmbiguous = true; 445193326Sed 446193326Sed // We are supposed to be inside a template argument, so if after 447193326Sed // the abstract declarator we encounter a '>', '>>' (in C++0x), or 448193326Sed // ',', this is a type-id. Otherwise, it's an expression. 449193326Sed } else if (Context == TypeIdAsTemplateArgument && 450193326Sed (Tok.is(tok::greater) || Tok.is(tok::comma) || 451249423Sdim (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) { 452193326Sed TPR = TPResult::True(); 453193326Sed isAmbiguous = true; 454193326Sed 455193326Sed } else 456193326Sed TPR = TPResult::False(); 457193326Sed } 458193326Sed 459193326Sed PA.Revert(); 460193326Sed 461193326Sed assert(TPR == TPResult::True() || TPR == TPResult::False()); 462193326Sed return TPR == TPResult::True(); 463193326Sed} 464193326Sed 465234353Sdim/// \brief Returns true if this is a C++11 attribute-specifier. Per 466234353Sdim/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens 467234353Sdim/// always introduce an attribute. In Objective-C++11, this rule does not 468234353Sdim/// apply if either '[' begins a message-send. 469199990Srdivacky/// 470234353Sdim/// If Disambiguate is true, we try harder to determine whether a '[[' starts 471234353Sdim/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not. 472199990Srdivacky/// 473234353Sdim/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an 474234353Sdim/// Obj-C message send or the start of an attribute. Otherwise, we assume it 475234353Sdim/// is not an Obj-C message send. 476199990Srdivacky/// 477234353Sdim/// C++11 [dcl.attr.grammar]: 478234353Sdim/// 479234353Sdim/// attribute-specifier: 480199990Srdivacky/// '[' '[' attribute-list ']' ']' 481226633Sdim/// alignment-specifier 482199990Srdivacky/// 483234353Sdim/// attribute-list: 484199990Srdivacky/// attribute[opt] 485199990Srdivacky/// attribute-list ',' attribute[opt] 486234353Sdim/// attribute '...' 487234353Sdim/// attribute-list ',' attribute '...' 488199990Srdivacky/// 489234353Sdim/// attribute: 490199990Srdivacky/// attribute-token attribute-argument-clause[opt] 491199990Srdivacky/// 492234353Sdim/// attribute-token: 493199990Srdivacky/// identifier 494234353Sdim/// identifier '::' identifier 495199990Srdivacky/// 496234353Sdim/// attribute-argument-clause: 497199990Srdivacky/// '(' balanced-token-seq ')' 498234353SdimParser::CXX11AttributeKind 499234353SdimParser::isCXX11AttributeSpecifier(bool Disambiguate, 500234353Sdim bool OuterMightBeMessageSend) { 501226633Sdim if (Tok.is(tok::kw_alignas)) 502234353Sdim return CAK_AttributeSpecifier; 503226633Sdim 504199990Srdivacky if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square)) 505234353Sdim return CAK_NotAttributeSpecifier; 506199990Srdivacky 507234353Sdim // No tentative parsing if we don't need to look for ']]' or a lambda. 508234353Sdim if (!Disambiguate && !getLangOpts().ObjC1) 509234353Sdim return CAK_AttributeSpecifier; 510199990Srdivacky 511234353Sdim TentativeParsingAction PA(*this); 512234353Sdim 513199990Srdivacky // Opening brackets were checked for above. 514199990Srdivacky ConsumeBracket(); 515199990Srdivacky 516234353Sdim // Outside Obj-C++11, treat anything with a matching ']]' as an attribute. 517234353Sdim if (!getLangOpts().ObjC1) { 518234353Sdim ConsumeBracket(); 519199990Srdivacky 520263508Sdim bool IsAttribute = SkipUntil(tok::r_square); 521234353Sdim IsAttribute &= Tok.is(tok::r_square); 522234353Sdim 523234353Sdim PA.Revert(); 524234353Sdim 525234353Sdim return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier; 526234353Sdim } 527234353Sdim 528234353Sdim // In Obj-C++11, we need to distinguish four situations: 529234353Sdim // 1a) int x[[attr]]; C++11 attribute. 530234353Sdim // 1b) [[attr]]; C++11 statement attribute. 531234353Sdim // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index. 532234353Sdim // 3a) int x[[obj get]]; Message send in array size/index. 533234353Sdim // 3b) [[Class alloc] init]; Message send in message send. 534234353Sdim // 4) [[obj]{ return self; }() doStuff]; Lambda in message send. 535234353Sdim // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted. 536234353Sdim 537234353Sdim // If we have a lambda-introducer, then this is definitely not a message send. 538234353Sdim // FIXME: If this disambiguation is too slow, fold the tentative lambda parse 539234353Sdim // into the tentative attribute parse below. 540234353Sdim LambdaIntroducer Intro; 541234353Sdim if (!TryParseLambdaIntroducer(Intro)) { 542234353Sdim // A lambda cannot end with ']]', and an attribute must. 543234353Sdim bool IsAttribute = Tok.is(tok::r_square); 544234353Sdim 545234353Sdim PA.Revert(); 546234353Sdim 547234353Sdim if (IsAttribute) 548234353Sdim // Case 1: C++11 attribute. 549234353Sdim return CAK_AttributeSpecifier; 550234353Sdim 551234353Sdim if (OuterMightBeMessageSend) 552234353Sdim // Case 4: Lambda in message send. 553234353Sdim return CAK_NotAttributeSpecifier; 554234353Sdim 555234353Sdim // Case 2: Lambda in array size / index. 556234353Sdim return CAK_InvalidAttributeSpecifier; 557234353Sdim } 558234353Sdim 559199990Srdivacky ConsumeBracket(); 560199990Srdivacky 561234353Sdim // If we don't have a lambda-introducer, then we have an attribute or a 562234353Sdim // message-send. 563234353Sdim bool IsAttribute = true; 564234353Sdim while (Tok.isNot(tok::r_square)) { 565234353Sdim if (Tok.is(tok::comma)) { 566234353Sdim // Case 1: Stray commas can only occur in attributes. 567234353Sdim PA.Revert(); 568234353Sdim return CAK_AttributeSpecifier; 569234353Sdim } 570199990Srdivacky 571234353Sdim // Parse the attribute-token, if present. 572234353Sdim // C++11 [dcl.attr.grammar]: 573234353Sdim // If a keyword or an alternative token that satisfies the syntactic 574234353Sdim // requirements of an identifier is contained in an attribute-token, 575234353Sdim // it is considered an identifier. 576234353Sdim SourceLocation Loc; 577234353Sdim if (!TryParseCXX11AttributeIdentifier(Loc)) { 578234353Sdim IsAttribute = false; 579234353Sdim break; 580234353Sdim } 581234353Sdim if (Tok.is(tok::coloncolon)) { 582234353Sdim ConsumeToken(); 583234353Sdim if (!TryParseCXX11AttributeIdentifier(Loc)) { 584234353Sdim IsAttribute = false; 585234353Sdim break; 586234353Sdim } 587234353Sdim } 588234353Sdim 589234353Sdim // Parse the attribute-argument-clause, if present. 590234353Sdim if (Tok.is(tok::l_paren)) { 591234353Sdim ConsumeParen(); 592263508Sdim if (!SkipUntil(tok::r_paren)) { 593234353Sdim IsAttribute = false; 594234353Sdim break; 595234353Sdim } 596234353Sdim } 597234353Sdim 598234353Sdim if (Tok.is(tok::ellipsis)) 599234353Sdim ConsumeToken(); 600234353Sdim 601234353Sdim if (Tok.isNot(tok::comma)) 602234353Sdim break; 603234353Sdim 604234353Sdim ConsumeToken(); 605234353Sdim } 606234353Sdim 607234353Sdim // An attribute must end ']]'. 608234353Sdim if (IsAttribute) { 609234353Sdim if (Tok.is(tok::r_square)) { 610234353Sdim ConsumeBracket(); 611234353Sdim IsAttribute = Tok.is(tok::r_square); 612234353Sdim } else { 613234353Sdim IsAttribute = false; 614234353Sdim } 615234353Sdim } 616234353Sdim 617234353Sdim PA.Revert(); 618234353Sdim 619234353Sdim if (IsAttribute) 620234353Sdim // Case 1: C++11 statement attribute. 621234353Sdim return CAK_AttributeSpecifier; 622234353Sdim 623234353Sdim // Case 3: Message send. 624234353Sdim return CAK_NotAttributeSpecifier; 625199990Srdivacky} 626199990Srdivacky 627263508SdimParser::TPResult Parser::TryParsePtrOperatorSeq() { 628263508Sdim while (true) { 629263508Sdim if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier)) 630263508Sdim if (TryAnnotateCXXScopeToken(true)) 631263508Sdim return TPResult::Error(); 632263508Sdim 633263508Sdim if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) || 634263508Sdim Tok.is(tok::ampamp) || 635263508Sdim (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) { 636263508Sdim // ptr-operator 637263508Sdim ConsumeToken(); 638263508Sdim while (Tok.is(tok::kw_const) || 639263508Sdim Tok.is(tok::kw_volatile) || 640263508Sdim Tok.is(tok::kw_restrict)) 641263508Sdim ConsumeToken(); 642263508Sdim } else { 643263508Sdim return TPResult::True(); 644263508Sdim } 645263508Sdim } 646263508Sdim} 647263508Sdim 648263508Sdim/// operator-function-id: 649263508Sdim/// 'operator' operator 650263508Sdim/// 651263508Sdim/// operator: one of 652263508Sdim/// new delete new[] delete[] + - * / % ^ [...] 653263508Sdim/// 654263508Sdim/// conversion-function-id: 655263508Sdim/// 'operator' conversion-type-id 656263508Sdim/// 657263508Sdim/// conversion-type-id: 658263508Sdim/// type-specifier-seq conversion-declarator[opt] 659263508Sdim/// 660263508Sdim/// conversion-declarator: 661263508Sdim/// ptr-operator conversion-declarator[opt] 662263508Sdim/// 663263508Sdim/// literal-operator-id: 664263508Sdim/// 'operator' string-literal identifier 665263508Sdim/// 'operator' user-defined-string-literal 666263508SdimParser::TPResult Parser::TryParseOperatorId() { 667263508Sdim assert(Tok.is(tok::kw_operator)); 668263508Sdim ConsumeToken(); 669263508Sdim 670263508Sdim // Maybe this is an operator-function-id. 671263508Sdim switch (Tok.getKind()) { 672263508Sdim case tok::kw_new: case tok::kw_delete: 673263508Sdim ConsumeToken(); 674263508Sdim if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) { 675263508Sdim ConsumeBracket(); 676263508Sdim ConsumeBracket(); 677263508Sdim } 678263508Sdim return TPResult::True(); 679263508Sdim 680263508Sdim#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \ 681263508Sdim case tok::Token: 682263508Sdim#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly) 683263508Sdim#include "clang/Basic/OperatorKinds.def" 684263508Sdim ConsumeToken(); 685263508Sdim return TPResult::True(); 686263508Sdim 687263508Sdim case tok::l_square: 688263508Sdim if (NextToken().is(tok::r_square)) { 689263508Sdim ConsumeBracket(); 690263508Sdim ConsumeBracket(); 691263508Sdim return TPResult::True(); 692263508Sdim } 693263508Sdim break; 694263508Sdim 695263508Sdim case tok::l_paren: 696263508Sdim if (NextToken().is(tok::r_paren)) { 697263508Sdim ConsumeParen(); 698263508Sdim ConsumeParen(); 699263508Sdim return TPResult::True(); 700263508Sdim } 701263508Sdim break; 702263508Sdim 703263508Sdim default: 704263508Sdim break; 705263508Sdim } 706263508Sdim 707263508Sdim // Maybe this is a literal-operator-id. 708263508Sdim if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) { 709263508Sdim bool FoundUDSuffix = false; 710263508Sdim do { 711263508Sdim FoundUDSuffix |= Tok.hasUDSuffix(); 712263508Sdim ConsumeStringToken(); 713263508Sdim } while (isTokenStringLiteral()); 714263508Sdim 715263508Sdim if (!FoundUDSuffix) { 716263508Sdim if (Tok.is(tok::identifier)) 717263508Sdim ConsumeToken(); 718263508Sdim else 719263508Sdim return TPResult::Error(); 720263508Sdim } 721263508Sdim return TPResult::True(); 722263508Sdim } 723263508Sdim 724263508Sdim // Maybe this is a conversion-function-id. 725263508Sdim bool AnyDeclSpecifiers = false; 726263508Sdim while (true) { 727263508Sdim TPResult TPR = isCXXDeclarationSpecifier(); 728263508Sdim if (TPR == TPResult::Error()) 729263508Sdim return TPR; 730263508Sdim if (TPR == TPResult::False()) { 731263508Sdim if (!AnyDeclSpecifiers) 732263508Sdim return TPResult::Error(); 733263508Sdim break; 734263508Sdim } 735263508Sdim if (TryConsumeDeclarationSpecifier() == TPResult::Error()) 736263508Sdim return TPResult::Error(); 737263508Sdim AnyDeclSpecifiers = true; 738263508Sdim } 739263508Sdim return TryParsePtrOperatorSeq(); 740263508Sdim} 741263508Sdim 742193326Sed/// declarator: 743193326Sed/// direct-declarator 744193326Sed/// ptr-operator declarator 745193326Sed/// 746193326Sed/// direct-declarator: 747193326Sed/// declarator-id 748193326Sed/// direct-declarator '(' parameter-declaration-clause ')' 749193326Sed/// cv-qualifier-seq[opt] exception-specification[opt] 750193326Sed/// direct-declarator '[' constant-expression[opt] ']' 751193326Sed/// '(' declarator ')' 752193326Sed/// [GNU] '(' attributes declarator ')' 753193326Sed/// 754193326Sed/// abstract-declarator: 755193326Sed/// ptr-operator abstract-declarator[opt] 756193326Sed/// direct-abstract-declarator 757218893Sdim/// ... 758193326Sed/// 759193326Sed/// direct-abstract-declarator: 760193326Sed/// direct-abstract-declarator[opt] 761193326Sed/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 762193326Sed/// exception-specification[opt] 763193326Sed/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 764193326Sed/// '(' abstract-declarator ')' 765193326Sed/// 766193326Sed/// ptr-operator: 767193326Sed/// '*' cv-qualifier-seq[opt] 768193326Sed/// '&' 769193326Sed/// [C++0x] '&&' [TODO] 770193326Sed/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] 771193326Sed/// 772193326Sed/// cv-qualifier-seq: 773193326Sed/// cv-qualifier cv-qualifier-seq[opt] 774193326Sed/// 775193326Sed/// cv-qualifier: 776193326Sed/// 'const' 777193326Sed/// 'volatile' 778193326Sed/// 779193326Sed/// declarator-id: 780218893Sdim/// '...'[opt] id-expression 781193326Sed/// 782193326Sed/// id-expression: 783193326Sed/// unqualified-id 784193326Sed/// qualified-id [TODO] 785193326Sed/// 786193326Sed/// unqualified-id: 787193326Sed/// identifier 788263508Sdim/// operator-function-id 789263508Sdim/// conversion-function-id 790263508Sdim/// literal-operator-id 791193326Sed/// '~' class-name [TODO] 792263508Sdim/// '~' decltype-specifier [TODO] 793193326Sed/// template-id [TODO] 794193326Sed/// 795193326SedParser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, 796193326Sed bool mayHaveIdentifier) { 797193326Sed // declarator: 798193326Sed // direct-declarator 799193326Sed // ptr-operator declarator 800263508Sdim if (TryParsePtrOperatorSeq() == TPResult::Error()) 801263508Sdim return TPResult::Error(); 802193326Sed 803193326Sed // direct-declarator: 804193326Sed // direct-abstract-declarator: 805218893Sdim if (Tok.is(tok::ellipsis)) 806218893Sdim ConsumeToken(); 807263508Sdim 808263508Sdim if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || 809263508Sdim (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) || 810263508Sdim NextToken().is(tok::kw_operator)))) && 811198092Srdivacky mayHaveIdentifier) { 812193326Sed // declarator-id 813198092Srdivacky if (Tok.is(tok::annot_cxxscope)) 814198092Srdivacky ConsumeToken(); 815263508Sdim else if (Tok.is(tok::identifier)) 816243830Sdim TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo()); 817263508Sdim if (Tok.is(tok::kw_operator)) { 818263508Sdim if (TryParseOperatorId() == TPResult::Error()) 819263508Sdim return TPResult::Error(); 820263508Sdim } else 821263508Sdim ConsumeToken(); 822193326Sed } else if (Tok.is(tok::l_paren)) { 823193326Sed ConsumeParen(); 824193326Sed if (mayBeAbstract && 825193326Sed (Tok.is(tok::r_paren) || // 'int()' is a function. 826234353Sdim // 'int(...)' is a function. 827234353Sdim (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) || 828193326Sed isDeclarationSpecifier())) { // 'int(int)' is a function. 829193326Sed // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 830193326Sed // exception-specification[opt] 831193326Sed TPResult TPR = TryParseFunctionDeclarator(); 832193326Sed if (TPR != TPResult::Ambiguous()) 833193326Sed return TPR; 834193326Sed } else { 835193326Sed // '(' declarator ')' 836193326Sed // '(' attributes declarator ')' 837193326Sed // '(' abstract-declarator ')' 838218893Sdim if (Tok.is(tok::kw___attribute) || 839218893Sdim Tok.is(tok::kw___declspec) || 840218893Sdim Tok.is(tok::kw___cdecl) || 841218893Sdim Tok.is(tok::kw___stdcall) || 842218893Sdim Tok.is(tok::kw___fastcall) || 843226633Sdim Tok.is(tok::kw___thiscall) || 844226633Sdim Tok.is(tok::kw___unaligned)) 845193326Sed return TPResult::True(); // attributes indicate declaration 846193326Sed TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); 847193326Sed if (TPR != TPResult::Ambiguous()) 848193326Sed return TPR; 849193326Sed if (Tok.isNot(tok::r_paren)) 850193326Sed return TPResult::False(); 851193326Sed ConsumeParen(); 852193326Sed } 853193326Sed } else if (!mayBeAbstract) { 854193326Sed return TPResult::False(); 855193326Sed } 856193326Sed 857193326Sed while (1) { 858193326Sed TPResult TPR(TPResult::Ambiguous()); 859193326Sed 860218893Sdim // abstract-declarator: ... 861218893Sdim if (Tok.is(tok::ellipsis)) 862218893Sdim ConsumeToken(); 863218893Sdim 864193326Sed if (Tok.is(tok::l_paren)) { 865193326Sed // Check whether we have a function declarator or a possible ctor-style 866193326Sed // initializer that follows the declarator. Note that ctor-style 867193326Sed // initializers are not possible in contexts where abstract declarators 868193326Sed // are allowed. 869239462Sdim if (!mayBeAbstract && !isCXXFunctionDeclarator()) 870193326Sed break; 871193326Sed 872193326Sed // direct-declarator '(' parameter-declaration-clause ')' 873193326Sed // cv-qualifier-seq[opt] exception-specification[opt] 874193326Sed ConsumeParen(); 875193326Sed TPR = TryParseFunctionDeclarator(); 876193326Sed } else if (Tok.is(tok::l_square)) { 877193326Sed // direct-declarator '[' constant-expression[opt] ']' 878193326Sed // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 879193326Sed TPR = TryParseBracketDeclarator(); 880193326Sed } else { 881193326Sed break; 882193326Sed } 883193326Sed 884193326Sed if (TPR != TPResult::Ambiguous()) 885193326Sed return TPR; 886193326Sed } 887193326Sed 888193326Sed return TPResult::Ambiguous(); 889193326Sed} 890193326Sed 891218893SdimParser::TPResult 892218893SdimParser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) { 893218893Sdim switch (Kind) { 894218893Sdim // Obviously starts an expression. 895218893Sdim case tok::numeric_constant: 896218893Sdim case tok::char_constant: 897226633Sdim case tok::wide_char_constant: 898226633Sdim case tok::utf16_char_constant: 899226633Sdim case tok::utf32_char_constant: 900218893Sdim case tok::string_literal: 901218893Sdim case tok::wide_string_literal: 902226633Sdim case tok::utf8_string_literal: 903226633Sdim case tok::utf16_string_literal: 904226633Sdim case tok::utf32_string_literal: 905218893Sdim case tok::l_square: 906218893Sdim case tok::l_paren: 907218893Sdim case tok::amp: 908218893Sdim case tok::ampamp: 909218893Sdim case tok::star: 910218893Sdim case tok::plus: 911218893Sdim case tok::plusplus: 912218893Sdim case tok::minus: 913218893Sdim case tok::minusminus: 914218893Sdim case tok::tilde: 915218893Sdim case tok::exclaim: 916218893Sdim case tok::kw_sizeof: 917218893Sdim case tok::kw___func__: 918218893Sdim case tok::kw_const_cast: 919218893Sdim case tok::kw_delete: 920218893Sdim case tok::kw_dynamic_cast: 921218893Sdim case tok::kw_false: 922218893Sdim case tok::kw_new: 923218893Sdim case tok::kw_operator: 924218893Sdim case tok::kw_reinterpret_cast: 925218893Sdim case tok::kw_static_cast: 926218893Sdim case tok::kw_this: 927218893Sdim case tok::kw_throw: 928218893Sdim case tok::kw_true: 929218893Sdim case tok::kw_typeid: 930218893Sdim case tok::kw_alignof: 931218893Sdim case tok::kw_noexcept: 932218893Sdim case tok::kw_nullptr: 933239462Sdim case tok::kw__Alignof: 934218893Sdim case tok::kw___null: 935218893Sdim case tok::kw___alignof: 936218893Sdim case tok::kw___builtin_choose_expr: 937218893Sdim case tok::kw___builtin_offsetof: 938218893Sdim case tok::kw___builtin_types_compatible_p: 939218893Sdim case tok::kw___builtin_va_arg: 940218893Sdim case tok::kw___imag: 941218893Sdim case tok::kw___real: 942218893Sdim case tok::kw___FUNCTION__: 943263508Sdim case tok::kw___FUNCDNAME__: 944239462Sdim case tok::kw_L__FUNCTION__: 945218893Sdim case tok::kw___PRETTY_FUNCTION__: 946218893Sdim case tok::kw___has_nothrow_assign: 947218893Sdim case tok::kw___has_nothrow_copy: 948218893Sdim case tok::kw___has_nothrow_constructor: 949218893Sdim case tok::kw___has_trivial_assign: 950218893Sdim case tok::kw___has_trivial_copy: 951218893Sdim case tok::kw___has_trivial_constructor: 952218893Sdim case tok::kw___has_trivial_destructor: 953218893Sdim case tok::kw___has_virtual_destructor: 954218893Sdim case tok::kw___is_abstract: 955218893Sdim case tok::kw___is_base_of: 956218893Sdim case tok::kw___is_class: 957218893Sdim case tok::kw___is_convertible_to: 958218893Sdim case tok::kw___is_empty: 959218893Sdim case tok::kw___is_enum: 960243830Sdim case tok::kw___is_interface_class: 961234353Sdim case tok::kw___is_final: 962221345Sdim case tok::kw___is_literal: 963221345Sdim case tok::kw___is_literal_type: 964218893Sdim case tok::kw___is_pod: 965218893Sdim case tok::kw___is_polymorphic: 966263508Sdim case tok::kw___is_sealed: 967221345Sdim case tok::kw___is_trivial: 968234353Sdim case tok::kw___is_trivially_assignable: 969234353Sdim case tok::kw___is_trivially_constructible: 970223017Sdim case tok::kw___is_trivially_copyable: 971218893Sdim case tok::kw___is_union: 972218893Sdim case tok::kw___uuidof: 973218893Sdim return TPResult::True(); 974218893Sdim 975218893Sdim // Obviously starts a type-specifier-seq: 976218893Sdim case tok::kw_char: 977218893Sdim case tok::kw_const: 978218893Sdim case tok::kw_double: 979218893Sdim case tok::kw_enum: 980226633Sdim case tok::kw_half: 981218893Sdim case tok::kw_float: 982218893Sdim case tok::kw_int: 983218893Sdim case tok::kw_long: 984221345Sdim case tok::kw___int64: 985234353Sdim case tok::kw___int128: 986218893Sdim case tok::kw_restrict: 987218893Sdim case tok::kw_short: 988218893Sdim case tok::kw_signed: 989218893Sdim case tok::kw_struct: 990218893Sdim case tok::kw_union: 991218893Sdim case tok::kw_unsigned: 992218893Sdim case tok::kw_void: 993218893Sdim case tok::kw_volatile: 994218893Sdim case tok::kw__Bool: 995218893Sdim case tok::kw__Complex: 996218893Sdim case tok::kw_class: 997218893Sdim case tok::kw_typename: 998218893Sdim case tok::kw_wchar_t: 999218893Sdim case tok::kw_char16_t: 1000218893Sdim case tok::kw_char32_t: 1001218893Sdim case tok::kw__Decimal32: 1002218893Sdim case tok::kw__Decimal64: 1003218893Sdim case tok::kw__Decimal128: 1004263508Sdim case tok::kw___interface: 1005218893Sdim case tok::kw___thread: 1006251662Sdim case tok::kw_thread_local: 1007251662Sdim case tok::kw__Thread_local: 1008218893Sdim case tok::kw_typeof: 1009263508Sdim case tok::kw___underlying_type: 1010218893Sdim case tok::kw___cdecl: 1011218893Sdim case tok::kw___stdcall: 1012218893Sdim case tok::kw___fastcall: 1013218893Sdim case tok::kw___thiscall: 1014226633Sdim case tok::kw___unaligned: 1015218893Sdim case tok::kw___vector: 1016218893Sdim case tok::kw___pixel: 1017226633Sdim case tok::kw__Atomic: 1018249423Sdim case tok::kw_image1d_t: 1019249423Sdim case tok::kw_image1d_array_t: 1020249423Sdim case tok::kw_image1d_buffer_t: 1021249423Sdim case tok::kw_image2d_t: 1022249423Sdim case tok::kw_image2d_array_t: 1023249423Sdim case tok::kw_image3d_t: 1024249423Sdim case tok::kw_sampler_t: 1025249423Sdim case tok::kw_event_t: 1026249423Sdim case tok::kw___unknown_anytype: 1027218893Sdim return TPResult::False(); 1028218893Sdim 1029218893Sdim default: 1030218893Sdim break; 1031218893Sdim } 1032218893Sdim 1033218893Sdim return TPResult::Ambiguous(); 1034218893Sdim} 1035218893Sdim 1036243830Sdimbool Parser::isTentativelyDeclared(IdentifierInfo *II) { 1037243830Sdim return std::find(TentativelyDeclaredIdentifiers.begin(), 1038243830Sdim TentativelyDeclaredIdentifiers.end(), II) 1039243830Sdim != TentativelyDeclaredIdentifiers.end(); 1040243830Sdim} 1041243830Sdim 1042193326Sed/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration 1043193326Sed/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could 1044193326Sed/// be either a decl-specifier or a function-style cast, and TPResult::Error() 1045193326Sed/// if a parsing error was found and reported. 1046193326Sed/// 1047239462Sdim/// If HasMissingTypename is provided, a name with a dependent scope specifier 1048239462Sdim/// will be treated as ambiguous if the 'typename' keyword is missing. If this 1049243830Sdim/// happens, *HasMissingTypename will be set to 'true'. This will also be used 1050243830Sdim/// as an indicator that undeclared identifiers (which will trigger a later 1051243830Sdim/// parse error) should be treated as types. Returns TPResult::Ambiguous() in 1052243830Sdim/// such cases. 1053239462Sdim/// 1054193326Sed/// decl-specifier: 1055193326Sed/// storage-class-specifier 1056193326Sed/// type-specifier 1057193326Sed/// function-specifier 1058193326Sed/// 'friend' 1059193326Sed/// 'typedef' 1060251662Sdim/// [C++11] 'constexpr' 1061193326Sed/// [GNU] attributes declaration-specifiers[opt] 1062193326Sed/// 1063193326Sed/// storage-class-specifier: 1064193326Sed/// 'register' 1065193326Sed/// 'static' 1066193326Sed/// 'extern' 1067193326Sed/// 'mutable' 1068193326Sed/// 'auto' 1069193326Sed/// [GNU] '__thread' 1070251662Sdim/// [C++11] 'thread_local' 1071251662Sdim/// [C11] '_Thread_local' 1072193326Sed/// 1073193326Sed/// function-specifier: 1074193326Sed/// 'inline' 1075193326Sed/// 'virtual' 1076193326Sed/// 'explicit' 1077193326Sed/// 1078193326Sed/// typedef-name: 1079193326Sed/// identifier 1080193326Sed/// 1081193326Sed/// type-specifier: 1082193326Sed/// simple-type-specifier 1083193326Sed/// class-specifier 1084193326Sed/// enum-specifier 1085193326Sed/// elaborated-type-specifier 1086193326Sed/// typename-specifier 1087193326Sed/// cv-qualifier 1088193326Sed/// 1089193326Sed/// simple-type-specifier: 1090193326Sed/// '::'[opt] nested-name-specifier[opt] type-name 1091193326Sed/// '::'[opt] nested-name-specifier 'template' 1092193326Sed/// simple-template-id [TODO] 1093193326Sed/// 'char' 1094193326Sed/// 'wchar_t' 1095193326Sed/// 'bool' 1096193326Sed/// 'short' 1097193326Sed/// 'int' 1098193326Sed/// 'long' 1099193326Sed/// 'signed' 1100193326Sed/// 'unsigned' 1101193326Sed/// 'float' 1102193326Sed/// 'double' 1103193326Sed/// 'void' 1104193326Sed/// [GNU] typeof-specifier 1105193326Sed/// [GNU] '_Complex' 1106251662Sdim/// [C++11] 'auto' 1107251662Sdim/// [C++11] 'decltype' ( expression ) 1108251662Sdim/// [C++1y] 'decltype' ( 'auto' ) 1109193326Sed/// 1110193326Sed/// type-name: 1111193326Sed/// class-name 1112193326Sed/// enum-name 1113193326Sed/// typedef-name 1114193326Sed/// 1115193326Sed/// elaborated-type-specifier: 1116193326Sed/// class-key '::'[opt] nested-name-specifier[opt] identifier 1117193326Sed/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] 1118193326Sed/// simple-template-id 1119193326Sed/// 'enum' '::'[opt] nested-name-specifier[opt] identifier 1120193326Sed/// 1121193326Sed/// enum-name: 1122193326Sed/// identifier 1123193326Sed/// 1124193326Sed/// enum-specifier: 1125193326Sed/// 'enum' identifier[opt] '{' enumerator-list[opt] '}' 1126193326Sed/// 'enum' identifier[opt] '{' enumerator-list ',' '}' 1127193326Sed/// 1128193326Sed/// class-specifier: 1129193326Sed/// class-head '{' member-specification[opt] '}' 1130193326Sed/// 1131193326Sed/// class-head: 1132193326Sed/// class-key identifier[opt] base-clause[opt] 1133193326Sed/// class-key nested-name-specifier identifier base-clause[opt] 1134193326Sed/// class-key nested-name-specifier[opt] simple-template-id 1135193326Sed/// base-clause[opt] 1136193326Sed/// 1137193326Sed/// class-key: 1138193326Sed/// 'class' 1139193326Sed/// 'struct' 1140193326Sed/// 'union' 1141193326Sed/// 1142193326Sed/// cv-qualifier: 1143193326Sed/// 'const' 1144193326Sed/// 'volatile' 1145193326Sed/// [GNU] restrict 1146193326Sed/// 1147234353SdimParser::TPResult 1148239462SdimParser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult, 1149239462Sdim bool *HasMissingTypename) { 1150193326Sed switch (Tok.getKind()) { 1151243830Sdim case tok::identifier: { 1152203955Srdivacky // Check for need to substitute AltiVec __vector keyword 1153203955Srdivacky // for "vector" identifier. 1154203955Srdivacky if (TryAltiVecVectorToken()) 1155203955Srdivacky return TPResult::True(); 1156243830Sdim 1157243830Sdim const Token &Next = NextToken(); 1158243830Sdim // In 'foo bar', 'foo' is always a type name outside of Objective-C. 1159243830Sdim if (!getLangOpts().ObjC1 && Next.is(tok::identifier)) 1160243830Sdim return TPResult::True(); 1161243830Sdim 1162243830Sdim if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) { 1163243830Sdim // Determine whether this is a valid expression. If not, we will hit 1164243830Sdim // a parse error one way or another. In that case, tell the caller that 1165243830Sdim // this is ambiguous. Typo-correct to type and expression keywords and 1166243830Sdim // to types and identifiers, in order to try to recover from errors. 1167243830Sdim CorrectionCandidateCallback TypoCorrection; 1168243830Sdim TypoCorrection.WantRemainingKeywords = false; 1169249423Sdim TypoCorrection.WantTypeSpecifiers = Next.isNot(tok::arrow); 1170243830Sdim switch (TryAnnotateName(false /* no nested name specifier */, 1171243830Sdim &TypoCorrection)) { 1172243830Sdim case ANK_Error: 1173243830Sdim return TPResult::Error(); 1174243830Sdim case ANK_TentativeDecl: 1175243830Sdim return TPResult::False(); 1176243830Sdim case ANK_TemplateName: 1177243830Sdim // A bare type template-name which can't be a template template 1178243830Sdim // argument is an error, and was probably intended to be a type. 1179243830Sdim return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); 1180243830Sdim case ANK_Unresolved: 1181243830Sdim return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False(); 1182243830Sdim case ANK_Success: 1183243830Sdim break; 1184243830Sdim } 1185243830Sdim assert(Tok.isNot(tok::identifier) && 1186243830Sdim "TryAnnotateName succeeded without producing an annotation"); 1187243830Sdim } else { 1188243830Sdim // This might possibly be a type with a dependent scope specifier and 1189243830Sdim // a missing 'typename' keyword. Don't use TryAnnotateName in this case, 1190243830Sdim // since it will annotate as a primary expression, and we want to use the 1191243830Sdim // "missing 'typename'" logic. 1192243830Sdim if (TryAnnotateTypeOrScopeToken()) 1193243830Sdim return TPResult::Error(); 1194243830Sdim // If annotation failed, assume it's a non-type. 1195243830Sdim // FIXME: If this happens due to an undeclared identifier, treat it as 1196243830Sdim // ambiguous. 1197243830Sdim if (Tok.is(tok::identifier)) 1198243830Sdim return TPResult::False(); 1199243830Sdim } 1200243830Sdim 1201243830Sdim // We annotated this token as something. Recurse to handle whatever we got. 1202243830Sdim return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1203243830Sdim } 1204243830Sdim 1205193326Sed case tok::kw_typename: // typename T::type 1206193326Sed // Annotate typenames and C++ scope specifiers. If we get one, just 1207193326Sed // recurse to handle whatever we get. 1208193326Sed if (TryAnnotateTypeOrScopeToken()) 1209204643Srdivacky return TPResult::Error(); 1210239462Sdim return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1211193326Sed 1212201361Srdivacky case tok::coloncolon: { // ::foo::bar 1213201361Srdivacky const Token &Next = NextToken(); 1214201361Srdivacky if (Next.is(tok::kw_new) || // ::new 1215201361Srdivacky Next.is(tok::kw_delete)) // ::delete 1216201361Srdivacky return TPResult::False(); 1217234353Sdim } 1218234353Sdim // Fall through. 1219234353Sdim case tok::kw_decltype: 1220193326Sed // Annotate typenames and C++ scope specifiers. If we get one, just 1221193326Sed // recurse to handle whatever we get. 1222193326Sed if (TryAnnotateTypeOrScopeToken()) 1223204643Srdivacky return TPResult::Error(); 1224239462Sdim return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1225234353Sdim 1226193326Sed // decl-specifier: 1227193326Sed // storage-class-specifier 1228193326Sed // type-specifier 1229193326Sed // function-specifier 1230193326Sed // 'friend' 1231193326Sed // 'typedef' 1232198954Srdivacky // 'constexpr' 1233193326Sed case tok::kw_friend: 1234193326Sed case tok::kw_typedef: 1235198954Srdivacky case tok::kw_constexpr: 1236193326Sed // storage-class-specifier 1237193326Sed case tok::kw_register: 1238193326Sed case tok::kw_static: 1239193326Sed case tok::kw_extern: 1240193326Sed case tok::kw_mutable: 1241193326Sed case tok::kw_auto: 1242193326Sed case tok::kw___thread: 1243251662Sdim case tok::kw_thread_local: 1244251662Sdim case tok::kw__Thread_local: 1245193326Sed // function-specifier 1246193326Sed case tok::kw_inline: 1247193326Sed case tok::kw_virtual: 1248193326Sed case tok::kw_explicit: 1249193326Sed 1250226633Sdim // Modules 1251226633Sdim case tok::kw___module_private__: 1252249423Sdim 1253249423Sdim // Debugger support 1254249423Sdim case tok::kw___unknown_anytype: 1255226633Sdim 1256193326Sed // type-specifier: 1257193326Sed // simple-type-specifier 1258193326Sed // class-specifier 1259193326Sed // enum-specifier 1260193326Sed // elaborated-type-specifier 1261193326Sed // typename-specifier 1262193326Sed // cv-qualifier 1263193326Sed 1264193326Sed // class-specifier 1265193326Sed // elaborated-type-specifier 1266193326Sed case tok::kw_class: 1267193326Sed case tok::kw_struct: 1268193326Sed case tok::kw_union: 1269263508Sdim case tok::kw___interface: 1270193326Sed // enum-specifier 1271193326Sed case tok::kw_enum: 1272193326Sed // cv-qualifier 1273193326Sed case tok::kw_const: 1274193326Sed case tok::kw_volatile: 1275193326Sed 1276193326Sed // GNU 1277193326Sed case tok::kw_restrict: 1278193326Sed case tok::kw__Complex: 1279193326Sed case tok::kw___attribute: 1280193326Sed return TPResult::True(); 1281198092Srdivacky 1282193326Sed // Microsoft 1283193326Sed case tok::kw___declspec: 1284193326Sed case tok::kw___cdecl: 1285193326Sed case tok::kw___stdcall: 1286193326Sed case tok::kw___fastcall: 1287208600Srdivacky case tok::kw___thiscall: 1288194179Sed case tok::kw___w64: 1289263508Sdim case tok::kw___sptr: 1290263508Sdim case tok::kw___uptr: 1291194179Sed case tok::kw___ptr64: 1292226633Sdim case tok::kw___ptr32: 1293194179Sed case tok::kw___forceinline: 1294226633Sdim case tok::kw___unaligned: 1295194179Sed return TPResult::True(); 1296212904Sdim 1297212904Sdim // Borland 1298212904Sdim case tok::kw___pascal: 1299212904Sdim return TPResult::True(); 1300203955Srdivacky 1301203955Srdivacky // AltiVec 1302203955Srdivacky case tok::kw___vector: 1303203955Srdivacky return TPResult::True(); 1304193326Sed 1305207619Srdivacky case tok::annot_template_id: { 1306224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1307207619Srdivacky if (TemplateId->Kind != TNK_Type_template) 1308207619Srdivacky return TPResult::False(); 1309207619Srdivacky CXXScopeSpec SS; 1310221345Sdim AnnotateTemplateIdTokenAsType(); 1311207619Srdivacky assert(Tok.is(tok::annot_typename)); 1312207619Srdivacky goto case_typename; 1313207619Srdivacky } 1314207619Srdivacky 1315201361Srdivacky case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed 1316201361Srdivacky // We've already annotated a scope; try to annotate a type. 1317204643Srdivacky if (TryAnnotateTypeOrScopeToken()) 1318204643Srdivacky return TPResult::Error(); 1319234353Sdim if (!Tok.is(tok::annot_typename)) { 1320234353Sdim // If the next token is an identifier or a type qualifier, then this 1321234353Sdim // can't possibly be a valid expression either. 1322234353Sdim if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) { 1323234353Sdim CXXScopeSpec SS; 1324234353Sdim Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), 1325234353Sdim Tok.getAnnotationRange(), 1326234353Sdim SS); 1327234353Sdim if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) { 1328234353Sdim TentativeParsingAction PA(*this); 1329234353Sdim ConsumeToken(); 1330234353Sdim ConsumeToken(); 1331234353Sdim bool isIdentifier = Tok.is(tok::identifier); 1332234353Sdim TPResult TPR = TPResult::False(); 1333234353Sdim if (!isIdentifier) 1334239462Sdim TPR = isCXXDeclarationSpecifier(BracedCastResult, 1335239462Sdim HasMissingTypename); 1336234353Sdim PA.Revert(); 1337234353Sdim 1338234353Sdim if (isIdentifier || 1339234353Sdim TPR == TPResult::True() || TPR == TPResult::Error()) 1340234353Sdim return TPResult::Error(); 1341239462Sdim 1342239462Sdim if (HasMissingTypename) { 1343239462Sdim // We can't tell whether this is a missing 'typename' or a valid 1344239462Sdim // expression. 1345239462Sdim *HasMissingTypename = true; 1346239462Sdim return TPResult::Ambiguous(); 1347239462Sdim } 1348243830Sdim } else { 1349243830Sdim // Try to resolve the name. If it doesn't exist, assume it was 1350243830Sdim // intended to name a type and keep disambiguating. 1351243830Sdim switch (TryAnnotateName(false /* SS is not dependent */)) { 1352243830Sdim case ANK_Error: 1353243830Sdim return TPResult::Error(); 1354243830Sdim case ANK_TentativeDecl: 1355243830Sdim return TPResult::False(); 1356243830Sdim case ANK_TemplateName: 1357243830Sdim // A bare type template-name which can't be a template template 1358243830Sdim // argument is an error, and was probably intended to be a type. 1359243830Sdim return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); 1360243830Sdim case ANK_Unresolved: 1361243830Sdim return HasMissingTypename ? TPResult::Ambiguous() 1362243830Sdim : TPResult::False(); 1363243830Sdim case ANK_Success: 1364243830Sdim // Annotated it, check again. 1365243830Sdim assert(Tok.isNot(tok::annot_cxxscope) || 1366243830Sdim NextToken().isNot(tok::identifier)); 1367243830Sdim return isCXXDeclarationSpecifier(BracedCastResult, 1368243830Sdim HasMissingTypename); 1369243830Sdim } 1370234353Sdim } 1371234353Sdim } 1372201361Srdivacky return TPResult::False(); 1373234353Sdim } 1374201361Srdivacky // If that succeeded, fallthrough into the generic simple-type-id case. 1375201361Srdivacky 1376193326Sed // The ambiguity resides in a simple-type-specifier/typename-specifier 1377193326Sed // followed by a '('. The '(' could either be the start of: 1378193326Sed // 1379193326Sed // direct-declarator: 1380193326Sed // '(' declarator ')' 1381193326Sed // 1382193326Sed // direct-abstract-declarator: 1383193326Sed // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1384193326Sed // exception-specification[opt] 1385193326Sed // '(' abstract-declarator ')' 1386193326Sed // 1387193326Sed // or part of a function-style cast expression: 1388193326Sed // 1389193326Sed // simple-type-specifier '(' expression-list[opt] ')' 1390193326Sed // 1391193326Sed 1392193326Sed // simple-type-specifier: 1393193326Sed 1394218893Sdim case tok::annot_typename: 1395218893Sdim case_typename: 1396218893Sdim // In Objective-C, we might have a protocol-qualified type. 1397234353Sdim if (getLangOpts().ObjC1 && NextToken().is(tok::less)) { 1398218893Sdim // Tentatively parse the 1399218893Sdim TentativeParsingAction PA(*this); 1400218893Sdim ConsumeToken(); // The type token 1401218893Sdim 1402218893Sdim TPResult TPR = TryParseProtocolQualifiers(); 1403218893Sdim bool isFollowedByParen = Tok.is(tok::l_paren); 1404234353Sdim bool isFollowedByBrace = Tok.is(tok::l_brace); 1405218893Sdim 1406218893Sdim PA.Revert(); 1407218893Sdim 1408218893Sdim if (TPR == TPResult::Error()) 1409218893Sdim return TPResult::Error(); 1410218893Sdim 1411218893Sdim if (isFollowedByParen) 1412218893Sdim return TPResult::Ambiguous(); 1413234353Sdim 1414249423Sdim if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 1415234353Sdim return BracedCastResult; 1416218893Sdim 1417218893Sdim return TPResult::True(); 1418218893Sdim } 1419218893Sdim 1420193326Sed case tok::kw_char: 1421193326Sed case tok::kw_wchar_t: 1422198092Srdivacky case tok::kw_char16_t: 1423198092Srdivacky case tok::kw_char32_t: 1424193326Sed case tok::kw_bool: 1425193326Sed case tok::kw_short: 1426193326Sed case tok::kw_int: 1427193326Sed case tok::kw_long: 1428221345Sdim case tok::kw___int64: 1429234353Sdim case tok::kw___int128: 1430193326Sed case tok::kw_signed: 1431193326Sed case tok::kw_unsigned: 1432226633Sdim case tok::kw_half: 1433193326Sed case tok::kw_float: 1434193326Sed case tok::kw_double: 1435193326Sed case tok::kw_void: 1436234353Sdim case tok::annot_decltype: 1437193326Sed if (NextToken().is(tok::l_paren)) 1438193326Sed return TPResult::Ambiguous(); 1439193326Sed 1440234353Sdim // This is a function-style cast in all cases we disambiguate other than 1441234353Sdim // one: 1442234353Sdim // struct S { 1443234353Sdim // enum E : int { a = 4 }; // enum 1444234353Sdim // enum E : int { 4 }; // bit-field 1445234353Sdim // }; 1446249423Sdim if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace)) 1447234353Sdim return BracedCastResult; 1448234353Sdim 1449218893Sdim if (isStartOfObjCClassMessageMissingOpenBracket()) 1450218893Sdim return TPResult::False(); 1451218893Sdim 1452193326Sed return TPResult::True(); 1453193326Sed 1454195099Sed // GNU typeof support. 1455193326Sed case tok::kw_typeof: { 1456193326Sed if (NextToken().isNot(tok::l_paren)) 1457193326Sed return TPResult::True(); 1458193326Sed 1459193326Sed TentativeParsingAction PA(*this); 1460193326Sed 1461193326Sed TPResult TPR = TryParseTypeofSpecifier(); 1462193326Sed bool isFollowedByParen = Tok.is(tok::l_paren); 1463234353Sdim bool isFollowedByBrace = Tok.is(tok::l_brace); 1464193326Sed 1465193326Sed PA.Revert(); 1466193326Sed 1467193326Sed if (TPR == TPResult::Error()) 1468193326Sed return TPResult::Error(); 1469193326Sed 1470193326Sed if (isFollowedByParen) 1471193326Sed return TPResult::Ambiguous(); 1472193326Sed 1473249423Sdim if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 1474234353Sdim return BracedCastResult; 1475234353Sdim 1476193326Sed return TPResult::True(); 1477193326Sed } 1478193326Sed 1479223017Sdim // C++0x type traits support 1480223017Sdim case tok::kw___underlying_type: 1481223017Sdim return TPResult::True(); 1482223017Sdim 1483234353Sdim // C11 _Atomic 1484226633Sdim case tok::kw__Atomic: 1485226633Sdim return TPResult::True(); 1486226633Sdim 1487193326Sed default: 1488193326Sed return TPResult::False(); 1489193326Sed } 1490193326Sed} 1491193326Sed 1492263508Sdimbool Parser::isCXXDeclarationSpecifierAType() { 1493263508Sdim switch (Tok.getKind()) { 1494263508Sdim // typename-specifier 1495263508Sdim case tok::annot_decltype: 1496263508Sdim case tok::annot_template_id: 1497263508Sdim case tok::annot_typename: 1498263508Sdim case tok::kw_typeof: 1499263508Sdim case tok::kw___underlying_type: 1500263508Sdim return true; 1501263508Sdim 1502263508Sdim // elaborated-type-specifier 1503263508Sdim case tok::kw_class: 1504263508Sdim case tok::kw_struct: 1505263508Sdim case tok::kw_union: 1506263508Sdim case tok::kw___interface: 1507263508Sdim case tok::kw_enum: 1508263508Sdim return true; 1509263508Sdim 1510263508Sdim // simple-type-specifier 1511263508Sdim case tok::kw_char: 1512263508Sdim case tok::kw_wchar_t: 1513263508Sdim case tok::kw_char16_t: 1514263508Sdim case tok::kw_char32_t: 1515263508Sdim case tok::kw_bool: 1516263508Sdim case tok::kw_short: 1517263508Sdim case tok::kw_int: 1518263508Sdim case tok::kw_long: 1519263508Sdim case tok::kw___int64: 1520263508Sdim case tok::kw___int128: 1521263508Sdim case tok::kw_signed: 1522263508Sdim case tok::kw_unsigned: 1523263508Sdim case tok::kw_half: 1524263508Sdim case tok::kw_float: 1525263508Sdim case tok::kw_double: 1526263508Sdim case tok::kw_void: 1527263508Sdim case tok::kw___unknown_anytype: 1528263508Sdim return true; 1529263508Sdim 1530263508Sdim case tok::kw_auto: 1531263508Sdim return getLangOpts().CPlusPlus11; 1532263508Sdim 1533263508Sdim case tok::kw__Atomic: 1534263508Sdim // "_Atomic foo" 1535263508Sdim return NextToken().is(tok::l_paren); 1536263508Sdim 1537263508Sdim default: 1538263508Sdim return false; 1539263508Sdim } 1540263508Sdim} 1541263508Sdim 1542193326Sed/// [GNU] typeof-specifier: 1543193326Sed/// 'typeof' '(' expressions ')' 1544193326Sed/// 'typeof' '(' type-name ')' 1545193326Sed/// 1546193326SedParser::TPResult Parser::TryParseTypeofSpecifier() { 1547193326Sed assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); 1548193326Sed ConsumeToken(); 1549193326Sed 1550193326Sed assert(Tok.is(tok::l_paren) && "Expected '('"); 1551193326Sed // Parse through the parens after 'typeof'. 1552193326Sed ConsumeParen(); 1553263508Sdim if (!SkipUntil(tok::r_paren, StopAtSemi)) 1554193326Sed return TPResult::Error(); 1555193326Sed 1556193326Sed return TPResult::Ambiguous(); 1557193326Sed} 1558193326Sed 1559218893Sdim/// [ObjC] protocol-qualifiers: 1560218893Sdim//// '<' identifier-list '>' 1561218893SdimParser::TPResult Parser::TryParseProtocolQualifiers() { 1562218893Sdim assert(Tok.is(tok::less) && "Expected '<' for qualifier list"); 1563218893Sdim ConsumeToken(); 1564218893Sdim do { 1565218893Sdim if (Tok.isNot(tok::identifier)) 1566218893Sdim return TPResult::Error(); 1567218893Sdim ConsumeToken(); 1568218893Sdim 1569218893Sdim if (Tok.is(tok::comma)) { 1570218893Sdim ConsumeToken(); 1571218893Sdim continue; 1572218893Sdim } 1573218893Sdim 1574218893Sdim if (Tok.is(tok::greater)) { 1575218893Sdim ConsumeToken(); 1576218893Sdim return TPResult::Ambiguous(); 1577218893Sdim } 1578218893Sdim } while (false); 1579218893Sdim 1580218893Sdim return TPResult::Error(); 1581218893Sdim} 1582218893Sdim 1583193326Sed/// isCXXFunctionDeclarator - Disambiguates between a function declarator or 1584193326Sed/// a constructor-style initializer, when parsing declaration statements. 1585193326Sed/// Returns true for function declarator and false for constructor-style 1586193326Sed/// initializer. 1587193326Sed/// If during the disambiguation process a parsing error is encountered, 1588193326Sed/// the function returns true to let the declaration parsing code handle it. 1589193326Sed/// 1590193326Sed/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1591193326Sed/// exception-specification[opt] 1592193326Sed/// 1593239462Sdimbool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) { 1594193326Sed 1595193326Sed // C++ 8.2p1: 1596193326Sed // The ambiguity arising from the similarity between a function-style cast and 1597193326Sed // a declaration mentioned in 6.8 can also occur in the context of a 1598193326Sed // declaration. In that context, the choice is between a function declaration 1599193326Sed // with a redundant set of parentheses around a parameter name and an object 1600193326Sed // declaration with a function-style cast as the initializer. Just as for the 1601193326Sed // ambiguities mentioned in 6.8, the resolution is to consider any construct 1602193326Sed // that could possibly be a declaration a declaration. 1603193326Sed 1604193326Sed TentativeParsingAction PA(*this); 1605193326Sed 1606193326Sed ConsumeParen(); 1607239462Sdim bool InvalidAsDeclaration = false; 1608239462Sdim TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration); 1609239462Sdim if (TPR == TPResult::Ambiguous()) { 1610239462Sdim if (Tok.isNot(tok::r_paren)) 1611239462Sdim TPR = TPResult::False(); 1612239462Sdim else { 1613239462Sdim const Token &Next = NextToken(); 1614239462Sdim if (Next.is(tok::amp) || Next.is(tok::ampamp) || 1615239462Sdim Next.is(tok::kw_const) || Next.is(tok::kw_volatile) || 1616239462Sdim Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) || 1617249423Sdim Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) || 1618239462Sdim Next.is(tok::l_brace) || Next.is(tok::kw_try) || 1619239462Sdim Next.is(tok::equal) || Next.is(tok::arrow)) 1620239462Sdim // The next token cannot appear after a constructor-style initializer, 1621239462Sdim // and can appear next in a function definition. This must be a function 1622239462Sdim // declarator. 1623239462Sdim TPR = TPResult::True(); 1624239462Sdim else if (InvalidAsDeclaration) 1625239462Sdim // Use the absence of 'typename' as a tie-breaker. 1626239462Sdim TPR = TPResult::False(); 1627239462Sdim } 1628239462Sdim } 1629193326Sed 1630193326Sed PA.Revert(); 1631193326Sed 1632239462Sdim if (IsAmbiguous && TPR == TPResult::Ambiguous()) 1633239462Sdim *IsAmbiguous = true; 1634239462Sdim 1635193326Sed // In case of an error, let the declaration parsing code handle it. 1636239462Sdim return TPR != TPResult::False(); 1637193326Sed} 1638193326Sed 1639193326Sed/// parameter-declaration-clause: 1640193326Sed/// parameter-declaration-list[opt] '...'[opt] 1641193326Sed/// parameter-declaration-list ',' '...' 1642193326Sed/// 1643193326Sed/// parameter-declaration-list: 1644193326Sed/// parameter-declaration 1645193326Sed/// parameter-declaration-list ',' parameter-declaration 1646193326Sed/// 1647193326Sed/// parameter-declaration: 1648234353Sdim/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 1649234353Sdim/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 1650218893Sdim/// '=' assignment-expression 1651234353Sdim/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 1652234353Sdim/// attributes[opt] 1653234353Sdim/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 1654234353Sdim/// attributes[opt] '=' assignment-expression 1655193326Sed/// 1656239462SdimParser::TPResult 1657263508SdimParser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration, 1658263508Sdim bool VersusTemplateArgument) { 1659193326Sed 1660193326Sed if (Tok.is(tok::r_paren)) 1661239462Sdim return TPResult::Ambiguous(); 1662193326Sed 1663193326Sed // parameter-declaration-list[opt] '...'[opt] 1664193326Sed // parameter-declaration-list ',' '...' 1665193326Sed // 1666193326Sed // parameter-declaration-list: 1667193326Sed // parameter-declaration 1668193326Sed // parameter-declaration-list ',' parameter-declaration 1669193326Sed // 1670193326Sed while (1) { 1671193326Sed // '...'[opt] 1672193326Sed if (Tok.is(tok::ellipsis)) { 1673193326Sed ConsumeToken(); 1674234353Sdim if (Tok.is(tok::r_paren)) 1675234353Sdim return TPResult::True(); // '...)' is a sign of a function declarator. 1676234353Sdim else 1677234353Sdim return TPResult::False(); 1678193326Sed } 1679193326Sed 1680234353Sdim // An attribute-specifier-seq here is a sign of a function declarator. 1681234353Sdim if (isCXX11AttributeSpecifier(/*Disambiguate*/false, 1682234353Sdim /*OuterMightBeMessageSend*/true)) 1683234353Sdim return TPResult::True(); 1684234353Sdim 1685221345Sdim ParsedAttributes attrs(AttrFactory); 1686218893Sdim MaybeParseMicrosoftAttributes(attrs); 1687218893Sdim 1688193326Sed // decl-specifier-seq 1689234353Sdim // A parameter-declaration's initializer must be preceded by an '=', so 1690234353Sdim // decl-specifier-seq '{' is not a parameter in C++11. 1691263508Sdim TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(), 1692263508Sdim InvalidAsDeclaration); 1693263508Sdim 1694263508Sdim if (VersusTemplateArgument && TPR == TPResult::True()) { 1695263508Sdim // Consume the decl-specifier-seq. We have to look past it, since a 1696263508Sdim // type-id might appear here in a template argument. 1697263508Sdim bool SeenType = false; 1698263508Sdim do { 1699263508Sdim SeenType |= isCXXDeclarationSpecifierAType(); 1700263508Sdim if (TryConsumeDeclarationSpecifier() == TPResult::Error()) 1701263508Sdim return TPResult::Error(); 1702263508Sdim 1703263508Sdim // If we see a parameter name, this can't be a template argument. 1704263508Sdim if (SeenType && Tok.is(tok::identifier)) 1705263508Sdim return TPResult::True(); 1706263508Sdim 1707263508Sdim TPR = isCXXDeclarationSpecifier(TPResult::False(), 1708263508Sdim InvalidAsDeclaration); 1709263508Sdim if (TPR == TPResult::Error()) 1710263508Sdim return TPR; 1711263508Sdim } while (TPR != TPResult::False()); 1712263508Sdim } else if (TPR == TPResult::Ambiguous()) { 1713263508Sdim // Disambiguate what follows the decl-specifier. 1714263508Sdim if (TryConsumeDeclarationSpecifier() == TPResult::Error()) 1715263508Sdim return TPResult::Error(); 1716263508Sdim } else 1717193326Sed return TPR; 1718193326Sed 1719193326Sed // declarator 1720193326Sed // abstract-declarator[opt] 1721193326Sed TPR = TryParseDeclarator(true/*mayBeAbstract*/); 1722193326Sed if (TPR != TPResult::Ambiguous()) 1723193326Sed return TPR; 1724193326Sed 1725218893Sdim // [GNU] attributes[opt] 1726218893Sdim if (Tok.is(tok::kw___attribute)) 1727218893Sdim return TPResult::True(); 1728218893Sdim 1729263508Sdim // If we're disambiguating a template argument in a default argument in 1730263508Sdim // a class definition versus a parameter declaration, an '=' here 1731263508Sdim // disambiguates the parse one way or the other. 1732263508Sdim // If this is a parameter, it must have a default argument because 1733263508Sdim // (a) the previous parameter did, and 1734263508Sdim // (b) this must be the first declaration of the function, so we can't 1735263508Sdim // inherit any default arguments from elsewhere. 1736263508Sdim // If we see an ')', then we've reached the end of a 1737263508Sdim // parameter-declaration-clause, and the last param is missing its default 1738263508Sdim // argument. 1739263508Sdim if (VersusTemplateArgument) 1740263508Sdim return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True() 1741263508Sdim : TPResult::False(); 1742263508Sdim 1743193326Sed if (Tok.is(tok::equal)) { 1744193326Sed // '=' assignment-expression 1745193326Sed // Parse through assignment-expression. 1746263508Sdim // FIXME: assignment-expression may contain an unparenthesized comma. 1747263508Sdim if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch)) 1748193326Sed return TPResult::Error(); 1749193326Sed } 1750193326Sed 1751193326Sed if (Tok.is(tok::ellipsis)) { 1752193326Sed ConsumeToken(); 1753234353Sdim if (Tok.is(tok::r_paren)) 1754234353Sdim return TPResult::True(); // '...)' is a sign of a function declarator. 1755234353Sdim else 1756234353Sdim return TPResult::False(); 1757193326Sed } 1758193326Sed 1759193326Sed if (Tok.isNot(tok::comma)) 1760193326Sed break; 1761193326Sed ConsumeToken(); // the comma. 1762193326Sed } 1763193326Sed 1764193326Sed return TPResult::Ambiguous(); 1765193326Sed} 1766193326Sed 1767193326Sed/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue 1768193326Sed/// parsing as a function declarator. 1769193326Sed/// If TryParseFunctionDeclarator fully parsed the function declarator, it will 1770193326Sed/// return TPResult::Ambiguous(), otherwise it will return either False() or 1771193326Sed/// Error(). 1772198092Srdivacky/// 1773193326Sed/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1774193326Sed/// exception-specification[opt] 1775193326Sed/// 1776193326Sed/// exception-specification: 1777193326Sed/// 'throw' '(' type-id-list[opt] ')' 1778193326Sed/// 1779193326SedParser::TPResult Parser::TryParseFunctionDeclarator() { 1780193326Sed 1781193326Sed // The '(' is already parsed. 1782193326Sed 1783193326Sed TPResult TPR = TryParseParameterDeclarationClause(); 1784193326Sed if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) 1785193326Sed TPR = TPResult::False(); 1786193326Sed 1787193326Sed if (TPR == TPResult::False() || TPR == TPResult::Error()) 1788193326Sed return TPR; 1789193326Sed 1790193326Sed // Parse through the parens. 1791263508Sdim if (!SkipUntil(tok::r_paren, StopAtSemi)) 1792193326Sed return TPResult::Error(); 1793193326Sed 1794193326Sed // cv-qualifier-seq 1795193326Sed while (Tok.is(tok::kw_const) || 1796193326Sed Tok.is(tok::kw_volatile) || 1797193326Sed Tok.is(tok::kw_restrict) ) 1798193326Sed ConsumeToken(); 1799193326Sed 1800218893Sdim // ref-qualifier[opt] 1801218893Sdim if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) 1802218893Sdim ConsumeToken(); 1803218893Sdim 1804193326Sed // exception-specification 1805193326Sed if (Tok.is(tok::kw_throw)) { 1806193326Sed ConsumeToken(); 1807193326Sed if (Tok.isNot(tok::l_paren)) 1808193326Sed return TPResult::Error(); 1809193326Sed 1810193326Sed // Parse through the parens after 'throw'. 1811193326Sed ConsumeParen(); 1812263508Sdim if (!SkipUntil(tok::r_paren, StopAtSemi)) 1813193326Sed return TPResult::Error(); 1814193326Sed } 1815221345Sdim if (Tok.is(tok::kw_noexcept)) { 1816221345Sdim ConsumeToken(); 1817221345Sdim // Possibly an expression as well. 1818221345Sdim if (Tok.is(tok::l_paren)) { 1819221345Sdim // Find the matching rparen. 1820221345Sdim ConsumeParen(); 1821263508Sdim if (!SkipUntil(tok::r_paren, StopAtSemi)) 1822221345Sdim return TPResult::Error(); 1823221345Sdim } 1824221345Sdim } 1825193326Sed 1826193326Sed return TPResult::Ambiguous(); 1827193326Sed} 1828193326Sed 1829193326Sed/// '[' constant-expression[opt] ']' 1830193326Sed/// 1831193326SedParser::TPResult Parser::TryParseBracketDeclarator() { 1832193326Sed ConsumeBracket(); 1833263508Sdim if (!SkipUntil(tok::r_square, StopAtSemi)) 1834193326Sed return TPResult::Error(); 1835193326Sed 1836193326Sed return TPResult::Ambiguous(); 1837193326Sed} 1838