blob: 5826fab131b8c8413cb075d3cbb0019ce254107b [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000018#include "clang/Basic/TargetInfo.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000019#include "llvm/Support/Compiler.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000020using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000021using llvm::APSInt;
Anders Carlssonc44eec62008-07-03 04:20:39 +000022
Anders Carlssonc754aa62008-07-08 05:13:58 +000023#define USE_NEW_EVALUATOR 0
Anders Carlssonc44eec62008-07-03 04:20:39 +000024
Chris Lattnerf5eeb052008-07-11 18:11:29 +000025static bool CalcFakeICEVal(const Expr *Expr,
26 llvm::APSInt &Result,
27 ASTContext &Context) {
Anders Carlssonc44eec62008-07-03 04:20:39 +000028 // Calculate the value of an expression that has a calculatable
29 // value, but isn't an ICE. Currently, this only supports
30 // a very narrow set of extensions, but it can be expanded if needed.
31 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr))
32 return CalcFakeICEVal(PE->getSubExpr(), Result, Context);
33
34 if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) {
35 QualType CETy = CE->getType();
36 if ((CETy->isIntegralType() && !CETy->isBooleanType()) ||
37 CETy->isPointerType()) {
38 if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) {
39 Result.extOrTrunc(Context.getTypeSize(CETy));
40 // FIXME: This assumes pointers are signed.
41 Result.setIsSigned(CETy->isSignedIntegerType() ||
42 CETy->isPointerType());
43 return true;
44 }
45 }
46 }
47
48 if (Expr->getType()->isIntegralType())
49 return Expr->isIntegerConstantExpr(Result, Context);
50
51 return false;
52}
53
Chris Lattnerf5eeb052008-07-11 18:11:29 +000054static bool EvaluatePointer(const Expr *E, APValue &Result, ASTContext &Ctx);
55static bool EvaluateInteger(const Expr *E, APSInt &Result, ASTContext &Ctx);
56
57
58//===----------------------------------------------------------------------===//
59// Pointer Evaluation
60//===----------------------------------------------------------------------===//
61
Anders Carlssonc754aa62008-07-08 05:13:58 +000062namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +000063class VISIBILITY_HIDDEN PointerExprEvaluator
64 : public StmtVisitor<PointerExprEvaluator, APValue> {
65 ASTContext &Ctx;
Anders Carlsson2bad1682008-07-08 14:30:00 +000066public:
Anders Carlsson2bad1682008-07-08 14:30:00 +000067
Chris Lattnerf5eeb052008-07-11 18:11:29 +000068 PointerExprEvaluator(ASTContext &ctx) : Ctx(ctx) {}
69
Anders Carlsson2bad1682008-07-08 14:30:00 +000070 APValue VisitStmt(Stmt *S) {
71 // FIXME: Remove this when we support more expressions.
Anders Carlsson650c92f2008-07-08 15:34:11 +000072 printf("Unhandled pointer statement\n");
Anders Carlsson2bad1682008-07-08 14:30:00 +000073 S->dump();
74 return APValue();
75 }
76
77 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
78
Anders Carlsson650c92f2008-07-08 15:34:11 +000079 APValue VisitBinaryOperator(const BinaryOperator *E);
80 APValue VisitCastExpr(const CastExpr* E);
Anders Carlsson650c92f2008-07-08 15:34:11 +000081};
Chris Lattnerf5eeb052008-07-11 18:11:29 +000082} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +000083
Chris Lattnerf5eeb052008-07-11 18:11:29 +000084static bool EvaluatePointer(const Expr* E, APValue& Result, ASTContext &Ctx) {
85 if (!E->getType()->isPointerType())
86 return false;
87 Result = PointerExprEvaluator(Ctx).Visit(const_cast<Expr*>(E));
88 return Result.isLValue();
89}
90
91APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
92 if (E->getOpcode() != BinaryOperator::Add &&
93 E->getOpcode() != BinaryOperator::Sub)
94 return APValue();
95
96 const Expr *PExp = E->getLHS();
97 const Expr *IExp = E->getRHS();
98 if (IExp->getType()->isPointerType())
99 std::swap(PExp, IExp);
100
101 APValue ResultLValue;
102 if (!EvaluatePointer(PExp, ResultLValue, Ctx))
103 return APValue();
104
105 llvm::APSInt AdditionalOffset(32);
106 if (!EvaluateInteger(IExp, AdditionalOffset, Ctx))
107 return APValue();
108
109 uint64_t Offset = ResultLValue.getLValueOffset();
110 if (E->getOpcode() == BinaryOperator::Add)
111 Offset += AdditionalOffset.getZExtValue();
112 else
113 Offset -= AdditionalOffset.getZExtValue();
114
115 return APValue(ResultLValue.getLValueBase(), Offset);
116}
117
118
Chris Lattnerb542afe2008-07-11 19:10:17 +0000119APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000120 const Expr* SubExpr = E->getSubExpr();
121
122 // Check for pointer->pointer cast
123 if (SubExpr->getType()->isPointerType()) {
124 APValue Result;
125 if (EvaluatePointer(SubExpr, Result, Ctx))
126 return Result;
127 return APValue();
128 }
129
130 if (SubExpr->getType()->isArithmeticType()) {
131 llvm::APSInt Result(32);
132 if (EvaluateInteger(SubExpr, Result, Ctx)) {
133 Result.extOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(E->getType())));
134 return APValue(0, Result.getZExtValue());
135 }
136 }
137
138 assert(0 && "Unhandled cast");
139 return APValue();
140}
141
142
143//===----------------------------------------------------------------------===//
144// Integer Evaluation
145//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000146
147namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000148class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000149 : public StmtVisitor<IntExprEvaluator, bool> {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000150 ASTContext &Ctx;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000151 APSInt &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000152public:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000153 IntExprEvaluator(ASTContext &ctx, APSInt &result) : Ctx(ctx), Result(result){}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000154
Anders Carlssonc754aa62008-07-08 05:13:58 +0000155 //===--------------------------------------------------------------------===//
156 // Visitor Methods
157 //===--------------------------------------------------------------------===//
Chris Lattnerb542afe2008-07-11 19:10:17 +0000158 bool VisitStmt(Stmt *S) {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000159 // FIXME: Remove this when we support more expressions.
Anders Carlsson650c92f2008-07-08 15:34:11 +0000160 printf("unhandled int expression");
Anders Carlssonc754aa62008-07-08 05:13:58 +0000161 S->dump();
Chris Lattnerb542afe2008-07-11 19:10:17 +0000162 return false;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000163 }
164
Chris Lattnerb542afe2008-07-11 19:10:17 +0000165 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000166
Chris Lattnerb542afe2008-07-11 19:10:17 +0000167 bool VisitBinaryOperator(const BinaryOperator *E);
168 bool VisitUnaryOperator(const UnaryOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000169
Chris Lattnerb542afe2008-07-11 19:10:17 +0000170 bool HandleCast(const Expr* SubExpr, QualType DestType);
171 bool VisitCastExpr(const CastExpr* E) {
Anders Carlsson650c92f2008-07-08 15:34:11 +0000172 return HandleCast(E->getSubExpr(), E->getType());
173 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000174 bool VisitImplicitCastExpr(const ImplicitCastExpr* E) {
Anders Carlsson650c92f2008-07-08 15:34:11 +0000175 return HandleCast(E->getSubExpr(), E->getType());
176 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000177 bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E);
Anders Carlsson650c92f2008-07-08 15:34:11 +0000178
Chris Lattnerb542afe2008-07-11 19:10:17 +0000179 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Anders Carlsson650c92f2008-07-08 15:34:11 +0000180 Result = E->getValue();
Chris Lattnerb542afe2008-07-11 19:10:17 +0000181 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000182 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000183};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000184} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000185
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000186static bool EvaluateInteger(const Expr* E, APSInt &Result, ASTContext &Ctx) {
Chris Lattnerb542afe2008-07-11 19:10:17 +0000187 return IntExprEvaluator(Ctx, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson650c92f2008-07-08 15:34:11 +0000188}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000189
Anders Carlsson650c92f2008-07-08 15:34:11 +0000190
Chris Lattnerb542afe2008-07-11 19:10:17 +0000191bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000192 // The LHS of a constant expr is always evaluated and needed.
Chris Lattnerb542afe2008-07-11 19:10:17 +0000193 if (!Visit(E->getLHS()))
194 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000195
196 llvm::APSInt RHS(32);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000197 if (!EvaluateInteger(E->getRHS(), RHS, Ctx))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000198 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000199
200 switch (E->getOpcode()) {
201 default:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000202 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000203 case BinaryOperator::Mul:
204 Result *= RHS;
205 break;
206 case BinaryOperator::Div:
207 if (RHS == 0)
Chris Lattnerb542afe2008-07-11 19:10:17 +0000208 return false;
209 Result /= RHS;
210 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000211 case BinaryOperator::Rem:
212 if (RHS == 0)
Chris Lattnerb542afe2008-07-11 19:10:17 +0000213 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000214 Result %= RHS;
215 break;
216 case BinaryOperator::Add: Result += RHS; break;
217 case BinaryOperator::Sub: Result -= RHS; break;
218 case BinaryOperator::Shl:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000219 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000220 break;
221 case BinaryOperator::Shr:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000222 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000223 break;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000224
225 // FIXME: Need to set the result width?
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000226 case BinaryOperator::LT: Result = Result < RHS; break;
227 case BinaryOperator::GT: Result = Result > RHS; break;
228 case BinaryOperator::LE: Result = Result <= RHS; break;
229 case BinaryOperator::GE: Result = Result >= RHS; break;
230 case BinaryOperator::EQ: Result = Result == RHS; break;
231 case BinaryOperator::NE: Result = Result != RHS; break;
232 case BinaryOperator::And: Result &= RHS; break;
233 case BinaryOperator::Xor: Result ^= RHS; break;
234 case BinaryOperator::Or: Result |= RHS; break;
Anders Carlsson06a36752008-07-08 05:49:43 +0000235
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000236 case BinaryOperator::Comma:
237 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
238 // *except* when they are contained within a subexpression that is not
239 // evaluated". Note that Assignment can never happen due to constraints
240 // on the LHS subexpr, so we don't need to check it here.
241 // FIXME: Need to come up with an efficient way to deal with the C99
242 // rules on evaluation while still evaluating this. Maybe a
243 // "evaluated comma" out parameter?
Chris Lattnerb542afe2008-07-11 19:10:17 +0000244 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000245 }
246
247 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000248 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000249}
250
Chris Lattnerb542afe2008-07-11 19:10:17 +0000251bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000252 if (E->isOffsetOfOp())
253 Result = E->evaluateOffsetOf(Ctx);
254 else if (E->isSizeOfAlignOfOp()) {
255 // Return the result in the right width.
256 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(E->getType())));
257
258 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
259 if (E->getSubExpr()->getType()->isVoidType())
260 Result = 1;
261
262 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
263 if (!E->getSubExpr()->getType()->isConstantSizeType()) {
264 // FIXME: Should we attempt to evaluate this?
Chris Lattnerb542afe2008-07-11 19:10:17 +0000265 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000266 }
267
268 // Get information about the size or align.
269 if (E->getSubExpr()->getType()->isFunctionType()) {
270 // GCC extension: sizeof(function) = 1.
271 // FIXME: AlignOf shouldn't be unconditionally 4!
272 Result = E->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
273 } else {
274 unsigned CharSize = Ctx.Target.getCharWidth();
275 if (E->getOpcode() == UnaryOperator::AlignOf)
276 Result = Ctx.getTypeAlign(E->getSubExpr()->getType()) / CharSize;
277 else
278 Result = Ctx.getTypeSize(E->getSubExpr()->getType()) / CharSize;
279 }
280 } else {
281 // Get the operand value. If this is sizeof/alignof, do not evalute the
282 // operand. This affects C99 6.6p3.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000283 if (!EvaluateInteger(E->getSubExpr(), Result, Ctx))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000284 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000285
Anders Carlsson06a36752008-07-08 05:49:43 +0000286 switch (E->getOpcode()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000287 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
288 // See C99 6.6p3.
Anders Carlsson06a36752008-07-08 05:49:43 +0000289 default:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000290 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000291 case UnaryOperator::Extension:
292 assert(0 && "Handle UnaryOperator::Extension");
Chris Lattnerb542afe2008-07-11 19:10:17 +0000293 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000294 case UnaryOperator::LNot: {
295 bool Val = Result == 0;
296 uint32_t typeSize = Ctx.getTypeSize(E->getType());
297 Result.zextOrTrunc(typeSize);
298 Result = Val;
Anders Carlsson06a36752008-07-08 05:49:43 +0000299 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000300 }
301 case UnaryOperator::Plus:
Anders Carlsson06a36752008-07-08 05:49:43 +0000302 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000303 case UnaryOperator::Minus:
304 Result = -Result;
Anders Carlsson06a36752008-07-08 05:49:43 +0000305 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000306 case UnaryOperator::Not:
307 Result = ~Result;
Anders Carlsson06a36752008-07-08 05:49:43 +0000308 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000309 }
310 }
311
312 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000313 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000314}
315
Chris Lattnerb542afe2008-07-11 19:10:17 +0000316bool IntExprEvaluator::HandleCast(const Expr* SubExpr, QualType DestType) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000317 llvm::APSInt Result(32);
318
319 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(DestType));
320
321 // Handle simple integer->integer casts.
322 if (SubExpr->getType()->isIntegerType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000323 if (!EvaluateInteger(SubExpr, Result, Ctx))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000324 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000325
326 // Figure out if this is a truncate, extend or noop cast.
327 // If the input is signed, do a sign extend, noop, or truncate.
328 if (DestType->isBooleanType()) {
329 // Conversion to bool compares against zero.
330 Result = Result != 0;
331 Result.zextOrTrunc(DestWidth);
Anders Carlsson06a36752008-07-08 05:49:43 +0000332 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000333 else
334 Result.extOrTrunc(DestWidth);
335 } else if (SubExpr->getType()->isPointerType()) {
336 APValue LV;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000337 if (!EvaluatePointer(SubExpr, LV, Ctx))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000338 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000339 if (LV.getLValueBase())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000340 return false;
Anders Carlsson06a36752008-07-08 05:49:43 +0000341
Anders Carlsson559e56b2008-07-08 16:49:00 +0000342 Result.extOrTrunc(DestWidth);
343 Result = LV.getLValueOffset();
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000344 } else {
345 assert(0 && "Unhandled cast!");
Anders Carlsson2bad1682008-07-08 14:30:00 +0000346 }
347
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000348 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000349 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000350}
Anders Carlsson2bad1682008-07-08 14:30:00 +0000351
Chris Lattnerb542afe2008-07-11 19:10:17 +0000352bool IntExprEvaluator::
353VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
Anders Carlsson650c92f2008-07-08 15:34:11 +0000354 // Return the result in the right width.
355 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(E->getType())));
356
357 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
358 if (E->getArgumentType()->isVoidType()) {
359 Result = 1;
360 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000361 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000362 }
363
364 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
365 if (E->isSizeOf() && !E->getArgumentType()->isConstantSizeType())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000366 return false;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000367
368 // Get information about the size or align.
369 if (E->getArgumentType()->isFunctionType()) {
370 // GCC extension: sizeof(function) = 1.
371 Result = E->isSizeOf() ? 1 : 4;
372 } else {
373 unsigned CharSize = Ctx.Target.getCharWidth();
374 if (E->isSizeOf())
375 Result = Ctx.getTypeSize(E->getArgumentType()) / CharSize;
376 else
377 Result = Ctx.getTypeAlign(E->getArgumentType()) / CharSize;
378 }
379
380 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000381 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000382}
383
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000384//===----------------------------------------------------------------------===//
385// Top level TryEvaluate.
386//===----------------------------------------------------------------------===//
387
Chris Lattnerb542afe2008-07-11 19:10:17 +0000388bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000389#if USE_NEW_EVALUATOR
Anders Carlsson06a36752008-07-08 05:49:43 +0000390 if (getType()->isIntegerType()) {
Chris Lattnerb542afe2008-07-11 19:10:17 +0000391 llvm::APSInt sInt(32);
392 if (EvaluateInteger(this, sInt, Ctx)) {
Anders Carlsson06a36752008-07-08 05:49:43 +0000393 Result = APValue(sInt);
394 return true;
395 }
396 } else
Anders Carlssonc754aa62008-07-08 05:13:58 +0000397 return false;
398
399#else
Anders Carlssonc44eec62008-07-03 04:20:39 +0000400 if (CalcFakeICEVal(this, sInt, Ctx)) {
401 Result = APValue(sInt);
402 return true;
403 }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000404#endif
Anders Carlssonc44eec62008-07-03 04:20:39 +0000405
406 return false;
407}