blob: 618c3e2bd4de504194d77c469d7073367981d061 [file] [log] [blame]
Argyrios Kyrtzidis2c7137d2008-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 Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Argyrios Kyrtzidis2c7137d2008-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 Carlssonf24fcff62009-03-11 16:27:10 +000033/// [C++0x] static_assert-declaration
Argyrios Kyrtzidis2c7137d2008-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 Kyrtzidis2c7137d2008-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 Carlssonf24fcff62009-03-11 16:27:10 +000059 // static_assert-declaration
Alexis Hunt96d5c762009-11-21 08:43:09 +000060 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +000061 case tok::kw__Static_assert:
Anders Carlssonf24fcff62009-03-11 16:27:10 +000062 return true;
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +000063 // simple-declaration
Alexis Hunt96d5c762009-11-21 08:43:09 +000064 default:
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +000065 return isCXXSimpleDeclaration();
66 }
67}
68
69/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70/// between a simple-declaration or an expression-statement.
71/// If during the disambiguation process a parsing error is encountered,
72/// the function returns true to let the declaration parsing code handle it.
73/// Returns false if the statement is disambiguated as expression.
74///
75/// simple-declaration:
76/// decl-specifier-seq init-declarator-list[opt] ';'
77///
78bool Parser::isCXXSimpleDeclaration() {
79 // C++ 6.8p1:
80 // There is an ambiguity in the grammar involving expression-statements and
81 // declarations: An expression-statement with a function-style explicit type
82 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
83 // from a declaration where the first declarator starts with a '('. In those
84 // cases the statement is a declaration. [Note: To disambiguate, the whole
85 // statement might have to be examined to determine if it is an
86 // expression-statement or a declaration].
87
88 // C++ 6.8p3:
89 // The disambiguation is purely syntactic; that is, the meaning of the names
90 // occurring in such a statement, beyond whether they are type-names or not,
91 // is not generally used in or changed by the disambiguation. Class
92 // templates are instantiated as necessary to determine if a qualified name
93 // is a type-name. Disambiguation precedes parsing, and a statement
94 // disambiguated as a declaration may be an ill-formed declaration.
95
96 // We don't have to parse all of the decl-specifier-seq part. There's only
97 // an ambiguity if the first decl-specifier is
98 // simple-type-specifier/typename-specifier followed by a '(', which may
99 // indicate a function-style cast expression.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000100 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
101 // a case.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000102
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000103 TPResult TPR = isCXXDeclarationSpecifier();
104 if (TPR != TPResult::Ambiguous())
105 return TPR != TPResult::False(); // Returns true for TPResult::True() or
106 // TPResult::Error().
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000107
108 // FIXME: Add statistics about the number of ambiguous statements encountered
109 // and how they were resolved (number of declarations+number of expressions).
110
111 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
112 // We need tentative parsing...
113
114 TentativeParsingAction PA(*this);
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000115 TPR = TryParseSimpleDeclaration();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000116 PA.Revert();
117
118 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000119 if (TPR == TPResult::Error())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000120 return true;
121
122 // Declarations take precedence over expressions.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000123 if (TPR == TPResult::Ambiguous())
124 TPR = TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000125
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000126 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000127 return TPR == TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000128}
129
130/// simple-declaration:
131/// decl-specifier-seq init-declarator-list[opt] ';'
132///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000133Parser::TPResult Parser::TryParseSimpleDeclaration() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000134 // We know that we have a simple-type-specifier/typename-specifier followed
135 // by a '('.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000136 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000137
138 if (Tok.is(tok::kw_typeof))
139 TryParseTypeofSpecifier();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000140 else {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000141 ConsumeToken();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000142
143 if (getLang().ObjC1 && Tok.is(tok::less))
144 TryParseProtocolQualifiers();
145 }
146
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000147 assert(Tok.is(tok::l_paren) && "Expected '('");
148
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000149 TPResult TPR = TryParseInitDeclaratorList();
150 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000151 return TPR;
152
153 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000154 return TPResult::False();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000155
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000156 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000157}
158
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000159/// init-declarator-list:
160/// init-declarator
161/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000162///
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000163/// init-declarator:
164/// declarator initializer[opt]
165/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000166///
167/// initializer:
168/// '=' initializer-clause
169/// '(' expression-list ')'
170///
171/// initializer-clause:
172/// assignment-expression
173/// '{' initializer-list ','[opt] '}'
174/// '{' '}'
175///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000176Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000177 while (1) {
178 // declarator
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000179 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
180 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000181 return TPR;
182
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000183 // [GNU] simple-asm-expr[opt] attributes[opt]
184 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000185 return TPResult::True();
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000186
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000187 // initializer[opt]
188 if (Tok.is(tok::l_paren)) {
189 // Parse through the parens.
190 ConsumeParen();
191 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000192 return TPResult::Error();
Fariborz Jahanian161848a2010-08-29 17:20:53 +0000193 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregordc936182010-07-15 21:05:01 +0000194 // MSVC and g++ won't examine the rest of declarators if '=' is
195 // encountered; they just conclude that we have a declaration.
196 // EDG parses the initializer completely, which is the proper behavior
197 // for this case.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000198 //
Douglas Gregordc936182010-07-15 21:05:01 +0000199 // At present, Clang follows MSVC and g++, since the parser does not have
200 // the ability to parse an expression fully without recording the
201 // results of that parse.
Fariborz Jahanian161848a2010-08-29 17:20:53 +0000202 // Also allow 'in' after on objective-c declaration as in:
203 // for (int (^b)(void) in array). Ideally this should be done in the
204 // context of parsing for-init-statement of a foreach statement only. But,
205 // in any other context 'in' is invalid after a declaration and parser
206 // issues the error regardless of outcome of this decision.
207 // FIXME. Change if above assumption does not hold.
Douglas Gregordc936182010-07-15 21:05:01 +0000208 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000209 }
210
211 if (Tok.isNot(tok::comma))
212 break;
213 ConsumeToken(); // the comma.
214 }
215
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000216 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000217}
218
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000219/// isCXXConditionDeclaration - Disambiguates between a declaration or an
220/// expression for a condition of a if/switch/while/for statement.
221/// If during the disambiguation process a parsing error is encountered,
222/// the function returns true to let the declaration parsing code handle it.
223///
224/// condition:
225/// expression
226/// type-specifier-seq declarator '=' assignment-expression
227/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
228/// '=' assignment-expression
229///
230bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000231 TPResult TPR = isCXXDeclarationSpecifier();
232 if (TPR != TPResult::Ambiguous())
233 return TPR != TPResult::False(); // Returns true for TPResult::True() or
234 // TPResult::Error().
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000235
236 // FIXME: Add statistics about the number of ambiguous statements encountered
237 // and how they were resolved (number of declarations+number of expressions).
238
239 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
240 // We need tentative parsing...
241
242 TentativeParsingAction PA(*this);
243
244 // type-specifier-seq
245 if (Tok.is(tok::kw_typeof))
246 TryParseTypeofSpecifier();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000247 else {
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000248 ConsumeToken();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000249
250 if (getLang().ObjC1 && Tok.is(tok::less))
251 TryParseProtocolQualifiers();
252 }
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000253 assert(Tok.is(tok::l_paren) && "Expected '('");
254
255 // declarator
256 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
257
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000258 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000259 if (TPR == TPResult::Error())
260 TPR = TPResult::True();
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000261
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000262 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000263 // '='
264 // [GNU] simple-asm-expr[opt] attributes[opt]
265 if (Tok.is(tok::equal) ||
266 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000267 TPR = TPResult::True();
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000268 else
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000269 TPR = TPResult::False();
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000270 }
271
Argyrios Kyrtzidis25346202008-10-05 15:19:49 +0000272 PA.Revert();
273
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000274 assert(TPR == TPResult::True() || TPR == TPResult::False());
275 return TPR == TPResult::True();
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000276}
277
Mike Stump11289f42009-09-09 15:08:12 +0000278 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +0000279 ///
280 /// The context parameter states what context we're parsing right
281 /// now, which affects how this routine copes with the token
282 /// following the type-id. If the context is TypeIdInParens, we have
283 /// already parsed the '(' and we will cease lookahead when we hit
284 /// the corresponding ')'. If the context is
285 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
286 /// before this template argument, and will cease lookahead when we
287 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
288 /// and false for an expression. If during the disambiguation
289 /// process a parsing error is encountered, the function returns
290 /// true to let the declaration parsing code handle it.
291 ///
292 /// type-id:
293 /// type-specifier-seq abstract-declarator[opt]
294 ///
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000295bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump11289f42009-09-09 15:08:12 +0000296
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000297 isAmbiguous = false;
Argyrios Kyrtzidis279d9812008-10-05 21:10:08 +0000298
299 // C++ 8.2p2:
300 // The ambiguity arising from the similarity between a function-style cast and
301 // a type-id can occur in different contexts. The ambiguity appears as a
302 // choice between a function-style cast expression and a declaration of a
303 // type. The resolution is that any construct that could possibly be a type-id
304 // in its syntactic context shall be considered a type-id.
305
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000306 TPResult TPR = isCXXDeclarationSpecifier();
307 if (TPR != TPResult::Ambiguous())
308 return TPR != TPResult::False(); // Returns true for TPResult::True() or
309 // TPResult::Error().
310
311 // FIXME: Add statistics about the number of ambiguous statements encountered
312 // and how they were resolved (number of declarations+number of expressions).
313
314 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
315 // We need tentative parsing...
316
317 TentativeParsingAction PA(*this);
318
319 // type-specifier-seq
320 if (Tok.is(tok::kw_typeof))
321 TryParseTypeofSpecifier();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000322 else {
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000323 ConsumeToken();
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000324
325 if (getLang().ObjC1 && Tok.is(tok::less))
326 TryParseProtocolQualifiers();
327 }
328
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000329 assert(Tok.is(tok::l_paren) && "Expected '('");
330
331 // declarator
332 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
333
334 // In case of an error, let the declaration parsing code handle it.
335 if (TPR == TPResult::Error())
336 TPR = TPResult::True();
337
338 if (TPR == TPResult::Ambiguous()) {
339 // We are supposed to be inside parens, so if after the abstract declarator
340 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000341 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor97f34572009-02-10 00:53:15 +0000342 TPR = TPResult::True();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000343 isAmbiguous = true;
344
Douglas Gregor97f34572009-02-10 00:53:15 +0000345 // We are supposed to be inside a template argument, so if after
346 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
347 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000348 } else if (Context == TypeIdAsTemplateArgument &&
349 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
350 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000351 TPR = TPResult::True();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000352 isAmbiguous = true;
353
354 } else
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000355 TPR = TPResult::False();
356 }
357
358 PA.Revert();
359
360 assert(TPR == TPResult::True() || TPR == TPResult::False());
361 return TPR == TPResult::True();
362}
363
Alexis Hunt96d5c762009-11-21 08:43:09 +0000364/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
365/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
366/// performed that will simply return true if a [[ is seen. Currently C++ has no
367/// syntactical ambiguities from this check, but it may inhibit error recovery.
368/// If CheckClosing is true, a check is made for closing ]] brackets.
369///
370/// If given, After is set to the token after the attribute-specifier so that
371/// appropriate parsing decisions can be made; it is left untouched if false is
372/// returned.
373///
374/// FIXME: If an error is in the closing ]] brackets, the program assumes
375/// the absence of an attribute-specifier, which can cause very yucky errors
376/// to occur.
377///
378/// [C++0x] attribute-specifier:
379/// '[' '[' attribute-list ']' ']'
380///
381/// [C++0x] attribute-list:
382/// attribute[opt]
383/// attribute-list ',' attribute[opt]
384///
385/// [C++0x] attribute:
386/// attribute-token attribute-argument-clause[opt]
387///
388/// [C++0x] attribute-token:
389/// identifier
390/// attribute-scoped-token
391///
392/// [C++0x] attribute-scoped-token:
393/// attribute-namespace '::' identifier
394///
395/// [C++0x] attribute-namespace:
396/// identifier
397///
398/// [C++0x] attribute-argument-clause:
399/// '(' balanced-token-seq ')'
400///
401/// [C++0x] balanced-token-seq:
402/// balanced-token
403/// balanced-token-seq balanced-token
404///
405/// [C++0x] balanced-token:
406/// '(' balanced-token-seq ')'
407/// '[' balanced-token-seq ']'
408/// '{' balanced-token-seq '}'
409/// any token but '(', ')', '[', ']', '{', or '}'
410bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
411 tok::TokenKind *After) {
412 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
413 return false;
414
415 // No tentative parsing if we don't need to look for ]]
416 if (!CheckClosing && !getLang().ObjC1)
417 return true;
418
419 struct TentativeReverter {
420 TentativeParsingAction PA;
421
422 TentativeReverter (Parser& P)
423 : PA(P)
424 {}
425 ~TentativeReverter () {
426 PA.Revert();
427 }
428 } R(*this);
429
430 // Opening brackets were checked for above.
431 ConsumeBracket();
432 ConsumeBracket();
433
434 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
435 SkipUntil(tok::r_square, false);
436
437 if (Tok.isNot(tok::r_square))
438 return false;
439 ConsumeBracket();
440
441 if (After)
442 *After = Tok.getKind();
443
444 return true;
445}
446
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000447/// declarator:
448/// direct-declarator
449/// ptr-operator declarator
450///
451/// direct-declarator:
452/// declarator-id
453/// direct-declarator '(' parameter-declaration-clause ')'
454/// cv-qualifier-seq[opt] exception-specification[opt]
455/// direct-declarator '[' constant-expression[opt] ']'
456/// '(' declarator ')'
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000457/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000458///
459/// abstract-declarator:
460/// ptr-operator abstract-declarator[opt]
461/// direct-abstract-declarator
Douglas Gregor27b4c162010-12-23 22:44:42 +0000462/// ...
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000463///
464/// direct-abstract-declarator:
465/// direct-abstract-declarator[opt]
466/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
467/// exception-specification[opt]
468/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
469/// '(' abstract-declarator ')'
470///
471/// ptr-operator:
472/// '*' cv-qualifier-seq[opt]
473/// '&'
474/// [C++0x] '&&' [TODO]
Sebastian Redlc6d52f52009-01-24 23:29:36 +0000475/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000476///
477/// cv-qualifier-seq:
478/// cv-qualifier cv-qualifier-seq[opt]
479///
480/// cv-qualifier:
481/// 'const'
482/// 'volatile'
483///
484/// declarator-id:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000485/// '...'[opt] id-expression
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000486///
487/// id-expression:
488/// unqualified-id
489/// qualified-id [TODO]
490///
491/// unqualified-id:
492/// identifier
493/// operator-function-id [TODO]
494/// conversion-function-id [TODO]
495/// '~' class-name [TODO]
496/// template-id [TODO]
497///
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000498Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
499 bool mayHaveIdentifier) {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000500 // declarator:
501 // direct-declarator
502 // ptr-operator declarator
503
504 while (1) {
Sebastian Redlc6d52f52009-01-24 23:29:36 +0000505 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall1f476a12010-02-26 08:45:28 +0000506 if (TryAnnotateCXXScopeToken(true))
507 return TPResult::Error();
Sebastian Redlc6d52f52009-01-24 23:29:36 +0000508
Chris Lattner9eac9312009-03-27 04:18:06 +0000509 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000510 Tok.is(tok::ampamp) ||
Sebastian Redlc6d52f52009-01-24 23:29:36 +0000511 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000512 // ptr-operator
513 ConsumeToken();
514 while (Tok.is(tok::kw_const) ||
515 Tok.is(tok::kw_volatile) ||
Chris Lattner9eac9312009-03-27 04:18:06 +0000516 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000517 ConsumeToken();
518 } else {
519 break;
520 }
521 }
522
523 // direct-declarator:
524 // direct-abstract-declarator:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000525 if (Tok.is(tok::ellipsis))
526 ConsumeToken();
527
Argyrios Kyrtzidis33c70c92009-07-21 17:05:03 +0000528 if ((Tok.is(tok::identifier) ||
529 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
530 mayHaveIdentifier) {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000531 // declarator-id
Argyrios Kyrtzidis33c70c92009-07-21 17:05:03 +0000532 if (Tok.is(tok::annot_cxxscope))
533 ConsumeToken();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000534 ConsumeToken();
535 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +0000536 ConsumeParen();
537 if (mayBeAbstract &&
538 (Tok.is(tok::r_paren) || // 'int()' is a function.
539 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
540 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000541 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
542 // exception-specification[opt]
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000543 TPResult TPR = TryParseFunctionDeclarator();
544 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000545 return TPR;
546 } else {
547 // '(' declarator ')'
Argyrios Kyrtzidis7b87f272008-10-05 14:27:18 +0000548 // '(' attributes declarator ')'
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000549 // '(' abstract-declarator ')'
Chris Lattnerd7821e42010-09-28 23:35:09 +0000550 if (Tok.is(tok::kw___attribute) ||
551 Tok.is(tok::kw___declspec) ||
552 Tok.is(tok::kw___cdecl) ||
553 Tok.is(tok::kw___stdcall) ||
554 Tok.is(tok::kw___fastcall) ||
555 Tok.is(tok::kw___thiscall))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000556 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +0000557 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000558 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000559 return TPR;
560 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000561 return TPResult::False();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000562 ConsumeParen();
563 }
564 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000565 return TPResult::False();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000566 }
567
568 while (1) {
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000569 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000570
Douglas Gregor27b4c162010-12-23 22:44:42 +0000571 // abstract-declarator: ...
572 if (Tok.is(tok::ellipsis))
573 ConsumeToken();
574
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000575 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +0000576 // Check whether we have a function declarator or a possible ctor-style
577 // initializer that follows the declarator. Note that ctor-style
578 // initializers are not possible in contexts where abstract declarators
579 // are allowed.
Argyrios Kyrtzidis3a0558a2008-10-17 23:23:35 +0000580 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +0000581 break;
582
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000583 // direct-declarator '(' parameter-declaration-clause ')'
584 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +0000585 ConsumeParen();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000586 TPR = TryParseFunctionDeclarator();
587 } else if (Tok.is(tok::l_square)) {
588 // direct-declarator '[' constant-expression[opt] ']'
589 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
590 TPR = TryParseBracketDeclarator();
591 } else {
592 break;
593 }
594
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000595 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000596 return TPR;
597 }
598
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000599 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000600}
601
Douglas Gregord1f69f62010-12-01 17:42:47 +0000602Parser::TPResult
603Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
604 switch (Kind) {
605 // Obviously starts an expression.
606 case tok::numeric_constant:
607 case tok::char_constant:
608 case tok::string_literal:
609 case tok::wide_string_literal:
610 case tok::l_square:
611 case tok::l_paren:
612 case tok::amp:
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000613 case tok::ampamp:
Douglas Gregord1f69f62010-12-01 17:42:47 +0000614 case tok::star:
615 case tok::plus:
616 case tok::plusplus:
617 case tok::minus:
618 case tok::minusminus:
619 case tok::tilde:
620 case tok::exclaim:
621 case tok::kw_sizeof:
622 case tok::kw___func__:
623 case tok::kw_const_cast:
624 case tok::kw_delete:
625 case tok::kw_dynamic_cast:
626 case tok::kw_false:
627 case tok::kw_new:
628 case tok::kw_operator:
629 case tok::kw_reinterpret_cast:
630 case tok::kw_static_cast:
631 case tok::kw_this:
632 case tok::kw_throw:
633 case tok::kw_true:
634 case tok::kw_typeid:
635 case tok::kw_alignof:
636 case tok::kw_noexcept:
637 case tok::kw_nullptr:
638 case tok::kw___null:
639 case tok::kw___alignof:
640 case tok::kw___builtin_choose_expr:
641 case tok::kw___builtin_offsetof:
642 case tok::kw___builtin_types_compatible_p:
643 case tok::kw___builtin_va_arg:
644 case tok::kw___imag:
645 case tok::kw___real:
646 case tok::kw___FUNCTION__:
647 case tok::kw___PRETTY_FUNCTION__:
648 case tok::kw___has_nothrow_assign:
649 case tok::kw___has_nothrow_copy:
650 case tok::kw___has_nothrow_constructor:
651 case tok::kw___has_trivial_assign:
652 case tok::kw___has_trivial_copy:
653 case tok::kw___has_trivial_constructor:
654 case tok::kw___has_trivial_destructor:
655 case tok::kw___has_virtual_destructor:
656 case tok::kw___is_abstract:
657 case tok::kw___is_base_of:
658 case tok::kw___is_class:
Douglas Gregor8006e762011-01-27 20:28:01 +0000659 case tok::kw___is_convertible_to:
Douglas Gregord1f69f62010-12-01 17:42:47 +0000660 case tok::kw___is_empty:
661 case tok::kw___is_enum:
Chandler Carruth79803482011-04-23 10:47:20 +0000662 case tok::kw___is_literal:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +0000663 case tok::kw___is_literal_type:
Douglas Gregord1f69f62010-12-01 17:42:47 +0000664 case tok::kw___is_pod:
665 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000666 case tok::kw___is_trivial:
Douglas Gregord1f69f62010-12-01 17:42:47 +0000667 case tok::kw___is_union:
Douglas Gregord1f69f62010-12-01 17:42:47 +0000668 case tok::kw___uuidof:
669 return TPResult::True();
670
671 // Obviously starts a type-specifier-seq:
672 case tok::kw_char:
673 case tok::kw_const:
674 case tok::kw_double:
675 case tok::kw_enum:
676 case tok::kw_float:
677 case tok::kw_int:
678 case tok::kw_long:
679 case tok::kw_restrict:
680 case tok::kw_short:
681 case tok::kw_signed:
682 case tok::kw_struct:
683 case tok::kw_union:
684 case tok::kw_unsigned:
685 case tok::kw_void:
686 case tok::kw_volatile:
687 case tok::kw__Bool:
688 case tok::kw__Complex:
689 case tok::kw_class:
690 case tok::kw_typename:
691 case tok::kw_wchar_t:
692 case tok::kw_char16_t:
693 case tok::kw_char32_t:
694 case tok::kw_decltype:
695 case tok::kw_thread_local:
696 case tok::kw__Decimal32:
697 case tok::kw__Decimal64:
698 case tok::kw__Decimal128:
699 case tok::kw___thread:
700 case tok::kw_typeof:
701 case tok::kw___cdecl:
702 case tok::kw___stdcall:
703 case tok::kw___fastcall:
704 case tok::kw___thiscall:
705 case tok::kw___vector:
706 case tok::kw___pixel:
707 return TPResult::False();
708
709 default:
710 break;
711 }
712
713 return TPResult::Ambiguous();
714}
715
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000716/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
717/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
718/// be either a decl-specifier or a function-style cast, and TPResult::Error()
719/// if a parsing error was found and reported.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000720///
721/// decl-specifier:
722/// storage-class-specifier
723/// type-specifier
724/// function-specifier
725/// 'friend'
726/// 'typedef'
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000727/// [C++0x] 'constexpr'
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000728/// [GNU] attributes declaration-specifiers[opt]
729///
730/// storage-class-specifier:
731/// 'register'
732/// 'static'
733/// 'extern'
734/// 'mutable'
735/// 'auto'
736/// [GNU] '__thread'
737///
738/// function-specifier:
739/// 'inline'
740/// 'virtual'
741/// 'explicit'
742///
743/// typedef-name:
744/// identifier
745///
746/// type-specifier:
747/// simple-type-specifier
748/// class-specifier
749/// enum-specifier
750/// elaborated-type-specifier
Douglas Gregor333489b2009-03-27 23:10:48 +0000751/// typename-specifier
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000752/// cv-qualifier
753///
754/// simple-type-specifier:
Douglas Gregor333489b2009-03-27 23:10:48 +0000755/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000756/// '::'[opt] nested-name-specifier 'template'
757/// simple-template-id [TODO]
758/// 'char'
759/// 'wchar_t'
760/// 'bool'
761/// 'short'
762/// 'int'
763/// 'long'
764/// 'signed'
765/// 'unsigned'
766/// 'float'
767/// 'double'
768/// 'void'
769/// [GNU] typeof-specifier
770/// [GNU] '_Complex'
771/// [C++0x] 'auto' [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +0000772/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000773///
774/// type-name:
775/// class-name
776/// enum-name
777/// typedef-name
778///
779/// elaborated-type-specifier:
780/// class-key '::'[opt] nested-name-specifier[opt] identifier
781/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
782/// simple-template-id
783/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
784///
785/// enum-name:
786/// identifier
787///
788/// enum-specifier:
789/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
790/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
791///
792/// class-specifier:
793/// class-head '{' member-specification[opt] '}'
794///
795/// class-head:
796/// class-key identifier[opt] base-clause[opt]
797/// class-key nested-name-specifier identifier base-clause[opt]
798/// class-key nested-name-specifier[opt] simple-template-id
799/// base-clause[opt]
800///
801/// class-key:
802/// 'class'
803/// 'struct'
804/// 'union'
805///
806/// cv-qualifier:
807/// 'const'
808/// 'volatile'
809/// [GNU] restrict
810///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000811Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000812 switch (Tok.getKind()) {
Chris Lattnerb7895c42009-01-04 23:33:56 +0000813 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +0000814 // Check for need to substitute AltiVec __vector keyword
815 // for "vector" identifier.
816 if (TryAltiVecVectorToken())
817 return TPResult::True();
818 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +0000819 case tok::kw_typename: // typename T::type
Chris Lattnerb7895c42009-01-04 23:33:56 +0000820 // Annotate typenames and C++ scope specifiers. If we get one, just
821 // recurse to handle whatever we get.
822 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000823 return TPResult::Error();
824 if (Tok.is(tok::identifier))
825 return TPResult::False();
826 return isCXXDeclarationSpecifier();
Chris Lattnerb7895c42009-01-04 23:33:56 +0000827
Chris Lattner25d862b2009-12-19 01:11:05 +0000828 case tok::coloncolon: { // ::foo::bar
829 const Token &Next = NextToken();
830 if (Next.is(tok::kw_new) || // ::new
831 Next.is(tok::kw_delete)) // ::delete
John McCalle2ade282009-12-19 00:35:18 +0000832 return TPResult::False();
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattnerb7895c42009-01-04 23:33:56 +0000834 // Annotate typenames and C++ scope specifiers. If we get one, just
835 // recurse to handle whatever we get.
836 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000837 return TPResult::Error();
838 return isCXXDeclarationSpecifier();
Chris Lattner25d862b2009-12-19 01:11:05 +0000839 }
840
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000841 // decl-specifier:
842 // storage-class-specifier
843 // type-specifier
844 // function-specifier
845 // 'friend'
846 // 'typedef'
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000847 // 'constexpr'
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000848 case tok::kw_friend:
849 case tok::kw_typedef:
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000850 case tok::kw_constexpr:
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000851 // storage-class-specifier
852 case tok::kw_register:
853 case tok::kw_static:
854 case tok::kw_extern:
855 case tok::kw_mutable:
856 case tok::kw_auto:
857 case tok::kw___thread:
858 // function-specifier
859 case tok::kw_inline:
860 case tok::kw_virtual:
861 case tok::kw_explicit:
862
863 // type-specifier:
864 // simple-type-specifier
865 // class-specifier
866 // enum-specifier
867 // elaborated-type-specifier
868 // typename-specifier
869 // cv-qualifier
870
871 // class-specifier
872 // elaborated-type-specifier
873 case tok::kw_class:
874 case tok::kw_struct:
875 case tok::kw_union:
876 // enum-specifier
877 case tok::kw_enum:
878 // cv-qualifier
879 case tok::kw_const:
880 case tok::kw_volatile:
881
882 // GNU
883 case tok::kw_restrict:
884 case tok::kw__Complex:
885 case tok::kw___attribute:
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000886 return TPResult::True();
Mike Stump11289f42009-09-09 15:08:12 +0000887
Steve Naroff1f42c2e2009-01-06 17:40:00 +0000888 // Microsoft
Steve Narofff192fab2009-01-06 19:34:12 +0000889 case tok::kw___declspec:
Steve Naroff1f42c2e2009-01-06 17:40:00 +0000890 case tok::kw___cdecl:
891 case tok::kw___stdcall:
892 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +0000893 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +0000894 case tok::kw___w64:
895 case tok::kw___ptr64:
896 case tok::kw___forceinline:
897 return TPResult::True();
Dawn Perchik335e16b2010-09-03 01:29:35 +0000898
899 // Borland
900 case tok::kw___pascal:
901 return TPResult::True();
John Thompson22334602010-02-05 00:12:22 +0000902
903 // AltiVec
904 case tok::kw___vector:
905 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000906
John McCalle7db86d2010-04-30 03:11:01 +0000907 case tok::annot_template_id: {
908 TemplateIdAnnotation *TemplateId
909 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
910 if (TemplateId->Kind != TNK_Type_template)
911 return TPResult::False();
912 CXXScopeSpec SS;
Douglas Gregore7c20652011-03-02 00:47:37 +0000913 AnnotateTemplateIdTokenAsType();
John McCalle7db86d2010-04-30 03:11:01 +0000914 assert(Tok.is(tok::annot_typename));
915 goto case_typename;
916 }
917
John McCalle2ade282009-12-19 00:35:18 +0000918 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
919 // We've already annotated a scope; try to annotate a type.
John McCall1f476a12010-02-26 08:45:28 +0000920 if (TryAnnotateTypeOrScopeToken())
921 return TPResult::Error();
922 if (!Tok.is(tok::annot_typename))
John McCalle2ade282009-12-19 00:35:18 +0000923 return TPResult::False();
924 // If that succeeded, fallthrough into the generic simple-type-id case.
925
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000926 // The ambiguity resides in a simple-type-specifier/typename-specifier
927 // followed by a '('. The '(' could either be the start of:
928 //
929 // direct-declarator:
930 // '(' declarator ')'
931 //
932 // direct-abstract-declarator:
933 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
934 // exception-specification[opt]
935 // '(' abstract-declarator ')'
936 //
937 // or part of a function-style cast expression:
938 //
939 // simple-type-specifier '(' expression-list[opt] ')'
940 //
941
942 // simple-type-specifier:
943
Douglas Gregor06e41ae2010-10-21 23:17:00 +0000944 case tok::annot_typename:
945 case_typename:
946 // In Objective-C, we might have a protocol-qualified type.
947 if (getLang().ObjC1 && NextToken().is(tok::less)) {
948 // Tentatively parse the
949 TentativeParsingAction PA(*this);
950 ConsumeToken(); // The type token
951
952 TPResult TPR = TryParseProtocolQualifiers();
953 bool isFollowedByParen = Tok.is(tok::l_paren);
954
955 PA.Revert();
956
957 if (TPR == TPResult::Error())
958 return TPResult::Error();
959
960 if (isFollowedByParen)
961 return TPResult::Ambiguous();
962
963 return TPResult::True();
964 }
965
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000966 case tok::kw_char:
967 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000968 case tok::kw_char16_t:
969 case tok::kw_char32_t:
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000970 case tok::kw_bool:
971 case tok::kw_short:
972 case tok::kw_int:
973 case tok::kw_long:
974 case tok::kw_signed:
975 case tok::kw_unsigned:
976 case tok::kw_float:
977 case tok::kw_double:
978 case tok::kw_void:
979 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000980 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000981
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000982 if (isStartOfObjCClassMessageMissingOpenBracket())
983 return TPResult::False();
984
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000985 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000986
Anders Carlsson74948d02009-06-24 17:47:40 +0000987 // GNU typeof support.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000988 case tok::kw_typeof: {
989 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000990 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000991
992 TentativeParsingAction PA(*this);
993
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000994 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000995 bool isFollowedByParen = Tok.is(tok::l_paren);
996
997 PA.Revert();
998
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +0000999 if (TPR == TPResult::Error())
1000 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001001
1002 if (isFollowedByParen)
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001003 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001004
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001005 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001006 }
1007
Anders Carlsson74948d02009-06-24 17:47:40 +00001008 // C++0x decltype support.
1009 case tok::kw_decltype:
1010 return TPResult::True();
1011
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001012 default:
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001013 return TPResult::False();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001014 }
1015}
1016
1017/// [GNU] typeof-specifier:
1018/// 'typeof' '(' expressions ')'
1019/// 'typeof' '(' type-name ')'
1020///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001021Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001022 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1023 ConsumeToken();
1024
1025 assert(Tok.is(tok::l_paren) && "Expected '('");
1026 // Parse through the parens after 'typeof'.
1027 ConsumeParen();
1028 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001029 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001030
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001031 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001032}
1033
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001034/// [ObjC] protocol-qualifiers:
1035//// '<' identifier-list '>'
1036Parser::TPResult Parser::TryParseProtocolQualifiers() {
1037 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1038 ConsumeToken();
1039 do {
1040 if (Tok.isNot(tok::identifier))
1041 return TPResult::Error();
1042 ConsumeToken();
1043
1044 if (Tok.is(tok::comma)) {
1045 ConsumeToken();
1046 continue;
1047 }
1048
1049 if (Tok.is(tok::greater)) {
1050 ConsumeToken();
1051 return TPResult::Ambiguous();
1052 }
1053 } while (false);
1054
1055 return TPResult::Error();
1056}
1057
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001058Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1059 TPResult TPR = isCXXDeclarationSpecifier();
1060 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001061 return TPR;
1062
1063 if (Tok.is(tok::kw_typeof))
1064 TryParseTypeofSpecifier();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001065 else {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001066 ConsumeToken();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001067
1068 if (getLang().ObjC1 && Tok.is(tok::less))
1069 TryParseProtocolQualifiers();
1070 }
Mike Stump11289f42009-09-09 15:08:12 +00001071
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001072 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001073 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001074}
1075
1076/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1077/// a constructor-style initializer, when parsing declaration statements.
1078/// Returns true for function declarator and false for constructor-style
1079/// initializer.
1080/// If during the disambiguation process a parsing error is encountered,
1081/// the function returns true to let the declaration parsing code handle it.
1082///
1083/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1084/// exception-specification[opt]
1085///
Argyrios Kyrtzidis3a0558a2008-10-17 23:23:35 +00001086bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidis279d9812008-10-05 21:10:08 +00001087
1088 // C++ 8.2p1:
1089 // The ambiguity arising from the similarity between a function-style cast and
1090 // a declaration mentioned in 6.8 can also occur in the context of a
1091 // declaration. In that context, the choice is between a function declaration
1092 // with a redundant set of parentheses around a parameter name and an object
1093 // declaration with a function-style cast as the initializer. Just as for the
1094 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1095 // that could possibly be a declaration a declaration.
1096
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001097 TentativeParsingAction PA(*this);
1098
1099 ConsumeParen();
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001100 TPResult TPR = TryParseParameterDeclarationClause();
1101 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1102 TPR = TPResult::False();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001103
Argyrios Kyrtzidis84a4df82008-10-15 23:21:32 +00001104 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001105 PA.Revert();
1106
1107 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001108 if (TPR == TPResult::Error())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001109 return true;
1110
Argyrios Kyrtzidis84a4df82008-10-15 23:21:32 +00001111 if (TPR == TPResult::Ambiguous()) {
1112 // Function declarator has precedence over constructor-style initializer.
1113 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidis3a0558a2008-10-17 23:23:35 +00001114 if (warnIfAmbiguous)
Chris Lattner3d31c6c2008-11-18 07:50:21 +00001115 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1116 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001117 return true;
Argyrios Kyrtzidis84a4df82008-10-15 23:21:32 +00001118 }
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001119
1120 return TPR == TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001121}
1122
1123/// parameter-declaration-clause:
1124/// parameter-declaration-list[opt] '...'[opt]
1125/// parameter-declaration-list ',' '...'
1126///
1127/// parameter-declaration-list:
1128/// parameter-declaration
1129/// parameter-declaration-list ',' parameter-declaration
1130///
1131/// parameter-declaration:
Argyrios Kyrtzidisac3b3e82010-12-08 02:02:46 +00001132/// decl-specifier-seq declarator attributes[opt]
1133/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1134/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1135/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1136/// '=' assignment-expression
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001137///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001138Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001139
1140 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001141 return TPResult::True();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001142
1143 // parameter-declaration-list[opt] '...'[opt]
1144 // parameter-declaration-list ',' '...'
1145 //
1146 // parameter-declaration-list:
1147 // parameter-declaration
1148 // parameter-declaration-list ',' parameter-declaration
1149 //
1150 while (1) {
1151 // '...'[opt]
1152 if (Tok.is(tok::ellipsis)) {
1153 ConsumeToken();
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001154 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001155 }
1156
John McCall084e83d2011-03-24 11:26:52 +00001157 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001158 MaybeParseMicrosoftAttributes(attrs);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00001159
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001160 // decl-specifier-seq
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001161 TPResult TPR = TryParseDeclarationSpecifier();
1162 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001163 return TPR;
1164
1165 // declarator
1166 // abstract-declarator[opt]
1167 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001168 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001169 return TPR;
1170
Argyrios Kyrtzidisac3b3e82010-12-08 02:02:46 +00001171 // [GNU] attributes[opt]
1172 if (Tok.is(tok::kw___attribute))
1173 return TPResult::True();
1174
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001175 if (Tok.is(tok::equal)) {
1176 // '=' assignment-expression
1177 // Parse through assignment-expression.
Douglas Gregor27b4c162010-12-23 22:44:42 +00001178 tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1179 if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001180 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001181 }
1182
1183 if (Tok.is(tok::ellipsis)) {
1184 ConsumeToken();
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001185 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001186 }
1187
1188 if (Tok.isNot(tok::comma))
1189 break;
1190 ConsumeToken(); // the comma.
1191 }
1192
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001193 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001194}
1195
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +00001196/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1197/// parsing as a function declarator.
1198/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1199/// return TPResult::Ambiguous(), otherwise it will return either False() or
1200/// Error().
Mike Stump11289f42009-09-09 15:08:12 +00001201///
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001202/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1203/// exception-specification[opt]
1204///
1205/// exception-specification:
1206/// 'throw' '(' type-id-list[opt] ')'
1207///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001208Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidis4217c7e2008-10-05 23:15:41 +00001209
1210 // The '(' is already parsed.
1211
1212 TPResult TPR = TryParseParameterDeclarationClause();
1213 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1214 TPR = TPResult::False();
1215
1216 if (TPR == TPResult::False() || TPR == TPResult::Error())
1217 return TPR;
1218
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001219 // Parse through the parens.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001220 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001221 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001222
1223 // cv-qualifier-seq
1224 while (Tok.is(tok::kw_const) ||
1225 Tok.is(tok::kw_volatile) ||
1226 Tok.is(tok::kw_restrict) )
1227 ConsumeToken();
1228
Douglas Gregor54e462a2011-01-26 16:50:54 +00001229 // ref-qualifier[opt]
1230 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1231 ConsumeToken();
1232
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001233 // exception-specification
1234 if (Tok.is(tok::kw_throw)) {
1235 ConsumeToken();
1236 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001237 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001238
1239 // Parse through the parens after 'throw'.
1240 ConsumeParen();
1241 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001242 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001243 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +00001244 if (Tok.is(tok::kw_noexcept)) {
1245 ConsumeToken();
1246 // Possibly an expression as well.
1247 if (Tok.is(tok::l_paren)) {
1248 // Find the matching rparen.
1249 ConsumeParen();
1250 if (!SkipUntil(tok::r_paren))
1251 return TPResult::Error();
1252 }
1253 }
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001254
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001255 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001256}
1257
1258/// '[' constant-expression[opt] ']'
1259///
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001260Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001261 ConsumeBracket();
1262 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001263 return TPResult::Error();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001264
Argyrios Kyrtzidisdf788f42008-10-05 18:52:21 +00001265 return TPResult::Ambiguous();
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001266}