blob: e21409a62fcba0ac0ac9b722a96b08c6bbd9058e [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...
128
129 TentativeParsingAction PA(*this);
130 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
131 PA.Revert();
132
133 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000134 if (TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000135 return true;
136
137 // Declarations take precedence over expressions.
Richard Smithee390432014-05-16 01:56:53 +0000138 if (TPR == TPResult::Ambiguous)
139 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000140
Richard Smithee390432014-05-16 01:56:53 +0000141 assert(TPR == TPResult::True || TPR == TPResult::False);
142 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000143}
144
Richard Smith1fff95c2013-09-12 23:28:08 +0000145/// Try to consume a token sequence that we've already identified as
146/// (potentially) starting a decl-specifier.
147Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
148 switch (Tok.getKind()) {
149 case tok::kw__Atomic:
150 if (NextToken().isNot(tok::l_paren)) {
151 ConsumeToken();
152 break;
153 }
154 // Fall through.
155 case tok::kw_typeof:
156 case tok::kw___attribute:
157 case tok::kw___underlying_type: {
158 ConsumeToken();
159 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000160 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000161 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000162 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000163 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000164 break;
165 }
166
167 case tok::kw_class:
168 case tok::kw_struct:
169 case tok::kw_union:
170 case tok::kw___interface:
171 case tok::kw_enum:
172 // elaborated-type-specifier:
173 // class-key attribute-specifier-seq[opt]
174 // nested-name-specifier[opt] identifier
175 // class-key nested-name-specifier[opt] template[opt] simple-template-id
176 // enum nested-name-specifier[opt] identifier
177 //
178 // FIXME: We don't support class-specifiers nor enum-specifiers here.
179 ConsumeToken();
180
181 // Skip attributes.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000182 while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
183 tok::kw_alignas)) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000184 if (Tok.is(tok::l_square)) {
185 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000186 if (!SkipUntil(tok::r_square))
Richard Smithee390432014-05-16 01:56:53 +0000187 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000188 } else {
189 ConsumeToken();
190 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +0000191 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000192 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000193 if (!SkipUntil(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000194 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000195 }
196 }
197
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000198 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
199 tok::annot_template_id) &&
Nico Weberc29c4832014-12-28 23:24:02 +0000200 TryAnnotateCXXScopeToken())
Richard Smithee390432014-05-16 01:56:53 +0000201 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000202 if (Tok.is(tok::annot_cxxscope))
203 ConsumeToken();
204 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Richard Smithee390432014-05-16 01:56:53 +0000205 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000206 ConsumeToken();
207 break;
208
209 case tok::annot_cxxscope:
210 ConsumeToken();
211 // Fall through.
212 default:
213 ConsumeToken();
214
215 if (getLangOpts().ObjC1 && Tok.is(tok::less))
216 return TryParseProtocolQualifiers();
217 break;
218 }
219
Richard Smithee390432014-05-16 01:56:53 +0000220 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000221}
222
Guy Benyei11169dd2012-12-18 14:30:41 +0000223/// simple-declaration:
224/// decl-specifier-seq init-declarator-list[opt] ';'
225///
226/// (if AllowForRangeDecl specified)
227/// for ( for-range-declaration : for-range-initializer ) statement
228/// for-range-declaration:
229/// attribute-specifier-seqopt type-specifier-seq declarator
230///
231Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000232 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
233 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000234
235 // Two decl-specifiers in a row conclusively disambiguate this as being a
236 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
237 // overwhelmingly common case that the next token is a '('.
238 if (Tok.isNot(tok::l_paren)) {
239 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000240 if (TPR == TPResult::Ambiguous)
241 return TPResult::True;
242 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000243 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000244 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000245 }
246
247 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000248 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000249 return TPR;
250
251 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000252 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000253
Richard Smithee390432014-05-16 01:56:53 +0000254 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000255}
256
Richard Smith22c7c412013-03-20 03:35:02 +0000257/// Tentatively parse an init-declarator-list in order to disambiguate it from
258/// an expression.
259///
Guy Benyei11169dd2012-12-18 14:30:41 +0000260/// init-declarator-list:
261/// init-declarator
262/// init-declarator-list ',' init-declarator
263///
264/// init-declarator:
265/// declarator initializer[opt]
266/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
267///
Richard Smith22c7c412013-03-20 03:35:02 +0000268/// initializer:
269/// brace-or-equal-initializer
270/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000271///
Richard Smith22c7c412013-03-20 03:35:02 +0000272/// brace-or-equal-initializer:
273/// '=' initializer-clause
274/// [C++11] braced-init-list
275///
276/// initializer-clause:
277/// assignment-expression
278/// braced-init-list
279///
280/// braced-init-list:
281/// '{' initializer-list ','[opt] '}'
282/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000283///
284Parser::TPResult Parser::TryParseInitDeclaratorList() {
285 while (1) {
286 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000287 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000288 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000289 return TPR;
290
291 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000292 if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000293 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000294
295 // initializer[opt]
296 if (Tok.is(tok::l_paren)) {
297 // Parse through the parens.
298 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000299 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000300 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000301 } else if (Tok.is(tok::l_brace)) {
302 // A left-brace here is sufficient to disambiguate the parse; an
303 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000304 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000305 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000306 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000307 // encountered; they just conclude that we have a declaration.
308 // EDG parses the initializer completely, which is the proper behavior
309 // for this case.
310 //
311 // At present, Clang follows MSVC and g++, since the parser does not have
312 // the ability to parse an expression fully without recording the
313 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000314 // FIXME: Handle this case correctly.
315 //
316 // Also allow 'in' after an Objective-C declaration as in:
317 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000318 // context of parsing for-init-statement of a foreach statement only. But,
319 // in any other context 'in' is invalid after a declaration and parser
320 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000321 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000322 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000323 }
324
Alp Toker97650562014-01-10 11:19:30 +0000325 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000326 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 }
328
Richard Smithee390432014-05-16 01:56:53 +0000329 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000330}
331
332/// isCXXConditionDeclaration - Disambiguates between a declaration or an
333/// expression for a condition of a if/switch/while/for statement.
334/// If during the disambiguation process a parsing error is encountered,
335/// the function returns true to let the declaration parsing code handle it.
336///
337/// condition:
338/// expression
339/// type-specifier-seq declarator '=' assignment-expression
340/// [C++11] type-specifier-seq declarator '=' initializer-clause
341/// [C++11] type-specifier-seq declarator braced-init-list
342/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
343/// '=' assignment-expression
344///
345bool Parser::isCXXConditionDeclaration() {
346 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000347 if (TPR != TPResult::Ambiguous)
348 return TPR != TPResult::False; // Returns true for TPResult::True or
349 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000350
351 // FIXME: Add statistics about the number of ambiguous statements encountered
352 // and how they were resolved (number of declarations+number of expressions).
353
354 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
355 // We need tentative parsing...
356
357 TentativeParsingAction PA(*this);
358
359 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000360 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000361 assert(Tok.is(tok::l_paren) && "Expected '('");
362
363 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000364 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000365
366 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000367 if (TPR == TPResult::Error)
368 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000369
Richard Smithee390432014-05-16 01:56:53 +0000370 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000371 // '='
372 // [GNU] simple-asm-expr[opt] attributes[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000373 if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000374 TPR = TPResult::True;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000375 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
Richard Smithee390432014-05-16 01:56:53 +0000376 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000377 else
Richard Smithee390432014-05-16 01:56:53 +0000378 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000379 }
380
381 PA.Revert();
382
Richard Smithee390432014-05-16 01:56:53 +0000383 assert(TPR == TPResult::True || TPR == TPResult::False);
384 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000385}
386
387 /// \brief Determine whether the next set of tokens contains a type-id.
388 ///
389 /// The context parameter states what context we're parsing right
390 /// now, which affects how this routine copes with the token
391 /// following the type-id. If the context is TypeIdInParens, we have
392 /// already parsed the '(' and we will cease lookahead when we hit
393 /// the corresponding ')'. If the context is
394 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
395 /// before this template argument, and will cease lookahead when we
396 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
397 /// and false for an expression. If during the disambiguation
398 /// process a parsing error is encountered, the function returns
399 /// true to let the declaration parsing code handle it.
400 ///
401 /// type-id:
402 /// type-specifier-seq abstract-declarator[opt]
403 ///
404bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
405
406 isAmbiguous = false;
407
408 // C++ 8.2p2:
409 // The ambiguity arising from the similarity between a function-style cast and
410 // a type-id can occur in different contexts. The ambiguity appears as a
411 // choice between a function-style cast expression and a declaration of a
412 // type. The resolution is that any construct that could possibly be a type-id
413 // in its syntactic context shall be considered a type-id.
414
415 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000416 if (TPR != TPResult::Ambiguous)
417 return TPR != TPResult::False; // Returns true for TPResult::True or
418 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
420 // FIXME: Add statistics about the number of ambiguous statements encountered
421 // and how they were resolved (number of declarations+number of expressions).
422
423 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
424 // We need tentative parsing...
425
426 TentativeParsingAction PA(*this);
427
428 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000429 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000430 assert(Tok.is(tok::l_paren) && "Expected '('");
431
432 // declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000433 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
Guy Benyei11169dd2012-12-18 14:30:41 +0000434
435 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000436 if (TPR == TPResult::Error)
437 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000438
Richard Smithee390432014-05-16 01:56:53 +0000439 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 // We are supposed to be inside parens, so if after the abstract declarator
441 // we encounter a ')' this is a type-id, otherwise it's an expression.
442 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000443 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000444 isAmbiguous = true;
445
446 // We are supposed to be inside a template argument, so if after
447 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
448 // ',', this is a type-id. Otherwise, it's an expression.
449 } else if (Context == TypeIdAsTemplateArgument &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000450 (Tok.isOneOf(tok::greater, tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000451 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000452 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000453 isAmbiguous = true;
454
455 } else
Richard Smithee390432014-05-16 01:56:53 +0000456 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000457 }
458
459 PA.Revert();
460
Richard Smithee390432014-05-16 01:56:53 +0000461 assert(TPR == TPResult::True || TPR == TPResult::False);
462 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000463}
464
465/// \brief Returns true if this is a C++11 attribute-specifier. Per
466/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
467/// always introduce an attribute. In Objective-C++11, this rule does not
468/// apply if either '[' begins a message-send.
469///
470/// If Disambiguate is true, we try harder to determine whether a '[[' starts
471/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
472///
473/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
474/// Obj-C message send or the start of an attribute. Otherwise, we assume it
475/// is not an Obj-C message send.
476///
477/// C++11 [dcl.attr.grammar]:
478///
479/// attribute-specifier:
480/// '[' '[' attribute-list ']' ']'
481/// alignment-specifier
482///
483/// attribute-list:
484/// attribute[opt]
485/// attribute-list ',' attribute[opt]
486/// attribute '...'
487/// attribute-list ',' attribute '...'
488///
489/// attribute:
490/// attribute-token attribute-argument-clause[opt]
491///
492/// attribute-token:
493/// identifier
494/// identifier '::' identifier
495///
496/// attribute-argument-clause:
497/// '(' balanced-token-seq ')'
498Parser::CXX11AttributeKind
499Parser::isCXX11AttributeSpecifier(bool Disambiguate,
500 bool OuterMightBeMessageSend) {
501 if (Tok.is(tok::kw_alignas))
502 return CAK_AttributeSpecifier;
503
504 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
505 return CAK_NotAttributeSpecifier;
506
507 // No tentative parsing if we don't need to look for ']]' or a lambda.
508 if (!Disambiguate && !getLangOpts().ObjC1)
509 return CAK_AttributeSpecifier;
510
511 TentativeParsingAction PA(*this);
512
513 // Opening brackets were checked for above.
514 ConsumeBracket();
515
516 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
517 if (!getLangOpts().ObjC1) {
518 ConsumeBracket();
519
Alexey Bataevee6507d2013-11-18 08:17:37 +0000520 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000521 IsAttribute &= Tok.is(tok::r_square);
522
523 PA.Revert();
524
525 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
526 }
527
528 // In Obj-C++11, we need to distinguish four situations:
529 // 1a) int x[[attr]]; C++11 attribute.
530 // 1b) [[attr]]; C++11 statement attribute.
531 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
532 // 3a) int x[[obj get]]; Message send in array size/index.
533 // 3b) [[Class alloc] init]; Message send in message send.
534 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
535 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
536
537 // If we have a lambda-introducer, then this is definitely not a message send.
538 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
539 // into the tentative attribute parse below.
540 LambdaIntroducer Intro;
541 if (!TryParseLambdaIntroducer(Intro)) {
542 // A lambda cannot end with ']]', and an attribute must.
543 bool IsAttribute = Tok.is(tok::r_square);
544
545 PA.Revert();
546
547 if (IsAttribute)
548 // Case 1: C++11 attribute.
549 return CAK_AttributeSpecifier;
550
551 if (OuterMightBeMessageSend)
552 // Case 4: Lambda in message send.
553 return CAK_NotAttributeSpecifier;
554
555 // Case 2: Lambda in array size / index.
556 return CAK_InvalidAttributeSpecifier;
557 }
558
559 ConsumeBracket();
560
561 // If we don't have a lambda-introducer, then we have an attribute or a
562 // message-send.
563 bool IsAttribute = true;
564 while (Tok.isNot(tok::r_square)) {
565 if (Tok.is(tok::comma)) {
566 // Case 1: Stray commas can only occur in attributes.
567 PA.Revert();
568 return CAK_AttributeSpecifier;
569 }
570
571 // Parse the attribute-token, if present.
572 // C++11 [dcl.attr.grammar]:
573 // If a keyword or an alternative token that satisfies the syntactic
574 // requirements of an identifier is contained in an attribute-token,
575 // it is considered an identifier.
576 SourceLocation Loc;
577 if (!TryParseCXX11AttributeIdentifier(Loc)) {
578 IsAttribute = false;
579 break;
580 }
581 if (Tok.is(tok::coloncolon)) {
582 ConsumeToken();
583 if (!TryParseCXX11AttributeIdentifier(Loc)) {
584 IsAttribute = false;
585 break;
586 }
587 }
588
589 // Parse the attribute-argument-clause, if present.
590 if (Tok.is(tok::l_paren)) {
591 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000592 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 IsAttribute = false;
594 break;
595 }
596 }
597
Alp Toker97650562014-01-10 11:19:30 +0000598 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000599
Alp Toker97650562014-01-10 11:19:30 +0000600 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000602 }
603
604 // An attribute must end ']]'.
605 if (IsAttribute) {
606 if (Tok.is(tok::r_square)) {
607 ConsumeBracket();
608 IsAttribute = Tok.is(tok::r_square);
609 } else {
610 IsAttribute = false;
611 }
612 }
613
614 PA.Revert();
615
616 if (IsAttribute)
617 // Case 1: C++11 statement attribute.
618 return CAK_AttributeSpecifier;
619
620 // Case 3: Message send.
621 return CAK_NotAttributeSpecifier;
622}
623
Richard Smith1fff95c2013-09-12 23:28:08 +0000624Parser::TPResult Parser::TryParsePtrOperatorSeq() {
625 while (true) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000626 if (Tok.isOneOf(tok::coloncolon, tok::identifier))
Richard Smith1fff95c2013-09-12 23:28:08 +0000627 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000628 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000629
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000630 if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000631 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
632 // ptr-operator
633 ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000634 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Richard Smith1fff95c2013-09-12 23:28:08 +0000635 ConsumeToken();
636 } else {
Justin Bognerd26f95b2015-02-23 22:36:28 +0000637 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000638 }
639 }
640}
641
642/// operator-function-id:
643/// 'operator' operator
644///
645/// operator: one of
646/// new delete new[] delete[] + - * / % ^ [...]
647///
648/// conversion-function-id:
649/// 'operator' conversion-type-id
650///
651/// conversion-type-id:
652/// type-specifier-seq conversion-declarator[opt]
653///
654/// conversion-declarator:
655/// ptr-operator conversion-declarator[opt]
656///
657/// literal-operator-id:
658/// 'operator' string-literal identifier
659/// 'operator' user-defined-string-literal
660Parser::TPResult Parser::TryParseOperatorId() {
661 assert(Tok.is(tok::kw_operator));
662 ConsumeToken();
663
664 // Maybe this is an operator-function-id.
665 switch (Tok.getKind()) {
666 case tok::kw_new: case tok::kw_delete:
667 ConsumeToken();
668 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
669 ConsumeBracket();
670 ConsumeBracket();
671 }
Richard Smithee390432014-05-16 01:56:53 +0000672 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000673
674#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
675 case tok::Token:
676#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
677#include "clang/Basic/OperatorKinds.def"
678 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000679 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000680
681 case tok::l_square:
682 if (NextToken().is(tok::r_square)) {
683 ConsumeBracket();
684 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000685 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000686 }
687 break;
688
689 case tok::l_paren:
690 if (NextToken().is(tok::r_paren)) {
691 ConsumeParen();
692 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000693 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000694 }
695 break;
696
697 default:
698 break;
699 }
700
701 // Maybe this is a literal-operator-id.
702 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
703 bool FoundUDSuffix = false;
704 do {
705 FoundUDSuffix |= Tok.hasUDSuffix();
706 ConsumeStringToken();
707 } while (isTokenStringLiteral());
708
709 if (!FoundUDSuffix) {
710 if (Tok.is(tok::identifier))
711 ConsumeToken();
712 else
Richard Smithee390432014-05-16 01:56:53 +0000713 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000714 }
Richard Smithee390432014-05-16 01:56:53 +0000715 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000716 }
717
718 // Maybe this is a conversion-function-id.
719 bool AnyDeclSpecifiers = false;
720 while (true) {
721 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000722 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000723 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000724 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000725 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000726 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000727 break;
728 }
Richard Smithee390432014-05-16 01:56:53 +0000729 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
730 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000731 AnyDeclSpecifiers = true;
732 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000733 return TryParsePtrOperatorSeq();
Richard Smith1fff95c2013-09-12 23:28:08 +0000734}
735
Guy Benyei11169dd2012-12-18 14:30:41 +0000736/// declarator:
737/// direct-declarator
738/// ptr-operator declarator
739///
740/// direct-declarator:
741/// declarator-id
742/// direct-declarator '(' parameter-declaration-clause ')'
743/// cv-qualifier-seq[opt] exception-specification[opt]
744/// direct-declarator '[' constant-expression[opt] ']'
745/// '(' declarator ')'
746/// [GNU] '(' attributes declarator ')'
747///
748/// abstract-declarator:
749/// ptr-operator abstract-declarator[opt]
750/// direct-abstract-declarator
751/// ...
752///
753/// direct-abstract-declarator:
754/// direct-abstract-declarator[opt]
755/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
756/// exception-specification[opt]
757/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
758/// '(' abstract-declarator ')'
759///
760/// ptr-operator:
761/// '*' cv-qualifier-seq[opt]
762/// '&'
763/// [C++0x] '&&' [TODO]
764/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
765///
766/// cv-qualifier-seq:
767/// cv-qualifier cv-qualifier-seq[opt]
768///
769/// cv-qualifier:
770/// 'const'
771/// 'volatile'
772///
773/// declarator-id:
774/// '...'[opt] id-expression
775///
776/// id-expression:
777/// unqualified-id
778/// qualified-id [TODO]
779///
780/// unqualified-id:
781/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000782/// operator-function-id
783/// conversion-function-id
784/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000785/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000786/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000787/// template-id [TODO]
788///
Justin Bognerd26f95b2015-02-23 22:36:28 +0000789Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
790 bool mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000791 // declarator:
792 // direct-declarator
793 // ptr-operator declarator
Justin Bognerd26f95b2015-02-23 22:36:28 +0000794 if (TryParsePtrOperatorSeq() == TPResult::Error)
795 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000796
797 // direct-declarator:
798 // direct-abstract-declarator:
799 if (Tok.is(tok::ellipsis))
800 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000801
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000802 if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
Richard Smith1fff95c2013-09-12 23:28:08 +0000803 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
804 NextToken().is(tok::kw_operator)))) &&
Justin Bognerd26f95b2015-02-23 22:36:28 +0000805 mayHaveIdentifier) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000806 // declarator-id
807 if (Tok.is(tok::annot_cxxscope))
808 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000809 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000810 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000811 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000812 if (TryParseOperatorId() == TPResult::Error)
813 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000814 } else
815 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 } else if (Tok.is(tok::l_paren)) {
817 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000818 if (mayBeAbstract &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 (Tok.is(tok::r_paren) || // 'int()' is a function.
820 // 'int(...)' is a function.
821 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
822 isDeclarationSpecifier())) { // 'int(int)' is a function.
823 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
824 // exception-specification[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +0000825 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000826 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000827 return TPR;
828 } else {
829 // '(' declarator ')'
830 // '(' attributes declarator ')'
831 // '(' abstract-declarator ')'
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000832 if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
833 tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
834 tok::kw___vectorcall, tok::kw___unaligned))
Richard Smithee390432014-05-16 01:56:53 +0000835 return TPResult::True; // attributes indicate declaration
Justin Bognerd26f95b2015-02-23 22:36:28 +0000836 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000837 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 return TPR;
839 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000840 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000841 ConsumeParen();
842 }
Justin Bognerd26f95b2015-02-23 22:36:28 +0000843 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000844 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 }
846
847 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000848 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000849
850 // abstract-declarator: ...
851 if (Tok.is(tok::ellipsis))
852 ConsumeToken();
853
854 if (Tok.is(tok::l_paren)) {
855 // Check whether we have a function declarator or a possible ctor-style
856 // initializer that follows the declarator. Note that ctor-style
857 // initializers are not possible in contexts where abstract declarators
858 // are allowed.
Justin Bognerd26f95b2015-02-23 22:36:28 +0000859 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Guy Benyei11169dd2012-12-18 14:30:41 +0000860 break;
861
862 // direct-declarator '(' parameter-declaration-clause ')'
863 // cv-qualifier-seq[opt] exception-specification[opt]
864 ConsumeParen();
Justin Bognerd26f95b2015-02-23 22:36:28 +0000865 TPR = TryParseFunctionDeclarator();
Guy Benyei11169dd2012-12-18 14:30:41 +0000866 } else if (Tok.is(tok::l_square)) {
867 // direct-declarator '[' constant-expression[opt] ']'
868 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
869 TPR = TryParseBracketDeclarator();
870 } else {
871 break;
872 }
873
Richard Smithee390432014-05-16 01:56:53 +0000874 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000875 return TPR;
876 }
877
Richard Smithee390432014-05-16 01:56:53 +0000878 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000879}
880
881Parser::TPResult
882Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
883 switch (Kind) {
884 // Obviously starts an expression.
885 case tok::numeric_constant:
886 case tok::char_constant:
887 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000888 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000889 case tok::utf16_char_constant:
890 case tok::utf32_char_constant:
891 case tok::string_literal:
892 case tok::wide_string_literal:
893 case tok::utf8_string_literal:
894 case tok::utf16_string_literal:
895 case tok::utf32_string_literal:
896 case tok::l_square:
897 case tok::l_paren:
898 case tok::amp:
899 case tok::ampamp:
900 case tok::star:
901 case tok::plus:
902 case tok::plusplus:
903 case tok::minus:
904 case tok::minusminus:
905 case tok::tilde:
906 case tok::exclaim:
907 case tok::kw_sizeof:
908 case tok::kw___func__:
909 case tok::kw_const_cast:
910 case tok::kw_delete:
911 case tok::kw_dynamic_cast:
912 case tok::kw_false:
913 case tok::kw_new:
914 case tok::kw_operator:
915 case tok::kw_reinterpret_cast:
916 case tok::kw_static_cast:
917 case tok::kw_this:
918 case tok::kw_throw:
919 case tok::kw_true:
920 case tok::kw_typeid:
921 case tok::kw_alignof:
922 case tok::kw_noexcept:
923 case tok::kw_nullptr:
924 case tok::kw__Alignof:
925 case tok::kw___null:
926 case tok::kw___alignof:
927 case tok::kw___builtin_choose_expr:
928 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +0000929 case tok::kw___builtin_va_arg:
930 case tok::kw___imag:
931 case tok::kw___real:
932 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +0000933 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +0000934 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000935 case tok::kw_L__FUNCTION__:
936 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000937 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +0000938#define TYPE_TRAIT(N,Spelling,K) \
939 case tok::kw_##Spelling:
940#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +0000941 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000942
943 // Obviously starts a type-specifier-seq:
944 case tok::kw_char:
945 case tok::kw_const:
946 case tok::kw_double:
947 case tok::kw_enum:
948 case tok::kw_half:
949 case tok::kw_float:
950 case tok::kw_int:
951 case tok::kw_long:
952 case tok::kw___int64:
953 case tok::kw___int128:
954 case tok::kw_restrict:
955 case tok::kw_short:
956 case tok::kw_signed:
957 case tok::kw_struct:
958 case tok::kw_union:
959 case tok::kw_unsigned:
960 case tok::kw_void:
961 case tok::kw_volatile:
962 case tok::kw__Bool:
963 case tok::kw__Complex:
964 case tok::kw_class:
965 case tok::kw_typename:
966 case tok::kw_wchar_t:
967 case tok::kw_char16_t:
968 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +0000969 case tok::kw__Decimal32:
970 case tok::kw__Decimal64:
971 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +0000972 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +0000973 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +0000974 case tok::kw_thread_local:
975 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +0000976 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +0000977 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 case tok::kw___cdecl:
979 case tok::kw___stdcall:
980 case tok::kw___fastcall:
981 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +0000982 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +0000983 case tok::kw___unaligned:
984 case tok::kw___vector:
985 case tok::kw___pixel:
Bill Seurercf2c96b2015-01-12 19:35:51 +0000986 case tok::kw___bool:
Guy Benyei11169dd2012-12-18 14:30:41 +0000987 case tok::kw__Atomic:
988 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +0000989 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000990
991 default:
992 break;
993 }
994
Richard Smithee390432014-05-16 01:56:53 +0000995 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000996}
997
998bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
999 return std::find(TentativelyDeclaredIdentifiers.begin(),
1000 TentativelyDeclaredIdentifiers.end(), II)
1001 != TentativelyDeclaredIdentifiers.end();
1002}
1003
Kaelyn Takata445b0652014-11-05 00:09:29 +00001004namespace {
1005class TentativeParseCCC : public CorrectionCandidateCallback {
1006public:
1007 TentativeParseCCC(const Token &Next) {
1008 WantRemainingKeywords = false;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001009 WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1010 tok::l_brace, tok::identifier);
Kaelyn Takata445b0652014-11-05 00:09:29 +00001011 }
1012
1013 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1014 // Reject any candidate that only resolves to instance members since they
1015 // aren't viable as standalone identifiers instead of member references.
1016 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1017 std::all_of(Candidate.begin(), Candidate.end(),
1018 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1019 return false;
1020
1021 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1022 }
1023};
1024}
Richard Smithee390432014-05-16 01:56:53 +00001025/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1026/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1027/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001028/// if a parsing error was found and reported.
1029///
1030/// If HasMissingTypename is provided, a name with a dependent scope specifier
1031/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1032/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1033/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001034/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001035/// such cases.
1036///
1037/// decl-specifier:
1038/// storage-class-specifier
1039/// type-specifier
1040/// function-specifier
1041/// 'friend'
1042/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001043/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001044/// [GNU] attributes declaration-specifiers[opt]
1045///
1046/// storage-class-specifier:
1047/// 'register'
1048/// 'static'
1049/// 'extern'
1050/// 'mutable'
1051/// 'auto'
1052/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001053/// [C++11] 'thread_local'
1054/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001055///
1056/// function-specifier:
1057/// 'inline'
1058/// 'virtual'
1059/// 'explicit'
1060///
1061/// typedef-name:
1062/// identifier
1063///
1064/// type-specifier:
1065/// simple-type-specifier
1066/// class-specifier
1067/// enum-specifier
1068/// elaborated-type-specifier
1069/// typename-specifier
1070/// cv-qualifier
1071///
1072/// simple-type-specifier:
1073/// '::'[opt] nested-name-specifier[opt] type-name
1074/// '::'[opt] nested-name-specifier 'template'
1075/// simple-template-id [TODO]
1076/// 'char'
1077/// 'wchar_t'
1078/// 'bool'
1079/// 'short'
1080/// 'int'
1081/// 'long'
1082/// 'signed'
1083/// 'unsigned'
1084/// 'float'
1085/// 'double'
1086/// 'void'
1087/// [GNU] typeof-specifier
1088/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001089/// [C++11] 'auto'
1090/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001091/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001092///
1093/// type-name:
1094/// class-name
1095/// enum-name
1096/// typedef-name
1097///
1098/// elaborated-type-specifier:
1099/// class-key '::'[opt] nested-name-specifier[opt] identifier
1100/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1101/// simple-template-id
1102/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1103///
1104/// enum-name:
1105/// identifier
1106///
1107/// enum-specifier:
1108/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1109/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1110///
1111/// class-specifier:
1112/// class-head '{' member-specification[opt] '}'
1113///
1114/// class-head:
1115/// class-key identifier[opt] base-clause[opt]
1116/// class-key nested-name-specifier identifier base-clause[opt]
1117/// class-key nested-name-specifier[opt] simple-template-id
1118/// base-clause[opt]
1119///
1120/// class-key:
1121/// 'class'
1122/// 'struct'
1123/// 'union'
1124///
1125/// cv-qualifier:
1126/// 'const'
1127/// 'volatile'
1128/// [GNU] restrict
1129///
1130Parser::TPResult
1131Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1132 bool *HasMissingTypename) {
1133 switch (Tok.getKind()) {
1134 case tok::identifier: {
1135 // Check for need to substitute AltiVec __vector keyword
1136 // for "vector" identifier.
1137 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001138 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001139
1140 const Token &Next = NextToken();
1141 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1142 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001143 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001144
1145 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1146 // Determine whether this is a valid expression. If not, we will hit
1147 // a parse error one way or another. In that case, tell the caller that
1148 // this is ambiguous. Typo-correct to type and expression keywords and
1149 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001151 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001153 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001154 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001155 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001156 case ANK_TemplateName:
1157 // A bare type template-name which can't be a template template
1158 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001159 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001160 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001161 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001162 case ANK_Success:
1163 break;
1164 }
1165 assert(Tok.isNot(tok::identifier) &&
1166 "TryAnnotateName succeeded without producing an annotation");
1167 } else {
1168 // This might possibly be a type with a dependent scope specifier and
1169 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1170 // since it will annotate as a primary expression, and we want to use the
1171 // "missing 'typename'" logic.
1172 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001173 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 // If annotation failed, assume it's a non-type.
1175 // FIXME: If this happens due to an undeclared identifier, treat it as
1176 // ambiguous.
1177 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001178 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001179 }
1180
1181 // We annotated this token as something. Recurse to handle whatever we got.
1182 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1183 }
1184
1185 case tok::kw_typename: // typename T::type
1186 // Annotate typenames and C++ scope specifiers. If we get one, just
1187 // recurse to handle whatever we get.
1188 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001189 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001190 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1191
1192 case tok::coloncolon: { // ::foo::bar
1193 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001194 if (Next.isOneOf(tok::kw_new, // ::new
1195 tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001196 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001197 }
1198 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001199 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001200 case tok::kw_decltype:
1201 // Annotate typenames and C++ scope specifiers. If we get one, just
1202 // recurse to handle whatever we get.
1203 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001204 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1206
1207 // decl-specifier:
1208 // storage-class-specifier
1209 // type-specifier
1210 // function-specifier
1211 // 'friend'
1212 // 'typedef'
1213 // 'constexpr'
1214 case tok::kw_friend:
1215 case tok::kw_typedef:
1216 case tok::kw_constexpr:
1217 // storage-class-specifier
1218 case tok::kw_register:
1219 case tok::kw_static:
1220 case tok::kw_extern:
1221 case tok::kw_mutable:
1222 case tok::kw_auto:
1223 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001224 case tok::kw_thread_local:
1225 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001226 // function-specifier
1227 case tok::kw_inline:
1228 case tok::kw_virtual:
1229 case tok::kw_explicit:
1230
1231 // Modules
1232 case tok::kw___module_private__:
1233
1234 // Debugger support
1235 case tok::kw___unknown_anytype:
1236
1237 // type-specifier:
1238 // simple-type-specifier
1239 // class-specifier
1240 // enum-specifier
1241 // elaborated-type-specifier
1242 // typename-specifier
1243 // cv-qualifier
1244
1245 // class-specifier
1246 // elaborated-type-specifier
1247 case tok::kw_class:
1248 case tok::kw_struct:
1249 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001250 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001251 // enum-specifier
1252 case tok::kw_enum:
1253 // cv-qualifier
1254 case tok::kw_const:
1255 case tok::kw_volatile:
1256
1257 // GNU
1258 case tok::kw_restrict:
1259 case tok::kw__Complex:
1260 case tok::kw___attribute:
Richard Smithee390432014-05-16 01:56:53 +00001261 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001262
1263 // Microsoft
1264 case tok::kw___declspec:
1265 case tok::kw___cdecl:
1266 case tok::kw___stdcall:
1267 case tok::kw___fastcall:
1268 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001269 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001270 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001271 case tok::kw___sptr:
1272 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001273 case tok::kw___ptr64:
1274 case tok::kw___ptr32:
1275 case tok::kw___forceinline:
1276 case tok::kw___unaligned:
Richard Smithee390432014-05-16 01:56:53 +00001277 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001278
1279 // Borland
1280 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001281 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001282
1283 // AltiVec
1284 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001285 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001286
1287 case tok::annot_template_id: {
1288 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1289 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001290 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001291 CXXScopeSpec SS;
1292 AnnotateTemplateIdTokenAsType();
1293 assert(Tok.is(tok::annot_typename));
1294 goto case_typename;
1295 }
1296
1297 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1298 // We've already annotated a scope; try to annotate a type.
1299 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001300 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001301 if (!Tok.is(tok::annot_typename)) {
1302 // If the next token is an identifier or a type qualifier, then this
1303 // can't possibly be a valid expression either.
1304 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1305 CXXScopeSpec SS;
1306 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1307 Tok.getAnnotationRange(),
1308 SS);
1309 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1310 TentativeParsingAction PA(*this);
1311 ConsumeToken();
1312 ConsumeToken();
1313 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001314 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001315 if (!isIdentifier)
1316 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1317 HasMissingTypename);
1318 PA.Revert();
1319
1320 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001321 TPR == TPResult::True || TPR == TPResult::Error)
1322 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001323
1324 if (HasMissingTypename) {
1325 // We can't tell whether this is a missing 'typename' or a valid
1326 // expression.
1327 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001328 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001329 }
1330 } else {
1331 // Try to resolve the name. If it doesn't exist, assume it was
1332 // intended to name a type and keep disambiguating.
1333 switch (TryAnnotateName(false /* SS is not dependent */)) {
1334 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001335 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001337 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001338 case ANK_TemplateName:
1339 // A bare type template-name which can't be a template template
1340 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001341 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001342 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001343 return HasMissingTypename ? TPResult::Ambiguous
1344 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001345 case ANK_Success:
1346 // Annotated it, check again.
1347 assert(Tok.isNot(tok::annot_cxxscope) ||
1348 NextToken().isNot(tok::identifier));
1349 return isCXXDeclarationSpecifier(BracedCastResult,
1350 HasMissingTypename);
1351 }
1352 }
1353 }
Richard Smithee390432014-05-16 01:56:53 +00001354 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 }
1356 // If that succeeded, fallthrough into the generic simple-type-id case.
1357
1358 // The ambiguity resides in a simple-type-specifier/typename-specifier
1359 // followed by a '('. The '(' could either be the start of:
1360 //
1361 // direct-declarator:
1362 // '(' declarator ')'
1363 //
1364 // direct-abstract-declarator:
1365 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1366 // exception-specification[opt]
1367 // '(' abstract-declarator ')'
1368 //
1369 // or part of a function-style cast expression:
1370 //
1371 // simple-type-specifier '(' expression-list[opt] ')'
1372 //
1373
1374 // simple-type-specifier:
1375
1376 case tok::annot_typename:
1377 case_typename:
1378 // In Objective-C, we might have a protocol-qualified type.
1379 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1380 // Tentatively parse the
1381 TentativeParsingAction PA(*this);
1382 ConsumeToken(); // The type token
1383
1384 TPResult TPR = TryParseProtocolQualifiers();
1385 bool isFollowedByParen = Tok.is(tok::l_paren);
1386 bool isFollowedByBrace = Tok.is(tok::l_brace);
1387
1388 PA.Revert();
1389
Richard Smithee390432014-05-16 01:56:53 +00001390 if (TPR == TPResult::Error)
1391 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001392
1393 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001394 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001395
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001396 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 return BracedCastResult;
1398
Richard Smithee390432014-05-16 01:56:53 +00001399 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 }
1401
1402 case tok::kw_char:
1403 case tok::kw_wchar_t:
1404 case tok::kw_char16_t:
1405 case tok::kw_char32_t:
1406 case tok::kw_bool:
1407 case tok::kw_short:
1408 case tok::kw_int:
1409 case tok::kw_long:
1410 case tok::kw___int64:
1411 case tok::kw___int128:
1412 case tok::kw_signed:
1413 case tok::kw_unsigned:
1414 case tok::kw_half:
1415 case tok::kw_float:
1416 case tok::kw_double:
1417 case tok::kw_void:
1418 case tok::annot_decltype:
1419 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001420 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001421
1422 // This is a function-style cast in all cases we disambiguate other than
1423 // one:
1424 // struct S {
1425 // enum E : int { a = 4 }; // enum
1426 // enum E : int { 4 }; // bit-field
1427 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001428 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001429 return BracedCastResult;
1430
1431 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001432 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001433
Richard Smithee390432014-05-16 01:56:53 +00001434 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001435
1436 // GNU typeof support.
1437 case tok::kw_typeof: {
1438 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001439 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001440
1441 TentativeParsingAction PA(*this);
1442
1443 TPResult TPR = TryParseTypeofSpecifier();
1444 bool isFollowedByParen = Tok.is(tok::l_paren);
1445 bool isFollowedByBrace = Tok.is(tok::l_brace);
1446
1447 PA.Revert();
1448
Richard Smithee390432014-05-16 01:56:53 +00001449 if (TPR == TPResult::Error)
1450 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001451
1452 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001453 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001454
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001455 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001456 return BracedCastResult;
1457
Richard Smithee390432014-05-16 01:56:53 +00001458 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001459 }
1460
1461 // C++0x type traits support
1462 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001463 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001464
1465 // C11 _Atomic
1466 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001467 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001468
1469 default:
Richard Smithee390432014-05-16 01:56:53 +00001470 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001471 }
1472}
1473
Richard Smith1fff95c2013-09-12 23:28:08 +00001474bool Parser::isCXXDeclarationSpecifierAType() {
1475 switch (Tok.getKind()) {
1476 // typename-specifier
1477 case tok::annot_decltype:
1478 case tok::annot_template_id:
1479 case tok::annot_typename:
1480 case tok::kw_typeof:
1481 case tok::kw___underlying_type:
1482 return true;
1483
1484 // elaborated-type-specifier
1485 case tok::kw_class:
1486 case tok::kw_struct:
1487 case tok::kw_union:
1488 case tok::kw___interface:
1489 case tok::kw_enum:
1490 return true;
1491
1492 // simple-type-specifier
1493 case tok::kw_char:
1494 case tok::kw_wchar_t:
1495 case tok::kw_char16_t:
1496 case tok::kw_char32_t:
1497 case tok::kw_bool:
1498 case tok::kw_short:
1499 case tok::kw_int:
1500 case tok::kw_long:
1501 case tok::kw___int64:
1502 case tok::kw___int128:
1503 case tok::kw_signed:
1504 case tok::kw_unsigned:
1505 case tok::kw_half:
1506 case tok::kw_float:
1507 case tok::kw_double:
1508 case tok::kw_void:
1509 case tok::kw___unknown_anytype:
1510 return true;
1511
1512 case tok::kw_auto:
1513 return getLangOpts().CPlusPlus11;
1514
1515 case tok::kw__Atomic:
1516 // "_Atomic foo"
1517 return NextToken().is(tok::l_paren);
1518
1519 default:
1520 return false;
1521 }
1522}
1523
Guy Benyei11169dd2012-12-18 14:30:41 +00001524/// [GNU] typeof-specifier:
1525/// 'typeof' '(' expressions ')'
1526/// 'typeof' '(' type-name ')'
1527///
1528Parser::TPResult Parser::TryParseTypeofSpecifier() {
1529 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1530 ConsumeToken();
1531
1532 assert(Tok.is(tok::l_paren) && "Expected '('");
1533 // Parse through the parens after 'typeof'.
1534 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001535 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001536 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001537
Richard Smithee390432014-05-16 01:56:53 +00001538 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001539}
1540
1541/// [ObjC] protocol-qualifiers:
1542//// '<' identifier-list '>'
1543Parser::TPResult Parser::TryParseProtocolQualifiers() {
1544 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1545 ConsumeToken();
1546 do {
1547 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001548 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 ConsumeToken();
1550
1551 if (Tok.is(tok::comma)) {
1552 ConsumeToken();
1553 continue;
1554 }
1555
1556 if (Tok.is(tok::greater)) {
1557 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001558 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001559 }
1560 } while (false);
1561
Richard Smithee390432014-05-16 01:56:53 +00001562 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001563}
1564
Guy Benyei11169dd2012-12-18 14:30:41 +00001565/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1566/// a constructor-style initializer, when parsing declaration statements.
1567/// Returns true for function declarator and false for constructor-style
1568/// initializer.
1569/// If during the disambiguation process a parsing error is encountered,
1570/// the function returns true to let the declaration parsing code handle it.
1571///
1572/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1573/// exception-specification[opt]
1574///
1575bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1576
1577 // C++ 8.2p1:
1578 // The ambiguity arising from the similarity between a function-style cast and
1579 // a declaration mentioned in 6.8 can also occur in the context of a
1580 // declaration. In that context, the choice is between a function declaration
1581 // with a redundant set of parentheses around a parameter name and an object
1582 // declaration with a function-style cast as the initializer. Just as for the
1583 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1584 // that could possibly be a declaration a declaration.
1585
1586 TentativeParsingAction PA(*this);
1587
1588 ConsumeParen();
1589 bool InvalidAsDeclaration = false;
1590 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001591 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001593 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 else {
1595 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001596 if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1597 tok::kw_throw, tok::kw_noexcept, tok::l_square,
1598 tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1599 isCXX11VirtSpecifier(Next))
Guy Benyei11169dd2012-12-18 14:30:41 +00001600 // The next token cannot appear after a constructor-style initializer,
1601 // and can appear next in a function definition. This must be a function
1602 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001603 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001604 else if (InvalidAsDeclaration)
1605 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001606 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001607 }
1608 }
1609
1610 PA.Revert();
1611
Richard Smithee390432014-05-16 01:56:53 +00001612 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001613 *IsAmbiguous = true;
1614
1615 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001616 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001617}
1618
1619/// parameter-declaration-clause:
1620/// parameter-declaration-list[opt] '...'[opt]
1621/// parameter-declaration-list ',' '...'
1622///
1623/// parameter-declaration-list:
1624/// parameter-declaration
1625/// parameter-declaration-list ',' parameter-declaration
1626///
1627/// parameter-declaration:
1628/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1629/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1630/// '=' assignment-expression
1631/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1632/// attributes[opt]
1633/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1634/// attributes[opt] '=' assignment-expression
1635///
1636Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001637Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1638 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001639
1640 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001641 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001642
1643 // parameter-declaration-list[opt] '...'[opt]
1644 // parameter-declaration-list ',' '...'
1645 //
1646 // parameter-declaration-list:
1647 // parameter-declaration
1648 // parameter-declaration-list ',' parameter-declaration
1649 //
1650 while (1) {
1651 // '...'[opt]
1652 if (Tok.is(tok::ellipsis)) {
1653 ConsumeToken();
1654 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001655 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001656 else
Richard Smithee390432014-05-16 01:56:53 +00001657 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 }
1659
1660 // An attribute-specifier-seq here is a sign of a function declarator.
1661 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1662 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001663 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001664
1665 ParsedAttributes attrs(AttrFactory);
1666 MaybeParseMicrosoftAttributes(attrs);
1667
1668 // decl-specifier-seq
1669 // A parameter-declaration's initializer must be preceded by an '=', so
1670 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001671 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001672 InvalidAsDeclaration);
1673
Richard Smithee390432014-05-16 01:56:53 +00001674 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001675 // Consume the decl-specifier-seq. We have to look past it, since a
1676 // type-id might appear here in a template argument.
1677 bool SeenType = false;
1678 do {
1679 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001680 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1681 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001682
1683 // If we see a parameter name, this can't be a template argument.
1684 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001685 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001686
Richard Smithee390432014-05-16 01:56:53 +00001687 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001688 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001689 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001690 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001691 } while (TPR != TPResult::False);
1692 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001693 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001694 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1695 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001696 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001697 return TPR;
1698
1699 // declarator
1700 // abstract-declarator[opt]
Justin Bognerd26f95b2015-02-23 22:36:28 +00001701 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001702 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001703 return TPR;
1704
1705 // [GNU] attributes[opt]
1706 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001707 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001708
Richard Smith1fff95c2013-09-12 23:28:08 +00001709 // If we're disambiguating a template argument in a default argument in
1710 // a class definition versus a parameter declaration, an '=' here
1711 // disambiguates the parse one way or the other.
1712 // If this is a parameter, it must have a default argument because
1713 // (a) the previous parameter did, and
1714 // (b) this must be the first declaration of the function, so we can't
1715 // inherit any default arguments from elsewhere.
1716 // If we see an ')', then we've reached the end of a
1717 // parameter-declaration-clause, and the last param is missing its default
1718 // argument.
1719 if (VersusTemplateArgument)
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001720 return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
1721 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001722
Guy Benyei11169dd2012-12-18 14:30:41 +00001723 if (Tok.is(tok::equal)) {
1724 // '=' assignment-expression
1725 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001726 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001727 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001728 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 }
1730
1731 if (Tok.is(tok::ellipsis)) {
1732 ConsumeToken();
1733 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001734 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001735 else
Richard Smithee390432014-05-16 01:56:53 +00001736 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 }
1738
Alp Toker97650562014-01-10 11:19:30 +00001739 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001740 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001741 }
1742
Richard Smithee390432014-05-16 01:56:53 +00001743 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001744}
1745
1746/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1747/// parsing as a function declarator.
1748/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Justin Bognerd26f95b2015-02-23 22:36:28 +00001749/// return TPResult::Ambiguous, otherwise it will return either False() or
1750/// Error().
Guy Benyei11169dd2012-12-18 14:30:41 +00001751///
1752/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1753/// exception-specification[opt]
1754///
1755/// exception-specification:
1756/// 'throw' '(' type-id-list[opt] ')'
1757///
Justin Bognerd26f95b2015-02-23 22:36:28 +00001758Parser::TPResult Parser::TryParseFunctionDeclarator() {
Guy Benyei11169dd2012-12-18 14:30:41 +00001759
1760 // The '(' is already parsed.
1761
1762 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001763 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1764 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001765
Justin Bognerd26f95b2015-02-23 22:36:28 +00001766 if (TPR == TPResult::False || TPR == TPResult::Error)
1767 return TPR;
Guy Benyei11169dd2012-12-18 14:30:41 +00001768
1769 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001770 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001771 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001772
1773 // cv-qualifier-seq
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001774 while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 ConsumeToken();
1776
1777 // ref-qualifier[opt]
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001778 if (Tok.isOneOf(tok::amp, tok::ampamp))
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 ConsumeToken();
1780
1781 // exception-specification
1782 if (Tok.is(tok::kw_throw)) {
1783 ConsumeToken();
1784 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001785 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001786
1787 // Parse through the parens after 'throw'.
1788 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001789 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001790 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 }
1792 if (Tok.is(tok::kw_noexcept)) {
1793 ConsumeToken();
1794 // Possibly an expression as well.
1795 if (Tok.is(tok::l_paren)) {
1796 // Find the matching rparen.
1797 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001798 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001799 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 }
1801 }
1802
Richard Smithee390432014-05-16 01:56:53 +00001803 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001804}
1805
1806/// '[' constant-expression[opt] ']'
1807///
1808Parser::TPResult Parser::TryParseBracketDeclarator() {
1809 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001810 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001811 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001812
Richard Smithee390432014-05-16 01:56:53 +00001813 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001814}