blob: 5e64e6162b73f7afc8a680de903c32d601170dc4 [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 McCall51e2a5d2010-04-30 03:11:01 +000017#include "clang/Parse/Template.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();
142 else
143 ConsumeToken();
144
145 assert(Tok.is(tok::l_paren) && "Expected '('");
146
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000147 TPResult TPR = TryParseInitDeclaratorList();
148 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000149 return TPR;
150
151 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000152 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000153
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000154 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000155}
156
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000157/// init-declarator-list:
158/// init-declarator
159/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000160///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000161/// init-declarator:
162/// declarator initializer[opt]
163/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000164///
165/// initializer:
166/// '=' initializer-clause
167/// '(' expression-list ')'
168///
169/// initializer-clause:
170/// assignment-expression
171/// '{' initializer-list ','[opt] '}'
172/// '{' '}'
173///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000174Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000175 // GCC only examines the first declarator for disambiguation:
176 // i.e:
177 // int(x), ++x; // GCC regards it as ill-formed declaration.
178 //
179 // Comeau and MSVC will regard the above statement as correct expression.
180 // Clang examines all of the declarators and also regards the above statement
181 // as correct expression.
182
183 while (1) {
184 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000185 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
186 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000187 return TPR;
188
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000189 // [GNU] simple-asm-expr[opt] attributes[opt]
190 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000191 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000192
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000193 // initializer[opt]
194 if (Tok.is(tok::l_paren)) {
195 // Parse through the parens.
196 ConsumeParen();
197 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000198 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000199 } else if (Tok.is(tok::equal)) {
200 // MSVC won't examine the rest of declarators if '=' is encountered, it
201 // will conclude that it is a declaration.
202 // Comeau and Clang will examine the rest of declarators.
203 // Note that "int(x) = {0}, ++x;" will be interpreted as ill-formed
204 // expression.
205 //
206 // Parse through the initializer-clause.
207 SkipUntil(tok::comma, true/*StopAtSemi*/, true/*DontConsume*/);
208 }
209
210 if (Tok.isNot(tok::comma))
211 break;
212 ConsumeToken(); // the comma.
213 }
214
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000215 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000216}
217
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000218/// isCXXConditionDeclaration - Disambiguates between a declaration or an
219/// expression for a condition of a if/switch/while/for statement.
220/// If during the disambiguation process a parsing error is encountered,
221/// the function returns true to let the declaration parsing code handle it.
222///
223/// condition:
224/// expression
225/// type-specifier-seq declarator '=' assignment-expression
226/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
227/// '=' assignment-expression
228///
229bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000230 TPResult TPR = isCXXDeclarationSpecifier();
231 if (TPR != TPResult::Ambiguous())
232 return TPR != TPResult::False(); // Returns true for TPResult::True() or
233 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000234
235 // FIXME: Add statistics about the number of ambiguous statements encountered
236 // and how they were resolved (number of declarations+number of expressions).
237
238 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
239 // We need tentative parsing...
240
241 TentativeParsingAction PA(*this);
242
243 // type-specifier-seq
244 if (Tok.is(tok::kw_typeof))
245 TryParseTypeofSpecifier();
246 else
247 ConsumeToken();
248 assert(Tok.is(tok::l_paren) && "Expected '('");
249
250 // declarator
251 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
252
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000253 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000254 if (TPR == TPResult::Error())
255 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000256
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000257 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000258 // '='
259 // [GNU] simple-asm-expr[opt] attributes[opt]
260 if (Tok.is(tok::equal) ||
261 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000262 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000263 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000264 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000265 }
266
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000267 PA.Revert();
268
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000269 assert(TPR == TPResult::True() || TPR == TPResult::False());
270 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000274 ///
275 /// The context parameter states what context we're parsing right
276 /// now, which affects how this routine copes with the token
277 /// following the type-id. If the context is TypeIdInParens, we have
278 /// already parsed the '(' and we will cease lookahead when we hit
279 /// the corresponding ')'. If the context is
280 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
281 /// before this template argument, and will cease lookahead when we
282 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
283 /// and false for an expression. If during the disambiguation
284 /// process a parsing error is encountered, the function returns
285 /// true to let the declaration parsing code handle it.
286 ///
287 /// type-id:
288 /// type-specifier-seq abstract-declarator[opt]
289 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000290bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000292 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000293
294 // C++ 8.2p2:
295 // The ambiguity arising from the similarity between a function-style cast and
296 // a type-id can occur in different contexts. The ambiguity appears as a
297 // choice between a function-style cast expression and a declaration of a
298 // type. The resolution is that any construct that could possibly be a type-id
299 // in its syntactic context shall be considered a type-id.
300
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000301 TPResult TPR = isCXXDeclarationSpecifier();
302 if (TPR != TPResult::Ambiguous())
303 return TPR != TPResult::False(); // Returns true for TPResult::True() or
304 // TPResult::Error().
305
306 // FIXME: Add statistics about the number of ambiguous statements encountered
307 // and how they were resolved (number of declarations+number of expressions).
308
309 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
310 // We need tentative parsing...
311
312 TentativeParsingAction PA(*this);
313
314 // type-specifier-seq
315 if (Tok.is(tok::kw_typeof))
316 TryParseTypeofSpecifier();
317 else
318 ConsumeToken();
319 assert(Tok.is(tok::l_paren) && "Expected '('");
320
321 // declarator
322 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
323
324 // In case of an error, let the declaration parsing code handle it.
325 if (TPR == TPResult::Error())
326 TPR = TPResult::True();
327
328 if (TPR == TPResult::Ambiguous()) {
329 // We are supposed to be inside parens, so if after the abstract declarator
330 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000331 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000332 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000333 isAmbiguous = true;
334
Douglas Gregor8b642592009-02-10 00:53:15 +0000335 // We are supposed to be inside a template argument, so if after
336 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
337 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000338 } else if (Context == TypeIdAsTemplateArgument &&
339 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
340 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000341 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000342 isAmbiguous = true;
343
344 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000345 TPR = TPResult::False();
346 }
347
348 PA.Revert();
349
350 assert(TPR == TPResult::True() || TPR == TPResult::False());
351 return TPR == TPResult::True();
352}
353
Sean Huntbbd37c62009-11-21 08:43:09 +0000354/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
355/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
356/// performed that will simply return true if a [[ is seen. Currently C++ has no
357/// syntactical ambiguities from this check, but it may inhibit error recovery.
358/// If CheckClosing is true, a check is made for closing ]] brackets.
359///
360/// If given, After is set to the token after the attribute-specifier so that
361/// appropriate parsing decisions can be made; it is left untouched if false is
362/// returned.
363///
364/// FIXME: If an error is in the closing ]] brackets, the program assumes
365/// the absence of an attribute-specifier, which can cause very yucky errors
366/// to occur.
367///
368/// [C++0x] attribute-specifier:
369/// '[' '[' attribute-list ']' ']'
370///
371/// [C++0x] attribute-list:
372/// attribute[opt]
373/// attribute-list ',' attribute[opt]
374///
375/// [C++0x] attribute:
376/// attribute-token attribute-argument-clause[opt]
377///
378/// [C++0x] attribute-token:
379/// identifier
380/// attribute-scoped-token
381///
382/// [C++0x] attribute-scoped-token:
383/// attribute-namespace '::' identifier
384///
385/// [C++0x] attribute-namespace:
386/// identifier
387///
388/// [C++0x] attribute-argument-clause:
389/// '(' balanced-token-seq ')'
390///
391/// [C++0x] balanced-token-seq:
392/// balanced-token
393/// balanced-token-seq balanced-token
394///
395/// [C++0x] balanced-token:
396/// '(' balanced-token-seq ')'
397/// '[' balanced-token-seq ']'
398/// '{' balanced-token-seq '}'
399/// any token but '(', ')', '[', ']', '{', or '}'
400bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
401 tok::TokenKind *After) {
402 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
403 return false;
404
405 // No tentative parsing if we don't need to look for ]]
406 if (!CheckClosing && !getLang().ObjC1)
407 return true;
408
409 struct TentativeReverter {
410 TentativeParsingAction PA;
411
412 TentativeReverter (Parser& P)
413 : PA(P)
414 {}
415 ~TentativeReverter () {
416 PA.Revert();
417 }
418 } R(*this);
419
420 // Opening brackets were checked for above.
421 ConsumeBracket();
422 ConsumeBracket();
423
424 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
425 SkipUntil(tok::r_square, false);
426
427 if (Tok.isNot(tok::r_square))
428 return false;
429 ConsumeBracket();
430
431 if (After)
432 *After = Tok.getKind();
433
434 return true;
435}
436
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000437/// declarator:
438/// direct-declarator
439/// ptr-operator declarator
440///
441/// direct-declarator:
442/// declarator-id
443/// direct-declarator '(' parameter-declaration-clause ')'
444/// cv-qualifier-seq[opt] exception-specification[opt]
445/// direct-declarator '[' constant-expression[opt] ']'
446/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000447/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000448///
449/// abstract-declarator:
450/// ptr-operator abstract-declarator[opt]
451/// direct-abstract-declarator
452///
453/// direct-abstract-declarator:
454/// direct-abstract-declarator[opt]
455/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
456/// exception-specification[opt]
457/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
458/// '(' abstract-declarator ')'
459///
460/// ptr-operator:
461/// '*' cv-qualifier-seq[opt]
462/// '&'
463/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000464/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000465///
466/// cv-qualifier-seq:
467/// cv-qualifier cv-qualifier-seq[opt]
468///
469/// cv-qualifier:
470/// 'const'
471/// 'volatile'
472///
473/// declarator-id:
474/// id-expression
475///
476/// id-expression:
477/// unqualified-id
478/// qualified-id [TODO]
479///
480/// unqualified-id:
481/// identifier
482/// operator-function-id [TODO]
483/// conversion-function-id [TODO]
484/// '~' class-name [TODO]
485/// template-id [TODO]
486///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000487Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
488 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000489 // declarator:
490 // direct-declarator
491 // ptr-operator declarator
492
493 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000494 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000495 if (TryAnnotateCXXScopeToken(true))
496 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000497
Chris Lattner9af55002009-03-27 04:18:06 +0000498 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000499 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000500 // ptr-operator
501 ConsumeToken();
502 while (Tok.is(tok::kw_const) ||
503 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000504 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000505 ConsumeToken();
506 } else {
507 break;
508 }
509 }
510
511 // direct-declarator:
512 // direct-abstract-declarator:
513
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000514 if ((Tok.is(tok::identifier) ||
515 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
516 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000517 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000518 if (Tok.is(tok::annot_cxxscope))
519 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000520 ConsumeToken();
521 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000522 ConsumeParen();
523 if (mayBeAbstract &&
524 (Tok.is(tok::r_paren) || // 'int()' is a function.
525 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
526 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000527 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
528 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000529 TPResult TPR = TryParseFunctionDeclarator();
530 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000531 return TPR;
532 } else {
533 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000534 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000535 // '(' abstract-declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000536 if (Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000537 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000538 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000539 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000540 return TPR;
541 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000542 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000543 ConsumeParen();
544 }
545 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000546 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000547 }
548
549 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000550 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000551
552 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000553 // Check whether we have a function declarator or a possible ctor-style
554 // initializer that follows the declarator. Note that ctor-style
555 // initializers are not possible in contexts where abstract declarators
556 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000557 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000558 break;
559
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000560 // direct-declarator '(' parameter-declaration-clause ')'
561 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000562 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000563 TPR = TryParseFunctionDeclarator();
564 } else if (Tok.is(tok::l_square)) {
565 // direct-declarator '[' constant-expression[opt] ']'
566 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
567 TPR = TryParseBracketDeclarator();
568 } else {
569 break;
570 }
571
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000572 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000573 return TPR;
574 }
575
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000576 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000577}
578
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000579/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
580/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
581/// be either a decl-specifier or a function-style cast, and TPResult::Error()
582/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000583///
584/// decl-specifier:
585/// storage-class-specifier
586/// type-specifier
587/// function-specifier
588/// 'friend'
589/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000590/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000591/// [GNU] attributes declaration-specifiers[opt]
592///
593/// storage-class-specifier:
594/// 'register'
595/// 'static'
596/// 'extern'
597/// 'mutable'
598/// 'auto'
599/// [GNU] '__thread'
600///
601/// function-specifier:
602/// 'inline'
603/// 'virtual'
604/// 'explicit'
605///
606/// typedef-name:
607/// identifier
608///
609/// type-specifier:
610/// simple-type-specifier
611/// class-specifier
612/// enum-specifier
613/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000614/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000615/// cv-qualifier
616///
617/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000618/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000619/// '::'[opt] nested-name-specifier 'template'
620/// simple-template-id [TODO]
621/// 'char'
622/// 'wchar_t'
623/// 'bool'
624/// 'short'
625/// 'int'
626/// 'long'
627/// 'signed'
628/// 'unsigned'
629/// 'float'
630/// 'double'
631/// 'void'
632/// [GNU] typeof-specifier
633/// [GNU] '_Complex'
634/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000635/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000636///
637/// type-name:
638/// class-name
639/// enum-name
640/// typedef-name
641///
642/// elaborated-type-specifier:
643/// class-key '::'[opt] nested-name-specifier[opt] identifier
644/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
645/// simple-template-id
646/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
647///
648/// enum-name:
649/// identifier
650///
651/// enum-specifier:
652/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
653/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
654///
655/// class-specifier:
656/// class-head '{' member-specification[opt] '}'
657///
658/// class-head:
659/// class-key identifier[opt] base-clause[opt]
660/// class-key nested-name-specifier identifier base-clause[opt]
661/// class-key nested-name-specifier[opt] simple-template-id
662/// base-clause[opt]
663///
664/// class-key:
665/// 'class'
666/// 'struct'
667/// 'union'
668///
669/// cv-qualifier:
670/// 'const'
671/// 'volatile'
672/// [GNU] restrict
673///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000674Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000675 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000676 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000677 // Check for need to substitute AltiVec __vector keyword
678 // for "vector" identifier.
679 if (TryAltiVecVectorToken())
680 return TPResult::True();
681 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000682 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000683 // Annotate typenames and C++ scope specifiers. If we get one, just
684 // recurse to handle whatever we get.
685 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000686 return TPResult::Error();
687 if (Tok.is(tok::identifier))
688 return TPResult::False();
689 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000690
Chris Lattner2ee9b402009-12-19 01:11:05 +0000691 case tok::coloncolon: { // ::foo::bar
692 const Token &Next = NextToken();
693 if (Next.is(tok::kw_new) || // ::new
694 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000695 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattnere5849262009-01-04 23:33:56 +0000697 // Annotate typenames and C++ scope specifiers. If we get one, just
698 // recurse to handle whatever we get.
699 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000700 return TPResult::Error();
701 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000702 }
703
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000704 // decl-specifier:
705 // storage-class-specifier
706 // type-specifier
707 // function-specifier
708 // 'friend'
709 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000710 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000711 case tok::kw_friend:
712 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000713 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000714 // storage-class-specifier
715 case tok::kw_register:
716 case tok::kw_static:
717 case tok::kw_extern:
718 case tok::kw_mutable:
719 case tok::kw_auto:
720 case tok::kw___thread:
721 // function-specifier
722 case tok::kw_inline:
723 case tok::kw_virtual:
724 case tok::kw_explicit:
725
726 // type-specifier:
727 // simple-type-specifier
728 // class-specifier
729 // enum-specifier
730 // elaborated-type-specifier
731 // typename-specifier
732 // cv-qualifier
733
734 // class-specifier
735 // elaborated-type-specifier
736 case tok::kw_class:
737 case tok::kw_struct:
738 case tok::kw_union:
739 // enum-specifier
740 case tok::kw_enum:
741 // cv-qualifier
742 case tok::kw_const:
743 case tok::kw_volatile:
744
745 // GNU
746 case tok::kw_restrict:
747 case tok::kw__Complex:
748 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000749 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Steve Naroff7ec56582009-01-06 17:40:00 +0000751 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000752 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000753 case tok::kw___cdecl:
754 case tok::kw___stdcall:
755 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000756 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000757 case tok::kw___w64:
758 case tok::kw___ptr64:
759 case tok::kw___forceinline:
760 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000761
762 // AltiVec
763 case tok::kw___vector:
764 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000765
John McCall51e2a5d2010-04-30 03:11:01 +0000766 case tok::annot_template_id: {
767 TemplateIdAnnotation *TemplateId
768 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
769 if (TemplateId->Kind != TNK_Type_template)
770 return TPResult::False();
771 CXXScopeSpec SS;
772 AnnotateTemplateIdTokenAsType(&SS);
773 assert(Tok.is(tok::annot_typename));
774 goto case_typename;
775 }
776
John McCallae03cb52009-12-19 00:35:18 +0000777 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
778 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000779 if (TryAnnotateTypeOrScopeToken())
780 return TPResult::Error();
781 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000782 return TPResult::False();
783 // If that succeeded, fallthrough into the generic simple-type-id case.
784
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000785 // The ambiguity resides in a simple-type-specifier/typename-specifier
786 // followed by a '('. The '(' could either be the start of:
787 //
788 // direct-declarator:
789 // '(' declarator ')'
790 //
791 // direct-abstract-declarator:
792 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
793 // exception-specification[opt]
794 // '(' abstract-declarator ')'
795 //
796 // or part of a function-style cast expression:
797 //
798 // simple-type-specifier '(' expression-list[opt] ')'
799 //
800
801 // simple-type-specifier:
802
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000803 case tok::kw_char:
804 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000805 case tok::kw_char16_t:
806 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000807 case tok::kw_bool:
808 case tok::kw_short:
809 case tok::kw_int:
810 case tok::kw_long:
811 case tok::kw_signed:
812 case tok::kw_unsigned:
813 case tok::kw_float:
814 case tok::kw_double:
815 case tok::kw_void:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000816 case tok::annot_typename:
John McCall51e2a5d2010-04-30 03:11:01 +0000817 case_typename:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000818 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000819 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000820
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000821 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000822
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000823 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000824 case tok::kw_typeof: {
825 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000826 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000827
828 TentativeParsingAction PA(*this);
829
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000830 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000831 bool isFollowedByParen = Tok.is(tok::l_paren);
832
833 PA.Revert();
834
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000835 if (TPR == TPResult::Error())
836 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000837
838 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000839 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000840
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000841 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000842 }
843
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000844 // C++0x decltype support.
845 case tok::kw_decltype:
846 return TPResult::True();
847
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000848 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000849 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000850 }
851}
852
853/// [GNU] typeof-specifier:
854/// 'typeof' '(' expressions ')'
855/// 'typeof' '(' type-name ')'
856///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000857Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000858 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
859 ConsumeToken();
860
861 assert(Tok.is(tok::l_paren) && "Expected '('");
862 // Parse through the parens after 'typeof'.
863 ConsumeParen();
864 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000865 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000866
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000867 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000868}
869
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000870Parser::TPResult Parser::TryParseDeclarationSpecifier() {
871 TPResult TPR = isCXXDeclarationSpecifier();
872 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000873 return TPR;
874
875 if (Tok.is(tok::kw_typeof))
876 TryParseTypeofSpecifier();
877 else
878 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000880 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000881 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000882}
883
884/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
885/// a constructor-style initializer, when parsing declaration statements.
886/// Returns true for function declarator and false for constructor-style
887/// initializer.
888/// If during the disambiguation process a parsing error is encountered,
889/// the function returns true to let the declaration parsing code handle it.
890///
891/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
892/// exception-specification[opt]
893///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000894bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000895
896 // C++ 8.2p1:
897 // The ambiguity arising from the similarity between a function-style cast and
898 // a declaration mentioned in 6.8 can also occur in the context of a
899 // declaration. In that context, the choice is between a function declaration
900 // with a redundant set of parentheses around a parameter name and an object
901 // declaration with a function-style cast as the initializer. Just as for the
902 // ambiguities mentioned in 6.8, the resolution is to consider any construct
903 // that could possibly be a declaration a declaration.
904
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000905 TentativeParsingAction PA(*this);
906
907 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000908 TPResult TPR = TryParseParameterDeclarationClause();
909 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
910 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000911
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000912 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000913 PA.Revert();
914
915 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000916 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000917 return true;
918
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000919 if (TPR == TPResult::Ambiguous()) {
920 // Function declarator has precedence over constructor-style initializer.
921 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000922 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +0000923 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
924 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000925 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000926 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000927
928 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000929}
930
931/// parameter-declaration-clause:
932/// parameter-declaration-list[opt] '...'[opt]
933/// parameter-declaration-list ',' '...'
934///
935/// parameter-declaration-list:
936/// parameter-declaration
937/// parameter-declaration-list ',' parameter-declaration
938///
939/// parameter-declaration:
940/// decl-specifier-seq declarator
941/// decl-specifier-seq declarator '=' assignment-expression
942/// decl-specifier-seq abstract-declarator[opt]
943/// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
944///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000945Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000946
947 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000948 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000949
950 // parameter-declaration-list[opt] '...'[opt]
951 // parameter-declaration-list ',' '...'
952 //
953 // parameter-declaration-list:
954 // parameter-declaration
955 // parameter-declaration-list ',' parameter-declaration
956 //
957 while (1) {
958 // '...'[opt]
959 if (Tok.is(tok::ellipsis)) {
960 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000961 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000962 }
963
964 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000965 TPResult TPR = TryParseDeclarationSpecifier();
966 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000967 return TPR;
968
969 // declarator
970 // abstract-declarator[opt]
971 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000972 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000973 return TPR;
974
975 if (Tok.is(tok::equal)) {
976 // '=' assignment-expression
977 // Parse through assignment-expression.
978 tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
979 if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000980 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000981 }
982
983 if (Tok.is(tok::ellipsis)) {
984 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000985 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000986 }
987
988 if (Tok.isNot(tok::comma))
989 break;
990 ConsumeToken(); // the comma.
991 }
992
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000993 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000994}
995
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000996/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
997/// parsing as a function declarator.
998/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
999/// return TPResult::Ambiguous(), otherwise it will return either False() or
1000/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001001///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001002/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1003/// exception-specification[opt]
1004///
1005/// exception-specification:
1006/// 'throw' '(' type-id-list[opt] ')'
1007///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001008Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001009
1010 // The '(' is already parsed.
1011
1012 TPResult TPR = TryParseParameterDeclarationClause();
1013 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1014 TPR = TPResult::False();
1015
1016 if (TPR == TPResult::False() || TPR == TPResult::Error())
1017 return TPR;
1018
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001019 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001020 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 // cv-qualifier-seq
1024 while (Tok.is(tok::kw_const) ||
1025 Tok.is(tok::kw_volatile) ||
1026 Tok.is(tok::kw_restrict) )
1027 ConsumeToken();
1028
1029 // exception-specification
1030 if (Tok.is(tok::kw_throw)) {
1031 ConsumeToken();
1032 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001033 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001034
1035 // Parse through the parens after 'throw'.
1036 ConsumeParen();
1037 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001038 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001039 }
1040
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001041 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001042}
1043
1044/// '[' constant-expression[opt] ']'
1045///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001046Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001047 ConsumeBracket();
1048 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001049 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001050
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001051 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001052}