blob: b650169b2592fb8655d5d3dd933d9b9ffb7461ef [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
Sean Huntbbd37c62009-11-21 08:43:09 +0000377/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
378/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
379/// performed that will simply return true if a [[ is seen. Currently C++ has no
380/// syntactical ambiguities from this check, but it may inhibit error recovery.
381/// If CheckClosing is true, a check is made for closing ]] brackets.
382///
383/// If given, After is set to the token after the attribute-specifier so that
384/// appropriate parsing decisions can be made; it is left untouched if false is
385/// returned.
386///
387/// FIXME: If an error is in the closing ]] brackets, the program assumes
388/// the absence of an attribute-specifier, which can cause very yucky errors
389/// to occur.
390///
391/// [C++0x] attribute-specifier:
392/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000393/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +0000394///
395/// [C++0x] attribute-list:
396/// attribute[opt]
397/// attribute-list ',' attribute[opt]
398///
399/// [C++0x] attribute:
400/// attribute-token attribute-argument-clause[opt]
401///
402/// [C++0x] attribute-token:
403/// identifier
404/// attribute-scoped-token
405///
406/// [C++0x] attribute-scoped-token:
407/// attribute-namespace '::' identifier
408///
409/// [C++0x] attribute-namespace:
410/// identifier
411///
412/// [C++0x] attribute-argument-clause:
413/// '(' balanced-token-seq ')'
414///
415/// [C++0x] balanced-token-seq:
416/// balanced-token
417/// balanced-token-seq balanced-token
418///
419/// [C++0x] balanced-token:
420/// '(' balanced-token-seq ')'
421/// '[' balanced-token-seq ']'
422/// '{' balanced-token-seq '}'
423/// any token but '(', ')', '[', ']', '{', or '}'
424bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
425 tok::TokenKind *After) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000426 if (Tok.is(tok::kw_alignas))
427 return true;
428
Sean Huntbbd37c62009-11-21 08:43:09 +0000429 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
430 return false;
431
432 // No tentative parsing if we don't need to look for ]]
David Blaikie4e4d0842012-03-11 07:00:24 +0000433 if (!CheckClosing && !getLangOpts().ObjC1)
Sean Huntbbd37c62009-11-21 08:43:09 +0000434 return true;
435
436 struct TentativeReverter {
437 TentativeParsingAction PA;
438
439 TentativeReverter (Parser& P)
440 : PA(P)
441 {}
442 ~TentativeReverter () {
443 PA.Revert();
444 }
445 } R(*this);
446
447 // Opening brackets were checked for above.
448 ConsumeBracket();
449 ConsumeBracket();
450
451 // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
452 SkipUntil(tok::r_square, false);
453
454 if (Tok.isNot(tok::r_square))
455 return false;
456 ConsumeBracket();
457
458 if (After)
459 *After = Tok.getKind();
460
461 return true;
462}
463
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000464/// declarator:
465/// direct-declarator
466/// ptr-operator declarator
467///
468/// direct-declarator:
469/// declarator-id
470/// direct-declarator '(' parameter-declaration-clause ')'
471/// cv-qualifier-seq[opt] exception-specification[opt]
472/// direct-declarator '[' constant-expression[opt] ']'
473/// '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000474/// [GNU] '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000475///
476/// abstract-declarator:
477/// ptr-operator abstract-declarator[opt]
478/// direct-abstract-declarator
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000479/// ...
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000480///
481/// direct-abstract-declarator:
482/// direct-abstract-declarator[opt]
483/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
484/// exception-specification[opt]
485/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
486/// '(' abstract-declarator ')'
487///
488/// ptr-operator:
489/// '*' cv-qualifier-seq[opt]
490/// '&'
491/// [C++0x] '&&' [TODO]
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000492/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000493///
494/// cv-qualifier-seq:
495/// cv-qualifier cv-qualifier-seq[opt]
496///
497/// cv-qualifier:
498/// 'const'
499/// 'volatile'
500///
501/// declarator-id:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000502/// '...'[opt] id-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000503///
504/// id-expression:
505/// unqualified-id
506/// qualified-id [TODO]
507///
508/// unqualified-id:
509/// identifier
510/// operator-function-id [TODO]
511/// conversion-function-id [TODO]
512/// '~' class-name [TODO]
513/// template-id [TODO]
514///
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000515Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
516 bool mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000517 // declarator:
518 // direct-declarator
519 // ptr-operator declarator
520
521 while (1) {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000522 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
John McCall9ba61662010-02-26 08:45:28 +0000523 if (TryAnnotateCXXScopeToken(true))
524 return TPResult::Error();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000525
Chris Lattner9af55002009-03-27 04:18:06 +0000526 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
Douglas Gregor69d83162011-01-20 16:08:06 +0000527 Tok.is(tok::ampamp) ||
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000528 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000529 // ptr-operator
530 ConsumeToken();
531 while (Tok.is(tok::kw_const) ||
532 Tok.is(tok::kw_volatile) ||
Chris Lattner9af55002009-03-27 04:18:06 +0000533 Tok.is(tok::kw_restrict))
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000534 ConsumeToken();
535 } else {
536 break;
537 }
538 }
539
540 // direct-declarator:
541 // direct-abstract-declarator:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000542 if (Tok.is(tok::ellipsis))
543 ConsumeToken();
544
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000545 if ((Tok.is(tok::identifier) ||
546 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
547 mayHaveIdentifier) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000548 // declarator-id
Argyrios Kyrtzidis1e054212009-07-21 17:05:03 +0000549 if (Tok.is(tok::annot_cxxscope))
550 ConsumeToken();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000551 ConsumeToken();
552 } else if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000553 ConsumeParen();
554 if (mayBeAbstract &&
555 (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +0000556 // 'int(...)' is a function.
557 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000558 isDeclarationSpecifier())) { // 'int(int)' is a function.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000559 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
560 // exception-specification[opt]
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000561 TPResult TPR = TryParseFunctionDeclarator();
562 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000563 return TPR;
564 } else {
565 // '(' declarator ')'
Argyrios Kyrtzidis1ee2c432008-10-05 14:27:18 +0000566 // '(' attributes declarator ')'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000567 // '(' abstract-declarator ')'
Chris Lattner6229c8e2010-09-28 23:35:09 +0000568 if (Tok.is(tok::kw___attribute) ||
569 Tok.is(tok::kw___declspec) ||
570 Tok.is(tok::kw___cdecl) ||
571 Tok.is(tok::kw___stdcall) ||
572 Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000573 Tok.is(tok::kw___thiscall) ||
Douglas Gregor8d267c52011-09-09 02:06:17 +0000574 Tok.is(tok::kw___unaligned))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000575 return TPResult::True(); // attributes indicate declaration
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +0000576 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000577 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000578 return TPR;
579 if (Tok.isNot(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000580 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000581 ConsumeParen();
582 }
583 } else if (!mayBeAbstract) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000584 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000585 }
586
587 while (1) {
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000588 TPResult TPR(TPResult::Ambiguous());
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000589
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000590 // abstract-declarator: ...
591 if (Tok.is(tok::ellipsis))
592 ConsumeToken();
593
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000594 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000595 // Check whether we have a function declarator or a possible ctor-style
596 // initializer that follows the declarator. Note that ctor-style
597 // initializers are not possible in contexts where abstract declarators
598 // are allowed.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +0000599 if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000600 break;
601
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000602 // direct-declarator '(' parameter-declaration-clause ')'
603 // cv-qualifier-seq[opt] exception-specification[opt]
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +0000604 ConsumeParen();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000605 TPR = TryParseFunctionDeclarator();
606 } else if (Tok.is(tok::l_square)) {
607 // direct-declarator '[' constant-expression[opt] ']'
608 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
609 TPR = TryParseBracketDeclarator();
610 } else {
611 break;
612 }
613
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000614 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000615 return TPR;
616 }
617
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000618 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000619}
620
Douglas Gregora61b3e72010-12-01 17:42:47 +0000621Parser::TPResult
622Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
623 switch (Kind) {
624 // Obviously starts an expression.
625 case tok::numeric_constant:
626 case tok::char_constant:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000627 case tok::wide_char_constant:
628 case tok::utf16_char_constant:
629 case tok::utf32_char_constant:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000630 case tok::string_literal:
631 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000632 case tok::utf8_string_literal:
633 case tok::utf16_string_literal:
634 case tok::utf32_string_literal:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000635 case tok::l_square:
636 case tok::l_paren:
637 case tok::amp:
Douglas Gregor69d83162011-01-20 16:08:06 +0000638 case tok::ampamp:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000639 case tok::star:
640 case tok::plus:
641 case tok::plusplus:
642 case tok::minus:
643 case tok::minusminus:
644 case tok::tilde:
645 case tok::exclaim:
646 case tok::kw_sizeof:
647 case tok::kw___func__:
648 case tok::kw_const_cast:
649 case tok::kw_delete:
650 case tok::kw_dynamic_cast:
651 case tok::kw_false:
652 case tok::kw_new:
653 case tok::kw_operator:
654 case tok::kw_reinterpret_cast:
655 case tok::kw_static_cast:
656 case tok::kw_this:
657 case tok::kw_throw:
658 case tok::kw_true:
659 case tok::kw_typeid:
660 case tok::kw_alignof:
661 case tok::kw_noexcept:
662 case tok::kw_nullptr:
663 case tok::kw___null:
664 case tok::kw___alignof:
665 case tok::kw___builtin_choose_expr:
666 case tok::kw___builtin_offsetof:
667 case tok::kw___builtin_types_compatible_p:
668 case tok::kw___builtin_va_arg:
669 case tok::kw___imag:
670 case tok::kw___real:
671 case tok::kw___FUNCTION__:
672 case tok::kw___PRETTY_FUNCTION__:
673 case tok::kw___has_nothrow_assign:
674 case tok::kw___has_nothrow_copy:
675 case tok::kw___has_nothrow_constructor:
676 case tok::kw___has_trivial_assign:
677 case tok::kw___has_trivial_copy:
678 case tok::kw___has_trivial_constructor:
679 case tok::kw___has_trivial_destructor:
680 case tok::kw___has_virtual_destructor:
681 case tok::kw___is_abstract:
682 case tok::kw___is_base_of:
683 case tok::kw___is_class:
Douglas Gregor9f361132011-01-27 20:28:01 +0000684 case tok::kw___is_convertible_to:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000685 case tok::kw___is_empty:
686 case tok::kw___is_enum:
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000687 case tok::kw___is_final:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000688 case tok::kw___is_literal:
Chandler Carruth38402812011-04-24 02:49:28 +0000689 case tok::kw___is_literal_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000690 case tok::kw___is_pod:
691 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +0000692 case tok::kw___is_trivial:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +0000693 case tok::kw___is_trivially_assignable:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000694 case tok::kw___is_trivially_constructible:
Sean Huntfeb375d2011-05-13 00:31:07 +0000695 case tok::kw___is_trivially_copyable:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000696 case tok::kw___is_union:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000697 case tok::kw___uuidof:
698 return TPResult::True();
699
700 // Obviously starts a type-specifier-seq:
701 case tok::kw_char:
702 case tok::kw_const:
703 case tok::kw_double:
704 case tok::kw_enum:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000705 case tok::kw_half:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000706 case tok::kw_float:
707 case tok::kw_int:
708 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000709 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000710 case tok::kw___int128:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000711 case tok::kw_restrict:
712 case tok::kw_short:
713 case tok::kw_signed:
714 case tok::kw_struct:
715 case tok::kw_union:
716 case tok::kw_unsigned:
717 case tok::kw_void:
718 case tok::kw_volatile:
719 case tok::kw__Bool:
720 case tok::kw__Complex:
721 case tok::kw_class:
722 case tok::kw_typename:
723 case tok::kw_wchar_t:
724 case tok::kw_char16_t:
725 case tok::kw_char32_t:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000726 case tok::kw___underlying_type:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000727 case tok::kw_thread_local:
728 case tok::kw__Decimal32:
729 case tok::kw__Decimal64:
730 case tok::kw__Decimal128:
731 case tok::kw___thread:
732 case tok::kw_typeof:
733 case tok::kw___cdecl:
734 case tok::kw___stdcall:
735 case tok::kw___fastcall:
736 case tok::kw___thiscall:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000737 case tok::kw___unaligned:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000738 case tok::kw___vector:
739 case tok::kw___pixel:
Eli Friedmanb001de72011-10-06 23:00:33 +0000740 case tok::kw__Atomic:
Douglas Gregora61b3e72010-12-01 17:42:47 +0000741 return TPResult::False();
742
743 default:
744 break;
745 }
746
747 return TPResult::Ambiguous();
748}
749
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000750/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
751/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
752/// be either a decl-specifier or a function-style cast, and TPResult::Error()
753/// if a parsing error was found and reported.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000754///
755/// decl-specifier:
756/// storage-class-specifier
757/// type-specifier
758/// function-specifier
759/// 'friend'
760/// 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000761/// [C++0x] 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000762/// [GNU] attributes declaration-specifiers[opt]
763///
764/// storage-class-specifier:
765/// 'register'
766/// 'static'
767/// 'extern'
768/// 'mutable'
769/// 'auto'
770/// [GNU] '__thread'
771///
772/// function-specifier:
773/// 'inline'
774/// 'virtual'
775/// 'explicit'
776///
777/// typedef-name:
778/// identifier
779///
780/// type-specifier:
781/// simple-type-specifier
782/// class-specifier
783/// enum-specifier
784/// elaborated-type-specifier
Douglas Gregord57959a2009-03-27 23:10:48 +0000785/// typename-specifier
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000786/// cv-qualifier
787///
788/// simple-type-specifier:
Douglas Gregord57959a2009-03-27 23:10:48 +0000789/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000790/// '::'[opt] nested-name-specifier 'template'
791/// simple-template-id [TODO]
792/// 'char'
793/// 'wchar_t'
794/// 'bool'
795/// 'short'
796/// 'int'
797/// 'long'
798/// 'signed'
799/// 'unsigned'
800/// 'float'
801/// 'double'
802/// 'void'
803/// [GNU] typeof-specifier
804/// [GNU] '_Complex'
805/// [C++0x] 'auto' [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000806/// [C++0x] 'decltype' ( expression )
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000807///
808/// type-name:
809/// class-name
810/// enum-name
811/// typedef-name
812///
813/// elaborated-type-specifier:
814/// class-key '::'[opt] nested-name-specifier[opt] identifier
815/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
816/// simple-template-id
817/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
818///
819/// enum-name:
820/// identifier
821///
822/// enum-specifier:
823/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
824/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
825///
826/// class-specifier:
827/// class-head '{' member-specification[opt] '}'
828///
829/// class-head:
830/// class-key identifier[opt] base-clause[opt]
831/// class-key nested-name-specifier identifier base-clause[opt]
832/// class-key nested-name-specifier[opt] simple-template-id
833/// base-clause[opt]
834///
835/// class-key:
836/// 'class'
837/// 'struct'
838/// 'union'
839///
840/// cv-qualifier:
841/// 'const'
842/// 'volatile'
843/// [GNU] restrict
844///
Richard Smithd81e9612012-02-23 01:36:12 +0000845Parser::TPResult
846Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult) {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000847 switch (Tok.getKind()) {
Chris Lattnere5849262009-01-04 23:33:56 +0000848 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +0000849 // Check for need to substitute AltiVec __vector keyword
850 // for "vector" identifier.
851 if (TryAltiVecVectorToken())
852 return TPResult::True();
853 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +0000854 case tok::kw_typename: // typename T::type
Chris Lattnere5849262009-01-04 23:33:56 +0000855 // Annotate typenames and C++ scope specifiers. If we get one, just
856 // recurse to handle whatever we get.
857 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000858 return TPResult::Error();
859 if (Tok.is(tok::identifier))
860 return TPResult::False();
Richard Smithd81e9612012-02-23 01:36:12 +0000861 return isCXXDeclarationSpecifier(BracedCastResult);
Chris Lattnere5849262009-01-04 23:33:56 +0000862
Chris Lattner2ee9b402009-12-19 01:11:05 +0000863 case tok::coloncolon: { // ::foo::bar
864 const Token &Next = NextToken();
865 if (Next.is(tok::kw_new) || // ::new
866 Next.is(tok::kw_delete)) // ::delete
John McCallae03cb52009-12-19 00:35:18 +0000867 return TPResult::False();
David Blaikie42d6d0c2011-12-04 05:04:18 +0000868 }
869 // Fall through.
870 case tok::kw_decltype:
Chris Lattnere5849262009-01-04 23:33:56 +0000871 // Annotate typenames and C++ scope specifiers. If we get one, just
872 // recurse to handle whatever we get.
873 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000874 return TPResult::Error();
Richard Smithd81e9612012-02-23 01:36:12 +0000875 return isCXXDeclarationSpecifier(BracedCastResult);
Nick Lewycky443aac92012-01-25 01:19:14 +0000876
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000877 // decl-specifier:
878 // storage-class-specifier
879 // type-specifier
880 // function-specifier
881 // 'friend'
882 // 'typedef'
Sebastian Redl2ac67232009-11-05 15:47:02 +0000883 // 'constexpr'
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000884 case tok::kw_friend:
885 case tok::kw_typedef:
Sebastian Redl2ac67232009-11-05 15:47:02 +0000886 case tok::kw_constexpr:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000887 // storage-class-specifier
888 case tok::kw_register:
889 case tok::kw_static:
890 case tok::kw_extern:
891 case tok::kw_mutable:
892 case tok::kw_auto:
893 case tok::kw___thread:
894 // function-specifier
895 case tok::kw_inline:
896 case tok::kw_virtual:
897 case tok::kw_explicit:
898
Douglas Gregor8d267c52011-09-09 02:06:17 +0000899 // Modules
900 case tok::kw___module_private__:
901
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000902 // type-specifier:
903 // simple-type-specifier
904 // class-specifier
905 // enum-specifier
906 // elaborated-type-specifier
907 // typename-specifier
908 // cv-qualifier
909
910 // class-specifier
911 // elaborated-type-specifier
912 case tok::kw_class:
913 case tok::kw_struct:
914 case tok::kw_union:
915 // enum-specifier
916 case tok::kw_enum:
917 // cv-qualifier
918 case tok::kw_const:
919 case tok::kw_volatile:
920
921 // GNU
922 case tok::kw_restrict:
923 case tok::kw__Complex:
924 case tok::kw___attribute:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +0000925 return TPResult::True();
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Steve Naroff7ec56582009-01-06 17:40:00 +0000927 // Microsoft
Steve Naroff47f52092009-01-06 19:34:12 +0000928 case tok::kw___declspec:
Steve Naroff7ec56582009-01-06 17:40:00 +0000929 case tok::kw___cdecl:
930 case tok::kw___stdcall:
931 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000932 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000933 case tok::kw___w64:
934 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +0000935 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +0000936 case tok::kw___forceinline:
Douglas Gregor8d267c52011-09-09 02:06:17 +0000937 case tok::kw___unaligned:
Eli Friedman290eeb02009-06-08 23:27:34 +0000938 return TPResult::True();
Dawn Perchik52fc3142010-09-03 01:29:35 +0000939
940 // Borland
941 case tok::kw___pascal:
942 return TPResult::True();
John Thompson82287d12010-02-05 00:12:22 +0000943
944 // AltiVec
945 case tok::kw___vector:
946 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000947
John McCall51e2a5d2010-04-30 03:11:01 +0000948 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000949 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
John McCall51e2a5d2010-04-30 03:11:01 +0000950 if (TemplateId->Kind != TNK_Type_template)
951 return TPResult::False();
952 CXXScopeSpec SS;
Douglas Gregor059101f2011-03-02 00:47:37 +0000953 AnnotateTemplateIdTokenAsType();
John McCall51e2a5d2010-04-30 03:11:01 +0000954 assert(Tok.is(tok::annot_typename));
955 goto case_typename;
956 }
957
John McCallae03cb52009-12-19 00:35:18 +0000958 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
959 // We've already annotated a scope; try to annotate a type.
John McCall9ba61662010-02-26 08:45:28 +0000960 if (TryAnnotateTypeOrScopeToken())
961 return TPResult::Error();
Nick Lewycky443aac92012-01-25 01:19:14 +0000962 if (!Tok.is(tok::annot_typename)) {
963 // If the next token is an identifier or a type qualifier, then this
964 // can't possibly be a valid expression either.
965 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
966 CXXScopeSpec SS;
967 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
968 Tok.getAnnotationRange(),
969 SS);
970 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
971 TentativeParsingAction PA(*this);
972 ConsumeToken();
973 ConsumeToken();
974 bool isIdentifier = Tok.is(tok::identifier);
975 TPResult TPR = TPResult::False();
976 if (!isIdentifier)
Richard Smithd81e9612012-02-23 01:36:12 +0000977 TPR = isCXXDeclarationSpecifier(BracedCastResult);
Nick Lewycky443aac92012-01-25 01:19:14 +0000978 PA.Revert();
979
980 if (isIdentifier ||
981 TPR == TPResult::True() || TPR == TPResult::Error())
982 return TPResult::Error();
983 }
984 }
John McCallae03cb52009-12-19 00:35:18 +0000985 return TPResult::False();
Nick Lewycky443aac92012-01-25 01:19:14 +0000986 }
John McCallae03cb52009-12-19 00:35:18 +0000987 // If that succeeded, fallthrough into the generic simple-type-id case.
988
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000989 // The ambiguity resides in a simple-type-specifier/typename-specifier
990 // followed by a '('. The '(' could either be the start of:
991 //
992 // direct-declarator:
993 // '(' declarator ')'
994 //
995 // direct-abstract-declarator:
996 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
997 // exception-specification[opt]
998 // '(' abstract-declarator ')'
999 //
1000 // or part of a function-style cast expression:
1001 //
1002 // simple-type-specifier '(' expression-list[opt] ')'
1003 //
1004
1005 // simple-type-specifier:
1006
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001007 case tok::annot_typename:
1008 case_typename:
1009 // In Objective-C, we might have a protocol-qualified type.
David Blaikie4e4d0842012-03-11 07:00:24 +00001010 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001011 // Tentatively parse the
1012 TentativeParsingAction PA(*this);
1013 ConsumeToken(); // The type token
1014
1015 TPResult TPR = TryParseProtocolQualifiers();
1016 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001017 bool isFollowedByBrace = Tok.is(tok::l_brace);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001018
1019 PA.Revert();
1020
1021 if (TPR == TPResult::Error())
1022 return TPResult::Error();
1023
1024 if (isFollowedByParen)
1025 return TPResult::Ambiguous();
Richard Smithd81e9612012-02-23 01:36:12 +00001026
David Blaikie4e4d0842012-03-11 07:00:24 +00001027 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001028 return BracedCastResult;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001029
1030 return TPResult::True();
1031 }
1032
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001033 case tok::kw_char:
1034 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001035 case tok::kw_char16_t:
1036 case tok::kw_char32_t:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001037 case tok::kw_bool:
1038 case tok::kw_short:
1039 case tok::kw_int:
1040 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001041 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00001042 case tok::kw___int128:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001043 case tok::kw_signed:
1044 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001045 case tok::kw_half:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001046 case tok::kw_float:
1047 case tok::kw_double:
1048 case tok::kw_void:
David Blaikie5e089fe2012-01-24 05:47:35 +00001049 case tok::annot_decltype:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001050 if (NextToken().is(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001051 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001052
Richard Smithd81e9612012-02-23 01:36:12 +00001053 // This is a function-style cast in all cases we disambiguate other than
1054 // one:
1055 // struct S {
1056 // enum E : int { a = 4 }; // enum
1057 // enum E : int { 4 }; // bit-field
1058 // };
David Blaikie4e4d0842012-03-11 07:00:24 +00001059 if (getLangOpts().CPlusPlus0x && NextToken().is(tok::l_brace))
Richard Smithd81e9612012-02-23 01:36:12 +00001060 return BracedCastResult;
1061
Douglas Gregor9497a732010-09-16 01:51:54 +00001062 if (isStartOfObjCClassMessageMissingOpenBracket())
1063 return TPResult::False();
1064
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001065 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001066
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001067 // GNU typeof support.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001068 case tok::kw_typeof: {
1069 if (NextToken().isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001070 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001071
1072 TentativeParsingAction PA(*this);
1073
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001074 TPResult TPR = TryParseTypeofSpecifier();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001075 bool isFollowedByParen = Tok.is(tok::l_paren);
Richard Smithd81e9612012-02-23 01:36:12 +00001076 bool isFollowedByBrace = Tok.is(tok::l_brace);
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001077
1078 PA.Revert();
1079
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001080 if (TPR == TPResult::Error())
1081 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001082
1083 if (isFollowedByParen)
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001084 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001085
David Blaikie4e4d0842012-03-11 07:00:24 +00001086 if (getLangOpts().CPlusPlus0x && isFollowedByBrace)
Richard Smithd81e9612012-02-23 01:36:12 +00001087 return BracedCastResult;
1088
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001089 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001090 }
1091
Sean Huntdb5d44b2011-05-19 05:37:45 +00001092 // C++0x type traits support
1093 case tok::kw___underlying_type:
1094 return TPResult::True();
1095
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001096 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00001097 case tok::kw__Atomic:
1098 return TPResult::True();
1099
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001100 default:
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001101 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001102 }
1103}
1104
1105/// [GNU] typeof-specifier:
1106/// 'typeof' '(' expressions ')'
1107/// 'typeof' '(' type-name ')'
1108///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001109Parser::TPResult Parser::TryParseTypeofSpecifier() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001110 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1111 ConsumeToken();
1112
1113 assert(Tok.is(tok::l_paren) && "Expected '('");
1114 // Parse through the parens after 'typeof'.
1115 ConsumeParen();
1116 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001117 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001118
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001119 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001120}
1121
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001122/// [ObjC] protocol-qualifiers:
1123//// '<' identifier-list '>'
1124Parser::TPResult Parser::TryParseProtocolQualifiers() {
1125 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1126 ConsumeToken();
1127 do {
1128 if (Tok.isNot(tok::identifier))
1129 return TPResult::Error();
1130 ConsumeToken();
1131
1132 if (Tok.is(tok::comma)) {
1133 ConsumeToken();
1134 continue;
1135 }
1136
1137 if (Tok.is(tok::greater)) {
1138 ConsumeToken();
1139 return TPResult::Ambiguous();
1140 }
1141 } while (false);
1142
1143 return TPResult::Error();
1144}
1145
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001146Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1147 TPResult TPR = isCXXDeclarationSpecifier();
1148 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001149 return TPR;
1150
1151 if (Tok.is(tok::kw_typeof))
1152 TryParseTypeofSpecifier();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001153 else {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001154 ConsumeToken();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001155
David Blaikie4e4d0842012-03-11 07:00:24 +00001156 if (getLangOpts().ObjC1 && Tok.is(tok::less))
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001157 TryParseProtocolQualifiers();
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001160 assert(Tok.is(tok::l_paren) && "Expected '('!");
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001161 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001162}
1163
1164/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1165/// a constructor-style initializer, when parsing declaration statements.
1166/// Returns true for function declarator and false for constructor-style
1167/// initializer.
1168/// If during the disambiguation process a parsing error is encountered,
1169/// the function returns true to let the declaration parsing code handle it.
1170///
1171/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1172/// exception-specification[opt]
1173///
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001174bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
Argyrios Kyrtzidisd3dbbb62008-10-05 21:10:08 +00001175
1176 // C++ 8.2p1:
1177 // The ambiguity arising from the similarity between a function-style cast and
1178 // a declaration mentioned in 6.8 can also occur in the context of a
1179 // declaration. In that context, the choice is between a function declaration
1180 // with a redundant set of parentheses around a parameter name and an object
1181 // declaration with a function-style cast as the initializer. Just as for the
1182 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1183 // that could possibly be a declaration a declaration.
1184
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001185 TentativeParsingAction PA(*this);
1186
1187 ConsumeParen();
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001188 TPResult TPR = TryParseParameterDeclarationClause();
1189 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1190 TPR = TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001191
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001192 SourceLocation TPLoc = Tok.getLocation();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001193 PA.Revert();
1194
1195 // In case of an error, let the declaration parsing code handle it.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001196 if (TPR == TPResult::Error())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001197 return true;
1198
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001199 if (TPR == TPResult::Ambiguous()) {
1200 // Function declarator has precedence over constructor-style initializer.
1201 // Emit a warning just in case the author intended a variable definition.
Argyrios Kyrtzidise75d8492008-10-17 23:23:35 +00001202 if (warnIfAmbiguous)
Chris Lattneref708fd2008-11-18 07:50:21 +00001203 Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1204 << SourceRange(Tok.getLocation(), TPLoc);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001205 return true;
Argyrios Kyrtzidis259b0d92008-10-15 23:21:32 +00001206 }
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001207
1208 return TPR == TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001209}
1210
1211/// parameter-declaration-clause:
1212/// parameter-declaration-list[opt] '...'[opt]
1213/// parameter-declaration-list ',' '...'
1214///
1215/// parameter-declaration-list:
1216/// parameter-declaration
1217/// parameter-declaration-list ',' parameter-declaration
1218///
1219/// parameter-declaration:
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001220/// decl-specifier-seq declarator attributes[opt]
1221/// decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1222/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1223/// decl-specifier-seq abstract-declarator[opt] attributes[opt]
1224/// '=' assignment-expression
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001225///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001226Parser::TPResult Parser::TryParseParameterDeclarationClause() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001227
1228 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001229 return TPResult::True();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001230
1231 // parameter-declaration-list[opt] '...'[opt]
1232 // parameter-declaration-list ',' '...'
1233 //
1234 // parameter-declaration-list:
1235 // parameter-declaration
1236 // parameter-declaration-list ',' parameter-declaration
1237 //
1238 while (1) {
1239 // '...'[opt]
1240 if (Tok.is(tok::ellipsis)) {
1241 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001242 if (Tok.is(tok::r_paren))
1243 return TPResult::True(); // '...)' is a sign of a function declarator.
1244 else
1245 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001246 }
1247
John McCall0b7e6782011-03-24 11:26:52 +00001248 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001249 MaybeParseMicrosoftAttributes(attrs);
Francois Pichet334d47e2010-10-11 12:59:39 +00001250
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001251 // decl-specifier-seq
Richard Smithd81e9612012-02-23 01:36:12 +00001252 // A parameter-declaration's initializer must be preceded by an '=', so
1253 // decl-specifier-seq '{' is not a parameter in C++11.
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001254 TPResult TPR = TryParseDeclarationSpecifier();
1255 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001256 return TPR;
1257
1258 // declarator
1259 // abstract-declarator[opt]
1260 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001261 if (TPR != TPResult::Ambiguous())
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001262 return TPR;
1263
Argyrios Kyrtzidis346af032010-12-08 02:02:46 +00001264 // [GNU] attributes[opt]
1265 if (Tok.is(tok::kw___attribute))
1266 return TPResult::True();
1267
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001268 if (Tok.is(tok::equal)) {
1269 // '=' assignment-expression
1270 // Parse through assignment-expression.
David Blaikieeb52f86a2012-04-09 16:37:11 +00001271 if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/,
1272 true/*DontConsume*/))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001273 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001274 }
1275
1276 if (Tok.is(tok::ellipsis)) {
1277 ConsumeToken();
Richard Smith22592862012-03-27 23:05:05 +00001278 if (Tok.is(tok::r_paren))
1279 return TPResult::True(); // '...)' is a sign of a function declarator.
1280 else
1281 return TPResult::False();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001282 }
1283
1284 if (Tok.isNot(tok::comma))
1285 break;
1286 ConsumeToken(); // the comma.
1287 }
1288
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001289 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001290}
1291
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001292/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1293/// parsing as a function declarator.
1294/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1295/// return TPResult::Ambiguous(), otherwise it will return either False() or
1296/// Error().
Mike Stump1eb44332009-09-09 15:08:12 +00001297///
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001298/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1299/// exception-specification[opt]
1300///
1301/// exception-specification:
1302/// 'throw' '(' type-id-list[opt] ')'
1303///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001304Parser::TPResult Parser::TryParseFunctionDeclarator() {
Argyrios Kyrtzidisd3616a82008-10-05 23:15:41 +00001305
1306 // The '(' is already parsed.
1307
1308 TPResult TPR = TryParseParameterDeclarationClause();
1309 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1310 TPR = TPResult::False();
1311
1312 if (TPR == TPResult::False() || TPR == TPResult::Error())
1313 return TPR;
1314
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001315 // Parse through the parens.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001316 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001317 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001318
1319 // cv-qualifier-seq
1320 while (Tok.is(tok::kw_const) ||
1321 Tok.is(tok::kw_volatile) ||
1322 Tok.is(tok::kw_restrict) )
1323 ConsumeToken();
1324
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001325 // ref-qualifier[opt]
1326 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1327 ConsumeToken();
1328
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001329 // exception-specification
1330 if (Tok.is(tok::kw_throw)) {
1331 ConsumeToken();
1332 if (Tok.isNot(tok::l_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001333 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001334
1335 // Parse through the parens after 'throw'.
1336 ConsumeParen();
1337 if (!SkipUntil(tok::r_paren))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001338 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001339 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001340 if (Tok.is(tok::kw_noexcept)) {
1341 ConsumeToken();
1342 // Possibly an expression as well.
1343 if (Tok.is(tok::l_paren)) {
1344 // Find the matching rparen.
1345 ConsumeParen();
1346 if (!SkipUntil(tok::r_paren))
1347 return TPResult::Error();
1348 }
1349 }
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001350
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001351 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001352}
1353
1354/// '[' constant-expression[opt] ']'
1355///
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001356Parser::TPResult Parser::TryParseBracketDeclarator() {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001357 ConsumeBracket();
1358 if (!SkipUntil(tok::r_square))
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001359 return TPResult::Error();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001360
Argyrios Kyrtzidisb9f34192008-10-05 18:52:21 +00001361 return TPResult::Ambiguous();
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +00001362}