blob: 0ea3f8d951793d165b90b42c6b880c9467ac2e58 [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,
Erich Keane757d3172016-11-02 18:29:35 +0000912 tok::kw___regcall, 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:
Erich Keane757d3172016-11-02 18:29:35 +00001061 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001062 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001063 case tok::kw___unaligned:
1064 case tok::kw___vector:
1065 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +00001066 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +00001067 case tok::kw__Atomic:
Alexey Bader954ba212016-04-08 13:40:33 +00001068#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00001069#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00001070 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +00001071 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001072
1073 default:
1074 break;
1075 }
1076
Richard Smithee390432014-05-16 01:56:53 +00001077 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001078}
1079
1080bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1081 return std::find(TentativelyDeclaredIdentifiers.begin(),
1082 TentativelyDeclaredIdentifiers.end(), II)
1083 != TentativelyDeclaredIdentifiers.end();
1084}
1085
Kaelyn Takata445b0652014-11-05 00:09:29 +00001086namespace {
1087class TentativeParseCCC : public CorrectionCandidateCallback {
1088public:
1089 TentativeParseCCC(const Token &Next) {
1090 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001091 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1092 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001093 }
1094
1095 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1096 // Reject any candidate that only resolves to instance members since they
1097 // aren't viable as standalone identifiers instead of member references.
1098 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1099 std::all_of(Candidate.begin(), Candidate.end(),
1100 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1101 return false;
1102
1103 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1104 }
1105};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001106}
Richard Smithee390432014-05-16 01:56:53 +00001107/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1108/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1109/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001110/// if a parsing error was found and reported.
1111///
1112/// If HasMissingTypename is provided, a name with a dependent scope specifier
1113/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1114/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1115/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001116/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001117/// such cases.
1118///
1119/// decl-specifier:
1120/// storage-class-specifier
1121/// type-specifier
1122/// function-specifier
1123/// 'friend'
1124/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001125/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001126/// [GNU] attributes declaration-specifiers[opt]
1127///
1128/// storage-class-specifier:
1129/// 'register'
1130/// 'static'
1131/// 'extern'
1132/// 'mutable'
1133/// 'auto'
1134/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001135/// [C++11] 'thread_local'
1136/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001137///
1138/// function-specifier:
1139/// 'inline'
1140/// 'virtual'
1141/// 'explicit'
1142///
1143/// typedef-name:
1144/// identifier
1145///
1146/// type-specifier:
1147/// simple-type-specifier
1148/// class-specifier
1149/// enum-specifier
1150/// elaborated-type-specifier
1151/// typename-specifier
1152/// cv-qualifier
1153///
1154/// simple-type-specifier:
1155/// '::'[opt] nested-name-specifier[opt] type-name
1156/// '::'[opt] nested-name-specifier 'template'
1157/// simple-template-id [TODO]
1158/// 'char'
1159/// 'wchar_t'
1160/// 'bool'
1161/// 'short'
1162/// 'int'
1163/// 'long'
1164/// 'signed'
1165/// 'unsigned'
1166/// 'float'
1167/// 'double'
1168/// 'void'
1169/// [GNU] typeof-specifier
1170/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001171/// [C++11] 'auto'
Richard Smithe301ba22015-11-11 02:02:15 +00001172/// [GNU] '__auto_type'
Richard Smithb4a9e862013-04-12 22:46:28 +00001173/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001174/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001175///
1176/// type-name:
1177/// class-name
1178/// enum-name
1179/// typedef-name
1180///
1181/// elaborated-type-specifier:
1182/// class-key '::'[opt] nested-name-specifier[opt] identifier
1183/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1184/// simple-template-id
1185/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1186///
1187/// enum-name:
1188/// identifier
1189///
1190/// enum-specifier:
1191/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1192/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1193///
1194/// class-specifier:
1195/// class-head '{' member-specification[opt] '}'
1196///
1197/// class-head:
1198/// class-key identifier[opt] base-clause[opt]
1199/// class-key nested-name-specifier identifier base-clause[opt]
1200/// class-key nested-name-specifier[opt] simple-template-id
1201/// base-clause[opt]
1202///
1203/// class-key:
1204/// 'class'
1205/// 'struct'
1206/// 'union'
1207///
1208/// cv-qualifier:
1209/// 'const'
1210/// 'volatile'
1211/// [GNU] restrict
1212///
1213Parser::TPResult
1214Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1215 bool *HasMissingTypename) {
1216 switch (Tok.getKind()) {
1217 case tok::identifier: {
1218 // Check for need to substitute AltiVec __vector keyword
1219 // for "vector" identifier.
1220 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001221 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001222
1223 const Token &Next = NextToken();
1224 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1225 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001226 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001227
1228 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1229 // Determine whether this is a valid expression. If not, we will hit
1230 // a parse error one way or another. In that case, tell the caller that
1231 // this is ambiguous. Typo-correct to type and expression keywords and
1232 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001233 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001234 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001236 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001238 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001239 case ANK_TemplateName:
1240 // A bare type template-name which can't be a template template
1241 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001242 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001244 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 case ANK_Success:
1246 break;
1247 }
1248 assert(Tok.isNot(tok::identifier) &&
1249 "TryAnnotateName succeeded without producing an annotation");
1250 } else {
1251 // This might possibly be a type with a dependent scope specifier and
1252 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1253 // since it will annotate as a primary expression, and we want to use the
1254 // "missing 'typename'" logic.
1255 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001256 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001257 // If annotation failed, assume it's a non-type.
1258 // FIXME: If this happens due to an undeclared identifier, treat it as
1259 // ambiguous.
1260 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001261 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001262 }
1263
1264 // We annotated this token as something. Recurse to handle whatever we got.
1265 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1266 }
1267
1268 case tok::kw_typename: // typename T::type
1269 // Annotate typenames and C++ scope specifiers. If we get one, just
1270 // recurse to handle whatever we get.
1271 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001272 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001273 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1274
1275 case tok::coloncolon: { // ::foo::bar
1276 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001277 if (Next.isOneOf(tok::kw_new, // ::new
1278 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001279 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001280 }
1281 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001282 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001283 case tok::kw_decltype:
1284 // Annotate typenames and C++ scope specifiers. If we get one, just
1285 // recurse to handle whatever we get.
1286 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001287 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001288 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1289
1290 // decl-specifier:
1291 // storage-class-specifier
1292 // type-specifier
1293 // function-specifier
1294 // 'friend'
1295 // 'typedef'
1296 // 'constexpr'
Hubert Tong375f00a2015-06-30 12:14:52 +00001297 // 'concept'
Guy Benyei11169dd2012-12-18 14:30:41 +00001298 case tok::kw_friend:
1299 case tok::kw_typedef:
1300 case tok::kw_constexpr:
Hubert Tong375f00a2015-06-30 12:14:52 +00001301 case tok::kw_concept:
Guy Benyei11169dd2012-12-18 14:30:41 +00001302 // storage-class-specifier
1303 case tok::kw_register:
1304 case tok::kw_static:
1305 case tok::kw_extern:
1306 case tok::kw_mutable:
1307 case tok::kw_auto:
1308 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001309 case tok::kw_thread_local:
1310 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 // function-specifier
1312 case tok::kw_inline:
1313 case tok::kw_virtual:
1314 case tok::kw_explicit:
1315
1316 // Modules
1317 case tok::kw___module_private__:
1318
1319 // Debugger support
1320 case tok::kw___unknown_anytype:
1321
1322 // type-specifier:
1323 // simple-type-specifier
1324 // class-specifier
1325 // enum-specifier
1326 // elaborated-type-specifier
1327 // typename-specifier
1328 // cv-qualifier
1329
1330 // class-specifier
1331 // elaborated-type-specifier
1332 case tok::kw_class:
1333 case tok::kw_struct:
1334 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001335 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 // enum-specifier
1337 case tok::kw_enum:
1338 // cv-qualifier
1339 case tok::kw_const:
1340 case tok::kw_volatile:
1341
1342 // GNU
1343 case tok::kw_restrict:
1344 case tok::kw__Complex:
1345 case tok::kw___attribute:
Richard Smithe301ba22015-11-11 02:02:15 +00001346 case tok::kw___auto_type:
Richard Smithee390432014-05-16 01:56:53 +00001347 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001348
1349 // Microsoft
1350 case tok::kw___declspec:
1351 case tok::kw___cdecl:
1352 case tok::kw___stdcall:
1353 case tok::kw___fastcall:
1354 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00001355 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001356 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001358 case tok::kw___sptr:
1359 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 case tok::kw___ptr64:
1361 case tok::kw___ptr32:
1362 case tok::kw___forceinline:
1363 case tok::kw___unaligned:
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001364 case tok::kw__Nonnull:
1365 case tok::kw__Nullable:
1366 case tok::kw__Null_unspecified:
Douglas Gregorab209d82015-07-07 03:58:42 +00001367 case tok::kw___kindof:
Richard Smithee390432014-05-16 01:56:53 +00001368 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001369
1370 // Borland
1371 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001372 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001373
1374 // AltiVec
1375 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001376 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001377
1378 case tok::annot_template_id: {
1379 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1380 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001381 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001382 CXXScopeSpec SS;
1383 AnnotateTemplateIdTokenAsType();
1384 assert(Tok.is(tok::annot_typename));
1385 goto case_typename;
1386 }
1387
1388 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1389 // We've already annotated a scope; try to annotate a type.
1390 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001391 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 if (!Tok.is(tok::annot_typename)) {
1393 // If the next token is an identifier or a type qualifier, then this
1394 // can't possibly be a valid expression either.
1395 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1396 CXXScopeSpec SS;
1397 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1398 Tok.getAnnotationRange(),
1399 SS);
1400 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
Richard Smith4556ebe2016-06-29 21:12:37 +00001401 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 ConsumeToken();
1403 ConsumeToken();
1404 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001405 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 if (!isIdentifier)
1407 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1408 HasMissingTypename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001409
1410 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001411 TPR == TPResult::True || TPR == TPResult::Error)
1412 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001413
1414 if (HasMissingTypename) {
1415 // We can't tell whether this is a missing 'typename' or a valid
1416 // expression.
1417 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001418 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001419 }
Richard Smithc7a05a92016-06-29 21:17:59 +00001420
1421 // FIXME: Fails to either revert or commit the tentative parse!
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 } else {
1423 // Try to resolve the name. If it doesn't exist, assume it was
1424 // intended to name a type and keep disambiguating.
1425 switch (TryAnnotateName(false /* SS is not dependent */)) {
1426 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001427 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001428 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001429 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001430 case ANK_TemplateName:
1431 // A bare type template-name which can't be a template template
1432 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001433 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001435 return HasMissingTypename ? TPResult::Ambiguous
1436 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 case ANK_Success:
1438 // Annotated it, check again.
1439 assert(Tok.isNot(tok::annot_cxxscope) ||
1440 NextToken().isNot(tok::identifier));
1441 return isCXXDeclarationSpecifier(BracedCastResult,
1442 HasMissingTypename);
1443 }
1444 }
1445 }
Richard Smithee390432014-05-16 01:56:53 +00001446 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001447 }
1448 // If that succeeded, fallthrough into the generic simple-type-id case.
1449
1450 // The ambiguity resides in a simple-type-specifier/typename-specifier
1451 // followed by a '('. The '(' could either be the start of:
1452 //
1453 // direct-declarator:
1454 // '(' declarator ')'
1455 //
1456 // direct-abstract-declarator:
1457 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1458 // exception-specification[opt]
1459 // '(' abstract-declarator ')'
1460 //
1461 // or part of a function-style cast expression:
1462 //
1463 // simple-type-specifier '(' expression-list[opt] ')'
1464 //
1465
1466 // simple-type-specifier:
1467
1468 case tok::annot_typename:
1469 case_typename:
1470 // In Objective-C, we might have a protocol-qualified type.
1471 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001472 // Tentatively parse the protocol qualifiers.
Richard Smith91b73f22016-06-29 21:06:51 +00001473 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001474 ConsumeToken(); // The type token
1475
1476 TPResult TPR = TryParseProtocolQualifiers();
1477 bool isFollowedByParen = Tok.is(tok::l_paren);
1478 bool isFollowedByBrace = Tok.is(tok::l_brace);
1479
Richard Smithee390432014-05-16 01:56:53 +00001480 if (TPR == TPResult::Error)
1481 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001482
1483 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001484 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001485
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001486 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001487 return BracedCastResult;
1488
Richard Smithee390432014-05-16 01:56:53 +00001489 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001490 }
1491
1492 case tok::kw_char:
1493 case tok::kw_wchar_t:
1494 case tok::kw_char16_t:
1495 case tok::kw_char32_t:
1496 case tok::kw_bool:
1497 case tok::kw_short:
1498 case tok::kw_int:
1499 case tok::kw_long:
1500 case tok::kw___int64:
1501 case tok::kw___int128:
1502 case tok::kw_signed:
1503 case tok::kw_unsigned:
1504 case tok::kw_half:
1505 case tok::kw_float:
1506 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001507 case tok::kw___float128:
Guy Benyei11169dd2012-12-18 14:30:41 +00001508 case tok::kw_void:
1509 case tok::annot_decltype:
1510 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001511 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001512
1513 // This is a function-style cast in all cases we disambiguate other than
1514 // one:
1515 // struct S {
1516 // enum E : int { a = 4 }; // enum
1517 // enum E : int { 4 }; // bit-field
1518 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001519 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 return BracedCastResult;
1521
1522 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001523 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001524
Richard Smithee390432014-05-16 01:56:53 +00001525 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001526
1527 // GNU typeof support.
1528 case tok::kw_typeof: {
1529 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001530 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001531
Richard Smith91b73f22016-06-29 21:06:51 +00001532 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001533
1534 TPResult TPR = TryParseTypeofSpecifier();
1535 bool isFollowedByParen = Tok.is(tok::l_paren);
1536 bool isFollowedByBrace = Tok.is(tok::l_brace);
1537
Richard Smithee390432014-05-16 01:56:53 +00001538 if (TPR == TPResult::Error)
1539 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001540
1541 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001542 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001543
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001544 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 return BracedCastResult;
1546
Richard Smithee390432014-05-16 01:56:53 +00001547 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 }
1549
1550 // C++0x type traits support
1551 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001552 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001553
1554 // C11 _Atomic
1555 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001556 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001557
1558 default:
Richard Smithee390432014-05-16 01:56:53 +00001559 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001560 }
1561}
1562
Richard Smith1fff95c2013-09-12 23:28:08 +00001563bool Parser::isCXXDeclarationSpecifierAType() {
1564 switch (Tok.getKind()) {
1565 // typename-specifier
1566 case tok::annot_decltype:
1567 case tok::annot_template_id:
1568 case tok::annot_typename:
1569 case tok::kw_typeof:
1570 case tok::kw___underlying_type:
1571 return true;
1572
1573 // elaborated-type-specifier
1574 case tok::kw_class:
1575 case tok::kw_struct:
1576 case tok::kw_union:
1577 case tok::kw___interface:
1578 case tok::kw_enum:
1579 return true;
1580
1581 // simple-type-specifier
1582 case tok::kw_char:
1583 case tok::kw_wchar_t:
1584 case tok::kw_char16_t:
1585 case tok::kw_char32_t:
1586 case tok::kw_bool:
1587 case tok::kw_short:
1588 case tok::kw_int:
1589 case tok::kw_long:
1590 case tok::kw___int64:
1591 case tok::kw___int128:
1592 case tok::kw_signed:
1593 case tok::kw_unsigned:
1594 case tok::kw_half:
1595 case tok::kw_float:
1596 case tok::kw_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001597 case tok::kw___float128:
Richard Smith1fff95c2013-09-12 23:28:08 +00001598 case tok::kw_void:
1599 case tok::kw___unknown_anytype:
Richard Smithe301ba22015-11-11 02:02:15 +00001600 case tok::kw___auto_type:
Richard Smith1fff95c2013-09-12 23:28:08 +00001601 return true;
1602
1603 case tok::kw_auto:
1604 return getLangOpts().CPlusPlus11;
1605
1606 case tok::kw__Atomic:
1607 // "_Atomic foo"
1608 return NextToken().is(tok::l_paren);
1609
1610 default:
1611 return false;
1612 }
1613}
1614
Guy Benyei11169dd2012-12-18 14:30:41 +00001615/// [GNU] typeof-specifier:
1616/// 'typeof' '(' expressions ')'
1617/// 'typeof' '(' type-name ')'
1618///
1619Parser::TPResult Parser::TryParseTypeofSpecifier() {
1620 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1621 ConsumeToken();
1622
1623 assert(Tok.is(tok::l_paren) && "Expected '('");
1624 // Parse through the parens after 'typeof'.
1625 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001626 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001627 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001628
Richard Smithee390432014-05-16 01:56:53 +00001629 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001630}
1631
1632/// [ObjC] protocol-qualifiers:
1633//// '<' identifier-list '>'
1634Parser::TPResult Parser::TryParseProtocolQualifiers() {
1635 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1636 ConsumeToken();
1637 do {
1638 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001639 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001640 ConsumeToken();
1641
1642 if (Tok.is(tok::comma)) {
1643 ConsumeToken();
1644 continue;
1645 }
1646
1647 if (Tok.is(tok::greater)) {
1648 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001649 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001650 }
1651 } while (false);
1652
Richard Smithee390432014-05-16 01:56:53 +00001653 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001654}
1655
Guy Benyei11169dd2012-12-18 14:30:41 +00001656/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1657/// a constructor-style initializer, when parsing declaration statements.
1658/// Returns true for function declarator and false for constructor-style
1659/// initializer.
1660/// If during the disambiguation process a parsing error is encountered,
1661/// the function returns true to let the declaration parsing code handle it.
1662///
1663/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1664/// exception-specification[opt]
1665///
1666bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1667
1668 // C++ 8.2p1:
1669 // The ambiguity arising from the similarity between a function-style cast and
1670 // a declaration mentioned in 6.8 can also occur in the context of a
1671 // declaration. In that context, the choice is between a function declaration
1672 // with a redundant set of parentheses around a parameter name and an object
1673 // declaration with a function-style cast as the initializer. Just as for the
1674 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1675 // that could possibly be a declaration a declaration.
1676
Richard Smith91b73f22016-06-29 21:06:51 +00001677 RevertingTentativeParsingAction PA(*this);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
1679 ConsumeParen();
1680 bool InvalidAsDeclaration = false;
1681 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001682 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001684 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 else {
1686 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001687 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1688 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1689 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1690 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001691 // The next token cannot appear after a constructor-style initializer,
1692 // and can appear next in a function definition. This must be a function
1693 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001694 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001695 else if (InvalidAsDeclaration)
1696 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001697 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698 }
1699 }
1700
Richard Smithee390432014-05-16 01:56:53 +00001701 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001702 *IsAmbiguous = true;
1703
1704 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001705 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001706}
1707
1708/// parameter-declaration-clause:
1709/// parameter-declaration-list[opt] '...'[opt]
1710/// parameter-declaration-list ',' '...'
1711///
1712/// parameter-declaration-list:
1713/// parameter-declaration
1714/// parameter-declaration-list ',' parameter-declaration
1715///
1716/// parameter-declaration:
1717/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1718/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1719/// '=' assignment-expression
1720/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1721/// attributes[opt]
1722/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1723/// attributes[opt] '=' assignment-expression
1724///
1725Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001726Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1727 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001728
1729 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001730 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001731
1732 // parameter-declaration-list[opt] '...'[opt]
1733 // parameter-declaration-list ',' '...'
1734 //
1735 // parameter-declaration-list:
1736 // parameter-declaration
1737 // parameter-declaration-list ',' parameter-declaration
1738 //
1739 while (1) {
1740 // '...'[opt]
1741 if (Tok.is(tok::ellipsis)) {
1742 ConsumeToken();
1743 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001744 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 else
Richard Smithee390432014-05-16 01:56:53 +00001746 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001747 }
1748
1749 // An attribute-specifier-seq here is a sign of a function declarator.
1750 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1751 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001752 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001753
1754 ParsedAttributes attrs(AttrFactory);
1755 MaybeParseMicrosoftAttributes(attrs);
1756
1757 // decl-specifier-seq
1758 // A parameter-declaration's initializer must be preceded by an '=', so
1759 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001760 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001761 InvalidAsDeclaration);
1762
Richard Smithee390432014-05-16 01:56:53 +00001763 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001764 // Consume the decl-specifier-seq. We have to look past it, since a
1765 // type-id might appear here in a template argument.
1766 bool SeenType = false;
1767 do {
1768 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001769 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1770 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001771
1772 // If we see a parameter name, this can't be a template argument.
1773 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001774 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001775
Richard Smithee390432014-05-16 01:56:53 +00001776 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001777 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001778 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001779 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001780 } while (TPR != TPResult::False);
1781 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001782 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001783 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1784 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001785 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 return TPR;
1787
1788 // declarator
1789 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001790 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001791 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 return TPR;
1793
1794 // [GNU] attributes[opt]
1795 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001796 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001797
Richard Smith1fff95c2013-09-12 23:28:08 +00001798 // If we're disambiguating a template argument in a default argument in
1799 // a class definition versus a parameter declaration, an '=' here
1800 // disambiguates the parse one way or the other.
1801 // If this is a parameter, it must have a default argument because
1802 // (a) the previous parameter did, and
1803 // (b) this must be the first declaration of the function, so we can't
1804 // inherit any default arguments from elsewhere.
1805 // If we see an ')', then we've reached the end of a
1806 // parameter-declaration-clause, and the last param is missing its default
1807 // argument.
1808 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001809 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1810 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001811
Guy Benyei11169dd2012-12-18 14:30:41 +00001812 if (Tok.is(tok::equal)) {
1813 // '=' assignment-expression
1814 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001815 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001816 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001817 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 }
1819
1820 if (Tok.is(tok::ellipsis)) {
1821 ConsumeToken();
1822 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001823 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001824 else
Richard Smithee390432014-05-16 01:56:53 +00001825 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 }
1827
Alp Toker97650562014-01-10 11:19:30 +00001828 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001830 }
1831
Richard Smithee390432014-05-16 01:56:53 +00001832 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001833}
1834
1835/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1836/// parsing as a function declarator.
1837/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001838/// return TPResult::Ambiguous, otherwise it will return either False() or
1839/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001840///
1841/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1842/// exception-specification[opt]
1843///
1844/// exception-specification:
1845/// 'throw' '(' type-id-list[opt] ')'
1846///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001847Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001848
1849 // The '(' is already parsed.
1850
1851 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001852 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1853 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001854
Justin Bognerd26f95b2015-02-23 22:36:28 +00001855 if (TPR == TPResult::False || TPR == TPResult::Error)
1856 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001857
1858 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001859 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001860 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001861
1862 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001863 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 ConsumeToken();
1865
1866 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001867 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 ConsumeToken();
1869
1870 // exception-specification
1871 if (Tok.is(tok::kw_throw)) {
1872 ConsumeToken();
1873 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001874 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001875
1876 // Parse through the parens after 'throw'.
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 if (Tok.is(tok::kw_noexcept)) {
1882 ConsumeToken();
1883 // Possibly an expression as well.
1884 if (Tok.is(tok::l_paren)) {
1885 // Find the matching rparen.
1886 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001887 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001888 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 }
1890 }
1891
Richard Smithee390432014-05-16 01:56:53 +00001892 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001893}
1894
1895/// '[' constant-expression[opt] ']'
1896///
1897Parser::TPResult Parser::TryParseBracketDeclarator() {
1898 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001899 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001900 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001901
Richard Smithee390432014-05-16 01:56:53 +00001902 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001903}