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