blob: 987ae65d066b45c5f7ceceb591fd5c2b42d3f6cf [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:
Eli Friedman9490ab42011-12-20 01:50:37 +000065 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000066 }
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///
Eli Friedman9490ab42011-12-20 01:50:37 +000078/// (if AllowForRangeDecl specified)
79/// for ( for-range-declaration : for-range-initializer ) statement
80/// for-range-declaration:
81/// attribute-specifier-seqopt type-specifier-seq declarator
82bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000083 // C++ 6.8p1:
84 // There is an ambiguity in the grammar involving expression-statements and
85 // declarations: An expression-statement with a function-style explicit type
86 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87 // from a declaration where the first declarator starts with a '('. In those
88 // cases the statement is a declaration. [Note: To disambiguate, the whole
89 // statement might have to be examined to determine if it is an
90 // expression-statement or a declaration].
91
92 // C++ 6.8p3:
93 // The disambiguation is purely syntactic; that is, the meaning of the names
94 // occurring in such a statement, beyond whether they are type-names or not,
95 // is not generally used in or changed by the disambiguation. Class
96 // templates are instantiated as necessary to determine if a qualified name
97 // is a type-name. Disambiguation precedes parsing, and a statement
98 // disambiguated as a declaration may be an ill-formed declaration.
99
100 // We don't have to parse all of the decl-specifier-seq part. There's only
101 // an ambiguity if the first decl-specifier is
102 // simple-type-specifier/typename-specifier followed by a '(', which may
103 // indicate a function-style cast expression.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
105 // a case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000106
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000107 TPResult TPR = isCXXDeclarationSpecifier();
108 if (TPR != TPResult::Ambiguous())
109 return TPR != TPResult::False(); // Returns true for TPResult::True() or
110 // TPResult::Error().
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000111
112 // FIXME: Add statistics about the number of ambiguous statements encountered
113 // and how they were resolved (number of declarations+number of expressions).
114
115 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
116 // We need tentative parsing...
117
118 TentativeParsingAction PA(*this);
Eli Friedman9490ab42011-12-20 01:50:37 +0000119 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000120 PA.Revert();
121
122 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000123 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000124 return true;
125
126 // Declarations take precedence over expressions.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000127 if (TPR == TPResult::Ambiguous())
128 TPR = TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000129
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000130 assert(TPR == TPResult::True() || TPR == TPResult::False());
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000131 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000132}
133
134/// simple-declaration:
135/// decl-specifier-seq init-declarator-list[opt] ';'
136///
Eli Friedman9490ab42011-12-20 01:50:37 +0000137/// (if AllowForRangeDecl specified)
138/// for ( for-range-declaration : for-range-initializer ) statement
139/// for-range-declaration:
140/// attribute-specifier-seqopt type-specifier-seq declarator
141///
142Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000143 // We know that we have a simple-type-specifier/typename-specifier followed
144 // by a '('.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000145 assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000146
147 if (Tok.is(tok::kw_typeof))
148 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000149 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000150 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000151
152 if (getLang().ObjC1 && Tok.is(tok::less))
153 TryParseProtocolQualifiers();
154 }
155
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000156 assert(Tok.is(tok::l_paren) && "Expected '('");
157
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000158 TPResult TPR = TryParseInitDeclaratorList();
159 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000160 return TPR;
161
Eli Friedman9490ab42011-12-20 01:50:37 +0000162 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000163 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000164
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000165 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000166}
167
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000168/// init-declarator-list:
169/// init-declarator
170/// init-declarator-list ',' init-declarator
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000171///
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000172/// init-declarator:
173/// declarator initializer[opt]
174/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000175///
176/// initializer:
177/// '=' initializer-clause
178/// '(' expression-list ')'
179///
180/// initializer-clause:
181/// assignment-expression
182/// '{' initializer-list ','[opt] '}'
183/// '{' '}'
184///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000185Parser::TPResult Parser::TryParseInitDeclaratorList() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000186 while (1) {
187 // declarator
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000188 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
189 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000190 return TPR;
191
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000192 // [GNU] simple-asm-expr[opt] attributes[opt]
193 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000194 return TPResult::True();
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000195
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000196 // initializer[opt]
197 if (Tok.is(tok::l_paren)) {
198 // Parse through the parens.
199 ConsumeParen();
200 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000201 return TPResult::Error();
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000202 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
Douglas Gregor06b70802010-07-15 21:05:01 +0000203 // MSVC and g++ won't examine the rest of declarators if '=' is
204 // encountered; they just conclude that we have a declaration.
205 // EDG parses the initializer completely, which is the proper behavior
206 // for this case.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000207 //
Douglas Gregor06b70802010-07-15 21:05:01 +0000208 // At present, Clang follows MSVC and g++, since the parser does not have
209 // the ability to parse an expression fully without recording the
210 // results of that parse.
Fariborz Jahanianf459beb2010-08-29 17:20:53 +0000211 // Also allow 'in' after on objective-c declaration as in:
212 // for (int (^b)(void) in array). Ideally this should be done in the
213 // context of parsing for-init-statement of a foreach statement only. But,
214 // in any other context 'in' is invalid after a declaration and parser
215 // issues the error regardless of outcome of this decision.
216 // FIXME. Change if above assumption does not hold.
Douglas Gregor06b70802010-07-15 21:05:01 +0000217 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000218 }
219
220 if (Tok.isNot(tok::comma))
221 break;
222 ConsumeToken(); // the comma.
223 }
224
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000225 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000226}
227
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000228/// isCXXConditionDeclaration - Disambiguates between a declaration or an
229/// expression for a condition of a if/switch/while/for statement.
230/// If during the disambiguation process a parsing error is encountered,
231/// the function returns true to let the declaration parsing code handle it.
232///
233/// condition:
234/// expression
235/// type-specifier-seq declarator '=' assignment-expression
236/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
237/// '=' assignment-expression
238///
239bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000240 TPResult TPR = isCXXDeclarationSpecifier();
241 if (TPR != TPResult::Ambiguous())
242 return TPR != TPResult::False(); // Returns true for TPResult::True() or
243 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000244
245 // FIXME: Add statistics about the number of ambiguous statements encountered
246 // and how they were resolved (number of declarations+number of expressions).
247
248 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
249 // We need tentative parsing...
250
251 TentativeParsingAction PA(*this);
252
253 // type-specifier-seq
254 if (Tok.is(tok::kw_typeof))
255 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000256 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000257 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000258
259 if (getLang().ObjC1 && Tok.is(tok::less))
260 TryParseProtocolQualifiers();
261 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000262 assert(Tok.is(tok::l_paren) && "Expected '('");
263
264 // declarator
265 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
266
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000267 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000268 if (TPR == TPResult::Error())
269 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000270
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000271 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000272 // '='
273 // [GNU] simple-asm-expr[opt] attributes[opt]
274 if (Tok.is(tok::equal) ||
275 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000276 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000277 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000278 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000279 }
280
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000281 PA.Revert();
282
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000283 assert(TPR == TPResult::True() || TPR == TPResult::False());
284 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000285}
286
Mike Stump1eb44332009-09-09 15:08:12 +0000287 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000288 ///
289 /// The context parameter states what context we're parsing right
290 /// now, which affects how this routine copes with the token
291 /// following the type-id. If the context is TypeIdInParens, we have
292 /// already parsed the '(' and we will cease lookahead when we hit
293 /// the corresponding ')'. If the context is
294 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
295 /// before this template argument, and will cease lookahead when we
296 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
297 /// and false for an expression. If during the disambiguation
298 /// process a parsing error is encountered, the function returns
299 /// true to let the declaration parsing code handle it.
300 ///
301 /// type-id:
302 /// type-specifier-seq abstract-declarator[opt]
303 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000304bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000306 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000307
308 // C++ 8.2p2:
309 // The ambiguity arising from the similarity between a function-style cast and
310 // a type-id can occur in different contexts. The ambiguity appears as a
311 // choice between a function-style cast expression and a declaration of a
312 // type. The resolution is that any construct that could possibly be a type-id
313 // in its syntactic context shall be considered a type-id.
314
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000315 TPResult TPR = isCXXDeclarationSpecifier();
316 if (TPR != TPResult::Ambiguous())
317 return TPR != TPResult::False(); // Returns true for TPResult::True() or
318 // TPResult::Error().
319
320 // FIXME: Add statistics about the number of ambiguous statements encountered
321 // and how they were resolved (number of declarations+number of expressions).
322
323 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
324 // We need tentative parsing...
325
326 TentativeParsingAction PA(*this);
327
328 // type-specifier-seq
329 if (Tok.is(tok::kw_typeof))
330 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000331 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000332 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000333
334 if (getLang().ObjC1 && Tok.is(tok::less))
335 TryParseProtocolQualifiers();
336 }
337
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000338 assert(Tok.is(tok::l_paren) && "Expected '('");
339
340 // declarator
341 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
342
343 // In case of an error, let the declaration parsing code handle it.
344 if (TPR == TPResult::Error())
345 TPR = TPResult::True();
346
347 if (TPR == TPResult::Ambiguous()) {
348 // We are supposed to be inside parens, so if after the abstract declarator
349 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000350 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000351 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000352 isAmbiguous = true;
353
Douglas Gregor8b642592009-02-10 00:53:15 +0000354 // We are supposed to be inside a template argument, so if after
355 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
356 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000357 } else if (Context == TypeIdAsTemplateArgument &&
358 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
359 (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000360 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000361 isAmbiguous = true;
362
363 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000364 TPR = TPResult::False();
365 }
366
367 PA.Revert();
368
369 assert(TPR == TPResult::True() || TPR == TPResult::False());
370 return TPR == TPResult::True();
371}
372
Sean Huntbbd37c62009-11-21 08:43:09 +0000373/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
374/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
375/// performed that will simply return true if a [[ is seen. Currently C++ has no
376/// syntactical ambiguities from this check, but it may inhibit error recovery.
377/// If CheckClosing is true, a check is made for closing ]] brackets.
378///
379/// If given, After is set to the token after the attribute-specifier so that
380/// appropriate parsing decisions can be made; it is left untouched if false is
381/// returned.
382///
383/// FIXME: If an error is in the closing ]] brackets, the program assumes
384/// the absence of an attribute-specifier, which can cause very yucky errors
385/// to occur.
386///
387/// [C++0x] attribute-specifier:
388/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000389/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000390///
391/// [C++0x] attribute-list:
392/// attribute[opt]
393/// attribute-list ',' attribute[opt]
394///
395/// [C++0x] attribute:
396/// attribute-token attribute-argument-clause[opt]
397///
398/// [C++0x] attribute-token:
399/// identifier
400/// attribute-scoped-token
401///
402/// [C++0x] attribute-scoped-token:
403/// attribute-namespace '::' identifier
404///
405/// [C++0x] attribute-namespace:
406/// identifier
407///
408/// [C++0x] attribute-argument-clause:
409/// '(' balanced-token-seq ')'
410///
411/// [C++0x] balanced-token-seq:
412/// balanced-token
413/// balanced-token-seq balanced-token
414///
415/// [C++0x] balanced-token:
416/// '(' balanced-token-seq ')'
417/// '[' balanced-token-seq ']'
418/// '{' balanced-token-seq '}'
419/// any token but '(', ')', '[', ']', '{', or '}'
420bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
421 tok::TokenKind *After) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000422 if (Tok.is(tok::kw_alignas))
423 return true;
424
Sean Huntbbd37c62009-11-21 08:43:09 +0000425 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
426 return false;
427
428 // No tentative parsing if we don't need to look for ]]
429 if (!CheckClosing && !getLang().ObjC1)
430 return true;
431
432 struct TentativeReverter {
433 TentativeParsingAction PA;
434
435 TentativeReverter (Parser& P)
436 : PA(P)
437 {}
438 ~TentativeReverter () {
439 PA.Revert();
440 }
441 } R(*this);
442
443 // Opening brackets were checked for above.
444 ConsumeBracket();
445 ConsumeBracket();
446
447 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
448 SkipUntil(tok::r_square, false);
449
450 if (Tok.isNot(tok::r_square))
451 return false;
452 ConsumeBracket();
453
454 if (After)
455 *After = Tok.getKind();
456
457 return true;
458}
459
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000460/// declarator:
461/// direct-declarator
462/// ptr-operator declarator
463///
464/// direct-declarator:
465/// declarator-id
466/// direct-declarator '(' parameter-declaration-clause ')'
467/// cv-qualifier-seq[opt] exception-specification[opt]
468/// direct-declarator '[' constant-expression[opt] ']'
469/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000470/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000471///
472/// abstract-declarator:
473/// ptr-operator abstract-declarator[opt]
474/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000475/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000476///
477/// direct-abstract-declarator:
478/// direct-abstract-declarator[opt]
479/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
480/// exception-specification[opt]
481/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
482/// '(' abstract-declarator ')'
483///
484/// ptr-operator:
485/// '*' cv-qualifier-seq[opt]
486/// '&'
487/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000488/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000489///
490/// cv-qualifier-seq:
491/// cv-qualifier cv-qualifier-seq[opt]
492///
493/// cv-qualifier:
494/// 'const'
495/// 'volatile'
496///
497/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000498/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000499///
500/// id-expression:
501/// unqualified-id
502/// qualified-id [TODO]
503///
504/// unqualified-id:
505/// identifier
506/// operator-function-id [TODO]
507/// conversion-function-id [TODO]
508/// '~' class-name [TODO]
509/// template-id [TODO]
510///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000511Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
512 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000513 // declarator:
514 // direct-declarator
515 // ptr-operator declarator
516
517 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000518 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000519 if (TryAnnotateCXXScopeToken(true))
520 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000521
Chris Lattner9af55002009-03-27 04:18:06 +0000522 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor69d83162011-01-20 16:08:06 +0000523 Tok.is(tok::ampamp) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000524 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000525 // ptr-operator
526 ConsumeToken();
527 while (Tok.is(tok::kw_const) ||
528 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000529 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000530 ConsumeToken();
531 } else {
532 break;
533 }
534 }
535
536 // direct-declarator:
537 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000538 if (Tok.is(tok::ellipsis))
539 ConsumeToken();
540
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000541 if ((Tok.is(tok::identifier) ||
542 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
543 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000544 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000545 if (Tok.is(tok::annot_cxxscope))
546 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000547 ConsumeToken();
548 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000549 ConsumeParen();
550 if (mayBeAbstract &&
551 (Tok.is(tok::r_paren) || // 'int()' is a function.
552 Tok.is(tok::ellipsis) || // 'int(...)' is a function.
553 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000554 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
555 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000556 TPResult TPR = TryParseFunctionDeclarator();
557 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000558 return TPR;
559 } else {
560 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000561 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000562 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000563 if (Tok.is(tok::kw___attribute) ||
564 Tok.is(tok::kw___declspec) ||
565 Tok.is(tok::kw___cdecl) ||
566 Tok.is(tok::kw___stdcall) ||
567 Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000568 Tok.is(tok::kw___thiscall) ||
Douglas Gregor8d267c52011-09-09 02:06:17 +0000569 Tok.is(tok::kw___unaligned))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000570 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000571 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000572 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000573 return TPR;
574 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000575 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000576 ConsumeParen();
577 }
578 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000579 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000580 }
581
582 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000583 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000584
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000585 // abstract-declarator: ...
586 if (Tok.is(tok::ellipsis))
587 ConsumeToken();
588
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000589 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000590 // Check whether we have a function declarator or a possible ctor-style
591 // initializer that follows the declarator. Note that ctor-style
592 // initializers are not possible in contexts where abstract declarators
593 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000594 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000595 break;
596
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000597 // direct-declarator '(' parameter-declaration-clause ')'
598 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000599 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000600 TPR = TryParseFunctionDeclarator();
601 } else if (Tok.is(tok::l_square)) {
602 // direct-declarator '[' constant-expression[opt] ']'
603 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
604 TPR = TryParseBracketDeclarator();
605 } else {
606 break;
607 }
608
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000609 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000610 return TPR;
611 }
612
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000613 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000614}
615
Douglas Gregora61b3e72010-12-01 17:42:47 +0000616Parser::TPResult
617Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
618 switch (Kind) {
619 // Obviously starts an expression.
620 case tok::numeric_constant:
621 case tok::char_constant:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000622 case tok::wide_char_constant:
623 case tok::utf16_char_constant:
624 case tok::utf32_char_constant:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000625 case tok::string_literal:
626 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000627 case tok::utf8_string_literal:
628 case tok::utf16_string_literal:
629 case tok::utf32_string_literal:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000630 case tok::l_square:
631 case tok::l_paren:
632 case tok::amp:
Douglas Gregor69d83162011-01-20 16:08:06 +0000633 case tok::ampamp:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000634 case tok::star:
635 case tok::plus:
636 case tok::plusplus:
637 case tok::minus:
638 case tok::minusminus:
639 case tok::tilde:
640 case tok::exclaim:
641 case tok::kw_sizeof:
642 case tok::kw___func__:
643 case tok::kw_const_cast:
644 case tok::kw_delete:
645 case tok::kw_dynamic_cast:
646 case tok::kw_false:
647 case tok::kw_new:
648 case tok::kw_operator:
649 case tok::kw_reinterpret_cast:
650 case tok::kw_static_cast:
651 case tok::kw_this:
652 case tok::kw_throw:
653 case tok::kw_true:
654 case tok::kw_typeid:
655 case tok::kw_alignof:
656 case tok::kw_noexcept:
657 case tok::kw_nullptr:
658 case tok::kw___null:
659 case tok::kw___alignof:
660 case tok::kw___builtin_choose_expr:
661 case tok::kw___builtin_offsetof:
662 case tok::kw___builtin_types_compatible_p:
663 case tok::kw___builtin_va_arg:
664 case tok::kw___imag:
665 case tok::kw___real:
666 case tok::kw___FUNCTION__:
667 case tok::kw___PRETTY_FUNCTION__:
668 case tok::kw___has_nothrow_assign:
669 case tok::kw___has_nothrow_copy:
670 case tok::kw___has_nothrow_constructor:
671 case tok::kw___has_trivial_assign:
672 case tok::kw___has_trivial_copy:
673 case tok::kw___has_trivial_constructor:
674 case tok::kw___has_trivial_destructor:
675 case tok::kw___has_virtual_destructor:
676 case tok::kw___is_abstract:
677 case tok::kw___is_base_of:
678 case tok::kw___is_class:
Douglas Gregor9f361132011-01-27 20:28:01 +0000679 case tok::kw___is_convertible_to:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000680 case tok::kw___is_empty:
681 case tok::kw___is_enum:
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000682 case tok::kw___is_final:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000683 case tok::kw___is_literal:
Chandler Carruth38402812011-04-24 02:49:28 +0000684 case tok::kw___is_literal_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000685 case tok::kw___is_pod:
686 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +0000687 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +0000688 case tok::kw___is_trivially_copyable:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000689 case tok::kw___is_union:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000690 case tok::kw___uuidof:
691 return TPResult::True();
692
693 // Obviously starts a type-specifier-seq:
694 case tok::kw_char:
695 case tok::kw_const:
696 case tok::kw_double:
697 case tok::kw_enum:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000698 case tok::kw_half:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000699 case tok::kw_float:
700 case tok::kw_int:
701 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000702 case tok::kw___int64:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000703 case tok::kw_restrict:
704 case tok::kw_short:
705 case tok::kw_signed:
706 case tok::kw_struct:
707 case tok::kw_union:
708 case tok::kw_unsigned:
709 case tok::kw_void:
710 case tok::kw_volatile:
711 case tok::kw__Bool:
712 case tok::kw__Complex:
713 case tok::kw_class:
714 case tok::kw_typename:
715 case tok::kw_wchar_t:
716 case tok::kw_char16_t:
717 case tok::kw_char32_t:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000718 case tok::kw___underlying_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000719 case tok::kw_thread_local:
720 case tok::kw__Decimal32:
721 case tok::kw__Decimal64:
722 case tok::kw__Decimal128:
723 case tok::kw___thread:
724 case tok::kw_typeof:
725 case tok::kw___cdecl:
726 case tok::kw___stdcall:
727 case tok::kw___fastcall:
728 case tok::kw___thiscall:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000729 case tok::kw___unaligned:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000730 case tok::kw___vector:
731 case tok::kw___pixel:
Eli Friedmanb001de72011-10-06 23:00:33 +0000732 case tok::kw__Atomic:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000733 return TPResult::False();
734
735 default:
736 break;
737 }
738
739 return TPResult::Ambiguous();
740}
741
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000742/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
743/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
744/// be either a decl-specifier or a function-style cast, and TPResult::Error()
745/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000746///
747/// decl-specifier:
748/// storage-class-specifier
749/// type-specifier
750/// function-specifier
751/// 'friend'
752/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000753/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000754/// [GNU] attributes declaration-specifiers[opt]
755///
756/// storage-class-specifier:
757/// 'register'
758/// 'static'
759/// 'extern'
760/// 'mutable'
761/// 'auto'
762/// [GNU] '__thread'
763///
764/// function-specifier:
765/// 'inline'
766/// 'virtual'
767/// 'explicit'
768///
769/// typedef-name:
770/// identifier
771///
772/// type-specifier:
773/// simple-type-specifier
774/// class-specifier
775/// enum-specifier
776/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000777/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000778/// cv-qualifier
779///
780/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000781/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000782/// '::'[opt] nested-name-specifier 'template'
783/// simple-template-id [TODO]
784/// 'char'
785/// 'wchar_t'
786/// 'bool'
787/// 'short'
788/// 'int'
789/// 'long'
790/// 'signed'
791/// 'unsigned'
792/// 'float'
793/// 'double'
794/// 'void'
795/// [GNU] typeof-specifier
796/// [GNU] '_Complex'
797/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000798/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000799///
800/// type-name:
801/// class-name
802/// enum-name
803/// typedef-name
804///
805/// elaborated-type-specifier:
806/// class-key '::'[opt] nested-name-specifier[opt] identifier
807/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
808/// simple-template-id
809/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
810///
811/// enum-name:
812/// identifier
813///
814/// enum-specifier:
815/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
816/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
817///
818/// class-specifier:
819/// class-head '{' member-specification[opt] '}'
820///
821/// class-head:
822/// class-key identifier[opt] base-clause[opt]
823/// class-key nested-name-specifier identifier base-clause[opt]
824/// class-key nested-name-specifier[opt] simple-template-id
825/// base-clause[opt]
826///
827/// class-key:
828/// 'class'
829/// 'struct'
830/// 'union'
831///
832/// cv-qualifier:
833/// 'const'
834/// 'volatile'
835/// [GNU] restrict
836///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000837Parser::TPResult Parser::isCXXDeclarationSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000838 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000839 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000840 // Check for need to substitute AltiVec __vector keyword
841 // for "vector" identifier.
842 if (TryAltiVecVectorToken())
843 return TPResult::True();
844 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000845 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000846 // Annotate typenames and C++ scope specifiers. If we get one, just
847 // recurse to handle whatever we get.
848 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000849 return TPResult::Error();
850 if (Tok.is(tok::identifier))
851 return TPResult::False();
852 return isCXXDeclarationSpecifier();
Chris Lattnere5849262009-01-04 23:33:56 +0000853
Chris Lattner2ee9b402009-12-19 01:11:05 +0000854 case tok::coloncolon: { // ::foo::bar
855 const Token &Next = NextToken();
856 if (Next.is(tok::kw_new) || // ::new
857 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000858 return TPResult::False();
David Blaikie42d6d0c2011-12-04 05:04:18 +0000859 }
860 // Fall through.
861 case tok::kw_decltype:
Chris Lattnere5849262009-01-04 23:33:56 +0000862 // Annotate typenames and C++ scope specifiers. If we get one, just
863 // recurse to handle whatever we get.
864 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000865 return TPResult::Error();
866 return isCXXDeclarationSpecifier();
Nick Lewycky443aac92012-01-25 01:19:14 +0000867
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000868 // decl-specifier:
869 // storage-class-specifier
870 // type-specifier
871 // function-specifier
872 // 'friend'
873 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000874 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000875 case tok::kw_friend:
876 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000877 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000878 // storage-class-specifier
879 case tok::kw_register:
880 case tok::kw_static:
881 case tok::kw_extern:
882 case tok::kw_mutable:
883 case tok::kw_auto:
884 case tok::kw___thread:
885 // function-specifier
886 case tok::kw_inline:
887 case tok::kw_virtual:
888 case tok::kw_explicit:
889
Douglas Gregor8d267c52011-09-09 02:06:17 +0000890 // Modules
891 case tok::kw___module_private__:
892
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000893 // type-specifier:
894 // simple-type-specifier
895 // class-specifier
896 // enum-specifier
897 // elaborated-type-specifier
898 // typename-specifier
899 // cv-qualifier
900
901 // class-specifier
902 // elaborated-type-specifier
903 case tok::kw_class:
904 case tok::kw_struct:
905 case tok::kw_union:
906 // enum-specifier
907 case tok::kw_enum:
908 // cv-qualifier
909 case tok::kw_const:
910 case tok::kw_volatile:
911
912 // GNU
913 case tok::kw_restrict:
914 case tok::kw__Complex:
915 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000916 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Steve Naroff7ec56582009-01-06 17:40:00 +0000918 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000919 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000920 case tok::kw___cdecl:
921 case tok::kw___stdcall:
922 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000923 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000924 case tok::kw___w64:
925 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +0000926 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +0000927 case tok::kw___forceinline:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000928 case tok::kw___unaligned:
Eli Friedman290eeb02009-06-08 23:27:34 +0000929 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000930
931 // Borland
932 case tok::kw___pascal:
933 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000934
935 // AltiVec
936 case tok::kw___vector:
937 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000938
John McCall51e2a5d2010-04-30 03:11:01 +0000939 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000940 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
John McCall51e2a5d2010-04-30 03:11:01 +0000941 if (TemplateId->Kind != TNK_Type_template)
942 return TPResult::False();
943 CXXScopeSpec SS;
Douglas Gregor059101f2011-03-02 00:47:37 +0000944 AnnotateTemplateIdTokenAsType();
John McCall51e2a5d2010-04-30 03:11:01 +0000945 assert(Tok.is(tok::annot_typename));
946 goto case_typename;
947 }
948
John McCallae03cb52009-12-19 00:35:18 +0000949 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
950 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000951 if (TryAnnotateTypeOrScopeToken())
952 return TPResult::Error();
Nick Lewycky443aac92012-01-25 01:19:14 +0000953 if (!Tok.is(tok::annot_typename)) {
954 // If the next token is an identifier or a type qualifier, then this
955 // can't possibly be a valid expression either.
956 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
957 CXXScopeSpec SS;
958 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
959 Tok.getAnnotationRange(),
960 SS);
961 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
962 TentativeParsingAction PA(*this);
963 ConsumeToken();
964 ConsumeToken();
965 bool isIdentifier = Tok.is(tok::identifier);
966 TPResult TPR = TPResult::False();
967 if (!isIdentifier)
968 TPR = isCXXDeclarationSpecifier();
969 PA.Revert();
970
971 if (isIdentifier ||
972 TPR == TPResult::True() || TPR == TPResult::Error())
973 return TPResult::Error();
974 }
975 }
John McCallae03cb52009-12-19 00:35:18 +0000976 return TPResult::False();
Nick Lewycky443aac92012-01-25 01:19:14 +0000977 }
John McCallae03cb52009-12-19 00:35:18 +0000978 // If that succeeded, fallthrough into the generic simple-type-id case.
979
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000980 // The ambiguity resides in a simple-type-specifier/typename-specifier
981 // followed by a '('. The '(' could either be the start of:
982 //
983 // direct-declarator:
984 // '(' declarator ')'
985 //
986 // direct-abstract-declarator:
987 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
988 // exception-specification[opt]
989 // '(' abstract-declarator ')'
990 //
991 // or part of a function-style cast expression:
992 //
993 // simple-type-specifier '(' expression-list[opt] ')'
994 //
995
996 // simple-type-specifier:
997
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000998 case tok::annot_typename:
999 case_typename:
1000 // In Objective-C, we might have a protocol-qualified type.
1001 if (getLang().ObjC1 && NextToken().is(tok::less)) {
1002 // Tentatively parse the
1003 TentativeParsingAction PA(*this);
1004 ConsumeToken(); // The type token
1005
1006 TPResult TPR = TryParseProtocolQualifiers();
1007 bool isFollowedByParen = Tok.is(tok::l_paren);
1008
1009 PA.Revert();
1010
1011 if (TPR == TPResult::Error())
1012 return TPResult::Error();
1013
1014 if (isFollowedByParen)
1015 return TPResult::Ambiguous();
1016
1017 return TPResult::True();
1018 }
1019
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001020 case tok::kw_char:
1021 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001022 case tok::kw_char16_t:
1023 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001024 case tok::kw_bool:
1025 case tok::kw_short:
1026 case tok::kw_int:
1027 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001028 case tok::kw___int64:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001029 case tok::kw_signed:
1030 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001031 case tok::kw_half:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001032 case tok::kw_float:
1033 case tok::kw_double:
1034 case tok::kw_void:
David Blaikie5e089fe2012-01-24 05:47:35 +00001035 case tok::annot_decltype:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001036 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001037 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001038
Douglas Gregor9497a732010-09-16 01:51:54 +00001039 if (isStartOfObjCClassMessageMissingOpenBracket())
1040 return TPResult::False();
1041
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001042 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001043
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001044 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001045 case tok::kw_typeof: {
1046 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001047 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001048
1049 TentativeParsingAction PA(*this);
1050
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001051 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001052 bool isFollowedByParen = Tok.is(tok::l_paren);
1053
1054 PA.Revert();
1055
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001056 if (TPR == TPResult::Error())
1057 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001058
1059 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001060 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001061
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001062 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001063 }
1064
Sean Huntdb5d44b2011-05-19 05:37:45 +00001065 // C++0x type traits support
1066 case tok::kw___underlying_type:
1067 return TPResult::True();
1068
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001069 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00001070 case tok::kw__Atomic:
1071 return TPResult::True();
1072
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001073 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001074 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001075 }
1076}
1077
1078/// [GNU] typeof-specifier:
1079/// 'typeof' '(' expressions ')'
1080/// 'typeof' '(' type-name ')'
1081///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001082Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001083 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1084 ConsumeToken();
1085
1086 assert(Tok.is(tok::l_paren) && "Expected '('");
1087 // Parse through the parens after 'typeof'.
1088 ConsumeParen();
1089 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001090 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001091
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001092 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001093}
1094
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001095/// [ObjC] protocol-qualifiers:
1096//// '<' identifier-list '>'
1097Parser::TPResult Parser::TryParseProtocolQualifiers() {
1098 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1099 ConsumeToken();
1100 do {
1101 if (Tok.isNot(tok::identifier))
1102 return TPResult::Error();
1103 ConsumeToken();
1104
1105 if (Tok.is(tok::comma)) {
1106 ConsumeToken();
1107 continue;
1108 }
1109
1110 if (Tok.is(tok::greater)) {
1111 ConsumeToken();
1112 return TPResult::Ambiguous();
1113 }
1114 } while (false);
1115
1116 return TPResult::Error();
1117}
1118
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001119Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1120 TPResult TPR = isCXXDeclarationSpecifier();
1121 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001122 return TPR;
1123
1124 if (Tok.is(tok::kw_typeof))
1125 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001126 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001127 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001128
1129 if (getLang().ObjC1 && Tok.is(tok::less))
1130 TryParseProtocolQualifiers();
1131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001133 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001134 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001135}
1136
1137/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1138/// a constructor-style initializer, when parsing declaration statements.
1139/// Returns true for function declarator and false for constructor-style
1140/// initializer.
1141/// If during the disambiguation process a parsing error is encountered,
1142/// the function returns true to let the declaration parsing code handle it.
1143///
1144/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1145/// exception-specification[opt]
1146///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001147bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001148
1149 // C++ 8.2p1:
1150 // The ambiguity arising from the similarity between a function-style cast and
1151 // a declaration mentioned in 6.8 can also occur in the context of a
1152 // declaration. In that context, the choice is between a function declaration
1153 // with a redundant set of parentheses around a parameter name and an object
1154 // declaration with a function-style cast as the initializer. Just as for the
1155 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1156 // that could possibly be a declaration a declaration.
1157
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001158 TentativeParsingAction PA(*this);
1159
1160 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001161 TPResult TPR = TryParseParameterDeclarationClause();
1162 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1163 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001164
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001165 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001166 PA.Revert();
1167
1168 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001169 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001170 return true;
1171
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001172 if (TPR == TPResult::Ambiguous()) {
1173 // Function declarator has precedence over constructor-style initializer.
1174 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001175 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001176 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1177 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001178 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001179 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001180
1181 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001182}
1183
1184/// parameter-declaration-clause:
1185/// parameter-declaration-list[opt] '...'[opt]
1186/// parameter-declaration-list ',' '...'
1187///
1188/// parameter-declaration-list:
1189/// parameter-declaration
1190/// parameter-declaration-list ',' parameter-declaration
1191///
1192/// parameter-declaration:
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001193/// decl-specifier-seq declarator attributes[opt]
1194/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1195/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1196/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1197/// '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001198///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001199Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001200
1201 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001202 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001203
1204 // parameter-declaration-list[opt] '...'[opt]
1205 // parameter-declaration-list ',' '...'
1206 //
1207 // parameter-declaration-list:
1208 // parameter-declaration
1209 // parameter-declaration-list ',' parameter-declaration
1210 //
1211 while (1) {
1212 // '...'[opt]
1213 if (Tok.is(tok::ellipsis)) {
1214 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001215 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001216 }
1217
John McCall0b7e6782011-03-24 11:26:52 +00001218 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001219 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001220
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001221 // decl-specifier-seq
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001222 TPResult TPR = TryParseDeclarationSpecifier();
1223 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001224 return TPR;
1225
1226 // declarator
1227 // abstract-declarator[opt]
1228 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001229 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001230 return TPR;
1231
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001232 // [GNU] attributes[opt]
1233 if (Tok.is(tok::kw___attribute))
1234 return TPResult::True();
1235
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001236 if (Tok.is(tok::equal)) {
1237 // '=' assignment-expression
1238 // Parse through assignment-expression.
Douglas Gregora8bc8c92010-12-23 22:44:42 +00001239 tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1240 if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001241 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001242 }
1243
1244 if (Tok.is(tok::ellipsis)) {
1245 ConsumeToken();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001246 return TPResult::True(); // '...' is a sign of a function declarator.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001247 }
1248
1249 if (Tok.isNot(tok::comma))
1250 break;
1251 ConsumeToken(); // the comma.
1252 }
1253
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001254 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001255}
1256
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001257/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1258/// parsing as a function declarator.
1259/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1260/// return TPResult::Ambiguous(), otherwise it will return either False() or
1261/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001262///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001263/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1264/// exception-specification[opt]
1265///
1266/// exception-specification:
1267/// 'throw' '(' type-id-list[opt] ')'
1268///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001269Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001270
1271 // The '(' is already parsed.
1272
1273 TPResult TPR = TryParseParameterDeclarationClause();
1274 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1275 TPR = TPResult::False();
1276
1277 if (TPR == TPResult::False() || TPR == TPResult::Error())
1278 return TPR;
1279
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001280 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001281 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001282 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001283
1284 // cv-qualifier-seq
1285 while (Tok.is(tok::kw_const) ||
1286 Tok.is(tok::kw_volatile) ||
1287 Tok.is(tok::kw_restrict) )
1288 ConsumeToken();
1289
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001290 // ref-qualifier[opt]
1291 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1292 ConsumeToken();
1293
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001294 // exception-specification
1295 if (Tok.is(tok::kw_throw)) {
1296 ConsumeToken();
1297 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001298 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001299
1300 // Parse through the parens after 'throw'.
1301 ConsumeParen();
1302 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001303 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001304 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001305 if (Tok.is(tok::kw_noexcept)) {
1306 ConsumeToken();
1307 // Possibly an expression as well.
1308 if (Tok.is(tok::l_paren)) {
1309 // Find the matching rparen.
1310 ConsumeParen();
1311 if (!SkipUntil(tok::r_paren))
1312 return TPResult::Error();
1313 }
1314 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001315
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001316 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001317}
1318
1319/// '[' constant-expression[opt] ']'
1320///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001321Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001322 ConsumeBracket();
1323 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001324 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001325
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001326 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001327}