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
:
64 return isCXXSimpleDeclaration();
68 /// isCXXSimpleDeclaration - C++-specialized function that disambiguates
69 /// between a simple-declaration or an expression-statement.
70 /// If during the disambiguation process a parsing error is encountered,
71 /// the function returns true to let the declaration parsing code handle it.
72 /// Returns false if the statement is disambiguated as expression.
74 /// simple-declaration:
75 /// decl-specifier-seq init-declarator-list[opt] ';'
77 bool Parser::isCXXSimpleDeclaration() {
79 // There is an ambiguity in the grammar involving expression-statements and
80 // declarations: An expression-statement with a function-style explicit type
81 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
82 // from a declaration where the first declarator starts with a '('. In those
83 // cases the statement is a declaration. [Note: To disambiguate, the whole
84 // statement might have to be examined to determine if it is an
85 // expression-statement or a declaration].
88 // The disambiguation is purely syntactic; that is, the meaning of the names
89 // occurring in such a statement, beyond whether they are type-names or not,
90 // is not generally used in or changed by the disambiguation. Class
91 // templates are instantiated as necessary to determine if a qualified name
92 // is a type-name. Disambiguation precedes parsing, and a statement
93 // disambiguated as a declaration may be an ill-formed declaration.
95 // We don't have to parse all of the decl-specifier-seq part. There's only
96 // an ambiguity if the first decl-specifier is
97 // simple-type-specifier/typename-specifier followed by a '(', which may
98 // indicate a function-style cast expression.
99 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
102 TPResult TPR
= isCXXDeclarationSpecifier();
103 if (TPR
!= TPResult::Ambiguous())
104 return TPR
!= TPResult::False(); // Returns true for TPResult::True() or
105 // TPResult::Error().
107 // FIXME: Add statistics about the number of ambiguous statements encountered
108 // and how they were resolved (number of declarations+number of expressions).
110 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
111 // We need tentative parsing...
113 TentativeParsingAction
PA(*this);
114 TPR
= TryParseSimpleDeclaration();
117 // In case of an error, let the declaration parsing code handle it.
118 if (TPR
== TPResult::Error())
121 // Declarations take precedence over expressions.
122 if (TPR
== TPResult::Ambiguous())
123 TPR
= TPResult::True();
125 assert(TPR
== TPResult::True() || TPR
== TPResult::False());
126 return TPR
== TPResult::True();
129 /// simple-declaration:
130 /// decl-specifier-seq init-declarator-list[opt] ';'
132 Parser::TPResult
Parser::TryParseSimpleDeclaration() {
133 // We know that we have a simple-type-specifier/typename-specifier followed
135 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
137 if (Tok
.is(tok::kw_typeof
))
138 TryParseTypeofSpecifier();
142 if (getLang().ObjC1
&& Tok
.is(tok::less
))
143 TryParseProtocolQualifiers();
146 assert(Tok
.is(tok::l_paren
) && "Expected '('");
148 TPResult TPR
= TryParseInitDeclaratorList();
149 if (TPR
!= TPResult::Ambiguous())
152 if (Tok
.isNot(tok::semi
))
153 return TPResult::False();
155 return TPResult::Ambiguous();
158 /// init-declarator-list:
160 /// init-declarator-list ',' init-declarator
163 /// declarator initializer[opt]
164 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
167 /// '=' initializer-clause
168 /// '(' expression-list ')'
170 /// initializer-clause:
171 /// assignment-expression
172 /// '{' initializer-list ','[opt] '}'
175 Parser::TPResult
Parser::TryParseInitDeclaratorList() {
178 TPResult TPR
= TryParseDeclarator(false/*mayBeAbstract*/);
179 if (TPR
!= TPResult::Ambiguous())
182 // [GNU] simple-asm-expr[opt] attributes[opt]
183 if (Tok
.is(tok::kw_asm
) || Tok
.is(tok::kw___attribute
))
184 return TPResult::True();
187 if (Tok
.is(tok::l_paren
)) {
188 // Parse through the parens.
190 if (!SkipUntil(tok::r_paren
))
191 return TPResult::Error();
192 } else if (Tok
.is(tok::equal
) || isTokIdentifier_in()) {
193 // MSVC and g++ won't examine the rest of declarators if '=' is
194 // encountered; they just conclude that we have a declaration.
195 // EDG parses the initializer completely, which is the proper behavior
198 // At present, Clang follows MSVC and g++, since the parser does not have
199 // the ability to parse an expression fully without recording the
200 // results of that parse.
201 // Also allow 'in' after on objective-c declaration as in:
202 // for (int (^b)(void) in array). Ideally this should be done in the
203 // context of parsing for-init-statement of a foreach statement only. But,
204 // in any other context 'in' is invalid after a declaration and parser
205 // issues the error regardless of outcome of this decision.
206 // FIXME. Change if above assumption does not hold.
207 return TPResult::True();
210 if (Tok
.isNot(tok::comma
))
212 ConsumeToken(); // the comma.
215 return TPResult::Ambiguous();
218 /// isCXXConditionDeclaration - Disambiguates between a declaration or an
219 /// expression for a condition of a if/switch/while/for statement.
220 /// If during the disambiguation process a parsing error is encountered,
221 /// the function returns true to let the declaration parsing code handle it.
225 /// type-specifier-seq declarator '=' assignment-expression
226 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
227 /// '=' assignment-expression
229 bool Parser::isCXXConditionDeclaration() {
230 TPResult TPR
= isCXXDeclarationSpecifier();
231 if (TPR
!= TPResult::Ambiguous())
232 return TPR
!= TPResult::False(); // Returns true for TPResult::True() or
233 // TPResult::Error().
235 // FIXME: Add statistics about the number of ambiguous statements encountered
236 // and how they were resolved (number of declarations+number of expressions).
238 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
239 // We need tentative parsing...
241 TentativeParsingAction
PA(*this);
243 // type-specifier-seq
244 if (Tok
.is(tok::kw_typeof
))
245 TryParseTypeofSpecifier();
249 if (getLang().ObjC1
&& Tok
.is(tok::less
))
250 TryParseProtocolQualifiers();
252 assert(Tok
.is(tok::l_paren
) && "Expected '('");
255 TPR
= TryParseDeclarator(false/*mayBeAbstract*/);
257 // In case of an error, let the declaration parsing code handle it.
258 if (TPR
== TPResult::Error())
259 TPR
= TPResult::True();
261 if (TPR
== TPResult::Ambiguous()) {
263 // [GNU] simple-asm-expr[opt] attributes[opt]
264 if (Tok
.is(tok::equal
) ||
265 Tok
.is(tok::kw_asm
) || Tok
.is(tok::kw___attribute
))
266 TPR
= TPResult::True();
268 TPR
= TPResult::False();
273 assert(TPR
== TPResult::True() || TPR
== TPResult::False());
274 return TPR
== TPResult::True();
277 /// \brief Determine whether the next set of tokens contains a type-id.
279 /// The context parameter states what context we're parsing right
280 /// now, which affects how this routine copes with the token
281 /// following the type-id. If the context is TypeIdInParens, we have
282 /// already parsed the '(' and we will cease lookahead when we hit
283 /// the corresponding ')'. If the context is
284 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
285 /// before this template argument, and will cease lookahead when we
286 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
287 /// and false for an expression. If during the disambiguation
288 /// process a parsing error is encountered, the function returns
289 /// true to let the declaration parsing code handle it.
292 /// type-specifier-seq abstract-declarator[opt]
294 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context
, bool &isAmbiguous
) {
299 // The ambiguity arising from the similarity between a function-style cast and
300 // a type-id can occur in different contexts. The ambiguity appears as a
301 // choice between a function-style cast expression and a declaration of a
302 // type. The resolution is that any construct that could possibly be a type-id
303 // in its syntactic context shall be considered a type-id.
305 TPResult TPR
= isCXXDeclarationSpecifier();
306 if (TPR
!= TPResult::Ambiguous())
307 return TPR
!= TPResult::False(); // Returns true for TPResult::True() or
308 // TPResult::Error().
310 // FIXME: Add statistics about the number of ambiguous statements encountered
311 // and how they were resolved (number of declarations+number of expressions).
313 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
314 // We need tentative parsing...
316 TentativeParsingAction
PA(*this);
318 // type-specifier-seq
319 if (Tok
.is(tok::kw_typeof
))
320 TryParseTypeofSpecifier();
324 if (getLang().ObjC1
&& Tok
.is(tok::less
))
325 TryParseProtocolQualifiers();
328 assert(Tok
.is(tok::l_paren
) && "Expected '('");
331 TPR
= TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
333 // In case of an error, let the declaration parsing code handle it.
334 if (TPR
== TPResult::Error())
335 TPR
= TPResult::True();
337 if (TPR
== TPResult::Ambiguous()) {
338 // We are supposed to be inside parens, so if after the abstract declarator
339 // we encounter a ')' this is a type-id, otherwise it's an expression.
340 if (Context
== TypeIdInParens
&& Tok
.is(tok::r_paren
)) {
341 TPR
= TPResult::True();
344 // We are supposed to be inside a template argument, so if after
345 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
346 // ',', this is a type-id. Otherwise, it's an expression.
347 } else if (Context
== TypeIdAsTemplateArgument
&&
348 (Tok
.is(tok::greater
) || Tok
.is(tok::comma
) ||
349 (getLang().CPlusPlus0x
&& Tok
.is(tok::greatergreater
)))) {
350 TPR
= TPResult::True();
354 TPR
= TPResult::False();
359 assert(TPR
== TPResult::True() || TPR
== TPResult::False());
360 return TPR
== TPResult::True();
363 /// isCXX0XAttributeSpecifier - returns true if this is a C++0x
364 /// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
365 /// performed that will simply return true if a [[ is seen. Currently C++ has no
366 /// syntactical ambiguities from this check, but it may inhibit error recovery.
367 /// If CheckClosing is true, a check is made for closing ]] brackets.
369 /// If given, After is set to the token after the attribute-specifier so that
370 /// appropriate parsing decisions can be made; it is left untouched if false is
373 /// FIXME: If an error is in the closing ]] brackets, the program assumes
374 /// the absence of an attribute-specifier, which can cause very yucky errors
377 /// [C++0x] attribute-specifier:
378 /// '[' '[' attribute-list ']' ']'
380 /// [C++0x] attribute-list:
382 /// attribute-list ',' attribute[opt]
384 /// [C++0x] attribute:
385 /// attribute-token attribute-argument-clause[opt]
387 /// [C++0x] attribute-token:
389 /// attribute-scoped-token
391 /// [C++0x] attribute-scoped-token:
392 /// attribute-namespace '::' identifier
394 /// [C++0x] attribute-namespace:
397 /// [C++0x] attribute-argument-clause:
398 /// '(' balanced-token-seq ')'
400 /// [C++0x] balanced-token-seq:
402 /// balanced-token-seq balanced-token
404 /// [C++0x] balanced-token:
405 /// '(' balanced-token-seq ')'
406 /// '[' balanced-token-seq ']'
407 /// '{' balanced-token-seq '}'
408 /// any token but '(', ')', '[', ']', '{', or '}'
409 bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing
,
410 tok::TokenKind
*After
) {
411 if (Tok
.isNot(tok::l_square
) || NextToken().isNot(tok::l_square
))
414 // No tentative parsing if we don't need to look for ]]
415 if (!CheckClosing
&& !getLang().ObjC1
)
418 struct TentativeReverter
{
419 TentativeParsingAction PA
;
421 TentativeReverter (Parser
& P
)
424 ~TentativeReverter () {
429 // Opening brackets were checked for above.
433 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
434 SkipUntil(tok::r_square
, false);
436 if (Tok
.isNot(tok::r_square
))
441 *After
= Tok
.getKind();
447 /// direct-declarator
448 /// ptr-operator declarator
450 /// direct-declarator:
452 /// direct-declarator '(' parameter-declaration-clause ')'
453 /// cv-qualifier-seq[opt] exception-specification[opt]
454 /// direct-declarator '[' constant-expression[opt] ']'
455 /// '(' declarator ')'
456 /// [GNU] '(' attributes declarator ')'
458 /// abstract-declarator:
459 /// ptr-operator abstract-declarator[opt]
460 /// direct-abstract-declarator
463 /// direct-abstract-declarator:
464 /// direct-abstract-declarator[opt]
465 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
466 /// exception-specification[opt]
467 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
468 /// '(' abstract-declarator ')'
471 /// '*' cv-qualifier-seq[opt]
473 /// [C++0x] '&&' [TODO]
474 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
476 /// cv-qualifier-seq:
477 /// cv-qualifier cv-qualifier-seq[opt]
484 /// '...'[opt] id-expression
488 /// qualified-id [TODO]
492 /// operator-function-id [TODO]
493 /// conversion-function-id [TODO]
494 /// '~' class-name [TODO]
495 /// template-id [TODO]
497 Parser::TPResult
Parser::TryParseDeclarator(bool mayBeAbstract
,
498 bool mayHaveIdentifier
) {
501 // ptr-operator declarator
504 if (Tok
.is(tok::coloncolon
) || Tok
.is(tok::identifier
))
505 if (TryAnnotateCXXScopeToken(true))
506 return TPResult::Error();
508 if (Tok
.is(tok::star
) || Tok
.is(tok::amp
) || Tok
.is(tok::caret
) ||
509 Tok
.is(tok::ampamp
) ||
510 (Tok
.is(tok::annot_cxxscope
) && NextToken().is(tok::star
))) {
513 while (Tok
.is(tok::kw_const
) ||
514 Tok
.is(tok::kw_volatile
) ||
515 Tok
.is(tok::kw_restrict
))
522 // direct-declarator:
523 // direct-abstract-declarator:
524 if (Tok
.is(tok::ellipsis
))
527 if ((Tok
.is(tok::identifier
) ||
528 (Tok
.is(tok::annot_cxxscope
) && NextToken().is(tok::identifier
))) &&
531 if (Tok
.is(tok::annot_cxxscope
))
534 } else if (Tok
.is(tok::l_paren
)) {
537 (Tok
.is(tok::r_paren
) || // 'int()' is a function.
538 Tok
.is(tok::ellipsis
) || // 'int(...)' is a function.
539 isDeclarationSpecifier())) { // 'int(int)' is a function.
540 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
541 // exception-specification[opt]
542 TPResult TPR
= TryParseFunctionDeclarator();
543 if (TPR
!= TPResult::Ambiguous())
546 // '(' declarator ')'
547 // '(' attributes declarator ')'
548 // '(' abstract-declarator ')'
549 if (Tok
.is(tok::kw___attribute
) ||
550 Tok
.is(tok::kw___declspec
) ||
551 Tok
.is(tok::kw___cdecl
) ||
552 Tok
.is(tok::kw___stdcall
) ||
553 Tok
.is(tok::kw___fastcall
) ||
554 Tok
.is(tok::kw___thiscall
))
555 return TPResult::True(); // attributes indicate declaration
556 TPResult TPR
= TryParseDeclarator(mayBeAbstract
, mayHaveIdentifier
);
557 if (TPR
!= TPResult::Ambiguous())
559 if (Tok
.isNot(tok::r_paren
))
560 return TPResult::False();
563 } else if (!mayBeAbstract
) {
564 return TPResult::False();
568 TPResult
TPR(TPResult::Ambiguous());
570 // abstract-declarator: ...
571 if (Tok
.is(tok::ellipsis
))
574 if (Tok
.is(tok::l_paren
)) {
575 // Check whether we have a function declarator or a possible ctor-style
576 // initializer that follows the declarator. Note that ctor-style
577 // initializers are not possible in contexts where abstract declarators
579 if (!mayBeAbstract
&& !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
582 // direct-declarator '(' parameter-declaration-clause ')'
583 // cv-qualifier-seq[opt] exception-specification[opt]
585 TPR
= TryParseFunctionDeclarator();
586 } else if (Tok
.is(tok::l_square
)) {
587 // direct-declarator '[' constant-expression[opt] ']'
588 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
589 TPR
= TryParseBracketDeclarator();
594 if (TPR
!= TPResult::Ambiguous())
598 return TPResult::Ambiguous();
602 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind
) {
604 // Obviously starts an expression.
605 case tok::numeric_constant
:
606 case tok::char_constant
:
607 case tok::string_literal
:
608 case tok::wide_string_literal
:
617 case tok::minusminus
:
621 case tok::kw___func__
:
622 case tok::kw_const_cast
:
624 case tok::kw_dynamic_cast
:
627 case tok::kw_operator
:
628 case tok::kw_reinterpret_cast
:
629 case tok::kw_static_cast
:
634 case tok::kw_alignof
:
635 case tok::kw_noexcept
:
636 case tok::kw_nullptr
:
638 case tok::kw___alignof
:
639 case tok::kw___builtin_choose_expr
:
640 case tok::kw___builtin_offsetof
:
641 case tok::kw___builtin_types_compatible_p
:
642 case tok::kw___builtin_va_arg
:
645 case tok::kw___FUNCTION__
:
646 case tok::kw___PRETTY_FUNCTION__
:
647 case tok::kw___has_nothrow_assign
:
648 case tok::kw___has_nothrow_copy
:
649 case tok::kw___has_nothrow_constructor
:
650 case tok::kw___has_trivial_assign
:
651 case tok::kw___has_trivial_copy
:
652 case tok::kw___has_trivial_constructor
:
653 case tok::kw___has_trivial_destructor
:
654 case tok::kw___has_virtual_destructor
:
655 case tok::kw___is_abstract
:
656 case tok::kw___is_base_of
:
657 case tok::kw___is_class
:
658 case tok::kw___is_convertible_to
:
659 case tok::kw___is_empty
:
660 case tok::kw___is_enum
:
661 case tok::kw___is_pod
:
662 case tok::kw___is_polymorphic
:
663 case tok::kw___is_union
:
664 case tok::kw___is_literal
:
665 case tok::kw___uuidof
:
666 return TPResult::True();
668 // Obviously starts a type-specifier-seq:
676 case tok::kw_restrict
:
681 case tok::kw_unsigned
:
683 case tok::kw_volatile
:
685 case tok::kw__Complex
:
687 case tok::kw_typename
:
688 case tok::kw_wchar_t
:
689 case tok::kw_char16_t
:
690 case tok::kw_char32_t
:
691 case tok::kw_decltype
:
692 case tok::kw_thread_local
:
693 case tok::kw__Decimal32
:
694 case tok::kw__Decimal64
:
695 case tok::kw__Decimal128
:
696 case tok::kw___thread
:
698 case tok::kw___cdecl
:
699 case tok::kw___stdcall
:
700 case tok::kw___fastcall
:
701 case tok::kw___thiscall
:
702 case tok::kw___vector
:
703 case tok::kw___pixel
:
704 return TPResult::False();
710 return TPResult::Ambiguous();
713 /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
714 /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
715 /// be either a decl-specifier or a function-style cast, and TPResult::Error()
716 /// if a parsing error was found and reported.
719 /// storage-class-specifier
721 /// function-specifier
724 /// [C++0x] 'constexpr'
725 /// [GNU] attributes declaration-specifiers[opt]
727 /// storage-class-specifier:
735 /// function-specifier:
744 /// simple-type-specifier
747 /// elaborated-type-specifier
748 /// typename-specifier
751 /// simple-type-specifier:
752 /// '::'[opt] nested-name-specifier[opt] type-name
753 /// '::'[opt] nested-name-specifier 'template'
754 /// simple-template-id [TODO]
766 /// [GNU] typeof-specifier
768 /// [C++0x] 'auto' [TODO]
769 /// [C++0x] 'decltype' ( expression )
776 /// elaborated-type-specifier:
777 /// class-key '::'[opt] nested-name-specifier[opt] identifier
778 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
779 /// simple-template-id
780 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
786 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
787 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
790 /// class-head '{' member-specification[opt] '}'
793 /// class-key identifier[opt] base-clause[opt]
794 /// class-key nested-name-specifier identifier base-clause[opt]
795 /// class-key nested-name-specifier[opt] simple-template-id
808 Parser::TPResult
Parser::isCXXDeclarationSpecifier() {
809 switch (Tok
.getKind()) {
810 case tok::identifier
: // foo::bar
811 // Check for need to substitute AltiVec __vector keyword
812 // for "vector" identifier.
813 if (TryAltiVecVectorToken())
814 return TPResult::True();
816 case tok::kw_typename
: // typename T::type
817 // Annotate typenames and C++ scope specifiers. If we get one, just
818 // recurse to handle whatever we get.
819 if (TryAnnotateTypeOrScopeToken())
820 return TPResult::Error();
821 if (Tok
.is(tok::identifier
))
822 return TPResult::False();
823 return isCXXDeclarationSpecifier();
825 case tok::coloncolon
: { // ::foo::bar
826 const Token
&Next
= NextToken();
827 if (Next
.is(tok::kw_new
) || // ::new
828 Next
.is(tok::kw_delete
)) // ::delete
829 return TPResult::False();
831 // Annotate typenames and C++ scope specifiers. If we get one, just
832 // recurse to handle whatever we get.
833 if (TryAnnotateTypeOrScopeToken())
834 return TPResult::Error();
835 return isCXXDeclarationSpecifier();
839 // storage-class-specifier
841 // function-specifier
846 case tok::kw_typedef
:
847 case tok::kw_constexpr
:
848 // storage-class-specifier
849 case tok::kw_register
:
852 case tok::kw_mutable
:
854 case tok::kw___thread
:
855 // function-specifier
857 case tok::kw_virtual
:
858 case tok::kw_explicit
:
861 // simple-type-specifier
864 // elaborated-type-specifier
865 // typename-specifier
869 // elaborated-type-specifier
877 case tok::kw_volatile
:
880 case tok::kw_restrict
:
881 case tok::kw__Complex
:
882 case tok::kw___attribute
:
883 return TPResult::True();
886 case tok::kw___declspec
:
887 case tok::kw___cdecl
:
888 case tok::kw___stdcall
:
889 case tok::kw___fastcall
:
890 case tok::kw___thiscall
:
892 case tok::kw___ptr64
:
893 case tok::kw___forceinline
:
894 return TPResult::True();
897 case tok::kw___pascal
:
898 return TPResult::True();
901 case tok::kw___vector
:
902 return TPResult::True();
904 case tok::annot_template_id
: {
905 TemplateIdAnnotation
*TemplateId
906 = static_cast<TemplateIdAnnotation
*>(Tok
.getAnnotationValue());
907 if (TemplateId
->Kind
!= TNK_Type_template
)
908 return TPResult::False();
910 AnnotateTemplateIdTokenAsType(&SS
);
911 assert(Tok
.is(tok::annot_typename
));
915 case tok::annot_cxxscope
: // foo::bar or ::foo::bar, but already parsed
916 // We've already annotated a scope; try to annotate a type.
917 if (TryAnnotateTypeOrScopeToken())
918 return TPResult::Error();
919 if (!Tok
.is(tok::annot_typename
))
920 return TPResult::False();
921 // If that succeeded, fallthrough into the generic simple-type-id case.
923 // The ambiguity resides in a simple-type-specifier/typename-specifier
924 // followed by a '('. The '(' could either be the start of:
926 // direct-declarator:
927 // '(' declarator ')'
929 // direct-abstract-declarator:
930 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
931 // exception-specification[opt]
932 // '(' abstract-declarator ')'
934 // or part of a function-style cast expression:
936 // simple-type-specifier '(' expression-list[opt] ')'
939 // simple-type-specifier:
941 case tok::annot_typename
:
943 // In Objective-C, we might have a protocol-qualified type.
944 if (getLang().ObjC1
&& NextToken().is(tok::less
)) {
945 // Tentatively parse the
946 TentativeParsingAction
PA(*this);
947 ConsumeToken(); // The type token
949 TPResult TPR
= TryParseProtocolQualifiers();
950 bool isFollowedByParen
= Tok
.is(tok::l_paren
);
954 if (TPR
== TPResult::Error())
955 return TPResult::Error();
957 if (isFollowedByParen
)
958 return TPResult::Ambiguous();
960 return TPResult::True();
964 case tok::kw_wchar_t
:
965 case tok::kw_char16_t
:
966 case tok::kw_char32_t
:
972 case tok::kw_unsigned
:
976 if (NextToken().is(tok::l_paren
))
977 return TPResult::Ambiguous();
979 if (isStartOfObjCClassMessageMissingOpenBracket())
980 return TPResult::False();
982 return TPResult::True();
984 // GNU typeof support.
985 case tok::kw_typeof
: {
986 if (NextToken().isNot(tok::l_paren
))
987 return TPResult::True();
989 TentativeParsingAction
PA(*this);
991 TPResult TPR
= TryParseTypeofSpecifier();
992 bool isFollowedByParen
= Tok
.is(tok::l_paren
);
996 if (TPR
== TPResult::Error())
997 return TPResult::Error();
999 if (isFollowedByParen
)
1000 return TPResult::Ambiguous();
1002 return TPResult::True();
1005 // C++0x decltype support.
1006 case tok::kw_decltype
:
1007 return TPResult::True();
1010 return TPResult::False();
1014 /// [GNU] typeof-specifier:
1015 /// 'typeof' '(' expressions ')'
1016 /// 'typeof' '(' type-name ')'
1018 Parser::TPResult
Parser::TryParseTypeofSpecifier() {
1019 assert(Tok
.is(tok::kw_typeof
) && "Expected 'typeof'!");
1022 assert(Tok
.is(tok::l_paren
) && "Expected '('");
1023 // Parse through the parens after 'typeof'.
1025 if (!SkipUntil(tok::r_paren
))
1026 return TPResult::Error();
1028 return TPResult::Ambiguous();
1031 /// [ObjC] protocol-qualifiers:
1032 //// '<' identifier-list '>'
1033 Parser::TPResult
Parser::TryParseProtocolQualifiers() {
1034 assert(Tok
.is(tok::less
) && "Expected '<' for qualifier list");
1037 if (Tok
.isNot(tok::identifier
))
1038 return TPResult::Error();
1041 if (Tok
.is(tok::comma
)) {
1046 if (Tok
.is(tok::greater
)) {
1048 return TPResult::Ambiguous();
1052 return TPResult::Error();
1055 Parser::TPResult
Parser::TryParseDeclarationSpecifier() {
1056 TPResult TPR
= isCXXDeclarationSpecifier();
1057 if (TPR
!= TPResult::Ambiguous())
1060 if (Tok
.is(tok::kw_typeof
))
1061 TryParseTypeofSpecifier();
1065 if (getLang().ObjC1
&& Tok
.is(tok::less
))
1066 TryParseProtocolQualifiers();
1069 assert(Tok
.is(tok::l_paren
) && "Expected '('!");
1070 return TPResult::Ambiguous();
1073 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1074 /// a constructor-style initializer, when parsing declaration statements.
1075 /// Returns true for function declarator and false for constructor-style
1077 /// If during the disambiguation process a parsing error is encountered,
1078 /// the function returns true to let the declaration parsing code handle it.
1080 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1081 /// exception-specification[opt]
1083 bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous
) {
1086 // The ambiguity arising from the similarity between a function-style cast and
1087 // a declaration mentioned in 6.8 can also occur in the context of a
1088 // declaration. In that context, the choice is between a function declaration
1089 // with a redundant set of parentheses around a parameter name and an object
1090 // declaration with a function-style cast as the initializer. Just as for the
1091 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1092 // that could possibly be a declaration a declaration.
1094 TentativeParsingAction
PA(*this);
1097 TPResult TPR
= TryParseParameterDeclarationClause();
1098 if (TPR
== TPResult::Ambiguous() && Tok
.isNot(tok::r_paren
))
1099 TPR
= TPResult::False();
1101 SourceLocation TPLoc
= Tok
.getLocation();
1104 // In case of an error, let the declaration parsing code handle it.
1105 if (TPR
== TPResult::Error())
1108 if (TPR
== TPResult::Ambiguous()) {
1109 // Function declarator has precedence over constructor-style initializer.
1110 // Emit a warning just in case the author intended a variable definition.
1111 if (warnIfAmbiguous
)
1112 Diag(Tok
, diag::warn_parens_disambiguated_as_function_decl
)
1113 << SourceRange(Tok
.getLocation(), TPLoc
);
1117 return TPR
== TPResult::True();
1120 /// parameter-declaration-clause:
1121 /// parameter-declaration-list[opt] '...'[opt]
1122 /// parameter-declaration-list ',' '...'
1124 /// parameter-declaration-list:
1125 /// parameter-declaration
1126 /// parameter-declaration-list ',' parameter-declaration
1128 /// parameter-declaration:
1129 /// decl-specifier-seq declarator attributes[opt]
1130 /// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1131 /// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1132 /// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1133 /// '=' assignment-expression
1135 Parser::TPResult
Parser::TryParseParameterDeclarationClause() {
1137 if (Tok
.is(tok::r_paren
))
1138 return TPResult::True();
1140 // parameter-declaration-list[opt] '...'[opt]
1141 // parameter-declaration-list ',' '...'
1143 // parameter-declaration-list:
1144 // parameter-declaration
1145 // parameter-declaration-list ',' parameter-declaration
1149 if (Tok
.is(tok::ellipsis
)) {
1151 return TPResult::True(); // '...' is a sign of a function declarator.
1154 ParsedAttributes attrs
;
1155 MaybeParseMicrosoftAttributes(attrs
);
1157 // decl-specifier-seq
1158 TPResult TPR
= TryParseDeclarationSpecifier();
1159 if (TPR
!= TPResult::Ambiguous())
1163 // abstract-declarator[opt]
1164 TPR
= TryParseDeclarator(true/*mayBeAbstract*/);
1165 if (TPR
!= TPResult::Ambiguous())
1168 // [GNU] attributes[opt]
1169 if (Tok
.is(tok::kw___attribute
))
1170 return TPResult::True();
1172 if (Tok
.is(tok::equal
)) {
1173 // '=' assignment-expression
1174 // Parse through assignment-expression.
1175 tok::TokenKind StopToks
[2] ={ tok::comma
, tok::r_paren
};
1176 if (!SkipUntil(StopToks
, 2, true/*StopAtSemi*/, true/*DontConsume*/))
1177 return TPResult::Error();
1180 if (Tok
.is(tok::ellipsis
)) {
1182 return TPResult::True(); // '...' is a sign of a function declarator.
1185 if (Tok
.isNot(tok::comma
))
1187 ConsumeToken(); // the comma.
1190 return TPResult::Ambiguous();
1193 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1194 /// parsing as a function declarator.
1195 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1196 /// return TPResult::Ambiguous(), otherwise it will return either False() or
1199 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1200 /// exception-specification[opt]
1202 /// exception-specification:
1203 /// 'throw' '(' type-id-list[opt] ')'
1205 Parser::TPResult
Parser::TryParseFunctionDeclarator() {
1207 // The '(' is already parsed.
1209 TPResult TPR
= TryParseParameterDeclarationClause();
1210 if (TPR
== TPResult::Ambiguous() && Tok
.isNot(tok::r_paren
))
1211 TPR
= TPResult::False();
1213 if (TPR
== TPResult::False() || TPR
== TPResult::Error())
1216 // Parse through the parens.
1217 if (!SkipUntil(tok::r_paren
))
1218 return TPResult::Error();
1221 while (Tok
.is(tok::kw_const
) ||
1222 Tok
.is(tok::kw_volatile
) ||
1223 Tok
.is(tok::kw_restrict
) )
1226 // ref-qualifier[opt]
1227 if (Tok
.is(tok::amp
) || Tok
.is(tok::ampamp
))
1230 // exception-specification
1231 if (Tok
.is(tok::kw_throw
)) {
1233 if (Tok
.isNot(tok::l_paren
))
1234 return TPResult::Error();
1236 // Parse through the parens after 'throw'.
1238 if (!SkipUntil(tok::r_paren
))
1239 return TPResult::Error();
1242 return TPResult::Ambiguous();
1245 /// '[' constant-expression[opt] ']'
1247 Parser::TPResult
Parser::TryParseBracketDeclarator() {
1249 if (!SkipUntil(tok::r_square
))
1250 return TPResult::Error();
1252 return TPResult::Ambiguous();