1 //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the tentative parsing portions of the Parser
10 // interfaces, for ambiguity resolution.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/ParseDiagnostic.h"
16 #include "clang/Sema/ParsedTemplate.h"
17 using namespace clang;
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.
23 /// declaration-statement:
26 /// block-declaration:
27 /// simple-declaration
29 /// namespace-alias-definition
32 /// [C++0x] static_assert-declaration
35 /// 'asm' '(' string-literal ')' ';'
37 /// namespace-alias-definition:
38 /// 'namespace' identifier = qualified-namespace-specifier ';'
40 /// using-declaration:
41 /// 'using' typename[opt] '::'[opt] nested-name-specifier
42 /// unqualified-id ';'
43 /// 'using' '::' unqualified-id ;
46 /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
47 /// namespace-name ';'
49 bool Parser::isCXXDeclarationStatement() {
50 switch (Tok.getKind()) {
53 // namespace-alias-definition
54 case tok::kw_namespace:
58 // static_assert-declaration
59 case tok::kw_static_assert:
60 case tok::kw__Static_assert:
64 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
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.
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]
79 /// (if AllowForRangeDecl specified)
80 /// for ( for-range-declaration : for-range-initializer ) statement
82 /// for-range-declaration:
83 /// decl-specifier-seq declarator
84 /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
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) {
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].
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.
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
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
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)
128 // FIXME: Add statistics about the number of ambiguous statements encountered
129 // and how they were resolved (number of declarations+number of expressions).
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
136 RevertingTentativeParsingAction PA(*this);
137 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
140 // In case of an error, let the declaration parsing code handle it.
141 if (TPR == TPResult::Error)
144 // Declarations take precedence over expressions.
145 if (TPR == TPResult::Ambiguous)
146 TPR = TPResult::True;
148 assert(TPR == TPResult::True || TPR == TPResult::False);
149 return TPR == TPResult::True;
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)) {
163 case tok::kw___attribute:
164 case tok::kw___underlying_type: {
166 if (Tok.isNot(tok::l_paren))
167 return TPResult::Error;
169 if (!SkipUntil(tok::r_paren))
170 return TPResult::Error;
177 case tok::kw___interface:
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
185 // FIXME: We don't support class-specifiers nor enum-specifiers here.
189 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
191 if (Tok.is(tok::l_square)) {
193 if (!SkipUntil(tok::r_square))
194 return TPResult::Error;
197 if (Tok.isNot(tok::l_paren))
198 return TPResult::Error;
200 if (!SkipUntil(tok::r_paren))
201 return TPResult::Error;
205 if (TryAnnotateOptionalCXXScopeToken())
206 return TPResult::Error;
207 if (Tok.is(tok::annot_cxxscope))
208 ConsumeAnnotationToken();
209 if (Tok.is(tok::identifier))
211 else if (Tok.is(tok::annot_template_id))
212 ConsumeAnnotationToken();
214 return TPResult::Error;
217 case tok::annot_cxxscope:
218 ConsumeAnnotationToken();
223 if (getLangOpts().ObjC && Tok.is(tok::less))
224 return TryParseProtocolQualifiers();
228 return TPResult::Ambiguous;
231 /// simple-declaration:
232 /// decl-specifier-seq init-declarator-list[opt] ';'
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
239 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
240 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
241 return TPResult::Error;
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)
252 assert(TPR == TPResult::False);
255 TPResult TPR = TryParseInitDeclaratorList();
256 if (TPR != TPResult::Ambiguous)
259 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
260 return TPResult::False;
262 return TPResult::Ambiguous;
265 /// Tentatively parse an init-declarator-list in order to disambiguate it from
268 /// init-declarator-list:
270 /// init-declarator-list ',' init-declarator
273 /// declarator initializer[opt]
274 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
277 /// brace-or-equal-initializer
278 /// '(' expression-list ')'
280 /// brace-or-equal-initializer:
281 /// '=' initializer-clause
282 /// [C++11] braced-init-list
284 /// initializer-clause:
285 /// assignment-expression
288 /// braced-init-list:
289 /// '{' initializer-list ','[opt] '}'
292 Parser::TPResult Parser::TryParseInitDeclaratorList() {
295 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
296 if (TPR != TPResult::Ambiguous)
299 // [GNU] simple-asm-expr[opt] attributes[opt]
300 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
301 return TPResult::True;
304 if (Tok.is(tok::l_paren)) {
305 // Parse through the parens.
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
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.
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;
333 if (!TryConsumeToken(tok::comma))
337 return TPResult::Ambiguous;
340 struct Parser::ConditionDeclarationOrInitStatementState {
342 bool CanBeExpression = true;
343 bool CanBeCondition = true;
344 bool CanBeInitStatement;
345 bool CanBeForRangeDecl;
347 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
348 bool CanBeForRangeDecl)
349 : P(P), CanBeInitStatement(CanBeInitStatement),
350 CanBeForRangeDecl(CanBeForRangeDecl) {}
353 return CanBeExpression + CanBeCondition + CanBeInitStatement +
354 CanBeForRangeDecl < 2;
357 void markNotExpression() {
358 CanBeExpression = false;
361 // FIXME: Unify the parsing codepaths for condition variables and
362 // simple-declarations so that we don't need to eagerly figure out which
363 // kind we have here. (Just parse init-declarators until we reach a
364 // semicolon or right paren.)
365 RevertingTentativeParsingAction PA(P);
366 if (CanBeForRangeDecl) {
367 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
368 // The final case is a for range declaration, the rest are not.
370 unsigned QuestionColonDepth = 0;
371 P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
373 if (P.Tok.is(tok::question))
374 ++QuestionColonDepth;
375 else if (P.Tok.is(tok::colon)) {
376 if (QuestionColonDepth)
377 --QuestionColonDepth;
379 CanBeCondition = CanBeInitStatement = false;
383 CanBeForRangeDecl = false;
389 // Just skip until we hit a ')' or ';'.
390 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
392 if (P.Tok.isNot(tok::r_paren))
393 CanBeCondition = CanBeForRangeDecl = false;
394 if (P.Tok.isNot(tok::semi))
395 CanBeInitStatement = false;
399 bool markNotCondition() {
400 CanBeCondition = false;
404 bool markNotForRangeDecl() {
405 CanBeForRangeDecl = false;
409 bool update(TPResult IsDecl) {
413 assert(resolved() && "can't continue after tentative parsing bails out");
415 case TPResult::False:
416 CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
418 case TPResult::Ambiguous:
420 case TPResult::Error:
421 CanBeExpression = CanBeCondition = CanBeInitStatement =
422 CanBeForRangeDecl = false;
428 ConditionOrInitStatement result() const {
429 assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
430 CanBeForRangeDecl < 2 &&
431 "result called but not yet resolved");
433 return ConditionOrInitStatement::Expression;
435 return ConditionOrInitStatement::ConditionDecl;
436 if (CanBeInitStatement)
437 return ConditionOrInitStatement::InitStmtDecl;
438 if (CanBeForRangeDecl)
439 return ConditionOrInitStatement::ForRangeDecl;
440 return ConditionOrInitStatement::Error;
444 /// Disambiguates between a declaration in a condition, a
445 /// simple-declaration in an init-statement, and an expression for
446 /// a condition of a if/switch statement.
450 /// type-specifier-seq declarator '=' assignment-expression
451 /// [C++11] type-specifier-seq declarator '=' initializer-clause
452 /// [C++11] type-specifier-seq declarator braced-init-list
453 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
454 /// '=' assignment-expression
455 /// simple-declaration:
456 /// decl-specifier-seq init-declarator-list[opt] ';'
458 /// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
459 /// to the ';' to disambiguate cases like 'int(x))' (an expression) from
460 /// 'int(x);' (a simple-declaration in an init-statement).
461 Parser::ConditionOrInitStatement
462 Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
463 bool CanBeForRangeDecl) {
464 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
467 if (State.update(isCXXDeclarationSpecifier()))
468 return State.result();
470 // It might be a declaration; we need tentative parsing.
471 RevertingTentativeParsingAction PA(*this);
473 // FIXME: A tag definition unambiguously tells us this is an init-statement.
474 if (State.update(TryConsumeDeclarationSpecifier()))
475 return State.result();
476 assert(Tok.is(tok::l_paren) && "Expected '('");
479 // Consume a declarator.
480 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
481 return State.result();
483 // Attributes, asm label, or an initializer imply this is not an expression.
484 // FIXME: Disambiguate properly after an = instead of assuming that it's a
485 // valid declaration.
486 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
487 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
488 State.markNotExpression();
489 return State.result();
492 // A colon here identifies a for-range declaration.
493 if (State.CanBeForRangeDecl && Tok.is(tok::colon))
494 return ConditionOrInitStatement::ForRangeDecl;
496 // At this point, it can't be a condition any more, because a condition
497 // must have a brace-or-equal-initializer.
498 if (State.markNotCondition())
499 return State.result();
501 // Likewise, it can't be a for-range declaration any more.
502 if (State.markNotForRangeDecl())
503 return State.result();
505 // A parenthesized initializer could be part of an expression or a
506 // simple-declaration.
507 if (Tok.is(tok::l_paren)) {
509 SkipUntil(tok::r_paren, StopAtSemi);
512 if (!TryConsumeToken(tok::comma))
516 // We reached the end. If it can now be some kind of decl, then it is.
517 if (State.CanBeCondition && Tok.is(tok::r_paren))
518 return ConditionOrInitStatement::ConditionDecl;
519 else if (State.CanBeInitStatement && Tok.is(tok::semi))
520 return ConditionOrInitStatement::InitStmtDecl;
522 return ConditionOrInitStatement::Expression;
525 /// Determine whether the next set of tokens contains a type-id.
527 /// The context parameter states what context we're parsing right
528 /// now, which affects how this routine copes with the token
529 /// following the type-id. If the context is TypeIdInParens, we have
530 /// already parsed the '(' and we will cease lookahead when we hit
531 /// the corresponding ')'. If the context is
532 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
533 /// before this template argument, and will cease lookahead when we
534 /// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
535 /// preceding such. Returns true for a type-id and false for an expression.
536 /// If during the disambiguation process a parsing error is encountered,
537 /// the function returns true to let the declaration parsing code handle it.
540 /// type-specifier-seq abstract-declarator[opt]
542 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
547 // The ambiguity arising from the similarity between a function-style cast and
548 // a type-id can occur in different contexts. The ambiguity appears as a
549 // choice between a function-style cast expression and a declaration of a
550 // type. The resolution is that any construct that could possibly be a type-id
551 // in its syntactic context shall be considered a type-id.
553 TPResult TPR = isCXXDeclarationSpecifier();
554 if (TPR != TPResult::Ambiguous)
555 return TPR != TPResult::False; // Returns true for TPResult::True or
558 // FIXME: Add statistics about the number of ambiguous statements encountered
559 // and how they were resolved (number of declarations+number of expressions).
561 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
562 // We need tentative parsing...
564 RevertingTentativeParsingAction PA(*this);
566 // type-specifier-seq
567 TryConsumeDeclarationSpecifier();
568 assert(Tok.is(tok::l_paren) && "Expected '('");
571 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
573 // In case of an error, let the declaration parsing code handle it.
574 if (TPR == TPResult::Error)
575 TPR = TPResult::True;
577 if (TPR == TPResult::Ambiguous) {
578 // We are supposed to be inside parens, so if after the abstract declarator
579 // we encounter a ')' this is a type-id, otherwise it's an expression.
580 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
581 TPR = TPResult::True;
584 // We are supposed to be inside a template argument, so if after
585 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
586 // ','; or, in C++0x, an ellipsis immediately preceding such, this
587 // is a type-id. Otherwise, it's an expression.
588 } else if (Context == TypeIdAsTemplateArgument &&
589 (Tok.isOneOf(tok::greater, tok::comma) ||
590 (getLangOpts().CPlusPlus11 &&
591 (Tok.isOneOf(tok::greatergreater,
592 tok::greatergreatergreater) ||
593 (Tok.is(tok::ellipsis) &&
594 NextToken().isOneOf(tok::greater, tok::greatergreater,
595 tok::greatergreatergreater,
597 TPR = TPResult::True;
601 TPR = TPResult::False;
604 assert(TPR == TPResult::True || TPR == TPResult::False);
605 return TPR == TPResult::True;
608 /// Returns true if this is a C++11 attribute-specifier. Per
609 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
610 /// always introduce an attribute. In Objective-C++11, this rule does not
611 /// apply if either '[' begins a message-send.
613 /// If Disambiguate is true, we try harder to determine whether a '[[' starts
614 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
616 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
617 /// Obj-C message send or the start of an attribute. Otherwise, we assume it
618 /// is not an Obj-C message send.
620 /// C++11 [dcl.attr.grammar]:
622 /// attribute-specifier:
623 /// '[' '[' attribute-list ']' ']'
624 /// alignment-specifier
628 /// attribute-list ',' attribute[opt]
630 /// attribute-list ',' attribute '...'
633 /// attribute-token attribute-argument-clause[opt]
637 /// identifier '::' identifier
639 /// attribute-argument-clause:
640 /// '(' balanced-token-seq ')'
641 Parser::CXX11AttributeKind
642 Parser::isCXX11AttributeSpecifier(bool Disambiguate,
643 bool OuterMightBeMessageSend) {
644 if (Tok.is(tok::kw_alignas))
645 return CAK_AttributeSpecifier;
647 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
648 return CAK_NotAttributeSpecifier;
650 // No tentative parsing if we don't need to look for ']]' or a lambda.
651 if (!Disambiguate && !getLangOpts().ObjC)
652 return CAK_AttributeSpecifier;
654 // '[[using ns: ...]]' is an attribute.
655 if (GetLookAheadToken(2).is(tok::kw_using))
656 return CAK_AttributeSpecifier;
658 RevertingTentativeParsingAction PA(*this);
660 // Opening brackets were checked for above.
663 if (!getLangOpts().ObjC) {
666 bool IsAttribute = SkipUntil(tok::r_square);
667 IsAttribute &= Tok.is(tok::r_square);
669 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
672 // In Obj-C++11, we need to distinguish four situations:
673 // 1a) int x[[attr]]; C++11 attribute.
674 // 1b) [[attr]]; C++11 statement attribute.
675 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
676 // 3a) int x[[obj get]]; Message send in array size/index.
677 // 3b) [[Class alloc] init]; Message send in message send.
678 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
679 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
681 // Check to see if this is a lambda-expression.
682 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
683 // into the tentative attribute parse below.
685 RevertingTentativeParsingAction LambdaTPA(*this);
686 LambdaIntroducer Intro;
687 LambdaIntroducerTentativeParse Tentative;
688 if (ParseLambdaIntroducer(Intro, &Tentative)) {
689 // We hit a hard error after deciding this was not an attribute.
690 // FIXME: Don't parse and annotate expressions when disambiguating
691 // against an attribute.
692 return CAK_NotAttributeSpecifier;
696 case LambdaIntroducerTentativeParse::MessageSend:
697 // Case 3: The inner construct is definitely a message send, so the
698 // outer construct is definitely not an attribute.
699 return CAK_NotAttributeSpecifier;
701 case LambdaIntroducerTentativeParse::Success:
702 case LambdaIntroducerTentativeParse::Incomplete:
703 // This is a lambda-introducer or attribute-specifier.
704 if (Tok.is(tok::r_square))
705 // Case 1: C++11 attribute.
706 return CAK_AttributeSpecifier;
708 if (OuterMightBeMessageSend)
709 // Case 4: Lambda in message send.
710 return CAK_NotAttributeSpecifier;
712 // Case 2: Lambda in array size / index.
713 return CAK_InvalidAttributeSpecifier;
715 case LambdaIntroducerTentativeParse::Invalid:
716 // No idea what this is; we couldn't parse it as a lambda-introducer.
717 // Might still be an attribute-specifier or a message send.
724 // If we don't have a lambda-introducer, then we have an attribute or a
726 bool IsAttribute = true;
727 while (Tok.isNot(tok::r_square)) {
728 if (Tok.is(tok::comma)) {
729 // Case 1: Stray commas can only occur in attributes.
730 return CAK_AttributeSpecifier;
733 // Parse the attribute-token, if present.
734 // C++11 [dcl.attr.grammar]:
735 // If a keyword or an alternative token that satisfies the syntactic
736 // requirements of an identifier is contained in an attribute-token,
737 // it is considered an identifier.
739 if (!TryParseCXX11AttributeIdentifier(Loc)) {
743 if (Tok.is(tok::coloncolon)) {
745 if (!TryParseCXX11AttributeIdentifier(Loc)) {
751 // Parse the attribute-argument-clause, if present.
752 if (Tok.is(tok::l_paren)) {
754 if (!SkipUntil(tok::r_paren)) {
760 TryConsumeToken(tok::ellipsis);
762 if (!TryConsumeToken(tok::comma))
766 // An attribute must end ']]'.
768 if (Tok.is(tok::r_square)) {
770 IsAttribute = Tok.is(tok::r_square);
777 // Case 1: C++11 statement attribute.
778 return CAK_AttributeSpecifier;
780 // Case 3: Message send.
781 return CAK_NotAttributeSpecifier;
784 Parser::TPResult Parser::TryParsePtrOperatorSeq() {
786 if (TryAnnotateOptionalCXXScopeToken(true))
787 return TPResult::Error;
789 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
790 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
793 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
794 tok::kw__Nonnull, tok::kw__Nullable,
795 tok::kw__Null_unspecified))
798 return TPResult::True;
803 /// operator-function-id:
804 /// 'operator' operator
807 /// new delete new[] delete[] + - * / % ^ [...]
809 /// conversion-function-id:
810 /// 'operator' conversion-type-id
812 /// conversion-type-id:
813 /// type-specifier-seq conversion-declarator[opt]
815 /// conversion-declarator:
816 /// ptr-operator conversion-declarator[opt]
818 /// literal-operator-id:
819 /// 'operator' string-literal identifier
820 /// 'operator' user-defined-string-literal
821 Parser::TPResult Parser::TryParseOperatorId() {
822 assert(Tok.is(tok::kw_operator));
825 // Maybe this is an operator-function-id.
826 switch (Tok.getKind()) {
827 case tok::kw_new: case tok::kw_delete:
829 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
833 return TPResult::True;
835 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
837 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
838 #include "clang/Basic/OperatorKinds.def"
840 return TPResult::True;
843 if (NextToken().is(tok::r_square)) {
846 return TPResult::True;
851 if (NextToken().is(tok::r_paren)) {
854 return TPResult::True;
862 // Maybe this is a literal-operator-id.
863 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
864 bool FoundUDSuffix = false;
866 FoundUDSuffix |= Tok.hasUDSuffix();
867 ConsumeStringToken();
868 } while (isTokenStringLiteral());
870 if (!FoundUDSuffix) {
871 if (Tok.is(tok::identifier))
874 return TPResult::Error;
876 return TPResult::True;
879 // Maybe this is a conversion-function-id.
880 bool AnyDeclSpecifiers = false;
882 TPResult TPR = isCXXDeclarationSpecifier();
883 if (TPR == TPResult::Error)
885 if (TPR == TPResult::False) {
886 if (!AnyDeclSpecifiers)
887 return TPResult::Error;
890 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
891 return TPResult::Error;
892 AnyDeclSpecifiers = true;
894 return TryParsePtrOperatorSeq();
898 /// direct-declarator
899 /// ptr-operator declarator
901 /// direct-declarator:
903 /// direct-declarator '(' parameter-declaration-clause ')'
904 /// cv-qualifier-seq[opt] exception-specification[opt]
905 /// direct-declarator '[' constant-expression[opt] ']'
906 /// '(' declarator ')'
907 /// [GNU] '(' attributes declarator ')'
909 /// abstract-declarator:
910 /// ptr-operator abstract-declarator[opt]
911 /// direct-abstract-declarator
913 /// direct-abstract-declarator:
914 /// direct-abstract-declarator[opt]
915 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
916 /// exception-specification[opt]
917 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
918 /// '(' abstract-declarator ')'
922 /// '*' cv-qualifier-seq[opt]
924 /// [C++0x] '&&' [TODO]
925 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
927 /// cv-qualifier-seq:
928 /// cv-qualifier cv-qualifier-seq[opt]
935 /// '...'[opt] id-expression
939 /// qualified-id [TODO]
943 /// operator-function-id
944 /// conversion-function-id
945 /// literal-operator-id
946 /// '~' class-name [TODO]
947 /// '~' decltype-specifier [TODO]
948 /// template-id [TODO]
950 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
951 bool mayHaveIdentifier,
952 bool mayHaveDirectInit) {
955 // ptr-operator declarator
956 if (TryParsePtrOperatorSeq() == TPResult::Error)
957 return TPResult::Error;
959 // direct-declarator:
960 // direct-abstract-declarator:
961 if (Tok.is(tok::ellipsis))
964 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
965 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
966 NextToken().is(tok::kw_operator)))) &&
969 if (Tok.is(tok::annot_cxxscope))
970 ConsumeAnnotationToken();
971 else if (Tok.is(tok::identifier))
972 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
973 if (Tok.is(tok::kw_operator)) {
974 if (TryParseOperatorId() == TPResult::Error)
975 return TPResult::Error;
978 } else if (Tok.is(tok::l_paren)) {
981 (Tok.is(tok::r_paren) || // 'int()' is a function.
982 // 'int(...)' is a function.
983 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
984 isDeclarationSpecifier())) { // 'int(int)' is a function.
985 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
986 // exception-specification[opt]
987 TPResult TPR = TryParseFunctionDeclarator();
988 if (TPR != TPResult::Ambiguous)
991 // '(' declarator ')'
992 // '(' attributes declarator ')'
993 // '(' abstract-declarator ')'
994 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
995 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
996 tok::kw___regcall, tok::kw___vectorcall))
997 return TPResult::True; // attributes indicate declaration
998 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
999 if (TPR != TPResult::Ambiguous)
1001 if (Tok.isNot(tok::r_paren))
1002 return TPResult::False;
1005 } else if (!mayBeAbstract) {
1006 return TPResult::False;
1009 if (mayHaveDirectInit)
1010 return TPResult::Ambiguous;
1013 TPResult TPR(TPResult::Ambiguous);
1015 if (Tok.is(tok::l_paren)) {
1016 // Check whether we have a function declarator or a possible ctor-style
1017 // initializer that follows the declarator. Note that ctor-style
1018 // initializers are not possible in contexts where abstract declarators
1020 if (!mayBeAbstract && !isCXXFunctionDeclarator())
1023 // direct-declarator '(' parameter-declaration-clause ')'
1024 // cv-qualifier-seq[opt] exception-specification[opt]
1026 TPR = TryParseFunctionDeclarator();
1027 } else if (Tok.is(tok::l_square)) {
1028 // direct-declarator '[' constant-expression[opt] ']'
1029 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
1030 TPR = TryParseBracketDeclarator();
1031 } else if (Tok.is(tok::kw_requires)) {
1032 // declarator requires-clause
1033 // A requires clause indicates a function declaration.
1034 TPR = TPResult::True;
1039 if (TPR != TPResult::Ambiguous)
1043 return TPResult::Ambiguous;
1047 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
1049 // Obviously starts an expression.
1050 case tok::numeric_constant:
1051 case tok::char_constant:
1052 case tok::wide_char_constant:
1053 case tok::utf8_char_constant:
1054 case tok::utf16_char_constant:
1055 case tok::utf32_char_constant:
1056 case tok::string_literal:
1057 case tok::wide_string_literal:
1058 case tok::utf8_string_literal:
1059 case tok::utf16_string_literal:
1060 case tok::utf32_string_literal:
1069 case tok::minusminus:
1072 case tok::kw_sizeof:
1073 case tok::kw___func__:
1074 case tok::kw_const_cast:
1075 case tok::kw_delete:
1076 case tok::kw_dynamic_cast:
1079 case tok::kw_operator:
1080 case tok::kw_reinterpret_cast:
1081 case tok::kw_static_cast:
1085 case tok::kw_typeid:
1086 case tok::kw_alignof:
1087 case tok::kw_noexcept:
1088 case tok::kw_nullptr:
1089 case tok::kw__Alignof:
1090 case tok::kw___null:
1091 case tok::kw___alignof:
1092 case tok::kw___builtin_choose_expr:
1093 case tok::kw___builtin_offsetof:
1094 case tok::kw___builtin_va_arg:
1095 case tok::kw___imag:
1096 case tok::kw___real:
1097 case tok::kw___FUNCTION__:
1098 case tok::kw___FUNCDNAME__:
1099 case tok::kw___FUNCSIG__:
1100 case tok::kw_L__FUNCTION__:
1101 case tok::kw_L__FUNCSIG__:
1102 case tok::kw___PRETTY_FUNCTION__:
1103 case tok::kw___uuidof:
1104 #define TYPE_TRAIT(N,Spelling,K) \
1105 case tok::kw_##Spelling:
1106 #include "clang/Basic/TokenKinds.def"
1107 return TPResult::True;
1109 // Obviously starts a type-specifier-seq:
1112 case tok::kw_double:
1113 case tok::kw__Float16:
1114 case tok::kw___float128:
1120 case tok::kw___int64:
1121 case tok::kw___int128:
1122 case tok::kw_restrict:
1124 case tok::kw_signed:
1125 case tok::kw_struct:
1127 case tok::kw_unsigned:
1129 case tok::kw_volatile:
1131 case tok::kw__Complex:
1133 case tok::kw_typename:
1134 case tok::kw_wchar_t:
1135 case tok::kw_char8_t:
1136 case tok::kw_char16_t:
1137 case tok::kw_char32_t:
1138 case tok::kw__Decimal32:
1139 case tok::kw__Decimal64:
1140 case tok::kw__Decimal128:
1141 case tok::kw___interface:
1142 case tok::kw___thread:
1143 case tok::kw_thread_local:
1144 case tok::kw__Thread_local:
1145 case tok::kw_typeof:
1146 case tok::kw___underlying_type:
1147 case tok::kw___cdecl:
1148 case tok::kw___stdcall:
1149 case tok::kw___fastcall:
1150 case tok::kw___thiscall:
1151 case tok::kw___regcall:
1152 case tok::kw___vectorcall:
1153 case tok::kw___unaligned:
1154 case tok::kw___vector:
1155 case tok::kw___pixel:
1156 case tok::kw___bool:
1157 case tok::kw__Atomic:
1158 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1159 #include "clang/Basic/OpenCLImageTypes.def"
1160 case tok::kw___unknown_anytype:
1161 return TPResult::False;
1167 return TPResult::Ambiguous;
1170 bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1171 return std::find(TentativelyDeclaredIdentifiers.begin(),
1172 TentativelyDeclaredIdentifiers.end(), II)
1173 != TentativelyDeclaredIdentifiers.end();
1177 class TentativeParseCCC final : public CorrectionCandidateCallback {
1179 TentativeParseCCC(const Token &Next) {
1180 WantRemainingKeywords = false;
1181 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1182 tok::l_brace, tok::identifier);
1185 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1186 // Reject any candidate that only resolves to instance members since they
1187 // aren't viable as standalone identifiers instead of member references.
1188 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1189 llvm::all_of(Candidate,
1190 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1193 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1196 std::unique_ptr<CorrectionCandidateCallback> clone() override {
1197 return std::make_unique<TentativeParseCCC>(*this);
1201 /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1202 /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1203 /// be either a decl-specifier or a function-style cast, and TPResult::Error
1204 /// if a parsing error was found and reported.
1206 /// If InvalidAsDeclSpec is not null, some cases that would be ill-formed as
1207 /// declaration specifiers but possibly valid as some other kind of construct
1208 /// return TPResult::Ambiguous instead of TPResult::False. When this happens,
1209 /// the intent is to keep trying to disambiguate, on the basis that we might
1210 /// find a better reason to treat this construct as a declaration later on.
1211 /// When this happens and the name could possibly be valid in some other
1212 /// syntactic context, *InvalidAsDeclSpec is set to 'true'. The current cases
1213 /// that trigger this are:
1215 /// * When parsing X::Y (with no 'typename') where X is dependent
1216 /// * When parsing X<Y> where X is undeclared
1219 /// storage-class-specifier
1221 /// function-specifier
1224 /// [C++11] 'constexpr'
1225 /// [C++20] 'consteval'
1226 /// [GNU] attributes declaration-specifiers[opt]
1228 /// storage-class-specifier:
1234 /// [GNU] '__thread'
1235 /// [C++11] 'thread_local'
1236 /// [C11] '_Thread_local'
1238 /// function-specifier:
1247 /// simple-type-specifier
1250 /// elaborated-type-specifier
1251 /// typename-specifier
1254 /// simple-type-specifier:
1255 /// '::'[opt] nested-name-specifier[opt] type-name
1256 /// '::'[opt] nested-name-specifier 'template'
1257 /// simple-template-id [TODO]
1269 /// [GNU] typeof-specifier
1270 /// [GNU] '_Complex'
1272 /// [GNU] '__auto_type'
1273 /// [C++11] 'decltype' ( expression )
1274 /// [C++1y] 'decltype' ( 'auto' )
1281 /// elaborated-type-specifier:
1282 /// class-key '::'[opt] nested-name-specifier[opt] identifier
1283 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1284 /// simple-template-id
1285 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1291 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1292 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1294 /// class-specifier:
1295 /// class-head '{' member-specification[opt] '}'
1298 /// class-key identifier[opt] base-clause[opt]
1299 /// class-key nested-name-specifier identifier base-clause[opt]
1300 /// class-key nested-name-specifier[opt] simple-template-id
1301 /// base-clause[opt]
1314 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1315 bool *InvalidAsDeclSpec) {
1316 auto IsPlaceholderSpecifier = [&] (TemplateIdAnnotation *TemplateId,
1318 // We have a placeholder-constraint (we check for 'auto' or 'decltype' to
1319 // distinguish 'C<int>;' from 'C<int> auto c = 1;')
1320 return TemplateId->Kind == TNK_Concept_template &&
1321 GetLookAheadToken(Lookahead + 1).isOneOf(tok::kw_auto, tok::kw_decltype,
1322 // If we have an identifier here, the user probably forgot the
1323 // 'auto' in the placeholder constraint, e.g. 'C<int> x = 2;'
1324 // This will be diagnosed nicely later, so disambiguate as a
1328 switch (Tok.getKind()) {
1329 case tok::identifier: {
1330 // Check for need to substitute AltiVec __vector keyword
1331 // for "vector" identifier.
1332 if (TryAltiVecVectorToken())
1333 return TPResult::True;
1335 const Token &Next = NextToken();
1336 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1337 if (!getLangOpts().ObjC && Next.is(tok::identifier))
1338 return TPResult::True;
1340 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1341 // Determine whether this is a valid expression. If not, we will hit
1342 // a parse error one way or another. In that case, tell the caller that
1343 // this is ambiguous. Typo-correct to type and expression keywords and
1344 // to types and identifiers, in order to try to recover from errors.
1345 TentativeParseCCC CCC(Next);
1346 switch (TryAnnotateName(&CCC)) {
1348 return TPResult::Error;
1349 case ANK_TentativeDecl:
1350 return TPResult::False;
1351 case ANK_TemplateName:
1352 // In C++17, this could be a type template for class template argument
1353 // deduction. Try to form a type annotation for it. If we're in a
1354 // template template argument, we'll undo this when checking the
1355 // validity of the argument.
1356 if (getLangOpts().CPlusPlus17) {
1357 if (TryAnnotateTypeOrScopeToken())
1358 return TPResult::Error;
1359 if (Tok.isNot(tok::identifier))
1363 // A bare type template-name which can't be a template template
1364 // argument is an error, and was probably intended to be a type.
1365 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1366 case ANK_Unresolved:
1367 return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
1371 assert(Tok.isNot(tok::identifier) &&
1372 "TryAnnotateName succeeded without producing an annotation");
1374 // This might possibly be a type with a dependent scope specifier and
1375 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1376 // since it will annotate as a primary expression, and we want to use the
1377 // "missing 'typename'" logic.
1378 if (TryAnnotateTypeOrScopeToken())
1379 return TPResult::Error;
1380 // If annotation failed, assume it's a non-type.
1381 // FIXME: If this happens due to an undeclared identifier, treat it as
1383 if (Tok.is(tok::identifier))
1384 return TPResult::False;
1387 // We annotated this token as something. Recurse to handle whatever we got.
1388 return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1391 case tok::kw_typename: // typename T::type
1392 // Annotate typenames and C++ scope specifiers. If we get one, just
1393 // recurse to handle whatever we get.
1394 if (TryAnnotateTypeOrScopeToken())
1395 return TPResult::Error;
1396 return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1398 case tok::coloncolon: { // ::foo::bar
1399 const Token &Next = NextToken();
1400 if (Next.isOneOf(tok::kw_new, // ::new
1401 tok::kw_delete)) // ::delete
1402 return TPResult::False;
1405 case tok::kw___super:
1406 case tok::kw_decltype:
1407 // Annotate typenames and C++ scope specifiers. If we get one, just
1408 // recurse to handle whatever we get.
1409 if (TryAnnotateTypeOrScopeToken())
1410 return TPResult::Error;
1411 return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1414 // storage-class-specifier
1416 // function-specifier
1420 case tok::kw_friend:
1421 case tok::kw_typedef:
1422 case tok::kw_constexpr:
1423 case tok::kw_consteval:
1424 case tok::kw_constinit:
1425 // storage-class-specifier
1426 case tok::kw_register:
1427 case tok::kw_static:
1428 case tok::kw_extern:
1429 case tok::kw_mutable:
1431 case tok::kw___thread:
1432 case tok::kw_thread_local:
1433 case tok::kw__Thread_local:
1434 // function-specifier
1435 case tok::kw_inline:
1436 case tok::kw_virtual:
1437 case tok::kw_explicit:
1440 case tok::kw___module_private__:
1443 case tok::kw___unknown_anytype:
1446 // simple-type-specifier
1449 // elaborated-type-specifier
1450 // typename-specifier
1454 // elaborated-type-specifier
1456 case tok::kw_struct:
1458 case tok::kw___interface:
1463 case tok::kw_volatile:
1464 return TPResult::True;
1466 // OpenCL address space qualifiers
1467 case tok::kw_private:
1468 if (!getLangOpts().OpenCL)
1469 return TPResult::False;
1471 case tok::kw___private:
1472 case tok::kw___local:
1473 case tok::kw___global:
1474 case tok::kw___constant:
1475 case tok::kw___generic:
1476 // OpenCL access qualifiers
1477 case tok::kw___read_only:
1478 case tok::kw___write_only:
1479 case tok::kw___read_write:
1484 case tok::kw_restrict:
1485 case tok::kw__Complex:
1486 case tok::kw___attribute:
1487 case tok::kw___auto_type:
1488 return TPResult::True;
1491 case tok::kw___declspec:
1492 case tok::kw___cdecl:
1493 case tok::kw___stdcall:
1494 case tok::kw___fastcall:
1495 case tok::kw___thiscall:
1496 case tok::kw___regcall:
1497 case tok::kw___vectorcall:
1499 case tok::kw___sptr:
1500 case tok::kw___uptr:
1501 case tok::kw___ptr64:
1502 case tok::kw___ptr32:
1503 case tok::kw___forceinline:
1504 case tok::kw___unaligned:
1505 case tok::kw__Nonnull:
1506 case tok::kw__Nullable:
1507 case tok::kw__Null_unspecified:
1508 case tok::kw___kindof:
1509 return TPResult::True;
1512 case tok::kw___pascal:
1513 return TPResult::True;
1516 case tok::kw___vector:
1517 return TPResult::True;
1519 case tok::annot_template_id: {
1520 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1521 // If lookup for the template-name found nothing, don't assume we have a
1522 // definitive disambiguation result yet.
1523 if (TemplateId->Kind == TNK_Undeclared_template && InvalidAsDeclSpec) {
1524 // 'template-id(' can be a valid expression but not a valid decl spec if
1525 // the template-name is not declared, but we don't consider this to be a
1526 // definitive disambiguation. In any other context, it's an error either
1528 *InvalidAsDeclSpec = NextToken().is(tok::l_paren);
1529 return TPResult::Ambiguous;
1531 if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/0))
1532 return TPResult::True;
1533 if (TemplateId->Kind != TNK_Type_template)
1534 return TPResult::False;
1536 AnnotateTemplateIdTokenAsType(SS);
1537 assert(Tok.is(tok::annot_typename));
1541 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1542 // We've already annotated a scope; try to annotate a type.
1543 if (TryAnnotateTypeOrScopeToken())
1544 return TPResult::Error;
1545 if (!Tok.is(tok::annot_typename)) {
1546 if (Tok.is(tok::annot_cxxscope) &&
1547 NextToken().is(tok::annot_template_id)) {
1548 TemplateIdAnnotation *TemplateId =
1549 takeTemplateIdAnnotation(NextToken());
1550 if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/1))
1551 return TPResult::True;
1553 // If the next token is an identifier or a type qualifier, then this
1554 // can't possibly be a valid expression either.
1555 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1557 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1558 Tok.getAnnotationRange(),
1560 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1561 RevertingTentativeParsingAction PA(*this);
1562 ConsumeAnnotationToken();
1564 bool isIdentifier = Tok.is(tok::identifier);
1565 TPResult TPR = TPResult::False;
1567 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1571 TPR == TPResult::True || TPR == TPResult::Error)
1572 return TPResult::Error;
1574 if (InvalidAsDeclSpec) {
1575 // We can't tell whether this is a missing 'typename' or a valid
1577 *InvalidAsDeclSpec = true;
1578 return TPResult::Ambiguous;
1580 // In MS mode, if InvalidAsDeclSpec is not provided, and the tokens
1581 // are or the form *) or &) *> or &> &&>, this can't be an expression.
1582 // The typename must be missing.
1583 if (getLangOpts().MSVCCompat) {
1584 if (((Tok.is(tok::amp) || Tok.is(tok::star)) &&
1585 (NextToken().is(tok::r_paren) ||
1586 NextToken().is(tok::greater))) ||
1587 (Tok.is(tok::ampamp) && NextToken().is(tok::greater)))
1588 return TPResult::True;
1592 // Try to resolve the name. If it doesn't exist, assume it was
1593 // intended to name a type and keep disambiguating.
1594 switch (TryAnnotateName()) {
1596 return TPResult::Error;
1597 case ANK_TentativeDecl:
1598 return TPResult::False;
1599 case ANK_TemplateName:
1600 // In C++17, this could be a type template for class template
1601 // argument deduction.
1602 if (getLangOpts().CPlusPlus17) {
1603 if (TryAnnotateTypeOrScopeToken())
1604 return TPResult::Error;
1605 if (Tok.isNot(tok::identifier))
1609 // A bare type template-name which can't be a template template
1610 // argument is an error, and was probably intended to be a type.
1611 // In C++17, this could be class template argument deduction.
1612 return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
1615 case ANK_Unresolved:
1616 return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
1621 // Annotated it, check again.
1622 assert(Tok.isNot(tok::annot_cxxscope) ||
1623 NextToken().isNot(tok::identifier));
1624 return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1627 return TPResult::False;
1629 // If that succeeded, fallthrough into the generic simple-type-id case.
1632 // The ambiguity resides in a simple-type-specifier/typename-specifier
1633 // followed by a '('. The '(' could either be the start of:
1635 // direct-declarator:
1636 // '(' declarator ')'
1638 // direct-abstract-declarator:
1639 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1640 // exception-specification[opt]
1641 // '(' abstract-declarator ')'
1643 // or part of a function-style cast expression:
1645 // simple-type-specifier '(' expression-list[opt] ')'
1648 // simple-type-specifier:
1650 case tok::annot_typename:
1652 // In Objective-C, we might have a protocol-qualified type.
1653 if (getLangOpts().ObjC && NextToken().is(tok::less)) {
1654 // Tentatively parse the protocol qualifiers.
1655 RevertingTentativeParsingAction PA(*this);
1656 ConsumeAnyToken(); // The type token
1658 TPResult TPR = TryParseProtocolQualifiers();
1659 bool isFollowedByParen = Tok.is(tok::l_paren);
1660 bool isFollowedByBrace = Tok.is(tok::l_brace);
1662 if (TPR == TPResult::Error)
1663 return TPResult::Error;
1665 if (isFollowedByParen)
1666 return TPResult::Ambiguous;
1668 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1669 return BracedCastResult;
1671 return TPResult::True;
1676 case tok::kw_wchar_t:
1677 case tok::kw_char8_t:
1678 case tok::kw_char16_t:
1679 case tok::kw_char32_t:
1684 case tok::kw___int64:
1685 case tok::kw___int128:
1686 case tok::kw_signed:
1687 case tok::kw_unsigned:
1690 case tok::kw_double:
1691 case tok::kw__Float16:
1692 case tok::kw___float128:
1694 case tok::annot_decltype:
1695 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1696 #include "clang/Basic/OpenCLImageTypes.def"
1697 if (NextToken().is(tok::l_paren))
1698 return TPResult::Ambiguous;
1700 // This is a function-style cast in all cases we disambiguate other than
1703 // enum E : int { a = 4 }; // enum
1704 // enum E : int { 4 }; // bit-field
1706 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1707 return BracedCastResult;
1709 if (isStartOfObjCClassMessageMissingOpenBracket())
1710 return TPResult::False;
1712 return TPResult::True;
1714 // GNU typeof support.
1715 case tok::kw_typeof: {
1716 if (NextToken().isNot(tok::l_paren))
1717 return TPResult::True;
1719 RevertingTentativeParsingAction PA(*this);
1721 TPResult TPR = TryParseTypeofSpecifier();
1722 bool isFollowedByParen = Tok.is(tok::l_paren);
1723 bool isFollowedByBrace = Tok.is(tok::l_brace);
1725 if (TPR == TPResult::Error)
1726 return TPResult::Error;
1728 if (isFollowedByParen)
1729 return TPResult::Ambiguous;
1731 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1732 return BracedCastResult;
1734 return TPResult::True;
1737 // C++0x type traits support
1738 case tok::kw___underlying_type:
1739 return TPResult::True;
1742 case tok::kw__Atomic:
1743 return TPResult::True;
1746 return TPResult::False;
1750 bool Parser::isCXXDeclarationSpecifierAType() {
1751 switch (Tok.getKind()) {
1752 // typename-specifier
1753 case tok::annot_decltype:
1754 case tok::annot_template_id:
1755 case tok::annot_typename:
1756 case tok::kw_typeof:
1757 case tok::kw___underlying_type:
1760 // elaborated-type-specifier
1762 case tok::kw_struct:
1764 case tok::kw___interface:
1768 // simple-type-specifier
1770 case tok::kw_wchar_t:
1771 case tok::kw_char8_t:
1772 case tok::kw_char16_t:
1773 case tok::kw_char32_t:
1778 case tok::kw___int64:
1779 case tok::kw___int128:
1780 case tok::kw_signed:
1781 case tok::kw_unsigned:
1784 case tok::kw_double:
1785 case tok::kw__Float16:
1786 case tok::kw___float128:
1788 case tok::kw___unknown_anytype:
1789 case tok::kw___auto_type:
1790 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1791 #include "clang/Basic/OpenCLImageTypes.def"
1795 return getLangOpts().CPlusPlus11;
1797 case tok::kw__Atomic:
1799 return NextToken().is(tok::l_paren);
1806 /// [GNU] typeof-specifier:
1807 /// 'typeof' '(' expressions ')'
1808 /// 'typeof' '(' type-name ')'
1810 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1811 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1814 assert(Tok.is(tok::l_paren) && "Expected '('");
1815 // Parse through the parens after 'typeof'.
1817 if (!SkipUntil(tok::r_paren, StopAtSemi))
1818 return TPResult::Error;
1820 return TPResult::Ambiguous;
1823 /// [ObjC] protocol-qualifiers:
1824 //// '<' identifier-list '>'
1825 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1826 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1829 if (Tok.isNot(tok::identifier))
1830 return TPResult::Error;
1833 if (Tok.is(tok::comma)) {
1838 if (Tok.is(tok::greater)) {
1840 return TPResult::Ambiguous;
1844 return TPResult::Error;
1847 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1848 /// a constructor-style initializer, when parsing declaration statements.
1849 /// Returns true for function declarator and false for constructor-style
1851 /// If during the disambiguation process a parsing error is encountered,
1852 /// the function returns true to let the declaration parsing code handle it.
1854 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1855 /// exception-specification[opt]
1857 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1860 // The ambiguity arising from the similarity between a function-style cast and
1861 // a declaration mentioned in 6.8 can also occur in the context of a
1862 // declaration. In that context, the choice is between a function declaration
1863 // with a redundant set of parentheses around a parameter name and an object
1864 // declaration with a function-style cast as the initializer. Just as for the
1865 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1866 // that could possibly be a declaration a declaration.
1868 RevertingTentativeParsingAction PA(*this);
1871 bool InvalidAsDeclaration = false;
1872 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1873 if (TPR == TPResult::Ambiguous) {
1874 if (Tok.isNot(tok::r_paren))
1875 TPR = TPResult::False;
1877 const Token &Next = NextToken();
1878 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1879 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1880 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1881 isCXX11VirtSpecifier(Next))
1882 // The next token cannot appear after a constructor-style initializer,
1883 // and can appear next in a function definition. This must be a function
1885 TPR = TPResult::True;
1886 else if (InvalidAsDeclaration)
1887 // Use the absence of 'typename' as a tie-breaker.
1888 TPR = TPResult::False;
1892 if (IsAmbiguous && TPR == TPResult::Ambiguous)
1893 *IsAmbiguous = true;
1895 // In case of an error, let the declaration parsing code handle it.
1896 return TPR != TPResult::False;
1899 /// parameter-declaration-clause:
1900 /// parameter-declaration-list[opt] '...'[opt]
1901 /// parameter-declaration-list ',' '...'
1903 /// parameter-declaration-list:
1904 /// parameter-declaration
1905 /// parameter-declaration-list ',' parameter-declaration
1907 /// parameter-declaration:
1908 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1909 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1910 /// '=' assignment-expression
1911 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1913 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1914 /// attributes[opt] '=' assignment-expression
1917 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1918 bool VersusTemplateArgument) {
1920 if (Tok.is(tok::r_paren))
1921 return TPResult::Ambiguous;
1923 // parameter-declaration-list[opt] '...'[opt]
1924 // parameter-declaration-list ',' '...'
1926 // parameter-declaration-list:
1927 // parameter-declaration
1928 // parameter-declaration-list ',' parameter-declaration
1932 if (Tok.is(tok::ellipsis)) {
1934 if (Tok.is(tok::r_paren))
1935 return TPResult::True; // '...)' is a sign of a function declarator.
1937 return TPResult::False;
1940 // An attribute-specifier-seq here is a sign of a function declarator.
1941 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1942 /*OuterMightBeMessageSend*/true))
1943 return TPResult::True;
1945 ParsedAttributes attrs(AttrFactory);
1946 MaybeParseMicrosoftAttributes(attrs);
1948 // decl-specifier-seq
1949 // A parameter-declaration's initializer must be preceded by an '=', so
1950 // decl-specifier-seq '{' is not a parameter in C++11.
1951 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1952 InvalidAsDeclaration);
1953 // A declaration-specifier (not followed by '(' or '{') means this can't be
1954 // an expression, but it could still be a template argument.
1955 if (TPR != TPResult::Ambiguous &&
1956 !(VersusTemplateArgument && TPR == TPResult::True))
1959 bool SeenType = false;
1961 SeenType |= isCXXDeclarationSpecifierAType();
1962 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1963 return TPResult::Error;
1965 // If we see a parameter name, this can't be a template argument.
1966 if (SeenType && Tok.is(tok::identifier))
1967 return TPResult::True;
1969 TPR = isCXXDeclarationSpecifier(TPResult::False,
1970 InvalidAsDeclaration);
1971 if (TPR == TPResult::Error)
1974 // Two declaration-specifiers means this can't be an expression.
1975 if (TPR == TPResult::True && !VersusTemplateArgument)
1977 } while (TPR != TPResult::False);
1980 // abstract-declarator[opt]
1981 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1982 if (TPR != TPResult::Ambiguous)
1985 // [GNU] attributes[opt]
1986 if (Tok.is(tok::kw___attribute))
1987 return TPResult::True;
1989 // If we're disambiguating a template argument in a default argument in
1990 // a class definition versus a parameter declaration, an '=' here
1991 // disambiguates the parse one way or the other.
1992 // If this is a parameter, it must have a default argument because
1993 // (a) the previous parameter did, and
1994 // (b) this must be the first declaration of the function, so we can't
1995 // inherit any default arguments from elsewhere.
1996 // If we see an ')', then we've reached the end of a
1997 // parameter-declaration-clause, and the last param is missing its default
1999 if (VersusTemplateArgument)
2000 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
2003 if (Tok.is(tok::equal)) {
2004 // '=' assignment-expression
2005 // Parse through assignment-expression.
2006 // FIXME: assignment-expression may contain an unparenthesized comma.
2007 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
2008 return TPResult::Error;
2011 if (Tok.is(tok::ellipsis)) {
2013 if (Tok.is(tok::r_paren))
2014 return TPResult::True; // '...)' is a sign of a function declarator.
2016 return TPResult::False;
2019 if (!TryConsumeToken(tok::comma))
2023 return TPResult::Ambiguous;
2026 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
2027 /// parsing as a function declarator.
2028 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
2029 /// return TPResult::Ambiguous, otherwise it will return either False() or
2032 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
2033 /// exception-specification[opt]
2035 /// exception-specification:
2036 /// 'throw' '(' type-id-list[opt] ')'
2038 Parser::TPResult Parser::TryParseFunctionDeclarator() {
2039 // The '(' is already parsed.
2041 TPResult TPR = TryParseParameterDeclarationClause();
2042 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
2043 TPR = TPResult::False;
2045 if (TPR == TPResult::False || TPR == TPResult::Error)
2048 // Parse through the parens.
2049 if (!SkipUntil(tok::r_paren, StopAtSemi))
2050 return TPResult::Error;
2053 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
2057 // ref-qualifier[opt]
2058 if (Tok.isOneOf(tok::amp, tok::ampamp))
2061 // exception-specification
2062 if (Tok.is(tok::kw_throw)) {
2064 if (Tok.isNot(tok::l_paren))
2065 return TPResult::Error;
2067 // Parse through the parens after 'throw'.
2069 if (!SkipUntil(tok::r_paren, StopAtSemi))
2070 return TPResult::Error;
2072 if (Tok.is(tok::kw_noexcept)) {
2074 // Possibly an expression as well.
2075 if (Tok.is(tok::l_paren)) {
2076 // Find the matching rparen.
2078 if (!SkipUntil(tok::r_paren, StopAtSemi))
2079 return TPResult::Error;
2083 return TPResult::Ambiguous;
2086 /// '[' constant-expression[opt] ']'
2088 Parser::TPResult Parser::TryParseBracketDeclarator() {
2091 // A constant-expression cannot begin with a '{', but the
2092 // expr-or-braced-init-list of a postfix-expression can.
2093 if (Tok.is(tok::l_brace))
2094 return TPResult::False;
2096 if (!SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch))
2097 return TPResult::Error;
2099 // If we hit a comma before the ']', this is not a constant-expression,
2100 // but might still be the expr-or-braced-init-list of a postfix-expression.
2101 if (Tok.isNot(tok::r_square))
2102 return TPResult::False;
2105 return TPResult::Ambiguous;
2108 /// Determine whether we might be looking at the '<' template-argument-list '>'
2109 /// of a template-id or simple-template-id, rather than a less-than comparison.
2110 /// This will often fail and produce an ambiguity, but should never be wrong
2111 /// if it returns True or False.
2112 Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
2113 if (!TokensToSkip) {
2114 if (Tok.isNot(tok::less))
2115 return TPResult::False;
2116 if (NextToken().is(tok::greater))
2117 return TPResult::True;
2120 RevertingTentativeParsingAction PA(*this);
2122 while (TokensToSkip) {
2127 if (!TryConsumeToken(tok::less))
2128 return TPResult::False;
2130 // We can't do much to tell an expression apart from a template-argument,
2131 // but one good distinguishing factor is that a "decl-specifier" not
2132 // followed by '(' or '{' can't appear in an expression.
2133 bool InvalidAsTemplateArgumentList = false;
2134 if (isCXXDeclarationSpecifier(TPResult::False,
2135 &InvalidAsTemplateArgumentList) ==
2137 return TPResult::True;
2138 if (InvalidAsTemplateArgumentList)
2139 return TPResult::False;
2141 // FIXME: In many contexts, X<thing1, Type> can only be a
2142 // template-argument-list. But that's not true in general:
2146 // int a = A<B, b, c = C>D; // OK, declares b, not a template-id.
2148 // X<Y<0, int> // ', int>' might be end of X's template argument list
2150 // We might be able to disambiguate a few more cases if we're careful.
2152 // A template-argument-list must be terminated by a '>'.
2153 if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
2154 StopAtSemi | StopBeforeMatch))
2155 return TPResult::Ambiguous;
2156 return TPResult::False;
2159 /// Determine whether we might be looking at the '(' of a C++20 explicit(bool)
2160 /// in an earlier language mode.
2161 Parser::TPResult Parser::isExplicitBool() {
2162 assert(Tok.is(tok::l_paren) && "expected to be looking at a '(' token");
2164 RevertingTentativeParsingAction PA(*this);
2167 // We can only have 'explicit' on a constructor, conversion function, or
2168 // deduction guide. The declarator of a deduction guide cannot be
2169 // parenthesized, so we know this isn't a deduction guide. So the only
2170 // thing we need to check for is some number of parens followed by either
2171 // the current class name or 'operator'.
2172 while (Tok.is(tok::l_paren))
2175 if (TryAnnotateOptionalCXXScopeToken())
2176 return TPResult::Error;
2178 // Class-scope constructor and conversion function names can't really be
2179 // qualified, but we get better diagnostics if we assume they can be.
2181 if (Tok.is(tok::annot_cxxscope)) {
2182 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2183 Tok.getAnnotationRange(),
2185 ConsumeAnnotationToken();
2188 // 'explicit(operator' might be explicit(bool) or the declaration of a
2189 // conversion function, but it's probably a conversion function.
2190 if (Tok.is(tok::kw_operator))
2191 return TPResult::Ambiguous;
2193 // If this can't be a constructor name, it can only be explicit(bool).
2194 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
2195 return TPResult::True;
2196 if (!Actions.isCurrentClassName(Tok.is(tok::identifier)
2197 ? *Tok.getIdentifierInfo()
2198 : *takeTemplateIdAnnotation(Tok)->Name,
2199 getCurScope(), &SS))
2200 return TPResult::True;
2201 // Formally, we must have a right-paren after the constructor name to match
2202 // the grammar for a constructor. But clang permits a parenthesized
2203 // constructor declarator, so also allow a constructor declarator to follow
2204 // with no ')' token after the constructor name.
2205 if (!NextToken().is(tok::r_paren) &&
2206 !isConstructorDeclarator(/*Unqualified=*/SS.isEmpty(),
2207 /*DeductionGuide=*/false))
2208 return TPResult::True;
2210 // Might be explicit(bool) or a parenthesized constructor name.
2211 return TPResult::Ambiguous;