blob: d05c00292dbabb18bae392b63a3080e2eb71adbf [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
14#include "clang/Basic/Diagnostic.h"
15#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 Redl20df9b72008-12-11 22:51:44 +0000134Parser::OwningExprResult Parser::ParseCXXIdExpression() {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000135 // qualified-id:
136 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
137 // '::' unqualified-id
138 //
139 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000140 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000141
142 // unqualified-id:
143 // identifier
144 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000145 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000146 // '~' class-name [TODO]
147 // template-id [TODO]
148 //
149 switch (Tok.getKind()) {
150 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000151 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000152
153 case tok::identifier: {
154 // Consume the identifier so that we can see if it is followed by a '('.
155 IdentifierInfo &II = *Tok.getIdentifierInfo();
156 SourceLocation L = ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000157 return Owned(Actions.ActOnIdentifierExpr(CurScope, L, II,
158 Tok.is(tok::l_paren), &SS));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000159 }
160
161 case tok::kw_operator: {
162 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000163 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000164 return Owned(Actions.ActOnCXXOperatorFunctionIdExpr(
165 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS));
Chris Lattner7452c6f2009-01-05 01:24:05 +0000166 if (TypeTy *Type = ParseConversionFunctionId())
167 return Owned(Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc,
168 Type,
169 Tok.is(tok::l_paren), SS));
Sebastian Redl20df9b72008-12-11 22:51:44 +0000170
Douglas Gregor2def4832008-11-17 20:34:05 +0000171 // We already complained about a bad conversion-function-id,
172 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000173 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000174 }
175
176 } // switch.
177
178 assert(0 && "The switch was supposed to take care everything.");
179}
180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181/// ParseCXXCasts - This handles the various ways to cast expressions to another
182/// type.
183///
184/// postfix-expression: [C++ 5.2p1]
185/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
186/// 'static_cast' '<' type-name '>' '(' expression ')'
187/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
188/// 'const_cast' '<' type-name '>' '(' expression ')'
189///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000190Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 tok::TokenKind Kind = Tok.getKind();
192 const char *CastName = 0; // For error messages
193
194 switch (Kind) {
195 default: assert(0 && "Unknown C++ cast!"); abort();
196 case tok::kw_const_cast: CastName = "const_cast"; break;
197 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
198 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
199 case tok::kw_static_cast: CastName = "static_cast"; break;
200 }
201
202 SourceLocation OpLoc = ConsumeToken();
203 SourceLocation LAngleBracketLoc = Tok.getLocation();
204
205 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000206 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
208 TypeTy *CastTy = ParseTypeName();
209 SourceLocation RAngleBracketLoc = Tok.getLocation();
210
Chris Lattner1ab3b962008-11-18 07:48:38 +0000211 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000212 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000213
214 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
215
Chris Lattner1ab3b962008-11-18 07:48:38 +0000216 if (Tok.isNot(tok::l_paren))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000217 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Sebastian Redld8c4e152008-12-11 22:33:27 +0000219 OwningExprResult Result(ParseSimpleParenExpression(RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000220
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000221 if (!Result.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000222 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
223 LAngleBracketLoc, CastTy, RAngleBracketLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000224 LParenLoc, Result.release(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
Sebastian Redl20df9b72008-12-11 22:51:44 +0000226 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000227}
228
Sebastian Redlc42e1182008-11-11 11:37:55 +0000229/// ParseCXXTypeid - This handles the C++ typeid expression.
230///
231/// postfix-expression: [C++ 5.2p1]
232/// 'typeid' '(' expression ')'
233/// 'typeid' '(' type-id ')'
234///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000235Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000236 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
237
238 SourceLocation OpLoc = ConsumeToken();
239 SourceLocation LParenLoc = Tok.getLocation();
240 SourceLocation RParenLoc;
241
242 // typeid expressions are always parenthesized.
243 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
244 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000245 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000246
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000247 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000248
249 if (isTypeIdInParens()) {
250 TypeTy *Ty = ParseTypeName();
251
252 // Match the ')'.
253 MatchRHSPunctuation(tok::r_paren, LParenLoc);
254
255 if (!Ty)
Sebastian Redl20df9b72008-12-11 22:51:44 +0000256 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000257
258 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
259 Ty, RParenLoc);
260 } else {
261 Result = ParseExpression();
262
263 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000264 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000265 SkipUntil(tok::r_paren);
266 else {
267 MatchRHSPunctuation(tok::r_paren, LParenLoc);
268
269 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000270 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000271 }
272 }
273
Sebastian Redl20df9b72008-12-11 22:51:44 +0000274 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000275}
276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
278///
279/// boolean-literal: [C++ 2.13.5]
280/// 'true'
281/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000282Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000284 return Owned(Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind));
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
Chris Lattner50dd2892008-02-26 00:51:44 +0000286
287/// ParseThrowExpression - This handles the C++ throw expression.
288///
289/// throw-expression: [C++ 15]
290/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000291Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000292 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000293 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294
Chris Lattner2a2819a2008-04-06 06:02:23 +0000295 // If the current token isn't the start of an assignment-expression,
296 // then the expression is not present. This handles things like:
297 // "C ? throw : (void)42", which is crazy but legal.
298 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
299 case tok::semi:
300 case tok::r_paren:
301 case tok::r_square:
302 case tok::r_brace:
303 case tok::colon:
304 case tok::comma:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000305 return Owned(Actions.ActOnCXXThrow(ThrowLoc));
Chris Lattner50dd2892008-02-26 00:51:44 +0000306
Chris Lattner2a2819a2008-04-06 06:02:23 +0000307 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000308 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000309 if (Expr.isInvalid()) return move(Expr);
310 return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release()));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000311 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000312}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000313
314/// ParseCXXThis - This handles the C++ 'this' pointer.
315///
316/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
317/// a non-lvalue expression whose value is the address of the object for which
318/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000319Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000320 assert(Tok.is(tok::kw_this) && "Not 'this'!");
321 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000322 return Owned(Actions.ActOnCXXThis(ThisLoc));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000323}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000324
325/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
326/// Can be interpreted either as function-style casting ("int(x)")
327/// or class type construction ("ClassType(x,y,z)")
328/// or creation of a value-initialized type ("int()").
329///
330/// postfix-expression: [C++ 5.2p1]
331/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
332/// typename-specifier '(' expression-list[opt] ')' [TODO]
333///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000334Parser::OwningExprResult
335Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000336 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
337 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
338
339 assert(Tok.is(tok::l_paren) && "Expected '('!");
340 SourceLocation LParenLoc = ConsumeParen();
341
Sebastian Redla55e52c2008-11-25 22:21:31 +0000342 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000343 CommaLocsTy CommaLocs;
344
345 if (Tok.isNot(tok::r_paren)) {
346 if (ParseExpressionList(Exprs, CommaLocs)) {
347 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000348 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000349 }
350 }
351
352 // Match the ')'.
353 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
354
355 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
356 "Unexpected number of commas!");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000357 return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
358 LParenLoc,
359 Exprs.take(), Exprs.size(),
360 &CommaLocs[0], RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000361}
362
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000363/// ParseCXXCondition - if/switch/while/for condition expression.
364///
365/// condition:
366/// expression
367/// type-specifier-seq declarator '=' assignment-expression
368/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
369/// '=' assignment-expression
370///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000371Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000372 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000373 return ParseExpression(); // expression
374
375 SourceLocation StartLoc = Tok.getLocation();
376
377 // type-specifier-seq
378 DeclSpec DS;
379 ParseSpecifierQualifierList(DS);
380
381 // declarator
382 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
383 ParseDeclarator(DeclaratorInfo);
384
385 // simple-asm-expr[opt]
386 if (Tok.is(tok::kw_asm)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000387 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000388 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000389 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000390 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000391 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000392 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000393 }
394
395 // If attributes are present, parse them.
396 if (Tok.is(tok::kw___attribute))
397 DeclaratorInfo.AddAttributes(ParseAttributes());
398
399 // '=' assignment-expression
400 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000401 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000402 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000403 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000404 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000405 return ExprError();
406
407 return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
408 DeclaratorInfo,EqualLoc,
409 AssignExpr.release()));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000410}
411
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000412/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
413/// This should only be called when the current token is known to be part of
414/// simple-type-specifier.
415///
416/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000417/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000418/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
419/// char
420/// wchar_t
421/// bool
422/// short
423/// int
424/// long
425/// signed
426/// unsigned
427/// float
428/// double
429/// void
430/// [GNU] typeof-specifier
431/// [C++0x] auto [TODO]
432///
433/// type-name:
434/// class-name
435/// enum-name
436/// typedef-name
437///
438void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
439 DS.SetRangeStart(Tok.getLocation());
440 const char *PrevSpec;
441 SourceLocation Loc = Tok.getLocation();
442
443 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000444 case tok::identifier: // foo::bar
445 case tok::coloncolon: // ::foo::bar
446 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000447 default:
448 assert(0 && "Not a simple-type-specifier token!");
449 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000450
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000451 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000452 case tok::annot_typename: {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000453 DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
454 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000455 break;
456 }
457
458 // builtin types
459 case tok::kw_short:
460 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
461 break;
462 case tok::kw_long:
463 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
464 break;
465 case tok::kw_signed:
466 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
467 break;
468 case tok::kw_unsigned:
469 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
470 break;
471 case tok::kw_void:
472 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
473 break;
474 case tok::kw_char:
475 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
476 break;
477 case tok::kw_int:
478 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
479 break;
480 case tok::kw_float:
481 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
482 break;
483 case tok::kw_double:
484 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
485 break;
486 case tok::kw_wchar_t:
487 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
488 break;
489 case tok::kw_bool:
490 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
491 break;
492
493 // GNU typeof support.
494 case tok::kw_typeof:
495 ParseTypeofSpecifier(DS);
496 DS.Finish(Diags, PP.getSourceManager(), getLang());
497 return;
498 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000499 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000500 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
501 else
502 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000503 ConsumeToken();
504 DS.Finish(Diags, PP.getSourceManager(), getLang());
505}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000506
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000507/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
508/// [dcl.name]), which is a non-empty sequence of type-specifiers,
509/// e.g., "const short int". Note that the DeclSpec is *not* finished
510/// by parsing the type-specifier-seq, because these sequences are
511/// typically followed by some form of declarator. Returns true and
512/// emits diagnostics if this is not a type-specifier-seq, false
513/// otherwise.
514///
515/// type-specifier-seq: [C++ 8.1]
516/// type-specifier type-specifier-seq[opt]
517///
518bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
519 DS.SetRangeStart(Tok.getLocation());
520 const char *PrevSpec = 0;
521 int isInvalid = 0;
522
523 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000524 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000525 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000526 return true;
527 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000528
Ted Kremenekb8006e52009-01-06 19:17:58 +0000529 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000530
531 return false;
532}
533
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000534/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000535/// operator name (C++ [over.oper]). If successful, returns the
536/// predefined identifier that corresponds to that overloaded
537/// operator. Otherwise, returns NULL and does not consume any tokens.
538///
539/// operator-function-id: [C++ 13.5]
540/// 'operator' operator
541///
542/// operator: one of
543/// new delete new[] delete[]
544/// + - * / % ^ & | ~
545/// ! = < > += -= *= /= %=
546/// ^= &= |= << >> >>= <<= == !=
547/// <= >= && || ++ -- , ->* ->
548/// () []
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000549OverloadedOperatorKind Parser::TryParseOperatorFunctionId() {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000550 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000551
552 OverloadedOperatorKind Op = OO_None;
553 switch (NextToken().getKind()) {
554 case tok::kw_new:
555 ConsumeToken(); // 'operator'
556 ConsumeToken(); // 'new'
557 if (Tok.is(tok::l_square)) {
558 ConsumeBracket(); // '['
559 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
560 Op = OO_Array_New;
561 } else {
562 Op = OO_New;
563 }
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000564 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000565
566 case tok::kw_delete:
567 ConsumeToken(); // 'operator'
568 ConsumeToken(); // 'delete'
569 if (Tok.is(tok::l_square)) {
570 ConsumeBracket(); // '['
571 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
572 Op = OO_Array_Delete;
573 } else {
574 Op = OO_Delete;
575 }
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000576 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000577
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000578#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000579 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000580#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000581#include "clang/Basic/OperatorKinds.def"
582
583 case tok::l_paren:
584 ConsumeToken(); // 'operator'
585 ConsumeParen(); // '('
586 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000587 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000588
589 case tok::l_square:
590 ConsumeToken(); // 'operator'
591 ConsumeBracket(); // '['
592 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000593 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000594
595 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000596 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000597 }
598
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000599 ConsumeToken(); // 'operator'
600 ConsumeAnyToken(); // the operator itself
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000601 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000602}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000603
604/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
605/// which expresses the name of a user-defined conversion operator
606/// (C++ [class.conv.fct]p1). Returns the type that this operator is
607/// specifying a conversion for, or NULL if there was an error.
608///
609/// conversion-function-id: [C++ 12.3.2]
610/// operator conversion-type-id
611///
612/// conversion-type-id:
613/// type-specifier-seq conversion-declarator[opt]
614///
615/// conversion-declarator:
616/// ptr-operator conversion-declarator[opt]
617Parser::TypeTy *Parser::ParseConversionFunctionId() {
618 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
619 ConsumeToken(); // 'operator'
620
621 // Parse the type-specifier-seq.
622 DeclSpec DS;
623 if (ParseCXXTypeSpecifierSeq(DS))
624 return 0;
625
626 // Parse the conversion-declarator, which is merely a sequence of
627 // ptr-operators.
628 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000629 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000630
631 // Finish up the type.
632 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
633 if (Result.isInvalid)
634 return 0;
635 else
636 return Result.Val;
637}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000638
639/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
640/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000641///
642/// This method is called to parse the new expression after the optional :: has
643/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
644/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000645///
646/// new-expression:
647/// '::'[opt] 'new' new-placement[opt] new-type-id
648/// new-initializer[opt]
649/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
650/// new-initializer[opt]
651///
652/// new-placement:
653/// '(' expression-list ')'
654///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000655/// new-type-id:
656/// type-specifier-seq new-declarator[opt]
657///
658/// new-declarator:
659/// ptr-operator new-declarator[opt]
660/// direct-new-declarator
661///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000662/// new-initializer:
663/// '(' expression-list[opt] ')'
664/// [C++0x] braced-init-list [TODO]
665///
Chris Lattner59232d32009-01-04 21:25:24 +0000666Parser::OwningExprResult
667Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
668 assert(Tok.is(tok::kw_new) && "expected 'new' token");
669 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000670
671 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
672 // second form of new-expression. It can't be a new-type-id.
673
Sebastian Redla55e52c2008-11-25 22:21:31 +0000674 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000675 SourceLocation PlacementLParen, PlacementRParen;
676
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000677 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000678 DeclSpec DS;
679 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000680 if (Tok.is(tok::l_paren)) {
681 // If it turns out to be a placement, we change the type location.
682 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000683 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
684 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000685 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000686 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000687
688 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000689 if (PlacementRParen.isInvalid()) {
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
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000694 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000695 // Reset the placement locations. There was no placement.
696 PlacementLParen = PlacementRParen = SourceLocation();
697 ParenTypeId = true;
698 } else {
699 // We still need the type.
700 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000701 SourceLocation LParen = ConsumeParen();
702 ParseSpecifierQualifierList(DS);
703 ParseDeclarator(DeclaratorInfo);
704 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000705 ParenTypeId = true;
706 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000707 if (ParseCXXTypeSpecifierSeq(DS))
708 DeclaratorInfo.setInvalidType(true);
709 else
710 ParseDeclaratorInternal(DeclaratorInfo,
711 &Parser::ParseDirectNewDeclarator);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000712 ParenTypeId = false;
713 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000714 }
715 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000716 // A new-type-id is a simplified type-id, where essentially the
717 // direct-declarator is replaced by a direct-new-declarator.
718 if (ParseCXXTypeSpecifierSeq(DS))
719 DeclaratorInfo.setInvalidType(true);
720 else
721 ParseDeclaratorInternal(DeclaratorInfo,
722 &Parser::ParseDirectNewDeclarator);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000723 ParenTypeId = false;
724 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000725 if (DeclaratorInfo.getInvalidType()) {
726 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000727 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000728 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000729
Sebastian Redla55e52c2008-11-25 22:21:31 +0000730 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000731 SourceLocation ConstructorLParen, ConstructorRParen;
732
733 if (Tok.is(tok::l_paren)) {
734 ConstructorLParen = ConsumeParen();
735 if (Tok.isNot(tok::r_paren)) {
736 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000737 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
738 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000739 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000740 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000741 }
742 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000743 if (ConstructorRParen.isInvalid()) {
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
Sebastian Redl20df9b72008-12-11 22:51:44 +0000749 return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
750 PlacementArgs.take(), PlacementArgs.size(),
751 PlacementRParen, ParenTypeId, DeclaratorInfo,
752 ConstructorLParen, ConstructorArgs.take(),
753 ConstructorArgs.size(), ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000754}
755
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000756/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
757/// passed to ParseDeclaratorInternal.
758///
759/// direct-new-declarator:
760/// '[' expression ']'
761/// direct-new-declarator '[' constant-expression ']'
762///
Chris Lattner59232d32009-01-04 21:25:24 +0000763void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000764 // Parse the array dimensions.
765 bool first = true;
766 while (Tok.is(tok::l_square)) {
767 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000768 OwningExprResult Size(first ? ParseExpression()
769 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000770 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000771 // Recover
772 SkipUntil(tok::r_square);
773 return;
774 }
775 first = false;
776
777 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000778 Size.release(), LLoc));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000779
780 if (MatchRHSPunctuation(tok::r_square, LLoc).isInvalid())
781 return;
782 }
783}
784
785/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
786/// This ambiguity appears in the syntax of the C++ new operator.
787///
788/// new-expression:
789/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
790/// new-initializer[opt]
791///
792/// new-placement:
793/// '(' expression-list ')'
794///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000795bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000796 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000797 // The '(' was already consumed.
798 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000799 ParseSpecifierQualifierList(D.getMutableDeclSpec());
800 ParseDeclarator(D);
801 return D.getInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000802 }
803
804 // It's not a type, it has to be an expression list.
805 // Discard the comma locations - ActOnCXXNew has enough parameters.
806 CommaLocsTy CommaLocs;
807 return ParseExpressionList(PlacementArgs, CommaLocs);
808}
809
810/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
811/// to free memory allocated by new.
812///
Chris Lattner59232d32009-01-04 21:25:24 +0000813/// This method is called to parse the 'delete' expression after the optional
814/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
815/// and "Start" is its location. Otherwise, "Start" is the location of the
816/// 'delete' token.
817///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000818/// delete-expression:
819/// '::'[opt] 'delete' cast-expression
820/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000821Parser::OwningExprResult
822Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
823 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
824 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000825
826 // Array delete?
827 bool ArrayDelete = false;
828 if (Tok.is(tok::l_square)) {
829 ArrayDelete = true;
830 SourceLocation LHS = ConsumeBracket();
831 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
832 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000833 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000834 }
835
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000836 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000837 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000838 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000839
Sebastian Redl20df9b72008-12-11 22:51:44 +0000840 return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete,
841 Operand.release()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000842}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000843
844static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
845{
846 switch(kind) {
847 default: assert(false && "Not a known unary type trait.");
848 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
849 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
850 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
851 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
852 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
853 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
854 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
855 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
856 case tok::kw___is_abstract: return UTT_IsAbstract;
857 case tok::kw___is_class: return UTT_IsClass;
858 case tok::kw___is_empty: return UTT_IsEmpty;
859 case tok::kw___is_enum: return UTT_IsEnum;
860 case tok::kw___is_pod: return UTT_IsPOD;
861 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
862 case tok::kw___is_union: return UTT_IsUnion;
863 }
864}
865
866/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
867/// pseudo-functions that allow implementation of the TR1/C++0x type traits
868/// templates.
869///
870/// primary-expression:
871/// [GNU] unary-type-trait '(' type-id ')'
872///
873Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
874{
875 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
876 SourceLocation Loc = ConsumeToken();
877
878 SourceLocation LParen = Tok.getLocation();
879 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
880 return ExprError();
881
882 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
883 // there will be cryptic errors about mismatched parentheses and missing
884 // specifiers.
885 TypeTy *Ty = ParseTypeName();
886
887 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
888
889 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty, RParen);
890}