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