blob: bec201ce23f6adae9f98b324db699156ce17555b [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:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000061 return true;
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000062 // simple-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000063 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000064 return isCXXSimpleDeclaration();
65 }
66}
67
68/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
69/// between a simple-declaration or an expression-statement.
70/// If during the disambiguation process a parsing error is encountered,
71/// the function returns true to let the declaration parsing code handle it.
72/// Returns false if the statement is disambiguated as expression.
73///
74/// simple-declaration:
75/// decl-specifier-seq init-declarator-list[opt] ';'
76///
77bool Parser::isCXXSimpleDeclaration() {
78 // C++ 6.8p1:
79 // There is an ambiguity in the grammar involving expression-statements and
80 // declarations: An expression-statement with a function-style explicit type
81 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
82 // from a declaration where the first declarator starts with a '('. In those
83 // cases the statement is a declaration. [Note: To disambiguate, the whole
84 // statement might have to be examined to determine if it is an
85 // expression-statement or a declaration].
86
87 // C++ 6.8p3:
88 // The disambiguation is purely syntactic; that is, the meaning of the names
89 // occurring in such a statement, beyond whether they are type-names or not,
90 // is not generally used in or changed by the disambiguation. Class
91 // templates are instantiated as necessary to determine if a qualified name
92 // is a type-name. Disambiguation precedes parsing, and a statement
93 // disambiguated as a declaration may be an ill-formed declaration.
94
95 // We don't have to parse all of the decl-specifier-seq part. There's only
96 // an ambiguity if the first decl-specifier is
97 // simple-type-specifier/typename-specifier followed by a '(', which may
98 // indicate a function-style cast expression.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +000099 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
100 // a case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000101
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000102 TPResult TPR = isCXXDeclarationSpecifier();
103 if (TPR != TPResult::Ambiguous())
104 return TPR != TPResult::False(); // Returns true for TPResult::True() or
105 // TPResult::Error().
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000106
107 // FIXME: Add statistics about the number of ambiguous statements encountered
108 // and how they were resolved (number of declarations+number of expressions).
109
110 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
111 // We need tentative parsing...
112
113 TentativeParsingAction PA(*this);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000114 TPR = TryParseSimpleDeclaration();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000115 PA.Revert();
116
117 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000118 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000119 return true;
120
121 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000122 if (TPR == TPResult::Ambiguous())
123 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000124
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000125 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000126 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000127}
128
129/// simple-declaration:
130/// decl-specifier-seq init-declarator-list[opt] ';'
131///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000132Parser::TPResult Parser::TryParseSimpleDeclaration() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000133 // We know that we have a simple-type-specifier/typename-specifier followed
134 // by a '('.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000135 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000136
137 if (Tok.is(tok::kw_typeof))
138 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000139 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000140 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000141
142 if (getLang().ObjC1 && Tok.is(tok::less))
143 TryParseProtocolQualifiers();
144 }
145
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000146 assert(Tok.is(tok::l_paren) && "Expected '('");
147
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000148 TPResult TPR = TryParseInitDeclaratorList();
149 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000150 return TPR;
151
152 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000153 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000154
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000155 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000156}
157
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000158/// init-declarator-list:
159/// init-declarator
160/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000161///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000162/// init-declarator:
163/// declarator initializer[opt]
164/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000165///
166/// initializer:
167/// '=' initializer-clause
168/// '(' expression-list ')'
169///
170/// initializer-clause:
171/// assignment-expression
172/// '{' initializer-list ','[opt] '}'
173/// '{' '}'
174///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000175Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000176 while (1) {
177 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000178 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
179 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000180 return TPR;
181
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000182 // [GNU] simple-asm-expr[opt] attributes[opt]
183 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000184 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000185
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000186 // initializer[opt]
187 if (Tok.is(tok::l_paren)) {
188 // Parse through the parens.
189 ConsumeParen();
190 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000191 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000192 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-07-15 21:05:01 +0000193 // MSVC and g++ won't examine the rest of declarators if '=' is
194 // encountered; they just conclude that we have a declaration.
195 // EDG parses the initializer completely, which is the proper behavior
196 // for this case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000197 //
Douglas Gregor06b70802010-07-15 21:05:01 +0000198 // At present, Clang follows MSVC and g++, since the parser does not have
199 // the ability to parse an expression fully without recording the
200 // results of that parse.
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000201 // Also allow 'in' after on objective-c declaration as in:
202 // for (int (^b)(void) in array). Ideally this should be done in the
203 // context of parsing for-init-statement of a foreach statement only. But,
204 // in any other context 'in' is invalid after a declaration and parser
205 // issues the error regardless of outcome of this decision.
206 // FIXME. Change if above assumption does not hold.
Douglas Gregor06b70802010-07-15 21:05:01 +0000207 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000208 }
209
210 if (Tok.isNot(tok::comma))
211 break;
212 ConsumeToken(); // the comma.
213 }
214
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000215 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000216}
217
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000218/// isCXXConditionDeclaration - Disambiguates between a declaration or an
219/// expression for a condition of a if/switch/while/for statement.
220/// If during the disambiguation process a parsing error is encountered,
221/// the function returns true to let the declaration parsing code handle it.
222///
223/// condition:
224/// expression
225/// type-specifier-seq declarator '=' assignment-expression
226/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
227/// '=' assignment-expression
228///
229bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000230 TPResult TPR = isCXXDeclarationSpecifier();
231 if (TPR != TPResult::Ambiguous())
232 return TPR != TPResult::False(); // Returns true for TPResult::True() or
233 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000234
235 // FIXME: Add statistics about the number of ambiguous statements encountered
236 // and how they were resolved (number of declarations+number of expressions).
237
238 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
239 // We need tentative parsing...
240
241 TentativeParsingAction PA(*this);
242
243 // type-specifier-seq
244 if (Tok.is(tok::kw_typeof))
245 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000246 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000247 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000248
249 if (getLang().ObjC1 && Tok.is(tok::less))
250 TryParseProtocolQualifiers();
251 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000252 assert(Tok.is(tok::l_paren) && "Expected '('");
253
254 // declarator
255 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
256
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000257 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000258 if (TPR == TPResult::Error())
259 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000260
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000261 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000262 // '='
263 // [GNU] simple-asm-expr[opt] attributes[opt]
264 if (Tok.is(tok::equal) ||
265 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000266 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000267 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000268 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000269 }
270
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000271 PA.Revert();
272
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000273 assert(TPR == TPResult::True() || TPR == TPResult::False());
274 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000275}
276
Mike Stump1eb44332009-09-09 15:08:12 +0000277 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000278 ///
279 /// The context parameter states what context we're parsing right
280 /// now, which affects how this routine copes with the token
281 /// following the type-id. If the context is TypeIdInParens, we have
282 /// already parsed the '(' and we will cease lookahead when we hit
283 /// the corresponding ')'. If the context is
284 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
285 /// before this template argument, and will cease lookahead when we
286 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
287 /// and false for an expression. If during the disambiguation
288 /// process a parsing error is encountered, the function returns
289 /// true to let the declaration parsing code handle it.
290 ///
291 /// type-id:
292 /// type-specifier-seq abstract-declarator[opt]
293 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000294bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000296 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000297
298 // C++ 8.2p2:
299 // The ambiguity arising from the similarity between a function-style cast and
300 // a type-id can occur in different contexts. The ambiguity appears as a
301 // choice between a function-style cast expression and a declaration of a
302 // type. The resolution is that any construct that could possibly be a type-id
303 // in its syntactic context shall be considered a type-id.
304
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000305 TPResult TPR = isCXXDeclarationSpecifier();
306 if (TPR != TPResult::Ambiguous())
307 return TPR != TPResult::False(); // Returns true for TPResult::True() or
308 // TPResult::Error().
309
310 // FIXME: Add statistics about the number of ambiguous statements encountered
311 // and how they were resolved (number of declarations+number of expressions).
312
313 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
314 // We need tentative parsing...
315
316 TentativeParsingAction PA(*this);
317
318 // type-specifier-seq
319 if (Tok.is(tok::kw_typeof))
320 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000321 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000322 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000323
324 if (getLang().ObjC1 && Tok.is(tok::less))
325 TryParseProtocolQualifiers();
326 }
327
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000328 assert(Tok.is(tok::l_paren) && "Expected '('");
329
330 // declarator
331 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
332
333 // In case of an error, let the declaration parsing code handle it.
334 if (TPR == TPResult::Error())
335 TPR = TPResult::True();
336
337 if (TPR == TPResult::Ambiguous()) {
338 // We are supposed to be inside parens, so if after the abstract declarator
339 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000340 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000341 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000342 isAmbiguous = true;
343
Douglas Gregor8b642592009-02-10 00:53:15 +0000344 // We are supposed to be inside a template argument, so if after
345 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
346 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000347 } else if (Context == TypeIdAsTemplateArgument &&
348 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
349 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000350 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000351 isAmbiguous = true;
352
353 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000354 TPR = TPResult::False();
355 }
356
357 PA.Revert();
358
359 assert(TPR == TPResult::True() || TPR == TPResult::False());
360 return TPR == TPResult::True();
361}
362
Sean Huntbbd37c62009-11-21 08:43:09 +0000363/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
364/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
365/// performed that will simply return true if a [[ is seen. Currently C++ has no
366/// syntactical ambiguities from this check, but it may inhibit error recovery.
367/// If CheckClosing is true, a check is made for closing ]] brackets.
368///
369/// If given, After is set to the token after the attribute-specifier so that
370/// appropriate parsing decisions can be made; it is left untouched if false is
371/// returned.
372///
373/// FIXME: If an error is in the closing ]] brackets, the program assumes
374/// the absence of an attribute-specifier, which can cause very yucky errors
375/// to occur.
376///
377/// [C++0x] attribute-specifier:
378/// '[' '[' attribute-list ']' ']'
379///
380/// [C++0x] attribute-list:
381/// attribute[opt]
382/// attribute-list ',' attribute[opt]
383///
384/// [C++0x] attribute:
385/// attribute-token attribute-argument-clause[opt]
386///
387/// [C++0x] attribute-token:
388/// identifier
389/// attribute-scoped-token
390///
391/// [C++0x] attribute-scoped-token:
392/// attribute-namespace '::' identifier
393///
394/// [C++0x] attribute-namespace:
395/// identifier
396///
397/// [C++0x] attribute-argument-clause:
398/// '(' balanced-token-seq ')'
399///
400/// [C++0x] balanced-token-seq:
401/// balanced-token
402/// balanced-token-seq balanced-token
403///
404/// [C++0x] balanced-token:
405/// '(' balanced-token-seq ')'
406/// '[' balanced-token-seq ']'
407/// '{' balanced-token-seq '}'
408/// any token but '(', ')', '[', ']', '{', or '}'
409bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
410 tok::TokenKind *After) {
411 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
412 return false;
413
414 // No tentative parsing if we don't need to look for ]]
415 if (!CheckClosing && !getLang().ObjC1)
416 return true;
417
418 struct TentativeReverter {
419 TentativeParsingAction PA;
420
421 TentativeReverter (Parser& P)
422 : PA(P)
423 {}
424 ~TentativeReverter () {
425 PA.Revert();
426 }
427 } R(*this);
428
429 // Opening brackets were checked for above.
430 ConsumeBracket();
431 ConsumeBracket();
432
433 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
434 SkipUntil(tok::r_square, false);
435
436 if (Tok.isNot(tok::r_square))
437 return false;
438 ConsumeBracket();
439
440 if (After)
441 *After = Tok.getKind();
442
443 return true;
444}
445
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000446/// declarator:
447/// direct-declarator
448/// ptr-operator declarator
449///
450/// direct-declarator:
451/// declarator-id
452/// direct-declarator '(' parameter-declaration-clause ')'
453/// cv-qualifier-seq[opt] exception-specification[opt]
454/// direct-declarator '[' constant-expression[opt] ']'
455/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000456/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000457///
458/// abstract-declarator:
459/// ptr-operator abstract-declarator[opt]
460/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000461/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000462///
463/// direct-abstract-declarator:
464/// direct-abstract-declarator[opt]
465/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
466/// exception-specification[opt]
467/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
468/// '(' abstract-declarator ')'
469///
470/// ptr-operator:
471/// '*' cv-qualifier-seq[opt]
472/// '&'
473/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000474/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000475///
476/// cv-qualifier-seq:
477/// cv-qualifier cv-qualifier-seq[opt]
478///
479/// cv-qualifier:
480/// 'const'
481/// 'volatile'
482///
483/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000484/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000485///
486/// id-expression:
487/// unqualified-id
488/// qualified-id [TODO]
489///
490/// unqualified-id:
491/// identifier
492/// operator-function-id [TODO]
493/// conversion-function-id [TODO]
494/// '~' class-name [TODO]
495/// template-id [TODO]
496///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000497Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
498 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000499 // declarator:
500 // direct-declarator
501 // ptr-operator declarator
502
503 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000504 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000505 if (TryAnnotateCXXScopeToken(true))
506 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000507
Chris Lattner9af55002009-03-27 04:18:06 +0000508 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000509 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000510 // ptr-operator
511 ConsumeToken();
512 while (Tok.is(tok::kw_const) ||
513 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000514 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000515 ConsumeToken();
516 } else {
517 break;
518 }
519 }
520
521 // direct-declarator:
522 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000523 if (Tok.is(tok::ellipsis))
524 ConsumeToken();
525
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000526 if ((Tok.is(tok::identifier) ||
527 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
528 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000529 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000530 if (Tok.is(tok::annot_cxxscope))
531 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000532 ConsumeToken();
533 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000534 ConsumeParen();
535 if (mayBeAbstract &&
536 (Tok.is(tok::r_paren) || // 'int()' is a function.
537 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
538 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000539 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
540 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000541 TPResult TPR = TryParseFunctionDeclarator();
542 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000543 return TPR;
544 } else {
545 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000546 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000547 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000548 if (Tok.is(tok::kw___attribute) ||
549 Tok.is(tok::kw___declspec) ||
550 Tok.is(tok::kw___cdecl) ||
551 Tok.is(tok::kw___stdcall) ||
552 Tok.is(tok::kw___fastcall) ||
553 Tok.is(tok::kw___thiscall))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000554 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000555 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000556 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000557 return TPR;
558 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000559 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000560 ConsumeParen();
561 }
562 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000563 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000564 }
565
566 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000567 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000568
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000569 // abstract-declarator: ...
570 if (Tok.is(tok::ellipsis))
571 ConsumeToken();
572
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000573 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000574 // Check whether we have a function declarator or a possible ctor-style
575 // initializer that follows the declarator. Note that ctor-style
576 // initializers are not possible in contexts where abstract declarators
577 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000578 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000579 break;
580
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000581 // direct-declarator '(' parameter-declaration-clause ')'
582 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000583 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000584 TPR = TryParseFunctionDeclarator();
585 } else if (Tok.is(tok::l_square)) {
586 // direct-declarator '[' constant-expression[opt] ']'
587 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
588 TPR = TryParseBracketDeclarator();
589 } else {
590 break;
591 }
592
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000593 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000594 return TPR;
595 }
596
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000597 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000598}
599
Douglas Gregora61b3e72010-12-01 17:42:47 +0000600Parser::TPResult
601Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
602 switch (Kind) {
603 // Obviously starts an expression.
604 case tok::numeric_constant:
605 case tok::char_constant:
606 case tok::string_literal:
607 case tok::wide_string_literal:
608 case tok::l_square:
609 case tok::l_paren:
610 case tok::amp:
611 case tok::star:
612 case tok::plus:
613 case tok::plusplus:
614 case tok::minus:
615 case tok::minusminus:
616 case tok::tilde:
617 case tok::exclaim:
618 case tok::kw_sizeof:
619 case tok::kw___func__:
620 case tok::kw_const_cast:
621 case tok::kw_delete:
622 case tok::kw_dynamic_cast:
623 case tok::kw_false:
624 case tok::kw_new:
625 case tok::kw_operator:
626 case tok::kw_reinterpret_cast:
627 case tok::kw_static_cast:
628 case tok::kw_this:
629 case tok::kw_throw:
630 case tok::kw_true:
631 case tok::kw_typeid:
632 case tok::kw_alignof:
633 case tok::kw_noexcept:
634 case tok::kw_nullptr:
635 case tok::kw___null:
636 case tok::kw___alignof:
637 case tok::kw___builtin_choose_expr:
638 case tok::kw___builtin_offsetof:
639 case tok::kw___builtin_types_compatible_p:
640 case tok::kw___builtin_va_arg:
641 case tok::kw___imag:
642 case tok::kw___real:
643 case tok::kw___FUNCTION__:
644 case tok::kw___PRETTY_FUNCTION__:
645 case tok::kw___has_nothrow_assign:
646 case tok::kw___has_nothrow_copy:
647 case tok::kw___has_nothrow_constructor:
648 case tok::kw___has_trivial_assign:
649 case tok::kw___has_trivial_copy:
650 case tok::kw___has_trivial_constructor:
651 case tok::kw___has_trivial_destructor:
652 case tok::kw___has_virtual_destructor:
653 case tok::kw___is_abstract:
654 case tok::kw___is_base_of:
655 case tok::kw___is_class:
656 case tok::kw___is_empty:
657 case tok::kw___is_enum:
658 case tok::kw___is_pod:
659 case tok::kw___is_polymorphic:
660 case tok::kw___is_union:
661 case tok::kw___is_literal:
662 case tok::kw___uuidof:
663 return TPResult::True();
664
665 // Obviously starts a type-specifier-seq:
666 case tok::kw_char:
667 case tok::kw_const:
668 case tok::kw_double:
669 case tok::kw_enum:
670 case tok::kw_float:
671 case tok::kw_int:
672 case tok::kw_long:
673 case tok::kw_restrict:
674 case tok::kw_short:
675 case tok::kw_signed:
676 case tok::kw_struct:
677 case tok::kw_union:
678 case tok::kw_unsigned:
679 case tok::kw_void:
680 case tok::kw_volatile:
681 case tok::kw__Bool:
682 case tok::kw__Complex:
683 case tok::kw_class:
684 case tok::kw_typename:
685 case tok::kw_wchar_t:
686 case tok::kw_char16_t:
687 case tok::kw_char32_t:
688 case tok::kw_decltype:
689 case tok::kw_thread_local:
690 case tok::kw__Decimal32:
691 case tok::kw__Decimal64:
692 case tok::kw__Decimal128:
693 case tok::kw___thread:
694 case tok::kw_typeof:
695 case tok::kw___cdecl:
696 case tok::kw___stdcall:
697 case tok::kw___fastcall:
698 case tok::kw___thiscall:
699 case tok::kw___vector:
700 case tok::kw___pixel:
701 return TPResult::False();
702
703 default:
704 break;
705 }
706
707 return TPResult::Ambiguous();
708}
709
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000710/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
711/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
712/// be either a decl-specifier or a function-style cast, and TPResult::Error()
713/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000714///
715/// decl-specifier:
716/// storage-class-specifier
717/// type-specifier
718/// function-specifier
719/// 'friend'
720/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000721/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000722/// [GNU] attributes declaration-specifiers[opt]
723///
724/// storage-class-specifier:
725/// 'register'
726/// 'static'
727/// 'extern'
728/// 'mutable'
729/// 'auto'
730/// [GNU] '__thread'
731///
732/// function-specifier:
733/// 'inline'
734/// 'virtual'
735/// 'explicit'
736///
737/// typedef-name:
738/// identifier
739///
740/// type-specifier:
741/// simple-type-specifier
742/// class-specifier
743/// enum-specifier
744/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000745/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000746/// cv-qualifier
747///
748/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000749/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000750/// '::'[opt] nested-name-specifier 'template'
751/// simple-template-id [TODO]
752/// 'char'
753/// 'wchar_t'
754/// 'bool'
755/// 'short'
756/// 'int'
757/// 'long'
758/// 'signed'
759/// 'unsigned'
760/// 'float'
761/// 'double'
762/// 'void'
763/// [GNU] typeof-specifier
764/// [GNU] '_Complex'
765/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000766/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000767///
768/// type-name:
769/// class-name
770/// enum-name
771/// typedef-name
772///
773/// elaborated-type-specifier:
774/// class-key '::'[opt] nested-name-specifier[opt] identifier
775/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
776/// simple-template-id
777/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
778///
779/// enum-name:
780/// identifier
781///
782/// enum-specifier:
783/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
784/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
785///
786/// class-specifier:
787/// class-head '{' member-specification[opt] '}'
788///
789/// class-head:
790/// class-key identifier[opt] base-clause[opt]
791/// class-key nested-name-specifier identifier base-clause[opt]
792/// class-key nested-name-specifier[opt] simple-template-id
793/// base-clause[opt]
794///
795/// class-key:
796/// 'class'
797/// 'struct'
798/// 'union'
799///
800/// cv-qualifier:
801/// 'const'
802/// 'volatile'
803/// [GNU] restrict
804///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000805Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000806 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000807 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000808 // Check for need to substitute AltiVec __vector keyword
809 // for "vector" identifier.
810 if (TryAltiVecVectorToken())
811 return TPResult::True();
812 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000813 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000814 // Annotate typenames and C++ scope specifiers. If we get one, just
815 // recurse to handle whatever we get.
816 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000817 return TPResult::Error();
818 if (Tok.is(tok::identifier))
819 return TPResult::False();
820 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000821
Chris Lattner2ee9b402009-12-19 01:11:05 +0000822 case tok::coloncolon: { // ::foo::bar
823 const Token &Next = NextToken();
824 if (Next.is(tok::kw_new) || // ::new
825 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000826 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattnere5849262009-01-04 23:33:56 +0000828 // Annotate typenames and C++ scope specifiers. If we get one, just
829 // recurse to handle whatever we get.
830 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000831 return TPResult::Error();
832 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000833 }
834
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000835 // decl-specifier:
836 // storage-class-specifier
837 // type-specifier
838 // function-specifier
839 // 'friend'
840 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000841 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000842 case tok::kw_friend:
843 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000844 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000845 // storage-class-specifier
846 case tok::kw_register:
847 case tok::kw_static:
848 case tok::kw_extern:
849 case tok::kw_mutable:
850 case tok::kw_auto:
851 case tok::kw___thread:
852 // function-specifier
853 case tok::kw_inline:
854 case tok::kw_virtual:
855 case tok::kw_explicit:
856
857 // type-specifier:
858 // simple-type-specifier
859 // class-specifier
860 // enum-specifier
861 // elaborated-type-specifier
862 // typename-specifier
863 // cv-qualifier
864
865 // class-specifier
866 // elaborated-type-specifier
867 case tok::kw_class:
868 case tok::kw_struct:
869 case tok::kw_union:
870 // enum-specifier
871 case tok::kw_enum:
872 // cv-qualifier
873 case tok::kw_const:
874 case tok::kw_volatile:
875
876 // GNU
877 case tok::kw_restrict:
878 case tok::kw__Complex:
879 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000880 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Steve Naroff7ec56582009-01-06 17:40:00 +0000882 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000883 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000884 case tok::kw___cdecl:
885 case tok::kw___stdcall:
886 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000887 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000888 case tok::kw___w64:
889 case tok::kw___ptr64:
890 case tok::kw___forceinline:
891 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000892
893 // Borland
894 case tok::kw___pascal:
895 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000896
897 // AltiVec
898 case tok::kw___vector:
899 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000900
John McCall51e2a5d2010-04-30 03:11:01 +0000901 case tok::annot_template_id: {
902 TemplateIdAnnotation *TemplateId
903 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
904 if (TemplateId->Kind != TNK_Type_template)
905 return TPResult::False();
906 CXXScopeSpec SS;
907 AnnotateTemplateIdTokenAsType(&SS);
908 assert(Tok.is(tok::annot_typename));
909 goto case_typename;
910 }
911
John McCallae03cb52009-12-19 00:35:18 +0000912 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
913 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000914 if (TryAnnotateTypeOrScopeToken())
915 return TPResult::Error();
916 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000917 return TPResult::False();
918 // If that succeeded, fallthrough into the generic simple-type-id case.
919
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000920 // The ambiguity resides in a simple-type-specifier/typename-specifier
921 // followed by a '('. The '(' could either be the start of:
922 //
923 // direct-declarator:
924 // '(' declarator ')'
925 //
926 // direct-abstract-declarator:
927 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
928 // exception-specification[opt]
929 // '(' abstract-declarator ')'
930 //
931 // or part of a function-style cast expression:
932 //
933 // simple-type-specifier '(' expression-list[opt] ')'
934 //
935
936 // simple-type-specifier:
937
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000938 case tok::annot_typename:
939 case_typename:
940 // In Objective-C, we might have a protocol-qualified type.
941 if (getLang().ObjC1 && NextToken().is(tok::less)) {
942 // Tentatively parse the
943 TentativeParsingAction PA(*this);
944 ConsumeToken(); // The type token
945
946 TPResult TPR = TryParseProtocolQualifiers();
947 bool isFollowedByParen = Tok.is(tok::l_paren);
948
949 PA.Revert();
950
951 if (TPR == TPResult::Error())
952 return TPResult::Error();
953
954 if (isFollowedByParen)
955 return TPResult::Ambiguous();
956
957 return TPResult::True();
958 }
959
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000960 case tok::kw_char:
961 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000962 case tok::kw_char16_t:
963 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000964 case tok::kw_bool:
965 case tok::kw_short:
966 case tok::kw_int:
967 case tok::kw_long:
968 case tok::kw_signed:
969 case tok::kw_unsigned:
970 case tok::kw_float:
971 case tok::kw_double:
972 case tok::kw_void:
973 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000974 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000975
Douglas Gregor9497a732010-09-16 01:51:54 +0000976 if (isStartOfObjCClassMessageMissingOpenBracket())
977 return TPResult::False();
978
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000979 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000980
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000981 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000982 case tok::kw_typeof: {
983 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000984 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000985
986 TentativeParsingAction PA(*this);
987
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000988 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000989 bool isFollowedByParen = Tok.is(tok::l_paren);
990
991 PA.Revert();
992
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000993 if (TPR == TPResult::Error())
994 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000995
996 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000997 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000998
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000999 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001000 }
1001
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001002 // C++0x decltype support.
1003 case tok::kw_decltype:
1004 return TPResult::True();
1005
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001006 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001007 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001008 }
1009}
1010
1011/// [GNU] typeof-specifier:
1012/// 'typeof' '(' expressions ')'
1013/// 'typeof' '(' type-name ')'
1014///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001015Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001016 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1017 ConsumeToken();
1018
1019 assert(Tok.is(tok::l_paren) && "Expected '('");
1020 // Parse through the parens after 'typeof'.
1021 ConsumeParen();
1022 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001023 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001024
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001025 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001026}
1027
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001028/// [ObjC] protocol-qualifiers:
1029//// '<' identifier-list '>'
1030Parser::TPResult Parser::TryParseProtocolQualifiers() {
1031 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1032 ConsumeToken();
1033 do {
1034 if (Tok.isNot(tok::identifier))
1035 return TPResult::Error();
1036 ConsumeToken();
1037
1038 if (Tok.is(tok::comma)) {
1039 ConsumeToken();
1040 continue;
1041 }
1042
1043 if (Tok.is(tok::greater)) {
1044 ConsumeToken();
1045 return TPResult::Ambiguous();
1046 }
1047 } while (false);
1048
1049 return TPResult::Error();
1050}
1051
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001052Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1053 TPResult TPR = isCXXDeclarationSpecifier();
1054 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001055 return TPR;
1056
1057 if (Tok.is(tok::kw_typeof))
1058 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001059 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001060 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001061
1062 if (getLang().ObjC1 && Tok.is(tok::less))
1063 TryParseProtocolQualifiers();
1064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001066 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001067 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001068}
1069
1070/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1071/// a constructor-style initializer, when parsing declaration statements.
1072/// Returns true for function declarator and false for constructor-style
1073/// initializer.
1074/// If during the disambiguation process a parsing error is encountered,
1075/// the function returns true to let the declaration parsing code handle it.
1076///
1077/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1078/// exception-specification[opt]
1079///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001080bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001081
1082 // C++ 8.2p1:
1083 // The ambiguity arising from the similarity between a function-style cast and
1084 // a declaration mentioned in 6.8 can also occur in the context of a
1085 // declaration. In that context, the choice is between a function declaration
1086 // with a redundant set of parentheses around a parameter name and an object
1087 // declaration with a function-style cast as the initializer. Just as for the
1088 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1089 // that could possibly be a declaration a declaration.
1090
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001091 TentativeParsingAction PA(*this);
1092
1093 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001094 TPResult TPR = TryParseParameterDeclarationClause();
1095 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1096 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001097
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001098 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001099 PA.Revert();
1100
1101 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001102 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001103 return true;
1104
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001105 if (TPR == TPResult::Ambiguous()) {
1106 // Function declarator has precedence over constructor-style initializer.
1107 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001108 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001109 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1110 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001111 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001112 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001113
1114 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001115}
1116
1117/// parameter-declaration-clause:
1118/// parameter-declaration-list[opt] '...'[opt]
1119/// parameter-declaration-list ',' '...'
1120///
1121/// parameter-declaration-list:
1122/// parameter-declaration
1123/// parameter-declaration-list ',' parameter-declaration
1124///
1125/// parameter-declaration:
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001126/// decl-specifier-seq declarator attributes[opt]
1127/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1128/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1129/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1130/// '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001131///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001132Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001133
1134 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001135 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001136
1137 // parameter-declaration-list[opt] '...'[opt]
1138 // parameter-declaration-list ',' '...'
1139 //
1140 // parameter-declaration-list:
1141 // parameter-declaration
1142 // parameter-declaration-list ',' parameter-declaration
1143 //
1144 while (1) {
1145 // '...'[opt]
1146 if (Tok.is(tok::ellipsis)) {
1147 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001148 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001149 }
1150
John McCall7f040a92010-12-24 02:08:15 +00001151 ParsedAttributes attrs;
1152 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001153
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001154 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001155 TPResult TPR = TryParseDeclarationSpecifier();
1156 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001157 return TPR;
1158
1159 // declarator
1160 // abstract-declarator[opt]
1161 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001162 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001163 return TPR;
1164
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001165 // [GNU] attributes[opt]
1166 if (Tok.is(tok::kw___attribute))
1167 return TPResult::True();
1168
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001169 if (Tok.is(tok::equal)) {
1170 // '=' assignment-expression
1171 // Parse through assignment-expression.
Douglas Gregora8bc8c92010-12-23 22:44:42 +00001172 tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1173 if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001174 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001175 }
1176
1177 if (Tok.is(tok::ellipsis)) {
1178 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001179 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001180 }
1181
1182 if (Tok.isNot(tok::comma))
1183 break;
1184 ConsumeToken(); // the comma.
1185 }
1186
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001187 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001188}
1189
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001190/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1191/// parsing as a function declarator.
1192/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1193/// return TPResult::Ambiguous(), otherwise it will return either False() or
1194/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001195///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001196/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1197/// exception-specification[opt]
1198///
1199/// exception-specification:
1200/// 'throw' '(' type-id-list[opt] ')'
1201///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001202Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001203
1204 // The '(' is already parsed.
1205
1206 TPResult TPR = TryParseParameterDeclarationClause();
1207 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1208 TPR = TPResult::False();
1209
1210 if (TPR == TPResult::False() || TPR == TPResult::Error())
1211 return TPR;
1212
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001213 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001214 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001215 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001216
1217 // cv-qualifier-seq
1218 while (Tok.is(tok::kw_const) ||
1219 Tok.is(tok::kw_volatile) ||
1220 Tok.is(tok::kw_restrict) )
1221 ConsumeToken();
1222
1223 // exception-specification
1224 if (Tok.is(tok::kw_throw)) {
1225 ConsumeToken();
1226 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001227 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001228
1229 // Parse through the parens after 'throw'.
1230 ConsumeParen();
1231 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001232 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001233 }
1234
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001235 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001236}
1237
1238/// '[' constant-expression[opt] ']'
1239///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001240Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001241 ConsumeBracket();
1242 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001243 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001244
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001245 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001246}