blob: 5cfc6e6ab31ef1153f04863480c563f8c89a81ac [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
Anders Carlsson54da0492008-11-30 16:38:33 +000045 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Eli Friedman33ef1452009-02-26 10:19:36 +000046 EvalResult(evalresult) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000047};
48
49
Eli Friedman4efaa272008-11-12 09:44:48 +000050static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000051static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
52static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000053static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000054static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000055static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000056
57//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000058// Misc utilities
59//===----------------------------------------------------------------------===//
60
61static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
62 if (E->getType()->isIntegralType()) {
63 APSInt IntResult;
64 if (!EvaluateInteger(E, IntResult, Info))
65 return false;
66 Result = IntResult != 0;
67 return true;
68 } else if (E->getType()->isRealFloatingType()) {
69 APFloat FloatResult(0.0);
70 if (!EvaluateFloat(E, FloatResult, Info))
71 return false;
72 Result = !FloatResult.isZero();
73 return true;
74 } else if (E->getType()->isPointerType()) {
75 APValue PointerResult;
76 if (!EvaluatePointer(E, PointerResult, Info))
77 return false;
78 // FIXME: Is this accurate for all kinds of bases? If not, what would
79 // the check look like?
80 Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
81 return true;
82 }
Eli Friedman2217c872009-02-22 11:46:18 +000083 // FIXME: Handle pointer-like types, complex types
Eli Friedman4efaa272008-11-12 09:44:48 +000084
85 return false;
86}
87
Daniel Dunbara2cfd342009-01-29 06:16:07 +000088static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
89 APFloat &Value, ASTContext &Ctx) {
90 unsigned DestWidth = Ctx.getIntWidth(DestType);
91 // Determine whether we are converting to unsigned or signed.
92 bool DestSigned = DestType->isSignedIntegerType();
93
94 // FIXME: Warning for overflow.
95 uint64_t Space[4];
96 bool ignored;
97 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
98 llvm::APFloat::rmTowardZero, &ignored);
99 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
100}
101
102static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
103 APFloat &Value, ASTContext &Ctx) {
104 bool ignored;
105 APFloat Result = Value;
106 Result.convert(Ctx.getFloatTypeSemantics(DestType),
107 APFloat::rmNearestTiesToEven, &ignored);
108 return Result;
109}
110
111static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
112 APSInt &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 APSInt Result = Value;
115 // Figure out if this is a truncate, extend or noop cast.
116 // If the input is signed, do a sign extend, noop, or truncate.
117 Result.extOrTrunc(DestWidth);
118 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
119 return Result;
120}
121
122static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
123 APSInt &Value, ASTContext &Ctx) {
124
125 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
126 Result.convertFromAPInt(Value, Value.isSigned(),
127 APFloat::rmNearestTiesToEven);
128 return Result;
129}
130
Eli Friedman4efaa272008-11-12 09:44:48 +0000131//===----------------------------------------------------------------------===//
132// LValue Evaluation
133//===----------------------------------------------------------------------===//
134namespace {
135class VISIBILITY_HIDDEN LValueExprEvaluator
136 : public StmtVisitor<LValueExprEvaluator, APValue> {
137 EvalInfo &Info;
138public:
139
140 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
141
142 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000143 return APValue();
144 }
145
146 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000147 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000148 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
149 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
150 APValue VisitMemberExpr(MemberExpr *E);
151 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000152 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000153 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000154 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +0000155 // FIXME: Missing: __extension__, __real__, __imag__, __builtin_choose_expr
Eli Friedman4efaa272008-11-12 09:44:48 +0000156};
157} // end anonymous namespace
158
159static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
160 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
161 return Result.isLValue();
162}
163
Anders Carlsson35873c42008-11-24 04:41:22 +0000164APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
165{
166 if (!E->hasGlobalStorage())
167 return APValue();
168
169 return APValue(E, 0);
170}
171
Eli Friedman4efaa272008-11-12 09:44:48 +0000172APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
173 if (E->isFileScope())
174 return APValue(E, 0);
175 return APValue();
176}
177
178APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
179 APValue result;
180 QualType Ty;
181 if (E->isArrow()) {
182 if (!EvaluatePointer(E->getBase(), result, Info))
183 return APValue();
184 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
185 } else {
186 result = Visit(E->getBase());
187 if (result.isUninit())
188 return APValue();
189 Ty = E->getBase()->getType();
190 }
191
192 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
193 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000194
195 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
196 if (!FD) // FIXME: deal with other kinds of member expressions
197 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000198
199 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000200 unsigned i = 0;
201 for (RecordDecl::field_iterator Field = RD->field_begin(),
202 FieldEnd = RD->field_end();
203 Field != FieldEnd; (void)++Field, ++i) {
204 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000205 break;
206 }
207
208 result.setLValue(result.getLValueBase(),
209 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
210
211 return result;
212}
213
Anders Carlsson3068d112008-11-16 19:01:22 +0000214APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
215{
216 APValue Result;
217
218 if (!EvaluatePointer(E->getBase(), Result, Info))
219 return APValue();
220
221 APSInt Index;
222 if (!EvaluateInteger(E->getIdx(), Index, Info))
223 return APValue();
224
225 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
226
227 uint64_t Offset = Index.getSExtValue() * ElementSize;
228 Result.setLValue(Result.getLValueBase(),
229 Result.getLValueOffset() + Offset);
230 return Result;
231}
Eli Friedman4efaa272008-11-12 09:44:48 +0000232
Eli Friedmane8761c82009-02-20 01:57:15 +0000233APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E)
234{
235 APValue Result;
236 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
237 return APValue();
238 return Result;
239}
240
Eli Friedman4efaa272008-11-12 09:44:48 +0000241//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000242// Pointer Evaluation
243//===----------------------------------------------------------------------===//
244
Anders Carlssonc754aa62008-07-08 05:13:58 +0000245namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000246class VISIBILITY_HIDDEN PointerExprEvaluator
247 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000248 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000249public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000250
Chris Lattner87eae5e2008-07-11 22:52:41 +0000251 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000252
Anders Carlsson2bad1682008-07-08 14:30:00 +0000253 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000254 return APValue();
255 }
256
257 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
258
Anders Carlsson650c92f2008-07-08 15:34:11 +0000259 APValue VisitBinaryOperator(const BinaryOperator *E);
260 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000261 APValue VisitUnaryExtension(const UnaryOperator *E)
262 { return Visit(E->getSubExpr()); }
263 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000264 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
265 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000266 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
267 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000268 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000269 APValue VisitBlockExpr(BlockExpr *E) {
270 if (!E->hasBlockDeclRefExprs())
271 return APValue(E, 0);
272 return APValue();
273 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000274 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
275 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000276 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000277 APValue VisitChooseExpr(ChooseExpr *E);
278 // FIXME: Missing: @encode, @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000279};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000280} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000281
Chris Lattner87eae5e2008-07-11 22:52:41 +0000282static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000283 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000284 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000285 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000286 return Result.isLValue();
287}
288
289APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
290 if (E->getOpcode() != BinaryOperator::Add &&
291 E->getOpcode() != BinaryOperator::Sub)
292 return APValue();
293
294 const Expr *PExp = E->getLHS();
295 const Expr *IExp = E->getRHS();
296 if (IExp->getType()->isPointerType())
297 std::swap(PExp, IExp);
298
299 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000300 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000301 return APValue();
302
303 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000304 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000305 return APValue();
306
Eli Friedman4efaa272008-11-12 09:44:48 +0000307 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000308 uint64_t SizeOfPointee;
309
310 // Explicitly handle GNU void* and function pointer arithmetic extensions.
311 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
312 SizeOfPointee = 1;
313 else
314 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000315
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000316 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000317
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000318 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000319 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000320 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000321 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
322
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000323 return APValue(ResultLValue.getLValueBase(), Offset);
324}
Eli Friedman4efaa272008-11-12 09:44:48 +0000325
Eli Friedman2217c872009-02-22 11:46:18 +0000326APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
327 APValue result;
328 if (EvaluateLValue(E->getSubExpr(), result, Info))
329 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000330 return APValue();
331}
Anders Carlssond407a762008-12-05 05:24:13 +0000332
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000333
Chris Lattnerb542afe2008-07-11 19:10:17 +0000334APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000335 const Expr* SubExpr = E->getSubExpr();
336
337 // Check for pointer->pointer cast
338 if (SubExpr->getType()->isPointerType()) {
339 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000340 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 return Result;
342 return APValue();
343 }
344
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000345 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000346 APValue Result;
347 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
348 return APValue();
349
350 if (Result.isInt()) {
351 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
352 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000353 }
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000354
355 // Cast is of an lvalue, no need to change value.
356 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000357 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000358
359 if (SubExpr->getType()->isFunctionType() ||
360 SubExpr->getType()->isArrayType()) {
361 APValue Result;
362 if (EvaluateLValue(SubExpr, Result, Info))
363 return Result;
364 return APValue();
365 }
366
367 //assert(0 && "Unhandled cast");
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000368 return APValue();
369}
370
Eli Friedman3941b182009-01-25 01:54:01 +0000371APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000372 if (E->isBuiltinCall(Info.Ctx) ==
373 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000374 return APValue(E, 0);
375 return APValue();
376}
377
Eli Friedman4efaa272008-11-12 09:44:48 +0000378APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
379 bool BoolResult;
380 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
381 return APValue();
382
383 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
384
385 APValue Result;
386 if (EvaluatePointer(EvalExpr, Result, Info))
387 return Result;
388 return APValue();
389}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000390
Eli Friedman91110ee2009-02-23 04:23:56 +0000391APValue PointerExprEvaluator::VisitChooseExpr(ChooseExpr *E) {
392 Expr* EvalExpr = E->isConditionTrue(Info.Ctx) ? E->getLHS() : E->getRHS();
393
394 APValue Result;
395 if (EvaluatePointer(EvalExpr, Result, Info))
396 return Result;
397 return APValue();
398}
399
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000400//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000401// Vector Evaluation
402//===----------------------------------------------------------------------===//
403
404namespace {
405 class VISIBILITY_HIDDEN VectorExprEvaluator
406 : public StmtVisitor<VectorExprEvaluator, APValue> {
407 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000408 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000409 public:
410
411 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
412
413 APValue VisitStmt(Stmt *S) {
414 return APValue();
415 }
416
Eli Friedman91110ee2009-02-23 04:23:56 +0000417 APValue VisitParenExpr(ParenExpr *E)
418 { return Visit(E->getSubExpr()); }
419 APValue VisitUnaryExtension(const UnaryOperator *E)
420 { return Visit(E->getSubExpr()); }
421 APValue VisitUnaryPlus(const UnaryOperator *E)
422 { return Visit(E->getSubExpr()); }
423 APValue VisitUnaryReal(const UnaryOperator *E)
424 { return Visit(E->getSubExpr()); }
425 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
426 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000427 APValue VisitCastExpr(const CastExpr* E);
428 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
429 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000430 APValue VisitConditionalOperator(const ConditionalOperator *E);
431 APValue VisitChooseExpr(const ChooseExpr *E);
432 APValue VisitUnaryImag(const UnaryOperator *E);
433 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000434 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000435 // shufflevector, ExtVectorElementExpr
436 // (Note that these require implementing conversions
437 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000438 };
439} // end anonymous namespace
440
441static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
442 if (!E->getType()->isVectorType())
443 return false;
444 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
445 return !Result.isUninit();
446}
447
448APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
449 const Expr* SE = E->getSubExpr();
450
451 // Check for vector->vector bitcast.
452 if (SE->getType()->isVectorType())
453 return this->Visit(const_cast<Expr*>(SE));
454
455 return APValue();
456}
457
458APValue
459VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
460 return this->Visit(const_cast<Expr*>(E->getInitializer()));
461}
462
463APValue
464VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
465 const VectorType *VT = E->getType()->getAsVectorType();
466 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000467 unsigned NumElements = VT->getNumElements();
Nate Begeman59b5da62009-01-18 03:20:47 +0000468
469 QualType EltTy = VT->getElementType();
470 llvm::SmallVector<APValue, 4> Elements;
471
Eli Friedman91110ee2009-02-23 04:23:56 +0000472 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000473 if (EltTy->isIntegerType()) {
474 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000475 if (i < NumInits) {
476 if (!EvaluateInteger(E->getInit(i), sInt, Info))
477 return APValue();
478 } else {
479 sInt = Info.Ctx.MakeIntValue(0, EltTy);
480 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000481 Elements.push_back(APValue(sInt));
482 } else {
483 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000484 if (i < NumInits) {
485 if (!EvaluateFloat(E->getInit(i), f, Info))
486 return APValue();
487 } else {
488 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
489 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000490 Elements.push_back(APValue(f));
491 }
492 }
493 return APValue(&Elements[0], Elements.size());
494}
495
Eli Friedman91110ee2009-02-23 04:23:56 +0000496APValue
497VectorExprEvaluator::GetZeroVector(QualType T) {
498 const VectorType *VT = T->getAsVectorType();
499 QualType EltTy = VT->getElementType();
500 APValue ZeroElement;
501 if (EltTy->isIntegerType())
502 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
503 else
504 ZeroElement =
505 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
506
507 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
508 return APValue(&Elements[0], Elements.size());
509}
510
511APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
512 bool BoolResult;
513 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
514 return APValue();
515
516 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
517
518 APValue Result;
519 if (EvaluateVector(EvalExpr, Result, Info))
520 return Result;
521 return APValue();
522}
523
524APValue VectorExprEvaluator::VisitChooseExpr(const ChooseExpr *E) {
525 Expr* EvalExpr = E->isConditionTrue(Info.Ctx) ? E->getLHS() : E->getRHS();
526
527 APValue Result;
528 if (EvaluateVector(EvalExpr, Result, Info))
529 return Result;
530 return APValue();
531}
532
533APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
534 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
535 Info.EvalResult.HasSideEffects = true;
536 return GetZeroVector(E->getType());
537}
538
Nate Begeman59b5da62009-01-18 03:20:47 +0000539//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000540// Integer Evaluation
541//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000542
543namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000544class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000545 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000546 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000547 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000548public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000549 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000550 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000551
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000552 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000553 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000554 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000555 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000556 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000557 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000558 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000559 return true;
560 }
561
Daniel Dunbar131eb432009-02-19 09:06:44 +0000562 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000563 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000564 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000565 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000566 Result = APValue(APSInt(I));
567 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000568 return true;
569 }
570
571 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000572 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000573 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000574 return true;
575 }
576
Anders Carlsson82206e22008-11-30 18:14:57 +0000577 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000578 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000579 if (Info.EvalResult.Diag == 0) {
580 Info.EvalResult.DiagLoc = L;
581 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000582 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000583 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000584 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000585 }
586
Anders Carlssonc754aa62008-07-08 05:13:58 +0000587 //===--------------------------------------------------------------------===//
588 // Visitor Methods
589 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000590
591 bool VisitStmt(Stmt *) {
592 assert(0 && "This should be called on integers, stmts are not integers");
593 return false;
594 }
Chris Lattner7a767782008-07-11 19:24:49 +0000595
Chris Lattner32fea9d2008-11-12 07:43:42 +0000596 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000597 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000598 }
599
Chris Lattnerb542afe2008-07-11 19:10:17 +0000600 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000601
Chris Lattner4c4867e2008-07-12 00:38:25 +0000602 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000603 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000604 }
605 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000606 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000607 }
608 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000609 // Per gcc docs "this built-in function ignores top level
610 // qualifiers". We need to use the canonical version to properly
611 // be able to strip CRV qualifiers from the type.
612 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
613 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000614 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
615 T1.getUnqualifiedType()),
616 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000617 }
618 bool VisitDeclRefExpr(const DeclRefExpr *E);
619 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000620 bool VisitBinaryOperator(const BinaryOperator *E);
621 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000622 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000623
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000624 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000625 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
626
Anders Carlsson3068d112008-11-16 19:01:22 +0000627 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000628 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000629 }
630
Anders Carlsson3f704562008-12-21 22:39:40 +0000631 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000632 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000633 }
634
Anders Carlsson3068d112008-11-16 19:01:22 +0000635 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000636 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000637 }
638
Sebastian Redl64b45f72009-01-05 20:52:13 +0000639 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000640 return Success(E->EvaluateTrait(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000641 }
642
Chris Lattnerfcee0012008-07-11 21:24:13 +0000643private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000644 unsigned GetAlignOfExpr(const Expr *E);
645 unsigned GetAlignOfType(QualType T);
Eli Friedman2217c872009-02-22 11:46:18 +0000646 // FIXME: Missing: __real__/__imag__, array subscript of vector,
647 // member of vector, __builtin_choose_expr,
648 // ImplicitValueInitExpr
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000649};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000650} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000651
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000652static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000653 if (!E->getType()->isIntegralType())
654 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000655
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000656 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
657}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000658
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000659static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
660 APValue Val;
661 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
662 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000663 Result = Val.getInt();
664 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000665}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000666
Chris Lattner4c4867e2008-07-12 00:38:25 +0000667bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
668 // Enums are integer constant exprs.
669 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000670 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000671 // signedness consistently; see PR3173.
672 APSInt SI = D->getInitVal();
673 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
674 // FIXME: This is an ugly hack around the fact that enums don't
675 // set their width (!?!) consistently; see PR3173.
676 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
677 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000678 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000679
680 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
681 if (Info.Ctx.getLangOptions().CPlusPlus &&
682 E->getType().getCVRQualifiers() == QualType::Const) {
683 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
684 if (const Expr *Init = D->getInit())
685 return Visit(const_cast<Expr*>(Init));
686 }
687 }
688
Chris Lattner4c4867e2008-07-12 00:38:25 +0000689 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000690 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000691}
692
Chris Lattnera4d55d82008-10-06 06:40:35 +0000693/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
694/// as GCC.
695static int EvaluateBuiltinClassifyType(const CallExpr *E) {
696 // The following enum mimics the values returned by GCC.
697 enum gcc_type_class {
698 no_type_class = -1,
699 void_type_class, integer_type_class, char_type_class,
700 enumeral_type_class, boolean_type_class,
701 pointer_type_class, reference_type_class, offset_type_class,
702 real_type_class, complex_type_class,
703 function_type_class, method_type_class,
704 record_type_class, union_type_class,
705 array_type_class, string_type_class,
706 lang_type_class
707 };
708
709 // If no argument was supplied, default to "no_type_class". This isn't
710 // ideal, however it is what gcc does.
711 if (E->getNumArgs() == 0)
712 return no_type_class;
713
714 QualType ArgTy = E->getArg(0)->getType();
715 if (ArgTy->isVoidType())
716 return void_type_class;
717 else if (ArgTy->isEnumeralType())
718 return enumeral_type_class;
719 else if (ArgTy->isBooleanType())
720 return boolean_type_class;
721 else if (ArgTy->isCharType())
722 return string_type_class; // gcc doesn't appear to use char_type_class
723 else if (ArgTy->isIntegerType())
724 return integer_type_class;
725 else if (ArgTy->isPointerType())
726 return pointer_type_class;
727 else if (ArgTy->isReferenceType())
728 return reference_type_class;
729 else if (ArgTy->isRealType())
730 return real_type_class;
731 else if (ArgTy->isComplexType())
732 return complex_type_class;
733 else if (ArgTy->isFunctionType())
734 return function_type_class;
735 else if (ArgTy->isStructureType())
736 return record_type_class;
737 else if (ArgTy->isUnionType())
738 return union_type_class;
739 else if (ArgTy->isArrayType())
740 return array_type_class;
741 else if (ArgTy->isUnionType())
742 return union_type_class;
743 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
744 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
745 return -1;
746}
747
Chris Lattner4c4867e2008-07-12 00:38:25 +0000748bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000749 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000750 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000751 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000752 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000753 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000754
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000755 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000756 // __builtin_constant_p always has one operand: it returns true if that
757 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000758 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000759 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000760}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000761
Chris Lattnerb542afe2008-07-11 19:10:17 +0000762bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000763 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000764 if (!Visit(E->getRHS()))
765 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000766
Eli Friedman33ef1452009-02-26 10:19:36 +0000767 // If we can't evaluate the LHS, it might have side effects;
768 // conservatively mark it.
769 if (!E->getLHS()->isEvaluatable(Info.Ctx))
770 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000771
Anders Carlsson027f62e2008-12-01 02:07:06 +0000772 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000773 }
774
775 if (E->isLogicalOp()) {
776 // These need to be handled specially because the operands aren't
777 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000778 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000779
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000780 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000781 // We were able to evaluate the LHS, see if we can get away with not
782 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000783 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000784 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000785
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000786 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000787 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000788 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000789 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000790 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000791 }
792 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000793 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000794 // We can't evaluate the LHS; however, sometimes the result
795 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000796 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
797 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000798 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000799 // must have had side effects.
800 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000801
802 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000803 }
804 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000805 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000806
Eli Friedmana6afa762008-11-13 06:09:17 +0000807 return false;
808 }
809
Anders Carlsson286f85e2008-11-16 07:17:21 +0000810 QualType LHSTy = E->getLHS()->getType();
811 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000812
813 if (LHSTy->isAnyComplexType()) {
814 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
815 APValue LHS, RHS;
816
817 if (!EvaluateComplex(E->getLHS(), LHS, Info))
818 return false;
819
820 if (!EvaluateComplex(E->getRHS(), RHS, Info))
821 return false;
822
823 if (LHS.isComplexFloat()) {
824 APFloat::cmpResult CR_r =
825 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
826 APFloat::cmpResult CR_i =
827 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
828
Daniel Dunbar4087e242009-01-29 06:43:41 +0000829 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000830 return Success((CR_r == APFloat::cmpEqual &&
831 CR_i == APFloat::cmpEqual), E);
832 else {
833 assert(E->getOpcode() == BinaryOperator::NE &&
834 "Invalid complex comparison.");
835 return Success(((CR_r == APFloat::cmpGreaterThan ||
836 CR_r == APFloat::cmpLessThan) &&
837 (CR_i == APFloat::cmpGreaterThan ||
838 CR_i == APFloat::cmpLessThan)), E);
839 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000840 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000841 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000842 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
843 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
844 else {
845 assert(E->getOpcode() == BinaryOperator::NE &&
846 "Invalid compex comparison.");
847 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
848 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
849 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000850 }
851 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000852
853 if (LHSTy->isRealFloatingType() &&
854 RHSTy->isRealFloatingType()) {
855 APFloat RHS(0.0), LHS(0.0);
856
857 if (!EvaluateFloat(E->getRHS(), RHS, Info))
858 return false;
859
860 if (!EvaluateFloat(E->getLHS(), LHS, Info))
861 return false;
862
863 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000864
Anders Carlsson286f85e2008-11-16 07:17:21 +0000865 switch (E->getOpcode()) {
866 default:
867 assert(0 && "Invalid binary operator!");
868 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000869 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000870 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000871 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000872 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000873 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000874 case BinaryOperator::GE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000875 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
876 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000877 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000878 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000879 case BinaryOperator::NE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000880 return Success(CR == APFloat::cmpGreaterThan
881 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000882 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000883 }
884
Anders Carlsson3068d112008-11-16 19:01:22 +0000885 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000886 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000887 APValue LHSValue;
888 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
889 return false;
890
891 APValue RHSValue;
892 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
893 return false;
894
895 // FIXME: Is this correct? What if only one of the operands has a base?
896 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
897 return false;
898
899 const QualType Type = E->getLHS()->getType();
900 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
901
902 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
903 D /= Info.Ctx.getTypeSize(ElementType) / 8;
904
Daniel Dunbar131eb432009-02-19 09:06:44 +0000905 return Success(D, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000906 }
907 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000908 if (!LHSTy->isIntegralType() ||
909 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000910 // We can't continue from here for non-integral types, and they
911 // could potentially confuse the following operations.
912 // FIXME: Deal with EQ and friends.
913 return false;
914 }
915
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000916 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000917 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +0000918 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000919
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000920 // Only support arithmetic on integers for now.
921 if (!Result.isInt())
922 return false;
923
Daniel Dunbar131eb432009-02-19 09:06:44 +0000924 llvm::APSInt RHS;
Chris Lattner54176fd2008-07-12 00:14:42 +0000925 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000926 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000927
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000928 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000929 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000930 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000931 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
932 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
933 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
934 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
935 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
936 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000937 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000938 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000939 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000940 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000941 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000942 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000943 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000944 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000945 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +0000946 // FIXME: Warn about out of range shift amounts!
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000947 unsigned SA =
948 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
949 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000950 }
951 case BinaryOperator::Shr: {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000952 unsigned SA =
953 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
954 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000955 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000956
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000957 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
958 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
959 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
960 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
961 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
962 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +0000963 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000964}
965
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000966bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +0000967 bool Cond;
968 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000969 return false;
970
Nuno Lopesa25bd552008-11-16 22:06:39 +0000971 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000972}
973
Chris Lattneraf707ab2009-01-24 21:53:27 +0000974unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +0000975 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
976
977 // __alignof__(void) = 1 as a gcc extension.
978 if (Ty->isVoidType())
979 return 1;
980
981 // GCC extension: alignof(function) = 4.
982 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
983 // attribute(align) directive.
984 if (Ty->isFunctionType())
985 return 4;
986
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000987 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
988 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere9feb472009-01-24 21:09:06 +0000989
990 // alignof VLA/incomplete array.
991 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
992 return GetAlignOfType(VAT->getElementType());
993
994 // sizeof (objc class)?
995 if (isa<ObjCInterfaceType>(Ty))
996 return 1; // FIXME: This probably isn't right.
997
998 // Get information about the alignment.
999 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanc5082032009-02-22 03:31:23 +00001000 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001001}
1002
Chris Lattneraf707ab2009-01-24 21:53:27 +00001003unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1004 E = E->IgnoreParens();
1005
1006 // alignof decl is always accepted, even if it doesn't make sense: we default
1007 // to 1 in those cases.
1008 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001009 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001010
1011 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001012 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001013
Chris Lattnere9feb472009-01-24 21:09:06 +00001014 return GetAlignOfType(E->getType());
1015}
1016
1017
Sebastian Redl05189992008-11-11 17:56:53 +00001018/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1019/// expression's type.
1020bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1021 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001022
Chris Lattnere9feb472009-01-24 21:09:06 +00001023 // Handle alignof separately.
1024 if (!E->isSizeOf()) {
1025 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001026 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001027 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001028 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001029 }
1030
Sebastian Redl05189992008-11-11 17:56:53 +00001031 QualType SrcTy = E->getTypeOfArgument();
1032
Daniel Dunbar131eb432009-02-19 09:06:44 +00001033 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1034 // extension.
1035 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1036 return Success(1, E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001037
1038 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001039 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001040 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001041
1042 if (SrcTy->isObjCInterfaceType()) {
1043 // Slightly unusual case: the size of an ObjC interface type is the
1044 // size of the class. This code intentionally falls through to the normal
1045 // case.
1046 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
1047 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
1048 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
1049 }
1050
Chris Lattnere9feb472009-01-24 21:09:06 +00001051 // Get information about the size.
Chris Lattner87eae5e2008-07-11 22:52:41 +00001052 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Daniel Dunbar131eb432009-02-19 09:06:44 +00001053 return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001054}
1055
Chris Lattnerb542afe2008-07-11 19:10:17 +00001056bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001057 // Special case unary operators that do not need their subexpression
1058 // evaluated. offsetof/sizeof/alignof are all special.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001059 if (E->isOffsetOfOp())
1060 return Success(E->evaluateOffsetOf(Info.Ctx), E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001061
1062 if (E->getOpcode() == UnaryOperator::LNot) {
1063 // LNot's operand isn't necessarily an integer, so we handle it specially.
1064 bool bres;
1065 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1066 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001067 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001068 }
1069
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001070 // Only handle integral operations...
1071 if (!E->getSubExpr()->getType()->isIntegralType())
1072 return false;
1073
Chris Lattner87eae5e2008-07-11 22:52:41 +00001074 // Get the operand value into 'Result'.
1075 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001076 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001077
Chris Lattner75a48812008-07-11 22:15:16 +00001078 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001079 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001080 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1081 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001082 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001083 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001084 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1085 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001086 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001087 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001088 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001089 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001090 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001091 if (!Result.isInt()) return false;
1092 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001093 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001094 if (!Result.isInt()) return false;
1095 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001096 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001097}
1098
Chris Lattner732b2232008-07-12 01:15:53 +00001099/// HandleCast - This is used to evaluate implicit or explicit casts where the
1100/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001101bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001102 Expr *SubExpr = E->getSubExpr();
1103 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001104 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001105
Eli Friedman4efaa272008-11-12 09:44:48 +00001106 if (DestType->isBooleanType()) {
1107 bool BoolResult;
1108 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1109 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001110 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001111 }
1112
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001113 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001114 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001115 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001116 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001117
Eli Friedmanbe265702009-02-20 01:15:07 +00001118 if (!Result.isInt()) {
1119 // Only allow casts of lvalues if they are lossless.
1120 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1121 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001122
Daniel Dunbardd211642009-02-19 22:24:01 +00001123 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001124 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001125 }
1126
1127 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001128 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001129 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001130 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001131 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001132
Daniel Dunbardd211642009-02-19 22:24:01 +00001133 if (LV.getLValueBase()) {
1134 // Only allow based lvalue casts if they are lossless.
1135 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1136 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001137
Daniel Dunbardd211642009-02-19 22:24:01 +00001138 Result = LV;
1139 return true;
1140 }
1141
1142 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1143 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001144 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001145
Eli Friedmanbe265702009-02-20 01:15:07 +00001146 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1147 // This handles double-conversion cases, where there's both
1148 // an l-value promotion and an implicit conversion to int.
1149 APValue LV;
1150 if (!EvaluateLValue(SubExpr, LV, Info))
1151 return false;
1152
1153 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1154 return false;
1155
1156 Result = LV;
1157 return true;
1158 }
1159
Eli Friedman2217c872009-02-22 11:46:18 +00001160 // FIXME: Handle complex types
1161 // FIXME: Handle vectors
1162
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001163 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001164 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001165
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001166 APFloat F(0.0);
1167 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001168 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001169
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001170 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001171}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001172
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001173//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001174// Float Evaluation
1175//===----------------------------------------------------------------------===//
1176
1177namespace {
1178class VISIBILITY_HIDDEN FloatExprEvaluator
1179 : public StmtVisitor<FloatExprEvaluator, bool> {
1180 EvalInfo &Info;
1181 APFloat &Result;
1182public:
1183 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1184 : Info(info), Result(result) {}
1185
1186 bool VisitStmt(Stmt *S) {
1187 return false;
1188 }
1189
1190 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001191 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001192
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001193 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001194 bool VisitBinaryOperator(const BinaryOperator *E);
1195 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001196 bool VisitCastExpr(CastExpr *E);
1197 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001198
1199 // FIXME: Missing: __real__/__imag__, __extension__,
1200 // array subscript of vector, member of vector,
1201 // __builtin_choose_expr, ImplicitValueInitExpr,
1202 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001203};
1204} // end anonymous namespace
1205
1206static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1207 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1208}
1209
Chris Lattner019f4e82008-10-06 05:28:25 +00001210bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001211 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001212 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001213 case Builtin::BI__builtin_huge_val:
1214 case Builtin::BI__builtin_huge_valf:
1215 case Builtin::BI__builtin_huge_vall:
1216 case Builtin::BI__builtin_inf:
1217 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001218 case Builtin::BI__builtin_infl: {
1219 const llvm::fltSemantics &Sem =
1220 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001221 Result = llvm::APFloat::getInf(Sem);
1222 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001223 }
Chris Lattner9e621712008-10-06 06:31:58 +00001224
1225 case Builtin::BI__builtin_nan:
1226 case Builtin::BI__builtin_nanf:
1227 case Builtin::BI__builtin_nanl:
1228 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1229 // can't constant fold it.
1230 if (const StringLiteral *S =
1231 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1232 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001233 const llvm::fltSemantics &Sem =
1234 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001235 Result = llvm::APFloat::getNaN(Sem);
1236 return true;
1237 }
1238 }
1239 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001240
1241 case Builtin::BI__builtin_fabs:
1242 case Builtin::BI__builtin_fabsf:
1243 case Builtin::BI__builtin_fabsl:
1244 if (!EvaluateFloat(E->getArg(0), Result, Info))
1245 return false;
1246
1247 if (Result.isNegative())
1248 Result.changeSign();
1249 return true;
1250
1251 case Builtin::BI__builtin_copysign:
1252 case Builtin::BI__builtin_copysignf:
1253 case Builtin::BI__builtin_copysignl: {
1254 APFloat RHS(0.);
1255 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1256 !EvaluateFloat(E->getArg(1), RHS, Info))
1257 return false;
1258 Result.copySign(RHS);
1259 return true;
1260 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001261 }
1262}
1263
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001264bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001265 if (E->getOpcode() == UnaryOperator::Deref)
1266 return false;
1267
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001268 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1269 return false;
1270
1271 switch (E->getOpcode()) {
1272 default: return false;
1273 case UnaryOperator::Plus:
1274 return true;
1275 case UnaryOperator::Minus:
1276 Result.changeSign();
1277 return true;
1278 }
1279}
Chris Lattner019f4e82008-10-06 05:28:25 +00001280
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001281bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1282 // FIXME: Diagnostics? I really don't understand how the warnings
1283 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001284 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001285 if (!EvaluateFloat(E->getLHS(), Result, Info))
1286 return false;
1287 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1288 return false;
1289
1290 switch (E->getOpcode()) {
1291 default: return false;
1292 case BinaryOperator::Mul:
1293 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1294 return true;
1295 case BinaryOperator::Add:
1296 Result.add(RHS, APFloat::rmNearestTiesToEven);
1297 return true;
1298 case BinaryOperator::Sub:
1299 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1300 return true;
1301 case BinaryOperator::Div:
1302 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1303 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001304 }
1305}
1306
1307bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1308 Result = E->getValue();
1309 return true;
1310}
1311
Eli Friedman4efaa272008-11-12 09:44:48 +00001312bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1313 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001314
Eli Friedman4efaa272008-11-12 09:44:48 +00001315 if (SubExpr->getType()->isIntegralType()) {
1316 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001317 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001318 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001319 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1320 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001321 return true;
1322 }
1323 if (SubExpr->getType()->isRealFloatingType()) {
1324 if (!Visit(SubExpr))
1325 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001326 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1327 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001328 return true;
1329 }
Eli Friedman2217c872009-02-22 11:46:18 +00001330 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001331
1332 return false;
1333}
1334
1335bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1336 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1337 return true;
1338}
1339
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001340//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001341// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001342//===----------------------------------------------------------------------===//
1343
1344namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001345class VISIBILITY_HIDDEN ComplexExprEvaluator
1346 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001347 EvalInfo &Info;
1348
1349public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001350 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001351
1352 //===--------------------------------------------------------------------===//
1353 // Visitor Methods
1354 //===--------------------------------------------------------------------===//
1355
1356 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001357 return APValue();
1358 }
1359
1360 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1361
1362 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001363 Expr* SubExpr = E->getSubExpr();
1364
1365 if (SubExpr->getType()->isRealFloatingType()) {
1366 APFloat Result(0.0);
1367
1368 if (!EvaluateFloat(SubExpr, Result, Info))
1369 return APValue();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001370
Daniel Dunbar3f279872009-01-29 01:32:56 +00001371 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001372 Result);
1373 } else {
1374 assert(SubExpr->getType()->isIntegerType() &&
1375 "Unexpected imaginary literal.");
1376
1377 llvm::APSInt Result;
1378 if (!EvaluateInteger(SubExpr, Result, Info))
1379 return APValue();
1380
1381 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1382 Zero = 0;
1383 return APValue(Zero, Result);
1384 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001385 }
1386
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001387 APValue VisitCastExpr(CastExpr *E) {
1388 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001389 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1390 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001391
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001392 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001393 APFloat Result(0.0);
1394
1395 if (!EvaluateFloat(SubExpr, Result, Info))
1396 return APValue();
1397
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001398 // Apply float conversion if necessary.
1399 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbar8f826f02009-01-24 19:08:01 +00001400 return APValue(Result,
Daniel Dunbar3f279872009-01-29 01:32:56 +00001401 APFloat(Result.getSemantics(), APFloat::fcZero, false));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001402 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001403 APSInt Result;
1404
1405 if (!EvaluateInteger(SubExpr, Result, Info))
1406 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001407
1408 // Apply integer conversion if necessary.
1409 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001410 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1411 Zero = 0;
1412 return APValue(Result, Zero);
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001413 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1414 APValue Src;
1415
1416 if (!EvaluateComplex(SubExpr, Src, Info))
1417 return APValue();
1418
1419 QualType SrcType = CT->getElementType();
1420
1421 if (Src.isComplexFloat()) {
1422 if (EltType->isRealFloatingType()) {
1423 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1424 Src.getComplexFloatReal(),
1425 Info.Ctx),
1426 HandleFloatToFloatCast(EltType, SrcType,
1427 Src.getComplexFloatImag(),
1428 Info.Ctx));
1429 } else {
1430 return APValue(HandleFloatToIntCast(EltType, SrcType,
1431 Src.getComplexFloatReal(),
1432 Info.Ctx),
1433 HandleFloatToIntCast(EltType, SrcType,
1434 Src.getComplexFloatImag(),
1435 Info.Ctx));
1436 }
1437 } else {
1438 assert(Src.isComplexInt() && "Invalid evaluate result.");
1439 if (EltType->isRealFloatingType()) {
1440 return APValue(HandleIntToFloatCast(EltType, SrcType,
1441 Src.getComplexIntReal(),
1442 Info.Ctx),
1443 HandleIntToFloatCast(EltType, SrcType,
1444 Src.getComplexIntImag(),
1445 Info.Ctx));
1446 } else {
1447 return APValue(HandleIntToIntCast(EltType, SrcType,
1448 Src.getComplexIntReal(),
1449 Info.Ctx),
1450 HandleIntToIntCast(EltType, SrcType,
1451 Src.getComplexIntImag(),
1452 Info.Ctx));
1453 }
1454 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001455 }
1456
1457 // FIXME: Handle more casts.
1458 return APValue();
1459 }
1460
1461 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001462 // FIXME Missing: unary +/-/~, __extension__, binary div,
1463 // __builtin_choose_expr, ImplicitValueInitExpr,
1464 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001465};
1466} // end anonymous namespace
1467
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001468static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001469{
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001470 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1471 assert((!Result.isComplexFloat() ||
1472 (&Result.getComplexFloatReal().getSemantics() ==
1473 &Result.getComplexFloatImag().getSemantics())) &&
1474 "Invalid complex evaluation.");
1475 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001476}
1477
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001478APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001479{
1480 APValue Result, RHS;
1481
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001482 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001483 return APValue();
1484
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001485 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001486 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001487
Daniel Dunbar3f279872009-01-29 01:32:56 +00001488 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1489 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001490 switch (E->getOpcode()) {
1491 default: return APValue();
1492 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001493 if (Result.isComplexFloat()) {
1494 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1495 APFloat::rmNearestTiesToEven);
1496 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1497 APFloat::rmNearestTiesToEven);
1498 } else {
1499 Result.getComplexIntReal() += RHS.getComplexIntReal();
1500 Result.getComplexIntImag() += RHS.getComplexIntImag();
1501 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001502 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001503 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001504 if (Result.isComplexFloat()) {
1505 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1506 APFloat::rmNearestTiesToEven);
1507 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1508 APFloat::rmNearestTiesToEven);
1509 } else {
1510 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1511 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1512 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001513 break;
1514 case BinaryOperator::Mul:
1515 if (Result.isComplexFloat()) {
1516 APValue LHS = Result;
1517 APFloat &LHS_r = LHS.getComplexFloatReal();
1518 APFloat &LHS_i = LHS.getComplexFloatImag();
1519 APFloat &RHS_r = RHS.getComplexFloatReal();
1520 APFloat &RHS_i = RHS.getComplexFloatImag();
1521
1522 APFloat Tmp = LHS_r;
1523 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1524 Result.getComplexFloatReal() = Tmp;
1525 Tmp = LHS_i;
1526 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1527 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1528
1529 Tmp = LHS_r;
1530 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1531 Result.getComplexFloatImag() = Tmp;
1532 Tmp = LHS_i;
1533 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1534 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1535 } else {
1536 APValue LHS = Result;
1537 Result.getComplexIntReal() =
1538 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1539 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1540 Result.getComplexIntImag() =
1541 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1542 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1543 }
1544 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001545 }
1546
1547 return Result;
1548}
1549
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001550//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001551// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001552//===----------------------------------------------------------------------===//
1553
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001554/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001555/// any crazy technique (that has nothing to do with language standards) that
1556/// we want to. If this function returns true, it returns the folded constant
1557/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001558bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1559 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001560
Nate Begeman59b5da62009-01-18 03:20:47 +00001561 if (getType()->isVectorType()) {
1562 if (!EvaluateVector(this, Result.Val, Info))
1563 return false;
1564 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001565 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001566 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001567 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001568 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001569 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001570 } else if (getType()->isRealFloatingType()) {
1571 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001572 if (!EvaluateFloat(this, f, Info))
1573 return false;
1574
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001575 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001576 } else if (getType()->isAnyComplexType()) {
1577 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001578 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001579 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001580 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001581
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001582 return true;
1583}
1584
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001585/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001586/// folded, but discard the result.
1587bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001588 EvalResult Result;
1589 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001590}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001591
1592APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001593 EvalResult EvalResult;
1594 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001595 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001596 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001597 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001598
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001599 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001600}