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();
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 bool Parser::isCXXSimpleDeclaration() {
80 // There is an ambiguity in the grammar involving expression-statements and
81 // declarations: An expression-statement with a function-style explicit type
82 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
83 // from a declaration where the first declarator starts with a '('. In those
84 // cases the statement is a declaration. [Note: To disambiguate, the whole
85 // statement might have to be examined to determine if it is an
86 // expression-statement or a declaration].
89 // The disambiguation is purely syntactic; that is, the meaning of the names
90 // occurring in such a statement, beyond whether they are type-names or not,
91 // is not generally used in or changed by the disambiguation. Class
92 // templates are instantiated as necessary to determine if a qualified name
93 // is a type-name. Disambiguation precedes parsing, and a statement
94 // disambiguated as a declaration may be an ill-formed declaration.
96 // We don't have to parse all of the decl-specifier-seq part. There's only
97 // an ambiguity if the first decl-specifier is
98 // simple-type-specifier/typename-specifier followed by a '(', which may
99 // indicate a function-style cast expression.
100 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
103 TPResult TPR = isCXXDeclarationSpecifier();
104 if (TPR != TPResult::Ambiguous())
105 return TPR != TPResult::False(); // Returns true for TPResult::True() or
106 // TPResult::Error().
108 // FIXME: Add statistics about the number of ambiguous statements encountered
109 // and how they were resolved (number of declarations+number of expressions).
111 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
112 // We need tentative parsing...
114 TentativeParsingAction PA(*this);
115 TPR = TryParseSimpleDeclaration();
118 // In case of an error, let the declaration parsing code handle it.
119 if (TPR == TPResult::Error())
122 // Declarations take precedence over expressions.
123 if (TPR == TPResult::Ambiguous())
124 TPR = TPResult::True();
126 assert(TPR == TPResult::True() || TPR == TPResult::False());
127 return TPR == TPResult::True();
130 /// simple-declaration:
131 /// decl-specifier-seq init-declarator-list[opt] ';'
133 Parser::TPResult Parser::TryParseSimpleDeclaration() {
134 // We know that we have a simple-type-specifier/typename-specifier followed
136 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
138 if (Tok.is(tok::kw_typeof))
139 TryParseTypeofSpecifier();
143 if (getLang().ObjC1 && Tok.is(tok::less))
144 TryParseProtocolQualifiers();
147 assert(Tok.is(tok::l_paren) && "Expected '('");
149 TPResult TPR = TryParseInitDeclaratorList();
150 if (TPR != TPResult::Ambiguous())
153 if (Tok.isNot(tok::semi))
154 return TPResult::False();
156 return TPResult::Ambiguous();
159 /// init-declarator-list:
161 /// init-declarator-list ',' init-declarator
164 /// declarator initializer[opt]
165 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
168 /// '=' initializer-clause
169 /// '(' expression-list ')'
171 /// initializer-clause:
172 /// assignment-expression
173 /// '{' initializer-list ','[opt] '}'
176 Parser::TPResult Parser::TryParseInitDeclaratorList() {
179 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
180 if (TPR != TPResult::Ambiguous())
183 // [GNU] simple-asm-expr[opt] attributes[opt]
184 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
185 return TPResult::True();
188 if (Tok.is(tok::l_paren)) {
189 // Parse through the parens.
191 if (!SkipUntil(tok::r_paren))
192 return TPResult::Error();
193 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
194 // MSVC and g++ won't examine the rest of declarators if '=' is
195 // encountered; they just conclude that we have a declaration.
196 // EDG parses the initializer completely, which is the proper behavior
199 // At present, Clang follows MSVC and g++, since the parser does not have
200 // the ability to parse an expression fully without recording the
201 // results of that parse.
202 // Also allow 'in' after on objective-c declaration as in:
203 // for (int (^b)(void) in array). Ideally this should be done in the
204 // context of parsing for-init-statement of a foreach statement only. But,
205 // in any other context 'in' is invalid after a declaration and parser
206 // issues the error regardless of outcome of this decision.
207 // FIXME. Change if above assumption does not hold.
208 return TPResult::True();
211 if (Tok.isNot(tok::comma))
213 ConsumeToken(); // the comma.
216 return TPResult::Ambiguous();
219 /// isCXXConditionDeclaration - Disambiguates between a declaration or an
220 /// expression for a condition of a if/switch/while/for statement.
221 /// If during the disambiguation process a parsing error is encountered,
222 /// the function returns true to let the declaration parsing code handle it.
226 /// type-specifier-seq declarator '=' assignment-expression
227 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
228 /// '=' assignment-expression
230 bool Parser::isCXXConditionDeclaration() {
231 TPResult TPR = isCXXDeclarationSpecifier();
232 if (TPR != TPResult::Ambiguous())
233 return TPR != TPResult::False(); // Returns true for TPResult::True() or
234 // TPResult::Error().
236 // FIXME: Add statistics about the number of ambiguous statements encountered
237 // and how they were resolved (number of declarations+number of expressions).
239 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
240 // We need tentative parsing...
242 TentativeParsingAction PA(*this);
244 // type-specifier-seq
245 if (Tok.is(tok::kw_typeof))
246 TryParseTypeofSpecifier();
250 if (getLang().ObjC1 && Tok.is(tok::less))
251 TryParseProtocolQualifiers();
253 assert(Tok.is(tok::l_paren) && "Expected '('");
256 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
258 // In case of an error, let the declaration parsing code handle it.
259 if (TPR == TPResult::Error())
260 TPR = TPResult::True();
262 if (TPR == TPResult::Ambiguous()) {
264 // [GNU] simple-asm-expr[opt] attributes[opt]
265 if (Tok.is(tok::equal) ||
266 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
267 TPR = TPResult::True();
269 TPR = TPResult::False();
274 assert(TPR == TPResult::True() || TPR == TPResult::False());
275 return TPR == TPResult::True();
278 /// \brief Determine whether the next set of tokens contains a type-id.
280 /// The context parameter states what context we're parsing right
281 /// now, which affects how this routine copes with the token
282 /// following the type-id. If the context is TypeIdInParens, we have
283 /// already parsed the '(' and we will cease lookahead when we hit
284 /// the corresponding ')'. If the context is
285 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
286 /// before this template argument, and will cease lookahead when we
287 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
288 /// and false for an expression. If during the disambiguation
289 /// process a parsing error is encountered, the function returns
290 /// true to let the declaration parsing code handle it.
293 /// type-specifier-seq abstract-declarator[opt]
295 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
300 // The ambiguity arising from the similarity between a function-style cast and
301 // a type-id can occur in different contexts. The ambiguity appears as a
302 // choice between a function-style cast expression and a declaration of a
303 // type. The resolution is that any construct that could possibly be a type-id
304 // in its syntactic context shall be considered a type-id.
306 TPResult TPR = isCXXDeclarationSpecifier();
307 if (TPR != TPResult::Ambiguous())
308 return TPR != TPResult::False(); // Returns true for TPResult::True() or
309 // TPResult::Error().
311 // FIXME: Add statistics about the number of ambiguous statements encountered
312 // and how they were resolved (number of declarations+number of expressions).
314 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
315 // We need tentative parsing...
317 TentativeParsingAction PA(*this);
319 // type-specifier-seq
320 if (Tok.is(tok::kw_typeof))
321 TryParseTypeofSpecifier();
325 if (getLang().ObjC1 && Tok.is(tok::less))
326 TryParseProtocolQualifiers();
329 assert(Tok.is(tok::l_paren) && "Expected '('");
332 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
334 // In case of an error, let the declaration parsing code handle it.
335 if (TPR == TPResult::Error())
336 TPR = TPResult::True();
338 if (TPR == TPResult::Ambiguous()) {
339 // We are supposed to be inside parens, so if after the abstract declarator
340 // we encounter a ')' this is a type-id, otherwise it's an expression.
341 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
342 TPR = TPResult::True();
345 // We are supposed to be inside a template argument, so if after
346 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
347 // ',', this is a type-id. Otherwise, it's an expression.
348 } else if (Context == TypeIdAsTemplateArgument &&
349 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
350 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
351 TPR = TPResult::True();
355 TPR = TPResult::False();
360 assert(TPR == TPResult::True() || TPR == TPResult::False());
361 return TPR == TPResult::True();
364 /// isCXX0XAttributeSpecifier - returns true if this is a C++0x
365 /// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
366 /// performed that will simply return true if a [[ is seen. Currently C++ has no
367 /// syntactical ambiguities from this check, but it may inhibit error recovery.
368 /// If CheckClosing is true, a check is made for closing ]] brackets.
370 /// If given, After is set to the token after the attribute-specifier so that
371 /// appropriate parsing decisions can be made; it is left untouched if false is
374 /// FIXME: If an error is in the closing ]] brackets, the program assumes
375 /// the absence of an attribute-specifier, which can cause very yucky errors
378 /// [C++0x] attribute-specifier:
379 /// '[' '[' attribute-list ']' ']'
381 /// [C++0x] attribute-list:
383 /// attribute-list ',' attribute[opt]
385 /// [C++0x] attribute:
386 /// attribute-token attribute-argument-clause[opt]
388 /// [C++0x] attribute-token:
390 /// attribute-scoped-token
392 /// [C++0x] attribute-scoped-token:
393 /// attribute-namespace '::' identifier
395 /// [C++0x] attribute-namespace:
398 /// [C++0x] attribute-argument-clause:
399 /// '(' balanced-token-seq ')'
401 /// [C++0x] balanced-token-seq:
403 /// balanced-token-seq balanced-token
405 /// [C++0x] balanced-token:
406 /// '(' balanced-token-seq ')'
407 /// '[' balanced-token-seq ']'
408 /// '{' balanced-token-seq '}'
409 /// any token but '(', ')', '[', ']', '{', or '}'
410 bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
411 tok::TokenKind *After) {
412 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
415 // No tentative parsing if we don't need to look for ]]
416 if (!CheckClosing && !getLang().ObjC1)
419 struct TentativeReverter {
420 TentativeParsingAction PA;
422 TentativeReverter (Parser& P)
425 ~TentativeReverter () {
430 // Opening brackets were checked for above.
434 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
435 SkipUntil(tok::r_square, false);
437 if (Tok.isNot(tok::r_square))
442 *After = Tok.getKind();
448 /// direct-declarator
449 /// ptr-operator declarator
451 /// direct-declarator:
453 /// direct-declarator '(' parameter-declaration-clause ')'
454 /// cv-qualifier-seq[opt] exception-specification[opt]
455 /// direct-declarator '[' constant-expression[opt] ']'
456 /// '(' declarator ')'
457 /// [GNU] '(' attributes declarator ')'
459 /// abstract-declarator:
460 /// ptr-operator abstract-declarator[opt]
461 /// direct-abstract-declarator
464 /// direct-abstract-declarator:
465 /// direct-abstract-declarator[opt]
466 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
467 /// exception-specification[opt]
468 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
469 /// '(' abstract-declarator ')'
472 /// '*' cv-qualifier-seq[opt]
474 /// [C++0x] '&&' [TODO]
475 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
477 /// cv-qualifier-seq:
478 /// cv-qualifier cv-qualifier-seq[opt]
485 /// '...'[opt] id-expression
489 /// qualified-id [TODO]
493 /// operator-function-id [TODO]
494 /// conversion-function-id [TODO]
495 /// '~' class-name [TODO]
496 /// template-id [TODO]
498 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
499 bool mayHaveIdentifier) {
502 // ptr-operator declarator
505 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
506 if (TryAnnotateCXXScopeToken(true))
507 return TPResult::Error();
509 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
510 Tok.is(tok::ampamp) ||
511 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
514 while (Tok.is(tok::kw_const) ||
515 Tok.is(tok::kw_volatile) ||
516 Tok.is(tok::kw_restrict))
523 // direct-declarator:
524 // direct-abstract-declarator:
525 if (Tok.is(tok::ellipsis))
528 if ((Tok.is(tok::identifier) ||
529 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
532 if (Tok.is(tok::annot_cxxscope))
535 } else if (Tok.is(tok::l_paren)) {
538 (Tok.is(tok::r_paren) || // 'int()' is a function.
539 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
540 isDeclarationSpecifier())) { // 'int(int)' is a function.
541 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
542 // exception-specification[opt]
543 TPResult TPR = TryParseFunctionDeclarator();
544 if (TPR != TPResult::Ambiguous())
547 // '(' declarator ')'
548 // '(' attributes declarator ')'
549 // '(' abstract-declarator ')'
550 if (Tok.is(tok::kw___attribute) ||
551 Tok.is(tok::kw___declspec) ||
552 Tok.is(tok::kw___cdecl) ||
553 Tok.is(tok::kw___stdcall) ||
554 Tok.is(tok::kw___fastcall) ||
555 Tok.is(tok::kw___thiscall))
556 return TPResult::True(); // attributes indicate declaration
557 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
558 if (TPR != TPResult::Ambiguous())
560 if (Tok.isNot(tok::r_paren))
561 return TPResult::False();
564 } else if (!mayBeAbstract) {
565 return TPResult::False();
569 TPResult TPR(TPResult::Ambiguous());
571 // abstract-declarator: ...
572 if (Tok.is(tok::ellipsis))
575 if (Tok.is(tok::l_paren)) {
576 // Check whether we have a function declarator or a possible ctor-style
577 // initializer that follows the declarator. Note that ctor-style
578 // initializers are not possible in contexts where abstract declarators
580 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
583 // direct-declarator '(' parameter-declaration-clause ')'
584 // cv-qualifier-seq[opt] exception-specification[opt]
586 TPR = TryParseFunctionDeclarator();
587 } else if (Tok.is(tok::l_square)) {
588 // direct-declarator '[' constant-expression[opt] ']'
589 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
590 TPR = TryParseBracketDeclarator();
595 if (TPR != TPResult::Ambiguous())
599 return TPResult::Ambiguous();
603 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
605 // Obviously starts an expression.
606 case tok::numeric_constant:
607 case tok::char_constant:
608 case tok::string_literal:
609 case tok::wide_string_literal:
618 case tok::minusminus:
622 case tok::kw___func__:
623 case tok::kw_const_cast:
625 case tok::kw_dynamic_cast:
628 case tok::kw_operator:
629 case tok::kw_reinterpret_cast:
630 case tok::kw_static_cast:
635 case tok::kw_alignof:
636 case tok::kw_noexcept:
637 case tok::kw_nullptr:
639 case tok::kw___alignof:
640 case tok::kw___builtin_choose_expr:
641 case tok::kw___builtin_offsetof:
642 case tok::kw___builtin_types_compatible_p:
643 case tok::kw___builtin_va_arg:
646 case tok::kw___FUNCTION__:
647 case tok::kw___PRETTY_FUNCTION__:
648 case tok::kw___has_nothrow_assign:
649 case tok::kw___has_nothrow_copy:
650 case tok::kw___has_nothrow_constructor:
651 case tok::kw___has_trivial_assign:
652 case tok::kw___has_trivial_copy:
653 case tok::kw___has_trivial_constructor:
654 case tok::kw___has_trivial_destructor:
655 case tok::kw___has_virtual_destructor:
656 case tok::kw___is_abstract:
657 case tok::kw___is_base_of:
658 case tok::kw___is_class:
659 case tok::kw___is_convertible_to:
660 case tok::kw___is_empty:
661 case tok::kw___is_enum:
662 case tok::kw___is_literal:
663 case tok::kw___is_literal_type:
664 case tok::kw___is_pod:
665 case tok::kw___is_polymorphic:
666 case tok::kw___is_trivial:
667 case tok::kw___is_trivially_copyable:
668 case tok::kw___is_union:
669 case tok::kw___uuidof:
670 return TPResult::True();
672 // Obviously starts a type-specifier-seq:
680 case tok::kw___int64:
681 case tok::kw_restrict:
686 case tok::kw_unsigned:
688 case tok::kw_volatile:
690 case tok::kw__Complex:
692 case tok::kw_typename:
693 case tok::kw_wchar_t:
694 case tok::kw_char16_t:
695 case tok::kw_char32_t:
696 case tok::kw_decltype:
697 case tok::kw___underlying_type:
698 case tok::kw_thread_local:
699 case tok::kw__Decimal32:
700 case tok::kw__Decimal64:
701 case tok::kw__Decimal128:
702 case tok::kw___thread:
704 case tok::kw___cdecl:
705 case tok::kw___stdcall:
706 case tok::kw___fastcall:
707 case tok::kw___thiscall:
708 case tok::kw___vector:
709 case tok::kw___pixel:
710 return TPResult::False();
716 return TPResult::Ambiguous();
719 /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
720 /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
721 /// be either a decl-specifier or a function-style cast, and TPResult::Error()
722 /// if a parsing error was found and reported.
725 /// storage-class-specifier
727 /// function-specifier
730 /// [C++0x] 'constexpr'
731 /// [GNU] attributes declaration-specifiers[opt]
733 /// storage-class-specifier:
741 /// function-specifier:
750 /// simple-type-specifier
753 /// elaborated-type-specifier
754 /// typename-specifier
757 /// simple-type-specifier:
758 /// '::'[opt] nested-name-specifier[opt] type-name
759 /// '::'[opt] nested-name-specifier 'template'
760 /// simple-template-id [TODO]
772 /// [GNU] typeof-specifier
774 /// [C++0x] 'auto' [TODO]
775 /// [C++0x] 'decltype' ( expression )
782 /// elaborated-type-specifier:
783 /// class-key '::'[opt] nested-name-specifier[opt] identifier
784 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
785 /// simple-template-id
786 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
792 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
793 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
796 /// class-head '{' member-specification[opt] '}'
799 /// class-key identifier[opt] base-clause[opt]
800 /// class-key nested-name-specifier identifier base-clause[opt]
801 /// class-key nested-name-specifier[opt] simple-template-id
814 Parser::TPResult Parser::isCXXDeclarationSpecifier() {
815 switch (Tok.getKind()) {
816 case tok::identifier: // foo::bar
817 // Check for need to substitute AltiVec __vector keyword
818 // for "vector" identifier.
819 if (TryAltiVecVectorToken())
820 return TPResult::True();
822 case tok::kw_typename: // typename T::type
823 // Annotate typenames and C++ scope specifiers. If we get one, just
824 // recurse to handle whatever we get.
825 if (TryAnnotateTypeOrScopeToken())
826 return TPResult::Error();
827 if (Tok.is(tok::identifier))
828 return TPResult::False();
829 return isCXXDeclarationSpecifier();
831 case tok::coloncolon: { // ::foo::bar
832 const Token &Next = NextToken();
833 if (Next.is(tok::kw_new) || // ::new
834 Next.is(tok::kw_delete)) // ::delete
835 return TPResult::False();
837 // Annotate typenames and C++ scope specifiers. If we get one, just
838 // recurse to handle whatever we get.
839 if (TryAnnotateTypeOrScopeToken())
840 return TPResult::Error();
841 return isCXXDeclarationSpecifier();
845 // storage-class-specifier
847 // function-specifier
852 case tok::kw_typedef:
853 case tok::kw_constexpr:
854 // storage-class-specifier
855 case tok::kw_register:
858 case tok::kw_mutable:
860 case tok::kw___thread:
861 // function-specifier
863 case tok::kw_virtual:
864 case tok::kw_explicit:
867 // simple-type-specifier
870 // elaborated-type-specifier
871 // typename-specifier
875 // elaborated-type-specifier
883 case tok::kw_volatile:
886 case tok::kw_restrict:
887 case tok::kw__Complex:
888 case tok::kw___attribute:
889 return TPResult::True();
892 case tok::kw___declspec:
893 case tok::kw___cdecl:
894 case tok::kw___stdcall:
895 case tok::kw___fastcall:
896 case tok::kw___thiscall:
898 case tok::kw___ptr64:
899 case tok::kw___forceinline:
900 return TPResult::True();
903 case tok::kw___pascal:
904 return TPResult::True();
907 case tok::kw___vector:
908 return TPResult::True();
910 case tok::annot_template_id: {
911 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
912 if (TemplateId->Kind != TNK_Type_template)
913 return TPResult::False();
915 AnnotateTemplateIdTokenAsType();
916 assert(Tok.is(tok::annot_typename));
920 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
921 // We've already annotated a scope; try to annotate a type.
922 if (TryAnnotateTypeOrScopeToken())
923 return TPResult::Error();
924 if (!Tok.is(tok::annot_typename))
925 return TPResult::False();
926 // If that succeeded, fallthrough into the generic simple-type-id case.
928 // The ambiguity resides in a simple-type-specifier/typename-specifier
929 // followed by a '('. The '(' could either be the start of:
931 // direct-declarator:
932 // '(' declarator ')'
934 // direct-abstract-declarator:
935 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
936 // exception-specification[opt]
937 // '(' abstract-declarator ')'
939 // or part of a function-style cast expression:
941 // simple-type-specifier '(' expression-list[opt] ')'
944 // simple-type-specifier:
946 case tok::annot_typename:
948 // In Objective-C, we might have a protocol-qualified type.
949 if (getLang().ObjC1 && NextToken().is(tok::less)) {
950 // Tentatively parse the
951 TentativeParsingAction PA(*this);
952 ConsumeToken(); // The type token
954 TPResult TPR = TryParseProtocolQualifiers();
955 bool isFollowedByParen = Tok.is(tok::l_paren);
959 if (TPR == TPResult::Error())
960 return TPResult::Error();
962 if (isFollowedByParen)
963 return TPResult::Ambiguous();
965 return TPResult::True();
969 case tok::kw_wchar_t:
970 case tok::kw_char16_t:
971 case tok::kw_char32_t:
976 case tok::kw___int64:
978 case tok::kw_unsigned:
982 if (NextToken().is(tok::l_paren))
983 return TPResult::Ambiguous();
985 if (isStartOfObjCClassMessageMissingOpenBracket())
986 return TPResult::False();
988 return TPResult::True();
990 // GNU typeof support.
991 case tok::kw_typeof: {
992 if (NextToken().isNot(tok::l_paren))
993 return TPResult::True();
995 TentativeParsingAction PA(*this);
997 TPResult TPR = TryParseTypeofSpecifier();
998 bool isFollowedByParen = Tok.is(tok::l_paren);
1002 if (TPR == TPResult::Error())
1003 return TPResult::Error();
1005 if (isFollowedByParen)
1006 return TPResult::Ambiguous();
1008 return TPResult::True();
1011 // C++0x decltype support.
1012 case tok::kw_decltype:
1013 return TPResult::True();
1015 // C++0x type traits support
1016 case tok::kw___underlying_type:
1017 return TPResult::True();
1020 return TPResult::False();
1024 /// [GNU] typeof-specifier:
1025 /// 'typeof' '(' expressions ')'
1026 /// 'typeof' '(' type-name ')'
1028 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1029 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1032 assert(Tok.is(tok::l_paren) && "Expected '('");
1033 // Parse through the parens after 'typeof'.
1035 if (!SkipUntil(tok::r_paren))
1036 return TPResult::Error();
1038 return TPResult::Ambiguous();
1041 /// [ObjC] protocol-qualifiers:
1042 //// '<' identifier-list '>'
1043 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1044 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1047 if (Tok.isNot(tok::identifier))
1048 return TPResult::Error();
1051 if (Tok.is(tok::comma)) {
1056 if (Tok.is(tok::greater)) {
1058 return TPResult::Ambiguous();
1062 return TPResult::Error();
1065 Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1066 TPResult TPR = isCXXDeclarationSpecifier();
1067 if (TPR != TPResult::Ambiguous())
1070 if (Tok.is(tok::kw_typeof))
1071 TryParseTypeofSpecifier();
1075 if (getLang().ObjC1 && Tok.is(tok::less))
1076 TryParseProtocolQualifiers();
1079 assert(Tok.is(tok::l_paren) && "Expected '('!");
1080 return TPResult::Ambiguous();
1083 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1084 /// a constructor-style initializer, when parsing declaration statements.
1085 /// Returns true for function declarator and false for constructor-style
1087 /// If during the disambiguation process a parsing error is encountered,
1088 /// the function returns true to let the declaration parsing code handle it.
1090 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1091 /// exception-specification[opt]
1093 bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
1096 // The ambiguity arising from the similarity between a function-style cast and
1097 // a declaration mentioned in 6.8 can also occur in the context of a
1098 // declaration. In that context, the choice is between a function declaration
1099 // with a redundant set of parentheses around a parameter name and an object
1100 // declaration with a function-style cast as the initializer. Just as for the
1101 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1102 // that could possibly be a declaration a declaration.
1104 TentativeParsingAction PA(*this);
1107 TPResult TPR = TryParseParameterDeclarationClause();
1108 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1109 TPR = TPResult::False();
1111 SourceLocation TPLoc = Tok.getLocation();
1114 // In case of an error, let the declaration parsing code handle it.
1115 if (TPR == TPResult::Error())
1118 if (TPR == TPResult::Ambiguous()) {
1119 // Function declarator has precedence over constructor-style initializer.
1120 // Emit a warning just in case the author intended a variable definition.
1121 if (warnIfAmbiguous)
1122 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1123 << SourceRange(Tok.getLocation(), TPLoc);
1127 return TPR == TPResult::True();
1130 /// parameter-declaration-clause:
1131 /// parameter-declaration-list[opt] '...'[opt]
1132 /// parameter-declaration-list ',' '...'
1134 /// parameter-declaration-list:
1135 /// parameter-declaration
1136 /// parameter-declaration-list ',' parameter-declaration
1138 /// parameter-declaration:
1139 /// decl-specifier-seq declarator attributes[opt]
1140 /// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1141 /// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1142 /// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1143 /// '=' assignment-expression
1145 Parser::TPResult Parser::TryParseParameterDeclarationClause() {
1147 if (Tok.is(tok::r_paren))
1148 return TPResult::True();
1150 // parameter-declaration-list[opt] '...'[opt]
1151 // parameter-declaration-list ',' '...'
1153 // parameter-declaration-list:
1154 // parameter-declaration
1155 // parameter-declaration-list ',' parameter-declaration
1159 if (Tok.is(tok::ellipsis)) {
1161 return TPResult::True(); // '...' is a sign of a function declarator.
1164 ParsedAttributes attrs(AttrFactory);
1165 MaybeParseMicrosoftAttributes(attrs);
1167 // decl-specifier-seq
1168 TPResult TPR = TryParseDeclarationSpecifier();
1169 if (TPR != TPResult::Ambiguous())
1173 // abstract-declarator[opt]
1174 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1175 if (TPR != TPResult::Ambiguous())
1178 // [GNU] attributes[opt]
1179 if (Tok.is(tok::kw___attribute))
1180 return TPResult::True();
1182 if (Tok.is(tok::equal)) {
1183 // '=' assignment-expression
1184 // Parse through assignment-expression.
1185 tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1186 if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
1187 return TPResult::Error();
1190 if (Tok.is(tok::ellipsis)) {
1192 return TPResult::True(); // '...' is a sign of a function declarator.
1195 if (Tok.isNot(tok::comma))
1197 ConsumeToken(); // the comma.
1200 return TPResult::Ambiguous();
1203 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1204 /// parsing as a function declarator.
1205 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1206 /// return TPResult::Ambiguous(), otherwise it will return either False() or
1209 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1210 /// exception-specification[opt]
1212 /// exception-specification:
1213 /// 'throw' '(' type-id-list[opt] ')'
1215 Parser::TPResult Parser::TryParseFunctionDeclarator() {
1217 // The '(' is already parsed.
1219 TPResult TPR = TryParseParameterDeclarationClause();
1220 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1221 TPR = TPResult::False();
1223 if (TPR == TPResult::False() || TPR == TPResult::Error())
1226 // Parse through the parens.
1227 if (!SkipUntil(tok::r_paren))
1228 return TPResult::Error();
1231 while (Tok.is(tok::kw_const) ||
1232 Tok.is(tok::kw_volatile) ||
1233 Tok.is(tok::kw_restrict) )
1236 // ref-qualifier[opt]
1237 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1240 // exception-specification
1241 if (Tok.is(tok::kw_throw)) {
1243 if (Tok.isNot(tok::l_paren))
1244 return TPResult::Error();
1246 // Parse through the parens after 'throw'.
1248 if (!SkipUntil(tok::r_paren))
1249 return TPResult::Error();
1251 if (Tok.is(tok::kw_noexcept)) {
1253 // Possibly an expression as well.
1254 if (Tok.is(tok::l_paren)) {
1255 // Find the matching rparen.
1257 if (!SkipUntil(tok::r_paren))
1258 return TPResult::Error();
1262 return TPResult::Ambiguous();
1265 /// '[' constant-expression[opt] ']'
1267 Parser::TPResult Parser::TryParseBracketDeclarator() {
1269 if (!SkipUntil(tok::r_square))
1270 return TPResult::Error();
1272 return TPResult::Ambiguous();