blob: 1efa274083213c87fb802044a8b450960516b0de [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000020/// ParseOptionalCXXScopeSpecifier - Parse global scope or
21/// nested-name-specifier if present. Returns true if a nested-name-specifier
22/// was parsed from the token stream. Note that this routine will not parse
23/// ::new or ::delete, it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000024///
25/// '::'[opt] nested-name-specifier
26/// '::'
27///
28/// nested-name-specifier:
29/// type-name '::'
30/// namespace-name '::'
31/// nested-name-specifier identifier '::'
32/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
33///
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000034bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000035 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000036 "Call sites of this function should be guarded by checking for C++");
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000037
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000038 if (Tok.is(tok::annot_cxxscope)) {
39 SS.setScopeRep(Tok.getAnnotationValue());
40 SS.setRange(Tok.getAnnotationRange());
41 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000042 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000043 }
Chris Lattnere607e802009-01-04 21:14:15 +000044
Chris Lattner5b454732009-01-05 03:55:46 +000045 if (Tok.is(tok::coloncolon)) {
46 // ::new and ::delete aren't nested-name-specifiers.
47 tok::TokenKind NextKind = NextToken().getKind();
48 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
49 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000050
Chris Lattner55a7cef2009-01-05 00:13:00 +000051 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000052 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000053 SS.setBeginLoc(CCLoc);
54 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
55 SS.setEndLoc(CCLoc);
56 } else if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
57 SS.setBeginLoc(Tok.getLocation());
58 } else {
59 // Not a CXXScopeSpecifier.
60 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000061 }
62
63 // nested-name-specifier:
64 // type-name '::'
65 // namespace-name '::'
66 // nested-name-specifier identifier '::'
67 // nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000068 while (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
69 IdentifierInfo *II = Tok.getIdentifierInfo();
70 SourceLocation IdLoc = ConsumeToken();
Chris Lattnere607e802009-01-04 21:14:15 +000071 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000072 SourceLocation CCLoc = ConsumeToken();
73 if (SS.isInvalid())
74 continue;
75
76 SS.setScopeRep(
Chris Lattnere607e802009-01-04 21:14:15 +000077 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000078 SS.setEndLoc(CCLoc);
79 }
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000080
81 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000082}
83
84/// ParseCXXIdExpression - Handle id-expression.
85///
86/// id-expression:
87/// unqualified-id
88/// qualified-id
89///
90/// unqualified-id:
91/// identifier
92/// operator-function-id
93/// conversion-function-id [TODO]
94/// '~' class-name [TODO]
95/// template-id [TODO]
96///
97/// qualified-id:
98/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
99/// '::' identifier
100/// '::' operator-function-id
101/// '::' template-id [TODO]
102///
103/// nested-name-specifier:
104/// type-name '::'
105/// namespace-name '::'
106/// nested-name-specifier identifier '::'
107/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
108///
109/// NOTE: The standard specifies that, for qualified-id, the parser does not
110/// expect:
111///
112/// '::' conversion-function-id
113/// '::' '~' class-name
114///
115/// This may cause a slight inconsistency on diagnostics:
116///
117/// class C {};
118/// namespace A {}
119/// void f() {
120/// :: A :: ~ C(); // Some Sema error about using destructor with a
121/// // namespace.
122/// :: ~ C(); // Some Parser error like 'unexpected ~'.
123/// }
124///
125/// We simplify the parser a bit and make it work like:
126///
127/// qualified-id:
128/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
129/// '::' unqualified-id
130///
131/// That way Sema can handle and report similar errors for namespaces and the
132/// global scope.
133///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000134/// The isAddressOfOperand parameter indicates that this id-expression is a
135/// direct operand of the address-of operator. This is, besides member contexts,
136/// the only place where a qualified-id naming a non-static class member may
137/// appear.
138///
139Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000140 // qualified-id:
141 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
142 // '::' unqualified-id
143 //
144 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000145 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000146
147 // unqualified-id:
148 // identifier
149 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000150 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000151 // '~' class-name [TODO]
152 // template-id [TODO]
153 //
154 switch (Tok.getKind()) {
155 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000156 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000157
158 case tok::identifier: {
159 // Consume the identifier so that we can see if it is followed by a '('.
160 IdentifierInfo &II = *Tok.getIdentifierInfo();
161 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000162 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
163 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000164 }
165
166 case tok::kw_operator: {
167 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000168 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000169 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000170 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
171 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000172 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000173 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000174 Tok.is(tok::l_paren), SS,
175 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000176
Douglas Gregor2def4832008-11-17 20:34:05 +0000177 // We already complained about a bad conversion-function-id,
178 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000179 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000180 }
181
182 } // switch.
183
184 assert(0 && "The switch was supposed to take care everything.");
185}
186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187/// ParseCXXCasts - This handles the various ways to cast expressions to another
188/// type.
189///
190/// postfix-expression: [C++ 5.2p1]
191/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
192/// 'static_cast' '<' type-name '>' '(' expression ')'
193/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
194/// 'const_cast' '<' type-name '>' '(' expression ')'
195///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000196Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 tok::TokenKind Kind = Tok.getKind();
198 const char *CastName = 0; // For error messages
199
200 switch (Kind) {
201 default: assert(0 && "Unknown C++ cast!"); abort();
202 case tok::kw_const_cast: CastName = "const_cast"; break;
203 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
204 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
205 case tok::kw_static_cast: CastName = "static_cast"; break;
206 }
207
208 SourceLocation OpLoc = ConsumeToken();
209 SourceLocation LAngleBracketLoc = Tok.getLocation();
210
211 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000212 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213
214 TypeTy *CastTy = ParseTypeName();
215 SourceLocation RAngleBracketLoc = Tok.getLocation();
216
Chris Lattner1ab3b962008-11-18 07:48:38 +0000217 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000218 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
221
Chris Lattner1ab3b962008-11-18 07:48:38 +0000222 if (Tok.isNot(tok::l_paren))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000223 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
Sebastian Redld8c4e152008-12-11 22:33:27 +0000225 OwningExprResult Result(ParseSimpleParenExpression(RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000227 if (!Result.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000228 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
229 LAngleBracketLoc, CastTy, RAngleBracketLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000230 LParenLoc, Result.release(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231
Sebastian Redl20df9b72008-12-11 22:51:44 +0000232 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Sebastian Redlc42e1182008-11-11 11:37:55 +0000235/// ParseCXXTypeid - This handles the C++ typeid expression.
236///
237/// postfix-expression: [C++ 5.2p1]
238/// 'typeid' '(' expression ')'
239/// 'typeid' '(' type-id ')'
240///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000241Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000242 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
243
244 SourceLocation OpLoc = ConsumeToken();
245 SourceLocation LParenLoc = Tok.getLocation();
246 SourceLocation RParenLoc;
247
248 // typeid expressions are always parenthesized.
249 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
250 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000251 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000252
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000253 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000254
255 if (isTypeIdInParens()) {
256 TypeTy *Ty = ParseTypeName();
257
258 // Match the ')'.
259 MatchRHSPunctuation(tok::r_paren, LParenLoc);
260
261 if (!Ty)
Sebastian Redl20df9b72008-12-11 22:51:44 +0000262 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000263
264 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
265 Ty, RParenLoc);
266 } else {
267 Result = ParseExpression();
268
269 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000270 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000271 SkipUntil(tok::r_paren);
272 else {
273 MatchRHSPunctuation(tok::r_paren, LParenLoc);
274
275 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000276 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000277 }
278 }
279
Sebastian Redl20df9b72008-12-11 22:51:44 +0000280 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000281}
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
284///
285/// boolean-literal: [C++ 2.13.5]
286/// 'true'
287/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000288Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000290 return Owned(Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind));
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
Chris Lattner50dd2892008-02-26 00:51:44 +0000292
293/// ParseThrowExpression - This handles the C++ throw expression.
294///
295/// throw-expression: [C++ 15]
296/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000297Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000298 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000299 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000300
Chris Lattner2a2819a2008-04-06 06:02:23 +0000301 // If the current token isn't the start of an assignment-expression,
302 // then the expression is not present. This handles things like:
303 // "C ? throw : (void)42", which is crazy but legal.
304 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
305 case tok::semi:
306 case tok::r_paren:
307 case tok::r_square:
308 case tok::r_brace:
309 case tok::colon:
310 case tok::comma:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000311 return Owned(Actions.ActOnCXXThrow(ThrowLoc));
Chris Lattner50dd2892008-02-26 00:51:44 +0000312
Chris Lattner2a2819a2008-04-06 06:02:23 +0000313 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000314 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000315 if (Expr.isInvalid()) return move(Expr);
316 return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release()));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000317 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000318}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000319
320/// ParseCXXThis - This handles the C++ 'this' pointer.
321///
322/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
323/// a non-lvalue expression whose value is the address of the object for which
324/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000326 assert(Tok.is(tok::kw_this) && "Not 'this'!");
327 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000328 return Owned(Actions.ActOnCXXThis(ThisLoc));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000329}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000330
331/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
332/// Can be interpreted either as function-style casting ("int(x)")
333/// or class type construction ("ClassType(x,y,z)")
334/// or creation of a value-initialized type ("int()").
335///
336/// postfix-expression: [C++ 5.2p1]
337/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
338/// typename-specifier '(' expression-list[opt] ')' [TODO]
339///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000340Parser::OwningExprResult
341Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000342 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000343 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000344
345 assert(Tok.is(tok::l_paren) && "Expected '('!");
346 SourceLocation LParenLoc = ConsumeParen();
347
Sebastian Redla55e52c2008-11-25 22:21:31 +0000348 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000349 CommaLocsTy CommaLocs;
350
351 if (Tok.isNot(tok::r_paren)) {
352 if (ParseExpressionList(Exprs, CommaLocs)) {
353 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000354 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000355 }
356 }
357
358 // Match the ')'.
359 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
360
361 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
362 "Unexpected number of commas!");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000363 return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
364 LParenLoc,
365 Exprs.take(), Exprs.size(),
366 &CommaLocs[0], RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000367}
368
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000369/// ParseCXXCondition - if/switch/while/for condition expression.
370///
371/// condition:
372/// expression
373/// type-specifier-seq declarator '=' assignment-expression
374/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
375/// '=' assignment-expression
376///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000377Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000378 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000379 return ParseExpression(); // expression
380
381 SourceLocation StartLoc = Tok.getLocation();
382
383 // type-specifier-seq
384 DeclSpec DS;
385 ParseSpecifierQualifierList(DS);
386
387 // declarator
388 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
389 ParseDeclarator(DeclaratorInfo);
390
391 // simple-asm-expr[opt]
392 if (Tok.is(tok::kw_asm)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000393 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000394 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000395 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000396 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000397 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000398 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000399 }
400
401 // If attributes are present, parse them.
402 if (Tok.is(tok::kw___attribute))
403 DeclaratorInfo.AddAttributes(ParseAttributes());
404
405 // '=' assignment-expression
406 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000407 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000408 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000409 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000410 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000411 return ExprError();
412
413 return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
414 DeclaratorInfo,EqualLoc,
415 AssignExpr.release()));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000416}
417
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000418/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
419/// This should only be called when the current token is known to be part of
420/// simple-type-specifier.
421///
422/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000423/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000424/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
425/// char
426/// wchar_t
427/// bool
428/// short
429/// int
430/// long
431/// signed
432/// unsigned
433/// float
434/// double
435/// void
436/// [GNU] typeof-specifier
437/// [C++0x] auto [TODO]
438///
439/// type-name:
440/// class-name
441/// enum-name
442/// typedef-name
443///
444void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
445 DS.SetRangeStart(Tok.getLocation());
446 const char *PrevSpec;
447 SourceLocation Loc = Tok.getLocation();
448
449 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000450 case tok::identifier: // foo::bar
451 case tok::coloncolon: // ::foo::bar
452 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000453 default:
454 assert(0 && "Not a simple-type-specifier token!");
455 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000456
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000457 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000458 case tok::annot_typename: {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000459 DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
460 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000461 break;
462 }
463
464 // builtin types
465 case tok::kw_short:
466 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
467 break;
468 case tok::kw_long:
469 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
470 break;
471 case tok::kw_signed:
472 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
473 break;
474 case tok::kw_unsigned:
475 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
476 break;
477 case tok::kw_void:
478 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
479 break;
480 case tok::kw_char:
481 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
482 break;
483 case tok::kw_int:
484 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
485 break;
486 case tok::kw_float:
487 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
488 break;
489 case tok::kw_double:
490 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
491 break;
492 case tok::kw_wchar_t:
493 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
494 break;
495 case tok::kw_bool:
496 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
497 break;
498
499 // GNU typeof support.
500 case tok::kw_typeof:
501 ParseTypeofSpecifier(DS);
502 DS.Finish(Diags, PP.getSourceManager(), getLang());
503 return;
504 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000505 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000506 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
507 else
508 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000509 ConsumeToken();
510 DS.Finish(Diags, PP.getSourceManager(), getLang());
511}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000512
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000513/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
514/// [dcl.name]), which is a non-empty sequence of type-specifiers,
515/// e.g., "const short int". Note that the DeclSpec is *not* finished
516/// by parsing the type-specifier-seq, because these sequences are
517/// typically followed by some form of declarator. Returns true and
518/// emits diagnostics if this is not a type-specifier-seq, false
519/// otherwise.
520///
521/// type-specifier-seq: [C++ 8.1]
522/// type-specifier type-specifier-seq[opt]
523///
524bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
525 DS.SetRangeStart(Tok.getLocation());
526 const char *PrevSpec = 0;
527 int isInvalid = 0;
528
529 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000530 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000531 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000532 return true;
533 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000534
Ted Kremenekb8006e52009-01-06 19:17:58 +0000535 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000536
537 return false;
538}
539
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000540/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000541/// operator name (C++ [over.oper]). If successful, returns the
542/// predefined identifier that corresponds to that overloaded
543/// operator. Otherwise, returns NULL and does not consume any tokens.
544///
545/// operator-function-id: [C++ 13.5]
546/// 'operator' operator
547///
548/// operator: one of
549/// new delete new[] delete[]
550/// + - * / % ^ & | ~
551/// ! = < > += -= *= /= %=
552/// ^= &= |= << >> >>= <<= == !=
553/// <= >= && || ++ -- , ->* ->
554/// () []
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000555OverloadedOperatorKind Parser::TryParseOperatorFunctionId() {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000556 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000557
558 OverloadedOperatorKind Op = OO_None;
559 switch (NextToken().getKind()) {
560 case tok::kw_new:
561 ConsumeToken(); // 'operator'
562 ConsumeToken(); // 'new'
563 if (Tok.is(tok::l_square)) {
564 ConsumeBracket(); // '['
565 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
566 Op = OO_Array_New;
567 } else {
568 Op = OO_New;
569 }
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000570 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000571
572 case tok::kw_delete:
573 ConsumeToken(); // 'operator'
574 ConsumeToken(); // 'delete'
575 if (Tok.is(tok::l_square)) {
576 ConsumeBracket(); // '['
577 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
578 Op = OO_Array_Delete;
579 } else {
580 Op = OO_Delete;
581 }
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000582 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000583
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000584#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000585 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000586#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000587#include "clang/Basic/OperatorKinds.def"
588
589 case tok::l_paren:
590 ConsumeToken(); // 'operator'
591 ConsumeParen(); // '('
592 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000593 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000594
595 case tok::l_square:
596 ConsumeToken(); // 'operator'
597 ConsumeBracket(); // '['
598 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000599 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000600
601 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000602 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000603 }
604
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000605 ConsumeToken(); // 'operator'
606 ConsumeAnyToken(); // the operator itself
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000607 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000608}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000609
610/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
611/// which expresses the name of a user-defined conversion operator
612/// (C++ [class.conv.fct]p1). Returns the type that this operator is
613/// specifying a conversion for, or NULL if there was an error.
614///
615/// conversion-function-id: [C++ 12.3.2]
616/// operator conversion-type-id
617///
618/// conversion-type-id:
619/// type-specifier-seq conversion-declarator[opt]
620///
621/// conversion-declarator:
622/// ptr-operator conversion-declarator[opt]
623Parser::TypeTy *Parser::ParseConversionFunctionId() {
624 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
625 ConsumeToken(); // 'operator'
626
627 // Parse the type-specifier-seq.
628 DeclSpec DS;
629 if (ParseCXXTypeSpecifierSeq(DS))
630 return 0;
631
632 // Parse the conversion-declarator, which is merely a sequence of
633 // ptr-operators.
634 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000635 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000636
637 // Finish up the type.
638 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000639 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000640 return 0;
641 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000642 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000643}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000644
645/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
646/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000647///
648/// This method is called to parse the new expression after the optional :: has
649/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
650/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000651///
652/// new-expression:
653/// '::'[opt] 'new' new-placement[opt] new-type-id
654/// new-initializer[opt]
655/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
656/// new-initializer[opt]
657///
658/// new-placement:
659/// '(' expression-list ')'
660///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000661/// new-type-id:
662/// type-specifier-seq new-declarator[opt]
663///
664/// new-declarator:
665/// ptr-operator new-declarator[opt]
666/// direct-new-declarator
667///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000668/// new-initializer:
669/// '(' expression-list[opt] ')'
670/// [C++0x] braced-init-list [TODO]
671///
Chris Lattner59232d32009-01-04 21:25:24 +0000672Parser::OwningExprResult
673Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
674 assert(Tok.is(tok::kw_new) && "expected 'new' token");
675 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000676
677 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
678 // second form of new-expression. It can't be a new-type-id.
679
Sebastian Redla55e52c2008-11-25 22:21:31 +0000680 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000681 SourceLocation PlacementLParen, PlacementRParen;
682
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000683 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000684 DeclSpec DS;
685 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000686 if (Tok.is(tok::l_paren)) {
687 // If it turns out to be a placement, we change the type location.
688 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000689 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
690 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000691 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000692 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000693
694 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000695 if (PlacementRParen.isInvalid()) {
696 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000697 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000698 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000699
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000700 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000701 // Reset the placement locations. There was no placement.
702 PlacementLParen = PlacementRParen = SourceLocation();
703 ParenTypeId = true;
704 } else {
705 // We still need the type.
706 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000707 SourceLocation LParen = ConsumeParen();
708 ParseSpecifierQualifierList(DS);
709 ParseDeclarator(DeclaratorInfo);
710 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000711 ParenTypeId = true;
712 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000713 if (ParseCXXTypeSpecifierSeq(DS))
714 DeclaratorInfo.setInvalidType(true);
715 else
716 ParseDeclaratorInternal(DeclaratorInfo,
717 &Parser::ParseDirectNewDeclarator);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000718 ParenTypeId = false;
719 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000720 }
721 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000722 // A new-type-id is a simplified type-id, where essentially the
723 // direct-declarator is replaced by a direct-new-declarator.
724 if (ParseCXXTypeSpecifierSeq(DS))
725 DeclaratorInfo.setInvalidType(true);
726 else
727 ParseDeclaratorInternal(DeclaratorInfo,
728 &Parser::ParseDirectNewDeclarator);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000729 ParenTypeId = false;
730 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000731 if (DeclaratorInfo.getInvalidType()) {
732 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000733 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000734 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000735
Sebastian Redla55e52c2008-11-25 22:21:31 +0000736 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000737 SourceLocation ConstructorLParen, ConstructorRParen;
738
739 if (Tok.is(tok::l_paren)) {
740 ConstructorLParen = ConsumeParen();
741 if (Tok.isNot(tok::r_paren)) {
742 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000743 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
744 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000745 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000746 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000747 }
748 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000749 if (ConstructorRParen.isInvalid()) {
750 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000751 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000752 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000753 }
754
Sebastian Redl20df9b72008-12-11 22:51:44 +0000755 return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
756 PlacementArgs.take(), PlacementArgs.size(),
757 PlacementRParen, ParenTypeId, DeclaratorInfo,
758 ConstructorLParen, ConstructorArgs.take(),
759 ConstructorArgs.size(), ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000760}
761
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000762/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
763/// passed to ParseDeclaratorInternal.
764///
765/// direct-new-declarator:
766/// '[' expression ']'
767/// direct-new-declarator '[' constant-expression ']'
768///
Chris Lattner59232d32009-01-04 21:25:24 +0000769void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000770 // Parse the array dimensions.
771 bool first = true;
772 while (Tok.is(tok::l_square)) {
773 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000774 OwningExprResult Size(first ? ParseExpression()
775 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000776 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000777 // Recover
778 SkipUntil(tok::r_square);
779 return;
780 }
781 first = false;
782
783 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000784 Size.release(), LLoc));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000785
786 if (MatchRHSPunctuation(tok::r_square, LLoc).isInvalid())
787 return;
788 }
789}
790
791/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
792/// This ambiguity appears in the syntax of the C++ new operator.
793///
794/// new-expression:
795/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
796/// new-initializer[opt]
797///
798/// new-placement:
799/// '(' expression-list ')'
800///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000801bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000802 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000803 // The '(' was already consumed.
804 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000805 ParseSpecifierQualifierList(D.getMutableDeclSpec());
806 ParseDeclarator(D);
807 return D.getInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000808 }
809
810 // It's not a type, it has to be an expression list.
811 // Discard the comma locations - ActOnCXXNew has enough parameters.
812 CommaLocsTy CommaLocs;
813 return ParseExpressionList(PlacementArgs, CommaLocs);
814}
815
816/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
817/// to free memory allocated by new.
818///
Chris Lattner59232d32009-01-04 21:25:24 +0000819/// This method is called to parse the 'delete' expression after the optional
820/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
821/// and "Start" is its location. Otherwise, "Start" is the location of the
822/// 'delete' token.
823///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000824/// delete-expression:
825/// '::'[opt] 'delete' cast-expression
826/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000827Parser::OwningExprResult
828Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
829 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
830 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000831
832 // Array delete?
833 bool ArrayDelete = false;
834 if (Tok.is(tok::l_square)) {
835 ArrayDelete = true;
836 SourceLocation LHS = ConsumeBracket();
837 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
838 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000839 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000840 }
841
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000842 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000843 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000844 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000845
Sebastian Redl20df9b72008-12-11 22:51:44 +0000846 return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete,
847 Operand.release()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000848}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000849
850static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
851{
852 switch(kind) {
853 default: assert(false && "Not a known unary type trait.");
854 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
855 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
856 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
857 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
858 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
859 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
860 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
861 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
862 case tok::kw___is_abstract: return UTT_IsAbstract;
863 case tok::kw___is_class: return UTT_IsClass;
864 case tok::kw___is_empty: return UTT_IsEmpty;
865 case tok::kw___is_enum: return UTT_IsEnum;
866 case tok::kw___is_pod: return UTT_IsPOD;
867 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
868 case tok::kw___is_union: return UTT_IsUnion;
869 }
870}
871
872/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
873/// pseudo-functions that allow implementation of the TR1/C++0x type traits
874/// templates.
875///
876/// primary-expression:
877/// [GNU] unary-type-trait '(' type-id ')'
878///
879Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
880{
881 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
882 SourceLocation Loc = ConsumeToken();
883
884 SourceLocation LParen = Tok.getLocation();
885 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
886 return ExprError();
887
888 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
889 // there will be cryptic errors about mismatched parentheses and missing
890 // specifiers.
891 TypeTy *Ty = ParseTypeName();
892
893 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
894
895 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty, RParen);
896}