blob: dcf1d40628994f64798fb766ca84b4d3b0754ccc [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();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000191 } else if (Tok.is(tok::equal)) {
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.
200 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000201 }
202
203 if (Tok.isNot(tok::comma))
204 break;
205 ConsumeToken(); // the comma.
206 }
207
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000208 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000209}
210
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000211/// isCXXConditionDeclaration - Disambiguates between a declaration or an
212/// expression for a condition of a if/switch/while/for statement.
213/// If during the disambiguation process a parsing error is encountered,
214/// the function returns true to let the declaration parsing code handle it.
215///
216/// condition:
217/// expression
218/// type-specifier-seq declarator '=' assignment-expression
219/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
220/// '=' assignment-expression
221///
222bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000223 TPResult TPR = isCXXDeclarationSpecifier();
224 if (TPR != TPResult::Ambiguous())
225 return TPR != TPResult::False(); // Returns true for TPResult::True() or
226 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000227
228 // FIXME: Add statistics about the number of ambiguous statements encountered
229 // and how they were resolved (number of declarations+number of expressions).
230
231 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
232 // We need tentative parsing...
233
234 TentativeParsingAction PA(*this);
235
236 // type-specifier-seq
237 if (Tok.is(tok::kw_typeof))
238 TryParseTypeofSpecifier();
239 else
240 ConsumeToken();
241 assert(Tok.is(tok::l_paren) && "Expected '('");
242
243 // declarator
244 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
245
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000246 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000247 if (TPR == TPResult::Error())
248 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000249
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000250 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000251 // '='
252 // [GNU] simple-asm-expr[opt] attributes[opt]
253 if (Tok.is(tok::equal) ||
254 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000255 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000256 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000257 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000258 }
259
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000260 PA.Revert();
261
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000262 assert(TPR == TPResult::True() || TPR == TPResult::False());
263 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000264}
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000267 ///
268 /// The context parameter states what context we're parsing right
269 /// now, which affects how this routine copes with the token
270 /// following the type-id. If the context is TypeIdInParens, we have
271 /// already parsed the '(' and we will cease lookahead when we hit
272 /// the corresponding ')'. If the context is
273 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
274 /// before this template argument, and will cease lookahead when we
275 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
276 /// and false for an expression. If during the disambiguation
277 /// process a parsing error is encountered, the function returns
278 /// true to let the declaration parsing code handle it.
279 ///
280 /// type-id:
281 /// type-specifier-seq abstract-declarator[opt]
282 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000283bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000285 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000286
287 // C++ 8.2p2:
288 // The ambiguity arising from the similarity between a function-style cast and
289 // a type-id can occur in different contexts. The ambiguity appears as a
290 // choice between a function-style cast expression and a declaration of a
291 // type. The resolution is that any construct that could possibly be a type-id
292 // in its syntactic context shall be considered a type-id.
293
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000294 TPResult TPR = isCXXDeclarationSpecifier();
295 if (TPR != TPResult::Ambiguous())
296 return TPR != TPResult::False(); // Returns true for TPResult::True() or
297 // TPResult::Error().
298
299 // FIXME: Add statistics about the number of ambiguous statements encountered
300 // and how they were resolved (number of declarations+number of expressions).
301
302 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
303 // We need tentative parsing...
304
305 TentativeParsingAction PA(*this);
306
307 // type-specifier-seq
308 if (Tok.is(tok::kw_typeof))
309 TryParseTypeofSpecifier();
310 else
311 ConsumeToken();
312 assert(Tok.is(tok::l_paren) && "Expected '('");
313
314 // declarator
315 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
316
317 // In case of an error, let the declaration parsing code handle it.
318 if (TPR == TPResult::Error())
319 TPR = TPResult::True();
320
321 if (TPR == TPResult::Ambiguous()) {
322 // We are supposed to be inside parens, so if after the abstract declarator
323 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000324 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000325 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000326 isAmbiguous = true;
327
Douglas Gregor8b642592009-02-10 00:53:15 +0000328 // We are supposed to be inside a template argument, so if after
329 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
330 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000331 } else if (Context == TypeIdAsTemplateArgument &&
332 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
333 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000334 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000335 isAmbiguous = true;
336
337 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000338 TPR = TPResult::False();
339 }
340
341 PA.Revert();
342
343 assert(TPR == TPResult::True() || TPR == TPResult::False());
344 return TPR == TPResult::True();
345}
346
Sean Huntbbd37c62009-11-21 08:43:09 +0000347/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
348/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
349/// performed that will simply return true if a [[ is seen. Currently C++ has no
350/// syntactical ambiguities from this check, but it may inhibit error recovery.
351/// If CheckClosing is true, a check is made for closing ]] brackets.
352///
353/// If given, After is set to the token after the attribute-specifier so that
354/// appropriate parsing decisions can be made; it is left untouched if false is
355/// returned.
356///
357/// FIXME: If an error is in the closing ]] brackets, the program assumes
358/// the absence of an attribute-specifier, which can cause very yucky errors
359/// to occur.
360///
361/// [C++0x] attribute-specifier:
362/// '[' '[' attribute-list ']' ']'
363///
364/// [C++0x] attribute-list:
365/// attribute[opt]
366/// attribute-list ',' attribute[opt]
367///
368/// [C++0x] attribute:
369/// attribute-token attribute-argument-clause[opt]
370///
371/// [C++0x] attribute-token:
372/// identifier
373/// attribute-scoped-token
374///
375/// [C++0x] attribute-scoped-token:
376/// attribute-namespace '::' identifier
377///
378/// [C++0x] attribute-namespace:
379/// identifier
380///
381/// [C++0x] attribute-argument-clause:
382/// '(' balanced-token-seq ')'
383///
384/// [C++0x] balanced-token-seq:
385/// balanced-token
386/// balanced-token-seq balanced-token
387///
388/// [C++0x] balanced-token:
389/// '(' balanced-token-seq ')'
390/// '[' balanced-token-seq ']'
391/// '{' balanced-token-seq '}'
392/// any token but '(', ')', '[', ']', '{', or '}'
393bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
394 tok::TokenKind *After) {
395 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
396 return false;
397
398 // No tentative parsing if we don't need to look for ]]
399 if (!CheckClosing && !getLang().ObjC1)
400 return true;
401
402 struct TentativeReverter {
403 TentativeParsingAction PA;
404
405 TentativeReverter (Parser& P)
406 : PA(P)
407 {}
408 ~TentativeReverter () {
409 PA.Revert();
410 }
411 } R(*this);
412
413 // Opening brackets were checked for above.
414 ConsumeBracket();
415 ConsumeBracket();
416
417 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
418 SkipUntil(tok::r_square, false);
419
420 if (Tok.isNot(tok::r_square))
421 return false;
422 ConsumeBracket();
423
424 if (After)
425 *After = Tok.getKind();
426
427 return true;
428}
429
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000430/// declarator:
431/// direct-declarator
432/// ptr-operator declarator
433///
434/// direct-declarator:
435/// declarator-id
436/// direct-declarator '(' parameter-declaration-clause ')'
437/// cv-qualifier-seq[opt] exception-specification[opt]
438/// direct-declarator '[' constant-expression[opt] ']'
439/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000440/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000441///
442/// abstract-declarator:
443/// ptr-operator abstract-declarator[opt]
444/// direct-abstract-declarator
445///
446/// direct-abstract-declarator:
447/// direct-abstract-declarator[opt]
448/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
449/// exception-specification[opt]
450/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
451/// '(' abstract-declarator ')'
452///
453/// ptr-operator:
454/// '*' cv-qualifier-seq[opt]
455/// '&'
456/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000457/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000458///
459/// cv-qualifier-seq:
460/// cv-qualifier cv-qualifier-seq[opt]
461///
462/// cv-qualifier:
463/// 'const'
464/// 'volatile'
465///
466/// declarator-id:
467/// id-expression
468///
469/// id-expression:
470/// unqualified-id
471/// qualified-id [TODO]
472///
473/// unqualified-id:
474/// identifier
475/// operator-function-id [TODO]
476/// conversion-function-id [TODO]
477/// '~' class-name [TODO]
478/// template-id [TODO]
479///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000480Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
481 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000482 // declarator:
483 // direct-declarator
484 // ptr-operator declarator
485
486 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000487 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000488 if (TryAnnotateCXXScopeToken(true))
489 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000490
Chris Lattner9af55002009-03-27 04:18:06 +0000491 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000492 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000493 // ptr-operator
494 ConsumeToken();
495 while (Tok.is(tok::kw_const) ||
496 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000497 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000498 ConsumeToken();
499 } else {
500 break;
501 }
502 }
503
504 // direct-declarator:
505 // direct-abstract-declarator:
506
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000507 if ((Tok.is(tok::identifier) ||
508 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
509 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000510 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000511 if (Tok.is(tok::annot_cxxscope))
512 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000513 ConsumeToken();
514 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000515 ConsumeParen();
516 if (mayBeAbstract &&
517 (Tok.is(tok::r_paren) || // 'int()' is a function.
518 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
519 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000520 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
521 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000522 TPResult TPR = TryParseFunctionDeclarator();
523 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000524 return TPR;
525 } else {
526 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000527 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000528 // '(' abstract-declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000529 if (Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000530 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000531 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000532 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000533 return TPR;
534 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000535 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000536 ConsumeParen();
537 }
538 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000539 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000540 }
541
542 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000543 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000544
545 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000546 // Check whether we have a function declarator or a possible ctor-style
547 // initializer that follows the declarator. Note that ctor-style
548 // initializers are not possible in contexts where abstract declarators
549 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000550 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000551 break;
552
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000553 // direct-declarator '(' parameter-declaration-clause ')'
554 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000555 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000556 TPR = TryParseFunctionDeclarator();
557 } else if (Tok.is(tok::l_square)) {
558 // direct-declarator '[' constant-expression[opt] ']'
559 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
560 TPR = TryParseBracketDeclarator();
561 } else {
562 break;
563 }
564
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000565 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000566 return TPR;
567 }
568
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000569 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000570}
571
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000572/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
573/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
574/// be either a decl-specifier or a function-style cast, and TPResult::Error()
575/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000576///
577/// decl-specifier:
578/// storage-class-specifier
579/// type-specifier
580/// function-specifier
581/// 'friend'
582/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000583/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000584/// [GNU] attributes declaration-specifiers[opt]
585///
586/// storage-class-specifier:
587/// 'register'
588/// 'static'
589/// 'extern'
590/// 'mutable'
591/// 'auto'
592/// [GNU] '__thread'
593///
594/// function-specifier:
595/// 'inline'
596/// 'virtual'
597/// 'explicit'
598///
599/// typedef-name:
600/// identifier
601///
602/// type-specifier:
603/// simple-type-specifier
604/// class-specifier
605/// enum-specifier
606/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000607/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000608/// cv-qualifier
609///
610/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000611/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000612/// '::'[opt] nested-name-specifier 'template'
613/// simple-template-id [TODO]
614/// 'char'
615/// 'wchar_t'
616/// 'bool'
617/// 'short'
618/// 'int'
619/// 'long'
620/// 'signed'
621/// 'unsigned'
622/// 'float'
623/// 'double'
624/// 'void'
625/// [GNU] typeof-specifier
626/// [GNU] '_Complex'
627/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000628/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000629///
630/// type-name:
631/// class-name
632/// enum-name
633/// typedef-name
634///
635/// elaborated-type-specifier:
636/// class-key '::'[opt] nested-name-specifier[opt] identifier
637/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
638/// simple-template-id
639/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
640///
641/// enum-name:
642/// identifier
643///
644/// enum-specifier:
645/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
646/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
647///
648/// class-specifier:
649/// class-head '{' member-specification[opt] '}'
650///
651/// class-head:
652/// class-key identifier[opt] base-clause[opt]
653/// class-key nested-name-specifier identifier base-clause[opt]
654/// class-key nested-name-specifier[opt] simple-template-id
655/// base-clause[opt]
656///
657/// class-key:
658/// 'class'
659/// 'struct'
660/// 'union'
661///
662/// cv-qualifier:
663/// 'const'
664/// 'volatile'
665/// [GNU] restrict
666///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000667Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000668 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000669 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000670 // Check for need to substitute AltiVec __vector keyword
671 // for "vector" identifier.
672 if (TryAltiVecVectorToken())
673 return TPResult::True();
674 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000675 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000676 // Annotate typenames and C++ scope specifiers. If we get one, just
677 // recurse to handle whatever we get.
678 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000679 return TPResult::Error();
680 if (Tok.is(tok::identifier))
681 return TPResult::False();
682 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000683
Chris Lattner2ee9b402009-12-19 01:11:05 +0000684 case tok::coloncolon: { // ::foo::bar
685 const Token &Next = NextToken();
686 if (Next.is(tok::kw_new) || // ::new
687 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000688 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattnere5849262009-01-04 23:33:56 +0000690 // Annotate typenames and C++ scope specifiers. If we get one, just
691 // recurse to handle whatever we get.
692 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000693 return TPResult::Error();
694 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000695 }
696
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000697 // decl-specifier:
698 // storage-class-specifier
699 // type-specifier
700 // function-specifier
701 // 'friend'
702 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000703 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000704 case tok::kw_friend:
705 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000706 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000707 // storage-class-specifier
708 case tok::kw_register:
709 case tok::kw_static:
710 case tok::kw_extern:
711 case tok::kw_mutable:
712 case tok::kw_auto:
713 case tok::kw___thread:
714 // function-specifier
715 case tok::kw_inline:
716 case tok::kw_virtual:
717 case tok::kw_explicit:
718
719 // type-specifier:
720 // simple-type-specifier
721 // class-specifier
722 // enum-specifier
723 // elaborated-type-specifier
724 // typename-specifier
725 // cv-qualifier
726
727 // class-specifier
728 // elaborated-type-specifier
729 case tok::kw_class:
730 case tok::kw_struct:
731 case tok::kw_union:
732 // enum-specifier
733 case tok::kw_enum:
734 // cv-qualifier
735 case tok::kw_const:
736 case tok::kw_volatile:
737
738 // GNU
739 case tok::kw_restrict:
740 case tok::kw__Complex:
741 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000742 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Steve Naroff7ec56582009-01-06 17:40:00 +0000744 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000745 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000746 case tok::kw___cdecl:
747 case tok::kw___stdcall:
748 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000749 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000750 case tok::kw___w64:
751 case tok::kw___ptr64:
752 case tok::kw___forceinline:
753 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000754
755 // AltiVec
756 case tok::kw___vector:
757 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000758
John McCall51e2a5d2010-04-30 03:11:01 +0000759 case tok::annot_template_id: {
760 TemplateIdAnnotation *TemplateId
761 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
762 if (TemplateId->Kind != TNK_Type_template)
763 return TPResult::False();
764 CXXScopeSpec SS;
765 AnnotateTemplateIdTokenAsType(&SS);
766 assert(Tok.is(tok::annot_typename));
767 goto case_typename;
768 }
769
John McCallae03cb52009-12-19 00:35:18 +0000770 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
771 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000772 if (TryAnnotateTypeOrScopeToken())
773 return TPResult::Error();
774 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000775 return TPResult::False();
776 // If that succeeded, fallthrough into the generic simple-type-id case.
777
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000778 // The ambiguity resides in a simple-type-specifier/typename-specifier
779 // followed by a '('. The '(' could either be the start of:
780 //
781 // direct-declarator:
782 // '(' declarator ')'
783 //
784 // direct-abstract-declarator:
785 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
786 // exception-specification[opt]
787 // '(' abstract-declarator ')'
788 //
789 // or part of a function-style cast expression:
790 //
791 // simple-type-specifier '(' expression-list[opt] ')'
792 //
793
794 // simple-type-specifier:
795
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000796 case tok::kw_char:
797 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000798 case tok::kw_char16_t:
799 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000800 case tok::kw_bool:
801 case tok::kw_short:
802 case tok::kw_int:
803 case tok::kw_long:
804 case tok::kw_signed:
805 case tok::kw_unsigned:
806 case tok::kw_float:
807 case tok::kw_double:
808 case tok::kw_void:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000809 case tok::annot_typename:
John McCall51e2a5d2010-04-30 03:11:01 +0000810 case_typename:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000811 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000812 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000813
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000814 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000815
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000816 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000817 case tok::kw_typeof: {
818 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000819 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000820
821 TentativeParsingAction PA(*this);
822
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000823 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000824 bool isFollowedByParen = Tok.is(tok::l_paren);
825
826 PA.Revert();
827
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000828 if (TPR == TPResult::Error())
829 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000830
831 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000832 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000833
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000834 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000835 }
836
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000837 // C++0x decltype support.
838 case tok::kw_decltype:
839 return TPResult::True();
840
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000841 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000842 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000843 }
844}
845
846/// [GNU] typeof-specifier:
847/// 'typeof' '(' expressions ')'
848/// 'typeof' '(' type-name ')'
849///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000850Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000851 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
852 ConsumeToken();
853
854 assert(Tok.is(tok::l_paren) && "Expected '('");
855 // Parse through the parens after 'typeof'.
856 ConsumeParen();
857 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000858 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000859
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000860 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000861}
862
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000863Parser::TPResult Parser::TryParseDeclarationSpecifier() {
864 TPResult TPR = isCXXDeclarationSpecifier();
865 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000866 return TPR;
867
868 if (Tok.is(tok::kw_typeof))
869 TryParseTypeofSpecifier();
870 else
871 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000873 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000874 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000875}
876
877/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
878/// a constructor-style initializer, when parsing declaration statements.
879/// Returns true for function declarator and false for constructor-style
880/// initializer.
881/// If during the disambiguation process a parsing error is encountered,
882/// the function returns true to let the declaration parsing code handle it.
883///
884/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
885/// exception-specification[opt]
886///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000887bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000888
889 // C++ 8.2p1:
890 // The ambiguity arising from the similarity between a function-style cast and
891 // a declaration mentioned in 6.8 can also occur in the context of a
892 // declaration. In that context, the choice is between a function declaration
893 // with a redundant set of parentheses around a parameter name and an object
894 // declaration with a function-style cast as the initializer. Just as for the
895 // ambiguities mentioned in 6.8, the resolution is to consider any construct
896 // that could possibly be a declaration a declaration.
897
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000898 TentativeParsingAction PA(*this);
899
900 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000901 TPResult TPR = TryParseParameterDeclarationClause();
902 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
903 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000904
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000905 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000906 PA.Revert();
907
908 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000909 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000910 return true;
911
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000912 if (TPR == TPResult::Ambiguous()) {
913 // Function declarator has precedence over constructor-style initializer.
914 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000915 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +0000916 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
917 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000918 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +0000919 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000920
921 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000922}
923
924/// parameter-declaration-clause:
925/// parameter-declaration-list[opt] '...'[opt]
926/// parameter-declaration-list ',' '...'
927///
928/// parameter-declaration-list:
929/// parameter-declaration
930/// parameter-declaration-list ',' parameter-declaration
931///
932/// parameter-declaration:
933/// decl-specifier-seq declarator
934/// decl-specifier-seq declarator '=' assignment-expression
935/// decl-specifier-seq abstract-declarator[opt]
936/// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
937///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000938Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000939
940 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000941 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000942
943 // parameter-declaration-list[opt] '...'[opt]
944 // parameter-declaration-list ',' '...'
945 //
946 // parameter-declaration-list:
947 // parameter-declaration
948 // parameter-declaration-list ',' parameter-declaration
949 //
950 while (1) {
951 // '...'[opt]
952 if (Tok.is(tok::ellipsis)) {
953 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000954 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000955 }
956
957 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000958 TPResult TPR = TryParseDeclarationSpecifier();
959 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000960 return TPR;
961
962 // declarator
963 // abstract-declarator[opt]
964 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000965 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000966 return TPR;
967
968 if (Tok.is(tok::equal)) {
969 // '=' assignment-expression
970 // Parse through assignment-expression.
971 tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
972 if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000973 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000974 }
975
976 if (Tok.is(tok::ellipsis)) {
977 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000978 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000979 }
980
981 if (Tok.isNot(tok::comma))
982 break;
983 ConsumeToken(); // the comma.
984 }
985
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000986 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000987}
988
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000989/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
990/// parsing as a function declarator.
991/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
992/// return TPResult::Ambiguous(), otherwise it will return either False() or
993/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +0000994///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000995/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
996/// exception-specification[opt]
997///
998/// exception-specification:
999/// 'throw' '(' type-id-list[opt] ')'
1000///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001001Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001002
1003 // The '(' is already parsed.
1004
1005 TPResult TPR = TryParseParameterDeclarationClause();
1006 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1007 TPR = TPResult::False();
1008
1009 if (TPR == TPResult::False() || TPR == TPResult::Error())
1010 return TPR;
1011
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001012 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001013 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001014 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001015
1016 // cv-qualifier-seq
1017 while (Tok.is(tok::kw_const) ||
1018 Tok.is(tok::kw_volatile) ||
1019 Tok.is(tok::kw_restrict) )
1020 ConsumeToken();
1021
1022 // exception-specification
1023 if (Tok.is(tok::kw_throw)) {
1024 ConsumeToken();
1025 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001026 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001027
1028 // Parse through the parens after 'throw'.
1029 ConsumeParen();
1030 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001031 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001032 }
1033
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001034 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001035}
1036
1037/// '[' constant-expression[opt] ']'
1038///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001039Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001040 ConsumeBracket();
1041 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001042 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001043
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001044 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001045}