blob: 1ea252fecb76297d1255c6db54444d29f00333b6 [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);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000159 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000160 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
161 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
162 APValue VisitMemberExpr(MemberExpr *E);
163 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000164 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000165 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000166 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000167 APValue VisitUnaryExtension(const UnaryOperator *E)
168 { return Visit(E->getSubExpr()); }
169 APValue VisitChooseExpr(const ChooseExpr *E)
170 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
171 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000172};
173} // end anonymous namespace
174
175static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
176 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
177 return Result.isLValue();
178}
179
Anders Carlsson35873c42008-11-24 04:41:22 +0000180APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
181{
182 if (!E->hasGlobalStorage())
183 return APValue();
184
185 return APValue(E, 0);
186}
187
Steve Naroff3aaa4822009-04-16 19:02:57 +0000188APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E)
189{
190 if (E->hasBlockDeclRefExprs())
191 return APValue();
192
193 return APValue(E, 0);
194}
195
Eli Friedman4efaa272008-11-12 09:44:48 +0000196APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
197 if (E->isFileScope())
198 return APValue(E, 0);
199 return APValue();
200}
201
202APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
203 APValue result;
204 QualType Ty;
205 if (E->isArrow()) {
206 if (!EvaluatePointer(E->getBase(), result, Info))
207 return APValue();
208 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
209 } else {
210 result = Visit(E->getBase());
211 if (result.isUninit())
212 return APValue();
213 Ty = E->getBase()->getType();
214 }
215
216 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
217 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000218
219 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
220 if (!FD) // FIXME: deal with other kinds of member expressions
221 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000222
223 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000224 unsigned i = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000225 for (RecordDecl::field_iterator Field = RD->field_begin(Info.Ctx),
226 FieldEnd = RD->field_end(Info.Ctx);
Douglas Gregor44b43212008-12-11 16:49:14 +0000227 Field != FieldEnd; (void)++Field, ++i) {
228 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000229 break;
230 }
231
232 result.setLValue(result.getLValueBase(),
233 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
234
235 return result;
236}
237
Anders Carlsson3068d112008-11-16 19:01:22 +0000238APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
239{
240 APValue Result;
241
242 if (!EvaluatePointer(E->getBase(), Result, Info))
243 return APValue();
244
245 APSInt Index;
246 if (!EvaluateInteger(E->getIdx(), Index, Info))
247 return APValue();
248
249 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
250
251 uint64_t Offset = Index.getSExtValue() * ElementSize;
252 Result.setLValue(Result.getLValueBase(),
253 Result.getLValueOffset() + Offset);
254 return Result;
255}
Eli Friedman4efaa272008-11-12 09:44:48 +0000256
Eli Friedmane8761c82009-02-20 01:57:15 +0000257APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E)
258{
259 APValue Result;
260 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
261 return APValue();
262 return Result;
263}
264
Eli Friedman4efaa272008-11-12 09:44:48 +0000265//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000266// Pointer Evaluation
267//===----------------------------------------------------------------------===//
268
Anders Carlssonc754aa62008-07-08 05:13:58 +0000269namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000270class VISIBILITY_HIDDEN PointerExprEvaluator
271 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000272 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000273public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000274
Chris Lattner87eae5e2008-07-11 22:52:41 +0000275 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000276
Anders Carlsson2bad1682008-07-08 14:30:00 +0000277 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000278 return APValue();
279 }
280
281 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
282
Anders Carlsson650c92f2008-07-08 15:34:11 +0000283 APValue VisitBinaryOperator(const BinaryOperator *E);
284 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000285 APValue VisitUnaryExtension(const UnaryOperator *E)
286 { return Visit(E->getSubExpr()); }
287 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000288 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
289 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000290 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
291 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000292 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000293 APValue VisitBlockExpr(BlockExpr *E) {
294 if (!E->hasBlockDeclRefExprs())
295 return APValue(E, 0);
296 return APValue();
297 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000298 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
299 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000300 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000301 APValue VisitChooseExpr(ChooseExpr *E)
302 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
303 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000304};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000305} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000306
Chris Lattner87eae5e2008-07-11 22:52:41 +0000307static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000308 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000309 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000310 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000311 return Result.isLValue();
312}
313
314APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
315 if (E->getOpcode() != BinaryOperator::Add &&
316 E->getOpcode() != BinaryOperator::Sub)
317 return APValue();
318
319 const Expr *PExp = E->getLHS();
320 const Expr *IExp = E->getRHS();
321 if (IExp->getType()->isPointerType())
322 std::swap(PExp, IExp);
323
324 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000325 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000326 return APValue();
327
328 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000329 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000330 return APValue();
331
Eli Friedman4efaa272008-11-12 09:44:48 +0000332 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000333 uint64_t SizeOfPointee;
334
335 // Explicitly handle GNU void* and function pointer arithmetic extensions.
336 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
337 SizeOfPointee = 1;
338 else
339 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000340
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000342
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000343 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000344 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000345 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000346 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
347
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000348 return APValue(ResultLValue.getLValueBase(), Offset);
349}
Eli Friedman4efaa272008-11-12 09:44:48 +0000350
Eli Friedman2217c872009-02-22 11:46:18 +0000351APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
352 APValue result;
353 if (EvaluateLValue(E->getSubExpr(), result, Info))
354 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000355 return APValue();
356}
Anders Carlssond407a762008-12-05 05:24:13 +0000357
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000358
Chris Lattnerb542afe2008-07-11 19:10:17 +0000359APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000360 const Expr* SubExpr = E->getSubExpr();
361
362 // Check for pointer->pointer cast
363 if (SubExpr->getType()->isPointerType()) {
364 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000365 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000366 return Result;
367 return APValue();
368 }
369
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000370 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000371 APValue Result;
372 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
373 return APValue();
374
375 if (Result.isInt()) {
376 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
377 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000378 }
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000379
380 // Cast is of an lvalue, no need to change value.
381 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000382 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000383
384 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000385 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000386 SubExpr->getType()->isArrayType()) {
387 APValue Result;
388 if (EvaluateLValue(SubExpr, Result, Info))
389 return Result;
390 return APValue();
391 }
392
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393 return APValue();
394}
395
Eli Friedman3941b182009-01-25 01:54:01 +0000396APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000397 if (E->isBuiltinCall(Info.Ctx) ==
398 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000399 return APValue(E, 0);
400 return APValue();
401}
402
Eli Friedman4efaa272008-11-12 09:44:48 +0000403APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
404 bool BoolResult;
405 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
406 return APValue();
407
408 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
409
410 APValue Result;
411 if (EvaluatePointer(EvalExpr, Result, Info))
412 return Result;
413 return APValue();
414}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000415
416//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000417// Vector Evaluation
418//===----------------------------------------------------------------------===//
419
420namespace {
421 class VISIBILITY_HIDDEN VectorExprEvaluator
422 : public StmtVisitor<VectorExprEvaluator, APValue> {
423 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000424 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000425 public:
426
427 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
428
429 APValue VisitStmt(Stmt *S) {
430 return APValue();
431 }
432
Eli Friedman91110ee2009-02-23 04:23:56 +0000433 APValue VisitParenExpr(ParenExpr *E)
434 { return Visit(E->getSubExpr()); }
435 APValue VisitUnaryExtension(const UnaryOperator *E)
436 { return Visit(E->getSubExpr()); }
437 APValue VisitUnaryPlus(const UnaryOperator *E)
438 { return Visit(E->getSubExpr()); }
439 APValue VisitUnaryReal(const UnaryOperator *E)
440 { return Visit(E->getSubExpr()); }
441 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
442 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000443 APValue VisitCastExpr(const CastExpr* E);
444 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
445 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000446 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000447 APValue VisitChooseExpr(const ChooseExpr *E)
448 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000449 APValue VisitUnaryImag(const UnaryOperator *E);
450 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000451 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000452 // shufflevector, ExtVectorElementExpr
453 // (Note that these require implementing conversions
454 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000455 };
456} // end anonymous namespace
457
458static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
459 if (!E->getType()->isVectorType())
460 return false;
461 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
462 return !Result.isUninit();
463}
464
465APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
466 const Expr* SE = E->getSubExpr();
467
468 // Check for vector->vector bitcast.
469 if (SE->getType()->isVectorType())
470 return this->Visit(const_cast<Expr*>(SE));
471
472 return APValue();
473}
474
475APValue
476VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
477 return this->Visit(const_cast<Expr*>(E->getInitializer()));
478}
479
480APValue
481VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
482 const VectorType *VT = E->getType()->getAsVectorType();
483 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000484 unsigned NumElements = VT->getNumElements();
Nate Begeman59b5da62009-01-18 03:20:47 +0000485
486 QualType EltTy = VT->getElementType();
487 llvm::SmallVector<APValue, 4> Elements;
488
Eli Friedman91110ee2009-02-23 04:23:56 +0000489 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000490 if (EltTy->isIntegerType()) {
491 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000492 if (i < NumInits) {
493 if (!EvaluateInteger(E->getInit(i), sInt, Info))
494 return APValue();
495 } else {
496 sInt = Info.Ctx.MakeIntValue(0, EltTy);
497 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000498 Elements.push_back(APValue(sInt));
499 } else {
500 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000501 if (i < NumInits) {
502 if (!EvaluateFloat(E->getInit(i), f, Info))
503 return APValue();
504 } else {
505 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
506 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000507 Elements.push_back(APValue(f));
508 }
509 }
510 return APValue(&Elements[0], Elements.size());
511}
512
Eli Friedman91110ee2009-02-23 04:23:56 +0000513APValue
514VectorExprEvaluator::GetZeroVector(QualType T) {
515 const VectorType *VT = T->getAsVectorType();
516 QualType EltTy = VT->getElementType();
517 APValue ZeroElement;
518 if (EltTy->isIntegerType())
519 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
520 else
521 ZeroElement =
522 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
523
524 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
525 return APValue(&Elements[0], Elements.size());
526}
527
528APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
529 bool BoolResult;
530 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
531 return APValue();
532
533 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
534
535 APValue Result;
536 if (EvaluateVector(EvalExpr, Result, Info))
537 return Result;
538 return APValue();
539}
540
Eli Friedman91110ee2009-02-23 04:23:56 +0000541APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
542 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
543 Info.EvalResult.HasSideEffects = true;
544 return GetZeroVector(E->getType());
545}
546
Nate Begeman59b5da62009-01-18 03:20:47 +0000547//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000548// Integer Evaluation
549//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000550
551namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000552class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000553 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000554 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000555 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000556public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000557 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000558 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000559
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000560 bool Success(const llvm::APSInt &SI, 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(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000563 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000564 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000565 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000566 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000567 return true;
568 }
569
Daniel Dunbar131eb432009-02-19 09:06:44 +0000570 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000571 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000572 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000573 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000574 Result = APValue(APSInt(I));
575 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000576 return true;
577 }
578
579 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000580 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000581 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000582 return true;
583 }
584
Anders Carlsson82206e22008-11-30 18:14:57 +0000585 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000586 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000587 if (Info.EvalResult.Diag == 0) {
588 Info.EvalResult.DiagLoc = L;
589 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000590 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000591 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000592 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000593 }
594
Anders Carlssonc754aa62008-07-08 05:13:58 +0000595 //===--------------------------------------------------------------------===//
596 // Visitor Methods
597 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000598
599 bool VisitStmt(Stmt *) {
600 assert(0 && "This should be called on integers, stmts are not integers");
601 return false;
602 }
Chris Lattner7a767782008-07-11 19:24:49 +0000603
Chris Lattner32fea9d2008-11-12 07:43:42 +0000604 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000605 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000606 }
607
Chris Lattnerb542afe2008-07-11 19:10:17 +0000608 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000609
Chris Lattner4c4867e2008-07-12 00:38:25 +0000610 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000611 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000612 }
613 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000614 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000615 }
616 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000617 // Per gcc docs "this built-in function ignores top level
618 // qualifiers". We need to use the canonical version to properly
619 // be able to strip CRV qualifiers from the type.
620 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
621 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000622 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
623 T1.getUnqualifiedType()),
624 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000625 }
626 bool VisitDeclRefExpr(const DeclRefExpr *E);
627 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000628 bool VisitBinaryOperator(const BinaryOperator *E);
629 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000630 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000631
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000632 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000633 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
634
Anders Carlsson3068d112008-11-16 19:01:22 +0000635 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000636 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000637 }
638
Anders Carlsson3f704562008-12-21 22:39:40 +0000639 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000640 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000641 }
642
Anders Carlsson3068d112008-11-16 19:01:22 +0000643 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000644 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000645 }
646
Eli Friedman664a1042009-02-27 04:45:43 +0000647 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
648 return Success(0, E);
649 }
650
Sebastian Redl64b45f72009-01-05 20:52:13 +0000651 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000652 return Success(E->EvaluateTrait(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000653 }
654
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000655 bool VisitChooseExpr(const ChooseExpr *E) {
656 return Visit(E->getChosenSubExpr(Info.Ctx));
657 }
658
Eli Friedman722c7172009-02-28 03:59:05 +0000659 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000660 bool VisitUnaryImag(const UnaryOperator *E);
661
Chris Lattnerfcee0012008-07-11 21:24:13 +0000662private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000663 unsigned GetAlignOfExpr(const Expr *E);
664 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000665 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000666};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000667} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000668
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000669static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000670 if (!E->getType()->isIntegralType())
671 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000672
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000673 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
674}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000675
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000676static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
677 APValue Val;
678 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
679 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000680 Result = Val.getInt();
681 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000682}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000683
Chris Lattner4c4867e2008-07-12 00:38:25 +0000684bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
685 // Enums are integer constant exprs.
686 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000687 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000688 // signedness consistently; see PR3173.
689 APSInt SI = D->getInitVal();
690 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
691 // FIXME: This is an ugly hack around the fact that enums don't
692 // set their width (!?!) consistently; see PR3173.
693 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
694 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000695 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000696
697 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000698 // In C, they can also be folded, although they are not ICEs.
699 if (E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000700 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
701 if (const Expr *Init = D->getInit())
702 return Visit(const_cast<Expr*>(Init));
703 }
704 }
705
Chris Lattner4c4867e2008-07-12 00:38:25 +0000706 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000707 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000708}
709
Chris Lattnera4d55d82008-10-06 06:40:35 +0000710/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
711/// as GCC.
712static int EvaluateBuiltinClassifyType(const CallExpr *E) {
713 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000714 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000715 enum gcc_type_class {
716 no_type_class = -1,
717 void_type_class, integer_type_class, char_type_class,
718 enumeral_type_class, boolean_type_class,
719 pointer_type_class, reference_type_class, offset_type_class,
720 real_type_class, complex_type_class,
721 function_type_class, method_type_class,
722 record_type_class, union_type_class,
723 array_type_class, string_type_class,
724 lang_type_class
725 };
726
727 // If no argument was supplied, default to "no_type_class". This isn't
728 // ideal, however it is what gcc does.
729 if (E->getNumArgs() == 0)
730 return no_type_class;
731
732 QualType ArgTy = E->getArg(0)->getType();
733 if (ArgTy->isVoidType())
734 return void_type_class;
735 else if (ArgTy->isEnumeralType())
736 return enumeral_type_class;
737 else if (ArgTy->isBooleanType())
738 return boolean_type_class;
739 else if (ArgTy->isCharType())
740 return string_type_class; // gcc doesn't appear to use char_type_class
741 else if (ArgTy->isIntegerType())
742 return integer_type_class;
743 else if (ArgTy->isPointerType())
744 return pointer_type_class;
745 else if (ArgTy->isReferenceType())
746 return reference_type_class;
747 else if (ArgTy->isRealType())
748 return real_type_class;
749 else if (ArgTy->isComplexType())
750 return complex_type_class;
751 else if (ArgTy->isFunctionType())
752 return function_type_class;
753 else if (ArgTy->isStructureType())
754 return record_type_class;
755 else if (ArgTy->isUnionType())
756 return union_type_class;
757 else if (ArgTy->isArrayType())
758 return array_type_class;
759 else if (ArgTy->isUnionType())
760 return union_type_class;
761 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
762 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
763 return -1;
764}
765
Chris Lattner4c4867e2008-07-12 00:38:25 +0000766bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000767 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000768 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000769 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000770 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000771 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000772
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000773 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000774 // __builtin_constant_p always has one operand: it returns true if that
775 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000776 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000777 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000778}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000779
Chris Lattnerb542afe2008-07-11 19:10:17 +0000780bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000781 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000782 if (!Visit(E->getRHS()))
783 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000784
Eli Friedman33ef1452009-02-26 10:19:36 +0000785 // If we can't evaluate the LHS, it might have side effects;
786 // conservatively mark it.
787 if (!E->getLHS()->isEvaluatable(Info.Ctx))
788 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000789
Anders Carlsson027f62e2008-12-01 02:07:06 +0000790 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000791 }
792
793 if (E->isLogicalOp()) {
794 // These need to be handled specially because the operands aren't
795 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000796 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000797
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000798 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000799 // We were able to evaluate the LHS, see if we can get away with not
800 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000801 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000802 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000803
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000804 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000805 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000806 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000807 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000808 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000809 }
810 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000811 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000812 // We can't evaluate the LHS; however, sometimes the result
813 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000814 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
815 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000816 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000817 // must have had side effects.
818 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000819
820 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000821 }
822 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000823 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000824
Eli Friedmana6afa762008-11-13 06:09:17 +0000825 return false;
826 }
827
Anders Carlsson286f85e2008-11-16 07:17:21 +0000828 QualType LHSTy = E->getLHS()->getType();
829 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000830
831 if (LHSTy->isAnyComplexType()) {
832 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
833 APValue LHS, RHS;
834
835 if (!EvaluateComplex(E->getLHS(), LHS, Info))
836 return false;
837
838 if (!EvaluateComplex(E->getRHS(), RHS, Info))
839 return false;
840
841 if (LHS.isComplexFloat()) {
842 APFloat::cmpResult CR_r =
843 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
844 APFloat::cmpResult CR_i =
845 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
846
Daniel Dunbar4087e242009-01-29 06:43:41 +0000847 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000848 return Success((CR_r == APFloat::cmpEqual &&
849 CR_i == APFloat::cmpEqual), E);
850 else {
851 assert(E->getOpcode() == BinaryOperator::NE &&
852 "Invalid complex comparison.");
853 return Success(((CR_r == APFloat::cmpGreaterThan ||
854 CR_r == APFloat::cmpLessThan) &&
855 (CR_i == APFloat::cmpGreaterThan ||
856 CR_i == APFloat::cmpLessThan)), E);
857 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000858 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000859 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000860 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
861 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
862 else {
863 assert(E->getOpcode() == BinaryOperator::NE &&
864 "Invalid compex comparison.");
865 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
866 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
867 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000868 }
869 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000870
871 if (LHSTy->isRealFloatingType() &&
872 RHSTy->isRealFloatingType()) {
873 APFloat RHS(0.0), LHS(0.0);
874
875 if (!EvaluateFloat(E->getRHS(), RHS, Info))
876 return false;
877
878 if (!EvaluateFloat(E->getLHS(), LHS, Info))
879 return false;
880
881 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000882
Anders Carlsson286f85e2008-11-16 07:17:21 +0000883 switch (E->getOpcode()) {
884 default:
885 assert(0 && "Invalid binary operator!");
886 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000887 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000888 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000889 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000890 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000891 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000892 case BinaryOperator::GE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000893 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
894 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000895 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000896 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000897 case BinaryOperator::NE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000898 return Success(CR == APFloat::cmpGreaterThan
899 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000900 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000901 }
902
Anders Carlsson3068d112008-11-16 19:01:22 +0000903 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000904 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000905 APValue LHSValue;
906 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
907 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000908
Anders Carlsson3068d112008-11-16 19:01:22 +0000909 APValue RHSValue;
910 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
911 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000912
913 // Reject any bases; this is conservative, but good enough for
914 // common uses
Anders Carlsson3068d112008-11-16 19:01:22 +0000915 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
916 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000917
Anders Carlsson3068d112008-11-16 19:01:22 +0000918 const QualType Type = E->getLHS()->getType();
919 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
920
921 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
922 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000923
Daniel Dunbar131eb432009-02-19 09:06:44 +0000924 return Success(D, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000925 }
926 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000927 if (!LHSTy->isIntegralType() ||
928 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000929 // We can't continue from here for non-integral types, and they
930 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +0000931 return false;
932 }
933
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000934 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000935 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +0000936 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000937
Eli Friedman42edd0d2009-03-24 01:14:50 +0000938 APValue RHSVal;
939 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000940 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +0000941
942 // Handle cases like (unsigned long)&a + 4.
943 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
944 uint64_t offset = Result.getLValueOffset();
945 if (E->getOpcode() == BinaryOperator::Add)
946 offset += RHSVal.getInt().getZExtValue();
947 else
948 offset -= RHSVal.getInt().getZExtValue();
949 Result = APValue(Result.getLValueBase(), offset);
950 return true;
951 }
952
953 // Handle cases like 4 + (unsigned long)&a
954 if (E->getOpcode() == BinaryOperator::Add &&
955 RHSVal.isLValue() && Result.isInt()) {
956 uint64_t offset = RHSVal.getLValueOffset();
957 offset += Result.getInt().getZExtValue();
958 Result = APValue(RHSVal.getLValueBase(), offset);
959 return true;
960 }
961
962 // All the following cases expect both operands to be an integer
963 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000964 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000965
Eli Friedman42edd0d2009-03-24 01:14:50 +0000966 APSInt& RHS = RHSVal.getInt();
967
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000968 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000969 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000970 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000971 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
972 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
973 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
974 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
975 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
976 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000977 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000978 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000979 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000980 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000981 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000982 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000983 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000984 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000985 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +0000986 // FIXME: Warn about out of range shift amounts!
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000987 unsigned SA =
988 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
989 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000990 }
991 case BinaryOperator::Shr: {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000992 unsigned SA =
993 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
994 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000995 }
Chris Lattnerb542afe2008-07-11 19:10:17 +0000996
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000997 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
998 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
999 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1000 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1001 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1002 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001003 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001004}
1005
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001006bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001007 bool Cond;
1008 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001009 return false;
1010
Nuno Lopesa25bd552008-11-16 22:06:39 +00001011 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001012}
1013
Chris Lattneraf707ab2009-01-24 21:53:27 +00001014unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001015 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
1016
1017 // __alignof__(void) = 1 as a gcc extension.
1018 if (Ty->isVoidType())
1019 return 1;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001020
Chris Lattnere9feb472009-01-24 21:09:06 +00001021 // GCC extension: alignof(function) = 4.
1022 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
1023 // attribute(align) directive.
1024 if (Ty->isFunctionType())
1025 return 4;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001026
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001027 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
1028 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere9feb472009-01-24 21:09:06 +00001029
1030 // alignof VLA/incomplete array.
1031 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
1032 return GetAlignOfType(VAT->getElementType());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001033
Chris Lattnere9feb472009-01-24 21:09:06 +00001034 // sizeof (objc class)?
1035 if (isa<ObjCInterfaceType>(Ty))
1036 return 1; // FIXME: This probably isn't right.
1037
1038 // Get information about the alignment.
1039 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanc5082032009-02-22 03:31:23 +00001040 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001041}
1042
Chris Lattneraf707ab2009-01-24 21:53:27 +00001043unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1044 E = E->IgnoreParens();
1045
1046 // alignof decl is always accepted, even if it doesn't make sense: we default
1047 // to 1 in those cases.
1048 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001049 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001050
Chris Lattneraf707ab2009-01-24 21:53:27 +00001051 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001052 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001053
Chris Lattnere9feb472009-01-24 21:09:06 +00001054 return GetAlignOfType(E->getType());
1055}
1056
1057
Sebastian Redl05189992008-11-11 17:56:53 +00001058/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1059/// expression's type.
1060bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1061 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001062
Chris Lattnere9feb472009-01-24 21:09:06 +00001063 // Handle alignof separately.
1064 if (!E->isSizeOf()) {
1065 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001066 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001067 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001068 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001069 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001070
Sebastian Redl05189992008-11-11 17:56:53 +00001071 QualType SrcTy = E->getTypeOfArgument();
1072
Daniel Dunbar131eb432009-02-19 09:06:44 +00001073 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1074 // extension.
1075 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1076 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001077
Chris Lattnerfcee0012008-07-11 21:24:13 +00001078 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001079 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001080 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001081
Daniel Dunbarff896662009-04-21 15:48:54 +00001082 unsigned BitWidth = 0;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001083 if (SrcTy->isObjCInterfaceType()) {
1084 // Slightly unusual case: the size of an ObjC interface type is the
Daniel Dunbarff896662009-04-21 15:48:54 +00001085 // size of the class.
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001086 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbarff896662009-04-21 15:48:54 +00001087 const ASTRecordLayout &Layout = Info.Ctx.getASTObjCInterfaceLayout(OI);
1088 BitWidth = Layout.getSize();
1089 } else
1090 BitWidth = Info.Ctx.getTypeSize(SrcTy);
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001091
Chris Lattnere9feb472009-01-24 21:09:06 +00001092 // Get information about the size.
Daniel Dunbarff896662009-04-21 15:48:54 +00001093 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001094}
1095
Chris Lattnerb542afe2008-07-11 19:10:17 +00001096bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001097 // Special case unary operators that do not need their subexpression
1098 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001099 if (E->isOffsetOfOp()) {
1100 // The AST for offsetof is defined in such a way that we can just
1101 // directly Evaluate it as an l-value.
1102 APValue LV;
1103 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1104 return false;
1105 if (LV.getLValueBase())
1106 return false;
1107 return Success(LV.getLValueOffset(), E);
1108 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001109
1110 if (E->getOpcode() == UnaryOperator::LNot) {
1111 // LNot's operand isn't necessarily an integer, so we handle it specially.
1112 bool bres;
1113 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1114 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001115 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001116 }
1117
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001118 // Only handle integral operations...
1119 if (!E->getSubExpr()->getType()->isIntegralType())
1120 return false;
1121
Chris Lattner87eae5e2008-07-11 22:52:41 +00001122 // Get the operand value into 'Result'.
1123 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001124 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001125
Chris Lattner75a48812008-07-11 22:15:16 +00001126 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001127 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001128 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1129 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001130 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001131 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001132 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1133 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001134 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001135 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001136 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001137 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001138 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001139 if (!Result.isInt()) return false;
1140 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001141 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001142 if (!Result.isInt()) return false;
1143 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001144 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001145}
1146
Chris Lattner732b2232008-07-12 01:15:53 +00001147/// HandleCast - This is used to evaluate implicit or explicit casts where the
1148/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001149bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001150 Expr *SubExpr = E->getSubExpr();
1151 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001152 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001153
Eli Friedman4efaa272008-11-12 09:44:48 +00001154 if (DestType->isBooleanType()) {
1155 bool BoolResult;
1156 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1157 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001158 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001159 }
1160
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001161 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001162 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001163 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001164 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001165
Eli Friedmanbe265702009-02-20 01:15:07 +00001166 if (!Result.isInt()) {
1167 // Only allow casts of lvalues if they are lossless.
1168 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1169 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001170
Daniel Dunbardd211642009-02-19 22:24:01 +00001171 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001172 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001173 }
1174
1175 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001176 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001177 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001178 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001179 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001180
Daniel Dunbardd211642009-02-19 22:24:01 +00001181 if (LV.getLValueBase()) {
1182 // Only allow based lvalue casts if they are lossless.
1183 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1184 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001185
Daniel Dunbardd211642009-02-19 22:24:01 +00001186 Result = LV;
1187 return true;
1188 }
1189
1190 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1191 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001192 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001193
Eli Friedmanbe265702009-02-20 01:15:07 +00001194 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1195 // This handles double-conversion cases, where there's both
1196 // an l-value promotion and an implicit conversion to int.
1197 APValue LV;
1198 if (!EvaluateLValue(SubExpr, LV, Info))
1199 return false;
1200
1201 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1202 return false;
1203
1204 Result = LV;
1205 return true;
1206 }
1207
Eli Friedman1725f682009-04-22 19:23:09 +00001208 if (SrcType->isAnyComplexType()) {
1209 APValue C;
1210 if (!EvaluateComplex(SubExpr, C, Info))
1211 return false;
1212 if (C.isComplexFloat())
1213 return Success(HandleFloatToIntCast(DestType, SrcType,
1214 C.getComplexFloatReal(), Info.Ctx),
1215 E);
1216 else
1217 return Success(HandleIntToIntCast(DestType, SrcType,
1218 C.getComplexIntReal(), Info.Ctx), E);
1219 }
Eli Friedman2217c872009-02-22 11:46:18 +00001220 // FIXME: Handle vectors
1221
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001222 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001223 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001224
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001225 APFloat F(0.0);
1226 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001227 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001228
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001229 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001230}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001231
Eli Friedman722c7172009-02-28 03:59:05 +00001232bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1233 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1234 APValue LV;
1235 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1236 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1237 return Success(LV.getComplexIntReal(), E);
1238 }
1239
1240 return Visit(E->getSubExpr());
1241}
1242
Eli Friedman664a1042009-02-27 04:45:43 +00001243bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001244 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1245 APValue LV;
1246 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1247 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1248 return Success(LV.getComplexIntImag(), E);
1249 }
1250
Eli Friedman664a1042009-02-27 04:45:43 +00001251 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1252 Info.EvalResult.HasSideEffects = true;
1253 return Success(0, E);
1254}
1255
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001256//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001257// Float Evaluation
1258//===----------------------------------------------------------------------===//
1259
1260namespace {
1261class VISIBILITY_HIDDEN FloatExprEvaluator
1262 : public StmtVisitor<FloatExprEvaluator, bool> {
1263 EvalInfo &Info;
1264 APFloat &Result;
1265public:
1266 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1267 : Info(info), Result(result) {}
1268
1269 bool VisitStmt(Stmt *S) {
1270 return false;
1271 }
1272
1273 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001274 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001275
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001276 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001277 bool VisitBinaryOperator(const BinaryOperator *E);
1278 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001279 bool VisitCastExpr(CastExpr *E);
1280 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001281
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001282 bool VisitChooseExpr(const ChooseExpr *E)
1283 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1284 bool VisitUnaryExtension(const UnaryOperator *E)
1285 { return Visit(E->getSubExpr()); }
1286
1287 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1288 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001289 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001290};
1291} // end anonymous namespace
1292
1293static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1294 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1295}
1296
Chris Lattner019f4e82008-10-06 05:28:25 +00001297bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001298 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001299 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001300 case Builtin::BI__builtin_huge_val:
1301 case Builtin::BI__builtin_huge_valf:
1302 case Builtin::BI__builtin_huge_vall:
1303 case Builtin::BI__builtin_inf:
1304 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001305 case Builtin::BI__builtin_infl: {
1306 const llvm::fltSemantics &Sem =
1307 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001308 Result = llvm::APFloat::getInf(Sem);
1309 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001310 }
Chris Lattner9e621712008-10-06 06:31:58 +00001311
1312 case Builtin::BI__builtin_nan:
1313 case Builtin::BI__builtin_nanf:
1314 case Builtin::BI__builtin_nanl:
1315 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1316 // can't constant fold it.
1317 if (const StringLiteral *S =
1318 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1319 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001320 const llvm::fltSemantics &Sem =
1321 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001322 Result = llvm::APFloat::getNaN(Sem);
1323 return true;
1324 }
1325 }
1326 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001327
1328 case Builtin::BI__builtin_fabs:
1329 case Builtin::BI__builtin_fabsf:
1330 case Builtin::BI__builtin_fabsl:
1331 if (!EvaluateFloat(E->getArg(0), Result, Info))
1332 return false;
1333
1334 if (Result.isNegative())
1335 Result.changeSign();
1336 return true;
1337
1338 case Builtin::BI__builtin_copysign:
1339 case Builtin::BI__builtin_copysignf:
1340 case Builtin::BI__builtin_copysignl: {
1341 APFloat RHS(0.);
1342 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1343 !EvaluateFloat(E->getArg(1), RHS, Info))
1344 return false;
1345 Result.copySign(RHS);
1346 return true;
1347 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001348 }
1349}
1350
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001351bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001352 if (E->getOpcode() == UnaryOperator::Deref)
1353 return false;
1354
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001355 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1356 return false;
1357
1358 switch (E->getOpcode()) {
1359 default: return false;
1360 case UnaryOperator::Plus:
1361 return true;
1362 case UnaryOperator::Minus:
1363 Result.changeSign();
1364 return true;
1365 }
1366}
Chris Lattner019f4e82008-10-06 05:28:25 +00001367
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001368bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1369 // FIXME: Diagnostics? I really don't understand how the warnings
1370 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001371 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001372 if (!EvaluateFloat(E->getLHS(), Result, Info))
1373 return false;
1374 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1375 return false;
1376
1377 switch (E->getOpcode()) {
1378 default: return false;
1379 case BinaryOperator::Mul:
1380 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1381 return true;
1382 case BinaryOperator::Add:
1383 Result.add(RHS, APFloat::rmNearestTiesToEven);
1384 return true;
1385 case BinaryOperator::Sub:
1386 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1387 return true;
1388 case BinaryOperator::Div:
1389 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1390 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001391 }
1392}
1393
1394bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1395 Result = E->getValue();
1396 return true;
1397}
1398
Eli Friedman4efaa272008-11-12 09:44:48 +00001399bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1400 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001401
Eli Friedman4efaa272008-11-12 09:44:48 +00001402 if (SubExpr->getType()->isIntegralType()) {
1403 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001404 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001405 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001406 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1407 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001408 return true;
1409 }
1410 if (SubExpr->getType()->isRealFloatingType()) {
1411 if (!Visit(SubExpr))
1412 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001413 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1414 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001415 return true;
1416 }
Eli Friedman2217c872009-02-22 11:46:18 +00001417 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001418
1419 return false;
1420}
1421
1422bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1423 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1424 return true;
1425}
1426
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001427//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001428// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001429//===----------------------------------------------------------------------===//
1430
1431namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001432class VISIBILITY_HIDDEN ComplexExprEvaluator
1433 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001434 EvalInfo &Info;
1435
1436public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001437 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001438
1439 //===--------------------------------------------------------------------===//
1440 // Visitor Methods
1441 //===--------------------------------------------------------------------===//
1442
1443 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001444 return APValue();
1445 }
1446
1447 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1448
1449 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001450 Expr* SubExpr = E->getSubExpr();
1451
1452 if (SubExpr->getType()->isRealFloatingType()) {
1453 APFloat Result(0.0);
1454
1455 if (!EvaluateFloat(SubExpr, Result, Info))
1456 return APValue();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001457
Daniel Dunbar3f279872009-01-29 01:32:56 +00001458 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001459 Result);
1460 } else {
1461 assert(SubExpr->getType()->isIntegerType() &&
1462 "Unexpected imaginary literal.");
1463
1464 llvm::APSInt Result;
1465 if (!EvaluateInteger(SubExpr, Result, Info))
1466 return APValue();
1467
1468 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1469 Zero = 0;
1470 return APValue(Zero, Result);
1471 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001472 }
1473
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001474 APValue VisitCastExpr(CastExpr *E) {
1475 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001476 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1477 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001478
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001479 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001480 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001481
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001482 if (!EvaluateFloat(SubExpr, Result, Info))
1483 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001484
1485 if (EltType->isRealFloatingType()) {
1486 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
1487 return APValue(Result,
1488 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1489 } else {
1490 llvm::APSInt IResult;
1491 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1492 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1493 Zero = 0;
1494 return APValue(IResult, Zero);
1495 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001496 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001497 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001498
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001499 if (!EvaluateInteger(SubExpr, Result, Info))
1500 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001501
Eli Friedman1725f682009-04-22 19:23:09 +00001502 if (EltType->isRealFloatingType()) {
1503 APFloat FResult =
1504 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
1505 return APValue(FResult,
1506 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1507 } else {
1508 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1509 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1510 Zero = 0;
1511 return APValue(Result, Zero);
1512 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001513 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1514 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001515
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001516 if (!EvaluateComplex(SubExpr, Src, Info))
1517 return APValue();
1518
1519 QualType SrcType = CT->getElementType();
1520
1521 if (Src.isComplexFloat()) {
1522 if (EltType->isRealFloatingType()) {
1523 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1524 Src.getComplexFloatReal(),
1525 Info.Ctx),
1526 HandleFloatToFloatCast(EltType, SrcType,
1527 Src.getComplexFloatImag(),
1528 Info.Ctx));
1529 } else {
1530 return APValue(HandleFloatToIntCast(EltType, SrcType,
1531 Src.getComplexFloatReal(),
1532 Info.Ctx),
1533 HandleFloatToIntCast(EltType, SrcType,
1534 Src.getComplexFloatImag(),
1535 Info.Ctx));
1536 }
1537 } else {
1538 assert(Src.isComplexInt() && "Invalid evaluate result.");
1539 if (EltType->isRealFloatingType()) {
1540 return APValue(HandleIntToFloatCast(EltType, SrcType,
1541 Src.getComplexIntReal(),
1542 Info.Ctx),
1543 HandleIntToFloatCast(EltType, SrcType,
1544 Src.getComplexIntImag(),
1545 Info.Ctx));
1546 } else {
1547 return APValue(HandleIntToIntCast(EltType, SrcType,
1548 Src.getComplexIntReal(),
1549 Info.Ctx),
1550 HandleIntToIntCast(EltType, SrcType,
1551 Src.getComplexIntImag(),
1552 Info.Ctx));
1553 }
1554 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001555 }
1556
1557 // FIXME: Handle more casts.
1558 return APValue();
1559 }
1560
1561 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001562 APValue VisitChooseExpr(const ChooseExpr *E)
1563 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1564 APValue VisitUnaryExtension(const UnaryOperator *E)
1565 { return Visit(E->getSubExpr()); }
1566 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001567 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001568};
1569} // end anonymous namespace
1570
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001571static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001572{
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001573 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1574 assert((!Result.isComplexFloat() ||
1575 (&Result.getComplexFloatReal().getSemantics() ==
1576 &Result.getComplexFloatImag().getSemantics())) &&
1577 "Invalid complex evaluation.");
1578 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001579}
1580
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001581APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001582{
1583 APValue Result, RHS;
1584
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001585 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001586 return APValue();
1587
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001588 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001589 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001590
Daniel Dunbar3f279872009-01-29 01:32:56 +00001591 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1592 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001593 switch (E->getOpcode()) {
1594 default: return APValue();
1595 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001596 if (Result.isComplexFloat()) {
1597 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1598 APFloat::rmNearestTiesToEven);
1599 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1600 APFloat::rmNearestTiesToEven);
1601 } else {
1602 Result.getComplexIntReal() += RHS.getComplexIntReal();
1603 Result.getComplexIntImag() += RHS.getComplexIntImag();
1604 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001605 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001606 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001607 if (Result.isComplexFloat()) {
1608 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1609 APFloat::rmNearestTiesToEven);
1610 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1611 APFloat::rmNearestTiesToEven);
1612 } else {
1613 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1614 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1615 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001616 break;
1617 case BinaryOperator::Mul:
1618 if (Result.isComplexFloat()) {
1619 APValue LHS = Result;
1620 APFloat &LHS_r = LHS.getComplexFloatReal();
1621 APFloat &LHS_i = LHS.getComplexFloatImag();
1622 APFloat &RHS_r = RHS.getComplexFloatReal();
1623 APFloat &RHS_i = RHS.getComplexFloatImag();
1624
1625 APFloat Tmp = LHS_r;
1626 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1627 Result.getComplexFloatReal() = Tmp;
1628 Tmp = LHS_i;
1629 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1630 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1631
1632 Tmp = LHS_r;
1633 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1634 Result.getComplexFloatImag() = Tmp;
1635 Tmp = LHS_i;
1636 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1637 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1638 } else {
1639 APValue LHS = Result;
1640 Result.getComplexIntReal() =
1641 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1642 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1643 Result.getComplexIntImag() =
1644 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1645 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1646 }
1647 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001648 }
1649
1650 return Result;
1651}
1652
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001653//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001654// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001655//===----------------------------------------------------------------------===//
1656
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001657/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001658/// any crazy technique (that has nothing to do with language standards) that
1659/// we want to. If this function returns true, it returns the folded constant
1660/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001661bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1662 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001663
Nate Begeman59b5da62009-01-18 03:20:47 +00001664 if (getType()->isVectorType()) {
1665 if (!EvaluateVector(this, Result.Val, Info))
1666 return false;
1667 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001668 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001669 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001670 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001671 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001672 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001673 } else if (getType()->isRealFloatingType()) {
1674 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001675 if (!EvaluateFloat(this, f, Info))
1676 return false;
1677
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001678 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001679 } else if (getType()->isAnyComplexType()) {
1680 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001681 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001682 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001683 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001684
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001685 return true;
1686}
1687
Anders Carlsson1b782762009-04-10 04:54:13 +00001688bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1689 EvalInfo Info(Ctx, Result);
1690
1691 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1692}
1693
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001694/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001695/// folded, but discard the result.
1696bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001697 EvalResult Result;
1698 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001699}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001700
1701APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001702 EvalResult EvalResult;
1703 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001704 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001705 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001706 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001707
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001708 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001709}