blob: 2b5e266104cb28517250c39ba2ff30bc23103813 [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] ';'
Richard Smithbdb84f32016-07-22 23:36:59 +000077/// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
78/// brace-or-equal-initializer ';' [C++17]
Guy Benyei11169dd2012-12-18 14:30:41 +000079///
80/// (if AllowForRangeDecl specified)
81/// for ( for-range-declaration : for-range-initializer ) statement
Richard Smithbdb84f32016-07-22 23:36:59 +000082///
Fangrui Song6907ce22018-07-30 19:24:48 +000083/// for-range-declaration:
Richard Smithbdb84f32016-07-22 23:36:59 +000084/// decl-specifier-seq declarator
85/// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
Fangrui Song6907ce22018-07-30 19:24:48 +000086///
Richard Smithbdb84f32016-07-22 23:36:59 +000087/// In any of the above cases there can be a preceding attribute-specifier-seq,
88/// but the caller is expected to handle that.
Guy Benyei11169dd2012-12-18 14:30:41 +000089bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
90 // C++ 6.8p1:
91 // There is an ambiguity in the grammar involving expression-statements and
92 // declarations: An expression-statement with a function-style explicit type
93 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
94 // from a declaration where the first declarator starts with a '('. In those
95 // cases the statement is a declaration. [Note: To disambiguate, the whole
96 // statement might have to be examined to determine if it is an
97 // expression-statement or a declaration].
98
99 // C++ 6.8p3:
100 // The disambiguation is purely syntactic; that is, the meaning of the names
101 // occurring in such a statement, beyond whether they are type-names or not,
102 // is not generally used in or changed by the disambiguation. Class
103 // templates are instantiated as necessary to determine if a qualified name
104 // is a type-name. Disambiguation precedes parsing, and a statement
105 // disambiguated as a declaration may be an ill-formed declaration.
106
107 // We don't have to parse all of the decl-specifier-seq part. There's only
108 // an ambiguity if the first decl-specifier is
109 // simple-type-specifier/typename-specifier followed by a '(', which may
110 // indicate a function-style cast expression.
Richard Smithee390432014-05-16 01:56:53 +0000111 // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
Guy Benyei11169dd2012-12-18 14:30:41 +0000112 // a case.
113
114 bool InvalidAsDeclaration = false;
Richard Smithee390432014-05-16 01:56:53 +0000115 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Guy Benyei11169dd2012-12-18 14:30:41 +0000116 &InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +0000117 if (TPR != TPResult::Ambiguous)
118 return TPR != TPResult::False; // Returns true for TPResult::True or
119 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000120
121 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
122 // and so gets some cases wrong. We can't carry on if we've already seen
123 // something which makes this statement invalid as a declaration in this case,
124 // since it can cause us to misparse valid code. Revisit this once
125 // TryParseInitDeclaratorList is fixed.
126 if (InvalidAsDeclaration)
127 return false;
128
129 // FIXME: Add statistics about the number of ambiguous statements encountered
130 // and how they were resolved (number of declarations+number of expressions).
131
132 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
133 // or an identifier which doesn't resolve as anything. We need tentative
134 // parsing...
Fangrui Song6907ce22018-07-30 19:24:48 +0000135
Richard Smith91b73f22016-06-29 21:06:51 +0000136 {
137 RevertingTentativeParsingAction PA(*this);
138 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
139 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000140
141 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000142 if (TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000143 return true;
144
145 // Declarations take precedence over expressions.
Richard Smithee390432014-05-16 01:56:53 +0000146 if (TPR == TPResult::Ambiguous)
147 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000148
Richard Smithee390432014-05-16 01:56:53 +0000149 assert(TPR == TPResult::True || TPR == TPResult::False);
150 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000151}
152
Richard Smith1fff95c2013-09-12 23:28:08 +0000153/// Try to consume a token sequence that we've already identified as
154/// (potentially) starting a decl-specifier.
155Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
156 switch (Tok.getKind()) {
157 case tok::kw__Atomic:
158 if (NextToken().isNot(tok::l_paren)) {
159 ConsumeToken();
160 break;
161 }
162 // Fall through.
163 case tok::kw_typeof:
164 case tok::kw___attribute:
165 case tok::kw___underlying_type: {
166 ConsumeToken();
167 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000168 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000169 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000170 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000171 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000172 break;
173 }
174
175 case tok::kw_class:
176 case tok::kw_struct:
177 case tok::kw_union:
178 case tok::kw___interface:
179 case tok::kw_enum:
180 // elaborated-type-specifier:
181 // class-key attribute-specifier-seq[opt]
182 // nested-name-specifier[opt] identifier
183 // class-key nested-name-specifier[opt] template[opt] simple-template-id
184 // enum nested-name-specifier[opt] identifier
185 //
186 // FIXME: We don't support class-specifiers nor enum-specifiers here.
187 ConsumeToken();
188
189 // Skip attributes.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000190 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
191 tok::kw_alignas)) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000192 if (Tok.is(tok::l_square)) {
193 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 if (!SkipUntil(tok::r_square))
Richard Smithee390432014-05-16 01:56:53 +0000195 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000196 } else {
197 ConsumeToken();
198 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000199 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000200 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000201 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000202 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000203 }
204 }
205
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000206 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
207 tok::annot_template_id) &&
Nico Weberc29c4832014-12-28 23:24:02 +0000208 TryAnnotateCXXScopeToken())
Richard Smithee390432014-05-16 01:56:53 +0000209 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000210 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +0000211 ConsumeAnnotationToken();
212 if (Tok.is(tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000213 ConsumeToken();
Richard Smithaf3b3252017-05-18 19:21:48 +0000214 else if (Tok.is(tok::annot_template_id))
215 ConsumeAnnotationToken();
216 else
Richard Smithee390432014-05-16 01:56:53 +0000217 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000218 break;
219
220 case tok::annot_cxxscope:
Richard Smithaf3b3252017-05-18 19:21:48 +0000221 ConsumeAnnotationToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000222 // Fall through.
223 default:
Richard Smithaf3b3252017-05-18 19:21:48 +0000224 ConsumeAnyToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000225
226 if (getLangOpts().ObjC1 && Tok.is(tok::less))
227 return TryParseProtocolQualifiers();
228 break;
229 }
230
Richard Smithee390432014-05-16 01:56:53 +0000231 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000232}
233
Guy Benyei11169dd2012-12-18 14:30:41 +0000234/// simple-declaration:
235/// decl-specifier-seq init-declarator-list[opt] ';'
236///
237/// (if AllowForRangeDecl specified)
238/// for ( for-range-declaration : for-range-initializer ) statement
Fangrui Song6907ce22018-07-30 19:24:48 +0000239/// for-range-declaration:
Guy Benyei11169dd2012-12-18 14:30:41 +0000240/// attribute-specifier-seqopt type-specifier-seq declarator
241///
242Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000243 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
244 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000245
246 // Two decl-specifiers in a row conclusively disambiguate this as being a
247 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
248 // overwhelmingly common case that the next token is a '('.
249 if (Tok.isNot(tok::l_paren)) {
250 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000251 if (TPR == TPResult::Ambiguous)
252 return TPResult::True;
253 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000254 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000255 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000256 }
257
258 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000259 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000260 return TPR;
261
262 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000263 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000264
Richard Smithee390432014-05-16 01:56:53 +0000265 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000266}
267
Richard Smith22c7c412013-03-20 03:35:02 +0000268/// Tentatively parse an init-declarator-list in order to disambiguate it from
269/// an expression.
270///
Guy Benyei11169dd2012-12-18 14:30:41 +0000271/// init-declarator-list:
272/// init-declarator
273/// init-declarator-list ',' init-declarator
274///
275/// init-declarator:
276/// declarator initializer[opt]
277/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
278///
Richard Smith22c7c412013-03-20 03:35:02 +0000279/// initializer:
280/// brace-or-equal-initializer
281/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000282///
Richard Smith22c7c412013-03-20 03:35:02 +0000283/// brace-or-equal-initializer:
284/// '=' initializer-clause
285/// [C++11] braced-init-list
286///
287/// initializer-clause:
288/// assignment-expression
289/// braced-init-list
290///
291/// braced-init-list:
292/// '{' initializer-list ','[opt] '}'
293/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000294///
295Parser::TPResult Parser::TryParseInitDeclaratorList() {
296 while (1) {
297 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000298 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000299 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000300 return TPR;
301
302 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000303 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000304 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000305
306 // initializer[opt]
307 if (Tok.is(tok::l_paren)) {
308 // Parse through the parens.
309 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000310 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000311 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000312 } else if (Tok.is(tok::l_brace)) {
313 // A left-brace here is sufficient to disambiguate the parse; an
314 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000315 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000316 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000317 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000318 // encountered; they just conclude that we have a declaration.
319 // EDG parses the initializer completely, which is the proper behavior
320 // for this case.
321 //
322 // At present, Clang follows MSVC and g++, since the parser does not have
323 // the ability to parse an expression fully without recording the
324 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000325 // FIXME: Handle this case correctly.
326 //
327 // Also allow 'in' after an Objective-C declaration as in:
328 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000329 // context of parsing for-init-statement of a foreach statement only. But,
330 // in any other context 'in' is invalid after a declaration and parser
331 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000332 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000333 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000334 }
335
Alp Toker97650562014-01-10 11:19:30 +0000336 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000337 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000338 }
339
Richard Smithee390432014-05-16 01:56:53 +0000340 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000341}
342
Richard Smithc7a05a92016-06-29 21:17:59 +0000343struct Parser::ConditionDeclarationOrInitStatementState {
344 Parser &P;
345 bool CanBeExpression = true;
346 bool CanBeCondition = true;
347 bool CanBeInitStatement;
Richard Smith8baa5002018-09-28 18:44:09 +0000348 bool CanBeForRangeDecl;
Richard Smithc7a05a92016-06-29 21:17:59 +0000349
Richard Smith8baa5002018-09-28 18:44:09 +0000350 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
351 bool CanBeForRangeDecl)
352 : P(P), CanBeInitStatement(CanBeInitStatement),
353 CanBeForRangeDecl(CanBeForRangeDecl) {}
354
355 bool resolved() {
356 return CanBeExpression + CanBeCondition + CanBeInitStatement +
357 CanBeForRangeDecl < 2;
358 }
Richard Smithc7a05a92016-06-29 21:17:59 +0000359
360 void markNotExpression() {
361 CanBeExpression = false;
362
Richard Smith8baa5002018-09-28 18:44:09 +0000363 if (!resolved()) {
Richard Smithc7a05a92016-06-29 21:17:59 +0000364 // FIXME: Unify the parsing codepaths for condition variables and
365 // simple-declarations so that we don't need to eagerly figure out which
366 // kind we have here. (Just parse init-declarators until we reach a
367 // semicolon or right paren.)
368 RevertingTentativeParsingAction PA(P);
Richard Smith8baa5002018-09-28 18:44:09 +0000369 if (CanBeForRangeDecl) {
370 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
371 // The final case is a for range declaration, the rest are not.
372 while (true) {
373 unsigned QuestionColonDepth = 0;
374 P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
375 StopBeforeMatch);
376 if (P.Tok.is(tok::question))
377 ++QuestionColonDepth;
378 else if (P.Tok.is(tok::colon)) {
379 if (QuestionColonDepth)
380 --QuestionColonDepth;
381 else {
382 CanBeCondition = CanBeInitStatement = false;
383 return;
384 }
385 } else {
386 CanBeForRangeDecl = false;
387 break;
388 }
389 P.ConsumeToken();
390 }
391 } else {
392 // Just skip until we hit a ')' or ';'.
393 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
394 }
Richard Smithc7a05a92016-06-29 21:17:59 +0000395 if (P.Tok.isNot(tok::r_paren))
Richard Smith8baa5002018-09-28 18:44:09 +0000396 CanBeCondition = CanBeForRangeDecl = false;
Richard Smithc7a05a92016-06-29 21:17:59 +0000397 if (P.Tok.isNot(tok::semi))
398 CanBeInitStatement = false;
399 }
400 }
401
402 bool markNotCondition() {
403 CanBeCondition = false;
Richard Smith8baa5002018-09-28 18:44:09 +0000404 return resolved();
405 }
406
407 bool markNotForRangeDecl() {
408 CanBeForRangeDecl = false;
409 return resolved();
Richard Smithc7a05a92016-06-29 21:17:59 +0000410 }
411
412 bool update(TPResult IsDecl) {
413 switch (IsDecl) {
414 case TPResult::True:
415 markNotExpression();
Richard Smith8baa5002018-09-28 18:44:09 +0000416 assert(resolved() && "can't continue after tentative parsing bails out");
417 break;
Richard Smithc7a05a92016-06-29 21:17:59 +0000418 case TPResult::False:
Richard Smith8baa5002018-09-28 18:44:09 +0000419 CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
420 break;
Richard Smithc7a05a92016-06-29 21:17:59 +0000421 case TPResult::Ambiguous:
Richard Smith8baa5002018-09-28 18:44:09 +0000422 break;
Richard Smithc7a05a92016-06-29 21:17:59 +0000423 case TPResult::Error:
Richard Smith8baa5002018-09-28 18:44:09 +0000424 CanBeExpression = CanBeCondition = CanBeInitStatement =
425 CanBeForRangeDecl = false;
426 break;
Richard Smithc7a05a92016-06-29 21:17:59 +0000427 }
Richard Smith8baa5002018-09-28 18:44:09 +0000428 return resolved();
Richard Smithc7a05a92016-06-29 21:17:59 +0000429 }
430
431 ConditionOrInitStatement result() const {
Richard Smith8baa5002018-09-28 18:44:09 +0000432 assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
433 CanBeForRangeDecl < 2 &&
Richard Smithc7a05a92016-06-29 21:17:59 +0000434 "result called but not yet resolved");
435 if (CanBeExpression)
436 return ConditionOrInitStatement::Expression;
437 if (CanBeCondition)
438 return ConditionOrInitStatement::ConditionDecl;
439 if (CanBeInitStatement)
440 return ConditionOrInitStatement::InitStmtDecl;
Richard Smith8baa5002018-09-28 18:44:09 +0000441 if (CanBeForRangeDecl)
442 return ConditionOrInitStatement::ForRangeDecl;
Richard Smithc7a05a92016-06-29 21:17:59 +0000443 return ConditionOrInitStatement::Error;
444 }
445};
446
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000447/// Disambiguates between a declaration in a condition, a
Richard Smithc7a05a92016-06-29 21:17:59 +0000448/// simple-declaration in an init-statement, and an expression for
449/// a condition of a if/switch statement.
Guy Benyei11169dd2012-12-18 14:30:41 +0000450///
451/// condition:
452/// expression
453/// type-specifier-seq declarator '=' assignment-expression
454/// [C++11] type-specifier-seq declarator '=' initializer-clause
455/// [C++11] type-specifier-seq declarator braced-init-list
456/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
457/// '=' assignment-expression
Richard Smithc7a05a92016-06-29 21:17:59 +0000458/// simple-declaration:
459/// decl-specifier-seq init-declarator-list[opt] ';'
Guy Benyei11169dd2012-12-18 14:30:41 +0000460///
Richard Smithc7a05a92016-06-29 21:17:59 +0000461/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
462/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
463/// 'int(x);' (a simple-declaration in an init-statement).
464Parser::ConditionOrInitStatement
Richard Smith8baa5002018-09-28 18:44:09 +0000465Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
466 bool CanBeForRangeDecl) {
467 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
468 CanBeForRangeDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000469
Richard Smithc7a05a92016-06-29 21:17:59 +0000470 if (State.update(isCXXDeclarationSpecifier()))
471 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000472
Richard Smithc7a05a92016-06-29 21:17:59 +0000473 // It might be a declaration; we need tentative parsing.
Richard Smith91b73f22016-06-29 21:06:51 +0000474 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000475
Richard Smithc7a05a92016-06-29 21:17:59 +0000476 // FIXME: A tag definition unambiguously tells us this is an init-statement.
477 if (State.update(TryConsumeDeclarationSpecifier()))
478 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 assert(Tok.is(tok::l_paren) && "Expected '('");
480
Richard Smithc7a05a92016-06-29 21:17:59 +0000481 while (true) {
482 // Consume a declarator.
483 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
484 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000485
Richard Smithc7a05a92016-06-29 21:17:59 +0000486 // Attributes, asm label, or an initializer imply this is not an expression.
487 // FIXME: Disambiguate properly after an = instead of assuming that it's a
488 // valid declaration.
489 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
490 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
491 State.markNotExpression();
492 return State.result();
493 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000494
Richard Smith8baa5002018-09-28 18:44:09 +0000495 // A colon here identifies a for-range declaration.
496 if (State.CanBeForRangeDecl && Tok.is(tok::colon))
497 return ConditionOrInitStatement::ForRangeDecl;
498
Richard Smithc7a05a92016-06-29 21:17:59 +0000499 // At this point, it can't be a condition any more, because a condition
500 // must have a brace-or-equal-initializer.
501 if (State.markNotCondition())
502 return State.result();
503
Richard Smith8baa5002018-09-28 18:44:09 +0000504 // Likewise, it can't be a for-range declaration any more.
505 if (State.markNotForRangeDecl())
506 return State.result();
507
Richard Smithc7a05a92016-06-29 21:17:59 +0000508 // A parenthesized initializer could be part of an expression or a
509 // simple-declaration.
510 if (Tok.is(tok::l_paren)) {
511 ConsumeParen();
512 SkipUntil(tok::r_paren, StopAtSemi);
513 }
514
515 if (!TryConsumeToken(tok::comma))
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 }
518
Richard Smithc7a05a92016-06-29 21:17:59 +0000519 // We reached the end. If it can now be some kind of decl, then it is.
520 if (State.CanBeCondition && Tok.is(tok::r_paren))
521 return ConditionOrInitStatement::ConditionDecl;
522 else if (State.CanBeInitStatement && Tok.is(tok::semi))
523 return ConditionOrInitStatement::InitStmtDecl;
524 else
525 return ConditionOrInitStatement::Expression;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526}
527
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000528 /// Determine whether the next set of tokens contains a type-id.
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 ///
530 /// The context parameter states what context we're parsing right
531 /// now, which affects how this routine copes with the token
532 /// following the type-id. If the context is TypeIdInParens, we have
533 /// already parsed the '(' and we will cease lookahead when we hit
534 /// the corresponding ')'. If the context is
535 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
536 /// before this template argument, and will cease lookahead when we
Hubert Tong605eaca2017-05-20 00:21:55 +0000537 /// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
538 /// preceding such. Returns true for a type-id and false for an expression.
539 /// If during the disambiguation process a parsing error is encountered,
540 /// the function returns true to let the declaration parsing code handle it.
Guy Benyei11169dd2012-12-18 14:30:41 +0000541 ///
542 /// type-id:
543 /// type-specifier-seq abstract-declarator[opt]
544 ///
545bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
546
547 isAmbiguous = false;
548
549 // C++ 8.2p2:
550 // The ambiguity arising from the similarity between a function-style cast and
551 // a type-id can occur in different contexts. The ambiguity appears as a
552 // choice between a function-style cast expression and a declaration of a
553 // type. The resolution is that any construct that could possibly be a type-id
554 // in its syntactic context shall be considered a type-id.
555
556 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000557 if (TPR != TPResult::Ambiguous)
558 return TPR != TPResult::False; // Returns true for TPResult::True or
559 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000560
561 // FIXME: Add statistics about the number of ambiguous statements encountered
562 // and how they were resolved (number of declarations+number of expressions).
563
564 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
565 // We need tentative parsing...
566
Richard Smith91b73f22016-06-29 21:06:51 +0000567 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568
569 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000570 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 assert(Tok.is(tok::l_paren) && "Expected '('");
572
573 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000574 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000575
576 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000577 if (TPR == TPResult::Error)
578 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000579
Richard Smithee390432014-05-16 01:56:53 +0000580 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000581 // We are supposed to be inside parens, so if after the abstract declarator
582 // we encounter a ')' this is a type-id, otherwise it's an expression.
583 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000584 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 isAmbiguous = true;
586
587 // We are supposed to be inside a template argument, so if after
588 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
Hubert Tong605eaca2017-05-20 00:21:55 +0000589 // ','; or, in C++0x, an ellipsis immediately preceding such, this
590 // is a type-id. Otherwise, it's an expression.
Guy Benyei11169dd2012-12-18 14:30:41 +0000591 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000592 (Tok.isOneOf(tok::greater, tok::comma) ||
Hubert Tong605eaca2017-05-20 00:21:55 +0000593 (getLangOpts().CPlusPlus11 &&
594 (Tok.is(tok::greatergreater) ||
595 (Tok.is(tok::ellipsis) &&
596 NextToken().isOneOf(tok::greater, tok::greatergreater,
597 tok::comma)))))) {
Richard Smithee390432014-05-16 01:56:53 +0000598 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000599 isAmbiguous = true;
600
601 } else
Richard Smithee390432014-05-16 01:56:53 +0000602 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000603 }
604
Richard Smithee390432014-05-16 01:56:53 +0000605 assert(TPR == TPResult::True || TPR == TPResult::False);
606 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000607}
608
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000609/// Returns true if this is a C++11 attribute-specifier. Per
Guy Benyei11169dd2012-12-18 14:30:41 +0000610/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
611/// always introduce an attribute. In Objective-C++11, this rule does not
612/// apply if either '[' begins a message-send.
613///
614/// If Disambiguate is true, we try harder to determine whether a '[[' starts
615/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
616///
617/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
618/// Obj-C message send or the start of an attribute. Otherwise, we assume it
619/// is not an Obj-C message send.
620///
621/// C++11 [dcl.attr.grammar]:
622///
623/// attribute-specifier:
624/// '[' '[' attribute-list ']' ']'
625/// alignment-specifier
626///
627/// attribute-list:
628/// attribute[opt]
629/// attribute-list ',' attribute[opt]
630/// attribute '...'
631/// attribute-list ',' attribute '...'
632///
633/// attribute:
634/// attribute-token attribute-argument-clause[opt]
635///
636/// attribute-token:
637/// identifier
638/// identifier '::' identifier
639///
640/// attribute-argument-clause:
641/// '(' balanced-token-seq ')'
642Parser::CXX11AttributeKind
643Parser::isCXX11AttributeSpecifier(bool Disambiguate,
644 bool OuterMightBeMessageSend) {
645 if (Tok.is(tok::kw_alignas))
646 return CAK_AttributeSpecifier;
647
648 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
649 return CAK_NotAttributeSpecifier;
650
651 // No tentative parsing if we don't need to look for ']]' or a lambda.
652 if (!Disambiguate && !getLangOpts().ObjC1)
653 return CAK_AttributeSpecifier;
654
Richard Smith91b73f22016-06-29 21:06:51 +0000655 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000656
657 // Opening brackets were checked for above.
658 ConsumeBracket();
659
660 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
661 if (!getLangOpts().ObjC1) {
662 ConsumeBracket();
663
Alexey Bataevee6507d2013-11-18 08:17:37 +0000664 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000665 IsAttribute &= Tok.is(tok::r_square);
666
Guy Benyei11169dd2012-12-18 14:30:41 +0000667 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
668 }
669
670 // In Obj-C++11, we need to distinguish four situations:
671 // 1a) int x[[attr]]; C++11 attribute.
672 // 1b) [[attr]]; C++11 statement attribute.
673 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
674 // 3a) int x[[obj get]]; Message send in array size/index.
675 // 3b) [[Class alloc] init]; Message send in message send.
676 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
677 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
678
679 // If we have a lambda-introducer, then this is definitely not a message send.
680 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
681 // into the tentative attribute parse below.
682 LambdaIntroducer Intro;
683 if (!TryParseLambdaIntroducer(Intro)) {
684 // A lambda cannot end with ']]', and an attribute must.
685 bool IsAttribute = Tok.is(tok::r_square);
686
Guy Benyei11169dd2012-12-18 14:30:41 +0000687 if (IsAttribute)
688 // Case 1: C++11 attribute.
689 return CAK_AttributeSpecifier;
690
691 if (OuterMightBeMessageSend)
692 // Case 4: Lambda in message send.
693 return CAK_NotAttributeSpecifier;
694
695 // Case 2: Lambda in array size / index.
696 return CAK_InvalidAttributeSpecifier;
697 }
698
699 ConsumeBracket();
700
701 // If we don't have a lambda-introducer, then we have an attribute or a
702 // message-send.
703 bool IsAttribute = true;
704 while (Tok.isNot(tok::r_square)) {
705 if (Tok.is(tok::comma)) {
706 // Case 1: Stray commas can only occur in attributes.
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 return CAK_AttributeSpecifier;
708 }
709
710 // Parse the attribute-token, if present.
711 // C++11 [dcl.attr.grammar]:
712 // If a keyword or an alternative token that satisfies the syntactic
713 // requirements of an identifier is contained in an attribute-token,
714 // it is considered an identifier.
715 SourceLocation Loc;
716 if (!TryParseCXX11AttributeIdentifier(Loc)) {
717 IsAttribute = false;
718 break;
719 }
720 if (Tok.is(tok::coloncolon)) {
721 ConsumeToken();
722 if (!TryParseCXX11AttributeIdentifier(Loc)) {
723 IsAttribute = false;
724 break;
725 }
726 }
727
728 // Parse the attribute-argument-clause, if present.
729 if (Tok.is(tok::l_paren)) {
730 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000731 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 IsAttribute = false;
733 break;
734 }
735 }
736
Alp Toker97650562014-01-10 11:19:30 +0000737 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000738
Alp Toker97650562014-01-10 11:19:30 +0000739 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000740 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 }
742
743 // An attribute must end ']]'.
744 if (IsAttribute) {
745 if (Tok.is(tok::r_square)) {
746 ConsumeBracket();
747 IsAttribute = Tok.is(tok::r_square);
748 } else {
749 IsAttribute = false;
750 }
751 }
752
Guy Benyei11169dd2012-12-18 14:30:41 +0000753 if (IsAttribute)
754 // Case 1: C++11 statement attribute.
755 return CAK_AttributeSpecifier;
756
757 // Case 3: Message send.
758 return CAK_NotAttributeSpecifier;
759}
760
Richard Smith1fff95c2013-09-12 23:28:08 +0000761Parser::TPResult Parser::TryParsePtrOperatorSeq() {
762 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000763 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000764 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000765 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000766
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000767 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000768 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
769 // ptr-operator
Richard Smithaf3b3252017-05-18 19:21:48 +0000770 ConsumeAnyToken();
Douglas Gregor261a89b2015-06-19 17:51:05 +0000771 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000772 tok::kw__Nonnull, tok::kw__Nullable,
773 tok::kw__Null_unspecified))
Richard Smith1fff95c2013-09-12 23:28:08 +0000774 ConsumeToken();
775 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000776 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000777 }
778 }
779}
780
781/// operator-function-id:
782/// 'operator' operator
783///
784/// operator: one of
785/// new delete new[] delete[] + - * / % ^ [...]
786///
787/// conversion-function-id:
788/// 'operator' conversion-type-id
789///
790/// conversion-type-id:
791/// type-specifier-seq conversion-declarator[opt]
792///
793/// conversion-declarator:
794/// ptr-operator conversion-declarator[opt]
795///
796/// literal-operator-id:
797/// 'operator' string-literal identifier
798/// 'operator' user-defined-string-literal
799Parser::TPResult Parser::TryParseOperatorId() {
800 assert(Tok.is(tok::kw_operator));
801 ConsumeToken();
802
803 // Maybe this is an operator-function-id.
804 switch (Tok.getKind()) {
805 case tok::kw_new: case tok::kw_delete:
806 ConsumeToken();
807 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
808 ConsumeBracket();
809 ConsumeBracket();
810 }
Richard Smithee390432014-05-16 01:56:53 +0000811 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000812
813#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
814 case tok::Token:
815#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
816#include "clang/Basic/OperatorKinds.def"
817 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000818 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000819
820 case tok::l_square:
821 if (NextToken().is(tok::r_square)) {
822 ConsumeBracket();
823 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000824 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000825 }
826 break;
827
828 case tok::l_paren:
829 if (NextToken().is(tok::r_paren)) {
830 ConsumeParen();
831 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000832 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000833 }
834 break;
835
836 default:
837 break;
838 }
839
840 // Maybe this is a literal-operator-id.
841 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
842 bool FoundUDSuffix = false;
843 do {
844 FoundUDSuffix |= Tok.hasUDSuffix();
845 ConsumeStringToken();
846 } while (isTokenStringLiteral());
847
848 if (!FoundUDSuffix) {
849 if (Tok.is(tok::identifier))
850 ConsumeToken();
851 else
Richard Smithee390432014-05-16 01:56:53 +0000852 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000853 }
Richard Smithee390432014-05-16 01:56:53 +0000854 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000855 }
856
857 // Maybe this is a conversion-function-id.
858 bool AnyDeclSpecifiers = false;
859 while (true) {
860 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000861 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000862 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000863 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000864 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000865 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000866 break;
867 }
Richard Smithee390432014-05-16 01:56:53 +0000868 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
869 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000870 AnyDeclSpecifiers = true;
871 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000872 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000873}
874
Guy Benyei11169dd2012-12-18 14:30:41 +0000875/// declarator:
876/// direct-declarator
877/// ptr-operator declarator
878///
879/// direct-declarator:
880/// declarator-id
881/// direct-declarator '(' parameter-declaration-clause ')'
882/// cv-qualifier-seq[opt] exception-specification[opt]
883/// direct-declarator '[' constant-expression[opt] ']'
884/// '(' declarator ')'
885/// [GNU] '(' attributes declarator ')'
886///
887/// abstract-declarator:
888/// ptr-operator abstract-declarator[opt]
889/// direct-abstract-declarator
Guy Benyei11169dd2012-12-18 14:30:41 +0000890///
891/// direct-abstract-declarator:
892/// direct-abstract-declarator[opt]
Hubert Tong605eaca2017-05-20 00:21:55 +0000893/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
Guy Benyei11169dd2012-12-18 14:30:41 +0000894/// exception-specification[opt]
895/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
896/// '(' abstract-declarator ')'
Hubert Tong605eaca2017-05-20 00:21:55 +0000897/// [C++0x] ...
Guy Benyei11169dd2012-12-18 14:30:41 +0000898///
899/// ptr-operator:
900/// '*' cv-qualifier-seq[opt]
901/// '&'
902/// [C++0x] '&&' [TODO]
903/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
904///
905/// cv-qualifier-seq:
906/// cv-qualifier cv-qualifier-seq[opt]
907///
908/// cv-qualifier:
909/// 'const'
910/// 'volatile'
911///
912/// declarator-id:
913/// '...'[opt] id-expression
914///
915/// id-expression:
916/// unqualified-id
917/// qualified-id [TODO]
918///
919/// unqualified-id:
920/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000921/// operator-function-id
922/// conversion-function-id
923/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000924/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000925/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000926/// template-id [TODO]
927///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000928Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
Richard Smithe303e352018-02-02 22:24:54 +0000929 bool mayHaveIdentifier,
930 bool mayHaveDirectInit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 // declarator:
932 // direct-declarator
933 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000934 if (TryParsePtrOperatorSeq() == TPResult::Error)
935 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000936
937 // direct-declarator:
938 // direct-abstract-declarator:
939 if (Tok.is(tok::ellipsis))
940 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000941
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000942 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000943 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
944 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000945 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000946 // declarator-id
947 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +0000948 ConsumeAnnotationToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000949 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000950 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000951 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000952 if (TryParseOperatorId() == TPResult::Error)
953 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000954 } else
955 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 } else if (Tok.is(tok::l_paren)) {
957 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000958 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 (Tok.is(tok::r_paren) || // 'int()' is a function.
960 // 'int(...)' is a function.
961 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
962 isDeclarationSpecifier())) { // 'int(int)' is a function.
963 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
964 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000965 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000966 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000967 return TPR;
968 } else {
969 // '(' declarator ')'
970 // '(' attributes declarator ')'
971 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000972 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
973 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
Erich Keane757d3172016-11-02 18:29:35 +0000974 tok::kw___regcall, tok::kw___vectorcall))
Richard Smithee390432014-05-16 01:56:53 +0000975 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000976 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000977 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 return TPR;
979 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000980 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000981 ConsumeParen();
982 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000983 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000984 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000985 }
986
Richard Smithe303e352018-02-02 22:24:54 +0000987 if (mayHaveDirectInit)
988 return TPResult::Ambiguous;
989
Guy Benyei11169dd2012-12-18 14:30:41 +0000990 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000991 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000992
Guy Benyei11169dd2012-12-18 14:30:41 +0000993 if (Tok.is(tok::l_paren)) {
994 // Check whether we have a function declarator or a possible ctor-style
995 // initializer that follows the declarator. Note that ctor-style
996 // initializers are not possible in contexts where abstract declarators
997 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000998 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000999 break;
1000
1001 // direct-declarator '(' parameter-declaration-clause ')'
1002 // cv-qualifier-seq[opt] exception-specification[opt]
1003 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +00001004 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +00001005 } else if (Tok.is(tok::l_square)) {
1006 // direct-declarator '[' constant-expression[opt] ']'
1007 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
1008 TPR = TryParseBracketDeclarator();
1009 } else {
1010 break;
1011 }
1012
Richard Smithee390432014-05-16 01:56:53 +00001013 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001014 return TPR;
1015 }
1016
Richard Smithee390432014-05-16 01:56:53 +00001017 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001018}
1019
Fangrui Song6907ce22018-07-30 19:24:48 +00001020Parser::TPResult
Guy Benyei11169dd2012-12-18 14:30:41 +00001021Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
1022 switch (Kind) {
1023 // Obviously starts an expression.
1024 case tok::numeric_constant:
1025 case tok::char_constant:
1026 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +00001027 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +00001028 case tok::utf16_char_constant:
1029 case tok::utf32_char_constant:
1030 case tok::string_literal:
1031 case tok::wide_string_literal:
1032 case tok::utf8_string_literal:
1033 case tok::utf16_string_literal:
1034 case tok::utf32_string_literal:
1035 case tok::l_square:
1036 case tok::l_paren:
1037 case tok::amp:
1038 case tok::ampamp:
1039 case tok::star:
1040 case tok::plus:
1041 case tok::plusplus:
1042 case tok::minus:
1043 case tok::minusminus:
1044 case tok::tilde:
1045 case tok::exclaim:
1046 case tok::kw_sizeof:
1047 case tok::kw___func__:
1048 case tok::kw_const_cast:
1049 case tok::kw_delete:
1050 case tok::kw_dynamic_cast:
1051 case tok::kw_false:
1052 case tok::kw_new:
1053 case tok::kw_operator:
1054 case tok::kw_reinterpret_cast:
1055 case tok::kw_static_cast:
1056 case tok::kw_this:
1057 case tok::kw_throw:
1058 case tok::kw_true:
1059 case tok::kw_typeid:
1060 case tok::kw_alignof:
1061 case tok::kw_noexcept:
1062 case tok::kw_nullptr:
1063 case tok::kw__Alignof:
1064 case tok::kw___null:
1065 case tok::kw___alignof:
1066 case tok::kw___builtin_choose_expr:
1067 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +00001068 case tok::kw___builtin_va_arg:
1069 case tok::kw___imag:
1070 case tok::kw___real:
1071 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +00001072 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +00001073 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001074 case tok::kw_L__FUNCTION__:
Reid Kleckner4a83f0a2018-07-26 23:18:44 +00001075 case tok::kw_L__FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001076 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001077 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +00001078#define TYPE_TRAIT(N,Spelling,K) \
1079 case tok::kw_##Spelling:
1080#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +00001081 return TPResult::True;
Fangrui Song6907ce22018-07-30 19:24:48 +00001082
Guy Benyei11169dd2012-12-18 14:30:41 +00001083 // Obviously starts a type-specifier-seq:
1084 case tok::kw_char:
1085 case tok::kw_const:
1086 case tok::kw_double:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001087 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001088 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001089 case tok::kw_enum:
1090 case tok::kw_half:
1091 case tok::kw_float:
1092 case tok::kw_int:
1093 case tok::kw_long:
1094 case tok::kw___int64:
1095 case tok::kw___int128:
1096 case tok::kw_restrict:
1097 case tok::kw_short:
1098 case tok::kw_signed:
1099 case tok::kw_struct:
1100 case tok::kw_union:
1101 case tok::kw_unsigned:
1102 case tok::kw_void:
1103 case tok::kw_volatile:
1104 case tok::kw__Bool:
1105 case tok::kw__Complex:
1106 case tok::kw_class:
1107 case tok::kw_typename:
1108 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00001109 case tok::kw_char8_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001110 case tok::kw_char16_t:
1111 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 case tok::kw__Decimal32:
1113 case tok::kw__Decimal64:
1114 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001115 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001117 case tok::kw_thread_local:
1118 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001119 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +00001120 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +00001121 case tok::kw___cdecl:
1122 case tok::kw___stdcall:
1123 case tok::kw___fastcall:
1124 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00001125 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001126 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 case tok::kw___unaligned:
1128 case tok::kw___vector:
1129 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +00001130 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +00001131 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +00001132#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00001133#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00001134 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +00001135 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001136
1137 default:
1138 break;
1139 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001140
Richard Smithee390432014-05-16 01:56:53 +00001141 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001142}
1143
1144bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1145 return std::find(TentativelyDeclaredIdentifiers.begin(),
1146 TentativelyDeclaredIdentifiers.end(), II)
1147 != TentativelyDeclaredIdentifiers.end();
1148}
1149
Kaelyn Takata445b0652014-11-05 00:09:29 +00001150namespace {
1151class TentativeParseCCC : public CorrectionCandidateCallback {
1152public:
1153 TentativeParseCCC(const Token &Next) {
1154 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001155 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1156 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001157 }
1158
1159 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1160 // Reject any candidate that only resolves to instance members since they
1161 // aren't viable as standalone identifiers instead of member references.
1162 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1163 std::all_of(Candidate.begin(), Candidate.end(),
1164 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1165 return false;
1166
1167 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1168 }
1169};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001170}
Richard Smithee390432014-05-16 01:56:53 +00001171/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1172/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1173/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001174/// if a parsing error was found and reported.
1175///
1176/// If HasMissingTypename is provided, a name with a dependent scope specifier
1177/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1178/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1179/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001180/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001181/// such cases.
1182///
1183/// decl-specifier:
1184/// storage-class-specifier
1185/// type-specifier
1186/// function-specifier
1187/// 'friend'
1188/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001189/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001190/// [GNU] attributes declaration-specifiers[opt]
1191///
1192/// storage-class-specifier:
1193/// 'register'
1194/// 'static'
1195/// 'extern'
1196/// 'mutable'
1197/// 'auto'
1198/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001199/// [C++11] 'thread_local'
1200/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001201///
1202/// function-specifier:
1203/// 'inline'
1204/// 'virtual'
1205/// 'explicit'
1206///
1207/// typedef-name:
1208/// identifier
1209///
1210/// type-specifier:
1211/// simple-type-specifier
1212/// class-specifier
1213/// enum-specifier
1214/// elaborated-type-specifier
1215/// typename-specifier
1216/// cv-qualifier
1217///
1218/// simple-type-specifier:
1219/// '::'[opt] nested-name-specifier[opt] type-name
1220/// '::'[opt] nested-name-specifier 'template'
1221/// simple-template-id [TODO]
1222/// 'char'
1223/// 'wchar_t'
1224/// 'bool'
1225/// 'short'
1226/// 'int'
1227/// 'long'
1228/// 'signed'
1229/// 'unsigned'
1230/// 'float'
1231/// 'double'
1232/// 'void'
1233/// [GNU] typeof-specifier
1234/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001235/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001236/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001237/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001238/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001239///
1240/// type-name:
1241/// class-name
1242/// enum-name
1243/// typedef-name
1244///
1245/// elaborated-type-specifier:
1246/// class-key '::'[opt] nested-name-specifier[opt] identifier
1247/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1248/// simple-template-id
1249/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1250///
1251/// enum-name:
1252/// identifier
1253///
1254/// enum-specifier:
1255/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1256/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1257///
1258/// class-specifier:
1259/// class-head '{' member-specification[opt] '}'
1260///
1261/// class-head:
1262/// class-key identifier[opt] base-clause[opt]
1263/// class-key nested-name-specifier identifier base-clause[opt]
1264/// class-key nested-name-specifier[opt] simple-template-id
1265/// base-clause[opt]
1266///
1267/// class-key:
1268/// 'class'
1269/// 'struct'
1270/// 'union'
1271///
1272/// cv-qualifier:
1273/// 'const'
1274/// 'volatile'
1275/// [GNU] restrict
1276///
1277Parser::TPResult
1278Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1279 bool *HasMissingTypename) {
1280 switch (Tok.getKind()) {
1281 case tok::identifier: {
1282 // Check for need to substitute AltiVec __vector keyword
1283 // for "vector" identifier.
1284 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001285 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001286
1287 const Token &Next = NextToken();
1288 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1289 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001290 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001291
1292 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1293 // Determine whether this is a valid expression. If not, we will hit
1294 // a parse error one way or another. In that case, tell the caller that
1295 // this is ambiguous. Typo-correct to type and expression keywords and
1296 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001297 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001298 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001299 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001300 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001301 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001302 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001303 case ANK_TemplateName:
Richard Smith77a9c602018-02-28 03:02:23 +00001304 // In C++17, this could be a type template for class template argument
1305 // deduction. Try to form a type annotation for it. If we're in a
1306 // template template argument, we'll undo this when checking the
1307 // validity of the argument.
1308 if (getLangOpts().CPlusPlus17) {
1309 if (TryAnnotateTypeOrScopeToken())
1310 return TPResult::Error;
1311 if (Tok.isNot(tok::identifier))
1312 break;
1313 }
1314
Guy Benyei11169dd2012-12-18 14:30:41 +00001315 // A bare type template-name which can't be a template template
1316 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001317 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001318 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001319 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 case ANK_Success:
1321 break;
1322 }
1323 assert(Tok.isNot(tok::identifier) &&
1324 "TryAnnotateName succeeded without producing an annotation");
1325 } else {
1326 // This might possibly be a type with a dependent scope specifier and
1327 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1328 // since it will annotate as a primary expression, and we want to use the
1329 // "missing 'typename'" logic.
1330 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001331 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332 // If annotation failed, assume it's a non-type.
1333 // FIXME: If this happens due to an undeclared identifier, treat it as
1334 // ambiguous.
1335 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001336 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001337 }
1338
1339 // We annotated this token as something. Recurse to handle whatever we got.
1340 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1341 }
1342
1343 case tok::kw_typename: // typename T::type
1344 // Annotate typenames and C++ scope specifiers. If we get one, just
1345 // recurse to handle whatever we get.
1346 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001347 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1349
1350 case tok::coloncolon: { // ::foo::bar
1351 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001352 if (Next.isOneOf(tok::kw_new, // ::new
1353 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001354 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 }
1356 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001357 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001358 case tok::kw_decltype:
1359 // Annotate typenames and C++ scope specifiers. If we get one, just
1360 // recurse to handle whatever we get.
1361 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001362 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1364
1365 // decl-specifier:
1366 // storage-class-specifier
1367 // type-specifier
1368 // function-specifier
1369 // 'friend'
1370 // 'typedef'
1371 // 'constexpr'
1372 case tok::kw_friend:
1373 case tok::kw_typedef:
1374 case tok::kw_constexpr:
1375 // storage-class-specifier
1376 case tok::kw_register:
1377 case tok::kw_static:
1378 case tok::kw_extern:
1379 case tok::kw_mutable:
1380 case tok::kw_auto:
1381 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001382 case tok::kw_thread_local:
1383 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001384 // function-specifier
1385 case tok::kw_inline:
1386 case tok::kw_virtual:
1387 case tok::kw_explicit:
1388
1389 // Modules
1390 case tok::kw___module_private__:
1391
1392 // Debugger support
1393 case tok::kw___unknown_anytype:
Fangrui Song6907ce22018-07-30 19:24:48 +00001394
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 // type-specifier:
1396 // simple-type-specifier
1397 // class-specifier
1398 // enum-specifier
1399 // elaborated-type-specifier
1400 // typename-specifier
1401 // cv-qualifier
1402
1403 // class-specifier
1404 // elaborated-type-specifier
1405 case tok::kw_class:
1406 case tok::kw_struct:
1407 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001408 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 // enum-specifier
1410 case tok::kw_enum:
1411 // cv-qualifier
1412 case tok::kw_const:
1413 case tok::kw_volatile:
Anastasia Stulova7f785bb2018-06-22 16:20:21 +00001414 case tok::kw___private:
1415 case tok::kw___local:
1416 case tok::kw___global:
1417 case tok::kw___constant:
1418 case tok::kw___generic:
Guy Benyei11169dd2012-12-18 14:30:41 +00001419
1420 // GNU
1421 case tok::kw_restrict:
1422 case tok::kw__Complex:
1423 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001424 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001425 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001426
1427 // Microsoft
1428 case tok::kw___declspec:
1429 case tok::kw___cdecl:
1430 case tok::kw___stdcall:
1431 case tok::kw___fastcall:
1432 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00001433 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001434 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001435 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001436 case tok::kw___sptr:
1437 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 case tok::kw___ptr64:
1439 case tok::kw___ptr32:
1440 case tok::kw___forceinline:
1441 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001442 case tok::kw__Nonnull:
1443 case tok::kw__Nullable:
1444 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001445 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001446 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001447
1448 // Borland
1449 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001450 return TPResult::True;
Fangrui Song6907ce22018-07-30 19:24:48 +00001451
Guy Benyei11169dd2012-12-18 14:30:41 +00001452 // AltiVec
1453 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001454 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001455
1456 case tok::annot_template_id: {
1457 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1458 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001459 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001460 CXXScopeSpec SS;
1461 AnnotateTemplateIdTokenAsType();
1462 assert(Tok.is(tok::annot_typename));
1463 goto case_typename;
1464 }
1465
1466 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1467 // We've already annotated a scope; try to annotate a type.
1468 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001469 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 if (!Tok.is(tok::annot_typename)) {
1471 // If the next token is an identifier or a type qualifier, then this
1472 // can't possibly be a valid expression either.
1473 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1474 CXXScopeSpec SS;
1475 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1476 Tok.getAnnotationRange(),
1477 SS);
1478 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
Richard Smith4556ebe2016-06-29 21:12:37 +00001479 RevertingTentativeParsingAction PA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001480 ConsumeAnnotationToken();
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 ConsumeToken();
1482 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001483 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 if (!isIdentifier)
1485 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1486 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001487
1488 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001489 TPR == TPResult::True || TPR == TPResult::Error)
1490 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001491
1492 if (HasMissingTypename) {
1493 // We can't tell whether this is a missing 'typename' or a valid
1494 // expression.
1495 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001496 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001497 }
1498 } else {
1499 // Try to resolve the name. If it doesn't exist, assume it was
1500 // intended to name a type and keep disambiguating.
1501 switch (TryAnnotateName(false /* SS is not dependent */)) {
1502 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001503 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001504 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001505 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 case ANK_TemplateName:
Richard Smith77a9c602018-02-28 03:02:23 +00001507 // In C++17, this could be a type template for class template
1508 // argument deduction.
1509 if (getLangOpts().CPlusPlus17) {
1510 if (TryAnnotateTypeOrScopeToken())
1511 return TPResult::Error;
1512 if (Tok.isNot(tok::identifier))
1513 break;
1514 }
1515
Guy Benyei11169dd2012-12-18 14:30:41 +00001516 // A bare type template-name which can't be a template template
1517 // argument is an error, and was probably intended to be a type.
Richard Smith77a9c602018-02-28 03:02:23 +00001518 // In C++17, this could be class template argument deduction.
1519 return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
1520 ? TPResult::True
1521 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001522 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001523 return HasMissingTypename ? TPResult::Ambiguous
1524 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001525 case ANK_Success:
Richard Smith77a9c602018-02-28 03:02:23 +00001526 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 }
Richard Smith77a9c602018-02-28 03:02:23 +00001528
1529 // Annotated it, check again.
1530 assert(Tok.isNot(tok::annot_cxxscope) ||
1531 NextToken().isNot(tok::identifier));
1532 return isCXXDeclarationSpecifier(BracedCastResult,
1533 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 }
1535 }
Richard Smithee390432014-05-16 01:56:53 +00001536 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001537 }
1538 // If that succeeded, fallthrough into the generic simple-type-id case.
Galina Kistanova53ab4242017-06-01 21:29:45 +00001539 LLVM_FALLTHROUGH;
Guy Benyei11169dd2012-12-18 14:30:41 +00001540
1541 // The ambiguity resides in a simple-type-specifier/typename-specifier
1542 // followed by a '('. The '(' could either be the start of:
1543 //
1544 // direct-declarator:
1545 // '(' declarator ')'
1546 //
1547 // direct-abstract-declarator:
1548 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1549 // exception-specification[opt]
1550 // '(' abstract-declarator ')'
1551 //
1552 // or part of a function-style cast expression:
1553 //
1554 // simple-type-specifier '(' expression-list[opt] ')'
1555 //
1556
1557 // simple-type-specifier:
1558
1559 case tok::annot_typename:
1560 case_typename:
1561 // In Objective-C, we might have a protocol-qualified type.
1562 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001563 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001564 RevertingTentativeParsingAction PA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001565 ConsumeAnyToken(); // The type token
Fangrui Song6907ce22018-07-30 19:24:48 +00001566
Guy Benyei11169dd2012-12-18 14:30:41 +00001567 TPResult TPR = TryParseProtocolQualifiers();
1568 bool isFollowedByParen = Tok.is(tok::l_paren);
1569 bool isFollowedByBrace = Tok.is(tok::l_brace);
Fangrui Song6907ce22018-07-30 19:24:48 +00001570
Richard Smithee390432014-05-16 01:56:53 +00001571 if (TPR == TPResult::Error)
1572 return TPResult::Error;
Fangrui Song6907ce22018-07-30 19:24:48 +00001573
Guy Benyei11169dd2012-12-18 14:30:41 +00001574 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001575 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001576
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001577 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001578 return BracedCastResult;
Fangrui Song6907ce22018-07-30 19:24:48 +00001579
Richard Smithee390432014-05-16 01:56:53 +00001580 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001581 }
Galina Kistanova53ab4242017-06-01 21:29:45 +00001582 LLVM_FALLTHROUGH;
Fangrui Song6907ce22018-07-30 19:24:48 +00001583
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 case tok::kw_char:
1585 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00001586 case tok::kw_char8_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001587 case tok::kw_char16_t:
1588 case tok::kw_char32_t:
1589 case tok::kw_bool:
1590 case tok::kw_short:
1591 case tok::kw_int:
1592 case tok::kw_long:
1593 case tok::kw___int64:
1594 case tok::kw___int128:
1595 case tok::kw_signed:
1596 case tok::kw_unsigned:
1597 case tok::kw_half:
1598 case tok::kw_float:
1599 case tok::kw_double:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001600 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001601 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001602 case tok::kw_void:
1603 case tok::annot_decltype:
1604 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001605 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001606
1607 // This is a function-style cast in all cases we disambiguate other than
1608 // one:
1609 // struct S {
1610 // enum E : int { a = 4 }; // enum
1611 // enum E : int { 4 }; // bit-field
1612 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001613 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001614 return BracedCastResult;
1615
1616 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001617 return TPResult::False;
Fangrui Song6907ce22018-07-30 19:24:48 +00001618
Richard Smithee390432014-05-16 01:56:53 +00001619 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001620
1621 // GNU typeof support.
1622 case tok::kw_typeof: {
1623 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001624 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001625
Richard Smith91b73f22016-06-29 21:06:51 +00001626 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001627
1628 TPResult TPR = TryParseTypeofSpecifier();
1629 bool isFollowedByParen = Tok.is(tok::l_paren);
1630 bool isFollowedByBrace = Tok.is(tok::l_brace);
1631
Richard Smithee390432014-05-16 01:56:53 +00001632 if (TPR == TPResult::Error)
1633 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001634
1635 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001636 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001637
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001638 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001639 return BracedCastResult;
1640
Richard Smithee390432014-05-16 01:56:53 +00001641 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 }
1643
1644 // C++0x type traits support
1645 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001646 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001647
1648 // C11 _Atomic
1649 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001650 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001651
1652 default:
Richard Smithee390432014-05-16 01:56:53 +00001653 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001654 }
1655}
1656
Richard Smith1fff95c2013-09-12 23:28:08 +00001657bool Parser::isCXXDeclarationSpecifierAType() {
1658 switch (Tok.getKind()) {
1659 // typename-specifier
1660 case tok::annot_decltype:
1661 case tok::annot_template_id:
1662 case tok::annot_typename:
1663 case tok::kw_typeof:
1664 case tok::kw___underlying_type:
1665 return true;
1666
1667 // elaborated-type-specifier
1668 case tok::kw_class:
1669 case tok::kw_struct:
1670 case tok::kw_union:
1671 case tok::kw___interface:
1672 case tok::kw_enum:
1673 return true;
1674
1675 // simple-type-specifier
1676 case tok::kw_char:
1677 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00001678 case tok::kw_char8_t:
Richard Smith1fff95c2013-09-12 23:28:08 +00001679 case tok::kw_char16_t:
1680 case tok::kw_char32_t:
1681 case tok::kw_bool:
1682 case tok::kw_short:
1683 case tok::kw_int:
1684 case tok::kw_long:
1685 case tok::kw___int64:
1686 case tok::kw___int128:
1687 case tok::kw_signed:
1688 case tok::kw_unsigned:
1689 case tok::kw_half:
1690 case tok::kw_float:
1691 case tok::kw_double:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001692 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001693 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001694 case tok::kw_void:
1695 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001696 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001697 return true;
1698
1699 case tok::kw_auto:
1700 return getLangOpts().CPlusPlus11;
1701
1702 case tok::kw__Atomic:
1703 // "_Atomic foo"
1704 return NextToken().is(tok::l_paren);
1705
1706 default:
1707 return false;
1708 }
1709}
1710
Guy Benyei11169dd2012-12-18 14:30:41 +00001711/// [GNU] typeof-specifier:
1712/// 'typeof' '(' expressions ')'
1713/// 'typeof' '(' type-name ')'
1714///
1715Parser::TPResult Parser::TryParseTypeofSpecifier() {
1716 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1717 ConsumeToken();
1718
1719 assert(Tok.is(tok::l_paren) && "Expected '('");
1720 // Parse through the parens after 'typeof'.
1721 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001722 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001723 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001724
Richard Smithee390432014-05-16 01:56:53 +00001725 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001726}
1727
1728/// [ObjC] protocol-qualifiers:
1729//// '<' identifier-list '>'
1730Parser::TPResult Parser::TryParseProtocolQualifiers() {
1731 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1732 ConsumeToken();
1733 do {
1734 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001735 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 ConsumeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00001737
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 if (Tok.is(tok::comma)) {
1739 ConsumeToken();
1740 continue;
1741 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001742
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 if (Tok.is(tok::greater)) {
1744 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001745 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001746 }
1747 } while (false);
Fangrui Song6907ce22018-07-30 19:24:48 +00001748
Richard Smithee390432014-05-16 01:56:53 +00001749 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001750}
1751
Guy Benyei11169dd2012-12-18 14:30:41 +00001752/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1753/// a constructor-style initializer, when parsing declaration statements.
1754/// Returns true for function declarator and false for constructor-style
1755/// initializer.
1756/// If during the disambiguation process a parsing error is encountered,
1757/// the function returns true to let the declaration parsing code handle it.
1758///
1759/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1760/// exception-specification[opt]
1761///
1762bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1763
1764 // C++ 8.2p1:
1765 // The ambiguity arising from the similarity between a function-style cast and
1766 // a declaration mentioned in 6.8 can also occur in the context of a
1767 // declaration. In that context, the choice is between a function declaration
1768 // with a redundant set of parentheses around a parameter name and an object
1769 // declaration with a function-style cast as the initializer. Just as for the
1770 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1771 // that could possibly be a declaration a declaration.
1772
Richard Smith91b73f22016-06-29 21:06:51 +00001773 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001774
1775 ConsumeParen();
1776 bool InvalidAsDeclaration = false;
1777 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001778 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001780 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 else {
1782 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001783 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1784 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1785 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1786 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001787 // The next token cannot appear after a constructor-style initializer,
1788 // and can appear next in a function definition. This must be a function
1789 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001790 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 else if (InvalidAsDeclaration)
1792 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001793 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001794 }
1795 }
1796
Richard Smithee390432014-05-16 01:56:53 +00001797 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 *IsAmbiguous = true;
1799
1800 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001801 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001802}
1803
1804/// parameter-declaration-clause:
1805/// parameter-declaration-list[opt] '...'[opt]
1806/// parameter-declaration-list ',' '...'
1807///
1808/// parameter-declaration-list:
1809/// parameter-declaration
1810/// parameter-declaration-list ',' parameter-declaration
1811///
1812/// parameter-declaration:
1813/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1814/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1815/// '=' assignment-expression
1816/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1817/// attributes[opt]
1818/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1819/// attributes[opt] '=' assignment-expression
1820///
1821Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001822Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1823 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001824
1825 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001826 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001827
1828 // parameter-declaration-list[opt] '...'[opt]
1829 // parameter-declaration-list ',' '...'
1830 //
1831 // parameter-declaration-list:
1832 // parameter-declaration
1833 // parameter-declaration-list ',' parameter-declaration
1834 //
1835 while (1) {
1836 // '...'[opt]
1837 if (Tok.is(tok::ellipsis)) {
1838 ConsumeToken();
1839 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001840 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 else
Richard Smithee390432014-05-16 01:56:53 +00001842 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001843 }
1844
1845 // An attribute-specifier-seq here is a sign of a function declarator.
1846 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1847 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001848 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001849
1850 ParsedAttributes attrs(AttrFactory);
1851 MaybeParseMicrosoftAttributes(attrs);
1852
1853 // decl-specifier-seq
1854 // A parameter-declaration's initializer must be preceded by an '=', so
1855 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001856 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001857 InvalidAsDeclaration);
1858
Richard Smithee390432014-05-16 01:56:53 +00001859 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001860 // Consume the decl-specifier-seq. We have to look past it, since a
1861 // type-id might appear here in a template argument.
1862 bool SeenType = false;
1863 do {
1864 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001865 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1866 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001867
1868 // If we see a parameter name, this can't be a template argument.
1869 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001870 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001871
Richard Smithee390432014-05-16 01:56:53 +00001872 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001873 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001874 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001875 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001876 } while (TPR != TPResult::False);
1877 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001878 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001879 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1880 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001881 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 return TPR;
1883
1884 // declarator
1885 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001886 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001887 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 return TPR;
1889
1890 // [GNU] attributes[opt]
1891 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001892 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001893
Richard Smith1fff95c2013-09-12 23:28:08 +00001894 // If we're disambiguating a template argument in a default argument in
1895 // a class definition versus a parameter declaration, an '=' here
1896 // disambiguates the parse one way or the other.
1897 // If this is a parameter, it must have a default argument because
1898 // (a) the previous parameter did, and
1899 // (b) this must be the first declaration of the function, so we can't
1900 // inherit any default arguments from elsewhere.
1901 // If we see an ')', then we've reached the end of a
1902 // parameter-declaration-clause, and the last param is missing its default
1903 // argument.
1904 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001905 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1906 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001907
Guy Benyei11169dd2012-12-18 14:30:41 +00001908 if (Tok.is(tok::equal)) {
1909 // '=' assignment-expression
1910 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001911 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001912 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001913 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001914 }
1915
1916 if (Tok.is(tok::ellipsis)) {
1917 ConsumeToken();
1918 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001919 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 else
Richard Smithee390432014-05-16 01:56:53 +00001921 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 }
1923
Alp Toker97650562014-01-10 11:19:30 +00001924 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001925 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 }
1927
Richard Smithee390432014-05-16 01:56:53 +00001928 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001929}
1930
1931/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1932/// parsing as a function declarator.
1933/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001934/// return TPResult::Ambiguous, otherwise it will return either False() or
1935/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001936///
1937/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1938/// exception-specification[opt]
1939///
1940/// exception-specification:
1941/// 'throw' '(' type-id-list[opt] ')'
1942///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001943Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001944
1945 // The '(' is already parsed.
1946
1947 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001948 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1949 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001950
Justin Bognerd26f95b2015-02-23 22:36:28 +00001951 if (TPR == TPResult::False || TPR == TPResult::Error)
1952 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001953
1954 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001955 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001956 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001957
1958 // cv-qualifier-seq
Reid Klecknerc6663b72018-03-07 23:26:02 +00001959 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
1960 tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 ConsumeToken();
1962
1963 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001964 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001965 ConsumeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00001966
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 // exception-specification
1968 if (Tok.is(tok::kw_throw)) {
1969 ConsumeToken();
1970 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001971 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001972
1973 // Parse through the parens after 'throw'.
1974 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001975 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001976 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 }
1978 if (Tok.is(tok::kw_noexcept)) {
1979 ConsumeToken();
1980 // Possibly an expression as well.
1981 if (Tok.is(tok::l_paren)) {
1982 // Find the matching rparen.
1983 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001984 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001985 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001986 }
1987 }
1988
Richard Smithee390432014-05-16 01:56:53 +00001989 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001990}
1991
1992/// '[' constant-expression[opt] ']'
1993///
1994Parser::TPResult Parser::TryParseBracketDeclarator() {
1995 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001996 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001997 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001998
Richard Smithee390432014-05-16 01:56:53 +00001999 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00002000}