blob: c22d99fa9d38aa6a2a622644bcc70a3f418930b1 [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();
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 while (1) {
176 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000177 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
178 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000179 return TPR;
180
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000181 // [GNU] simple-asm-expr[opt] attributes[opt]
182 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000183 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000184
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000185 // initializer[opt]
186 if (Tok.is(tok::l_paren)) {
187 // Parse through the parens.
188 ConsumeParen();
189 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000190 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000191 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-07-15 21:05:01 +0000192 // MSVC and g++ won't examine the rest of declarators if '=' is
193 // encountered; they just conclude that we have a declaration.
194 // EDG parses the initializer completely, which is the proper behavior
195 // for this case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000196 //
Douglas Gregor06b70802010-07-15 21:05:01 +0000197 // At present, Clang follows MSVC and g++, since the parser does not have
198 // the ability to parse an expression fully without recording the
199 // results of that parse.
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000200 // Also allow 'in' after on objective-c declaration as in:
201 // for (int (^b)(void) in array). Ideally this should be done in the
202 // context of parsing for-init-statement of a foreach statement only. But,
203 // in any other context 'in' is invalid after a declaration and parser
204 // issues the error regardless of outcome of this decision.
205 // FIXME. Change if above assumption does not hold.
Douglas Gregor06b70802010-07-15 21:05:01 +0000206 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000207 }
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))
John McCall9ba61662010-02-26 08:45:28 +0000494 if (TryAnnotateCXXScopeToken(true))
495 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000496
Chris Lattner9af55002009-03-27 04:18:06 +0000497 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000498 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000499 // ptr-operator
500 ConsumeToken();
501 while (Tok.is(tok::kw_const) ||
502 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000503 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000504 ConsumeToken();
505 } else {
506 break;
507 }
508 }
509
510 // direct-declarator:
511 // direct-abstract-declarator:
512
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000513 if ((Tok.is(tok::identifier) ||
514 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
515 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000516 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000517 if (Tok.is(tok::annot_cxxscope))
518 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000519 ConsumeToken();
520 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000521 ConsumeParen();
522 if (mayBeAbstract &&
523 (Tok.is(tok::r_paren) || // 'int()' is a function.
524 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
525 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000526 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
527 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000528 TPResult TPR = TryParseFunctionDeclarator();
529 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000530 return TPR;
531 } else {
532 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000533 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000534 // '(' abstract-declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000535 if (Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000536 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000537 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000538 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000539 return TPR;
540 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000541 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000542 ConsumeParen();
543 }
544 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000545 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000546 }
547
548 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000549 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000550
551 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000552 // Check whether we have a function declarator or a possible ctor-style
553 // initializer that follows the declarator. Note that ctor-style
554 // initializers are not possible in contexts where abstract declarators
555 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000556 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000557 break;
558
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000559 // direct-declarator '(' parameter-declaration-clause ')'
560 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000561 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000562 TPR = TryParseFunctionDeclarator();
563 } else if (Tok.is(tok::l_square)) {
564 // direct-declarator '[' constant-expression[opt] ']'
565 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
566 TPR = TryParseBracketDeclarator();
567 } else {
568 break;
569 }
570
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000571 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000572 return TPR;
573 }
574
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000575 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000576}
577
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000578/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
579/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
580/// be either a decl-specifier or a function-style cast, and TPResult::Error()
581/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000582///
583/// decl-specifier:
584/// storage-class-specifier
585/// type-specifier
586/// function-specifier
587/// 'friend'
588/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000589/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000590/// [GNU] attributes declaration-specifiers[opt]
591///
592/// storage-class-specifier:
593/// 'register'
594/// 'static'
595/// 'extern'
596/// 'mutable'
597/// 'auto'
598/// [GNU] '__thread'
599///
600/// function-specifier:
601/// 'inline'
602/// 'virtual'
603/// 'explicit'
604///
605/// typedef-name:
606/// identifier
607///
608/// type-specifier:
609/// simple-type-specifier
610/// class-specifier
611/// enum-specifier
612/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000613/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000614/// cv-qualifier
615///
616/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000617/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000618/// '::'[opt] nested-name-specifier 'template'
619/// simple-template-id [TODO]
620/// 'char'
621/// 'wchar_t'
622/// 'bool'
623/// 'short'
624/// 'int'
625/// 'long'
626/// 'signed'
627/// 'unsigned'
628/// 'float'
629/// 'double'
630/// 'void'
631/// [GNU] typeof-specifier
632/// [GNU] '_Complex'
633/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000634/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000635///
636/// type-name:
637/// class-name
638/// enum-name
639/// typedef-name
640///
641/// elaborated-type-specifier:
642/// class-key '::'[opt] nested-name-specifier[opt] identifier
643/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
644/// simple-template-id
645/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
646///
647/// enum-name:
648/// identifier
649///
650/// enum-specifier:
651/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
652/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
653///
654/// class-specifier:
655/// class-head '{' member-specification[opt] '}'
656///
657/// class-head:
658/// class-key identifier[opt] base-clause[opt]
659/// class-key nested-name-specifier identifier base-clause[opt]
660/// class-key nested-name-specifier[opt] simple-template-id
661/// base-clause[opt]
662///
663/// class-key:
664/// 'class'
665/// 'struct'
666/// 'union'
667///
668/// cv-qualifier:
669/// 'const'
670/// 'volatile'
671/// [GNU] restrict
672///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000673Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000674 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000675 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000676 // Check for need to substitute AltiVec __vector keyword
677 // for "vector" identifier.
678 if (TryAltiVecVectorToken())
679 return TPResult::True();
680 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000681 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000682 // Annotate typenames and C++ scope specifiers. If we get one, just
683 // recurse to handle whatever we get.
684 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000685 return TPResult::Error();
686 if (Tok.is(tok::identifier))
687 return TPResult::False();
688 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000689
Chris Lattner2ee9b402009-12-19 01:11:05 +0000690 case tok::coloncolon: { // ::foo::bar
691 const Token &Next = NextToken();
692 if (Next.is(tok::kw_new) || // ::new
693 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000694 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnere5849262009-01-04 23:33:56 +0000696 // Annotate typenames and C++ scope specifiers. If we get one, just
697 // recurse to handle whatever we get.
698 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000699 return TPResult::Error();
700 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000701 }
702
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000703 // decl-specifier:
704 // storage-class-specifier
705 // type-specifier
706 // function-specifier
707 // 'friend'
708 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000709 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000710 case tok::kw_friend:
711 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000712 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000713 // storage-class-specifier
714 case tok::kw_register:
715 case tok::kw_static:
716 case tok::kw_extern:
717 case tok::kw_mutable:
718 case tok::kw_auto:
719 case tok::kw___thread:
720 // function-specifier
721 case tok::kw_inline:
722 case tok::kw_virtual:
723 case tok::kw_explicit:
724
725 // type-specifier:
726 // simple-type-specifier
727 // class-specifier
728 // enum-specifier
729 // elaborated-type-specifier
730 // typename-specifier
731 // cv-qualifier
732
733 // class-specifier
734 // elaborated-type-specifier
735 case tok::kw_class:
736 case tok::kw_struct:
737 case tok::kw_union:
738 // enum-specifier
739 case tok::kw_enum:
740 // cv-qualifier
741 case tok::kw_const:
742 case tok::kw_volatile:
743
744 // GNU
745 case tok::kw_restrict:
746 case tok::kw__Complex:
747 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000748 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Steve Naroff7ec56582009-01-06 17:40:00 +0000750 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000751 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000752 case tok::kw___cdecl:
753 case tok::kw___stdcall:
754 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000755 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000756 case tok::kw___w64:
757 case tok::kw___ptr64:
758 case tok::kw___forceinline:
759 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000760
761 // Borland
762 case tok::kw___pascal:
763 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000764
765 // AltiVec
766 case tok::kw___vector:
767 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000768
John McCall51e2a5d2010-04-30 03:11:01 +0000769 case tok::annot_template_id: {
770 TemplateIdAnnotation *TemplateId
771 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
772 if (TemplateId->Kind != TNK_Type_template)
773 return TPResult::False();
774 CXXScopeSpec SS;
775 AnnotateTemplateIdTokenAsType(&SS);
776 assert(Tok.is(tok::annot_typename));
777 goto case_typename;
778 }
779
John McCallae03cb52009-12-19 00:35:18 +0000780 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
781 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000782 if (TryAnnotateTypeOrScopeToken())
783 return TPResult::Error();
784 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000785 return TPResult::False();
786 // If that succeeded, fallthrough into the generic simple-type-id case.
787
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000788 // The ambiguity resides in a simple-type-specifier/typename-specifier
789 // followed by a '('. The '(' could either be the start of:
790 //
791 // direct-declarator:
792 // '(' declarator ')'
793 //
794 // direct-abstract-declarator:
795 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
796 // exception-specification[opt]
797 // '(' abstract-declarator ')'
798 //
799 // or part of a function-style cast expression:
800 //
801 // simple-type-specifier '(' expression-list[opt] ')'
802 //
803
804 // simple-type-specifier:
805
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000806 case tok::kw_char:
807 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000808 case tok::kw_char16_t:
809 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000810 case tok::kw_bool:
811 case tok::kw_short:
812 case tok::kw_int:
813 case tok::kw_long:
814 case tok::kw_signed:
815 case tok::kw_unsigned:
816 case tok::kw_float:
817 case tok::kw_double:
818 case tok::kw_void:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000819 case tok::annot_typename:
John McCall51e2a5d2010-04-30 03:11:01 +0000820 case_typename:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000821 if (NextToken().is(tok::l_paren))
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
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000826 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000827 case tok::kw_typeof: {
828 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000829 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000830
831 TentativeParsingAction PA(*this);
832
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000833 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000834 bool isFollowedByParen = Tok.is(tok::l_paren);
835
836 PA.Revert();
837
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000838 if (TPR == TPResult::Error())
839 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000840
841 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000842 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000843
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000844 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000845 }
846
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000847 // C++0x decltype support.
848 case tok::kw_decltype:
849 return TPResult::True();
850
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000851 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000852 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000853 }
854}
855
856/// [GNU] typeof-specifier:
857/// 'typeof' '(' expressions ')'
858/// 'typeof' '(' type-name ')'
859///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000860Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000861 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
862 ConsumeToken();
863
864 assert(Tok.is(tok::l_paren) && "Expected '('");
865 // Parse through the parens after 'typeof'.
866 ConsumeParen();
867 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000868 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000869
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000870 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000871}
872
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000873Parser::TPResult Parser::TryParseDeclarationSpecifier() {
874 TPResult TPR = isCXXDeclarationSpecifier();
875 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000876 return TPR;
877
878 if (Tok.is(tok::kw_typeof))
879 TryParseTypeofSpecifier();
880 else
881 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000883 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000884 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000885}
886
887/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
888/// a constructor-style initializer, when parsing declaration statements.
889/// Returns true for function declarator and false for constructor-style
890/// initializer.
891/// If during the disambiguation process a parsing error is encountered,
892/// the function returns true to let the declaration parsing code handle it.
893///
894/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
895/// exception-specification[opt]
896///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000897bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000898
899 // C++ 8.2p1:
900 // The ambiguity arising from the similarity between a function-style cast and
901 // a declaration mentioned in 6.8 can also occur in the context of a
902 // declaration. In that context, the choice is between a function declaration
903 // with a redundant set of parentheses around a parameter name and an object
904 // declaration with a function-style cast as the initializer. Just as for the
905 // ambiguities mentioned in 6.8, the resolution is to consider any construct
906 // that could possibly be a declaration a declaration.
907
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000908 TentativeParsingAction PA(*this);
909
910 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000911 TPResult TPR = TryParseParameterDeclarationClause();
912 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
913 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000914
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000915 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000916 PA.Revert();
917
918 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000919 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000920 return true;
921
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000922 if (TPR == TPResult::Ambiguous()) {
923 // Function declarator has precedence over constructor-style initializer.
924 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000925 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +0000926 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
927 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000928 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000929 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000930
931 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000932}
933
934/// parameter-declaration-clause:
935/// parameter-declaration-list[opt] '...'[opt]
936/// parameter-declaration-list ',' '...'
937///
938/// parameter-declaration-list:
939/// parameter-declaration
940/// parameter-declaration-list ',' parameter-declaration
941///
942/// parameter-declaration:
943/// decl-specifier-seq declarator
944/// decl-specifier-seq declarator '=' assignment-expression
945/// decl-specifier-seq abstract-declarator[opt]
946/// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
947///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000948Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000949
950 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000951 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000952
953 // parameter-declaration-list[opt] '...'[opt]
954 // parameter-declaration-list ',' '...'
955 //
956 // parameter-declaration-list:
957 // parameter-declaration
958 // parameter-declaration-list ',' parameter-declaration
959 //
960 while (1) {
961 // '...'[opt]
962 if (Tok.is(tok::ellipsis)) {
963 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000964 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000965 }
966
967 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000968 TPResult TPR = TryParseDeclarationSpecifier();
969 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000970 return TPR;
971
972 // declarator
973 // abstract-declarator[opt]
974 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000975 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000976 return TPR;
977
978 if (Tok.is(tok::equal)) {
979 // '=' assignment-expression
980 // Parse through assignment-expression.
981 tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
982 if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000983 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000984 }
985
986 if (Tok.is(tok::ellipsis)) {
987 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000988 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000989 }
990
991 if (Tok.isNot(tok::comma))
992 break;
993 ConsumeToken(); // the comma.
994 }
995
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000996 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000997}
998
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000999/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1000/// parsing as a function declarator.
1001/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1002/// return TPResult::Ambiguous(), otherwise it will return either False() or
1003/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001004///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001005/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1006/// exception-specification[opt]
1007///
1008/// exception-specification:
1009/// 'throw' '(' type-id-list[opt] ')'
1010///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001011Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001012
1013 // The '(' is already parsed.
1014
1015 TPResult TPR = TryParseParameterDeclarationClause();
1016 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1017 TPR = TPResult::False();
1018
1019 if (TPR == TPResult::False() || TPR == TPResult::Error())
1020 return TPR;
1021
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001022 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001023 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001024 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001025
1026 // cv-qualifier-seq
1027 while (Tok.is(tok::kw_const) ||
1028 Tok.is(tok::kw_volatile) ||
1029 Tok.is(tok::kw_restrict) )
1030 ConsumeToken();
1031
1032 // exception-specification
1033 if (Tok.is(tok::kw_throw)) {
1034 ConsumeToken();
1035 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001036 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001037
1038 // Parse through the parens after 'throw'.
1039 ConsumeParen();
1040 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001041 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001042 }
1043
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001044 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001045}
1046
1047/// '[' constant-expression[opt] ']'
1048///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001049Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001050 ConsumeBracket();
1051 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001052 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001053
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001054 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001055}