blob: b7dbcc7de60ac4f22d31daf849f7eb518bcd20a3 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000015#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000016#include "clang/Lex/IdentifierTable.h"
Chris Lattner1b926492006-08-23 06:42:10 +000017using namespace clang;
18
Chris Lattner0eedafe2006-08-24 04:56:27 +000019//===----------------------------------------------------------------------===//
20// Primary Expressions.
21//===----------------------------------------------------------------------===//
22
Steve Naroff408451b2007-02-26 22:17:12 +000023StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroff53f07dc2007-05-17 21:49:33 +000024 bool Wide, QualType t, SourceLocation firstLoc,
25 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000026 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000027 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000028 char *AStrData = new char[byteLength];
29 memcpy(AStrData, strData, byteLength);
30 StrData = AStrData;
31 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000032 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000033 firstTokLoc = firstLoc;
34 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000035}
36
Steve Naroffdf7855b2007-02-21 23:46:25 +000037StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000038 delete[] StrData;
39}
40
Chris Lattner15768702006-11-05 23:54:51 +000041bool UnaryOperator::isPostfix(Opcode Op) {
42 switch (Op) {
43 case PostInc:
44 case PostDec:
45 return true;
46 default:
47 return false;
48 }
49}
50
Chris Lattner1b926492006-08-23 06:42:10 +000051/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
52/// corresponds to, e.g. "sizeof" or "[pre]++".
53const char *UnaryOperator::getOpcodeStr(Opcode Op) {
54 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000055 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000056 case PostInc: return "++";
57 case PostDec: return "--";
58 case PreInc: return "++";
59 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000060 case AddrOf: return "&";
61 case Deref: return "*";
62 case Plus: return "+";
63 case Minus: return "-";
64 case Not: return "~";
65 case LNot: return "!";
66 case Real: return "__real";
67 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000068 case SizeOf: return "sizeof";
69 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000070 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000071 }
72}
73
Chris Lattner0eedafe2006-08-24 04:56:27 +000074//===----------------------------------------------------------------------===//
75// Postfix Operators.
76//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000077
Steve Naroff509fe022007-05-17 01:16:00 +000078CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
Chris Lattner9b3b9a12007-06-27 06:08:24 +000079 SourceLocation rparenloc)
Steve Naroffae4143e2007-04-26 20:39:23 +000080 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000081 Args = new Expr*[numargs];
82 for (unsigned i = 0; i != numargs; ++i)
83 Args[i] = args[i];
Chris Lattner9b3b9a12007-06-27 06:08:24 +000084 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +000085}
86
Chris Lattner1b926492006-08-23 06:42:10 +000087/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
88/// corresponds to, e.g. "<<=".
89const char *BinaryOperator::getOpcodeStr(Opcode Op) {
90 switch (Op) {
91 default: assert(0 && "Unknown binary operator");
92 case Mul: return "*";
93 case Div: return "/";
94 case Rem: return "%";
95 case Add: return "+";
96 case Sub: return "-";
97 case Shl: return "<<";
98 case Shr: return ">>";
99 case LT: return "<";
100 case GT: return ">";
101 case LE: return "<=";
102 case GE: return ">=";
103 case EQ: return "==";
104 case NE: return "!=";
105 case And: return "&";
106 case Xor: return "^";
107 case Or: return "|";
108 case LAnd: return "&&";
109 case LOr: return "||";
110 case Assign: return "=";
111 case MulAssign: return "*=";
112 case DivAssign: return "/=";
113 case RemAssign: return "%=";
114 case AddAssign: return "+=";
115 case SubAssign: return "-=";
116 case ShlAssign: return "<<=";
117 case ShrAssign: return ">>=";
118 case AndAssign: return "&=";
119 case XorAssign: return "^=";
120 case OrAssign: return "|=";
121 case Comma: return ",";
122 }
123}
Steve Naroff47500512007-04-19 23:00:49 +0000124
Chris Lattner1ec5f562007-06-27 05:38:08 +0000125
126//===----------------------------------------------------------------------===//
127// Generic Expression Routines
128//===----------------------------------------------------------------------===//
129
130/// hasLocalSideEffect - Return true if this immediate expression has side
131/// effects, not counting any sub-expressions.
132bool Expr::hasLocalSideEffect() const {
133 switch (getStmtClass()) {
134 default:
135 return false;
136 case ParenExprClass:
137 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
138 case UnaryOperatorClass: {
139 const UnaryOperator *UO = cast<UnaryOperator>(this);
140
141 switch (UO->getOpcode()) {
142 default: return false;
143 case UnaryOperator::PostInc:
144 case UnaryOperator::PostDec:
145 case UnaryOperator::PreInc:
146 case UnaryOperator::PreDec:
147 return true; // ++/--
148
Chris Lattnera44d1162007-06-27 05:58:59 +0000149 case UnaryOperator::Deref:
150 // Dereferencing a volatile pointer is a side-effect.
151 return getType().isVolatileQualified();
152 case UnaryOperator::Real:
153 case UnaryOperator::Imag:
154 // accessing a piece of a volatile complex is a side-effect.
155 return UO->getSubExpr()->getType().isVolatileQualified();
156
Chris Lattner1ec5f562007-06-27 05:38:08 +0000157 case UnaryOperator::Extension:
158 return UO->getSubExpr()->hasLocalSideEffect();
159 }
160 }
161 case BinaryOperatorClass:
162 return cast<BinaryOperator>(this)->isAssignmentOp();
163
Chris Lattnera44d1162007-06-27 05:58:59 +0000164 case MemberExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000165 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000166 // If the base pointer or element is to a volatile pointer/field, accessing
167 // if is a side effect.
168 return getType().isVolatileQualified();
Chris Lattner1ec5f562007-06-27 05:38:08 +0000169
170 case CallExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000171 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
172 // should warn.
Chris Lattner1ec5f562007-06-27 05:38:08 +0000173 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000174
175 case CastExprClass:
176 // If this is a cast to void, check the operand. Otherwise, the result of
177 // the cast is unused.
178 if (getType()->isVoidType())
179 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
180 return false;
181 }
182}
183
Steve Naroff475cca02007-05-14 17:19:29 +0000184/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
185/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000186/// - name, where name must be a variable
187/// - e[i]
188/// - (e), where e must be an lvalue
189/// - e.name, where e must be an lvalue
190/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000191/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000192/// - string-constant
193///
Steve Naroff9358c712007-05-27 23:58:33 +0000194Expr::isLvalueResult Expr::isLvalue() {
Steve Naroff475cca02007-05-14 17:19:29 +0000195 // first, check the type (C99 6.3.2.1)
Steve Naroff9358c712007-05-27 23:58:33 +0000196 if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType()
197 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000198
Steve Naroff475cca02007-05-14 17:19:29 +0000199 if (TR->isIncompleteType() && TR->isVoidType())
Steve Naroff9358c712007-05-27 23:58:33 +0000200 return LV_IncompleteVoidType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000201
Steve Naroff5dd642e2007-05-14 18:14:51 +0000202 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000203 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000204 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000205 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000206 // For vectors, make sure base is an lvalue (i.e. not a function call).
207 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
208 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000209 return LV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000210 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff9358c712007-05-27 23:58:33 +0000211 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
212 return LV_Valid;
213 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000214 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000215 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff9358c712007-05-27 23:58:33 +0000216 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000217 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff9358c712007-05-27 23:58:33 +0000218 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
219 return LV_Valid;
220 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000221 case ParenExprClass: // C99 6.5.1p5
222 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000223 default:
224 break;
Steve Naroff47500512007-04-19 23:00:49 +0000225 }
Steve Naroff9358c712007-05-27 23:58:33 +0000226 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000227}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000228
Steve Naroff475cca02007-05-14 17:19:29 +0000229/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
230/// does not have an incomplete type, does not have a const-qualified type, and
231/// if it is a structure or union, does not have any member (including,
232/// recursively, any member or element of all contained aggregates or unions)
233/// with a const-qualified type.
Steve Naroff9358c712007-05-27 23:58:33 +0000234Expr::isModifiableLvalueResult Expr::isModifiableLvalue() {
235 isLvalueResult lvalResult = isLvalue();
236
237 switch (lvalResult) {
Chris Lattner1ec5f562007-06-27 05:38:08 +0000238 case LV_Valid: break;
239 case LV_NotObjectType: return MLV_NotObjectType;
240 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
241 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000242 }
Steve Naroff475cca02007-05-14 17:19:29 +0000243 if (TR.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000244 return MLV_ConstQualified;
Steve Naroff475cca02007-05-14 17:19:29 +0000245 if (TR->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000246 return MLV_ArrayType;
Steve Naroff475cca02007-05-14 17:19:29 +0000247 if (TR->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000248 return MLV_IncompleteType;
249
250 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
251 if (r->hasConstFields())
252 return MLV_ConstQualified;
253 }
254 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000255}
256
Chris Lattner1f4479e2007-06-05 04:15:44 +0000257/// isIntegerConstantExpr - this recursive routine will test if an expression is
258/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000259/// C99 the result of the sizeof operator is no longer always a constant
260/// expression. The generalization of the wording to include any subexpression
261/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
262/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
263/// "0 || f()" can be treated as a constant expression. In C90 this expression,
264/// occurring in a context requiring a constant, would have been a constraint
265/// violation. FIXME: This routine currently implements C90 semantics.
266/// To properly implement C99 semantics this routine will need to evaluate
267/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000268
269/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
270/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000271///
272/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
273/// permit this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000274bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, SourceLocation *Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000275 bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000276 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000277 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000278 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000279 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000280 case ParenExprClass:
281 return cast<ParenExpr>(this)->getSubExpr()->
282 isIntegerConstantExpr(Result, Loc, isEvaluated);
283 case IntegerLiteralClass:
284 Result = cast<IntegerLiteral>(this)->getValue();
285 break;
286 case CharacterLiteralClass:
287 // FIXME: This doesn't set the right width etc.
288 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
Chris Lattner4cd73fd2007-06-08 21:51:02 +0000289 Result = cast<CharacterLiteral>(this)->getValue();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000290 break;
291 case DeclRefExprClass:
292 if (const EnumConstantDecl *D =
293 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000294 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000295 break;
296 }
297 if (Loc) *Loc = getLocStart();
298 return false;
299 case UnaryOperatorClass: {
300 const UnaryOperator *Exp = cast<UnaryOperator>(this);
301
302 // Get the operand value. If this is sizeof/alignof, do not evalute the
303 // operand. This affects C99 6.6p3.
304 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
305 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
306 return false;
307
308 switch (Exp->getOpcode()) {
309 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
310 // See C99 6.6p3.
311 default:
312 if (Loc) *Loc = Exp->getOperatorLoc();
313 return false;
314 case UnaryOperator::Extension:
315 return true;
316 case UnaryOperator::SizeOf:
317 case UnaryOperator::AlignOf:
318 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
319 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Loc))
320 return false;
321
322 // FIXME: Evaluate sizeof/alignof.
323 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
324 Result = 1; // FIXME: Obviously bogus
325 break;
326 case UnaryOperator::LNot: {
327 bool Val = Result != 0;
328 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
329 Result = Val;
330 break;
331 }
332 case UnaryOperator::Plus:
333 // FIXME: Do usual unary promotions here!
334 break;
335 case UnaryOperator::Minus:
336 // FIXME: Do usual unary promotions here!
337 Result = -Result;
338 break;
339 case UnaryOperator::Not:
340 // FIXME: Do usual unary promotions here!
341 Result = ~Result;
342 break;
343 }
344 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000345 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000346 case SizeOfAlignOfTypeExprClass: {
347 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
348 // alignof always evaluates to a constant.
349 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Loc))
350 return false;
351
352 // FIXME: Evaluate sizeof/alignof.
353 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
354 Result = 1; // FIXME: Obviously bogus
355 break;
356 }
357 case BinaryOperatorClass: {
358 const BinaryOperator *Exp = cast<BinaryOperator>(this);
359
360 // The LHS of a constant expr is always evaluated and needed.
361 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Loc, isEvaluated))
362 return false;
363
Chris Lattner23b7eb62007-06-15 23:05:46 +0000364 llvm::APSInt RHS(Result);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000365
366 // The short-circuiting &&/|| operators don't necessarily evaluate their
367 // RHS. Make sure to pass isEvaluated down correctly.
368 if (Exp->isLogicalOp()) {
369 bool RHSEval;
370 if (Exp->getOpcode() == BinaryOperator::LAnd)
371 RHSEval = Result != 0;
372 else {
373 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
374 RHSEval = Result == 0;
375 }
376
377 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc,
378 isEvaluated & RHSEval))
379 return false;
380 } else {
381 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc, isEvaluated))
382 return false;
383 }
384
385 // FIXME: These should all do the standard promotions, etc.
386 switch (Exp->getOpcode()) {
387 default:
388 if (Loc) *Loc = getLocStart();
389 return false;
390 case BinaryOperator::Mul:
391 Result *= RHS;
392 break;
393 case BinaryOperator::Div:
394 if (RHS == 0) {
395 if (!isEvaluated) break;
396 if (Loc) *Loc = getLocStart();
397 return false;
398 }
399 Result /= RHS;
400 break;
401 case BinaryOperator::Rem:
402 if (RHS == 0) {
403 if (!isEvaluated) break;
404 if (Loc) *Loc = getLocStart();
405 return false;
406 }
407 Result %= RHS;
408 break;
409 case BinaryOperator::Add: Result += RHS; break;
410 case BinaryOperator::Sub: Result -= RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000411 case BinaryOperator::Shl:
412 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
413 break;
414 case BinaryOperator::Shr:
415 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
416 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000417 case BinaryOperator::LT: Result = Result < RHS; break;
418 case BinaryOperator::GT: Result = Result > RHS; break;
419 case BinaryOperator::LE: Result = Result <= RHS; break;
420 case BinaryOperator::GE: Result = Result >= RHS; break;
421 case BinaryOperator::EQ: Result = Result == RHS; break;
422 case BinaryOperator::NE: Result = Result != RHS; break;
423 case BinaryOperator::And: Result &= RHS; break;
424 case BinaryOperator::Xor: Result ^= RHS; break;
425 case BinaryOperator::Or: Result |= RHS; break;
426 case BinaryOperator::LAnd:
427 Result = Result != 0 && RHS != 0;
428 break;
429 case BinaryOperator::LOr:
430 Result = Result != 0 || RHS != 0;
431 break;
432
433 case BinaryOperator::Comma:
434 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
435 // *except* when they are contained within a subexpression that is not
436 // evaluated". Note that Assignment can never happen due to constraints
437 // on the LHS subexpr, so we don't need to check it here.
438 if (isEvaluated) {
439 if (Loc) *Loc = getLocStart();
440 return false;
441 }
442
443 // The result of the constant expr is the RHS.
444 Result = RHS;
445 return true;
446 }
447
448 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
449 break;
450 }
451 case CastExprClass: {
452 const CastExpr *Exp = cast<CastExpr>(this);
453 // C99 6.6p6: shall only convert arithmetic types to integer types.
454 if (!Exp->getSubExpr()->getType()->isArithmeticType() ||
455 !Exp->getDestType()->isIntegerType()) {
456 if (Loc) *Loc = Exp->getSubExpr()->getLocStart();
457 return false;
458 }
459
460 // Handle simple integer->integer casts.
461 if (Exp->getSubExpr()->getType()->isIntegerType()) {
462 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
463 return false;
464 // FIXME: do the conversion on Result.
465 break;
466 }
467
468 // Allow floating constants that are the immediate operands of casts or that
469 // are parenthesized.
470 const Expr *Operand = Exp->getSubExpr();
471 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
472 Operand = PE->getSubExpr();
473
474 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
475 // FIXME: Evaluate this correctly!
476 Result = (int)FL->getValue();
477 break;
478 }
Chris Lattner6046e002007-06-05 06:39:23 +0000479 if (Loc) *Loc = Operand->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000480 return false;
481 }
482 case ConditionalOperatorClass: {
483 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
484
485 if (!Exp->getCond()->isIntegerConstantExpr(Result, Loc, isEvaluated))
486 return false;
487
488 const Expr *TrueExp = Exp->getLHS();
489 const Expr *FalseExp = Exp->getRHS();
490 if (Result == 0) std::swap(TrueExp, FalseExp);
491
492 // Evaluate the false one first, discard the result.
493 if (!FalseExp->isIntegerConstantExpr(Result, Loc, false))
494 return false;
495 // Evalute the true one, capture the result.
496 if (!TrueExp->isIntegerConstantExpr(Result, Loc, isEvaluated))
497 return false;
498 // FIXME: promotions on result.
499 break;
500 }
501 }
502
503 // Cases that are valid constant exprs fall through to here.
504 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
505 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000506}
507
Chris Lattner1f4479e2007-06-05 04:15:44 +0000508
Chris Lattner7eef9192007-05-24 01:23:49 +0000509/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
510/// integer constant expression with the value zero, or if this is one that is
511/// cast to void*.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000512bool Expr::isNullPointerConstant() const {
Chris Lattner7eef9192007-05-24 01:23:49 +0000513 // Strip off a cast to void*, if it exists.
514 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
515 // Check that it is a cast to void*.
516 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
517 QualType Pointee = PT->getPointeeType();
518 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
519 CE->getSubExpr()->getType()->isIntegerType()) // from int.
520 return CE->getSubExpr()->isNullPointerConstant();
Steve Naroffada7d422007-05-20 17:54:12 +0000521 }
Chris Lattner7eef9192007-05-24 01:23:49 +0000522 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
523 // Accept ((void*)0) as a null pointer constant, as many other
524 // implementations do.
525 return PE->getSubExpr()->isNullPointerConstant();
Chris Lattnere6b0f622007-05-21 05:58:35 +0000526 }
527
Chris Lattner7eef9192007-05-24 01:23:49 +0000528 // This expression must be an integer type.
529 if (!getType()->isIntegerType())
530 return false;
531
Chris Lattner1abbd412007-06-08 17:58:43 +0000532 // If we have an integer constant expression, we need to *evaluate* it and
533 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000534 llvm::APSInt Val(32);
Chris Lattner1abbd412007-06-08 17:58:43 +0000535 return isIntegerConstantExpr(Val, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000536}