blob: 7703c33b87800f98002426ae04a58b38163d7f6c [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the tentative parsing portions of the Parser
11// interfaces, for ambiguity resolution.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Parse/ParseDiagnostic.h"
17#include "clang/Sema/ParsedTemplate.h"
18using namespace clang;
19
20/// isCXXDeclarationStatement - C++-specialized function that disambiguates
21/// between a declaration or an expression statement, when parsing function
22/// bodies. Returns true for declaration, false for expression.
23///
24/// declaration-statement:
25/// block-declaration
26///
27/// block-declaration:
28/// simple-declaration
29/// asm-definition
30/// namespace-alias-definition
31/// using-declaration
32/// using-directive
33/// [C++0x] static_assert-declaration
34///
35/// asm-definition:
36/// 'asm' '(' string-literal ')' ';'
37///
38/// namespace-alias-definition:
39/// 'namespace' identifier = qualified-namespace-specifier ';'
40///
41/// using-declaration:
42/// 'using' typename[opt] '::'[opt] nested-name-specifier
43/// unqualified-id ';'
44/// 'using' '::' unqualified-id ;
45///
46/// using-directive:
47/// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
48/// namespace-name ';'
49///
50bool Parser::isCXXDeclarationStatement() {
51 switch (Tok.getKind()) {
52 // asm-definition
53 case tok::kw_asm:
54 // namespace-alias-definition
55 case tok::kw_namespace:
56 // using-declaration
57 // using-directive
58 case tok::kw_using:
59 // static_assert-declaration
60 case tok::kw_static_assert:
61 case tok::kw__Static_assert:
62 return true;
63 // simple-declaration
64 default:
65 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
66 }
67}
68
69/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70/// between a simple-declaration or an expression-statement.
71/// If during the disambiguation process a parsing error is encountered,
72/// the function returns true to let the declaration parsing code handle it.
73/// Returns false if the statement is disambiguated as expression.
74///
75/// simple-declaration:
76/// decl-specifier-seq init-declarator-list[opt] ';'
77///
78/// (if AllowForRangeDecl specified)
79/// for ( for-range-declaration : for-range-initializer ) statement
80/// for-range-declaration:
81/// attribute-specifier-seqopt type-specifier-seq declarator
82bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
83 // C++ 6.8p1:
84 // There is an ambiguity in the grammar involving expression-statements and
85 // declarations: An expression-statement with a function-style explicit type
86 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87 // from a declaration where the first declarator starts with a '('. In those
88 // cases the statement is a declaration. [Note: To disambiguate, the whole
89 // statement might have to be examined to determine if it is an
90 // expression-statement or a declaration].
91
92 // C++ 6.8p3:
93 // The disambiguation is purely syntactic; that is, the meaning of the names
94 // occurring in such a statement, beyond whether they are type-names or not,
95 // is not generally used in or changed by the disambiguation. Class
96 // templates are instantiated as necessary to determine if a qualified name
97 // is a type-name. Disambiguation precedes parsing, and a statement
98 // disambiguated as a declaration may be an ill-formed declaration.
99
100 // We don't have to parse all of the decl-specifier-seq part. There's only
101 // an ambiguity if the first decl-specifier is
102 // simple-type-specifier/typename-specifier followed by a '(', which may
103 // indicate a function-style cast expression.
Richard Smithee390432014-05-16 01:56:53 +0000104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
Guy Benyei11169dd2012-12-18 14:30:41 +0000105 // a case.
106
107 bool InvalidAsDeclaration = false;
Richard Smithee390432014-05-16 01:56:53 +0000108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Guy Benyei11169dd2012-12-18 14:30:41 +0000109 &InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +0000110 if (TPR != TPResult::Ambiguous)
111 return TPR != TPResult::False; // Returns true for TPResult::True or
112 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000113
114 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
115 // and so gets some cases wrong. We can't carry on if we've already seen
116 // something which makes this statement invalid as a declaration in this case,
117 // since it can cause us to misparse valid code. Revisit this once
118 // TryParseInitDeclaratorList is fixed.
119 if (InvalidAsDeclaration)
120 return false;
121
122 // FIXME: Add statistics about the number of ambiguous statements encountered
123 // and how they were resolved (number of declarations+number of expressions).
124
125 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
126 // or an identifier which doesn't resolve as anything. We need tentative
127 // parsing...
Richard Smith91b73f22016-06-29 21:06:51 +0000128
129 {
130 RevertingTentativeParsingAction PA(*this);
131 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
132 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000133
134 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000135 if (TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 return true;
137
138 // Declarations take precedence over expressions.
Richard Smithee390432014-05-16 01:56:53 +0000139 if (TPR == TPResult::Ambiguous)
140 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000141
Richard Smithee390432014-05-16 01:56:53 +0000142 assert(TPR == TPResult::True || TPR == TPResult::False);
143 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000144}
145
Richard Smith1fff95c2013-09-12 23:28:08 +0000146/// Try to consume a token sequence that we've already identified as
147/// (potentially) starting a decl-specifier.
148Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
149 switch (Tok.getKind()) {
150 case tok::kw__Atomic:
151 if (NextToken().isNot(tok::l_paren)) {
152 ConsumeToken();
153 break;
154 }
155 // Fall through.
156 case tok::kw_typeof:
157 case tok::kw___attribute:
158 case tok::kw___underlying_type: {
159 ConsumeToken();
160 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000161 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000162 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000163 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000164 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000165 break;
166 }
167
168 case tok::kw_class:
169 case tok::kw_struct:
170 case tok::kw_union:
171 case tok::kw___interface:
172 case tok::kw_enum:
173 // elaborated-type-specifier:
174 // class-key attribute-specifier-seq[opt]
175 // nested-name-specifier[opt] identifier
176 // class-key nested-name-specifier[opt] template[opt] simple-template-id
177 // enum nested-name-specifier[opt] identifier
178 //
179 // FIXME: We don't support class-specifiers nor enum-specifiers here.
180 ConsumeToken();
181
182 // Skip attributes.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000183 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
184 tok::kw_alignas)) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000185 if (Tok.is(tok::l_square)) {
186 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000187 if (!SkipUntil(tok::r_square))
Richard Smithee390432014-05-16 01:56:53 +0000188 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000189 } else {
190 ConsumeToken();
191 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000192 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000193 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000195 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000196 }
197 }
198
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000199 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
200 tok::annot_template_id) &&
Nico Weberc29c4832014-12-28 23:24:02 +0000201 TryAnnotateCXXScopeToken())
Richard Smithee390432014-05-16 01:56:53 +0000202 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000203 if (Tok.is(tok::annot_cxxscope))
204 ConsumeToken();
205 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Richard Smithee390432014-05-16 01:56:53 +0000206 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000207 ConsumeToken();
208 break;
209
210 case tok::annot_cxxscope:
211 ConsumeToken();
212 // Fall through.
213 default:
214 ConsumeToken();
215
216 if (getLangOpts().ObjC1 && Tok.is(tok::less))
217 return TryParseProtocolQualifiers();
218 break;
219 }
220
Richard Smithee390432014-05-16 01:56:53 +0000221 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000222}
223
Guy Benyei11169dd2012-12-18 14:30:41 +0000224/// simple-declaration:
225/// decl-specifier-seq init-declarator-list[opt] ';'
226///
227/// (if AllowForRangeDecl specified)
228/// for ( for-range-declaration : for-range-initializer ) statement
229/// for-range-declaration:
230/// attribute-specifier-seqopt type-specifier-seq declarator
231///
232Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000233 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
234 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000235
236 // Two decl-specifiers in a row conclusively disambiguate this as being a
237 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
238 // overwhelmingly common case that the next token is a '('.
239 if (Tok.isNot(tok::l_paren)) {
240 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000241 if (TPR == TPResult::Ambiguous)
242 return TPResult::True;
243 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000244 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000245 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000246 }
247
248 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000249 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 return TPR;
251
252 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000253 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000254
Richard Smithee390432014-05-16 01:56:53 +0000255 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000256}
257
Richard Smith22c7c412013-03-20 03:35:02 +0000258/// Tentatively parse an init-declarator-list in order to disambiguate it from
259/// an expression.
260///
Guy Benyei11169dd2012-12-18 14:30:41 +0000261/// init-declarator-list:
262/// init-declarator
263/// init-declarator-list ',' init-declarator
264///
265/// init-declarator:
266/// declarator initializer[opt]
267/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
268///
Richard Smith22c7c412013-03-20 03:35:02 +0000269/// initializer:
270/// brace-or-equal-initializer
271/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000272///
Richard Smith22c7c412013-03-20 03:35:02 +0000273/// brace-or-equal-initializer:
274/// '=' initializer-clause
275/// [C++11] braced-init-list
276///
277/// initializer-clause:
278/// assignment-expression
279/// braced-init-list
280///
281/// braced-init-list:
282/// '{' initializer-list ','[opt] '}'
283/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000284///
285Parser::TPResult Parser::TryParseInitDeclaratorList() {
286 while (1) {
287 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000288 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000289 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000290 return TPR;
291
292 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000293 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000294 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000295
296 // initializer[opt]
297 if (Tok.is(tok::l_paren)) {
298 // Parse through the parens.
299 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000300 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000301 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000302 } else if (Tok.is(tok::l_brace)) {
303 // A left-brace here is sufficient to disambiguate the parse; an
304 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000305 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000306 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000307 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000308 // encountered; they just conclude that we have a declaration.
309 // EDG parses the initializer completely, which is the proper behavior
310 // for this case.
311 //
312 // At present, Clang follows MSVC and g++, since the parser does not have
313 // the ability to parse an expression fully without recording the
314 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000315 // FIXME: Handle this case correctly.
316 //
317 // Also allow 'in' after an Objective-C declaration as in:
318 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000319 // context of parsing for-init-statement of a foreach statement only. But,
320 // in any other context 'in' is invalid after a declaration and parser
321 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000322 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000323 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 }
325
Alp Toker97650562014-01-10 11:19:30 +0000326 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 }
329
Richard Smithee390432014-05-16 01:56:53 +0000330 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000331}
332
Richard Smithc7a05a92016-06-29 21:17:59 +0000333struct Parser::ConditionDeclarationOrInitStatementState {
334 Parser &P;
335 bool CanBeExpression = true;
336 bool CanBeCondition = true;
337 bool CanBeInitStatement;
338
339 ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement)
340 : P(P), CanBeInitStatement(CanBeInitStatement) {}
341
342 void markNotExpression() {
343 CanBeExpression = false;
344
345 if (CanBeCondition && CanBeInitStatement) {
346 // FIXME: Unify the parsing codepaths for condition variables and
347 // simple-declarations so that we don't need to eagerly figure out which
348 // kind we have here. (Just parse init-declarators until we reach a
349 // semicolon or right paren.)
350 RevertingTentativeParsingAction PA(P);
351 P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
352 if (P.Tok.isNot(tok::r_paren))
353 CanBeCondition = false;
354 if (P.Tok.isNot(tok::semi))
355 CanBeInitStatement = false;
356 }
357 }
358
359 bool markNotCondition() {
360 CanBeCondition = false;
361 return !CanBeInitStatement || !CanBeExpression;
362 }
363
364 bool update(TPResult IsDecl) {
365 switch (IsDecl) {
366 case TPResult::True:
367 markNotExpression();
368 return true;
369 case TPResult::False:
370 CanBeCondition = CanBeInitStatement = false;
371 return true;
372 case TPResult::Ambiguous:
373 return false;
374 case TPResult::Error:
375 CanBeExpression = CanBeCondition = CanBeInitStatement = false;
376 return true;
377 }
378 llvm_unreachable("unknown tentative parse result");
379 }
380
381 ConditionOrInitStatement result() const {
382 assert(CanBeExpression + CanBeCondition + CanBeInitStatement < 2 &&
383 "result called but not yet resolved");
384 if (CanBeExpression)
385 return ConditionOrInitStatement::Expression;
386 if (CanBeCondition)
387 return ConditionOrInitStatement::ConditionDecl;
388 if (CanBeInitStatement)
389 return ConditionOrInitStatement::InitStmtDecl;
390 return ConditionOrInitStatement::Error;
391 }
392};
393
394/// \brief Disambiguates between a declaration in a condition, a
395/// simple-declaration in an init-statement, and an expression for
396/// a condition of a if/switch statement.
Guy Benyei11169dd2012-12-18 14:30:41 +0000397///
398/// condition:
399/// expression
400/// type-specifier-seq declarator '=' assignment-expression
401/// [C++11] type-specifier-seq declarator '=' initializer-clause
402/// [C++11] type-specifier-seq declarator braced-init-list
403/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
404/// '=' assignment-expression
Richard Smithc7a05a92016-06-29 21:17:59 +0000405/// simple-declaration:
406/// decl-specifier-seq init-declarator-list[opt] ';'
Guy Benyei11169dd2012-12-18 14:30:41 +0000407///
Richard Smithc7a05a92016-06-29 21:17:59 +0000408/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
409/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
410/// 'int(x);' (a simple-declaration in an init-statement).
411Parser::ConditionOrInitStatement
412Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement) {
413 ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement);
Guy Benyei11169dd2012-12-18 14:30:41 +0000414
Richard Smithc7a05a92016-06-29 21:17:59 +0000415 if (State.update(isCXXDeclarationSpecifier()))
416 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000417
Richard Smithc7a05a92016-06-29 21:17:59 +0000418 // It might be a declaration; we need tentative parsing.
Richard Smith91b73f22016-06-29 21:06:51 +0000419 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000420
Richard Smithc7a05a92016-06-29 21:17:59 +0000421 // FIXME: A tag definition unambiguously tells us this is an init-statement.
422 if (State.update(TryConsumeDeclarationSpecifier()))
423 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 assert(Tok.is(tok::l_paren) && "Expected '('");
425
Richard Smithc7a05a92016-06-29 21:17:59 +0000426 while (true) {
427 // Consume a declarator.
428 if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
429 return State.result();
Guy Benyei11169dd2012-12-18 14:30:41 +0000430
Richard Smithc7a05a92016-06-29 21:17:59 +0000431 // Attributes, asm label, or an initializer imply this is not an expression.
432 // FIXME: Disambiguate properly after an = instead of assuming that it's a
433 // valid declaration.
434 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
435 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
436 State.markNotExpression();
437 return State.result();
438 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000439
Richard Smithc7a05a92016-06-29 21:17:59 +0000440 // At this point, it can't be a condition any more, because a condition
441 // must have a brace-or-equal-initializer.
442 if (State.markNotCondition())
443 return State.result();
444
445 // A parenthesized initializer could be part of an expression or a
446 // simple-declaration.
447 if (Tok.is(tok::l_paren)) {
448 ConsumeParen();
449 SkipUntil(tok::r_paren, StopAtSemi);
450 }
451
452 if (!TryConsumeToken(tok::comma))
453 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 }
455
Richard Smithc7a05a92016-06-29 21:17:59 +0000456 // We reached the end. If it can now be some kind of decl, then it is.
457 if (State.CanBeCondition && Tok.is(tok::r_paren))
458 return ConditionOrInitStatement::ConditionDecl;
459 else if (State.CanBeInitStatement && Tok.is(tok::semi))
460 return ConditionOrInitStatement::InitStmtDecl;
461 else
462 return ConditionOrInitStatement::Expression;
Guy Benyei11169dd2012-12-18 14:30:41 +0000463}
464
465 /// \brief Determine whether the next set of tokens contains a type-id.
466 ///
467 /// The context parameter states what context we're parsing right
468 /// now, which affects how this routine copes with the token
469 /// following the type-id. If the context is TypeIdInParens, we have
470 /// already parsed the '(' and we will cease lookahead when we hit
471 /// the corresponding ')'. If the context is
472 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
473 /// before this template argument, and will cease lookahead when we
474 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
475 /// and false for an expression. If during the disambiguation
476 /// process a parsing error is encountered, the function returns
477 /// true to let the declaration parsing code handle it.
478 ///
479 /// type-id:
480 /// type-specifier-seq abstract-declarator[opt]
481 ///
482bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
483
484 isAmbiguous = false;
485
486 // C++ 8.2p2:
487 // The ambiguity arising from the similarity between a function-style cast and
488 // a type-id can occur in different contexts. The ambiguity appears as a
489 // choice between a function-style cast expression and a declaration of a
490 // type. The resolution is that any construct that could possibly be a type-id
491 // in its syntactic context shall be considered a type-id.
492
493 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000494 if (TPR != TPResult::Ambiguous)
495 return TPR != TPResult::False; // Returns true for TPResult::True or
496 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000497
498 // FIXME: Add statistics about the number of ambiguous statements encountered
499 // and how they were resolved (number of declarations+number of expressions).
500
501 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
502 // We need tentative parsing...
503
Richard Smith91b73f22016-06-29 21:06:51 +0000504 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000505
506 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000507 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000508 assert(Tok.is(tok::l_paren) && "Expected '('");
509
510 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000511 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000512
513 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000514 if (TPR == TPResult::Error)
515 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516
Richard Smithee390432014-05-16 01:56:53 +0000517 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 // We are supposed to be inside parens, so if after the abstract declarator
519 // we encounter a ')' this is a type-id, otherwise it's an expression.
520 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000521 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 isAmbiguous = true;
523
524 // We are supposed to be inside a template argument, so if after
525 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
526 // ',', this is a type-id. Otherwise, it's an expression.
527 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000528 (Tok.isOneOf(tok::greater, tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000529 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000530 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000531 isAmbiguous = true;
532
533 } else
Richard Smithee390432014-05-16 01:56:53 +0000534 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000535 }
536
Richard Smithee390432014-05-16 01:56:53 +0000537 assert(TPR == TPResult::True || TPR == TPResult::False);
538 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000539}
540
541/// \brief Returns true if this is a C++11 attribute-specifier. Per
542/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
543/// always introduce an attribute. In Objective-C++11, this rule does not
544/// apply if either '[' begins a message-send.
545///
546/// If Disambiguate is true, we try harder to determine whether a '[[' starts
547/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
548///
549/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
550/// Obj-C message send or the start of an attribute. Otherwise, we assume it
551/// is not an Obj-C message send.
552///
553/// C++11 [dcl.attr.grammar]:
554///
555/// attribute-specifier:
556/// '[' '[' attribute-list ']' ']'
557/// alignment-specifier
558///
559/// attribute-list:
560/// attribute[opt]
561/// attribute-list ',' attribute[opt]
562/// attribute '...'
563/// attribute-list ',' attribute '...'
564///
565/// attribute:
566/// attribute-token attribute-argument-clause[opt]
567///
568/// attribute-token:
569/// identifier
570/// identifier '::' identifier
571///
572/// attribute-argument-clause:
573/// '(' balanced-token-seq ')'
574Parser::CXX11AttributeKind
575Parser::isCXX11AttributeSpecifier(bool Disambiguate,
576 bool OuterMightBeMessageSend) {
577 if (Tok.is(tok::kw_alignas))
578 return CAK_AttributeSpecifier;
579
580 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
581 return CAK_NotAttributeSpecifier;
582
583 // No tentative parsing if we don't need to look for ']]' or a lambda.
584 if (!Disambiguate && !getLangOpts().ObjC1)
585 return CAK_AttributeSpecifier;
586
Richard Smith91b73f22016-06-29 21:06:51 +0000587 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +0000588
589 // Opening brackets were checked for above.
590 ConsumeBracket();
591
592 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
593 if (!getLangOpts().ObjC1) {
594 ConsumeBracket();
595
Alexey Bataevee6507d2013-11-18 08:17:37 +0000596 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000597 IsAttribute &= Tok.is(tok::r_square);
598
Guy Benyei11169dd2012-12-18 14:30:41 +0000599 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
600 }
601
602 // In Obj-C++11, we need to distinguish four situations:
603 // 1a) int x[[attr]]; C++11 attribute.
604 // 1b) [[attr]]; C++11 statement attribute.
605 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
606 // 3a) int x[[obj get]]; Message send in array size/index.
607 // 3b) [[Class alloc] init]; Message send in message send.
608 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
609 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
610
611 // If we have a lambda-introducer, then this is definitely not a message send.
612 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
613 // into the tentative attribute parse below.
614 LambdaIntroducer Intro;
615 if (!TryParseLambdaIntroducer(Intro)) {
616 // A lambda cannot end with ']]', and an attribute must.
617 bool IsAttribute = Tok.is(tok::r_square);
618
Guy Benyei11169dd2012-12-18 14:30:41 +0000619 if (IsAttribute)
620 // Case 1: C++11 attribute.
621 return CAK_AttributeSpecifier;
622
623 if (OuterMightBeMessageSend)
624 // Case 4: Lambda in message send.
625 return CAK_NotAttributeSpecifier;
626
627 // Case 2: Lambda in array size / index.
628 return CAK_InvalidAttributeSpecifier;
629 }
630
631 ConsumeBracket();
632
633 // If we don't have a lambda-introducer, then we have an attribute or a
634 // message-send.
635 bool IsAttribute = true;
636 while (Tok.isNot(tok::r_square)) {
637 if (Tok.is(tok::comma)) {
638 // Case 1: Stray commas can only occur in attributes.
Guy Benyei11169dd2012-12-18 14:30:41 +0000639 return CAK_AttributeSpecifier;
640 }
641
642 // Parse the attribute-token, if present.
643 // C++11 [dcl.attr.grammar]:
644 // If a keyword or an alternative token that satisfies the syntactic
645 // requirements of an identifier is contained in an attribute-token,
646 // it is considered an identifier.
647 SourceLocation Loc;
648 if (!TryParseCXX11AttributeIdentifier(Loc)) {
649 IsAttribute = false;
650 break;
651 }
652 if (Tok.is(tok::coloncolon)) {
653 ConsumeToken();
654 if (!TryParseCXX11AttributeIdentifier(Loc)) {
655 IsAttribute = false;
656 break;
657 }
658 }
659
660 // Parse the attribute-argument-clause, if present.
661 if (Tok.is(tok::l_paren)) {
662 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000663 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000664 IsAttribute = false;
665 break;
666 }
667 }
668
Alp Toker97650562014-01-10 11:19:30 +0000669 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000670
Alp Toker97650562014-01-10 11:19:30 +0000671 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 }
674
675 // An attribute must end ']]'.
676 if (IsAttribute) {
677 if (Tok.is(tok::r_square)) {
678 ConsumeBracket();
679 IsAttribute = Tok.is(tok::r_square);
680 } else {
681 IsAttribute = false;
682 }
683 }
684
Guy Benyei11169dd2012-12-18 14:30:41 +0000685 if (IsAttribute)
686 // Case 1: C++11 statement attribute.
687 return CAK_AttributeSpecifier;
688
689 // Case 3: Message send.
690 return CAK_NotAttributeSpecifier;
691}
692
Richard Smith1fff95c2013-09-12 23:28:08 +0000693Parser::TPResult Parser::TryParsePtrOperatorSeq() {
694 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000695 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000696 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000697 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000698
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000699 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000700 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
701 // ptr-operator
702 ConsumeToken();
Douglas Gregor261a89b2015-06-19 17:51:05 +0000703 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000704 tok::kw__Nonnull, tok::kw__Nullable,
705 tok::kw__Null_unspecified))
Richard Smith1fff95c2013-09-12 23:28:08 +0000706 ConsumeToken();
707 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000708 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000709 }
710 }
711}
712
713/// operator-function-id:
714/// 'operator' operator
715///
716/// operator: one of
717/// new delete new[] delete[] + - * / % ^ [...]
718///
719/// conversion-function-id:
720/// 'operator' conversion-type-id
721///
722/// conversion-type-id:
723/// type-specifier-seq conversion-declarator[opt]
724///
725/// conversion-declarator:
726/// ptr-operator conversion-declarator[opt]
727///
728/// literal-operator-id:
729/// 'operator' string-literal identifier
730/// 'operator' user-defined-string-literal
731Parser::TPResult Parser::TryParseOperatorId() {
732 assert(Tok.is(tok::kw_operator));
733 ConsumeToken();
734
735 // Maybe this is an operator-function-id.
736 switch (Tok.getKind()) {
737 case tok::kw_new: case tok::kw_delete:
738 ConsumeToken();
739 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
740 ConsumeBracket();
741 ConsumeBracket();
742 }
Richard Smithee390432014-05-16 01:56:53 +0000743 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000744
745#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
746 case tok::Token:
747#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
748#include "clang/Basic/OperatorKinds.def"
749 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000750 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000751
752 case tok::l_square:
753 if (NextToken().is(tok::r_square)) {
754 ConsumeBracket();
755 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000756 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000757 }
758 break;
759
760 case tok::l_paren:
761 if (NextToken().is(tok::r_paren)) {
762 ConsumeParen();
763 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000764 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000765 }
766 break;
767
768 default:
769 break;
770 }
771
772 // Maybe this is a literal-operator-id.
773 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
774 bool FoundUDSuffix = false;
775 do {
776 FoundUDSuffix |= Tok.hasUDSuffix();
777 ConsumeStringToken();
778 } while (isTokenStringLiteral());
779
780 if (!FoundUDSuffix) {
781 if (Tok.is(tok::identifier))
782 ConsumeToken();
783 else
Richard Smithee390432014-05-16 01:56:53 +0000784 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000785 }
Richard Smithee390432014-05-16 01:56:53 +0000786 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000787 }
788
789 // Maybe this is a conversion-function-id.
790 bool AnyDeclSpecifiers = false;
791 while (true) {
792 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000793 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000794 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000795 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000796 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000797 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000798 break;
799 }
Richard Smithee390432014-05-16 01:56:53 +0000800 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
801 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000802 AnyDeclSpecifiers = true;
803 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000804 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000805}
806
Guy Benyei11169dd2012-12-18 14:30:41 +0000807/// declarator:
808/// direct-declarator
809/// ptr-operator declarator
810///
811/// direct-declarator:
812/// declarator-id
813/// direct-declarator '(' parameter-declaration-clause ')'
814/// cv-qualifier-seq[opt] exception-specification[opt]
815/// direct-declarator '[' constant-expression[opt] ']'
816/// '(' declarator ')'
817/// [GNU] '(' attributes declarator ')'
818///
819/// abstract-declarator:
820/// ptr-operator abstract-declarator[opt]
821/// direct-abstract-declarator
822/// ...
823///
824/// direct-abstract-declarator:
825/// direct-abstract-declarator[opt]
826/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
827/// exception-specification[opt]
828/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
829/// '(' abstract-declarator ')'
830///
831/// ptr-operator:
832/// '*' cv-qualifier-seq[opt]
833/// '&'
834/// [C++0x] '&&' [TODO]
835/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
836///
837/// cv-qualifier-seq:
838/// cv-qualifier cv-qualifier-seq[opt]
839///
840/// cv-qualifier:
841/// 'const'
842/// 'volatile'
843///
844/// declarator-id:
845/// '...'[opt] id-expression
846///
847/// id-expression:
848/// unqualified-id
849/// qualified-id [TODO]
850///
851/// unqualified-id:
852/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000853/// operator-function-id
854/// conversion-function-id
855/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000856/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000857/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000858/// template-id [TODO]
859///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000860Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
861 bool mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000862 // declarator:
863 // direct-declarator
864 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000865 if (TryParsePtrOperatorSeq() == TPResult::Error)
866 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000867
868 // direct-declarator:
869 // direct-abstract-declarator:
870 if (Tok.is(tok::ellipsis))
871 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000872
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000873 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000874 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
875 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000876 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000877 // declarator-id
878 if (Tok.is(tok::annot_cxxscope))
879 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000880 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000882 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000883 if (TryParseOperatorId() == TPResult::Error)
884 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000885 } else
886 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000887 } else if (Tok.is(tok::l_paren)) {
888 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000889 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000890 (Tok.is(tok::r_paren) || // 'int()' is a function.
891 // 'int(...)' is a function.
892 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
893 isDeclarationSpecifier())) { // 'int(int)' is a function.
894 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
895 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000896 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000897 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000898 return TPR;
899 } else {
900 // '(' declarator ')'
901 // '(' attributes declarator ')'
902 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000903 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
904 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000905 tok::kw___vectorcall))
Richard Smithee390432014-05-16 01:56:53 +0000906 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000907 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000908 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000909 return TPR;
910 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000911 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000912 ConsumeParen();
913 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000914 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000915 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000916 }
917
918 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000919 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000920
921 // abstract-declarator: ...
922 if (Tok.is(tok::ellipsis))
923 ConsumeToken();
924
925 if (Tok.is(tok::l_paren)) {
926 // Check whether we have a function declarator or a possible ctor-style
927 // initializer that follows the declarator. Note that ctor-style
928 // initializers are not possible in contexts where abstract declarators
929 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000930 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 break;
932
933 // direct-declarator '(' parameter-declaration-clause ')'
934 // cv-qualifier-seq[opt] exception-specification[opt]
935 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000936 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +0000937 } else if (Tok.is(tok::l_square)) {
938 // direct-declarator '[' constant-expression[opt] ']'
939 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
940 TPR = TryParseBracketDeclarator();
941 } else {
942 break;
943 }
944
Richard Smithee390432014-05-16 01:56:53 +0000945 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000946 return TPR;
947 }
948
Richard Smithee390432014-05-16 01:56:53 +0000949 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000950}
951
952Parser::TPResult
953Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
954 switch (Kind) {
955 // Obviously starts an expression.
956 case tok::numeric_constant:
957 case tok::char_constant:
958 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000959 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000960 case tok::utf16_char_constant:
961 case tok::utf32_char_constant:
962 case tok::string_literal:
963 case tok::wide_string_literal:
964 case tok::utf8_string_literal:
965 case tok::utf16_string_literal:
966 case tok::utf32_string_literal:
967 case tok::l_square:
968 case tok::l_paren:
969 case tok::amp:
970 case tok::ampamp:
971 case tok::star:
972 case tok::plus:
973 case tok::plusplus:
974 case tok::minus:
975 case tok::minusminus:
976 case tok::tilde:
977 case tok::exclaim:
978 case tok::kw_sizeof:
979 case tok::kw___func__:
980 case tok::kw_const_cast:
981 case tok::kw_delete:
982 case tok::kw_dynamic_cast:
983 case tok::kw_false:
984 case tok::kw_new:
985 case tok::kw_operator:
986 case tok::kw_reinterpret_cast:
987 case tok::kw_static_cast:
988 case tok::kw_this:
989 case tok::kw_throw:
990 case tok::kw_true:
991 case tok::kw_typeid:
992 case tok::kw_alignof:
993 case tok::kw_noexcept:
994 case tok::kw_nullptr:
995 case tok::kw__Alignof:
996 case tok::kw___null:
997 case tok::kw___alignof:
998 case tok::kw___builtin_choose_expr:
999 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +00001000 case tok::kw___builtin_va_arg:
1001 case tok::kw___imag:
1002 case tok::kw___real:
1003 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +00001004 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +00001005 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001006 case tok::kw_L__FUNCTION__:
1007 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +00001008 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +00001009#define TYPE_TRAIT(N,Spelling,K) \
1010 case tok::kw_##Spelling:
1011#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +00001012 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001013
1014 // Obviously starts a type-specifier-seq:
1015 case tok::kw_char:
1016 case tok::kw_const:
1017 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001018 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001019 case tok::kw_enum:
1020 case tok::kw_half:
1021 case tok::kw_float:
1022 case tok::kw_int:
1023 case tok::kw_long:
1024 case tok::kw___int64:
1025 case tok::kw___int128:
1026 case tok::kw_restrict:
1027 case tok::kw_short:
1028 case tok::kw_signed:
1029 case tok::kw_struct:
1030 case tok::kw_union:
1031 case tok::kw_unsigned:
1032 case tok::kw_void:
1033 case tok::kw_volatile:
1034 case tok::kw__Bool:
1035 case tok::kw__Complex:
1036 case tok::kw_class:
1037 case tok::kw_typename:
1038 case tok::kw_wchar_t:
1039 case tok::kw_char16_t:
1040 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +00001041 case tok::kw__Decimal32:
1042 case tok::kw__Decimal64:
1043 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001044 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001045 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001046 case tok::kw_thread_local:
1047 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001048 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +00001049 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 case tok::kw___cdecl:
1051 case tok::kw___stdcall:
1052 case tok::kw___fastcall:
1053 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001054 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 case tok::kw___unaligned:
1056 case tok::kw___vector:
1057 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +00001058 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +00001059 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +00001060#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00001061#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +00001063 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001064
1065 default:
1066 break;
1067 }
1068
Richard Smithee390432014-05-16 01:56:53 +00001069 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001070}
1071
1072bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1073 return std::find(TentativelyDeclaredIdentifiers.begin(),
1074 TentativelyDeclaredIdentifiers.end(), II)
1075 != TentativelyDeclaredIdentifiers.end();
1076}
1077
Kaelyn Takata445b0652014-11-05 00:09:29 +00001078namespace {
1079class TentativeParseCCC : public CorrectionCandidateCallback {
1080public:
1081 TentativeParseCCC(const Token &Next) {
1082 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001083 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1084 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001085 }
1086
1087 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1088 // Reject any candidate that only resolves to instance members since they
1089 // aren't viable as standalone identifiers instead of member references.
1090 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1091 std::all_of(Candidate.begin(), Candidate.end(),
1092 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1093 return false;
1094
1095 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1096 }
1097};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001098}
Richard Smithee390432014-05-16 01:56:53 +00001099/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1100/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1101/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001102/// if a parsing error was found and reported.
1103///
1104/// If HasMissingTypename is provided, a name with a dependent scope specifier
1105/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1106/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1107/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001108/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001109/// such cases.
1110///
1111/// decl-specifier:
1112/// storage-class-specifier
1113/// type-specifier
1114/// function-specifier
1115/// 'friend'
1116/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001117/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001118/// [GNU] attributes declaration-specifiers[opt]
1119///
1120/// storage-class-specifier:
1121/// 'register'
1122/// 'static'
1123/// 'extern'
1124/// 'mutable'
1125/// 'auto'
1126/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001127/// [C++11] 'thread_local'
1128/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001129///
1130/// function-specifier:
1131/// 'inline'
1132/// 'virtual'
1133/// 'explicit'
1134///
1135/// typedef-name:
1136/// identifier
1137///
1138/// type-specifier:
1139/// simple-type-specifier
1140/// class-specifier
1141/// enum-specifier
1142/// elaborated-type-specifier
1143/// typename-specifier
1144/// cv-qualifier
1145///
1146/// simple-type-specifier:
1147/// '::'[opt] nested-name-specifier[opt] type-name
1148/// '::'[opt] nested-name-specifier 'template'
1149/// simple-template-id [TODO]
1150/// 'char'
1151/// 'wchar_t'
1152/// 'bool'
1153/// 'short'
1154/// 'int'
1155/// 'long'
1156/// 'signed'
1157/// 'unsigned'
1158/// 'float'
1159/// 'double'
1160/// 'void'
1161/// [GNU] typeof-specifier
1162/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001163/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001164/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001165/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001166/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001167///
1168/// type-name:
1169/// class-name
1170/// enum-name
1171/// typedef-name
1172///
1173/// elaborated-type-specifier:
1174/// class-key '::'[opt] nested-name-specifier[opt] identifier
1175/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1176/// simple-template-id
1177/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1178///
1179/// enum-name:
1180/// identifier
1181///
1182/// enum-specifier:
1183/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1184/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1185///
1186/// class-specifier:
1187/// class-head '{' member-specification[opt] '}'
1188///
1189/// class-head:
1190/// class-key identifier[opt] base-clause[opt]
1191/// class-key nested-name-specifier identifier base-clause[opt]
1192/// class-key nested-name-specifier[opt] simple-template-id
1193/// base-clause[opt]
1194///
1195/// class-key:
1196/// 'class'
1197/// 'struct'
1198/// 'union'
1199///
1200/// cv-qualifier:
1201/// 'const'
1202/// 'volatile'
1203/// [GNU] restrict
1204///
1205Parser::TPResult
1206Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1207 bool *HasMissingTypename) {
1208 switch (Tok.getKind()) {
1209 case tok::identifier: {
1210 // Check for need to substitute AltiVec __vector keyword
1211 // for "vector" identifier.
1212 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001213 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001214
1215 const Token &Next = NextToken();
1216 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1217 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001218 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001219
1220 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1221 // Determine whether this is a valid expression. If not, we will hit
1222 // a parse error one way or another. In that case, tell the caller that
1223 // this is ambiguous. Typo-correct to type and expression keywords and
1224 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001225 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001226 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001228 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001229 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001230 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001231 case ANK_TemplateName:
1232 // A bare type template-name which can't be a template template
1233 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001234 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001236 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 case ANK_Success:
1238 break;
1239 }
1240 assert(Tok.isNot(tok::identifier) &&
1241 "TryAnnotateName succeeded without producing an annotation");
1242 } else {
1243 // This might possibly be a type with a dependent scope specifier and
1244 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1245 // since it will annotate as a primary expression, and we want to use the
1246 // "missing 'typename'" logic.
1247 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001248 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 // If annotation failed, assume it's a non-type.
1250 // FIXME: If this happens due to an undeclared identifier, treat it as
1251 // ambiguous.
1252 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001253 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 }
1255
1256 // We annotated this token as something. Recurse to handle whatever we got.
1257 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1258 }
1259
1260 case tok::kw_typename: // typename T::type
1261 // Annotate typenames and C++ scope specifiers. If we get one, just
1262 // recurse to handle whatever we get.
1263 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001264 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001265 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1266
1267 case tok::coloncolon: { // ::foo::bar
1268 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001269 if (Next.isOneOf(tok::kw_new, // ::new
1270 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001271 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001272 }
1273 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001274 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001275 case tok::kw_decltype:
1276 // Annotate typenames and C++ scope specifiers. If we get one, just
1277 // recurse to handle whatever we get.
1278 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001279 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001280 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1281
1282 // decl-specifier:
1283 // storage-class-specifier
1284 // type-specifier
1285 // function-specifier
1286 // 'friend'
1287 // 'typedef'
1288 // 'constexpr'
Hubert Tong375f00a2015-06-30 12:14:52 +00001289 // 'concept'
Guy Benyei11169dd2012-12-18 14:30:41 +00001290 case tok::kw_friend:
1291 case tok::kw_typedef:
1292 case tok::kw_constexpr:
Hubert Tong375f00a2015-06-30 12:14:52 +00001293 case tok::kw_concept:
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 // storage-class-specifier
1295 case tok::kw_register:
1296 case tok::kw_static:
1297 case tok::kw_extern:
1298 case tok::kw_mutable:
1299 case tok::kw_auto:
1300 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001301 case tok::kw_thread_local:
1302 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001303 // function-specifier
1304 case tok::kw_inline:
1305 case tok::kw_virtual:
1306 case tok::kw_explicit:
1307
1308 // Modules
1309 case tok::kw___module_private__:
1310
1311 // Debugger support
1312 case tok::kw___unknown_anytype:
1313
1314 // type-specifier:
1315 // simple-type-specifier
1316 // class-specifier
1317 // enum-specifier
1318 // elaborated-type-specifier
1319 // typename-specifier
1320 // cv-qualifier
1321
1322 // class-specifier
1323 // elaborated-type-specifier
1324 case tok::kw_class:
1325 case tok::kw_struct:
1326 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001327 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 // enum-specifier
1329 case tok::kw_enum:
1330 // cv-qualifier
1331 case tok::kw_const:
1332 case tok::kw_volatile:
1333
1334 // GNU
1335 case tok::kw_restrict:
1336 case tok::kw__Complex:
1337 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001338 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001339 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
1341 // Microsoft
1342 case tok::kw___declspec:
1343 case tok::kw___cdecl:
1344 case tok::kw___stdcall:
1345 case tok::kw___fastcall:
1346 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001347 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001349 case tok::kw___sptr:
1350 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001351 case tok::kw___ptr64:
1352 case tok::kw___ptr32:
1353 case tok::kw___forceinline:
1354 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001355 case tok::kw__Nonnull:
1356 case tok::kw__Nullable:
1357 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001358 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001359 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001360
1361 // Borland
1362 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001363 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001364
1365 // AltiVec
1366 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001367 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001368
1369 case tok::annot_template_id: {
1370 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1371 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001372 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 CXXScopeSpec SS;
1374 AnnotateTemplateIdTokenAsType();
1375 assert(Tok.is(tok::annot_typename));
1376 goto case_typename;
1377 }
1378
1379 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1380 // We've already annotated a scope; try to annotate a type.
1381 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001382 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 if (!Tok.is(tok::annot_typename)) {
1384 // If the next token is an identifier or a type qualifier, then this
1385 // can't possibly be a valid expression either.
1386 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1387 CXXScopeSpec SS;
1388 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1389 Tok.getAnnotationRange(),
1390 SS);
1391 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
Richard Smith4556ebe2016-06-29 21:12:37 +00001392 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001393 ConsumeToken();
1394 ConsumeToken();
1395 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001396 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 if (!isIdentifier)
1398 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1399 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001400
1401 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001402 TPR == TPResult::True || TPR == TPResult::Error)
1403 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001404
1405 if (HasMissingTypename) {
1406 // We can't tell whether this is a missing 'typename' or a valid
1407 // expression.
1408 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001409 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 }
Richard Smithc7a05a92016-06-29 21:17:59 +00001411
1412 // FIXME: Fails to either revert or commit the tentative parse!
Guy Benyei11169dd2012-12-18 14:30:41 +00001413 } else {
1414 // Try to resolve the name. If it doesn't exist, assume it was
1415 // intended to name a type and keep disambiguating.
1416 switch (TryAnnotateName(false /* SS is not dependent */)) {
1417 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001418 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001419 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001420 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 case ANK_TemplateName:
1422 // A bare type template-name which can't be a template template
1423 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001424 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001425 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001426 return HasMissingTypename ? TPResult::Ambiguous
1427 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001428 case ANK_Success:
1429 // Annotated it, check again.
1430 assert(Tok.isNot(tok::annot_cxxscope) ||
1431 NextToken().isNot(tok::identifier));
1432 return isCXXDeclarationSpecifier(BracedCastResult,
1433 HasMissingTypename);
1434 }
1435 }
1436 }
Richard Smithee390432014-05-16 01:56:53 +00001437 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 }
1439 // If that succeeded, fallthrough into the generic simple-type-id case.
1440
1441 // The ambiguity resides in a simple-type-specifier/typename-specifier
1442 // followed by a '('. The '(' could either be the start of:
1443 //
1444 // direct-declarator:
1445 // '(' declarator ')'
1446 //
1447 // direct-abstract-declarator:
1448 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1449 // exception-specification[opt]
1450 // '(' abstract-declarator ')'
1451 //
1452 // or part of a function-style cast expression:
1453 //
1454 // simple-type-specifier '(' expression-list[opt] ')'
1455 //
1456
1457 // simple-type-specifier:
1458
1459 case tok::annot_typename:
1460 case_typename:
1461 // In Objective-C, we might have a protocol-qualified type.
1462 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001463 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001464 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001465 ConsumeToken(); // The type token
1466
1467 TPResult TPR = TryParseProtocolQualifiers();
1468 bool isFollowedByParen = Tok.is(tok::l_paren);
1469 bool isFollowedByBrace = Tok.is(tok::l_brace);
1470
Richard Smithee390432014-05-16 01:56:53 +00001471 if (TPR == TPResult::Error)
1472 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001473
1474 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001475 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001476
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001477 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001478 return BracedCastResult;
1479
Richard Smithee390432014-05-16 01:56:53 +00001480 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 }
1482
1483 case tok::kw_char:
1484 case tok::kw_wchar_t:
1485 case tok::kw_char16_t:
1486 case tok::kw_char32_t:
1487 case tok::kw_bool:
1488 case tok::kw_short:
1489 case tok::kw_int:
1490 case tok::kw_long:
1491 case tok::kw___int64:
1492 case tok::kw___int128:
1493 case tok::kw_signed:
1494 case tok::kw_unsigned:
1495 case tok::kw_half:
1496 case tok::kw_float:
1497 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001498 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001499 case tok::kw_void:
1500 case tok::annot_decltype:
1501 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001502 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001503
1504 // This is a function-style cast in all cases we disambiguate other than
1505 // one:
1506 // struct S {
1507 // enum E : int { a = 4 }; // enum
1508 // enum E : int { 4 }; // bit-field
1509 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001510 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001511 return BracedCastResult;
1512
1513 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001514 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001515
Richard Smithee390432014-05-16 01:56:53 +00001516 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001517
1518 // GNU typeof support.
1519 case tok::kw_typeof: {
1520 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001521 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001522
Richard Smith91b73f22016-06-29 21:06:51 +00001523 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001524
1525 TPResult TPR = TryParseTypeofSpecifier();
1526 bool isFollowedByParen = Tok.is(tok::l_paren);
1527 bool isFollowedByBrace = Tok.is(tok::l_brace);
1528
Richard Smithee390432014-05-16 01:56:53 +00001529 if (TPR == TPResult::Error)
1530 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001531
1532 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001533 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001534
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001535 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001536 return BracedCastResult;
1537
Richard Smithee390432014-05-16 01:56:53 +00001538 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001539 }
1540
1541 // C++0x type traits support
1542 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001543 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001544
1545 // C11 _Atomic
1546 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001547 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001548
1549 default:
Richard Smithee390432014-05-16 01:56:53 +00001550 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 }
1552}
1553
Richard Smith1fff95c2013-09-12 23:28:08 +00001554bool Parser::isCXXDeclarationSpecifierAType() {
1555 switch (Tok.getKind()) {
1556 // typename-specifier
1557 case tok::annot_decltype:
1558 case tok::annot_template_id:
1559 case tok::annot_typename:
1560 case tok::kw_typeof:
1561 case tok::kw___underlying_type:
1562 return true;
1563
1564 // elaborated-type-specifier
1565 case tok::kw_class:
1566 case tok::kw_struct:
1567 case tok::kw_union:
1568 case tok::kw___interface:
1569 case tok::kw_enum:
1570 return true;
1571
1572 // simple-type-specifier
1573 case tok::kw_char:
1574 case tok::kw_wchar_t:
1575 case tok::kw_char16_t:
1576 case tok::kw_char32_t:
1577 case tok::kw_bool:
1578 case tok::kw_short:
1579 case tok::kw_int:
1580 case tok::kw_long:
1581 case tok::kw___int64:
1582 case tok::kw___int128:
1583 case tok::kw_signed:
1584 case tok::kw_unsigned:
1585 case tok::kw_half:
1586 case tok::kw_float:
1587 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001588 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001589 case tok::kw_void:
1590 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001591 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001592 return true;
1593
1594 case tok::kw_auto:
1595 return getLangOpts().CPlusPlus11;
1596
1597 case tok::kw__Atomic:
1598 // "_Atomic foo"
1599 return NextToken().is(tok::l_paren);
1600
1601 default:
1602 return false;
1603 }
1604}
1605
Guy Benyei11169dd2012-12-18 14:30:41 +00001606/// [GNU] typeof-specifier:
1607/// 'typeof' '(' expressions ')'
1608/// 'typeof' '(' type-name ')'
1609///
1610Parser::TPResult Parser::TryParseTypeofSpecifier() {
1611 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1612 ConsumeToken();
1613
1614 assert(Tok.is(tok::l_paren) && "Expected '('");
1615 // Parse through the parens after 'typeof'.
1616 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001617 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001618 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001619
Richard Smithee390432014-05-16 01:56:53 +00001620 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001621}
1622
1623/// [ObjC] protocol-qualifiers:
1624//// '<' identifier-list '>'
1625Parser::TPResult Parser::TryParseProtocolQualifiers() {
1626 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1627 ConsumeToken();
1628 do {
1629 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001630 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 ConsumeToken();
1632
1633 if (Tok.is(tok::comma)) {
1634 ConsumeToken();
1635 continue;
1636 }
1637
1638 if (Tok.is(tok::greater)) {
1639 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001640 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001641 }
1642 } while (false);
1643
Richard Smithee390432014-05-16 01:56:53 +00001644 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001645}
1646
Guy Benyei11169dd2012-12-18 14:30:41 +00001647/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1648/// a constructor-style initializer, when parsing declaration statements.
1649/// Returns true for function declarator and false for constructor-style
1650/// initializer.
1651/// If during the disambiguation process a parsing error is encountered,
1652/// the function returns true to let the declaration parsing code handle it.
1653///
1654/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1655/// exception-specification[opt]
1656///
1657bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1658
1659 // C++ 8.2p1:
1660 // The ambiguity arising from the similarity between a function-style cast and
1661 // a declaration mentioned in 6.8 can also occur in the context of a
1662 // declaration. In that context, the choice is between a function declaration
1663 // with a redundant set of parentheses around a parameter name and an object
1664 // declaration with a function-style cast as the initializer. Just as for the
1665 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1666 // that could possibly be a declaration a declaration.
1667
Richard Smith91b73f22016-06-29 21:06:51 +00001668 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001669
1670 ConsumeParen();
1671 bool InvalidAsDeclaration = false;
1672 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001673 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001675 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001676 else {
1677 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001678 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1679 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1680 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1681 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001682 // The next token cannot appear after a constructor-style initializer,
1683 // and can appear next in a function definition. This must be a function
1684 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001685 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 else if (InvalidAsDeclaration)
1687 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001688 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001689 }
1690 }
1691
Richard Smithee390432014-05-16 01:56:53 +00001692 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001693 *IsAmbiguous = true;
1694
1695 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001696 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001697}
1698
1699/// parameter-declaration-clause:
1700/// parameter-declaration-list[opt] '...'[opt]
1701/// parameter-declaration-list ',' '...'
1702///
1703/// parameter-declaration-list:
1704/// parameter-declaration
1705/// parameter-declaration-list ',' parameter-declaration
1706///
1707/// parameter-declaration:
1708/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1709/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1710/// '=' assignment-expression
1711/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1712/// attributes[opt]
1713/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1714/// attributes[opt] '=' assignment-expression
1715///
1716Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001717Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1718 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001719
1720 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001721 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001722
1723 // parameter-declaration-list[opt] '...'[opt]
1724 // parameter-declaration-list ',' '...'
1725 //
1726 // parameter-declaration-list:
1727 // parameter-declaration
1728 // parameter-declaration-list ',' parameter-declaration
1729 //
1730 while (1) {
1731 // '...'[opt]
1732 if (Tok.is(tok::ellipsis)) {
1733 ConsumeToken();
1734 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001735 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 else
Richard Smithee390432014-05-16 01:56:53 +00001737 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 }
1739
1740 // An attribute-specifier-seq here is a sign of a function declarator.
1741 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1742 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001743 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001744
1745 ParsedAttributes attrs(AttrFactory);
1746 MaybeParseMicrosoftAttributes(attrs);
1747
1748 // decl-specifier-seq
1749 // A parameter-declaration's initializer must be preceded by an '=', so
1750 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001751 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001752 InvalidAsDeclaration);
1753
Richard Smithee390432014-05-16 01:56:53 +00001754 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001755 // Consume the decl-specifier-seq. We have to look past it, since a
1756 // type-id might appear here in a template argument.
1757 bool SeenType = false;
1758 do {
1759 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001760 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1761 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001762
1763 // If we see a parameter name, this can't be a template argument.
1764 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001765 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001766
Richard Smithee390432014-05-16 01:56:53 +00001767 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001768 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001769 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001770 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001771 } while (TPR != TPResult::False);
1772 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001773 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001774 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1775 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001776 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001777 return TPR;
1778
1779 // declarator
1780 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001781 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001782 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 return TPR;
1784
1785 // [GNU] attributes[opt]
1786 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001787 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001788
Richard Smith1fff95c2013-09-12 23:28:08 +00001789 // If we're disambiguating a template argument in a default argument in
1790 // a class definition versus a parameter declaration, an '=' here
1791 // disambiguates the parse one way or the other.
1792 // If this is a parameter, it must have a default argument because
1793 // (a) the previous parameter did, and
1794 // (b) this must be the first declaration of the function, so we can't
1795 // inherit any default arguments from elsewhere.
1796 // If we see an ')', then we've reached the end of a
1797 // parameter-declaration-clause, and the last param is missing its default
1798 // argument.
1799 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001800 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1801 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001802
Guy Benyei11169dd2012-12-18 14:30:41 +00001803 if (Tok.is(tok::equal)) {
1804 // '=' assignment-expression
1805 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001806 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001807 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001808 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 }
1810
1811 if (Tok.is(tok::ellipsis)) {
1812 ConsumeToken();
1813 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001814 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001815 else
Richard Smithee390432014-05-16 01:56:53 +00001816 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001817 }
1818
Alp Toker97650562014-01-10 11:19:30 +00001819 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001820 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 }
1822
Richard Smithee390432014-05-16 01:56:53 +00001823 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001824}
1825
1826/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1827/// parsing as a function declarator.
1828/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001829/// return TPResult::Ambiguous, otherwise it will return either False() or
1830/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001831///
1832/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1833/// exception-specification[opt]
1834///
1835/// exception-specification:
1836/// 'throw' '(' type-id-list[opt] ')'
1837///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001838Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001839
1840 // The '(' is already parsed.
1841
1842 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001843 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1844 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001845
Justin Bognerd26f95b2015-02-23 22:36:28 +00001846 if (TPR == TPResult::False || TPR == TPResult::Error)
1847 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001848
1849 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001850 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001851 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001852
1853 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001854 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 ConsumeToken();
1856
1857 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001858 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001859 ConsumeToken();
1860
1861 // exception-specification
1862 if (Tok.is(tok::kw_throw)) {
1863 ConsumeToken();
1864 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001865 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001866
1867 // Parse through the parens after 'throw'.
1868 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001869 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001870 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 }
1872 if (Tok.is(tok::kw_noexcept)) {
1873 ConsumeToken();
1874 // Possibly an expression as well.
1875 if (Tok.is(tok::l_paren)) {
1876 // Find the matching rparen.
1877 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001878 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001879 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 }
1881 }
1882
Richard Smithee390432014-05-16 01:56:53 +00001883 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001884}
1885
1886/// '[' constant-expression[opt] ']'
1887///
1888Parser::TPResult Parser::TryParseBracketDeclarator() {
1889 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001890 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001891 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001892
Richard Smithee390432014-05-16 01:56:53 +00001893 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001894}