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" 18 using namespace clang; 19 20 /// isCXXDeclarationStatement - C++-specialized function that disambiguates 21 /// between a declaration or an expression statement, when parsing function 22 /// bodies. Returns true for declaration, false for expression. 23 /// 24 /// declaration-statement: 25 /// block-declaration 26 /// 27 /// block-declaration: 28 /// simple-declaration 29 /// asm-definition 30 /// namespace-alias-definition 31 /// using-declaration 32 /// using-directive 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 /// 50 bool Parser::isCXXDeclarationStatement() { 51 switch (Tok.getKind()) { 52 // asm-definition 53 case tok::kw_asm: 54 // namespace-alias-definition 55 case tok::kw_namespace: 56 // using-declaration 57 // using-directive 58 case tok::kw_using: 59 // static_assert-declaration 60 case tok::kw_static_assert: 61 case tok::kw__Static_assert: 62 return true; 63 // simple-declaration 64 default: 65 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); 66 } 67 } 68 69 /// isCXXSimpleDeclaration - C++-specialized function that disambiguates 70 /// between a simple-declaration or an expression-statement. 71 /// If during the disambiguation process a parsing error is encountered, 72 /// the function returns true to let the declaration parsing code handle it. 73 /// Returns false if the statement is disambiguated as expression. 74 /// 75 /// simple-declaration: 76 /// decl-specifier-seq init-declarator-list[opt] ';' 77 /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']' 78 /// brace-or-equal-initializer ';' [C++17] 79 /// 80 /// (if AllowForRangeDecl specified) 81 /// for ( for-range-declaration : for-range-initializer ) statement 82 /// 83 /// for-range-declaration: 84 /// decl-specifier-seq declarator 85 /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']' 86 /// 87 /// In any of the above cases there can be a preceding attribute-specifier-seq, 88 /// but the caller is expected to handle that. 89 bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) { 90 // C++ 6.8p1: 91 // There is an ambiguity in the grammar involving expression-statements and 92 // declarations: An expression-statement with a function-style explicit type 93 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable 94 // from a declaration where the first declarator starts with a '('. In those 95 // cases the statement is a declaration. [Note: To disambiguate, the whole 96 // statement might have to be examined to determine if it is an 97 // expression-statement or a declaration]. 98 99 // C++ 6.8p3: 100 // The disambiguation is purely syntactic; that is, the meaning of the names 101 // occurring in such a statement, beyond whether they are type-names or not, 102 // is not generally used in or changed by the disambiguation. Class 103 // templates are instantiated as necessary to determine if a qualified name 104 // is a type-name. Disambiguation precedes parsing, and a statement 105 // disambiguated as a declaration may be an ill-formed declaration. 106 107 // We don't have to parse all of the decl-specifier-seq part. There's only 108 // an ambiguity if the first decl-specifier is 109 // simple-type-specifier/typename-specifier followed by a '(', which may 110 // indicate a function-style cast expression. 111 // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such 112 // a case. 113 114 bool InvalidAsDeclaration = false; 115 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False, 116 &InvalidAsDeclaration); 117 if (TPR != TPResult::Ambiguous) 118 return TPR != TPResult::False; // Returns true for TPResult::True or 119 // TPResult::Error. 120 121 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer, 122 // and so gets some cases wrong. We can't carry on if we've already seen 123 // something which makes this statement invalid as a declaration in this case, 124 // since it can cause us to misparse valid code. Revisit this once 125 // TryParseInitDeclaratorList is fixed. 126 if (InvalidAsDeclaration) 127 return false; 128 129 // FIXME: Add statistics about the number of ambiguous statements encountered 130 // and how they were resolved (number of declarations+number of expressions). 131 132 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(', 133 // or an identifier which doesn't resolve as anything. We need tentative 134 // parsing... 135 136 { 137 RevertingTentativeParsingAction PA(*this); 138 TPR = TryParseSimpleDeclaration(AllowForRangeDecl); 139 } 140 141 // In case of an error, let the declaration parsing code handle it. 142 if (TPR == TPResult::Error) 143 return true; 144 145 // Declarations take precedence over expressions. 146 if (TPR == TPResult::Ambiguous) 147 TPR = TPResult::True; 148 149 assert(TPR == TPResult::True || TPR == TPResult::False); 150 return TPR == TPResult::True; 151 } 152 153 /// Try to consume a token sequence that we've already identified as 154 /// (potentially) starting a decl-specifier. 155 Parser::TPResult Parser::TryConsumeDeclarationSpecifier() { 156 switch (Tok.getKind()) { 157 case tok::kw__Atomic: 158 if (NextToken().isNot(tok::l_paren)) { 159 ConsumeToken(); 160 break; 161 } 162 // Fall through. 163 case tok::kw_typeof: 164 case tok::kw___attribute: 165 case tok::kw___underlying_type: { 166 ConsumeToken(); 167 if (Tok.isNot(tok::l_paren)) 168 return TPResult::Error; 169 ConsumeParen(); 170 if (!SkipUntil(tok::r_paren)) 171 return TPResult::Error; 172 break; 173 } 174 175 case tok::kw_class: 176 case tok::kw_struct: 177 case tok::kw_union: 178 case tok::kw___interface: 179 case tok::kw_enum: 180 // elaborated-type-specifier: 181 // class-key attribute-specifier-seq[opt] 182 // nested-name-specifier[opt] identifier 183 // class-key nested-name-specifier[opt] template[opt] simple-template-id 184 // enum nested-name-specifier[opt] identifier 185 // 186 // FIXME: We don't support class-specifiers nor enum-specifiers here. 187 ConsumeToken(); 188 189 // Skip attributes. 190 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec, 191 tok::kw_alignas)) { 192 if (Tok.is(tok::l_square)) { 193 ConsumeBracket(); 194 if (!SkipUntil(tok::r_square)) 195 return TPResult::Error; 196 } else { 197 ConsumeToken(); 198 if (Tok.isNot(tok::l_paren)) 199 return TPResult::Error; 200 ConsumeParen(); 201 if (!SkipUntil(tok::r_paren)) 202 return TPResult::Error; 203 } 204 } 205 206 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype, 207 tok::annot_template_id) && 208 TryAnnotateCXXScopeToken()) 209 return TPResult::Error; 210 if (Tok.is(tok::annot_cxxscope)) 211 ConsumeToken(); 212 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) 213 return TPResult::Error; 214 ConsumeToken(); 215 break; 216 217 case tok::annot_cxxscope: 218 ConsumeToken(); 219 // Fall through. 220 default: 221 ConsumeToken(); 222 223 if (getLangOpts().ObjC1 && Tok.is(tok::less)) 224 return TryParseProtocolQualifiers(); 225 break; 226 } 227 228 return TPResult::Ambiguous; 229 } 230 231 /// simple-declaration: 232 /// decl-specifier-seq init-declarator-list[opt] ';' 233 /// 234 /// (if AllowForRangeDecl specified) 235 /// for ( for-range-declaration : for-range-initializer ) statement 236 /// for-range-declaration: 237 /// attribute-specifier-seqopt type-specifier-seq declarator 238 /// 239 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) { 240 if (TryConsumeDeclarationSpecifier() == TPResult::Error) 241 return TPResult::Error; 242 243 // Two decl-specifiers in a row conclusively disambiguate this as being a 244 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the 245 // overwhelmingly common case that the next token is a '('. 246 if (Tok.isNot(tok::l_paren)) { 247 TPResult TPR = isCXXDeclarationSpecifier(); 248 if (TPR == TPResult::Ambiguous) 249 return TPResult::True; 250 if (TPR == TPResult::True || TPR == TPResult::Error) 251 return TPR; 252 assert(TPR == TPResult::False); 253 } 254 255 TPResult TPR = TryParseInitDeclaratorList(); 256 if (TPR != TPResult::Ambiguous) 257 return TPR; 258 259 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon))) 260 return TPResult::False; 261 262 return TPResult::Ambiguous; 263 } 264 265 /// Tentatively parse an init-declarator-list in order to disambiguate it from 266 /// an expression. 267 /// 268 /// init-declarator-list: 269 /// init-declarator 270 /// init-declarator-list ',' init-declarator 271 /// 272 /// init-declarator: 273 /// declarator initializer[opt] 274 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] 275 /// 276 /// initializer: 277 /// brace-or-equal-initializer 278 /// '(' expression-list ')' 279 /// 280 /// brace-or-equal-initializer: 281 /// '=' initializer-clause 282 /// [C++11] braced-init-list 283 /// 284 /// initializer-clause: 285 /// assignment-expression 286 /// braced-init-list 287 /// 288 /// braced-init-list: 289 /// '{' initializer-list ','[opt] '}' 290 /// '{' '}' 291 /// 292 Parser::TPResult Parser::TryParseInitDeclaratorList() { 293 while (1) { 294 // declarator 295 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); 296 if (TPR != TPResult::Ambiguous) 297 return TPR; 298 299 // [GNU] simple-asm-expr[opt] attributes[opt] 300 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute)) 301 return TPResult::True; 302 303 // initializer[opt] 304 if (Tok.is(tok::l_paren)) { 305 // Parse through the parens. 306 ConsumeParen(); 307 if (!SkipUntil(tok::r_paren, StopAtSemi)) 308 return TPResult::Error; 309 } else if (Tok.is(tok::l_brace)) { 310 // A left-brace here is sufficient to disambiguate the parse; an 311 // expression can never be followed directly by a braced-init-list. 312 return TPResult::True; 313 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) { 314 // MSVC and g++ won't examine the rest of declarators if '=' is 315 // encountered; they just conclude that we have a declaration. 316 // EDG parses the initializer completely, which is the proper behavior 317 // for this case. 318 // 319 // At present, Clang follows MSVC and g++, since the parser does not have 320 // the ability to parse an expression fully without recording the 321 // results of that parse. 322 // FIXME: Handle this case correctly. 323 // 324 // Also allow 'in' after an Objective-C declaration as in: 325 // for (int (^b)(void) in array). Ideally this should be done in the 326 // context of parsing for-init-statement of a foreach statement only. But, 327 // in any other context 'in' is invalid after a declaration and parser 328 // issues the error regardless of outcome of this decision. 329 // FIXME: Change if above assumption does not hold. 330 return TPResult::True; 331 } 332 333 if (!TryConsumeToken(tok::comma)) 334 break; 335 } 336 337 return TPResult::Ambiguous; 338 } 339 340 struct Parser::ConditionDeclarationOrInitStatementState { 341 Parser &P; 342 bool CanBeExpression = true; 343 bool CanBeCondition = true; 344 bool CanBeInitStatement; 345 346 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement) 347 : P(P), CanBeInitStatement(CanBeInitStatement) {} 348 349 void markNotExpression() { 350 CanBeExpression = false; 351 352 if (CanBeCondition && CanBeInitStatement) { 353 // FIXME: Unify the parsing codepaths for condition variables and 354 // simple-declarations so that we don't need to eagerly figure out which 355 // kind we have here. (Just parse init-declarators until we reach a 356 // semicolon or right paren.) 357 RevertingTentativeParsingAction PA(P); 358 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch); 359 if (P.Tok.isNot(tok::r_paren)) 360 CanBeCondition = false; 361 if (P.Tok.isNot(tok::semi)) 362 CanBeInitStatement = false; 363 } 364 } 365 366 bool markNotCondition() { 367 CanBeCondition = false; 368 return !CanBeInitStatement || !CanBeExpression; 369 } 370 371 bool update(TPResult IsDecl) { 372 switch (IsDecl) { 373 case TPResult::True: 374 markNotExpression(); 375 return true; 376 case TPResult::False: 377 CanBeCondition = CanBeInitStatement = false; 378 return true; 379 case TPResult::Ambiguous: 380 return false; 381 case TPResult::Error: 382 CanBeExpression = CanBeCondition = CanBeInitStatement = false; 383 return true; 384 } 385 llvm_unreachable("unknown tentative parse result"); 386 } 387 388 ConditionOrInitStatement result() const { 389 assert(CanBeExpression + CanBeCondition + CanBeInitStatement < 2 && 390 "result called but not yet resolved"); 391 if (CanBeExpression) 392 return ConditionOrInitStatement::Expression; 393 if (CanBeCondition) 394 return ConditionOrInitStatement::ConditionDecl; 395 if (CanBeInitStatement) 396 return ConditionOrInitStatement::InitStmtDecl; 397 return ConditionOrInitStatement::Error; 398 } 399 }; 400 401 /// \brief Disambiguates between a declaration in a condition, a 402 /// simple-declaration in an init-statement, and an expression for 403 /// a condition of a if/switch statement. 404 /// 405 /// condition: 406 /// expression 407 /// type-specifier-seq declarator '=' assignment-expression 408 /// [C++11] type-specifier-seq declarator '=' initializer-clause 409 /// [C++11] type-specifier-seq declarator braced-init-list 410 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 411 /// '=' assignment-expression 412 /// simple-declaration: 413 /// decl-specifier-seq init-declarator-list[opt] ';' 414 /// 415 /// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way 416 /// to the ';' to disambiguate cases like 'int(x))' (an expression) from 417 /// 'int(x);' (a simple-declaration in an init-statement). 418 Parser::ConditionOrInitStatement 419 Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement) { 420 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement); 421 422 if (State.update(isCXXDeclarationSpecifier())) 423 return State.result(); 424 425 // It might be a declaration; we need tentative parsing. 426 RevertingTentativeParsingAction PA(*this); 427 428 // FIXME: A tag definition unambiguously tells us this is an init-statement. 429 if (State.update(TryConsumeDeclarationSpecifier())) 430 return State.result(); 431 assert(Tok.is(tok::l_paren) && "Expected '('"); 432 433 while (true) { 434 // Consume a declarator. 435 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/))) 436 return State.result(); 437 438 // Attributes, asm label, or an initializer imply this is not an expression. 439 // FIXME: Disambiguate properly after an = instead of assuming that it's a 440 // valid declaration. 441 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) || 442 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) { 443 State.markNotExpression(); 444 return State.result(); 445 } 446 447 // At this point, it can't be a condition any more, because a condition 448 // must have a brace-or-equal-initializer. 449 if (State.markNotCondition()) 450 return State.result(); 451 452 // A parenthesized initializer could be part of an expression or a 453 // simple-declaration. 454 if (Tok.is(tok::l_paren)) { 455 ConsumeParen(); 456 SkipUntil(tok::r_paren, StopAtSemi); 457 } 458 459 if (!TryConsumeToken(tok::comma)) 460 break; 461 } 462 463 // We reached the end. If it can now be some kind of decl, then it is. 464 if (State.CanBeCondition && Tok.is(tok::r_paren)) 465 return ConditionOrInitStatement::ConditionDecl; 466 else if (State.CanBeInitStatement && Tok.is(tok::semi)) 467 return ConditionOrInitStatement::InitStmtDecl; 468 else 469 return ConditionOrInitStatement::Expression; 470 } 471 472 /// \brief Determine whether the next set of tokens contains a type-id. 473 /// 474 /// The context parameter states what context we're parsing right 475 /// now, which affects how this routine copes with the token 476 /// following the type-id. If the context is TypeIdInParens, we have 477 /// already parsed the '(' and we will cease lookahead when we hit 478 /// the corresponding ')'. If the context is 479 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ',' 480 /// before this template argument, and will cease lookahead when we 481 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id 482 /// and false for an expression. If during the disambiguation 483 /// process a parsing error is encountered, the function returns 484 /// true to let the declaration parsing code handle it. 485 /// 486 /// type-id: 487 /// type-specifier-seq abstract-declarator[opt] 488 /// 489 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) { 490 491 isAmbiguous = false; 492 493 // C++ 8.2p2: 494 // The ambiguity arising from the similarity between a function-style cast and 495 // a type-id can occur in different contexts. The ambiguity appears as a 496 // choice between a function-style cast expression and a declaration of a 497 // type. The resolution is that any construct that could possibly be a type-id 498 // in its syntactic context shall be considered a type-id. 499 500 TPResult TPR = isCXXDeclarationSpecifier(); 501 if (TPR != TPResult::Ambiguous) 502 return TPR != TPResult::False; // Returns true for TPResult::True or 503 // TPResult::Error. 504 505 // FIXME: Add statistics about the number of ambiguous statements encountered 506 // and how they were resolved (number of declarations+number of expressions). 507 508 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. 509 // We need tentative parsing... 510 511 RevertingTentativeParsingAction PA(*this); 512 513 // type-specifier-seq 514 TryConsumeDeclarationSpecifier(); 515 assert(Tok.is(tok::l_paren) && "Expected '('"); 516 517 // declarator 518 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); 519 520 // In case of an error, let the declaration parsing code handle it. 521 if (TPR == TPResult::Error) 522 TPR = TPResult::True; 523 524 if (TPR == TPResult::Ambiguous) { 525 // We are supposed to be inside parens, so if after the abstract declarator 526 // we encounter a ')' this is a type-id, otherwise it's an expression. 527 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) { 528 TPR = TPResult::True; 529 isAmbiguous = true; 530 531 // We are supposed to be inside a template argument, so if after 532 // the abstract declarator we encounter a '>', '>>' (in C++0x), or 533 // ',', this is a type-id. Otherwise, it's an expression. 534 } else if (Context == TypeIdAsTemplateArgument && 535 (Tok.isOneOf(tok::greater, tok::comma) || 536 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) { 537 TPR = TPResult::True; 538 isAmbiguous = true; 539 540 } else 541 TPR = TPResult::False; 542 } 543 544 assert(TPR == TPResult::True || TPR == TPResult::False); 545 return TPR == TPResult::True; 546 } 547 548 /// \brief Returns true if this is a C++11 attribute-specifier. Per 549 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens 550 /// always introduce an attribute. In Objective-C++11, this rule does not 551 /// apply if either '[' begins a message-send. 552 /// 553 /// If Disambiguate is true, we try harder to determine whether a '[[' starts 554 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not. 555 /// 556 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an 557 /// Obj-C message send or the start of an attribute. Otherwise, we assume it 558 /// is not an Obj-C message send. 559 /// 560 /// C++11 [dcl.attr.grammar]: 561 /// 562 /// attribute-specifier: 563 /// '[' '[' attribute-list ']' ']' 564 /// alignment-specifier 565 /// 566 /// attribute-list: 567 /// attribute[opt] 568 /// attribute-list ',' attribute[opt] 569 /// attribute '...' 570 /// attribute-list ',' attribute '...' 571 /// 572 /// attribute: 573 /// attribute-token attribute-argument-clause[opt] 574 /// 575 /// attribute-token: 576 /// identifier 577 /// identifier '::' identifier 578 /// 579 /// attribute-argument-clause: 580 /// '(' balanced-token-seq ')' 581 Parser::CXX11AttributeKind 582 Parser::isCXX11AttributeSpecifier(bool Disambiguate, 583 bool OuterMightBeMessageSend) { 584 if (Tok.is(tok::kw_alignas)) 585 return CAK_AttributeSpecifier; 586 587 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square)) 588 return CAK_NotAttributeSpecifier; 589 590 // No tentative parsing if we don't need to look for ']]' or a lambda. 591 if (!Disambiguate && !getLangOpts().ObjC1) 592 return CAK_AttributeSpecifier; 593 594 RevertingTentativeParsingAction PA(*this); 595 596 // Opening brackets were checked for above. 597 ConsumeBracket(); 598 599 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute. 600 if (!getLangOpts().ObjC1) { 601 ConsumeBracket(); 602 603 bool IsAttribute = SkipUntil(tok::r_square); 604 IsAttribute &= Tok.is(tok::r_square); 605 606 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier; 607 } 608 609 // In Obj-C++11, we need to distinguish four situations: 610 // 1a) int x[[attr]]; C++11 attribute. 611 // 1b) [[attr]]; C++11 statement attribute. 612 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index. 613 // 3a) int x[[obj get]]; Message send in array size/index. 614 // 3b) [[Class alloc] init]; Message send in message send. 615 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send. 616 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted. 617 618 // If we have a lambda-introducer, then this is definitely not a message send. 619 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse 620 // into the tentative attribute parse below. 621 LambdaIntroducer Intro; 622 if (!TryParseLambdaIntroducer(Intro)) { 623 // A lambda cannot end with ']]', and an attribute must. 624 bool IsAttribute = Tok.is(tok::r_square); 625 626 if (IsAttribute) 627 // Case 1: C++11 attribute. 628 return CAK_AttributeSpecifier; 629 630 if (OuterMightBeMessageSend) 631 // Case 4: Lambda in message send. 632 return CAK_NotAttributeSpecifier; 633 634 // Case 2: Lambda in array size / index. 635 return CAK_InvalidAttributeSpecifier; 636 } 637 638 ConsumeBracket(); 639 640 // If we don't have a lambda-introducer, then we have an attribute or a 641 // message-send. 642 bool IsAttribute = true; 643 while (Tok.isNot(tok::r_square)) { 644 if (Tok.is(tok::comma)) { 645 // Case 1: Stray commas can only occur in attributes. 646 return CAK_AttributeSpecifier; 647 } 648 649 // Parse the attribute-token, if present. 650 // C++11 [dcl.attr.grammar]: 651 // If a keyword or an alternative token that satisfies the syntactic 652 // requirements of an identifier is contained in an attribute-token, 653 // it is considered an identifier. 654 SourceLocation Loc; 655 if (!TryParseCXX11AttributeIdentifier(Loc)) { 656 IsAttribute = false; 657 break; 658 } 659 if (Tok.is(tok::coloncolon)) { 660 ConsumeToken(); 661 if (!TryParseCXX11AttributeIdentifier(Loc)) { 662 IsAttribute = false; 663 break; 664 } 665 } 666 667 // Parse the attribute-argument-clause, if present. 668 if (Tok.is(tok::l_paren)) { 669 ConsumeParen(); 670 if (!SkipUntil(tok::r_paren)) { 671 IsAttribute = false; 672 break; 673 } 674 } 675 676 TryConsumeToken(tok::ellipsis); 677 678 if (!TryConsumeToken(tok::comma)) 679 break; 680 } 681 682 // An attribute must end ']]'. 683 if (IsAttribute) { 684 if (Tok.is(tok::r_square)) { 685 ConsumeBracket(); 686 IsAttribute = Tok.is(tok::r_square); 687 } else { 688 IsAttribute = false; 689 } 690 } 691 692 if (IsAttribute) 693 // Case 1: C++11 statement attribute. 694 return CAK_AttributeSpecifier; 695 696 // Case 3: Message send. 697 return CAK_NotAttributeSpecifier; 698 } 699 700 Parser::TPResult Parser::TryParsePtrOperatorSeq() { 701 while (true) { 702 if (Tok.isOneOf(tok::coloncolon, tok::identifier)) 703 if (TryAnnotateCXXScopeToken(true)) 704 return TPResult::Error; 705 706 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) || 707 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) { 708 // ptr-operator 709 ConsumeToken(); 710 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict, 711 tok::kw__Nonnull, tok::kw__Nullable, 712 tok::kw__Null_unspecified)) 713 ConsumeToken(); 714 } else { 715 return TPResult::True; 716 } 717 } 718 } 719 720 /// operator-function-id: 721 /// 'operator' operator 722 /// 723 /// operator: one of 724 /// new delete new[] delete[] + - * / % ^ [...] 725 /// 726 /// conversion-function-id: 727 /// 'operator' conversion-type-id 728 /// 729 /// conversion-type-id: 730 /// type-specifier-seq conversion-declarator[opt] 731 /// 732 /// conversion-declarator: 733 /// ptr-operator conversion-declarator[opt] 734 /// 735 /// literal-operator-id: 736 /// 'operator' string-literal identifier 737 /// 'operator' user-defined-string-literal 738 Parser::TPResult Parser::TryParseOperatorId() { 739 assert(Tok.is(tok::kw_operator)); 740 ConsumeToken(); 741 742 // Maybe this is an operator-function-id. 743 switch (Tok.getKind()) { 744 case tok::kw_new: case tok::kw_delete: 745 ConsumeToken(); 746 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) { 747 ConsumeBracket(); 748 ConsumeBracket(); 749 } 750 return TPResult::True; 751 752 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \ 753 case tok::Token: 754 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly) 755 #include "clang/Basic/OperatorKinds.def" 756 ConsumeToken(); 757 return TPResult::True; 758 759 case tok::l_square: 760 if (NextToken().is(tok::r_square)) { 761 ConsumeBracket(); 762 ConsumeBracket(); 763 return TPResult::True; 764 } 765 break; 766 767 case tok::l_paren: 768 if (NextToken().is(tok::r_paren)) { 769 ConsumeParen(); 770 ConsumeParen(); 771 return TPResult::True; 772 } 773 break; 774 775 default: 776 break; 777 } 778 779 // Maybe this is a literal-operator-id. 780 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) { 781 bool FoundUDSuffix = false; 782 do { 783 FoundUDSuffix |= Tok.hasUDSuffix(); 784 ConsumeStringToken(); 785 } while (isTokenStringLiteral()); 786 787 if (!FoundUDSuffix) { 788 if (Tok.is(tok::identifier)) 789 ConsumeToken(); 790 else 791 return TPResult::Error; 792 } 793 return TPResult::True; 794 } 795 796 // Maybe this is a conversion-function-id. 797 bool AnyDeclSpecifiers = false; 798 while (true) { 799 TPResult TPR = isCXXDeclarationSpecifier(); 800 if (TPR == TPResult::Error) 801 return TPR; 802 if (TPR == TPResult::False) { 803 if (!AnyDeclSpecifiers) 804 return TPResult::Error; 805 break; 806 } 807 if (TryConsumeDeclarationSpecifier() == TPResult::Error) 808 return TPResult::Error; 809 AnyDeclSpecifiers = true; 810 } 811 return TryParsePtrOperatorSeq(); 812 } 813 814 /// declarator: 815 /// direct-declarator 816 /// ptr-operator declarator 817 /// 818 /// direct-declarator: 819 /// declarator-id 820 /// direct-declarator '(' parameter-declaration-clause ')' 821 /// cv-qualifier-seq[opt] exception-specification[opt] 822 /// direct-declarator '[' constant-expression[opt] ']' 823 /// '(' declarator ')' 824 /// [GNU] '(' attributes declarator ')' 825 /// 826 /// abstract-declarator: 827 /// ptr-operator abstract-declarator[opt] 828 /// direct-abstract-declarator 829 /// ... 830 /// 831 /// direct-abstract-declarator: 832 /// direct-abstract-declarator[opt] 833 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 834 /// exception-specification[opt] 835 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 836 /// '(' abstract-declarator ')' 837 /// 838 /// ptr-operator: 839 /// '*' cv-qualifier-seq[opt] 840 /// '&' 841 /// [C++0x] '&&' [TODO] 842 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] 843 /// 844 /// cv-qualifier-seq: 845 /// cv-qualifier cv-qualifier-seq[opt] 846 /// 847 /// cv-qualifier: 848 /// 'const' 849 /// 'volatile' 850 /// 851 /// declarator-id: 852 /// '...'[opt] id-expression 853 /// 854 /// id-expression: 855 /// unqualified-id 856 /// qualified-id [TODO] 857 /// 858 /// unqualified-id: 859 /// identifier 860 /// operator-function-id 861 /// conversion-function-id 862 /// literal-operator-id 863 /// '~' class-name [TODO] 864 /// '~' decltype-specifier [TODO] 865 /// template-id [TODO] 866 /// 867 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, 868 bool mayHaveIdentifier) { 869 // declarator: 870 // direct-declarator 871 // ptr-operator declarator 872 if (TryParsePtrOperatorSeq() == TPResult::Error) 873 return TPResult::Error; 874 875 // direct-declarator: 876 // direct-abstract-declarator: 877 if (Tok.is(tok::ellipsis)) 878 ConsumeToken(); 879 880 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) || 881 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) || 882 NextToken().is(tok::kw_operator)))) && 883 mayHaveIdentifier) { 884 // declarator-id 885 if (Tok.is(tok::annot_cxxscope)) 886 ConsumeToken(); 887 else if (Tok.is(tok::identifier)) 888 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo()); 889 if (Tok.is(tok::kw_operator)) { 890 if (TryParseOperatorId() == TPResult::Error) 891 return TPResult::Error; 892 } else 893 ConsumeToken(); 894 } else if (Tok.is(tok::l_paren)) { 895 ConsumeParen(); 896 if (mayBeAbstract && 897 (Tok.is(tok::r_paren) || // 'int()' is a function. 898 // 'int(...)' is a function. 899 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) || 900 isDeclarationSpecifier())) { // 'int(int)' is a function. 901 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 902 // exception-specification[opt] 903 TPResult TPR = TryParseFunctionDeclarator(); 904 if (TPR != TPResult::Ambiguous) 905 return TPR; 906 } else { 907 // '(' declarator ')' 908 // '(' attributes declarator ')' 909 // '(' abstract-declarator ')' 910 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl, 911 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall, 912 tok::kw___regcall, tok::kw___vectorcall)) 913 return TPResult::True; // attributes indicate declaration 914 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); 915 if (TPR != TPResult::Ambiguous) 916 return TPR; 917 if (Tok.isNot(tok::r_paren)) 918 return TPResult::False; 919 ConsumeParen(); 920 } 921 } else if (!mayBeAbstract) { 922 return TPResult::False; 923 } 924 925 while (1) { 926 TPResult TPR(TPResult::Ambiguous); 927 928 // abstract-declarator: ... 929 if (Tok.is(tok::ellipsis)) 930 ConsumeToken(); 931 932 if (Tok.is(tok::l_paren)) { 933 // Check whether we have a function declarator or a possible ctor-style 934 // initializer that follows the declarator. Note that ctor-style 935 // initializers are not possible in contexts where abstract declarators 936 // are allowed. 937 if (!mayBeAbstract && !isCXXFunctionDeclarator()) 938 break; 939 940 // direct-declarator '(' parameter-declaration-clause ')' 941 // cv-qualifier-seq[opt] exception-specification[opt] 942 ConsumeParen(); 943 TPR = TryParseFunctionDeclarator(); 944 } else if (Tok.is(tok::l_square)) { 945 // direct-declarator '[' constant-expression[opt] ']' 946 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 947 TPR = TryParseBracketDeclarator(); 948 } else { 949 break; 950 } 951 952 if (TPR != TPResult::Ambiguous) 953 return TPR; 954 } 955 956 return TPResult::Ambiguous; 957 } 958 959 Parser::TPResult 960 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) { 961 switch (Kind) { 962 // Obviously starts an expression. 963 case tok::numeric_constant: 964 case tok::char_constant: 965 case tok::wide_char_constant: 966 case tok::utf8_char_constant: 967 case tok::utf16_char_constant: 968 case tok::utf32_char_constant: 969 case tok::string_literal: 970 case tok::wide_string_literal: 971 case tok::utf8_string_literal: 972 case tok::utf16_string_literal: 973 case tok::utf32_string_literal: 974 case tok::l_square: 975 case tok::l_paren: 976 case tok::amp: 977 case tok::ampamp: 978 case tok::star: 979 case tok::plus: 980 case tok::plusplus: 981 case tok::minus: 982 case tok::minusminus: 983 case tok::tilde: 984 case tok::exclaim: 985 case tok::kw_sizeof: 986 case tok::kw___func__: 987 case tok::kw_const_cast: 988 case tok::kw_delete: 989 case tok::kw_dynamic_cast: 990 case tok::kw_false: 991 case tok::kw_new: 992 case tok::kw_operator: 993 case tok::kw_reinterpret_cast: 994 case tok::kw_static_cast: 995 case tok::kw_this: 996 case tok::kw_throw: 997 case tok::kw_true: 998 case tok::kw_typeid: 999 case tok::kw_alignof: 1000 case tok::kw_noexcept: 1001 case tok::kw_nullptr: 1002 case tok::kw__Alignof: 1003 case tok::kw___null: 1004 case tok::kw___alignof: 1005 case tok::kw___builtin_choose_expr: 1006 case tok::kw___builtin_offsetof: 1007 case tok::kw___builtin_va_arg: 1008 case tok::kw___imag: 1009 case tok::kw___real: 1010 case tok::kw___FUNCTION__: 1011 case tok::kw___FUNCDNAME__: 1012 case tok::kw___FUNCSIG__: 1013 case tok::kw_L__FUNCTION__: 1014 case tok::kw___PRETTY_FUNCTION__: 1015 case tok::kw___uuidof: 1016 #define TYPE_TRAIT(N,Spelling,K) \ 1017 case tok::kw_##Spelling: 1018 #include "clang/Basic/TokenKinds.def" 1019 return TPResult::True; 1020 1021 // Obviously starts a type-specifier-seq: 1022 case tok::kw_char: 1023 case tok::kw_const: 1024 case tok::kw_double: 1025 case tok::kw___float128: 1026 case tok::kw_enum: 1027 case tok::kw_half: 1028 case tok::kw_float: 1029 case tok::kw_int: 1030 case tok::kw_long: 1031 case tok::kw___int64: 1032 case tok::kw___int128: 1033 case tok::kw_restrict: 1034 case tok::kw_short: 1035 case tok::kw_signed: 1036 case tok::kw_struct: 1037 case tok::kw_union: 1038 case tok::kw_unsigned: 1039 case tok::kw_void: 1040 case tok::kw_volatile: 1041 case tok::kw__Bool: 1042 case tok::kw__Complex: 1043 case tok::kw_class: 1044 case tok::kw_typename: 1045 case tok::kw_wchar_t: 1046 case tok::kw_char16_t: 1047 case tok::kw_char32_t: 1048 case tok::kw__Decimal32: 1049 case tok::kw__Decimal64: 1050 case tok::kw__Decimal128: 1051 case tok::kw___interface: 1052 case tok::kw___thread: 1053 case tok::kw_thread_local: 1054 case tok::kw__Thread_local: 1055 case tok::kw_typeof: 1056 case tok::kw___underlying_type: 1057 case tok::kw___cdecl: 1058 case tok::kw___stdcall: 1059 case tok::kw___fastcall: 1060 case tok::kw___thiscall: 1061 case tok::kw___regcall: 1062 case tok::kw___vectorcall: 1063 case tok::kw___unaligned: 1064 case tok::kw___vector: 1065 case tok::kw___pixel: 1066 case tok::kw___bool: 1067 case tok::kw__Atomic: 1068 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: 1069 #include "clang/Basic/OpenCLImageTypes.def" 1070 case tok::kw___unknown_anytype: 1071 return TPResult::False; 1072 1073 default: 1074 break; 1075 } 1076 1077 return TPResult::Ambiguous; 1078 } 1079 1080 bool Parser::isTentativelyDeclared(IdentifierInfo *II) { 1081 return std::find(TentativelyDeclaredIdentifiers.begin(), 1082 TentativelyDeclaredIdentifiers.end(), II) 1083 != TentativelyDeclaredIdentifiers.end(); 1084 } 1085 1086 namespace { 1087 class TentativeParseCCC : public CorrectionCandidateCallback { 1088 public: 1089 TentativeParseCCC(const Token &Next) { 1090 WantRemainingKeywords = false; 1091 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater, 1092 tok::l_brace, tok::identifier); 1093 } 1094 1095 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1096 // Reject any candidate that only resolves to instance members since they 1097 // aren't viable as standalone identifiers instead of member references. 1098 if (Candidate.isResolved() && !Candidate.isKeyword() && 1099 std::all_of(Candidate.begin(), Candidate.end(), 1100 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); })) 1101 return false; 1102 1103 return CorrectionCandidateCallback::ValidateCandidate(Candidate); 1104 } 1105 }; 1106 } 1107 /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration 1108 /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could 1109 /// be either a decl-specifier or a function-style cast, and TPResult::Error 1110 /// if a parsing error was found and reported. 1111 /// 1112 /// If HasMissingTypename is provided, a name with a dependent scope specifier 1113 /// will be treated as ambiguous if the 'typename' keyword is missing. If this 1114 /// happens, *HasMissingTypename will be set to 'true'. This will also be used 1115 /// as an indicator that undeclared identifiers (which will trigger a later 1116 /// parse error) should be treated as types. Returns TPResult::Ambiguous in 1117 /// such cases. 1118 /// 1119 /// decl-specifier: 1120 /// storage-class-specifier 1121 /// type-specifier 1122 /// function-specifier 1123 /// 'friend' 1124 /// 'typedef' 1125 /// [C++11] 'constexpr' 1126 /// [GNU] attributes declaration-specifiers[opt] 1127 /// 1128 /// storage-class-specifier: 1129 /// 'register' 1130 /// 'static' 1131 /// 'extern' 1132 /// 'mutable' 1133 /// 'auto' 1134 /// [GNU] '__thread' 1135 /// [C++11] 'thread_local' 1136 /// [C11] '_Thread_local' 1137 /// 1138 /// function-specifier: 1139 /// 'inline' 1140 /// 'virtual' 1141 /// 'explicit' 1142 /// 1143 /// typedef-name: 1144 /// identifier 1145 /// 1146 /// type-specifier: 1147 /// simple-type-specifier 1148 /// class-specifier 1149 /// enum-specifier 1150 /// elaborated-type-specifier 1151 /// typename-specifier 1152 /// cv-qualifier 1153 /// 1154 /// simple-type-specifier: 1155 /// '::'[opt] nested-name-specifier[opt] type-name 1156 /// '::'[opt] nested-name-specifier 'template' 1157 /// simple-template-id [TODO] 1158 /// 'char' 1159 /// 'wchar_t' 1160 /// 'bool' 1161 /// 'short' 1162 /// 'int' 1163 /// 'long' 1164 /// 'signed' 1165 /// 'unsigned' 1166 /// 'float' 1167 /// 'double' 1168 /// 'void' 1169 /// [GNU] typeof-specifier 1170 /// [GNU] '_Complex' 1171 /// [C++11] 'auto' 1172 /// [GNU] '__auto_type' 1173 /// [C++11] 'decltype' ( expression ) 1174 /// [C++1y] 'decltype' ( 'auto' ) 1175 /// 1176 /// type-name: 1177 /// class-name 1178 /// enum-name 1179 /// typedef-name 1180 /// 1181 /// elaborated-type-specifier: 1182 /// class-key '::'[opt] nested-name-specifier[opt] identifier 1183 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] 1184 /// simple-template-id 1185 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier 1186 /// 1187 /// enum-name: 1188 /// identifier 1189 /// 1190 /// enum-specifier: 1191 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}' 1192 /// 'enum' identifier[opt] '{' enumerator-list ',' '}' 1193 /// 1194 /// class-specifier: 1195 /// class-head '{' member-specification[opt] '}' 1196 /// 1197 /// class-head: 1198 /// class-key identifier[opt] base-clause[opt] 1199 /// class-key nested-name-specifier identifier base-clause[opt] 1200 /// class-key nested-name-specifier[opt] simple-template-id 1201 /// base-clause[opt] 1202 /// 1203 /// class-key: 1204 /// 'class' 1205 /// 'struct' 1206 /// 'union' 1207 /// 1208 /// cv-qualifier: 1209 /// 'const' 1210 /// 'volatile' 1211 /// [GNU] restrict 1212 /// 1213 Parser::TPResult 1214 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult, 1215 bool *HasMissingTypename) { 1216 switch (Tok.getKind()) { 1217 case tok::identifier: { 1218 // Check for need to substitute AltiVec __vector keyword 1219 // for "vector" identifier. 1220 if (TryAltiVecVectorToken()) 1221 return TPResult::True; 1222 1223 const Token &Next = NextToken(); 1224 // In 'foo bar', 'foo' is always a type name outside of Objective-C. 1225 if (!getLangOpts().ObjC1 && Next.is(tok::identifier)) 1226 return TPResult::True; 1227 1228 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) { 1229 // Determine whether this is a valid expression. If not, we will hit 1230 // a parse error one way or another. In that case, tell the caller that 1231 // this is ambiguous. Typo-correct to type and expression keywords and 1232 // to types and identifiers, in order to try to recover from errors. 1233 switch (TryAnnotateName(false /* no nested name specifier */, 1234 llvm::make_unique<TentativeParseCCC>(Next))) { 1235 case ANK_Error: 1236 return TPResult::Error; 1237 case ANK_TentativeDecl: 1238 return TPResult::False; 1239 case ANK_TemplateName: 1240 // A bare type template-name which can't be a template template 1241 // argument is an error, and was probably intended to be a type. 1242 return GreaterThanIsOperator ? TPResult::True : TPResult::False; 1243 case ANK_Unresolved: 1244 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False; 1245 case ANK_Success: 1246 break; 1247 } 1248 assert(Tok.isNot(tok::identifier) && 1249 "TryAnnotateName succeeded without producing an annotation"); 1250 } else { 1251 // This might possibly be a type with a dependent scope specifier and 1252 // a missing 'typename' keyword. Don't use TryAnnotateName in this case, 1253 // since it will annotate as a primary expression, and we want to use the 1254 // "missing 'typename'" logic. 1255 if (TryAnnotateTypeOrScopeToken()) 1256 return TPResult::Error; 1257 // If annotation failed, assume it's a non-type. 1258 // FIXME: If this happens due to an undeclared identifier, treat it as 1259 // ambiguous. 1260 if (Tok.is(tok::identifier)) 1261 return TPResult::False; 1262 } 1263 1264 // We annotated this token as something. Recurse to handle whatever we got. 1265 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1266 } 1267 1268 case tok::kw_typename: // typename T::type 1269 // Annotate typenames and C++ scope specifiers. If we get one, just 1270 // recurse to handle whatever we get. 1271 if (TryAnnotateTypeOrScopeToken()) 1272 return TPResult::Error; 1273 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1274 1275 case tok::coloncolon: { // ::foo::bar 1276 const Token &Next = NextToken(); 1277 if (Next.isOneOf(tok::kw_new, // ::new 1278 tok::kw_delete)) // ::delete 1279 return TPResult::False; 1280 } 1281 // Fall through. 1282 case tok::kw___super: 1283 case tok::kw_decltype: 1284 // Annotate typenames and C++ scope specifiers. If we get one, just 1285 // recurse to handle whatever we get. 1286 if (TryAnnotateTypeOrScopeToken()) 1287 return TPResult::Error; 1288 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 1289 1290 // decl-specifier: 1291 // storage-class-specifier 1292 // type-specifier 1293 // function-specifier 1294 // 'friend' 1295 // 'typedef' 1296 // 'constexpr' 1297 // 'concept' 1298 case tok::kw_friend: 1299 case tok::kw_typedef: 1300 case tok::kw_constexpr: 1301 case tok::kw_concept: 1302 // storage-class-specifier 1303 case tok::kw_register: 1304 case tok::kw_static: 1305 case tok::kw_extern: 1306 case tok::kw_mutable: 1307 case tok::kw_auto: 1308 case tok::kw___thread: 1309 case tok::kw_thread_local: 1310 case tok::kw__Thread_local: 1311 // function-specifier 1312 case tok::kw_inline: 1313 case tok::kw_virtual: 1314 case tok::kw_explicit: 1315 1316 // Modules 1317 case tok::kw___module_private__: 1318 1319 // Debugger support 1320 case tok::kw___unknown_anytype: 1321 1322 // type-specifier: 1323 // simple-type-specifier 1324 // class-specifier 1325 // enum-specifier 1326 // elaborated-type-specifier 1327 // typename-specifier 1328 // cv-qualifier 1329 1330 // class-specifier 1331 // elaborated-type-specifier 1332 case tok::kw_class: 1333 case tok::kw_struct: 1334 case tok::kw_union: 1335 case tok::kw___interface: 1336 // enum-specifier 1337 case tok::kw_enum: 1338 // cv-qualifier 1339 case tok::kw_const: 1340 case tok::kw_volatile: 1341 1342 // GNU 1343 case tok::kw_restrict: 1344 case tok::kw__Complex: 1345 case tok::kw___attribute: 1346 case tok::kw___auto_type: 1347 return TPResult::True; 1348 1349 // Microsoft 1350 case tok::kw___declspec: 1351 case tok::kw___cdecl: 1352 case tok::kw___stdcall: 1353 case tok::kw___fastcall: 1354 case tok::kw___thiscall: 1355 case tok::kw___regcall: 1356 case tok::kw___vectorcall: 1357 case tok::kw___w64: 1358 case tok::kw___sptr: 1359 case tok::kw___uptr: 1360 case tok::kw___ptr64: 1361 case tok::kw___ptr32: 1362 case tok::kw___forceinline: 1363 case tok::kw___unaligned: 1364 case tok::kw__Nonnull: 1365 case tok::kw__Nullable: 1366 case tok::kw__Null_unspecified: 1367 case tok::kw___kindof: 1368 return TPResult::True; 1369 1370 // Borland 1371 case tok::kw___pascal: 1372 return TPResult::True; 1373 1374 // AltiVec 1375 case tok::kw___vector: 1376 return TPResult::True; 1377 1378 case tok::annot_template_id: { 1379 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1380 if (TemplateId->Kind != TNK_Type_template) 1381 return TPResult::False; 1382 CXXScopeSpec SS; 1383 AnnotateTemplateIdTokenAsType(); 1384 assert(Tok.is(tok::annot_typename)); 1385 goto case_typename; 1386 } 1387 1388 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed 1389 // We've already annotated a scope; try to annotate a type. 1390 if (TryAnnotateTypeOrScopeToken()) 1391 return TPResult::Error; 1392 if (!Tok.is(tok::annot_typename)) { 1393 // If the next token is an identifier or a type qualifier, then this 1394 // can't possibly be a valid expression either. 1395 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) { 1396 CXXScopeSpec SS; 1397 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), 1398 Tok.getAnnotationRange(), 1399 SS); 1400 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) { 1401 RevertingTentativeParsingAction PA(*this); 1402 ConsumeToken(); 1403 ConsumeToken(); 1404 bool isIdentifier = Tok.is(tok::identifier); 1405 TPResult TPR = TPResult::False; 1406 if (!isIdentifier) 1407 TPR = isCXXDeclarationSpecifier(BracedCastResult, 1408 HasMissingTypename); 1409 1410 if (isIdentifier || 1411 TPR == TPResult::True || TPR == TPResult::Error) 1412 return TPResult::Error; 1413 1414 if (HasMissingTypename) { 1415 // We can't tell whether this is a missing 'typename' or a valid 1416 // expression. 1417 *HasMissingTypename = true; 1418 return TPResult::Ambiguous; 1419 } 1420 1421 // FIXME: Fails to either revert or commit the tentative parse! 1422 } else { 1423 // Try to resolve the name. If it doesn't exist, assume it was 1424 // intended to name a type and keep disambiguating. 1425 switch (TryAnnotateName(false /* SS is not dependent */)) { 1426 case ANK_Error: 1427 return TPResult::Error; 1428 case ANK_TentativeDecl: 1429 return TPResult::False; 1430 case ANK_TemplateName: 1431 // A bare type template-name which can't be a template template 1432 // argument is an error, and was probably intended to be a type. 1433 return GreaterThanIsOperator ? TPResult::True : TPResult::False; 1434 case ANK_Unresolved: 1435 return HasMissingTypename ? TPResult::Ambiguous 1436 : TPResult::False; 1437 case ANK_Success: 1438 // Annotated it, check again. 1439 assert(Tok.isNot(tok::annot_cxxscope) || 1440 NextToken().isNot(tok::identifier)); 1441 return isCXXDeclarationSpecifier(BracedCastResult, 1442 HasMissingTypename); 1443 } 1444 } 1445 } 1446 return TPResult::False; 1447 } 1448 // If that succeeded, fallthrough into the generic simple-type-id case. 1449 1450 // The ambiguity resides in a simple-type-specifier/typename-specifier 1451 // followed by a '('. The '(' could either be the start of: 1452 // 1453 // direct-declarator: 1454 // '(' declarator ')' 1455 // 1456 // direct-abstract-declarator: 1457 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1458 // exception-specification[opt] 1459 // '(' abstract-declarator ')' 1460 // 1461 // or part of a function-style cast expression: 1462 // 1463 // simple-type-specifier '(' expression-list[opt] ')' 1464 // 1465 1466 // simple-type-specifier: 1467 1468 case tok::annot_typename: 1469 case_typename: 1470 // In Objective-C, we might have a protocol-qualified type. 1471 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) { 1472 // Tentatively parse the protocol qualifiers. 1473 RevertingTentativeParsingAction PA(*this); 1474 ConsumeToken(); // The type token 1475 1476 TPResult TPR = TryParseProtocolQualifiers(); 1477 bool isFollowedByParen = Tok.is(tok::l_paren); 1478 bool isFollowedByBrace = Tok.is(tok::l_brace); 1479 1480 if (TPR == TPResult::Error) 1481 return TPResult::Error; 1482 1483 if (isFollowedByParen) 1484 return TPResult::Ambiguous; 1485 1486 if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 1487 return BracedCastResult; 1488 1489 return TPResult::True; 1490 } 1491 1492 case tok::kw_char: 1493 case tok::kw_wchar_t: 1494 case tok::kw_char16_t: 1495 case tok::kw_char32_t: 1496 case tok::kw_bool: 1497 case tok::kw_short: 1498 case tok::kw_int: 1499 case tok::kw_long: 1500 case tok::kw___int64: 1501 case tok::kw___int128: 1502 case tok::kw_signed: 1503 case tok::kw_unsigned: 1504 case tok::kw_half: 1505 case tok::kw_float: 1506 case tok::kw_double: 1507 case tok::kw___float128: 1508 case tok::kw_void: 1509 case tok::annot_decltype: 1510 if (NextToken().is(tok::l_paren)) 1511 return TPResult::Ambiguous; 1512 1513 // This is a function-style cast in all cases we disambiguate other than 1514 // one: 1515 // struct S { 1516 // enum E : int { a = 4 }; // enum 1517 // enum E : int { 4 }; // bit-field 1518 // }; 1519 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace)) 1520 return BracedCastResult; 1521 1522 if (isStartOfObjCClassMessageMissingOpenBracket()) 1523 return TPResult::False; 1524 1525 return TPResult::True; 1526 1527 // GNU typeof support. 1528 case tok::kw_typeof: { 1529 if (NextToken().isNot(tok::l_paren)) 1530 return TPResult::True; 1531 1532 RevertingTentativeParsingAction PA(*this); 1533 1534 TPResult TPR = TryParseTypeofSpecifier(); 1535 bool isFollowedByParen = Tok.is(tok::l_paren); 1536 bool isFollowedByBrace = Tok.is(tok::l_brace); 1537 1538 if (TPR == TPResult::Error) 1539 return TPResult::Error; 1540 1541 if (isFollowedByParen) 1542 return TPResult::Ambiguous; 1543 1544 if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 1545 return BracedCastResult; 1546 1547 return TPResult::True; 1548 } 1549 1550 // C++0x type traits support 1551 case tok::kw___underlying_type: 1552 return TPResult::True; 1553 1554 // C11 _Atomic 1555 case tok::kw__Atomic: 1556 return TPResult::True; 1557 1558 default: 1559 return TPResult::False; 1560 } 1561 } 1562 1563 bool Parser::isCXXDeclarationSpecifierAType() { 1564 switch (Tok.getKind()) { 1565 // typename-specifier 1566 case tok::annot_decltype: 1567 case tok::annot_template_id: 1568 case tok::annot_typename: 1569 case tok::kw_typeof: 1570 case tok::kw___underlying_type: 1571 return true; 1572 1573 // elaborated-type-specifier 1574 case tok::kw_class: 1575 case tok::kw_struct: 1576 case tok::kw_union: 1577 case tok::kw___interface: 1578 case tok::kw_enum: 1579 return true; 1580 1581 // simple-type-specifier 1582 case tok::kw_char: 1583 case tok::kw_wchar_t: 1584 case tok::kw_char16_t: 1585 case tok::kw_char32_t: 1586 case tok::kw_bool: 1587 case tok::kw_short: 1588 case tok::kw_int: 1589 case tok::kw_long: 1590 case tok::kw___int64: 1591 case tok::kw___int128: 1592 case tok::kw_signed: 1593 case tok::kw_unsigned: 1594 case tok::kw_half: 1595 case tok::kw_float: 1596 case tok::kw_double: 1597 case tok::kw___float128: 1598 case tok::kw_void: 1599 case tok::kw___unknown_anytype: 1600 case tok::kw___auto_type: 1601 return true; 1602 1603 case tok::kw_auto: 1604 return getLangOpts().CPlusPlus11; 1605 1606 case tok::kw__Atomic: 1607 // "_Atomic foo" 1608 return NextToken().is(tok::l_paren); 1609 1610 default: 1611 return false; 1612 } 1613 } 1614 1615 /// [GNU] typeof-specifier: 1616 /// 'typeof' '(' expressions ')' 1617 /// 'typeof' '(' type-name ')' 1618 /// 1619 Parser::TPResult Parser::TryParseTypeofSpecifier() { 1620 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); 1621 ConsumeToken(); 1622 1623 assert(Tok.is(tok::l_paren) && "Expected '('"); 1624 // Parse through the parens after 'typeof'. 1625 ConsumeParen(); 1626 if (!SkipUntil(tok::r_paren, StopAtSemi)) 1627 return TPResult::Error; 1628 1629 return TPResult::Ambiguous; 1630 } 1631 1632 /// [ObjC] protocol-qualifiers: 1633 //// '<' identifier-list '>' 1634 Parser::TPResult Parser::TryParseProtocolQualifiers() { 1635 assert(Tok.is(tok::less) && "Expected '<' for qualifier list"); 1636 ConsumeToken(); 1637 do { 1638 if (Tok.isNot(tok::identifier)) 1639 return TPResult::Error; 1640 ConsumeToken(); 1641 1642 if (Tok.is(tok::comma)) { 1643 ConsumeToken(); 1644 continue; 1645 } 1646 1647 if (Tok.is(tok::greater)) { 1648 ConsumeToken(); 1649 return TPResult::Ambiguous; 1650 } 1651 } while (false); 1652 1653 return TPResult::Error; 1654 } 1655 1656 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or 1657 /// a constructor-style initializer, when parsing declaration statements. 1658 /// Returns true for function declarator and false for constructor-style 1659 /// initializer. 1660 /// If during the disambiguation process a parsing error is encountered, 1661 /// the function returns true to let the declaration parsing code handle it. 1662 /// 1663 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1664 /// exception-specification[opt] 1665 /// 1666 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) { 1667 1668 // C++ 8.2p1: 1669 // The ambiguity arising from the similarity between a function-style cast and 1670 // a declaration mentioned in 6.8 can also occur in the context of a 1671 // declaration. In that context, the choice is between a function declaration 1672 // with a redundant set of parentheses around a parameter name and an object 1673 // declaration with a function-style cast as the initializer. Just as for the 1674 // ambiguities mentioned in 6.8, the resolution is to consider any construct 1675 // that could possibly be a declaration a declaration. 1676 1677 RevertingTentativeParsingAction PA(*this); 1678 1679 ConsumeParen(); 1680 bool InvalidAsDeclaration = false; 1681 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration); 1682 if (TPR == TPResult::Ambiguous) { 1683 if (Tok.isNot(tok::r_paren)) 1684 TPR = TPResult::False; 1685 else { 1686 const Token &Next = NextToken(); 1687 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile, 1688 tok::kw_throw, tok::kw_noexcept, tok::l_square, 1689 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) || 1690 isCXX11VirtSpecifier(Next)) 1691 // The next token cannot appear after a constructor-style initializer, 1692 // and can appear next in a function definition. This must be a function 1693 // declarator. 1694 TPR = TPResult::True; 1695 else if (InvalidAsDeclaration) 1696 // Use the absence of 'typename' as a tie-breaker. 1697 TPR = TPResult::False; 1698 } 1699 } 1700 1701 if (IsAmbiguous && TPR == TPResult::Ambiguous) 1702 *IsAmbiguous = true; 1703 1704 // In case of an error, let the declaration parsing code handle it. 1705 return TPR != TPResult::False; 1706 } 1707 1708 /// parameter-declaration-clause: 1709 /// parameter-declaration-list[opt] '...'[opt] 1710 /// parameter-declaration-list ',' '...' 1711 /// 1712 /// parameter-declaration-list: 1713 /// parameter-declaration 1714 /// parameter-declaration-list ',' parameter-declaration 1715 /// 1716 /// parameter-declaration: 1717 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 1718 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 1719 /// '=' assignment-expression 1720 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 1721 /// attributes[opt] 1722 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 1723 /// attributes[opt] '=' assignment-expression 1724 /// 1725 Parser::TPResult 1726 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration, 1727 bool VersusTemplateArgument) { 1728 1729 if (Tok.is(tok::r_paren)) 1730 return TPResult::Ambiguous; 1731 1732 // parameter-declaration-list[opt] '...'[opt] 1733 // parameter-declaration-list ',' '...' 1734 // 1735 // parameter-declaration-list: 1736 // parameter-declaration 1737 // parameter-declaration-list ',' parameter-declaration 1738 // 1739 while (1) { 1740 // '...'[opt] 1741 if (Tok.is(tok::ellipsis)) { 1742 ConsumeToken(); 1743 if (Tok.is(tok::r_paren)) 1744 return TPResult::True; // '...)' is a sign of a function declarator. 1745 else 1746 return TPResult::False; 1747 } 1748 1749 // An attribute-specifier-seq here is a sign of a function declarator. 1750 if (isCXX11AttributeSpecifier(/*Disambiguate*/false, 1751 /*OuterMightBeMessageSend*/true)) 1752 return TPResult::True; 1753 1754 ParsedAttributes attrs(AttrFactory); 1755 MaybeParseMicrosoftAttributes(attrs); 1756 1757 // decl-specifier-seq 1758 // A parameter-declaration's initializer must be preceded by an '=', so 1759 // decl-specifier-seq '{' is not a parameter in C++11. 1760 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False, 1761 InvalidAsDeclaration); 1762 1763 if (VersusTemplateArgument && TPR == TPResult::True) { 1764 // Consume the decl-specifier-seq. We have to look past it, since a 1765 // type-id might appear here in a template argument. 1766 bool SeenType = false; 1767 do { 1768 SeenType |= isCXXDeclarationSpecifierAType(); 1769 if (TryConsumeDeclarationSpecifier() == TPResult::Error) 1770 return TPResult::Error; 1771 1772 // If we see a parameter name, this can't be a template argument. 1773 if (SeenType && Tok.is(tok::identifier)) 1774 return TPResult::True; 1775 1776 TPR = isCXXDeclarationSpecifier(TPResult::False, 1777 InvalidAsDeclaration); 1778 if (TPR == TPResult::Error) 1779 return TPR; 1780 } while (TPR != TPResult::False); 1781 } else if (TPR == TPResult::Ambiguous) { 1782 // Disambiguate what follows the decl-specifier. 1783 if (TryConsumeDeclarationSpecifier() == TPResult::Error) 1784 return TPResult::Error; 1785 } else 1786 return TPR; 1787 1788 // declarator 1789 // abstract-declarator[opt] 1790 TPR = TryParseDeclarator(true/*mayBeAbstract*/); 1791 if (TPR != TPResult::Ambiguous) 1792 return TPR; 1793 1794 // [GNU] attributes[opt] 1795 if (Tok.is(tok::kw___attribute)) 1796 return TPResult::True; 1797 1798 // If we're disambiguating a template argument in a default argument in 1799 // a class definition versus a parameter declaration, an '=' here 1800 // disambiguates the parse one way or the other. 1801 // If this is a parameter, it must have a default argument because 1802 // (a) the previous parameter did, and 1803 // (b) this must be the first declaration of the function, so we can't 1804 // inherit any default arguments from elsewhere. 1805 // If we see an ')', then we've reached the end of a 1806 // parameter-declaration-clause, and the last param is missing its default 1807 // argument. 1808 if (VersusTemplateArgument) 1809 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True 1810 : TPResult::False; 1811 1812 if (Tok.is(tok::equal)) { 1813 // '=' assignment-expression 1814 // Parse through assignment-expression. 1815 // FIXME: assignment-expression may contain an unparenthesized comma. 1816 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch)) 1817 return TPResult::Error; 1818 } 1819 1820 if (Tok.is(tok::ellipsis)) { 1821 ConsumeToken(); 1822 if (Tok.is(tok::r_paren)) 1823 return TPResult::True; // '...)' is a sign of a function declarator. 1824 else 1825 return TPResult::False; 1826 } 1827 1828 if (!TryConsumeToken(tok::comma)) 1829 break; 1830 } 1831 1832 return TPResult::Ambiguous; 1833 } 1834 1835 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue 1836 /// parsing as a function declarator. 1837 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will 1838 /// return TPResult::Ambiguous, otherwise it will return either False() or 1839 /// Error(). 1840 /// 1841 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 1842 /// exception-specification[opt] 1843 /// 1844 /// exception-specification: 1845 /// 'throw' '(' type-id-list[opt] ')' 1846 /// 1847 Parser::TPResult Parser::TryParseFunctionDeclarator() { 1848 1849 // The '(' is already parsed. 1850 1851 TPResult TPR = TryParseParameterDeclarationClause(); 1852 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren)) 1853 TPR = TPResult::False; 1854 1855 if (TPR == TPResult::False || TPR == TPResult::Error) 1856 return TPR; 1857 1858 // Parse through the parens. 1859 if (!SkipUntil(tok::r_paren, StopAtSemi)) 1860 return TPResult::Error; 1861 1862 // cv-qualifier-seq 1863 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict)) 1864 ConsumeToken(); 1865 1866 // ref-qualifier[opt] 1867 if (Tok.isOneOf(tok::amp, tok::ampamp)) 1868 ConsumeToken(); 1869 1870 // exception-specification 1871 if (Tok.is(tok::kw_throw)) { 1872 ConsumeToken(); 1873 if (Tok.isNot(tok::l_paren)) 1874 return TPResult::Error; 1875 1876 // Parse through the parens after 'throw'. 1877 ConsumeParen(); 1878 if (!SkipUntil(tok::r_paren, StopAtSemi)) 1879 return TPResult::Error; 1880 } 1881 if (Tok.is(tok::kw_noexcept)) { 1882 ConsumeToken(); 1883 // Possibly an expression as well. 1884 if (Tok.is(tok::l_paren)) { 1885 // Find the matching rparen. 1886 ConsumeParen(); 1887 if (!SkipUntil(tok::r_paren, StopAtSemi)) 1888 return TPResult::Error; 1889 } 1890 } 1891 1892 return TPResult::Ambiguous; 1893 } 1894 1895 /// '[' constant-expression[opt] ']' 1896 /// 1897 Parser::TPResult Parser::TryParseBracketDeclarator() { 1898 ConsumeBracket(); 1899 if (!SkipUntil(tok::r_square, StopAtSemi)) 1900 return TPResult::Error; 1901 1902 return TPResult::Ambiguous; 1903 } 1904