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
112 // TPResult::Error().
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 (TryAnnotateCXXScopeToken())
199 return TPResult::Error();
200 if (Tok.is(tok::annot_cxxscope))
202 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
203 return TPResult::Error();
207 case tok::annot_cxxscope:
213 if (getLangOpts().ObjC1 && Tok.is(tok::less))
214 return TryParseProtocolQualifiers();
218 return TPResult::Ambiguous();
221 /// simple-declaration:
222 /// decl-specifier-seq init-declarator-list[opt] ';'
224 /// (if AllowForRangeDecl specified)
225 /// for ( for-range-declaration : for-range-initializer ) statement
226 /// for-range-declaration:
227 /// attribute-specifier-seqopt type-specifier-seq declarator
229 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
230 if (TryConsumeDeclarationSpecifier() == TPResult::Error())
231 return TPResult::Error();
233 // Two decl-specifiers in a row conclusively disambiguate this as being a
234 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
235 // overwhelmingly common case that the next token is a '('.
236 if (Tok.isNot(tok::l_paren)) {
237 TPResult TPR = isCXXDeclarationSpecifier();
238 if (TPR == TPResult::Ambiguous())
239 return TPResult::True();
240 if (TPR == TPResult::True() || TPR == TPResult::Error())
242 assert(TPR == TPResult::False());
245 TPResult TPR = TryParseInitDeclaratorList();
246 if (TPR != TPResult::Ambiguous())
249 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
250 return TPResult::False();
252 return TPResult::Ambiguous();
255 /// Tentatively parse an init-declarator-list in order to disambiguate it from
258 /// init-declarator-list:
260 /// init-declarator-list ',' init-declarator
263 /// declarator initializer[opt]
264 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
267 /// brace-or-equal-initializer
268 /// '(' expression-list ')'
270 /// brace-or-equal-initializer:
271 /// '=' initializer-clause
272 /// [C++11] braced-init-list
274 /// initializer-clause:
275 /// assignment-expression
278 /// braced-init-list:
279 /// '{' initializer-list ','[opt] '}'
282 Parser::TPResult Parser::TryParseInitDeclaratorList() {
285 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
286 if (TPR != TPResult::Ambiguous())
289 // [GNU] simple-asm-expr[opt] attributes[opt]
290 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
291 return TPResult::True();
294 if (Tok.is(tok::l_paren)) {
295 // Parse through the parens.
297 if (!SkipUntil(tok::r_paren, StopAtSemi))
298 return TPResult::Error();
299 } else if (Tok.is(tok::l_brace)) {
300 // A left-brace here is sufficient to disambiguate the parse; an
301 // expression can never be followed directly by a braced-init-list.
302 return TPResult::True();
303 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
304 // MSVC and g++ won't examine the rest of declarators if '=' is
305 // encountered; they just conclude that we have a declaration.
306 // EDG parses the initializer completely, which is the proper behavior
309 // At present, Clang follows MSVC and g++, since the parser does not have
310 // the ability to parse an expression fully without recording the
311 // results of that parse.
312 // FIXME: Handle this case correctly.
314 // Also allow 'in' after an Objective-C declaration as in:
315 // for (int (^b)(void) in array). Ideally this should be done in the
316 // context of parsing for-init-statement of a foreach statement only. But,
317 // in any other context 'in' is invalid after a declaration and parser
318 // issues the error regardless of outcome of this decision.
319 // FIXME: Change if above assumption does not hold.
320 return TPResult::True();
323 if (Tok.isNot(tok::comma))
325 ConsumeToken(); // the comma.
328 return TPResult::Ambiguous();
331 /// isCXXConditionDeclaration - Disambiguates between a declaration or an
332 /// expression for a condition of a if/switch/while/for statement.
333 /// If during the disambiguation process a parsing error is encountered,
334 /// the function returns true to let the declaration parsing code handle it.
338 /// type-specifier-seq declarator '=' assignment-expression
339 /// [C++11] type-specifier-seq declarator '=' initializer-clause
340 /// [C++11] type-specifier-seq declarator braced-init-list
341 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
342 /// '=' assignment-expression
344 bool Parser::isCXXConditionDeclaration() {
345 TPResult TPR = isCXXDeclarationSpecifier();
346 if (TPR != TPResult::Ambiguous())
347 return TPR != TPResult::False(); // Returns true for TPResult::True() or
348 // TPResult::Error().
350 // FIXME: Add statistics about the number of ambiguous statements encountered
351 // and how they were resolved (number of declarations+number of expressions).
353 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
354 // We need tentative parsing...
356 TentativeParsingAction PA(*this);
358 // type-specifier-seq
359 TryConsumeDeclarationSpecifier();
360 assert(Tok.is(tok::l_paren) && "Expected '('");
363 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
365 // In case of an error, let the declaration parsing code handle it.
366 if (TPR == TPResult::Error())
367 TPR = TPResult::True();
369 if (TPR == TPResult::Ambiguous()) {
371 // [GNU] simple-asm-expr[opt] attributes[opt]
372 if (Tok.is(tok::equal) ||
373 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
374 TPR = TPResult::True();
375 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
376 TPR = TPResult::True();
378 TPR = TPResult::False();
383 assert(TPR == TPResult::True() || TPR == TPResult::False());
384 return TPR == TPResult::True();
387 /// \brief Determine whether the next set of tokens contains a type-id.
389 /// The context parameter states what context we're parsing right
390 /// now, which affects how this routine copes with the token
391 /// following the type-id. If the context is TypeIdInParens, we have
392 /// already parsed the '(' and we will cease lookahead when we hit
393 /// the corresponding ')'. If the context is
394 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
395 /// before this template argument, and will cease lookahead when we
396 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
397 /// and false for an expression. If during the disambiguation
398 /// process a parsing error is encountered, the function returns
399 /// true to let the declaration parsing code handle it.
402 /// type-specifier-seq abstract-declarator[opt]
404 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
409 // The ambiguity arising from the similarity between a function-style cast and
410 // a type-id can occur in different contexts. The ambiguity appears as a
411 // choice between a function-style cast expression and a declaration of a
412 // type. The resolution is that any construct that could possibly be a type-id
413 // in its syntactic context shall be considered a type-id.
415 TPResult TPR = isCXXDeclarationSpecifier();
416 if (TPR != TPResult::Ambiguous())
417 return TPR != TPResult::False(); // Returns true for TPResult::True() or
418 // TPResult::Error().
420 // FIXME: Add statistics about the number of ambiguous statements encountered
421 // and how they were resolved (number of declarations+number of expressions).
423 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
424 // We need tentative parsing...
426 TentativeParsingAction PA(*this);
428 // type-specifier-seq
429 TryConsumeDeclarationSpecifier();
430 assert(Tok.is(tok::l_paren) && "Expected '('");
433 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
435 // In case of an error, let the declaration parsing code handle it.
436 if (TPR == TPResult::Error())
437 TPR = TPResult::True();
439 if (TPR == TPResult::Ambiguous()) {
440 // We are supposed to be inside parens, so if after the abstract declarator
441 // we encounter a ')' this is a type-id, otherwise it's an expression.
442 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
443 TPR = TPResult::True();
446 // We are supposed to be inside a template argument, so if after
447 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
448 // ',', this is a type-id. Otherwise, it's an expression.
449 } else if (Context == TypeIdAsTemplateArgument &&
450 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
451 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
452 TPR = TPResult::True();
456 TPR = TPResult::False();
461 assert(TPR == TPResult::True() || TPR == TPResult::False());
462 return TPR == TPResult::True();
465 /// \brief Returns true if this is a C++11 attribute-specifier. Per
466 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
467 /// always introduce an attribute. In Objective-C++11, this rule does not
468 /// apply if either '[' begins a message-send.
470 /// If Disambiguate is true, we try harder to determine whether a '[[' starts
471 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
473 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
474 /// Obj-C message send or the start of an attribute. Otherwise, we assume it
475 /// is not an Obj-C message send.
477 /// C++11 [dcl.attr.grammar]:
479 /// attribute-specifier:
480 /// '[' '[' attribute-list ']' ']'
481 /// alignment-specifier
485 /// attribute-list ',' attribute[opt]
487 /// attribute-list ',' attribute '...'
490 /// attribute-token attribute-argument-clause[opt]
494 /// identifier '::' identifier
496 /// attribute-argument-clause:
497 /// '(' balanced-token-seq ')'
498 Parser::CXX11AttributeKind
499 Parser::isCXX11AttributeSpecifier(bool Disambiguate,
500 bool OuterMightBeMessageSend) {
501 if (Tok.is(tok::kw_alignas))
502 return CAK_AttributeSpecifier;
504 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
505 return CAK_NotAttributeSpecifier;
507 // No tentative parsing if we don't need to look for ']]' or a lambda.
508 if (!Disambiguate && !getLangOpts().ObjC1)
509 return CAK_AttributeSpecifier;
511 TentativeParsingAction PA(*this);
513 // Opening brackets were checked for above.
516 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
517 if (!getLangOpts().ObjC1) {
520 bool IsAttribute = SkipUntil(tok::r_square);
521 IsAttribute &= Tok.is(tok::r_square);
525 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
528 // In Obj-C++11, we need to distinguish four situations:
529 // 1a) int x[[attr]]; C++11 attribute.
530 // 1b) [[attr]]; C++11 statement attribute.
531 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
532 // 3a) int x[[obj get]]; Message send in array size/index.
533 // 3b) [[Class alloc] init]; Message send in message send.
534 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
535 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
537 // If we have a lambda-introducer, then this is definitely not a message send.
538 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
539 // into the tentative attribute parse below.
540 LambdaIntroducer Intro;
541 if (!TryParseLambdaIntroducer(Intro)) {
542 // A lambda cannot end with ']]', and an attribute must.
543 bool IsAttribute = Tok.is(tok::r_square);
548 // Case 1: C++11 attribute.
549 return CAK_AttributeSpecifier;
551 if (OuterMightBeMessageSend)
552 // Case 4: Lambda in message send.
553 return CAK_NotAttributeSpecifier;
555 // Case 2: Lambda in array size / index.
556 return CAK_InvalidAttributeSpecifier;
561 // If we don't have a lambda-introducer, then we have an attribute or a
563 bool IsAttribute = true;
564 while (Tok.isNot(tok::r_square)) {
565 if (Tok.is(tok::comma)) {
566 // Case 1: Stray commas can only occur in attributes.
568 return CAK_AttributeSpecifier;
571 // Parse the attribute-token, if present.
572 // C++11 [dcl.attr.grammar]:
573 // If a keyword or an alternative token that satisfies the syntactic
574 // requirements of an identifier is contained in an attribute-token,
575 // it is considered an identifier.
577 if (!TryParseCXX11AttributeIdentifier(Loc)) {
581 if (Tok.is(tok::coloncolon)) {
583 if (!TryParseCXX11AttributeIdentifier(Loc)) {
589 // Parse the attribute-argument-clause, if present.
590 if (Tok.is(tok::l_paren)) {
592 if (!SkipUntil(tok::r_paren)) {
598 if (Tok.is(tok::ellipsis))
601 if (Tok.isNot(tok::comma))
607 // An attribute must end ']]'.
609 if (Tok.is(tok::r_square)) {
611 IsAttribute = Tok.is(tok::r_square);
620 // Case 1: C++11 statement attribute.
621 return CAK_AttributeSpecifier;
623 // Case 3: Message send.
624 return CAK_NotAttributeSpecifier;
627 Parser::TPResult Parser::TryParsePtrOperatorSeq() {
629 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
630 if (TryAnnotateCXXScopeToken(true))
631 return TPResult::Error();
633 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
634 Tok.is(tok::ampamp) ||
635 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
638 while (Tok.is(tok::kw_const) ||
639 Tok.is(tok::kw_volatile) ||
640 Tok.is(tok::kw_restrict))
643 return TPResult::True();
648 /// operator-function-id:
649 /// 'operator' operator
652 /// new delete new[] delete[] + - * / % ^ [...]
654 /// conversion-function-id:
655 /// 'operator' conversion-type-id
657 /// conversion-type-id:
658 /// type-specifier-seq conversion-declarator[opt]
660 /// conversion-declarator:
661 /// ptr-operator conversion-declarator[opt]
663 /// literal-operator-id:
664 /// 'operator' string-literal identifier
665 /// 'operator' user-defined-string-literal
666 Parser::TPResult Parser::TryParseOperatorId() {
667 assert(Tok.is(tok::kw_operator));
670 // Maybe this is an operator-function-id.
671 switch (Tok.getKind()) {
672 case tok::kw_new: case tok::kw_delete:
674 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
678 return TPResult::True();
680 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
682 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
683 #include "clang/Basic/OperatorKinds.def"
685 return TPResult::True();
688 if (NextToken().is(tok::r_square)) {
691 return TPResult::True();
696 if (NextToken().is(tok::r_paren)) {
699 return TPResult::True();
707 // Maybe this is a literal-operator-id.
708 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
709 bool FoundUDSuffix = false;
711 FoundUDSuffix |= Tok.hasUDSuffix();
712 ConsumeStringToken();
713 } while (isTokenStringLiteral());
715 if (!FoundUDSuffix) {
716 if (Tok.is(tok::identifier))
719 return TPResult::Error();
721 return TPResult::True();
724 // Maybe this is a conversion-function-id.
725 bool AnyDeclSpecifiers = false;
727 TPResult TPR = isCXXDeclarationSpecifier();
728 if (TPR == TPResult::Error())
730 if (TPR == TPResult::False()) {
731 if (!AnyDeclSpecifiers)
732 return TPResult::Error();
735 if (TryConsumeDeclarationSpecifier() == TPResult::Error())
736 return TPResult::Error();
737 AnyDeclSpecifiers = true;
739 return TryParsePtrOperatorSeq();
743 /// direct-declarator
744 /// ptr-operator declarator
746 /// direct-declarator:
748 /// direct-declarator '(' parameter-declaration-clause ')'
749 /// cv-qualifier-seq[opt] exception-specification[opt]
750 /// direct-declarator '[' constant-expression[opt] ']'
751 /// '(' declarator ')'
752 /// [GNU] '(' attributes declarator ')'
754 /// abstract-declarator:
755 /// ptr-operator abstract-declarator[opt]
756 /// direct-abstract-declarator
759 /// direct-abstract-declarator:
760 /// direct-abstract-declarator[opt]
761 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
762 /// exception-specification[opt]
763 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
764 /// '(' abstract-declarator ')'
767 /// '*' cv-qualifier-seq[opt]
769 /// [C++0x] '&&' [TODO]
770 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
772 /// cv-qualifier-seq:
773 /// cv-qualifier cv-qualifier-seq[opt]
780 /// '...'[opt] id-expression
784 /// qualified-id [TODO]
788 /// operator-function-id
789 /// conversion-function-id
790 /// literal-operator-id
791 /// '~' class-name [TODO]
792 /// '~' decltype-specifier [TODO]
793 /// template-id [TODO]
795 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
796 bool mayHaveIdentifier) {
799 // ptr-operator declarator
800 if (TryParsePtrOperatorSeq() == TPResult::Error())
801 return TPResult::Error();
803 // direct-declarator:
804 // direct-abstract-declarator:
805 if (Tok.is(tok::ellipsis))
808 if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
809 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
810 NextToken().is(tok::kw_operator)))) &&
813 if (Tok.is(tok::annot_cxxscope))
815 else if (Tok.is(tok::identifier))
816 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
817 if (Tok.is(tok::kw_operator)) {
818 if (TryParseOperatorId() == TPResult::Error())
819 return TPResult::Error();
822 } else if (Tok.is(tok::l_paren)) {
825 (Tok.is(tok::r_paren) || // 'int()' is a function.
826 // 'int(...)' is a function.
827 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
828 isDeclarationSpecifier())) { // 'int(int)' is a function.
829 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
830 // exception-specification[opt]
831 TPResult TPR = TryParseFunctionDeclarator();
832 if (TPR != TPResult::Ambiguous())
835 // '(' declarator ')'
836 // '(' attributes declarator ')'
837 // '(' abstract-declarator ')'
838 if (Tok.is(tok::kw___attribute) ||
839 Tok.is(tok::kw___declspec) ||
840 Tok.is(tok::kw___cdecl) ||
841 Tok.is(tok::kw___stdcall) ||
842 Tok.is(tok::kw___fastcall) ||
843 Tok.is(tok::kw___thiscall) ||
844 Tok.is(tok::kw___unaligned))
845 return TPResult::True(); // attributes indicate declaration
846 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
847 if (TPR != TPResult::Ambiguous())
849 if (Tok.isNot(tok::r_paren))
850 return TPResult::False();
853 } else if (!mayBeAbstract) {
854 return TPResult::False();
858 TPResult TPR(TPResult::Ambiguous());
860 // abstract-declarator: ...
861 if (Tok.is(tok::ellipsis))
864 if (Tok.is(tok::l_paren)) {
865 // Check whether we have a function declarator or a possible ctor-style
866 // initializer that follows the declarator. Note that ctor-style
867 // initializers are not possible in contexts where abstract declarators
869 if (!mayBeAbstract && !isCXXFunctionDeclarator())
872 // direct-declarator '(' parameter-declaration-clause ')'
873 // cv-qualifier-seq[opt] exception-specification[opt]
875 TPR = TryParseFunctionDeclarator();
876 } else if (Tok.is(tok::l_square)) {
877 // direct-declarator '[' constant-expression[opt] ']'
878 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
879 TPR = TryParseBracketDeclarator();
884 if (TPR != TPResult::Ambiguous())
888 return TPResult::Ambiguous();
892 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
894 // Obviously starts an expression.
895 case tok::numeric_constant:
896 case tok::char_constant:
897 case tok::wide_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_types_compatible_p:
939 case tok::kw___builtin_va_arg:
942 case tok::kw___FUNCTION__:
943 case tok::kw___FUNCDNAME__:
944 case tok::kw_L__FUNCTION__:
945 case tok::kw___PRETTY_FUNCTION__:
946 case tok::kw___has_nothrow_assign:
947 case tok::kw___has_nothrow_copy:
948 case tok::kw___has_nothrow_constructor:
949 case tok::kw___has_trivial_assign:
950 case tok::kw___has_trivial_copy:
951 case tok::kw___has_trivial_constructor:
952 case tok::kw___has_trivial_destructor:
953 case tok::kw___has_virtual_destructor:
954 case tok::kw___is_abstract:
955 case tok::kw___is_base_of:
956 case tok::kw___is_class:
957 case tok::kw___is_convertible_to:
958 case tok::kw___is_empty:
959 case tok::kw___is_enum:
960 case tok::kw___is_interface_class:
961 case tok::kw___is_final:
962 case tok::kw___is_literal:
963 case tok::kw___is_literal_type:
964 case tok::kw___is_pod:
965 case tok::kw___is_polymorphic:
966 case tok::kw___is_sealed:
967 case tok::kw___is_trivial:
968 case tok::kw___is_trivially_assignable:
969 case tok::kw___is_trivially_constructible:
970 case tok::kw___is_trivially_copyable:
971 case tok::kw___is_union:
972 case tok::kw___uuidof:
973 return TPResult::True();
975 // Obviously starts a type-specifier-seq:
984 case tok::kw___int64:
985 case tok::kw___int128:
986 case tok::kw_restrict:
991 case tok::kw_unsigned:
993 case tok::kw_volatile:
995 case tok::kw__Complex:
997 case tok::kw_typename:
998 case tok::kw_wchar_t:
999 case tok::kw_char16_t:
1000 case tok::kw_char32_t:
1001 case tok::kw__Decimal32:
1002 case tok::kw__Decimal64:
1003 case tok::kw__Decimal128:
1004 case tok::kw___interface:
1005 case tok::kw___thread:
1006 case tok::kw_thread_local:
1007 case tok::kw__Thread_local:
1008 case tok::kw_typeof:
1009 case tok::kw___underlying_type:
1010 case tok::kw___cdecl:
1011 case tok::kw___stdcall:
1012 case tok::kw___fastcall:
1013 case tok::kw___thiscall:
1014 case tok::kw___unaligned:
1015 case tok::kw___vector:
1016 case tok::kw___pixel:
1017 case tok::kw__Atomic:
1018 case tok::kw_image1d_t:
1019 case tok::kw_image1d_array_t:
1020 case tok::kw_image1d_buffer_t:
1021 case tok::kw_image2d_t:
1022 case tok::kw_image2d_array_t:
1023 case tok::kw_image3d_t:
1024 case tok::kw_sampler_t:
1025 case tok::kw_event_t:
1026 case tok::kw___unknown_anytype:
1027 return TPResult::False();
1033 return TPResult::Ambiguous();
1036 bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1037 return std::find(TentativelyDeclaredIdentifiers.begin(),
1038 TentativelyDeclaredIdentifiers.end(), II)
1039 != TentativelyDeclaredIdentifiers.end();
1042 /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
1043 /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
1044 /// be either a decl-specifier or a function-style cast, and TPResult::Error()
1045 /// if a parsing error was found and reported.
1047 /// If HasMissingTypename is provided, a name with a dependent scope specifier
1048 /// will be treated as ambiguous if the 'typename' keyword is missing. If this
1049 /// happens, *HasMissingTypename will be set to 'true'. This will also be used
1050 /// as an indicator that undeclared identifiers (which will trigger a later
1051 /// parse error) should be treated as types. Returns TPResult::Ambiguous() in
1055 /// storage-class-specifier
1057 /// function-specifier
1060 /// [C++11] 'constexpr'
1061 /// [GNU] attributes declaration-specifiers[opt]
1063 /// storage-class-specifier:
1069 /// [GNU] '__thread'
1070 /// [C++11] 'thread_local'
1071 /// [C11] '_Thread_local'
1073 /// function-specifier:
1082 /// simple-type-specifier
1085 /// elaborated-type-specifier
1086 /// typename-specifier
1089 /// simple-type-specifier:
1090 /// '::'[opt] nested-name-specifier[opt] type-name
1091 /// '::'[opt] nested-name-specifier 'template'
1092 /// simple-template-id [TODO]
1104 /// [GNU] typeof-specifier
1105 /// [GNU] '_Complex'
1107 /// [C++11] 'decltype' ( expression )
1108 /// [C++1y] 'decltype' ( 'auto' )
1115 /// elaborated-type-specifier:
1116 /// class-key '::'[opt] nested-name-specifier[opt] identifier
1117 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1118 /// simple-template-id
1119 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1125 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1126 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1128 /// class-specifier:
1129 /// class-head '{' member-specification[opt] '}'
1132 /// class-key identifier[opt] base-clause[opt]
1133 /// class-key nested-name-specifier identifier base-clause[opt]
1134 /// class-key nested-name-specifier[opt] simple-template-id
1135 /// base-clause[opt]
1148 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1149 bool *HasMissingTypename) {
1150 switch (Tok.getKind()) {
1151 case tok::identifier: {
1152 // Check for need to substitute AltiVec __vector keyword
1153 // for "vector" identifier.
1154 if (TryAltiVecVectorToken())
1155 return TPResult::True();
1157 const Token &Next = NextToken();
1158 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1159 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
1160 return TPResult::True();
1162 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1163 // Determine whether this is a valid expression. If not, we will hit
1164 // a parse error one way or another. In that case, tell the caller that
1165 // this is ambiguous. Typo-correct to type and expression keywords and
1166 // to types and identifiers, in order to try to recover from errors.
1167 CorrectionCandidateCallback TypoCorrection;
1168 TypoCorrection.WantRemainingKeywords = false;
1169 TypoCorrection.WantTypeSpecifiers = Next.isNot(tok::arrow);
1170 switch (TryAnnotateName(false /* no nested name specifier */,
1173 return TPResult::Error();
1174 case ANK_TentativeDecl:
1175 return TPResult::False();
1176 case ANK_TemplateName:
1177 // A bare type template-name which can't be a template template
1178 // argument is an error, and was probably intended to be a type.
1179 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
1180 case ANK_Unresolved:
1181 return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False();
1185 assert(Tok.isNot(tok::identifier) &&
1186 "TryAnnotateName succeeded without producing an annotation");
1188 // This might possibly be a type with a dependent scope specifier and
1189 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1190 // since it will annotate as a primary expression, and we want to use the
1191 // "missing 'typename'" logic.
1192 if (TryAnnotateTypeOrScopeToken())
1193 return TPResult::Error();
1194 // If annotation failed, assume it's a non-type.
1195 // FIXME: If this happens due to an undeclared identifier, treat it as
1197 if (Tok.is(tok::identifier))
1198 return TPResult::False();
1201 // We annotated this token as something. Recurse to handle whatever we got.
1202 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1205 case tok::kw_typename: // typename T::type
1206 // Annotate typenames and C++ scope specifiers. If we get one, just
1207 // recurse to handle whatever we get.
1208 if (TryAnnotateTypeOrScopeToken())
1209 return TPResult::Error();
1210 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1212 case tok::coloncolon: { // ::foo::bar
1213 const Token &Next = NextToken();
1214 if (Next.is(tok::kw_new) || // ::new
1215 Next.is(tok::kw_delete)) // ::delete
1216 return TPResult::False();
1219 case tok::kw_decltype:
1220 // Annotate typenames and C++ scope specifiers. If we get one, just
1221 // recurse to handle whatever we get.
1222 if (TryAnnotateTypeOrScopeToken())
1223 return TPResult::Error();
1224 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1227 // storage-class-specifier
1229 // function-specifier
1233 case tok::kw_friend:
1234 case tok::kw_typedef:
1235 case tok::kw_constexpr:
1236 // storage-class-specifier
1237 case tok::kw_register:
1238 case tok::kw_static:
1239 case tok::kw_extern:
1240 case tok::kw_mutable:
1242 case tok::kw___thread:
1243 case tok::kw_thread_local:
1244 case tok::kw__Thread_local:
1245 // function-specifier
1246 case tok::kw_inline:
1247 case tok::kw_virtual:
1248 case tok::kw_explicit:
1251 case tok::kw___module_private__:
1254 case tok::kw___unknown_anytype:
1257 // simple-type-specifier
1260 // elaborated-type-specifier
1261 // typename-specifier
1265 // elaborated-type-specifier
1267 case tok::kw_struct:
1269 case tok::kw___interface:
1274 case tok::kw_volatile:
1277 case tok::kw_restrict:
1278 case tok::kw__Complex:
1279 case tok::kw___attribute:
1280 return TPResult::True();
1283 case tok::kw___declspec:
1284 case tok::kw___cdecl:
1285 case tok::kw___stdcall:
1286 case tok::kw___fastcall:
1287 case tok::kw___thiscall:
1289 case tok::kw___sptr:
1290 case tok::kw___uptr:
1291 case tok::kw___ptr64:
1292 case tok::kw___ptr32:
1293 case tok::kw___forceinline:
1294 case tok::kw___unaligned:
1295 return TPResult::True();
1298 case tok::kw___pascal:
1299 return TPResult::True();
1302 case tok::kw___vector:
1303 return TPResult::True();
1305 case tok::annot_template_id: {
1306 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1307 if (TemplateId->Kind != TNK_Type_template)
1308 return TPResult::False();
1310 AnnotateTemplateIdTokenAsType();
1311 assert(Tok.is(tok::annot_typename));
1315 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1316 // We've already annotated a scope; try to annotate a type.
1317 if (TryAnnotateTypeOrScopeToken())
1318 return TPResult::Error();
1319 if (!Tok.is(tok::annot_typename)) {
1320 // If the next token is an identifier or a type qualifier, then this
1321 // can't possibly be a valid expression either.
1322 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1324 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1325 Tok.getAnnotationRange(),
1327 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1328 TentativeParsingAction PA(*this);
1331 bool isIdentifier = Tok.is(tok::identifier);
1332 TPResult TPR = TPResult::False();
1334 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1335 HasMissingTypename);
1339 TPR == TPResult::True() || TPR == TPResult::Error())
1340 return TPResult::Error();
1342 if (HasMissingTypename) {
1343 // We can't tell whether this is a missing 'typename' or a valid
1345 *HasMissingTypename = true;
1346 return TPResult::Ambiguous();
1349 // Try to resolve the name. If it doesn't exist, assume it was
1350 // intended to name a type and keep disambiguating.
1351 switch (TryAnnotateName(false /* SS is not dependent */)) {
1353 return TPResult::Error();
1354 case ANK_TentativeDecl:
1355 return TPResult::False();
1356 case ANK_TemplateName:
1357 // A bare type template-name which can't be a template template
1358 // argument is an error, and was probably intended to be a type.
1359 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
1360 case ANK_Unresolved:
1361 return HasMissingTypename ? TPResult::Ambiguous()
1362 : TPResult::False();
1364 // Annotated it, check again.
1365 assert(Tok.isNot(tok::annot_cxxscope) ||
1366 NextToken().isNot(tok::identifier));
1367 return isCXXDeclarationSpecifier(BracedCastResult,
1368 HasMissingTypename);
1372 return TPResult::False();
1374 // If that succeeded, fallthrough into the generic simple-type-id case.
1376 // The ambiguity resides in a simple-type-specifier/typename-specifier
1377 // followed by a '('. The '(' could either be the start of:
1379 // direct-declarator:
1380 // '(' declarator ')'
1382 // direct-abstract-declarator:
1383 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1384 // exception-specification[opt]
1385 // '(' abstract-declarator ')'
1387 // or part of a function-style cast expression:
1389 // simple-type-specifier '(' expression-list[opt] ')'
1392 // simple-type-specifier:
1394 case tok::annot_typename:
1396 // In Objective-C, we might have a protocol-qualified type.
1397 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1398 // Tentatively parse the
1399 TentativeParsingAction PA(*this);
1400 ConsumeToken(); // The type token
1402 TPResult TPR = TryParseProtocolQualifiers();
1403 bool isFollowedByParen = Tok.is(tok::l_paren);
1404 bool isFollowedByBrace = Tok.is(tok::l_brace);
1408 if (TPR == TPResult::Error())
1409 return TPResult::Error();
1411 if (isFollowedByParen)
1412 return TPResult::Ambiguous();
1414 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1415 return BracedCastResult;
1417 return TPResult::True();
1421 case tok::kw_wchar_t:
1422 case tok::kw_char16_t:
1423 case tok::kw_char32_t:
1428 case tok::kw___int64:
1429 case tok::kw___int128:
1430 case tok::kw_signed:
1431 case tok::kw_unsigned:
1434 case tok::kw_double:
1436 case tok::annot_decltype:
1437 if (NextToken().is(tok::l_paren))
1438 return TPResult::Ambiguous();
1440 // This is a function-style cast in all cases we disambiguate other than
1443 // enum E : int { a = 4 }; // enum
1444 // enum E : int { 4 }; // bit-field
1446 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1447 return BracedCastResult;
1449 if (isStartOfObjCClassMessageMissingOpenBracket())
1450 return TPResult::False();
1452 return TPResult::True();
1454 // GNU typeof support.
1455 case tok::kw_typeof: {
1456 if (NextToken().isNot(tok::l_paren))
1457 return TPResult::True();
1459 TentativeParsingAction PA(*this);
1461 TPResult TPR = TryParseTypeofSpecifier();
1462 bool isFollowedByParen = Tok.is(tok::l_paren);
1463 bool isFollowedByBrace = Tok.is(tok::l_brace);
1467 if (TPR == TPResult::Error())
1468 return TPResult::Error();
1470 if (isFollowedByParen)
1471 return TPResult::Ambiguous();
1473 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1474 return BracedCastResult;
1476 return TPResult::True();
1479 // C++0x type traits support
1480 case tok::kw___underlying_type:
1481 return TPResult::True();
1484 case tok::kw__Atomic:
1485 return TPResult::True();
1488 return TPResult::False();
1492 bool Parser::isCXXDeclarationSpecifierAType() {
1493 switch (Tok.getKind()) {
1494 // typename-specifier
1495 case tok::annot_decltype:
1496 case tok::annot_template_id:
1497 case tok::annot_typename:
1498 case tok::kw_typeof:
1499 case tok::kw___underlying_type:
1502 // elaborated-type-specifier
1504 case tok::kw_struct:
1506 case tok::kw___interface:
1510 // simple-type-specifier
1512 case tok::kw_wchar_t:
1513 case tok::kw_char16_t:
1514 case tok::kw_char32_t:
1519 case tok::kw___int64:
1520 case tok::kw___int128:
1521 case tok::kw_signed:
1522 case tok::kw_unsigned:
1525 case tok::kw_double:
1527 case tok::kw___unknown_anytype:
1531 return getLangOpts().CPlusPlus11;
1533 case tok::kw__Atomic:
1535 return NextToken().is(tok::l_paren);
1542 /// [GNU] typeof-specifier:
1543 /// 'typeof' '(' expressions ')'
1544 /// 'typeof' '(' type-name ')'
1546 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1547 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1550 assert(Tok.is(tok::l_paren) && "Expected '('");
1551 // Parse through the parens after 'typeof'.
1553 if (!SkipUntil(tok::r_paren, StopAtSemi))
1554 return TPResult::Error();
1556 return TPResult::Ambiguous();
1559 /// [ObjC] protocol-qualifiers:
1560 //// '<' identifier-list '>'
1561 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1562 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1565 if (Tok.isNot(tok::identifier))
1566 return TPResult::Error();
1569 if (Tok.is(tok::comma)) {
1574 if (Tok.is(tok::greater)) {
1576 return TPResult::Ambiguous();
1580 return TPResult::Error();
1583 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1584 /// a constructor-style initializer, when parsing declaration statements.
1585 /// Returns true for function declarator and false for constructor-style
1587 /// If during the disambiguation process a parsing error is encountered,
1588 /// the function returns true to let the declaration parsing code handle it.
1590 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1591 /// exception-specification[opt]
1593 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1596 // The ambiguity arising from the similarity between a function-style cast and
1597 // a declaration mentioned in 6.8 can also occur in the context of a
1598 // declaration. In that context, the choice is between a function declaration
1599 // with a redundant set of parentheses around a parameter name and an object
1600 // declaration with a function-style cast as the initializer. Just as for the
1601 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1602 // that could possibly be a declaration a declaration.
1604 TentativeParsingAction PA(*this);
1607 bool InvalidAsDeclaration = false;
1608 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1609 if (TPR == TPResult::Ambiguous()) {
1610 if (Tok.isNot(tok::r_paren))
1611 TPR = TPResult::False();
1613 const Token &Next = NextToken();
1614 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1615 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1616 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1617 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
1618 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1619 Next.is(tok::equal) || Next.is(tok::arrow))
1620 // The next token cannot appear after a constructor-style initializer,
1621 // and can appear next in a function definition. This must be a function
1623 TPR = TPResult::True();
1624 else if (InvalidAsDeclaration)
1625 // Use the absence of 'typename' as a tie-breaker.
1626 TPR = TPResult::False();
1632 if (IsAmbiguous && TPR == TPResult::Ambiguous())
1633 *IsAmbiguous = true;
1635 // In case of an error, let the declaration parsing code handle it.
1636 return TPR != TPResult::False();
1639 /// parameter-declaration-clause:
1640 /// parameter-declaration-list[opt] '...'[opt]
1641 /// parameter-declaration-list ',' '...'
1643 /// parameter-declaration-list:
1644 /// parameter-declaration
1645 /// parameter-declaration-list ',' parameter-declaration
1647 /// parameter-declaration:
1648 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1649 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1650 /// '=' assignment-expression
1651 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1653 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1654 /// attributes[opt] '=' assignment-expression
1657 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1658 bool VersusTemplateArgument) {
1660 if (Tok.is(tok::r_paren))
1661 return TPResult::Ambiguous();
1663 // parameter-declaration-list[opt] '...'[opt]
1664 // parameter-declaration-list ',' '...'
1666 // parameter-declaration-list:
1667 // parameter-declaration
1668 // parameter-declaration-list ',' parameter-declaration
1672 if (Tok.is(tok::ellipsis)) {
1674 if (Tok.is(tok::r_paren))
1675 return TPResult::True(); // '...)' is a sign of a function declarator.
1677 return TPResult::False();
1680 // An attribute-specifier-seq here is a sign of a function declarator.
1681 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1682 /*OuterMightBeMessageSend*/true))
1683 return TPResult::True();
1685 ParsedAttributes attrs(AttrFactory);
1686 MaybeParseMicrosoftAttributes(attrs);
1688 // decl-specifier-seq
1689 // A parameter-declaration's initializer must be preceded by an '=', so
1690 // decl-specifier-seq '{' is not a parameter in C++11.
1691 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
1692 InvalidAsDeclaration);
1694 if (VersusTemplateArgument && TPR == TPResult::True()) {
1695 // Consume the decl-specifier-seq. We have to look past it, since a
1696 // type-id might appear here in a template argument.
1697 bool SeenType = false;
1699 SeenType |= isCXXDeclarationSpecifierAType();
1700 if (TryConsumeDeclarationSpecifier() == TPResult::Error())
1701 return TPResult::Error();
1703 // If we see a parameter name, this can't be a template argument.
1704 if (SeenType && Tok.is(tok::identifier))
1705 return TPResult::True();
1707 TPR = isCXXDeclarationSpecifier(TPResult::False(),
1708 InvalidAsDeclaration);
1709 if (TPR == TPResult::Error())
1711 } while (TPR != TPResult::False());
1712 } else if (TPR == TPResult::Ambiguous()) {
1713 // Disambiguate what follows the decl-specifier.
1714 if (TryConsumeDeclarationSpecifier() == TPResult::Error())
1715 return TPResult::Error();
1720 // abstract-declarator[opt]
1721 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1722 if (TPR != TPResult::Ambiguous())
1725 // [GNU] attributes[opt]
1726 if (Tok.is(tok::kw___attribute))
1727 return TPResult::True();
1729 // If we're disambiguating a template argument in a default argument in
1730 // a class definition versus a parameter declaration, an '=' here
1731 // disambiguates the parse one way or the other.
1732 // If this is a parameter, it must have a default argument because
1733 // (a) the previous parameter did, and
1734 // (b) this must be the first declaration of the function, so we can't
1735 // inherit any default arguments from elsewhere.
1736 // If we see an ')', then we've reached the end of a
1737 // parameter-declaration-clause, and the last param is missing its default
1739 if (VersusTemplateArgument)
1740 return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True()
1741 : TPResult::False();
1743 if (Tok.is(tok::equal)) {
1744 // '=' assignment-expression
1745 // Parse through assignment-expression.
1746 // FIXME: assignment-expression may contain an unparenthesized comma.
1747 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
1748 return TPResult::Error();
1751 if (Tok.is(tok::ellipsis)) {
1753 if (Tok.is(tok::r_paren))
1754 return TPResult::True(); // '...)' is a sign of a function declarator.
1756 return TPResult::False();
1759 if (Tok.isNot(tok::comma))
1761 ConsumeToken(); // the comma.
1764 return TPResult::Ambiguous();
1767 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1768 /// parsing as a function declarator.
1769 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1770 /// return TPResult::Ambiguous(), otherwise it will return either False() or
1773 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1774 /// exception-specification[opt]
1776 /// exception-specification:
1777 /// 'throw' '(' type-id-list[opt] ')'
1779 Parser::TPResult Parser::TryParseFunctionDeclarator() {
1781 // The '(' is already parsed.
1783 TPResult TPR = TryParseParameterDeclarationClause();
1784 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1785 TPR = TPResult::False();
1787 if (TPR == TPResult::False() || TPR == TPResult::Error())
1790 // Parse through the parens.
1791 if (!SkipUntil(tok::r_paren, StopAtSemi))
1792 return TPResult::Error();
1795 while (Tok.is(tok::kw_const) ||
1796 Tok.is(tok::kw_volatile) ||
1797 Tok.is(tok::kw_restrict) )
1800 // ref-qualifier[opt]
1801 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1804 // exception-specification
1805 if (Tok.is(tok::kw_throw)) {
1807 if (Tok.isNot(tok::l_paren))
1808 return TPResult::Error();
1810 // Parse through the parens after 'throw'.
1812 if (!SkipUntil(tok::r_paren, StopAtSemi))
1813 return TPResult::Error();
1815 if (Tok.is(tok::kw_noexcept)) {
1817 // Possibly an expression as well.
1818 if (Tok.is(tok::l_paren)) {
1819 // Find the matching rparen.
1821 if (!SkipUntil(tok::r_paren, StopAtSemi))
1822 return TPResult::Error();
1826 return TPResult::Ambiguous();
1829 /// '[' constant-expression[opt] ']'
1831 Parser::TPResult Parser::TryParseBracketDeclarator() {
1833 if (!SkipUntil(tok::r_square, StopAtSemi))
1834 return TPResult::Error();
1836 return TPResult::Ambiguous();