blob: 210d179db1fa50c0962de9f99c15e4d5f1e65583 [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:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +000061 case tok::kw__Static_assert:
Anders Carlsson511d7ab2009-03-11 16:27:10 +000062 return true;
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000063 // simple-declaration
Sean Huntbbd37c62009-11-21 08:43:09 +000064 default:
Argyrios Kyrtzidis5404a152008-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 Kyrtzidisb9f34192008-10-05 18:52:21 +0000100 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
101 // a case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000102
Argyrios Kyrtzidisb9f34192008-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 Kyrtzidis5404a152008-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 Kyrtzidis5404a152008-10-05 00:06:24 +0000115 TPR = TryParseSimpleDeclaration();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000116 PA.Revert();
117
118 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000119 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000120 return true;
121
122 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000123 if (TPR == TPResult::Ambiguous())
124 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000125
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000126 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000127 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000128}
129
130/// simple-declaration:
131/// decl-specifier-seq init-declarator-list[opt] ';'
132///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000133Parser::TPResult Parser::TryParseSimpleDeclaration() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000134 // We know that we have a simple-type-specifier/typename-specifier followed
135 // by a '('.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000136 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000137
138 if (Tok.is(tok::kw_typeof))
139 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000140 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000141 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000142
143 if (getLang().ObjC1 && Tok.is(tok::less))
144 TryParseProtocolQualifiers();
145 }
146
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000147 assert(Tok.is(tok::l_paren) && "Expected '('");
148
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000149 TPResult TPR = TryParseInitDeclaratorList();
150 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000151 return TPR;
152
153 if (Tok.isNot(tok::semi))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000154 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000155
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000156 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000157}
158
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000159/// init-declarator-list:
160/// init-declarator
161/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000162///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000163/// init-declarator:
164/// declarator initializer[opt]
165/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-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 Kyrtzidisb9f34192008-10-05 18:52:21 +0000176Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000177 while (1) {
178 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000179 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
180 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000181 return TPR;
182
Argyrios Kyrtzidis1ee2c432008-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 Kyrtzidisb9f34192008-10-05 18:52:21 +0000185 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000186
Argyrios Kyrtzidis5404a152008-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 Kyrtzidisb9f34192008-10-05 18:52:21 +0000192 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000193 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-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 Kyrtzidis5404a152008-10-05 00:06:24 +0000198 //
Douglas Gregor06b70802010-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 Jahanianf459beb2010-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 Gregor06b70802010-07-15 21:05:01 +0000208 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000209 }
210
211 if (Tok.isNot(tok::comma))
212 break;
213 ConsumeToken(); // the comma.
214 }
215
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000216 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000217}
218
Argyrios Kyrtzidisa8a45982008-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 Kyrtzidisb9f34192008-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 Kyrtzidisa8a45982008-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 Gregor9bd1d8d2010-10-21 23:17:00 +0000247 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000248 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000249
250 if (getLang().ObjC1 && Tok.is(tok::less))
251 TryParseProtocolQualifiers();
252 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000253 assert(Tok.is(tok::l_paren) && "Expected '('");
254
255 // declarator
256 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
257
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000258 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000259 if (TPR == TPResult::Error())
260 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000261
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000262 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-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 Kyrtzidisb9f34192008-10-05 18:52:21 +0000267 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000268 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000269 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000270 }
271
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000272 PA.Revert();
273
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000274 assert(TPR == TPResult::True() || TPR == TPResult::False());
275 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000276}
277
Mike Stump1eb44332009-09-09 15:08:12 +0000278 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-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 Kyrtzidisf58f45e2009-05-22 10:24:42 +0000295bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000297 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-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 Kyrtzidis78c8d802008-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 Gregor9bd1d8d2010-10-21 23:17:00 +0000322 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000323 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000324
325 if (getLang().ObjC1 && Tok.is(tok::less))
326 TryParseProtocolQualifiers();
327 }
328
Argyrios Kyrtzidis78c8d802008-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 Kyrtzidisf58f45e2009-05-22 10:24:42 +0000341 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000342 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000343 isAmbiguous = true;
344
Douglas Gregor8b642592009-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 Kyrtzidisf58f45e2009-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 Kyrtzidis78c8d802008-10-05 19:56:22 +0000351 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000352 isAmbiguous = true;
353
354 } else
Argyrios Kyrtzidis78c8d802008-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
Sean Huntbbd37c62009-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 ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000380/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000381///
382/// [C++0x] attribute-list:
383/// attribute[opt]
384/// attribute-list ',' attribute[opt]
385///
386/// [C++0x] attribute:
387/// attribute-token attribute-argument-clause[opt]
388///
389/// [C++0x] attribute-token:
390/// identifier
391/// attribute-scoped-token
392///
393/// [C++0x] attribute-scoped-token:
394/// attribute-namespace '::' identifier
395///
396/// [C++0x] attribute-namespace:
397/// identifier
398///
399/// [C++0x] attribute-argument-clause:
400/// '(' balanced-token-seq ')'
401///
402/// [C++0x] balanced-token-seq:
403/// balanced-token
404/// balanced-token-seq balanced-token
405///
406/// [C++0x] balanced-token:
407/// '(' balanced-token-seq ')'
408/// '[' balanced-token-seq ']'
409/// '{' balanced-token-seq '}'
410/// any token but '(', ')', '[', ']', '{', or '}'
411bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
412 tok::TokenKind *After) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000413 if (Tok.is(tok::kw_alignas))
414 return true;
415
Sean Huntbbd37c62009-11-21 08:43:09 +0000416 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
417 return false;
418
419 // No tentative parsing if we don't need to look for ]]
420 if (!CheckClosing && !getLang().ObjC1)
421 return true;
422
423 struct TentativeReverter {
424 TentativeParsingAction PA;
425
426 TentativeReverter (Parser& P)
427 : PA(P)
428 {}
429 ~TentativeReverter () {
430 PA.Revert();
431 }
432 } R(*this);
433
434 // Opening brackets were checked for above.
435 ConsumeBracket();
436 ConsumeBracket();
437
438 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
439 SkipUntil(tok::r_square, false);
440
441 if (Tok.isNot(tok::r_square))
442 return false;
443 ConsumeBracket();
444
445 if (After)
446 *After = Tok.getKind();
447
448 return true;
449}
450
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000451/// declarator:
452/// direct-declarator
453/// ptr-operator declarator
454///
455/// direct-declarator:
456/// declarator-id
457/// direct-declarator '(' parameter-declaration-clause ')'
458/// cv-qualifier-seq[opt] exception-specification[opt]
459/// direct-declarator '[' constant-expression[opt] ']'
460/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000461/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000462///
463/// abstract-declarator:
464/// ptr-operator abstract-declarator[opt]
465/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000466/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000467///
468/// direct-abstract-declarator:
469/// direct-abstract-declarator[opt]
470/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
471/// exception-specification[opt]
472/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
473/// '(' abstract-declarator ')'
474///
475/// ptr-operator:
476/// '*' cv-qualifier-seq[opt]
477/// '&'
478/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000479/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000480///
481/// cv-qualifier-seq:
482/// cv-qualifier cv-qualifier-seq[opt]
483///
484/// cv-qualifier:
485/// 'const'
486/// 'volatile'
487///
488/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000489/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000490///
491/// id-expression:
492/// unqualified-id
493/// qualified-id [TODO]
494///
495/// unqualified-id:
496/// identifier
497/// operator-function-id [TODO]
498/// conversion-function-id [TODO]
499/// '~' class-name [TODO]
500/// template-id [TODO]
501///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000502Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
503 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000504 // declarator:
505 // direct-declarator
506 // ptr-operator declarator
507
508 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000509 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000510 if (TryAnnotateCXXScopeToken(true))
511 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000512
Chris Lattner9af55002009-03-27 04:18:06 +0000513 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor69d83162011-01-20 16:08:06 +0000514 Tok.is(tok::ampamp) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000515 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000516 // ptr-operator
517 ConsumeToken();
518 while (Tok.is(tok::kw_const) ||
519 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000520 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000521 ConsumeToken();
522 } else {
523 break;
524 }
525 }
526
527 // direct-declarator:
528 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000529 if (Tok.is(tok::ellipsis))
530 ConsumeToken();
531
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000532 if ((Tok.is(tok::identifier) ||
533 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
534 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000535 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000536 if (Tok.is(tok::annot_cxxscope))
537 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000538 ConsumeToken();
539 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000540 ConsumeParen();
541 if (mayBeAbstract &&
542 (Tok.is(tok::r_paren) || // 'int()' is a function.
543 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
544 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000545 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
546 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000547 TPResult TPR = TryParseFunctionDeclarator();
548 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000549 return TPR;
550 } else {
551 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000552 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000553 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000554 if (Tok.is(tok::kw___attribute) ||
555 Tok.is(tok::kw___declspec) ||
556 Tok.is(tok::kw___cdecl) ||
557 Tok.is(tok::kw___stdcall) ||
558 Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000559 Tok.is(tok::kw___thiscall) ||
Douglas Gregor8d267c52011-09-09 02:06:17 +0000560 Tok.is(tok::kw___unaligned))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000561 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000562 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000563 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000564 return TPR;
565 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000566 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000567 ConsumeParen();
568 }
569 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000570 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000571 }
572
573 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000574 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000575
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000576 // abstract-declarator: ...
577 if (Tok.is(tok::ellipsis))
578 ConsumeToken();
579
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000580 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000581 // Check whether we have a function declarator or a possible ctor-style
582 // initializer that follows the declarator. Note that ctor-style
583 // initializers are not possible in contexts where abstract declarators
584 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000585 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000586 break;
587
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000588 // direct-declarator '(' parameter-declaration-clause ')'
589 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000590 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000591 TPR = TryParseFunctionDeclarator();
592 } else if (Tok.is(tok::l_square)) {
593 // direct-declarator '[' constant-expression[opt] ']'
594 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
595 TPR = TryParseBracketDeclarator();
596 } else {
597 break;
598 }
599
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000600 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000601 return TPR;
602 }
603
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000604 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000605}
606
Douglas Gregora61b3e72010-12-01 17:42:47 +0000607Parser::TPResult
608Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
609 switch (Kind) {
610 // Obviously starts an expression.
611 case tok::numeric_constant:
612 case tok::char_constant:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000613 case tok::wide_char_constant:
614 case tok::utf16_char_constant:
615 case tok::utf32_char_constant:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000616 case tok::string_literal:
617 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000618 case tok::utf8_string_literal:
619 case tok::utf16_string_literal:
620 case tok::utf32_string_literal:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000621 case tok::l_square:
622 case tok::l_paren:
623 case tok::amp:
Douglas Gregor69d83162011-01-20 16:08:06 +0000624 case tok::ampamp:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000625 case tok::star:
626 case tok::plus:
627 case tok::plusplus:
628 case tok::minus:
629 case tok::minusminus:
630 case tok::tilde:
631 case tok::exclaim:
632 case tok::kw_sizeof:
633 case tok::kw___func__:
634 case tok::kw_const_cast:
635 case tok::kw_delete:
636 case tok::kw_dynamic_cast:
637 case tok::kw_false:
638 case tok::kw_new:
639 case tok::kw_operator:
640 case tok::kw_reinterpret_cast:
641 case tok::kw_static_cast:
642 case tok::kw_this:
643 case tok::kw_throw:
644 case tok::kw_true:
645 case tok::kw_typeid:
646 case tok::kw_alignof:
647 case tok::kw_noexcept:
648 case tok::kw_nullptr:
649 case tok::kw___null:
650 case tok::kw___alignof:
651 case tok::kw___builtin_choose_expr:
652 case tok::kw___builtin_offsetof:
653 case tok::kw___builtin_types_compatible_p:
654 case tok::kw___builtin_va_arg:
655 case tok::kw___imag:
656 case tok::kw___real:
657 case tok::kw___FUNCTION__:
658 case tok::kw___PRETTY_FUNCTION__:
659 case tok::kw___has_nothrow_assign:
660 case tok::kw___has_nothrow_copy:
661 case tok::kw___has_nothrow_constructor:
662 case tok::kw___has_trivial_assign:
663 case tok::kw___has_trivial_copy:
664 case tok::kw___has_trivial_constructor:
665 case tok::kw___has_trivial_destructor:
666 case tok::kw___has_virtual_destructor:
667 case tok::kw___is_abstract:
668 case tok::kw___is_base_of:
669 case tok::kw___is_class:
Douglas Gregor9f361132011-01-27 20:28:01 +0000670 case tok::kw___is_convertible_to:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000671 case tok::kw___is_empty:
672 case tok::kw___is_enum:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000673 case tok::kw___is_literal:
Chandler Carruth38402812011-04-24 02:49:28 +0000674 case tok::kw___is_literal_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000675 case tok::kw___is_pod:
676 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +0000677 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +0000678 case tok::kw___is_trivially_copyable:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000679 case tok::kw___is_union:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000680 case tok::kw___uuidof:
681 return TPResult::True();
682
683 // Obviously starts a type-specifier-seq:
684 case tok::kw_char:
685 case tok::kw_const:
686 case tok::kw_double:
687 case tok::kw_enum:
688 case tok::kw_float:
689 case tok::kw_int:
690 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000691 case tok::kw___int64:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000692 case tok::kw_restrict:
693 case tok::kw_short:
694 case tok::kw_signed:
695 case tok::kw_struct:
696 case tok::kw_union:
697 case tok::kw_unsigned:
698 case tok::kw_void:
699 case tok::kw_volatile:
700 case tok::kw__Bool:
701 case tok::kw__Complex:
702 case tok::kw_class:
703 case tok::kw_typename:
704 case tok::kw_wchar_t:
705 case tok::kw_char16_t:
706 case tok::kw_char32_t:
707 case tok::kw_decltype:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000708 case tok::kw___underlying_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000709 case tok::kw_thread_local:
710 case tok::kw__Decimal32:
711 case tok::kw__Decimal64:
712 case tok::kw__Decimal128:
713 case tok::kw___thread:
714 case tok::kw_typeof:
715 case tok::kw___cdecl:
716 case tok::kw___stdcall:
717 case tok::kw___fastcall:
718 case tok::kw___thiscall:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000719 case tok::kw___unaligned:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000720 case tok::kw___vector:
721 case tok::kw___pixel:
722 return TPResult::False();
723
724 default:
725 break;
726 }
727
728 return TPResult::Ambiguous();
729}
730
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000731/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
732/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
733/// be either a decl-specifier or a function-style cast, and TPResult::Error()
734/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000735///
736/// decl-specifier:
737/// storage-class-specifier
738/// type-specifier
739/// function-specifier
740/// 'friend'
741/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000742/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000743/// [GNU] attributes declaration-specifiers[opt]
744///
745/// storage-class-specifier:
746/// 'register'
747/// 'static'
748/// 'extern'
749/// 'mutable'
750/// 'auto'
751/// [GNU] '__thread'
752///
753/// function-specifier:
754/// 'inline'
755/// 'virtual'
756/// 'explicit'
757///
758/// typedef-name:
759/// identifier
760///
761/// type-specifier:
762/// simple-type-specifier
763/// class-specifier
764/// enum-specifier
765/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000766/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000767/// cv-qualifier
768///
769/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000770/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000771/// '::'[opt] nested-name-specifier 'template'
772/// simple-template-id [TODO]
773/// 'char'
774/// 'wchar_t'
775/// 'bool'
776/// 'short'
777/// 'int'
778/// 'long'
779/// 'signed'
780/// 'unsigned'
781/// 'float'
782/// 'double'
783/// 'void'
784/// [GNU] typeof-specifier
785/// [GNU] '_Complex'
786/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000787/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000788///
789/// type-name:
790/// class-name
791/// enum-name
792/// typedef-name
793///
794/// elaborated-type-specifier:
795/// class-key '::'[opt] nested-name-specifier[opt] identifier
796/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
797/// simple-template-id
798/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
799///
800/// enum-name:
801/// identifier
802///
803/// enum-specifier:
804/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
805/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
806///
807/// class-specifier:
808/// class-head '{' member-specification[opt] '}'
809///
810/// class-head:
811/// class-key identifier[opt] base-clause[opt]
812/// class-key nested-name-specifier identifier base-clause[opt]
813/// class-key nested-name-specifier[opt] simple-template-id
814/// base-clause[opt]
815///
816/// class-key:
817/// 'class'
818/// 'struct'
819/// 'union'
820///
821/// cv-qualifier:
822/// 'const'
823/// 'volatile'
824/// [GNU] restrict
825///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000826Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000827 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000828 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000829 // Check for need to substitute AltiVec __vector keyword
830 // for "vector" identifier.
831 if (TryAltiVecVectorToken())
832 return TPResult::True();
833 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000834 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000835 // Annotate typenames and C++ scope specifiers. If we get one, just
836 // recurse to handle whatever we get.
837 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000838 return TPResult::Error();
839 if (Tok.is(tok::identifier))
840 return TPResult::False();
841 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000842
Chris Lattner2ee9b402009-12-19 01:11:05 +0000843 case tok::coloncolon: { // ::foo::bar
844 const Token &Next = NextToken();
845 if (Next.is(tok::kw_new) || // ::new
846 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000847 return TPResult::False();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Chris Lattnere5849262009-01-04 23:33:56 +0000849 // Annotate typenames and C++ scope specifiers. If we get one, just
850 // recurse to handle whatever we get.
851 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000852 return TPResult::Error();
853 return isCXXDeclarationSpecifier();
Chris Lattner2ee9b402009-12-19 01:11:05 +0000854 }
855
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000856 // decl-specifier:
857 // storage-class-specifier
858 // type-specifier
859 // function-specifier
860 // 'friend'
861 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000862 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000863 case tok::kw_friend:
864 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000865 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000866 // storage-class-specifier
867 case tok::kw_register:
868 case tok::kw_static:
869 case tok::kw_extern:
870 case tok::kw_mutable:
871 case tok::kw_auto:
872 case tok::kw___thread:
873 // function-specifier
874 case tok::kw_inline:
875 case tok::kw_virtual:
876 case tok::kw_explicit:
877
Douglas Gregor8d267c52011-09-09 02:06:17 +0000878 // Modules
879 case tok::kw___module_private__:
880
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000881 // type-specifier:
882 // simple-type-specifier
883 // class-specifier
884 // enum-specifier
885 // elaborated-type-specifier
886 // typename-specifier
887 // cv-qualifier
888
889 // class-specifier
890 // elaborated-type-specifier
891 case tok::kw_class:
892 case tok::kw_struct:
893 case tok::kw_union:
894 // enum-specifier
895 case tok::kw_enum:
896 // cv-qualifier
897 case tok::kw_const:
898 case tok::kw_volatile:
899
900 // GNU
901 case tok::kw_restrict:
902 case tok::kw__Complex:
903 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000904 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Steve Naroff7ec56582009-01-06 17:40:00 +0000906 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000907 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000908 case tok::kw___cdecl:
909 case tok::kw___stdcall:
910 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000911 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000912 case tok::kw___w64:
913 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +0000914 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +0000915 case tok::kw___forceinline:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000916 case tok::kw___unaligned:
Eli Friedman290eeb02009-06-08 23:27:34 +0000917 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000918
919 // Borland
920 case tok::kw___pascal:
921 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000922
923 // AltiVec
924 case tok::kw___vector:
925 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000926
John McCall51e2a5d2010-04-30 03:11:01 +0000927 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000928 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
John McCall51e2a5d2010-04-30 03:11:01 +0000929 if (TemplateId->Kind != TNK_Type_template)
930 return TPResult::False();
931 CXXScopeSpec SS;
Douglas Gregor059101f2011-03-02 00:47:37 +0000932 AnnotateTemplateIdTokenAsType();
John McCall51e2a5d2010-04-30 03:11:01 +0000933 assert(Tok.is(tok::annot_typename));
934 goto case_typename;
935 }
936
John McCallae03cb52009-12-19 00:35:18 +0000937 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
938 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000939 if (TryAnnotateTypeOrScopeToken())
940 return TPResult::Error();
941 if (!Tok.is(tok::annot_typename))
John McCallae03cb52009-12-19 00:35:18 +0000942 return TPResult::False();
943 // If that succeeded, fallthrough into the generic simple-type-id case.
944
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000945 // The ambiguity resides in a simple-type-specifier/typename-specifier
946 // followed by a '('. The '(' could either be the start of:
947 //
948 // direct-declarator:
949 // '(' declarator ')'
950 //
951 // direct-abstract-declarator:
952 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
953 // exception-specification[opt]
954 // '(' abstract-declarator ')'
955 //
956 // or part of a function-style cast expression:
957 //
958 // simple-type-specifier '(' expression-list[opt] ')'
959 //
960
961 // simple-type-specifier:
962
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000963 case tok::annot_typename:
964 case_typename:
965 // In Objective-C, we might have a protocol-qualified type.
966 if (getLang().ObjC1 && NextToken().is(tok::less)) {
967 // Tentatively parse the
968 TentativeParsingAction PA(*this);
969 ConsumeToken(); // The type token
970
971 TPResult TPR = TryParseProtocolQualifiers();
972 bool isFollowedByParen = Tok.is(tok::l_paren);
973
974 PA.Revert();
975
976 if (TPR == TPResult::Error())
977 return TPResult::Error();
978
979 if (isFollowedByParen)
980 return TPResult::Ambiguous();
981
982 return TPResult::True();
983 }
984
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000985 case tok::kw_char:
986 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000987 case tok::kw_char16_t:
988 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000989 case tok::kw_bool:
990 case tok::kw_short:
991 case tok::kw_int:
992 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000993 case tok::kw___int64:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000994 case tok::kw_signed:
995 case tok::kw_unsigned:
996 case tok::kw_float:
997 case tok::kw_double:
998 case tok::kw_void:
999 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001000 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001001
Douglas Gregor9497a732010-09-16 01:51:54 +00001002 if (isStartOfObjCClassMessageMissingOpenBracket())
1003 return TPResult::False();
1004
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001005 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001006
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001007 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001008 case tok::kw_typeof: {
1009 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001010 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001011
1012 TentativeParsingAction PA(*this);
1013
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001014 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001015 bool isFollowedByParen = Tok.is(tok::l_paren);
1016
1017 PA.Revert();
1018
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001019 if (TPR == TPResult::Error())
1020 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001021
1022 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001023 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001024
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001025 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001026 }
1027
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001028 // C++0x decltype support.
1029 case tok::kw_decltype:
1030 return TPResult::True();
1031
Sean Huntdb5d44b2011-05-19 05:37:45 +00001032 // C++0x type traits support
1033 case tok::kw___underlying_type:
1034 return TPResult::True();
1035
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001036 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001037 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001038 }
1039}
1040
1041/// [GNU] typeof-specifier:
1042/// 'typeof' '(' expressions ')'
1043/// 'typeof' '(' type-name ')'
1044///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001045Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001046 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1047 ConsumeToken();
1048
1049 assert(Tok.is(tok::l_paren) && "Expected '('");
1050 // Parse through the parens after 'typeof'.
1051 ConsumeParen();
1052 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001053 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001054
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001055 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001056}
1057
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001058/// [ObjC] protocol-qualifiers:
1059//// '<' identifier-list '>'
1060Parser::TPResult Parser::TryParseProtocolQualifiers() {
1061 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1062 ConsumeToken();
1063 do {
1064 if (Tok.isNot(tok::identifier))
1065 return TPResult::Error();
1066 ConsumeToken();
1067
1068 if (Tok.is(tok::comma)) {
1069 ConsumeToken();
1070 continue;
1071 }
1072
1073 if (Tok.is(tok::greater)) {
1074 ConsumeToken();
1075 return TPResult::Ambiguous();
1076 }
1077 } while (false);
1078
1079 return TPResult::Error();
1080}
1081
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001082Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1083 TPResult TPR = isCXXDeclarationSpecifier();
1084 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001085 return TPR;
1086
1087 if (Tok.is(tok::kw_typeof))
1088 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001089 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001090 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001091
1092 if (getLang().ObjC1 && Tok.is(tok::less))
1093 TryParseProtocolQualifiers();
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001096 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001097 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001098}
1099
1100/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1101/// a constructor-style initializer, when parsing declaration statements.
1102/// Returns true for function declarator and false for constructor-style
1103/// initializer.
1104/// If during the disambiguation process a parsing error is encountered,
1105/// the function returns true to let the declaration parsing code handle it.
1106///
1107/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1108/// exception-specification[opt]
1109///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001110bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001111
1112 // C++ 8.2p1:
1113 // The ambiguity arising from the similarity between a function-style cast and
1114 // a declaration mentioned in 6.8 can also occur in the context of a
1115 // declaration. In that context, the choice is between a function declaration
1116 // with a redundant set of parentheses around a parameter name and an object
1117 // declaration with a function-style cast as the initializer. Just as for the
1118 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1119 // that could possibly be a declaration a declaration.
1120
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001121 TentativeParsingAction PA(*this);
1122
1123 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001124 TPResult TPR = TryParseParameterDeclarationClause();
1125 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1126 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001127
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001128 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001129 PA.Revert();
1130
1131 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001132 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001133 return true;
1134
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001135 if (TPR == TPResult::Ambiguous()) {
1136 // Function declarator has precedence over constructor-style initializer.
1137 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001138 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001139 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1140 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001141 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001142 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001143
1144 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001145}
1146
1147/// parameter-declaration-clause:
1148/// parameter-declaration-list[opt] '...'[opt]
1149/// parameter-declaration-list ',' '...'
1150///
1151/// parameter-declaration-list:
1152/// parameter-declaration
1153/// parameter-declaration-list ',' parameter-declaration
1154///
1155/// parameter-declaration:
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001156/// decl-specifier-seq declarator attributes[opt]
1157/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1158/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1159/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1160/// '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001161///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001162Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001163
1164 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001165 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001166
1167 // parameter-declaration-list[opt] '...'[opt]
1168 // parameter-declaration-list ',' '...'
1169 //
1170 // parameter-declaration-list:
1171 // parameter-declaration
1172 // parameter-declaration-list ',' parameter-declaration
1173 //
1174 while (1) {
1175 // '...'[opt]
1176 if (Tok.is(tok::ellipsis)) {
1177 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001178 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001179 }
1180
John McCall0b7e6782011-03-24 11:26:52 +00001181 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001182 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001183
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001184 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001185 TPResult TPR = TryParseDeclarationSpecifier();
1186 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001187 return TPR;
1188
1189 // declarator
1190 // abstract-declarator[opt]
1191 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001192 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001193 return TPR;
1194
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001195 // [GNU] attributes[opt]
1196 if (Tok.is(tok::kw___attribute))
1197 return TPResult::True();
1198
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001199 if (Tok.is(tok::equal)) {
1200 // '=' assignment-expression
1201 // Parse through assignment-expression.
Douglas Gregora8bc8c92010-12-23 22:44:42 +00001202 tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1203 if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001204 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001205 }
1206
1207 if (Tok.is(tok::ellipsis)) {
1208 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001209 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001210 }
1211
1212 if (Tok.isNot(tok::comma))
1213 break;
1214 ConsumeToken(); // the comma.
1215 }
1216
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001217 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001218}
1219
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001220/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1221/// parsing as a function declarator.
1222/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1223/// return TPResult::Ambiguous(), otherwise it will return either False() or
1224/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001225///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001226/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1227/// exception-specification[opt]
1228///
1229/// exception-specification:
1230/// 'throw' '(' type-id-list[opt] ')'
1231///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001232Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001233
1234 // The '(' is already parsed.
1235
1236 TPResult TPR = TryParseParameterDeclarationClause();
1237 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1238 TPR = TPResult::False();
1239
1240 if (TPR == TPResult::False() || TPR == TPResult::Error())
1241 return TPR;
1242
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001243 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001244 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001245 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001246
1247 // cv-qualifier-seq
1248 while (Tok.is(tok::kw_const) ||
1249 Tok.is(tok::kw_volatile) ||
1250 Tok.is(tok::kw_restrict) )
1251 ConsumeToken();
1252
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001253 // ref-qualifier[opt]
1254 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1255 ConsumeToken();
1256
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001257 // exception-specification
1258 if (Tok.is(tok::kw_throw)) {
1259 ConsumeToken();
1260 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001261 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001262
1263 // Parse through the parens after 'throw'.
1264 ConsumeParen();
1265 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001266 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001267 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001268 if (Tok.is(tok::kw_noexcept)) {
1269 ConsumeToken();
1270 // Possibly an expression as well.
1271 if (Tok.is(tok::l_paren)) {
1272 // Find the matching rparen.
1273 ConsumeParen();
1274 if (!SkipUntil(tok::r_paren))
1275 return TPResult::Error();
1276 }
1277 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001278
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001279 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001280}
1281
1282/// '[' constant-expression[opt] ']'
1283///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001284Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001285 ConsumeBracket();
1286 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001287 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001288
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001289 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001290}