blob: 2edea9796d4992f31b0b0dc1c341ef9b4a3e31a1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
17#include "clang/Lex/IdentifierTable.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
24StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
25 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
27 Expr(StringLiteralClass, t) {
28 // OPTIMIZE: could allocate this appended to the StringLiteral.
29 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
33 IsWide = Wide;
34 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
36}
37
38StringLiteral::~StringLiteral() {
39 delete[] StrData;
40}
41
42bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
52/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
53/// corresponds to, e.g. "sizeof" or "[pre]++".
54const char *UnaryOperator::getOpcodeStr(Opcode Op) {
55 switch (Op) {
56 default: assert(0 && "Unknown unary operator");
57 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
61 case AddrOf: return "&";
62 case Deref: return "*";
63 case Plus: return "+";
64 case Minus: return "-";
65 case Not: return "~";
66 case LNot: return "!";
67 case Real: return "__real";
68 case Imag: return "__imag";
69 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
71 case Extension: return "__extension__";
72 }
73}
74
75//===----------------------------------------------------------------------===//
76// Postfix Operators.
77//===----------------------------------------------------------------------===//
78
79CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
80 SourceLocation rparenloc)
81 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
82 Args = new Expr*[numargs];
83 for (unsigned i = 0; i != numargs; ++i)
84 Args[i] = args[i];
85 RParenLoc = rparenloc;
86}
87
88/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
89/// corresponds to, e.g. "<<=".
90const char *BinaryOperator::getOpcodeStr(Opcode Op) {
91 switch (Op) {
92 default: assert(0 && "Unknown binary operator");
93 case Mul: return "*";
94 case Div: return "/";
95 case Rem: return "%";
96 case Add: return "+";
97 case Sub: return "-";
98 case Shl: return "<<";
99 case Shr: return ">>";
100 case LT: return "<";
101 case GT: return ">";
102 case LE: return "<=";
103 case GE: return ">=";
104 case EQ: return "==";
105 case NE: return "!=";
106 case And: return "&";
107 case Xor: return "^";
108 case Or: return "|";
109 case LAnd: return "&&";
110 case LOr: return "||";
111 case Assign: return "=";
112 case MulAssign: return "*=";
113 case DivAssign: return "/=";
114 case RemAssign: return "%=";
115 case AddAssign: return "+=";
116 case SubAssign: return "-=";
117 case ShlAssign: return "<<=";
118 case ShrAssign: return ">>=";
119 case AndAssign: return "&=";
120 case XorAssign: return "^=";
121 case OrAssign: return "|=";
122 case Comma: return ",";
123 }
124}
125
126
127//===----------------------------------------------------------------------===//
128// Generic Expression Routines
129//===----------------------------------------------------------------------===//
130
131/// hasLocalSideEffect - Return true if this immediate expression has side
132/// effects, not counting any sub-expressions.
133bool Expr::hasLocalSideEffect() const {
134 switch (getStmtClass()) {
135 default:
136 return false;
137 case ParenExprClass:
138 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
139 case UnaryOperatorClass: {
140 const UnaryOperator *UO = cast<UnaryOperator>(this);
141
142 switch (UO->getOpcode()) {
143 default: return false;
144 case UnaryOperator::PostInc:
145 case UnaryOperator::PostDec:
146 case UnaryOperator::PreInc:
147 case UnaryOperator::PreDec:
148 return true; // ++/--
149
150 case UnaryOperator::Deref:
151 // Dereferencing a volatile pointer is a side-effect.
152 return getType().isVolatileQualified();
153 case UnaryOperator::Real:
154 case UnaryOperator::Imag:
155 // accessing a piece of a volatile complex is a side-effect.
156 return UO->getSubExpr()->getType().isVolatileQualified();
157
158 case UnaryOperator::Extension:
159 return UO->getSubExpr()->hasLocalSideEffect();
160 }
161 }
162 case BinaryOperatorClass:
163 return cast<BinaryOperator>(this)->isAssignmentOp();
164
165 case MemberExprClass:
166 case ArraySubscriptExprClass:
167 // If the base pointer or element is to a volatile pointer/field, accessing
168 // if is a side effect.
169 return getType().isVolatileQualified();
170
171 case CallExprClass:
172 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
173 // should warn.
174 return true;
175
176 case CastExprClass:
177 // If this is a cast to void, check the operand. Otherwise, the result of
178 // the cast is unused.
179 if (getType()->isVoidType())
180 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
181 return false;
182 }
183}
184
185/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
186/// incomplete type other than void. Nonarray expressions that can be lvalues:
187/// - name, where name must be a variable
188/// - e[i]
189/// - (e), where e must be an lvalue
190/// - e.name, where e must be an lvalue
191/// - e->name
192/// - *e, the type of e cannot be a function type
193/// - string-constant
194///
Bill Wendlingca51c972007-07-16 07:07:56 +0000195Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // first, check the type (C99 6.3.2.1)
197 if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType()
198 return LV_NotObjectType;
199
200 if (TR->isIncompleteType() && TR->isVoidType())
201 return LV_IncompleteVoidType;
202
203 // the type looks fine, now check the expression
204 switch (getStmtClass()) {
205 case StringLiteralClass: // C99 6.5.1p4
206 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
207 // For vectors, make sure base is an lvalue (i.e. not a function call).
208 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
209 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
210 return LV_Valid;
211 case DeclRefExprClass: // C99 6.5.1p2
212 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
213 return LV_Valid;
214 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000215 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 const MemberExpr *m = cast<MemberExpr>(this);
217 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000218 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 case UnaryOperatorClass: // C99 6.5.3p4
220 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
221 return LV_Valid;
222 break;
223 case ParenExprClass: // C99 6.5.1p5
224 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
225 default:
226 break;
227 }
228 return LV_InvalidExpression;
229}
230
231/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
232/// does not have an incomplete type, does not have a const-qualified type, and
233/// if it is a structure or union, does not have any member (including,
234/// recursively, any member or element of all contained aggregates or unions)
235/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000236Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 isLvalueResult lvalResult = isLvalue();
238
239 switch (lvalResult) {
240 case LV_Valid: break;
241 case LV_NotObjectType: return MLV_NotObjectType;
242 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
243 case LV_InvalidExpression: return MLV_InvalidExpression;
244 }
245 if (TR.isConstQualified())
246 return MLV_ConstQualified;
247 if (TR->isArrayType())
248 return MLV_ArrayType;
249 if (TR->isIncompleteType())
250 return MLV_IncompleteType;
251
252 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
253 if (r->hasConstFields())
254 return MLV_ConstQualified;
255 }
256 return MLV_Valid;
257}
258
259/// isIntegerConstantExpr - this recursive routine will test if an expression is
260/// an integer constant expression. Note: With the introduction of VLA's in
261/// C99 the result of the sizeof operator is no longer always a constant
262/// expression. The generalization of the wording to include any subexpression
263/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
264/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
265/// "0 || f()" can be treated as a constant expression. In C90 this expression,
266/// occurring in a context requiring a constant, would have been a constraint
267/// violation. FIXME: This routine currently implements C90 semantics.
268/// To properly implement C99 semantics this routine will need to evaluate
269/// expressions involving operators previously mentioned.
270
271/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
272/// comma, etc
273///
274/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
275/// permit this.
Chris Lattner590b6642007-07-15 23:26:56 +0000276bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
277 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 switch (getStmtClass()) {
279 default:
280 if (Loc) *Loc = getLocStart();
281 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 case ParenExprClass:
283 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000284 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 case IntegerLiteralClass:
286 Result = cast<IntegerLiteral>(this)->getValue();
287 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000288 case CharacterLiteralClass: {
289 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
290 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
291 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000292 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000294 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 case DeclRefExprClass:
296 if (const EnumConstantDecl *D =
297 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
298 Result = D->getInitVal();
299 break;
300 }
301 if (Loc) *Loc = getLocStart();
302 return false;
303 case UnaryOperatorClass: {
304 const UnaryOperator *Exp = cast<UnaryOperator>(this);
305
306 // Get the operand value. If this is sizeof/alignof, do not evalute the
307 // operand. This affects C99 6.6p3.
308 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
Chris Lattner590b6642007-07-15 23:26:56 +0000309 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 return false;
311
312 switch (Exp->getOpcode()) {
313 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
314 // See C99 6.6p3.
315 default:
316 if (Loc) *Loc = Exp->getOperatorLoc();
317 return false;
318 case UnaryOperator::Extension:
319 return true;
320 case UnaryOperator::SizeOf:
321 case UnaryOperator::AlignOf:
322 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000323 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 return false;
325
326 // FIXME: Evaluate sizeof/alignof.
327 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
328 Result = 1; // FIXME: Obviously bogus
329 break;
330 case UnaryOperator::LNot: {
331 bool Val = Result != 0;
332 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
333 Result = Val;
334 break;
335 }
336 case UnaryOperator::Plus:
337 // FIXME: Do usual unary promotions here!
338 break;
339 case UnaryOperator::Minus:
340 // FIXME: Do usual unary promotions here!
341 Result = -Result;
342 break;
343 case UnaryOperator::Not:
344 // FIXME: Do usual unary promotions here!
345 Result = ~Result;
346 break;
347 }
348 break;
349 }
350 case SizeOfAlignOfTypeExprClass: {
351 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
352 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000353 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 return false;
355
356 // FIXME: Evaluate sizeof/alignof.
357 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
358 Result = 1; // FIXME: Obviously bogus
359 break;
360 }
361 case BinaryOperatorClass: {
362 const BinaryOperator *Exp = cast<BinaryOperator>(this);
363
364 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000365 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 return false;
367
368 llvm::APSInt RHS(Result);
369
370 // The short-circuiting &&/|| operators don't necessarily evaluate their
371 // RHS. Make sure to pass isEvaluated down correctly.
372 if (Exp->isLogicalOp()) {
373 bool RHSEval;
374 if (Exp->getOpcode() == BinaryOperator::LAnd)
375 RHSEval = Result != 0;
376 else {
377 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
378 RHSEval = Result == 0;
379 }
380
Chris Lattner590b6642007-07-15 23:26:56 +0000381 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 isEvaluated & RHSEval))
383 return false;
384 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000385 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 return false;
387 }
388
389 // FIXME: These should all do the standard promotions, etc.
390 switch (Exp->getOpcode()) {
391 default:
392 if (Loc) *Loc = getLocStart();
393 return false;
394 case BinaryOperator::Mul:
395 Result *= RHS;
396 break;
397 case BinaryOperator::Div:
398 if (RHS == 0) {
399 if (!isEvaluated) break;
400 if (Loc) *Loc = getLocStart();
401 return false;
402 }
403 Result /= RHS;
404 break;
405 case BinaryOperator::Rem:
406 if (RHS == 0) {
407 if (!isEvaluated) break;
408 if (Loc) *Loc = getLocStart();
409 return false;
410 }
411 Result %= RHS;
412 break;
413 case BinaryOperator::Add: Result += RHS; break;
414 case BinaryOperator::Sub: Result -= RHS; break;
415 case BinaryOperator::Shl:
416 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
417 break;
418 case BinaryOperator::Shr:
419 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
420 break;
421 case BinaryOperator::LT: Result = Result < RHS; break;
422 case BinaryOperator::GT: Result = Result > RHS; break;
423 case BinaryOperator::LE: Result = Result <= RHS; break;
424 case BinaryOperator::GE: Result = Result >= RHS; break;
425 case BinaryOperator::EQ: Result = Result == RHS; break;
426 case BinaryOperator::NE: Result = Result != RHS; break;
427 case BinaryOperator::And: Result &= RHS; break;
428 case BinaryOperator::Xor: Result ^= RHS; break;
429 case BinaryOperator::Or: Result |= RHS; break;
430 case BinaryOperator::LAnd:
431 Result = Result != 0 && RHS != 0;
432 break;
433 case BinaryOperator::LOr:
434 Result = Result != 0 || RHS != 0;
435 break;
436
437 case BinaryOperator::Comma:
438 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
439 // *except* when they are contained within a subexpression that is not
440 // evaluated". Note that Assignment can never happen due to constraints
441 // on the LHS subexpr, so we don't need to check it here.
442 if (isEvaluated) {
443 if (Loc) *Loc = getLocStart();
444 return false;
445 }
446
447 // The result of the constant expr is the RHS.
448 Result = RHS;
449 return true;
450 }
451
452 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
453 break;
454 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000455 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000457 const Expr *SubExpr;
458 SourceLocation CastLoc;
459 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
460 SubExpr = C->getSubExpr();
461 CastLoc = C->getLParenLoc();
462 } else {
463 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
464 CastLoc = getLocStart();
465 }
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000468 if (!SubExpr->getType()->isArithmeticType() ||
469 !getType()->isIntegerType()) {
470 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 return false;
472 }
473
474 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000475 if (SubExpr->getType()->isIntegerType()) {
476 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000478
479 // Figure out if this is a truncate, extend or noop cast.
480 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
481
482 // If the input is signed, do a sign extend, noop, or truncate.
483 if (SubExpr->getType()->isSignedIntegerType())
484 Result.sextOrTrunc(DestWidth);
485 else // If the input is unsigned, do a zero extend, noop, or truncate.
486 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 break;
488 }
489
490 // Allow floating constants that are the immediate operands of casts or that
491 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000492 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
494 Operand = PE->getSubExpr();
495
496 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
497 // FIXME: Evaluate this correctly!
498 Result = (int)FL->getValue();
499 break;
500 }
501 if (Loc) *Loc = Operand->getLocStart();
502 return false;
503 }
504 case ConditionalOperatorClass: {
505 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
506
Chris Lattner590b6642007-07-15 23:26:56 +0000507 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 return false;
509
510 const Expr *TrueExp = Exp->getLHS();
511 const Expr *FalseExp = Exp->getRHS();
512 if (Result == 0) std::swap(TrueExp, FalseExp);
513
514 // Evaluate the false one first, discard the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000515 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 return false;
517 // Evalute the true one, capture the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000518 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 return false;
520 // FIXME: promotions on result.
521 break;
522 }
523 }
524
525 // Cases that are valid constant exprs fall through to here.
526 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
527 return true;
528}
529
530
531/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
532/// integer constant expression with the value zero, or if this is one that is
533/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000534bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 // Strip off a cast to void*, if it exists.
536 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
537 // Check that it is a cast to void*.
538 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
539 QualType Pointee = PT->getPointeeType();
540 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
541 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000542 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 }
544 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
545 // Accept ((void*)0) as a null pointer constant, as many other
546 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000547 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 }
549
550 // This expression must be an integer type.
551 if (!getType()->isIntegerType())
552 return false;
553
554 // If we have an integer constant expression, we need to *evaluate* it and
555 // test for the value 0.
556 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000557 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000558}