1 //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the tentative parsing portions of the Parser
11 // interfaces, for ambiguity resolution.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Parse/Parser.h"
16 #include "clang/Parse/ParseDiagnostic.h"
17 #include "clang/Sema/ParsedTemplate.h"
18 using namespace clang;
20 /// isCXXDeclarationStatement - C++-specialized function that disambiguates
21 /// between a declaration or an expression statement, when parsing function
22 /// bodies. Returns true for declaration, false for expression.
24 /// declaration-statement:
27 /// block-declaration:
28 /// simple-declaration
30 /// namespace-alias-definition
33 /// [C++0x] static_assert-declaration
36 /// 'asm' '(' string-literal ')' ';'
38 /// namespace-alias-definition:
39 /// 'namespace' identifier = qualified-namespace-specifier ';'
41 /// using-declaration:
42 /// 'using' typename[opt] '::'[opt] nested-name-specifier
43 /// unqualified-id ';'
44 /// 'using' '::' unqualified-id ;
47 /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
48 /// namespace-name ';'
50 bool Parser::isCXXDeclarationStatement() {
51 switch (Tok.getKind()) {
54 // namespace-alias-definition
55 case tok::kw_namespace:
59 // static_assert-declaration
60 case tok::kw_static_assert:
61 case tok::kw__Static_assert:
65 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
69 /// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70 /// between a simple-declaration or an expression-statement.
71 /// If during the disambiguation process a parsing error is encountered,
72 /// the function returns true to let the declaration parsing code handle it.
73 /// Returns false if the statement is disambiguated as expression.
75 /// simple-declaration:
76 /// decl-specifier-seq init-declarator-list[opt] ';'
78 /// (if AllowForRangeDecl specified)
79 /// for ( for-range-declaration : for-range-initializer ) statement
80 /// for-range-declaration:
81 /// attribute-specifier-seqopt type-specifier-seq declarator
82 bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
84 // There is an ambiguity in the grammar involving expression-statements and
85 // declarations: An expression-statement with a function-style explicit type
86 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87 // from a declaration where the first declarator starts with a '('. In those
88 // cases the statement is a declaration. [Note: To disambiguate, the whole
89 // statement might have to be examined to determine if it is an
90 // expression-statement or a declaration].
93 // The disambiguation is purely syntactic; that is, the meaning of the names
94 // occurring in such a statement, beyond whether they are type-names or not,
95 // is not generally used in or changed by the disambiguation. Class
96 // templates are instantiated as necessary to determine if a qualified name
97 // is a type-name. Disambiguation precedes parsing, and a statement
98 // disambiguated as a declaration may be an ill-formed declaration.
100 // We don't have to parse all of the decl-specifier-seq part. There's only
101 // an ambiguity if the first decl-specifier is
102 // simple-type-specifier/typename-specifier followed by a '(', which may
103 // indicate a function-style cast expression.
104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
107 bool InvalidAsDeclaration = false;
108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
109 &InvalidAsDeclaration);
110 if (TPR != TPResult::Ambiguous)
111 return TPR != TPResult::False; // Returns true for TPResult::True or
114 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
115 // and so gets some cases wrong. We can't carry on if we've already seen
116 // something which makes this statement invalid as a declaration in this case,
117 // since it can cause us to misparse valid code. Revisit this once
118 // TryParseInitDeclaratorList is fixed.
119 if (InvalidAsDeclaration)
122 // FIXME: Add statistics about the number of ambiguous statements encountered
123 // and how they were resolved (number of declarations+number of expressions).
125 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
126 // or an identifier which doesn't resolve as anything. We need tentative
129 TentativeParsingAction PA(*this);
130 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
133 // In case of an error, let the declaration parsing code handle it.
134 if (TPR == TPResult::Error)
137 // Declarations take precedence over expressions.
138 if (TPR == TPResult::Ambiguous)
139 TPR = TPResult::True;
141 assert(TPR == TPResult::True || TPR == TPResult::False);
142 return TPR == TPResult::True;
145 /// Try to consume a token sequence that we've already identified as
146 /// (potentially) starting a decl-specifier.
147 Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
148 switch (Tok.getKind()) {
149 case tok::kw__Atomic:
150 if (NextToken().isNot(tok::l_paren)) {
156 case tok::kw___attribute:
157 case tok::kw___underlying_type: {
159 if (Tok.isNot(tok::l_paren))
160 return TPResult::Error;
162 if (!SkipUntil(tok::r_paren))
163 return TPResult::Error;
170 case tok::kw___interface:
172 // elaborated-type-specifier:
173 // class-key attribute-specifier-seq[opt]
174 // nested-name-specifier[opt] identifier
175 // class-key nested-name-specifier[opt] template[opt] simple-template-id
176 // enum nested-name-specifier[opt] identifier
178 // FIXME: We don't support class-specifiers nor enum-specifiers here.
182 while (Tok.is(tok::l_square) || Tok.is(tok::kw___attribute) ||
183 Tok.is(tok::kw___declspec) || Tok.is(tok::kw_alignas)) {
184 if (Tok.is(tok::l_square)) {
186 if (!SkipUntil(tok::r_square))
187 return TPResult::Error;
190 if (Tok.isNot(tok::l_paren))
191 return TPResult::Error;
193 if (!SkipUntil(tok::r_paren))
194 return TPResult::Error;
198 if ((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
199 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
200 TryAnnotateCXXScopeToken())
201 return TPResult::Error;
202 if (Tok.is(tok::annot_cxxscope))
204 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
205 return TPResult::Error;
209 case tok::annot_cxxscope:
215 if (getLangOpts().ObjC1 && Tok.is(tok::less))
216 return TryParseProtocolQualifiers();
220 return TPResult::Ambiguous;
223 /// simple-declaration:
224 /// decl-specifier-seq init-declarator-list[opt] ';'
226 /// (if AllowForRangeDecl specified)
227 /// for ( for-range-declaration : for-range-initializer ) statement
228 /// for-range-declaration:
229 /// attribute-specifier-seqopt type-specifier-seq declarator
231 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
232 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
233 return TPResult::Error;
235 // Two decl-specifiers in a row conclusively disambiguate this as being a
236 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
237 // overwhelmingly common case that the next token is a '('.
238 if (Tok.isNot(tok::l_paren)) {
239 TPResult TPR = isCXXDeclarationSpecifier();
240 if (TPR == TPResult::Ambiguous)
241 return TPResult::True;
242 if (TPR == TPResult::True || TPR == TPResult::Error)
244 assert(TPR == TPResult::False);
247 TPResult TPR = TryParseInitDeclaratorList();
248 if (TPR != TPResult::Ambiguous)
251 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
252 return TPResult::False;
254 return TPResult::Ambiguous;
257 /// Tentatively parse an init-declarator-list in order to disambiguate it from
260 /// init-declarator-list:
262 /// init-declarator-list ',' init-declarator
265 /// declarator initializer[opt]
266 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
269 /// brace-or-equal-initializer
270 /// '(' expression-list ')'
272 /// brace-or-equal-initializer:
273 /// '=' initializer-clause
274 /// [C++11] braced-init-list
276 /// initializer-clause:
277 /// assignment-expression
280 /// braced-init-list:
281 /// '{' initializer-list ','[opt] '}'
284 Parser::TPResult Parser::TryParseInitDeclaratorList() {
287 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
288 if (TPR != TPResult::Ambiguous)
291 // [GNU] simple-asm-expr[opt] attributes[opt]
292 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
293 return TPResult::True;
296 if (Tok.is(tok::l_paren)) {
297 // Parse through the parens.
299 if (!SkipUntil(tok::r_paren, StopAtSemi))
300 return TPResult::Error;
301 } else if (Tok.is(tok::l_brace)) {
302 // A left-brace here is sufficient to disambiguate the parse; an
303 // expression can never be followed directly by a braced-init-list.
304 return TPResult::True;
305 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
306 // MSVC and g++ won't examine the rest of declarators if '=' is
307 // encountered; they just conclude that we have a declaration.
308 // EDG parses the initializer completely, which is the proper behavior
311 // At present, Clang follows MSVC and g++, since the parser does not have
312 // the ability to parse an expression fully without recording the
313 // results of that parse.
314 // FIXME: Handle this case correctly.
316 // Also allow 'in' after an Objective-C declaration as in:
317 // for (int (^b)(void) in array). Ideally this should be done in the
318 // context of parsing for-init-statement of a foreach statement only. But,
319 // in any other context 'in' is invalid after a declaration and parser
320 // issues the error regardless of outcome of this decision.
321 // FIXME: Change if above assumption does not hold.
322 return TPResult::True;
325 if (!TryConsumeToken(tok::comma))
329 return TPResult::Ambiguous;
332 /// isCXXConditionDeclaration - Disambiguates between a declaration or an
333 /// expression for a condition of a if/switch/while/for statement.
334 /// If during the disambiguation process a parsing error is encountered,
335 /// the function returns true to let the declaration parsing code handle it.
339 /// type-specifier-seq declarator '=' assignment-expression
340 /// [C++11] type-specifier-seq declarator '=' initializer-clause
341 /// [C++11] type-specifier-seq declarator braced-init-list
342 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
343 /// '=' assignment-expression
345 bool Parser::isCXXConditionDeclaration() {
346 TPResult TPR = isCXXDeclarationSpecifier();
347 if (TPR != TPResult::Ambiguous)
348 return TPR != TPResult::False; // Returns true for TPResult::True or
351 // FIXME: Add statistics about the number of ambiguous statements encountered
352 // and how they were resolved (number of declarations+number of expressions).
354 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
355 // We need tentative parsing...
357 TentativeParsingAction PA(*this);
359 // type-specifier-seq
360 TryConsumeDeclarationSpecifier();
361 assert(Tok.is(tok::l_paren) && "Expected '('");
364 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
366 // In case of an error, let the declaration parsing code handle it.
367 if (TPR == TPResult::Error)
368 TPR = TPResult::True;
370 if (TPR == TPResult::Ambiguous) {
372 // [GNU] simple-asm-expr[opt] attributes[opt]
373 if (Tok.is(tok::equal) ||
374 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
375 TPR = TPResult::True;
376 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
377 TPR = TPResult::True;
379 TPR = TPResult::False;
384 assert(TPR == TPResult::True || TPR == TPResult::False);
385 return TPR == TPResult::True;
388 /// \brief Determine whether the next set of tokens contains a type-id.
390 /// The context parameter states what context we're parsing right
391 /// now, which affects how this routine copes with the token
392 /// following the type-id. If the context is TypeIdInParens, we have
393 /// already parsed the '(' and we will cease lookahead when we hit
394 /// the corresponding ')'. If the context is
395 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
396 /// before this template argument, and will cease lookahead when we
397 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
398 /// and false for an expression. If during the disambiguation
399 /// process a parsing error is encountered, the function returns
400 /// true to let the declaration parsing code handle it.
403 /// type-specifier-seq abstract-declarator[opt]
405 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
410 // The ambiguity arising from the similarity between a function-style cast and
411 // a type-id can occur in different contexts. The ambiguity appears as a
412 // choice between a function-style cast expression and a declaration of a
413 // type. The resolution is that any construct that could possibly be a type-id
414 // in its syntactic context shall be considered a type-id.
416 TPResult TPR = isCXXDeclarationSpecifier();
417 if (TPR != TPResult::Ambiguous)
418 return TPR != TPResult::False; // Returns true for TPResult::True or
421 // FIXME: Add statistics about the number of ambiguous statements encountered
422 // and how they were resolved (number of declarations+number of expressions).
424 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
425 // We need tentative parsing...
427 TentativeParsingAction PA(*this);
429 // type-specifier-seq
430 TryConsumeDeclarationSpecifier();
431 assert(Tok.is(tok::l_paren) && "Expected '('");
434 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
436 // In case of an error, let the declaration parsing code handle it.
437 if (TPR == TPResult::Error)
438 TPR = TPResult::True;
440 if (TPR == TPResult::Ambiguous) {
441 // We are supposed to be inside parens, so if after the abstract declarator
442 // we encounter a ')' this is a type-id, otherwise it's an expression.
443 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
444 TPR = TPResult::True;
447 // We are supposed to be inside a template argument, so if after
448 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
449 // ',', this is a type-id. Otherwise, it's an expression.
450 } else if (Context == TypeIdAsTemplateArgument &&
451 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
452 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
453 TPR = TPResult::True;
457 TPR = TPResult::False;
462 assert(TPR == TPResult::True || TPR == TPResult::False);
463 return TPR == TPResult::True;
466 /// \brief Returns true if this is a C++11 attribute-specifier. Per
467 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
468 /// always introduce an attribute. In Objective-C++11, this rule does not
469 /// apply if either '[' begins a message-send.
471 /// If Disambiguate is true, we try harder to determine whether a '[[' starts
472 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
474 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
475 /// Obj-C message send or the start of an attribute. Otherwise, we assume it
476 /// is not an Obj-C message send.
478 /// C++11 [dcl.attr.grammar]:
480 /// attribute-specifier:
481 /// '[' '[' attribute-list ']' ']'
482 /// alignment-specifier
486 /// attribute-list ',' attribute[opt]
488 /// attribute-list ',' attribute '...'
491 /// attribute-token attribute-argument-clause[opt]
495 /// identifier '::' identifier
497 /// attribute-argument-clause:
498 /// '(' balanced-token-seq ')'
499 Parser::CXX11AttributeKind
500 Parser::isCXX11AttributeSpecifier(bool Disambiguate,
501 bool OuterMightBeMessageSend) {
502 if (Tok.is(tok::kw_alignas))
503 return CAK_AttributeSpecifier;
505 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
506 return CAK_NotAttributeSpecifier;
508 // No tentative parsing if we don't need to look for ']]' or a lambda.
509 if (!Disambiguate && !getLangOpts().ObjC1)
510 return CAK_AttributeSpecifier;
512 TentativeParsingAction PA(*this);
514 // Opening brackets were checked for above.
517 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
518 if (!getLangOpts().ObjC1) {
521 bool IsAttribute = SkipUntil(tok::r_square);
522 IsAttribute &= Tok.is(tok::r_square);
526 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
529 // In Obj-C++11, we need to distinguish four situations:
530 // 1a) int x[[attr]]; C++11 attribute.
531 // 1b) [[attr]]; C++11 statement attribute.
532 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
533 // 3a) int x[[obj get]]; Message send in array size/index.
534 // 3b) [[Class alloc] init]; Message send in message send.
535 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
536 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
538 // If we have a lambda-introducer, then this is definitely not a message send.
539 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
540 // into the tentative attribute parse below.
541 LambdaIntroducer Intro;
542 if (!TryParseLambdaIntroducer(Intro)) {
543 // A lambda cannot end with ']]', and an attribute must.
544 bool IsAttribute = Tok.is(tok::r_square);
549 // Case 1: C++11 attribute.
550 return CAK_AttributeSpecifier;
552 if (OuterMightBeMessageSend)
553 // Case 4: Lambda in message send.
554 return CAK_NotAttributeSpecifier;
556 // Case 2: Lambda in array size / index.
557 return CAK_InvalidAttributeSpecifier;
562 // If we don't have a lambda-introducer, then we have an attribute or a
564 bool IsAttribute = true;
565 while (Tok.isNot(tok::r_square)) {
566 if (Tok.is(tok::comma)) {
567 // Case 1: Stray commas can only occur in attributes.
569 return CAK_AttributeSpecifier;
572 // Parse the attribute-token, if present.
573 // C++11 [dcl.attr.grammar]:
574 // If a keyword or an alternative token that satisfies the syntactic
575 // requirements of an identifier is contained in an attribute-token,
576 // it is considered an identifier.
578 if (!TryParseCXX11AttributeIdentifier(Loc)) {
582 if (Tok.is(tok::coloncolon)) {
584 if (!TryParseCXX11AttributeIdentifier(Loc)) {
590 // Parse the attribute-argument-clause, if present.
591 if (Tok.is(tok::l_paren)) {
593 if (!SkipUntil(tok::r_paren)) {
599 TryConsumeToken(tok::ellipsis);
601 if (!TryConsumeToken(tok::comma))
605 // An attribute must end ']]'.
607 if (Tok.is(tok::r_square)) {
609 IsAttribute = Tok.is(tok::r_square);
618 // Case 1: C++11 statement attribute.
619 return CAK_AttributeSpecifier;
621 // Case 3: Message send.
622 return CAK_NotAttributeSpecifier;
625 Parser::TPResult Parser::TryParsePtrOperatorSeq() {
627 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
628 if (TryAnnotateCXXScopeToken(true))
629 return TPResult::Error;
631 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
632 Tok.is(tok::ampamp) ||
633 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
636 while (Tok.is(tok::kw_const) ||
637 Tok.is(tok::kw_volatile) ||
638 Tok.is(tok::kw_restrict))
641 return TPResult::True;
646 /// operator-function-id:
647 /// 'operator' operator
650 /// new delete new[] delete[] + - * / % ^ [...]
652 /// conversion-function-id:
653 /// 'operator' conversion-type-id
655 /// conversion-type-id:
656 /// type-specifier-seq conversion-declarator[opt]
658 /// conversion-declarator:
659 /// ptr-operator conversion-declarator[opt]
661 /// literal-operator-id:
662 /// 'operator' string-literal identifier
663 /// 'operator' user-defined-string-literal
664 Parser::TPResult Parser::TryParseOperatorId() {
665 assert(Tok.is(tok::kw_operator));
668 // Maybe this is an operator-function-id.
669 switch (Tok.getKind()) {
670 case tok::kw_new: case tok::kw_delete:
672 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
676 return TPResult::True;
678 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
680 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
681 #include "clang/Basic/OperatorKinds.def"
683 return TPResult::True;
686 if (NextToken().is(tok::r_square)) {
689 return TPResult::True;
694 if (NextToken().is(tok::r_paren)) {
697 return TPResult::True;
705 // Maybe this is a literal-operator-id.
706 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
707 bool FoundUDSuffix = false;
709 FoundUDSuffix |= Tok.hasUDSuffix();
710 ConsumeStringToken();
711 } while (isTokenStringLiteral());
713 if (!FoundUDSuffix) {
714 if (Tok.is(tok::identifier))
717 return TPResult::Error;
719 return TPResult::True;
722 // Maybe this is a conversion-function-id.
723 bool AnyDeclSpecifiers = false;
725 TPResult TPR = isCXXDeclarationSpecifier();
726 if (TPR == TPResult::Error)
728 if (TPR == TPResult::False) {
729 if (!AnyDeclSpecifiers)
730 return TPResult::Error;
733 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
734 return TPResult::Error;
735 AnyDeclSpecifiers = true;
737 return TryParsePtrOperatorSeq();
741 /// direct-declarator
742 /// ptr-operator declarator
744 /// direct-declarator:
746 /// direct-declarator '(' parameter-declaration-clause ')'
747 /// cv-qualifier-seq[opt] exception-specification[opt]
748 /// direct-declarator '[' constant-expression[opt] ']'
749 /// '(' declarator ')'
750 /// [GNU] '(' attributes declarator ')'
752 /// abstract-declarator:
753 /// ptr-operator abstract-declarator[opt]
754 /// direct-abstract-declarator
757 /// direct-abstract-declarator:
758 /// direct-abstract-declarator[opt]
759 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
760 /// exception-specification[opt]
761 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
762 /// '(' abstract-declarator ')'
765 /// '*' cv-qualifier-seq[opt]
767 /// [C++0x] '&&' [TODO]
768 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
770 /// cv-qualifier-seq:
771 /// cv-qualifier cv-qualifier-seq[opt]
778 /// '...'[opt] id-expression
782 /// qualified-id [TODO]
786 /// operator-function-id
787 /// conversion-function-id
788 /// literal-operator-id
789 /// '~' class-name [TODO]
790 /// '~' decltype-specifier [TODO]
791 /// template-id [TODO]
793 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
794 bool mayHaveIdentifier) {
797 // ptr-operator declarator
798 if (TryParsePtrOperatorSeq() == TPResult::Error)
799 return TPResult::Error;
801 // direct-declarator:
802 // direct-abstract-declarator:
803 if (Tok.is(tok::ellipsis))
806 if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
807 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
808 NextToken().is(tok::kw_operator)))) &&
811 if (Tok.is(tok::annot_cxxscope))
813 else if (Tok.is(tok::identifier))
814 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
815 if (Tok.is(tok::kw_operator)) {
816 if (TryParseOperatorId() == TPResult::Error)
817 return TPResult::Error;
820 } else if (Tok.is(tok::l_paren)) {
823 (Tok.is(tok::r_paren) || // 'int()' is a function.
824 // 'int(...)' is a function.
825 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
826 isDeclarationSpecifier())) { // 'int(int)' is a function.
827 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
828 // exception-specification[opt]
829 TPResult TPR = TryParseFunctionDeclarator();
830 if (TPR != TPResult::Ambiguous)
833 // '(' declarator ')'
834 // '(' attributes declarator ')'
835 // '(' abstract-declarator ')'
836 if (Tok.is(tok::kw___attribute) ||
837 Tok.is(tok::kw___declspec) ||
838 Tok.is(tok::kw___cdecl) ||
839 Tok.is(tok::kw___stdcall) ||
840 Tok.is(tok::kw___fastcall) ||
841 Tok.is(tok::kw___thiscall) ||
842 Tok.is(tok::kw___vectorcall) ||
843 Tok.is(tok::kw___unaligned))
844 return TPResult::True; // attributes indicate declaration
845 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
846 if (TPR != TPResult::Ambiguous)
848 if (Tok.isNot(tok::r_paren))
849 return TPResult::False;
852 } else if (!mayBeAbstract) {
853 return TPResult::False;
857 TPResult TPR(TPResult::Ambiguous);
859 // abstract-declarator: ...
860 if (Tok.is(tok::ellipsis))
863 if (Tok.is(tok::l_paren)) {
864 // Check whether we have a function declarator or a possible ctor-style
865 // initializer that follows the declarator. Note that ctor-style
866 // initializers are not possible in contexts where abstract declarators
868 if (!mayBeAbstract && !isCXXFunctionDeclarator())
871 // direct-declarator '(' parameter-declaration-clause ')'
872 // cv-qualifier-seq[opt] exception-specification[opt]
874 TPR = TryParseFunctionDeclarator();
875 } else if (Tok.is(tok::l_square)) {
876 // direct-declarator '[' constant-expression[opt] ']'
877 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
878 TPR = TryParseBracketDeclarator();
883 if (TPR != TPResult::Ambiguous)
887 return TPResult::Ambiguous;
891 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
893 // Obviously starts an expression.
894 case tok::numeric_constant:
895 case tok::char_constant:
896 case tok::wide_char_constant:
897 case tok::utf8_char_constant:
898 case tok::utf16_char_constant:
899 case tok::utf32_char_constant:
900 case tok::string_literal:
901 case tok::wide_string_literal:
902 case tok::utf8_string_literal:
903 case tok::utf16_string_literal:
904 case tok::utf32_string_literal:
913 case tok::minusminus:
917 case tok::kw___func__:
918 case tok::kw_const_cast:
920 case tok::kw_dynamic_cast:
923 case tok::kw_operator:
924 case tok::kw_reinterpret_cast:
925 case tok::kw_static_cast:
930 case tok::kw_alignof:
931 case tok::kw_noexcept:
932 case tok::kw_nullptr:
933 case tok::kw__Alignof:
935 case tok::kw___alignof:
936 case tok::kw___builtin_choose_expr:
937 case tok::kw___builtin_offsetof:
938 case tok::kw___builtin_va_arg:
941 case tok::kw___FUNCTION__:
942 case tok::kw___FUNCDNAME__:
943 case tok::kw___FUNCSIG__:
944 case tok::kw_L__FUNCTION__:
945 case tok::kw___PRETTY_FUNCTION__:
946 case tok::kw___uuidof:
947 #define TYPE_TRAIT(N,Spelling,K) \
948 case tok::kw_##Spelling:
949 #include "clang/Basic/TokenKinds.def"
950 return TPResult::True;
952 // Obviously starts a type-specifier-seq:
961 case tok::kw___int64:
962 case tok::kw___int128:
963 case tok::kw_restrict:
968 case tok::kw_unsigned:
970 case tok::kw_volatile:
972 case tok::kw__Complex:
974 case tok::kw_typename:
975 case tok::kw_wchar_t:
976 case tok::kw_char16_t:
977 case tok::kw_char32_t:
978 case tok::kw__Decimal32:
979 case tok::kw__Decimal64:
980 case tok::kw__Decimal128:
981 case tok::kw___interface:
982 case tok::kw___thread:
983 case tok::kw_thread_local:
984 case tok::kw__Thread_local:
986 case tok::kw___underlying_type:
987 case tok::kw___cdecl:
988 case tok::kw___stdcall:
989 case tok::kw___fastcall:
990 case tok::kw___thiscall:
991 case tok::kw___vectorcall:
992 case tok::kw___unaligned:
993 case tok::kw___vector:
994 case tok::kw___pixel:
996 case tok::kw__Atomic:
997 case tok::kw___unknown_anytype:
998 return TPResult::False;
1004 return TPResult::Ambiguous;
1007 bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1008 return std::find(TentativelyDeclaredIdentifiers.begin(),
1009 TentativelyDeclaredIdentifiers.end(), II)
1010 != TentativelyDeclaredIdentifiers.end();
1014 class TentativeParseCCC : public CorrectionCandidateCallback {
1016 TentativeParseCCC(const Token &Next) {
1017 WantRemainingKeywords = false;
1018 WantTypeSpecifiers = Next.is(tok::l_paren) || Next.is(tok::r_paren) ||
1019 Next.is(tok::greater) || Next.is(tok::l_brace) ||
1020 Next.is(tok::identifier);
1023 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1024 // Reject any candidate that only resolves to instance members since they
1025 // aren't viable as standalone identifiers instead of member references.
1026 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1027 std::all_of(Candidate.begin(), Candidate.end(),
1028 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1031 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1035 /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1036 /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1037 /// be either a decl-specifier or a function-style cast, and TPResult::Error
1038 /// if a parsing error was found and reported.
1040 /// If HasMissingTypename is provided, a name with a dependent scope specifier
1041 /// will be treated as ambiguous if the 'typename' keyword is missing. If this
1042 /// happens, *HasMissingTypename will be set to 'true'. This will also be used
1043 /// as an indicator that undeclared identifiers (which will trigger a later
1044 /// parse error) should be treated as types. Returns TPResult::Ambiguous in
1048 /// storage-class-specifier
1050 /// function-specifier
1053 /// [C++11] 'constexpr'
1054 /// [GNU] attributes declaration-specifiers[opt]
1056 /// storage-class-specifier:
1062 /// [GNU] '__thread'
1063 /// [C++11] 'thread_local'
1064 /// [C11] '_Thread_local'
1066 /// function-specifier:
1075 /// simple-type-specifier
1078 /// elaborated-type-specifier
1079 /// typename-specifier
1082 /// simple-type-specifier:
1083 /// '::'[opt] nested-name-specifier[opt] type-name
1084 /// '::'[opt] nested-name-specifier 'template'
1085 /// simple-template-id [TODO]
1097 /// [GNU] typeof-specifier
1098 /// [GNU] '_Complex'
1100 /// [C++11] 'decltype' ( expression )
1101 /// [C++1y] 'decltype' ( 'auto' )
1108 /// elaborated-type-specifier:
1109 /// class-key '::'[opt] nested-name-specifier[opt] identifier
1110 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1111 /// simple-template-id
1112 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1118 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1119 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1121 /// class-specifier:
1122 /// class-head '{' member-specification[opt] '}'
1125 /// class-key identifier[opt] base-clause[opt]
1126 /// class-key nested-name-specifier identifier base-clause[opt]
1127 /// class-key nested-name-specifier[opt] simple-template-id
1128 /// base-clause[opt]
1141 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1142 bool *HasMissingTypename) {
1143 switch (Tok.getKind()) {
1144 case tok::identifier: {
1145 // Check for need to substitute AltiVec __vector keyword
1146 // for "vector" identifier.
1147 if (TryAltiVecVectorToken())
1148 return TPResult::True;
1150 const Token &Next = NextToken();
1151 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1152 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
1153 return TPResult::True;
1155 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1156 // Determine whether this is a valid expression. If not, we will hit
1157 // a parse error one way or another. In that case, tell the caller that
1158 // this is ambiguous. Typo-correct to type and expression keywords and
1159 // to types and identifiers, in order to try to recover from errors.
1160 switch (TryAnnotateName(false /* no nested name specifier */,
1161 llvm::make_unique<TentativeParseCCC>(Next))) {
1163 return TPResult::Error;
1164 case ANK_TentativeDecl:
1165 return TPResult::False;
1166 case ANK_TemplateName:
1167 // A bare type template-name which can't be a template template
1168 // argument is an error, and was probably intended to be a type.
1169 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1170 case ANK_Unresolved:
1171 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
1175 assert(Tok.isNot(tok::identifier) &&
1176 "TryAnnotateName succeeded without producing an annotation");
1178 // This might possibly be a type with a dependent scope specifier and
1179 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1180 // since it will annotate as a primary expression, and we want to use the
1181 // "missing 'typename'" logic.
1182 if (TryAnnotateTypeOrScopeToken())
1183 return TPResult::Error;
1184 // If annotation failed, assume it's a non-type.
1185 // FIXME: If this happens due to an undeclared identifier, treat it as
1187 if (Tok.is(tok::identifier))
1188 return TPResult::False;
1191 // We annotated this token as something. Recurse to handle whatever we got.
1192 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1195 case tok::kw_typename: // typename T::type
1196 // Annotate typenames and C++ scope specifiers. If we get one, just
1197 // recurse to handle whatever we get.
1198 if (TryAnnotateTypeOrScopeToken())
1199 return TPResult::Error;
1200 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1202 case tok::coloncolon: { // ::foo::bar
1203 const Token &Next = NextToken();
1204 if (Next.is(tok::kw_new) || // ::new
1205 Next.is(tok::kw_delete)) // ::delete
1206 return TPResult::False;
1209 case tok::kw___super:
1210 case tok::kw_decltype:
1211 // Annotate typenames and C++ scope specifiers. If we get one, just
1212 // recurse to handle whatever we get.
1213 if (TryAnnotateTypeOrScopeToken())
1214 return TPResult::Error;
1215 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1218 // storage-class-specifier
1220 // function-specifier
1224 case tok::kw_friend:
1225 case tok::kw_typedef:
1226 case tok::kw_constexpr:
1227 // storage-class-specifier
1228 case tok::kw_register:
1229 case tok::kw_static:
1230 case tok::kw_extern:
1231 case tok::kw_mutable:
1233 case tok::kw___thread:
1234 case tok::kw_thread_local:
1235 case tok::kw__Thread_local:
1236 // function-specifier
1237 case tok::kw_inline:
1238 case tok::kw_virtual:
1239 case tok::kw_explicit:
1242 case tok::kw___module_private__:
1245 case tok::kw___unknown_anytype:
1248 // simple-type-specifier
1251 // elaborated-type-specifier
1252 // typename-specifier
1256 // elaborated-type-specifier
1258 case tok::kw_struct:
1260 case tok::kw___interface:
1265 case tok::kw_volatile:
1268 case tok::kw_restrict:
1269 case tok::kw__Complex:
1270 case tok::kw___attribute:
1271 return TPResult::True;
1274 case tok::kw___declspec:
1275 case tok::kw___cdecl:
1276 case tok::kw___stdcall:
1277 case tok::kw___fastcall:
1278 case tok::kw___thiscall:
1279 case tok::kw___vectorcall:
1281 case tok::kw___sptr:
1282 case tok::kw___uptr:
1283 case tok::kw___ptr64:
1284 case tok::kw___ptr32:
1285 case tok::kw___forceinline:
1286 case tok::kw___unaligned:
1287 return TPResult::True;
1290 case tok::kw___pascal:
1291 return TPResult::True;
1294 case tok::kw___vector:
1295 return TPResult::True;
1297 case tok::annot_template_id: {
1298 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1299 if (TemplateId->Kind != TNK_Type_template)
1300 return TPResult::False;
1302 AnnotateTemplateIdTokenAsType();
1303 assert(Tok.is(tok::annot_typename));
1307 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1308 // We've already annotated a scope; try to annotate a type.
1309 if (TryAnnotateTypeOrScopeToken())
1310 return TPResult::Error;
1311 if (!Tok.is(tok::annot_typename)) {
1312 // If the next token is an identifier or a type qualifier, then this
1313 // can't possibly be a valid expression either.
1314 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1316 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1317 Tok.getAnnotationRange(),
1319 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1320 TentativeParsingAction PA(*this);
1323 bool isIdentifier = Tok.is(tok::identifier);
1324 TPResult TPR = TPResult::False;
1326 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1327 HasMissingTypename);
1331 TPR == TPResult::True || TPR == TPResult::Error)
1332 return TPResult::Error;
1334 if (HasMissingTypename) {
1335 // We can't tell whether this is a missing 'typename' or a valid
1337 *HasMissingTypename = true;
1338 return TPResult::Ambiguous;
1341 // Try to resolve the name. If it doesn't exist, assume it was
1342 // intended to name a type and keep disambiguating.
1343 switch (TryAnnotateName(false /* SS is not dependent */)) {
1345 return TPResult::Error;
1346 case ANK_TentativeDecl:
1347 return TPResult::False;
1348 case ANK_TemplateName:
1349 // A bare type template-name which can't be a template template
1350 // argument is an error, and was probably intended to be a type.
1351 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1352 case ANK_Unresolved:
1353 return HasMissingTypename ? TPResult::Ambiguous
1356 // Annotated it, check again.
1357 assert(Tok.isNot(tok::annot_cxxscope) ||
1358 NextToken().isNot(tok::identifier));
1359 return isCXXDeclarationSpecifier(BracedCastResult,
1360 HasMissingTypename);
1364 return TPResult::False;
1366 // If that succeeded, fallthrough into the generic simple-type-id case.
1368 // The ambiguity resides in a simple-type-specifier/typename-specifier
1369 // followed by a '('. The '(' could either be the start of:
1371 // direct-declarator:
1372 // '(' declarator ')'
1374 // direct-abstract-declarator:
1375 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1376 // exception-specification[opt]
1377 // '(' abstract-declarator ')'
1379 // or part of a function-style cast expression:
1381 // simple-type-specifier '(' expression-list[opt] ')'
1384 // simple-type-specifier:
1386 case tok::annot_typename:
1388 // In Objective-C, we might have a protocol-qualified type.
1389 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1390 // Tentatively parse the
1391 TentativeParsingAction PA(*this);
1392 ConsumeToken(); // The type token
1394 TPResult TPR = TryParseProtocolQualifiers();
1395 bool isFollowedByParen = Tok.is(tok::l_paren);
1396 bool isFollowedByBrace = Tok.is(tok::l_brace);
1400 if (TPR == TPResult::Error)
1401 return TPResult::Error;
1403 if (isFollowedByParen)
1404 return TPResult::Ambiguous;
1406 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1407 return BracedCastResult;
1409 return TPResult::True;
1413 case tok::kw_wchar_t:
1414 case tok::kw_char16_t:
1415 case tok::kw_char32_t:
1420 case tok::kw___int64:
1421 case tok::kw___int128:
1422 case tok::kw_signed:
1423 case tok::kw_unsigned:
1426 case tok::kw_double:
1428 case tok::annot_decltype:
1429 if (NextToken().is(tok::l_paren))
1430 return TPResult::Ambiguous;
1432 // This is a function-style cast in all cases we disambiguate other than
1435 // enum E : int { a = 4 }; // enum
1436 // enum E : int { 4 }; // bit-field
1438 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1439 return BracedCastResult;
1441 if (isStartOfObjCClassMessageMissingOpenBracket())
1442 return TPResult::False;
1444 return TPResult::True;
1446 // GNU typeof support.
1447 case tok::kw_typeof: {
1448 if (NextToken().isNot(tok::l_paren))
1449 return TPResult::True;
1451 TentativeParsingAction PA(*this);
1453 TPResult TPR = TryParseTypeofSpecifier();
1454 bool isFollowedByParen = Tok.is(tok::l_paren);
1455 bool isFollowedByBrace = Tok.is(tok::l_brace);
1459 if (TPR == TPResult::Error)
1460 return TPResult::Error;
1462 if (isFollowedByParen)
1463 return TPResult::Ambiguous;
1465 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1466 return BracedCastResult;
1468 return TPResult::True;
1471 // C++0x type traits support
1472 case tok::kw___underlying_type:
1473 return TPResult::True;
1476 case tok::kw__Atomic:
1477 return TPResult::True;
1480 return TPResult::False;
1484 bool Parser::isCXXDeclarationSpecifierAType() {
1485 switch (Tok.getKind()) {
1486 // typename-specifier
1487 case tok::annot_decltype:
1488 case tok::annot_template_id:
1489 case tok::annot_typename:
1490 case tok::kw_typeof:
1491 case tok::kw___underlying_type:
1494 // elaborated-type-specifier
1496 case tok::kw_struct:
1498 case tok::kw___interface:
1502 // simple-type-specifier
1504 case tok::kw_wchar_t:
1505 case tok::kw_char16_t:
1506 case tok::kw_char32_t:
1511 case tok::kw___int64:
1512 case tok::kw___int128:
1513 case tok::kw_signed:
1514 case tok::kw_unsigned:
1517 case tok::kw_double:
1519 case tok::kw___unknown_anytype:
1523 return getLangOpts().CPlusPlus11;
1525 case tok::kw__Atomic:
1527 return NextToken().is(tok::l_paren);
1534 /// [GNU] typeof-specifier:
1535 /// 'typeof' '(' expressions ')'
1536 /// 'typeof' '(' type-name ')'
1538 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1539 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1542 assert(Tok.is(tok::l_paren) && "Expected '('");
1543 // Parse through the parens after 'typeof'.
1545 if (!SkipUntil(tok::r_paren, StopAtSemi))
1546 return TPResult::Error;
1548 return TPResult::Ambiguous;
1551 /// [ObjC] protocol-qualifiers:
1552 //// '<' identifier-list '>'
1553 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1554 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1557 if (Tok.isNot(tok::identifier))
1558 return TPResult::Error;
1561 if (Tok.is(tok::comma)) {
1566 if (Tok.is(tok::greater)) {
1568 return TPResult::Ambiguous;
1572 return TPResult::Error;
1575 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1576 /// a constructor-style initializer, when parsing declaration statements.
1577 /// Returns true for function declarator and false for constructor-style
1579 /// If during the disambiguation process a parsing error is encountered,
1580 /// the function returns true to let the declaration parsing code handle it.
1582 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1583 /// exception-specification[opt]
1585 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1588 // The ambiguity arising from the similarity between a function-style cast and
1589 // a declaration mentioned in 6.8 can also occur in the context of a
1590 // declaration. In that context, the choice is between a function declaration
1591 // with a redundant set of parentheses around a parameter name and an object
1592 // declaration with a function-style cast as the initializer. Just as for the
1593 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1594 // that could possibly be a declaration a declaration.
1596 TentativeParsingAction PA(*this);
1599 bool InvalidAsDeclaration = false;
1600 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1601 if (TPR == TPResult::Ambiguous) {
1602 if (Tok.isNot(tok::r_paren))
1603 TPR = TPResult::False;
1605 const Token &Next = NextToken();
1606 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1607 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1608 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1609 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
1610 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1611 Next.is(tok::equal) || Next.is(tok::arrow))
1612 // The next token cannot appear after a constructor-style initializer,
1613 // and can appear next in a function definition. This must be a function
1615 TPR = TPResult::True;
1616 else if (InvalidAsDeclaration)
1617 // Use the absence of 'typename' as a tie-breaker.
1618 TPR = TPResult::False;
1624 if (IsAmbiguous && TPR == TPResult::Ambiguous)
1625 *IsAmbiguous = true;
1627 // In case of an error, let the declaration parsing code handle it.
1628 return TPR != TPResult::False;
1631 /// parameter-declaration-clause:
1632 /// parameter-declaration-list[opt] '...'[opt]
1633 /// parameter-declaration-list ',' '...'
1635 /// parameter-declaration-list:
1636 /// parameter-declaration
1637 /// parameter-declaration-list ',' parameter-declaration
1639 /// parameter-declaration:
1640 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1641 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1642 /// '=' assignment-expression
1643 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1645 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1646 /// attributes[opt] '=' assignment-expression
1649 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1650 bool VersusTemplateArgument) {
1652 if (Tok.is(tok::r_paren))
1653 return TPResult::Ambiguous;
1655 // parameter-declaration-list[opt] '...'[opt]
1656 // parameter-declaration-list ',' '...'
1658 // parameter-declaration-list:
1659 // parameter-declaration
1660 // parameter-declaration-list ',' parameter-declaration
1664 if (Tok.is(tok::ellipsis)) {
1666 if (Tok.is(tok::r_paren))
1667 return TPResult::True; // '...)' is a sign of a function declarator.
1669 return TPResult::False;
1672 // An attribute-specifier-seq here is a sign of a function declarator.
1673 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1674 /*OuterMightBeMessageSend*/true))
1675 return TPResult::True;
1677 ParsedAttributes attrs(AttrFactory);
1678 MaybeParseMicrosoftAttributes(attrs);
1680 // decl-specifier-seq
1681 // A parameter-declaration's initializer must be preceded by an '=', so
1682 // decl-specifier-seq '{' is not a parameter in C++11.
1683 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1684 InvalidAsDeclaration);
1686 if (VersusTemplateArgument && TPR == TPResult::True) {
1687 // Consume the decl-specifier-seq. We have to look past it, since a
1688 // type-id might appear here in a template argument.
1689 bool SeenType = false;
1691 SeenType |= isCXXDeclarationSpecifierAType();
1692 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1693 return TPResult::Error;
1695 // If we see a parameter name, this can't be a template argument.
1696 if (SeenType && Tok.is(tok::identifier))
1697 return TPResult::True;
1699 TPR = isCXXDeclarationSpecifier(TPResult::False,
1700 InvalidAsDeclaration);
1701 if (TPR == TPResult::Error)
1703 } while (TPR != TPResult::False);
1704 } else if (TPR == TPResult::Ambiguous) {
1705 // Disambiguate what follows the decl-specifier.
1706 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1707 return TPResult::Error;
1712 // abstract-declarator[opt]
1713 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1714 if (TPR != TPResult::Ambiguous)
1717 // [GNU] attributes[opt]
1718 if (Tok.is(tok::kw___attribute))
1719 return TPResult::True;
1721 // If we're disambiguating a template argument in a default argument in
1722 // a class definition versus a parameter declaration, an '=' here
1723 // disambiguates the parse one way or the other.
1724 // If this is a parameter, it must have a default argument because
1725 // (a) the previous parameter did, and
1726 // (b) this must be the first declaration of the function, so we can't
1727 // inherit any default arguments from elsewhere.
1728 // If we see an ')', then we've reached the end of a
1729 // parameter-declaration-clause, and the last param is missing its default
1731 if (VersusTemplateArgument)
1732 return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True
1735 if (Tok.is(tok::equal)) {
1736 // '=' assignment-expression
1737 // Parse through assignment-expression.
1738 // FIXME: assignment-expression may contain an unparenthesized comma.
1739 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
1740 return TPResult::Error;
1743 if (Tok.is(tok::ellipsis)) {
1745 if (Tok.is(tok::r_paren))
1746 return TPResult::True; // '...)' is a sign of a function declarator.
1748 return TPResult::False;
1751 if (!TryConsumeToken(tok::comma))
1755 return TPResult::Ambiguous;
1758 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1759 /// parsing as a function declarator.
1760 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1761 /// return TPResult::Ambiguous, otherwise it will return either False() or
1764 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1765 /// exception-specification[opt]
1767 /// exception-specification:
1768 /// 'throw' '(' type-id-list[opt] ')'
1770 Parser::TPResult Parser::TryParseFunctionDeclarator() {
1772 // The '(' is already parsed.
1774 TPResult TPR = TryParseParameterDeclarationClause();
1775 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1776 TPR = TPResult::False;
1778 if (TPR == TPResult::False || TPR == TPResult::Error)
1781 // Parse through the parens.
1782 if (!SkipUntil(tok::r_paren, StopAtSemi))
1783 return TPResult::Error;
1786 while (Tok.is(tok::kw_const) ||
1787 Tok.is(tok::kw_volatile) ||
1788 Tok.is(tok::kw_restrict) )
1791 // ref-qualifier[opt]
1792 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1795 // exception-specification
1796 if (Tok.is(tok::kw_throw)) {
1798 if (Tok.isNot(tok::l_paren))
1799 return TPResult::Error;
1801 // Parse through the parens after 'throw'.
1803 if (!SkipUntil(tok::r_paren, StopAtSemi))
1804 return TPResult::Error;
1806 if (Tok.is(tok::kw_noexcept)) {
1808 // Possibly an expression as well.
1809 if (Tok.is(tok::l_paren)) {
1810 // Find the matching rparen.
1812 if (!SkipUntil(tok::r_paren, StopAtSemi))
1813 return TPResult::Error;
1817 return TPResult::Ambiguous;
1820 /// '[' constant-expression[opt] ']'
1822 Parser::TPResult Parser::TryParseBracketDeclarator() {
1824 if (!SkipUntil(tok::r_square, StopAtSemi))
1825 return TPResult::Error;
1827 return TPResult::Ambiguous;