blob: 8b076b69fa5ba77e845ef16d786714f2edd1a461 [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///
Guy Benyei11169dd2012-12-18 14:30:41 +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 ']'
86///
87/// 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...
Richard Smith91b73f22016-06-29 21:06:51 +0000135
136 {
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
239/// for-range-declaration:
240/// 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;
348
349 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement)
350 : P(P), CanBeInitStatement(CanBeInitStatement) {}
351
352 void markNotExpression() {
353 CanBeExpression = false;
354
355 if (CanBeCondition && CanBeInitStatement) {
356 // FIXME: Unify the parsing codepaths for condition variables and
357 // simple-declarations so that we don't need to eagerly figure out which
358 // kind we have here. (Just parse init-declarators until we reach a
359 // semicolon or right paren.)
360 RevertingTentativeParsingAction PA(P);
361 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
362 if (P.Tok.isNot(tok::r_paren))
363 CanBeCondition = false;
364 if (P.Tok.isNot(tok::semi))
365 CanBeInitStatement = false;
366 }
367 }
368
369 bool markNotCondition() {
370 CanBeCondition = false;
371 return !CanBeInitStatement || !CanBeExpression;
372 }
373
374 bool update(TPResult IsDecl) {
375 switch (IsDecl) {
376 case TPResult::True:
377 markNotExpression();
378 return true;
379 case TPResult::False:
380 CanBeCondition = CanBeInitStatement = false;
381 return true;
382 case TPResult::Ambiguous:
383 return false;
384 case TPResult::Error:
385 CanBeExpression = CanBeCondition = CanBeInitStatement = false;
386 return true;
387 }
388 llvm_unreachable("unknown tentative parse result");
389 }
390
391 ConditionOrInitStatement result() const {
392 assert(CanBeExpression + CanBeCondition + CanBeInitStatement < 2 &&
393 "result called but not yet resolved");
394 if (CanBeExpression)
395 return ConditionOrInitStatement::Expression;
396 if (CanBeCondition)
397 return ConditionOrInitStatement::ConditionDecl;
398 if (CanBeInitStatement)
399 return ConditionOrInitStatement::InitStmtDecl;
400 return ConditionOrInitStatement::Error;
401 }
402};
403
404/// \brief Disambiguates between a declaration in a condition, a
405/// simple-declaration in an init-statement, and an expression for
406/// a condition of a if/switch statement.
Guy Benyei11169dd2012-12-18 14:30:41 +0000407///
408/// condition:
409/// expression
410/// type-specifier-seq declarator '=' assignment-expression
411/// [C++11] type-specifier-seq declarator '=' initializer-clause
412/// [C++11] type-specifier-seq declarator braced-init-list
413/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
414/// '=' assignment-expression
Richard Smithc7a05a92016-06-29 21:17:59 +0000415/// simple-declaration:
416/// decl-specifier-seq init-declarator-list[opt] ';'
Guy Benyei11169dd2012-12-18 14:30:41 +0000417///
Richard Smithc7a05a92016-06-29 21:17:59 +0000418/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
419/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
420/// 'int(x);' (a simple-declaration in an init-statement).
421Parser::ConditionOrInitStatement
422Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement) {
423 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement);
Guy Benyei11169dd2012-12-18 14:30:41 +0000424
Richard Smithc7a05a92016-06-29 21:17:59 +0000425 if (State.update(isCXXDeclarationSpecifier()))
426 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000427
Richard Smithc7a05a92016-06-29 21:17:59 +0000428 // It might be a declaration; we need tentative parsing.
Richard Smith91b73f22016-06-29 21:06:51 +0000429 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000430
Richard Smithc7a05a92016-06-29 21:17:59 +0000431 // FIXME: A tag definition unambiguously tells us this is an init-statement.
432 if (State.update(TryConsumeDeclarationSpecifier()))
433 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 assert(Tok.is(tok::l_paren) && "Expected '('");
435
Richard Smithc7a05a92016-06-29 21:17:59 +0000436 while (true) {
437 // Consume a declarator.
438 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
439 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000440
Richard Smithc7a05a92016-06-29 21:17:59 +0000441 // Attributes, asm label, or an initializer imply this is not an expression.
442 // FIXME: Disambiguate properly after an = instead of assuming that it's a
443 // valid declaration.
444 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
445 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
446 State.markNotExpression();
447 return State.result();
448 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000449
Richard Smithc7a05a92016-06-29 21:17:59 +0000450 // At this point, it can't be a condition any more, because a condition
451 // must have a brace-or-equal-initializer.
452 if (State.markNotCondition())
453 return State.result();
454
455 // A parenthesized initializer could be part of an expression or a
456 // simple-declaration.
457 if (Tok.is(tok::l_paren)) {
458 ConsumeParen();
459 SkipUntil(tok::r_paren, StopAtSemi);
460 }
461
462 if (!TryConsumeToken(tok::comma))
463 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 }
465
Richard Smithc7a05a92016-06-29 21:17:59 +0000466 // We reached the end. If it can now be some kind of decl, then it is.
467 if (State.CanBeCondition && Tok.is(tok::r_paren))
468 return ConditionOrInitStatement::ConditionDecl;
469 else if (State.CanBeInitStatement && Tok.is(tok::semi))
470 return ConditionOrInitStatement::InitStmtDecl;
471 else
472 return ConditionOrInitStatement::Expression;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473}
474
475 /// \brief Determine whether the next set of tokens contains a type-id.
476 ///
477 /// The context parameter states what context we're parsing right
478 /// now, which affects how this routine copes with the token
479 /// following the type-id. If the context is TypeIdInParens, we have
480 /// already parsed the '(' and we will cease lookahead when we hit
481 /// the corresponding ')'. If the context is
482 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
483 /// before this template argument, and will cease lookahead when we
484 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
485 /// and false for an expression. If during the disambiguation
486 /// process a parsing error is encountered, the function returns
487 /// true to let the declaration parsing code handle it.
488 ///
489 /// type-id:
490 /// type-specifier-seq abstract-declarator[opt]
491 ///
492bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
493
494 isAmbiguous = false;
495
496 // C++ 8.2p2:
497 // The ambiguity arising from the similarity between a function-style cast and
498 // a type-id can occur in different contexts. The ambiguity appears as a
499 // choice between a function-style cast expression and a declaration of a
500 // type. The resolution is that any construct that could possibly be a type-id
501 // in its syntactic context shall be considered a type-id.
502
503 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000504 if (TPR != TPResult::Ambiguous)
505 return TPR != TPResult::False; // Returns true for TPResult::True or
506 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000507
508 // FIXME: Add statistics about the number of ambiguous statements encountered
509 // and how they were resolved (number of declarations+number of expressions).
510
511 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
512 // We need tentative parsing...
513
Richard Smith91b73f22016-06-29 21:06:51 +0000514 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000515
516 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000517 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 assert(Tok.is(tok::l_paren) && "Expected '('");
519
520 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000521 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000522
523 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000524 if (TPR == TPResult::Error)
525 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526
Richard Smithee390432014-05-16 01:56:53 +0000527 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 // We are supposed to be inside parens, so if after the abstract declarator
529 // we encounter a ')' this is a type-id, otherwise it's an expression.
530 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000531 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000532 isAmbiguous = true;
533
534 // We are supposed to be inside a template argument, so if after
535 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
536 // ',', this is a type-id. Otherwise, it's an expression.
537 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000538 (Tok.isOneOf(tok::greater, tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000539 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000540 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000541 isAmbiguous = true;
542
543 } else
Richard Smithee390432014-05-16 01:56:53 +0000544 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 }
546
Richard Smithee390432014-05-16 01:56:53 +0000547 assert(TPR == TPResult::True || TPR == TPResult::False);
548 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000549}
550
551/// \brief Returns true if this is a C++11 attribute-specifier. Per
552/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
553/// always introduce an attribute. In Objective-C++11, this rule does not
554/// apply if either '[' begins a message-send.
555///
556/// If Disambiguate is true, we try harder to determine whether a '[[' starts
557/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
558///
559/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
560/// Obj-C message send or the start of an attribute. Otherwise, we assume it
561/// is not an Obj-C message send.
562///
563/// C++11 [dcl.attr.grammar]:
564///
565/// attribute-specifier:
566/// '[' '[' attribute-list ']' ']'
567/// alignment-specifier
568///
569/// attribute-list:
570/// attribute[opt]
571/// attribute-list ',' attribute[opt]
572/// attribute '...'
573/// attribute-list ',' attribute '...'
574///
575/// attribute:
576/// attribute-token attribute-argument-clause[opt]
577///
578/// attribute-token:
579/// identifier
580/// identifier '::' identifier
581///
582/// attribute-argument-clause:
583/// '(' balanced-token-seq ')'
584Parser::CXX11AttributeKind
585Parser::isCXX11AttributeSpecifier(bool Disambiguate,
586 bool OuterMightBeMessageSend) {
587 if (Tok.is(tok::kw_alignas))
588 return CAK_AttributeSpecifier;
589
590 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
591 return CAK_NotAttributeSpecifier;
592
593 // No tentative parsing if we don't need to look for ']]' or a lambda.
594 if (!Disambiguate && !getLangOpts().ObjC1)
595 return CAK_AttributeSpecifier;
596
Richard Smith91b73f22016-06-29 21:06:51 +0000597 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000598
599 // Opening brackets were checked for above.
600 ConsumeBracket();
601
602 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
603 if (!getLangOpts().ObjC1) {
604 ConsumeBracket();
605
Alexey Bataevee6507d2013-11-18 08:17:37 +0000606 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000607 IsAttribute &= Tok.is(tok::r_square);
608
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
610 }
611
612 // In Obj-C++11, we need to distinguish four situations:
613 // 1a) int x[[attr]]; C++11 attribute.
614 // 1b) [[attr]]; C++11 statement attribute.
615 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
616 // 3a) int x[[obj get]]; Message send in array size/index.
617 // 3b) [[Class alloc] init]; Message send in message send.
618 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
619 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
620
621 // If we have a lambda-introducer, then this is definitely not a message send.
622 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
623 // into the tentative attribute parse below.
624 LambdaIntroducer Intro;
625 if (!TryParseLambdaIntroducer(Intro)) {
626 // A lambda cannot end with ']]', and an attribute must.
627 bool IsAttribute = Tok.is(tok::r_square);
628
Guy Benyei11169dd2012-12-18 14:30:41 +0000629 if (IsAttribute)
630 // Case 1: C++11 attribute.
631 return CAK_AttributeSpecifier;
632
633 if (OuterMightBeMessageSend)
634 // Case 4: Lambda in message send.
635 return CAK_NotAttributeSpecifier;
636
637 // Case 2: Lambda in array size / index.
638 return CAK_InvalidAttributeSpecifier;
639 }
640
641 ConsumeBracket();
642
643 // If we don't have a lambda-introducer, then we have an attribute or a
644 // message-send.
645 bool IsAttribute = true;
646 while (Tok.isNot(tok::r_square)) {
647 if (Tok.is(tok::comma)) {
648 // Case 1: Stray commas can only occur in attributes.
Guy Benyei11169dd2012-12-18 14:30:41 +0000649 return CAK_AttributeSpecifier;
650 }
651
652 // Parse the attribute-token, if present.
653 // C++11 [dcl.attr.grammar]:
654 // If a keyword or an alternative token that satisfies the syntactic
655 // requirements of an identifier is contained in an attribute-token,
656 // it is considered an identifier.
657 SourceLocation Loc;
658 if (!TryParseCXX11AttributeIdentifier(Loc)) {
659 IsAttribute = false;
660 break;
661 }
662 if (Tok.is(tok::coloncolon)) {
663 ConsumeToken();
664 if (!TryParseCXX11AttributeIdentifier(Loc)) {
665 IsAttribute = false;
666 break;
667 }
668 }
669
670 // Parse the attribute-argument-clause, if present.
671 if (Tok.is(tok::l_paren)) {
672 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000673 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 IsAttribute = false;
675 break;
676 }
677 }
678
Alp Toker97650562014-01-10 11:19:30 +0000679 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000680
Alp Toker97650562014-01-10 11:19:30 +0000681 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 }
684
685 // An attribute must end ']]'.
686 if (IsAttribute) {
687 if (Tok.is(tok::r_square)) {
688 ConsumeBracket();
689 IsAttribute = Tok.is(tok::r_square);
690 } else {
691 IsAttribute = false;
692 }
693 }
694
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 if (IsAttribute)
696 // Case 1: C++11 statement attribute.
697 return CAK_AttributeSpecifier;
698
699 // Case 3: Message send.
700 return CAK_NotAttributeSpecifier;
701}
702
Richard Smith1fff95c2013-09-12 23:28:08 +0000703Parser::TPResult Parser::TryParsePtrOperatorSeq() {
704 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000705 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000706 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000707 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000708
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000709 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000710 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
711 // ptr-operator
Richard Smithaf3b3252017-05-18 19:21:48 +0000712 ConsumeAnyToken();
Douglas Gregor261a89b2015-06-19 17:51:05 +0000713 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000714 tok::kw__Nonnull, tok::kw__Nullable,
715 tok::kw__Null_unspecified))
Richard Smith1fff95c2013-09-12 23:28:08 +0000716 ConsumeToken();
717 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000718 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000719 }
720 }
721}
722
723/// operator-function-id:
724/// 'operator' operator
725///
726/// operator: one of
727/// new delete new[] delete[] + - * / % ^ [...]
728///
729/// conversion-function-id:
730/// 'operator' conversion-type-id
731///
732/// conversion-type-id:
733/// type-specifier-seq conversion-declarator[opt]
734///
735/// conversion-declarator:
736/// ptr-operator conversion-declarator[opt]
737///
738/// literal-operator-id:
739/// 'operator' string-literal identifier
740/// 'operator' user-defined-string-literal
741Parser::TPResult Parser::TryParseOperatorId() {
742 assert(Tok.is(tok::kw_operator));
743 ConsumeToken();
744
745 // Maybe this is an operator-function-id.
746 switch (Tok.getKind()) {
747 case tok::kw_new: case tok::kw_delete:
748 ConsumeToken();
749 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
750 ConsumeBracket();
751 ConsumeBracket();
752 }
Richard Smithee390432014-05-16 01:56:53 +0000753 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000754
755#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
756 case tok::Token:
757#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
758#include "clang/Basic/OperatorKinds.def"
759 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000760 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000761
762 case tok::l_square:
763 if (NextToken().is(tok::r_square)) {
764 ConsumeBracket();
765 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000766 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000767 }
768 break;
769
770 case tok::l_paren:
771 if (NextToken().is(tok::r_paren)) {
772 ConsumeParen();
773 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000774 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000775 }
776 break;
777
778 default:
779 break;
780 }
781
782 // Maybe this is a literal-operator-id.
783 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
784 bool FoundUDSuffix = false;
785 do {
786 FoundUDSuffix |= Tok.hasUDSuffix();
787 ConsumeStringToken();
788 } while (isTokenStringLiteral());
789
790 if (!FoundUDSuffix) {
791 if (Tok.is(tok::identifier))
792 ConsumeToken();
793 else
Richard Smithee390432014-05-16 01:56:53 +0000794 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000795 }
Richard Smithee390432014-05-16 01:56:53 +0000796 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000797 }
798
799 // Maybe this is a conversion-function-id.
800 bool AnyDeclSpecifiers = false;
801 while (true) {
802 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000803 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000804 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000805 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000806 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000807 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000808 break;
809 }
Richard Smithee390432014-05-16 01:56:53 +0000810 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
811 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000812 AnyDeclSpecifiers = true;
813 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000814 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000815}
816
Guy Benyei11169dd2012-12-18 14:30:41 +0000817/// declarator:
818/// direct-declarator
819/// ptr-operator declarator
820///
821/// direct-declarator:
822/// declarator-id
823/// direct-declarator '(' parameter-declaration-clause ')'
824/// cv-qualifier-seq[opt] exception-specification[opt]
825/// direct-declarator '[' constant-expression[opt] ']'
826/// '(' declarator ')'
827/// [GNU] '(' attributes declarator ')'
828///
829/// abstract-declarator:
830/// ptr-operator abstract-declarator[opt]
831/// direct-abstract-declarator
832/// ...
833///
834/// direct-abstract-declarator:
835/// direct-abstract-declarator[opt]
836/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
837/// exception-specification[opt]
838/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
839/// '(' abstract-declarator ')'
840///
841/// ptr-operator:
842/// '*' cv-qualifier-seq[opt]
843/// '&'
844/// [C++0x] '&&' [TODO]
845/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
846///
847/// cv-qualifier-seq:
848/// cv-qualifier cv-qualifier-seq[opt]
849///
850/// cv-qualifier:
851/// 'const'
852/// 'volatile'
853///
854/// declarator-id:
855/// '...'[opt] id-expression
856///
857/// id-expression:
858/// unqualified-id
859/// qualified-id [TODO]
860///
861/// unqualified-id:
862/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000863/// operator-function-id
864/// conversion-function-id
865/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000866/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000867/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000868/// template-id [TODO]
869///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000870Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
871 bool mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000872 // declarator:
873 // direct-declarator
874 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000875 if (TryParsePtrOperatorSeq() == TPResult::Error)
876 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000877
878 // direct-declarator:
879 // direct-abstract-declarator:
880 if (Tok.is(tok::ellipsis))
881 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000882
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000883 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000884 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
885 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000886 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000887 // declarator-id
888 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +0000889 ConsumeAnnotationToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000890 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000891 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000892 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000893 if (TryParseOperatorId() == TPResult::Error)
894 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000895 } else
896 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000897 } else if (Tok.is(tok::l_paren)) {
898 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000899 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000900 (Tok.is(tok::r_paren) || // 'int()' is a function.
901 // 'int(...)' is a function.
902 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
903 isDeclarationSpecifier())) { // 'int(int)' is a function.
904 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
905 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000906 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000907 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000908 return TPR;
909 } else {
910 // '(' declarator ')'
911 // '(' attributes declarator ')'
912 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000913 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
914 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
Erich Keane757d3172016-11-02 18:29:35 +0000915 tok::kw___regcall, tok::kw___vectorcall))
Richard Smithee390432014-05-16 01:56:53 +0000916 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000917 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000918 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000919 return TPR;
920 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000921 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000922 ConsumeParen();
923 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000924 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000925 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000926 }
927
928 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000929 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000930
931 // abstract-declarator: ...
932 if (Tok.is(tok::ellipsis))
933 ConsumeToken();
934
935 if (Tok.is(tok::l_paren)) {
936 // Check whether we have a function declarator or a possible ctor-style
937 // initializer that follows the declarator. Note that ctor-style
938 // initializers are not possible in contexts where abstract declarators
939 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000940 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000941 break;
942
943 // direct-declarator '(' parameter-declaration-clause ')'
944 // cv-qualifier-seq[opt] exception-specification[opt]
945 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000946 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +0000947 } else if (Tok.is(tok::l_square)) {
948 // direct-declarator '[' constant-expression[opt] ']'
949 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
950 TPR = TryParseBracketDeclarator();
951 } else {
952 break;
953 }
954
Richard Smithee390432014-05-16 01:56:53 +0000955 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 return TPR;
957 }
958
Richard Smithee390432014-05-16 01:56:53 +0000959 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000960}
961
962Parser::TPResult
963Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
964 switch (Kind) {
965 // Obviously starts an expression.
966 case tok::numeric_constant:
967 case tok::char_constant:
968 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000969 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000970 case tok::utf16_char_constant:
971 case tok::utf32_char_constant:
972 case tok::string_literal:
973 case tok::wide_string_literal:
974 case tok::utf8_string_literal:
975 case tok::utf16_string_literal:
976 case tok::utf32_string_literal:
977 case tok::l_square:
978 case tok::l_paren:
979 case tok::amp:
980 case tok::ampamp:
981 case tok::star:
982 case tok::plus:
983 case tok::plusplus:
984 case tok::minus:
985 case tok::minusminus:
986 case tok::tilde:
987 case tok::exclaim:
988 case tok::kw_sizeof:
989 case tok::kw___func__:
990 case tok::kw_const_cast:
991 case tok::kw_delete:
992 case tok::kw_dynamic_cast:
993 case tok::kw_false:
994 case tok::kw_new:
995 case tok::kw_operator:
996 case tok::kw_reinterpret_cast:
997 case tok::kw_static_cast:
998 case tok::kw_this:
999 case tok::kw_throw:
1000 case tok::kw_true:
1001 case tok::kw_typeid:
1002 case tok::kw_alignof:
1003 case tok::kw_noexcept:
1004 case tok::kw_nullptr:
1005 case tok::kw__Alignof:
1006 case tok::kw___null:
1007 case tok::kw___alignof:
1008 case tok::kw___builtin_choose_expr:
1009 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 case tok::kw___builtin_va_arg:
1011 case tok::kw___imag:
1012 case tok::kw___real:
1013 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +00001014 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +00001015 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001016 case tok::kw_L__FUNCTION__:
1017 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001018 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +00001019#define TYPE_TRAIT(N,Spelling,K) \
1020 case tok::kw_##Spelling:
1021#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +00001022 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001023
1024 // Obviously starts a type-specifier-seq:
1025 case tok::kw_char:
1026 case tok::kw_const:
1027 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001028 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001029 case tok::kw_enum:
1030 case tok::kw_half:
1031 case tok::kw_float:
1032 case tok::kw_int:
1033 case tok::kw_long:
1034 case tok::kw___int64:
1035 case tok::kw___int128:
1036 case tok::kw_restrict:
1037 case tok::kw_short:
1038 case tok::kw_signed:
1039 case tok::kw_struct:
1040 case tok::kw_union:
1041 case tok::kw_unsigned:
1042 case tok::kw_void:
1043 case tok::kw_volatile:
1044 case tok::kw__Bool:
1045 case tok::kw__Complex:
1046 case tok::kw_class:
1047 case tok::kw_typename:
1048 case tok::kw_wchar_t:
1049 case tok::kw_char16_t:
1050 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001051 case tok::kw__Decimal32:
1052 case tok::kw__Decimal64:
1053 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001054 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001056 case tok::kw_thread_local:
1057 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +00001059 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +00001060 case tok::kw___cdecl:
1061 case tok::kw___stdcall:
1062 case tok::kw___fastcall:
1063 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00001064 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001065 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 case tok::kw___unaligned:
1067 case tok::kw___vector:
1068 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +00001069 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +00001070 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +00001071#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00001072#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00001073 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +00001074 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001075
1076 default:
1077 break;
1078 }
1079
Richard Smithee390432014-05-16 01:56:53 +00001080 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001081}
1082
1083bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1084 return std::find(TentativelyDeclaredIdentifiers.begin(),
1085 TentativelyDeclaredIdentifiers.end(), II)
1086 != TentativelyDeclaredIdentifiers.end();
1087}
1088
Kaelyn Takata445b0652014-11-05 00:09:29 +00001089namespace {
1090class TentativeParseCCC : public CorrectionCandidateCallback {
1091public:
1092 TentativeParseCCC(const Token &Next) {
1093 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001094 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1095 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001096 }
1097
1098 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1099 // Reject any candidate that only resolves to instance members since they
1100 // aren't viable as standalone identifiers instead of member references.
1101 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1102 std::all_of(Candidate.begin(), Candidate.end(),
1103 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1104 return false;
1105
1106 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1107 }
1108};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001109}
Richard Smithee390432014-05-16 01:56:53 +00001110/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1111/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1112/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001113/// if a parsing error was found and reported.
1114///
1115/// If HasMissingTypename is provided, a name with a dependent scope specifier
1116/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1117/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1118/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001119/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001120/// such cases.
1121///
1122/// decl-specifier:
1123/// storage-class-specifier
1124/// type-specifier
1125/// function-specifier
1126/// 'friend'
1127/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001128/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001129/// [GNU] attributes declaration-specifiers[opt]
1130///
1131/// storage-class-specifier:
1132/// 'register'
1133/// 'static'
1134/// 'extern'
1135/// 'mutable'
1136/// 'auto'
1137/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001138/// [C++11] 'thread_local'
1139/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001140///
1141/// function-specifier:
1142/// 'inline'
1143/// 'virtual'
1144/// 'explicit'
1145///
1146/// typedef-name:
1147/// identifier
1148///
1149/// type-specifier:
1150/// simple-type-specifier
1151/// class-specifier
1152/// enum-specifier
1153/// elaborated-type-specifier
1154/// typename-specifier
1155/// cv-qualifier
1156///
1157/// simple-type-specifier:
1158/// '::'[opt] nested-name-specifier[opt] type-name
1159/// '::'[opt] nested-name-specifier 'template'
1160/// simple-template-id [TODO]
1161/// 'char'
1162/// 'wchar_t'
1163/// 'bool'
1164/// 'short'
1165/// 'int'
1166/// 'long'
1167/// 'signed'
1168/// 'unsigned'
1169/// 'float'
1170/// 'double'
1171/// 'void'
1172/// [GNU] typeof-specifier
1173/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001174/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001175/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001176/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001177/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001178///
1179/// type-name:
1180/// class-name
1181/// enum-name
1182/// typedef-name
1183///
1184/// elaborated-type-specifier:
1185/// class-key '::'[opt] nested-name-specifier[opt] identifier
1186/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1187/// simple-template-id
1188/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1189///
1190/// enum-name:
1191/// identifier
1192///
1193/// enum-specifier:
1194/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1195/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1196///
1197/// class-specifier:
1198/// class-head '{' member-specification[opt] '}'
1199///
1200/// class-head:
1201/// class-key identifier[opt] base-clause[opt]
1202/// class-key nested-name-specifier identifier base-clause[opt]
1203/// class-key nested-name-specifier[opt] simple-template-id
1204/// base-clause[opt]
1205///
1206/// class-key:
1207/// 'class'
1208/// 'struct'
1209/// 'union'
1210///
1211/// cv-qualifier:
1212/// 'const'
1213/// 'volatile'
1214/// [GNU] restrict
1215///
1216Parser::TPResult
1217Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1218 bool *HasMissingTypename) {
1219 switch (Tok.getKind()) {
1220 case tok::identifier: {
1221 // Check for need to substitute AltiVec __vector keyword
1222 // for "vector" identifier.
1223 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001224 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001225
1226 const Token &Next = NextToken();
1227 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1228 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001229 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001230
1231 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1232 // Determine whether this is a valid expression. If not, we will hit
1233 // a parse error one way or another. In that case, tell the caller that
1234 // this is ambiguous. Typo-correct to type and expression keywords and
1235 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001236 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001237 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001239 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001241 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 case ANK_TemplateName:
1243 // A bare type template-name which can't be a template template
1244 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001245 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001247 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 case ANK_Success:
1249 break;
1250 }
1251 assert(Tok.isNot(tok::identifier) &&
1252 "TryAnnotateName succeeded without producing an annotation");
1253 } else {
1254 // This might possibly be a type with a dependent scope specifier and
1255 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1256 // since it will annotate as a primary expression, and we want to use the
1257 // "missing 'typename'" logic.
1258 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001259 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 // If annotation failed, assume it's a non-type.
1261 // FIXME: If this happens due to an undeclared identifier, treat it as
1262 // ambiguous.
1263 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001264 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001265 }
1266
1267 // We annotated this token as something. Recurse to handle whatever we got.
1268 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1269 }
1270
1271 case tok::kw_typename: // typename T::type
1272 // Annotate typenames and C++ scope specifiers. If we get one, just
1273 // recurse to handle whatever we get.
1274 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001275 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1277
1278 case tok::coloncolon: { // ::foo::bar
1279 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001280 if (Next.isOneOf(tok::kw_new, // ::new
1281 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001282 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001283 }
1284 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001285 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001286 case tok::kw_decltype:
1287 // Annotate typenames and C++ scope specifiers. If we get one, just
1288 // recurse to handle whatever we get.
1289 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001290 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001291 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1292
1293 // decl-specifier:
1294 // storage-class-specifier
1295 // type-specifier
1296 // function-specifier
1297 // 'friend'
1298 // 'typedef'
1299 // 'constexpr'
Hubert Tong375f00a2015-06-30 12:14:52 +00001300 // 'concept'
Guy Benyei11169dd2012-12-18 14:30:41 +00001301 case tok::kw_friend:
1302 case tok::kw_typedef:
1303 case tok::kw_constexpr:
Hubert Tong375f00a2015-06-30 12:14:52 +00001304 case tok::kw_concept:
Guy Benyei11169dd2012-12-18 14:30:41 +00001305 // storage-class-specifier
1306 case tok::kw_register:
1307 case tok::kw_static:
1308 case tok::kw_extern:
1309 case tok::kw_mutable:
1310 case tok::kw_auto:
1311 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001312 case tok::kw_thread_local:
1313 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001314 // function-specifier
1315 case tok::kw_inline:
1316 case tok::kw_virtual:
1317 case tok::kw_explicit:
1318
1319 // Modules
1320 case tok::kw___module_private__:
1321
1322 // Debugger support
1323 case tok::kw___unknown_anytype:
1324
1325 // type-specifier:
1326 // simple-type-specifier
1327 // class-specifier
1328 // enum-specifier
1329 // elaborated-type-specifier
1330 // typename-specifier
1331 // cv-qualifier
1332
1333 // class-specifier
1334 // elaborated-type-specifier
1335 case tok::kw_class:
1336 case tok::kw_struct:
1337 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001338 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001339 // enum-specifier
1340 case tok::kw_enum:
1341 // cv-qualifier
1342 case tok::kw_const:
1343 case tok::kw_volatile:
1344
1345 // GNU
1346 case tok::kw_restrict:
1347 case tok::kw__Complex:
1348 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001349 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001350 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001351
1352 // Microsoft
1353 case tok::kw___declspec:
1354 case tok::kw___cdecl:
1355 case tok::kw___stdcall:
1356 case tok::kw___fastcall:
1357 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00001358 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001359 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001361 case tok::kw___sptr:
1362 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 case tok::kw___ptr64:
1364 case tok::kw___ptr32:
1365 case tok::kw___forceinline:
1366 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001367 case tok::kw__Nonnull:
1368 case tok::kw__Nullable:
1369 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001370 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001371 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001372
1373 // Borland
1374 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001375 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001376
1377 // AltiVec
1378 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001379 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001380
1381 case tok::annot_template_id: {
1382 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1383 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001384 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 CXXScopeSpec SS;
1386 AnnotateTemplateIdTokenAsType();
1387 assert(Tok.is(tok::annot_typename));
1388 goto case_typename;
1389 }
1390
1391 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1392 // We've already annotated a scope; try to annotate a type.
1393 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001394 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 if (!Tok.is(tok::annot_typename)) {
1396 // If the next token is an identifier or a type qualifier, then this
1397 // can't possibly be a valid expression either.
1398 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1399 CXXScopeSpec SS;
1400 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1401 Tok.getAnnotationRange(),
1402 SS);
1403 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
Richard Smith4556ebe2016-06-29 21:12:37 +00001404 RevertingTentativeParsingAction PA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001405 ConsumeAnnotationToken();
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 ConsumeToken();
1407 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001408 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 if (!isIdentifier)
1410 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1411 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001412
1413 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001414 TPR == TPResult::True || TPR == TPResult::Error)
1415 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001416
1417 if (HasMissingTypename) {
1418 // We can't tell whether this is a missing 'typename' or a valid
1419 // expression.
1420 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001421 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 }
Richard Smithc7a05a92016-06-29 21:17:59 +00001423
1424 // FIXME: Fails to either revert or commit the tentative parse!
Guy Benyei11169dd2012-12-18 14:30:41 +00001425 } else {
1426 // Try to resolve the name. If it doesn't exist, assume it was
1427 // intended to name a type and keep disambiguating.
1428 switch (TryAnnotateName(false /* SS is not dependent */)) {
1429 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001430 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001431 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001432 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001433 case ANK_TemplateName:
1434 // A bare type template-name which can't be a template template
1435 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001436 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001438 return HasMissingTypename ? TPResult::Ambiguous
1439 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 case ANK_Success:
1441 // Annotated it, check again.
1442 assert(Tok.isNot(tok::annot_cxxscope) ||
1443 NextToken().isNot(tok::identifier));
1444 return isCXXDeclarationSpecifier(BracedCastResult,
1445 HasMissingTypename);
1446 }
1447 }
1448 }
Richard Smithee390432014-05-16 01:56:53 +00001449 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 }
1451 // If that succeeded, fallthrough into the generic simple-type-id case.
1452
1453 // The ambiguity resides in a simple-type-specifier/typename-specifier
1454 // followed by a '('. The '(' could either be the start of:
1455 //
1456 // direct-declarator:
1457 // '(' declarator ')'
1458 //
1459 // direct-abstract-declarator:
1460 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1461 // exception-specification[opt]
1462 // '(' abstract-declarator ')'
1463 //
1464 // or part of a function-style cast expression:
1465 //
1466 // simple-type-specifier '(' expression-list[opt] ')'
1467 //
1468
1469 // simple-type-specifier:
1470
1471 case tok::annot_typename:
1472 case_typename:
1473 // In Objective-C, we might have a protocol-qualified type.
1474 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001475 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001476 RevertingTentativeParsingAction PA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001477 ConsumeAnyToken(); // The type token
Guy Benyei11169dd2012-12-18 14:30:41 +00001478
1479 TPResult TPR = TryParseProtocolQualifiers();
1480 bool isFollowedByParen = Tok.is(tok::l_paren);
1481 bool isFollowedByBrace = Tok.is(tok::l_brace);
1482
Richard Smithee390432014-05-16 01:56:53 +00001483 if (TPR == TPResult::Error)
1484 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001485
1486 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001487 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001488
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001489 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001490 return BracedCastResult;
1491
Richard Smithee390432014-05-16 01:56:53 +00001492 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001493 }
1494
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:
Guy Benyei11169dd2012-12-18 14:30:41 +00001511 case tok::kw_void:
1512 case tok::annot_decltype:
1513 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001514 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001515
1516 // This is a function-style cast in all cases we disambiguate other than
1517 // one:
1518 // struct S {
1519 // enum E : int { a = 4 }; // enum
1520 // enum E : int { 4 }; // bit-field
1521 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001522 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001523 return BracedCastResult;
1524
1525 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001526 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001527
Richard Smithee390432014-05-16 01:56:53 +00001528 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001529
1530 // GNU typeof support.
1531 case tok::kw_typeof: {
1532 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001533 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001534
Richard Smith91b73f22016-06-29 21:06:51 +00001535 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001536
1537 TPResult TPR = TryParseTypeofSpecifier();
1538 bool isFollowedByParen = Tok.is(tok::l_paren);
1539 bool isFollowedByBrace = Tok.is(tok::l_brace);
1540
Richard Smithee390432014-05-16 01:56:53 +00001541 if (TPR == TPResult::Error)
1542 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001543
1544 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001545 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001546
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001547 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 return BracedCastResult;
1549
Richard Smithee390432014-05-16 01:56:53 +00001550 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 }
1552
1553 // C++0x type traits support
1554 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001555 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001556
1557 // C11 _Atomic
1558 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001559 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001560
1561 default:
Richard Smithee390432014-05-16 01:56:53 +00001562 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 }
1564}
1565
Richard Smith1fff95c2013-09-12 23:28:08 +00001566bool Parser::isCXXDeclarationSpecifierAType() {
1567 switch (Tok.getKind()) {
1568 // typename-specifier
1569 case tok::annot_decltype:
1570 case tok::annot_template_id:
1571 case tok::annot_typename:
1572 case tok::kw_typeof:
1573 case tok::kw___underlying_type:
1574 return true;
1575
1576 // elaborated-type-specifier
1577 case tok::kw_class:
1578 case tok::kw_struct:
1579 case tok::kw_union:
1580 case tok::kw___interface:
1581 case tok::kw_enum:
1582 return true;
1583
1584 // simple-type-specifier
1585 case tok::kw_char:
1586 case tok::kw_wchar_t:
1587 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:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001600 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001601 case tok::kw_void:
1602 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001603 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001604 return true;
1605
1606 case tok::kw_auto:
1607 return getLangOpts().CPlusPlus11;
1608
1609 case tok::kw__Atomic:
1610 // "_Atomic foo"
1611 return NextToken().is(tok::l_paren);
1612
1613 default:
1614 return false;
1615 }
1616}
1617
Guy Benyei11169dd2012-12-18 14:30:41 +00001618/// [GNU] typeof-specifier:
1619/// 'typeof' '(' expressions ')'
1620/// 'typeof' '(' type-name ')'
1621///
1622Parser::TPResult Parser::TryParseTypeofSpecifier() {
1623 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1624 ConsumeToken();
1625
1626 assert(Tok.is(tok::l_paren) && "Expected '('");
1627 // Parse through the parens after 'typeof'.
1628 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001629 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001630 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001631
Richard Smithee390432014-05-16 01:56:53 +00001632 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001633}
1634
1635/// [ObjC] protocol-qualifiers:
1636//// '<' identifier-list '>'
1637Parser::TPResult Parser::TryParseProtocolQualifiers() {
1638 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1639 ConsumeToken();
1640 do {
1641 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001642 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 ConsumeToken();
1644
1645 if (Tok.is(tok::comma)) {
1646 ConsumeToken();
1647 continue;
1648 }
1649
1650 if (Tok.is(tok::greater)) {
1651 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001652 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001653 }
1654 } while (false);
1655
Richard Smithee390432014-05-16 01:56:53 +00001656 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001657}
1658
Guy Benyei11169dd2012-12-18 14:30:41 +00001659/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1660/// a constructor-style initializer, when parsing declaration statements.
1661/// Returns true for function declarator and false for constructor-style
1662/// initializer.
1663/// If during the disambiguation process a parsing error is encountered,
1664/// the function returns true to let the declaration parsing code handle it.
1665///
1666/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1667/// exception-specification[opt]
1668///
1669bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1670
1671 // C++ 8.2p1:
1672 // The ambiguity arising from the similarity between a function-style cast and
1673 // a declaration mentioned in 6.8 can also occur in the context of a
1674 // declaration. In that context, the choice is between a function declaration
1675 // with a redundant set of parentheses around a parameter name and an object
1676 // declaration with a function-style cast as the initializer. Just as for the
1677 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1678 // that could possibly be a declaration a declaration.
1679
Richard Smith91b73f22016-06-29 21:06:51 +00001680 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
1682 ConsumeParen();
1683 bool InvalidAsDeclaration = false;
1684 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001685 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001687 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001688 else {
1689 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001690 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1691 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1692 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1693 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 // The next token cannot appear after a constructor-style initializer,
1695 // and can appear next in a function definition. This must be a function
1696 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001697 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698 else if (InvalidAsDeclaration)
1699 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001700 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 }
1702 }
1703
Richard Smithee390432014-05-16 01:56:53 +00001704 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 *IsAmbiguous = true;
1706
1707 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001708 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001709}
1710
1711/// parameter-declaration-clause:
1712/// parameter-declaration-list[opt] '...'[opt]
1713/// parameter-declaration-list ',' '...'
1714///
1715/// parameter-declaration-list:
1716/// parameter-declaration
1717/// parameter-declaration-list ',' parameter-declaration
1718///
1719/// parameter-declaration:
1720/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1721/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1722/// '=' assignment-expression
1723/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1724/// attributes[opt]
1725/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1726/// attributes[opt] '=' assignment-expression
1727///
1728Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001729Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1730 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001731
1732 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001733 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001734
1735 // parameter-declaration-list[opt] '...'[opt]
1736 // parameter-declaration-list ',' '...'
1737 //
1738 // parameter-declaration-list:
1739 // parameter-declaration
1740 // parameter-declaration-list ',' parameter-declaration
1741 //
1742 while (1) {
1743 // '...'[opt]
1744 if (Tok.is(tok::ellipsis)) {
1745 ConsumeToken();
1746 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001747 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 else
Richard Smithee390432014-05-16 01:56:53 +00001749 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001750 }
1751
1752 // An attribute-specifier-seq here is a sign of a function declarator.
1753 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1754 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001755 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001756
1757 ParsedAttributes attrs(AttrFactory);
1758 MaybeParseMicrosoftAttributes(attrs);
1759
1760 // decl-specifier-seq
1761 // A parameter-declaration's initializer must be preceded by an '=', so
1762 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001763 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001764 InvalidAsDeclaration);
1765
Richard Smithee390432014-05-16 01:56:53 +00001766 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001767 // Consume the decl-specifier-seq. We have to look past it, since a
1768 // type-id might appear here in a template argument.
1769 bool SeenType = false;
1770 do {
1771 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001772 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1773 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001774
1775 // If we see a parameter name, this can't be a template argument.
1776 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001777 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001778
Richard Smithee390432014-05-16 01:56:53 +00001779 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001780 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001781 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001782 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001783 } while (TPR != TPResult::False);
1784 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001785 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001786 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1787 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001788 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 return TPR;
1790
1791 // declarator
1792 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001793 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001794 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 return TPR;
1796
1797 // [GNU] attributes[opt]
1798 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001799 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001800
Richard Smith1fff95c2013-09-12 23:28:08 +00001801 // If we're disambiguating a template argument in a default argument in
1802 // a class definition versus a parameter declaration, an '=' here
1803 // disambiguates the parse one way or the other.
1804 // If this is a parameter, it must have a default argument because
1805 // (a) the previous parameter did, and
1806 // (b) this must be the first declaration of the function, so we can't
1807 // inherit any default arguments from elsewhere.
1808 // If we see an ')', then we've reached the end of a
1809 // parameter-declaration-clause, and the last param is missing its default
1810 // argument.
1811 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001812 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1813 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001814
Guy Benyei11169dd2012-12-18 14:30:41 +00001815 if (Tok.is(tok::equal)) {
1816 // '=' assignment-expression
1817 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001818 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001819 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001820 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 }
1822
1823 if (Tok.is(tok::ellipsis)) {
1824 ConsumeToken();
1825 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001826 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001827 else
Richard Smithee390432014-05-16 01:56:53 +00001828 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 }
1830
Alp Toker97650562014-01-10 11:19:30 +00001831 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001833 }
1834
Richard Smithee390432014-05-16 01:56:53 +00001835 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001836}
1837
1838/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1839/// parsing as a function declarator.
1840/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001841/// return TPResult::Ambiguous, otherwise it will return either False() or
1842/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001843///
1844/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1845/// exception-specification[opt]
1846///
1847/// exception-specification:
1848/// 'throw' '(' type-id-list[opt] ')'
1849///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001850Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001851
1852 // The '(' is already parsed.
1853
1854 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001855 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1856 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001857
Justin Bognerd26f95b2015-02-23 22:36:28 +00001858 if (TPR == TPResult::False || TPR == TPResult::Error)
1859 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001860
1861 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001862 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001863 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001864
1865 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001866 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001867 ConsumeToken();
1868
1869 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001870 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 ConsumeToken();
1872
1873 // exception-specification
1874 if (Tok.is(tok::kw_throw)) {
1875 ConsumeToken();
1876 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001877 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001878
1879 // Parse through the parens after 'throw'.
1880 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001881 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001882 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 }
1884 if (Tok.is(tok::kw_noexcept)) {
1885 ConsumeToken();
1886 // Possibly an expression as well.
1887 if (Tok.is(tok::l_paren)) {
1888 // Find the matching rparen.
1889 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001890 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001891 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001892 }
1893 }
1894
Richard Smithee390432014-05-16 01:56:53 +00001895 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001896}
1897
1898/// '[' constant-expression[opt] ']'
1899///
1900Parser::TPResult Parser::TryParseBracketDeclarator() {
1901 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001902 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001903 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001904
Richard Smithee390432014-05-16 01:56:53 +00001905 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001906}