blob: 929242f2004f763f19f72efba4dd48f0a89f1f36 [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.
182 while (Tok.is(tok::l_square) || Tok.is(tok::kw___attribute) ||
183 Tok.is(tok::kw___declspec) || Tok.is(tok::kw_alignas)) {
184 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
Nico Weberc29c4832014-12-28 23:24:02 +0000198 if ((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
199 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
200 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
287 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]
292 if (Tok.is(tok::kw_asm) || Tok.is(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
364 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
365
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]
373 if (Tok.is(tok::equal) ||
374 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000375 TPR = TPResult::True;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000376 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
Richard Smithee390432014-05-16 01:56:53 +0000377 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 else
Richard Smithee390432014-05-16 01:56:53 +0000379 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000380 }
381
382 PA.Revert();
383
Richard Smithee390432014-05-16 01:56:53 +0000384 assert(TPR == TPResult::True || TPR == TPResult::False);
385 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000386}
387
388 /// \brief Determine whether the next set of tokens contains a type-id.
389 ///
390 /// The context parameter states what context we're parsing right
391 /// now, which affects how this routine copes with the token
392 /// following the type-id. If the context is TypeIdInParens, we have
393 /// already parsed the '(' and we will cease lookahead when we hit
394 /// the corresponding ')'. If the context is
395 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
396 /// before this template argument, and will cease lookahead when we
397 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
398 /// and false for an expression. If during the disambiguation
399 /// process a parsing error is encountered, the function returns
400 /// true to let the declaration parsing code handle it.
401 ///
402 /// type-id:
403 /// type-specifier-seq abstract-declarator[opt]
404 ///
405bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
406
407 isAmbiguous = false;
408
409 // C++ 8.2p2:
410 // The ambiguity arising from the similarity between a function-style cast and
411 // a type-id can occur in different contexts. The ambiguity appears as a
412 // choice between a function-style cast expression and a declaration of a
413 // type. The resolution is that any construct that could possibly be a type-id
414 // in its syntactic context shall be considered a type-id.
415
416 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000417 if (TPR != TPResult::Ambiguous)
418 return TPR != TPResult::False; // Returns true for TPResult::True or
419 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000420
421 // FIXME: Add statistics about the number of ambiguous statements encountered
422 // and how they were resolved (number of declarations+number of expressions).
423
424 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
425 // We need tentative parsing...
426
427 TentativeParsingAction PA(*this);
428
429 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000430 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 assert(Tok.is(tok::l_paren) && "Expected '('");
432
433 // declarator
434 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
435
436 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000437 if (TPR == TPResult::Error)
438 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000439
Richard Smithee390432014-05-16 01:56:53 +0000440 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000441 // We are supposed to be inside parens, so if after the abstract declarator
442 // we encounter a ')' this is a type-id, otherwise it's an expression.
443 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000444 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000445 isAmbiguous = true;
446
447 // We are supposed to be inside a template argument, so if after
448 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
449 // ',', this is a type-id. Otherwise, it's an expression.
450 } else if (Context == TypeIdAsTemplateArgument &&
451 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000452 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000453 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 isAmbiguous = true;
455
456 } else
Richard Smithee390432014-05-16 01:56:53 +0000457 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000458 }
459
460 PA.Revert();
461
Richard Smithee390432014-05-16 01:56:53 +0000462 assert(TPR == TPResult::True || TPR == TPResult::False);
463 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000464}
465
466/// \brief Returns true if this is a C++11 attribute-specifier. Per
467/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
468/// always introduce an attribute. In Objective-C++11, this rule does not
469/// apply if either '[' begins a message-send.
470///
471/// If Disambiguate is true, we try harder to determine whether a '[[' starts
472/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
473///
474/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
475/// Obj-C message send or the start of an attribute. Otherwise, we assume it
476/// is not an Obj-C message send.
477///
478/// C++11 [dcl.attr.grammar]:
479///
480/// attribute-specifier:
481/// '[' '[' attribute-list ']' ']'
482/// alignment-specifier
483///
484/// attribute-list:
485/// attribute[opt]
486/// attribute-list ',' attribute[opt]
487/// attribute '...'
488/// attribute-list ',' attribute '...'
489///
490/// attribute:
491/// attribute-token attribute-argument-clause[opt]
492///
493/// attribute-token:
494/// identifier
495/// identifier '::' identifier
496///
497/// attribute-argument-clause:
498/// '(' balanced-token-seq ')'
499Parser::CXX11AttributeKind
500Parser::isCXX11AttributeSpecifier(bool Disambiguate,
501 bool OuterMightBeMessageSend) {
502 if (Tok.is(tok::kw_alignas))
503 return CAK_AttributeSpecifier;
504
505 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
506 return CAK_NotAttributeSpecifier;
507
508 // No tentative parsing if we don't need to look for ']]' or a lambda.
509 if (!Disambiguate && !getLangOpts().ObjC1)
510 return CAK_AttributeSpecifier;
511
512 TentativeParsingAction PA(*this);
513
514 // Opening brackets were checked for above.
515 ConsumeBracket();
516
517 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
518 if (!getLangOpts().ObjC1) {
519 ConsumeBracket();
520
Alexey Bataevee6507d2013-11-18 08:17:37 +0000521 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 IsAttribute &= Tok.is(tok::r_square);
523
524 PA.Revert();
525
526 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
527 }
528
529 // In Obj-C++11, we need to distinguish four situations:
530 // 1a) int x[[attr]]; C++11 attribute.
531 // 1b) [[attr]]; C++11 statement attribute.
532 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
533 // 3a) int x[[obj get]]; Message send in array size/index.
534 // 3b) [[Class alloc] init]; Message send in message send.
535 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
536 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
537
538 // If we have a lambda-introducer, then this is definitely not a message send.
539 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
540 // into the tentative attribute parse below.
541 LambdaIntroducer Intro;
542 if (!TryParseLambdaIntroducer(Intro)) {
543 // A lambda cannot end with ']]', and an attribute must.
544 bool IsAttribute = Tok.is(tok::r_square);
545
546 PA.Revert();
547
548 if (IsAttribute)
549 // Case 1: C++11 attribute.
550 return CAK_AttributeSpecifier;
551
552 if (OuterMightBeMessageSend)
553 // Case 4: Lambda in message send.
554 return CAK_NotAttributeSpecifier;
555
556 // Case 2: Lambda in array size / index.
557 return CAK_InvalidAttributeSpecifier;
558 }
559
560 ConsumeBracket();
561
562 // If we don't have a lambda-introducer, then we have an attribute or a
563 // message-send.
564 bool IsAttribute = true;
565 while (Tok.isNot(tok::r_square)) {
566 if (Tok.is(tok::comma)) {
567 // Case 1: Stray commas can only occur in attributes.
568 PA.Revert();
569 return CAK_AttributeSpecifier;
570 }
571
572 // Parse the attribute-token, if present.
573 // C++11 [dcl.attr.grammar]:
574 // If a keyword or an alternative token that satisfies the syntactic
575 // requirements of an identifier is contained in an attribute-token,
576 // it is considered an identifier.
577 SourceLocation Loc;
578 if (!TryParseCXX11AttributeIdentifier(Loc)) {
579 IsAttribute = false;
580 break;
581 }
582 if (Tok.is(tok::coloncolon)) {
583 ConsumeToken();
584 if (!TryParseCXX11AttributeIdentifier(Loc)) {
585 IsAttribute = false;
586 break;
587 }
588 }
589
590 // Parse the attribute-argument-clause, if present.
591 if (Tok.is(tok::l_paren)) {
592 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000593 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000594 IsAttribute = false;
595 break;
596 }
597 }
598
Alp Toker97650562014-01-10 11:19:30 +0000599 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000600
Alp Toker97650562014-01-10 11:19:30 +0000601 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000602 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000603 }
604
605 // An attribute must end ']]'.
606 if (IsAttribute) {
607 if (Tok.is(tok::r_square)) {
608 ConsumeBracket();
609 IsAttribute = Tok.is(tok::r_square);
610 } else {
611 IsAttribute = false;
612 }
613 }
614
615 PA.Revert();
616
617 if (IsAttribute)
618 // Case 1: C++11 statement attribute.
619 return CAK_AttributeSpecifier;
620
621 // Case 3: Message send.
622 return CAK_NotAttributeSpecifier;
623}
624
Richard Smith1fff95c2013-09-12 23:28:08 +0000625Parser::TPResult Parser::TryParsePtrOperatorSeq() {
626 while (true) {
627 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
628 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000629 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000630
631 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
632 Tok.is(tok::ampamp) ||
633 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
634 // ptr-operator
635 ConsumeToken();
636 while (Tok.is(tok::kw_const) ||
637 Tok.is(tok::kw_volatile) ||
638 Tok.is(tok::kw_restrict))
639 ConsumeToken();
640 } else {
Richard Smithee390432014-05-16 01:56:53 +0000641 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000642 }
643 }
644}
645
646/// operator-function-id:
647/// 'operator' operator
648///
649/// operator: one of
650/// new delete new[] delete[] + - * / % ^ [...]
651///
652/// conversion-function-id:
653/// 'operator' conversion-type-id
654///
655/// conversion-type-id:
656/// type-specifier-seq conversion-declarator[opt]
657///
658/// conversion-declarator:
659/// ptr-operator conversion-declarator[opt]
660///
661/// literal-operator-id:
662/// 'operator' string-literal identifier
663/// 'operator' user-defined-string-literal
664Parser::TPResult Parser::TryParseOperatorId() {
665 assert(Tok.is(tok::kw_operator));
666 ConsumeToken();
667
668 // Maybe this is an operator-function-id.
669 switch (Tok.getKind()) {
670 case tok::kw_new: case tok::kw_delete:
671 ConsumeToken();
672 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
673 ConsumeBracket();
674 ConsumeBracket();
675 }
Richard Smithee390432014-05-16 01:56:53 +0000676 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000677
678#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
679 case tok::Token:
680#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
681#include "clang/Basic/OperatorKinds.def"
682 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000683 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000684
685 case tok::l_square:
686 if (NextToken().is(tok::r_square)) {
687 ConsumeBracket();
688 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000689 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000690 }
691 break;
692
693 case tok::l_paren:
694 if (NextToken().is(tok::r_paren)) {
695 ConsumeParen();
696 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000697 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000698 }
699 break;
700
701 default:
702 break;
703 }
704
705 // Maybe this is a literal-operator-id.
706 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
707 bool FoundUDSuffix = false;
708 do {
709 FoundUDSuffix |= Tok.hasUDSuffix();
710 ConsumeStringToken();
711 } while (isTokenStringLiteral());
712
713 if (!FoundUDSuffix) {
714 if (Tok.is(tok::identifier))
715 ConsumeToken();
716 else
Richard Smithee390432014-05-16 01:56:53 +0000717 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000718 }
Richard Smithee390432014-05-16 01:56:53 +0000719 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000720 }
721
722 // Maybe this is a conversion-function-id.
723 bool AnyDeclSpecifiers = false;
724 while (true) {
725 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000726 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000727 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000728 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000729 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000730 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000731 break;
732 }
Richard Smithee390432014-05-16 01:56:53 +0000733 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
734 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000735 AnyDeclSpecifiers = true;
736 }
737 return TryParsePtrOperatorSeq();
738}
739
Guy Benyei11169dd2012-12-18 14:30:41 +0000740/// declarator:
741/// direct-declarator
742/// ptr-operator declarator
743///
744/// direct-declarator:
745/// declarator-id
746/// direct-declarator '(' parameter-declaration-clause ')'
747/// cv-qualifier-seq[opt] exception-specification[opt]
748/// direct-declarator '[' constant-expression[opt] ']'
749/// '(' declarator ')'
750/// [GNU] '(' attributes declarator ')'
751///
752/// abstract-declarator:
753/// ptr-operator abstract-declarator[opt]
754/// direct-abstract-declarator
755/// ...
756///
757/// direct-abstract-declarator:
758/// direct-abstract-declarator[opt]
759/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
760/// exception-specification[opt]
761/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
762/// '(' abstract-declarator ')'
763///
764/// ptr-operator:
765/// '*' cv-qualifier-seq[opt]
766/// '&'
767/// [C++0x] '&&' [TODO]
768/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
769///
770/// cv-qualifier-seq:
771/// cv-qualifier cv-qualifier-seq[opt]
772///
773/// cv-qualifier:
774/// 'const'
775/// 'volatile'
776///
777/// declarator-id:
778/// '...'[opt] id-expression
779///
780/// id-expression:
781/// unqualified-id
782/// qualified-id [TODO]
783///
784/// unqualified-id:
785/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000786/// operator-function-id
787/// conversion-function-id
788/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000789/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000790/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000791/// template-id [TODO]
792///
793Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
794 bool mayHaveIdentifier) {
795 // declarator:
796 // direct-declarator
797 // ptr-operator declarator
Richard Smithee390432014-05-16 01:56:53 +0000798 if (TryParsePtrOperatorSeq() == TPResult::Error)
799 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000800
801 // direct-declarator:
802 // direct-abstract-declarator:
803 if (Tok.is(tok::ellipsis))
804 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000805
806 if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
807 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
808 NextToken().is(tok::kw_operator)))) &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000809 mayHaveIdentifier) {
810 // declarator-id
811 if (Tok.is(tok::annot_cxxscope))
812 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000813 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000814 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000815 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000816 if (TryParseOperatorId() == TPResult::Error)
817 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000818 } else
819 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000820 } else if (Tok.is(tok::l_paren)) {
821 ConsumeParen();
822 if (mayBeAbstract &&
823 (Tok.is(tok::r_paren) || // 'int()' is a function.
824 // 'int(...)' is a function.
825 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
826 isDeclarationSpecifier())) { // 'int(int)' is a function.
827 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
828 // exception-specification[opt]
829 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000830 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000831 return TPR;
832 } else {
833 // '(' declarator ')'
834 // '(' attributes declarator ')'
835 // '(' abstract-declarator ')'
836 if (Tok.is(tok::kw___attribute) ||
837 Tok.is(tok::kw___declspec) ||
838 Tok.is(tok::kw___cdecl) ||
839 Tok.is(tok::kw___stdcall) ||
840 Tok.is(tok::kw___fastcall) ||
841 Tok.is(tok::kw___thiscall) ||
Reid Klecknerd7857f02014-10-24 17:42:17 +0000842 Tok.is(tok::kw___vectorcall) ||
Guy Benyei11169dd2012-12-18 14:30:41 +0000843 Tok.is(tok::kw___unaligned))
Richard Smithee390432014-05-16 01:56:53 +0000844 return TPResult::True; // attributes indicate declaration
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000846 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000847 return TPR;
848 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000849 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000850 ConsumeParen();
851 }
852 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000853 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 }
855
856 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000857 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000858
859 // abstract-declarator: ...
860 if (Tok.is(tok::ellipsis))
861 ConsumeToken();
862
863 if (Tok.is(tok::l_paren)) {
864 // Check whether we have a function declarator or a possible ctor-style
865 // initializer that follows the declarator. Note that ctor-style
866 // initializers are not possible in contexts where abstract declarators
867 // are allowed.
868 if (!mayBeAbstract && !isCXXFunctionDeclarator())
869 break;
870
871 // direct-declarator '(' parameter-declaration-clause ')'
872 // cv-qualifier-seq[opt] exception-specification[opt]
873 ConsumeParen();
874 TPR = TryParseFunctionDeclarator();
875 } else if (Tok.is(tok::l_square)) {
876 // direct-declarator '[' constant-expression[opt] ']'
877 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
878 TPR = TryParseBracketDeclarator();
879 } else {
880 break;
881 }
882
Richard Smithee390432014-05-16 01:56:53 +0000883 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000884 return TPR;
885 }
886
Richard Smithee390432014-05-16 01:56:53 +0000887 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000888}
889
890Parser::TPResult
891Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
892 switch (Kind) {
893 // Obviously starts an expression.
894 case tok::numeric_constant:
895 case tok::char_constant:
896 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000897 case tok::utf8_char_constant:
Guy Benyei11169dd2012-12-18 14:30:41 +0000898 case tok::utf16_char_constant:
899 case tok::utf32_char_constant:
900 case tok::string_literal:
901 case tok::wide_string_literal:
902 case tok::utf8_string_literal:
903 case tok::utf16_string_literal:
904 case tok::utf32_string_literal:
905 case tok::l_square:
906 case tok::l_paren:
907 case tok::amp:
908 case tok::ampamp:
909 case tok::star:
910 case tok::plus:
911 case tok::plusplus:
912 case tok::minus:
913 case tok::minusminus:
914 case tok::tilde:
915 case tok::exclaim:
916 case tok::kw_sizeof:
917 case tok::kw___func__:
918 case tok::kw_const_cast:
919 case tok::kw_delete:
920 case tok::kw_dynamic_cast:
921 case tok::kw_false:
922 case tok::kw_new:
923 case tok::kw_operator:
924 case tok::kw_reinterpret_cast:
925 case tok::kw_static_cast:
926 case tok::kw_this:
927 case tok::kw_throw:
928 case tok::kw_true:
929 case tok::kw_typeid:
930 case tok::kw_alignof:
931 case tok::kw_noexcept:
932 case tok::kw_nullptr:
933 case tok::kw__Alignof:
934 case tok::kw___null:
935 case tok::kw___alignof:
936 case tok::kw___builtin_choose_expr:
937 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +0000938 case tok::kw___builtin_va_arg:
939 case tok::kw___imag:
940 case tok::kw___real:
941 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +0000942 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +0000943 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000944 case tok::kw_L__FUNCTION__:
945 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000946 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +0000947#define TYPE_TRAIT(N,Spelling,K) \
948 case tok::kw_##Spelling:
949#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +0000950 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000951
952 // Obviously starts a type-specifier-seq:
953 case tok::kw_char:
954 case tok::kw_const:
955 case tok::kw_double:
956 case tok::kw_enum:
957 case tok::kw_half:
958 case tok::kw_float:
959 case tok::kw_int:
960 case tok::kw_long:
961 case tok::kw___int64:
962 case tok::kw___int128:
963 case tok::kw_restrict:
964 case tok::kw_short:
965 case tok::kw_signed:
966 case tok::kw_struct:
967 case tok::kw_union:
968 case tok::kw_unsigned:
969 case tok::kw_void:
970 case tok::kw_volatile:
971 case tok::kw__Bool:
972 case tok::kw__Complex:
973 case tok::kw_class:
974 case tok::kw_typename:
975 case tok::kw_wchar_t:
976 case tok::kw_char16_t:
977 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 case tok::kw__Decimal32:
979 case tok::kw__Decimal64:
980 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +0000981 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +0000982 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +0000983 case tok::kw_thread_local:
984 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +0000985 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +0000986 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +0000987 case tok::kw___cdecl:
988 case tok::kw___stdcall:
989 case tok::kw___fastcall:
990 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +0000991 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +0000992 case tok::kw___unaligned:
993 case tok::kw___vector:
994 case tok::kw___pixel:
995 case tok::kw__Atomic:
996 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +0000997 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000998
999 default:
1000 break;
1001 }
1002
Richard Smithee390432014-05-16 01:56:53 +00001003 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001004}
1005
1006bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1007 return std::find(TentativelyDeclaredIdentifiers.begin(),
1008 TentativelyDeclaredIdentifiers.end(), II)
1009 != TentativelyDeclaredIdentifiers.end();
1010}
1011
Kaelyn Takata445b0652014-11-05 00:09:29 +00001012namespace {
1013class TentativeParseCCC : public CorrectionCandidateCallback {
1014public:
1015 TentativeParseCCC(const Token &Next) {
1016 WantRemainingKeywords = false;
1017 WantTypeSpecifiers = Next.is(tok::l_paren) || Next.is(tok::r_paren) ||
1018 Next.is(tok::greater) || Next.is(tok::l_brace) ||
1019 Next.is(tok::identifier);
1020 }
1021
1022 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1023 // Reject any candidate that only resolves to instance members since they
1024 // aren't viable as standalone identifiers instead of member references.
1025 if (Candidate.isResolved() && !Candidate.isKeyword() &&
1026 std::all_of(Candidate.begin(), Candidate.end(),
1027 [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1028 return false;
1029
1030 return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1031 }
1032};
1033}
Richard Smithee390432014-05-16 01:56:53 +00001034/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1035/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1036/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001037/// if a parsing error was found and reported.
1038///
1039/// If HasMissingTypename is provided, a name with a dependent scope specifier
1040/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1041/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1042/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001043/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001044/// such cases.
1045///
1046/// decl-specifier:
1047/// storage-class-specifier
1048/// type-specifier
1049/// function-specifier
1050/// 'friend'
1051/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001052/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001053/// [GNU] attributes declaration-specifiers[opt]
1054///
1055/// storage-class-specifier:
1056/// 'register'
1057/// 'static'
1058/// 'extern'
1059/// 'mutable'
1060/// 'auto'
1061/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001062/// [C++11] 'thread_local'
1063/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001064///
1065/// function-specifier:
1066/// 'inline'
1067/// 'virtual'
1068/// 'explicit'
1069///
1070/// typedef-name:
1071/// identifier
1072///
1073/// type-specifier:
1074/// simple-type-specifier
1075/// class-specifier
1076/// enum-specifier
1077/// elaborated-type-specifier
1078/// typename-specifier
1079/// cv-qualifier
1080///
1081/// simple-type-specifier:
1082/// '::'[opt] nested-name-specifier[opt] type-name
1083/// '::'[opt] nested-name-specifier 'template'
1084/// simple-template-id [TODO]
1085/// 'char'
1086/// 'wchar_t'
1087/// 'bool'
1088/// 'short'
1089/// 'int'
1090/// 'long'
1091/// 'signed'
1092/// 'unsigned'
1093/// 'float'
1094/// 'double'
1095/// 'void'
1096/// [GNU] typeof-specifier
1097/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001098/// [C++11] 'auto'
1099/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001100/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001101///
1102/// type-name:
1103/// class-name
1104/// enum-name
1105/// typedef-name
1106///
1107/// elaborated-type-specifier:
1108/// class-key '::'[opt] nested-name-specifier[opt] identifier
1109/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1110/// simple-template-id
1111/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1112///
1113/// enum-name:
1114/// identifier
1115///
1116/// enum-specifier:
1117/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1118/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1119///
1120/// class-specifier:
1121/// class-head '{' member-specification[opt] '}'
1122///
1123/// class-head:
1124/// class-key identifier[opt] base-clause[opt]
1125/// class-key nested-name-specifier identifier base-clause[opt]
1126/// class-key nested-name-specifier[opt] simple-template-id
1127/// base-clause[opt]
1128///
1129/// class-key:
1130/// 'class'
1131/// 'struct'
1132/// 'union'
1133///
1134/// cv-qualifier:
1135/// 'const'
1136/// 'volatile'
1137/// [GNU] restrict
1138///
1139Parser::TPResult
1140Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1141 bool *HasMissingTypename) {
1142 switch (Tok.getKind()) {
1143 case tok::identifier: {
1144 // Check for need to substitute AltiVec __vector keyword
1145 // for "vector" identifier.
1146 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001147 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001148
1149 const Token &Next = NextToken();
1150 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1151 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001152 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001153
1154 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1155 // Determine whether this is a valid expression. If not, we will hit
1156 // a parse error one way or another. In that case, tell the caller that
1157 // this is ambiguous. Typo-correct to type and expression keywords and
1158 // to types and identifiers, in order to try to recover from errors.
Guy Benyei11169dd2012-12-18 14:30:41 +00001159 switch (TryAnnotateName(false /* no nested name specifier */,
Kaelyn Takata445b0652014-11-05 00:09:29 +00001160 llvm::make_unique<TentativeParseCCC>(Next))) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001161 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001162 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001163 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001164 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 case ANK_TemplateName:
1166 // A bare type template-name which can't be a template template
1167 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001168 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001169 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001170 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001171 case ANK_Success:
1172 break;
1173 }
1174 assert(Tok.isNot(tok::identifier) &&
1175 "TryAnnotateName succeeded without producing an annotation");
1176 } else {
1177 // This might possibly be a type with a dependent scope specifier and
1178 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1179 // since it will annotate as a primary expression, and we want to use the
1180 // "missing 'typename'" logic.
1181 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001182 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001183 // If annotation failed, assume it's a non-type.
1184 // FIXME: If this happens due to an undeclared identifier, treat it as
1185 // ambiguous.
1186 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001187 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001188 }
1189
1190 // We annotated this token as something. Recurse to handle whatever we got.
1191 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1192 }
1193
1194 case tok::kw_typename: // typename T::type
1195 // Annotate typenames and C++ scope specifiers. If we get one, just
1196 // recurse to handle whatever we get.
1197 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001198 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1200
1201 case tok::coloncolon: { // ::foo::bar
1202 const Token &Next = NextToken();
1203 if (Next.is(tok::kw_new) || // ::new
1204 Next.is(tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001205 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001206 }
1207 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001208 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001209 case tok::kw_decltype:
1210 // Annotate typenames and C++ scope specifiers. If we get one, just
1211 // recurse to handle whatever we get.
1212 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001213 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1215
1216 // decl-specifier:
1217 // storage-class-specifier
1218 // type-specifier
1219 // function-specifier
1220 // 'friend'
1221 // 'typedef'
1222 // 'constexpr'
1223 case tok::kw_friend:
1224 case tok::kw_typedef:
1225 case tok::kw_constexpr:
1226 // storage-class-specifier
1227 case tok::kw_register:
1228 case tok::kw_static:
1229 case tok::kw_extern:
1230 case tok::kw_mutable:
1231 case tok::kw_auto:
1232 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001233 case tok::kw_thread_local:
1234 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 // function-specifier
1236 case tok::kw_inline:
1237 case tok::kw_virtual:
1238 case tok::kw_explicit:
1239
1240 // Modules
1241 case tok::kw___module_private__:
1242
1243 // Debugger support
1244 case tok::kw___unknown_anytype:
1245
1246 // type-specifier:
1247 // simple-type-specifier
1248 // class-specifier
1249 // enum-specifier
1250 // elaborated-type-specifier
1251 // typename-specifier
1252 // cv-qualifier
1253
1254 // class-specifier
1255 // elaborated-type-specifier
1256 case tok::kw_class:
1257 case tok::kw_struct:
1258 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001259 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 // enum-specifier
1261 case tok::kw_enum:
1262 // cv-qualifier
1263 case tok::kw_const:
1264 case tok::kw_volatile:
1265
1266 // GNU
1267 case tok::kw_restrict:
1268 case tok::kw__Complex:
1269 case tok::kw___attribute:
Richard Smithee390432014-05-16 01:56:53 +00001270 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001271
1272 // Microsoft
1273 case tok::kw___declspec:
1274 case tok::kw___cdecl:
1275 case tok::kw___stdcall:
1276 case tok::kw___fastcall:
1277 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00001278 case tok::kw___vectorcall:
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001280 case tok::kw___sptr:
1281 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001282 case tok::kw___ptr64:
1283 case tok::kw___ptr32:
1284 case tok::kw___forceinline:
1285 case tok::kw___unaligned:
Richard Smithee390432014-05-16 01:56:53 +00001286 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001287
1288 // Borland
1289 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001290 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001291
1292 // AltiVec
1293 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001294 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001295
1296 case tok::annot_template_id: {
1297 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1298 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001299 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001300 CXXScopeSpec SS;
1301 AnnotateTemplateIdTokenAsType();
1302 assert(Tok.is(tok::annot_typename));
1303 goto case_typename;
1304 }
1305
1306 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1307 // We've already annotated a scope; try to annotate a type.
1308 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001309 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 if (!Tok.is(tok::annot_typename)) {
1311 // If the next token is an identifier or a type qualifier, then this
1312 // can't possibly be a valid expression either.
1313 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1314 CXXScopeSpec SS;
1315 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1316 Tok.getAnnotationRange(),
1317 SS);
1318 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1319 TentativeParsingAction PA(*this);
1320 ConsumeToken();
1321 ConsumeToken();
1322 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001323 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001324 if (!isIdentifier)
1325 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1326 HasMissingTypename);
1327 PA.Revert();
1328
1329 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001330 TPR == TPResult::True || TPR == TPResult::Error)
1331 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332
1333 if (HasMissingTypename) {
1334 // We can't tell whether this is a missing 'typename' or a valid
1335 // expression.
1336 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001337 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001338 }
1339 } else {
1340 // Try to resolve the name. If it doesn't exist, assume it was
1341 // intended to name a type and keep disambiguating.
1342 switch (TryAnnotateName(false /* SS is not dependent */)) {
1343 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001344 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001345 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001346 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 case ANK_TemplateName:
1348 // A bare type template-name which can't be a template template
1349 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001350 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001351 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001352 return HasMissingTypename ? TPResult::Ambiguous
1353 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001354 case ANK_Success:
1355 // Annotated it, check again.
1356 assert(Tok.isNot(tok::annot_cxxscope) ||
1357 NextToken().isNot(tok::identifier));
1358 return isCXXDeclarationSpecifier(BracedCastResult,
1359 HasMissingTypename);
1360 }
1361 }
1362 }
Richard Smithee390432014-05-16 01:56:53 +00001363 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 }
1365 // If that succeeded, fallthrough into the generic simple-type-id case.
1366
1367 // The ambiguity resides in a simple-type-specifier/typename-specifier
1368 // followed by a '('. The '(' could either be the start of:
1369 //
1370 // direct-declarator:
1371 // '(' declarator ')'
1372 //
1373 // direct-abstract-declarator:
1374 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1375 // exception-specification[opt]
1376 // '(' abstract-declarator ')'
1377 //
1378 // or part of a function-style cast expression:
1379 //
1380 // simple-type-specifier '(' expression-list[opt] ')'
1381 //
1382
1383 // simple-type-specifier:
1384
1385 case tok::annot_typename:
1386 case_typename:
1387 // In Objective-C, we might have a protocol-qualified type.
1388 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1389 // Tentatively parse the
1390 TentativeParsingAction PA(*this);
1391 ConsumeToken(); // The type token
1392
1393 TPResult TPR = TryParseProtocolQualifiers();
1394 bool isFollowedByParen = Tok.is(tok::l_paren);
1395 bool isFollowedByBrace = Tok.is(tok::l_brace);
1396
1397 PA.Revert();
1398
Richard Smithee390432014-05-16 01:56:53 +00001399 if (TPR == TPResult::Error)
1400 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001401
1402 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001403 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001404
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001405 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 return BracedCastResult;
1407
Richard Smithee390432014-05-16 01:56:53 +00001408 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 }
1410
1411 case tok::kw_char:
1412 case tok::kw_wchar_t:
1413 case tok::kw_char16_t:
1414 case tok::kw_char32_t:
1415 case tok::kw_bool:
1416 case tok::kw_short:
1417 case tok::kw_int:
1418 case tok::kw_long:
1419 case tok::kw___int64:
1420 case tok::kw___int128:
1421 case tok::kw_signed:
1422 case tok::kw_unsigned:
1423 case tok::kw_half:
1424 case tok::kw_float:
1425 case tok::kw_double:
1426 case tok::kw_void:
1427 case tok::annot_decltype:
1428 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001429 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001430
1431 // This is a function-style cast in all cases we disambiguate other than
1432 // one:
1433 // struct S {
1434 // enum E : int { a = 4 }; // enum
1435 // enum E : int { 4 }; // bit-field
1436 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001437 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 return BracedCastResult;
1439
1440 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001441 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001442
Richard Smithee390432014-05-16 01:56:53 +00001443 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001444
1445 // GNU typeof support.
1446 case tok::kw_typeof: {
1447 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001448 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001449
1450 TentativeParsingAction PA(*this);
1451
1452 TPResult TPR = TryParseTypeofSpecifier();
1453 bool isFollowedByParen = Tok.is(tok::l_paren);
1454 bool isFollowedByBrace = Tok.is(tok::l_brace);
1455
1456 PA.Revert();
1457
Richard Smithee390432014-05-16 01:56:53 +00001458 if (TPR == TPResult::Error)
1459 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001460
1461 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001462 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001463
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001464 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001465 return BracedCastResult;
1466
Richard Smithee390432014-05-16 01:56:53 +00001467 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001468 }
1469
1470 // C++0x type traits support
1471 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001472 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001473
1474 // C11 _Atomic
1475 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001476 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001477
1478 default:
Richard Smithee390432014-05-16 01:56:53 +00001479 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001480 }
1481}
1482
Richard Smith1fff95c2013-09-12 23:28:08 +00001483bool Parser::isCXXDeclarationSpecifierAType() {
1484 switch (Tok.getKind()) {
1485 // typename-specifier
1486 case tok::annot_decltype:
1487 case tok::annot_template_id:
1488 case tok::annot_typename:
1489 case tok::kw_typeof:
1490 case tok::kw___underlying_type:
1491 return true;
1492
1493 // elaborated-type-specifier
1494 case tok::kw_class:
1495 case tok::kw_struct:
1496 case tok::kw_union:
1497 case tok::kw___interface:
1498 case tok::kw_enum:
1499 return true;
1500
1501 // simple-type-specifier
1502 case tok::kw_char:
1503 case tok::kw_wchar_t:
1504 case tok::kw_char16_t:
1505 case tok::kw_char32_t:
1506 case tok::kw_bool:
1507 case tok::kw_short:
1508 case tok::kw_int:
1509 case tok::kw_long:
1510 case tok::kw___int64:
1511 case tok::kw___int128:
1512 case tok::kw_signed:
1513 case tok::kw_unsigned:
1514 case tok::kw_half:
1515 case tok::kw_float:
1516 case tok::kw_double:
1517 case tok::kw_void:
1518 case tok::kw___unknown_anytype:
1519 return true;
1520
1521 case tok::kw_auto:
1522 return getLangOpts().CPlusPlus11;
1523
1524 case tok::kw__Atomic:
1525 // "_Atomic foo"
1526 return NextToken().is(tok::l_paren);
1527
1528 default:
1529 return false;
1530 }
1531}
1532
Guy Benyei11169dd2012-12-18 14:30:41 +00001533/// [GNU] typeof-specifier:
1534/// 'typeof' '(' expressions ')'
1535/// 'typeof' '(' type-name ')'
1536///
1537Parser::TPResult Parser::TryParseTypeofSpecifier() {
1538 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1539 ConsumeToken();
1540
1541 assert(Tok.is(tok::l_paren) && "Expected '('");
1542 // Parse through the parens after 'typeof'.
1543 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001544 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001545 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001546
Richard Smithee390432014-05-16 01:56:53 +00001547 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001548}
1549
1550/// [ObjC] protocol-qualifiers:
1551//// '<' identifier-list '>'
1552Parser::TPResult Parser::TryParseProtocolQualifiers() {
1553 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1554 ConsumeToken();
1555 do {
1556 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001557 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001558 ConsumeToken();
1559
1560 if (Tok.is(tok::comma)) {
1561 ConsumeToken();
1562 continue;
1563 }
1564
1565 if (Tok.is(tok::greater)) {
1566 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001567 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001568 }
1569 } while (false);
1570
Richard Smithee390432014-05-16 01:56:53 +00001571 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001572}
1573
Guy Benyei11169dd2012-12-18 14:30:41 +00001574/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1575/// a constructor-style initializer, when parsing declaration statements.
1576/// Returns true for function declarator and false for constructor-style
1577/// initializer.
1578/// If during the disambiguation process a parsing error is encountered,
1579/// the function returns true to let the declaration parsing code handle it.
1580///
1581/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1582/// exception-specification[opt]
1583///
1584bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1585
1586 // C++ 8.2p1:
1587 // The ambiguity arising from the similarity between a function-style cast and
1588 // a declaration mentioned in 6.8 can also occur in the context of a
1589 // declaration. In that context, the choice is between a function declaration
1590 // with a redundant set of parentheses around a parameter name and an object
1591 // declaration with a function-style cast as the initializer. Just as for the
1592 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1593 // that could possibly be a declaration a declaration.
1594
1595 TentativeParsingAction PA(*this);
1596
1597 ConsumeParen();
1598 bool InvalidAsDeclaration = false;
1599 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001600 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001602 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 else {
1604 const Token &Next = NextToken();
1605 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1606 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1607 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001608 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1610 Next.is(tok::equal) || Next.is(tok::arrow))
1611 // The next token cannot appear after a constructor-style initializer,
1612 // and can appear next in a function definition. This must be a function
1613 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001614 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 else if (InvalidAsDeclaration)
1616 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001617 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001618 }
1619 }
1620
1621 PA.Revert();
1622
Richard Smithee390432014-05-16 01:56:53 +00001623 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 *IsAmbiguous = true;
1625
1626 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001627 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001628}
1629
1630/// parameter-declaration-clause:
1631/// parameter-declaration-list[opt] '...'[opt]
1632/// parameter-declaration-list ',' '...'
1633///
1634/// parameter-declaration-list:
1635/// parameter-declaration
1636/// parameter-declaration-list ',' parameter-declaration
1637///
1638/// parameter-declaration:
1639/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1640/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1641/// '=' assignment-expression
1642/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1643/// attributes[opt]
1644/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1645/// attributes[opt] '=' assignment-expression
1646///
1647Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001648Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1649 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001650
1651 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001652 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001653
1654 // parameter-declaration-list[opt] '...'[opt]
1655 // parameter-declaration-list ',' '...'
1656 //
1657 // parameter-declaration-list:
1658 // parameter-declaration
1659 // parameter-declaration-list ',' parameter-declaration
1660 //
1661 while (1) {
1662 // '...'[opt]
1663 if (Tok.is(tok::ellipsis)) {
1664 ConsumeToken();
1665 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001666 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 else
Richard Smithee390432014-05-16 01:56:53 +00001668 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001669 }
1670
1671 // An attribute-specifier-seq here is a sign of a function declarator.
1672 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1673 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001674 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001675
1676 ParsedAttributes attrs(AttrFactory);
1677 MaybeParseMicrosoftAttributes(attrs);
1678
1679 // decl-specifier-seq
1680 // A parameter-declaration's initializer must be preceded by an '=', so
1681 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001682 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001683 InvalidAsDeclaration);
1684
Richard Smithee390432014-05-16 01:56:53 +00001685 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001686 // Consume the decl-specifier-seq. We have to look past it, since a
1687 // type-id might appear here in a template argument.
1688 bool SeenType = false;
1689 do {
1690 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001691 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1692 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001693
1694 // If we see a parameter name, this can't be a template argument.
1695 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001696 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001697
Richard Smithee390432014-05-16 01:56:53 +00001698 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001699 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001700 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001701 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001702 } while (TPR != TPResult::False);
1703 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001704 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001705 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1706 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001707 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 return TPR;
1709
1710 // declarator
1711 // abstract-declarator[opt]
1712 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001713 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001714 return TPR;
1715
1716 // [GNU] attributes[opt]
1717 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001718 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001719
Richard Smith1fff95c2013-09-12 23:28:08 +00001720 // If we're disambiguating a template argument in a default argument in
1721 // a class definition versus a parameter declaration, an '=' here
1722 // disambiguates the parse one way or the other.
1723 // If this is a parameter, it must have a default argument because
1724 // (a) the previous parameter did, and
1725 // (b) this must be the first declaration of the function, so we can't
1726 // inherit any default arguments from elsewhere.
1727 // If we see an ')', then we've reached the end of a
1728 // parameter-declaration-clause, and the last param is missing its default
1729 // argument.
1730 if (VersusTemplateArgument)
Richard Smithee390432014-05-16 01:56:53 +00001731 return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True
1732 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001733
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 if (Tok.is(tok::equal)) {
1735 // '=' assignment-expression
1736 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001737 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001738 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001739 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001740 }
1741
1742 if (Tok.is(tok::ellipsis)) {
1743 ConsumeToken();
1744 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001745 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001746 else
Richard Smithee390432014-05-16 01:56:53 +00001747 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 }
1749
Alp Toker97650562014-01-10 11:19:30 +00001750 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001751 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001752 }
1753
Richard Smithee390432014-05-16 01:56:53 +00001754 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001755}
1756
1757/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1758/// parsing as a function declarator.
1759/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Richard Smithee390432014-05-16 01:56:53 +00001760/// return TPResult::Ambiguous, otherwise it will return either False() or
Guy Benyei11169dd2012-12-18 14:30:41 +00001761/// Error().
1762///
1763/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1764/// exception-specification[opt]
1765///
1766/// exception-specification:
1767/// 'throw' '(' type-id-list[opt] ')'
1768///
1769Parser::TPResult Parser::TryParseFunctionDeclarator() {
1770
1771 // The '(' is already parsed.
1772
1773 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001774 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1775 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001776
Richard Smithee390432014-05-16 01:56:53 +00001777 if (TPR == TPResult::False || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 return TPR;
1779
1780 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001781 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001782 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001783
1784 // cv-qualifier-seq
1785 while (Tok.is(tok::kw_const) ||
1786 Tok.is(tok::kw_volatile) ||
1787 Tok.is(tok::kw_restrict) )
1788 ConsumeToken();
1789
1790 // ref-qualifier[opt]
1791 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1792 ConsumeToken();
1793
1794 // exception-specification
1795 if (Tok.is(tok::kw_throw)) {
1796 ConsumeToken();
1797 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001798 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001799
1800 // Parse through the parens after 'throw'.
1801 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001802 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001803 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 }
1805 if (Tok.is(tok::kw_noexcept)) {
1806 ConsumeToken();
1807 // Possibly an expression as well.
1808 if (Tok.is(tok::l_paren)) {
1809 // Find the matching rparen.
1810 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001811 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001812 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001813 }
1814 }
1815
Richard Smithee390432014-05-16 01:56:53 +00001816 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001817}
1818
1819/// '[' constant-expression[opt] ']'
1820///
1821Parser::TPResult Parser::TryParseBracketDeclarator() {
1822 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001823 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001824 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001825
Richard Smithee390432014-05-16 01:56:53 +00001826 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001827}