blob: 68818dbf7b2a83e73c94fbe231cec9fdc2a1c4ae [file] [log] [blame]
Chris Lattnera42f09a2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc7436af2008-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 Friedman7888b932008-11-12 09:44:48 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeonefddb9c2008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Anders Carlssonc0328012008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssoncad17b52008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc7436af2008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnera823ccf2008-07-11 18:11:29 +000022using llvm::APSInt;
Eli Friedman2f445492008-08-22 00:06:13 +000023using llvm::APFloat;
Anders Carlssonc7436af2008-07-03 04:20:39 +000024
Chris Lattner422373c2008-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 Carlssondd8d41f2008-11-30 16:38:33 +000042 /// EvalResult - Contains information about the evaluation.
43 Expr::EvalResult &EvalResult;
Anders Carlsson6c1a9e22008-11-30 18:26:25 +000044
Anders Carlssondd8d41f2008-11-30 16:38:33 +000045 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Eli Friedmanc092e032009-02-26 10:19:36 +000046 EvalResult(evalresult) {}
Chris Lattner422373c2008-07-11 22:52:41 +000047};
48
49
Eli Friedman7888b932008-11-12 09:44:48 +000050static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner422373c2008-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 Dunbar56611002009-02-20 18:22:23 +000053static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedman2f445492008-08-22 00:06:13 +000054static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbaraf781bb2009-01-28 22:24:07 +000055static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnera823ccf2008-07-11 18:11:29 +000056
57//===----------------------------------------------------------------------===//
Eli Friedman7888b932008-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 Friedman2207dec2009-02-22 11:46:18 +000083 // FIXME: Handle pointer-like types, complex types
Eli Friedman7888b932008-11-12 09:44:48 +000084
85 return false;
86}
87
Daniel Dunbaraffa0e32009-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 Friedman7888b932008-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 Friedman7888b932008-11-12 09:44:48 +0000143 return APValue();
144 }
145
146 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssone284ebe2008-11-24 04:41:22 +0000147 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman7888b932008-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 Lattnerc5d32632009-02-24 22:18:39 +0000152 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson027f2882008-11-16 19:01:22 +0000153 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmandbd0a9b2009-02-20 01:57:15 +0000154 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman2207dec2009-02-22 11:46:18 +0000155 // FIXME: Missing: __extension__, __real__, __imag__, __builtin_choose_expr
Eli Friedman7888b932008-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 Carlssone284ebe2008-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 Friedman7888b932008-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 Gregor82d44772008-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 Friedman7888b932008-11-12 09:44:48 +0000198
199 // FIXME: This is linear time.
Douglas Gregor8acb7272008-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 Friedman7888b932008-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 Carlsson027f2882008-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 Friedman7888b932008-11-12 09:44:48 +0000232
Eli Friedmandbd0a9b2009-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 Friedman7888b932008-11-12 09:44:48 +0000241//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000242// Pointer Evaluation
243//===----------------------------------------------------------------------===//
244
Anders Carlssoncad17b52008-07-08 05:13:58 +0000245namespace {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000246class VISIBILITY_HIDDEN PointerExprEvaluator
247 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner422373c2008-07-11 22:52:41 +0000248 EvalInfo &Info;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000249public:
Anders Carlsson02a34c32008-07-08 14:30:00 +0000250
Chris Lattner422373c2008-07-11 22:52:41 +0000251 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000252
Anders Carlsson02a34c32008-07-08 14:30:00 +0000253 APValue VisitStmt(Stmt *S) {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000254 return APValue();
255 }
256
257 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
258
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000259 APValue VisitBinaryOperator(const BinaryOperator *E);
260 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2207dec2009-02-22 11:46:18 +0000261 APValue VisitUnaryExtension(const UnaryOperator *E)
262 { return Visit(E->getSubExpr()); }
263 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000264 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
265 { return APValue(E, 0); }
Eli Friedmanf1941132009-01-25 01:21:06 +0000266 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
267 { return APValue(E, 0); }
Eli Friedman67f4ac52009-01-25 01:54:01 +0000268 APValue VisitCallExpr(CallExpr *E);
Mike Stumpae93d652009-02-19 22:01:56 +0000269 APValue VisitBlockExpr(BlockExpr *E) {
270 if (!E->hasBlockDeclRefExprs())
271 return APValue(E, 0);
272 return APValue();
273 }
Eli Friedmana63aaeb2009-02-23 04:23:56 +0000274 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
275 { return APValue((Expr*)0, 0); }
Eli Friedman7888b932008-11-12 09:44:48 +0000276 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmana63aaeb2009-02-23 04:23:56 +0000277 APValue VisitChooseExpr(ChooseExpr *E);
278 // FIXME: Missing: @encode, @protocol, @selector
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000279};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000280} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000281
Chris Lattner422373c2008-07-11 22:52:41 +0000282static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000283 if (!E->getType()->hasPointerRepresentation())
Chris Lattnera823ccf2008-07-11 18:11:29 +0000284 return false;
Chris Lattner422373c2008-07-11 22:52:41 +0000285 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnera823ccf2008-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 Lattner422373c2008-07-11 22:52:41 +0000300 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000301 return APValue();
302
303 llvm::APSInt AdditionalOffset(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000304 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000305 return APValue();
306
Eli Friedman7888b932008-11-12 09:44:48 +0000307 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4fbb52c2009-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 Friedman7888b932008-11-12 09:44:48 +0000315
Chris Lattnera823ccf2008-07-11 18:11:29 +0000316 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman7888b932008-11-12 09:44:48 +0000317
Chris Lattnera823ccf2008-07-11 18:11:29 +0000318 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman7888b932008-11-12 09:44:48 +0000319 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnera823ccf2008-07-11 18:11:29 +0000320 else
Eli Friedman7888b932008-11-12 09:44:48 +0000321 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
322
Chris Lattnera823ccf2008-07-11 18:11:29 +0000323 return APValue(ResultLValue.getLValueBase(), Offset);
324}
Eli Friedman7888b932008-11-12 09:44:48 +0000325
Eli Friedman2207dec2009-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 Friedman7888b932008-11-12 09:44:48 +0000330 return APValue();
331}
Anders Carlsson4ab88da2008-12-05 05:24:13 +0000332
Chris Lattnera823ccf2008-07-11 18:11:29 +0000333
Chris Lattnera42f09a2008-07-11 19:10:17 +0000334APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnera823ccf2008-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 Lattner422373c2008-07-11 22:52:41 +0000340 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000341 return Result;
342 return APValue();
343 }
344
Eli Friedman3e64dd72008-07-27 05:46:18 +0000345 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar56611002009-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 Lattnera823ccf2008-07-11 18:11:29 +0000353 }
Daniel Dunbar56611002009-02-20 18:22:23 +0000354
355 // Cast is of an lvalue, no need to change value.
356 return Result;
Chris Lattnera823ccf2008-07-11 18:11:29 +0000357 }
Eli Friedman7888b932008-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 Lattnera823ccf2008-07-11 18:11:29 +0000368 return APValue();
369}
370
Eli Friedman67f4ac52009-01-25 01:54:01 +0000371APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregorb5af7382009-02-14 18:57:46 +0000372 if (E->isBuiltinCall(Info.Ctx) ==
373 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman67f4ac52009-01-25 01:54:01 +0000374 return APValue(E, 0);
375 return APValue();
376}
377
Eli Friedman7888b932008-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 Lattnera823ccf2008-07-11 18:11:29 +0000390
Eli Friedmana63aaeb2009-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 Lattnera823ccf2008-07-11 18:11:29 +0000400//===----------------------------------------------------------------------===//
Nate Begemand6d2f772009-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 Friedmana63aaeb2009-02-23 04:23:56 +0000408 APValue GetZeroVector(QualType VecType);
Nate Begemand6d2f772009-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 Friedmana63aaeb2009-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 Begemand6d2f772009-01-18 03:20:47 +0000427 APValue VisitCastExpr(const CastExpr* E);
428 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
429 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedmana63aaeb2009-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 Friedman2207dec2009-02-22 11:46:18 +0000434 // binary comparisons, binary and/or/xor,
Eli Friedmana63aaeb2009-02-23 04:23:56 +0000435 // shufflevector, ExtVectorElementExpr
436 // (Note that these require implementing conversions
437 // between vector types.)
Nate Begemand6d2f772009-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 Friedmana63aaeb2009-02-23 04:23:56 +0000467 unsigned NumElements = VT->getNumElements();
Nate Begemand6d2f772009-01-18 03:20:47 +0000468
469 QualType EltTy = VT->getElementType();
470 llvm::SmallVector<APValue, 4> Elements;
471
Eli Friedmana63aaeb2009-02-23 04:23:56 +0000472 for (unsigned i = 0; i < NumElements; i++) {
Nate Begemand6d2f772009-01-18 03:20:47 +0000473 if (EltTy->isIntegerType()) {
474 llvm::APSInt sInt(32);
Eli Friedmana63aaeb2009-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 Begemand6d2f772009-01-18 03:20:47 +0000481 Elements.push_back(APValue(sInt));
482 } else {
483 llvm::APFloat f(0.0);
Eli Friedmana63aaeb2009-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 Begemand6d2f772009-01-18 03:20:47 +0000490 Elements.push_back(APValue(f));
491 }
492 }
493 return APValue(&Elements[0], Elements.size());
494}
495
Eli Friedmana63aaeb2009-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 Begemand6d2f772009-01-18 03:20:47 +0000539//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000540// Integer Evaluation
541//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000542
543namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000544class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000545 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000546 EvalInfo &Info;
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000547 APValue &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000548public:
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000549 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner422373c2008-07-11 22:52:41 +0000550 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000551
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000552 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbarb5c66db2009-02-21 18:14:20 +0000553 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000554 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000555 "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000556 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000557 "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000558 Result = APValue(SI);
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000559 return true;
560 }
561
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000562 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbarb5c66db2009-02-21 18:14:20 +0000563 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000564 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000565 "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000566 Result = APValue(APSInt(I));
567 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000568 return true;
569 }
570
571 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbarb5c66db2009-02-21 18:14:20 +0000572 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000573 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000574 return true;
575 }
576
Anders Carlssonfa76d822008-11-30 18:14:57 +0000577 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000578 // Take the first error.
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000579 if (Info.EvalResult.Diag == 0) {
580 Info.EvalResult.DiagLoc = L;
581 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000582 Info.EvalResult.DiagExpr = E;
Chris Lattner438f3b12008-11-12 07:43:42 +0000583 }
Chris Lattner82437da2008-07-12 00:14:42 +0000584 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000585 }
586
Anders Carlssoncad17b52008-07-08 05:13:58 +0000587 //===--------------------------------------------------------------------===//
588 // Visitor Methods
589 //===--------------------------------------------------------------------===//
Chris Lattner438f3b12008-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 Lattner2c99c712008-07-11 19:24:49 +0000595
Chris Lattner438f3b12008-11-12 07:43:42 +0000596 bool VisitExpr(Expr *E) {
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000597 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000598 }
599
Chris Lattnera42f09a2008-07-11 19:10:17 +0000600 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000601
Chris Lattner15e59112008-07-12 00:38:25 +0000602 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000603 return Success(E->getValue(), E);
Chris Lattner15e59112008-07-12 00:38:25 +0000604 }
605 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000606 return Success(E->getValue(), E);
Chris Lattner15e59112008-07-12 00:38:25 +0000607 }
608 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarda8ebd22008-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 Dunbarf91896e2009-02-19 09:06:44 +0000614 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
615 T1.getUnqualifiedType()),
616 E);
Chris Lattner15e59112008-07-12 00:38:25 +0000617 }
618 bool VisitDeclRefExpr(const DeclRefExpr *E);
619 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000620 bool VisitBinaryOperator(const BinaryOperator *E);
621 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000622 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000623
Daniel Dunbaraffa0e32009-01-29 06:16:07 +0000624 bool VisitCastExpr(CastExpr* E);
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000625 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
626
Anders Carlsson027f2882008-11-16 19:01:22 +0000627 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000628 return Success(E->getValue(), E);
Anders Carlsson027f2882008-11-16 19:01:22 +0000629 }
630
Anders Carlsson774f9c72008-12-21 22:39:40 +0000631 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000632 return Success(0, E);
Anders Carlsson774f9c72008-12-21 22:39:40 +0000633 }
634
Anders Carlsson027f2882008-11-16 19:01:22 +0000635 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000636 return Success(0, E);
Anders Carlsson027f2882008-11-16 19:01:22 +0000637 }
638
Eli Friedman25cb1b62009-02-27 04:45:43 +0000639 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
640 return Success(0, E);
641 }
642
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000643 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000644 return Success(E->EvaluateTrait(), E);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000645 }
646
Eli Friedman25cb1b62009-02-27 04:45:43 +0000647 bool VisitChooseExpr(const ChooseExpr *E);
648 bool VisitUnaryReal(const UnaryOperator *E) {
649 return Visit(E->getSubExpr());
650 }
651 bool VisitUnaryImag(const UnaryOperator *E);
652
Chris Lattner265a0892008-07-11 21:24:13 +0000653private:
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000654 unsigned GetAlignOfExpr(const Expr *E);
655 unsigned GetAlignOfType(QualType T);
Eli Friedman25cb1b62009-02-27 04:45:43 +0000656 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssond1aa5812008-07-08 14:35:21 +0000657};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000658} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000659
Daniel Dunbar56611002009-02-20 18:22:23 +0000660static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000661 if (!E->getType()->isIntegralType())
662 return false;
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000663
Daniel Dunbar56611002009-02-20 18:22:23 +0000664 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
665}
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000666
Daniel Dunbar56611002009-02-20 18:22:23 +0000667static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
668 APValue Val;
669 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
670 return false;
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000671 Result = Val.getInt();
672 return true;
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000673}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000674
Chris Lattner15e59112008-07-12 00:38:25 +0000675bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
676 // Enums are integer constant exprs.
677 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedman8b10a232008-12-08 02:21:03 +0000678 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000679 // signedness consistently; see PR3173.
680 APSInt SI = D->getInitVal();
681 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
682 // FIXME: This is an ugly hack around the fact that enums don't
683 // set their width (!?!) consistently; see PR3173.
684 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
685 return Success(SI, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000686 }
Sebastian Redl410dd872009-02-08 15:51:17 +0000687
688 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
689 if (Info.Ctx.getLangOptions().CPlusPlus &&
690 E->getType().getCVRQualifiers() == QualType::Const) {
691 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
692 if (const Expr *Init = D->getInit())
693 return Visit(const_cast<Expr*>(Init));
694 }
695 }
696
Chris Lattner15e59112008-07-12 00:38:25 +0000697 // Otherwise, random variable references are not constants.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000698 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000699}
700
Chris Lattner1eee9402008-10-06 06:40:35 +0000701/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
702/// as GCC.
703static int EvaluateBuiltinClassifyType(const CallExpr *E) {
704 // The following enum mimics the values returned by GCC.
705 enum gcc_type_class {
706 no_type_class = -1,
707 void_type_class, integer_type_class, char_type_class,
708 enumeral_type_class, boolean_type_class,
709 pointer_type_class, reference_type_class, offset_type_class,
710 real_type_class, complex_type_class,
711 function_type_class, method_type_class,
712 record_type_class, union_type_class,
713 array_type_class, string_type_class,
714 lang_type_class
715 };
716
717 // If no argument was supplied, default to "no_type_class". This isn't
718 // ideal, however it is what gcc does.
719 if (E->getNumArgs() == 0)
720 return no_type_class;
721
722 QualType ArgTy = E->getArg(0)->getType();
723 if (ArgTy->isVoidType())
724 return void_type_class;
725 else if (ArgTy->isEnumeralType())
726 return enumeral_type_class;
727 else if (ArgTy->isBooleanType())
728 return boolean_type_class;
729 else if (ArgTy->isCharType())
730 return string_type_class; // gcc doesn't appear to use char_type_class
731 else if (ArgTy->isIntegerType())
732 return integer_type_class;
733 else if (ArgTy->isPointerType())
734 return pointer_type_class;
735 else if (ArgTy->isReferenceType())
736 return reference_type_class;
737 else if (ArgTy->isRealType())
738 return real_type_class;
739 else if (ArgTy->isComplexType())
740 return complex_type_class;
741 else if (ArgTy->isFunctionType())
742 return function_type_class;
743 else if (ArgTy->isStructureType())
744 return record_type_class;
745 else if (ArgTy->isUnionType())
746 return union_type_class;
747 else if (ArgTy->isArrayType())
748 return array_type_class;
749 else if (ArgTy->isUnionType())
750 return union_type_class;
751 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
752 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
753 return -1;
754}
755
Chris Lattner15e59112008-07-12 00:38:25 +0000756bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregorb5af7382009-02-14 18:57:46 +0000757 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner87293782008-10-06 05:28:25 +0000758 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000759 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner87293782008-10-06 05:28:25 +0000760 case Builtin::BI__builtin_classify_type:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000761 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner87293782008-10-06 05:28:25 +0000762
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000763 case Builtin::BI__builtin_constant_p:
Chris Lattner87293782008-10-06 05:28:25 +0000764 // __builtin_constant_p always has one operand: it returns true if that
765 // operand can be folded, false otherwise.
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000766 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner87293782008-10-06 05:28:25 +0000767 }
Chris Lattner15e59112008-07-12 00:38:25 +0000768}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000769
Chris Lattnera42f09a2008-07-11 19:10:17 +0000770bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000771 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000772 if (!Visit(E->getRHS()))
773 return false;
Anders Carlsson197f6f72008-12-01 06:44:05 +0000774
Eli Friedmanc092e032009-02-26 10:19:36 +0000775 // If we can't evaluate the LHS, it might have side effects;
776 // conservatively mark it.
777 if (!E->getLHS()->isEvaluatable(Info.Ctx))
778 Info.EvalResult.HasSideEffects = true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000779
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000780 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000781 }
782
783 if (E->isLogicalOp()) {
784 // These need to be handled specially because the operands aren't
785 // necessarily integral
Anders Carlsson501da1f2008-11-30 16:51:17 +0000786 bool lhsResult, rhsResult;
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000787
Anders Carlsson501da1f2008-11-30 16:51:17 +0000788 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000789 // We were able to evaluate the LHS, see if we can get away with not
790 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedmanc092e032009-02-26 10:19:36 +0000791 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000792 return Success(lhsResult, E);
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000793
Anders Carlsson501da1f2008-11-30 16:51:17 +0000794 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000795 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000796 return Success(lhsResult || rhsResult, E);
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000797 else
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000798 return Success(lhsResult && rhsResult, E);
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000799 }
800 } else {
Anders Carlsson501da1f2008-11-30 16:51:17 +0000801 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000802 // We can't evaluate the LHS; however, sometimes the result
803 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlsson501da1f2008-11-30 16:51:17 +0000804 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
805 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000806 // Since we weren't able to evaluate the left hand side, it
Anders Carlsson501da1f2008-11-30 16:51:17 +0000807 // must have had side effects.
808 Info.EvalResult.HasSideEffects = true;
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000809
810 return Success(rhsResult, E);
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000811 }
812 }
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000813 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000814
Eli Friedman14cc7542008-11-13 06:09:17 +0000815 return false;
816 }
817
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000818 QualType LHSTy = E->getLHS()->getType();
819 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbarf6060d62009-01-29 06:43:41 +0000820
821 if (LHSTy->isAnyComplexType()) {
822 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
823 APValue LHS, RHS;
824
825 if (!EvaluateComplex(E->getLHS(), LHS, Info))
826 return false;
827
828 if (!EvaluateComplex(E->getRHS(), RHS, Info))
829 return false;
830
831 if (LHS.isComplexFloat()) {
832 APFloat::cmpResult CR_r =
833 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
834 APFloat::cmpResult CR_i =
835 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
836
Daniel Dunbarf6060d62009-01-29 06:43:41 +0000837 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000838 return Success((CR_r == APFloat::cmpEqual &&
839 CR_i == APFloat::cmpEqual), E);
840 else {
841 assert(E->getOpcode() == BinaryOperator::NE &&
842 "Invalid complex comparison.");
843 return Success(((CR_r == APFloat::cmpGreaterThan ||
844 CR_r == APFloat::cmpLessThan) &&
845 (CR_i == APFloat::cmpGreaterThan ||
846 CR_i == APFloat::cmpLessThan)), E);
847 }
Daniel Dunbarf6060d62009-01-29 06:43:41 +0000848 } else {
Daniel Dunbarf6060d62009-01-29 06:43:41 +0000849 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000850 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
851 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
852 else {
853 assert(E->getOpcode() == BinaryOperator::NE &&
854 "Invalid compex comparison.");
855 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
856 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
857 }
Daniel Dunbarf6060d62009-01-29 06:43:41 +0000858 }
859 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000860
861 if (LHSTy->isRealFloatingType() &&
862 RHSTy->isRealFloatingType()) {
863 APFloat RHS(0.0), LHS(0.0);
864
865 if (!EvaluateFloat(E->getRHS(), RHS, Info))
866 return false;
867
868 if (!EvaluateFloat(E->getLHS(), LHS, Info))
869 return false;
870
871 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000872
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000873 switch (E->getOpcode()) {
874 default:
875 assert(0 && "Invalid binary operator!");
876 case BinaryOperator::LT:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000877 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000878 case BinaryOperator::GT:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000879 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000880 case BinaryOperator::LE:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000881 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000882 case BinaryOperator::GE:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000883 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
884 E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000885 case BinaryOperator::EQ:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000886 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000887 case BinaryOperator::NE:
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000888 return Success(CR == APFloat::cmpGreaterThan
889 || CR == APFloat::cmpLessThan, E);
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000890 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000891 }
892
Anders Carlsson027f2882008-11-16 19:01:22 +0000893 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000894 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson027f2882008-11-16 19:01:22 +0000895 APValue LHSValue;
896 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
897 return false;
898
899 APValue RHSValue;
900 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
901 return false;
902
903 // FIXME: Is this correct? What if only one of the operands has a base?
904 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
905 return false;
906
907 const QualType Type = E->getLHS()->getType();
908 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
909
910 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
911 D /= Info.Ctx.getTypeSize(ElementType) / 8;
912
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000913 return Success(D, E);
Anders Carlsson027f2882008-11-16 19:01:22 +0000914 }
915 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000916 if (!LHSTy->isIntegralType() ||
917 !RHSTy->isIntegralType()) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000918 // We can't continue from here for non-integral types, and they
919 // could potentially confuse the following operations.
920 // FIXME: Deal with EQ and friends.
921 return false;
922 }
923
Anders Carlssond1aa5812008-07-08 14:35:21 +0000924 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000925 if (!Visit(E->getLHS()))
Chris Lattner82437da2008-07-12 00:14:42 +0000926 return false; // error in subexpression.
Eli Friedman3e64dd72008-07-27 05:46:18 +0000927
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000928 // Only support arithmetic on integers for now.
929 if (!Result.isInt())
930 return false;
931
Daniel Dunbarf91896e2009-02-19 09:06:44 +0000932 llvm::APSInt RHS;
Chris Lattner82437da2008-07-12 00:14:42 +0000933 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000934 return false;
Eli Friedman14cc7542008-11-13 06:09:17 +0000935
Anders Carlssond1aa5812008-07-08 14:35:21 +0000936 switch (E->getOpcode()) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000937 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000938 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000939 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
940 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
941 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
942 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
943 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
944 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000945 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000946 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000947 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000948 return Success(Result.getInt() / RHS, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000949 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000950 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000951 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000952 return Success(Result.getInt() % RHS, E);
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000953 case BinaryOperator::Shl: {
Chris Lattner82437da2008-07-12 00:14:42 +0000954 // FIXME: Warn about out of range shift amounts!
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000955 unsigned SA =
956 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
957 return Success(Result.getInt() << SA, E);
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000958 }
959 case BinaryOperator::Shr: {
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000960 unsigned SA =
961 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
962 return Success(Result.getInt() >> SA, E);
Daniel Dunbar470c0b22009-02-19 18:37:50 +0000963 }
Chris Lattnera42f09a2008-07-11 19:10:17 +0000964
Daniel Dunbarf2dc6752009-02-19 20:17:33 +0000965 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
966 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
967 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
968 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
969 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
970 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000971 }
Anders Carlssond1aa5812008-07-08 14:35:21 +0000972}
973
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000974bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes308de752008-11-16 22:06:39 +0000975 bool Cond;
976 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000977 return false;
978
Nuno Lopes308de752008-11-16 22:06:39 +0000979 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000980}
981
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000982unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000983 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
984
985 // __alignof__(void) = 1 as a gcc extension.
986 if (Ty->isVoidType())
987 return 1;
988
989 // GCC extension: alignof(function) = 4.
990 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
991 // attribute(align) directive.
992 if (Ty->isFunctionType())
993 return 4;
994
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000995 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
996 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere3f61e12009-01-24 21:09:06 +0000997
998 // alignof VLA/incomplete array.
999 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
1000 return GetAlignOfType(VAT->getElementType());
1001
1002 // sizeof (objc class)?
1003 if (isa<ObjCInterfaceType>(Ty))
1004 return 1; // FIXME: This probably isn't right.
1005
1006 // Get information about the alignment.
1007 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanb1c0b542009-02-22 03:31:23 +00001008 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere3f61e12009-01-24 21:09:06 +00001009}
1010
Chris Lattnerbd3153e2009-01-24 21:53:27 +00001011unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1012 E = E->IgnoreParens();
1013
1014 // alignof decl is always accepted, even if it doesn't make sense: we default
1015 // to 1 in those cases.
1016 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +00001017 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Chris Lattnerbd3153e2009-01-24 21:53:27 +00001018
1019 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +00001020 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattnerbd3153e2009-01-24 21:53:27 +00001021
Chris Lattnere3f61e12009-01-24 21:09:06 +00001022 return GetAlignOfType(E->getType());
1023}
1024
1025
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001026/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1027/// expression's type.
1028bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1029 QualType DstTy = E->getType();
Chris Lattner265a0892008-07-11 21:24:13 +00001030
Chris Lattnere3f61e12009-01-24 21:09:06 +00001031 // Handle alignof separately.
1032 if (!E->isSizeOf()) {
1033 if (E->isArgumentType())
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001034 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere3f61e12009-01-24 21:09:06 +00001035 else
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001036 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere3f61e12009-01-24 21:09:06 +00001037 }
1038
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001039 QualType SrcTy = E->getTypeOfArgument();
1040
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001041 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1042 // extension.
1043 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1044 return Success(1, E);
Chris Lattner265a0892008-07-11 21:24:13 +00001045
1046 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere3f61e12009-01-24 21:09:06 +00001047 if (!SrcTy->isConstantSizeType())
Chris Lattner265a0892008-07-11 21:24:13 +00001048 return false;
Eli Friedman5a2c38f2009-01-24 22:19:05 +00001049
1050 if (SrcTy->isObjCInterfaceType()) {
1051 // Slightly unusual case: the size of an ObjC interface type is the
1052 // size of the class. This code intentionally falls through to the normal
1053 // case.
1054 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
1055 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
1056 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
1057 }
1058
Chris Lattnere3f61e12009-01-24 21:09:06 +00001059 // Get information about the size.
Chris Lattner422373c2008-07-11 22:52:41 +00001060 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001061 return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E);
Chris Lattner265a0892008-07-11 21:24:13 +00001062}
1063
Chris Lattnera42f09a2008-07-11 19:10:17 +00001064bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +00001065 // Special case unary operators that do not need their subexpression
1066 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman342d9432009-02-27 06:44:11 +00001067 if (E->isOffsetOfOp()) {
1068 // The AST for offsetof is defined in such a way that we can just
1069 // directly Evaluate it as an l-value.
1070 APValue LV;
1071 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1072 return false;
1073 if (LV.getLValueBase())
1074 return false;
1075 return Success(LV.getLValueOffset(), E);
1076 }
Eli Friedman14cc7542008-11-13 06:09:17 +00001077
1078 if (E->getOpcode() == UnaryOperator::LNot) {
1079 // LNot's operand isn't necessarily an integer, so we handle it specially.
1080 bool bres;
1081 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1082 return false;
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001083 return Success(!bres, E);
Eli Friedman14cc7542008-11-13 06:09:17 +00001084 }
1085
Daniel Dunbarb5c66db2009-02-21 18:14:20 +00001086 // Only handle integral operations...
1087 if (!E->getSubExpr()->getType()->isIntegralType())
1088 return false;
1089
Chris Lattner422373c2008-07-11 22:52:41 +00001090 // Get the operand value into 'Result'.
1091 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +00001092 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001093
Chris Lattner400d7402008-07-11 22:15:16 +00001094 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +00001095 default:
Chris Lattner400d7402008-07-11 22:15:16 +00001096 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1097 // See C99 6.6p3.
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001098 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner400d7402008-07-11 22:15:16 +00001099 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +00001100 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1101 // If so, we could clear the diagnostic ID.
Daniel Dunbar470c0b22009-02-19 18:37:50 +00001102 return true;
Chris Lattner400d7402008-07-11 22:15:16 +00001103 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +00001104 // The result is always just the subexpr.
Daniel Dunbar470c0b22009-02-19 18:37:50 +00001105 return true;
Chris Lattner400d7402008-07-11 22:15:16 +00001106 case UnaryOperator::Minus:
Daniel Dunbarf2dc6752009-02-19 20:17:33 +00001107 if (!Result.isInt()) return false;
1108 return Success(-Result.getInt(), E);
Chris Lattner400d7402008-07-11 22:15:16 +00001109 case UnaryOperator::Not:
Daniel Dunbarf2dc6752009-02-19 20:17:33 +00001110 if (!Result.isInt()) return false;
1111 return Success(~Result.getInt(), E);
Anders Carlssond1aa5812008-07-08 14:35:21 +00001112 }
Anders Carlssond1aa5812008-07-08 14:35:21 +00001113}
1114
Chris Lattnerff579ff2008-07-12 01:15:53 +00001115/// HandleCast - This is used to evaluate implicit or explicit casts where the
1116/// result type is integer.
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001117bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlssonfa76d822008-11-30 18:14:57 +00001118 Expr *SubExpr = E->getSubExpr();
1119 QualType DestType = E->getType();
Daniel Dunbar6dda4ba2009-02-19 22:16:29 +00001120 QualType SrcType = SubExpr->getType();
Anders Carlssonfa76d822008-11-30 18:14:57 +00001121
Eli Friedman7888b932008-11-12 09:44:48 +00001122 if (DestType->isBooleanType()) {
1123 bool BoolResult;
1124 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1125 return false;
Daniel Dunbarf91896e2009-02-19 09:06:44 +00001126 return Success(BoolResult, E);
Eli Friedman7888b932008-11-12 09:44:48 +00001127 }
1128
Anders Carlssond1aa5812008-07-08 14:35:21 +00001129 // Handle simple integer->integer casts.
Daniel Dunbar6dda4ba2009-02-19 22:16:29 +00001130 if (SrcType->isIntegralType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +00001131 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001132 return false;
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001133
Eli Friedman0366a812009-02-20 01:15:07 +00001134 if (!Result.isInt()) {
1135 // Only allow casts of lvalues if they are lossless.
1136 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1137 }
Daniel Dunbarf2dc6752009-02-19 20:17:33 +00001138
Daniel Dunbarc9967f92009-02-19 22:24:01 +00001139 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarf2dc6752009-02-19 20:17:33 +00001140 Result.getInt(), Info.Ctx), E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001141 }
1142
1143 // FIXME: Clean this up!
Daniel Dunbar6dda4ba2009-02-19 22:16:29 +00001144 if (SrcType->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +00001145 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +00001146 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001147 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001148
Daniel Dunbarc9967f92009-02-19 22:24:01 +00001149 if (LV.getLValueBase()) {
1150 // Only allow based lvalue casts if they are lossless.
1151 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1152 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001153
Daniel Dunbarc9967f92009-02-19 22:24:01 +00001154 Result = LV;
1155 return true;
1156 }
1157
1158 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1159 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson02a34c32008-07-08 14:30:00 +00001160 }
Eli Friedman7888b932008-11-12 09:44:48 +00001161
Eli Friedman0366a812009-02-20 01:15:07 +00001162 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1163 // This handles double-conversion cases, where there's both
1164 // an l-value promotion and an implicit conversion to int.
1165 APValue LV;
1166 if (!EvaluateLValue(SubExpr, LV, Info))
1167 return false;
1168
1169 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1170 return false;
1171
1172 Result = LV;
1173 return true;
1174 }
1175
Eli Friedman2207dec2009-02-22 11:46:18 +00001176 // FIXME: Handle complex types
1177 // FIXME: Handle vectors
1178
Daniel Dunbar6dda4ba2009-02-19 22:16:29 +00001179 if (!SrcType->isRealFloatingType())
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001180 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001181
Eli Friedman2f445492008-08-22 00:06:13 +00001182 APFloat F(0.0);
1183 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001184 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001185
Daniel Dunbar6dda4ba2009-02-19 22:16:29 +00001186 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssond1aa5812008-07-08 14:35:21 +00001187}
Anders Carlsson02a34c32008-07-08 14:30:00 +00001188
Eli Friedman25cb1b62009-02-27 04:45:43 +00001189bool IntExprEvaluator::VisitChooseExpr(const ChooseExpr *E) {
1190 Expr* EvalExpr = E->isConditionTrue(Info.Ctx) ? E->getLHS() : E->getRHS();
1191
1192 return Visit(EvalExpr);
1193}
1194
1195bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
1196 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1197 Info.EvalResult.HasSideEffects = true;
1198 return Success(0, E);
1199}
1200
Chris Lattnera823ccf2008-07-11 18:11:29 +00001201//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +00001202// Float Evaluation
1203//===----------------------------------------------------------------------===//
1204
1205namespace {
1206class VISIBILITY_HIDDEN FloatExprEvaluator
1207 : public StmtVisitor<FloatExprEvaluator, bool> {
1208 EvalInfo &Info;
1209 APFloat &Result;
1210public:
1211 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1212 : Info(info), Result(result) {}
1213
1214 bool VisitStmt(Stmt *S) {
1215 return false;
1216 }
1217
1218 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner87293782008-10-06 05:28:25 +00001219 bool VisitCallExpr(const CallExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001220
Daniel Dunbar804ead02008-10-16 03:51:50 +00001221 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001222 bool VisitBinaryOperator(const BinaryOperator *E);
1223 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman7888b932008-11-12 09:44:48 +00001224 bool VisitCastExpr(CastExpr *E);
1225 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2207dec2009-02-22 11:46:18 +00001226
1227 // FIXME: Missing: __real__/__imag__, __extension__,
1228 // array subscript of vector, member of vector,
1229 // __builtin_choose_expr, ImplicitValueInitExpr,
1230 // conditional ?:, comma
Eli Friedman2f445492008-08-22 00:06:13 +00001231};
1232} // end anonymous namespace
1233
1234static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1235 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1236}
1237
Chris Lattner87293782008-10-06 05:28:25 +00001238bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregorb5af7382009-02-14 18:57:46 +00001239 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner27cde262008-10-06 05:53:16 +00001240 default: return false;
Chris Lattner87293782008-10-06 05:28:25 +00001241 case Builtin::BI__builtin_huge_val:
1242 case Builtin::BI__builtin_huge_valf:
1243 case Builtin::BI__builtin_huge_vall:
1244 case Builtin::BI__builtin_inf:
1245 case Builtin::BI__builtin_inff:
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001246 case Builtin::BI__builtin_infl: {
1247 const llvm::fltSemantics &Sem =
1248 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner27cde262008-10-06 05:53:16 +00001249 Result = llvm::APFloat::getInf(Sem);
1250 return true;
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001251 }
Chris Lattner667e1ee2008-10-06 06:31:58 +00001252
1253 case Builtin::BI__builtin_nan:
1254 case Builtin::BI__builtin_nanf:
1255 case Builtin::BI__builtin_nanl:
1256 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1257 // can't constant fold it.
1258 if (const StringLiteral *S =
1259 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1260 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001261 const llvm::fltSemantics &Sem =
1262 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner667e1ee2008-10-06 06:31:58 +00001263 Result = llvm::APFloat::getNaN(Sem);
1264 return true;
1265 }
1266 }
1267 return false;
Daniel Dunbar804ead02008-10-16 03:51:50 +00001268
1269 case Builtin::BI__builtin_fabs:
1270 case Builtin::BI__builtin_fabsf:
1271 case Builtin::BI__builtin_fabsl:
1272 if (!EvaluateFloat(E->getArg(0), Result, Info))
1273 return false;
1274
1275 if (Result.isNegative())
1276 Result.changeSign();
1277 return true;
1278
1279 case Builtin::BI__builtin_copysign:
1280 case Builtin::BI__builtin_copysignf:
1281 case Builtin::BI__builtin_copysignl: {
1282 APFloat RHS(0.);
1283 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1284 !EvaluateFloat(E->getArg(1), RHS, Info))
1285 return false;
1286 Result.copySign(RHS);
1287 return true;
1288 }
Chris Lattner87293782008-10-06 05:28:25 +00001289 }
1290}
1291
Daniel Dunbar804ead02008-10-16 03:51:50 +00001292bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes1cea4f42008-11-19 17:44:31 +00001293 if (E->getOpcode() == UnaryOperator::Deref)
1294 return false;
1295
Daniel Dunbar804ead02008-10-16 03:51:50 +00001296 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1297 return false;
1298
1299 switch (E->getOpcode()) {
1300 default: return false;
1301 case UnaryOperator::Plus:
1302 return true;
1303 case UnaryOperator::Minus:
1304 Result.changeSign();
1305 return true;
1306 }
1307}
Chris Lattner87293782008-10-06 05:28:25 +00001308
Eli Friedman2f445492008-08-22 00:06:13 +00001309bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1310 // FIXME: Diagnostics? I really don't understand how the warnings
1311 // and errors are supposed to work.
Daniel Dunbar804ead02008-10-16 03:51:50 +00001312 APFloat RHS(0.0);
Eli Friedman2f445492008-08-22 00:06:13 +00001313 if (!EvaluateFloat(E->getLHS(), Result, Info))
1314 return false;
1315 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1316 return false;
1317
1318 switch (E->getOpcode()) {
1319 default: return false;
1320 case BinaryOperator::Mul:
1321 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1322 return true;
1323 case BinaryOperator::Add:
1324 Result.add(RHS, APFloat::rmNearestTiesToEven);
1325 return true;
1326 case BinaryOperator::Sub:
1327 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1328 return true;
1329 case BinaryOperator::Div:
1330 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1331 return true;
Eli Friedman2f445492008-08-22 00:06:13 +00001332 }
1333}
1334
1335bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1336 Result = E->getValue();
1337 return true;
1338}
1339
Eli Friedman7888b932008-11-12 09:44:48 +00001340bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1341 Expr* SubExpr = E->getSubExpr();
Nate Begemand6d2f772009-01-18 03:20:47 +00001342
Eli Friedman7888b932008-11-12 09:44:48 +00001343 if (SubExpr->getType()->isIntegralType()) {
1344 APSInt IntResult;
Daniel Dunbar470c0b22009-02-19 18:37:50 +00001345 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman7888b932008-11-12 09:44:48 +00001346 return false;
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001347 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1348 IntResult, Info.Ctx);
Eli Friedman7888b932008-11-12 09:44:48 +00001349 return true;
1350 }
1351 if (SubExpr->getType()->isRealFloatingType()) {
1352 if (!Visit(SubExpr))
1353 return false;
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001354 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1355 Result, Info.Ctx);
Eli Friedman7888b932008-11-12 09:44:48 +00001356 return true;
1357 }
Eli Friedman2207dec2009-02-22 11:46:18 +00001358 // FIXME: Handle complex types
Eli Friedman7888b932008-11-12 09:44:48 +00001359
1360 return false;
1361}
1362
1363bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1364 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1365 return true;
1366}
1367
Eli Friedman2f445492008-08-22 00:06:13 +00001368//===----------------------------------------------------------------------===//
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001369// Complex Evaluation (for float and integer)
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001370//===----------------------------------------------------------------------===//
1371
1372namespace {
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001373class VISIBILITY_HIDDEN ComplexExprEvaluator
1374 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001375 EvalInfo &Info;
1376
1377public:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001378 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001379
1380 //===--------------------------------------------------------------------===//
1381 // Visitor Methods
1382 //===--------------------------------------------------------------------===//
1383
1384 APValue VisitStmt(Stmt *S) {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001385 return APValue();
1386 }
1387
1388 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1389
1390 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001391 Expr* SubExpr = E->getSubExpr();
1392
1393 if (SubExpr->getType()->isRealFloatingType()) {
1394 APFloat Result(0.0);
1395
1396 if (!EvaluateFloat(SubExpr, Result, Info))
1397 return APValue();
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001398
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001399 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001400 Result);
1401 } else {
1402 assert(SubExpr->getType()->isIntegerType() &&
1403 "Unexpected imaginary literal.");
1404
1405 llvm::APSInt Result;
1406 if (!EvaluateInteger(SubExpr, Result, Info))
1407 return APValue();
1408
1409 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1410 Zero = 0;
1411 return APValue(Zero, Result);
1412 }
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001413 }
1414
Anders Carlssonad2794c2008-11-16 21:51:21 +00001415 APValue VisitCastExpr(CastExpr *E) {
1416 Expr* SubExpr = E->getSubExpr();
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001417 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1418 QualType SubType = SubExpr->getType();
Anders Carlssonad2794c2008-11-16 21:51:21 +00001419
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001420 if (SubType->isRealFloatingType()) {
Anders Carlssonad2794c2008-11-16 21:51:21 +00001421 APFloat Result(0.0);
1422
1423 if (!EvaluateFloat(SubExpr, Result, Info))
1424 return APValue();
1425
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001426 // Apply float conversion if necessary.
1427 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001428 return APValue(Result,
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001429 APFloat(Result.getSemantics(), APFloat::fcZero, false));
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001430 } else if (SubType->isIntegerType()) {
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001431 APSInt Result;
1432
1433 if (!EvaluateInteger(SubExpr, Result, Info))
1434 return APValue();
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001435
1436 // Apply integer conversion if necessary.
1437 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001438 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1439 Zero = 0;
1440 return APValue(Result, Zero);
Daniel Dunbaraffa0e32009-01-29 06:16:07 +00001441 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1442 APValue Src;
1443
1444 if (!EvaluateComplex(SubExpr, Src, Info))
1445 return APValue();
1446
1447 QualType SrcType = CT->getElementType();
1448
1449 if (Src.isComplexFloat()) {
1450 if (EltType->isRealFloatingType()) {
1451 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1452 Src.getComplexFloatReal(),
1453 Info.Ctx),
1454 HandleFloatToFloatCast(EltType, SrcType,
1455 Src.getComplexFloatImag(),
1456 Info.Ctx));
1457 } else {
1458 return APValue(HandleFloatToIntCast(EltType, SrcType,
1459 Src.getComplexFloatReal(),
1460 Info.Ctx),
1461 HandleFloatToIntCast(EltType, SrcType,
1462 Src.getComplexFloatImag(),
1463 Info.Ctx));
1464 }
1465 } else {
1466 assert(Src.isComplexInt() && "Invalid evaluate result.");
1467 if (EltType->isRealFloatingType()) {
1468 return APValue(HandleIntToFloatCast(EltType, SrcType,
1469 Src.getComplexIntReal(),
1470 Info.Ctx),
1471 HandleIntToFloatCast(EltType, SrcType,
1472 Src.getComplexIntImag(),
1473 Info.Ctx));
1474 } else {
1475 return APValue(HandleIntToIntCast(EltType, SrcType,
1476 Src.getComplexIntReal(),
1477 Info.Ctx),
1478 HandleIntToIntCast(EltType, SrcType,
1479 Src.getComplexIntImag(),
1480 Info.Ctx));
1481 }
1482 }
Anders Carlssonad2794c2008-11-16 21:51:21 +00001483 }
1484
1485 // FIXME: Handle more casts.
1486 return APValue();
1487 }
1488
1489 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman2207dec2009-02-22 11:46:18 +00001490 // FIXME Missing: unary +/-/~, __extension__, binary div,
1491 // __builtin_choose_expr, ImplicitValueInitExpr,
1492 // conditional ?:, comma
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001493};
1494} // end anonymous namespace
1495
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001496static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001497{
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001498 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1499 assert((!Result.isComplexFloat() ||
1500 (&Result.getComplexFloatReal().getSemantics() ==
1501 &Result.getComplexFloatImag().getSemantics())) &&
1502 "Invalid complex evaluation.");
1503 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001504}
1505
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001506APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonad2794c2008-11-16 21:51:21 +00001507{
1508 APValue Result, RHS;
1509
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001510 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonad2794c2008-11-16 21:51:21 +00001511 return APValue();
1512
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001513 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonad2794c2008-11-16 21:51:21 +00001514 return APValue();
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001515
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001516 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1517 "Invalid operands to binary operator.");
Anders Carlssonad2794c2008-11-16 21:51:21 +00001518 switch (E->getOpcode()) {
1519 default: return APValue();
1520 case BinaryOperator::Add:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001521 if (Result.isComplexFloat()) {
1522 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1523 APFloat::rmNearestTiesToEven);
1524 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1525 APFloat::rmNearestTiesToEven);
1526 } else {
1527 Result.getComplexIntReal() += RHS.getComplexIntReal();
1528 Result.getComplexIntImag() += RHS.getComplexIntImag();
1529 }
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001530 break;
Anders Carlssonad2794c2008-11-16 21:51:21 +00001531 case BinaryOperator::Sub:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001532 if (Result.isComplexFloat()) {
1533 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1534 APFloat::rmNearestTiesToEven);
1535 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1536 APFloat::rmNearestTiesToEven);
1537 } else {
1538 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1539 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1540 }
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001541 break;
1542 case BinaryOperator::Mul:
1543 if (Result.isComplexFloat()) {
1544 APValue LHS = Result;
1545 APFloat &LHS_r = LHS.getComplexFloatReal();
1546 APFloat &LHS_i = LHS.getComplexFloatImag();
1547 APFloat &RHS_r = RHS.getComplexFloatReal();
1548 APFloat &RHS_i = RHS.getComplexFloatImag();
1549
1550 APFloat Tmp = LHS_r;
1551 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1552 Result.getComplexFloatReal() = Tmp;
1553 Tmp = LHS_i;
1554 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1555 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1556
1557 Tmp = LHS_r;
1558 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1559 Result.getComplexFloatImag() = Tmp;
1560 Tmp = LHS_i;
1561 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1562 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1563 } else {
1564 APValue LHS = Result;
1565 Result.getComplexIntReal() =
1566 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1567 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1568 Result.getComplexIntImag() =
1569 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1570 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1571 }
1572 break;
Anders Carlssonad2794c2008-11-16 21:51:21 +00001573 }
1574
1575 return Result;
1576}
1577
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001578//===----------------------------------------------------------------------===//
Chris Lattneref069662008-11-16 21:24:15 +00001579// Top level Expr::Evaluate method.
Chris Lattnera823ccf2008-07-11 18:11:29 +00001580//===----------------------------------------------------------------------===//
1581
Chris Lattneref069662008-11-16 21:24:15 +00001582/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner87293782008-10-06 05:28:25 +00001583/// any crazy technique (that has nothing to do with language standards) that
1584/// we want to. If this function returns true, it returns the folded constant
1585/// in Result.
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001586bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1587 EvalInfo Info(Ctx, Result);
Anders Carlssondd8d41f2008-11-30 16:38:33 +00001588
Nate Begemand6d2f772009-01-18 03:20:47 +00001589 if (getType()->isVectorType()) {
1590 if (!EvaluateVector(this, Result.Val, Info))
1591 return false;
1592 } else if (getType()->isIntegerType()) {
Daniel Dunbarf2dc6752009-02-19 20:17:33 +00001593 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001594 return false;
Daniel Dunbarfc096bf2009-02-26 20:52:22 +00001595 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001596 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001597 return false;
Eli Friedman2f445492008-08-22 00:06:13 +00001598 } else if (getType()->isRealFloatingType()) {
1599 llvm::APFloat f(0.0);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001600 if (!EvaluateFloat(this, f, Info))
1601 return false;
1602
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001603 Result.Val = APValue(f);
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001604 } else if (getType()->isAnyComplexType()) {
1605 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001606 return false;
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001607 } else
Anders Carlssoncb6a2e82008-11-22 22:56:32 +00001608 return false;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001609
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001610 return true;
1611}
1612
Chris Lattneref069662008-11-16 21:24:15 +00001613/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001614/// folded, but discard the result.
1615bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson197f6f72008-12-01 06:44:05 +00001616 EvalResult Result;
1617 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001618}
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001619
1620APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson8c3de802008-12-19 20:58:05 +00001621 EvalResult EvalResult;
1622 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarddebeca2009-01-15 18:32:35 +00001623 Result = Result;
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001624 assert(Result && "Could not evaluate expression");
Anders Carlsson8c3de802008-12-19 20:58:05 +00001625 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001626
Anders Carlsson8c3de802008-12-19 20:58:05 +00001627 return EvalResult.Val.getInt();
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001628}