blob: c2449166fc68da3faaed19b4c84b557aca818dc1 [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;
Eli Friedmana1f47c42009-03-23 04:38:34 +000074 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000075 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;
Eli Friedmana1f47c42009-03-23 04:38:34 +000082 } else if (E->getType()->isAnyComplexType()) {
83 APValue ComplexResult;
84 if (!EvaluateComplex(E, ComplexResult, Info))
85 return false;
86 if (ComplexResult.isComplexFloat()) {
87 Result = !ComplexResult.getComplexFloatReal().isZero() ||
88 !ComplexResult.getComplexFloatImag().isZero();
89 } else {
90 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
91 ComplexResult.getComplexIntImag().getBoolValue();
92 }
93 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +000094 }
95
96 return false;
97}
98
Daniel Dunbara2cfd342009-01-29 06:16:07 +000099static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
100 APFloat &Value, ASTContext &Ctx) {
101 unsigned DestWidth = Ctx.getIntWidth(DestType);
102 // Determine whether we are converting to unsigned or signed.
103 bool DestSigned = DestType->isSignedIntegerType();
104
105 // FIXME: Warning for overflow.
106 uint64_t Space[4];
107 bool ignored;
108 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
109 llvm::APFloat::rmTowardZero, &ignored);
110 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
111}
112
113static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
114 APFloat &Value, ASTContext &Ctx) {
115 bool ignored;
116 APFloat Result = Value;
117 Result.convert(Ctx.getFloatTypeSemantics(DestType),
118 APFloat::rmNearestTiesToEven, &ignored);
119 return Result;
120}
121
122static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
123 APSInt &Value, ASTContext &Ctx) {
124 unsigned DestWidth = Ctx.getIntWidth(DestType);
125 APSInt Result = Value;
126 // Figure out if this is a truncate, extend or noop cast.
127 // If the input is signed, do a sign extend, noop, or truncate.
128 Result.extOrTrunc(DestWidth);
129 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
130 return Result;
131}
132
133static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
134 APSInt &Value, ASTContext &Ctx) {
135
136 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
137 Result.convertFromAPInt(Value, Value.isSigned(),
138 APFloat::rmNearestTiesToEven);
139 return Result;
140}
141
Eli Friedman4efaa272008-11-12 09:44:48 +0000142//===----------------------------------------------------------------------===//
143// LValue Evaluation
144//===----------------------------------------------------------------------===//
145namespace {
146class VISIBILITY_HIDDEN LValueExprEvaluator
147 : public StmtVisitor<LValueExprEvaluator, APValue> {
148 EvalInfo &Info;
149public:
150
151 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
152
153 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000154 return APValue();
155 }
156
157 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000158 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000159 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
160 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
161 APValue VisitMemberExpr(MemberExpr *E);
162 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000163 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000164 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000165 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000166 APValue VisitUnaryExtension(const UnaryOperator *E)
167 { return Visit(E->getSubExpr()); }
168 APValue VisitChooseExpr(const ChooseExpr *E)
169 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
170 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000171};
172} // end anonymous namespace
173
174static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
175 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
176 return Result.isLValue();
177}
178
Anders Carlsson35873c42008-11-24 04:41:22 +0000179APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
180{
181 if (!E->hasGlobalStorage())
182 return APValue();
183
184 return APValue(E, 0);
185}
186
Eli Friedman4efaa272008-11-12 09:44:48 +0000187APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
188 if (E->isFileScope())
189 return APValue(E, 0);
190 return APValue();
191}
192
193APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
194 APValue result;
195 QualType Ty;
196 if (E->isArrow()) {
197 if (!EvaluatePointer(E->getBase(), result, Info))
198 return APValue();
199 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
200 } else {
201 result = Visit(E->getBase());
202 if (result.isUninit())
203 return APValue();
204 Ty = E->getBase()->getType();
205 }
206
207 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
208 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000209
210 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
211 if (!FD) // FIXME: deal with other kinds of member expressions
212 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000213
214 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000215 unsigned i = 0;
216 for (RecordDecl::field_iterator Field = RD->field_begin(),
217 FieldEnd = RD->field_end();
218 Field != FieldEnd; (void)++Field, ++i) {
219 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000220 break;
221 }
222
223 result.setLValue(result.getLValueBase(),
224 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
225
226 return result;
227}
228
Anders Carlsson3068d112008-11-16 19:01:22 +0000229APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
230{
231 APValue Result;
232
233 if (!EvaluatePointer(E->getBase(), Result, Info))
234 return APValue();
235
236 APSInt Index;
237 if (!EvaluateInteger(E->getIdx(), Index, Info))
238 return APValue();
239
240 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
241
242 uint64_t Offset = Index.getSExtValue() * ElementSize;
243 Result.setLValue(Result.getLValueBase(),
244 Result.getLValueOffset() + Offset);
245 return Result;
246}
Eli Friedman4efaa272008-11-12 09:44:48 +0000247
Eli Friedmane8761c82009-02-20 01:57:15 +0000248APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E)
249{
250 APValue Result;
251 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
252 return APValue();
253 return Result;
254}
255
Eli Friedman4efaa272008-11-12 09:44:48 +0000256//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000257// Pointer Evaluation
258//===----------------------------------------------------------------------===//
259
Anders Carlssonc754aa62008-07-08 05:13:58 +0000260namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000261class VISIBILITY_HIDDEN PointerExprEvaluator
262 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000263 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000264public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000265
Chris Lattner87eae5e2008-07-11 22:52:41 +0000266 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000267
Anders Carlsson2bad1682008-07-08 14:30:00 +0000268 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000269 return APValue();
270 }
271
272 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
273
Anders Carlsson650c92f2008-07-08 15:34:11 +0000274 APValue VisitBinaryOperator(const BinaryOperator *E);
275 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000276 APValue VisitUnaryExtension(const UnaryOperator *E)
277 { return Visit(E->getSubExpr()); }
278 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000279 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
280 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000281 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
282 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000283 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000284 APValue VisitBlockExpr(BlockExpr *E) {
285 if (!E->hasBlockDeclRefExprs())
286 return APValue(E, 0);
287 return APValue();
288 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000289 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
290 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000291 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000292 APValue VisitChooseExpr(ChooseExpr *E)
293 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
294 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000295};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000296} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000297
Chris Lattner87eae5e2008-07-11 22:52:41 +0000298static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000299 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000300 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000301 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000302 return Result.isLValue();
303}
304
305APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
306 if (E->getOpcode() != BinaryOperator::Add &&
307 E->getOpcode() != BinaryOperator::Sub)
308 return APValue();
309
310 const Expr *PExp = E->getLHS();
311 const Expr *IExp = E->getRHS();
312 if (IExp->getType()->isPointerType())
313 std::swap(PExp, IExp);
314
315 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000316 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000317 return APValue();
318
319 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000320 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000321 return APValue();
322
Eli Friedman4efaa272008-11-12 09:44:48 +0000323 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000324 uint64_t SizeOfPointee;
325
326 // Explicitly handle GNU void* and function pointer arithmetic extensions.
327 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
328 SizeOfPointee = 1;
329 else
330 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000331
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000332 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000333
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000334 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000335 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000336 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000337 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
338
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000339 return APValue(ResultLValue.getLValueBase(), Offset);
340}
Eli Friedman4efaa272008-11-12 09:44:48 +0000341
Eli Friedman2217c872009-02-22 11:46:18 +0000342APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
343 APValue result;
344 if (EvaluateLValue(E->getSubExpr(), result, Info))
345 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000346 return APValue();
347}
Anders Carlssond407a762008-12-05 05:24:13 +0000348
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000349
Chris Lattnerb542afe2008-07-11 19:10:17 +0000350APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000351 const Expr* SubExpr = E->getSubExpr();
352
353 // Check for pointer->pointer cast
354 if (SubExpr->getType()->isPointerType()) {
355 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000356 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000357 return Result;
358 return APValue();
359 }
360
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000361 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000362 APValue Result;
363 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
364 return APValue();
365
366 if (Result.isInt()) {
367 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
368 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000369 }
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000370
371 // Cast is of an lvalue, no need to change value.
372 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000373 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000374
375 if (SubExpr->getType()->isFunctionType() ||
376 SubExpr->getType()->isArrayType()) {
377 APValue Result;
378 if (EvaluateLValue(SubExpr, Result, Info))
379 return Result;
380 return APValue();
381 }
382
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000383 return APValue();
384}
385
Eli Friedman3941b182009-01-25 01:54:01 +0000386APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000387 if (E->isBuiltinCall(Info.Ctx) ==
388 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000389 return APValue(E, 0);
390 return APValue();
391}
392
Eli Friedman4efaa272008-11-12 09:44:48 +0000393APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
394 bool BoolResult;
395 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
396 return APValue();
397
398 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
399
400 APValue Result;
401 if (EvaluatePointer(EvalExpr, Result, Info))
402 return Result;
403 return APValue();
404}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000405
406//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000407// Vector Evaluation
408//===----------------------------------------------------------------------===//
409
410namespace {
411 class VISIBILITY_HIDDEN VectorExprEvaluator
412 : public StmtVisitor<VectorExprEvaluator, APValue> {
413 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000414 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000415 public:
416
417 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
418
419 APValue VisitStmt(Stmt *S) {
420 return APValue();
421 }
422
Eli Friedman91110ee2009-02-23 04:23:56 +0000423 APValue VisitParenExpr(ParenExpr *E)
424 { return Visit(E->getSubExpr()); }
425 APValue VisitUnaryExtension(const UnaryOperator *E)
426 { return Visit(E->getSubExpr()); }
427 APValue VisitUnaryPlus(const UnaryOperator *E)
428 { return Visit(E->getSubExpr()); }
429 APValue VisitUnaryReal(const UnaryOperator *E)
430 { return Visit(E->getSubExpr()); }
431 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
432 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000433 APValue VisitCastExpr(const CastExpr* E);
434 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
435 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000436 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000437 APValue VisitChooseExpr(const ChooseExpr *E)
438 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000439 APValue VisitUnaryImag(const UnaryOperator *E);
440 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000441 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000442 // shufflevector, ExtVectorElementExpr
443 // (Note that these require implementing conversions
444 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000445 };
446} // end anonymous namespace
447
448static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
449 if (!E->getType()->isVectorType())
450 return false;
451 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
452 return !Result.isUninit();
453}
454
455APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
456 const Expr* SE = E->getSubExpr();
457
458 // Check for vector->vector bitcast.
459 if (SE->getType()->isVectorType())
460 return this->Visit(const_cast<Expr*>(SE));
461
462 return APValue();
463}
464
465APValue
466VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
467 return this->Visit(const_cast<Expr*>(E->getInitializer()));
468}
469
470APValue
471VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
472 const VectorType *VT = E->getType()->getAsVectorType();
473 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000474 unsigned NumElements = VT->getNumElements();
Nate Begeman59b5da62009-01-18 03:20:47 +0000475
476 QualType EltTy = VT->getElementType();
477 llvm::SmallVector<APValue, 4> Elements;
478
Eli Friedman91110ee2009-02-23 04:23:56 +0000479 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000480 if (EltTy->isIntegerType()) {
481 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000482 if (i < NumInits) {
483 if (!EvaluateInteger(E->getInit(i), sInt, Info))
484 return APValue();
485 } else {
486 sInt = Info.Ctx.MakeIntValue(0, EltTy);
487 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000488 Elements.push_back(APValue(sInt));
489 } else {
490 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000491 if (i < NumInits) {
492 if (!EvaluateFloat(E->getInit(i), f, Info))
493 return APValue();
494 } else {
495 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
496 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000497 Elements.push_back(APValue(f));
498 }
499 }
500 return APValue(&Elements[0], Elements.size());
501}
502
Eli Friedman91110ee2009-02-23 04:23:56 +0000503APValue
504VectorExprEvaluator::GetZeroVector(QualType T) {
505 const VectorType *VT = T->getAsVectorType();
506 QualType EltTy = VT->getElementType();
507 APValue ZeroElement;
508 if (EltTy->isIntegerType())
509 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
510 else
511 ZeroElement =
512 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
513
514 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
515 return APValue(&Elements[0], Elements.size());
516}
517
518APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
519 bool BoolResult;
520 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
521 return APValue();
522
523 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
524
525 APValue Result;
526 if (EvaluateVector(EvalExpr, Result, Info))
527 return Result;
528 return APValue();
529}
530
Eli Friedman91110ee2009-02-23 04:23:56 +0000531APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
532 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
533 Info.EvalResult.HasSideEffects = true;
534 return GetZeroVector(E->getType());
535}
536
Nate Begeman59b5da62009-01-18 03:20:47 +0000537//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000538// Integer Evaluation
539//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000540
541namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000542class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000543 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000544 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000545 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000546public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000547 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000548 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000549
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000550 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000551 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000552 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000553 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000554 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000555 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000556 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000557 return true;
558 }
559
Daniel Dunbar131eb432009-02-19 09:06:44 +0000560 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000561 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000562 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000563 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000564 Result = APValue(APSInt(I));
565 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000566 return true;
567 }
568
569 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000570 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000571 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000572 return true;
573 }
574
Anders Carlsson82206e22008-11-30 18:14:57 +0000575 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000576 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000577 if (Info.EvalResult.Diag == 0) {
578 Info.EvalResult.DiagLoc = L;
579 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000580 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000581 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000582 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000583 }
584
Anders Carlssonc754aa62008-07-08 05:13:58 +0000585 //===--------------------------------------------------------------------===//
586 // Visitor Methods
587 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000588
589 bool VisitStmt(Stmt *) {
590 assert(0 && "This should be called on integers, stmts are not integers");
591 return false;
592 }
Chris Lattner7a767782008-07-11 19:24:49 +0000593
Chris Lattner32fea9d2008-11-12 07:43:42 +0000594 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000595 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000596 }
597
Chris Lattnerb542afe2008-07-11 19:10:17 +0000598 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000599
Chris Lattner4c4867e2008-07-12 00:38:25 +0000600 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000601 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000602 }
603 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000604 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000605 }
606 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000607 // Per gcc docs "this built-in function ignores top level
608 // qualifiers". We need to use the canonical version to properly
609 // be able to strip CRV qualifiers from the type.
610 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
611 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000612 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
613 T1.getUnqualifiedType()),
614 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000615 }
616 bool VisitDeclRefExpr(const DeclRefExpr *E);
617 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000618 bool VisitBinaryOperator(const BinaryOperator *E);
619 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000620 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000621
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000622 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000623 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
624
Anders Carlsson3068d112008-11-16 19:01:22 +0000625 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000626 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000627 }
628
Anders Carlsson3f704562008-12-21 22:39:40 +0000629 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000630 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000631 }
632
Anders Carlsson3068d112008-11-16 19:01:22 +0000633 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000634 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000635 }
636
Eli Friedman664a1042009-02-27 04:45:43 +0000637 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
638 return Success(0, E);
639 }
640
Sebastian Redl64b45f72009-01-05 20:52:13 +0000641 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000642 return Success(E->EvaluateTrait(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000643 }
644
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000645 bool VisitChooseExpr(const ChooseExpr *E) {
646 return Visit(E->getChosenSubExpr(Info.Ctx));
647 }
648
Eli Friedman722c7172009-02-28 03:59:05 +0000649 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000650 bool VisitUnaryImag(const UnaryOperator *E);
651
Chris Lattnerfcee0012008-07-11 21:24:13 +0000652private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000653 unsigned GetAlignOfExpr(const Expr *E);
654 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000655 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000656};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000657} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000658
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000659static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000660 if (!E->getType()->isIntegralType())
661 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000662
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000663 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
664}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000665
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000666static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
667 APValue Val;
668 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
669 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000670 Result = Val.getInt();
671 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000672}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000673
Chris Lattner4c4867e2008-07-12 00:38:25 +0000674bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
675 // Enums are integer constant exprs.
676 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000677 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000678 // signedness consistently; see PR3173.
679 APSInt SI = D->getInitVal();
680 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
681 // FIXME: This is an ugly hack around the fact that enums don't
682 // set their width (!?!) consistently; see PR3173.
683 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
684 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000685 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000686
687 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
688 if (Info.Ctx.getLangOptions().CPlusPlus &&
689 E->getType().getCVRQualifiers() == QualType::Const) {
690 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
691 if (const Expr *Init = D->getInit())
692 return Visit(const_cast<Expr*>(Init));
693 }
694 }
695
Chris Lattner4c4867e2008-07-12 00:38:25 +0000696 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000697 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000698}
699
Chris Lattnera4d55d82008-10-06 06:40:35 +0000700/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
701/// as GCC.
702static int EvaluateBuiltinClassifyType(const CallExpr *E) {
703 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000704 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000705 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 Lattner4c4867e2008-07-12 00:38:25 +0000756bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000757 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000758 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000759 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000760 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000761 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000762
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000763 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-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 Dunbar131eb432009-02-19 09:06:44 +0000766 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000767 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000768}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000769
Chris Lattnerb542afe2008-07-11 19:10:17 +0000770bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000771 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000772 if (!Visit(E->getRHS()))
773 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000774
Eli Friedman33ef1452009-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 Friedmana6afa762008-11-13 06:09:17 +0000779
Anders Carlsson027f62e2008-12-01 02:07:06 +0000780 return true;
Eli Friedmana6afa762008-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 Carlssonfcb4d092008-11-30 16:51:17 +0000786 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000787
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000788 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-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 Friedman33ef1452009-02-26 10:19:36 +0000791 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000792 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000793
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000794 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000795 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000796 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000797 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000798 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000799 }
800 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000801 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-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 Carlssonfcb4d092008-11-30 16:51:17 +0000804 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
805 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000806 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000807 // must have had side effects.
808 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000809
810 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000811 }
812 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000813 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000814
Eli Friedmana6afa762008-11-13 06:09:17 +0000815 return false;
816 }
817
Anders Carlsson286f85e2008-11-16 07:17:21 +0000818 QualType LHSTy = E->getLHS()->getType();
819 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-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 Dunbar4087e242009-01-29 06:43:41 +0000837 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-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 Dunbar4087e242009-01-29 06:43:41 +0000848 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000849 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-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 Dunbar4087e242009-01-29 06:43:41 +0000858 }
859 }
Anders Carlsson286f85e2008-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 Carlsson529569e2008-11-16 22:46:56 +0000872
Anders Carlsson286f85e2008-11-16 07:17:21 +0000873 switch (E->getOpcode()) {
874 default:
875 assert(0 && "Invalid binary operator!");
876 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000877 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000878 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000879 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000880 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000881 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000882 case BinaryOperator::GE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000883 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
884 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000885 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000886 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000887 case BinaryOperator::NE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000888 return Success(CR == APFloat::cmpGreaterThan
889 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000890 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000891 }
892
Anders Carlsson3068d112008-11-16 19:01:22 +0000893 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000894 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000895 APValue LHSValue;
896 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
897 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000898
Anders Carlsson3068d112008-11-16 19:01:22 +0000899 APValue RHSValue;
900 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
901 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000902
903 // Reject any bases; this is conservative, but good enough for
904 // common uses
Anders Carlsson3068d112008-11-16 19:01:22 +0000905 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
906 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000907
Anders Carlsson3068d112008-11-16 19:01:22 +0000908 const QualType Type = E->getLHS()->getType();
909 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
910
911 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
912 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000913
Daniel Dunbar131eb432009-02-19 09:06:44 +0000914 return Success(D, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000915 }
916 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000917 if (!LHSTy->isIntegralType() ||
918 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000919 // We can't continue from here for non-integral types, and they
920 // could potentially confuse the following operations.
921 // FIXME: Deal with EQ and friends.
922 return false;
923 }
924
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000925 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000926 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +0000927 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000928
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000929 // Only support arithmetic on integers for now.
930 if (!Result.isInt())
931 return false;
932
Daniel Dunbar131eb432009-02-19 09:06:44 +0000933 llvm::APSInt RHS;
Chris Lattner54176fd2008-07-12 00:14:42 +0000934 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000935 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000936
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000937 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000938 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000939 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000940 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
941 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
942 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
943 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
944 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
945 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000946 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000947 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000948 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000949 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000950 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000951 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000952 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000953 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000954 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +0000955 // FIXME: Warn about out of range shift amounts!
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000956 unsigned SA =
957 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
958 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000959 }
960 case BinaryOperator::Shr: {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000961 unsigned SA =
962 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
963 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000964 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000965
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000966 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
967 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
968 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
969 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
970 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
971 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +0000972 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000973}
974
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000975bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +0000976 bool Cond;
977 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000978 return false;
979
Nuno Lopesa25bd552008-11-16 22:06:39 +0000980 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000981}
982
Chris Lattneraf707ab2009-01-24 21:53:27 +0000983unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +0000984 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
985
986 // __alignof__(void) = 1 as a gcc extension.
987 if (Ty->isVoidType())
988 return 1;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000989
Chris Lattnere9feb472009-01-24 21:09:06 +0000990 // GCC extension: alignof(function) = 4.
991 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
992 // attribute(align) directive.
993 if (Ty->isFunctionType())
994 return 4;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000995
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000996 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
997 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere9feb472009-01-24 21:09:06 +0000998
999 // alignof VLA/incomplete array.
1000 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
1001 return GetAlignOfType(VAT->getElementType());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001002
Chris Lattnere9feb472009-01-24 21:09:06 +00001003 // sizeof (objc class)?
1004 if (isa<ObjCInterfaceType>(Ty))
1005 return 1; // FIXME: This probably isn't right.
1006
1007 // Get information about the alignment.
1008 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanc5082032009-02-22 03:31:23 +00001009 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001010}
1011
Chris Lattneraf707ab2009-01-24 21:53:27 +00001012unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1013 E = E->IgnoreParens();
1014
1015 // alignof decl is always accepted, even if it doesn't make sense: we default
1016 // to 1 in those cases.
1017 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001018 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001019
Chris Lattneraf707ab2009-01-24 21:53:27 +00001020 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001021 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001022
Chris Lattnere9feb472009-01-24 21:09:06 +00001023 return GetAlignOfType(E->getType());
1024}
1025
1026
Sebastian Redl05189992008-11-11 17:56:53 +00001027/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1028/// expression's type.
1029bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1030 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001031
Chris Lattnere9feb472009-01-24 21:09:06 +00001032 // Handle alignof separately.
1033 if (!E->isSizeOf()) {
1034 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001035 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001036 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001037 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001038 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001039
Sebastian Redl05189992008-11-11 17:56:53 +00001040 QualType SrcTy = E->getTypeOfArgument();
1041
Daniel Dunbar131eb432009-02-19 09:06:44 +00001042 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1043 // extension.
1044 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1045 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001046
Chris Lattnerfcee0012008-07-11 21:24:13 +00001047 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001048 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001049 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001050
1051 if (SrcTy->isObjCInterfaceType()) {
1052 // Slightly unusual case: the size of an ObjC interface type is the
1053 // size of the class. This code intentionally falls through to the normal
1054 // case.
1055 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
1056 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
1057 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
1058 }
1059
Chris Lattnere9feb472009-01-24 21:09:06 +00001060 // Get information about the size.
Chris Lattner87eae5e2008-07-11 22:52:41 +00001061 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Daniel Dunbar131eb432009-02-19 09:06:44 +00001062 return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001063}
1064
Chris Lattnerb542afe2008-07-11 19:10:17 +00001065bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001066 // Special case unary operators that do not need their subexpression
1067 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001068 if (E->isOffsetOfOp()) {
1069 // The AST for offsetof is defined in such a way that we can just
1070 // directly Evaluate it as an l-value.
1071 APValue LV;
1072 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1073 return false;
1074 if (LV.getLValueBase())
1075 return false;
1076 return Success(LV.getLValueOffset(), E);
1077 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001078
1079 if (E->getOpcode() == UnaryOperator::LNot) {
1080 // LNot's operand isn't necessarily an integer, so we handle it specially.
1081 bool bres;
1082 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1083 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001084 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001085 }
1086
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001087 // Only handle integral operations...
1088 if (!E->getSubExpr()->getType()->isIntegralType())
1089 return false;
1090
Chris Lattner87eae5e2008-07-11 22:52:41 +00001091 // Get the operand value into 'Result'.
1092 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001093 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001094
Chris Lattner75a48812008-07-11 22:15:16 +00001095 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001096 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001097 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1098 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001099 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001100 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001101 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1102 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001103 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001104 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001105 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001106 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001107 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001108 if (!Result.isInt()) return false;
1109 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001110 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001111 if (!Result.isInt()) return false;
1112 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001113 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001114}
1115
Chris Lattner732b2232008-07-12 01:15:53 +00001116/// HandleCast - This is used to evaluate implicit or explicit casts where the
1117/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001118bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001119 Expr *SubExpr = E->getSubExpr();
1120 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001121 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001122
Eli Friedman4efaa272008-11-12 09:44:48 +00001123 if (DestType->isBooleanType()) {
1124 bool BoolResult;
1125 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1126 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001127 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001128 }
1129
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001130 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001131 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001132 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001133 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001134
Eli Friedmanbe265702009-02-20 01:15:07 +00001135 if (!Result.isInt()) {
1136 // Only allow casts of lvalues if they are lossless.
1137 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1138 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001139
Daniel Dunbardd211642009-02-19 22:24:01 +00001140 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001141 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001142 }
1143
1144 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001145 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001146 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001147 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001148 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001149
Daniel Dunbardd211642009-02-19 22:24:01 +00001150 if (LV.getLValueBase()) {
1151 // Only allow based lvalue casts if they are lossless.
1152 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1153 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001154
Daniel Dunbardd211642009-02-19 22:24:01 +00001155 Result = LV;
1156 return true;
1157 }
1158
1159 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1160 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001161 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001162
Eli Friedmanbe265702009-02-20 01:15:07 +00001163 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1164 // This handles double-conversion cases, where there's both
1165 // an l-value promotion and an implicit conversion to int.
1166 APValue LV;
1167 if (!EvaluateLValue(SubExpr, LV, Info))
1168 return false;
1169
1170 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1171 return false;
1172
1173 Result = LV;
1174 return true;
1175 }
1176
Eli Friedman2217c872009-02-22 11:46:18 +00001177 // FIXME: Handle complex types
1178 // FIXME: Handle vectors
1179
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001180 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001181 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001182
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001183 APFloat F(0.0);
1184 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001185 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001186
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001187 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001188}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001189
Eli Friedman722c7172009-02-28 03:59:05 +00001190bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1191 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1192 APValue LV;
1193 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1194 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1195 return Success(LV.getComplexIntReal(), E);
1196 }
1197
1198 return Visit(E->getSubExpr());
1199}
1200
Eli Friedman664a1042009-02-27 04:45:43 +00001201bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001202 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1203 APValue LV;
1204 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1205 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1206 return Success(LV.getComplexIntImag(), E);
1207 }
1208
Eli Friedman664a1042009-02-27 04:45:43 +00001209 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1210 Info.EvalResult.HasSideEffects = true;
1211 return Success(0, E);
1212}
1213
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001214//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001215// Float Evaluation
1216//===----------------------------------------------------------------------===//
1217
1218namespace {
1219class VISIBILITY_HIDDEN FloatExprEvaluator
1220 : public StmtVisitor<FloatExprEvaluator, bool> {
1221 EvalInfo &Info;
1222 APFloat &Result;
1223public:
1224 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1225 : Info(info), Result(result) {}
1226
1227 bool VisitStmt(Stmt *S) {
1228 return false;
1229 }
1230
1231 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001232 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001233
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001234 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001235 bool VisitBinaryOperator(const BinaryOperator *E);
1236 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001237 bool VisitCastExpr(CastExpr *E);
1238 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001239
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001240 bool VisitChooseExpr(const ChooseExpr *E)
1241 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1242 bool VisitUnaryExtension(const UnaryOperator *E)
1243 { return Visit(E->getSubExpr()); }
1244
1245 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1246 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001247 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001248};
1249} // end anonymous namespace
1250
1251static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1252 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1253}
1254
Chris Lattner019f4e82008-10-06 05:28:25 +00001255bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001256 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001257 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001258 case Builtin::BI__builtin_huge_val:
1259 case Builtin::BI__builtin_huge_valf:
1260 case Builtin::BI__builtin_huge_vall:
1261 case Builtin::BI__builtin_inf:
1262 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001263 case Builtin::BI__builtin_infl: {
1264 const llvm::fltSemantics &Sem =
1265 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001266 Result = llvm::APFloat::getInf(Sem);
1267 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001268 }
Chris Lattner9e621712008-10-06 06:31:58 +00001269
1270 case Builtin::BI__builtin_nan:
1271 case Builtin::BI__builtin_nanf:
1272 case Builtin::BI__builtin_nanl:
1273 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1274 // can't constant fold it.
1275 if (const StringLiteral *S =
1276 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1277 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001278 const llvm::fltSemantics &Sem =
1279 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001280 Result = llvm::APFloat::getNaN(Sem);
1281 return true;
1282 }
1283 }
1284 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001285
1286 case Builtin::BI__builtin_fabs:
1287 case Builtin::BI__builtin_fabsf:
1288 case Builtin::BI__builtin_fabsl:
1289 if (!EvaluateFloat(E->getArg(0), Result, Info))
1290 return false;
1291
1292 if (Result.isNegative())
1293 Result.changeSign();
1294 return true;
1295
1296 case Builtin::BI__builtin_copysign:
1297 case Builtin::BI__builtin_copysignf:
1298 case Builtin::BI__builtin_copysignl: {
1299 APFloat RHS(0.);
1300 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1301 !EvaluateFloat(E->getArg(1), RHS, Info))
1302 return false;
1303 Result.copySign(RHS);
1304 return true;
1305 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001306 }
1307}
1308
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001309bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001310 if (E->getOpcode() == UnaryOperator::Deref)
1311 return false;
1312
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001313 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1314 return false;
1315
1316 switch (E->getOpcode()) {
1317 default: return false;
1318 case UnaryOperator::Plus:
1319 return true;
1320 case UnaryOperator::Minus:
1321 Result.changeSign();
1322 return true;
1323 }
1324}
Chris Lattner019f4e82008-10-06 05:28:25 +00001325
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001326bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1327 // FIXME: Diagnostics? I really don't understand how the warnings
1328 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001329 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001330 if (!EvaluateFloat(E->getLHS(), Result, Info))
1331 return false;
1332 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1333 return false;
1334
1335 switch (E->getOpcode()) {
1336 default: return false;
1337 case BinaryOperator::Mul:
1338 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1339 return true;
1340 case BinaryOperator::Add:
1341 Result.add(RHS, APFloat::rmNearestTiesToEven);
1342 return true;
1343 case BinaryOperator::Sub:
1344 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1345 return true;
1346 case BinaryOperator::Div:
1347 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1348 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001349 }
1350}
1351
1352bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1353 Result = E->getValue();
1354 return true;
1355}
1356
Eli Friedman4efaa272008-11-12 09:44:48 +00001357bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1358 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001359
Eli Friedman4efaa272008-11-12 09:44:48 +00001360 if (SubExpr->getType()->isIntegralType()) {
1361 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001362 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001363 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001364 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1365 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001366 return true;
1367 }
1368 if (SubExpr->getType()->isRealFloatingType()) {
1369 if (!Visit(SubExpr))
1370 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001371 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1372 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001373 return true;
1374 }
Eli Friedman2217c872009-02-22 11:46:18 +00001375 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001376
1377 return false;
1378}
1379
1380bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1381 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1382 return true;
1383}
1384
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001385//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001386// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001387//===----------------------------------------------------------------------===//
1388
1389namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001390class VISIBILITY_HIDDEN ComplexExprEvaluator
1391 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001392 EvalInfo &Info;
1393
1394public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001395 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001396
1397 //===--------------------------------------------------------------------===//
1398 // Visitor Methods
1399 //===--------------------------------------------------------------------===//
1400
1401 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001402 return APValue();
1403 }
1404
1405 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1406
1407 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001408 Expr* SubExpr = E->getSubExpr();
1409
1410 if (SubExpr->getType()->isRealFloatingType()) {
1411 APFloat Result(0.0);
1412
1413 if (!EvaluateFloat(SubExpr, Result, Info))
1414 return APValue();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001415
Daniel Dunbar3f279872009-01-29 01:32:56 +00001416 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001417 Result);
1418 } else {
1419 assert(SubExpr->getType()->isIntegerType() &&
1420 "Unexpected imaginary literal.");
1421
1422 llvm::APSInt Result;
1423 if (!EvaluateInteger(SubExpr, Result, Info))
1424 return APValue();
1425
1426 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1427 Zero = 0;
1428 return APValue(Zero, Result);
1429 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001430 }
1431
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001432 APValue VisitCastExpr(CastExpr *E) {
1433 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001434 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1435 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001436
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001437 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001438 APFloat Result(0.0);
1439
1440 if (!EvaluateFloat(SubExpr, Result, Info))
1441 return APValue();
1442
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001443 // Apply float conversion if necessary.
1444 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbar8f826f02009-01-24 19:08:01 +00001445 return APValue(Result,
Daniel Dunbar3f279872009-01-29 01:32:56 +00001446 APFloat(Result.getSemantics(), APFloat::fcZero, false));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001447 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001448 APSInt Result;
1449
1450 if (!EvaluateInteger(SubExpr, Result, Info))
1451 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001452
1453 // Apply integer conversion if necessary.
1454 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001455 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1456 Zero = 0;
1457 return APValue(Result, Zero);
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001458 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1459 APValue Src;
1460
1461 if (!EvaluateComplex(SubExpr, Src, Info))
1462 return APValue();
1463
1464 QualType SrcType = CT->getElementType();
1465
1466 if (Src.isComplexFloat()) {
1467 if (EltType->isRealFloatingType()) {
1468 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1469 Src.getComplexFloatReal(),
1470 Info.Ctx),
1471 HandleFloatToFloatCast(EltType, SrcType,
1472 Src.getComplexFloatImag(),
1473 Info.Ctx));
1474 } else {
1475 return APValue(HandleFloatToIntCast(EltType, SrcType,
1476 Src.getComplexFloatReal(),
1477 Info.Ctx),
1478 HandleFloatToIntCast(EltType, SrcType,
1479 Src.getComplexFloatImag(),
1480 Info.Ctx));
1481 }
1482 } else {
1483 assert(Src.isComplexInt() && "Invalid evaluate result.");
1484 if (EltType->isRealFloatingType()) {
1485 return APValue(HandleIntToFloatCast(EltType, SrcType,
1486 Src.getComplexIntReal(),
1487 Info.Ctx),
1488 HandleIntToFloatCast(EltType, SrcType,
1489 Src.getComplexIntImag(),
1490 Info.Ctx));
1491 } else {
1492 return APValue(HandleIntToIntCast(EltType, SrcType,
1493 Src.getComplexIntReal(),
1494 Info.Ctx),
1495 HandleIntToIntCast(EltType, SrcType,
1496 Src.getComplexIntImag(),
1497 Info.Ctx));
1498 }
1499 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001500 }
1501
1502 // FIXME: Handle more casts.
1503 return APValue();
1504 }
1505
1506 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001507 APValue VisitChooseExpr(const ChooseExpr *E)
1508 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1509 APValue VisitUnaryExtension(const UnaryOperator *E)
1510 { return Visit(E->getSubExpr()); }
1511 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001512 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001513};
1514} // end anonymous namespace
1515
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001516static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001517{
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001518 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1519 assert((!Result.isComplexFloat() ||
1520 (&Result.getComplexFloatReal().getSemantics() ==
1521 &Result.getComplexFloatImag().getSemantics())) &&
1522 "Invalid complex evaluation.");
1523 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001524}
1525
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001526APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001527{
1528 APValue Result, RHS;
1529
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001530 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001531 return APValue();
1532
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001533 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001534 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001535
Daniel Dunbar3f279872009-01-29 01:32:56 +00001536 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1537 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001538 switch (E->getOpcode()) {
1539 default: return APValue();
1540 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001541 if (Result.isComplexFloat()) {
1542 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1543 APFloat::rmNearestTiesToEven);
1544 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1545 APFloat::rmNearestTiesToEven);
1546 } else {
1547 Result.getComplexIntReal() += RHS.getComplexIntReal();
1548 Result.getComplexIntImag() += RHS.getComplexIntImag();
1549 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001550 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001551 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001552 if (Result.isComplexFloat()) {
1553 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1554 APFloat::rmNearestTiesToEven);
1555 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1556 APFloat::rmNearestTiesToEven);
1557 } else {
1558 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1559 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1560 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001561 break;
1562 case BinaryOperator::Mul:
1563 if (Result.isComplexFloat()) {
1564 APValue LHS = Result;
1565 APFloat &LHS_r = LHS.getComplexFloatReal();
1566 APFloat &LHS_i = LHS.getComplexFloatImag();
1567 APFloat &RHS_r = RHS.getComplexFloatReal();
1568 APFloat &RHS_i = RHS.getComplexFloatImag();
1569
1570 APFloat Tmp = LHS_r;
1571 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1572 Result.getComplexFloatReal() = Tmp;
1573 Tmp = LHS_i;
1574 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1575 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1576
1577 Tmp = LHS_r;
1578 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1579 Result.getComplexFloatImag() = Tmp;
1580 Tmp = LHS_i;
1581 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1582 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1583 } else {
1584 APValue LHS = Result;
1585 Result.getComplexIntReal() =
1586 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1587 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1588 Result.getComplexIntImag() =
1589 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1590 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1591 }
1592 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001593 }
1594
1595 return Result;
1596}
1597
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001598//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001599// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001600//===----------------------------------------------------------------------===//
1601
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001602/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001603/// any crazy technique (that has nothing to do with language standards) that
1604/// we want to. If this function returns true, it returns the folded constant
1605/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001606bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1607 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001608
Nate Begeman59b5da62009-01-18 03:20:47 +00001609 if (getType()->isVectorType()) {
1610 if (!EvaluateVector(this, Result.Val, Info))
1611 return false;
1612 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001613 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001614 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001615 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001616 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001617 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001618 } else if (getType()->isRealFloatingType()) {
1619 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001620 if (!EvaluateFloat(this, f, Info))
1621 return false;
1622
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001623 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001624 } else if (getType()->isAnyComplexType()) {
1625 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001626 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001627 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001628 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001629
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001630 return true;
1631}
1632
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001633/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001634/// folded, but discard the result.
1635bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001636 EvalResult Result;
1637 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001638}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001639
1640APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001641 EvalResult EvalResult;
1642 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001643 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001644 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001645 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001646
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001647 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001648}