blob: 4060fab658ae874d4ccd7cbc03a2c7bbea9c5c39 [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
198 if (TryAnnotateCXXScopeToken())
Richard Smithee390432014-05-16 01:56:53 +0000199 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000200 if (Tok.is(tok::annot_cxxscope))
201 ConsumeToken();
202 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Richard Smithee390432014-05-16 01:56:53 +0000203 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000204 ConsumeToken();
205 break;
206
207 case tok::annot_cxxscope:
208 ConsumeToken();
209 // Fall through.
210 default:
211 ConsumeToken();
212
213 if (getLangOpts().ObjC1 && Tok.is(tok::less))
214 return TryParseProtocolQualifiers();
215 break;
216 }
217
Richard Smithee390432014-05-16 01:56:53 +0000218 return TPResult::Ambiguous;
Richard Smith1fff95c2013-09-12 23:28:08 +0000219}
220
Guy Benyei11169dd2012-12-18 14:30:41 +0000221/// simple-declaration:
222/// decl-specifier-seq init-declarator-list[opt] ';'
223///
224/// (if AllowForRangeDecl specified)
225/// for ( for-range-declaration : for-range-initializer ) statement
226/// for-range-declaration:
227/// attribute-specifier-seqopt type-specifier-seq declarator
228///
229Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Richard Smithee390432014-05-16 01:56:53 +0000230 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
231 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000232
233 // Two decl-specifiers in a row conclusively disambiguate this as being a
234 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
235 // overwhelmingly common case that the next token is a '('.
236 if (Tok.isNot(tok::l_paren)) {
237 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000238 if (TPR == TPResult::Ambiguous)
239 return TPResult::True;
240 if (TPR == TPResult::True || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +0000241 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000242 assert(TPR == TPResult::False);
Guy Benyei11169dd2012-12-18 14:30:41 +0000243 }
244
245 TPResult TPR = TryParseInitDeclaratorList();
Richard Smithee390432014-05-16 01:56:53 +0000246 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000247 return TPR;
248
249 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Richard Smithee390432014-05-16 01:56:53 +0000250 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000251
Richard Smithee390432014-05-16 01:56:53 +0000252 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000253}
254
Richard Smith22c7c412013-03-20 03:35:02 +0000255/// Tentatively parse an init-declarator-list in order to disambiguate it from
256/// an expression.
257///
Guy Benyei11169dd2012-12-18 14:30:41 +0000258/// init-declarator-list:
259/// init-declarator
260/// init-declarator-list ',' init-declarator
261///
262/// init-declarator:
263/// declarator initializer[opt]
264/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
265///
Richard Smith22c7c412013-03-20 03:35:02 +0000266/// initializer:
267/// brace-or-equal-initializer
268/// '(' expression-list ')'
Guy Benyei11169dd2012-12-18 14:30:41 +0000269///
Richard Smith22c7c412013-03-20 03:35:02 +0000270/// brace-or-equal-initializer:
271/// '=' initializer-clause
272/// [C++11] braced-init-list
273///
274/// initializer-clause:
275/// assignment-expression
276/// braced-init-list
277///
278/// braced-init-list:
279/// '{' initializer-list ','[opt] '}'
280/// '{' '}'
Guy Benyei11169dd2012-12-18 14:30:41 +0000281///
282Parser::TPResult Parser::TryParseInitDeclaratorList() {
283 while (1) {
284 // declarator
285 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +0000286 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000287 return TPR;
288
289 // [GNU] simple-asm-expr[opt] attributes[opt]
290 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000291 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000292
293 // initializer[opt]
294 if (Tok.is(tok::l_paren)) {
295 // Parse through the parens.
296 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000297 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +0000298 return TPResult::Error;
Richard Smith22c7c412013-03-20 03:35:02 +0000299 } else if (Tok.is(tok::l_brace)) {
300 // A left-brace here is sufficient to disambiguate the parse; an
301 // expression can never be followed directly by a braced-init-list.
Richard Smithee390432014-05-16 01:56:53 +0000302 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000303 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000304 // MSVC and g++ won't examine the rest of declarators if '=' is
Guy Benyei11169dd2012-12-18 14:30:41 +0000305 // encountered; they just conclude that we have a declaration.
306 // EDG parses the initializer completely, which is the proper behavior
307 // for this case.
308 //
309 // At present, Clang follows MSVC and g++, since the parser does not have
310 // the ability to parse an expression fully without recording the
311 // results of that parse.
Richard Smith1fff95c2013-09-12 23:28:08 +0000312 // FIXME: Handle this case correctly.
313 //
314 // Also allow 'in' after an Objective-C declaration as in:
315 // for (int (^b)(void) in array). Ideally this should be done in the
Guy Benyei11169dd2012-12-18 14:30:41 +0000316 // context of parsing for-init-statement of a foreach statement only. But,
317 // in any other context 'in' is invalid after a declaration and parser
318 // issues the error regardless of outcome of this decision.
Richard Smith1fff95c2013-09-12 23:28:08 +0000319 // FIXME: Change if above assumption does not hold.
Richard Smithee390432014-05-16 01:56:53 +0000320 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000321 }
322
Alp Toker97650562014-01-10 11:19:30 +0000323 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000325 }
326
Richard Smithee390432014-05-16 01:56:53 +0000327 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000328}
329
330/// isCXXConditionDeclaration - Disambiguates between a declaration or an
331/// expression for a condition of a if/switch/while/for statement.
332/// If during the disambiguation process a parsing error is encountered,
333/// the function returns true to let the declaration parsing code handle it.
334///
335/// condition:
336/// expression
337/// type-specifier-seq declarator '=' assignment-expression
338/// [C++11] type-specifier-seq declarator '=' initializer-clause
339/// [C++11] type-specifier-seq declarator braced-init-list
340/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
341/// '=' assignment-expression
342///
343bool Parser::isCXXConditionDeclaration() {
344 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000345 if (TPR != TPResult::Ambiguous)
346 return TPR != TPResult::False; // Returns true for TPResult::True or
347 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000348
349 // FIXME: Add statistics about the number of ambiguous statements encountered
350 // and how they were resolved (number of declarations+number of expressions).
351
352 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
353 // We need tentative parsing...
354
355 TentativeParsingAction PA(*this);
356
357 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000358 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000359 assert(Tok.is(tok::l_paren) && "Expected '('");
360
361 // declarator
362 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
363
364 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000365 if (TPR == TPResult::Error)
366 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000367
Richard Smithee390432014-05-16 01:56:53 +0000368 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000369 // '='
370 // [GNU] simple-asm-expr[opt] attributes[opt]
371 if (Tok.is(tok::equal) ||
372 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +0000373 TPR = TPResult::True;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000374 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
Richard Smithee390432014-05-16 01:56:53 +0000375 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000376 else
Richard Smithee390432014-05-16 01:56:53 +0000377 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 }
379
380 PA.Revert();
381
Richard Smithee390432014-05-16 01:56:53 +0000382 assert(TPR == TPResult::True || TPR == TPResult::False);
383 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000384}
385
386 /// \brief Determine whether the next set of tokens contains a type-id.
387 ///
388 /// The context parameter states what context we're parsing right
389 /// now, which affects how this routine copes with the token
390 /// following the type-id. If the context is TypeIdInParens, we have
391 /// already parsed the '(' and we will cease lookahead when we hit
392 /// the corresponding ')'. If the context is
393 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
394 /// before this template argument, and will cease lookahead when we
395 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
396 /// and false for an expression. If during the disambiguation
397 /// process a parsing error is encountered, the function returns
398 /// true to let the declaration parsing code handle it.
399 ///
400 /// type-id:
401 /// type-specifier-seq abstract-declarator[opt]
402 ///
403bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
404
405 isAmbiguous = false;
406
407 // C++ 8.2p2:
408 // The ambiguity arising from the similarity between a function-style cast and
409 // a type-id can occur in different contexts. The ambiguity appears as a
410 // choice between a function-style cast expression and a declaration of a
411 // type. The resolution is that any construct that could possibly be a type-id
412 // in its syntactic context shall be considered a type-id.
413
414 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000415 if (TPR != TPResult::Ambiguous)
416 return TPR != TPResult::False; // Returns true for TPResult::True or
417 // TPResult::Error.
Guy Benyei11169dd2012-12-18 14:30:41 +0000418
419 // FIXME: Add statistics about the number of ambiguous statements encountered
420 // and how they were resolved (number of declarations+number of expressions).
421
422 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
423 // We need tentative parsing...
424
425 TentativeParsingAction PA(*this);
426
427 // type-specifier-seq
Richard Smith1fff95c2013-09-12 23:28:08 +0000428 TryConsumeDeclarationSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 assert(Tok.is(tok::l_paren) && "Expected '('");
430
431 // declarator
432 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
433
434 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +0000435 if (TPR == TPResult::Error)
436 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000437
Richard Smithee390432014-05-16 01:56:53 +0000438 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 // We are supposed to be inside parens, so if after the abstract declarator
440 // we encounter a ')' this is a type-id, otherwise it's an expression.
441 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Richard Smithee390432014-05-16 01:56:53 +0000442 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000443 isAmbiguous = true;
444
445 // We are supposed to be inside a template argument, so if after
446 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
447 // ',', this is a type-id. Otherwise, it's an expression.
448 } else if (Context == TypeIdAsTemplateArgument &&
449 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000450 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Richard Smithee390432014-05-16 01:56:53 +0000451 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000452 isAmbiguous = true;
453
454 } else
Richard Smithee390432014-05-16 01:56:53 +0000455 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000456 }
457
458 PA.Revert();
459
Richard Smithee390432014-05-16 01:56:53 +0000460 assert(TPR == TPResult::True || TPR == TPResult::False);
461 return TPR == TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462}
463
464/// \brief Returns true if this is a C++11 attribute-specifier. Per
465/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
466/// always introduce an attribute. In Objective-C++11, this rule does not
467/// apply if either '[' begins a message-send.
468///
469/// If Disambiguate is true, we try harder to determine whether a '[[' starts
470/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
471///
472/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
473/// Obj-C message send or the start of an attribute. Otherwise, we assume it
474/// is not an Obj-C message send.
475///
476/// C++11 [dcl.attr.grammar]:
477///
478/// attribute-specifier:
479/// '[' '[' attribute-list ']' ']'
480/// alignment-specifier
481///
482/// attribute-list:
483/// attribute[opt]
484/// attribute-list ',' attribute[opt]
485/// attribute '...'
486/// attribute-list ',' attribute '...'
487///
488/// attribute:
489/// attribute-token attribute-argument-clause[opt]
490///
491/// attribute-token:
492/// identifier
493/// identifier '::' identifier
494///
495/// attribute-argument-clause:
496/// '(' balanced-token-seq ')'
497Parser::CXX11AttributeKind
498Parser::isCXX11AttributeSpecifier(bool Disambiguate,
499 bool OuterMightBeMessageSend) {
500 if (Tok.is(tok::kw_alignas))
501 return CAK_AttributeSpecifier;
502
503 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
504 return CAK_NotAttributeSpecifier;
505
506 // No tentative parsing if we don't need to look for ']]' or a lambda.
507 if (!Disambiguate && !getLangOpts().ObjC1)
508 return CAK_AttributeSpecifier;
509
510 TentativeParsingAction PA(*this);
511
512 // Opening brackets were checked for above.
513 ConsumeBracket();
514
515 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
516 if (!getLangOpts().ObjC1) {
517 ConsumeBracket();
518
Alexey Bataevee6507d2013-11-18 08:17:37 +0000519 bool IsAttribute = SkipUntil(tok::r_square);
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 IsAttribute &= Tok.is(tok::r_square);
521
522 PA.Revert();
523
524 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
525 }
526
527 // In Obj-C++11, we need to distinguish four situations:
528 // 1a) int x[[attr]]; C++11 attribute.
529 // 1b) [[attr]]; C++11 statement attribute.
530 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
531 // 3a) int x[[obj get]]; Message send in array size/index.
532 // 3b) [[Class alloc] init]; Message send in message send.
533 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
534 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
535
536 // If we have a lambda-introducer, then this is definitely not a message send.
537 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
538 // into the tentative attribute parse below.
539 LambdaIntroducer Intro;
540 if (!TryParseLambdaIntroducer(Intro)) {
541 // A lambda cannot end with ']]', and an attribute must.
542 bool IsAttribute = Tok.is(tok::r_square);
543
544 PA.Revert();
545
546 if (IsAttribute)
547 // Case 1: C++11 attribute.
548 return CAK_AttributeSpecifier;
549
550 if (OuterMightBeMessageSend)
551 // Case 4: Lambda in message send.
552 return CAK_NotAttributeSpecifier;
553
554 // Case 2: Lambda in array size / index.
555 return CAK_InvalidAttributeSpecifier;
556 }
557
558 ConsumeBracket();
559
560 // If we don't have a lambda-introducer, then we have an attribute or a
561 // message-send.
562 bool IsAttribute = true;
563 while (Tok.isNot(tok::r_square)) {
564 if (Tok.is(tok::comma)) {
565 // Case 1: Stray commas can only occur in attributes.
566 PA.Revert();
567 return CAK_AttributeSpecifier;
568 }
569
570 // Parse the attribute-token, if present.
571 // C++11 [dcl.attr.grammar]:
572 // If a keyword or an alternative token that satisfies the syntactic
573 // requirements of an identifier is contained in an attribute-token,
574 // it is considered an identifier.
575 SourceLocation Loc;
576 if (!TryParseCXX11AttributeIdentifier(Loc)) {
577 IsAttribute = false;
578 break;
579 }
580 if (Tok.is(tok::coloncolon)) {
581 ConsumeToken();
582 if (!TryParseCXX11AttributeIdentifier(Loc)) {
583 IsAttribute = false;
584 break;
585 }
586 }
587
588 // Parse the attribute-argument-clause, if present.
589 if (Tok.is(tok::l_paren)) {
590 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000591 if (!SkipUntil(tok::r_paren)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 IsAttribute = false;
593 break;
594 }
595 }
596
Alp Toker97650562014-01-10 11:19:30 +0000597 TryConsumeToken(tok::ellipsis);
Guy Benyei11169dd2012-12-18 14:30:41 +0000598
Alp Toker97650562014-01-10 11:19:30 +0000599 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +0000600 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 }
602
603 // An attribute must end ']]'.
604 if (IsAttribute) {
605 if (Tok.is(tok::r_square)) {
606 ConsumeBracket();
607 IsAttribute = Tok.is(tok::r_square);
608 } else {
609 IsAttribute = false;
610 }
611 }
612
613 PA.Revert();
614
615 if (IsAttribute)
616 // Case 1: C++11 statement attribute.
617 return CAK_AttributeSpecifier;
618
619 // Case 3: Message send.
620 return CAK_NotAttributeSpecifier;
621}
622
Richard Smith1fff95c2013-09-12 23:28:08 +0000623Parser::TPResult Parser::TryParsePtrOperatorSeq() {
624 while (true) {
625 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
626 if (TryAnnotateCXXScopeToken(true))
Richard Smithee390432014-05-16 01:56:53 +0000627 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000628
629 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
630 Tok.is(tok::ampamp) ||
631 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
632 // ptr-operator
633 ConsumeToken();
634 while (Tok.is(tok::kw_const) ||
635 Tok.is(tok::kw_volatile) ||
636 Tok.is(tok::kw_restrict))
637 ConsumeToken();
638 } else {
Richard Smithee390432014-05-16 01:56:53 +0000639 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000640 }
641 }
642}
643
644/// operator-function-id:
645/// 'operator' operator
646///
647/// operator: one of
648/// new delete new[] delete[] + - * / % ^ [...]
649///
650/// conversion-function-id:
651/// 'operator' conversion-type-id
652///
653/// conversion-type-id:
654/// type-specifier-seq conversion-declarator[opt]
655///
656/// conversion-declarator:
657/// ptr-operator conversion-declarator[opt]
658///
659/// literal-operator-id:
660/// 'operator' string-literal identifier
661/// 'operator' user-defined-string-literal
662Parser::TPResult Parser::TryParseOperatorId() {
663 assert(Tok.is(tok::kw_operator));
664 ConsumeToken();
665
666 // Maybe this is an operator-function-id.
667 switch (Tok.getKind()) {
668 case tok::kw_new: case tok::kw_delete:
669 ConsumeToken();
670 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
671 ConsumeBracket();
672 ConsumeBracket();
673 }
Richard Smithee390432014-05-16 01:56:53 +0000674 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000675
676#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
677 case tok::Token:
678#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
679#include "clang/Basic/OperatorKinds.def"
680 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +0000681 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000682
683 case tok::l_square:
684 if (NextToken().is(tok::r_square)) {
685 ConsumeBracket();
686 ConsumeBracket();
Richard Smithee390432014-05-16 01:56:53 +0000687 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000688 }
689 break;
690
691 case tok::l_paren:
692 if (NextToken().is(tok::r_paren)) {
693 ConsumeParen();
694 ConsumeParen();
Richard Smithee390432014-05-16 01:56:53 +0000695 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000696 }
697 break;
698
699 default:
700 break;
701 }
702
703 // Maybe this is a literal-operator-id.
704 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
705 bool FoundUDSuffix = false;
706 do {
707 FoundUDSuffix |= Tok.hasUDSuffix();
708 ConsumeStringToken();
709 } while (isTokenStringLiteral());
710
711 if (!FoundUDSuffix) {
712 if (Tok.is(tok::identifier))
713 ConsumeToken();
714 else
Richard Smithee390432014-05-16 01:56:53 +0000715 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000716 }
Richard Smithee390432014-05-16 01:56:53 +0000717 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +0000718 }
719
720 // Maybe this is a conversion-function-id.
721 bool AnyDeclSpecifiers = false;
722 while (true) {
723 TPResult TPR = isCXXDeclarationSpecifier();
Richard Smithee390432014-05-16 01:56:53 +0000724 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +0000725 return TPR;
Richard Smithee390432014-05-16 01:56:53 +0000726 if (TPR == TPResult::False) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000727 if (!AnyDeclSpecifiers)
Richard Smithee390432014-05-16 01:56:53 +0000728 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000729 break;
730 }
Richard Smithee390432014-05-16 01:56:53 +0000731 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
732 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000733 AnyDeclSpecifiers = true;
734 }
735 return TryParsePtrOperatorSeq();
736}
737
Guy Benyei11169dd2012-12-18 14:30:41 +0000738/// declarator:
739/// direct-declarator
740/// ptr-operator declarator
741///
742/// direct-declarator:
743/// declarator-id
744/// direct-declarator '(' parameter-declaration-clause ')'
745/// cv-qualifier-seq[opt] exception-specification[opt]
746/// direct-declarator '[' constant-expression[opt] ']'
747/// '(' declarator ')'
748/// [GNU] '(' attributes declarator ')'
749///
750/// abstract-declarator:
751/// ptr-operator abstract-declarator[opt]
752/// direct-abstract-declarator
753/// ...
754///
755/// direct-abstract-declarator:
756/// direct-abstract-declarator[opt]
757/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
758/// exception-specification[opt]
759/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
760/// '(' abstract-declarator ')'
761///
762/// ptr-operator:
763/// '*' cv-qualifier-seq[opt]
764/// '&'
765/// [C++0x] '&&' [TODO]
766/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
767///
768/// cv-qualifier-seq:
769/// cv-qualifier cv-qualifier-seq[opt]
770///
771/// cv-qualifier:
772/// 'const'
773/// 'volatile'
774///
775/// declarator-id:
776/// '...'[opt] id-expression
777///
778/// id-expression:
779/// unqualified-id
780/// qualified-id [TODO]
781///
782/// unqualified-id:
783/// identifier
Richard Smith1fff95c2013-09-12 23:28:08 +0000784/// operator-function-id
785/// conversion-function-id
786/// literal-operator-id
Guy Benyei11169dd2012-12-18 14:30:41 +0000787/// '~' class-name [TODO]
Richard Smith1fff95c2013-09-12 23:28:08 +0000788/// '~' decltype-specifier [TODO]
Guy Benyei11169dd2012-12-18 14:30:41 +0000789/// template-id [TODO]
790///
791Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
792 bool mayHaveIdentifier) {
793 // declarator:
794 // direct-declarator
795 // ptr-operator declarator
Richard Smithee390432014-05-16 01:56:53 +0000796 if (TryParsePtrOperatorSeq() == TPResult::Error)
797 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +0000798
799 // direct-declarator:
800 // direct-abstract-declarator:
801 if (Tok.is(tok::ellipsis))
802 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000803
804 if ((Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
805 (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
806 NextToken().is(tok::kw_operator)))) &&
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 mayHaveIdentifier) {
808 // declarator-id
809 if (Tok.is(tok::annot_cxxscope))
810 ConsumeToken();
Richard Smith1fff95c2013-09-12 23:28:08 +0000811 else if (Tok.is(tok::identifier))
Guy Benyei11169dd2012-12-18 14:30:41 +0000812 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Richard Smith1fff95c2013-09-12 23:28:08 +0000813 if (Tok.is(tok::kw_operator)) {
Richard Smithee390432014-05-16 01:56:53 +0000814 if (TryParseOperatorId() == TPResult::Error)
815 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +0000816 } else
817 ConsumeToken();
Guy Benyei11169dd2012-12-18 14:30:41 +0000818 } else if (Tok.is(tok::l_paren)) {
819 ConsumeParen();
820 if (mayBeAbstract &&
821 (Tok.is(tok::r_paren) || // 'int()' is a function.
822 // 'int(...)' is a function.
823 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
824 isDeclarationSpecifier())) { // 'int(int)' is a function.
825 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
826 // exception-specification[opt]
827 TPResult TPR = TryParseFunctionDeclarator();
Richard Smithee390432014-05-16 01:56:53 +0000828 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000829 return TPR;
830 } else {
831 // '(' declarator ')'
832 // '(' attributes declarator ')'
833 // '(' abstract-declarator ')'
834 if (Tok.is(tok::kw___attribute) ||
835 Tok.is(tok::kw___declspec) ||
836 Tok.is(tok::kw___cdecl) ||
837 Tok.is(tok::kw___stdcall) ||
838 Tok.is(tok::kw___fastcall) ||
839 Tok.is(tok::kw___thiscall) ||
840 Tok.is(tok::kw___unaligned))
Richard Smithee390432014-05-16 01:56:53 +0000841 return TPResult::True; // attributes indicate declaration
Guy Benyei11169dd2012-12-18 14:30:41 +0000842 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Richard Smithee390432014-05-16 01:56:53 +0000843 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000844 return TPR;
845 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +0000846 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000847 ConsumeParen();
848 }
849 } else if (!mayBeAbstract) {
Richard Smithee390432014-05-16 01:56:53 +0000850 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000851 }
852
853 while (1) {
Richard Smithee390432014-05-16 01:56:53 +0000854 TPResult TPR(TPResult::Ambiguous);
Guy Benyei11169dd2012-12-18 14:30:41 +0000855
856 // abstract-declarator: ...
857 if (Tok.is(tok::ellipsis))
858 ConsumeToken();
859
860 if (Tok.is(tok::l_paren)) {
861 // Check whether we have a function declarator or a possible ctor-style
862 // initializer that follows the declarator. Note that ctor-style
863 // initializers are not possible in contexts where abstract declarators
864 // are allowed.
865 if (!mayBeAbstract && !isCXXFunctionDeclarator())
866 break;
867
868 // direct-declarator '(' parameter-declaration-clause ')'
869 // cv-qualifier-seq[opt] exception-specification[opt]
870 ConsumeParen();
871 TPR = TryParseFunctionDeclarator();
872 } else if (Tok.is(tok::l_square)) {
873 // direct-declarator '[' constant-expression[opt] ']'
874 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
875 TPR = TryParseBracketDeclarator();
876 } else {
877 break;
878 }
879
Richard Smithee390432014-05-16 01:56:53 +0000880 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 return TPR;
882 }
883
Richard Smithee390432014-05-16 01:56:53 +0000884 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000885}
886
887Parser::TPResult
888Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
889 switch (Kind) {
890 // Obviously starts an expression.
891 case tok::numeric_constant:
892 case tok::char_constant:
893 case tok::wide_char_constant:
894 case tok::utf16_char_constant:
895 case tok::utf32_char_constant:
896 case tok::string_literal:
897 case tok::wide_string_literal:
898 case tok::utf8_string_literal:
899 case tok::utf16_string_literal:
900 case tok::utf32_string_literal:
901 case tok::l_square:
902 case tok::l_paren:
903 case tok::amp:
904 case tok::ampamp:
905 case tok::star:
906 case tok::plus:
907 case tok::plusplus:
908 case tok::minus:
909 case tok::minusminus:
910 case tok::tilde:
911 case tok::exclaim:
912 case tok::kw_sizeof:
913 case tok::kw___func__:
914 case tok::kw_const_cast:
915 case tok::kw_delete:
916 case tok::kw_dynamic_cast:
917 case tok::kw_false:
918 case tok::kw_new:
919 case tok::kw_operator:
920 case tok::kw_reinterpret_cast:
921 case tok::kw_static_cast:
922 case tok::kw_this:
923 case tok::kw_throw:
924 case tok::kw_true:
925 case tok::kw_typeid:
926 case tok::kw_alignof:
927 case tok::kw_noexcept:
928 case tok::kw_nullptr:
929 case tok::kw__Alignof:
930 case tok::kw___null:
931 case tok::kw___alignof:
932 case tok::kw___builtin_choose_expr:
933 case tok::kw___builtin_offsetof:
Guy Benyei11169dd2012-12-18 14:30:41 +0000934 case tok::kw___builtin_va_arg:
935 case tok::kw___imag:
936 case tok::kw___real:
937 case tok::kw___FUNCTION__:
David Majnemerbed356a2013-11-06 23:31:56 +0000938 case tok::kw___FUNCDNAME__:
Reid Kleckner52eddda2014-04-08 18:13:24 +0000939 case tok::kw___FUNCSIG__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000940 case tok::kw_L__FUNCTION__:
941 case tok::kw___PRETTY_FUNCTION__:
Guy Benyei11169dd2012-12-18 14:30:41 +0000942 case tok::kw___uuidof:
Alp Toker40f9b1c2013-12-12 21:23:03 +0000943#define TYPE_TRAIT(N,Spelling,K) \
944 case tok::kw_##Spelling:
945#include "clang/Basic/TokenKinds.def"
Richard Smithee390432014-05-16 01:56:53 +0000946 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +0000947
948 // Obviously starts a type-specifier-seq:
949 case tok::kw_char:
950 case tok::kw_const:
951 case tok::kw_double:
952 case tok::kw_enum:
953 case tok::kw_half:
954 case tok::kw_float:
955 case tok::kw_int:
956 case tok::kw_long:
957 case tok::kw___int64:
958 case tok::kw___int128:
959 case tok::kw_restrict:
960 case tok::kw_short:
961 case tok::kw_signed:
962 case tok::kw_struct:
963 case tok::kw_union:
964 case tok::kw_unsigned:
965 case tok::kw_void:
966 case tok::kw_volatile:
967 case tok::kw__Bool:
968 case tok::kw__Complex:
969 case tok::kw_class:
970 case tok::kw_typename:
971 case tok::kw_wchar_t:
972 case tok::kw_char16_t:
973 case tok::kw_char32_t:
Guy Benyei11169dd2012-12-18 14:30:41 +0000974 case tok::kw__Decimal32:
975 case tok::kw__Decimal64:
976 case tok::kw__Decimal128:
Richard Smith1fff95c2013-09-12 23:28:08 +0000977 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +0000979 case tok::kw_thread_local:
980 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +0000981 case tok::kw_typeof:
Richard Smith1fff95c2013-09-12 23:28:08 +0000982 case tok::kw___underlying_type:
Guy Benyei11169dd2012-12-18 14:30:41 +0000983 case tok::kw___cdecl:
984 case tok::kw___stdcall:
985 case tok::kw___fastcall:
986 case tok::kw___thiscall:
987 case tok::kw___unaligned:
988 case tok::kw___vector:
989 case tok::kw___pixel:
990 case tok::kw__Atomic:
991 case tok::kw___unknown_anytype:
Richard Smithee390432014-05-16 01:56:53 +0000992 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +0000993
994 default:
995 break;
996 }
997
Richard Smithee390432014-05-16 01:56:53 +0000998 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +0000999}
1000
1001bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1002 return std::find(TentativelyDeclaredIdentifiers.begin(),
1003 TentativelyDeclaredIdentifiers.end(), II)
1004 != TentativelyDeclaredIdentifiers.end();
1005}
1006
Richard Smithee390432014-05-16 01:56:53 +00001007/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1008/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1009/// be either a decl-specifier or a function-style cast, and TPResult::Error
Guy Benyei11169dd2012-12-18 14:30:41 +00001010/// if a parsing error was found and reported.
1011///
1012/// If HasMissingTypename is provided, a name with a dependent scope specifier
1013/// will be treated as ambiguous if the 'typename' keyword is missing. If this
1014/// happens, *HasMissingTypename will be set to 'true'. This will also be used
1015/// as an indicator that undeclared identifiers (which will trigger a later
Richard Smithee390432014-05-16 01:56:53 +00001016/// parse error) should be treated as types. Returns TPResult::Ambiguous in
Guy Benyei11169dd2012-12-18 14:30:41 +00001017/// such cases.
1018///
1019/// decl-specifier:
1020/// storage-class-specifier
1021/// type-specifier
1022/// function-specifier
1023/// 'friend'
1024/// 'typedef'
Richard Smithb4a9e862013-04-12 22:46:28 +00001025/// [C++11] 'constexpr'
Guy Benyei11169dd2012-12-18 14:30:41 +00001026/// [GNU] attributes declaration-specifiers[opt]
1027///
1028/// storage-class-specifier:
1029/// 'register'
1030/// 'static'
1031/// 'extern'
1032/// 'mutable'
1033/// 'auto'
1034/// [GNU] '__thread'
Richard Smithb4a9e862013-04-12 22:46:28 +00001035/// [C++11] 'thread_local'
1036/// [C11] '_Thread_local'
Guy Benyei11169dd2012-12-18 14:30:41 +00001037///
1038/// function-specifier:
1039/// 'inline'
1040/// 'virtual'
1041/// 'explicit'
1042///
1043/// typedef-name:
1044/// identifier
1045///
1046/// type-specifier:
1047/// simple-type-specifier
1048/// class-specifier
1049/// enum-specifier
1050/// elaborated-type-specifier
1051/// typename-specifier
1052/// cv-qualifier
1053///
1054/// simple-type-specifier:
1055/// '::'[opt] nested-name-specifier[opt] type-name
1056/// '::'[opt] nested-name-specifier 'template'
1057/// simple-template-id [TODO]
1058/// 'char'
1059/// 'wchar_t'
1060/// 'bool'
1061/// 'short'
1062/// 'int'
1063/// 'long'
1064/// 'signed'
1065/// 'unsigned'
1066/// 'float'
1067/// 'double'
1068/// 'void'
1069/// [GNU] typeof-specifier
1070/// [GNU] '_Complex'
Richard Smithb4a9e862013-04-12 22:46:28 +00001071/// [C++11] 'auto'
1072/// [C++11] 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +00001073/// [C++1y] 'decltype' ( 'auto' )
Guy Benyei11169dd2012-12-18 14:30:41 +00001074///
1075/// type-name:
1076/// class-name
1077/// enum-name
1078/// typedef-name
1079///
1080/// elaborated-type-specifier:
1081/// class-key '::'[opt] nested-name-specifier[opt] identifier
1082/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1083/// simple-template-id
1084/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1085///
1086/// enum-name:
1087/// identifier
1088///
1089/// enum-specifier:
1090/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1091/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1092///
1093/// class-specifier:
1094/// class-head '{' member-specification[opt] '}'
1095///
1096/// class-head:
1097/// class-key identifier[opt] base-clause[opt]
1098/// class-key nested-name-specifier identifier base-clause[opt]
1099/// class-key nested-name-specifier[opt] simple-template-id
1100/// base-clause[opt]
1101///
1102/// class-key:
1103/// 'class'
1104/// 'struct'
1105/// 'union'
1106///
1107/// cv-qualifier:
1108/// 'const'
1109/// 'volatile'
1110/// [GNU] restrict
1111///
1112Parser::TPResult
1113Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1114 bool *HasMissingTypename) {
1115 switch (Tok.getKind()) {
1116 case tok::identifier: {
1117 // Check for need to substitute AltiVec __vector keyword
1118 // for "vector" identifier.
1119 if (TryAltiVecVectorToken())
Richard Smithee390432014-05-16 01:56:53 +00001120 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001121
1122 const Token &Next = NextToken();
1123 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1124 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001125 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
1127 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1128 // Determine whether this is a valid expression. If not, we will hit
1129 // a parse error one way or another. In that case, tell the caller that
1130 // this is ambiguous. Typo-correct to type and expression keywords and
1131 // to types and identifiers, in order to try to recover from errors.
1132 CorrectionCandidateCallback TypoCorrection;
1133 TypoCorrection.WantRemainingKeywords = false;
Kaelyn Takata2f448462014-10-14 21:57:21 +00001134 TypoCorrection.WantTypeSpecifiers =
1135 Next.is(tok::l_paren) || Next.is(tok::r_paren) ||
1136 Next.is(tok::greater) || Next.is(tok::l_brace) ||
1137 Next.is(tok::identifier);
Guy Benyei11169dd2012-12-18 14:30:41 +00001138 switch (TryAnnotateName(false /* no nested name specifier */,
1139 &TypoCorrection)) {
1140 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001141 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001142 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001143 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 case ANK_TemplateName:
1145 // A bare type template-name which can't be a template template
1146 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001147 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001149 return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 case ANK_Success:
1151 break;
1152 }
1153 assert(Tok.isNot(tok::identifier) &&
1154 "TryAnnotateName succeeded without producing an annotation");
1155 } else {
1156 // This might possibly be a type with a dependent scope specifier and
1157 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1158 // since it will annotate as a primary expression, and we want to use the
1159 // "missing 'typename'" logic.
1160 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001161 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001162 // If annotation failed, assume it's a non-type.
1163 // FIXME: If this happens due to an undeclared identifier, treat it as
1164 // ambiguous.
1165 if (Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001166 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001167 }
1168
1169 // We annotated this token as something. Recurse to handle whatever we got.
1170 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1171 }
1172
1173 case tok::kw_typename: // typename T::type
1174 // Annotate typenames and C++ scope specifiers. If we get one, just
1175 // recurse to handle whatever we get.
1176 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001177 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001178 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1179
1180 case tok::coloncolon: { // ::foo::bar
1181 const Token &Next = NextToken();
1182 if (Next.is(tok::kw_new) || // ::new
1183 Next.is(tok::kw_delete)) // ::delete
Richard Smithee390432014-05-16 01:56:53 +00001184 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001185 }
1186 // Fall through.
Nikola Smiljanic67860242014-09-26 00:28:20 +00001187 case tok::kw___super:
Guy Benyei11169dd2012-12-18 14:30:41 +00001188 case tok::kw_decltype:
1189 // Annotate typenames and C++ scope specifiers. If we get one, just
1190 // recurse to handle whatever we get.
1191 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001192 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1194
1195 // decl-specifier:
1196 // storage-class-specifier
1197 // type-specifier
1198 // function-specifier
1199 // 'friend'
1200 // 'typedef'
1201 // 'constexpr'
1202 case tok::kw_friend:
1203 case tok::kw_typedef:
1204 case tok::kw_constexpr:
1205 // storage-class-specifier
1206 case tok::kw_register:
1207 case tok::kw_static:
1208 case tok::kw_extern:
1209 case tok::kw_mutable:
1210 case tok::kw_auto:
1211 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00001212 case tok::kw_thread_local:
1213 case tok::kw__Thread_local:
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 // function-specifier
1215 case tok::kw_inline:
1216 case tok::kw_virtual:
1217 case tok::kw_explicit:
1218
1219 // Modules
1220 case tok::kw___module_private__:
1221
1222 // Debugger support
1223 case tok::kw___unknown_anytype:
1224
1225 // type-specifier:
1226 // simple-type-specifier
1227 // class-specifier
1228 // enum-specifier
1229 // elaborated-type-specifier
1230 // typename-specifier
1231 // cv-qualifier
1232
1233 // class-specifier
1234 // elaborated-type-specifier
1235 case tok::kw_class:
1236 case tok::kw_struct:
1237 case tok::kw_union:
Richard Smith1fff95c2013-09-12 23:28:08 +00001238 case tok::kw___interface:
Guy Benyei11169dd2012-12-18 14:30:41 +00001239 // enum-specifier
1240 case tok::kw_enum:
1241 // cv-qualifier
1242 case tok::kw_const:
1243 case tok::kw_volatile:
1244
1245 // GNU
1246 case tok::kw_restrict:
1247 case tok::kw__Complex:
1248 case tok::kw___attribute:
Richard Smithee390432014-05-16 01:56:53 +00001249 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001250
1251 // Microsoft
1252 case tok::kw___declspec:
1253 case tok::kw___cdecl:
1254 case tok::kw___stdcall:
1255 case tok::kw___fastcall:
1256 case tok::kw___thiscall:
1257 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00001258 case tok::kw___sptr:
1259 case tok::kw___uptr:
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 case tok::kw___ptr64:
1261 case tok::kw___ptr32:
1262 case tok::kw___forceinline:
1263 case tok::kw___unaligned:
Richard Smithee390432014-05-16 01:56:53 +00001264 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001265
1266 // Borland
1267 case tok::kw___pascal:
Richard Smithee390432014-05-16 01:56:53 +00001268 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001269
1270 // AltiVec
1271 case tok::kw___vector:
Richard Smithee390432014-05-16 01:56:53 +00001272 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001273
1274 case tok::annot_template_id: {
1275 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1276 if (TemplateId->Kind != TNK_Type_template)
Richard Smithee390432014-05-16 01:56:53 +00001277 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001278 CXXScopeSpec SS;
1279 AnnotateTemplateIdTokenAsType();
1280 assert(Tok.is(tok::annot_typename));
1281 goto case_typename;
1282 }
1283
1284 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1285 // We've already annotated a scope; try to annotate a type.
1286 if (TryAnnotateTypeOrScopeToken())
Richard Smithee390432014-05-16 01:56:53 +00001287 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001288 if (!Tok.is(tok::annot_typename)) {
1289 // If the next token is an identifier or a type qualifier, then this
1290 // can't possibly be a valid expression either.
1291 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1292 CXXScopeSpec SS;
1293 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1294 Tok.getAnnotationRange(),
1295 SS);
1296 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1297 TentativeParsingAction PA(*this);
1298 ConsumeToken();
1299 ConsumeToken();
1300 bool isIdentifier = Tok.is(tok::identifier);
Richard Smithee390432014-05-16 01:56:53 +00001301 TPResult TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001302 if (!isIdentifier)
1303 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1304 HasMissingTypename);
1305 PA.Revert();
1306
1307 if (isIdentifier ||
Richard Smithee390432014-05-16 01:56:53 +00001308 TPR == TPResult::True || TPR == TPResult::Error)
1309 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001310
1311 if (HasMissingTypename) {
1312 // We can't tell whether this is a missing 'typename' or a valid
1313 // expression.
1314 *HasMissingTypename = true;
Richard Smithee390432014-05-16 01:56:53 +00001315 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 }
1317 } else {
1318 // Try to resolve the name. If it doesn't exist, assume it was
1319 // intended to name a type and keep disambiguating.
1320 switch (TryAnnotateName(false /* SS is not dependent */)) {
1321 case ANK_Error:
Richard Smithee390432014-05-16 01:56:53 +00001322 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001323 case ANK_TentativeDecl:
Richard Smithee390432014-05-16 01:56:53 +00001324 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001325 case ANK_TemplateName:
1326 // A bare type template-name which can't be a template template
1327 // argument is an error, and was probably intended to be a type.
Richard Smithee390432014-05-16 01:56:53 +00001328 return GreaterThanIsOperator ? TPResult::True : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001329 case ANK_Unresolved:
Richard Smithee390432014-05-16 01:56:53 +00001330 return HasMissingTypename ? TPResult::Ambiguous
1331 : TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332 case ANK_Success:
1333 // Annotated it, check again.
1334 assert(Tok.isNot(tok::annot_cxxscope) ||
1335 NextToken().isNot(tok::identifier));
1336 return isCXXDeclarationSpecifier(BracedCastResult,
1337 HasMissingTypename);
1338 }
1339 }
1340 }
Richard Smithee390432014-05-16 01:56:53 +00001341 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001342 }
1343 // If that succeeded, fallthrough into the generic simple-type-id case.
1344
1345 // The ambiguity resides in a simple-type-specifier/typename-specifier
1346 // followed by a '('. The '(' could either be the start of:
1347 //
1348 // direct-declarator:
1349 // '(' declarator ')'
1350 //
1351 // direct-abstract-declarator:
1352 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1353 // exception-specification[opt]
1354 // '(' abstract-declarator ')'
1355 //
1356 // or part of a function-style cast expression:
1357 //
1358 // simple-type-specifier '(' expression-list[opt] ')'
1359 //
1360
1361 // simple-type-specifier:
1362
1363 case tok::annot_typename:
1364 case_typename:
1365 // In Objective-C, we might have a protocol-qualified type.
1366 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1367 // Tentatively parse the
1368 TentativeParsingAction PA(*this);
1369 ConsumeToken(); // The type token
1370
1371 TPResult TPR = TryParseProtocolQualifiers();
1372 bool isFollowedByParen = Tok.is(tok::l_paren);
1373 bool isFollowedByBrace = Tok.is(tok::l_brace);
1374
1375 PA.Revert();
1376
Richard Smithee390432014-05-16 01:56:53 +00001377 if (TPR == TPResult::Error)
1378 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001379
1380 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001381 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001382
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001383 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001384 return BracedCastResult;
1385
Richard Smithee390432014-05-16 01:56:53 +00001386 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 }
1388
1389 case tok::kw_char:
1390 case tok::kw_wchar_t:
1391 case tok::kw_char16_t:
1392 case tok::kw_char32_t:
1393 case tok::kw_bool:
1394 case tok::kw_short:
1395 case tok::kw_int:
1396 case tok::kw_long:
1397 case tok::kw___int64:
1398 case tok::kw___int128:
1399 case tok::kw_signed:
1400 case tok::kw_unsigned:
1401 case tok::kw_half:
1402 case tok::kw_float:
1403 case tok::kw_double:
1404 case tok::kw_void:
1405 case tok::annot_decltype:
1406 if (NextToken().is(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001407 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001408
1409 // This is a function-style cast in all cases we disambiguate other than
1410 // one:
1411 // struct S {
1412 // enum E : int { a = 4 }; // enum
1413 // enum E : int { 4 }; // bit-field
1414 // };
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001415 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei11169dd2012-12-18 14:30:41 +00001416 return BracedCastResult;
1417
1418 if (isStartOfObjCClassMessageMissingOpenBracket())
Richard Smithee390432014-05-16 01:56:53 +00001419 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001420
Richard Smithee390432014-05-16 01:56:53 +00001421 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001422
1423 // GNU typeof support.
1424 case tok::kw_typeof: {
1425 if (NextToken().isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001426 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001427
1428 TentativeParsingAction PA(*this);
1429
1430 TPResult TPR = TryParseTypeofSpecifier();
1431 bool isFollowedByParen = Tok.is(tok::l_paren);
1432 bool isFollowedByBrace = Tok.is(tok::l_brace);
1433
1434 PA.Revert();
1435
Richard Smithee390432014-05-16 01:56:53 +00001436 if (TPR == TPResult::Error)
1437 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001438
1439 if (isFollowedByParen)
Richard Smithee390432014-05-16 01:56:53 +00001440 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001441
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001442 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 return BracedCastResult;
1444
Richard Smithee390432014-05-16 01:56:53 +00001445 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001446 }
1447
1448 // C++0x type traits support
1449 case tok::kw___underlying_type:
Richard Smithee390432014-05-16 01:56:53 +00001450 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001451
1452 // C11 _Atomic
1453 case tok::kw__Atomic:
Richard Smithee390432014-05-16 01:56:53 +00001454 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001455
1456 default:
Richard Smithee390432014-05-16 01:56:53 +00001457 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001458 }
1459}
1460
Richard Smith1fff95c2013-09-12 23:28:08 +00001461bool Parser::isCXXDeclarationSpecifierAType() {
1462 switch (Tok.getKind()) {
1463 // typename-specifier
1464 case tok::annot_decltype:
1465 case tok::annot_template_id:
1466 case tok::annot_typename:
1467 case tok::kw_typeof:
1468 case tok::kw___underlying_type:
1469 return true;
1470
1471 // elaborated-type-specifier
1472 case tok::kw_class:
1473 case tok::kw_struct:
1474 case tok::kw_union:
1475 case tok::kw___interface:
1476 case tok::kw_enum:
1477 return true;
1478
1479 // simple-type-specifier
1480 case tok::kw_char:
1481 case tok::kw_wchar_t:
1482 case tok::kw_char16_t:
1483 case tok::kw_char32_t:
1484 case tok::kw_bool:
1485 case tok::kw_short:
1486 case tok::kw_int:
1487 case tok::kw_long:
1488 case tok::kw___int64:
1489 case tok::kw___int128:
1490 case tok::kw_signed:
1491 case tok::kw_unsigned:
1492 case tok::kw_half:
1493 case tok::kw_float:
1494 case tok::kw_double:
1495 case tok::kw_void:
1496 case tok::kw___unknown_anytype:
1497 return true;
1498
1499 case tok::kw_auto:
1500 return getLangOpts().CPlusPlus11;
1501
1502 case tok::kw__Atomic:
1503 // "_Atomic foo"
1504 return NextToken().is(tok::l_paren);
1505
1506 default:
1507 return false;
1508 }
1509}
1510
Guy Benyei11169dd2012-12-18 14:30:41 +00001511/// [GNU] typeof-specifier:
1512/// 'typeof' '(' expressions ')'
1513/// 'typeof' '(' type-name ')'
1514///
1515Parser::TPResult Parser::TryParseTypeofSpecifier() {
1516 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1517 ConsumeToken();
1518
1519 assert(Tok.is(tok::l_paren) && "Expected '('");
1520 // Parse through the parens after 'typeof'.
1521 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001522 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001523 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001524
Richard Smithee390432014-05-16 01:56:53 +00001525 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001526}
1527
1528/// [ObjC] protocol-qualifiers:
1529//// '<' identifier-list '>'
1530Parser::TPResult Parser::TryParseProtocolQualifiers() {
1531 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1532 ConsumeToken();
1533 do {
1534 if (Tok.isNot(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001535 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001536 ConsumeToken();
1537
1538 if (Tok.is(tok::comma)) {
1539 ConsumeToken();
1540 continue;
1541 }
1542
1543 if (Tok.is(tok::greater)) {
1544 ConsumeToken();
Richard Smithee390432014-05-16 01:56:53 +00001545 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 }
1547 } while (false);
1548
Richard Smithee390432014-05-16 01:56:53 +00001549 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001550}
1551
Guy Benyei11169dd2012-12-18 14:30:41 +00001552/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1553/// a constructor-style initializer, when parsing declaration statements.
1554/// Returns true for function declarator and false for constructor-style
1555/// initializer.
1556/// If during the disambiguation process a parsing error is encountered,
1557/// the function returns true to let the declaration parsing code handle it.
1558///
1559/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1560/// exception-specification[opt]
1561///
1562bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1563
1564 // C++ 8.2p1:
1565 // The ambiguity arising from the similarity between a function-style cast and
1566 // a declaration mentioned in 6.8 can also occur in the context of a
1567 // declaration. In that context, the choice is between a function declaration
1568 // with a redundant set of parentheses around a parameter name and an object
1569 // declaration with a function-style cast as the initializer. Just as for the
1570 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1571 // that could possibly be a declaration a declaration.
1572
1573 TentativeParsingAction PA(*this);
1574
1575 ConsumeParen();
1576 bool InvalidAsDeclaration = false;
1577 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001578 if (TPR == TPResult::Ambiguous) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 if (Tok.isNot(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001580 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001581 else {
1582 const Token &Next = NextToken();
1583 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1584 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1585 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001586 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
Guy Benyei11169dd2012-12-18 14:30:41 +00001587 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1588 Next.is(tok::equal) || Next.is(tok::arrow))
1589 // The next token cannot appear after a constructor-style initializer,
1590 // and can appear next in a function definition. This must be a function
1591 // declarator.
Richard Smithee390432014-05-16 01:56:53 +00001592 TPR = TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001593 else if (InvalidAsDeclaration)
1594 // Use the absence of 'typename' as a tie-breaker.
Richard Smithee390432014-05-16 01:56:53 +00001595 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 }
1597 }
1598
1599 PA.Revert();
1600
Richard Smithee390432014-05-16 01:56:53 +00001601 if (IsAmbiguous && TPR == TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001602 *IsAmbiguous = true;
1603
1604 // In case of an error, let the declaration parsing code handle it.
Richard Smithee390432014-05-16 01:56:53 +00001605 return TPR != TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001606}
1607
1608/// parameter-declaration-clause:
1609/// parameter-declaration-list[opt] '...'[opt]
1610/// parameter-declaration-list ',' '...'
1611///
1612/// parameter-declaration-list:
1613/// parameter-declaration
1614/// parameter-declaration-list ',' parameter-declaration
1615///
1616/// parameter-declaration:
1617/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1618/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1619/// '=' assignment-expression
1620/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1621/// attributes[opt]
1622/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1623/// attributes[opt] '=' assignment-expression
1624///
1625Parser::TPResult
Richard Smith1fff95c2013-09-12 23:28:08 +00001626Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1627 bool VersusTemplateArgument) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001628
1629 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001630 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001631
1632 // parameter-declaration-list[opt] '...'[opt]
1633 // parameter-declaration-list ',' '...'
1634 //
1635 // parameter-declaration-list:
1636 // parameter-declaration
1637 // parameter-declaration-list ',' parameter-declaration
1638 //
1639 while (1) {
1640 // '...'[opt]
1641 if (Tok.is(tok::ellipsis)) {
1642 ConsumeToken();
1643 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001644 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 else
Richard Smithee390432014-05-16 01:56:53 +00001646 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 }
1648
1649 // An attribute-specifier-seq here is a sign of a function declarator.
1650 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1651 /*OuterMightBeMessageSend*/true))
Richard Smithee390432014-05-16 01:56:53 +00001652 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001653
1654 ParsedAttributes attrs(AttrFactory);
1655 MaybeParseMicrosoftAttributes(attrs);
1656
1657 // decl-specifier-seq
1658 // A parameter-declaration's initializer must be preceded by an '=', so
1659 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smithee390432014-05-16 01:56:53 +00001660 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001661 InvalidAsDeclaration);
1662
Richard Smithee390432014-05-16 01:56:53 +00001663 if (VersusTemplateArgument && TPR == TPResult::True) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001664 // Consume the decl-specifier-seq. We have to look past it, since a
1665 // type-id might appear here in a template argument.
1666 bool SeenType = false;
1667 do {
1668 SeenType |= isCXXDeclarationSpecifierAType();
Richard Smithee390432014-05-16 01:56:53 +00001669 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1670 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001671
1672 // If we see a parameter name, this can't be a template argument.
1673 if (SeenType && Tok.is(tok::identifier))
Richard Smithee390432014-05-16 01:56:53 +00001674 return TPResult::True;
Richard Smith1fff95c2013-09-12 23:28:08 +00001675
Richard Smithee390432014-05-16 01:56:53 +00001676 TPR = isCXXDeclarationSpecifier(TPResult::False,
Richard Smith1fff95c2013-09-12 23:28:08 +00001677 InvalidAsDeclaration);
Richard Smithee390432014-05-16 01:56:53 +00001678 if (TPR == TPResult::Error)
Richard Smith1fff95c2013-09-12 23:28:08 +00001679 return TPR;
Richard Smithee390432014-05-16 01:56:53 +00001680 } while (TPR != TPResult::False);
1681 } else if (TPR == TPResult::Ambiguous) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001682 // Disambiguate what follows the decl-specifier.
Richard Smithee390432014-05-16 01:56:53 +00001683 if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1684 return TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001685 } else
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 return TPR;
1687
1688 // declarator
1689 // abstract-declarator[opt]
1690 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Richard Smithee390432014-05-16 01:56:53 +00001691 if (TPR != TPResult::Ambiguous)
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 return TPR;
1693
1694 // [GNU] attributes[opt]
1695 if (Tok.is(tok::kw___attribute))
Richard Smithee390432014-05-16 01:56:53 +00001696 return TPResult::True;
Guy Benyei11169dd2012-12-18 14:30:41 +00001697
Richard Smith1fff95c2013-09-12 23:28:08 +00001698 // If we're disambiguating a template argument in a default argument in
1699 // a class definition versus a parameter declaration, an '=' here
1700 // disambiguates the parse one way or the other.
1701 // If this is a parameter, it must have a default argument because
1702 // (a) the previous parameter did, and
1703 // (b) this must be the first declaration of the function, so we can't
1704 // inherit any default arguments from elsewhere.
1705 // If we see an ')', then we've reached the end of a
1706 // parameter-declaration-clause, and the last param is missing its default
1707 // argument.
1708 if (VersusTemplateArgument)
Richard Smithee390432014-05-16 01:56:53 +00001709 return (Tok.is(tok::equal) || Tok.is(tok::r_paren)) ? TPResult::True
1710 : TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001711
Guy Benyei11169dd2012-12-18 14:30:41 +00001712 if (Tok.is(tok::equal)) {
1713 // '=' assignment-expression
1714 // Parse through assignment-expression.
Richard Smith1fff95c2013-09-12 23:28:08 +00001715 // FIXME: assignment-expression may contain an unparenthesized comma.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001716 if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smithee390432014-05-16 01:56:53 +00001717 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 }
1719
1720 if (Tok.is(tok::ellipsis)) {
1721 ConsumeToken();
1722 if (Tok.is(tok::r_paren))
Richard Smithee390432014-05-16 01:56:53 +00001723 return TPResult::True; // '...)' is a sign of a function declarator.
Guy Benyei11169dd2012-12-18 14:30:41 +00001724 else
Richard Smithee390432014-05-16 01:56:53 +00001725 return TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001726 }
1727
Alp Toker97650562014-01-10 11:19:30 +00001728 if (!TryConsumeToken(tok::comma))
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 }
1731
Richard Smithee390432014-05-16 01:56:53 +00001732 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001733}
1734
1735/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1736/// parsing as a function declarator.
1737/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
Richard Smithee390432014-05-16 01:56:53 +00001738/// return TPResult::Ambiguous, otherwise it will return either False() or
Guy Benyei11169dd2012-12-18 14:30:41 +00001739/// Error().
1740///
1741/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1742/// exception-specification[opt]
1743///
1744/// exception-specification:
1745/// 'throw' '(' type-id-list[opt] ')'
1746///
1747Parser::TPResult Parser::TryParseFunctionDeclarator() {
1748
1749 // The '(' is already parsed.
1750
1751 TPResult TPR = TryParseParameterDeclarationClause();
Richard Smithee390432014-05-16 01:56:53 +00001752 if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
1753 TPR = TPResult::False;
Guy Benyei11169dd2012-12-18 14:30:41 +00001754
Richard Smithee390432014-05-16 01:56:53 +00001755 if (TPR == TPResult::False || TPR == TPResult::Error)
Guy Benyei11169dd2012-12-18 14:30:41 +00001756 return TPR;
1757
1758 // Parse through the parens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001759 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001760 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001761
1762 // cv-qualifier-seq
1763 while (Tok.is(tok::kw_const) ||
1764 Tok.is(tok::kw_volatile) ||
1765 Tok.is(tok::kw_restrict) )
1766 ConsumeToken();
1767
1768 // ref-qualifier[opt]
1769 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1770 ConsumeToken();
1771
1772 // exception-specification
1773 if (Tok.is(tok::kw_throw)) {
1774 ConsumeToken();
1775 if (Tok.isNot(tok::l_paren))
Richard Smithee390432014-05-16 01:56:53 +00001776 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001777
1778 // Parse through the parens after 'throw'.
1779 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001780 if (!SkipUntil(tok::r_paren, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001781 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 }
1783 if (Tok.is(tok::kw_noexcept)) {
1784 ConsumeToken();
1785 // Possibly an expression as well.
1786 if (Tok.is(tok::l_paren)) {
1787 // Find the matching rparen.
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 }
1793
Richard Smithee390432014-05-16 01:56:53 +00001794 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001795}
1796
1797/// '[' constant-expression[opt] ']'
1798///
1799Parser::TPResult Parser::TryParseBracketDeclarator() {
1800 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001801 if (!SkipUntil(tok::r_square, StopAtSemi))
Richard Smithee390432014-05-16 01:56:53 +00001802 return TPResult::Error;
Guy Benyei11169dd2012-12-18 14:30:41 +00001803
Richard Smithee390432014-05-16 01:56:53 +00001804 return TPResult::Ambiguous;
Guy Benyei11169dd2012-12-18 14:30:41 +00001805}