blob: 6251a2f36754cbc1b324478abdc01803bf9f5591 [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"
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000017using namespace clang;
18
19/// isCXXDeclarationStatement - C++-specialized function that disambiguates
20/// between a declaration or an expression statement, when parsing function
21/// bodies. Returns true for declaration, false for expression.
22///
23/// declaration-statement:
24/// block-declaration
25///
26/// block-declaration:
27/// simple-declaration
28/// asm-definition
29/// namespace-alias-definition
30/// using-declaration
31/// using-directive
Anders Carlsson511d7ab2009-03-11 16:27:10 +000032/// [C++0x] static_assert-declaration
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000033///
34/// asm-definition:
35/// 'asm' '(' string-literal ')' ';'
36///
37/// namespace-alias-definition:
38/// 'namespace' identifier = qualified-namespace-specifier ';'
39///
40/// using-declaration:
41/// 'using' typename[opt] '::'[opt] nested-name-specifier
42/// unqualified-id ';'
43/// 'using' '::' unqualified-id ;
44///
45/// using-directive:
46/// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
47/// namespace-name ';'
48///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000049bool Parser::isCXXDeclarationStatement() {
50 switch (Tok.getKind()) {
51 // asm-definition
52 case tok::kw_asm:
53 // namespace-alias-definition
54 case tok::kw_namespace:
55 // using-declaration
56 // using-directive
57 case tok::kw_using:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000058 // static_assert-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000059 case tok::kw_static_assert:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000060 return true;
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000061 // simple-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000062 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000063 return isCXXSimpleDeclaration();
64 }
65}
66
67/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
68/// between a simple-declaration or an expression-statement.
69/// If during the disambiguation process a parsing error is encountered,
70/// the function returns true to let the declaration parsing code handle it.
71/// Returns false if the statement is disambiguated as expression.
72///
73/// simple-declaration:
74/// decl-specifier-seq init-declarator-list[opt] ';'
75///
76bool Parser::isCXXSimpleDeclaration() {
77 // C++ 6.8p1:
78 // There is an ambiguity in the grammar involving expression-statements and
79 // declarations: An expression-statement with a function-style explicit type
80 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
81 // from a declaration where the first declarator starts with a '('. In those
82 // cases the statement is a declaration. [Note: To disambiguate, the whole
83 // statement might have to be examined to determine if it is an
84 // expression-statement or a declaration].
85
86 // C++ 6.8p3:
87 // The disambiguation is purely syntactic; that is, the meaning of the names
88 // occurring in such a statement, beyond whether they are type-names or not,
89 // is not generally used in or changed by the disambiguation. Class
90 // templates are instantiated as necessary to determine if a qualified name
91 // is a type-name. Disambiguation precedes parsing, and a statement
92 // disambiguated as a declaration may be an ill-formed declaration.
93
94 // We don't have to parse all of the decl-specifier-seq part. There's only
95 // an ambiguity if the first decl-specifier is
96 // simple-type-specifier/typename-specifier followed by a '(', which may
97 // indicate a function-style cast expression.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +000098 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
99 // a case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000100
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000101 TPResult TPR = isCXXDeclarationSpecifier();
102 if (TPR != TPResult::Ambiguous())
103 return TPR != TPResult::False(); // Returns true for TPResult::True() or
104 // TPResult::Error().
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000105
106 // FIXME: Add statistics about the number of ambiguous statements encountered
107 // and how they were resolved (number of declarations+number of expressions).
108
109 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
110 // We need tentative parsing...
111
112 TentativeParsingAction PA(*this);
113
114 TPR = TryParseSimpleDeclaration();
115 SourceLocation TentativeParseLoc = Tok.getLocation();
116
117 PA.Revert();
118
119 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000120 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000121 return true;
122
123 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000124 if (TPR == TPResult::Ambiguous())
125 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000126
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000127 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000128 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000129}
130
131/// simple-declaration:
132/// decl-specifier-seq init-declarator-list[opt] ';'
133///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000134Parser::TPResult Parser::TryParseSimpleDeclaration() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000135 // We know that we have a simple-type-specifier/typename-specifier followed
136 // by a '('.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000137 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000138
139 if (Tok.is(tok::kw_typeof))
140 TryParseTypeofSpecifier();
141 else
142 ConsumeToken();
143
144 assert(Tok.is(tok::l_paren) && "Expected '('");
145
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000146 TPResult TPR = TryParseInitDeclaratorList();
147 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000148 return TPR;
149
150 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000151 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000152
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000153 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000154}
155
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000156/// init-declarator-list:
157/// init-declarator
158/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000159///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000160/// init-declarator:
161/// declarator initializer[opt]
162/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000163///
164/// initializer:
165/// '=' initializer-clause
166/// '(' expression-list ')'
167///
168/// initializer-clause:
169/// assignment-expression
170/// '{' initializer-list ','[opt] '}'
171/// '{' '}'
172///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000173Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000174 // GCC only examines the first declarator for disambiguation:
175 // i.e:
176 // int(x), ++x; // GCC regards it as ill-formed declaration.
177 //
178 // Comeau and MSVC will regard the above statement as correct expression.
179 // Clang examines all of the declarators and also regards the above statement
180 // as correct expression.
181
182 while (1) {
183 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000184 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
185 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000186 return TPR;
187
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000188 // [GNU] simple-asm-expr[opt] attributes[opt]
189 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000190 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000191
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000192 // initializer[opt]
193 if (Tok.is(tok::l_paren)) {
194 // Parse through the parens.
195 ConsumeParen();
196 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000197 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000198 } else if (Tok.is(tok::equal)) {
199 // MSVC won't examine the rest of declarators if '=' is encountered, it
200 // will conclude that it is a declaration.
201 // Comeau and Clang will examine the rest of declarators.
202 // Note that "int(x) = {0}, ++x;" will be interpreted as ill-formed
203 // expression.
204 //
205 // Parse through the initializer-clause.
206 SkipUntil(tok::comma, true/*StopAtSemi*/, true/*DontConsume*/);
207 }
208
209 if (Tok.isNot(tok::comma))
210 break;
211 ConsumeToken(); // the comma.
212 }
213
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000214 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000215}
216
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000217/// isCXXConditionDeclaration - Disambiguates between a declaration or an
218/// expression for a condition of a if/switch/while/for statement.
219/// If during the disambiguation process a parsing error is encountered,
220/// the function returns true to let the declaration parsing code handle it.
221///
222/// condition:
223/// expression
224/// type-specifier-seq declarator '=' assignment-expression
225/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
226/// '=' assignment-expression
227///
228bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000229 TPResult TPR = isCXXDeclarationSpecifier();
230 if (TPR != TPResult::Ambiguous())
231 return TPR != TPResult::False(); // Returns true for TPResult::True() or
232 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000233
234 // FIXME: Add statistics about the number of ambiguous statements encountered
235 // and how they were resolved (number of declarations+number of expressions).
236
237 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
238 // We need tentative parsing...
239
240 TentativeParsingAction PA(*this);
241
242 // type-specifier-seq
243 if (Tok.is(tok::kw_typeof))
244 TryParseTypeofSpecifier();
245 else
246 ConsumeToken();
247 assert(Tok.is(tok::l_paren) && "Expected '('");
248
249 // declarator
250 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
251
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000252 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000253 if (TPR == TPResult::Error())
254 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000255
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000256 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000257 // '='
258 // [GNU] simple-asm-expr[opt] attributes[opt]
259 if (Tok.is(tok::equal) ||
260 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000261 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000262 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000263 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000264 }
265
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000266 PA.Revert();
267
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000268 assert(TPR == TPResult::True() || TPR == TPResult::False());
269 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000270}
271
Mike Stump1eb44332009-09-09 15:08:12 +0000272 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000273 ///
274 /// The context parameter states what context we're parsing right
275 /// now, which affects how this routine copes with the token
276 /// following the type-id. If the context is TypeIdInParens, we have
277 /// already parsed the '(' and we will cease lookahead when we hit
278 /// the corresponding ')'. If the context is
279 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
280 /// before this template argument, and will cease lookahead when we
281 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
282 /// and false for an expression. If during the disambiguation
283 /// process a parsing error is encountered, the function returns
284 /// true to let the declaration parsing code handle it.
285 ///
286 /// type-id:
287 /// type-specifier-seq abstract-declarator[opt]
288 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000289bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000291 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000292
293 // C++ 8.2p2:
294 // The ambiguity arising from the similarity between a function-style cast and
295 // a type-id can occur in different contexts. The ambiguity appears as a
296 // choice between a function-style cast expression and a declaration of a
297 // type. The resolution is that any construct that could possibly be a type-id
298 // in its syntactic context shall be considered a type-id.
299
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000300 TPResult TPR = isCXXDeclarationSpecifier();
301 if (TPR != TPResult::Ambiguous())
302 return TPR != TPResult::False(); // Returns true for TPResult::True() or
303 // TPResult::Error().
304
305 // FIXME: Add statistics about the number of ambiguous statements encountered
306 // and how they were resolved (number of declarations+number of expressions).
307
308 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
309 // We need tentative parsing...
310
311 TentativeParsingAction PA(*this);
312
313 // type-specifier-seq
314 if (Tok.is(tok::kw_typeof))
315 TryParseTypeofSpecifier();
316 else
317 ConsumeToken();
318 assert(Tok.is(tok::l_paren) && "Expected '('");
319
320 // declarator
321 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
322
323 // In case of an error, let the declaration parsing code handle it.
324 if (TPR == TPResult::Error())
325 TPR = TPResult::True();
326
327 if (TPR == TPResult::Ambiguous()) {
328 // We are supposed to be inside parens, so if after the abstract declarator
329 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000330 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000331 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000332 isAmbiguous = true;
333
Douglas Gregor8b642592009-02-10 00:53:15 +0000334 // We are supposed to be inside a template argument, so if after
335 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
336 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000337 } else if (Context == TypeIdAsTemplateArgument &&
338 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
339 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000340 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000341 isAmbiguous = true;
342
343 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000344 TPR = TPResult::False();
345 }
346
347 PA.Revert();
348
349 assert(TPR == TPResult::True() || TPR == TPResult::False());
350 return TPR == TPResult::True();
351}
352
Sean Huntbbd37c62009-11-21 08:43:09 +0000353/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
354/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
355/// performed that will simply return true if a [[ is seen. Currently C++ has no
356/// syntactical ambiguities from this check, but it may inhibit error recovery.
357/// If CheckClosing is true, a check is made for closing ]] brackets.
358///
359/// If given, After is set to the token after the attribute-specifier so that
360/// appropriate parsing decisions can be made; it is left untouched if false is
361/// returned.
362///
363/// FIXME: If an error is in the closing ]] brackets, the program assumes
364/// the absence of an attribute-specifier, which can cause very yucky errors
365/// to occur.
366///
367/// [C++0x] attribute-specifier:
368/// '[' '[' attribute-list ']' ']'
369///
370/// [C++0x] attribute-list:
371/// attribute[opt]
372/// attribute-list ',' attribute[opt]
373///
374/// [C++0x] attribute:
375/// attribute-token attribute-argument-clause[opt]
376///
377/// [C++0x] attribute-token:
378/// identifier
379/// attribute-scoped-token
380///
381/// [C++0x] attribute-scoped-token:
382/// attribute-namespace '::' identifier
383///
384/// [C++0x] attribute-namespace:
385/// identifier
386///
387/// [C++0x] attribute-argument-clause:
388/// '(' balanced-token-seq ')'
389///
390/// [C++0x] balanced-token-seq:
391/// balanced-token
392/// balanced-token-seq balanced-token
393///
394/// [C++0x] balanced-token:
395/// '(' balanced-token-seq ')'
396/// '[' balanced-token-seq ']'
397/// '{' balanced-token-seq '}'
398/// any token but '(', ')', '[', ']', '{', or '}'
399bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
400 tok::TokenKind *After) {
401 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
402 return false;
403
404 // No tentative parsing if we don't need to look for ]]
405 if (!CheckClosing && !getLang().ObjC1)
406 return true;
407
408 struct TentativeReverter {
409 TentativeParsingAction PA;
410
411 TentativeReverter (Parser& P)
412 : PA(P)
413 {}
414 ~TentativeReverter () {
415 PA.Revert();
416 }
417 } R(*this);
418
419 // Opening brackets were checked for above.
420 ConsumeBracket();
421 ConsumeBracket();
422
423 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
424 SkipUntil(tok::r_square, false);
425
426 if (Tok.isNot(tok::r_square))
427 return false;
428 ConsumeBracket();
429
430 if (After)
431 *After = Tok.getKind();
432
433 return true;
434}
435
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000436/// declarator:
437/// direct-declarator
438/// ptr-operator declarator
439///
440/// direct-declarator:
441/// declarator-id
442/// direct-declarator '(' parameter-declaration-clause ')'
443/// cv-qualifier-seq[opt] exception-specification[opt]
444/// direct-declarator '[' constant-expression[opt] ']'
445/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000446/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000447///
448/// abstract-declarator:
449/// ptr-operator abstract-declarator[opt]
450/// direct-abstract-declarator
451///
452/// direct-abstract-declarator:
453/// direct-abstract-declarator[opt]
454/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
455/// exception-specification[opt]
456/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
457/// '(' abstract-declarator ')'
458///
459/// ptr-operator:
460/// '*' cv-qualifier-seq[opt]
461/// '&'
462/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000463/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000464///
465/// cv-qualifier-seq:
466/// cv-qualifier cv-qualifier-seq[opt]
467///
468/// cv-qualifier:
469/// 'const'
470/// 'volatile'
471///
472/// declarator-id:
473/// id-expression
474///
475/// id-expression:
476/// unqualified-id
477/// qualified-id [TODO]
478///
479/// unqualified-id:
480/// identifier
481/// operator-function-id [TODO]
482/// conversion-function-id [TODO]
483/// '~' class-name [TODO]
484/// template-id [TODO]
485///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000486Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
487 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000488 // declarator:
489 // direct-declarator
490 // ptr-operator declarator
491
492 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000493 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
Douglas Gregor495c35d2009-08-25 22:51:20 +0000494 TryAnnotateCXXScopeToken(true);
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000495
Chris Lattner9af55002009-03-27 04:18:06 +0000496 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000497 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000498 // ptr-operator
499 ConsumeToken();
500 while (Tok.is(tok::kw_const) ||
501 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000502 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000503 ConsumeToken();
504 } else {
505 break;
506 }
507 }
508
509 // direct-declarator:
510 // direct-abstract-declarator:
511
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000512 if ((Tok.is(tok::identifier) ||
513 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
514 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000515 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000516 if (Tok.is(tok::annot_cxxscope))
517 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000518 ConsumeToken();
519 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000520 ConsumeParen();
521 if (mayBeAbstract &&
522 (Tok.is(tok::r_paren) || // 'int()' is a function.
523 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
524 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000525 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
526 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000527 TPResult TPR = TryParseFunctionDeclarator();
528 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000529 return TPR;
530 } else {
531 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000532 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000533 // '(' abstract-declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000534 if (Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000535 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000536 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000537 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000538 return TPR;
539 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000540 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000541 ConsumeParen();
542 }
543 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000544 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000545 }
546
547 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000548 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000549
550 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000551 // Check whether we have a function declarator or a possible ctor-style
552 // initializer that follows the declarator. Note that ctor-style
553 // initializers are not possible in contexts where abstract declarators
554 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000555 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000556 break;
557
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000558 // direct-declarator '(' parameter-declaration-clause ')'
559 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000560 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000561 TPR = TryParseFunctionDeclarator();
562 } else if (Tok.is(tok::l_square)) {
563 // direct-declarator '[' constant-expression[opt] ']'
564 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
565 TPR = TryParseBracketDeclarator();
566 } else {
567 break;
568 }
569
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000570 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000571 return TPR;
572 }
573
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000574 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000575}
576
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000577/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
578/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
579/// be either a decl-specifier or a function-style cast, and TPResult::Error()
580/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000581///
582/// decl-specifier:
583/// storage-class-specifier
584/// type-specifier
585/// function-specifier
586/// 'friend'
587/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000588/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000589/// [GNU] attributes declaration-specifiers[opt]
590///
591/// storage-class-specifier:
592/// 'register'
593/// 'static'
594/// 'extern'
595/// 'mutable'
596/// 'auto'
597/// [GNU] '__thread'
598///
599/// function-specifier:
600/// 'inline'
601/// 'virtual'
602/// 'explicit'
603///
604/// typedef-name:
605/// identifier
606///
607/// type-specifier:
608/// simple-type-specifier
609/// class-specifier
610/// enum-specifier
611/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000612/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000613/// cv-qualifier
614///
615/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000616/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000617/// '::'[opt] nested-name-specifier 'template'
618/// simple-template-id [TODO]
619/// 'char'
620/// 'wchar_t'
621/// 'bool'
622/// 'short'
623/// 'int'
624/// 'long'
625/// 'signed'
626/// 'unsigned'
627/// 'float'
628/// 'double'
629/// 'void'
630/// [GNU] typeof-specifier
631/// [GNU] '_Complex'
632/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000633/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000634///
635/// type-name:
636/// class-name
637/// enum-name
638/// typedef-name
639///
640/// elaborated-type-specifier:
641/// class-key '::'[opt] nested-name-specifier[opt] identifier
642/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
643/// simple-template-id
644/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
645///
646/// enum-name:
647/// identifier
648///
649/// enum-specifier:
650/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
651/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
652///
653/// class-specifier:
654/// class-head '{' member-specification[opt] '}'
655///
656/// class-head:
657/// class-key identifier[opt] base-clause[opt]
658/// class-key nested-name-specifier identifier base-clause[opt]
659/// class-key nested-name-specifier[opt] simple-template-id
660/// base-clause[opt]
661///
662/// class-key:
663/// 'class'
664/// 'struct'
665/// 'union'
666///
667/// cv-qualifier:
668/// 'const'
669/// 'volatile'
670/// [GNU] restrict
671///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000672Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000673 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000674 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000675 // Check for need to substitute AltiVec __vector keyword
676 // for "vector" identifier.
677 if (TryAltiVecVectorToken())
678 return TPResult::True();
679 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000680 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000681 // Annotate typenames and C++ scope specifiers. If we get one, just
682 // recurse to handle whatever we get.
683 if (TryAnnotateTypeOrScopeToken())
684 return isCXXDeclarationSpecifier();
685 // Otherwise, not a typename.
686 return TPResult::False();
687
Chris Lattner2ee9b402009-12-19 01:11:05 +0000688 case tok::coloncolon: { // ::foo::bar
689 const Token &Next = NextToken();
690 if (Next.is(tok::kw_new) || // ::new
691 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000692 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattnere5849262009-01-04 23:33:56 +0000694 // Annotate typenames and C++ scope specifiers. If we get one, just
695 // recurse to handle whatever we get.
696 if (TryAnnotateTypeOrScopeToken())
697 return isCXXDeclarationSpecifier();
698 // Otherwise, not a typename.
699 return TPResult::False();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000700 }
701
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000702 // decl-specifier:
703 // storage-class-specifier
704 // type-specifier
705 // function-specifier
706 // 'friend'
707 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000708 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000709 case tok::kw_friend:
710 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000711 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000712 // storage-class-specifier
713 case tok::kw_register:
714 case tok::kw_static:
715 case tok::kw_extern:
716 case tok::kw_mutable:
717 case tok::kw_auto:
718 case tok::kw___thread:
719 // function-specifier
720 case tok::kw_inline:
721 case tok::kw_virtual:
722 case tok::kw_explicit:
723
724 // type-specifier:
725 // simple-type-specifier
726 // class-specifier
727 // enum-specifier
728 // elaborated-type-specifier
729 // typename-specifier
730 // cv-qualifier
731
732 // class-specifier
733 // elaborated-type-specifier
734 case tok::kw_class:
735 case tok::kw_struct:
736 case tok::kw_union:
737 // enum-specifier
738 case tok::kw_enum:
739 // cv-qualifier
740 case tok::kw_const:
741 case tok::kw_volatile:
742
743 // GNU
744 case tok::kw_restrict:
745 case tok::kw__Complex:
746 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000747 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Steve Naroff7ec56582009-01-06 17:40:00 +0000749 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000750 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000751 case tok::kw___cdecl:
752 case tok::kw___stdcall:
753 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000754 case tok::kw___w64:
755 case tok::kw___ptr64:
756 case tok::kw___forceinline:
757 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000758
759 // AltiVec
760 case tok::kw___vector:
761 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000762
John McCallae03cb52009-12-19 00:35:18 +0000763 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
764 // We've already annotated a scope; try to annotate a type.
765 if (!(TryAnnotateTypeOrScopeToken() && Tok.is(tok::annot_typename)))
766 return TPResult::False();
767 // If that succeeded, fallthrough into the generic simple-type-id case.
768
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000769 // The ambiguity resides in a simple-type-specifier/typename-specifier
770 // followed by a '('. The '(' could either be the start of:
771 //
772 // direct-declarator:
773 // '(' declarator ')'
774 //
775 // direct-abstract-declarator:
776 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
777 // exception-specification[opt]
778 // '(' abstract-declarator ')'
779 //
780 // or part of a function-style cast expression:
781 //
782 // simple-type-specifier '(' expression-list[opt] ')'
783 //
784
785 // simple-type-specifier:
786
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000787 case tok::kw_char:
788 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000789 case tok::kw_char16_t:
790 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000791 case tok::kw_bool:
792 case tok::kw_short:
793 case tok::kw_int:
794 case tok::kw_long:
795 case tok::kw_signed:
796 case tok::kw_unsigned:
797 case tok::kw_float:
798 case tok::kw_double:
799 case tok::kw_void:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000800 case tok::annot_typename:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000801 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000802 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000803
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000804 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000805
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000806 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000807 case tok::kw_typeof: {
808 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000809 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000810
811 TentativeParsingAction PA(*this);
812
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000813 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000814 bool isFollowedByParen = Tok.is(tok::l_paren);
815
816 PA.Revert();
817
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000818 if (TPR == TPResult::Error())
819 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000820
821 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000822 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000823
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000824 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000825 }
826
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000827 // C++0x decltype support.
828 case tok::kw_decltype:
829 return TPResult::True();
830
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000831 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000832 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000833 }
834}
835
836/// [GNU] typeof-specifier:
837/// 'typeof' '(' expressions ')'
838/// 'typeof' '(' type-name ')'
839///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000840Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000841 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
842 ConsumeToken();
843
844 assert(Tok.is(tok::l_paren) && "Expected '('");
845 // Parse through the parens after 'typeof'.
846 ConsumeParen();
847 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000848 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000849
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000850 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000851}
852
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000853Parser::TPResult Parser::TryParseDeclarationSpecifier() {
854 TPResult TPR = isCXXDeclarationSpecifier();
855 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000856 return TPR;
857
858 if (Tok.is(tok::kw_typeof))
859 TryParseTypeofSpecifier();
860 else
861 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000863 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000864 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000865}
866
867/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
868/// a constructor-style initializer, when parsing declaration statements.
869/// Returns true for function declarator and false for constructor-style
870/// initializer.
871/// If during the disambiguation process a parsing error is encountered,
872/// the function returns true to let the declaration parsing code handle it.
873///
874/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
875/// exception-specification[opt]
876///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000877bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000878
879 // C++ 8.2p1:
880 // The ambiguity arising from the similarity between a function-style cast and
881 // a declaration mentioned in 6.8 can also occur in the context of a
882 // declaration. In that context, the choice is between a function declaration
883 // with a redundant set of parentheses around a parameter name and an object
884 // declaration with a function-style cast as the initializer. Just as for the
885 // ambiguities mentioned in 6.8, the resolution is to consider any construct
886 // that could possibly be a declaration a declaration.
887
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000888 TentativeParsingAction PA(*this);
889
890 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000891 TPResult TPR = TryParseParameterDeclarationClause();
892 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
893 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000894
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000895 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000896 PA.Revert();
897
898 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000899 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000900 return true;
901
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000902 if (TPR == TPResult::Ambiguous()) {
903 // Function declarator has precedence over constructor-style initializer.
904 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000905 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +0000906 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
907 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000908 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000909 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000910
911 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000912}
913
914/// parameter-declaration-clause:
915/// parameter-declaration-list[opt] '...'[opt]
916/// parameter-declaration-list ',' '...'
917///
918/// parameter-declaration-list:
919/// parameter-declaration
920/// parameter-declaration-list ',' parameter-declaration
921///
922/// parameter-declaration:
923/// decl-specifier-seq declarator
924/// decl-specifier-seq declarator '=' assignment-expression
925/// decl-specifier-seq abstract-declarator[opt]
926/// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
927///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000928Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000929
930 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000931 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000932
933 // parameter-declaration-list[opt] '...'[opt]
934 // parameter-declaration-list ',' '...'
935 //
936 // parameter-declaration-list:
937 // parameter-declaration
938 // parameter-declaration-list ',' parameter-declaration
939 //
940 while (1) {
941 // '...'[opt]
942 if (Tok.is(tok::ellipsis)) {
943 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000944 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000945 }
946
947 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000948 TPResult TPR = TryParseDeclarationSpecifier();
949 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000950 return TPR;
951
952 // declarator
953 // abstract-declarator[opt]
954 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000955 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000956 return TPR;
957
958 if (Tok.is(tok::equal)) {
959 // '=' assignment-expression
960 // Parse through assignment-expression.
961 tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
962 if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000963 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000964 }
965
966 if (Tok.is(tok::ellipsis)) {
967 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000968 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000969 }
970
971 if (Tok.isNot(tok::comma))
972 break;
973 ConsumeToken(); // the comma.
974 }
975
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000976 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000977}
978
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000979/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
980/// parsing as a function declarator.
981/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
982/// return TPResult::Ambiguous(), otherwise it will return either False() or
983/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +0000984///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000985/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
986/// exception-specification[opt]
987///
988/// exception-specification:
989/// 'throw' '(' type-id-list[opt] ')'
990///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000991Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000992
993 // The '(' is already parsed.
994
995 TPResult TPR = TryParseParameterDeclarationClause();
996 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
997 TPR = TPResult::False();
998
999 if (TPR == TPResult::False() || TPR == TPResult::Error())
1000 return TPR;
1001
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001002 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001003 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001004 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001005
1006 // cv-qualifier-seq
1007 while (Tok.is(tok::kw_const) ||
1008 Tok.is(tok::kw_volatile) ||
1009 Tok.is(tok::kw_restrict) )
1010 ConsumeToken();
1011
1012 // exception-specification
1013 if (Tok.is(tok::kw_throw)) {
1014 ConsumeToken();
1015 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001016 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001017
1018 // Parse through the parens after 'throw'.
1019 ConsumeParen();
1020 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001021 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001022 }
1023
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001024 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001025}
1026
1027/// '[' constant-expression[opt] ']'
1028///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001029Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001030 ConsumeBracket();
1031 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001032 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001033
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001034 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001035}