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 (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 (!TryConsumeToken(tok::comma))
327 return TPResult::Ambiguous;
330 /// isCXXConditionDeclaration - Disambiguates between a declaration or an
331 /// expression for a condition of a if/switch/while/for statement.
332 /// If during the disambiguation process a parsing error is encountered,
333 /// the function returns true to let the declaration parsing code handle it.
337 /// type-specifier-seq declarator '=' assignment-expression
338 /// [C++11] type-specifier-seq declarator '=' initializer-clause
339 /// [C++11] type-specifier-seq declarator braced-init-list
340 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
341 /// '=' assignment-expression
343 bool Parser::isCXXConditionDeclaration() {
344 TPResult TPR = isCXXDeclarationSpecifier();
345 if (TPR != TPResult::Ambiguous)
346 return TPR != TPResult::False; // Returns true for TPResult::True or
349 // FIXME: Add statistics about the number of ambiguous statements encountered
350 // and how they were resolved (number of declarations+number of expressions).
352 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
353 // We need tentative parsing...
355 TentativeParsingAction PA(*this);
357 // type-specifier-seq
358 TryConsumeDeclarationSpecifier();
359 assert(Tok.is(tok::l_paren) && "Expected '('");
362 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
364 // In case of an error, let the declaration parsing code handle it.
365 if (TPR == TPResult::Error)
366 TPR = TPResult::True;
368 if (TPR == TPResult::Ambiguous) {
370 // [GNU] simple-asm-expr[opt] attributes[opt]
371 if (Tok.is(tok::equal) ||
372 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
373 TPR = TPResult::True;
374 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
375 TPR = TPResult::True;
377 TPR = TPResult::False;
382 assert(TPR == TPResult::True || TPR == TPResult::False);
383 return TPR == TPResult::True;
386 /// \brief Determine whether the next set of tokens contains a type-id.
388 /// The context parameter states what context we're parsing right
389 /// now, which affects how this routine copes with the token
390 /// following the type-id. If the context is TypeIdInParens, we have
391 /// already parsed the '(' and we will cease lookahead when we hit
392 /// the corresponding ')'. If the context is
393 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
394 /// before this template argument, and will cease lookahead when we
395 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
396 /// and false for an expression. If during the disambiguation
397 /// process a parsing error is encountered, the function returns
398 /// true to let the declaration parsing code handle it.
401 /// type-specifier-seq abstract-declarator[opt]
403 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
408 // The ambiguity arising from the similarity between a function-style cast and
409 // a type-id can occur in different contexts. The ambiguity appears as a
410 // choice between a function-style cast expression and a declaration of a
411 // type. The resolution is that any construct that could possibly be a type-id
412 // in its syntactic context shall be considered a type-id.
414 TPResult TPR = isCXXDeclarationSpecifier();
415 if (TPR != TPResult::Ambiguous)
416 return TPR != TPResult::False; // Returns true for TPResult::True or
419 // FIXME: Add statistics about the number of ambiguous statements encountered
420 // and how they were resolved (number of declarations+number of expressions).
422 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
423 // We need tentative parsing...
425 TentativeParsingAction PA(*this);
427 // type-specifier-seq
428 TryConsumeDeclarationSpecifier();
429 assert(Tok.is(tok::l_paren) && "Expected '('");
432 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
434 // In case of an error, let the declaration parsing code handle it.
435 if (TPR == TPResult::Error)
436 TPR = TPResult::True;
438 if (TPR == TPResult::Ambiguous) {
439 // We are supposed to be inside parens, so if after the abstract declarator
440 // we encounter a ')' this is a type-id, otherwise it's an expression.
441 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
442 TPR = TPResult::True;
445 // We are supposed to be inside a template argument, so if after
446 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
447 // ',', this is a type-id. Otherwise, it's an expression.
448 } else if (Context == TypeIdAsTemplateArgument &&
449 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
450 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
451 TPR = TPResult::True;
455 TPR = TPResult::False;
460 assert(TPR == TPResult::True || TPR == TPResult::False);
461 return TPR == TPResult::True;
464 /// \brief Returns true if this is a C++11 attribute-specifier. Per
465 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
466 /// always introduce an attribute. In Objective-C++11, this rule does not
467 /// apply if either '[' begins a message-send.
469 /// If Disambiguate is true, we try harder to determine whether a '[[' starts
470 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
472 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
473 /// Obj-C message send or the start of an attribute. Otherwise, we assume it
474 /// is not an Obj-C message send.
476 /// C++11 [dcl.attr.grammar]:
478 /// attribute-specifier:
479 /// '[' '[' attribute-list ']' ']'
480 /// alignment-specifier
484 /// attribute-list ',' attribute[opt]
486 /// attribute-list ',' attribute '...'
489 /// attribute-token attribute-argument-clause[opt]
493 /// identifier '::' identifier
495 /// attribute-argument-clause:
496 /// '(' balanced-token-seq ')'
497 Parser::CXX11AttributeKind
498 Parser::isCXX11AttributeSpecifier(bool Disambiguate,
499 bool OuterMightBeMessageSend) {
500 if (Tok.is(tok::kw_alignas))
501 return CAK_AttributeSpecifier;
503 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
504 return CAK_NotAttributeSpecifier;
506 // No tentative parsing if we don't need to look for ']]' or a lambda.
507 if (!Disambiguate && !getLangOpts().ObjC1)
508 return CAK_AttributeSpecifier;
510 TentativeParsingAction PA(*this);
512 // Opening brackets were checked for above.
515 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
516 if (!getLangOpts().ObjC1) {
519 bool IsAttribute = SkipUntil(tok::r_square);
520 IsAttribute &= Tok.is(tok::r_square);
524 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
527 // In Obj-C++11, we need to distinguish four situations:
528 // 1a) int x[[attr]]; C++11 attribute.
529 // 1b) [[attr]]; C++11 statement attribute.
530 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
531 // 3a) int x[[obj get]]; Message send in array size/index.
532 // 3b) [[Class alloc] init]; Message send in message send.
533 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
534 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
536 // If we have a lambda-introducer, then this is definitely not a message send.
537 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
538 // into the tentative attribute parse below.
539 LambdaIntroducer Intro;
540 if (!TryParseLambdaIntroducer(Intro)) {
541 // A lambda cannot end with ']]', and an attribute must.
542 bool IsAttribute = Tok.is(tok::r_square);
547 // Case 1: C++11 attribute.
548 return CAK_AttributeSpecifier;
550 if (OuterMightBeMessageSend)
551 // Case 4: Lambda in message send.
552 return CAK_NotAttributeSpecifier;
554 // Case 2: Lambda in array size / index.
555 return CAK_InvalidAttributeSpecifier;
560 // If we don't have a lambda-introducer, then we have an attribute or a
562 bool IsAttribute = true;
563 while (Tok.isNot(tok::r_square)) {
564 if (Tok.is(tok::comma)) {
565 // Case 1: Stray commas can only occur in attributes.
567 return CAK_AttributeSpecifier;
570 // Parse the attribute-token, if present.
571 // C++11 [dcl.attr.grammar]:
572 // If a keyword or an alternative token that satisfies the syntactic
573 // requirements of an identifier is contained in an attribute-token,
574 // it is considered an identifier.
576 if (!TryParseCXX11AttributeIdentifier(Loc)) {
580 if (Tok.is(tok::coloncolon)) {
582 if (!TryParseCXX11AttributeIdentifier(Loc)) {
588 // Parse the attribute-argument-clause, if present.
589 if (Tok.is(tok::l_paren)) {
591 if (!SkipUntil(tok::r_paren)) {
597 TryConsumeToken(tok::ellipsis);
599 if (!TryConsumeToken(tok::comma))
603 // An attribute must end ']]'.
605 if (Tok.is(tok::r_square)) {
607 IsAttribute = Tok.is(tok::r_square);
616 // Case 1: C++11 statement attribute.
617 return CAK_AttributeSpecifier;
619 // Case 3: Message send.
620 return CAK_NotAttributeSpecifier;
623 Parser::TPResult Parser::TryParsePtrOperatorSeq() {
625 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
626 if (TryAnnotateCXXScopeToken(true))
627 return TPResult::Error;
629 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
630 Tok.is(tok::ampamp) ||
631 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
634 while (Tok.is(tok::kw_const) ||
635 Tok.is(tok::kw_volatile) ||
636 Tok.is(tok::kw_restrict))
639 return TPResult::True;
644 /// operator-function-id:
645 /// 'operator' operator
648 /// new delete new[] delete[] + - * / % ^ [...]
650 /// conversion-function-id:
651 /// 'operator' conversion-type-id
653 /// conversion-type-id:
654 /// type-specifier-seq conversion-declarator[opt]
656 /// conversion-declarator:
657 /// ptr-operator conversion-declarator[opt]
659 /// literal-operator-id:
660 /// 'operator' string-literal identifier
661 /// 'operator' user-defined-string-literal
662 Parser::TPResult Parser::TryParseOperatorId() {
663 assert(Tok.is(tok::kw_operator));
666 // Maybe this is an operator-function-id.
667 switch (Tok.getKind()) {
668 case tok::kw_new: case tok::kw_delete:
670 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
674 return TPResult::True;
676 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
678 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
679 #include "clang/Basic/OperatorKinds.def"
681 return TPResult::True;
684 if (NextToken().is(tok::r_square)) {
687 return TPResult::True;
692 if (NextToken().is(tok::r_paren)) {
695 return TPResult::True;
703 // Maybe this is a literal-operator-id.
704 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
705 bool FoundUDSuffix = false;
707 FoundUDSuffix |= Tok.hasUDSuffix();
708 ConsumeStringToken();
709 } while (isTokenStringLiteral());
711 if (!FoundUDSuffix) {
712 if (Tok.is(tok::identifier))
715 return TPResult::Error;
717 return TPResult::True;
720 // Maybe this is a conversion-function-id.
721 bool AnyDeclSpecifiers = false;
723 TPResult TPR = isCXXDeclarationSpecifier();
724 if (TPR == TPResult::Error)
726 if (TPR == TPResult::False) {
727 if (!AnyDeclSpecifiers)
728 return TPResult::Error;
731 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
732 return TPResult::Error;
733 AnyDeclSpecifiers = true;
735 return TryParsePtrOperatorSeq();
739 /// direct-declarator
740 /// ptr-operator declarator
742 /// direct-declarator:
744 /// direct-declarator '(' parameter-declaration-clause ')'
745 /// cv-qualifier-seq[opt] exception-specification[opt]
746 /// direct-declarator '[' constant-expression[opt] ']'
747 /// '(' declarator ')'
748 /// [GNU] '(' attributes declarator ')'
750 /// abstract-declarator:
751 /// ptr-operator abstract-declarator[opt]
752 /// direct-abstract-declarator
755 /// direct-abstract-declarator:
756 /// direct-abstract-declarator[opt]
757 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
758 /// exception-specification[opt]
759 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
760 /// '(' abstract-declarator ')'
763 /// '*' cv-qualifier-seq[opt]
765 /// [C++0x] '&&' [TODO]
766 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
768 /// cv-qualifier-seq:
769 /// cv-qualifier cv-qualifier-seq[opt]
776 /// '...'[opt] id-expression
780 /// qualified-id [TODO]
784 /// operator-function-id
785 /// conversion-function-id
786 /// literal-operator-id
787 /// '~' class-name [TODO]
788 /// '~' decltype-specifier [TODO]
789 /// template-id [TODO]
791 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
792 bool mayHaveIdentifier) {
795 // ptr-operator declarator
796 if (TryParsePtrOperatorSeq() == TPResult::Error)
797 return TPResult::Error;
799 // direct-declarator:
800 // direct-abstract-declarator:
801 if (Tok.is(tok::ellipsis))
804 if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
805 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
806 NextToken().is(tok::kw_operator)))) &&
809 if (Tok.is(tok::annot_cxxscope))
811 else if (Tok.is(tok::identifier))
812 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
813 if (Tok.is(tok::kw_operator)) {
814 if (TryParseOperatorId() == TPResult::Error)
815 return TPResult::Error;
818 } else if (Tok.is(tok::l_paren)) {
821 (Tok.is(tok::r_paren) || // 'int()' is a function.
822 // 'int(...)' is a function.
823 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
824 isDeclarationSpecifier())) { // 'int(int)' is a function.
825 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
826 // exception-specification[opt]
827 TPResult TPR = TryParseFunctionDeclarator();
828 if (TPR != TPResult::Ambiguous)
831 // '(' declarator ')'
832 // '(' attributes declarator ')'
833 // '(' abstract-declarator ')'
834 if (Tok.is(tok::kw___attribute) ||
835 Tok.is(tok::kw___declspec) ||
836 Tok.is(tok::kw___cdecl) ||
837 Tok.is(tok::kw___stdcall) ||
838 Tok.is(tok::kw___fastcall) ||
839 Tok.is(tok::kw___thiscall) ||
840 Tok.is(tok::kw___unaligned))
841 return TPResult::True; // attributes indicate declaration
842 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
843 if (TPR != TPResult::Ambiguous)
845 if (Tok.isNot(tok::r_paren))
846 return TPResult::False;
849 } else if (!mayBeAbstract) {
850 return TPResult::False;
854 TPResult TPR(TPResult::Ambiguous);
856 // abstract-declarator: ...
857 if (Tok.is(tok::ellipsis))
860 if (Tok.is(tok::l_paren)) {
861 // Check whether we have a function declarator or a possible ctor-style
862 // initializer that follows the declarator. Note that ctor-style
863 // initializers are not possible in contexts where abstract declarators
865 if (!mayBeAbstract && !isCXXFunctionDeclarator())
868 // direct-declarator '(' parameter-declaration-clause ')'
869 // cv-qualifier-seq[opt] exception-specification[opt]
871 TPR = TryParseFunctionDeclarator();
872 } else if (Tok.is(tok::l_square)) {
873 // direct-declarator '[' constant-expression[opt] ']'
874 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
875 TPR = TryParseBracketDeclarator();
880 if (TPR != TPResult::Ambiguous)
884 return TPResult::Ambiguous;
888 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
890 // Obviously starts an expression.
891 case tok::numeric_constant:
892 case tok::char_constant:
893 case tok::wide_char_constant:
894 case tok::utf16_char_constant:
895 case tok::utf32_char_constant:
896 case tok::string_literal:
897 case tok::wide_string_literal:
898 case tok::utf8_string_literal:
899 case tok::utf16_string_literal:
900 case tok::utf32_string_literal:
909 case tok::minusminus:
913 case tok::kw___func__:
914 case tok::kw_const_cast:
916 case tok::kw_dynamic_cast:
919 case tok::kw_operator:
920 case tok::kw_reinterpret_cast:
921 case tok::kw_static_cast:
926 case tok::kw_alignof:
927 case tok::kw_noexcept:
928 case tok::kw_nullptr:
929 case tok::kw__Alignof:
931 case tok::kw___alignof:
932 case tok::kw___builtin_choose_expr:
933 case tok::kw___builtin_offsetof:
934 case tok::kw___builtin_va_arg:
937 case tok::kw___FUNCTION__:
938 case tok::kw___FUNCDNAME__:
939 case tok::kw___FUNCSIG__:
940 case tok::kw_L__FUNCTION__:
941 case tok::kw___PRETTY_FUNCTION__:
942 case tok::kw___uuidof:
943 #define TYPE_TRAIT(N,Spelling,K) \
944 case tok::kw_##Spelling:
945 #include "clang/Basic/TokenKinds.def"
946 return TPResult::True;
948 // Obviously starts a type-specifier-seq:
957 case tok::kw___int64:
958 case tok::kw___int128:
959 case tok::kw_restrict:
964 case tok::kw_unsigned:
966 case tok::kw_volatile:
968 case tok::kw__Complex:
970 case tok::kw_typename:
971 case tok::kw_wchar_t:
972 case tok::kw_char16_t:
973 case tok::kw_char32_t:
974 case tok::kw__Decimal32:
975 case tok::kw__Decimal64:
976 case tok::kw__Decimal128:
977 case tok::kw___interface:
978 case tok::kw___thread:
979 case tok::kw_thread_local:
980 case tok::kw__Thread_local:
982 case tok::kw___underlying_type:
983 case tok::kw___cdecl:
984 case tok::kw___stdcall:
985 case tok::kw___fastcall:
986 case tok::kw___thiscall:
987 case tok::kw___unaligned:
988 case tok::kw___vector:
989 case tok::kw___pixel:
990 case tok::kw__Atomic:
991 case tok::kw___unknown_anytype:
992 return TPResult::False;
998 return TPResult::Ambiguous;
1001 bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1002 return std::find(TentativelyDeclaredIdentifiers.begin(),
1003 TentativelyDeclaredIdentifiers.end(), II)
1004 != TentativelyDeclaredIdentifiers.end();
1007 /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1008 /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1009 /// be either a decl-specifier or a function-style cast, and TPResult::Error
1010 /// if a parsing error was found and reported.
1012 /// If HasMissingTypename is provided, a name with a dependent scope specifier
1013 /// will be treated as ambiguous if the 'typename' keyword is missing. If this
1014 /// happens, *HasMissingTypename will be set to 'true'. This will also be used
1015 /// as an indicator that undeclared identifiers (which will trigger a later
1016 /// parse error) should be treated as types. Returns TPResult::Ambiguous in
1020 /// storage-class-specifier
1022 /// function-specifier
1025 /// [C++11] 'constexpr'
1026 /// [GNU] attributes declaration-specifiers[opt]
1028 /// storage-class-specifier:
1034 /// [GNU] '__thread'
1035 /// [C++11] 'thread_local'
1036 /// [C11] '_Thread_local'
1038 /// function-specifier:
1047 /// simple-type-specifier
1050 /// elaborated-type-specifier
1051 /// typename-specifier
1054 /// simple-type-specifier:
1055 /// '::'[opt] nested-name-specifier[opt] type-name
1056 /// '::'[opt] nested-name-specifier 'template'
1057 /// simple-template-id [TODO]
1069 /// [GNU] typeof-specifier
1070 /// [GNU] '_Complex'
1072 /// [C++11] 'decltype' ( expression )
1073 /// [C++1y] 'decltype' ( 'auto' )
1080 /// elaborated-type-specifier:
1081 /// class-key '::'[opt] nested-name-specifier[opt] identifier
1082 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1083 /// simple-template-id
1084 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1090 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1091 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1093 /// class-specifier:
1094 /// class-head '{' member-specification[opt] '}'
1097 /// class-key identifier[opt] base-clause[opt]
1098 /// class-key nested-name-specifier identifier base-clause[opt]
1099 /// class-key nested-name-specifier[opt] simple-template-id
1100 /// base-clause[opt]
1113 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1114 bool *HasMissingTypename) {
1115 switch (Tok.getKind()) {
1116 case tok::identifier: {
1117 // Check for need to substitute AltiVec __vector keyword
1118 // for "vector" identifier.
1119 if (TryAltiVecVectorToken())
1120 return TPResult::True;
1122 const Token &Next = NextToken();
1123 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1124 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
1125 return TPResult::True;
1127 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1128 // Determine whether this is a valid expression. If not, we will hit
1129 // a parse error one way or another. In that case, tell the caller that
1130 // this is ambiguous. Typo-correct to type and expression keywords and
1131 // to types and identifiers, in order to try to recover from errors.
1132 CorrectionCandidateCallback TypoCorrection;
1133 TypoCorrection.WantRemainingKeywords = false;
1134 TypoCorrection.WantTypeSpecifiers = Next.isNot(tok::arrow);
1135 switch (TryAnnotateName(false /* no nested name specifier */,
1138 return TPResult::Error;
1139 case ANK_TentativeDecl:
1140 return TPResult::False;
1141 case ANK_TemplateName:
1142 // A bare type template-name which can't be a template template
1143 // argument is an error, and was probably intended to be a type.
1144 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1145 case ANK_Unresolved:
1146 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
1150 assert(Tok.isNot(tok::identifier) &&
1151 "TryAnnotateName succeeded without producing an annotation");
1153 // This might possibly be a type with a dependent scope specifier and
1154 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1155 // since it will annotate as a primary expression, and we want to use the
1156 // "missing 'typename'" logic.
1157 if (TryAnnotateTypeOrScopeToken())
1158 return TPResult::Error;
1159 // If annotation failed, assume it's a non-type.
1160 // FIXME: If this happens due to an undeclared identifier, treat it as
1162 if (Tok.is(tok::identifier))
1163 return TPResult::False;
1166 // We annotated this token as something. Recurse to handle whatever we got.
1167 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1170 case tok::kw_typename: // typename T::type
1171 // Annotate typenames and C++ scope specifiers. If we get one, just
1172 // recurse to handle whatever we get.
1173 if (TryAnnotateTypeOrScopeToken())
1174 return TPResult::Error;
1175 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1177 case tok::coloncolon: { // ::foo::bar
1178 const Token &Next = NextToken();
1179 if (Next.is(tok::kw_new) || // ::new
1180 Next.is(tok::kw_delete)) // ::delete
1181 return TPResult::False;
1184 case tok::kw_decltype:
1185 // Annotate typenames and C++ scope specifiers. If we get one, just
1186 // recurse to handle whatever we get.
1187 if (TryAnnotateTypeOrScopeToken())
1188 return TPResult::Error;
1189 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1192 // storage-class-specifier
1194 // function-specifier
1198 case tok::kw_friend:
1199 case tok::kw_typedef:
1200 case tok::kw_constexpr:
1201 // storage-class-specifier
1202 case tok::kw_register:
1203 case tok::kw_static:
1204 case tok::kw_extern:
1205 case tok::kw_mutable:
1207 case tok::kw___thread:
1208 case tok::kw_thread_local:
1209 case tok::kw__Thread_local:
1210 // function-specifier
1211 case tok::kw_inline:
1212 case tok::kw_virtual:
1213 case tok::kw_explicit:
1216 case tok::kw___module_private__:
1219 case tok::kw___unknown_anytype:
1222 // simple-type-specifier
1225 // elaborated-type-specifier
1226 // typename-specifier
1230 // elaborated-type-specifier
1232 case tok::kw_struct:
1234 case tok::kw___interface:
1239 case tok::kw_volatile:
1242 case tok::kw_restrict:
1243 case tok::kw__Complex:
1244 case tok::kw___attribute:
1245 return TPResult::True;
1248 case tok::kw___declspec:
1249 case tok::kw___cdecl:
1250 case tok::kw___stdcall:
1251 case tok::kw___fastcall:
1252 case tok::kw___thiscall:
1254 case tok::kw___sptr:
1255 case tok::kw___uptr:
1256 case tok::kw___ptr64:
1257 case tok::kw___ptr32:
1258 case tok::kw___forceinline:
1259 case tok::kw___unaligned:
1260 return TPResult::True;
1263 case tok::kw___pascal:
1264 return TPResult::True;
1267 case tok::kw___vector:
1268 return TPResult::True;
1270 case tok::annot_template_id: {
1271 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1272 if (TemplateId->Kind != TNK_Type_template)
1273 return TPResult::False;
1275 AnnotateTemplateIdTokenAsType();
1276 assert(Tok.is(tok::annot_typename));
1280 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1281 // We've already annotated a scope; try to annotate a type.
1282 if (TryAnnotateTypeOrScopeToken())
1283 return TPResult::Error;
1284 if (!Tok.is(tok::annot_typename)) {
1285 // If the next token is an identifier or a type qualifier, then this
1286 // can't possibly be a valid expression either.
1287 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1289 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1290 Tok.getAnnotationRange(),
1292 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1293 TentativeParsingAction PA(*this);
1296 bool isIdentifier = Tok.is(tok::identifier);
1297 TPResult TPR = TPResult::False;
1299 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1300 HasMissingTypename);
1304 TPR == TPResult::True || TPR == TPResult::Error)
1305 return TPResult::Error;
1307 if (HasMissingTypename) {
1308 // We can't tell whether this is a missing 'typename' or a valid
1310 *HasMissingTypename = true;
1311 return TPResult::Ambiguous;
1314 // Try to resolve the name. If it doesn't exist, assume it was
1315 // intended to name a type and keep disambiguating.
1316 switch (TryAnnotateName(false /* SS is not dependent */)) {
1318 return TPResult::Error;
1319 case ANK_TentativeDecl:
1320 return TPResult::False;
1321 case ANK_TemplateName:
1322 // A bare type template-name which can't be a template template
1323 // argument is an error, and was probably intended to be a type.
1324 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1325 case ANK_Unresolved:
1326 return HasMissingTypename ? TPResult::Ambiguous
1329 // Annotated it, check again.
1330 assert(Tok.isNot(tok::annot_cxxscope) ||
1331 NextToken().isNot(tok::identifier));
1332 return isCXXDeclarationSpecifier(BracedCastResult,
1333 HasMissingTypename);
1337 return TPResult::False;
1339 // If that succeeded, fallthrough into the generic simple-type-id case.
1341 // The ambiguity resides in a simple-type-specifier/typename-specifier
1342 // followed by a '('. The '(' could either be the start of:
1344 // direct-declarator:
1345 // '(' declarator ')'
1347 // direct-abstract-declarator:
1348 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1349 // exception-specification[opt]
1350 // '(' abstract-declarator ')'
1352 // or part of a function-style cast expression:
1354 // simple-type-specifier '(' expression-list[opt] ')'
1357 // simple-type-specifier:
1359 case tok::annot_typename:
1361 // In Objective-C, we might have a protocol-qualified type.
1362 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1363 // Tentatively parse the
1364 TentativeParsingAction PA(*this);
1365 ConsumeToken(); // The type token
1367 TPResult TPR = TryParseProtocolQualifiers();
1368 bool isFollowedByParen = Tok.is(tok::l_paren);
1369 bool isFollowedByBrace = Tok.is(tok::l_brace);
1373 if (TPR == TPResult::Error)
1374 return TPResult::Error;
1376 if (isFollowedByParen)
1377 return TPResult::Ambiguous;
1379 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1380 return BracedCastResult;
1382 return TPResult::True;
1386 case tok::kw_wchar_t:
1387 case tok::kw_char16_t:
1388 case tok::kw_char32_t:
1393 case tok::kw___int64:
1394 case tok::kw___int128:
1395 case tok::kw_signed:
1396 case tok::kw_unsigned:
1399 case tok::kw_double:
1401 case tok::annot_decltype:
1402 if (NextToken().is(tok::l_paren))
1403 return TPResult::Ambiguous;
1405 // This is a function-style cast in all cases we disambiguate other than
1408 // enum E : int { a = 4 }; // enum
1409 // enum E : int { 4 }; // bit-field
1411 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1412 return BracedCastResult;
1414 if (isStartOfObjCClassMessageMissingOpenBracket())
1415 return TPResult::False;
1417 return TPResult::True;
1419 // GNU typeof support.
1420 case tok::kw_typeof: {
1421 if (NextToken().isNot(tok::l_paren))
1422 return TPResult::True;
1424 TentativeParsingAction PA(*this);
1426 TPResult TPR = TryParseTypeofSpecifier();
1427 bool isFollowedByParen = Tok.is(tok::l_paren);
1428 bool isFollowedByBrace = Tok.is(tok::l_brace);
1432 if (TPR == TPResult::Error)
1433 return TPResult::Error;
1435 if (isFollowedByParen)
1436 return TPResult::Ambiguous;
1438 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1439 return BracedCastResult;
1441 return TPResult::True;
1444 // C++0x type traits support
1445 case tok::kw___underlying_type:
1446 return TPResult::True;
1449 case tok::kw__Atomic:
1450 return TPResult::True;
1453 return TPResult::False;
1457 bool Parser::isCXXDeclarationSpecifierAType() {
1458 switch (Tok.getKind()) {
1459 // typename-specifier
1460 case tok::annot_decltype:
1461 case tok::annot_template_id:
1462 case tok::annot_typename:
1463 case tok::kw_typeof:
1464 case tok::kw___underlying_type:
1467 // elaborated-type-specifier
1469 case tok::kw_struct:
1471 case tok::kw___interface:
1475 // simple-type-specifier
1477 case tok::kw_wchar_t:
1478 case tok::kw_char16_t:
1479 case tok::kw_char32_t:
1484 case tok::kw___int64:
1485 case tok::kw___int128:
1486 case tok::kw_signed:
1487 case tok::kw_unsigned:
1490 case tok::kw_double:
1492 case tok::kw___unknown_anytype:
1496 return getLangOpts().CPlusPlus11;
1498 case tok::kw__Atomic:
1500 return NextToken().is(tok::l_paren);
1507 /// [GNU] typeof-specifier:
1508 /// 'typeof' '(' expressions ')'
1509 /// 'typeof' '(' type-name ')'
1511 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1512 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1515 assert(Tok.is(tok::l_paren) && "Expected '('");
1516 // Parse through the parens after 'typeof'.
1518 if (!SkipUntil(tok::r_paren, StopAtSemi))
1519 return TPResult::Error;
1521 return TPResult::Ambiguous;
1524 /// [ObjC] protocol-qualifiers:
1525 //// '<' identifier-list '>'
1526 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1527 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1530 if (Tok.isNot(tok::identifier))
1531 return TPResult::Error;
1534 if (Tok.is(tok::comma)) {
1539 if (Tok.is(tok::greater)) {
1541 return TPResult::Ambiguous;
1545 return TPResult::Error;
1548 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1549 /// a constructor-style initializer, when parsing declaration statements.
1550 /// Returns true for function declarator and false for constructor-style
1552 /// If during the disambiguation process a parsing error is encountered,
1553 /// the function returns true to let the declaration parsing code handle it.
1555 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1556 /// exception-specification[opt]
1558 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1561 // The ambiguity arising from the similarity between a function-style cast and
1562 // a declaration mentioned in 6.8 can also occur in the context of a
1563 // declaration. In that context, the choice is between a function declaration
1564 // with a redundant set of parentheses around a parameter name and an object
1565 // declaration with a function-style cast as the initializer. Just as for the
1566 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1567 // that could possibly be a declaration a declaration.
1569 TentativeParsingAction PA(*this);
1572 bool InvalidAsDeclaration = false;
1573 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1574 if (TPR == TPResult::Ambiguous) {
1575 if (Tok.isNot(tok::r_paren))
1576 TPR = TPResult::False;
1578 const Token &Next = NextToken();
1579 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1580 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1581 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1582 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
1583 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1584 Next.is(tok::equal) || Next.is(tok::arrow))
1585 // The next token cannot appear after a constructor-style initializer,
1586 // and can appear next in a function definition. This must be a function
1588 TPR = TPResult::True;
1589 else if (InvalidAsDeclaration)
1590 // Use the absence of 'typename' as a tie-breaker.
1591 TPR = TPResult::False;
1597 if (IsAmbiguous && TPR == TPResult::Ambiguous)
1598 *IsAmbiguous = true;
1600 // In case of an error, let the declaration parsing code handle it.
1601 return TPR != TPResult::False;
1604 /// parameter-declaration-clause:
1605 /// parameter-declaration-list[opt] '...'[opt]
1606 /// parameter-declaration-list ',' '...'
1608 /// parameter-declaration-list:
1609 /// parameter-declaration
1610 /// parameter-declaration-list ',' parameter-declaration
1612 /// parameter-declaration:
1613 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1614 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1615 /// '=' assignment-expression
1616 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1618 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1619 /// attributes[opt] '=' assignment-expression
1622 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1623 bool VersusTemplateArgument) {
1625 if (Tok.is(tok::r_paren))
1626 return TPResult::Ambiguous;
1628 // parameter-declaration-list[opt] '...'[opt]
1629 // parameter-declaration-list ',' '...'
1631 // parameter-declaration-list:
1632 // parameter-declaration
1633 // parameter-declaration-list ',' parameter-declaration
1637 if (Tok.is(tok::ellipsis)) {
1639 if (Tok.is(tok::r_paren))
1640 return TPResult::True; // '...)' is a sign of a function declarator.
1642 return TPResult::False;
1645 // An attribute-specifier-seq here is a sign of a function declarator.
1646 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1647 /*OuterMightBeMessageSend*/true))
1648 return TPResult::True;
1650 ParsedAttributes attrs(AttrFactory);
1651 MaybeParseMicrosoftAttributes(attrs);
1653 // decl-specifier-seq
1654 // A parameter-declaration's initializer must be preceded by an '=', so
1655 // decl-specifier-seq '{' is not a parameter in C++11.
1656 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1657 InvalidAsDeclaration);
1659 if (VersusTemplateArgument && TPR == TPResult::True) {
1660 // Consume the decl-specifier-seq. We have to look past it, since a
1661 // type-id might appear here in a template argument.
1662 bool SeenType = false;
1664 SeenType |= isCXXDeclarationSpecifierAType();
1665 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1666 return TPResult::Error;
1668 // If we see a parameter name, this can't be a template argument.
1669 if (SeenType && Tok.is(tok::identifier))
1670 return TPResult::True;
1672 TPR = isCXXDeclarationSpecifier(TPResult::False,
1673 InvalidAsDeclaration);
1674 if (TPR == TPResult::Error)
1676 } while (TPR != TPResult::False);
1677 } else if (TPR == TPResult::Ambiguous) {
1678 // Disambiguate what follows the decl-specifier.
1679 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1680 return TPResult::Error;
1685 // abstract-declarator[opt]
1686 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1687 if (TPR != TPResult::Ambiguous)
1690 // [GNU] attributes[opt]
1691 if (Tok.is(tok::kw___attribute))
1692 return TPResult::True;
1694 // If we're disambiguating a template argument in a default argument in
1695 // a class definition versus a parameter declaration, an '=' here
1696 // disambiguates the parse one way or the other.
1697 // If this is a parameter, it must have a default argument because
1698 // (a) the previous parameter did, and
1699 // (b) this must be the first declaration of the function, so we can't
1700 // inherit any default arguments from elsewhere.
1701 // If we see an ')', then we've reached the end of a
1702 // parameter-declaration-clause, and the last param is missing its default
1704 if (VersusTemplateArgument)
1705 return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True
1708 if (Tok.is(tok::equal)) {
1709 // '=' assignment-expression
1710 // Parse through assignment-expression.
1711 // FIXME: assignment-expression may contain an unparenthesized comma.
1712 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
1713 return TPResult::Error;
1716 if (Tok.is(tok::ellipsis)) {
1718 if (Tok.is(tok::r_paren))
1719 return TPResult::True; // '...)' is a sign of a function declarator.
1721 return TPResult::False;
1724 if (!TryConsumeToken(tok::comma))
1728 return TPResult::Ambiguous;
1731 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1732 /// parsing as a function declarator.
1733 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1734 /// return TPResult::Ambiguous, otherwise it will return either False() or
1737 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1738 /// exception-specification[opt]
1740 /// exception-specification:
1741 /// 'throw' '(' type-id-list[opt] ')'
1743 Parser::TPResult Parser::TryParseFunctionDeclarator() {
1745 // The '(' is already parsed.
1747 TPResult TPR = TryParseParameterDeclarationClause();
1748 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1749 TPR = TPResult::False;
1751 if (TPR == TPResult::False || TPR == TPResult::Error)
1754 // Parse through the parens.
1755 if (!SkipUntil(tok::r_paren, StopAtSemi))
1756 return TPResult::Error;
1759 while (Tok.is(tok::kw_const) ||
1760 Tok.is(tok::kw_volatile) ||
1761 Tok.is(tok::kw_restrict) )
1764 // ref-qualifier[opt]
1765 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1768 // exception-specification
1769 if (Tok.is(tok::kw_throw)) {
1771 if (Tok.isNot(tok::l_paren))
1772 return TPResult::Error;
1774 // Parse through the parens after 'throw'.
1776 if (!SkipUntil(tok::r_paren, StopAtSemi))
1777 return TPResult::Error;
1779 if (Tok.is(tok::kw_noexcept)) {
1781 // Possibly an expression as well.
1782 if (Tok.is(tok::l_paren)) {
1783 // Find the matching rparen.
1785 if (!SkipUntil(tok::r_paren, StopAtSemi))
1786 return TPResult::Error;
1790 return TPResult::Ambiguous;
1793 /// '[' constant-expression[opt] ']'
1795 Parser::TPResult Parser::TryParseBracketDeclarator() {
1797 if (!SkipUntil(tok::r_square, StopAtSemi))
1798 return TPResult::Error;
1800 return TPResult::Ambiguous;