blob: 556fbf337b963f92a53dc1f39a93b80f281d5c88 [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))
211 ConsumeToken();
212 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Richard Smithee390432014-05-16 01:56:53 +0000213 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000214 ConsumeToken();
215 break;
216
217 case tok::annot_cxxscope:
218 ConsumeToken();
219 // Fall through.
220 default:
221 ConsumeToken();
222
223 if (getLangOpts().ObjC1 && Tok.is(tok::less))
224 return TryParseProtocolQualifiers();
225 break;
226 }
227
Richard Smithee390432014-05-16 01:56:53 +0000228 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000229}
230
Guy Benyei11169dd2012-12-18 14:30:41 +0000231/// simple-declaration:
232/// decl-specifier-seq init-declarator-list[opt] ';'
233///
234/// (if AllowForRangeDecl specified)
235/// for ( for-range-declaration : for-range-initializer ) statement
236/// for-range-declaration:
237/// attribute-specifier-seqopt type-specifier-seq declarator
238///
239Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000240 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
241 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000242
243 // Two decl-specifiers in a row conclusively disambiguate this as being a
244 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
245 // overwhelmingly common case that the next token is a '('.
246 if (Tok.isNot(tok::l_paren)) {
247 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000248 if (TPR == TPResult::Ambiguous)
249 return TPResult::True;
250 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000251 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000252 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000253 }
254
255 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000256 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000257 return TPR;
258
259 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000260 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
Richard Smithee390432014-05-16 01:56:53 +0000262 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000263}
264
Richard Smith22c7c412013-03-20 03:35:02 +0000265/// Tentatively parse an init-declarator-list in order to disambiguate it from
266/// an expression.
267///
Guy Benyei11169dd2012-12-18 14:30:41 +0000268/// init-declarator-list:
269/// init-declarator
270/// init-declarator-list ',' init-declarator
271///
272/// init-declarator:
273/// declarator initializer[opt]
274/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
275///
Richard Smith22c7c412013-03-20 03:35:02 +0000276/// initializer:
277/// brace-or-equal-initializer
278/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000279///
Richard Smith22c7c412013-03-20 03:35:02 +0000280/// brace-or-equal-initializer:
281/// '=' initializer-clause
282/// [C++11] braced-init-list
283///
284/// initializer-clause:
285/// assignment-expression
286/// braced-init-list
287///
288/// braced-init-list:
289/// '{' initializer-list ','[opt] '}'
290/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000291///
292Parser::TPResult Parser::TryParseInitDeclaratorList() {
293 while (1) {
294 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000295 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000296 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000297 return TPR;
298
299 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000300 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000301 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000302
303 // initializer[opt]
304 if (Tok.is(tok::l_paren)) {
305 // Parse through the parens.
306 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000307 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000308 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000309 } else if (Tok.is(tok::l_brace)) {
310 // A left-brace here is sufficient to disambiguate the parse; an
311 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000312 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000313 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000314 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000315 // encountered; they just conclude that we have a declaration.
316 // EDG parses the initializer completely, which is the proper behavior
317 // for this case.
318 //
319 // At present, Clang follows MSVC and g++, since the parser does not have
320 // the ability to parse an expression fully without recording the
321 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000322 // FIXME: Handle this case correctly.
323 //
324 // Also allow 'in' after an Objective-C declaration as in:
325 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000326 // context of parsing for-init-statement of a foreach statement only. But,
327 // in any other context 'in' is invalid after a declaration and parser
328 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000329 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000330 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000331 }
332
Alp Toker97650562014-01-10 11:19:30 +0000333 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000334 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000335 }
336
Richard Smithee390432014-05-16 01:56:53 +0000337 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000338}
339
Richard Smithc7a05a92016-06-29 21:17:59 +0000340struct Parser::ConditionDeclarationOrInitStatementState {
341 Parser &P;
342 bool CanBeExpression = true;
343 bool CanBeCondition = true;
344 bool CanBeInitStatement;
345
346 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement)
347 : P(P), CanBeInitStatement(CanBeInitStatement) {}
348
349 void markNotExpression() {
350 CanBeExpression = false;
351
352 if (CanBeCondition && CanBeInitStatement) {
353 // FIXME: Unify the parsing codepaths for condition variables and
354 // simple-declarations so that we don't need to eagerly figure out which
355 // kind we have here. (Just parse init-declarators until we reach a
356 // semicolon or right paren.)
357 RevertingTentativeParsingAction PA(P);
358 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
359 if (P.Tok.isNot(tok::r_paren))
360 CanBeCondition = false;
361 if (P.Tok.isNot(tok::semi))
362 CanBeInitStatement = false;
363 }
364 }
365
366 bool markNotCondition() {
367 CanBeCondition = false;
368 return !CanBeInitStatement || !CanBeExpression;
369 }
370
371 bool update(TPResult IsDecl) {
372 switch (IsDecl) {
373 case TPResult::True:
374 markNotExpression();
375 return true;
376 case TPResult::False:
377 CanBeCondition = CanBeInitStatement = false;
378 return true;
379 case TPResult::Ambiguous:
380 return false;
381 case TPResult::Error:
382 CanBeExpression = CanBeCondition = CanBeInitStatement = false;
383 return true;
384 }
385 llvm_unreachable("unknown tentative parse result");
386 }
387
388 ConditionOrInitStatement result() const {
389 assert(CanBeExpression + CanBeCondition + CanBeInitStatement < 2 &&
390 "result called but not yet resolved");
391 if (CanBeExpression)
392 return ConditionOrInitStatement::Expression;
393 if (CanBeCondition)
394 return ConditionOrInitStatement::ConditionDecl;
395 if (CanBeInitStatement)
396 return ConditionOrInitStatement::InitStmtDecl;
397 return ConditionOrInitStatement::Error;
398 }
399};
400
401/// \brief Disambiguates between a declaration in a condition, a
402/// simple-declaration in an init-statement, and an expression for
403/// a condition of a if/switch statement.
Guy Benyei11169dd2012-12-18 14:30:41 +0000404///
405/// condition:
406/// expression
407/// type-specifier-seq declarator '=' assignment-expression
408/// [C++11] type-specifier-seq declarator '=' initializer-clause
409/// [C++11] type-specifier-seq declarator braced-init-list
410/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
411/// '=' assignment-expression
Richard Smithc7a05a92016-06-29 21:17:59 +0000412/// simple-declaration:
413/// decl-specifier-seq init-declarator-list[opt] ';'
Guy Benyei11169dd2012-12-18 14:30:41 +0000414///
Richard Smithc7a05a92016-06-29 21:17:59 +0000415/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
416/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
417/// 'int(x);' (a simple-declaration in an init-statement).
418Parser::ConditionOrInitStatement
419Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement) {
420 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement);
Guy Benyei11169dd2012-12-18 14:30:41 +0000421
Richard Smithc7a05a92016-06-29 21:17:59 +0000422 if (State.update(isCXXDeclarationSpecifier()))
423 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000424
Richard Smithc7a05a92016-06-29 21:17:59 +0000425 // It might be a declaration; we need tentative parsing.
Richard Smith91b73f22016-06-29 21:06:51 +0000426 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000427
Richard Smithc7a05a92016-06-29 21:17:59 +0000428 // FIXME: A tag definition unambiguously tells us this is an init-statement.
429 if (State.update(TryConsumeDeclarationSpecifier()))
430 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 assert(Tok.is(tok::l_paren) && "Expected '('");
432
Richard Smithc7a05a92016-06-29 21:17:59 +0000433 while (true) {
434 // Consume a declarator.
435 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
436 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000437
Richard Smithc7a05a92016-06-29 21:17:59 +0000438 // Attributes, asm label, or an initializer imply this is not an expression.
439 // FIXME: Disambiguate properly after an = instead of assuming that it's a
440 // valid declaration.
441 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
442 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
443 State.markNotExpression();
444 return State.result();
445 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000446
Richard Smithc7a05a92016-06-29 21:17:59 +0000447 // At this point, it can't be a condition any more, because a condition
448 // must have a brace-or-equal-initializer.
449 if (State.markNotCondition())
450 return State.result();
451
452 // A parenthesized initializer could be part of an expression or a
453 // simple-declaration.
454 if (Tok.is(tok::l_paren)) {
455 ConsumeParen();
456 SkipUntil(tok::r_paren, StopAtSemi);
457 }
458
459 if (!TryConsumeToken(tok::comma))
460 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000461 }
462
Richard Smithc7a05a92016-06-29 21:17:59 +0000463 // We reached the end. If it can now be some kind of decl, then it is.
464 if (State.CanBeCondition && Tok.is(tok::r_paren))
465 return ConditionOrInitStatement::ConditionDecl;
466 else if (State.CanBeInitStatement && Tok.is(tok::semi))
467 return ConditionOrInitStatement::InitStmtDecl;
468 else
469 return ConditionOrInitStatement::Expression;
Guy Benyei11169dd2012-12-18 14:30:41 +0000470}
471
472 /// \brief Determine whether the next set of tokens contains a type-id.
473 ///
474 /// The context parameter states what context we're parsing right
475 /// now, which affects how this routine copes with the token
476 /// following the type-id. If the context is TypeIdInParens, we have
477 /// already parsed the '(' and we will cease lookahead when we hit
478 /// the corresponding ')'. If the context is
479 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
480 /// before this template argument, and will cease lookahead when we
481 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
482 /// and false for an expression. If during the disambiguation
483 /// process a parsing error is encountered, the function returns
484 /// true to let the declaration parsing code handle it.
485 ///
486 /// type-id:
487 /// type-specifier-seq abstract-declarator[opt]
488 ///
489bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
490
491 isAmbiguous = false;
492
493 // C++ 8.2p2:
494 // The ambiguity arising from the similarity between a function-style cast and
495 // a type-id can occur in different contexts. The ambiguity appears as a
496 // choice between a function-style cast expression and a declaration of a
497 // type. The resolution is that any construct that could possibly be a type-id
498 // in its syntactic context shall be considered a type-id.
499
500 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000501 if (TPR != TPResult::Ambiguous)
502 return TPR != TPResult::False; // Returns true for TPResult::True or
503 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000504
505 // FIXME: Add statistics about the number of ambiguous statements encountered
506 // and how they were resolved (number of declarations+number of expressions).
507
508 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
509 // We need tentative parsing...
510
Richard Smith91b73f22016-06-29 21:06:51 +0000511 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000512
513 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000514 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000515 assert(Tok.is(tok::l_paren) && "Expected '('");
516
517 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000518 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000519
520 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000521 if (TPR == TPResult::Error)
522 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000523
Richard Smithee390432014-05-16 01:56:53 +0000524 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 // We are supposed to be inside parens, so if after the abstract declarator
526 // we encounter a ')' this is a type-id, otherwise it's an expression.
527 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000528 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 isAmbiguous = true;
530
531 // We are supposed to be inside a template argument, so if after
532 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
533 // ',', this is a type-id. Otherwise, it's an expression.
534 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000535 (Tok.isOneOf(tok::greater, tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000536 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000537 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000538 isAmbiguous = true;
539
540 } else
Richard Smithee390432014-05-16 01:56:53 +0000541 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 }
543
Richard Smithee390432014-05-16 01:56:53 +0000544 assert(TPR == TPResult::True || TPR == TPResult::False);
545 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000546}
547
548/// \brief Returns true if this is a C++11 attribute-specifier. Per
549/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
550/// always introduce an attribute. In Objective-C++11, this rule does not
551/// apply if either '[' begins a message-send.
552///
553/// If Disambiguate is true, we try harder to determine whether a '[[' starts
554/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
555///
556/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
557/// Obj-C message send or the start of an attribute. Otherwise, we assume it
558/// is not an Obj-C message send.
559///
560/// C++11 [dcl.attr.grammar]:
561///
562/// attribute-specifier:
563/// '[' '[' attribute-list ']' ']'
564/// alignment-specifier
565///
566/// attribute-list:
567/// attribute[opt]
568/// attribute-list ',' attribute[opt]
569/// attribute '...'
570/// attribute-list ',' attribute '...'
571///
572/// attribute:
573/// attribute-token attribute-argument-clause[opt]
574///
575/// attribute-token:
576/// identifier
577/// identifier '::' identifier
578///
579/// attribute-argument-clause:
580/// '(' balanced-token-seq ')'
581Parser::CXX11AttributeKind
582Parser::isCXX11AttributeSpecifier(bool Disambiguate,
583 bool OuterMightBeMessageSend) {
584 if (Tok.is(tok::kw_alignas))
585 return CAK_AttributeSpecifier;
586
587 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
588 return CAK_NotAttributeSpecifier;
589
590 // No tentative parsing if we don't need to look for ']]' or a lambda.
591 if (!Disambiguate && !getLangOpts().ObjC1)
592 return CAK_AttributeSpecifier;
593
Richard Smith91b73f22016-06-29 21:06:51 +0000594 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000595
596 // Opening brackets were checked for above.
597 ConsumeBracket();
598
599 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
600 if (!getLangOpts().ObjC1) {
601 ConsumeBracket();
602
Alexey Bataevee6507d2013-11-18 08:17:37 +0000603 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604 IsAttribute &= Tok.is(tok::r_square);
605
Guy Benyei11169dd2012-12-18 14:30:41 +0000606 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
607 }
608
609 // In Obj-C++11, we need to distinguish four situations:
610 // 1a) int x[[attr]]; C++11 attribute.
611 // 1b) [[attr]]; C++11 statement attribute.
612 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
613 // 3a) int x[[obj get]]; Message send in array size/index.
614 // 3b) [[Class alloc] init]; Message send in message send.
615 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
616 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
617
618 // If we have a lambda-introducer, then this is definitely not a message send.
619 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
620 // into the tentative attribute parse below.
621 LambdaIntroducer Intro;
622 if (!TryParseLambdaIntroducer(Intro)) {
623 // A lambda cannot end with ']]', and an attribute must.
624 bool IsAttribute = Tok.is(tok::r_square);
625
Guy Benyei11169dd2012-12-18 14:30:41 +0000626 if (IsAttribute)
627 // Case 1: C++11 attribute.
628 return CAK_AttributeSpecifier;
629
630 if (OuterMightBeMessageSend)
631 // Case 4: Lambda in message send.
632 return CAK_NotAttributeSpecifier;
633
634 // Case 2: Lambda in array size / index.
635 return CAK_InvalidAttributeSpecifier;
636 }
637
638 ConsumeBracket();
639
640 // If we don't have a lambda-introducer, then we have an attribute or a
641 // message-send.
642 bool IsAttribute = true;
643 while (Tok.isNot(tok::r_square)) {
644 if (Tok.is(tok::comma)) {
645 // Case 1: Stray commas can only occur in attributes.
Guy Benyei11169dd2012-12-18 14:30:41 +0000646 return CAK_AttributeSpecifier;
647 }
648
649 // Parse the attribute-token, if present.
650 // C++11 [dcl.attr.grammar]:
651 // If a keyword or an alternative token that satisfies the syntactic
652 // requirements of an identifier is contained in an attribute-token,
653 // it is considered an identifier.
654 SourceLocation Loc;
655 if (!TryParseCXX11AttributeIdentifier(Loc)) {
656 IsAttribute = false;
657 break;
658 }
659 if (Tok.is(tok::coloncolon)) {
660 ConsumeToken();
661 if (!TryParseCXX11AttributeIdentifier(Loc)) {
662 IsAttribute = false;
663 break;
664 }
665 }
666
667 // Parse the attribute-argument-clause, if present.
668 if (Tok.is(tok::l_paren)) {
669 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000670 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000671 IsAttribute = false;
672 break;
673 }
674 }
675
Alp Toker97650562014-01-10 11:19:30 +0000676 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000677
Alp Toker97650562014-01-10 11:19:30 +0000678 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000680 }
681
682 // An attribute must end ']]'.
683 if (IsAttribute) {
684 if (Tok.is(tok::r_square)) {
685 ConsumeBracket();
686 IsAttribute = Tok.is(tok::r_square);
687 } else {
688 IsAttribute = false;
689 }
690 }
691
Guy Benyei11169dd2012-12-18 14:30:41 +0000692 if (IsAttribute)
693 // Case 1: C++11 statement attribute.
694 return CAK_AttributeSpecifier;
695
696 // Case 3: Message send.
697 return CAK_NotAttributeSpecifier;
698}
699
Richard Smith1fff95c2013-09-12 23:28:08 +0000700Parser::TPResult Parser::TryParsePtrOperatorSeq() {
701 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000702 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000703 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000704 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000705
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000706 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000707 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
708 // ptr-operator
709 ConsumeToken();
Douglas Gregor261a89b2015-06-19 17:51:05 +0000710 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000711 tok::kw__Nonnull, tok::kw__Nullable,
712 tok::kw__Null_unspecified))
Richard Smith1fff95c2013-09-12 23:28:08 +0000713 ConsumeToken();
714 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000715 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000716 }
717 }
718}
719
720/// operator-function-id:
721/// 'operator' operator
722///
723/// operator: one of
724/// new delete new[] delete[] + - * / % ^ [...]
725///
726/// conversion-function-id:
727/// 'operator' conversion-type-id
728///
729/// conversion-type-id:
730/// type-specifier-seq conversion-declarator[opt]
731///
732/// conversion-declarator:
733/// ptr-operator conversion-declarator[opt]
734///
735/// literal-operator-id:
736/// 'operator' string-literal identifier
737/// 'operator' user-defined-string-literal
738Parser::TPResult Parser::TryParseOperatorId() {
739 assert(Tok.is(tok::kw_operator));
740 ConsumeToken();
741
742 // Maybe this is an operator-function-id.
743 switch (Tok.getKind()) {
744 case tok::kw_new: case tok::kw_delete:
745 ConsumeToken();
746 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
747 ConsumeBracket();
748 ConsumeBracket();
749 }
Richard Smithee390432014-05-16 01:56:53 +0000750 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000751
752#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
753 case tok::Token:
754#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
755#include "clang/Basic/OperatorKinds.def"
756 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000757 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000758
759 case tok::l_square:
760 if (NextToken().is(tok::r_square)) {
761 ConsumeBracket();
762 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000763 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000764 }
765 break;
766
767 case tok::l_paren:
768 if (NextToken().is(tok::r_paren)) {
769 ConsumeParen();
770 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000771 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000772 }
773 break;
774
775 default:
776 break;
777 }
778
779 // Maybe this is a literal-operator-id.
780 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
781 bool FoundUDSuffix = false;
782 do {
783 FoundUDSuffix |= Tok.hasUDSuffix();
784 ConsumeStringToken();
785 } while (isTokenStringLiteral());
786
787 if (!FoundUDSuffix) {
788 if (Tok.is(tok::identifier))
789 ConsumeToken();
790 else
Richard Smithee390432014-05-16 01:56:53 +0000791 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000792 }
Richard Smithee390432014-05-16 01:56:53 +0000793 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000794 }
795
796 // Maybe this is a conversion-function-id.
797 bool AnyDeclSpecifiers = false;
798 while (true) {
799 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000800 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000801 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000802 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000803 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000804 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000805 break;
806 }
Richard Smithee390432014-05-16 01:56:53 +0000807 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
808 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000809 AnyDeclSpecifiers = true;
810 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000811 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000812}
813
Guy Benyei11169dd2012-12-18 14:30:41 +0000814/// declarator:
815/// direct-declarator
816/// ptr-operator declarator
817///
818/// direct-declarator:
819/// declarator-id
820/// direct-declarator '(' parameter-declaration-clause ')'
821/// cv-qualifier-seq[opt] exception-specification[opt]
822/// direct-declarator '[' constant-expression[opt] ']'
823/// '(' declarator ')'
824/// [GNU] '(' attributes declarator ')'
825///
826/// abstract-declarator:
827/// ptr-operator abstract-declarator[opt]
828/// direct-abstract-declarator
829/// ...
830///
831/// direct-abstract-declarator:
832/// direct-abstract-declarator[opt]
833/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
834/// exception-specification[opt]
835/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
836/// '(' abstract-declarator ')'
837///
838/// ptr-operator:
839/// '*' cv-qualifier-seq[opt]
840/// '&'
841/// [C++0x] '&&' [TODO]
842/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
843///
844/// cv-qualifier-seq:
845/// cv-qualifier cv-qualifier-seq[opt]
846///
847/// cv-qualifier:
848/// 'const'
849/// 'volatile'
850///
851/// declarator-id:
852/// '...'[opt] id-expression
853///
854/// id-expression:
855/// unqualified-id
856/// qualified-id [TODO]
857///
858/// unqualified-id:
859/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000860/// operator-function-id
861/// conversion-function-id
862/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000863/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000864/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000865/// template-id [TODO]
866///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000867Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
868 bool mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000869 // declarator:
870 // direct-declarator
871 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000872 if (TryParsePtrOperatorSeq() == TPResult::Error)
873 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000874
875 // direct-declarator:
876 // direct-abstract-declarator:
877 if (Tok.is(tok::ellipsis))
878 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000879
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000880 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000881 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
882 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000883 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000884 // declarator-id
885 if (Tok.is(tok::annot_cxxscope))
886 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000887 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000888 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000889 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000890 if (TryParseOperatorId() == TPResult::Error)
891 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000892 } else
893 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000894 } else if (Tok.is(tok::l_paren)) {
895 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000896 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000897 (Tok.is(tok::r_paren) || // 'int()' is a function.
898 // 'int(...)' is a function.
899 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
900 isDeclarationSpecifier())) { // 'int(int)' is a function.
901 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
902 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000903 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000904 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000905 return TPR;
906 } else {
907 // '(' declarator ')'
908 // '(' attributes declarator ')'
909 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000910 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
911 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000912 tok::kw___vectorcall))
Richard Smithee390432014-05-16 01:56:53 +0000913 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000914 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000915 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000916 return TPR;
917 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000918 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000919 ConsumeParen();
920 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000921 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000922 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000923 }
924
925 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000926 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000927
928 // abstract-declarator: ...
929 if (Tok.is(tok::ellipsis))
930 ConsumeToken();
931
932 if (Tok.is(tok::l_paren)) {
933 // Check whether we have a function declarator or a possible ctor-style
934 // initializer that follows the declarator. Note that ctor-style
935 // initializers are not possible in contexts where abstract declarators
936 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000937 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000938 break;
939
940 // direct-declarator '(' parameter-declaration-clause ')'
941 // cv-qualifier-seq[opt] exception-specification[opt]
942 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000943 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +0000944 } else if (Tok.is(tok::l_square)) {
945 // direct-declarator '[' constant-expression[opt] ']'
946 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
947 TPR = TryParseBracketDeclarator();
948 } else {
949 break;
950 }
951
Richard Smithee390432014-05-16 01:56:53 +0000952 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000953 return TPR;
954 }
955
Richard Smithee390432014-05-16 01:56:53 +0000956 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000957}
958
959Parser::TPResult
960Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
961 switch (Kind) {
962 // Obviously starts an expression.
963 case tok::numeric_constant:
964 case tok::char_constant:
965 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000966 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000967 case tok::utf16_char_constant:
968 case tok::utf32_char_constant:
969 case tok::string_literal:
970 case tok::wide_string_literal:
971 case tok::utf8_string_literal:
972 case tok::utf16_string_literal:
973 case tok::utf32_string_literal:
974 case tok::l_square:
975 case tok::l_paren:
976 case tok::amp:
977 case tok::ampamp:
978 case tok::star:
979 case tok::plus:
980 case tok::plusplus:
981 case tok::minus:
982 case tok::minusminus:
983 case tok::tilde:
984 case tok::exclaim:
985 case tok::kw_sizeof:
986 case tok::kw___func__:
987 case tok::kw_const_cast:
988 case tok::kw_delete:
989 case tok::kw_dynamic_cast:
990 case tok::kw_false:
991 case tok::kw_new:
992 case tok::kw_operator:
993 case tok::kw_reinterpret_cast:
994 case tok::kw_static_cast:
995 case tok::kw_this:
996 case tok::kw_throw:
997 case tok::kw_true:
998 case tok::kw_typeid:
999 case tok::kw_alignof:
1000 case tok::kw_noexcept:
1001 case tok::kw_nullptr:
1002 case tok::kw__Alignof:
1003 case tok::kw___null:
1004 case tok::kw___alignof:
1005 case tok::kw___builtin_choose_expr:
1006 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +00001007 case tok::kw___builtin_va_arg:
1008 case tok::kw___imag:
1009 case tok::kw___real:
1010 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +00001011 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +00001012 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001013 case tok::kw_L__FUNCTION__:
1014 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001015 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +00001016#define TYPE_TRAIT(N,Spelling,K) \
1017 case tok::kw_##Spelling:
1018#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +00001019 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001020
1021 // Obviously starts a type-specifier-seq:
1022 case tok::kw_char:
1023 case tok::kw_const:
1024 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001025 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001026 case tok::kw_enum:
1027 case tok::kw_half:
1028 case tok::kw_float:
1029 case tok::kw_int:
1030 case tok::kw_long:
1031 case tok::kw___int64:
1032 case tok::kw___int128:
1033 case tok::kw_restrict:
1034 case tok::kw_short:
1035 case tok::kw_signed:
1036 case tok::kw_struct:
1037 case tok::kw_union:
1038 case tok::kw_unsigned:
1039 case tok::kw_void:
1040 case tok::kw_volatile:
1041 case tok::kw__Bool:
1042 case tok::kw__Complex:
1043 case tok::kw_class:
1044 case tok::kw_typename:
1045 case tok::kw_wchar_t:
1046 case tok::kw_char16_t:
1047 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001048 case tok::kw__Decimal32:
1049 case tok::kw__Decimal64:
1050 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001051 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001052 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001053 case tok::kw_thread_local:
1054 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +00001056 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +00001057 case tok::kw___cdecl:
1058 case tok::kw___stdcall:
1059 case tok::kw___fastcall:
1060 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001061 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 case tok::kw___unaligned:
1063 case tok::kw___vector:
1064 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +00001065 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +00001067#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00001068#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00001069 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +00001070 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001071
1072 default:
1073 break;
1074 }
1075
Richard Smithee390432014-05-16 01:56:53 +00001076 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001077}
1078
1079bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1080 return std::find(TentativelyDeclaredIdentifiers.begin(),
1081 TentativelyDeclaredIdentifiers.end(), II)
1082 != TentativelyDeclaredIdentifiers.end();
1083}
1084
Kaelyn Takata445b0652014-11-05 00:09:29 +00001085namespace {
1086class TentativeParseCCC : public CorrectionCandidateCallback {
1087public:
1088 TentativeParseCCC(const Token &Next) {
1089 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001090 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1091 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001092 }
1093
1094 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1095 // Reject any candidate that only resolves to instance members since they
1096 // aren't viable as standalone identifiers instead of member references.
1097 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1098 std::all_of(Candidate.begin(), Candidate.end(),
1099 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1100 return false;
1101
1102 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1103 }
1104};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001105}
Richard Smithee390432014-05-16 01:56:53 +00001106/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1107/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1108/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001109/// if a parsing error was found and reported.
1110///
1111/// If HasMissingTypename is provided, a name with a dependent scope specifier
1112/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1113/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1114/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001115/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001116/// such cases.
1117///
1118/// decl-specifier:
1119/// storage-class-specifier
1120/// type-specifier
1121/// function-specifier
1122/// 'friend'
1123/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001124/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001125/// [GNU] attributes declaration-specifiers[opt]
1126///
1127/// storage-class-specifier:
1128/// 'register'
1129/// 'static'
1130/// 'extern'
1131/// 'mutable'
1132/// 'auto'
1133/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001134/// [C++11] 'thread_local'
1135/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001136///
1137/// function-specifier:
1138/// 'inline'
1139/// 'virtual'
1140/// 'explicit'
1141///
1142/// typedef-name:
1143/// identifier
1144///
1145/// type-specifier:
1146/// simple-type-specifier
1147/// class-specifier
1148/// enum-specifier
1149/// elaborated-type-specifier
1150/// typename-specifier
1151/// cv-qualifier
1152///
1153/// simple-type-specifier:
1154/// '::'[opt] nested-name-specifier[opt] type-name
1155/// '::'[opt] nested-name-specifier 'template'
1156/// simple-template-id [TODO]
1157/// 'char'
1158/// 'wchar_t'
1159/// 'bool'
1160/// 'short'
1161/// 'int'
1162/// 'long'
1163/// 'signed'
1164/// 'unsigned'
1165/// 'float'
1166/// 'double'
1167/// 'void'
1168/// [GNU] typeof-specifier
1169/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001170/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001171/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001172/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001173/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001174///
1175/// type-name:
1176/// class-name
1177/// enum-name
1178/// typedef-name
1179///
1180/// elaborated-type-specifier:
1181/// class-key '::'[opt] nested-name-specifier[opt] identifier
1182/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1183/// simple-template-id
1184/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1185///
1186/// enum-name:
1187/// identifier
1188///
1189/// enum-specifier:
1190/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1191/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1192///
1193/// class-specifier:
1194/// class-head '{' member-specification[opt] '}'
1195///
1196/// class-head:
1197/// class-key identifier[opt] base-clause[opt]
1198/// class-key nested-name-specifier identifier base-clause[opt]
1199/// class-key nested-name-specifier[opt] simple-template-id
1200/// base-clause[opt]
1201///
1202/// class-key:
1203/// 'class'
1204/// 'struct'
1205/// 'union'
1206///
1207/// cv-qualifier:
1208/// 'const'
1209/// 'volatile'
1210/// [GNU] restrict
1211///
1212Parser::TPResult
1213Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1214 bool *HasMissingTypename) {
1215 switch (Tok.getKind()) {
1216 case tok::identifier: {
1217 // Check for need to substitute AltiVec __vector keyword
1218 // for "vector" identifier.
1219 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001220 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001221
1222 const Token &Next = NextToken();
1223 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1224 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001225 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001226
1227 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1228 // Determine whether this is a valid expression. If not, we will hit
1229 // a parse error one way or another. In that case, tell the caller that
1230 // this is ambiguous. Typo-correct to type and expression keywords and
1231 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001233 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001234 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001235 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001236 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001237 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 case ANK_TemplateName:
1239 // A bare type template-name which can't be a template template
1240 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001241 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001243 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 case ANK_Success:
1245 break;
1246 }
1247 assert(Tok.isNot(tok::identifier) &&
1248 "TryAnnotateName succeeded without producing an annotation");
1249 } else {
1250 // This might possibly be a type with a dependent scope specifier and
1251 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1252 // since it will annotate as a primary expression, and we want to use the
1253 // "missing 'typename'" logic.
1254 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001255 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001256 // If annotation failed, assume it's a non-type.
1257 // FIXME: If this happens due to an undeclared identifier, treat it as
1258 // ambiguous.
1259 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001260 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001261 }
1262
1263 // We annotated this token as something. Recurse to handle whatever we got.
1264 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1265 }
1266
1267 case tok::kw_typename: // typename T::type
1268 // Annotate typenames and C++ scope specifiers. If we get one, just
1269 // recurse to handle whatever we get.
1270 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001271 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001272 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1273
1274 case tok::coloncolon: { // ::foo::bar
1275 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001276 if (Next.isOneOf(tok::kw_new, // ::new
1277 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001278 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 }
1280 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001281 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001282 case tok::kw_decltype:
1283 // Annotate typenames and C++ scope specifiers. If we get one, just
1284 // recurse to handle whatever we get.
1285 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001286 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001287 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1288
1289 // decl-specifier:
1290 // storage-class-specifier
1291 // type-specifier
1292 // function-specifier
1293 // 'friend'
1294 // 'typedef'
1295 // 'constexpr'
Hubert Tong375f00a2015-06-30 12:14:52 +00001296 // 'concept'
Guy Benyei11169dd2012-12-18 14:30:41 +00001297 case tok::kw_friend:
1298 case tok::kw_typedef:
1299 case tok::kw_constexpr:
Hubert Tong375f00a2015-06-30 12:14:52 +00001300 case tok::kw_concept:
Guy Benyei11169dd2012-12-18 14:30:41 +00001301 // storage-class-specifier
1302 case tok::kw_register:
1303 case tok::kw_static:
1304 case tok::kw_extern:
1305 case tok::kw_mutable:
1306 case tok::kw_auto:
1307 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001308 case tok::kw_thread_local:
1309 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 // function-specifier
1311 case tok::kw_inline:
1312 case tok::kw_virtual:
1313 case tok::kw_explicit:
1314
1315 // Modules
1316 case tok::kw___module_private__:
1317
1318 // Debugger support
1319 case tok::kw___unknown_anytype:
1320
1321 // type-specifier:
1322 // simple-type-specifier
1323 // class-specifier
1324 // enum-specifier
1325 // elaborated-type-specifier
1326 // typename-specifier
1327 // cv-qualifier
1328
1329 // class-specifier
1330 // elaborated-type-specifier
1331 case tok::kw_class:
1332 case tok::kw_struct:
1333 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001334 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001335 // enum-specifier
1336 case tok::kw_enum:
1337 // cv-qualifier
1338 case tok::kw_const:
1339 case tok::kw_volatile:
1340
1341 // GNU
1342 case tok::kw_restrict:
1343 case tok::kw__Complex:
1344 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001345 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001346 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001347
1348 // Microsoft
1349 case tok::kw___declspec:
1350 case tok::kw___cdecl:
1351 case tok::kw___stdcall:
1352 case tok::kw___fastcall:
1353 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001354 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001356 case tok::kw___sptr:
1357 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001358 case tok::kw___ptr64:
1359 case tok::kw___ptr32:
1360 case tok::kw___forceinline:
1361 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001362 case tok::kw__Nonnull:
1363 case tok::kw__Nullable:
1364 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001365 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001366 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001367
1368 // Borland
1369 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001370 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001371
1372 // AltiVec
1373 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001374 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001375
1376 case tok::annot_template_id: {
1377 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1378 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001379 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 CXXScopeSpec SS;
1381 AnnotateTemplateIdTokenAsType();
1382 assert(Tok.is(tok::annot_typename));
1383 goto case_typename;
1384 }
1385
1386 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1387 // We've already annotated a scope; try to annotate a type.
1388 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001389 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001390 if (!Tok.is(tok::annot_typename)) {
1391 // If the next token is an identifier or a type qualifier, then this
1392 // can't possibly be a valid expression either.
1393 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1394 CXXScopeSpec SS;
1395 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1396 Tok.getAnnotationRange(),
1397 SS);
1398 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
Richard Smith4556ebe2016-06-29 21:12:37 +00001399 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 ConsumeToken();
1401 ConsumeToken();
1402 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001403 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 if (!isIdentifier)
1405 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1406 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001407
1408 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001409 TPR == TPResult::True || TPR == TPResult::Error)
1410 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001411
1412 if (HasMissingTypename) {
1413 // We can't tell whether this is a missing 'typename' or a valid
1414 // expression.
1415 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001416 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 }
Richard Smithc7a05a92016-06-29 21:17:59 +00001418
1419 // FIXME: Fails to either revert or commit the tentative parse!
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 } else {
1421 // Try to resolve the name. If it doesn't exist, assume it was
1422 // intended to name a type and keep disambiguating.
1423 switch (TryAnnotateName(false /* SS is not dependent */)) {
1424 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001425 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001427 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001428 case ANK_TemplateName:
1429 // A bare type template-name which can't be a template template
1430 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001431 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001433 return HasMissingTypename ? TPResult::Ambiguous
1434 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001435 case ANK_Success:
1436 // Annotated it, check again.
1437 assert(Tok.isNot(tok::annot_cxxscope) ||
1438 NextToken().isNot(tok::identifier));
1439 return isCXXDeclarationSpecifier(BracedCastResult,
1440 HasMissingTypename);
1441 }
1442 }
1443 }
Richard Smithee390432014-05-16 01:56:53 +00001444 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 }
1446 // If that succeeded, fallthrough into the generic simple-type-id case.
1447
1448 // The ambiguity resides in a simple-type-specifier/typename-specifier
1449 // followed by a '('. The '(' could either be the start of:
1450 //
1451 // direct-declarator:
1452 // '(' declarator ')'
1453 //
1454 // direct-abstract-declarator:
1455 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1456 // exception-specification[opt]
1457 // '(' abstract-declarator ')'
1458 //
1459 // or part of a function-style cast expression:
1460 //
1461 // simple-type-specifier '(' expression-list[opt] ')'
1462 //
1463
1464 // simple-type-specifier:
1465
1466 case tok::annot_typename:
1467 case_typename:
1468 // In Objective-C, we might have a protocol-qualified type.
1469 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001470 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001471 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001472 ConsumeToken(); // The type token
1473
1474 TPResult TPR = TryParseProtocolQualifiers();
1475 bool isFollowedByParen = Tok.is(tok::l_paren);
1476 bool isFollowedByBrace = Tok.is(tok::l_brace);
1477
Richard Smithee390432014-05-16 01:56:53 +00001478 if (TPR == TPResult::Error)
1479 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001480
1481 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001482 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001483
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001484 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001485 return BracedCastResult;
1486
Richard Smithee390432014-05-16 01:56:53 +00001487 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001488 }
1489
1490 case tok::kw_char:
1491 case tok::kw_wchar_t:
1492 case tok::kw_char16_t:
1493 case tok::kw_char32_t:
1494 case tok::kw_bool:
1495 case tok::kw_short:
1496 case tok::kw_int:
1497 case tok::kw_long:
1498 case tok::kw___int64:
1499 case tok::kw___int128:
1500 case tok::kw_signed:
1501 case tok::kw_unsigned:
1502 case tok::kw_half:
1503 case tok::kw_float:
1504 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001505 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 case tok::kw_void:
1507 case tok::annot_decltype:
1508 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001509 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001510
1511 // This is a function-style cast in all cases we disambiguate other than
1512 // one:
1513 // struct S {
1514 // enum E : int { a = 4 }; // enum
1515 // enum E : int { 4 }; // bit-field
1516 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001517 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001518 return BracedCastResult;
1519
1520 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001521 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001522
Richard Smithee390432014-05-16 01:56:53 +00001523 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001524
1525 // GNU typeof support.
1526 case tok::kw_typeof: {
1527 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001528 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001529
Richard Smith91b73f22016-06-29 21:06:51 +00001530 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001531
1532 TPResult TPR = TryParseTypeofSpecifier();
1533 bool isFollowedByParen = Tok.is(tok::l_paren);
1534 bool isFollowedByBrace = Tok.is(tok::l_brace);
1535
Richard Smithee390432014-05-16 01:56:53 +00001536 if (TPR == TPResult::Error)
1537 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001538
1539 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001540 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001541
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001542 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 return BracedCastResult;
1544
Richard Smithee390432014-05-16 01:56:53 +00001545 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 }
1547
1548 // C++0x type traits support
1549 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001550 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001551
1552 // C11 _Atomic
1553 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001554 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001555
1556 default:
Richard Smithee390432014-05-16 01:56:53 +00001557 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001558 }
1559}
1560
Richard Smith1fff95c2013-09-12 23:28:08 +00001561bool Parser::isCXXDeclarationSpecifierAType() {
1562 switch (Tok.getKind()) {
1563 // typename-specifier
1564 case tok::annot_decltype:
1565 case tok::annot_template_id:
1566 case tok::annot_typename:
1567 case tok::kw_typeof:
1568 case tok::kw___underlying_type:
1569 return true;
1570
1571 // elaborated-type-specifier
1572 case tok::kw_class:
1573 case tok::kw_struct:
1574 case tok::kw_union:
1575 case tok::kw___interface:
1576 case tok::kw_enum:
1577 return true;
1578
1579 // simple-type-specifier
1580 case tok::kw_char:
1581 case tok::kw_wchar_t:
1582 case tok::kw_char16_t:
1583 case tok::kw_char32_t:
1584 case tok::kw_bool:
1585 case tok::kw_short:
1586 case tok::kw_int:
1587 case tok::kw_long:
1588 case tok::kw___int64:
1589 case tok::kw___int128:
1590 case tok::kw_signed:
1591 case tok::kw_unsigned:
1592 case tok::kw_half:
1593 case tok::kw_float:
1594 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001595 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001596 case tok::kw_void:
1597 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001598 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001599 return true;
1600
1601 case tok::kw_auto:
1602 return getLangOpts().CPlusPlus11;
1603
1604 case tok::kw__Atomic:
1605 // "_Atomic foo"
1606 return NextToken().is(tok::l_paren);
1607
1608 default:
1609 return false;
1610 }
1611}
1612
Guy Benyei11169dd2012-12-18 14:30:41 +00001613/// [GNU] typeof-specifier:
1614/// 'typeof' '(' expressions ')'
1615/// 'typeof' '(' type-name ')'
1616///
1617Parser::TPResult Parser::TryParseTypeofSpecifier() {
1618 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1619 ConsumeToken();
1620
1621 assert(Tok.is(tok::l_paren) && "Expected '('");
1622 // Parse through the parens after 'typeof'.
1623 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001624 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001625 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001626
Richard Smithee390432014-05-16 01:56:53 +00001627 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001628}
1629
1630/// [ObjC] protocol-qualifiers:
1631//// '<' identifier-list '>'
1632Parser::TPResult Parser::TryParseProtocolQualifiers() {
1633 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1634 ConsumeToken();
1635 do {
1636 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001637 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001638 ConsumeToken();
1639
1640 if (Tok.is(tok::comma)) {
1641 ConsumeToken();
1642 continue;
1643 }
1644
1645 if (Tok.is(tok::greater)) {
1646 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001647 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001648 }
1649 } while (false);
1650
Richard Smithee390432014-05-16 01:56:53 +00001651 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001652}
1653
Guy Benyei11169dd2012-12-18 14:30:41 +00001654/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1655/// a constructor-style initializer, when parsing declaration statements.
1656/// Returns true for function declarator and false for constructor-style
1657/// initializer.
1658/// If during the disambiguation process a parsing error is encountered,
1659/// the function returns true to let the declaration parsing code handle it.
1660///
1661/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1662/// exception-specification[opt]
1663///
1664bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1665
1666 // C++ 8.2p1:
1667 // The ambiguity arising from the similarity between a function-style cast and
1668 // a declaration mentioned in 6.8 can also occur in the context of a
1669 // declaration. In that context, the choice is between a function declaration
1670 // with a redundant set of parentheses around a parameter name and an object
1671 // declaration with a function-style cast as the initializer. Just as for the
1672 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1673 // that could possibly be a declaration a declaration.
1674
Richard Smith91b73f22016-06-29 21:06:51 +00001675 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001676
1677 ConsumeParen();
1678 bool InvalidAsDeclaration = false;
1679 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001680 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001681 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001682 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 else {
1684 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001685 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1686 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1687 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1688 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001689 // The next token cannot appear after a constructor-style initializer,
1690 // and can appear next in a function definition. This must be a function
1691 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001692 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001693 else if (InvalidAsDeclaration)
1694 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001695 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 }
1697 }
1698
Richard Smithee390432014-05-16 01:56:53 +00001699 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 *IsAmbiguous = true;
1701
1702 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001703 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001704}
1705
1706/// parameter-declaration-clause:
1707/// parameter-declaration-list[opt] '...'[opt]
1708/// parameter-declaration-list ',' '...'
1709///
1710/// parameter-declaration-list:
1711/// parameter-declaration
1712/// parameter-declaration-list ',' parameter-declaration
1713///
1714/// parameter-declaration:
1715/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1716/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1717/// '=' assignment-expression
1718/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1719/// attributes[opt]
1720/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1721/// attributes[opt] '=' assignment-expression
1722///
1723Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001724Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1725 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001726
1727 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001728 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001729
1730 // parameter-declaration-list[opt] '...'[opt]
1731 // parameter-declaration-list ',' '...'
1732 //
1733 // parameter-declaration-list:
1734 // parameter-declaration
1735 // parameter-declaration-list ',' parameter-declaration
1736 //
1737 while (1) {
1738 // '...'[opt]
1739 if (Tok.is(tok::ellipsis)) {
1740 ConsumeToken();
1741 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001742 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 else
Richard Smithee390432014-05-16 01:56:53 +00001744 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 }
1746
1747 // An attribute-specifier-seq here is a sign of a function declarator.
1748 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1749 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001750 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001751
1752 ParsedAttributes attrs(AttrFactory);
1753 MaybeParseMicrosoftAttributes(attrs);
1754
1755 // decl-specifier-seq
1756 // A parameter-declaration's initializer must be preceded by an '=', so
1757 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001758 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001759 InvalidAsDeclaration);
1760
Richard Smithee390432014-05-16 01:56:53 +00001761 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001762 // Consume the decl-specifier-seq. We have to look past it, since a
1763 // type-id might appear here in a template argument.
1764 bool SeenType = false;
1765 do {
1766 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001767 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1768 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001769
1770 // If we see a parameter name, this can't be a template argument.
1771 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001772 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001773
Richard Smithee390432014-05-16 01:56:53 +00001774 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001775 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001776 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001777 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001778 } while (TPR != TPResult::False);
1779 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001780 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001781 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1782 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001783 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001784 return TPR;
1785
1786 // declarator
1787 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001788 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001789 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 return TPR;
1791
1792 // [GNU] attributes[opt]
1793 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001794 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001795
Richard Smith1fff95c2013-09-12 23:28:08 +00001796 // If we're disambiguating a template argument in a default argument in
1797 // a class definition versus a parameter declaration, an '=' here
1798 // disambiguates the parse one way or the other.
1799 // If this is a parameter, it must have a default argument because
1800 // (a) the previous parameter did, and
1801 // (b) this must be the first declaration of the function, so we can't
1802 // inherit any default arguments from elsewhere.
1803 // If we see an ')', then we've reached the end of a
1804 // parameter-declaration-clause, and the last param is missing its default
1805 // argument.
1806 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001807 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1808 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001809
Guy Benyei11169dd2012-12-18 14:30:41 +00001810 if (Tok.is(tok::equal)) {
1811 // '=' assignment-expression
1812 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001813 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001814 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001815 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 }
1817
1818 if (Tok.is(tok::ellipsis)) {
1819 ConsumeToken();
1820 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001821 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 else
Richard Smithee390432014-05-16 01:56:53 +00001823 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001824 }
1825
Alp Toker97650562014-01-10 11:19:30 +00001826 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001827 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 }
1829
Richard Smithee390432014-05-16 01:56:53 +00001830 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001831}
1832
1833/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1834/// parsing as a function declarator.
1835/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001836/// return TPResult::Ambiguous, otherwise it will return either False() or
1837/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001838///
1839/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1840/// exception-specification[opt]
1841///
1842/// exception-specification:
1843/// 'throw' '(' type-id-list[opt] ')'
1844///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001845Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001846
1847 // The '(' is already parsed.
1848
1849 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001850 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1851 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001852
Justin Bognerd26f95b2015-02-23 22:36:28 +00001853 if (TPR == TPResult::False || TPR == TPResult::Error)
1854 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001855
1856 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001857 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001858 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001859
1860 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001861 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 ConsumeToken();
1863
1864 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001865 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001866 ConsumeToken();
1867
1868 // exception-specification
1869 if (Tok.is(tok::kw_throw)) {
1870 ConsumeToken();
1871 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001872 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001873
1874 // Parse through the parens after 'throw'.
1875 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001876 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001877 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 }
1879 if (Tok.is(tok::kw_noexcept)) {
1880 ConsumeToken();
1881 // Possibly an expression as well.
1882 if (Tok.is(tok::l_paren)) {
1883 // Find the matching rparen.
1884 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001885 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001886 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 }
1888 }
1889
Richard Smithee390432014-05-16 01:56:53 +00001890 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001891}
1892
1893/// '[' constant-expression[opt] ']'
1894///
1895Parser::TPResult Parser::TryParseBracketDeclarator() {
1896 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001897 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001898 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001899
Richard Smithee390432014-05-16 01:56:53 +00001900 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001901}