blob: 6ce06e9dddde3942033c0e7922011358b1ba9a20 [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"
Eli Friedman4efaa272008-11-12 09:44:48 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000022using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000023using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000024
Chris Lattner87eae5e2008-07-11 22:52:41 +000025/// EvalInfo - This is a private struct used by the evaluator to capture
26/// information about a subexpression as it is folded. It retains information
27/// about the AST context, but also maintains information about the folded
28/// expression.
29///
30/// If an expression could be evaluated, it is still possible it is not a C
31/// "integer constant expression" or constant expression. If not, this struct
32/// captures information about how and why not.
33///
34/// One bit of information passed *into* the request for constant folding
35/// indicates whether the subexpression is "evaluated" or not according to C
36/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
37/// evaluate the expression regardless of what the RHS is, but C only allows
38/// certain things in certain situations.
39struct EvalInfo {
40 ASTContext &Ctx;
41
Anders Carlsson54da0492008-11-30 16:38:33 +000042 /// EvalResult - Contains information about the evaluation.
43 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000044
45 /// ShortCircuit - will be greater than zero if the current subexpression has
46 /// will not be evaluated because it's short-circuited (according to C rules).
47 unsigned ShortCircuit;
Chris Lattner87eae5e2008-07-11 22:52:41 +000048
Anders Carlsson54da0492008-11-30 16:38:33 +000049 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000050 EvalResult(evalresult), ShortCircuit(0) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000051};
52
53
Eli Friedman4efaa272008-11-12 09:44:48 +000054static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000055static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
56static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000057static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000058static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000059static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000060
61//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000062// Misc utilities
63//===----------------------------------------------------------------------===//
64
65static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
66 if (E->getType()->isIntegralType()) {
67 APSInt IntResult;
68 if (!EvaluateInteger(E, IntResult, Info))
69 return false;
70 Result = IntResult != 0;
71 return true;
72 } else if (E->getType()->isRealFloatingType()) {
73 APFloat FloatResult(0.0);
74 if (!EvaluateFloat(E, FloatResult, Info))
75 return false;
76 Result = !FloatResult.isZero();
77 return true;
78 } else if (E->getType()->isPointerType()) {
79 APValue PointerResult;
80 if (!EvaluatePointer(E, PointerResult, Info))
81 return false;
82 // FIXME: Is this accurate for all kinds of bases? If not, what would
83 // the check look like?
84 Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
85 return true;
86 }
Eli Friedman2217c872009-02-22 11:46:18 +000087 // FIXME: Handle pointer-like types, complex types
Eli Friedman4efaa272008-11-12 09:44:48 +000088
89 return false;
90}
91
Daniel Dunbara2cfd342009-01-29 06:16:07 +000092static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
93 APFloat &Value, ASTContext &Ctx) {
94 unsigned DestWidth = Ctx.getIntWidth(DestType);
95 // Determine whether we are converting to unsigned or signed.
96 bool DestSigned = DestType->isSignedIntegerType();
97
98 // FIXME: Warning for overflow.
99 uint64_t Space[4];
100 bool ignored;
101 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
102 llvm::APFloat::rmTowardZero, &ignored);
103 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
104}
105
106static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
107 APFloat &Value, ASTContext &Ctx) {
108 bool ignored;
109 APFloat Result = Value;
110 Result.convert(Ctx.getFloatTypeSemantics(DestType),
111 APFloat::rmNearestTiesToEven, &ignored);
112 return Result;
113}
114
115static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
116 APSInt &Value, ASTContext &Ctx) {
117 unsigned DestWidth = Ctx.getIntWidth(DestType);
118 APSInt Result = Value;
119 // Figure out if this is a truncate, extend or noop cast.
120 // If the input is signed, do a sign extend, noop, or truncate.
121 Result.extOrTrunc(DestWidth);
122 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
123 return Result;
124}
125
126static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
127 APSInt &Value, ASTContext &Ctx) {
128
129 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
130 Result.convertFromAPInt(Value, Value.isSigned(),
131 APFloat::rmNearestTiesToEven);
132 return Result;
133}
134
Eli Friedman4efaa272008-11-12 09:44:48 +0000135//===----------------------------------------------------------------------===//
136// LValue Evaluation
137//===----------------------------------------------------------------------===//
138namespace {
139class VISIBILITY_HIDDEN LValueExprEvaluator
140 : public StmtVisitor<LValueExprEvaluator, APValue> {
141 EvalInfo &Info;
142public:
143
144 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
145
146 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000147 return APValue();
148 }
149
150 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000151 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000152 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
153 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
154 APValue VisitMemberExpr(MemberExpr *E);
155 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000156 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000157 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +0000158 // FIXME: Missing: __extension__, __real__, __imag__, __builtin_choose_expr
Eli Friedman4efaa272008-11-12 09:44:48 +0000159};
160} // end anonymous namespace
161
162static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
163 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
164 return Result.isLValue();
165}
166
Anders Carlsson35873c42008-11-24 04:41:22 +0000167APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
168{
169 if (!E->hasGlobalStorage())
170 return APValue();
171
172 return APValue(E, 0);
173}
174
Eli Friedman4efaa272008-11-12 09:44:48 +0000175APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
176 if (E->isFileScope())
177 return APValue(E, 0);
178 return APValue();
179}
180
181APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
182 APValue result;
183 QualType Ty;
184 if (E->isArrow()) {
185 if (!EvaluatePointer(E->getBase(), result, Info))
186 return APValue();
187 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
188 } else {
189 result = Visit(E->getBase());
190 if (result.isUninit())
191 return APValue();
192 Ty = E->getBase()->getType();
193 }
194
195 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
196 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000197
198 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
199 if (!FD) // FIXME: deal with other kinds of member expressions
200 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000201
202 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000203 unsigned i = 0;
204 for (RecordDecl::field_iterator Field = RD->field_begin(),
205 FieldEnd = RD->field_end();
206 Field != FieldEnd; (void)++Field, ++i) {
207 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000208 break;
209 }
210
211 result.setLValue(result.getLValueBase(),
212 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
213
214 return result;
215}
216
Anders Carlsson3068d112008-11-16 19:01:22 +0000217APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
218{
219 APValue Result;
220
221 if (!EvaluatePointer(E->getBase(), Result, Info))
222 return APValue();
223
224 APSInt Index;
225 if (!EvaluateInteger(E->getIdx(), Index, Info))
226 return APValue();
227
228 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
229
230 uint64_t Offset = Index.getSExtValue() * ElementSize;
231 Result.setLValue(Result.getLValueBase(),
232 Result.getLValueOffset() + Offset);
233 return Result;
234}
Eli Friedman4efaa272008-11-12 09:44:48 +0000235
Eli Friedmane8761c82009-02-20 01:57:15 +0000236APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E)
237{
238 APValue Result;
239 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
240 return APValue();
241 return Result;
242}
243
Eli Friedman4efaa272008-11-12 09:44:48 +0000244//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000245// Pointer Evaluation
246//===----------------------------------------------------------------------===//
247
Anders Carlssonc754aa62008-07-08 05:13:58 +0000248namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000249class VISIBILITY_HIDDEN PointerExprEvaluator
250 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000251 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000252public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000253
Chris Lattner87eae5e2008-07-11 22:52:41 +0000254 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000255
Anders Carlsson2bad1682008-07-08 14:30:00 +0000256 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000257 return APValue();
258 }
259
260 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
261
Anders Carlsson650c92f2008-07-08 15:34:11 +0000262 APValue VisitBinaryOperator(const BinaryOperator *E);
263 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000264 APValue VisitUnaryExtension(const UnaryOperator *E)
265 { return Visit(E->getSubExpr()); }
266 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000267 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
268 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000269 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
270 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000271 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000272 APValue VisitBlockExpr(BlockExpr *E) {
273 if (!E->hasBlockDeclRefExprs())
274 return APValue(E, 0);
275 return APValue();
276 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000277 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +0000278 // FIXME: Missing: __builtin_choose_expr, ImplicitValueInitExpr, comma,
279 // @encode, @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000280};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000281} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000282
Eli Friedman4bdf0872009-02-22 04:02:33 +0000283static bool HasPointerEvalType(const Expr* E) {
284 return E->getType()->isPointerType()
285 || E->getType()->isBlockPointerType()
286 || E->getType()->isObjCQualifiedIdType()
287 || E->getType()->isObjCQualifiedClassType();
288}
289
Chris Lattner87eae5e2008-07-11 22:52:41 +0000290static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Eli Friedman4bdf0872009-02-22 04:02:33 +0000291 if (!HasPointerEvalType(E))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000292 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000293 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000294 return Result.isLValue();
295}
296
297APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
298 if (E->getOpcode() != BinaryOperator::Add &&
299 E->getOpcode() != BinaryOperator::Sub)
300 return APValue();
301
302 const Expr *PExp = E->getLHS();
303 const Expr *IExp = E->getRHS();
304 if (IExp->getType()->isPointerType())
305 std::swap(PExp, IExp);
306
307 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000308 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000309 return APValue();
310
311 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000312 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000313 return APValue();
314
Eli Friedman4efaa272008-11-12 09:44:48 +0000315 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000316 uint64_t SizeOfPointee;
317
318 // Explicitly handle GNU void* and function pointer arithmetic extensions.
319 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
320 SizeOfPointee = 1;
321 else
322 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000323
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000324 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000325
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000326 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000327 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000328 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000329 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
330
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000331 return APValue(ResultLValue.getLValueBase(), Offset);
332}
Eli Friedman4efaa272008-11-12 09:44:48 +0000333
Eli Friedman2217c872009-02-22 11:46:18 +0000334APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
335 APValue result;
336 if (EvaluateLValue(E->getSubExpr(), result, Info))
337 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000338 return APValue();
339}
Anders Carlssond407a762008-12-05 05:24:13 +0000340
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341
Chris Lattnerb542afe2008-07-11 19:10:17 +0000342APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000343 const Expr* SubExpr = E->getSubExpr();
344
345 // Check for pointer->pointer cast
346 if (SubExpr->getType()->isPointerType()) {
347 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000348 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000349 return Result;
350 return APValue();
351 }
352
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000353 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000354 APValue Result;
355 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
356 return APValue();
357
358 if (Result.isInt()) {
359 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
360 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000361 }
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000362
363 // Cast is of an lvalue, no need to change value.
364 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000365 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000366
367 if (SubExpr->getType()->isFunctionType() ||
368 SubExpr->getType()->isArrayType()) {
369 APValue Result;
370 if (EvaluateLValue(SubExpr, Result, Info))
371 return Result;
372 return APValue();
373 }
374
375 //assert(0 && "Unhandled cast");
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000376 return APValue();
377}
378
Eli Friedman3941b182009-01-25 01:54:01 +0000379APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000380 if (E->isBuiltinCall(Info.Ctx) ==
381 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000382 return APValue(E, 0);
383 return APValue();
384}
385
Eli Friedman4efaa272008-11-12 09:44:48 +0000386APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
387 bool BoolResult;
388 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
389 return APValue();
390
391 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
392
393 APValue Result;
394 if (EvaluatePointer(EvalExpr, Result, Info))
395 return Result;
396 return APValue();
397}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000398
399//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000400// Vector Evaluation
401//===----------------------------------------------------------------------===//
402
403namespace {
404 class VISIBILITY_HIDDEN VectorExprEvaluator
405 : public StmtVisitor<VectorExprEvaluator, APValue> {
406 EvalInfo &Info;
407 public:
408
409 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
410
411 APValue VisitStmt(Stmt *S) {
412 return APValue();
413 }
414
415 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
416 APValue VisitCastExpr(const CastExpr* E);
417 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
418 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +0000419 // FIXME: Missing: __builtin_choose_expr, ImplicitValueInitExpr,
420 // __extension__, unary +/-, unary ~,
421 // __real__/__imag__, binary add/sub/mul/div,
422 // binary comparisons, binary and/or/xor,
423 // conditional ?:, shufflevector, ExtVectorElementExpr
424 // (Note that some of these would require acutually implementing
425 // conversions between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000426 };
427} // end anonymous namespace
428
429static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
430 if (!E->getType()->isVectorType())
431 return false;
432 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
433 return !Result.isUninit();
434}
435
436APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
437 const Expr* SE = E->getSubExpr();
438
439 // Check for vector->vector bitcast.
440 if (SE->getType()->isVectorType())
441 return this->Visit(const_cast<Expr*>(SE));
442
443 return APValue();
444}
445
446APValue
447VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
448 return this->Visit(const_cast<Expr*>(E->getInitializer()));
449}
450
451APValue
452VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
453 const VectorType *VT = E->getType()->getAsVectorType();
454 unsigned NumInits = E->getNumInits();
455
456 if (!VT || VT->getNumElements() != NumInits)
457 return APValue();
458
459 QualType EltTy = VT->getElementType();
460 llvm::SmallVector<APValue, 4> Elements;
461
462 for (unsigned i = 0; i < NumInits; i++) {
463 if (EltTy->isIntegerType()) {
464 llvm::APSInt sInt(32);
465 if (!EvaluateInteger(E->getInit(i), sInt, Info))
466 return APValue();
467 Elements.push_back(APValue(sInt));
468 } else {
469 llvm::APFloat f(0.0);
470 if (!EvaluateFloat(E->getInit(i), f, Info))
471 return APValue();
472 Elements.push_back(APValue(f));
473 }
474 }
475 return APValue(&Elements[0], Elements.size());
476}
477
478//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000479// Integer Evaluation
480//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000481
482namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000483class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000484 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000485 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000486 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000487public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000488 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000489 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000490
Anders Carlsson82206e22008-11-30 18:14:57 +0000491 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlsson54da0492008-11-30 16:38:33 +0000492 Info.EvalResult.DiagLoc = L;
493 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000494 Info.EvalResult.DiagExpr = E;
Chris Lattner54176fd2008-07-12 00:14:42 +0000495 return true; // still a constant.
496 }
Daniel Dunbar131eb432009-02-19 09:06:44 +0000497
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000498 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000499 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000500 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000501 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000502 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000503 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000504 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000505 return true;
506 }
507
Daniel Dunbar131eb432009-02-19 09:06:44 +0000508 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000509 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000510 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000511 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000512 Result = APValue(APSInt(I));
513 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000514 return true;
515 }
516
517 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000518 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000519 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000520 return true;
521 }
522
Anders Carlsson82206e22008-11-30 18:14:57 +0000523 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000524 // If this is in an unevaluated portion of the subexpression, ignore the
525 // error.
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000526 if (Info.ShortCircuit) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000527 // If error is ignored because the value isn't evaluated, get the real
528 // type at least to prevent errors downstream.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000529 return Success(0, E);
Chris Lattner32fea9d2008-11-12 07:43:42 +0000530 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000531
Chris Lattner32fea9d2008-11-12 07:43:42 +0000532 // Take the first error.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000533
534 // FIXME: This is wrong if we happen to have already emitted an
535 // extension diagnostic; in that case we should report this error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000536 if (Info.EvalResult.Diag == 0) {
537 Info.EvalResult.DiagLoc = L;
538 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000539 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000540 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000541 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000542 }
543
Anders Carlssonc754aa62008-07-08 05:13:58 +0000544 //===--------------------------------------------------------------------===//
545 // Visitor Methods
546 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000547
548 bool VisitStmt(Stmt *) {
549 assert(0 && "This should be called on integers, stmts are not integers");
550 return false;
551 }
Chris Lattner7a767782008-07-11 19:24:49 +0000552
Chris Lattner32fea9d2008-11-12 07:43:42 +0000553 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000554 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000555 }
556
Chris Lattnerb542afe2008-07-11 19:10:17 +0000557 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000558
Chris Lattner4c4867e2008-07-12 00:38:25 +0000559 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000560 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000561 }
562 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000563 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000564 }
565 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000566 // Per gcc docs "this built-in function ignores top level
567 // qualifiers". We need to use the canonical version to properly
568 // be able to strip CRV qualifiers from the type.
569 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
570 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000571 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
572 T1.getUnqualifiedType()),
573 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000574 }
575 bool VisitDeclRefExpr(const DeclRefExpr *E);
576 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000577 bool VisitBinaryOperator(const BinaryOperator *E);
578 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000579 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000580
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000581 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000582 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
583
Anders Carlsson3068d112008-11-16 19:01:22 +0000584 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000585 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000586 }
587
Anders Carlsson3f704562008-12-21 22:39:40 +0000588 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000589 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000590 }
591
Anders Carlsson3068d112008-11-16 19:01:22 +0000592 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000593 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000594 }
595
Sebastian Redl64b45f72009-01-05 20:52:13 +0000596 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000597 return Success(E->EvaluateTrait(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000598 }
599
Chris Lattnerfcee0012008-07-11 21:24:13 +0000600private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000601 unsigned GetAlignOfExpr(const Expr *E);
602 unsigned GetAlignOfType(QualType T);
Eli Friedman2217c872009-02-22 11:46:18 +0000603 // FIXME: Missing: __real__/__imag__, array subscript of vector,
604 // member of vector, __builtin_choose_expr,
605 // ImplicitValueInitExpr
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000606};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000607} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000608
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000609static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000610 if (!E->getType()->isIntegralType())
611 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000612
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000613 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
614}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000615
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000616static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
617 APValue Val;
618 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
619 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000620 Result = Val.getInt();
621 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000622}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000623
Chris Lattner4c4867e2008-07-12 00:38:25 +0000624bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
625 // Enums are integer constant exprs.
626 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000627 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000628 // signedness consistently; see PR3173.
629 APSInt SI = D->getInitVal();
630 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
631 // FIXME: This is an ugly hack around the fact that enums don't
632 // set their width (!?!) consistently; see PR3173.
633 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
634 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000635 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000636
637 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
638 if (Info.Ctx.getLangOptions().CPlusPlus &&
639 E->getType().getCVRQualifiers() == QualType::Const) {
640 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
641 if (const Expr *Init = D->getInit())
642 return Visit(const_cast<Expr*>(Init));
643 }
644 }
645
Chris Lattner4c4867e2008-07-12 00:38:25 +0000646 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000647 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000648}
649
Chris Lattnera4d55d82008-10-06 06:40:35 +0000650/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
651/// as GCC.
652static int EvaluateBuiltinClassifyType(const CallExpr *E) {
653 // The following enum mimics the values returned by GCC.
654 enum gcc_type_class {
655 no_type_class = -1,
656 void_type_class, integer_type_class, char_type_class,
657 enumeral_type_class, boolean_type_class,
658 pointer_type_class, reference_type_class, offset_type_class,
659 real_type_class, complex_type_class,
660 function_type_class, method_type_class,
661 record_type_class, union_type_class,
662 array_type_class, string_type_class,
663 lang_type_class
664 };
665
666 // If no argument was supplied, default to "no_type_class". This isn't
667 // ideal, however it is what gcc does.
668 if (E->getNumArgs() == 0)
669 return no_type_class;
670
671 QualType ArgTy = E->getArg(0)->getType();
672 if (ArgTy->isVoidType())
673 return void_type_class;
674 else if (ArgTy->isEnumeralType())
675 return enumeral_type_class;
676 else if (ArgTy->isBooleanType())
677 return boolean_type_class;
678 else if (ArgTy->isCharType())
679 return string_type_class; // gcc doesn't appear to use char_type_class
680 else if (ArgTy->isIntegerType())
681 return integer_type_class;
682 else if (ArgTy->isPointerType())
683 return pointer_type_class;
684 else if (ArgTy->isReferenceType())
685 return reference_type_class;
686 else if (ArgTy->isRealType())
687 return real_type_class;
688 else if (ArgTy->isComplexType())
689 return complex_type_class;
690 else if (ArgTy->isFunctionType())
691 return function_type_class;
692 else if (ArgTy->isStructureType())
693 return record_type_class;
694 else if (ArgTy->isUnionType())
695 return union_type_class;
696 else if (ArgTy->isArrayType())
697 return array_type_class;
698 else if (ArgTy->isUnionType())
699 return union_type_class;
700 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
701 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
702 return -1;
703}
704
Chris Lattner4c4867e2008-07-12 00:38:25 +0000705bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000706 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000707 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000708 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000709 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000710 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000711
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000712 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000713 // __builtin_constant_p always has one operand: it returns true if that
714 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000715 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000716 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000717}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000718
Chris Lattnerb542afe2008-07-11 19:10:17 +0000719bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000720 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000721 if (!Visit(E->getRHS()))
722 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000723
724 if (!Info.ShortCircuit) {
725 // If we can't evaluate the LHS, it must be because it has
726 // side effects.
727 if (!E->getLHS()->isEvaluatable(Info.Ctx))
728 Info.EvalResult.HasSideEffects = true;
729
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000730 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000731 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000732
Anders Carlsson027f62e2008-12-01 02:07:06 +0000733 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000734 }
735
736 if (E->isLogicalOp()) {
737 // These need to be handled specially because the operands aren't
738 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000739 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000740
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000741 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000742 // We were able to evaluate the LHS, see if we can get away with not
743 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000744 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
745 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000746 Info.ShortCircuit++;
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000747 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000748 Info.ShortCircuit--;
749
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000750 // FIXME: Return an extension warning saying that the RHS could not be
751 // evaluated.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000752 // if (!rhsEvaluated) ...
753 (void) rhsEvaluated;
754
755 return Success(lhsResult, E);
Eli Friedmana6afa762008-11-13 06:09:17 +0000756 }
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000757
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000758 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000759 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000760 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000761 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000762 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000763 }
764 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000765 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000766 // We can't evaluate the LHS; however, sometimes the result
767 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000768 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
769 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000770 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000771 // must have had side effects.
772 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000773
774 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000775 }
776 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000777 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000778
Eli Friedmana6afa762008-11-13 06:09:17 +0000779 return false;
780 }
781
Anders Carlsson286f85e2008-11-16 07:17:21 +0000782 QualType LHSTy = E->getLHS()->getType();
783 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000784
785 if (LHSTy->isAnyComplexType()) {
786 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
787 APValue LHS, RHS;
788
789 if (!EvaluateComplex(E->getLHS(), LHS, Info))
790 return false;
791
792 if (!EvaluateComplex(E->getRHS(), RHS, Info))
793 return false;
794
795 if (LHS.isComplexFloat()) {
796 APFloat::cmpResult CR_r =
797 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
798 APFloat::cmpResult CR_i =
799 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
800
Daniel Dunbar4087e242009-01-29 06:43:41 +0000801 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000802 return Success((CR_r == APFloat::cmpEqual &&
803 CR_i == APFloat::cmpEqual), E);
804 else {
805 assert(E->getOpcode() == BinaryOperator::NE &&
806 "Invalid complex comparison.");
807 return Success(((CR_r == APFloat::cmpGreaterThan ||
808 CR_r == APFloat::cmpLessThan) &&
809 (CR_i == APFloat::cmpGreaterThan ||
810 CR_i == APFloat::cmpLessThan)), E);
811 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000812 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000813 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000814 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
815 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
816 else {
817 assert(E->getOpcode() == BinaryOperator::NE &&
818 "Invalid compex comparison.");
819 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
820 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
821 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000822 }
823 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000824
825 if (LHSTy->isRealFloatingType() &&
826 RHSTy->isRealFloatingType()) {
827 APFloat RHS(0.0), LHS(0.0);
828
829 if (!EvaluateFloat(E->getRHS(), RHS, Info))
830 return false;
831
832 if (!EvaluateFloat(E->getLHS(), LHS, Info))
833 return false;
834
835 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000836
Anders Carlsson286f85e2008-11-16 07:17:21 +0000837 switch (E->getOpcode()) {
838 default:
839 assert(0 && "Invalid binary operator!");
840 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000841 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000842 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000843 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000844 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000845 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000846 case BinaryOperator::GE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000847 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
848 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000849 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000850 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000851 case BinaryOperator::NE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000852 return Success(CR == APFloat::cmpGreaterThan
853 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000854 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000855 }
856
Anders Carlsson3068d112008-11-16 19:01:22 +0000857 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000858 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000859 APValue LHSValue;
860 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
861 return false;
862
863 APValue RHSValue;
864 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
865 return false;
866
867 // FIXME: Is this correct? What if only one of the operands has a base?
868 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
869 return false;
870
871 const QualType Type = E->getLHS()->getType();
872 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
873
874 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
875 D /= Info.Ctx.getTypeSize(ElementType) / 8;
876
Daniel Dunbar131eb432009-02-19 09:06:44 +0000877 return Success(D, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000878 }
879 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000880 if (!LHSTy->isIntegralType() ||
881 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000882 // We can't continue from here for non-integral types, and they
883 // could potentially confuse the following operations.
884 // FIXME: Deal with EQ and friends.
885 return false;
886 }
887
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000888 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000889 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +0000890 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000891
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000892 // Only support arithmetic on integers for now.
893 if (!Result.isInt())
894 return false;
895
Daniel Dunbar131eb432009-02-19 09:06:44 +0000896 llvm::APSInt RHS;
Chris Lattner54176fd2008-07-12 00:14:42 +0000897 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000898 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000899
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000900 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000901 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000902 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000903 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
904 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
905 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
906 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
907 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
908 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000909 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000910 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000911 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000912 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000913 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000914 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000915 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000916 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000917 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +0000918 // FIXME: Warn about out of range shift amounts!
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000919 unsigned SA =
920 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
921 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000922 }
923 case BinaryOperator::Shr: {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000924 unsigned SA =
925 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
926 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000927 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000928
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000929 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
930 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
931 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
932 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
933 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
934 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +0000935 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000936}
937
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000938bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +0000939 bool Cond;
940 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000941 return false;
942
Nuno Lopesa25bd552008-11-16 22:06:39 +0000943 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000944}
945
Chris Lattneraf707ab2009-01-24 21:53:27 +0000946unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +0000947 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
948
949 // __alignof__(void) = 1 as a gcc extension.
950 if (Ty->isVoidType())
951 return 1;
952
953 // GCC extension: alignof(function) = 4.
954 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
955 // attribute(align) directive.
956 if (Ty->isFunctionType())
957 return 4;
958
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000959 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
960 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere9feb472009-01-24 21:09:06 +0000961
962 // alignof VLA/incomplete array.
963 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
964 return GetAlignOfType(VAT->getElementType());
965
966 // sizeof (objc class)?
967 if (isa<ObjCInterfaceType>(Ty))
968 return 1; // FIXME: This probably isn't right.
969
970 // Get information about the alignment.
971 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanc5082032009-02-22 03:31:23 +0000972 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +0000973}
974
Chris Lattneraf707ab2009-01-24 21:53:27 +0000975unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
976 E = E->IgnoreParens();
977
978 // alignof decl is always accepted, even if it doesn't make sense: we default
979 // to 1 in those cases.
980 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000981 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000982
983 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000984 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000985
Chris Lattnere9feb472009-01-24 21:09:06 +0000986 return GetAlignOfType(E->getType());
987}
988
989
Sebastian Redl05189992008-11-11 17:56:53 +0000990/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
991/// expression's type.
992bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
993 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000994
Chris Lattnere9feb472009-01-24 21:09:06 +0000995 // Handle alignof separately.
996 if (!E->isSizeOf()) {
997 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +0000998 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +0000999 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001000 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001001 }
1002
Sebastian Redl05189992008-11-11 17:56:53 +00001003 QualType SrcTy = E->getTypeOfArgument();
1004
Daniel Dunbar131eb432009-02-19 09:06:44 +00001005 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1006 // extension.
1007 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1008 return Success(1, E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001009
1010 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001011 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001012 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001013
1014 if (SrcTy->isObjCInterfaceType()) {
1015 // Slightly unusual case: the size of an ObjC interface type is the
1016 // size of the class. This code intentionally falls through to the normal
1017 // case.
1018 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
1019 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
1020 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
1021 }
1022
Chris Lattnere9feb472009-01-24 21:09:06 +00001023 // Get information about the size.
Chris Lattner87eae5e2008-07-11 22:52:41 +00001024 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Daniel Dunbar131eb432009-02-19 09:06:44 +00001025 return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001026}
1027
Chris Lattnerb542afe2008-07-11 19:10:17 +00001028bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001029 // Special case unary operators that do not need their subexpression
1030 // evaluated. offsetof/sizeof/alignof are all special.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001031 if (E->isOffsetOfOp())
1032 return Success(E->evaluateOffsetOf(Info.Ctx), E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001033
1034 if (E->getOpcode() == UnaryOperator::LNot) {
1035 // LNot's operand isn't necessarily an integer, so we handle it specially.
1036 bool bres;
1037 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1038 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001039 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001040 }
1041
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001042 // Only handle integral operations...
1043 if (!E->getSubExpr()->getType()->isIntegralType())
1044 return false;
1045
Chris Lattner87eae5e2008-07-11 22:52:41 +00001046 // Get the operand value into 'Result'.
1047 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001048 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001049
Chris Lattner75a48812008-07-11 22:15:16 +00001050 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001051 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001052 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1053 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001054 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001055 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001056 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1057 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001058 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001059 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001060 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001061 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001062 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001063 if (!Result.isInt()) return false;
1064 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001065 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001066 if (!Result.isInt()) return false;
1067 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001068 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001069}
1070
Chris Lattner732b2232008-07-12 01:15:53 +00001071/// HandleCast - This is used to evaluate implicit or explicit casts where the
1072/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001073bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001074 Expr *SubExpr = E->getSubExpr();
1075 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001076 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001077
Eli Friedman4efaa272008-11-12 09:44:48 +00001078 if (DestType->isBooleanType()) {
1079 bool BoolResult;
1080 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1081 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001082 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001083 }
1084
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001085 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001086 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001087 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001088 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001089
Eli Friedmanbe265702009-02-20 01:15:07 +00001090 if (!Result.isInt()) {
1091 // Only allow casts of lvalues if they are lossless.
1092 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1093 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001094
Daniel Dunbardd211642009-02-19 22:24:01 +00001095 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001096 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001097 }
1098
1099 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001100 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001101 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001102 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001103 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001104
Daniel Dunbardd211642009-02-19 22:24:01 +00001105 if (LV.getLValueBase()) {
1106 // Only allow based lvalue casts if they are lossless.
1107 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1108 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001109
Daniel Dunbardd211642009-02-19 22:24:01 +00001110 Result = LV;
1111 return true;
1112 }
1113
1114 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1115 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001116 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001117
Eli Friedmanbe265702009-02-20 01:15:07 +00001118 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1119 // This handles double-conversion cases, where there's both
1120 // an l-value promotion and an implicit conversion to int.
1121 APValue LV;
1122 if (!EvaluateLValue(SubExpr, LV, Info))
1123 return false;
1124
1125 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1126 return false;
1127
1128 Result = LV;
1129 return true;
1130 }
1131
Eli Friedman2217c872009-02-22 11:46:18 +00001132 // FIXME: Handle complex types
1133 // FIXME: Handle vectors
1134
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001135 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001136 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001137
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001138 APFloat F(0.0);
1139 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001140 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001141
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001142 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001143}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001144
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001145//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001146// Float Evaluation
1147//===----------------------------------------------------------------------===//
1148
1149namespace {
1150class VISIBILITY_HIDDEN FloatExprEvaluator
1151 : public StmtVisitor<FloatExprEvaluator, bool> {
1152 EvalInfo &Info;
1153 APFloat &Result;
1154public:
1155 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1156 : Info(info), Result(result) {}
1157
1158 bool VisitStmt(Stmt *S) {
1159 return false;
1160 }
1161
1162 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001163 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001164
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001165 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001166 bool VisitBinaryOperator(const BinaryOperator *E);
1167 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001168 bool VisitCastExpr(CastExpr *E);
1169 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001170
1171 // FIXME: Missing: __real__/__imag__, __extension__,
1172 // array subscript of vector, member of vector,
1173 // __builtin_choose_expr, ImplicitValueInitExpr,
1174 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001175};
1176} // end anonymous namespace
1177
1178static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1179 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1180}
1181
Chris Lattner019f4e82008-10-06 05:28:25 +00001182bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001183 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001184 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001185 case Builtin::BI__builtin_huge_val:
1186 case Builtin::BI__builtin_huge_valf:
1187 case Builtin::BI__builtin_huge_vall:
1188 case Builtin::BI__builtin_inf:
1189 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001190 case Builtin::BI__builtin_infl: {
1191 const llvm::fltSemantics &Sem =
1192 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001193 Result = llvm::APFloat::getInf(Sem);
1194 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001195 }
Chris Lattner9e621712008-10-06 06:31:58 +00001196
1197 case Builtin::BI__builtin_nan:
1198 case Builtin::BI__builtin_nanf:
1199 case Builtin::BI__builtin_nanl:
1200 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1201 // can't constant fold it.
1202 if (const StringLiteral *S =
1203 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1204 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001205 const llvm::fltSemantics &Sem =
1206 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001207 Result = llvm::APFloat::getNaN(Sem);
1208 return true;
1209 }
1210 }
1211 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001212
1213 case Builtin::BI__builtin_fabs:
1214 case Builtin::BI__builtin_fabsf:
1215 case Builtin::BI__builtin_fabsl:
1216 if (!EvaluateFloat(E->getArg(0), Result, Info))
1217 return false;
1218
1219 if (Result.isNegative())
1220 Result.changeSign();
1221 return true;
1222
1223 case Builtin::BI__builtin_copysign:
1224 case Builtin::BI__builtin_copysignf:
1225 case Builtin::BI__builtin_copysignl: {
1226 APFloat RHS(0.);
1227 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1228 !EvaluateFloat(E->getArg(1), RHS, Info))
1229 return false;
1230 Result.copySign(RHS);
1231 return true;
1232 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001233 }
1234}
1235
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001236bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001237 if (E->getOpcode() == UnaryOperator::Deref)
1238 return false;
1239
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001240 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1241 return false;
1242
1243 switch (E->getOpcode()) {
1244 default: return false;
1245 case UnaryOperator::Plus:
1246 return true;
1247 case UnaryOperator::Minus:
1248 Result.changeSign();
1249 return true;
1250 }
1251}
Chris Lattner019f4e82008-10-06 05:28:25 +00001252
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001253bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1254 // FIXME: Diagnostics? I really don't understand how the warnings
1255 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001256 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001257 if (!EvaluateFloat(E->getLHS(), Result, Info))
1258 return false;
1259 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1260 return false;
1261
1262 switch (E->getOpcode()) {
1263 default: return false;
1264 case BinaryOperator::Mul:
1265 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1266 return true;
1267 case BinaryOperator::Add:
1268 Result.add(RHS, APFloat::rmNearestTiesToEven);
1269 return true;
1270 case BinaryOperator::Sub:
1271 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1272 return true;
1273 case BinaryOperator::Div:
1274 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1275 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001276 }
1277}
1278
1279bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1280 Result = E->getValue();
1281 return true;
1282}
1283
Eli Friedman4efaa272008-11-12 09:44:48 +00001284bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1285 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001286
Eli Friedman4efaa272008-11-12 09:44:48 +00001287 if (SubExpr->getType()->isIntegralType()) {
1288 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001289 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001290 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001291 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1292 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001293 return true;
1294 }
1295 if (SubExpr->getType()->isRealFloatingType()) {
1296 if (!Visit(SubExpr))
1297 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001298 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1299 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001300 return true;
1301 }
Eli Friedman2217c872009-02-22 11:46:18 +00001302 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001303
1304 return false;
1305}
1306
1307bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1308 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1309 return true;
1310}
1311
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001312//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001313// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001314//===----------------------------------------------------------------------===//
1315
1316namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001317class VISIBILITY_HIDDEN ComplexExprEvaluator
1318 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001319 EvalInfo &Info;
1320
1321public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001322 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001323
1324 //===--------------------------------------------------------------------===//
1325 // Visitor Methods
1326 //===--------------------------------------------------------------------===//
1327
1328 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001329 return APValue();
1330 }
1331
1332 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1333
1334 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001335 Expr* SubExpr = E->getSubExpr();
1336
1337 if (SubExpr->getType()->isRealFloatingType()) {
1338 APFloat Result(0.0);
1339
1340 if (!EvaluateFloat(SubExpr, Result, Info))
1341 return APValue();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001342
Daniel Dunbar3f279872009-01-29 01:32:56 +00001343 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001344 Result);
1345 } else {
1346 assert(SubExpr->getType()->isIntegerType() &&
1347 "Unexpected imaginary literal.");
1348
1349 llvm::APSInt Result;
1350 if (!EvaluateInteger(SubExpr, Result, Info))
1351 return APValue();
1352
1353 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1354 Zero = 0;
1355 return APValue(Zero, Result);
1356 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001357 }
1358
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001359 APValue VisitCastExpr(CastExpr *E) {
1360 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001361 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1362 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001363
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001364 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001365 APFloat Result(0.0);
1366
1367 if (!EvaluateFloat(SubExpr, Result, Info))
1368 return APValue();
1369
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001370 // Apply float conversion if necessary.
1371 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbar8f826f02009-01-24 19:08:01 +00001372 return APValue(Result,
Daniel Dunbar3f279872009-01-29 01:32:56 +00001373 APFloat(Result.getSemantics(), APFloat::fcZero, false));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001374 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001375 APSInt Result;
1376
1377 if (!EvaluateInteger(SubExpr, Result, Info))
1378 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001379
1380 // Apply integer conversion if necessary.
1381 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001382 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1383 Zero = 0;
1384 return APValue(Result, Zero);
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001385 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1386 APValue Src;
1387
1388 if (!EvaluateComplex(SubExpr, Src, Info))
1389 return APValue();
1390
1391 QualType SrcType = CT->getElementType();
1392
1393 if (Src.isComplexFloat()) {
1394 if (EltType->isRealFloatingType()) {
1395 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1396 Src.getComplexFloatReal(),
1397 Info.Ctx),
1398 HandleFloatToFloatCast(EltType, SrcType,
1399 Src.getComplexFloatImag(),
1400 Info.Ctx));
1401 } else {
1402 return APValue(HandleFloatToIntCast(EltType, SrcType,
1403 Src.getComplexFloatReal(),
1404 Info.Ctx),
1405 HandleFloatToIntCast(EltType, SrcType,
1406 Src.getComplexFloatImag(),
1407 Info.Ctx));
1408 }
1409 } else {
1410 assert(Src.isComplexInt() && "Invalid evaluate result.");
1411 if (EltType->isRealFloatingType()) {
1412 return APValue(HandleIntToFloatCast(EltType, SrcType,
1413 Src.getComplexIntReal(),
1414 Info.Ctx),
1415 HandleIntToFloatCast(EltType, SrcType,
1416 Src.getComplexIntImag(),
1417 Info.Ctx));
1418 } else {
1419 return APValue(HandleIntToIntCast(EltType, SrcType,
1420 Src.getComplexIntReal(),
1421 Info.Ctx),
1422 HandleIntToIntCast(EltType, SrcType,
1423 Src.getComplexIntImag(),
1424 Info.Ctx));
1425 }
1426 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001427 }
1428
1429 // FIXME: Handle more casts.
1430 return APValue();
1431 }
1432
1433 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001434 // FIXME Missing: unary +/-/~, __extension__, binary div,
1435 // __builtin_choose_expr, ImplicitValueInitExpr,
1436 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001437};
1438} // end anonymous namespace
1439
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001440static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001441{
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001442 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1443 assert((!Result.isComplexFloat() ||
1444 (&Result.getComplexFloatReal().getSemantics() ==
1445 &Result.getComplexFloatImag().getSemantics())) &&
1446 "Invalid complex evaluation.");
1447 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001448}
1449
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001450APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001451{
1452 APValue Result, RHS;
1453
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001454 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001455 return APValue();
1456
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001457 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001458 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001459
Daniel Dunbar3f279872009-01-29 01:32:56 +00001460 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1461 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001462 switch (E->getOpcode()) {
1463 default: return APValue();
1464 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001465 if (Result.isComplexFloat()) {
1466 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1467 APFloat::rmNearestTiesToEven);
1468 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1469 APFloat::rmNearestTiesToEven);
1470 } else {
1471 Result.getComplexIntReal() += RHS.getComplexIntReal();
1472 Result.getComplexIntImag() += RHS.getComplexIntImag();
1473 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001474 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001475 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001476 if (Result.isComplexFloat()) {
1477 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1478 APFloat::rmNearestTiesToEven);
1479 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1480 APFloat::rmNearestTiesToEven);
1481 } else {
1482 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1483 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1484 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001485 break;
1486 case BinaryOperator::Mul:
1487 if (Result.isComplexFloat()) {
1488 APValue LHS = Result;
1489 APFloat &LHS_r = LHS.getComplexFloatReal();
1490 APFloat &LHS_i = LHS.getComplexFloatImag();
1491 APFloat &RHS_r = RHS.getComplexFloatReal();
1492 APFloat &RHS_i = RHS.getComplexFloatImag();
1493
1494 APFloat Tmp = LHS_r;
1495 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1496 Result.getComplexFloatReal() = Tmp;
1497 Tmp = LHS_i;
1498 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1499 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1500
1501 Tmp = LHS_r;
1502 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1503 Result.getComplexFloatImag() = Tmp;
1504 Tmp = LHS_i;
1505 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1506 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1507 } else {
1508 APValue LHS = Result;
1509 Result.getComplexIntReal() =
1510 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1511 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1512 Result.getComplexIntImag() =
1513 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1514 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1515 }
1516 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001517 }
1518
1519 return Result;
1520}
1521
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001522//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001523// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001524//===----------------------------------------------------------------------===//
1525
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001526/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001527/// any crazy technique (that has nothing to do with language standards) that
1528/// we want to. If this function returns true, it returns the folded constant
1529/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001530bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1531 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001532
Nate Begeman59b5da62009-01-18 03:20:47 +00001533 if (getType()->isVectorType()) {
1534 if (!EvaluateVector(this, Result.Val, Info))
1535 return false;
1536 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001537 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001538 return false;
Eli Friedman4bdf0872009-02-22 04:02:33 +00001539 } else if (HasPointerEvalType(this)) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001540 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001541 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001542 } else if (getType()->isRealFloatingType()) {
1543 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001544 if (!EvaluateFloat(this, f, Info))
1545 return false;
1546
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001547 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001548 } else if (getType()->isAnyComplexType()) {
1549 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001550 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001551 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001552 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001553
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001554 return true;
1555}
1556
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001557/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001558/// folded, but discard the result.
1559bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001560 EvalResult Result;
1561 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001562}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001563
1564APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001565 EvalResult EvalResult;
1566 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001567 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001568 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001569 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001570
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001571 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001572}