blob: 7415b8cd845e8549e5008fc7bae460eba1c5a3ac [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the tentative parsing portions of the Parser
11// interfaces, for ambiguity resolution.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Parse/ParseDiagnostic.h"
17#include "clang/Sema/ParsedTemplate.h"
18using namespace clang;
19
20/// isCXXDeclarationStatement - C++-specialized function that disambiguates
21/// between a declaration or an expression statement, when parsing function
22/// bodies. Returns true for declaration, false for expression.
23///
24/// declaration-statement:
25/// block-declaration
26///
27/// block-declaration:
28/// simple-declaration
29/// asm-definition
30/// namespace-alias-definition
31/// using-declaration
32/// using-directive
33/// [C++0x] static_assert-declaration
34///
35/// asm-definition:
36/// 'asm' '(' string-literal ')' ';'
37///
38/// namespace-alias-definition:
39/// 'namespace' identifier = qualified-namespace-specifier ';'
40///
41/// using-declaration:
42/// 'using' typename[opt] '::'[opt] nested-name-specifier
43/// unqualified-id ';'
44/// 'using' '::' unqualified-id ;
45///
46/// using-directive:
47/// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
48/// namespace-name ';'
49///
50bool Parser::isCXXDeclarationStatement() {
51 switch (Tok.getKind()) {
52 // asm-definition
53 case tok::kw_asm:
54 // namespace-alias-definition
55 case tok::kw_namespace:
56 // using-declaration
57 // using-directive
58 case tok::kw_using:
59 // static_assert-declaration
60 case tok::kw_static_assert:
61 case tok::kw__Static_assert:
62 return true;
63 // simple-declaration
64 default:
65 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
66 }
67}
68
69/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70/// between a simple-declaration or an expression-statement.
71/// If during the disambiguation process a parsing error is encountered,
72/// the function returns true to let the declaration parsing code handle it.
73/// Returns false if the statement is disambiguated as expression.
74///
75/// simple-declaration:
76/// decl-specifier-seq init-declarator-list[opt] ';'
77///
78/// (if AllowForRangeDecl specified)
79/// for ( for-range-declaration : for-range-initializer ) statement
80/// for-range-declaration:
81/// attribute-specifier-seqopt type-specifier-seq declarator
82bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
83 // C++ 6.8p1:
84 // There is an ambiguity in the grammar involving expression-statements and
85 // declarations: An expression-statement with a function-style explicit type
86 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87 // from a declaration where the first declarator starts with a '('. In those
88 // cases the statement is a declaration. [Note: To disambiguate, the whole
89 // statement might have to be examined to determine if it is an
90 // expression-statement or a declaration].
91
92 // C++ 6.8p3:
93 // The disambiguation is purely syntactic; that is, the meaning of the names
94 // occurring in such a statement, beyond whether they are type-names or not,
95 // is not generally used in or changed by the disambiguation. Class
96 // templates are instantiated as necessary to determine if a qualified name
97 // is a type-name. Disambiguation precedes parsing, and a statement
98 // disambiguated as a declaration may be an ill-formed declaration.
99
100 // We don't have to parse all of the decl-specifier-seq part. There's only
101 // an ambiguity if the first decl-specifier is
102 // simple-type-specifier/typename-specifier followed by a '(', which may
103 // indicate a function-style cast expression.
Richard Smithee390432014-05-16 01:56:53 +0000104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
Guy Benyei11169dd2012-12-18 14:30:41 +0000105 // a case.
106
107 bool InvalidAsDeclaration = false;
Richard Smithee390432014-05-16 01:56:53 +0000108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Guy Benyei11169dd2012-12-18 14:30:41 +0000109 &InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +0000110 if (TPR != TPResult::Ambiguous)
111 return TPR != TPResult::False; // Returns true for TPResult::True or
112 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000113
114 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
115 // and so gets some cases wrong. We can't carry on if we've already seen
116 // something which makes this statement invalid as a declaration in this case,
117 // since it can cause us to misparse valid code. Revisit this once
118 // TryParseInitDeclaratorList is fixed.
119 if (InvalidAsDeclaration)
120 return false;
121
122 // FIXME: Add statistics about the number of ambiguous statements encountered
123 // and how they were resolved (number of declarations+number of expressions).
124
125 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
126 // or an identifier which doesn't resolve as anything. We need tentative
127 // parsing...
Richard Smith91b73f22016-06-29 21:06:51 +0000128
129 {
130 RevertingTentativeParsingAction PA(*this);
131 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
132 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000133
134 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000135 if (TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 return true;
137
138 // Declarations take precedence over expressions.
Richard Smithee390432014-05-16 01:56:53 +0000139 if (TPR == TPResult::Ambiguous)
140 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000141
Richard Smithee390432014-05-16 01:56:53 +0000142 assert(TPR == TPResult::True || TPR == TPResult::False);
143 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000144}
145
Richard Smith1fff95c2013-09-12 23:28:08 +0000146/// Try to consume a token sequence that we've already identified as
147/// (potentially) starting a decl-specifier.
148Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
149 switch (Tok.getKind()) {
150 case tok::kw__Atomic:
151 if (NextToken().isNot(tok::l_paren)) {
152 ConsumeToken();
153 break;
154 }
155 // Fall through.
156 case tok::kw_typeof:
157 case tok::kw___attribute:
158 case tok::kw___underlying_type: {
159 ConsumeToken();
160 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000161 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000162 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000163 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000164 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000165 break;
166 }
167
168 case tok::kw_class:
169 case tok::kw_struct:
170 case tok::kw_union:
171 case tok::kw___interface:
172 case tok::kw_enum:
173 // elaborated-type-specifier:
174 // class-key attribute-specifier-seq[opt]
175 // nested-name-specifier[opt] identifier
176 // class-key nested-name-specifier[opt] template[opt] simple-template-id
177 // enum nested-name-specifier[opt] identifier
178 //
179 // FIXME: We don't support class-specifiers nor enum-specifiers here.
180 ConsumeToken();
181
182 // Skip attributes.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000183 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
184 tok::kw_alignas)) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000185 if (Tok.is(tok::l_square)) {
186 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000187 if (!SkipUntil(tok::r_square))
Richard Smithee390432014-05-16 01:56:53 +0000188 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000189 } else {
190 ConsumeToken();
191 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000192 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000193 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000195 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000196 }
197 }
198
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000199 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
200 tok::annot_template_id) &&
Nico Weberc29c4832014-12-28 23:24:02 +0000201 TryAnnotateCXXScopeToken())
Richard Smithee390432014-05-16 01:56:53 +0000202 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000203 if (Tok.is(tok::annot_cxxscope))
204 ConsumeToken();
205 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Richard Smithee390432014-05-16 01:56:53 +0000206 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000207 ConsumeToken();
208 break;
209
210 case tok::annot_cxxscope:
211 ConsumeToken();
212 // Fall through.
213 default:
214 ConsumeToken();
215
216 if (getLangOpts().ObjC1 && Tok.is(tok::less))
217 return TryParseProtocolQualifiers();
218 break;
219 }
220
Richard Smithee390432014-05-16 01:56:53 +0000221 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000222}
223
Guy Benyei11169dd2012-12-18 14:30:41 +0000224/// simple-declaration:
225/// decl-specifier-seq init-declarator-list[opt] ';'
226///
227/// (if AllowForRangeDecl specified)
228/// for ( for-range-declaration : for-range-initializer ) statement
229/// for-range-declaration:
230/// attribute-specifier-seqopt type-specifier-seq declarator
231///
232Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000233 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
234 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000235
236 // Two decl-specifiers in a row conclusively disambiguate this as being a
237 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
238 // overwhelmingly common case that the next token is a '('.
239 if (Tok.isNot(tok::l_paren)) {
240 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000241 if (TPR == TPResult::Ambiguous)
242 return TPResult::True;
243 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000244 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000245 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000246 }
247
248 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000249 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 return TPR;
251
252 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000253 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000254
Richard Smithee390432014-05-16 01:56:53 +0000255 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000256}
257
Richard Smith22c7c412013-03-20 03:35:02 +0000258/// Tentatively parse an init-declarator-list in order to disambiguate it from
259/// an expression.
260///
Guy Benyei11169dd2012-12-18 14:30:41 +0000261/// init-declarator-list:
262/// init-declarator
263/// init-declarator-list ',' init-declarator
264///
265/// init-declarator:
266/// declarator initializer[opt]
267/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
268///
Richard Smith22c7c412013-03-20 03:35:02 +0000269/// initializer:
270/// brace-or-equal-initializer
271/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000272///
Richard Smith22c7c412013-03-20 03:35:02 +0000273/// brace-or-equal-initializer:
274/// '=' initializer-clause
275/// [C++11] braced-init-list
276///
277/// initializer-clause:
278/// assignment-expression
279/// braced-init-list
280///
281/// braced-init-list:
282/// '{' initializer-list ','[opt] '}'
283/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000284///
285Parser::TPResult Parser::TryParseInitDeclaratorList() {
286 while (1) {
287 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000288 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000289 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000290 return TPR;
291
292 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000293 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000294 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000295
296 // initializer[opt]
297 if (Tok.is(tok::l_paren)) {
298 // Parse through the parens.
299 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000300 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000301 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000302 } else if (Tok.is(tok::l_brace)) {
303 // A left-brace here is sufficient to disambiguate the parse; an
304 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000305 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000306 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000307 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000308 // encountered; they just conclude that we have a declaration.
309 // EDG parses the initializer completely, which is the proper behavior
310 // for this case.
311 //
312 // At present, Clang follows MSVC and g++, since the parser does not have
313 // the ability to parse an expression fully without recording the
314 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000315 // FIXME: Handle this case correctly.
316 //
317 // Also allow 'in' after an Objective-C declaration as in:
318 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000319 // context of parsing for-init-statement of a foreach statement only. But,
320 // in any other context 'in' is invalid after a declaration and parser
321 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000322 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000323 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 }
325
Alp Toker97650562014-01-10 11:19:30 +0000326 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 }
329
Richard Smithee390432014-05-16 01:56:53 +0000330 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000331}
332
333/// isCXXConditionDeclaration - Disambiguates between a declaration or an
334/// expression for a condition of a if/switch/while/for statement.
335/// If during the disambiguation process a parsing error is encountered,
336/// the function returns true to let the declaration parsing code handle it.
337///
338/// condition:
339/// expression
340/// type-specifier-seq declarator '=' assignment-expression
341/// [C++11] type-specifier-seq declarator '=' initializer-clause
342/// [C++11] type-specifier-seq declarator braced-init-list
343/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
344/// '=' assignment-expression
345///
346bool Parser::isCXXConditionDeclaration() {
347 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000348 if (TPR != TPResult::Ambiguous)
349 return TPR != TPResult::False; // Returns true for TPResult::True or
350 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000351
352 // FIXME: Add statistics about the number of ambiguous statements encountered
353 // and how they were resolved (number of declarations+number of expressions).
354
355 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
356 // We need tentative parsing...
357
Richard Smith91b73f22016-06-29 21:06:51 +0000358 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000359
360 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000361 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000362 assert(Tok.is(tok::l_paren) && "Expected '('");
363
364 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000365 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000366
367 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000368 if (TPR == TPResult::Error)
369 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000370
Richard Smithee390432014-05-16 01:56:53 +0000371 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 // '='
373 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000374 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000375 TPR = TPResult::True;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000376 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
Richard Smithee390432014-05-16 01:56:53 +0000377 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 else
Richard Smithee390432014-05-16 01:56:53 +0000379 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000380 }
381
Richard Smithee390432014-05-16 01:56:53 +0000382 assert(TPR == TPResult::True || TPR == TPResult::False);
383 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000384}
385
386 /// \brief Determine whether the next set of tokens contains a type-id.
387 ///
388 /// The context parameter states what context we're parsing right
389 /// now, which affects how this routine copes with the token
390 /// following the type-id. If the context is TypeIdInParens, we have
391 /// already parsed the '(' and we will cease lookahead when we hit
392 /// the corresponding ')'. If the context is
393 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
394 /// before this template argument, and will cease lookahead when we
395 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
396 /// and false for an expression. If during the disambiguation
397 /// process a parsing error is encountered, the function returns
398 /// true to let the declaration parsing code handle it.
399 ///
400 /// type-id:
401 /// type-specifier-seq abstract-declarator[opt]
402 ///
403bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
404
405 isAmbiguous = false;
406
407 // C++ 8.2p2:
408 // The ambiguity arising from the similarity between a function-style cast and
409 // a type-id can occur in different contexts. The ambiguity appears as a
410 // choice between a function-style cast expression and a declaration of a
411 // type. The resolution is that any construct that could possibly be a type-id
412 // in its syntactic context shall be considered a type-id.
413
414 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000415 if (TPR != TPResult::Ambiguous)
416 return TPR != TPResult::False; // Returns true for TPResult::True or
417 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000418
419 // FIXME: Add statistics about the number of ambiguous statements encountered
420 // and how they were resolved (number of declarations+number of expressions).
421
422 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
423 // We need tentative parsing...
424
Richard Smith91b73f22016-06-29 21:06:51 +0000425 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000426
427 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000428 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 assert(Tok.is(tok::l_paren) && "Expected '('");
430
431 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000432 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000433
434 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000435 if (TPR == TPResult::Error)
436 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000437
Richard Smithee390432014-05-16 01:56:53 +0000438 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 // We are supposed to be inside parens, so if after the abstract declarator
440 // we encounter a ')' this is a type-id, otherwise it's an expression.
441 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000442 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000443 isAmbiguous = true;
444
445 // We are supposed to be inside a template argument, so if after
446 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
447 // ',', this is a type-id. Otherwise, it's an expression.
448 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000449 (Tok.isOneOf(tok::greater, tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000450 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000451 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000452 isAmbiguous = true;
453
454 } else
Richard Smithee390432014-05-16 01:56:53 +0000455 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000456 }
457
Richard Smithee390432014-05-16 01:56:53 +0000458 assert(TPR == TPResult::True || TPR == TPResult::False);
459 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000460}
461
462/// \brief Returns true if this is a C++11 attribute-specifier. Per
463/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
464/// always introduce an attribute. In Objective-C++11, this rule does not
465/// apply if either '[' begins a message-send.
466///
467/// If Disambiguate is true, we try harder to determine whether a '[[' starts
468/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
469///
470/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
471/// Obj-C message send or the start of an attribute. Otherwise, we assume it
472/// is not an Obj-C message send.
473///
474/// C++11 [dcl.attr.grammar]:
475///
476/// attribute-specifier:
477/// '[' '[' attribute-list ']' ']'
478/// alignment-specifier
479///
480/// attribute-list:
481/// attribute[opt]
482/// attribute-list ',' attribute[opt]
483/// attribute '...'
484/// attribute-list ',' attribute '...'
485///
486/// attribute:
487/// attribute-token attribute-argument-clause[opt]
488///
489/// attribute-token:
490/// identifier
491/// identifier '::' identifier
492///
493/// attribute-argument-clause:
494/// '(' balanced-token-seq ')'
495Parser::CXX11AttributeKind
496Parser::isCXX11AttributeSpecifier(bool Disambiguate,
497 bool OuterMightBeMessageSend) {
498 if (Tok.is(tok::kw_alignas))
499 return CAK_AttributeSpecifier;
500
501 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
502 return CAK_NotAttributeSpecifier;
503
504 // No tentative parsing if we don't need to look for ']]' or a lambda.
505 if (!Disambiguate && !getLangOpts().ObjC1)
506 return CAK_AttributeSpecifier;
507
Richard Smith91b73f22016-06-29 21:06:51 +0000508 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000509
510 // Opening brackets were checked for above.
511 ConsumeBracket();
512
513 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
514 if (!getLangOpts().ObjC1) {
515 ConsumeBracket();
516
Alexey Bataevee6507d2013-11-18 08:17:37 +0000517 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 IsAttribute &= Tok.is(tok::r_square);
519
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
521 }
522
523 // In Obj-C++11, we need to distinguish four situations:
524 // 1a) int x[[attr]]; C++11 attribute.
525 // 1b) [[attr]]; C++11 statement attribute.
526 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
527 // 3a) int x[[obj get]]; Message send in array size/index.
528 // 3b) [[Class alloc] init]; Message send in message send.
529 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
530 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
531
532 // If we have a lambda-introducer, then this is definitely not a message send.
533 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
534 // into the tentative attribute parse below.
535 LambdaIntroducer Intro;
536 if (!TryParseLambdaIntroducer(Intro)) {
537 // A lambda cannot end with ']]', and an attribute must.
538 bool IsAttribute = Tok.is(tok::r_square);
539
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 if (IsAttribute)
541 // Case 1: C++11 attribute.
542 return CAK_AttributeSpecifier;
543
544 if (OuterMightBeMessageSend)
545 // Case 4: Lambda in message send.
546 return CAK_NotAttributeSpecifier;
547
548 // Case 2: Lambda in array size / index.
549 return CAK_InvalidAttributeSpecifier;
550 }
551
552 ConsumeBracket();
553
554 // If we don't have a lambda-introducer, then we have an attribute or a
555 // message-send.
556 bool IsAttribute = true;
557 while (Tok.isNot(tok::r_square)) {
558 if (Tok.is(tok::comma)) {
559 // Case 1: Stray commas can only occur in attributes.
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 return CAK_AttributeSpecifier;
561 }
562
563 // Parse the attribute-token, if present.
564 // C++11 [dcl.attr.grammar]:
565 // If a keyword or an alternative token that satisfies the syntactic
566 // requirements of an identifier is contained in an attribute-token,
567 // it is considered an identifier.
568 SourceLocation Loc;
569 if (!TryParseCXX11AttributeIdentifier(Loc)) {
570 IsAttribute = false;
571 break;
572 }
573 if (Tok.is(tok::coloncolon)) {
574 ConsumeToken();
575 if (!TryParseCXX11AttributeIdentifier(Loc)) {
576 IsAttribute = false;
577 break;
578 }
579 }
580
581 // Parse the attribute-argument-clause, if present.
582 if (Tok.is(tok::l_paren)) {
583 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000584 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 IsAttribute = false;
586 break;
587 }
588 }
589
Alp Toker97650562014-01-10 11:19:30 +0000590 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000591
Alp Toker97650562014-01-10 11:19:30 +0000592 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000594 }
595
596 // An attribute must end ']]'.
597 if (IsAttribute) {
598 if (Tok.is(tok::r_square)) {
599 ConsumeBracket();
600 IsAttribute = Tok.is(tok::r_square);
601 } else {
602 IsAttribute = false;
603 }
604 }
605
Guy Benyei11169dd2012-12-18 14:30:41 +0000606 if (IsAttribute)
607 // Case 1: C++11 statement attribute.
608 return CAK_AttributeSpecifier;
609
610 // Case 3: Message send.
611 return CAK_NotAttributeSpecifier;
612}
613
Richard Smith1fff95c2013-09-12 23:28:08 +0000614Parser::TPResult Parser::TryParsePtrOperatorSeq() {
615 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000616 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000617 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000618 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000619
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000620 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000621 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
622 // ptr-operator
623 ConsumeToken();
Douglas Gregor261a89b2015-06-19 17:51:05 +0000624 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000625 tok::kw__Nonnull, tok::kw__Nullable,
626 tok::kw__Null_unspecified))
Richard Smith1fff95c2013-09-12 23:28:08 +0000627 ConsumeToken();
628 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000629 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000630 }
631 }
632}
633
634/// operator-function-id:
635/// 'operator' operator
636///
637/// operator: one of
638/// new delete new[] delete[] + - * / % ^ [...]
639///
640/// conversion-function-id:
641/// 'operator' conversion-type-id
642///
643/// conversion-type-id:
644/// type-specifier-seq conversion-declarator[opt]
645///
646/// conversion-declarator:
647/// ptr-operator conversion-declarator[opt]
648///
649/// literal-operator-id:
650/// 'operator' string-literal identifier
651/// 'operator' user-defined-string-literal
652Parser::TPResult Parser::TryParseOperatorId() {
653 assert(Tok.is(tok::kw_operator));
654 ConsumeToken();
655
656 // Maybe this is an operator-function-id.
657 switch (Tok.getKind()) {
658 case tok::kw_new: case tok::kw_delete:
659 ConsumeToken();
660 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
661 ConsumeBracket();
662 ConsumeBracket();
663 }
Richard Smithee390432014-05-16 01:56:53 +0000664 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000665
666#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
667 case tok::Token:
668#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
669#include "clang/Basic/OperatorKinds.def"
670 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000671 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000672
673 case tok::l_square:
674 if (NextToken().is(tok::r_square)) {
675 ConsumeBracket();
676 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000677 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000678 }
679 break;
680
681 case tok::l_paren:
682 if (NextToken().is(tok::r_paren)) {
683 ConsumeParen();
684 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000685 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000686 }
687 break;
688
689 default:
690 break;
691 }
692
693 // Maybe this is a literal-operator-id.
694 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
695 bool FoundUDSuffix = false;
696 do {
697 FoundUDSuffix |= Tok.hasUDSuffix();
698 ConsumeStringToken();
699 } while (isTokenStringLiteral());
700
701 if (!FoundUDSuffix) {
702 if (Tok.is(tok::identifier))
703 ConsumeToken();
704 else
Richard Smithee390432014-05-16 01:56:53 +0000705 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000706 }
Richard Smithee390432014-05-16 01:56:53 +0000707 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000708 }
709
710 // Maybe this is a conversion-function-id.
711 bool AnyDeclSpecifiers = false;
712 while (true) {
713 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000714 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000715 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000716 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000717 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000718 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000719 break;
720 }
Richard Smithee390432014-05-16 01:56:53 +0000721 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
722 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000723 AnyDeclSpecifiers = true;
724 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000725 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000726}
727
Guy Benyei11169dd2012-12-18 14:30:41 +0000728/// declarator:
729/// direct-declarator
730/// ptr-operator declarator
731///
732/// direct-declarator:
733/// declarator-id
734/// direct-declarator '(' parameter-declaration-clause ')'
735/// cv-qualifier-seq[opt] exception-specification[opt]
736/// direct-declarator '[' constant-expression[opt] ']'
737/// '(' declarator ')'
738/// [GNU] '(' attributes declarator ')'
739///
740/// abstract-declarator:
741/// ptr-operator abstract-declarator[opt]
742/// direct-abstract-declarator
743/// ...
744///
745/// direct-abstract-declarator:
746/// direct-abstract-declarator[opt]
747/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
748/// exception-specification[opt]
749/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
750/// '(' abstract-declarator ')'
751///
752/// ptr-operator:
753/// '*' cv-qualifier-seq[opt]
754/// '&'
755/// [C++0x] '&&' [TODO]
756/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
757///
758/// cv-qualifier-seq:
759/// cv-qualifier cv-qualifier-seq[opt]
760///
761/// cv-qualifier:
762/// 'const'
763/// 'volatile'
764///
765/// declarator-id:
766/// '...'[opt] id-expression
767///
768/// id-expression:
769/// unqualified-id
770/// qualified-id [TODO]
771///
772/// unqualified-id:
773/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000774/// operator-function-id
775/// conversion-function-id
776/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000777/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000778/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000779/// template-id [TODO]
780///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000781Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
782 bool mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000783 // declarator:
784 // direct-declarator
785 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000786 if (TryParsePtrOperatorSeq() == TPResult::Error)
787 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000788
789 // direct-declarator:
790 // direct-abstract-declarator:
791 if (Tok.is(tok::ellipsis))
792 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000793
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000794 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000795 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
796 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000797 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000798 // declarator-id
799 if (Tok.is(tok::annot_cxxscope))
800 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000801 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000802 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000803 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000804 if (TryParseOperatorId() == TPResult::Error)
805 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000806 } else
807 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 } else if (Tok.is(tok::l_paren)) {
809 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000810 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000811 (Tok.is(tok::r_paren) || // 'int()' is a function.
812 // 'int(...)' is a function.
813 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
814 isDeclarationSpecifier())) { // 'int(int)' is a function.
815 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
816 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000817 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000818 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 return TPR;
820 } else {
821 // '(' declarator ')'
822 // '(' attributes declarator ')'
823 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000824 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
825 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000826 tok::kw___vectorcall))
Richard Smithee390432014-05-16 01:56:53 +0000827 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000828 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000829 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 return TPR;
831 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000832 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000833 ConsumeParen();
834 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000835 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000836 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 }
838
839 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000840 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000841
842 // abstract-declarator: ...
843 if (Tok.is(tok::ellipsis))
844 ConsumeToken();
845
846 if (Tok.is(tok::l_paren)) {
847 // Check whether we have a function declarator or a possible ctor-style
848 // initializer that follows the declarator. Note that ctor-style
849 // initializers are not possible in contexts where abstract declarators
850 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000851 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 break;
853
854 // direct-declarator '(' parameter-declaration-clause ')'
855 // cv-qualifier-seq[opt] exception-specification[opt]
856 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000857 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +0000858 } else if (Tok.is(tok::l_square)) {
859 // direct-declarator '[' constant-expression[opt] ']'
860 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
861 TPR = TryParseBracketDeclarator();
862 } else {
863 break;
864 }
865
Richard Smithee390432014-05-16 01:56:53 +0000866 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000867 return TPR;
868 }
869
Richard Smithee390432014-05-16 01:56:53 +0000870 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000871}
872
873Parser::TPResult
874Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
875 switch (Kind) {
876 // Obviously starts an expression.
877 case tok::numeric_constant:
878 case tok::char_constant:
879 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000880 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 case tok::utf16_char_constant:
882 case tok::utf32_char_constant:
883 case tok::string_literal:
884 case tok::wide_string_literal:
885 case tok::utf8_string_literal:
886 case tok::utf16_string_literal:
887 case tok::utf32_string_literal:
888 case tok::l_square:
889 case tok::l_paren:
890 case tok::amp:
891 case tok::ampamp:
892 case tok::star:
893 case tok::plus:
894 case tok::plusplus:
895 case tok::minus:
896 case tok::minusminus:
897 case tok::tilde:
898 case tok::exclaim:
899 case tok::kw_sizeof:
900 case tok::kw___func__:
901 case tok::kw_const_cast:
902 case tok::kw_delete:
903 case tok::kw_dynamic_cast:
904 case tok::kw_false:
905 case tok::kw_new:
906 case tok::kw_operator:
907 case tok::kw_reinterpret_cast:
908 case tok::kw_static_cast:
909 case tok::kw_this:
910 case tok::kw_throw:
911 case tok::kw_true:
912 case tok::kw_typeid:
913 case tok::kw_alignof:
914 case tok::kw_noexcept:
915 case tok::kw_nullptr:
916 case tok::kw__Alignof:
917 case tok::kw___null:
918 case tok::kw___alignof:
919 case tok::kw___builtin_choose_expr:
920 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +0000921 case tok::kw___builtin_va_arg:
922 case tok::kw___imag:
923 case tok::kw___real:
924 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +0000925 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +0000926 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000927 case tok::kw_L__FUNCTION__:
928 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000929 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +0000930#define TYPE_TRAIT(N,Spelling,K) \
931 case tok::kw_##Spelling:
932#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +0000933 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000934
935 // Obviously starts a type-specifier-seq:
936 case tok::kw_char:
937 case tok::kw_const:
938 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000939 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +0000940 case tok::kw_enum:
941 case tok::kw_half:
942 case tok::kw_float:
943 case tok::kw_int:
944 case tok::kw_long:
945 case tok::kw___int64:
946 case tok::kw___int128:
947 case tok::kw_restrict:
948 case tok::kw_short:
949 case tok::kw_signed:
950 case tok::kw_struct:
951 case tok::kw_union:
952 case tok::kw_unsigned:
953 case tok::kw_void:
954 case tok::kw_volatile:
955 case tok::kw__Bool:
956 case tok::kw__Complex:
957 case tok::kw_class:
958 case tok::kw_typename:
959 case tok::kw_wchar_t:
960 case tok::kw_char16_t:
961 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 case tok::kw__Decimal32:
963 case tok::kw__Decimal64:
964 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +0000965 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +0000967 case tok::kw_thread_local:
968 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +0000969 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +0000970 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +0000971 case tok::kw___cdecl:
972 case tok::kw___stdcall:
973 case tok::kw___fastcall:
974 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +0000975 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +0000976 case tok::kw___unaligned:
977 case tok::kw___vector:
978 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +0000979 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +0000980 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +0000981#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +0000982#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +0000983 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +0000984 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000985
986 default:
987 break;
988 }
989
Richard Smithee390432014-05-16 01:56:53 +0000990 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000991}
992
993bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
994 return std::find(TentativelyDeclaredIdentifiers.begin(),
995 TentativelyDeclaredIdentifiers.end(), II)
996 != TentativelyDeclaredIdentifiers.end();
997}
998
Kaelyn Takata445b0652014-11-05 00:09:29 +0000999namespace {
1000class TentativeParseCCC : public CorrectionCandidateCallback {
1001public:
1002 TentativeParseCCC(const Token &Next) {
1003 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001004 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1005 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001006 }
1007
1008 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1009 // Reject any candidate that only resolves to instance members since they
1010 // aren't viable as standalone identifiers instead of member references.
1011 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1012 std::all_of(Candidate.begin(), Candidate.end(),
1013 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1014 return false;
1015
1016 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1017 }
1018};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001019}
Richard Smithee390432014-05-16 01:56:53 +00001020/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1021/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1022/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001023/// if a parsing error was found and reported.
1024///
1025/// If HasMissingTypename is provided, a name with a dependent scope specifier
1026/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1027/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1028/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001029/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001030/// such cases.
1031///
1032/// decl-specifier:
1033/// storage-class-specifier
1034/// type-specifier
1035/// function-specifier
1036/// 'friend'
1037/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001038/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001039/// [GNU] attributes declaration-specifiers[opt]
1040///
1041/// storage-class-specifier:
1042/// 'register'
1043/// 'static'
1044/// 'extern'
1045/// 'mutable'
1046/// 'auto'
1047/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001048/// [C++11] 'thread_local'
1049/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001050///
1051/// function-specifier:
1052/// 'inline'
1053/// 'virtual'
1054/// 'explicit'
1055///
1056/// typedef-name:
1057/// identifier
1058///
1059/// type-specifier:
1060/// simple-type-specifier
1061/// class-specifier
1062/// enum-specifier
1063/// elaborated-type-specifier
1064/// typename-specifier
1065/// cv-qualifier
1066///
1067/// simple-type-specifier:
1068/// '::'[opt] nested-name-specifier[opt] type-name
1069/// '::'[opt] nested-name-specifier 'template'
1070/// simple-template-id [TODO]
1071/// 'char'
1072/// 'wchar_t'
1073/// 'bool'
1074/// 'short'
1075/// 'int'
1076/// 'long'
1077/// 'signed'
1078/// 'unsigned'
1079/// 'float'
1080/// 'double'
1081/// 'void'
1082/// [GNU] typeof-specifier
1083/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001084/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001085/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001086/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001087/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001088///
1089/// type-name:
1090/// class-name
1091/// enum-name
1092/// typedef-name
1093///
1094/// elaborated-type-specifier:
1095/// class-key '::'[opt] nested-name-specifier[opt] identifier
1096/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1097/// simple-template-id
1098/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1099///
1100/// enum-name:
1101/// identifier
1102///
1103/// enum-specifier:
1104/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1105/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1106///
1107/// class-specifier:
1108/// class-head '{' member-specification[opt] '}'
1109///
1110/// class-head:
1111/// class-key identifier[opt] base-clause[opt]
1112/// class-key nested-name-specifier identifier base-clause[opt]
1113/// class-key nested-name-specifier[opt] simple-template-id
1114/// base-clause[opt]
1115///
1116/// class-key:
1117/// 'class'
1118/// 'struct'
1119/// 'union'
1120///
1121/// cv-qualifier:
1122/// 'const'
1123/// 'volatile'
1124/// [GNU] restrict
1125///
1126Parser::TPResult
1127Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1128 bool *HasMissingTypename) {
1129 switch (Tok.getKind()) {
1130 case tok::identifier: {
1131 // Check for need to substitute AltiVec __vector keyword
1132 // for "vector" identifier.
1133 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001134 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001135
1136 const Token &Next = NextToken();
1137 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1138 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001139 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001140
1141 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1142 // Determine whether this is a valid expression. If not, we will hit
1143 // a parse error one way or another. In that case, tell the caller that
1144 // this is ambiguous. Typo-correct to type and expression keywords and
1145 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001147 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001149 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001151 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 case ANK_TemplateName:
1153 // A bare type template-name which can't be a template template
1154 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001155 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001156 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001157 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001158 case ANK_Success:
1159 break;
1160 }
1161 assert(Tok.isNot(tok::identifier) &&
1162 "TryAnnotateName succeeded without producing an annotation");
1163 } else {
1164 // This might possibly be a type with a dependent scope specifier and
1165 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1166 // since it will annotate as a primary expression, and we want to use the
1167 // "missing 'typename'" logic.
1168 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001169 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001170 // If annotation failed, assume it's a non-type.
1171 // FIXME: If this happens due to an undeclared identifier, treat it as
1172 // ambiguous.
1173 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001174 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001175 }
1176
1177 // We annotated this token as something. Recurse to handle whatever we got.
1178 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1179 }
1180
1181 case tok::kw_typename: // typename T::type
1182 // Annotate typenames and C++ scope specifiers. If we get one, just
1183 // recurse to handle whatever we get.
1184 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001185 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001186 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1187
1188 case tok::coloncolon: { // ::foo::bar
1189 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001190 if (Next.isOneOf(tok::kw_new, // ::new
1191 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001192 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 }
1194 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001195 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001196 case tok::kw_decltype:
1197 // Annotate typenames and C++ scope specifiers. If we get one, just
1198 // recurse to handle whatever we get.
1199 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001200 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001201 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1202
1203 // decl-specifier:
1204 // storage-class-specifier
1205 // type-specifier
1206 // function-specifier
1207 // 'friend'
1208 // 'typedef'
1209 // 'constexpr'
Hubert Tong375f00a2015-06-30 12:14:52 +00001210 // 'concept'
Guy Benyei11169dd2012-12-18 14:30:41 +00001211 case tok::kw_friend:
1212 case tok::kw_typedef:
1213 case tok::kw_constexpr:
Hubert Tong375f00a2015-06-30 12:14:52 +00001214 case tok::kw_concept:
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 // storage-class-specifier
1216 case tok::kw_register:
1217 case tok::kw_static:
1218 case tok::kw_extern:
1219 case tok::kw_mutable:
1220 case tok::kw_auto:
1221 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001222 case tok::kw_thread_local:
1223 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001224 // function-specifier
1225 case tok::kw_inline:
1226 case tok::kw_virtual:
1227 case tok::kw_explicit:
1228
1229 // Modules
1230 case tok::kw___module_private__:
1231
1232 // Debugger support
1233 case tok::kw___unknown_anytype:
1234
1235 // type-specifier:
1236 // simple-type-specifier
1237 // class-specifier
1238 // enum-specifier
1239 // elaborated-type-specifier
1240 // typename-specifier
1241 // cv-qualifier
1242
1243 // class-specifier
1244 // elaborated-type-specifier
1245 case tok::kw_class:
1246 case tok::kw_struct:
1247 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001248 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 // enum-specifier
1250 case tok::kw_enum:
1251 // cv-qualifier
1252 case tok::kw_const:
1253 case tok::kw_volatile:
1254
1255 // GNU
1256 case tok::kw_restrict:
1257 case tok::kw__Complex:
1258 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001259 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001260 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001261
1262 // Microsoft
1263 case tok::kw___declspec:
1264 case tok::kw___cdecl:
1265 case tok::kw___stdcall:
1266 case tok::kw___fastcall:
1267 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001268 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001269 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001270 case tok::kw___sptr:
1271 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001272 case tok::kw___ptr64:
1273 case tok::kw___ptr32:
1274 case tok::kw___forceinline:
1275 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001276 case tok::kw__Nonnull:
1277 case tok::kw__Nullable:
1278 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001279 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001280 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001281
1282 // Borland
1283 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001284 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001285
1286 // AltiVec
1287 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001288 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001289
1290 case tok::annot_template_id: {
1291 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1292 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001293 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 CXXScopeSpec SS;
1295 AnnotateTemplateIdTokenAsType();
1296 assert(Tok.is(tok::annot_typename));
1297 goto case_typename;
1298 }
1299
1300 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1301 // We've already annotated a scope; try to annotate a type.
1302 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001303 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001304 if (!Tok.is(tok::annot_typename)) {
1305 // If the next token is an identifier or a type qualifier, then this
1306 // can't possibly be a valid expression either.
1307 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1308 CXXScopeSpec SS;
1309 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1310 Tok.getAnnotationRange(),
1311 SS);
1312 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1313 TentativeParsingAction PA(*this);
1314 ConsumeToken();
1315 ConsumeToken();
1316 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001317 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001318 if (!isIdentifier)
1319 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1320 HasMissingTypename);
1321 PA.Revert();
1322
1323 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001324 TPR == TPResult::True || TPR == TPResult::Error)
1325 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001326
1327 if (HasMissingTypename) {
1328 // We can't tell whether this is a missing 'typename' or a valid
1329 // expression.
1330 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001331 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332 }
Richard Smith91b73f22016-06-29 21:06:51 +00001333
1334 // FIXME: Fails to either revert or commit the tentative parse!
Guy Benyei11169dd2012-12-18 14:30:41 +00001335 } else {
1336 // Try to resolve the name. If it doesn't exist, assume it was
1337 // intended to name a type and keep disambiguating.
1338 switch (TryAnnotateName(false /* SS is not dependent */)) {
1339 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001340 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001341 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001342 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001343 case ANK_TemplateName:
1344 // A bare type template-name which can't be a template template
1345 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001346 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001348 return HasMissingTypename ? TPResult::Ambiguous
1349 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001350 case ANK_Success:
1351 // Annotated it, check again.
1352 assert(Tok.isNot(tok::annot_cxxscope) ||
1353 NextToken().isNot(tok::identifier));
1354 return isCXXDeclarationSpecifier(BracedCastResult,
1355 HasMissingTypename);
1356 }
1357 }
1358 }
Richard Smithee390432014-05-16 01:56:53 +00001359 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 }
1361 // If that succeeded, fallthrough into the generic simple-type-id case.
1362
1363 // The ambiguity resides in a simple-type-specifier/typename-specifier
1364 // followed by a '('. The '(' could either be the start of:
1365 //
1366 // direct-declarator:
1367 // '(' declarator ')'
1368 //
1369 // direct-abstract-declarator:
1370 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1371 // exception-specification[opt]
1372 // '(' abstract-declarator ')'
1373 //
1374 // or part of a function-style cast expression:
1375 //
1376 // simple-type-specifier '(' expression-list[opt] ')'
1377 //
1378
1379 // simple-type-specifier:
1380
1381 case tok::annot_typename:
1382 case_typename:
1383 // In Objective-C, we might have a protocol-qualified type.
1384 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001385 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001386 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 ConsumeToken(); // The type token
1388
1389 TPResult TPR = TryParseProtocolQualifiers();
1390 bool isFollowedByParen = Tok.is(tok::l_paren);
1391 bool isFollowedByBrace = Tok.is(tok::l_brace);
1392
Richard Smithee390432014-05-16 01:56:53 +00001393 if (TPR == TPResult::Error)
1394 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001395
1396 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001397 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001398
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001399 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 return BracedCastResult;
1401
Richard Smithee390432014-05-16 01:56:53 +00001402 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 }
1404
1405 case tok::kw_char:
1406 case tok::kw_wchar_t:
1407 case tok::kw_char16_t:
1408 case tok::kw_char32_t:
1409 case tok::kw_bool:
1410 case tok::kw_short:
1411 case tok::kw_int:
1412 case tok::kw_long:
1413 case tok::kw___int64:
1414 case tok::kw___int128:
1415 case tok::kw_signed:
1416 case tok::kw_unsigned:
1417 case tok::kw_half:
1418 case tok::kw_float:
1419 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001420 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 case tok::kw_void:
1422 case tok::annot_decltype:
1423 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001424 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001425
1426 // This is a function-style cast in all cases we disambiguate other than
1427 // one:
1428 // struct S {
1429 // enum E : int { a = 4 }; // enum
1430 // enum E : int { 4 }; // bit-field
1431 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001432 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001433 return BracedCastResult;
1434
1435 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001436 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001437
Richard Smithee390432014-05-16 01:56:53 +00001438 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001439
1440 // GNU typeof support.
1441 case tok::kw_typeof: {
1442 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001443 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001444
Richard Smith91b73f22016-06-29 21:06:51 +00001445 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001446
1447 TPResult TPR = TryParseTypeofSpecifier();
1448 bool isFollowedByParen = Tok.is(tok::l_paren);
1449 bool isFollowedByBrace = Tok.is(tok::l_brace);
1450
Richard Smithee390432014-05-16 01:56:53 +00001451 if (TPR == TPResult::Error)
1452 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001453
1454 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001455 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001456
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001457 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001458 return BracedCastResult;
1459
Richard Smithee390432014-05-16 01:56:53 +00001460 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 }
1462
1463 // C++0x type traits support
1464 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001465 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001466
1467 // C11 _Atomic
1468 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001469 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001470
1471 default:
Richard Smithee390432014-05-16 01:56:53 +00001472 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001473 }
1474}
1475
Richard Smith1fff95c2013-09-12 23:28:08 +00001476bool Parser::isCXXDeclarationSpecifierAType() {
1477 switch (Tok.getKind()) {
1478 // typename-specifier
1479 case tok::annot_decltype:
1480 case tok::annot_template_id:
1481 case tok::annot_typename:
1482 case tok::kw_typeof:
1483 case tok::kw___underlying_type:
1484 return true;
1485
1486 // elaborated-type-specifier
1487 case tok::kw_class:
1488 case tok::kw_struct:
1489 case tok::kw_union:
1490 case tok::kw___interface:
1491 case tok::kw_enum:
1492 return true;
1493
1494 // simple-type-specifier
1495 case tok::kw_char:
1496 case tok::kw_wchar_t:
1497 case tok::kw_char16_t:
1498 case tok::kw_char32_t:
1499 case tok::kw_bool:
1500 case tok::kw_short:
1501 case tok::kw_int:
1502 case tok::kw_long:
1503 case tok::kw___int64:
1504 case tok::kw___int128:
1505 case tok::kw_signed:
1506 case tok::kw_unsigned:
1507 case tok::kw_half:
1508 case tok::kw_float:
1509 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001510 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001511 case tok::kw_void:
1512 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001513 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001514 return true;
1515
1516 case tok::kw_auto:
1517 return getLangOpts().CPlusPlus11;
1518
1519 case tok::kw__Atomic:
1520 // "_Atomic foo"
1521 return NextToken().is(tok::l_paren);
1522
1523 default:
1524 return false;
1525 }
1526}
1527
Guy Benyei11169dd2012-12-18 14:30:41 +00001528/// [GNU] typeof-specifier:
1529/// 'typeof' '(' expressions ')'
1530/// 'typeof' '(' type-name ')'
1531///
1532Parser::TPResult Parser::TryParseTypeofSpecifier() {
1533 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1534 ConsumeToken();
1535
1536 assert(Tok.is(tok::l_paren) && "Expected '('");
1537 // Parse through the parens after 'typeof'.
1538 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001539 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001540 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001541
Richard Smithee390432014-05-16 01:56:53 +00001542 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001543}
1544
1545/// [ObjC] protocol-qualifiers:
1546//// '<' identifier-list '>'
1547Parser::TPResult Parser::TryParseProtocolQualifiers() {
1548 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1549 ConsumeToken();
1550 do {
1551 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001552 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001553 ConsumeToken();
1554
1555 if (Tok.is(tok::comma)) {
1556 ConsumeToken();
1557 continue;
1558 }
1559
1560 if (Tok.is(tok::greater)) {
1561 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001562 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 }
1564 } while (false);
1565
Richard Smithee390432014-05-16 01:56:53 +00001566 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001567}
1568
Guy Benyei11169dd2012-12-18 14:30:41 +00001569/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1570/// a constructor-style initializer, when parsing declaration statements.
1571/// Returns true for function declarator and false for constructor-style
1572/// initializer.
1573/// If during the disambiguation process a parsing error is encountered,
1574/// the function returns true to let the declaration parsing code handle it.
1575///
1576/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1577/// exception-specification[opt]
1578///
1579bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1580
1581 // C++ 8.2p1:
1582 // The ambiguity arising from the similarity between a function-style cast and
1583 // a declaration mentioned in 6.8 can also occur in the context of a
1584 // declaration. In that context, the choice is between a function declaration
1585 // with a redundant set of parentheses around a parameter name and an object
1586 // declaration with a function-style cast as the initializer. Just as for the
1587 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1588 // that could possibly be a declaration a declaration.
1589
Richard Smith91b73f22016-06-29 21:06:51 +00001590 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001591
1592 ConsumeParen();
1593 bool InvalidAsDeclaration = false;
1594 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001595 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001597 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 else {
1599 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001600 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1601 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1602 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1603 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001604 // The next token cannot appear after a constructor-style initializer,
1605 // and can appear next in a function definition. This must be a function
1606 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001607 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001608 else if (InvalidAsDeclaration)
1609 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001610 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 }
1612 }
1613
Richard Smithee390432014-05-16 01:56:53 +00001614 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 *IsAmbiguous = true;
1616
1617 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001618 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001619}
1620
1621/// parameter-declaration-clause:
1622/// parameter-declaration-list[opt] '...'[opt]
1623/// parameter-declaration-list ',' '...'
1624///
1625/// parameter-declaration-list:
1626/// parameter-declaration
1627/// parameter-declaration-list ',' parameter-declaration
1628///
1629/// parameter-declaration:
1630/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1631/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1632/// '=' assignment-expression
1633/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1634/// attributes[opt]
1635/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1636/// attributes[opt] '=' assignment-expression
1637///
1638Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001639Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1640 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001641
1642 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001643 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001644
1645 // parameter-declaration-list[opt] '...'[opt]
1646 // parameter-declaration-list ',' '...'
1647 //
1648 // parameter-declaration-list:
1649 // parameter-declaration
1650 // parameter-declaration-list ',' parameter-declaration
1651 //
1652 while (1) {
1653 // '...'[opt]
1654 if (Tok.is(tok::ellipsis)) {
1655 ConsumeToken();
1656 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001657 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 else
Richard Smithee390432014-05-16 01:56:53 +00001659 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 }
1661
1662 // An attribute-specifier-seq here is a sign of a function declarator.
1663 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1664 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001665 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001666
1667 ParsedAttributes attrs(AttrFactory);
1668 MaybeParseMicrosoftAttributes(attrs);
1669
1670 // decl-specifier-seq
1671 // A parameter-declaration's initializer must be preceded by an '=', so
1672 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001673 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001674 InvalidAsDeclaration);
1675
Richard Smithee390432014-05-16 01:56:53 +00001676 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001677 // Consume the decl-specifier-seq. We have to look past it, since a
1678 // type-id might appear here in a template argument.
1679 bool SeenType = false;
1680 do {
1681 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001682 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1683 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001684
1685 // If we see a parameter name, this can't be a template argument.
1686 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001687 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001688
Richard Smithee390432014-05-16 01:56:53 +00001689 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001690 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001691 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001692 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001693 } while (TPR != TPResult::False);
1694 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001695 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001696 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1697 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001698 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001699 return TPR;
1700
1701 // declarator
1702 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001703 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001704 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 return TPR;
1706
1707 // [GNU] attributes[opt]
1708 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001709 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001710
Richard Smith1fff95c2013-09-12 23:28:08 +00001711 // If we're disambiguating a template argument in a default argument in
1712 // a class definition versus a parameter declaration, an '=' here
1713 // disambiguates the parse one way or the other.
1714 // If this is a parameter, it must have a default argument because
1715 // (a) the previous parameter did, and
1716 // (b) this must be the first declaration of the function, so we can't
1717 // inherit any default arguments from elsewhere.
1718 // If we see an ')', then we've reached the end of a
1719 // parameter-declaration-clause, and the last param is missing its default
1720 // argument.
1721 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001722 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1723 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001724
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 if (Tok.is(tok::equal)) {
1726 // '=' assignment-expression
1727 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001728 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001729 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001730 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 }
1732
1733 if (Tok.is(tok::ellipsis)) {
1734 ConsumeToken();
1735 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001736 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 else
Richard Smithee390432014-05-16 01:56:53 +00001738 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 }
1740
Alp Toker97650562014-01-10 11:19:30 +00001741 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001742 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 }
1744
Richard Smithee390432014-05-16 01:56:53 +00001745 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001746}
1747
1748/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1749/// parsing as a function declarator.
1750/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001751/// return TPResult::Ambiguous, otherwise it will return either False() or
1752/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001753///
1754/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1755/// exception-specification[opt]
1756///
1757/// exception-specification:
1758/// 'throw' '(' type-id-list[opt] ')'
1759///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001760Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001761
1762 // The '(' is already parsed.
1763
1764 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001765 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1766 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001767
Justin Bognerd26f95b2015-02-23 22:36:28 +00001768 if (TPR == TPResult::False || TPR == TPResult::Error)
1769 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001770
1771 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001772 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001773 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001774
1775 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001776 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001777 ConsumeToken();
1778
1779 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001780 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 ConsumeToken();
1782
1783 // exception-specification
1784 if (Tok.is(tok::kw_throw)) {
1785 ConsumeToken();
1786 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001787 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001788
1789 // Parse through the parens after 'throw'.
1790 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001791 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001792 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 }
1794 if (Tok.is(tok::kw_noexcept)) {
1795 ConsumeToken();
1796 // Possibly an expression as well.
1797 if (Tok.is(tok::l_paren)) {
1798 // Find the matching rparen.
1799 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001800 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001801 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001802 }
1803 }
1804
Richard Smithee390432014-05-16 01:56:53 +00001805 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001806}
1807
1808/// '[' constant-expression[opt] ']'
1809///
1810Parser::TPResult Parser::TryParseBracketDeclarator() {
1811 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001812 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001813 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001814
Richard Smithee390432014-05-16 01:56:53 +00001815 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001816}