xref: /minix3/external/bsd/llvm/dist/clang/lib/Parse/ParseTentative.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file implements the tentative parsing portions of the Parser
11f4a2713aSLionel Sambuc //  interfaces, for ambiguity resolution.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h"
16f4a2713aSLionel Sambuc #include "clang/Parse/ParseDiagnostic.h"
17f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
18f4a2713aSLionel Sambuc using namespace clang;
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc /// isCXXDeclarationStatement - C++-specialized function that disambiguates
21f4a2713aSLionel Sambuc /// between a declaration or an expression statement, when parsing function
22f4a2713aSLionel Sambuc /// bodies. Returns true for declaration, false for expression.
23f4a2713aSLionel Sambuc ///
24f4a2713aSLionel Sambuc ///         declaration-statement:
25f4a2713aSLionel Sambuc ///           block-declaration
26f4a2713aSLionel Sambuc ///
27f4a2713aSLionel Sambuc ///         block-declaration:
28f4a2713aSLionel Sambuc ///           simple-declaration
29f4a2713aSLionel Sambuc ///           asm-definition
30f4a2713aSLionel Sambuc ///           namespace-alias-definition
31f4a2713aSLionel Sambuc ///           using-declaration
32f4a2713aSLionel Sambuc ///           using-directive
33f4a2713aSLionel Sambuc /// [C++0x]   static_assert-declaration
34f4a2713aSLionel Sambuc ///
35f4a2713aSLionel Sambuc ///         asm-definition:
36f4a2713aSLionel Sambuc ///           'asm' '(' string-literal ')' ';'
37f4a2713aSLionel Sambuc ///
38f4a2713aSLionel Sambuc ///         namespace-alias-definition:
39f4a2713aSLionel Sambuc ///           'namespace' identifier = qualified-namespace-specifier ';'
40f4a2713aSLionel Sambuc ///
41f4a2713aSLionel Sambuc ///         using-declaration:
42f4a2713aSLionel Sambuc ///           'using' typename[opt] '::'[opt] nested-name-specifier
43f4a2713aSLionel Sambuc ///                 unqualified-id ';'
44f4a2713aSLionel Sambuc ///           'using' '::' unqualified-id ;
45f4a2713aSLionel Sambuc ///
46f4a2713aSLionel Sambuc ///         using-directive:
47f4a2713aSLionel Sambuc ///           'using' 'namespace' '::'[opt] nested-name-specifier[opt]
48f4a2713aSLionel Sambuc ///                 namespace-name ';'
49f4a2713aSLionel Sambuc ///
isCXXDeclarationStatement()50f4a2713aSLionel Sambuc bool Parser::isCXXDeclarationStatement() {
51f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
52f4a2713aSLionel Sambuc     // asm-definition
53f4a2713aSLionel Sambuc   case tok::kw_asm:
54f4a2713aSLionel Sambuc     // namespace-alias-definition
55f4a2713aSLionel Sambuc   case tok::kw_namespace:
56f4a2713aSLionel Sambuc     // using-declaration
57f4a2713aSLionel Sambuc     // using-directive
58f4a2713aSLionel Sambuc   case tok::kw_using:
59f4a2713aSLionel Sambuc     // static_assert-declaration
60f4a2713aSLionel Sambuc   case tok::kw_static_assert:
61f4a2713aSLionel Sambuc   case tok::kw__Static_assert:
62f4a2713aSLionel Sambuc     return true;
63f4a2713aSLionel Sambuc     // simple-declaration
64f4a2713aSLionel Sambuc   default:
65f4a2713aSLionel Sambuc     return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
66f4a2713aSLionel Sambuc   }
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc /// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70f4a2713aSLionel Sambuc /// between a simple-declaration or an expression-statement.
71f4a2713aSLionel Sambuc /// If during the disambiguation process a parsing error is encountered,
72f4a2713aSLionel Sambuc /// the function returns true to let the declaration parsing code handle it.
73f4a2713aSLionel Sambuc /// Returns false if the statement is disambiguated as expression.
74f4a2713aSLionel Sambuc ///
75f4a2713aSLionel Sambuc /// simple-declaration:
76f4a2713aSLionel Sambuc ///   decl-specifier-seq init-declarator-list[opt] ';'
77f4a2713aSLionel Sambuc ///
78f4a2713aSLionel Sambuc /// (if AllowForRangeDecl specified)
79f4a2713aSLionel Sambuc /// for ( for-range-declaration : for-range-initializer ) statement
80f4a2713aSLionel Sambuc /// for-range-declaration:
81f4a2713aSLionel Sambuc ///    attribute-specifier-seqopt type-specifier-seq declarator
isCXXSimpleDeclaration(bool AllowForRangeDecl)82f4a2713aSLionel Sambuc bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
83f4a2713aSLionel Sambuc   // C++ 6.8p1:
84f4a2713aSLionel Sambuc   // There is an ambiguity in the grammar involving expression-statements and
85f4a2713aSLionel Sambuc   // declarations: An expression-statement with a function-style explicit type
86f4a2713aSLionel Sambuc   // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87f4a2713aSLionel Sambuc   // from a declaration where the first declarator starts with a '('. In those
88f4a2713aSLionel Sambuc   // cases the statement is a declaration. [Note: To disambiguate, the whole
89f4a2713aSLionel Sambuc   // statement might have to be examined to determine if it is an
90f4a2713aSLionel Sambuc   // expression-statement or a declaration].
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   // C++ 6.8p3:
93f4a2713aSLionel Sambuc   // The disambiguation is purely syntactic; that is, the meaning of the names
94f4a2713aSLionel Sambuc   // occurring in such a statement, beyond whether they are type-names or not,
95f4a2713aSLionel Sambuc   // is not generally used in or changed by the disambiguation. Class
96f4a2713aSLionel Sambuc   // templates are instantiated as necessary to determine if a qualified name
97f4a2713aSLionel Sambuc   // is a type-name. Disambiguation precedes parsing, and a statement
98f4a2713aSLionel Sambuc   // disambiguated as a declaration may be an ill-formed declaration.
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   // We don't have to parse all of the decl-specifier-seq part. There's only
101f4a2713aSLionel Sambuc   // an ambiguity if the first decl-specifier is
102f4a2713aSLionel Sambuc   // simple-type-specifier/typename-specifier followed by a '(', which may
103f4a2713aSLionel Sambuc   // indicate a function-style cast expression.
104*0a6a1f1dSLionel Sambuc   // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
105f4a2713aSLionel Sambuc   // a case.
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   bool InvalidAsDeclaration = false;
108*0a6a1f1dSLionel Sambuc   TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
109f4a2713aSLionel Sambuc                                            &InvalidAsDeclaration);
110*0a6a1f1dSLionel Sambuc   if (TPR != TPResult::Ambiguous)
111*0a6a1f1dSLionel Sambuc     return TPR != TPResult::False; // Returns true for TPResult::True or
112*0a6a1f1dSLionel Sambuc                                    // TPResult::Error.
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
115f4a2713aSLionel Sambuc   // and so gets some cases wrong. We can't carry on if we've already seen
116f4a2713aSLionel Sambuc   // something which makes this statement invalid as a declaration in this case,
117f4a2713aSLionel Sambuc   // since it can cause us to misparse valid code. Revisit this once
118f4a2713aSLionel Sambuc   // TryParseInitDeclaratorList is fixed.
119f4a2713aSLionel Sambuc   if (InvalidAsDeclaration)
120f4a2713aSLionel Sambuc     return false;
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   // FIXME: Add statistics about the number of ambiguous statements encountered
123f4a2713aSLionel Sambuc   // and how they were resolved (number of declarations+number of expressions).
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc   // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
126f4a2713aSLionel Sambuc   // or an identifier which doesn't resolve as anything. We need tentative
127f4a2713aSLionel Sambuc   // parsing...
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
130f4a2713aSLionel Sambuc   TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
131f4a2713aSLionel Sambuc   PA.Revert();
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc   // In case of an error, let the declaration parsing code handle it.
134*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Error)
135f4a2713aSLionel Sambuc     return true;
136f4a2713aSLionel Sambuc 
137f4a2713aSLionel Sambuc   // Declarations take precedence over expressions.
138*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Ambiguous)
139*0a6a1f1dSLionel Sambuc     TPR = TPResult::True;
140f4a2713aSLionel Sambuc 
141*0a6a1f1dSLionel Sambuc   assert(TPR == TPResult::True || TPR == TPResult::False);
142*0a6a1f1dSLionel Sambuc   return TPR == TPResult::True;
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc /// Try to consume a token sequence that we've already identified as
146f4a2713aSLionel Sambuc /// (potentially) starting a decl-specifier.
TryConsumeDeclarationSpecifier()147f4a2713aSLionel Sambuc Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
148f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
149f4a2713aSLionel Sambuc   case tok::kw__Atomic:
150f4a2713aSLionel Sambuc     if (NextToken().isNot(tok::l_paren)) {
151f4a2713aSLionel Sambuc       ConsumeToken();
152f4a2713aSLionel Sambuc       break;
153f4a2713aSLionel Sambuc     }
154f4a2713aSLionel Sambuc     // Fall through.
155f4a2713aSLionel Sambuc   case tok::kw_typeof:
156f4a2713aSLionel Sambuc   case tok::kw___attribute:
157f4a2713aSLionel Sambuc   case tok::kw___underlying_type: {
158f4a2713aSLionel Sambuc     ConsumeToken();
159f4a2713aSLionel Sambuc     if (Tok.isNot(tok::l_paren))
160*0a6a1f1dSLionel Sambuc       return TPResult::Error;
161f4a2713aSLionel Sambuc     ConsumeParen();
162f4a2713aSLionel Sambuc     if (!SkipUntil(tok::r_paren))
163*0a6a1f1dSLionel Sambuc       return TPResult::Error;
164f4a2713aSLionel Sambuc     break;
165f4a2713aSLionel Sambuc   }
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc   case tok::kw_class:
168f4a2713aSLionel Sambuc   case tok::kw_struct:
169f4a2713aSLionel Sambuc   case tok::kw_union:
170f4a2713aSLionel Sambuc   case tok::kw___interface:
171f4a2713aSLionel Sambuc   case tok::kw_enum:
172f4a2713aSLionel Sambuc     // elaborated-type-specifier:
173f4a2713aSLionel Sambuc     //     class-key attribute-specifier-seq[opt]
174f4a2713aSLionel Sambuc     //         nested-name-specifier[opt] identifier
175f4a2713aSLionel Sambuc     //     class-key nested-name-specifier[opt] template[opt] simple-template-id
176f4a2713aSLionel Sambuc     //     enum nested-name-specifier[opt] identifier
177f4a2713aSLionel Sambuc     //
178f4a2713aSLionel Sambuc     // FIXME: We don't support class-specifiers nor enum-specifiers here.
179f4a2713aSLionel Sambuc     ConsumeToken();
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc     // Skip attributes.
182f4a2713aSLionel Sambuc     while (Tok.is(tok::l_square) || Tok.is(tok::kw___attribute) ||
183f4a2713aSLionel Sambuc            Tok.is(tok::kw___declspec) || Tok.is(tok::kw_alignas)) {
184f4a2713aSLionel Sambuc       if (Tok.is(tok::l_square)) {
185f4a2713aSLionel Sambuc         ConsumeBracket();
186f4a2713aSLionel Sambuc         if (!SkipUntil(tok::r_square))
187*0a6a1f1dSLionel Sambuc           return TPResult::Error;
188f4a2713aSLionel Sambuc       } else {
189f4a2713aSLionel Sambuc         ConsumeToken();
190f4a2713aSLionel Sambuc         if (Tok.isNot(tok::l_paren))
191*0a6a1f1dSLionel Sambuc           return TPResult::Error;
192f4a2713aSLionel Sambuc         ConsumeParen();
193f4a2713aSLionel Sambuc         if (!SkipUntil(tok::r_paren))
194*0a6a1f1dSLionel Sambuc           return TPResult::Error;
195f4a2713aSLionel Sambuc       }
196f4a2713aSLionel Sambuc     }
197f4a2713aSLionel Sambuc 
198*0a6a1f1dSLionel Sambuc     if ((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
199*0a6a1f1dSLionel Sambuc          Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
200*0a6a1f1dSLionel Sambuc         TryAnnotateCXXScopeToken())
201*0a6a1f1dSLionel Sambuc       return TPResult::Error;
202f4a2713aSLionel Sambuc     if (Tok.is(tok::annot_cxxscope))
203f4a2713aSLionel Sambuc       ConsumeToken();
204f4a2713aSLionel Sambuc     if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
205*0a6a1f1dSLionel Sambuc       return TPResult::Error;
206f4a2713aSLionel Sambuc     ConsumeToken();
207f4a2713aSLionel Sambuc     break;
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   case tok::annot_cxxscope:
210f4a2713aSLionel Sambuc     ConsumeToken();
211f4a2713aSLionel Sambuc     // Fall through.
212f4a2713aSLionel Sambuc   default:
213f4a2713aSLionel Sambuc     ConsumeToken();
214f4a2713aSLionel Sambuc 
215f4a2713aSLionel Sambuc     if (getLangOpts().ObjC1 && Tok.is(tok::less))
216f4a2713aSLionel Sambuc       return TryParseProtocolQualifiers();
217f4a2713aSLionel Sambuc     break;
218f4a2713aSLionel Sambuc   }
219f4a2713aSLionel Sambuc 
220*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc 
223f4a2713aSLionel Sambuc /// simple-declaration:
224f4a2713aSLionel Sambuc ///   decl-specifier-seq init-declarator-list[opt] ';'
225f4a2713aSLionel Sambuc ///
226f4a2713aSLionel Sambuc /// (if AllowForRangeDecl specified)
227f4a2713aSLionel Sambuc /// for ( for-range-declaration : for-range-initializer ) statement
228f4a2713aSLionel Sambuc /// for-range-declaration:
229f4a2713aSLionel Sambuc ///    attribute-specifier-seqopt type-specifier-seq declarator
230f4a2713aSLionel Sambuc ///
TryParseSimpleDeclaration(bool AllowForRangeDecl)231f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
232*0a6a1f1dSLionel Sambuc   if (TryConsumeDeclarationSpecifier() == TPResult::Error)
233*0a6a1f1dSLionel Sambuc     return TPResult::Error;
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   // Two decl-specifiers in a row conclusively disambiguate this as being a
236f4a2713aSLionel Sambuc   // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
237f4a2713aSLionel Sambuc   // overwhelmingly common case that the next token is a '('.
238f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
239f4a2713aSLionel Sambuc     TPResult TPR = isCXXDeclarationSpecifier();
240*0a6a1f1dSLionel Sambuc     if (TPR == TPResult::Ambiguous)
241*0a6a1f1dSLionel Sambuc       return TPResult::True;
242*0a6a1f1dSLionel Sambuc     if (TPR == TPResult::True || TPR == TPResult::Error)
243f4a2713aSLionel Sambuc       return TPR;
244*0a6a1f1dSLionel Sambuc     assert(TPR == TPResult::False);
245f4a2713aSLionel Sambuc   }
246f4a2713aSLionel Sambuc 
247f4a2713aSLionel Sambuc   TPResult TPR = TryParseInitDeclaratorList();
248*0a6a1f1dSLionel Sambuc   if (TPR != TPResult::Ambiguous)
249f4a2713aSLionel Sambuc     return TPR;
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
252*0a6a1f1dSLionel Sambuc     return TPResult::False;
253f4a2713aSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
255f4a2713aSLionel Sambuc }
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc /// Tentatively parse an init-declarator-list in order to disambiguate it from
258f4a2713aSLionel Sambuc /// an expression.
259f4a2713aSLionel Sambuc ///
260f4a2713aSLionel Sambuc ///       init-declarator-list:
261f4a2713aSLionel Sambuc ///         init-declarator
262f4a2713aSLionel Sambuc ///         init-declarator-list ',' init-declarator
263f4a2713aSLionel Sambuc ///
264f4a2713aSLionel Sambuc ///       init-declarator:
265f4a2713aSLionel Sambuc ///         declarator initializer[opt]
266f4a2713aSLionel Sambuc /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
267f4a2713aSLionel Sambuc ///
268f4a2713aSLionel Sambuc ///       initializer:
269f4a2713aSLionel Sambuc ///         brace-or-equal-initializer
270f4a2713aSLionel Sambuc ///         '(' expression-list ')'
271f4a2713aSLionel Sambuc ///
272f4a2713aSLionel Sambuc ///       brace-or-equal-initializer:
273f4a2713aSLionel Sambuc ///         '=' initializer-clause
274f4a2713aSLionel Sambuc /// [C++11] braced-init-list
275f4a2713aSLionel Sambuc ///
276f4a2713aSLionel Sambuc ///       initializer-clause:
277f4a2713aSLionel Sambuc ///         assignment-expression
278f4a2713aSLionel Sambuc ///         braced-init-list
279f4a2713aSLionel Sambuc ///
280f4a2713aSLionel Sambuc ///       braced-init-list:
281f4a2713aSLionel Sambuc ///         '{' initializer-list ','[opt] '}'
282f4a2713aSLionel Sambuc ///         '{' '}'
283f4a2713aSLionel Sambuc ///
TryParseInitDeclaratorList()284f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseInitDeclaratorList() {
285f4a2713aSLionel Sambuc   while (1) {
286f4a2713aSLionel Sambuc     // declarator
287f4a2713aSLionel Sambuc     TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
288*0a6a1f1dSLionel Sambuc     if (TPR != TPResult::Ambiguous)
289f4a2713aSLionel Sambuc       return TPR;
290f4a2713aSLionel Sambuc 
291f4a2713aSLionel Sambuc     // [GNU] simple-asm-expr[opt] attributes[opt]
292f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
293*0a6a1f1dSLionel Sambuc       return TPResult::True;
294f4a2713aSLionel Sambuc 
295f4a2713aSLionel Sambuc     // initializer[opt]
296f4a2713aSLionel Sambuc     if (Tok.is(tok::l_paren)) {
297f4a2713aSLionel Sambuc       // Parse through the parens.
298f4a2713aSLionel Sambuc       ConsumeParen();
299f4a2713aSLionel Sambuc       if (!SkipUntil(tok::r_paren, StopAtSemi))
300*0a6a1f1dSLionel Sambuc         return TPResult::Error;
301f4a2713aSLionel Sambuc     } else if (Tok.is(tok::l_brace)) {
302f4a2713aSLionel Sambuc       // A left-brace here is sufficient to disambiguate the parse; an
303f4a2713aSLionel Sambuc       // expression can never be followed directly by a braced-init-list.
304*0a6a1f1dSLionel Sambuc       return TPResult::True;
305f4a2713aSLionel Sambuc     } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
306f4a2713aSLionel Sambuc       // MSVC and g++ won't examine the rest of declarators if '=' is
307f4a2713aSLionel Sambuc       // encountered; they just conclude that we have a declaration.
308f4a2713aSLionel Sambuc       // EDG parses the initializer completely, which is the proper behavior
309f4a2713aSLionel Sambuc       // for this case.
310f4a2713aSLionel Sambuc       //
311f4a2713aSLionel Sambuc       // At present, Clang follows MSVC and g++, since the parser does not have
312f4a2713aSLionel Sambuc       // the ability to parse an expression fully without recording the
313f4a2713aSLionel Sambuc       // results of that parse.
314f4a2713aSLionel Sambuc       // FIXME: Handle this case correctly.
315f4a2713aSLionel Sambuc       //
316f4a2713aSLionel Sambuc       // Also allow 'in' after an Objective-C declaration as in:
317f4a2713aSLionel Sambuc       // for (int (^b)(void) in array). Ideally this should be done in the
318f4a2713aSLionel Sambuc       // context of parsing for-init-statement of a foreach statement only. But,
319f4a2713aSLionel Sambuc       // in any other context 'in' is invalid after a declaration and parser
320f4a2713aSLionel Sambuc       // issues the error regardless of outcome of this decision.
321f4a2713aSLionel Sambuc       // FIXME: Change if above assumption does not hold.
322*0a6a1f1dSLionel Sambuc       return TPResult::True;
323f4a2713aSLionel Sambuc     }
324f4a2713aSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma))
326f4a2713aSLionel Sambuc       break;
327f4a2713aSLionel Sambuc   }
328f4a2713aSLionel Sambuc 
329*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
330f4a2713aSLionel Sambuc }
331f4a2713aSLionel Sambuc 
332f4a2713aSLionel Sambuc /// isCXXConditionDeclaration - Disambiguates between a declaration or an
333f4a2713aSLionel Sambuc /// expression for a condition of a if/switch/while/for statement.
334f4a2713aSLionel Sambuc /// If during the disambiguation process a parsing error is encountered,
335f4a2713aSLionel Sambuc /// the function returns true to let the declaration parsing code handle it.
336f4a2713aSLionel Sambuc ///
337f4a2713aSLionel Sambuc ///       condition:
338f4a2713aSLionel Sambuc ///         expression
339f4a2713aSLionel Sambuc ///         type-specifier-seq declarator '=' assignment-expression
340f4a2713aSLionel Sambuc /// [C++11] type-specifier-seq declarator '=' initializer-clause
341f4a2713aSLionel Sambuc /// [C++11] type-specifier-seq declarator braced-init-list
342f4a2713aSLionel Sambuc /// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
343f4a2713aSLionel Sambuc ///             '=' assignment-expression
344f4a2713aSLionel Sambuc ///
isCXXConditionDeclaration()345f4a2713aSLionel Sambuc bool Parser::isCXXConditionDeclaration() {
346f4a2713aSLionel Sambuc   TPResult TPR = isCXXDeclarationSpecifier();
347*0a6a1f1dSLionel Sambuc   if (TPR != TPResult::Ambiguous)
348*0a6a1f1dSLionel Sambuc     return TPR != TPResult::False; // Returns true for TPResult::True or
349*0a6a1f1dSLionel Sambuc                                    // TPResult::Error.
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc   // FIXME: Add statistics about the number of ambiguous statements encountered
352f4a2713aSLionel Sambuc   // and how they were resolved (number of declarations+number of expressions).
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc   // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
355f4a2713aSLionel Sambuc   // We need tentative parsing...
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
358f4a2713aSLionel Sambuc 
359f4a2713aSLionel Sambuc   // type-specifier-seq
360f4a2713aSLionel Sambuc   TryConsumeDeclarationSpecifier();
361f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_paren) && "Expected '('");
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   // declarator
364f4a2713aSLionel Sambuc   TPR = TryParseDeclarator(false/*mayBeAbstract*/);
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc   // In case of an error, let the declaration parsing code handle it.
367*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Error)
368*0a6a1f1dSLionel Sambuc     TPR = TPResult::True;
369f4a2713aSLionel Sambuc 
370*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Ambiguous) {
371f4a2713aSLionel Sambuc     // '='
372f4a2713aSLionel Sambuc     // [GNU] simple-asm-expr[opt] attributes[opt]
373f4a2713aSLionel Sambuc     if (Tok.is(tok::equal)  ||
374f4a2713aSLionel Sambuc         Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
375*0a6a1f1dSLionel Sambuc       TPR = TPResult::True;
376f4a2713aSLionel Sambuc     else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
377*0a6a1f1dSLionel Sambuc       TPR = TPResult::True;
378f4a2713aSLionel Sambuc     else
379*0a6a1f1dSLionel Sambuc       TPR = TPResult::False;
380f4a2713aSLionel Sambuc   }
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   PA.Revert();
383f4a2713aSLionel Sambuc 
384*0a6a1f1dSLionel Sambuc   assert(TPR == TPResult::True || TPR == TPResult::False);
385*0a6a1f1dSLionel Sambuc   return TPR == TPResult::True;
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc 
388f4a2713aSLionel Sambuc   /// \brief Determine whether the next set of tokens contains a type-id.
389f4a2713aSLionel Sambuc   ///
390f4a2713aSLionel Sambuc   /// The context parameter states what context we're parsing right
391f4a2713aSLionel Sambuc   /// now, which affects how this routine copes with the token
392f4a2713aSLionel Sambuc   /// following the type-id. If the context is TypeIdInParens, we have
393f4a2713aSLionel Sambuc   /// already parsed the '(' and we will cease lookahead when we hit
394f4a2713aSLionel Sambuc   /// the corresponding ')'. If the context is
395f4a2713aSLionel Sambuc   /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
396f4a2713aSLionel Sambuc   /// before this template argument, and will cease lookahead when we
397f4a2713aSLionel Sambuc   /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
398f4a2713aSLionel Sambuc   /// and false for an expression.  If during the disambiguation
399f4a2713aSLionel Sambuc   /// process a parsing error is encountered, the function returns
400f4a2713aSLionel Sambuc   /// true to let the declaration parsing code handle it.
401f4a2713aSLionel Sambuc   ///
402f4a2713aSLionel Sambuc   /// type-id:
403f4a2713aSLionel Sambuc   ///   type-specifier-seq abstract-declarator[opt]
404f4a2713aSLionel Sambuc   ///
isCXXTypeId(TentativeCXXTypeIdContext Context,bool & isAmbiguous)405f4a2713aSLionel Sambuc bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
406f4a2713aSLionel Sambuc 
407f4a2713aSLionel Sambuc   isAmbiguous = false;
408f4a2713aSLionel Sambuc 
409f4a2713aSLionel Sambuc   // C++ 8.2p2:
410f4a2713aSLionel Sambuc   // The ambiguity arising from the similarity between a function-style cast and
411f4a2713aSLionel Sambuc   // a type-id can occur in different contexts. The ambiguity appears as a
412f4a2713aSLionel Sambuc   // choice between a function-style cast expression and a declaration of a
413f4a2713aSLionel Sambuc   // type. The resolution is that any construct that could possibly be a type-id
414f4a2713aSLionel Sambuc   // in its syntactic context shall be considered a type-id.
415f4a2713aSLionel Sambuc 
416f4a2713aSLionel Sambuc   TPResult TPR = isCXXDeclarationSpecifier();
417*0a6a1f1dSLionel Sambuc   if (TPR != TPResult::Ambiguous)
418*0a6a1f1dSLionel Sambuc     return TPR != TPResult::False; // Returns true for TPResult::True or
419*0a6a1f1dSLionel Sambuc                                      // TPResult::Error.
420f4a2713aSLionel Sambuc 
421f4a2713aSLionel Sambuc   // FIXME: Add statistics about the number of ambiguous statements encountered
422f4a2713aSLionel Sambuc   // and how they were resolved (number of declarations+number of expressions).
423f4a2713aSLionel Sambuc 
424f4a2713aSLionel Sambuc   // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
425f4a2713aSLionel Sambuc   // We need tentative parsing...
426f4a2713aSLionel Sambuc 
427f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc   // type-specifier-seq
430f4a2713aSLionel Sambuc   TryConsumeDeclarationSpecifier();
431f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_paren) && "Expected '('");
432f4a2713aSLionel Sambuc 
433f4a2713aSLionel Sambuc   // declarator
434f4a2713aSLionel Sambuc   TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc   // In case of an error, let the declaration parsing code handle it.
437*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Error)
438*0a6a1f1dSLionel Sambuc     TPR = TPResult::True;
439f4a2713aSLionel Sambuc 
440*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Ambiguous) {
441f4a2713aSLionel Sambuc     // We are supposed to be inside parens, so if after the abstract declarator
442f4a2713aSLionel Sambuc     // we encounter a ')' this is a type-id, otherwise it's an expression.
443f4a2713aSLionel Sambuc     if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
444*0a6a1f1dSLionel Sambuc       TPR = TPResult::True;
445f4a2713aSLionel Sambuc       isAmbiguous = true;
446f4a2713aSLionel Sambuc 
447f4a2713aSLionel Sambuc     // We are supposed to be inside a template argument, so if after
448f4a2713aSLionel Sambuc     // the abstract declarator we encounter a '>', '>>' (in C++0x), or
449f4a2713aSLionel Sambuc     // ',', this is a type-id. Otherwise, it's an expression.
450f4a2713aSLionel Sambuc     } else if (Context == TypeIdAsTemplateArgument &&
451f4a2713aSLionel Sambuc                (Tok.is(tok::greater) || Tok.is(tok::comma) ||
452f4a2713aSLionel Sambuc                 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
453*0a6a1f1dSLionel Sambuc       TPR = TPResult::True;
454f4a2713aSLionel Sambuc       isAmbiguous = true;
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc     } else
457*0a6a1f1dSLionel Sambuc       TPR = TPResult::False;
458f4a2713aSLionel Sambuc   }
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   PA.Revert();
461f4a2713aSLionel Sambuc 
462*0a6a1f1dSLionel Sambuc   assert(TPR == TPResult::True || TPR == TPResult::False);
463*0a6a1f1dSLionel Sambuc   return TPR == TPResult::True;
464f4a2713aSLionel Sambuc }
465f4a2713aSLionel Sambuc 
466f4a2713aSLionel Sambuc /// \brief Returns true if this is a C++11 attribute-specifier. Per
467f4a2713aSLionel Sambuc /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
468f4a2713aSLionel Sambuc /// always introduce an attribute. In Objective-C++11, this rule does not
469f4a2713aSLionel Sambuc /// apply if either '[' begins a message-send.
470f4a2713aSLionel Sambuc ///
471f4a2713aSLionel Sambuc /// If Disambiguate is true, we try harder to determine whether a '[[' starts
472f4a2713aSLionel Sambuc /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
473f4a2713aSLionel Sambuc ///
474f4a2713aSLionel Sambuc /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
475f4a2713aSLionel Sambuc /// Obj-C message send or the start of an attribute. Otherwise, we assume it
476f4a2713aSLionel Sambuc /// is not an Obj-C message send.
477f4a2713aSLionel Sambuc ///
478f4a2713aSLionel Sambuc /// C++11 [dcl.attr.grammar]:
479f4a2713aSLionel Sambuc ///
480f4a2713aSLionel Sambuc ///     attribute-specifier:
481f4a2713aSLionel Sambuc ///         '[' '[' attribute-list ']' ']'
482f4a2713aSLionel Sambuc ///         alignment-specifier
483f4a2713aSLionel Sambuc ///
484f4a2713aSLionel Sambuc ///     attribute-list:
485f4a2713aSLionel Sambuc ///         attribute[opt]
486f4a2713aSLionel Sambuc ///         attribute-list ',' attribute[opt]
487f4a2713aSLionel Sambuc ///         attribute '...'
488f4a2713aSLionel Sambuc ///         attribute-list ',' attribute '...'
489f4a2713aSLionel Sambuc ///
490f4a2713aSLionel Sambuc ///     attribute:
491f4a2713aSLionel Sambuc ///         attribute-token attribute-argument-clause[opt]
492f4a2713aSLionel Sambuc ///
493f4a2713aSLionel Sambuc ///     attribute-token:
494f4a2713aSLionel Sambuc ///         identifier
495f4a2713aSLionel Sambuc ///         identifier '::' identifier
496f4a2713aSLionel Sambuc ///
497f4a2713aSLionel Sambuc ///     attribute-argument-clause:
498f4a2713aSLionel Sambuc ///         '(' balanced-token-seq ')'
499f4a2713aSLionel Sambuc Parser::CXX11AttributeKind
isCXX11AttributeSpecifier(bool Disambiguate,bool OuterMightBeMessageSend)500f4a2713aSLionel Sambuc Parser::isCXX11AttributeSpecifier(bool Disambiguate,
501f4a2713aSLionel Sambuc                                   bool OuterMightBeMessageSend) {
502f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_alignas))
503f4a2713aSLionel Sambuc     return CAK_AttributeSpecifier;
504f4a2713aSLionel Sambuc 
505f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
506f4a2713aSLionel Sambuc     return CAK_NotAttributeSpecifier;
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   // No tentative parsing if we don't need to look for ']]' or a lambda.
509f4a2713aSLionel Sambuc   if (!Disambiguate && !getLangOpts().ObjC1)
510f4a2713aSLionel Sambuc     return CAK_AttributeSpecifier;
511f4a2713aSLionel Sambuc 
512f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
513f4a2713aSLionel Sambuc 
514f4a2713aSLionel Sambuc   // Opening brackets were checked for above.
515f4a2713aSLionel Sambuc   ConsumeBracket();
516f4a2713aSLionel Sambuc 
517f4a2713aSLionel Sambuc   // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
518f4a2713aSLionel Sambuc   if (!getLangOpts().ObjC1) {
519f4a2713aSLionel Sambuc     ConsumeBracket();
520f4a2713aSLionel Sambuc 
521f4a2713aSLionel Sambuc     bool IsAttribute = SkipUntil(tok::r_square);
522f4a2713aSLionel Sambuc     IsAttribute &= Tok.is(tok::r_square);
523f4a2713aSLionel Sambuc 
524f4a2713aSLionel Sambuc     PA.Revert();
525f4a2713aSLionel Sambuc 
526f4a2713aSLionel Sambuc     return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
527f4a2713aSLionel Sambuc   }
528f4a2713aSLionel Sambuc 
529f4a2713aSLionel Sambuc   // In Obj-C++11, we need to distinguish four situations:
530f4a2713aSLionel Sambuc   //  1a) int x[[attr]];                     C++11 attribute.
531f4a2713aSLionel Sambuc   //  1b) [[attr]];                          C++11 statement attribute.
532f4a2713aSLionel Sambuc   //   2) int x[[obj](){ return 1; }()];     Lambda in array size/index.
533f4a2713aSLionel Sambuc   //  3a) int x[[obj get]];                  Message send in array size/index.
534f4a2713aSLionel Sambuc   //  3b) [[Class alloc] init];              Message send in message send.
535f4a2713aSLionel Sambuc   //   4) [[obj]{ return self; }() doStuff]; Lambda in message send.
536f4a2713aSLionel Sambuc   // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
537f4a2713aSLionel Sambuc 
538f4a2713aSLionel Sambuc   // If we have a lambda-introducer, then this is definitely not a message send.
539f4a2713aSLionel Sambuc   // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
540f4a2713aSLionel Sambuc   // into the tentative attribute parse below.
541f4a2713aSLionel Sambuc   LambdaIntroducer Intro;
542f4a2713aSLionel Sambuc   if (!TryParseLambdaIntroducer(Intro)) {
543f4a2713aSLionel Sambuc     // A lambda cannot end with ']]', and an attribute must.
544f4a2713aSLionel Sambuc     bool IsAttribute = Tok.is(tok::r_square);
545f4a2713aSLionel Sambuc 
546f4a2713aSLionel Sambuc     PA.Revert();
547f4a2713aSLionel Sambuc 
548f4a2713aSLionel Sambuc     if (IsAttribute)
549f4a2713aSLionel Sambuc       // Case 1: C++11 attribute.
550f4a2713aSLionel Sambuc       return CAK_AttributeSpecifier;
551f4a2713aSLionel Sambuc 
552f4a2713aSLionel Sambuc     if (OuterMightBeMessageSend)
553f4a2713aSLionel Sambuc       // Case 4: Lambda in message send.
554f4a2713aSLionel Sambuc       return CAK_NotAttributeSpecifier;
555f4a2713aSLionel Sambuc 
556f4a2713aSLionel Sambuc     // Case 2: Lambda in array size / index.
557f4a2713aSLionel Sambuc     return CAK_InvalidAttributeSpecifier;
558f4a2713aSLionel Sambuc   }
559f4a2713aSLionel Sambuc 
560f4a2713aSLionel Sambuc   ConsumeBracket();
561f4a2713aSLionel Sambuc 
562f4a2713aSLionel Sambuc   // If we don't have a lambda-introducer, then we have an attribute or a
563f4a2713aSLionel Sambuc   // message-send.
564f4a2713aSLionel Sambuc   bool IsAttribute = true;
565f4a2713aSLionel Sambuc   while (Tok.isNot(tok::r_square)) {
566f4a2713aSLionel Sambuc     if (Tok.is(tok::comma)) {
567f4a2713aSLionel Sambuc       // Case 1: Stray commas can only occur in attributes.
568f4a2713aSLionel Sambuc       PA.Revert();
569f4a2713aSLionel Sambuc       return CAK_AttributeSpecifier;
570f4a2713aSLionel Sambuc     }
571f4a2713aSLionel Sambuc 
572f4a2713aSLionel Sambuc     // Parse the attribute-token, if present.
573f4a2713aSLionel Sambuc     // C++11 [dcl.attr.grammar]:
574f4a2713aSLionel Sambuc     //   If a keyword or an alternative token that satisfies the syntactic
575f4a2713aSLionel Sambuc     //   requirements of an identifier is contained in an attribute-token,
576f4a2713aSLionel Sambuc     //   it is considered an identifier.
577f4a2713aSLionel Sambuc     SourceLocation Loc;
578f4a2713aSLionel Sambuc     if (!TryParseCXX11AttributeIdentifier(Loc)) {
579f4a2713aSLionel Sambuc       IsAttribute = false;
580f4a2713aSLionel Sambuc       break;
581f4a2713aSLionel Sambuc     }
582f4a2713aSLionel Sambuc     if (Tok.is(tok::coloncolon)) {
583f4a2713aSLionel Sambuc       ConsumeToken();
584f4a2713aSLionel Sambuc       if (!TryParseCXX11AttributeIdentifier(Loc)) {
585f4a2713aSLionel Sambuc         IsAttribute = false;
586f4a2713aSLionel Sambuc         break;
587f4a2713aSLionel Sambuc       }
588f4a2713aSLionel Sambuc     }
589f4a2713aSLionel Sambuc 
590f4a2713aSLionel Sambuc     // Parse the attribute-argument-clause, if present.
591f4a2713aSLionel Sambuc     if (Tok.is(tok::l_paren)) {
592f4a2713aSLionel Sambuc       ConsumeParen();
593f4a2713aSLionel Sambuc       if (!SkipUntil(tok::r_paren)) {
594f4a2713aSLionel Sambuc         IsAttribute = false;
595f4a2713aSLionel Sambuc         break;
596f4a2713aSLionel Sambuc       }
597f4a2713aSLionel Sambuc     }
598f4a2713aSLionel Sambuc 
599*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::ellipsis);
600f4a2713aSLionel Sambuc 
601*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma))
602f4a2713aSLionel Sambuc       break;
603f4a2713aSLionel Sambuc   }
604f4a2713aSLionel Sambuc 
605f4a2713aSLionel Sambuc   // An attribute must end ']]'.
606f4a2713aSLionel Sambuc   if (IsAttribute) {
607f4a2713aSLionel Sambuc     if (Tok.is(tok::r_square)) {
608f4a2713aSLionel Sambuc       ConsumeBracket();
609f4a2713aSLionel Sambuc       IsAttribute = Tok.is(tok::r_square);
610f4a2713aSLionel Sambuc     } else {
611f4a2713aSLionel Sambuc       IsAttribute = false;
612f4a2713aSLionel Sambuc     }
613f4a2713aSLionel Sambuc   }
614f4a2713aSLionel Sambuc 
615f4a2713aSLionel Sambuc   PA.Revert();
616f4a2713aSLionel Sambuc 
617f4a2713aSLionel Sambuc   if (IsAttribute)
618f4a2713aSLionel Sambuc     // Case 1: C++11 statement attribute.
619f4a2713aSLionel Sambuc     return CAK_AttributeSpecifier;
620f4a2713aSLionel Sambuc 
621f4a2713aSLionel Sambuc   // Case 3: Message send.
622f4a2713aSLionel Sambuc   return CAK_NotAttributeSpecifier;
623f4a2713aSLionel Sambuc }
624f4a2713aSLionel Sambuc 
TryParsePtrOperatorSeq()625f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParsePtrOperatorSeq() {
626f4a2713aSLionel Sambuc   while (true) {
627f4a2713aSLionel Sambuc     if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
628f4a2713aSLionel Sambuc       if (TryAnnotateCXXScopeToken(true))
629*0a6a1f1dSLionel Sambuc         return TPResult::Error;
630f4a2713aSLionel Sambuc 
631f4a2713aSLionel Sambuc     if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
632f4a2713aSLionel Sambuc         Tok.is(tok::ampamp) ||
633f4a2713aSLionel Sambuc         (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
634f4a2713aSLionel Sambuc       // ptr-operator
635f4a2713aSLionel Sambuc       ConsumeToken();
636f4a2713aSLionel Sambuc       while (Tok.is(tok::kw_const)    ||
637f4a2713aSLionel Sambuc              Tok.is(tok::kw_volatile) ||
638f4a2713aSLionel Sambuc              Tok.is(tok::kw_restrict))
639f4a2713aSLionel Sambuc         ConsumeToken();
640f4a2713aSLionel Sambuc     } else {
641*0a6a1f1dSLionel Sambuc       return TPResult::True;
642f4a2713aSLionel Sambuc     }
643f4a2713aSLionel Sambuc   }
644f4a2713aSLionel Sambuc }
645f4a2713aSLionel Sambuc 
646f4a2713aSLionel Sambuc ///         operator-function-id:
647f4a2713aSLionel Sambuc ///           'operator' operator
648f4a2713aSLionel Sambuc ///
649f4a2713aSLionel Sambuc ///         operator: one of
650f4a2713aSLionel Sambuc ///           new  delete  new[]  delete[]  +  -  *  /  %  ^  [...]
651f4a2713aSLionel Sambuc ///
652f4a2713aSLionel Sambuc ///         conversion-function-id:
653f4a2713aSLionel Sambuc ///           'operator' conversion-type-id
654f4a2713aSLionel Sambuc ///
655f4a2713aSLionel Sambuc ///         conversion-type-id:
656f4a2713aSLionel Sambuc ///           type-specifier-seq conversion-declarator[opt]
657f4a2713aSLionel Sambuc ///
658f4a2713aSLionel Sambuc ///         conversion-declarator:
659f4a2713aSLionel Sambuc ///           ptr-operator conversion-declarator[opt]
660f4a2713aSLionel Sambuc ///
661f4a2713aSLionel Sambuc ///         literal-operator-id:
662f4a2713aSLionel Sambuc ///           'operator' string-literal identifier
663f4a2713aSLionel Sambuc ///           'operator' user-defined-string-literal
TryParseOperatorId()664f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseOperatorId() {
665f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_operator));
666f4a2713aSLionel Sambuc   ConsumeToken();
667f4a2713aSLionel Sambuc 
668f4a2713aSLionel Sambuc   // Maybe this is an operator-function-id.
669f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
670f4a2713aSLionel Sambuc   case tok::kw_new: case tok::kw_delete:
671f4a2713aSLionel Sambuc     ConsumeToken();
672f4a2713aSLionel Sambuc     if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
673f4a2713aSLionel Sambuc       ConsumeBracket();
674f4a2713aSLionel Sambuc       ConsumeBracket();
675f4a2713aSLionel Sambuc     }
676*0a6a1f1dSLionel Sambuc     return TPResult::True;
677f4a2713aSLionel Sambuc 
678f4a2713aSLionel Sambuc #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
679f4a2713aSLionel Sambuc   case tok::Token:
680f4a2713aSLionel Sambuc #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
681f4a2713aSLionel Sambuc #include "clang/Basic/OperatorKinds.def"
682f4a2713aSLionel Sambuc     ConsumeToken();
683*0a6a1f1dSLionel Sambuc     return TPResult::True;
684f4a2713aSLionel Sambuc 
685f4a2713aSLionel Sambuc   case tok::l_square:
686f4a2713aSLionel Sambuc     if (NextToken().is(tok::r_square)) {
687f4a2713aSLionel Sambuc       ConsumeBracket();
688f4a2713aSLionel Sambuc       ConsumeBracket();
689*0a6a1f1dSLionel Sambuc       return TPResult::True;
690f4a2713aSLionel Sambuc     }
691f4a2713aSLionel Sambuc     break;
692f4a2713aSLionel Sambuc 
693f4a2713aSLionel Sambuc   case tok::l_paren:
694f4a2713aSLionel Sambuc     if (NextToken().is(tok::r_paren)) {
695f4a2713aSLionel Sambuc       ConsumeParen();
696f4a2713aSLionel Sambuc       ConsumeParen();
697*0a6a1f1dSLionel Sambuc       return TPResult::True;
698f4a2713aSLionel Sambuc     }
699f4a2713aSLionel Sambuc     break;
700f4a2713aSLionel Sambuc 
701f4a2713aSLionel Sambuc   default:
702f4a2713aSLionel Sambuc     break;
703f4a2713aSLionel Sambuc   }
704f4a2713aSLionel Sambuc 
705f4a2713aSLionel Sambuc   // Maybe this is a literal-operator-id.
706f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
707f4a2713aSLionel Sambuc     bool FoundUDSuffix = false;
708f4a2713aSLionel Sambuc     do {
709f4a2713aSLionel Sambuc       FoundUDSuffix |= Tok.hasUDSuffix();
710f4a2713aSLionel Sambuc       ConsumeStringToken();
711f4a2713aSLionel Sambuc     } while (isTokenStringLiteral());
712f4a2713aSLionel Sambuc 
713f4a2713aSLionel Sambuc     if (!FoundUDSuffix) {
714f4a2713aSLionel Sambuc       if (Tok.is(tok::identifier))
715f4a2713aSLionel Sambuc         ConsumeToken();
716f4a2713aSLionel Sambuc       else
717*0a6a1f1dSLionel Sambuc         return TPResult::Error;
718f4a2713aSLionel Sambuc     }
719*0a6a1f1dSLionel Sambuc     return TPResult::True;
720f4a2713aSLionel Sambuc   }
721f4a2713aSLionel Sambuc 
722f4a2713aSLionel Sambuc   // Maybe this is a conversion-function-id.
723f4a2713aSLionel Sambuc   bool AnyDeclSpecifiers = false;
724f4a2713aSLionel Sambuc   while (true) {
725f4a2713aSLionel Sambuc     TPResult TPR = isCXXDeclarationSpecifier();
726*0a6a1f1dSLionel Sambuc     if (TPR == TPResult::Error)
727f4a2713aSLionel Sambuc       return TPR;
728*0a6a1f1dSLionel Sambuc     if (TPR == TPResult::False) {
729f4a2713aSLionel Sambuc       if (!AnyDeclSpecifiers)
730*0a6a1f1dSLionel Sambuc         return TPResult::Error;
731f4a2713aSLionel Sambuc       break;
732f4a2713aSLionel Sambuc     }
733*0a6a1f1dSLionel Sambuc     if (TryConsumeDeclarationSpecifier() == TPResult::Error)
734*0a6a1f1dSLionel Sambuc       return TPResult::Error;
735f4a2713aSLionel Sambuc     AnyDeclSpecifiers = true;
736f4a2713aSLionel Sambuc   }
737f4a2713aSLionel Sambuc   return TryParsePtrOperatorSeq();
738f4a2713aSLionel Sambuc }
739f4a2713aSLionel Sambuc 
740f4a2713aSLionel Sambuc ///         declarator:
741f4a2713aSLionel Sambuc ///           direct-declarator
742f4a2713aSLionel Sambuc ///           ptr-operator declarator
743f4a2713aSLionel Sambuc ///
744f4a2713aSLionel Sambuc ///         direct-declarator:
745f4a2713aSLionel Sambuc ///           declarator-id
746f4a2713aSLionel Sambuc ///           direct-declarator '(' parameter-declaration-clause ')'
747f4a2713aSLionel Sambuc ///                 cv-qualifier-seq[opt] exception-specification[opt]
748f4a2713aSLionel Sambuc ///           direct-declarator '[' constant-expression[opt] ']'
749f4a2713aSLionel Sambuc ///           '(' declarator ')'
750f4a2713aSLionel Sambuc /// [GNU]     '(' attributes declarator ')'
751f4a2713aSLionel Sambuc ///
752f4a2713aSLionel Sambuc ///         abstract-declarator:
753f4a2713aSLionel Sambuc ///           ptr-operator abstract-declarator[opt]
754f4a2713aSLionel Sambuc ///           direct-abstract-declarator
755f4a2713aSLionel Sambuc ///           ...
756f4a2713aSLionel Sambuc ///
757f4a2713aSLionel Sambuc ///         direct-abstract-declarator:
758f4a2713aSLionel Sambuc ///           direct-abstract-declarator[opt]
759f4a2713aSLionel Sambuc ///           '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
760f4a2713aSLionel Sambuc ///                 exception-specification[opt]
761f4a2713aSLionel Sambuc ///           direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
762f4a2713aSLionel Sambuc ///           '(' abstract-declarator ')'
763f4a2713aSLionel Sambuc ///
764f4a2713aSLionel Sambuc ///         ptr-operator:
765f4a2713aSLionel Sambuc ///           '*' cv-qualifier-seq[opt]
766f4a2713aSLionel Sambuc ///           '&'
767f4a2713aSLionel Sambuc /// [C++0x]   '&&'                                                        [TODO]
768f4a2713aSLionel Sambuc ///           '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
769f4a2713aSLionel Sambuc ///
770f4a2713aSLionel Sambuc ///         cv-qualifier-seq:
771f4a2713aSLionel Sambuc ///           cv-qualifier cv-qualifier-seq[opt]
772f4a2713aSLionel Sambuc ///
773f4a2713aSLionel Sambuc ///         cv-qualifier:
774f4a2713aSLionel Sambuc ///           'const'
775f4a2713aSLionel Sambuc ///           'volatile'
776f4a2713aSLionel Sambuc ///
777f4a2713aSLionel Sambuc ///         declarator-id:
778f4a2713aSLionel Sambuc ///           '...'[opt] id-expression
779f4a2713aSLionel Sambuc ///
780f4a2713aSLionel Sambuc ///         id-expression:
781f4a2713aSLionel Sambuc ///           unqualified-id
782f4a2713aSLionel Sambuc ///           qualified-id                                                [TODO]
783f4a2713aSLionel Sambuc ///
784f4a2713aSLionel Sambuc ///         unqualified-id:
785f4a2713aSLionel Sambuc ///           identifier
786f4a2713aSLionel Sambuc ///           operator-function-id
787f4a2713aSLionel Sambuc ///           conversion-function-id
788f4a2713aSLionel Sambuc ///           literal-operator-id
789f4a2713aSLionel Sambuc ///           '~' class-name                                              [TODO]
790f4a2713aSLionel Sambuc ///           '~' decltype-specifier                                      [TODO]
791f4a2713aSLionel Sambuc ///           template-id                                                 [TODO]
792f4a2713aSLionel Sambuc ///
TryParseDeclarator(bool mayBeAbstract,bool mayHaveIdentifier)793f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
794f4a2713aSLionel Sambuc                                             bool mayHaveIdentifier) {
795f4a2713aSLionel Sambuc   // declarator:
796f4a2713aSLionel Sambuc   //   direct-declarator
797f4a2713aSLionel Sambuc   //   ptr-operator declarator
798*0a6a1f1dSLionel Sambuc   if (TryParsePtrOperatorSeq() == TPResult::Error)
799*0a6a1f1dSLionel Sambuc     return TPResult::Error;
800f4a2713aSLionel Sambuc 
801f4a2713aSLionel Sambuc   // direct-declarator:
802f4a2713aSLionel Sambuc   // direct-abstract-declarator:
803f4a2713aSLionel Sambuc   if (Tok.is(tok::ellipsis))
804f4a2713aSLionel Sambuc     ConsumeToken();
805f4a2713aSLionel Sambuc 
806f4a2713aSLionel Sambuc   if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
807f4a2713aSLionel Sambuc        (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
808f4a2713aSLionel Sambuc                                         NextToken().is(tok::kw_operator)))) &&
809f4a2713aSLionel Sambuc       mayHaveIdentifier) {
810f4a2713aSLionel Sambuc     // declarator-id
811f4a2713aSLionel Sambuc     if (Tok.is(tok::annot_cxxscope))
812f4a2713aSLionel Sambuc       ConsumeToken();
813f4a2713aSLionel Sambuc     else if (Tok.is(tok::identifier))
814f4a2713aSLionel Sambuc       TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
815f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_operator)) {
816*0a6a1f1dSLionel Sambuc       if (TryParseOperatorId() == TPResult::Error)
817*0a6a1f1dSLionel Sambuc         return TPResult::Error;
818f4a2713aSLionel Sambuc     } else
819f4a2713aSLionel Sambuc       ConsumeToken();
820f4a2713aSLionel Sambuc   } else if (Tok.is(tok::l_paren)) {
821f4a2713aSLionel Sambuc     ConsumeParen();
822f4a2713aSLionel Sambuc     if (mayBeAbstract &&
823f4a2713aSLionel Sambuc         (Tok.is(tok::r_paren) ||       // 'int()' is a function.
824f4a2713aSLionel Sambuc          // 'int(...)' is a function.
825f4a2713aSLionel Sambuc          (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
826f4a2713aSLionel Sambuc          isDeclarationSpecifier())) {   // 'int(int)' is a function.
827f4a2713aSLionel Sambuc       // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
828f4a2713aSLionel Sambuc       //        exception-specification[opt]
829f4a2713aSLionel Sambuc       TPResult TPR = TryParseFunctionDeclarator();
830*0a6a1f1dSLionel Sambuc       if (TPR != TPResult::Ambiguous)
831f4a2713aSLionel Sambuc         return TPR;
832f4a2713aSLionel Sambuc     } else {
833f4a2713aSLionel Sambuc       // '(' declarator ')'
834f4a2713aSLionel Sambuc       // '(' attributes declarator ')'
835f4a2713aSLionel Sambuc       // '(' abstract-declarator ')'
836f4a2713aSLionel Sambuc       if (Tok.is(tok::kw___attribute) ||
837f4a2713aSLionel Sambuc           Tok.is(tok::kw___declspec) ||
838f4a2713aSLionel Sambuc           Tok.is(tok::kw___cdecl) ||
839f4a2713aSLionel Sambuc           Tok.is(tok::kw___stdcall) ||
840f4a2713aSLionel Sambuc           Tok.is(tok::kw___fastcall) ||
841f4a2713aSLionel Sambuc           Tok.is(tok::kw___thiscall) ||
842*0a6a1f1dSLionel Sambuc           Tok.is(tok::kw___vectorcall) ||
843f4a2713aSLionel Sambuc           Tok.is(tok::kw___unaligned))
844*0a6a1f1dSLionel Sambuc         return TPResult::True; // attributes indicate declaration
845f4a2713aSLionel Sambuc       TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
846*0a6a1f1dSLionel Sambuc       if (TPR != TPResult::Ambiguous)
847f4a2713aSLionel Sambuc         return TPR;
848f4a2713aSLionel Sambuc       if (Tok.isNot(tok::r_paren))
849*0a6a1f1dSLionel Sambuc         return TPResult::False;
850f4a2713aSLionel Sambuc       ConsumeParen();
851f4a2713aSLionel Sambuc     }
852f4a2713aSLionel Sambuc   } else if (!mayBeAbstract) {
853*0a6a1f1dSLionel Sambuc     return TPResult::False;
854f4a2713aSLionel Sambuc   }
855f4a2713aSLionel Sambuc 
856f4a2713aSLionel Sambuc   while (1) {
857*0a6a1f1dSLionel Sambuc     TPResult TPR(TPResult::Ambiguous);
858f4a2713aSLionel Sambuc 
859f4a2713aSLionel Sambuc     // abstract-declarator: ...
860f4a2713aSLionel Sambuc     if (Tok.is(tok::ellipsis))
861f4a2713aSLionel Sambuc       ConsumeToken();
862f4a2713aSLionel Sambuc 
863f4a2713aSLionel Sambuc     if (Tok.is(tok::l_paren)) {
864f4a2713aSLionel Sambuc       // Check whether we have a function declarator or a possible ctor-style
865f4a2713aSLionel Sambuc       // initializer that follows the declarator. Note that ctor-style
866f4a2713aSLionel Sambuc       // initializers are not possible in contexts where abstract declarators
867f4a2713aSLionel Sambuc       // are allowed.
868f4a2713aSLionel Sambuc       if (!mayBeAbstract && !isCXXFunctionDeclarator())
869f4a2713aSLionel Sambuc         break;
870f4a2713aSLionel Sambuc 
871f4a2713aSLionel Sambuc       // direct-declarator '(' parameter-declaration-clause ')'
872f4a2713aSLionel Sambuc       //        cv-qualifier-seq[opt] exception-specification[opt]
873f4a2713aSLionel Sambuc       ConsumeParen();
874f4a2713aSLionel Sambuc       TPR = TryParseFunctionDeclarator();
875f4a2713aSLionel Sambuc     } else if (Tok.is(tok::l_square)) {
876f4a2713aSLionel Sambuc       // direct-declarator '[' constant-expression[opt] ']'
877f4a2713aSLionel Sambuc       // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
878f4a2713aSLionel Sambuc       TPR = TryParseBracketDeclarator();
879f4a2713aSLionel Sambuc     } else {
880f4a2713aSLionel Sambuc       break;
881f4a2713aSLionel Sambuc     }
882f4a2713aSLionel Sambuc 
883*0a6a1f1dSLionel Sambuc     if (TPR != TPResult::Ambiguous)
884f4a2713aSLionel Sambuc       return TPR;
885f4a2713aSLionel Sambuc   }
886f4a2713aSLionel Sambuc 
887*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
888f4a2713aSLionel Sambuc }
889f4a2713aSLionel Sambuc 
890f4a2713aSLionel Sambuc Parser::TPResult
isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind)891f4a2713aSLionel Sambuc Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
892f4a2713aSLionel Sambuc   switch (Kind) {
893f4a2713aSLionel Sambuc   // Obviously starts an expression.
894f4a2713aSLionel Sambuc   case tok::numeric_constant:
895f4a2713aSLionel Sambuc   case tok::char_constant:
896f4a2713aSLionel Sambuc   case tok::wide_char_constant:
897*0a6a1f1dSLionel Sambuc   case tok::utf8_char_constant:
898f4a2713aSLionel Sambuc   case tok::utf16_char_constant:
899f4a2713aSLionel Sambuc   case tok::utf32_char_constant:
900f4a2713aSLionel Sambuc   case tok::string_literal:
901f4a2713aSLionel Sambuc   case tok::wide_string_literal:
902f4a2713aSLionel Sambuc   case tok::utf8_string_literal:
903f4a2713aSLionel Sambuc   case tok::utf16_string_literal:
904f4a2713aSLionel Sambuc   case tok::utf32_string_literal:
905f4a2713aSLionel Sambuc   case tok::l_square:
906f4a2713aSLionel Sambuc   case tok::l_paren:
907f4a2713aSLionel Sambuc   case tok::amp:
908f4a2713aSLionel Sambuc   case tok::ampamp:
909f4a2713aSLionel Sambuc   case tok::star:
910f4a2713aSLionel Sambuc   case tok::plus:
911f4a2713aSLionel Sambuc   case tok::plusplus:
912f4a2713aSLionel Sambuc   case tok::minus:
913f4a2713aSLionel Sambuc   case tok::minusminus:
914f4a2713aSLionel Sambuc   case tok::tilde:
915f4a2713aSLionel Sambuc   case tok::exclaim:
916f4a2713aSLionel Sambuc   case tok::kw_sizeof:
917f4a2713aSLionel Sambuc   case tok::kw___func__:
918f4a2713aSLionel Sambuc   case tok::kw_const_cast:
919f4a2713aSLionel Sambuc   case tok::kw_delete:
920f4a2713aSLionel Sambuc   case tok::kw_dynamic_cast:
921f4a2713aSLionel Sambuc   case tok::kw_false:
922f4a2713aSLionel Sambuc   case tok::kw_new:
923f4a2713aSLionel Sambuc   case tok::kw_operator:
924f4a2713aSLionel Sambuc   case tok::kw_reinterpret_cast:
925f4a2713aSLionel Sambuc   case tok::kw_static_cast:
926f4a2713aSLionel Sambuc   case tok::kw_this:
927f4a2713aSLionel Sambuc   case tok::kw_throw:
928f4a2713aSLionel Sambuc   case tok::kw_true:
929f4a2713aSLionel Sambuc   case tok::kw_typeid:
930f4a2713aSLionel Sambuc   case tok::kw_alignof:
931f4a2713aSLionel Sambuc   case tok::kw_noexcept:
932f4a2713aSLionel Sambuc   case tok::kw_nullptr:
933f4a2713aSLionel Sambuc   case tok::kw__Alignof:
934f4a2713aSLionel Sambuc   case tok::kw___null:
935f4a2713aSLionel Sambuc   case tok::kw___alignof:
936f4a2713aSLionel Sambuc   case tok::kw___builtin_choose_expr:
937f4a2713aSLionel Sambuc   case tok::kw___builtin_offsetof:
938f4a2713aSLionel Sambuc   case tok::kw___builtin_va_arg:
939f4a2713aSLionel Sambuc   case tok::kw___imag:
940f4a2713aSLionel Sambuc   case tok::kw___real:
941f4a2713aSLionel Sambuc   case tok::kw___FUNCTION__:
942f4a2713aSLionel Sambuc   case tok::kw___FUNCDNAME__:
943*0a6a1f1dSLionel Sambuc   case tok::kw___FUNCSIG__:
944f4a2713aSLionel Sambuc   case tok::kw_L__FUNCTION__:
945f4a2713aSLionel Sambuc   case tok::kw___PRETTY_FUNCTION__:
946f4a2713aSLionel Sambuc   case tok::kw___uuidof:
947*0a6a1f1dSLionel Sambuc #define TYPE_TRAIT(N,Spelling,K) \
948*0a6a1f1dSLionel Sambuc   case tok::kw_##Spelling:
949*0a6a1f1dSLionel Sambuc #include "clang/Basic/TokenKinds.def"
950*0a6a1f1dSLionel Sambuc     return TPResult::True;
951f4a2713aSLionel Sambuc 
952f4a2713aSLionel Sambuc   // Obviously starts a type-specifier-seq:
953f4a2713aSLionel Sambuc   case tok::kw_char:
954f4a2713aSLionel Sambuc   case tok::kw_const:
955f4a2713aSLionel Sambuc   case tok::kw_double:
956f4a2713aSLionel Sambuc   case tok::kw_enum:
957f4a2713aSLionel Sambuc   case tok::kw_half:
958f4a2713aSLionel Sambuc   case tok::kw_float:
959f4a2713aSLionel Sambuc   case tok::kw_int:
960f4a2713aSLionel Sambuc   case tok::kw_long:
961f4a2713aSLionel Sambuc   case tok::kw___int64:
962f4a2713aSLionel Sambuc   case tok::kw___int128:
963f4a2713aSLionel Sambuc   case tok::kw_restrict:
964f4a2713aSLionel Sambuc   case tok::kw_short:
965f4a2713aSLionel Sambuc   case tok::kw_signed:
966f4a2713aSLionel Sambuc   case tok::kw_struct:
967f4a2713aSLionel Sambuc   case tok::kw_union:
968f4a2713aSLionel Sambuc   case tok::kw_unsigned:
969f4a2713aSLionel Sambuc   case tok::kw_void:
970f4a2713aSLionel Sambuc   case tok::kw_volatile:
971f4a2713aSLionel Sambuc   case tok::kw__Bool:
972f4a2713aSLionel Sambuc   case tok::kw__Complex:
973f4a2713aSLionel Sambuc   case tok::kw_class:
974f4a2713aSLionel Sambuc   case tok::kw_typename:
975f4a2713aSLionel Sambuc   case tok::kw_wchar_t:
976f4a2713aSLionel Sambuc   case tok::kw_char16_t:
977f4a2713aSLionel Sambuc   case tok::kw_char32_t:
978f4a2713aSLionel Sambuc   case tok::kw__Decimal32:
979f4a2713aSLionel Sambuc   case tok::kw__Decimal64:
980f4a2713aSLionel Sambuc   case tok::kw__Decimal128:
981f4a2713aSLionel Sambuc   case tok::kw___interface:
982f4a2713aSLionel Sambuc   case tok::kw___thread:
983f4a2713aSLionel Sambuc   case tok::kw_thread_local:
984f4a2713aSLionel Sambuc   case tok::kw__Thread_local:
985f4a2713aSLionel Sambuc   case tok::kw_typeof:
986f4a2713aSLionel Sambuc   case tok::kw___underlying_type:
987f4a2713aSLionel Sambuc   case tok::kw___cdecl:
988f4a2713aSLionel Sambuc   case tok::kw___stdcall:
989f4a2713aSLionel Sambuc   case tok::kw___fastcall:
990f4a2713aSLionel Sambuc   case tok::kw___thiscall:
991*0a6a1f1dSLionel Sambuc   case tok::kw___vectorcall:
992f4a2713aSLionel Sambuc   case tok::kw___unaligned:
993f4a2713aSLionel Sambuc   case tok::kw___vector:
994f4a2713aSLionel Sambuc   case tok::kw___pixel:
995*0a6a1f1dSLionel Sambuc   case tok::kw___bool:
996f4a2713aSLionel Sambuc   case tok::kw__Atomic:
997f4a2713aSLionel Sambuc   case tok::kw___unknown_anytype:
998*0a6a1f1dSLionel Sambuc     return TPResult::False;
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc   default:
1001f4a2713aSLionel Sambuc     break;
1002f4a2713aSLionel Sambuc   }
1003f4a2713aSLionel Sambuc 
1004*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
1005f4a2713aSLionel Sambuc }
1006f4a2713aSLionel Sambuc 
isTentativelyDeclared(IdentifierInfo * II)1007f4a2713aSLionel Sambuc bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1008f4a2713aSLionel Sambuc   return std::find(TentativelyDeclaredIdentifiers.begin(),
1009f4a2713aSLionel Sambuc                    TentativelyDeclaredIdentifiers.end(), II)
1010f4a2713aSLionel Sambuc       != TentativelyDeclaredIdentifiers.end();
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc 
1013*0a6a1f1dSLionel Sambuc namespace {
1014*0a6a1f1dSLionel Sambuc class TentativeParseCCC : public CorrectionCandidateCallback {
1015*0a6a1f1dSLionel Sambuc public:
TentativeParseCCC(const Token & Next)1016*0a6a1f1dSLionel Sambuc   TentativeParseCCC(const Token &Next) {
1017*0a6a1f1dSLionel Sambuc     WantRemainingKeywords = false;
1018*0a6a1f1dSLionel Sambuc     WantTypeSpecifiers = Next.is(tok::l_paren) || Next.is(tok::r_paren) ||
1019*0a6a1f1dSLionel Sambuc                          Next.is(tok::greater) || Next.is(tok::l_brace) ||
1020*0a6a1f1dSLionel Sambuc                          Next.is(tok::identifier);
1021*0a6a1f1dSLionel Sambuc   }
1022*0a6a1f1dSLionel Sambuc 
ValidateCandidate(const TypoCorrection & Candidate)1023*0a6a1f1dSLionel Sambuc   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1024*0a6a1f1dSLionel Sambuc     // Reject any candidate that only resolves to instance members since they
1025*0a6a1f1dSLionel Sambuc     // aren't viable as standalone identifiers instead of member references.
1026*0a6a1f1dSLionel Sambuc     if (Candidate.isResolved() && !Candidate.isKeyword() &&
1027*0a6a1f1dSLionel Sambuc         std::all_of(Candidate.begin(), Candidate.end(),
1028*0a6a1f1dSLionel Sambuc                     [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1029*0a6a1f1dSLionel Sambuc       return false;
1030*0a6a1f1dSLionel Sambuc 
1031*0a6a1f1dSLionel Sambuc     return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1032*0a6a1f1dSLionel Sambuc   }
1033*0a6a1f1dSLionel Sambuc };
1034*0a6a1f1dSLionel Sambuc }
1035*0a6a1f1dSLionel Sambuc /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1036*0a6a1f1dSLionel Sambuc /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1037*0a6a1f1dSLionel Sambuc /// be either a decl-specifier or a function-style cast, and TPResult::Error
1038f4a2713aSLionel Sambuc /// if a parsing error was found and reported.
1039f4a2713aSLionel Sambuc ///
1040f4a2713aSLionel Sambuc /// If HasMissingTypename is provided, a name with a dependent scope specifier
1041f4a2713aSLionel Sambuc /// will be treated as ambiguous if the 'typename' keyword is missing. If this
1042f4a2713aSLionel Sambuc /// happens, *HasMissingTypename will be set to 'true'. This will also be used
1043f4a2713aSLionel Sambuc /// as an indicator that undeclared identifiers (which will trigger a later
1044*0a6a1f1dSLionel Sambuc /// parse error) should be treated as types. Returns TPResult::Ambiguous in
1045f4a2713aSLionel Sambuc /// such cases.
1046f4a2713aSLionel Sambuc ///
1047f4a2713aSLionel Sambuc ///         decl-specifier:
1048f4a2713aSLionel Sambuc ///           storage-class-specifier
1049f4a2713aSLionel Sambuc ///           type-specifier
1050f4a2713aSLionel Sambuc ///           function-specifier
1051f4a2713aSLionel Sambuc ///           'friend'
1052f4a2713aSLionel Sambuc ///           'typedef'
1053f4a2713aSLionel Sambuc /// [C++11]   'constexpr'
1054f4a2713aSLionel Sambuc /// [GNU]     attributes declaration-specifiers[opt]
1055f4a2713aSLionel Sambuc ///
1056f4a2713aSLionel Sambuc ///         storage-class-specifier:
1057f4a2713aSLionel Sambuc ///           'register'
1058f4a2713aSLionel Sambuc ///           'static'
1059f4a2713aSLionel Sambuc ///           'extern'
1060f4a2713aSLionel Sambuc ///           'mutable'
1061f4a2713aSLionel Sambuc ///           'auto'
1062f4a2713aSLionel Sambuc /// [GNU]     '__thread'
1063f4a2713aSLionel Sambuc /// [C++11]   'thread_local'
1064f4a2713aSLionel Sambuc /// [C11]     '_Thread_local'
1065f4a2713aSLionel Sambuc ///
1066f4a2713aSLionel Sambuc ///         function-specifier:
1067f4a2713aSLionel Sambuc ///           'inline'
1068f4a2713aSLionel Sambuc ///           'virtual'
1069f4a2713aSLionel Sambuc ///           'explicit'
1070f4a2713aSLionel Sambuc ///
1071f4a2713aSLionel Sambuc ///         typedef-name:
1072f4a2713aSLionel Sambuc ///           identifier
1073f4a2713aSLionel Sambuc ///
1074f4a2713aSLionel Sambuc ///         type-specifier:
1075f4a2713aSLionel Sambuc ///           simple-type-specifier
1076f4a2713aSLionel Sambuc ///           class-specifier
1077f4a2713aSLionel Sambuc ///           enum-specifier
1078f4a2713aSLionel Sambuc ///           elaborated-type-specifier
1079f4a2713aSLionel Sambuc ///           typename-specifier
1080f4a2713aSLionel Sambuc ///           cv-qualifier
1081f4a2713aSLionel Sambuc ///
1082f4a2713aSLionel Sambuc ///         simple-type-specifier:
1083f4a2713aSLionel Sambuc ///           '::'[opt] nested-name-specifier[opt] type-name
1084f4a2713aSLionel Sambuc ///           '::'[opt] nested-name-specifier 'template'
1085f4a2713aSLionel Sambuc ///                 simple-template-id                              [TODO]
1086f4a2713aSLionel Sambuc ///           'char'
1087f4a2713aSLionel Sambuc ///           'wchar_t'
1088f4a2713aSLionel Sambuc ///           'bool'
1089f4a2713aSLionel Sambuc ///           'short'
1090f4a2713aSLionel Sambuc ///           'int'
1091f4a2713aSLionel Sambuc ///           'long'
1092f4a2713aSLionel Sambuc ///           'signed'
1093f4a2713aSLionel Sambuc ///           'unsigned'
1094f4a2713aSLionel Sambuc ///           'float'
1095f4a2713aSLionel Sambuc ///           'double'
1096f4a2713aSLionel Sambuc ///           'void'
1097f4a2713aSLionel Sambuc /// [GNU]     typeof-specifier
1098f4a2713aSLionel Sambuc /// [GNU]     '_Complex'
1099f4a2713aSLionel Sambuc /// [C++11]   'auto'
1100f4a2713aSLionel Sambuc /// [C++11]   'decltype' ( expression )
1101f4a2713aSLionel Sambuc /// [C++1y]   'decltype' ( 'auto' )
1102f4a2713aSLionel Sambuc ///
1103f4a2713aSLionel Sambuc ///         type-name:
1104f4a2713aSLionel Sambuc ///           class-name
1105f4a2713aSLionel Sambuc ///           enum-name
1106f4a2713aSLionel Sambuc ///           typedef-name
1107f4a2713aSLionel Sambuc ///
1108f4a2713aSLionel Sambuc ///         elaborated-type-specifier:
1109f4a2713aSLionel Sambuc ///           class-key '::'[opt] nested-name-specifier[opt] identifier
1110f4a2713aSLionel Sambuc ///           class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1111f4a2713aSLionel Sambuc ///               simple-template-id
1112f4a2713aSLionel Sambuc ///           'enum' '::'[opt] nested-name-specifier[opt] identifier
1113f4a2713aSLionel Sambuc ///
1114f4a2713aSLionel Sambuc ///         enum-name:
1115f4a2713aSLionel Sambuc ///           identifier
1116f4a2713aSLionel Sambuc ///
1117f4a2713aSLionel Sambuc ///         enum-specifier:
1118f4a2713aSLionel Sambuc ///           'enum' identifier[opt] '{' enumerator-list[opt] '}'
1119f4a2713aSLionel Sambuc ///           'enum' identifier[opt] '{' enumerator-list ',' '}'
1120f4a2713aSLionel Sambuc ///
1121f4a2713aSLionel Sambuc ///         class-specifier:
1122f4a2713aSLionel Sambuc ///           class-head '{' member-specification[opt] '}'
1123f4a2713aSLionel Sambuc ///
1124f4a2713aSLionel Sambuc ///         class-head:
1125f4a2713aSLionel Sambuc ///           class-key identifier[opt] base-clause[opt]
1126f4a2713aSLionel Sambuc ///           class-key nested-name-specifier identifier base-clause[opt]
1127f4a2713aSLionel Sambuc ///           class-key nested-name-specifier[opt] simple-template-id
1128f4a2713aSLionel Sambuc ///               base-clause[opt]
1129f4a2713aSLionel Sambuc ///
1130f4a2713aSLionel Sambuc ///         class-key:
1131f4a2713aSLionel Sambuc ///           'class'
1132f4a2713aSLionel Sambuc ///           'struct'
1133f4a2713aSLionel Sambuc ///           'union'
1134f4a2713aSLionel Sambuc ///
1135f4a2713aSLionel Sambuc ///         cv-qualifier:
1136f4a2713aSLionel Sambuc ///           'const'
1137f4a2713aSLionel Sambuc ///           'volatile'
1138f4a2713aSLionel Sambuc /// [GNU]     restrict
1139f4a2713aSLionel Sambuc ///
1140f4a2713aSLionel Sambuc Parser::TPResult
isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,bool * HasMissingTypename)1141f4a2713aSLionel Sambuc Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1142f4a2713aSLionel Sambuc                                   bool *HasMissingTypename) {
1143f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
1144f4a2713aSLionel Sambuc   case tok::identifier: {
1145f4a2713aSLionel Sambuc     // Check for need to substitute AltiVec __vector keyword
1146f4a2713aSLionel Sambuc     // for "vector" identifier.
1147f4a2713aSLionel Sambuc     if (TryAltiVecVectorToken())
1148*0a6a1f1dSLionel Sambuc       return TPResult::True;
1149f4a2713aSLionel Sambuc 
1150f4a2713aSLionel Sambuc     const Token &Next = NextToken();
1151f4a2713aSLionel Sambuc     // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1152f4a2713aSLionel Sambuc     if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
1153*0a6a1f1dSLionel Sambuc       return TPResult::True;
1154f4a2713aSLionel Sambuc 
1155f4a2713aSLionel Sambuc     if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1156f4a2713aSLionel Sambuc       // Determine whether this is a valid expression. If not, we will hit
1157f4a2713aSLionel Sambuc       // a parse error one way or another. In that case, tell the caller that
1158f4a2713aSLionel Sambuc       // this is ambiguous. Typo-correct to type and expression keywords and
1159f4a2713aSLionel Sambuc       // to types and identifiers, in order to try to recover from errors.
1160f4a2713aSLionel Sambuc       switch (TryAnnotateName(false /* no nested name specifier */,
1161*0a6a1f1dSLionel Sambuc                               llvm::make_unique<TentativeParseCCC>(Next))) {
1162f4a2713aSLionel Sambuc       case ANK_Error:
1163*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1164f4a2713aSLionel Sambuc       case ANK_TentativeDecl:
1165*0a6a1f1dSLionel Sambuc         return TPResult::False;
1166f4a2713aSLionel Sambuc       case ANK_TemplateName:
1167f4a2713aSLionel Sambuc         // A bare type template-name which can't be a template template
1168f4a2713aSLionel Sambuc         // argument is an error, and was probably intended to be a type.
1169*0a6a1f1dSLionel Sambuc         return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1170f4a2713aSLionel Sambuc       case ANK_Unresolved:
1171*0a6a1f1dSLionel Sambuc         return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
1172f4a2713aSLionel Sambuc       case ANK_Success:
1173f4a2713aSLionel Sambuc         break;
1174f4a2713aSLionel Sambuc       }
1175f4a2713aSLionel Sambuc       assert(Tok.isNot(tok::identifier) &&
1176f4a2713aSLionel Sambuc              "TryAnnotateName succeeded without producing an annotation");
1177f4a2713aSLionel Sambuc     } else {
1178f4a2713aSLionel Sambuc       // This might possibly be a type with a dependent scope specifier and
1179f4a2713aSLionel Sambuc       // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1180f4a2713aSLionel Sambuc       // since it will annotate as a primary expression, and we want to use the
1181f4a2713aSLionel Sambuc       // "missing 'typename'" logic.
1182f4a2713aSLionel Sambuc       if (TryAnnotateTypeOrScopeToken())
1183*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1184f4a2713aSLionel Sambuc       // If annotation failed, assume it's a non-type.
1185f4a2713aSLionel Sambuc       // FIXME: If this happens due to an undeclared identifier, treat it as
1186f4a2713aSLionel Sambuc       // ambiguous.
1187f4a2713aSLionel Sambuc       if (Tok.is(tok::identifier))
1188*0a6a1f1dSLionel Sambuc         return TPResult::False;
1189f4a2713aSLionel Sambuc     }
1190f4a2713aSLionel Sambuc 
1191f4a2713aSLionel Sambuc     // We annotated this token as something. Recurse to handle whatever we got.
1192f4a2713aSLionel Sambuc     return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1193f4a2713aSLionel Sambuc   }
1194f4a2713aSLionel Sambuc 
1195f4a2713aSLionel Sambuc   case tok::kw_typename:  // typename T::type
1196f4a2713aSLionel Sambuc     // Annotate typenames and C++ scope specifiers.  If we get one, just
1197f4a2713aSLionel Sambuc     // recurse to handle whatever we get.
1198f4a2713aSLionel Sambuc     if (TryAnnotateTypeOrScopeToken())
1199*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1200f4a2713aSLionel Sambuc     return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1201f4a2713aSLionel Sambuc 
1202f4a2713aSLionel Sambuc   case tok::coloncolon: {    // ::foo::bar
1203f4a2713aSLionel Sambuc     const Token &Next = NextToken();
1204f4a2713aSLionel Sambuc     if (Next.is(tok::kw_new) ||    // ::new
1205f4a2713aSLionel Sambuc         Next.is(tok::kw_delete))   // ::delete
1206*0a6a1f1dSLionel Sambuc       return TPResult::False;
1207f4a2713aSLionel Sambuc   }
1208f4a2713aSLionel Sambuc     // Fall through.
1209*0a6a1f1dSLionel Sambuc   case tok::kw___super:
1210f4a2713aSLionel Sambuc   case tok::kw_decltype:
1211f4a2713aSLionel Sambuc     // Annotate typenames and C++ scope specifiers.  If we get one, just
1212f4a2713aSLionel Sambuc     // recurse to handle whatever we get.
1213f4a2713aSLionel Sambuc     if (TryAnnotateTypeOrScopeToken())
1214*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1215f4a2713aSLionel Sambuc     return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1216f4a2713aSLionel Sambuc 
1217f4a2713aSLionel Sambuc     // decl-specifier:
1218f4a2713aSLionel Sambuc     //   storage-class-specifier
1219f4a2713aSLionel Sambuc     //   type-specifier
1220f4a2713aSLionel Sambuc     //   function-specifier
1221f4a2713aSLionel Sambuc     //   'friend'
1222f4a2713aSLionel Sambuc     //   'typedef'
1223f4a2713aSLionel Sambuc     //   'constexpr'
1224f4a2713aSLionel Sambuc   case tok::kw_friend:
1225f4a2713aSLionel Sambuc   case tok::kw_typedef:
1226f4a2713aSLionel Sambuc   case tok::kw_constexpr:
1227f4a2713aSLionel Sambuc     // storage-class-specifier
1228f4a2713aSLionel Sambuc   case tok::kw_register:
1229f4a2713aSLionel Sambuc   case tok::kw_static:
1230f4a2713aSLionel Sambuc   case tok::kw_extern:
1231f4a2713aSLionel Sambuc   case tok::kw_mutable:
1232f4a2713aSLionel Sambuc   case tok::kw_auto:
1233f4a2713aSLionel Sambuc   case tok::kw___thread:
1234f4a2713aSLionel Sambuc   case tok::kw_thread_local:
1235f4a2713aSLionel Sambuc   case tok::kw__Thread_local:
1236f4a2713aSLionel Sambuc     // function-specifier
1237f4a2713aSLionel Sambuc   case tok::kw_inline:
1238f4a2713aSLionel Sambuc   case tok::kw_virtual:
1239f4a2713aSLionel Sambuc   case tok::kw_explicit:
1240f4a2713aSLionel Sambuc 
1241f4a2713aSLionel Sambuc     // Modules
1242f4a2713aSLionel Sambuc   case tok::kw___module_private__:
1243f4a2713aSLionel Sambuc 
1244f4a2713aSLionel Sambuc     // Debugger support
1245f4a2713aSLionel Sambuc   case tok::kw___unknown_anytype:
1246f4a2713aSLionel Sambuc 
1247f4a2713aSLionel Sambuc     // type-specifier:
1248f4a2713aSLionel Sambuc     //   simple-type-specifier
1249f4a2713aSLionel Sambuc     //   class-specifier
1250f4a2713aSLionel Sambuc     //   enum-specifier
1251f4a2713aSLionel Sambuc     //   elaborated-type-specifier
1252f4a2713aSLionel Sambuc     //   typename-specifier
1253f4a2713aSLionel Sambuc     //   cv-qualifier
1254f4a2713aSLionel Sambuc 
1255f4a2713aSLionel Sambuc     // class-specifier
1256f4a2713aSLionel Sambuc     // elaborated-type-specifier
1257f4a2713aSLionel Sambuc   case tok::kw_class:
1258f4a2713aSLionel Sambuc   case tok::kw_struct:
1259f4a2713aSLionel Sambuc   case tok::kw_union:
1260f4a2713aSLionel Sambuc   case tok::kw___interface:
1261f4a2713aSLionel Sambuc     // enum-specifier
1262f4a2713aSLionel Sambuc   case tok::kw_enum:
1263f4a2713aSLionel Sambuc     // cv-qualifier
1264f4a2713aSLionel Sambuc   case tok::kw_const:
1265f4a2713aSLionel Sambuc   case tok::kw_volatile:
1266f4a2713aSLionel Sambuc 
1267f4a2713aSLionel Sambuc     // GNU
1268f4a2713aSLionel Sambuc   case tok::kw_restrict:
1269f4a2713aSLionel Sambuc   case tok::kw__Complex:
1270f4a2713aSLionel Sambuc   case tok::kw___attribute:
1271*0a6a1f1dSLionel Sambuc     return TPResult::True;
1272f4a2713aSLionel Sambuc 
1273f4a2713aSLionel Sambuc     // Microsoft
1274f4a2713aSLionel Sambuc   case tok::kw___declspec:
1275f4a2713aSLionel Sambuc   case tok::kw___cdecl:
1276f4a2713aSLionel Sambuc   case tok::kw___stdcall:
1277f4a2713aSLionel Sambuc   case tok::kw___fastcall:
1278f4a2713aSLionel Sambuc   case tok::kw___thiscall:
1279*0a6a1f1dSLionel Sambuc   case tok::kw___vectorcall:
1280f4a2713aSLionel Sambuc   case tok::kw___w64:
1281f4a2713aSLionel Sambuc   case tok::kw___sptr:
1282f4a2713aSLionel Sambuc   case tok::kw___uptr:
1283f4a2713aSLionel Sambuc   case tok::kw___ptr64:
1284f4a2713aSLionel Sambuc   case tok::kw___ptr32:
1285f4a2713aSLionel Sambuc   case tok::kw___forceinline:
1286f4a2713aSLionel Sambuc   case tok::kw___unaligned:
1287*0a6a1f1dSLionel Sambuc     return TPResult::True;
1288f4a2713aSLionel Sambuc 
1289f4a2713aSLionel Sambuc     // Borland
1290f4a2713aSLionel Sambuc   case tok::kw___pascal:
1291*0a6a1f1dSLionel Sambuc     return TPResult::True;
1292f4a2713aSLionel Sambuc 
1293f4a2713aSLionel Sambuc     // AltiVec
1294f4a2713aSLionel Sambuc   case tok::kw___vector:
1295*0a6a1f1dSLionel Sambuc     return TPResult::True;
1296f4a2713aSLionel Sambuc 
1297f4a2713aSLionel Sambuc   case tok::annot_template_id: {
1298f4a2713aSLionel Sambuc     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1299f4a2713aSLionel Sambuc     if (TemplateId->Kind != TNK_Type_template)
1300*0a6a1f1dSLionel Sambuc       return TPResult::False;
1301f4a2713aSLionel Sambuc     CXXScopeSpec SS;
1302f4a2713aSLionel Sambuc     AnnotateTemplateIdTokenAsType();
1303f4a2713aSLionel Sambuc     assert(Tok.is(tok::annot_typename));
1304f4a2713aSLionel Sambuc     goto case_typename;
1305f4a2713aSLionel Sambuc   }
1306f4a2713aSLionel Sambuc 
1307f4a2713aSLionel Sambuc   case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1308f4a2713aSLionel Sambuc     // We've already annotated a scope; try to annotate a type.
1309f4a2713aSLionel Sambuc     if (TryAnnotateTypeOrScopeToken())
1310*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1311f4a2713aSLionel Sambuc     if (!Tok.is(tok::annot_typename)) {
1312f4a2713aSLionel Sambuc       // If the next token is an identifier or a type qualifier, then this
1313f4a2713aSLionel Sambuc       // can't possibly be a valid expression either.
1314f4a2713aSLionel Sambuc       if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1315f4a2713aSLionel Sambuc         CXXScopeSpec SS;
1316f4a2713aSLionel Sambuc         Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1317f4a2713aSLionel Sambuc                                                      Tok.getAnnotationRange(),
1318f4a2713aSLionel Sambuc                                                      SS);
1319f4a2713aSLionel Sambuc         if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1320f4a2713aSLionel Sambuc           TentativeParsingAction PA(*this);
1321f4a2713aSLionel Sambuc           ConsumeToken();
1322f4a2713aSLionel Sambuc           ConsumeToken();
1323f4a2713aSLionel Sambuc           bool isIdentifier = Tok.is(tok::identifier);
1324*0a6a1f1dSLionel Sambuc           TPResult TPR = TPResult::False;
1325f4a2713aSLionel Sambuc           if (!isIdentifier)
1326f4a2713aSLionel Sambuc             TPR = isCXXDeclarationSpecifier(BracedCastResult,
1327f4a2713aSLionel Sambuc                                             HasMissingTypename);
1328f4a2713aSLionel Sambuc           PA.Revert();
1329f4a2713aSLionel Sambuc 
1330f4a2713aSLionel Sambuc           if (isIdentifier ||
1331*0a6a1f1dSLionel Sambuc               TPR == TPResult::True || TPR == TPResult::Error)
1332*0a6a1f1dSLionel Sambuc             return TPResult::Error;
1333f4a2713aSLionel Sambuc 
1334f4a2713aSLionel Sambuc           if (HasMissingTypename) {
1335f4a2713aSLionel Sambuc             // We can't tell whether this is a missing 'typename' or a valid
1336f4a2713aSLionel Sambuc             // expression.
1337f4a2713aSLionel Sambuc             *HasMissingTypename = true;
1338*0a6a1f1dSLionel Sambuc             return TPResult::Ambiguous;
1339f4a2713aSLionel Sambuc           }
1340f4a2713aSLionel Sambuc         } else {
1341f4a2713aSLionel Sambuc           // Try to resolve the name. If it doesn't exist, assume it was
1342f4a2713aSLionel Sambuc           // intended to name a type and keep disambiguating.
1343f4a2713aSLionel Sambuc           switch (TryAnnotateName(false /* SS is not dependent */)) {
1344f4a2713aSLionel Sambuc           case ANK_Error:
1345*0a6a1f1dSLionel Sambuc             return TPResult::Error;
1346f4a2713aSLionel Sambuc           case ANK_TentativeDecl:
1347*0a6a1f1dSLionel Sambuc             return TPResult::False;
1348f4a2713aSLionel Sambuc           case ANK_TemplateName:
1349f4a2713aSLionel Sambuc             // A bare type template-name which can't be a template template
1350f4a2713aSLionel Sambuc             // argument is an error, and was probably intended to be a type.
1351*0a6a1f1dSLionel Sambuc             return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1352f4a2713aSLionel Sambuc           case ANK_Unresolved:
1353*0a6a1f1dSLionel Sambuc             return HasMissingTypename ? TPResult::Ambiguous
1354*0a6a1f1dSLionel Sambuc                                       : TPResult::False;
1355f4a2713aSLionel Sambuc           case ANK_Success:
1356f4a2713aSLionel Sambuc             // Annotated it, check again.
1357f4a2713aSLionel Sambuc             assert(Tok.isNot(tok::annot_cxxscope) ||
1358f4a2713aSLionel Sambuc                    NextToken().isNot(tok::identifier));
1359f4a2713aSLionel Sambuc             return isCXXDeclarationSpecifier(BracedCastResult,
1360f4a2713aSLionel Sambuc                                              HasMissingTypename);
1361f4a2713aSLionel Sambuc           }
1362f4a2713aSLionel Sambuc         }
1363f4a2713aSLionel Sambuc       }
1364*0a6a1f1dSLionel Sambuc       return TPResult::False;
1365f4a2713aSLionel Sambuc     }
1366f4a2713aSLionel Sambuc     // If that succeeded, fallthrough into the generic simple-type-id case.
1367f4a2713aSLionel Sambuc 
1368f4a2713aSLionel Sambuc     // The ambiguity resides in a simple-type-specifier/typename-specifier
1369f4a2713aSLionel Sambuc     // followed by a '('. The '(' could either be the start of:
1370f4a2713aSLionel Sambuc     //
1371f4a2713aSLionel Sambuc     //   direct-declarator:
1372f4a2713aSLionel Sambuc     //     '(' declarator ')'
1373f4a2713aSLionel Sambuc     //
1374f4a2713aSLionel Sambuc     //   direct-abstract-declarator:
1375f4a2713aSLionel Sambuc     //     '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1376f4a2713aSLionel Sambuc     //              exception-specification[opt]
1377f4a2713aSLionel Sambuc     //     '(' abstract-declarator ')'
1378f4a2713aSLionel Sambuc     //
1379f4a2713aSLionel Sambuc     // or part of a function-style cast expression:
1380f4a2713aSLionel Sambuc     //
1381f4a2713aSLionel Sambuc     //     simple-type-specifier '(' expression-list[opt] ')'
1382f4a2713aSLionel Sambuc     //
1383f4a2713aSLionel Sambuc 
1384f4a2713aSLionel Sambuc     // simple-type-specifier:
1385f4a2713aSLionel Sambuc 
1386f4a2713aSLionel Sambuc   case tok::annot_typename:
1387f4a2713aSLionel Sambuc   case_typename:
1388f4a2713aSLionel Sambuc     // In Objective-C, we might have a protocol-qualified type.
1389f4a2713aSLionel Sambuc     if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1390f4a2713aSLionel Sambuc       // Tentatively parse the
1391f4a2713aSLionel Sambuc       TentativeParsingAction PA(*this);
1392f4a2713aSLionel Sambuc       ConsumeToken(); // The type token
1393f4a2713aSLionel Sambuc 
1394f4a2713aSLionel Sambuc       TPResult TPR = TryParseProtocolQualifiers();
1395f4a2713aSLionel Sambuc       bool isFollowedByParen = Tok.is(tok::l_paren);
1396f4a2713aSLionel Sambuc       bool isFollowedByBrace = Tok.is(tok::l_brace);
1397f4a2713aSLionel Sambuc 
1398f4a2713aSLionel Sambuc       PA.Revert();
1399f4a2713aSLionel Sambuc 
1400*0a6a1f1dSLionel Sambuc       if (TPR == TPResult::Error)
1401*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc       if (isFollowedByParen)
1404*0a6a1f1dSLionel Sambuc         return TPResult::Ambiguous;
1405f4a2713aSLionel Sambuc 
1406f4a2713aSLionel Sambuc       if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1407f4a2713aSLionel Sambuc         return BracedCastResult;
1408f4a2713aSLionel Sambuc 
1409*0a6a1f1dSLionel Sambuc       return TPResult::True;
1410f4a2713aSLionel Sambuc     }
1411f4a2713aSLionel Sambuc 
1412f4a2713aSLionel Sambuc   case tok::kw_char:
1413f4a2713aSLionel Sambuc   case tok::kw_wchar_t:
1414f4a2713aSLionel Sambuc   case tok::kw_char16_t:
1415f4a2713aSLionel Sambuc   case tok::kw_char32_t:
1416f4a2713aSLionel Sambuc   case tok::kw_bool:
1417f4a2713aSLionel Sambuc   case tok::kw_short:
1418f4a2713aSLionel Sambuc   case tok::kw_int:
1419f4a2713aSLionel Sambuc   case tok::kw_long:
1420f4a2713aSLionel Sambuc   case tok::kw___int64:
1421f4a2713aSLionel Sambuc   case tok::kw___int128:
1422f4a2713aSLionel Sambuc   case tok::kw_signed:
1423f4a2713aSLionel Sambuc   case tok::kw_unsigned:
1424f4a2713aSLionel Sambuc   case tok::kw_half:
1425f4a2713aSLionel Sambuc   case tok::kw_float:
1426f4a2713aSLionel Sambuc   case tok::kw_double:
1427f4a2713aSLionel Sambuc   case tok::kw_void:
1428f4a2713aSLionel Sambuc   case tok::annot_decltype:
1429f4a2713aSLionel Sambuc     if (NextToken().is(tok::l_paren))
1430*0a6a1f1dSLionel Sambuc       return TPResult::Ambiguous;
1431f4a2713aSLionel Sambuc 
1432f4a2713aSLionel Sambuc     // This is a function-style cast in all cases we disambiguate other than
1433f4a2713aSLionel Sambuc     // one:
1434f4a2713aSLionel Sambuc     //   struct S {
1435f4a2713aSLionel Sambuc     //     enum E : int { a = 4 }; // enum
1436f4a2713aSLionel Sambuc     //     enum E : int { 4 };     // bit-field
1437f4a2713aSLionel Sambuc     //   };
1438f4a2713aSLionel Sambuc     if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1439f4a2713aSLionel Sambuc       return BracedCastResult;
1440f4a2713aSLionel Sambuc 
1441f4a2713aSLionel Sambuc     if (isStartOfObjCClassMessageMissingOpenBracket())
1442*0a6a1f1dSLionel Sambuc       return TPResult::False;
1443f4a2713aSLionel Sambuc 
1444*0a6a1f1dSLionel Sambuc     return TPResult::True;
1445f4a2713aSLionel Sambuc 
1446f4a2713aSLionel Sambuc   // GNU typeof support.
1447f4a2713aSLionel Sambuc   case tok::kw_typeof: {
1448f4a2713aSLionel Sambuc     if (NextToken().isNot(tok::l_paren))
1449*0a6a1f1dSLionel Sambuc       return TPResult::True;
1450f4a2713aSLionel Sambuc 
1451f4a2713aSLionel Sambuc     TentativeParsingAction PA(*this);
1452f4a2713aSLionel Sambuc 
1453f4a2713aSLionel Sambuc     TPResult TPR = TryParseTypeofSpecifier();
1454f4a2713aSLionel Sambuc     bool isFollowedByParen = Tok.is(tok::l_paren);
1455f4a2713aSLionel Sambuc     bool isFollowedByBrace = Tok.is(tok::l_brace);
1456f4a2713aSLionel Sambuc 
1457f4a2713aSLionel Sambuc     PA.Revert();
1458f4a2713aSLionel Sambuc 
1459*0a6a1f1dSLionel Sambuc     if (TPR == TPResult::Error)
1460*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc     if (isFollowedByParen)
1463*0a6a1f1dSLionel Sambuc       return TPResult::Ambiguous;
1464f4a2713aSLionel Sambuc 
1465f4a2713aSLionel Sambuc     if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1466f4a2713aSLionel Sambuc       return BracedCastResult;
1467f4a2713aSLionel Sambuc 
1468*0a6a1f1dSLionel Sambuc     return TPResult::True;
1469f4a2713aSLionel Sambuc   }
1470f4a2713aSLionel Sambuc 
1471f4a2713aSLionel Sambuc   // C++0x type traits support
1472f4a2713aSLionel Sambuc   case tok::kw___underlying_type:
1473*0a6a1f1dSLionel Sambuc     return TPResult::True;
1474f4a2713aSLionel Sambuc 
1475f4a2713aSLionel Sambuc   // C11 _Atomic
1476f4a2713aSLionel Sambuc   case tok::kw__Atomic:
1477*0a6a1f1dSLionel Sambuc     return TPResult::True;
1478f4a2713aSLionel Sambuc 
1479f4a2713aSLionel Sambuc   default:
1480*0a6a1f1dSLionel Sambuc     return TPResult::False;
1481f4a2713aSLionel Sambuc   }
1482f4a2713aSLionel Sambuc }
1483f4a2713aSLionel Sambuc 
isCXXDeclarationSpecifierAType()1484f4a2713aSLionel Sambuc bool Parser::isCXXDeclarationSpecifierAType() {
1485f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
1486f4a2713aSLionel Sambuc     // typename-specifier
1487f4a2713aSLionel Sambuc   case tok::annot_decltype:
1488f4a2713aSLionel Sambuc   case tok::annot_template_id:
1489f4a2713aSLionel Sambuc   case tok::annot_typename:
1490f4a2713aSLionel Sambuc   case tok::kw_typeof:
1491f4a2713aSLionel Sambuc   case tok::kw___underlying_type:
1492f4a2713aSLionel Sambuc     return true;
1493f4a2713aSLionel Sambuc 
1494f4a2713aSLionel Sambuc     // elaborated-type-specifier
1495f4a2713aSLionel Sambuc   case tok::kw_class:
1496f4a2713aSLionel Sambuc   case tok::kw_struct:
1497f4a2713aSLionel Sambuc   case tok::kw_union:
1498f4a2713aSLionel Sambuc   case tok::kw___interface:
1499f4a2713aSLionel Sambuc   case tok::kw_enum:
1500f4a2713aSLionel Sambuc     return true;
1501f4a2713aSLionel Sambuc 
1502f4a2713aSLionel Sambuc     // simple-type-specifier
1503f4a2713aSLionel Sambuc   case tok::kw_char:
1504f4a2713aSLionel Sambuc   case tok::kw_wchar_t:
1505f4a2713aSLionel Sambuc   case tok::kw_char16_t:
1506f4a2713aSLionel Sambuc   case tok::kw_char32_t:
1507f4a2713aSLionel Sambuc   case tok::kw_bool:
1508f4a2713aSLionel Sambuc   case tok::kw_short:
1509f4a2713aSLionel Sambuc   case tok::kw_int:
1510f4a2713aSLionel Sambuc   case tok::kw_long:
1511f4a2713aSLionel Sambuc   case tok::kw___int64:
1512f4a2713aSLionel Sambuc   case tok::kw___int128:
1513f4a2713aSLionel Sambuc   case tok::kw_signed:
1514f4a2713aSLionel Sambuc   case tok::kw_unsigned:
1515f4a2713aSLionel Sambuc   case tok::kw_half:
1516f4a2713aSLionel Sambuc   case tok::kw_float:
1517f4a2713aSLionel Sambuc   case tok::kw_double:
1518f4a2713aSLionel Sambuc   case tok::kw_void:
1519f4a2713aSLionel Sambuc   case tok::kw___unknown_anytype:
1520f4a2713aSLionel Sambuc     return true;
1521f4a2713aSLionel Sambuc 
1522f4a2713aSLionel Sambuc   case tok::kw_auto:
1523f4a2713aSLionel Sambuc     return getLangOpts().CPlusPlus11;
1524f4a2713aSLionel Sambuc 
1525f4a2713aSLionel Sambuc   case tok::kw__Atomic:
1526f4a2713aSLionel Sambuc     // "_Atomic foo"
1527f4a2713aSLionel Sambuc     return NextToken().is(tok::l_paren);
1528f4a2713aSLionel Sambuc 
1529f4a2713aSLionel Sambuc   default:
1530f4a2713aSLionel Sambuc     return false;
1531f4a2713aSLionel Sambuc   }
1532f4a2713aSLionel Sambuc }
1533f4a2713aSLionel Sambuc 
1534f4a2713aSLionel Sambuc /// [GNU] typeof-specifier:
1535f4a2713aSLionel Sambuc ///         'typeof' '(' expressions ')'
1536f4a2713aSLionel Sambuc ///         'typeof' '(' type-name ')'
1537f4a2713aSLionel Sambuc ///
TryParseTypeofSpecifier()1538f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseTypeofSpecifier() {
1539f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1540f4a2713aSLionel Sambuc   ConsumeToken();
1541f4a2713aSLionel Sambuc 
1542f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_paren) && "Expected '('");
1543f4a2713aSLionel Sambuc   // Parse through the parens after 'typeof'.
1544f4a2713aSLionel Sambuc   ConsumeParen();
1545f4a2713aSLionel Sambuc   if (!SkipUntil(tok::r_paren, StopAtSemi))
1546*0a6a1f1dSLionel Sambuc     return TPResult::Error;
1547f4a2713aSLionel Sambuc 
1548*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
1549f4a2713aSLionel Sambuc }
1550f4a2713aSLionel Sambuc 
1551f4a2713aSLionel Sambuc /// [ObjC] protocol-qualifiers:
1552f4a2713aSLionel Sambuc ////         '<' identifier-list '>'
TryParseProtocolQualifiers()1553f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseProtocolQualifiers() {
1554f4a2713aSLionel Sambuc   assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1555f4a2713aSLionel Sambuc   ConsumeToken();
1556f4a2713aSLionel Sambuc   do {
1557f4a2713aSLionel Sambuc     if (Tok.isNot(tok::identifier))
1558*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1559f4a2713aSLionel Sambuc     ConsumeToken();
1560f4a2713aSLionel Sambuc 
1561f4a2713aSLionel Sambuc     if (Tok.is(tok::comma)) {
1562f4a2713aSLionel Sambuc       ConsumeToken();
1563f4a2713aSLionel Sambuc       continue;
1564f4a2713aSLionel Sambuc     }
1565f4a2713aSLionel Sambuc 
1566f4a2713aSLionel Sambuc     if (Tok.is(tok::greater)) {
1567f4a2713aSLionel Sambuc       ConsumeToken();
1568*0a6a1f1dSLionel Sambuc       return TPResult::Ambiguous;
1569f4a2713aSLionel Sambuc     }
1570f4a2713aSLionel Sambuc   } while (false);
1571f4a2713aSLionel Sambuc 
1572*0a6a1f1dSLionel Sambuc   return TPResult::Error;
1573f4a2713aSLionel Sambuc }
1574f4a2713aSLionel Sambuc 
1575f4a2713aSLionel Sambuc /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1576f4a2713aSLionel Sambuc /// a constructor-style initializer, when parsing declaration statements.
1577f4a2713aSLionel Sambuc /// Returns true for function declarator and false for constructor-style
1578f4a2713aSLionel Sambuc /// initializer.
1579f4a2713aSLionel Sambuc /// If during the disambiguation process a parsing error is encountered,
1580f4a2713aSLionel Sambuc /// the function returns true to let the declaration parsing code handle it.
1581f4a2713aSLionel Sambuc ///
1582f4a2713aSLionel Sambuc /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1583f4a2713aSLionel Sambuc ///         exception-specification[opt]
1584f4a2713aSLionel Sambuc ///
isCXXFunctionDeclarator(bool * IsAmbiguous)1585f4a2713aSLionel Sambuc bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1586f4a2713aSLionel Sambuc 
1587f4a2713aSLionel Sambuc   // C++ 8.2p1:
1588f4a2713aSLionel Sambuc   // The ambiguity arising from the similarity between a function-style cast and
1589f4a2713aSLionel Sambuc   // a declaration mentioned in 6.8 can also occur in the context of a
1590f4a2713aSLionel Sambuc   // declaration. In that context, the choice is between a function declaration
1591f4a2713aSLionel Sambuc   // with a redundant set of parentheses around a parameter name and an object
1592f4a2713aSLionel Sambuc   // declaration with a function-style cast as the initializer. Just as for the
1593f4a2713aSLionel Sambuc   // ambiguities mentioned in 6.8, the resolution is to consider any construct
1594f4a2713aSLionel Sambuc   // that could possibly be a declaration a declaration.
1595f4a2713aSLionel Sambuc 
1596f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
1597f4a2713aSLionel Sambuc 
1598f4a2713aSLionel Sambuc   ConsumeParen();
1599f4a2713aSLionel Sambuc   bool InvalidAsDeclaration = false;
1600f4a2713aSLionel Sambuc   TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1601*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Ambiguous) {
1602f4a2713aSLionel Sambuc     if (Tok.isNot(tok::r_paren))
1603*0a6a1f1dSLionel Sambuc       TPR = TPResult::False;
1604f4a2713aSLionel Sambuc     else {
1605f4a2713aSLionel Sambuc       const Token &Next = NextToken();
1606f4a2713aSLionel Sambuc       if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1607f4a2713aSLionel Sambuc           Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1608f4a2713aSLionel Sambuc           Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1609f4a2713aSLionel Sambuc           Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
1610f4a2713aSLionel Sambuc           Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1611f4a2713aSLionel Sambuc           Next.is(tok::equal) || Next.is(tok::arrow))
1612f4a2713aSLionel Sambuc         // The next token cannot appear after a constructor-style initializer,
1613f4a2713aSLionel Sambuc         // and can appear next in a function definition. This must be a function
1614f4a2713aSLionel Sambuc         // declarator.
1615*0a6a1f1dSLionel Sambuc         TPR = TPResult::True;
1616f4a2713aSLionel Sambuc       else if (InvalidAsDeclaration)
1617f4a2713aSLionel Sambuc         // Use the absence of 'typename' as a tie-breaker.
1618*0a6a1f1dSLionel Sambuc         TPR = TPResult::False;
1619f4a2713aSLionel Sambuc     }
1620f4a2713aSLionel Sambuc   }
1621f4a2713aSLionel Sambuc 
1622f4a2713aSLionel Sambuc   PA.Revert();
1623f4a2713aSLionel Sambuc 
1624*0a6a1f1dSLionel Sambuc   if (IsAmbiguous && TPR == TPResult::Ambiguous)
1625f4a2713aSLionel Sambuc     *IsAmbiguous = true;
1626f4a2713aSLionel Sambuc 
1627f4a2713aSLionel Sambuc   // In case of an error, let the declaration parsing code handle it.
1628*0a6a1f1dSLionel Sambuc   return TPR != TPResult::False;
1629f4a2713aSLionel Sambuc }
1630f4a2713aSLionel Sambuc 
1631f4a2713aSLionel Sambuc /// parameter-declaration-clause:
1632f4a2713aSLionel Sambuc ///   parameter-declaration-list[opt] '...'[opt]
1633f4a2713aSLionel Sambuc ///   parameter-declaration-list ',' '...'
1634f4a2713aSLionel Sambuc ///
1635f4a2713aSLionel Sambuc /// parameter-declaration-list:
1636f4a2713aSLionel Sambuc ///   parameter-declaration
1637f4a2713aSLionel Sambuc ///   parameter-declaration-list ',' parameter-declaration
1638f4a2713aSLionel Sambuc ///
1639f4a2713aSLionel Sambuc /// parameter-declaration:
1640f4a2713aSLionel Sambuc ///   attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1641f4a2713aSLionel Sambuc ///   attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1642f4a2713aSLionel Sambuc ///     '=' assignment-expression
1643f4a2713aSLionel Sambuc ///   attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1644f4a2713aSLionel Sambuc ///     attributes[opt]
1645f4a2713aSLionel Sambuc ///   attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1646f4a2713aSLionel Sambuc ///     attributes[opt] '=' assignment-expression
1647f4a2713aSLionel Sambuc ///
1648f4a2713aSLionel Sambuc Parser::TPResult
TryParseParameterDeclarationClause(bool * InvalidAsDeclaration,bool VersusTemplateArgument)1649f4a2713aSLionel Sambuc Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1650f4a2713aSLionel Sambuc                                            bool VersusTemplateArgument) {
1651f4a2713aSLionel Sambuc 
1652f4a2713aSLionel Sambuc   if (Tok.is(tok::r_paren))
1653*0a6a1f1dSLionel Sambuc     return TPResult::Ambiguous;
1654f4a2713aSLionel Sambuc 
1655f4a2713aSLionel Sambuc   //   parameter-declaration-list[opt] '...'[opt]
1656f4a2713aSLionel Sambuc   //   parameter-declaration-list ',' '...'
1657f4a2713aSLionel Sambuc   //
1658f4a2713aSLionel Sambuc   // parameter-declaration-list:
1659f4a2713aSLionel Sambuc   //   parameter-declaration
1660f4a2713aSLionel Sambuc   //   parameter-declaration-list ',' parameter-declaration
1661f4a2713aSLionel Sambuc   //
1662f4a2713aSLionel Sambuc   while (1) {
1663f4a2713aSLionel Sambuc     // '...'[opt]
1664f4a2713aSLionel Sambuc     if (Tok.is(tok::ellipsis)) {
1665f4a2713aSLionel Sambuc       ConsumeToken();
1666f4a2713aSLionel Sambuc       if (Tok.is(tok::r_paren))
1667*0a6a1f1dSLionel Sambuc         return TPResult::True; // '...)' is a sign of a function declarator.
1668f4a2713aSLionel Sambuc       else
1669*0a6a1f1dSLionel Sambuc         return TPResult::False;
1670f4a2713aSLionel Sambuc     }
1671f4a2713aSLionel Sambuc 
1672f4a2713aSLionel Sambuc     // An attribute-specifier-seq here is a sign of a function declarator.
1673f4a2713aSLionel Sambuc     if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1674f4a2713aSLionel Sambuc                                   /*OuterMightBeMessageSend*/true))
1675*0a6a1f1dSLionel Sambuc       return TPResult::True;
1676f4a2713aSLionel Sambuc 
1677f4a2713aSLionel Sambuc     ParsedAttributes attrs(AttrFactory);
1678f4a2713aSLionel Sambuc     MaybeParseMicrosoftAttributes(attrs);
1679f4a2713aSLionel Sambuc 
1680f4a2713aSLionel Sambuc     // decl-specifier-seq
1681f4a2713aSLionel Sambuc     // A parameter-declaration's initializer must be preceded by an '=', so
1682f4a2713aSLionel Sambuc     // decl-specifier-seq '{' is not a parameter in C++11.
1683*0a6a1f1dSLionel Sambuc     TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1684f4a2713aSLionel Sambuc                                              InvalidAsDeclaration);
1685f4a2713aSLionel Sambuc 
1686*0a6a1f1dSLionel Sambuc     if (VersusTemplateArgument && TPR == TPResult::True) {
1687f4a2713aSLionel Sambuc       // Consume the decl-specifier-seq. We have to look past it, since a
1688f4a2713aSLionel Sambuc       // type-id might appear here in a template argument.
1689f4a2713aSLionel Sambuc       bool SeenType = false;
1690f4a2713aSLionel Sambuc       do {
1691f4a2713aSLionel Sambuc         SeenType |= isCXXDeclarationSpecifierAType();
1692*0a6a1f1dSLionel Sambuc         if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1693*0a6a1f1dSLionel Sambuc           return TPResult::Error;
1694f4a2713aSLionel Sambuc 
1695f4a2713aSLionel Sambuc         // If we see a parameter name, this can't be a template argument.
1696f4a2713aSLionel Sambuc         if (SeenType && Tok.is(tok::identifier))
1697*0a6a1f1dSLionel Sambuc           return TPResult::True;
1698f4a2713aSLionel Sambuc 
1699*0a6a1f1dSLionel Sambuc         TPR = isCXXDeclarationSpecifier(TPResult::False,
1700f4a2713aSLionel Sambuc                                         InvalidAsDeclaration);
1701*0a6a1f1dSLionel Sambuc         if (TPR == TPResult::Error)
1702f4a2713aSLionel Sambuc           return TPR;
1703*0a6a1f1dSLionel Sambuc       } while (TPR != TPResult::False);
1704*0a6a1f1dSLionel Sambuc     } else if (TPR == TPResult::Ambiguous) {
1705f4a2713aSLionel Sambuc       // Disambiguate what follows the decl-specifier.
1706*0a6a1f1dSLionel Sambuc       if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1707*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1708f4a2713aSLionel Sambuc     } else
1709f4a2713aSLionel Sambuc       return TPR;
1710f4a2713aSLionel Sambuc 
1711f4a2713aSLionel Sambuc     // declarator
1712f4a2713aSLionel Sambuc     // abstract-declarator[opt]
1713f4a2713aSLionel Sambuc     TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1714*0a6a1f1dSLionel Sambuc     if (TPR != TPResult::Ambiguous)
1715f4a2713aSLionel Sambuc       return TPR;
1716f4a2713aSLionel Sambuc 
1717f4a2713aSLionel Sambuc     // [GNU] attributes[opt]
1718f4a2713aSLionel Sambuc     if (Tok.is(tok::kw___attribute))
1719*0a6a1f1dSLionel Sambuc       return TPResult::True;
1720f4a2713aSLionel Sambuc 
1721f4a2713aSLionel Sambuc     // If we're disambiguating a template argument in a default argument in
1722f4a2713aSLionel Sambuc     // a class definition versus a parameter declaration, an '=' here
1723f4a2713aSLionel Sambuc     // disambiguates the parse one way or the other.
1724f4a2713aSLionel Sambuc     // If this is a parameter, it must have a default argument because
1725f4a2713aSLionel Sambuc     //   (a) the previous parameter did, and
1726f4a2713aSLionel Sambuc     //   (b) this must be the first declaration of the function, so we can't
1727f4a2713aSLionel Sambuc     //       inherit any default arguments from elsewhere.
1728f4a2713aSLionel Sambuc     // If we see an ')', then we've reached the end of a
1729f4a2713aSLionel Sambuc     // parameter-declaration-clause, and the last param is missing its default
1730f4a2713aSLionel Sambuc     // argument.
1731f4a2713aSLionel Sambuc     if (VersusTemplateArgument)
1732*0a6a1f1dSLionel Sambuc       return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True
1733*0a6a1f1dSLionel Sambuc                                                           : TPResult::False;
1734f4a2713aSLionel Sambuc 
1735f4a2713aSLionel Sambuc     if (Tok.is(tok::equal)) {
1736f4a2713aSLionel Sambuc       // '=' assignment-expression
1737f4a2713aSLionel Sambuc       // Parse through assignment-expression.
1738f4a2713aSLionel Sambuc       // FIXME: assignment-expression may contain an unparenthesized comma.
1739f4a2713aSLionel Sambuc       if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
1740*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1741f4a2713aSLionel Sambuc     }
1742f4a2713aSLionel Sambuc 
1743f4a2713aSLionel Sambuc     if (Tok.is(tok::ellipsis)) {
1744f4a2713aSLionel Sambuc       ConsumeToken();
1745f4a2713aSLionel Sambuc       if (Tok.is(tok::r_paren))
1746*0a6a1f1dSLionel Sambuc         return TPResult::True; // '...)' is a sign of a function declarator.
1747f4a2713aSLionel Sambuc       else
1748*0a6a1f1dSLionel Sambuc         return TPResult::False;
1749f4a2713aSLionel Sambuc     }
1750f4a2713aSLionel Sambuc 
1751*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma))
1752f4a2713aSLionel Sambuc       break;
1753f4a2713aSLionel Sambuc   }
1754f4a2713aSLionel Sambuc 
1755*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
1756f4a2713aSLionel Sambuc }
1757f4a2713aSLionel Sambuc 
1758f4a2713aSLionel Sambuc /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1759f4a2713aSLionel Sambuc /// parsing as a function declarator.
1760f4a2713aSLionel Sambuc /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1761*0a6a1f1dSLionel Sambuc /// return TPResult::Ambiguous, otherwise it will return either False() or
1762f4a2713aSLionel Sambuc /// Error().
1763f4a2713aSLionel Sambuc ///
1764f4a2713aSLionel Sambuc /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1765f4a2713aSLionel Sambuc ///         exception-specification[opt]
1766f4a2713aSLionel Sambuc ///
1767f4a2713aSLionel Sambuc /// exception-specification:
1768f4a2713aSLionel Sambuc ///   'throw' '(' type-id-list[opt] ')'
1769f4a2713aSLionel Sambuc ///
TryParseFunctionDeclarator()1770f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseFunctionDeclarator() {
1771f4a2713aSLionel Sambuc 
1772f4a2713aSLionel Sambuc   // The '(' is already parsed.
1773f4a2713aSLionel Sambuc 
1774f4a2713aSLionel Sambuc   TPResult TPR = TryParseParameterDeclarationClause();
1775*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1776*0a6a1f1dSLionel Sambuc     TPR = TPResult::False;
1777f4a2713aSLionel Sambuc 
1778*0a6a1f1dSLionel Sambuc   if (TPR == TPResult::False || TPR == TPResult::Error)
1779f4a2713aSLionel Sambuc     return TPR;
1780f4a2713aSLionel Sambuc 
1781f4a2713aSLionel Sambuc   // Parse through the parens.
1782f4a2713aSLionel Sambuc   if (!SkipUntil(tok::r_paren, StopAtSemi))
1783*0a6a1f1dSLionel Sambuc     return TPResult::Error;
1784f4a2713aSLionel Sambuc 
1785f4a2713aSLionel Sambuc   // cv-qualifier-seq
1786f4a2713aSLionel Sambuc   while (Tok.is(tok::kw_const)    ||
1787f4a2713aSLionel Sambuc          Tok.is(tok::kw_volatile) ||
1788f4a2713aSLionel Sambuc          Tok.is(tok::kw_restrict)   )
1789f4a2713aSLionel Sambuc     ConsumeToken();
1790f4a2713aSLionel Sambuc 
1791f4a2713aSLionel Sambuc   // ref-qualifier[opt]
1792f4a2713aSLionel Sambuc   if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1793f4a2713aSLionel Sambuc     ConsumeToken();
1794f4a2713aSLionel Sambuc 
1795f4a2713aSLionel Sambuc   // exception-specification
1796f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_throw)) {
1797f4a2713aSLionel Sambuc     ConsumeToken();
1798f4a2713aSLionel Sambuc     if (Tok.isNot(tok::l_paren))
1799*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1800f4a2713aSLionel Sambuc 
1801f4a2713aSLionel Sambuc     // Parse through the parens after 'throw'.
1802f4a2713aSLionel Sambuc     ConsumeParen();
1803f4a2713aSLionel Sambuc     if (!SkipUntil(tok::r_paren, StopAtSemi))
1804*0a6a1f1dSLionel Sambuc       return TPResult::Error;
1805f4a2713aSLionel Sambuc   }
1806f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_noexcept)) {
1807f4a2713aSLionel Sambuc     ConsumeToken();
1808f4a2713aSLionel Sambuc     // Possibly an expression as well.
1809f4a2713aSLionel Sambuc     if (Tok.is(tok::l_paren)) {
1810f4a2713aSLionel Sambuc       // Find the matching rparen.
1811f4a2713aSLionel Sambuc       ConsumeParen();
1812f4a2713aSLionel Sambuc       if (!SkipUntil(tok::r_paren, StopAtSemi))
1813*0a6a1f1dSLionel Sambuc         return TPResult::Error;
1814f4a2713aSLionel Sambuc     }
1815f4a2713aSLionel Sambuc   }
1816f4a2713aSLionel Sambuc 
1817*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
1818f4a2713aSLionel Sambuc }
1819f4a2713aSLionel Sambuc 
1820f4a2713aSLionel Sambuc /// '[' constant-expression[opt] ']'
1821f4a2713aSLionel Sambuc ///
TryParseBracketDeclarator()1822f4a2713aSLionel Sambuc Parser::TPResult Parser::TryParseBracketDeclarator() {
1823f4a2713aSLionel Sambuc   ConsumeBracket();
1824f4a2713aSLionel Sambuc   if (!SkipUntil(tok::r_square, StopAtSemi))
1825*0a6a1f1dSLionel Sambuc     return TPResult::Error;
1826f4a2713aSLionel Sambuc 
1827*0a6a1f1dSLionel Sambuc   return TPResult::Ambiguous;
1828f4a2713aSLionel Sambuc }
1829