blob: 81abdb884abdc92bbf7d3fcc7a2b7f814aaed053 [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);
114
115 TPR = TryParseSimpleDeclaration();
116 SourceLocation TentativeParseLoc = Tok.getLocation();
117
118 PA.Revert();
119
120 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000121 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000122 return true;
123
124 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000125 if (TPR == TPResult::Ambiguous())
126 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000127
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000128 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000129 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000130}
131
132/// simple-declaration:
133/// decl-specifier-seq init-declarator-list[opt] ';'
134///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000135Parser::TPResult Parser::TryParseSimpleDeclaration() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000136 // We know that we have a simple-type-specifier/typename-specifier followed
137 // by a '('.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000138 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000139
140 if (Tok.is(tok::kw_typeof))
141 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000142 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000143 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000144
145 if (getLang().ObjC1 && Tok.is(tok::less))
146 TryParseProtocolQualifiers();
147 }
148
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000149 assert(Tok.is(tok::l_paren) && "Expected '('");
150
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000151 TPResult TPR = TryParseInitDeclaratorList();
152 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000153 return TPR;
154
155 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000156 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000157
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000158 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000159}
160
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000161/// init-declarator-list:
162/// init-declarator
163/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000164///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000165/// init-declarator:
166/// declarator initializer[opt]
167/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000168///
169/// initializer:
170/// '=' initializer-clause
171/// '(' expression-list ')'
172///
173/// initializer-clause:
174/// assignment-expression
175/// '{' initializer-list ','[opt] '}'
176/// '{' '}'
177///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000178Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000179 while (1) {
180 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000181 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
182 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000183 return TPR;
184
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000185 // [GNU] simple-asm-expr[opt] attributes[opt]
186 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000187 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000188
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000189 // initializer[opt]
190 if (Tok.is(tok::l_paren)) {
191 // Parse through the parens.
192 ConsumeParen();
193 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000194 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000195 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-07-15 21:05:01 +0000196 // MSVC and g++ won't examine the rest of declarators if '=' is
197 // encountered; they just conclude that we have a declaration.
198 // EDG parses the initializer completely, which is the proper behavior
199 // for this case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000200 //
Douglas Gregor06b70802010-07-15 21:05:01 +0000201 // At present, Clang follows MSVC and g++, since the parser does not have
202 // the ability to parse an expression fully without recording the
203 // results of that parse.
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000204 // Also allow 'in' after on objective-c declaration as in:
205 // for (int (^b)(void) in array). Ideally this should be done in the
206 // context of parsing for-init-statement of a foreach statement only. But,
207 // in any other context 'in' is invalid after a declaration and parser
208 // issues the error regardless of outcome of this decision.
209 // FIXME. Change if above assumption does not hold.
Douglas Gregor06b70802010-07-15 21:05:01 +0000210 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000211 }
212
213 if (Tok.isNot(tok::comma))
214 break;
215 ConsumeToken(); // the comma.
216 }
217
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000218 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000219}
220
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000221/// isCXXConditionDeclaration - Disambiguates between a declaration or an
222/// expression for a condition of a if/switch/while/for statement.
223/// If during the disambiguation process a parsing error is encountered,
224/// the function returns true to let the declaration parsing code handle it.
225///
226/// condition:
227/// expression
228/// type-specifier-seq declarator '=' assignment-expression
229/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
230/// '=' assignment-expression
231///
232bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000233 TPResult TPR = isCXXDeclarationSpecifier();
234 if (TPR != TPResult::Ambiguous())
235 return TPR != TPResult::False(); // Returns true for TPResult::True() or
236 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000237
238 // FIXME: Add statistics about the number of ambiguous statements encountered
239 // and how they were resolved (number of declarations+number of expressions).
240
241 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
242 // We need tentative parsing...
243
244 TentativeParsingAction PA(*this);
245
246 // type-specifier-seq
247 if (Tok.is(tok::kw_typeof))
248 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000249 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000250 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000251
252 if (getLang().ObjC1 && Tok.is(tok::less))
253 TryParseProtocolQualifiers();
254 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000255 assert(Tok.is(tok::l_paren) && "Expected '('");
256
257 // declarator
258 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
259
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000260 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000261 if (TPR == TPResult::Error())
262 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000263
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000264 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000265 // '='
266 // [GNU] simple-asm-expr[opt] attributes[opt]
267 if (Tok.is(tok::equal) ||
268 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000269 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000270 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000271 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000272 }
273
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000274 PA.Revert();
275
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000276 assert(TPR == TPResult::True() || TPR == TPResult::False());
277 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000278}
279
Mike Stump1eb44332009-09-09 15:08:12 +0000280 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000281 ///
282 /// The context parameter states what context we're parsing right
283 /// now, which affects how this routine copes with the token
284 /// following the type-id. If the context is TypeIdInParens, we have
285 /// already parsed the '(' and we will cease lookahead when we hit
286 /// the corresponding ')'. If the context is
287 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
288 /// before this template argument, and will cease lookahead when we
289 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
290 /// and false for an expression. If during the disambiguation
291 /// process a parsing error is encountered, the function returns
292 /// true to let the declaration parsing code handle it.
293 ///
294 /// type-id:
295 /// type-specifier-seq abstract-declarator[opt]
296 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000297bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000299 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000300
301 // C++ 8.2p2:
302 // The ambiguity arising from the similarity between a function-style cast and
303 // a type-id can occur in different contexts. The ambiguity appears as a
304 // choice between a function-style cast expression and a declaration of a
305 // type. The resolution is that any construct that could possibly be a type-id
306 // in its syntactic context shall be considered a type-id.
307
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000308 TPResult TPR = isCXXDeclarationSpecifier();
309 if (TPR != TPResult::Ambiguous())
310 return TPR != TPResult::False(); // Returns true for TPResult::True() or
311 // TPResult::Error().
312
313 // FIXME: Add statistics about the number of ambiguous statements encountered
314 // and how they were resolved (number of declarations+number of expressions).
315
316 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
317 // We need tentative parsing...
318
319 TentativeParsingAction PA(*this);
320
321 // type-specifier-seq
322 if (Tok.is(tok::kw_typeof))
323 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000324 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000325 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000326
327 if (getLang().ObjC1 && Tok.is(tok::less))
328 TryParseProtocolQualifiers();
329 }
330
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000331 assert(Tok.is(tok::l_paren) && "Expected '('");
332
333 // declarator
334 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
335
336 // In case of an error, let the declaration parsing code handle it.
337 if (TPR == TPResult::Error())
338 TPR = TPResult::True();
339
340 if (TPR == TPResult::Ambiguous()) {
341 // We are supposed to be inside parens, so if after the abstract declarator
342 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000343 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000344 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000345 isAmbiguous = true;
346
Douglas Gregor8b642592009-02-10 00:53:15 +0000347 // We are supposed to be inside a template argument, so if after
348 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
349 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000350 } else if (Context == TypeIdAsTemplateArgument &&
351 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
352 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000353 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000354 isAmbiguous = true;
355
356 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000357 TPR = TPResult::False();
358 }
359
360 PA.Revert();
361
362 assert(TPR == TPResult::True() || TPR == TPResult::False());
363 return TPR == TPResult::True();
364}
365
Sean Huntbbd37c62009-11-21 08:43:09 +0000366/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
367/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
368/// performed that will simply return true if a [[ is seen. Currently C++ has no
369/// syntactical ambiguities from this check, but it may inhibit error recovery.
370/// If CheckClosing is true, a check is made for closing ]] brackets.
371///
372/// If given, After is set to the token after the attribute-specifier so that
373/// appropriate parsing decisions can be made; it is left untouched if false is
374/// returned.
375///
376/// FIXME: If an error is in the closing ]] brackets, the program assumes
377/// the absence of an attribute-specifier, which can cause very yucky errors
378/// to occur.
379///
380/// [C++0x] attribute-specifier:
381/// '[' '[' attribute-list ']' ']'
382///
383/// [C++0x] attribute-list:
384/// attribute[opt]
385/// attribute-list ',' attribute[opt]
386///
387/// [C++0x] attribute:
388/// attribute-token attribute-argument-clause[opt]
389///
390/// [C++0x] attribute-token:
391/// identifier
392/// attribute-scoped-token
393///
394/// [C++0x] attribute-scoped-token:
395/// attribute-namespace '::' identifier
396///
397/// [C++0x] attribute-namespace:
398/// identifier
399///
400/// [C++0x] attribute-argument-clause:
401/// '(' balanced-token-seq ')'
402///
403/// [C++0x] balanced-token-seq:
404/// balanced-token
405/// balanced-token-seq balanced-token
406///
407/// [C++0x] balanced-token:
408/// '(' balanced-token-seq ')'
409/// '[' balanced-token-seq ']'
410/// '{' balanced-token-seq '}'
411/// any token but '(', ')', '[', ']', '{', or '}'
412bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
413 tok::TokenKind *After) {
414 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
415 return false;
416
417 // No tentative parsing if we don't need to look for ]]
418 if (!CheckClosing && !getLang().ObjC1)
419 return true;
420
421 struct TentativeReverter {
422 TentativeParsingAction PA;
423
424 TentativeReverter (Parser& P)
425 : PA(P)
426 {}
427 ~TentativeReverter () {
428 PA.Revert();
429 }
430 } R(*this);
431
432 // Opening brackets were checked for above.
433 ConsumeBracket();
434 ConsumeBracket();
435
436 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
437 SkipUntil(tok::r_square, false);
438
439 if (Tok.isNot(tok::r_square))
440 return false;
441 ConsumeBracket();
442
443 if (After)
444 *After = Tok.getKind();
445
446 return true;
447}
448
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000449/// declarator:
450/// direct-declarator
451/// ptr-operator declarator
452///
453/// direct-declarator:
454/// declarator-id
455/// direct-declarator '(' parameter-declaration-clause ')'
456/// cv-qualifier-seq[opt] exception-specification[opt]
457/// direct-declarator '[' constant-expression[opt] ']'
458/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000459/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000460///
461/// abstract-declarator:
462/// ptr-operator abstract-declarator[opt]
463/// direct-abstract-declarator
464///
465/// direct-abstract-declarator:
466/// direct-abstract-declarator[opt]
467/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
468/// exception-specification[opt]
469/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
470/// '(' abstract-declarator ')'
471///
472/// ptr-operator:
473/// '*' cv-qualifier-seq[opt]
474/// '&'
475/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000476/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000477///
478/// cv-qualifier-seq:
479/// cv-qualifier cv-qualifier-seq[opt]
480///
481/// cv-qualifier:
482/// 'const'
483/// 'volatile'
484///
485/// declarator-id:
486/// id-expression
487///
488/// id-expression:
489/// unqualified-id
490/// qualified-id [TODO]
491///
492/// unqualified-id:
493/// identifier
494/// operator-function-id [TODO]
495/// conversion-function-id [TODO]
496/// '~' class-name [TODO]
497/// template-id [TODO]
498///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000499Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
500 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000501 // declarator:
502 // direct-declarator
503 // ptr-operator declarator
504
505 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000506 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000507 if (TryAnnotateCXXScopeToken(true))
508 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000509
Chris Lattner9af55002009-03-27 04:18:06 +0000510 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000511 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000512 // ptr-operator
513 ConsumeToken();
514 while (Tok.is(tok::kw_const) ||
515 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000516 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000517 ConsumeToken();
518 } else {
519 break;
520 }
521 }
522
523 // direct-declarator:
524 // direct-abstract-declarator:
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
569 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000570 // Check whether we have a function declarator or a possible ctor-style
571 // initializer that follows the declarator. Note that ctor-style
572 // initializers are not possible in contexts where abstract declarators
573 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000574 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000575 break;
576
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000577 // direct-declarator '(' parameter-declaration-clause ')'
578 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000579 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000580 TPR = TryParseFunctionDeclarator();
581 } else if (Tok.is(tok::l_square)) {
582 // direct-declarator '[' constant-expression[opt] ']'
583 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
584 TPR = TryParseBracketDeclarator();
585 } else {
586 break;
587 }
588
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000589 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000590 return TPR;
591 }
592
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000593 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000594}
595
Douglas Gregora61b3e72010-12-01 17:42:47 +0000596Parser::TPResult
597Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
598 switch (Kind) {
599 // Obviously starts an expression.
600 case tok::numeric_constant:
601 case tok::char_constant:
602 case tok::string_literal:
603 case tok::wide_string_literal:
604 case tok::l_square:
605 case tok::l_paren:
606 case tok::amp:
607 case tok::star:
608 case tok::plus:
609 case tok::plusplus:
610 case tok::minus:
611 case tok::minusminus:
612 case tok::tilde:
613 case tok::exclaim:
614 case tok::kw_sizeof:
615 case tok::kw___func__:
616 case tok::kw_const_cast:
617 case tok::kw_delete:
618 case tok::kw_dynamic_cast:
619 case tok::kw_false:
620 case tok::kw_new:
621 case tok::kw_operator:
622 case tok::kw_reinterpret_cast:
623 case tok::kw_static_cast:
624 case tok::kw_this:
625 case tok::kw_throw:
626 case tok::kw_true:
627 case tok::kw_typeid:
628 case tok::kw_alignof:
629 case tok::kw_noexcept:
630 case tok::kw_nullptr:
631 case tok::kw___null:
632 case tok::kw___alignof:
633 case tok::kw___builtin_choose_expr:
634 case tok::kw___builtin_offsetof:
635 case tok::kw___builtin_types_compatible_p:
636 case tok::kw___builtin_va_arg:
637 case tok::kw___imag:
638 case tok::kw___real:
639 case tok::kw___FUNCTION__:
640 case tok::kw___PRETTY_FUNCTION__:
641 case tok::kw___has_nothrow_assign:
642 case tok::kw___has_nothrow_copy:
643 case tok::kw___has_nothrow_constructor:
644 case tok::kw___has_trivial_assign:
645 case tok::kw___has_trivial_copy:
646 case tok::kw___has_trivial_constructor:
647 case tok::kw___has_trivial_destructor:
648 case tok::kw___has_virtual_destructor:
649 case tok::kw___is_abstract:
650 case tok::kw___is_base_of:
651 case tok::kw___is_class:
652 case tok::kw___is_empty:
653 case tok::kw___is_enum:
654 case tok::kw___is_pod:
655 case tok::kw___is_polymorphic:
656 case tok::kw___is_union:
657 case tok::kw___is_literal:
658 case tok::kw___uuidof:
659 return TPResult::True();
660
661 // Obviously starts a type-specifier-seq:
662 case tok::kw_char:
663 case tok::kw_const:
664 case tok::kw_double:
665 case tok::kw_enum:
666 case tok::kw_float:
667 case tok::kw_int:
668 case tok::kw_long:
669 case tok::kw_restrict:
670 case tok::kw_short:
671 case tok::kw_signed:
672 case tok::kw_struct:
673 case tok::kw_union:
674 case tok::kw_unsigned:
675 case tok::kw_void:
676 case tok::kw_volatile:
677 case tok::kw__Bool:
678 case tok::kw__Complex:
679 case tok::kw_class:
680 case tok::kw_typename:
681 case tok::kw_wchar_t:
682 case tok::kw_char16_t:
683 case tok::kw_char32_t:
684 case tok::kw_decltype:
685 case tok::kw_thread_local:
686 case tok::kw__Decimal32:
687 case tok::kw__Decimal64:
688 case tok::kw__Decimal128:
689 case tok::kw___thread:
690 case tok::kw_typeof:
691 case tok::kw___cdecl:
692 case tok::kw___stdcall:
693 case tok::kw___fastcall:
694 case tok::kw___thiscall:
695 case tok::kw___vector:
696 case tok::kw___pixel:
697 return TPResult::False();
698
699 default:
700 break;
701 }
702
703 return TPResult::Ambiguous();
704}
705
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000706/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
707/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
708/// be either a decl-specifier or a function-style cast, and TPResult::Error()
709/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000710///
711/// decl-specifier:
712/// storage-class-specifier
713/// type-specifier
714/// function-specifier
715/// 'friend'
716/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000717/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000718/// [GNU] attributes declaration-specifiers[opt]
719///
720/// storage-class-specifier:
721/// 'register'
722/// 'static'
723/// 'extern'
724/// 'mutable'
725/// 'auto'
726/// [GNU] '__thread'
727///
728/// function-specifier:
729/// 'inline'
730/// 'virtual'
731/// 'explicit'
732///
733/// typedef-name:
734/// identifier
735///
736/// type-specifier:
737/// simple-type-specifier
738/// class-specifier
739/// enum-specifier
740/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000741/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000742/// cv-qualifier
743///
744/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000745/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000746/// '::'[opt] nested-name-specifier 'template'
747/// simple-template-id [TODO]
748/// 'char'
749/// 'wchar_t'
750/// 'bool'
751/// 'short'
752/// 'int'
753/// 'long'
754/// 'signed'
755/// 'unsigned'
756/// 'float'
757/// 'double'
758/// 'void'
759/// [GNU] typeof-specifier
760/// [GNU] '_Complex'
761/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000762/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000763///
764/// type-name:
765/// class-name
766/// enum-name
767/// typedef-name
768///
769/// elaborated-type-specifier:
770/// class-key '::'[opt] nested-name-specifier[opt] identifier
771/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
772/// simple-template-id
773/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
774///
775/// enum-name:
776/// identifier
777///
778/// enum-specifier:
779/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
780/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
781///
782/// class-specifier:
783/// class-head '{' member-specification[opt] '}'
784///
785/// class-head:
786/// class-key identifier[opt] base-clause[opt]
787/// class-key nested-name-specifier identifier base-clause[opt]
788/// class-key nested-name-specifier[opt] simple-template-id
789/// base-clause[opt]
790///
791/// class-key:
792/// 'class'
793/// 'struct'
794/// 'union'
795///
796/// cv-qualifier:
797/// 'const'
798/// 'volatile'
799/// [GNU] restrict
800///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000801Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000802 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000803 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000804 // Check for need to substitute AltiVec __vector keyword
805 // for "vector" identifier.
806 if (TryAltiVecVectorToken())
807 return TPResult::True();
808 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000809 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000810 // Annotate typenames and C++ scope specifiers. If we get one, just
811 // recurse to handle whatever we get.
812 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000813 return TPResult::Error();
814 if (Tok.is(tok::identifier))
815 return TPResult::False();
816 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000817
Chris Lattner2ee9b402009-12-19 01:11:05 +0000818 case tok::coloncolon: { // ::foo::bar
819 const Token &Next = NextToken();
820 if (Next.is(tok::kw_new) || // ::new
821 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000822 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattnere5849262009-01-04 23:33:56 +0000824 // Annotate typenames and C++ scope specifiers. If we get one, just
825 // recurse to handle whatever we get.
826 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000827 return TPResult::Error();
828 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000829 }
830
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000831 // decl-specifier:
832 // storage-class-specifier
833 // type-specifier
834 // function-specifier
835 // 'friend'
836 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000837 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000838 case tok::kw_friend:
839 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000840 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000841 // storage-class-specifier
842 case tok::kw_register:
843 case tok::kw_static:
844 case tok::kw_extern:
845 case tok::kw_mutable:
846 case tok::kw_auto:
847 case tok::kw___thread:
848 // function-specifier
849 case tok::kw_inline:
850 case tok::kw_virtual:
851 case tok::kw_explicit:
852
853 // type-specifier:
854 // simple-type-specifier
855 // class-specifier
856 // enum-specifier
857 // elaborated-type-specifier
858 // typename-specifier
859 // cv-qualifier
860
861 // class-specifier
862 // elaborated-type-specifier
863 case tok::kw_class:
864 case tok::kw_struct:
865 case tok::kw_union:
866 // enum-specifier
867 case tok::kw_enum:
868 // cv-qualifier
869 case tok::kw_const:
870 case tok::kw_volatile:
871
872 // GNU
873 case tok::kw_restrict:
874 case tok::kw__Complex:
875 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000876 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Steve Naroff7ec56582009-01-06 17:40:00 +0000878 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000879 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000880 case tok::kw___cdecl:
881 case tok::kw___stdcall:
882 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000883 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000884 case tok::kw___w64:
885 case tok::kw___ptr64:
886 case tok::kw___forceinline:
887 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000888
889 // Borland
890 case tok::kw___pascal:
891 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000892
893 // AltiVec
894 case tok::kw___vector:
895 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000896
John McCall51e2a5d2010-04-30 03:11:01 +0000897 case tok::annot_template_id: {
898 TemplateIdAnnotation *TemplateId
899 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
900 if (TemplateId->Kind != TNK_Type_template)
901 return TPResult::False();
902 CXXScopeSpec SS;
903 AnnotateTemplateIdTokenAsType(&SS);
904 assert(Tok.is(tok::annot_typename));
905 goto case_typename;
906 }
907
John McCallae03cb52009-12-19 00:35:18 +0000908 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
909 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000910 if (TryAnnotateTypeOrScopeToken())
911 return TPResult::Error();
912 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000913 return TPResult::False();
914 // If that succeeded, fallthrough into the generic simple-type-id case.
915
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000916 // The ambiguity resides in a simple-type-specifier/typename-specifier
917 // followed by a '('. The '(' could either be the start of:
918 //
919 // direct-declarator:
920 // '(' declarator ')'
921 //
922 // direct-abstract-declarator:
923 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
924 // exception-specification[opt]
925 // '(' abstract-declarator ')'
926 //
927 // or part of a function-style cast expression:
928 //
929 // simple-type-specifier '(' expression-list[opt] ')'
930 //
931
932 // simple-type-specifier:
933
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000934 case tok::annot_typename:
935 case_typename:
936 // In Objective-C, we might have a protocol-qualified type.
937 if (getLang().ObjC1 && NextToken().is(tok::less)) {
938 // Tentatively parse the
939 TentativeParsingAction PA(*this);
940 ConsumeToken(); // The type token
941
942 TPResult TPR = TryParseProtocolQualifiers();
943 bool isFollowedByParen = Tok.is(tok::l_paren);
944
945 PA.Revert();
946
947 if (TPR == TPResult::Error())
948 return TPResult::Error();
949
950 if (isFollowedByParen)
951 return TPResult::Ambiguous();
952
953 return TPResult::True();
954 }
955
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000956 case tok::kw_char:
957 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000958 case tok::kw_char16_t:
959 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000960 case tok::kw_bool:
961 case tok::kw_short:
962 case tok::kw_int:
963 case tok::kw_long:
964 case tok::kw_signed:
965 case tok::kw_unsigned:
966 case tok::kw_float:
967 case tok::kw_double:
968 case tok::kw_void:
969 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000970 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000971
Douglas Gregor9497a732010-09-16 01:51:54 +0000972 if (isStartOfObjCClassMessageMissingOpenBracket())
973 return TPResult::False();
974
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000975 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000976
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000977 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000978 case tok::kw_typeof: {
979 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000980 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000981
982 TentativeParsingAction PA(*this);
983
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000984 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000985 bool isFollowedByParen = Tok.is(tok::l_paren);
986
987 PA.Revert();
988
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000989 if (TPR == TPResult::Error())
990 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000991
992 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000993 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000994
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000995 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000996 }
997
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000998 // C++0x decltype support.
999 case tok::kw_decltype:
1000 return TPResult::True();
1001
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001002 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001003 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001004 }
1005}
1006
1007/// [GNU] typeof-specifier:
1008/// 'typeof' '(' expressions ')'
1009/// 'typeof' '(' type-name ')'
1010///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001011Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001012 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1013 ConsumeToken();
1014
1015 assert(Tok.is(tok::l_paren) && "Expected '('");
1016 // Parse through the parens after 'typeof'.
1017 ConsumeParen();
1018 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001019 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001020
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001021 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001022}
1023
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001024/// [ObjC] protocol-qualifiers:
1025//// '<' identifier-list '>'
1026Parser::TPResult Parser::TryParseProtocolQualifiers() {
1027 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1028 ConsumeToken();
1029 do {
1030 if (Tok.isNot(tok::identifier))
1031 return TPResult::Error();
1032 ConsumeToken();
1033
1034 if (Tok.is(tok::comma)) {
1035 ConsumeToken();
1036 continue;
1037 }
1038
1039 if (Tok.is(tok::greater)) {
1040 ConsumeToken();
1041 return TPResult::Ambiguous();
1042 }
1043 } while (false);
1044
1045 return TPResult::Error();
1046}
1047
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001048Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1049 TPResult TPR = isCXXDeclarationSpecifier();
1050 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001051 return TPR;
1052
1053 if (Tok.is(tok::kw_typeof))
1054 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001055 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001056 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001057
1058 if (getLang().ObjC1 && Tok.is(tok::less))
1059 TryParseProtocolQualifiers();
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001062 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001063 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001064}
1065
1066/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1067/// a constructor-style initializer, when parsing declaration statements.
1068/// Returns true for function declarator and false for constructor-style
1069/// initializer.
1070/// If during the disambiguation process a parsing error is encountered,
1071/// the function returns true to let the declaration parsing code handle it.
1072///
1073/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1074/// exception-specification[opt]
1075///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001076bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001077
1078 // C++ 8.2p1:
1079 // The ambiguity arising from the similarity between a function-style cast and
1080 // a declaration mentioned in 6.8 can also occur in the context of a
1081 // declaration. In that context, the choice is between a function declaration
1082 // with a redundant set of parentheses around a parameter name and an object
1083 // declaration with a function-style cast as the initializer. Just as for the
1084 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1085 // that could possibly be a declaration a declaration.
1086
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001087 TentativeParsingAction PA(*this);
1088
1089 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001090 TPResult TPR = TryParseParameterDeclarationClause();
1091 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1092 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001093
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001094 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001095 PA.Revert();
1096
1097 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001098 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001099 return true;
1100
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001101 if (TPR == TPResult::Ambiguous()) {
1102 // Function declarator has precedence over constructor-style initializer.
1103 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001104 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001105 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1106 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001107 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001108 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001109
1110 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001111}
1112
1113/// parameter-declaration-clause:
1114/// parameter-declaration-list[opt] '...'[opt]
1115/// parameter-declaration-list ',' '...'
1116///
1117/// parameter-declaration-list:
1118/// parameter-declaration
1119/// parameter-declaration-list ',' parameter-declaration
1120///
1121/// parameter-declaration:
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001122/// decl-specifier-seq declarator attributes[opt]
1123/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1124/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1125/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1126/// '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001127///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001128Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001129
1130 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001131 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001132
1133 // parameter-declaration-list[opt] '...'[opt]
1134 // parameter-declaration-list ',' '...'
1135 //
1136 // parameter-declaration-list:
1137 // parameter-declaration
1138 // parameter-declaration-list ',' parameter-declaration
1139 //
1140 while (1) {
1141 // '...'[opt]
1142 if (Tok.is(tok::ellipsis)) {
1143 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001144 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001145 }
1146
Francois Pichet334d47e2010-10-11 12:59:39 +00001147 if (getLang().Microsoft && Tok.is(tok::l_square))
1148 ParseMicrosoftAttributes();
1149
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001150 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001151 TPResult TPR = TryParseDeclarationSpecifier();
1152 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001153 return TPR;
1154
1155 // declarator
1156 // abstract-declarator[opt]
1157 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001158 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001159 return TPR;
1160
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001161 // [GNU] attributes[opt]
1162 if (Tok.is(tok::kw___attribute))
1163 return TPResult::True();
1164
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001165 if (Tok.is(tok::equal)) {
1166 // '=' assignment-expression
1167 // Parse through assignment-expression.
1168 tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
1169 if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001170 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001171 }
1172
1173 if (Tok.is(tok::ellipsis)) {
1174 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001175 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001176 }
1177
1178 if (Tok.isNot(tok::comma))
1179 break;
1180 ConsumeToken(); // the comma.
1181 }
1182
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001183 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001184}
1185
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001186/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1187/// parsing as a function declarator.
1188/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1189/// return TPResult::Ambiguous(), otherwise it will return either False() or
1190/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001191///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001192/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1193/// exception-specification[opt]
1194///
1195/// exception-specification:
1196/// 'throw' '(' type-id-list[opt] ')'
1197///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001198Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001199
1200 // The '(' is already parsed.
1201
1202 TPResult TPR = TryParseParameterDeclarationClause();
1203 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1204 TPR = TPResult::False();
1205
1206 if (TPR == TPResult::False() || TPR == TPResult::Error())
1207 return TPR;
1208
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001209 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001210 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001211 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001212
1213 // cv-qualifier-seq
1214 while (Tok.is(tok::kw_const) ||
1215 Tok.is(tok::kw_volatile) ||
1216 Tok.is(tok::kw_restrict) )
1217 ConsumeToken();
1218
1219 // exception-specification
1220 if (Tok.is(tok::kw_throw)) {
1221 ConsumeToken();
1222 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001223 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001224
1225 // Parse through the parens after 'throw'.
1226 ConsumeParen();
1227 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001228 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001229 }
1230
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001231 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001232}
1233
1234/// '[' constant-expression[opt] ']'
1235///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001236Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001237 ConsumeBracket();
1238 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001239 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001240
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001241 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001242}