blob: b26181f08eade62ba8cca97c2e81256d5c1bc62e [file] [log] [blame]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +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"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000018using 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
Anders Carlsson511d7ab2009-03-11 16:27:10 +000033/// [C++0x] static_assert-declaration
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000034///
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///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000050bool 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:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000059 // static_assert-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000060 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +000061 case tok::kw__Static_assert:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000062 return true;
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000063 // simple-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000064 default:
Eli Friedman9490ab42011-12-20 01:50:37 +000065 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000066 }
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///
Eli Friedman9490ab42011-12-20 01:50:37 +000078/// (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) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000083 // 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.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
105 // a case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000106
Richard Smith40b2e192012-08-23 20:19:14 +0000107 bool InvalidAsDeclaration = false;
108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
109 &InvalidAsDeclaration);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000110 if (TPR != TPResult::Ambiguous())
111 return TPR != TPResult::False(); // Returns true for TPResult::True() or
112 // TPResult::Error().
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000113
Richard Smith40b2e192012-08-23 20:19:14 +0000114 // 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
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000122 // FIXME: Add statistics about the number of ambiguous statements encountered
123 // and how they were resolved (number of declarations+number of expressions).
124
Richard Smith40b2e192012-08-23 20:19:14 +0000125 // 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...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000128
129 TentativeParsingAction PA(*this);
Eli Friedman9490ab42011-12-20 01:50:37 +0000130 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000131 PA.Revert();
132
133 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000134 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000135 return true;
136
137 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000138 if (TPR == TPResult::Ambiguous())
139 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000140
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000141 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000142 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000143}
144
145/// simple-declaration:
146/// decl-specifier-seq init-declarator-list[opt] ';'
147///
Eli Friedman9490ab42011-12-20 01:50:37 +0000148/// (if AllowForRangeDecl specified)
149/// for ( for-range-declaration : for-range-initializer ) statement
150/// for-range-declaration:
151/// attribute-specifier-seqopt type-specifier-seq declarator
152///
153Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000154 if (Tok.is(tok::kw_typeof))
155 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000156 else {
Richard Smith40b2e192012-08-23 20:19:14 +0000157 if (Tok.is(tok::annot_cxxscope))
158 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000159 ConsumeToken();
Richard Smith40b2e192012-08-23 20:19:14 +0000160
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000162 TryParseProtocolQualifiers();
163 }
Richard Smith40b2e192012-08-23 20:19:14 +0000164
165 // Two decl-specifiers in a row conclusively disambiguate this as being a
166 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
167 // overwhelmingly common case that the next token is a '('.
168 if (Tok.isNot(tok::l_paren)) {
169 TPResult TPR = isCXXDeclarationSpecifier();
170 if (TPR == TPResult::Ambiguous())
171 return TPResult::True();
172 if (TPR == TPResult::True() || TPR == TPResult::Error())
173 return TPR;
174 assert(TPR == TPResult::False());
175 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000176
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000177 TPResult TPR = TryParseInitDeclaratorList();
178 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000179 return TPR;
180
Eli Friedman9490ab42011-12-20 01:50:37 +0000181 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000182 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000183
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000184 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000185}
186
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000187/// init-declarator-list:
188/// init-declarator
189/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000190///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000191/// init-declarator:
192/// declarator initializer[opt]
193/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000194///
195/// initializer:
196/// '=' initializer-clause
197/// '(' expression-list ')'
198///
199/// initializer-clause:
200/// assignment-expression
201/// '{' initializer-list ','[opt] '}'
202/// '{' '}'
203///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000204Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000205 while (1) {
206 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000207 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
208 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000209 return TPR;
210
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000211 // [GNU] simple-asm-expr[opt] attributes[opt]
212 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000213 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000214
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000215 // initializer[opt]
216 if (Tok.is(tok::l_paren)) {
217 // Parse through the parens.
218 ConsumeParen();
219 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000220 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000221 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-07-15 21:05:01 +0000222 // MSVC and g++ won't examine the rest of declarators if '=' is
223 // encountered; they just conclude that we have a declaration.
224 // EDG parses the initializer completely, which is the proper behavior
225 // for this case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000226 //
Douglas Gregor06b70802010-07-15 21:05:01 +0000227 // At present, Clang follows MSVC and g++, since the parser does not have
228 // the ability to parse an expression fully without recording the
229 // results of that parse.
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000230 // Also allow 'in' after on objective-c declaration as in:
231 // for (int (^b)(void) in array). Ideally this should be done in the
232 // context of parsing for-init-statement of a foreach statement only. But,
233 // in any other context 'in' is invalid after a declaration and parser
234 // issues the error regardless of outcome of this decision.
235 // FIXME. Change if above assumption does not hold.
Douglas Gregor06b70802010-07-15 21:05:01 +0000236 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000237 }
238
239 if (Tok.isNot(tok::comma))
240 break;
241 ConsumeToken(); // the comma.
242 }
243
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000244 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000245}
246
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000247/// isCXXConditionDeclaration - Disambiguates between a declaration or an
248/// expression for a condition of a if/switch/while/for statement.
249/// If during the disambiguation process a parsing error is encountered,
250/// the function returns true to let the declaration parsing code handle it.
251///
252/// condition:
253/// expression
254/// type-specifier-seq declarator '=' assignment-expression
Richard Smithd81e9612012-02-23 01:36:12 +0000255/// [C++11] type-specifier-seq declarator '=' initializer-clause
256/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000257/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
258/// '=' assignment-expression
259///
260bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000261 TPResult TPR = isCXXDeclarationSpecifier();
262 if (TPR != TPResult::Ambiguous())
263 return TPR != TPResult::False(); // Returns true for TPResult::True() or
264 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000265
266 // FIXME: Add statistics about the number of ambiguous statements encountered
267 // and how they were resolved (number of declarations+number of expressions).
268
269 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
270 // We need tentative parsing...
271
272 TentativeParsingAction PA(*this);
273
274 // type-specifier-seq
275 if (Tok.is(tok::kw_typeof))
276 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000277 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000278 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000279
David Blaikie4e4d0842012-03-11 07:00:24 +0000280 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000281 TryParseProtocolQualifiers();
282 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000283 assert(Tok.is(tok::l_paren) && "Expected '('");
284
285 // declarator
286 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
287
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000288 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000289 if (TPR == TPResult::Error())
290 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000291
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000292 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000293 // '='
294 // [GNU] simple-asm-expr[opt] attributes[opt]
295 if (Tok.is(tok::equal) ||
296 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000297 TPR = TPResult::True();
David Blaikie4e4d0842012-03-11 07:00:24 +0000298 else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace))
Richard Smithd81e9612012-02-23 01:36:12 +0000299 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000300 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000301 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000302 }
303
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000304 PA.Revert();
305
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000306 assert(TPR == TPResult::True() || TPR == TPResult::False());
307 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000308}
309
Mike Stump1eb44332009-09-09 15:08:12 +0000310 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000311 ///
312 /// The context parameter states what context we're parsing right
313 /// now, which affects how this routine copes with the token
314 /// following the type-id. If the context is TypeIdInParens, we have
315 /// already parsed the '(' and we will cease lookahead when we hit
316 /// the corresponding ')'. If the context is
317 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
318 /// before this template argument, and will cease lookahead when we
319 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
320 /// and false for an expression. If during the disambiguation
321 /// process a parsing error is encountered, the function returns
322 /// true to let the declaration parsing code handle it.
323 ///
324 /// type-id:
325 /// type-specifier-seq abstract-declarator[opt]
326 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000327bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000329 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000330
331 // C++ 8.2p2:
332 // The ambiguity arising from the similarity between a function-style cast and
333 // a type-id can occur in different contexts. The ambiguity appears as a
334 // choice between a function-style cast expression and a declaration of a
335 // type. The resolution is that any construct that could possibly be a type-id
336 // in its syntactic context shall be considered a type-id.
337
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000338 TPResult TPR = isCXXDeclarationSpecifier();
339 if (TPR != TPResult::Ambiguous())
340 return TPR != TPResult::False(); // Returns true for TPResult::True() or
341 // TPResult::Error().
342
343 // FIXME: Add statistics about the number of ambiguous statements encountered
344 // and how they were resolved (number of declarations+number of expressions).
345
346 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
347 // We need tentative parsing...
348
349 TentativeParsingAction PA(*this);
350
351 // type-specifier-seq
352 if (Tok.is(tok::kw_typeof))
353 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000354 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000355 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000356
David Blaikie4e4d0842012-03-11 07:00:24 +0000357 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000358 TryParseProtocolQualifiers();
359 }
360
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000361 assert(Tok.is(tok::l_paren) && "Expected '('");
362
363 // declarator
364 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
365
366 // In case of an error, let the declaration parsing code handle it.
367 if (TPR == TPResult::Error())
368 TPR = TPResult::True();
369
370 if (TPR == TPResult::Ambiguous()) {
371 // We are supposed to be inside parens, so if after the abstract declarator
372 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000373 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000374 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000375 isAmbiguous = true;
376
Douglas Gregor8b642592009-02-10 00:53:15 +0000377 // We are supposed to be inside a template argument, so if after
378 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
379 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000380 } else if (Context == TypeIdAsTemplateArgument &&
381 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
David Blaikie4e4d0842012-03-11 07:00:24 +0000382 (getLangOpts().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000383 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000384 isAmbiguous = true;
385
386 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000387 TPR = TPResult::False();
388 }
389
390 PA.Revert();
391
392 assert(TPR == TPResult::True() || TPR == TPResult::False());
393 return TPR == TPResult::True();
394}
395
Richard Smith6ee326a2012-04-10 01:32:12 +0000396/// \brief Returns true if this is a C++11 attribute-specifier. Per
397/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
398/// always introduce an attribute. In Objective-C++11, this rule does not
399/// apply if either '[' begins a message-send.
Sean Huntbbd37c62009-11-21 08:43:09 +0000400///
Richard Smith6ee326a2012-04-10 01:32:12 +0000401/// If Disambiguate is true, we try harder to determine whether a '[[' starts
402/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
Sean Huntbbd37c62009-11-21 08:43:09 +0000403///
Richard Smith6ee326a2012-04-10 01:32:12 +0000404/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
405/// Obj-C message send or the start of an attribute. Otherwise, we assume it
406/// is not an Obj-C message send.
Sean Huntbbd37c62009-11-21 08:43:09 +0000407///
Richard Smith6ee326a2012-04-10 01:32:12 +0000408/// C++11 [dcl.attr.grammar]:
409///
410/// attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +0000411/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000412/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000413///
Richard Smith6ee326a2012-04-10 01:32:12 +0000414/// attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +0000415/// attribute[opt]
416/// attribute-list ',' attribute[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +0000417/// attribute '...'
418/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +0000419///
Richard Smith6ee326a2012-04-10 01:32:12 +0000420/// attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +0000421/// attribute-token attribute-argument-clause[opt]
422///
Richard Smith6ee326a2012-04-10 01:32:12 +0000423/// attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +0000424/// identifier
Richard Smith6ee326a2012-04-10 01:32:12 +0000425/// identifier '::' identifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000426///
Richard Smith6ee326a2012-04-10 01:32:12 +0000427/// attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +0000428/// '(' balanced-token-seq ')'
Richard Smith6ee326a2012-04-10 01:32:12 +0000429Parser::CXX11AttributeKind
430Parser::isCXX11AttributeSpecifier(bool Disambiguate,
431 bool OuterMightBeMessageSend) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000432 if (Tok.is(tok::kw_alignas))
Richard Smith6ee326a2012-04-10 01:32:12 +0000433 return CAK_AttributeSpecifier;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000434
Sean Huntbbd37c62009-11-21 08:43:09 +0000435 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
Richard Smith6ee326a2012-04-10 01:32:12 +0000436 return CAK_NotAttributeSpecifier;
Sean Huntbbd37c62009-11-21 08:43:09 +0000437
Richard Smith6ee326a2012-04-10 01:32:12 +0000438 // No tentative parsing if we don't need to look for ']]' or a lambda.
439 if (!Disambiguate && !getLangOpts().ObjC1)
440 return CAK_AttributeSpecifier;
441
442 TentativeParsingAction PA(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000443
444 // Opening brackets were checked for above.
445 ConsumeBracket();
Richard Smith6ee326a2012-04-10 01:32:12 +0000446
447 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
448 if (!getLangOpts().ObjC1) {
449 ConsumeBracket();
450
451 bool IsAttribute = SkipUntil(tok::r_square, false);
452 IsAttribute &= Tok.is(tok::r_square);
453
454 PA.Revert();
455
456 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
457 }
458
459 // In Obj-C++11, we need to distinguish four situations:
460 // 1a) int x[[attr]]; C++11 attribute.
461 // 1b) [[attr]]; C++11 statement attribute.
462 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
463 // 3a) int x[[obj get]]; Message send in array size/index.
464 // 3b) [[Class alloc] init]; Message send in message send.
465 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
466 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
467
468 // If we have a lambda-introducer, then this is definitely not a message send.
469 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
470 // into the tentative attribute parse below.
471 LambdaIntroducer Intro;
472 if (!TryParseLambdaIntroducer(Intro)) {
473 // A lambda cannot end with ']]', and an attribute must.
474 bool IsAttribute = Tok.is(tok::r_square);
475
476 PA.Revert();
477
478 if (IsAttribute)
479 // Case 1: C++11 attribute.
480 return CAK_AttributeSpecifier;
481
482 if (OuterMightBeMessageSend)
483 // Case 4: Lambda in message send.
484 return CAK_NotAttributeSpecifier;
485
486 // Case 2: Lambda in array size / index.
487 return CAK_InvalidAttributeSpecifier;
488 }
489
Sean Huntbbd37c62009-11-21 08:43:09 +0000490 ConsumeBracket();
491
Richard Smith6ee326a2012-04-10 01:32:12 +0000492 // If we don't have a lambda-introducer, then we have an attribute or a
493 // message-send.
494 bool IsAttribute = true;
495 while (Tok.isNot(tok::r_square)) {
496 if (Tok.is(tok::comma)) {
497 // Case 1: Stray commas can only occur in attributes.
498 PA.Revert();
499 return CAK_AttributeSpecifier;
500 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000501
Richard Smith6ee326a2012-04-10 01:32:12 +0000502 // Parse the attribute-token, if present.
503 // C++11 [dcl.attr.grammar]:
504 // If a keyword or an alternative token that satisfies the syntactic
505 // requirements of an identifier is contained in an attribute-token,
506 // it is considered an identifier.
Richard Smithc56298d2012-04-10 03:25:07 +0000507 SourceLocation Loc;
508 if (!TryParseCXX11AttributeIdentifier(Loc)) {
Richard Smith6ee326a2012-04-10 01:32:12 +0000509 IsAttribute = false;
510 break;
511 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000512 if (Tok.is(tok::coloncolon)) {
513 ConsumeToken();
Richard Smithc56298d2012-04-10 03:25:07 +0000514 if (!TryParseCXX11AttributeIdentifier(Loc)) {
Richard Smith6ee326a2012-04-10 01:32:12 +0000515 IsAttribute = false;
516 break;
517 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000518 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000519
Richard Smith6ee326a2012-04-10 01:32:12 +0000520 // Parse the attribute-argument-clause, if present.
521 if (Tok.is(tok::l_paren)) {
522 ConsumeParen();
523 if (!SkipUntil(tok::r_paren, false)) {
524 IsAttribute = false;
525 break;
526 }
527 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000528
Richard Smith6ee326a2012-04-10 01:32:12 +0000529 if (Tok.is(tok::ellipsis))
530 ConsumeToken();
531
532 if (Tok.isNot(tok::comma))
533 break;
534
535 ConsumeToken();
536 }
537
538 // An attribute must end ']]'.
539 if (IsAttribute) {
540 if (Tok.is(tok::r_square)) {
541 ConsumeBracket();
542 IsAttribute = Tok.is(tok::r_square);
543 } else {
544 IsAttribute = false;
545 }
546 }
547
548 PA.Revert();
549
550 if (IsAttribute)
551 // Case 1: C++11 statement attribute.
552 return CAK_AttributeSpecifier;
553
554 // Case 3: Message send.
555 return CAK_NotAttributeSpecifier;
Sean Huntbbd37c62009-11-21 08:43:09 +0000556}
557
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000558/// declarator:
559/// direct-declarator
560/// ptr-operator declarator
561///
562/// direct-declarator:
563/// declarator-id
564/// direct-declarator '(' parameter-declaration-clause ')'
565/// cv-qualifier-seq[opt] exception-specification[opt]
566/// direct-declarator '[' constant-expression[opt] ']'
567/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000568/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000569///
570/// abstract-declarator:
571/// ptr-operator abstract-declarator[opt]
572/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000573/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000574///
575/// direct-abstract-declarator:
576/// direct-abstract-declarator[opt]
577/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
578/// exception-specification[opt]
579/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
580/// '(' abstract-declarator ')'
581///
582/// ptr-operator:
583/// '*' cv-qualifier-seq[opt]
584/// '&'
585/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000586/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000587///
588/// cv-qualifier-seq:
589/// cv-qualifier cv-qualifier-seq[opt]
590///
591/// cv-qualifier:
592/// 'const'
593/// 'volatile'
594///
595/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000596/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000597///
598/// id-expression:
599/// unqualified-id
600/// qualified-id [TODO]
601///
602/// unqualified-id:
603/// identifier
604/// operator-function-id [TODO]
605/// conversion-function-id [TODO]
606/// '~' class-name [TODO]
607/// template-id [TODO]
608///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000609Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
610 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000611 // declarator:
612 // direct-declarator
613 // ptr-operator declarator
614
615 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000616 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000617 if (TryAnnotateCXXScopeToken(true))
618 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000619
Chris Lattner9af55002009-03-27 04:18:06 +0000620 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor69d83162011-01-20 16:08:06 +0000621 Tok.is(tok::ampamp) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000622 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000623 // ptr-operator
624 ConsumeToken();
625 while (Tok.is(tok::kw_const) ||
626 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000627 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000628 ConsumeToken();
629 } else {
630 break;
631 }
632 }
633
634 // direct-declarator:
635 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000636 if (Tok.is(tok::ellipsis))
637 ConsumeToken();
638
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000639 if ((Tok.is(tok::identifier) ||
640 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
641 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000642 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000643 if (Tok.is(tok::annot_cxxscope))
644 ConsumeToken();
Richard Smith05766812012-08-18 00:55:03 +0000645 else
646 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000647 ConsumeToken();
648 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000649 ConsumeParen();
650 if (mayBeAbstract &&
651 (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +0000652 // 'int(...)' is a function.
653 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000654 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000655 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
656 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000657 TPResult TPR = TryParseFunctionDeclarator();
658 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000659 return TPR;
660 } else {
661 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000662 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000663 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000664 if (Tok.is(tok::kw___attribute) ||
665 Tok.is(tok::kw___declspec) ||
666 Tok.is(tok::kw___cdecl) ||
667 Tok.is(tok::kw___stdcall) ||
668 Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000669 Tok.is(tok::kw___thiscall) ||
Douglas Gregor8d267c52011-09-09 02:06:17 +0000670 Tok.is(tok::kw___unaligned))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000671 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000672 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000673 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000674 return TPR;
675 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000676 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000677 ConsumeParen();
678 }
679 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000680 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000681 }
682
683 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000684 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000685
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000686 // abstract-declarator: ...
687 if (Tok.is(tok::ellipsis))
688 ConsumeToken();
689
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000690 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000691 // Check whether we have a function declarator or a possible ctor-style
692 // initializer that follows the declarator. Note that ctor-style
693 // initializers are not possible in contexts where abstract declarators
694 // are allowed.
Richard Smithb9c62612012-07-30 21:30:52 +0000695 if (!mayBeAbstract && !isCXXFunctionDeclarator())
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000696 break;
697
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000698 // direct-declarator '(' parameter-declaration-clause ')'
699 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000700 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000701 TPR = TryParseFunctionDeclarator();
702 } else if (Tok.is(tok::l_square)) {
703 // direct-declarator '[' constant-expression[opt] ']'
704 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
705 TPR = TryParseBracketDeclarator();
706 } else {
707 break;
708 }
709
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000710 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000711 return TPR;
712 }
713
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000714 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000715}
716
Douglas Gregora61b3e72010-12-01 17:42:47 +0000717Parser::TPResult
718Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
719 switch (Kind) {
720 // Obviously starts an expression.
721 case tok::numeric_constant:
722 case tok::char_constant:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000723 case tok::wide_char_constant:
724 case tok::utf16_char_constant:
725 case tok::utf32_char_constant:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000726 case tok::string_literal:
727 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000728 case tok::utf8_string_literal:
729 case tok::utf16_string_literal:
730 case tok::utf32_string_literal:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000731 case tok::l_square:
732 case tok::l_paren:
733 case tok::amp:
Douglas Gregor69d83162011-01-20 16:08:06 +0000734 case tok::ampamp:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000735 case tok::star:
736 case tok::plus:
737 case tok::plusplus:
738 case tok::minus:
739 case tok::minusminus:
740 case tok::tilde:
741 case tok::exclaim:
742 case tok::kw_sizeof:
743 case tok::kw___func__:
744 case tok::kw_const_cast:
745 case tok::kw_delete:
746 case tok::kw_dynamic_cast:
747 case tok::kw_false:
748 case tok::kw_new:
749 case tok::kw_operator:
750 case tok::kw_reinterpret_cast:
751 case tok::kw_static_cast:
752 case tok::kw_this:
753 case tok::kw_throw:
754 case tok::kw_true:
755 case tok::kw_typeid:
756 case tok::kw_alignof:
757 case tok::kw_noexcept:
758 case tok::kw_nullptr:
Jordan Rosef70a8862012-06-30 21:33:57 +0000759 case tok::kw__Alignof:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000760 case tok::kw___null:
761 case tok::kw___alignof:
762 case tok::kw___builtin_choose_expr:
763 case tok::kw___builtin_offsetof:
764 case tok::kw___builtin_types_compatible_p:
765 case tok::kw___builtin_va_arg:
766 case tok::kw___imag:
767 case tok::kw___real:
768 case tok::kw___FUNCTION__:
Nico Weber28ad0632012-06-23 02:07:59 +0000769 case tok::kw_L__FUNCTION__:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000770 case tok::kw___PRETTY_FUNCTION__:
771 case tok::kw___has_nothrow_assign:
772 case tok::kw___has_nothrow_copy:
773 case tok::kw___has_nothrow_constructor:
774 case tok::kw___has_trivial_assign:
775 case tok::kw___has_trivial_copy:
776 case tok::kw___has_trivial_constructor:
777 case tok::kw___has_trivial_destructor:
778 case tok::kw___has_virtual_destructor:
779 case tok::kw___is_abstract:
780 case tok::kw___is_base_of:
781 case tok::kw___is_class:
Douglas Gregor9f361132011-01-27 20:28:01 +0000782 case tok::kw___is_convertible_to:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000783 case tok::kw___is_empty:
784 case tok::kw___is_enum:
John McCallea30e2f2012-09-25 07:32:49 +0000785 case tok::kw___is_interface_class:
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000786 case tok::kw___is_final:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000787 case tok::kw___is_literal:
Chandler Carruth38402812011-04-24 02:49:28 +0000788 case tok::kw___is_literal_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000789 case tok::kw___is_pod:
790 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +0000791 case tok::kw___is_trivial:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +0000792 case tok::kw___is_trivially_assignable:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000793 case tok::kw___is_trivially_constructible:
Sean Huntfeb375d2011-05-13 00:31:07 +0000794 case tok::kw___is_trivially_copyable:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000795 case tok::kw___is_union:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000796 case tok::kw___uuidof:
797 return TPResult::True();
798
799 // Obviously starts a type-specifier-seq:
800 case tok::kw_char:
801 case tok::kw_const:
802 case tok::kw_double:
803 case tok::kw_enum:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000804 case tok::kw_half:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000805 case tok::kw_float:
806 case tok::kw_int:
807 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000808 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000809 case tok::kw___int128:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000810 case tok::kw_restrict:
811 case tok::kw_short:
812 case tok::kw_signed:
813 case tok::kw_struct:
814 case tok::kw_union:
815 case tok::kw_unsigned:
816 case tok::kw_void:
817 case tok::kw_volatile:
818 case tok::kw__Bool:
819 case tok::kw__Complex:
820 case tok::kw_class:
821 case tok::kw_typename:
822 case tok::kw_wchar_t:
823 case tok::kw_char16_t:
824 case tok::kw_char32_t:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000825 case tok::kw___underlying_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000826 case tok::kw_thread_local:
827 case tok::kw__Decimal32:
828 case tok::kw__Decimal64:
829 case tok::kw__Decimal128:
830 case tok::kw___thread:
831 case tok::kw_typeof:
832 case tok::kw___cdecl:
833 case tok::kw___stdcall:
834 case tok::kw___fastcall:
835 case tok::kw___thiscall:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000836 case tok::kw___unaligned:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000837 case tok::kw___vector:
838 case tok::kw___pixel:
Eli Friedmanb001de72011-10-06 23:00:33 +0000839 case tok::kw__Atomic:
John McCallb8a8de32012-11-14 00:49:39 +0000840 case tok::kw___unknown_anytype:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000841 return TPResult::False();
842
843 default:
844 break;
845 }
846
847 return TPResult::Ambiguous();
848}
849
Richard Smith05766812012-08-18 00:55:03 +0000850bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
851 return std::find(TentativelyDeclaredIdentifiers.begin(),
852 TentativelyDeclaredIdentifiers.end(), II)
853 != TentativelyDeclaredIdentifiers.end();
854}
855
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000856/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
857/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
858/// be either a decl-specifier or a function-style cast, and TPResult::Error()
859/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000860///
Richard Smith25582fc2012-05-16 23:40:17 +0000861/// If HasMissingTypename is provided, a name with a dependent scope specifier
862/// will be treated as ambiguous if the 'typename' keyword is missing. If this
Richard Smith05766812012-08-18 00:55:03 +0000863/// happens, *HasMissingTypename will be set to 'true'. This will also be used
864/// as an indicator that undeclared identifiers (which will trigger a later
865/// parse error) should be treated as types. Returns TPResult::Ambiguous() in
866/// such cases.
Richard Smith25582fc2012-05-16 23:40:17 +0000867///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000868/// decl-specifier:
869/// storage-class-specifier
870/// type-specifier
871/// function-specifier
872/// 'friend'
873/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000874/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000875/// [GNU] attributes declaration-specifiers[opt]
876///
877/// storage-class-specifier:
878/// 'register'
879/// 'static'
880/// 'extern'
881/// 'mutable'
882/// 'auto'
883/// [GNU] '__thread'
884///
885/// function-specifier:
886/// 'inline'
887/// 'virtual'
888/// 'explicit'
889///
890/// typedef-name:
891/// identifier
892///
893/// type-specifier:
894/// simple-type-specifier
895/// class-specifier
896/// enum-specifier
897/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000898/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000899/// cv-qualifier
900///
901/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000902/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000903/// '::'[opt] nested-name-specifier 'template'
904/// simple-template-id [TODO]
905/// 'char'
906/// 'wchar_t'
907/// 'bool'
908/// 'short'
909/// 'int'
910/// 'long'
911/// 'signed'
912/// 'unsigned'
913/// 'float'
914/// 'double'
915/// 'void'
916/// [GNU] typeof-specifier
917/// [GNU] '_Complex'
918/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000919/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000920///
921/// type-name:
922/// class-name
923/// enum-name
924/// typedef-name
925///
926/// elaborated-type-specifier:
927/// class-key '::'[opt] nested-name-specifier[opt] identifier
928/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
929/// simple-template-id
930/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
931///
932/// enum-name:
933/// identifier
934///
935/// enum-specifier:
936/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
937/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
938///
939/// class-specifier:
940/// class-head '{' member-specification[opt] '}'
941///
942/// class-head:
943/// class-key identifier[opt] base-clause[opt]
944/// class-key nested-name-specifier identifier base-clause[opt]
945/// class-key nested-name-specifier[opt] simple-template-id
946/// base-clause[opt]
947///
948/// class-key:
949/// 'class'
950/// 'struct'
951/// 'union'
952///
953/// cv-qualifier:
954/// 'const'
955/// 'volatile'
956/// [GNU] restrict
957///
Richard Smithd81e9612012-02-23 01:36:12 +0000958Parser::TPResult
Richard Smith25582fc2012-05-16 23:40:17 +0000959Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
960 bool *HasMissingTypename) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000961 switch (Tok.getKind()) {
Richard Smith05766812012-08-18 00:55:03 +0000962 case tok::identifier: {
John Thompson82287d12010-02-05 00:12:22 +0000963 // Check for need to substitute AltiVec __vector keyword
964 // for "vector" identifier.
965 if (TryAltiVecVectorToken())
966 return TPResult::True();
Richard Smith05766812012-08-18 00:55:03 +0000967
968 const Token &Next = NextToken();
969 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
970 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
971 return TPResult::True();
972
973 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
974 // Determine whether this is a valid expression. If not, we will hit
975 // a parse error one way or another. In that case, tell the caller that
976 // this is ambiguous. Typo-correct to type and expression keywords and
977 // to types and identifiers, in order to try to recover from errors.
978 CorrectionCandidateCallback TypoCorrection;
979 TypoCorrection.WantRemainingKeywords = false;
980 switch (TryAnnotateName(false /* no nested name specifier */,
981 &TypoCorrection)) {
982 case ANK_Error:
983 return TPResult::Error();
984 case ANK_TentativeDecl:
985 return TPResult::False();
986 case ANK_TemplateName:
987 // A bare type template-name which can't be a template template
988 // argument is an error, and was probably intended to be a type.
989 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
990 case ANK_Unresolved:
991 return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False();
992 case ANK_Success:
993 break;
994 }
995 assert(Tok.isNot(tok::identifier) &&
996 "TryAnnotateName succeeded without producing an annotation");
997 } else {
998 // This might possibly be a type with a dependent scope specifier and
999 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1000 // since it will annotate as a primary expression, and we want to use the
1001 // "missing 'typename'" logic.
1002 if (TryAnnotateTypeOrScopeToken())
1003 return TPResult::Error();
1004 // If annotation failed, assume it's a non-type.
1005 // FIXME: If this happens due to an undeclared identifier, treat it as
1006 // ambiguous.
1007 if (Tok.is(tok::identifier))
1008 return TPResult::False();
1009 }
1010
1011 // We annotated this token as something. Recurse to handle whatever we got.
1012 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1013 }
1014
Douglas Gregord57959a2009-03-27 23:10:48 +00001015 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +00001016 // Annotate typenames and C++ scope specifiers. If we get one, just
1017 // recurse to handle whatever we get.
1018 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001019 return TPResult::Error();
Richard Smith25582fc2012-05-16 23:40:17 +00001020 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
Chris Lattnere5849262009-01-04 23:33:56 +00001021
Chris Lattner2ee9b402009-12-19 01:11:05 +00001022 case tok::coloncolon: { // ::foo::bar
1023 const Token &Next = NextToken();
1024 if (Next.is(tok::kw_new) || // ::new
1025 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +00001026 return TPResult::False();
David Blaikie42d6d0c2011-12-04 05:04:18 +00001027 }
1028 // Fall through.
1029 case tok::kw_decltype:
Chris Lattnere5849262009-01-04 23:33:56 +00001030 // Annotate typenames and C++ scope specifiers. If we get one, just
1031 // recurse to handle whatever we get.
1032 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001033 return TPResult::Error();
Richard Smith25582fc2012-05-16 23:40:17 +00001034 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
Nick Lewycky443aac92012-01-25 01:19:14 +00001035
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001036 // decl-specifier:
1037 // storage-class-specifier
1038 // type-specifier
1039 // function-specifier
1040 // 'friend'
1041 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +00001042 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001043 case tok::kw_friend:
1044 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +00001045 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001046 // storage-class-specifier
1047 case tok::kw_register:
1048 case tok::kw_static:
1049 case tok::kw_extern:
1050 case tok::kw_mutable:
1051 case tok::kw_auto:
1052 case tok::kw___thread:
1053 // function-specifier
1054 case tok::kw_inline:
1055 case tok::kw_virtual:
1056 case tok::kw_explicit:
1057
Douglas Gregor8d267c52011-09-09 02:06:17 +00001058 // Modules
1059 case tok::kw___module_private__:
John McCallb8a8de32012-11-14 00:49:39 +00001060
1061 // Debugger support
1062 case tok::kw___unknown_anytype:
Douglas Gregor8d267c52011-09-09 02:06:17 +00001063
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001064 // type-specifier:
1065 // simple-type-specifier
1066 // class-specifier
1067 // enum-specifier
1068 // elaborated-type-specifier
1069 // typename-specifier
1070 // cv-qualifier
1071
1072 // class-specifier
1073 // elaborated-type-specifier
1074 case tok::kw_class:
1075 case tok::kw_struct:
1076 case tok::kw_union:
1077 // enum-specifier
1078 case tok::kw_enum:
1079 // cv-qualifier
1080 case tok::kw_const:
1081 case tok::kw_volatile:
1082
1083 // GNU
1084 case tok::kw_restrict:
1085 case tok::kw__Complex:
1086 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001087 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Steve Naroff7ec56582009-01-06 17:40:00 +00001089 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +00001090 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +00001091 case tok::kw___cdecl:
1092 case tok::kw___stdcall:
1093 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001094 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001095 case tok::kw___w64:
1096 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001097 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00001098 case tok::kw___forceinline:
Douglas Gregor8d267c52011-09-09 02:06:17 +00001099 case tok::kw___unaligned:
Eli Friedman290eeb02009-06-08 23:27:34 +00001100 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +00001101
1102 // Borland
1103 case tok::kw___pascal:
1104 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +00001105
1106 // AltiVec
1107 case tok::kw___vector:
1108 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001109
John McCall51e2a5d2010-04-30 03:11:01 +00001110 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001111 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
John McCall51e2a5d2010-04-30 03:11:01 +00001112 if (TemplateId->Kind != TNK_Type_template)
1113 return TPResult::False();
1114 CXXScopeSpec SS;
Douglas Gregor059101f2011-03-02 00:47:37 +00001115 AnnotateTemplateIdTokenAsType();
John McCall51e2a5d2010-04-30 03:11:01 +00001116 assert(Tok.is(tok::annot_typename));
1117 goto case_typename;
1118 }
1119
John McCallae03cb52009-12-19 00:35:18 +00001120 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1121 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +00001122 if (TryAnnotateTypeOrScopeToken())
1123 return TPResult::Error();
Nick Lewycky443aac92012-01-25 01:19:14 +00001124 if (!Tok.is(tok::annot_typename)) {
1125 // If the next token is an identifier or a type qualifier, then this
1126 // can't possibly be a valid expression either.
1127 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1128 CXXScopeSpec SS;
1129 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1130 Tok.getAnnotationRange(),
1131 SS);
1132 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1133 TentativeParsingAction PA(*this);
1134 ConsumeToken();
1135 ConsumeToken();
1136 bool isIdentifier = Tok.is(tok::identifier);
1137 TPResult TPR = TPResult::False();
1138 if (!isIdentifier)
Richard Smith25582fc2012-05-16 23:40:17 +00001139 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1140 HasMissingTypename);
Nick Lewycky443aac92012-01-25 01:19:14 +00001141 PA.Revert();
1142
1143 if (isIdentifier ||
1144 TPR == TPResult::True() || TPR == TPResult::Error())
1145 return TPResult::Error();
Richard Smith25582fc2012-05-16 23:40:17 +00001146
1147 if (HasMissingTypename) {
1148 // We can't tell whether this is a missing 'typename' or a valid
1149 // expression.
1150 *HasMissingTypename = true;
1151 return TPResult::Ambiguous();
1152 }
Richard Smith05766812012-08-18 00:55:03 +00001153 } else {
1154 // Try to resolve the name. If it doesn't exist, assume it was
1155 // intended to name a type and keep disambiguating.
1156 switch (TryAnnotateName(false /* SS is not dependent */)) {
1157 case ANK_Error:
1158 return TPResult::Error();
1159 case ANK_TentativeDecl:
1160 return TPResult::False();
1161 case ANK_TemplateName:
1162 // A bare type template-name which can't be a template template
1163 // argument is an error, and was probably intended to be a type.
1164 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
1165 case ANK_Unresolved:
1166 return HasMissingTypename ? TPResult::Ambiguous()
1167 : TPResult::False();
1168 case ANK_Success:
1169 // Annotated it, check again.
1170 assert(Tok.isNot(tok::annot_cxxscope) ||
1171 NextToken().isNot(tok::identifier));
1172 return isCXXDeclarationSpecifier(BracedCastResult,
1173 HasMissingTypename);
1174 }
Nick Lewycky443aac92012-01-25 01:19:14 +00001175 }
1176 }
John McCallae03cb52009-12-19 00:35:18 +00001177 return TPResult::False();
Nick Lewycky443aac92012-01-25 01:19:14 +00001178 }
John McCallae03cb52009-12-19 00:35:18 +00001179 // If that succeeded, fallthrough into the generic simple-type-id case.
1180
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001181 // The ambiguity resides in a simple-type-specifier/typename-specifier
1182 // followed by a '('. The '(' could either be the start of:
1183 //
1184 // direct-declarator:
1185 // '(' declarator ')'
1186 //
1187 // direct-abstract-declarator:
1188 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1189 // exception-specification[opt]
1190 // '(' abstract-declarator ')'
1191 //
1192 // or part of a function-style cast expression:
1193 //
1194 // simple-type-specifier '(' expression-list[opt] ')'
1195 //
1196
1197 // simple-type-specifier:
1198
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001199 case tok::annot_typename:
1200 case_typename:
1201 // In Objective-C, we might have a protocol-qualified type.
David Blaikie4e4d0842012-03-11 07:00:24 +00001202 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001203 // Tentatively parse the
1204 TentativeParsingAction PA(*this);
1205 ConsumeToken(); // The type token
1206
1207 TPResult TPR = TryParseProtocolQualifiers();
1208 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001209 bool isFollowedByBrace = Tok.is(tok::l_brace);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001210
1211 PA.Revert();
1212
1213 if (TPR == TPResult::Error())
1214 return TPResult::Error();
1215
1216 if (isFollowedByParen)
1217 return TPResult::Ambiguous();
Richard Smithd81e9612012-02-23 01:36:12 +00001218
David Blaikie4e4d0842012-03-11 07:00:24 +00001219 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001220 return BracedCastResult;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001221
1222 return TPResult::True();
1223 }
1224
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001225 case tok::kw_char:
1226 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001227 case tok::kw_char16_t:
1228 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001229 case tok::kw_bool:
1230 case tok::kw_short:
1231 case tok::kw_int:
1232 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001233 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00001234 case tok::kw___int128:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001235 case tok::kw_signed:
1236 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001237 case tok::kw_half:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001238 case tok::kw_float:
1239 case tok::kw_double:
1240 case tok::kw_void:
David Blaikie5e089fe2012-01-24 05:47:35 +00001241 case tok::annot_decltype:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001242 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001243 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001244
Richard Smithd81e9612012-02-23 01:36:12 +00001245 // This is a function-style cast in all cases we disambiguate other than
1246 // one:
1247 // struct S {
1248 // enum E : int { a = 4 }; // enum
1249 // enum E : int { 4 }; // bit-field
1250 // };
David Blaikie4e4d0842012-03-11 07:00:24 +00001251 if (getLangOpts().CPlusPlus0x && NextToken().is(tok::l_brace))
Richard Smithd81e9612012-02-23 01:36:12 +00001252 return BracedCastResult;
1253
Douglas Gregor9497a732010-09-16 01:51:54 +00001254 if (isStartOfObjCClassMessageMissingOpenBracket())
1255 return TPResult::False();
1256
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001257 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001258
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001259 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001260 case tok::kw_typeof: {
1261 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001262 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001263
1264 TentativeParsingAction PA(*this);
1265
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001266 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001267 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001268 bool isFollowedByBrace = Tok.is(tok::l_brace);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001269
1270 PA.Revert();
1271
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001272 if (TPR == TPResult::Error())
1273 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001274
1275 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001276 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001277
David Blaikie4e4d0842012-03-11 07:00:24 +00001278 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001279 return BracedCastResult;
1280
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001281 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001282 }
1283
Sean Huntdb5d44b2011-05-19 05:37:45 +00001284 // C++0x type traits support
1285 case tok::kw___underlying_type:
1286 return TPResult::True();
1287
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001288 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00001289 case tok::kw__Atomic:
1290 return TPResult::True();
1291
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001292 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001293 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001294 }
1295}
1296
1297/// [GNU] typeof-specifier:
1298/// 'typeof' '(' expressions ')'
1299/// 'typeof' '(' type-name ')'
1300///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001301Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001302 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1303 ConsumeToken();
1304
1305 assert(Tok.is(tok::l_paren) && "Expected '('");
1306 // Parse through the parens after 'typeof'.
1307 ConsumeParen();
1308 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001309 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001310
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001311 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001312}
1313
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001314/// [ObjC] protocol-qualifiers:
1315//// '<' identifier-list '>'
1316Parser::TPResult Parser::TryParseProtocolQualifiers() {
1317 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1318 ConsumeToken();
1319 do {
1320 if (Tok.isNot(tok::identifier))
1321 return TPResult::Error();
1322 ConsumeToken();
1323
1324 if (Tok.is(tok::comma)) {
1325 ConsumeToken();
1326 continue;
1327 }
1328
1329 if (Tok.is(tok::greater)) {
1330 ConsumeToken();
1331 return TPResult::Ambiguous();
1332 }
1333 } while (false);
1334
1335 return TPResult::Error();
1336}
1337
Richard Smith25582fc2012-05-16 23:40:17 +00001338Parser::TPResult
1339Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) {
1340 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
1341 HasMissingTypename);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001342 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001343 return TPR;
1344
1345 if (Tok.is(tok::kw_typeof))
1346 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001347 else {
Richard Smith25582fc2012-05-16 23:40:17 +00001348 if (Tok.is(tok::annot_cxxscope))
1349 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001350 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001351
David Blaikie4e4d0842012-03-11 07:00:24 +00001352 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001353 TryParseProtocolQualifiers();
1354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001356 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001357}
1358
1359/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1360/// a constructor-style initializer, when parsing declaration statements.
1361/// Returns true for function declarator and false for constructor-style
1362/// initializer.
1363/// If during the disambiguation process a parsing error is encountered,
1364/// the function returns true to let the declaration parsing code handle it.
1365///
1366/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1367/// exception-specification[opt]
1368///
Richard Smithb9c62612012-07-30 21:30:52 +00001369bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001370
1371 // C++ 8.2p1:
1372 // The ambiguity arising from the similarity between a function-style cast and
1373 // a declaration mentioned in 6.8 can also occur in the context of a
1374 // declaration. In that context, the choice is between a function declaration
1375 // with a redundant set of parentheses around a parameter name and an object
1376 // declaration with a function-style cast as the initializer. Just as for the
1377 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1378 // that could possibly be a declaration a declaration.
1379
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001380 TentativeParsingAction PA(*this);
1381
1382 ConsumeParen();
Richard Smith25582fc2012-05-16 23:40:17 +00001383 bool InvalidAsDeclaration = false;
1384 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1385 if (TPR == TPResult::Ambiguous()) {
1386 if (Tok.isNot(tok::r_paren))
1387 TPR = TPResult::False();
1388 else {
1389 const Token &Next = NextToken();
1390 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1391 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1392 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1393 Next.is(tok::l_square) || isCXX0XVirtSpecifier(Next) ||
1394 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
Richard Smith5969a5f2012-07-23 21:41:30 +00001395 Next.is(tok::equal) || Next.is(tok::arrow))
Richard Smith25582fc2012-05-16 23:40:17 +00001396 // The next token cannot appear after a constructor-style initializer,
1397 // and can appear next in a function definition. This must be a function
1398 // declarator.
1399 TPR = TPResult::True();
1400 else if (InvalidAsDeclaration)
1401 // Use the absence of 'typename' as a tie-breaker.
1402 TPR = TPResult::False();
1403 }
1404 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001405
1406 PA.Revert();
1407
Richard Smithb9c62612012-07-30 21:30:52 +00001408 if (IsAmbiguous && TPR == TPResult::Ambiguous())
1409 *IsAmbiguous = true;
1410
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001411 // In case of an error, let the declaration parsing code handle it.
Richard Smithb9c62612012-07-30 21:30:52 +00001412 return TPR != TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001413}
1414
1415/// parameter-declaration-clause:
1416/// parameter-declaration-list[opt] '...'[opt]
1417/// parameter-declaration-list ',' '...'
1418///
1419/// parameter-declaration-list:
1420/// parameter-declaration
1421/// parameter-declaration-list ',' parameter-declaration
1422///
1423/// parameter-declaration:
Richard Smith6ee326a2012-04-10 01:32:12 +00001424/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1425/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001426/// '=' assignment-expression
Richard Smith6ee326a2012-04-10 01:32:12 +00001427/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1428/// attributes[opt]
1429/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1430/// attributes[opt] '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001431///
Richard Smith25582fc2012-05-16 23:40:17 +00001432Parser::TPResult
1433Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001434
1435 if (Tok.is(tok::r_paren))
Richard Smithb9c62612012-07-30 21:30:52 +00001436 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001437
1438 // parameter-declaration-list[opt] '...'[opt]
1439 // parameter-declaration-list ',' '...'
1440 //
1441 // parameter-declaration-list:
1442 // parameter-declaration
1443 // parameter-declaration-list ',' parameter-declaration
1444 //
1445 while (1) {
1446 // '...'[opt]
1447 if (Tok.is(tok::ellipsis)) {
1448 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001449 if (Tok.is(tok::r_paren))
1450 return TPResult::True(); // '...)' is a sign of a function declarator.
1451 else
1452 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001453 }
1454
Richard Smith6ce48a72012-04-11 04:01:28 +00001455 // An attribute-specifier-seq here is a sign of a function declarator.
1456 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1457 /*OuterMightBeMessageSend*/true))
1458 return TPResult::True();
1459
John McCall0b7e6782011-03-24 11:26:52 +00001460 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001461 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001462
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001463 // decl-specifier-seq
Richard Smithd81e9612012-02-23 01:36:12 +00001464 // A parameter-declaration's initializer must be preceded by an '=', so
1465 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smith25582fc2012-05-16 23:40:17 +00001466 TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001467 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001468 return TPR;
1469
1470 // declarator
1471 // abstract-declarator[opt]
1472 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001473 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001474 return TPR;
1475
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001476 // [GNU] attributes[opt]
1477 if (Tok.is(tok::kw___attribute))
1478 return TPResult::True();
1479
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001480 if (Tok.is(tok::equal)) {
1481 // '=' assignment-expression
1482 // Parse through assignment-expression.
David Blaikieeb52f86a2012-04-09 16:37:11 +00001483 if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/,
1484 true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001485 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001486 }
1487
1488 if (Tok.is(tok::ellipsis)) {
1489 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001490 if (Tok.is(tok::r_paren))
1491 return TPResult::True(); // '...)' is a sign of a function declarator.
1492 else
1493 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001494 }
1495
1496 if (Tok.isNot(tok::comma))
1497 break;
1498 ConsumeToken(); // the comma.
1499 }
1500
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001501 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001502}
1503
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001504/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1505/// parsing as a function declarator.
1506/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1507/// return TPResult::Ambiguous(), otherwise it will return either False() or
1508/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001509///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001510/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1511/// exception-specification[opt]
1512///
1513/// exception-specification:
1514/// 'throw' '(' type-id-list[opt] ')'
1515///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001516Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001517
1518 // The '(' is already parsed.
1519
1520 TPResult TPR = TryParseParameterDeclarationClause();
1521 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1522 TPR = TPResult::False();
1523
1524 if (TPR == TPResult::False() || TPR == TPResult::Error())
1525 return TPR;
1526
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001527 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001528 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001529 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001530
1531 // cv-qualifier-seq
1532 while (Tok.is(tok::kw_const) ||
1533 Tok.is(tok::kw_volatile) ||
1534 Tok.is(tok::kw_restrict) )
1535 ConsumeToken();
1536
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001537 // ref-qualifier[opt]
1538 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1539 ConsumeToken();
1540
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001541 // exception-specification
1542 if (Tok.is(tok::kw_throw)) {
1543 ConsumeToken();
1544 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001545 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001546
1547 // Parse through the parens after 'throw'.
1548 ConsumeParen();
1549 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001550 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001551 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001552 if (Tok.is(tok::kw_noexcept)) {
1553 ConsumeToken();
1554 // Possibly an expression as well.
1555 if (Tok.is(tok::l_paren)) {
1556 // Find the matching rparen.
1557 ConsumeParen();
1558 if (!SkipUntil(tok::r_paren))
1559 return TPResult::Error();
1560 }
1561 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001562
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001563 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001564}
1565
1566/// '[' constant-expression[opt] ']'
1567///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001568Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001569 ConsumeBracket();
1570 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001571 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001572
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001573 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001574}