blob: c2e539ca61fadb60c06826f8b8ca89df65fd2d42 [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
David Blaikie4e4d0842012-03-11 07:00:24 +0000152 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000153 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
Richard Smithd81e9612012-02-23 01:36:12 +0000236/// [C++11] type-specifier-seq declarator '=' initializer-clause
237/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000238/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
239/// '=' assignment-expression
240///
241bool Parser::isCXXConditionDeclaration() {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000242 TPResult TPR = isCXXDeclarationSpecifier();
243 if (TPR != TPResult::Ambiguous())
244 return TPR != TPResult::False(); // Returns true for TPResult::True() or
245 // TPResult::Error().
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000246
247 // FIXME: Add statistics about the number of ambiguous statements encountered
248 // and how they were resolved (number of declarations+number of expressions).
249
250 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
251 // We need tentative parsing...
252
253 TentativeParsingAction PA(*this);
254
255 // type-specifier-seq
256 if (Tok.is(tok::kw_typeof))
257 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000258 else {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000259 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000260
David Blaikie4e4d0842012-03-11 07:00:24 +0000261 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000262 TryParseProtocolQualifiers();
263 }
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000264 assert(Tok.is(tok::l_paren) && "Expected '('");
265
266 // declarator
267 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
268
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000269 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000270 if (TPR == TPResult::Error())
271 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000272
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000273 if (TPR == TPResult::Ambiguous()) {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000274 // '='
275 // [GNU] simple-asm-expr[opt] attributes[opt]
276 if (Tok.is(tok::equal) ||
277 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000278 TPR = TPResult::True();
David Blaikie4e4d0842012-03-11 07:00:24 +0000279 else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace))
Richard Smithd81e9612012-02-23 01:36:12 +0000280 TPR = TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000281 else
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000282 TPR = TPResult::False();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000283 }
284
Argyrios Kyrtzidisca35baa2008-10-05 15:19:49 +0000285 PA.Revert();
286
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000287 assert(TPR == TPResult::True() || TPR == TPResult::False());
288 return TPR == TPResult::True();
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000289}
290
Mike Stump1eb44332009-09-09 15:08:12 +0000291 /// \brief Determine whether the next set of tokens contains a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000292 ///
293 /// The context parameter states what context we're parsing right
294 /// now, which affects how this routine copes with the token
295 /// following the type-id. If the context is TypeIdInParens, we have
296 /// already parsed the '(' and we will cease lookahead when we hit
297 /// the corresponding ')'. If the context is
298 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
299 /// before this template argument, and will cease lookahead when we
300 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
301 /// and false for an expression. If during the disambiguation
302 /// process a parsing error is encountered, the function returns
303 /// true to let the declaration parsing code handle it.
304 ///
305 /// type-id:
306 /// type-specifier-seq abstract-declarator[opt]
307 ///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000308bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000310 isAmbiguous = false;
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +0000311
312 // C++ 8.2p2:
313 // The ambiguity arising from the similarity between a function-style cast and
314 // a type-id can occur in different contexts. The ambiguity appears as a
315 // choice between a function-style cast expression and a declaration of a
316 // type. The resolution is that any construct that could possibly be a type-id
317 // in its syntactic context shall be considered a type-id.
318
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000319 TPResult TPR = isCXXDeclarationSpecifier();
320 if (TPR != TPResult::Ambiguous())
321 return TPR != TPResult::False(); // Returns true for TPResult::True() or
322 // TPResult::Error().
323
324 // FIXME: Add statistics about the number of ambiguous statements encountered
325 // and how they were resolved (number of declarations+number of expressions).
326
327 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
328 // We need tentative parsing...
329
330 TentativeParsingAction PA(*this);
331
332 // type-specifier-seq
333 if (Tok.is(tok::kw_typeof))
334 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000335 else {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000336 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000337
David Blaikie4e4d0842012-03-11 07:00:24 +0000338 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000339 TryParseProtocolQualifiers();
340 }
341
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000342 assert(Tok.is(tok::l_paren) && "Expected '('");
343
344 // declarator
345 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
346
347 // In case of an error, let the declaration parsing code handle it.
348 if (TPR == TPResult::Error())
349 TPR = TPResult::True();
350
351 if (TPR == TPResult::Ambiguous()) {
352 // We are supposed to be inside parens, so if after the abstract declarator
353 // we encounter a ')' this is a type-id, otherwise it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000354 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
Douglas Gregor8b642592009-02-10 00:53:15 +0000355 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000356 isAmbiguous = true;
357
Douglas Gregor8b642592009-02-10 00:53:15 +0000358 // We are supposed to be inside a template argument, so if after
359 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
360 // ',', this is a type-id. Otherwise, it's an expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000361 } else if (Context == TypeIdAsTemplateArgument &&
362 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
David Blaikie4e4d0842012-03-11 07:00:24 +0000363 (getLangOpts().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000364 TPR = TPResult::True();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000365 isAmbiguous = true;
366
367 } else
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000368 TPR = TPResult::False();
369 }
370
371 PA.Revert();
372
373 assert(TPR == TPResult::True() || TPR == TPResult::False());
374 return TPR == TPResult::True();
375}
376
Richard Smith6ee326a2012-04-10 01:32:12 +0000377/// \brief Returns true if this is a C++11 attribute-specifier. Per
378/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
379/// always introduce an attribute. In Objective-C++11, this rule does not
380/// apply if either '[' begins a message-send.
Sean Huntbbd37c62009-11-21 08:43:09 +0000381///
Richard Smith6ee326a2012-04-10 01:32:12 +0000382/// If Disambiguate is true, we try harder to determine whether a '[[' starts
383/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
Sean Huntbbd37c62009-11-21 08:43:09 +0000384///
Richard Smith6ee326a2012-04-10 01:32:12 +0000385/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
386/// Obj-C message send or the start of an attribute. Otherwise, we assume it
387/// is not an Obj-C message send.
Sean Huntbbd37c62009-11-21 08:43:09 +0000388///
Richard Smith6ee326a2012-04-10 01:32:12 +0000389/// C++11 [dcl.attr.grammar]:
390///
391/// attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +0000392/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000393/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000394///
Richard Smith6ee326a2012-04-10 01:32:12 +0000395/// attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +0000396/// attribute[opt]
397/// attribute-list ',' attribute[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +0000398/// attribute '...'
399/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +0000400///
Richard Smith6ee326a2012-04-10 01:32:12 +0000401/// attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +0000402/// attribute-token attribute-argument-clause[opt]
403///
Richard Smith6ee326a2012-04-10 01:32:12 +0000404/// attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +0000405/// identifier
Richard Smith6ee326a2012-04-10 01:32:12 +0000406/// identifier '::' identifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000407///
Richard Smith6ee326a2012-04-10 01:32:12 +0000408/// attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +0000409/// '(' balanced-token-seq ')'
Richard Smith6ee326a2012-04-10 01:32:12 +0000410Parser::CXX11AttributeKind
411Parser::isCXX11AttributeSpecifier(bool Disambiguate,
412 bool OuterMightBeMessageSend) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000413 if (Tok.is(tok::kw_alignas))
Richard Smith6ee326a2012-04-10 01:32:12 +0000414 return CAK_AttributeSpecifier;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000415
Sean Huntbbd37c62009-11-21 08:43:09 +0000416 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
Richard Smith6ee326a2012-04-10 01:32:12 +0000417 return CAK_NotAttributeSpecifier;
Sean Huntbbd37c62009-11-21 08:43:09 +0000418
Richard Smith6ee326a2012-04-10 01:32:12 +0000419 // No tentative parsing if we don't need to look for ']]' or a lambda.
420 if (!Disambiguate && !getLangOpts().ObjC1)
421 return CAK_AttributeSpecifier;
422
423 TentativeParsingAction PA(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000424
425 // Opening brackets were checked for above.
426 ConsumeBracket();
Richard Smith6ee326a2012-04-10 01:32:12 +0000427
428 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
429 if (!getLangOpts().ObjC1) {
430 ConsumeBracket();
431
432 bool IsAttribute = SkipUntil(tok::r_square, false);
433 IsAttribute &= Tok.is(tok::r_square);
434
435 PA.Revert();
436
437 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
438 }
439
440 // In Obj-C++11, we need to distinguish four situations:
441 // 1a) int x[[attr]]; C++11 attribute.
442 // 1b) [[attr]]; C++11 statement attribute.
443 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
444 // 3a) int x[[obj get]]; Message send in array size/index.
445 // 3b) [[Class alloc] init]; Message send in message send.
446 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
447 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
448
449 // If we have a lambda-introducer, then this is definitely not a message send.
450 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
451 // into the tentative attribute parse below.
452 LambdaIntroducer Intro;
453 if (!TryParseLambdaIntroducer(Intro)) {
454 // A lambda cannot end with ']]', and an attribute must.
455 bool IsAttribute = Tok.is(tok::r_square);
456
457 PA.Revert();
458
459 if (IsAttribute)
460 // Case 1: C++11 attribute.
461 return CAK_AttributeSpecifier;
462
463 if (OuterMightBeMessageSend)
464 // Case 4: Lambda in message send.
465 return CAK_NotAttributeSpecifier;
466
467 // Case 2: Lambda in array size / index.
468 return CAK_InvalidAttributeSpecifier;
469 }
470
Sean Huntbbd37c62009-11-21 08:43:09 +0000471 ConsumeBracket();
472
Richard Smith6ee326a2012-04-10 01:32:12 +0000473 // If we don't have a lambda-introducer, then we have an attribute or a
474 // message-send.
475 bool IsAttribute = true;
476 while (Tok.isNot(tok::r_square)) {
477 if (Tok.is(tok::comma)) {
478 // Case 1: Stray commas can only occur in attributes.
479 PA.Revert();
480 return CAK_AttributeSpecifier;
481 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000482
Richard Smith6ee326a2012-04-10 01:32:12 +0000483 // Parse the attribute-token, if present.
484 // C++11 [dcl.attr.grammar]:
485 // If a keyword or an alternative token that satisfies the syntactic
486 // requirements of an identifier is contained in an attribute-token,
487 // it is considered an identifier.
Richard Smithc56298d2012-04-10 03:25:07 +0000488 SourceLocation Loc;
489 if (!TryParseCXX11AttributeIdentifier(Loc)) {
Richard Smith6ee326a2012-04-10 01:32:12 +0000490 IsAttribute = false;
491 break;
492 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000493 if (Tok.is(tok::coloncolon)) {
494 ConsumeToken();
Richard Smithc56298d2012-04-10 03:25:07 +0000495 if (!TryParseCXX11AttributeIdentifier(Loc)) {
Richard Smith6ee326a2012-04-10 01:32:12 +0000496 IsAttribute = false;
497 break;
498 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000499 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000500
Richard Smith6ee326a2012-04-10 01:32:12 +0000501 // Parse the attribute-argument-clause, if present.
502 if (Tok.is(tok::l_paren)) {
503 ConsumeParen();
504 if (!SkipUntil(tok::r_paren, false)) {
505 IsAttribute = false;
506 break;
507 }
508 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000509
Richard Smith6ee326a2012-04-10 01:32:12 +0000510 if (Tok.is(tok::ellipsis))
511 ConsumeToken();
512
513 if (Tok.isNot(tok::comma))
514 break;
515
516 ConsumeToken();
517 }
518
519 // An attribute must end ']]'.
520 if (IsAttribute) {
521 if (Tok.is(tok::r_square)) {
522 ConsumeBracket();
523 IsAttribute = Tok.is(tok::r_square);
524 } else {
525 IsAttribute = false;
526 }
527 }
528
529 PA.Revert();
530
531 if (IsAttribute)
532 // Case 1: C++11 statement attribute.
533 return CAK_AttributeSpecifier;
534
535 // Case 3: Message send.
536 return CAK_NotAttributeSpecifier;
Sean Huntbbd37c62009-11-21 08:43:09 +0000537}
538
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000539/// declarator:
540/// direct-declarator
541/// ptr-operator declarator
542///
543/// direct-declarator:
544/// declarator-id
545/// direct-declarator '(' parameter-declaration-clause ')'
546/// cv-qualifier-seq[opt] exception-specification[opt]
547/// direct-declarator '[' constant-expression[opt] ']'
548/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000549/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000550///
551/// abstract-declarator:
552/// ptr-operator abstract-declarator[opt]
553/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000554/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000555///
556/// direct-abstract-declarator:
557/// direct-abstract-declarator[opt]
558/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
559/// exception-specification[opt]
560/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
561/// '(' abstract-declarator ')'
562///
563/// ptr-operator:
564/// '*' cv-qualifier-seq[opt]
565/// '&'
566/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000567/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000568///
569/// cv-qualifier-seq:
570/// cv-qualifier cv-qualifier-seq[opt]
571///
572/// cv-qualifier:
573/// 'const'
574/// 'volatile'
575///
576/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000577/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000578///
579/// id-expression:
580/// unqualified-id
581/// qualified-id [TODO]
582///
583/// unqualified-id:
584/// identifier
585/// operator-function-id [TODO]
586/// conversion-function-id [TODO]
587/// '~' class-name [TODO]
588/// template-id [TODO]
589///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000590Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
591 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000592 // declarator:
593 // direct-declarator
594 // ptr-operator declarator
595
596 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000597 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000598 if (TryAnnotateCXXScopeToken(true))
599 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000600
Chris Lattner9af55002009-03-27 04:18:06 +0000601 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor69d83162011-01-20 16:08:06 +0000602 Tok.is(tok::ampamp) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000603 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000604 // ptr-operator
605 ConsumeToken();
606 while (Tok.is(tok::kw_const) ||
607 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000608 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000609 ConsumeToken();
610 } else {
611 break;
612 }
613 }
614
615 // direct-declarator:
616 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000617 if (Tok.is(tok::ellipsis))
618 ConsumeToken();
619
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000620 if ((Tok.is(tok::identifier) ||
621 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
622 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000623 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000624 if (Tok.is(tok::annot_cxxscope))
625 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000626 ConsumeToken();
627 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000628 ConsumeParen();
629 if (mayBeAbstract &&
630 (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +0000631 // 'int(...)' is a function.
632 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000633 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000634 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
635 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000636 TPResult TPR = TryParseFunctionDeclarator();
637 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000638 return TPR;
639 } else {
640 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000641 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000642 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000643 if (Tok.is(tok::kw___attribute) ||
644 Tok.is(tok::kw___declspec) ||
645 Tok.is(tok::kw___cdecl) ||
646 Tok.is(tok::kw___stdcall) ||
647 Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000648 Tok.is(tok::kw___thiscall) ||
Douglas Gregor8d267c52011-09-09 02:06:17 +0000649 Tok.is(tok::kw___unaligned))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000650 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000651 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000652 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000653 return TPR;
654 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000655 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000656 ConsumeParen();
657 }
658 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000659 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000660 }
661
662 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000663 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000664
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000665 // abstract-declarator: ...
666 if (Tok.is(tok::ellipsis))
667 ConsumeToken();
668
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000669 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000670 // Check whether we have a function declarator or a possible ctor-style
671 // initializer that follows the declarator. Note that ctor-style
672 // initializers are not possible in contexts where abstract declarators
673 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000674 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000675 break;
676
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000677 // direct-declarator '(' parameter-declaration-clause ')'
678 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000679 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000680 TPR = TryParseFunctionDeclarator();
681 } else if (Tok.is(tok::l_square)) {
682 // direct-declarator '[' constant-expression[opt] ']'
683 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
684 TPR = TryParseBracketDeclarator();
685 } else {
686 break;
687 }
688
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000689 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000690 return TPR;
691 }
692
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000693 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000694}
695
Douglas Gregora61b3e72010-12-01 17:42:47 +0000696Parser::TPResult
697Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
698 switch (Kind) {
699 // Obviously starts an expression.
700 case tok::numeric_constant:
701 case tok::char_constant:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000702 case tok::wide_char_constant:
703 case tok::utf16_char_constant:
704 case tok::utf32_char_constant:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000705 case tok::string_literal:
706 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000707 case tok::utf8_string_literal:
708 case tok::utf16_string_literal:
709 case tok::utf32_string_literal:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000710 case tok::l_square:
711 case tok::l_paren:
712 case tok::amp:
Douglas Gregor69d83162011-01-20 16:08:06 +0000713 case tok::ampamp:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000714 case tok::star:
715 case tok::plus:
716 case tok::plusplus:
717 case tok::minus:
718 case tok::minusminus:
719 case tok::tilde:
720 case tok::exclaim:
721 case tok::kw_sizeof:
722 case tok::kw___func__:
723 case tok::kw_const_cast:
724 case tok::kw_delete:
725 case tok::kw_dynamic_cast:
726 case tok::kw_false:
727 case tok::kw_new:
728 case tok::kw_operator:
729 case tok::kw_reinterpret_cast:
730 case tok::kw_static_cast:
731 case tok::kw_this:
732 case tok::kw_throw:
733 case tok::kw_true:
734 case tok::kw_typeid:
735 case tok::kw_alignof:
736 case tok::kw_noexcept:
737 case tok::kw_nullptr:
738 case tok::kw___null:
739 case tok::kw___alignof:
740 case tok::kw___builtin_choose_expr:
741 case tok::kw___builtin_offsetof:
742 case tok::kw___builtin_types_compatible_p:
743 case tok::kw___builtin_va_arg:
744 case tok::kw___imag:
745 case tok::kw___real:
746 case tok::kw___FUNCTION__:
747 case tok::kw___PRETTY_FUNCTION__:
748 case tok::kw___has_nothrow_assign:
749 case tok::kw___has_nothrow_copy:
750 case tok::kw___has_nothrow_constructor:
751 case tok::kw___has_trivial_assign:
752 case tok::kw___has_trivial_copy:
753 case tok::kw___has_trivial_constructor:
754 case tok::kw___has_trivial_destructor:
755 case tok::kw___has_virtual_destructor:
756 case tok::kw___is_abstract:
757 case tok::kw___is_base_of:
758 case tok::kw___is_class:
Douglas Gregor9f361132011-01-27 20:28:01 +0000759 case tok::kw___is_convertible_to:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000760 case tok::kw___is_empty:
761 case tok::kw___is_enum:
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000762 case tok::kw___is_final:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000763 case tok::kw___is_literal:
Chandler Carruth38402812011-04-24 02:49:28 +0000764 case tok::kw___is_literal_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000765 case tok::kw___is_pod:
766 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +0000767 case tok::kw___is_trivial:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +0000768 case tok::kw___is_trivially_assignable:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000769 case tok::kw___is_trivially_constructible:
Sean Huntfeb375d2011-05-13 00:31:07 +0000770 case tok::kw___is_trivially_copyable:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000771 case tok::kw___is_union:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000772 case tok::kw___uuidof:
773 return TPResult::True();
774
775 // Obviously starts a type-specifier-seq:
776 case tok::kw_char:
777 case tok::kw_const:
778 case tok::kw_double:
779 case tok::kw_enum:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000780 case tok::kw_half:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000781 case tok::kw_float:
782 case tok::kw_int:
783 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000784 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000785 case tok::kw___int128:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000786 case tok::kw_restrict:
787 case tok::kw_short:
788 case tok::kw_signed:
789 case tok::kw_struct:
790 case tok::kw_union:
791 case tok::kw_unsigned:
792 case tok::kw_void:
793 case tok::kw_volatile:
794 case tok::kw__Bool:
795 case tok::kw__Complex:
796 case tok::kw_class:
797 case tok::kw_typename:
798 case tok::kw_wchar_t:
799 case tok::kw_char16_t:
800 case tok::kw_char32_t:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000801 case tok::kw___underlying_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000802 case tok::kw_thread_local:
803 case tok::kw__Decimal32:
804 case tok::kw__Decimal64:
805 case tok::kw__Decimal128:
806 case tok::kw___thread:
807 case tok::kw_typeof:
808 case tok::kw___cdecl:
809 case tok::kw___stdcall:
810 case tok::kw___fastcall:
811 case tok::kw___thiscall:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000812 case tok::kw___unaligned:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000813 case tok::kw___vector:
814 case tok::kw___pixel:
Eli Friedmanb001de72011-10-06 23:00:33 +0000815 case tok::kw__Atomic:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000816 return TPResult::False();
817
818 default:
819 break;
820 }
821
822 return TPResult::Ambiguous();
823}
824
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000825/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
826/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
827/// be either a decl-specifier or a function-style cast, and TPResult::Error()
828/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000829///
Richard Smith25582fc2012-05-16 23:40:17 +0000830/// If HasMissingTypename is provided, a name with a dependent scope specifier
831/// will be treated as ambiguous if the 'typename' keyword is missing. If this
832/// happens, *HasMissingTypename will be set to 'true'.
833///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000834/// decl-specifier:
835/// storage-class-specifier
836/// type-specifier
837/// function-specifier
838/// 'friend'
839/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000840/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000841/// [GNU] attributes declaration-specifiers[opt]
842///
843/// storage-class-specifier:
844/// 'register'
845/// 'static'
846/// 'extern'
847/// 'mutable'
848/// 'auto'
849/// [GNU] '__thread'
850///
851/// function-specifier:
852/// 'inline'
853/// 'virtual'
854/// 'explicit'
855///
856/// typedef-name:
857/// identifier
858///
859/// type-specifier:
860/// simple-type-specifier
861/// class-specifier
862/// enum-specifier
863/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000864/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000865/// cv-qualifier
866///
867/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000868/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000869/// '::'[opt] nested-name-specifier 'template'
870/// simple-template-id [TODO]
871/// 'char'
872/// 'wchar_t'
873/// 'bool'
874/// 'short'
875/// 'int'
876/// 'long'
877/// 'signed'
878/// 'unsigned'
879/// 'float'
880/// 'double'
881/// 'void'
882/// [GNU] typeof-specifier
883/// [GNU] '_Complex'
884/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000885/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000886///
887/// type-name:
888/// class-name
889/// enum-name
890/// typedef-name
891///
892/// elaborated-type-specifier:
893/// class-key '::'[opt] nested-name-specifier[opt] identifier
894/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
895/// simple-template-id
896/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
897///
898/// enum-name:
899/// identifier
900///
901/// enum-specifier:
902/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
903/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
904///
905/// class-specifier:
906/// class-head '{' member-specification[opt] '}'
907///
908/// class-head:
909/// class-key identifier[opt] base-clause[opt]
910/// class-key nested-name-specifier identifier base-clause[opt]
911/// class-key nested-name-specifier[opt] simple-template-id
912/// base-clause[opt]
913///
914/// class-key:
915/// 'class'
916/// 'struct'
917/// 'union'
918///
919/// cv-qualifier:
920/// 'const'
921/// 'volatile'
922/// [GNU] restrict
923///
Richard Smithd81e9612012-02-23 01:36:12 +0000924Parser::TPResult
Richard Smith25582fc2012-05-16 23:40:17 +0000925Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
926 bool *HasMissingTypename) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000927 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000928 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000929 // Check for need to substitute AltiVec __vector keyword
930 // for "vector" identifier.
931 if (TryAltiVecVectorToken())
932 return TPResult::True();
933 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000934 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000935 // Annotate typenames and C++ scope specifiers. If we get one, just
936 // recurse to handle whatever we get.
937 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000938 return TPResult::Error();
Kaelyn Uhrain434ed262012-04-19 23:17:45 +0000939 if (Tok.is(tok::identifier)) {
940 const Token &Next = NextToken();
Kaelyn Uhrain1a200a82012-05-01 01:16:25 +0000941 return (!getLangOpts().ObjC1 && Next.is(tok::identifier)) ?
Kaelyn Uhrain434ed262012-04-19 23:17:45 +0000942 TPResult::True() : TPResult::False();
943 }
Richard Smith25582fc2012-05-16 23:40:17 +0000944 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
Chris Lattnere5849262009-01-04 23:33:56 +0000945
Chris Lattner2ee9b402009-12-19 01:11:05 +0000946 case tok::coloncolon: { // ::foo::bar
947 const Token &Next = NextToken();
948 if (Next.is(tok::kw_new) || // ::new
949 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000950 return TPResult::False();
David Blaikie42d6d0c2011-12-04 05:04:18 +0000951 }
952 // Fall through.
953 case tok::kw_decltype:
Chris Lattnere5849262009-01-04 23:33:56 +0000954 // Annotate typenames and C++ scope specifiers. If we get one, just
955 // recurse to handle whatever we get.
956 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000957 return TPResult::Error();
Richard Smith25582fc2012-05-16 23:40:17 +0000958 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
Nick Lewycky443aac92012-01-25 01:19:14 +0000959
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000960 // decl-specifier:
961 // storage-class-specifier
962 // type-specifier
963 // function-specifier
964 // 'friend'
965 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000966 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000967 case tok::kw_friend:
968 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000969 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000970 // storage-class-specifier
971 case tok::kw_register:
972 case tok::kw_static:
973 case tok::kw_extern:
974 case tok::kw_mutable:
975 case tok::kw_auto:
976 case tok::kw___thread:
977 // function-specifier
978 case tok::kw_inline:
979 case tok::kw_virtual:
980 case tok::kw_explicit:
981
Douglas Gregor8d267c52011-09-09 02:06:17 +0000982 // Modules
983 case tok::kw___module_private__:
984
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000985 // type-specifier:
986 // simple-type-specifier
987 // class-specifier
988 // enum-specifier
989 // elaborated-type-specifier
990 // typename-specifier
991 // cv-qualifier
992
993 // class-specifier
994 // elaborated-type-specifier
995 case tok::kw_class:
996 case tok::kw_struct:
997 case tok::kw_union:
998 // enum-specifier
999 case tok::kw_enum:
1000 // cv-qualifier
1001 case tok::kw_const:
1002 case tok::kw_volatile:
1003
1004 // GNU
1005 case tok::kw_restrict:
1006 case tok::kw__Complex:
1007 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001008 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Steve Naroff7ec56582009-01-06 17:40:00 +00001010 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +00001011 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +00001012 case tok::kw___cdecl:
1013 case tok::kw___stdcall:
1014 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001015 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001016 case tok::kw___w64:
1017 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001018 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00001019 case tok::kw___forceinline:
Douglas Gregor8d267c52011-09-09 02:06:17 +00001020 case tok::kw___unaligned:
Eli Friedman290eeb02009-06-08 23:27:34 +00001021 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +00001022
1023 // Borland
1024 case tok::kw___pascal:
1025 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +00001026
1027 // AltiVec
1028 case tok::kw___vector:
1029 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001030
John McCall51e2a5d2010-04-30 03:11:01 +00001031 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001032 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
John McCall51e2a5d2010-04-30 03:11:01 +00001033 if (TemplateId->Kind != TNK_Type_template)
1034 return TPResult::False();
1035 CXXScopeSpec SS;
Douglas Gregor059101f2011-03-02 00:47:37 +00001036 AnnotateTemplateIdTokenAsType();
John McCall51e2a5d2010-04-30 03:11:01 +00001037 assert(Tok.is(tok::annot_typename));
1038 goto case_typename;
1039 }
1040
John McCallae03cb52009-12-19 00:35:18 +00001041 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1042 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +00001043 if (TryAnnotateTypeOrScopeToken())
1044 return TPResult::Error();
Nick Lewycky443aac92012-01-25 01:19:14 +00001045 if (!Tok.is(tok::annot_typename)) {
1046 // If the next token is an identifier or a type qualifier, then this
1047 // can't possibly be a valid expression either.
1048 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1049 CXXScopeSpec SS;
1050 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1051 Tok.getAnnotationRange(),
1052 SS);
1053 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1054 TentativeParsingAction PA(*this);
1055 ConsumeToken();
1056 ConsumeToken();
1057 bool isIdentifier = Tok.is(tok::identifier);
1058 TPResult TPR = TPResult::False();
1059 if (!isIdentifier)
Richard Smith25582fc2012-05-16 23:40:17 +00001060 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1061 HasMissingTypename);
Nick Lewycky443aac92012-01-25 01:19:14 +00001062 PA.Revert();
1063
1064 if (isIdentifier ||
1065 TPR == TPResult::True() || TPR == TPResult::Error())
1066 return TPResult::Error();
Richard Smith25582fc2012-05-16 23:40:17 +00001067
1068 if (HasMissingTypename) {
1069 // We can't tell whether this is a missing 'typename' or a valid
1070 // expression.
1071 *HasMissingTypename = true;
1072 return TPResult::Ambiguous();
1073 }
Nick Lewycky443aac92012-01-25 01:19:14 +00001074 }
1075 }
John McCallae03cb52009-12-19 00:35:18 +00001076 return TPResult::False();
Nick Lewycky443aac92012-01-25 01:19:14 +00001077 }
John McCallae03cb52009-12-19 00:35:18 +00001078 // If that succeeded, fallthrough into the generic simple-type-id case.
1079
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001080 // The ambiguity resides in a simple-type-specifier/typename-specifier
1081 // followed by a '('. The '(' could either be the start of:
1082 //
1083 // direct-declarator:
1084 // '(' declarator ')'
1085 //
1086 // direct-abstract-declarator:
1087 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1088 // exception-specification[opt]
1089 // '(' abstract-declarator ')'
1090 //
1091 // or part of a function-style cast expression:
1092 //
1093 // simple-type-specifier '(' expression-list[opt] ')'
1094 //
1095
1096 // simple-type-specifier:
1097
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001098 case tok::annot_typename:
1099 case_typename:
1100 // In Objective-C, we might have a protocol-qualified type.
David Blaikie4e4d0842012-03-11 07:00:24 +00001101 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001102 // Tentatively parse the
1103 TentativeParsingAction PA(*this);
1104 ConsumeToken(); // The type token
1105
1106 TPResult TPR = TryParseProtocolQualifiers();
1107 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001108 bool isFollowedByBrace = Tok.is(tok::l_brace);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001109
1110 PA.Revert();
1111
1112 if (TPR == TPResult::Error())
1113 return TPResult::Error();
1114
1115 if (isFollowedByParen)
1116 return TPResult::Ambiguous();
Richard Smithd81e9612012-02-23 01:36:12 +00001117
David Blaikie4e4d0842012-03-11 07:00:24 +00001118 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001119 return BracedCastResult;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001120
1121 return TPResult::True();
1122 }
1123
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001124 case tok::kw_char:
1125 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001126 case tok::kw_char16_t:
1127 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001128 case tok::kw_bool:
1129 case tok::kw_short:
1130 case tok::kw_int:
1131 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001132 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00001133 case tok::kw___int128:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001134 case tok::kw_signed:
1135 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001136 case tok::kw_half:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001137 case tok::kw_float:
1138 case tok::kw_double:
1139 case tok::kw_void:
David Blaikie5e089fe2012-01-24 05:47:35 +00001140 case tok::annot_decltype:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001141 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001142 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001143
Richard Smithd81e9612012-02-23 01:36:12 +00001144 // This is a function-style cast in all cases we disambiguate other than
1145 // one:
1146 // struct S {
1147 // enum E : int { a = 4 }; // enum
1148 // enum E : int { 4 }; // bit-field
1149 // };
David Blaikie4e4d0842012-03-11 07:00:24 +00001150 if (getLangOpts().CPlusPlus0x && NextToken().is(tok::l_brace))
Richard Smithd81e9612012-02-23 01:36:12 +00001151 return BracedCastResult;
1152
Douglas Gregor9497a732010-09-16 01:51:54 +00001153 if (isStartOfObjCClassMessageMissingOpenBracket())
1154 return TPResult::False();
1155
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001156 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001157
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001158 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001159 case tok::kw_typeof: {
1160 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001161 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001162
1163 TentativeParsingAction PA(*this);
1164
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001165 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001166 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001167 bool isFollowedByBrace = Tok.is(tok::l_brace);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001168
1169 PA.Revert();
1170
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001171 if (TPR == TPResult::Error())
1172 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001173
1174 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001175 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001176
David Blaikie4e4d0842012-03-11 07:00:24 +00001177 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001178 return BracedCastResult;
1179
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001180 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001181 }
1182
Sean Huntdb5d44b2011-05-19 05:37:45 +00001183 // C++0x type traits support
1184 case tok::kw___underlying_type:
1185 return TPResult::True();
1186
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001187 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00001188 case tok::kw__Atomic:
1189 return TPResult::True();
1190
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001191 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001192 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001193 }
1194}
1195
1196/// [GNU] typeof-specifier:
1197/// 'typeof' '(' expressions ')'
1198/// 'typeof' '(' type-name ')'
1199///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001200Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001201 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1202 ConsumeToken();
1203
1204 assert(Tok.is(tok::l_paren) && "Expected '('");
1205 // Parse through the parens after 'typeof'.
1206 ConsumeParen();
1207 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001208 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001209
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001210 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001211}
1212
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001213/// [ObjC] protocol-qualifiers:
1214//// '<' identifier-list '>'
1215Parser::TPResult Parser::TryParseProtocolQualifiers() {
1216 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1217 ConsumeToken();
1218 do {
1219 if (Tok.isNot(tok::identifier))
1220 return TPResult::Error();
1221 ConsumeToken();
1222
1223 if (Tok.is(tok::comma)) {
1224 ConsumeToken();
1225 continue;
1226 }
1227
1228 if (Tok.is(tok::greater)) {
1229 ConsumeToken();
1230 return TPResult::Ambiguous();
1231 }
1232 } while (false);
1233
1234 return TPResult::Error();
1235}
1236
Richard Smith25582fc2012-05-16 23:40:17 +00001237Parser::TPResult
1238Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) {
1239 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
1240 HasMissingTypename);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001241 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001242 return TPR;
1243
1244 if (Tok.is(tok::kw_typeof))
1245 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001246 else {
Richard Smith25582fc2012-05-16 23:40:17 +00001247 if (Tok.is(tok::annot_cxxscope))
1248 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001249 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001250
David Blaikie4e4d0842012-03-11 07:00:24 +00001251 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001252 TryParseProtocolQualifiers();
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001255 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001256}
1257
1258/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1259/// a constructor-style initializer, when parsing declaration statements.
1260/// Returns true for function declarator and false for constructor-style
1261/// initializer.
1262/// If during the disambiguation process a parsing error is encountered,
1263/// the function returns true to let the declaration parsing code handle it.
1264///
1265/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1266/// exception-specification[opt]
1267///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001268bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001269
1270 // C++ 8.2p1:
1271 // The ambiguity arising from the similarity between a function-style cast and
1272 // a declaration mentioned in 6.8 can also occur in the context of a
1273 // declaration. In that context, the choice is between a function declaration
1274 // with a redundant set of parentheses around a parameter name and an object
1275 // declaration with a function-style cast as the initializer. Just as for the
1276 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1277 // that could possibly be a declaration a declaration.
1278
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001279 TentativeParsingAction PA(*this);
1280
1281 ConsumeParen();
Richard Smith25582fc2012-05-16 23:40:17 +00001282 bool InvalidAsDeclaration = false;
1283 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1284 if (TPR == TPResult::Ambiguous()) {
1285 if (Tok.isNot(tok::r_paren))
1286 TPR = TPResult::False();
1287 else {
1288 const Token &Next = NextToken();
1289 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1290 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1291 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
1292 Next.is(tok::l_square) || isCXX0XVirtSpecifier(Next) ||
1293 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1294 Next.is(tok::equal))
1295 // The next token cannot appear after a constructor-style initializer,
1296 // and can appear next in a function definition. This must be a function
1297 // declarator.
1298 TPR = TPResult::True();
1299 else if (InvalidAsDeclaration)
1300 // Use the absence of 'typename' as a tie-breaker.
1301 TPR = TPResult::False();
1302 }
1303 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001304
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001305 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001306 PA.Revert();
1307
1308 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001309 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001310 return true;
1311
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001312 if (TPR == TPResult::Ambiguous()) {
1313 // Function declarator has precedence over constructor-style initializer.
1314 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001315 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001316 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1317 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001318 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001319 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001320
1321 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001322}
1323
1324/// parameter-declaration-clause:
1325/// parameter-declaration-list[opt] '...'[opt]
1326/// parameter-declaration-list ',' '...'
1327///
1328/// parameter-declaration-list:
1329/// parameter-declaration
1330/// parameter-declaration-list ',' parameter-declaration
1331///
1332/// parameter-declaration:
Richard Smith6ee326a2012-04-10 01:32:12 +00001333/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1334/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001335/// '=' assignment-expression
Richard Smith6ee326a2012-04-10 01:32:12 +00001336/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1337/// attributes[opt]
1338/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1339/// attributes[opt] '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001340///
Richard Smith25582fc2012-05-16 23:40:17 +00001341Parser::TPResult
1342Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001343
1344 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001345 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001346
1347 // parameter-declaration-list[opt] '...'[opt]
1348 // parameter-declaration-list ',' '...'
1349 //
1350 // parameter-declaration-list:
1351 // parameter-declaration
1352 // parameter-declaration-list ',' parameter-declaration
1353 //
1354 while (1) {
1355 // '...'[opt]
1356 if (Tok.is(tok::ellipsis)) {
1357 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001358 if (Tok.is(tok::r_paren))
1359 return TPResult::True(); // '...)' is a sign of a function declarator.
1360 else
1361 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001362 }
1363
Richard Smith6ce48a72012-04-11 04:01:28 +00001364 // An attribute-specifier-seq here is a sign of a function declarator.
1365 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1366 /*OuterMightBeMessageSend*/true))
1367 return TPResult::True();
1368
John McCall0b7e6782011-03-24 11:26:52 +00001369 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001370 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001371
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001372 // decl-specifier-seq
Richard Smithd81e9612012-02-23 01:36:12 +00001373 // A parameter-declaration's initializer must be preceded by an '=', so
1374 // decl-specifier-seq '{' is not a parameter in C++11.
Richard Smith25582fc2012-05-16 23:40:17 +00001375 TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001376 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001377 return TPR;
1378
1379 // declarator
1380 // abstract-declarator[opt]
1381 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001382 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001383 return TPR;
1384
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001385 // [GNU] attributes[opt]
1386 if (Tok.is(tok::kw___attribute))
1387 return TPResult::True();
1388
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001389 if (Tok.is(tok::equal)) {
1390 // '=' assignment-expression
1391 // Parse through assignment-expression.
David Blaikieeb52f86a2012-04-09 16:37:11 +00001392 if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/,
1393 true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001394 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001395 }
1396
1397 if (Tok.is(tok::ellipsis)) {
1398 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001399 if (Tok.is(tok::r_paren))
1400 return TPResult::True(); // '...)' is a sign of a function declarator.
1401 else
1402 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001403 }
1404
1405 if (Tok.isNot(tok::comma))
1406 break;
1407 ConsumeToken(); // the comma.
1408 }
1409
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001410 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001411}
1412
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001413/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1414/// parsing as a function declarator.
1415/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1416/// return TPResult::Ambiguous(), otherwise it will return either False() or
1417/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001418///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001419/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1420/// exception-specification[opt]
1421///
1422/// exception-specification:
1423/// 'throw' '(' type-id-list[opt] ')'
1424///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001425Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001426
1427 // The '(' is already parsed.
1428
1429 TPResult TPR = TryParseParameterDeclarationClause();
1430 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1431 TPR = TPResult::False();
1432
1433 if (TPR == TPResult::False() || TPR == TPResult::Error())
1434 return TPR;
1435
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001436 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001437 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001438 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001439
1440 // cv-qualifier-seq
1441 while (Tok.is(tok::kw_const) ||
1442 Tok.is(tok::kw_volatile) ||
1443 Tok.is(tok::kw_restrict) )
1444 ConsumeToken();
1445
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001446 // ref-qualifier[opt]
1447 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1448 ConsumeToken();
1449
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001450 // exception-specification
1451 if (Tok.is(tok::kw_throw)) {
1452 ConsumeToken();
1453 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001454 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001455
1456 // Parse through the parens after 'throw'.
1457 ConsumeParen();
1458 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001459 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001460 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001461 if (Tok.is(tok::kw_noexcept)) {
1462 ConsumeToken();
1463 // Possibly an expression as well.
1464 if (Tok.is(tok::l_paren)) {
1465 // Find the matching rparen.
1466 ConsumeParen();
1467 if (!SkipUntil(tok::r_paren))
1468 return TPResult::Error();
1469 }
1470 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001471
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001472 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001473}
1474
1475/// '[' constant-expression[opt] ']'
1476///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001477Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001478 ConsumeBracket();
1479 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001480 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001481
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001482 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001483}