blob: e53804c86ddb714f55728b72c973839740278cfa [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"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
45struct EvalInfo {
46 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson54da0492008-11-30 16:38:33 +000048 /// EvalResult - Contains information about the evaluation.
49 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000050
Eli Friedmanb2f295c2009-09-13 10:17:44 +000051 /// AnyLValue - Stack based LValue results are not discarded.
52 bool AnyLValue;
53
54 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
55 bool anylvalue = false)
56 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000057};
58
John McCallf4cf1a12010-05-07 17:22:02 +000059namespace {
60 struct ComplexValue {
61 private:
62 bool IsInt;
63
64 public:
65 APSInt IntReal, IntImag;
66 APFloat FloatReal, FloatImag;
67
68 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
69
70 void makeComplexFloat() { IsInt = false; }
71 bool isComplexFloat() const { return !IsInt; }
72 APFloat &getComplexFloatReal() { return FloatReal; }
73 APFloat &getComplexFloatImag() { return FloatImag; }
74
75 void makeComplexInt() { IsInt = true; }
76 bool isComplexInt() const { return IsInt; }
77 APSInt &getComplexIntReal() { return IntReal; }
78 APSInt &getComplexIntImag() { return IntImag; }
79
80 void moveInto(APValue &v) {
81 if (isComplexFloat())
82 v = APValue(FloatReal, FloatImag);
83 else
84 v = APValue(IntReal, IntImag);
85 }
86 };
John McCallefdb83e2010-05-07 21:00:08 +000087
88 struct LValue {
89 Expr *Base;
90 CharUnits Offset;
91
92 Expr *getLValueBase() { return Base; }
93 CharUnits getLValueOffset() { return Offset; }
94
95 void moveInto(APValue &v) {
96 v = APValue(Base, Offset);
97 }
98 };
John McCallf4cf1a12010-05-07 17:22:02 +000099}
Chris Lattner87eae5e2008-07-11 22:52:41 +0000100
John McCallefdb83e2010-05-07 21:00:08 +0000101static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
102static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000103static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +0000104static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
105 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000106static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCallf4cf1a12010-05-07 17:22:02 +0000107static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000108
109//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000110// Misc utilities
111//===----------------------------------------------------------------------===//
112
John McCallefdb83e2010-05-07 21:00:08 +0000113static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
114 const Expr* Base = Value.Base;
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000115
John McCall35542832010-05-07 21:34:32 +0000116 // A null base expression indicates a null pointer. These are always
117 // evaluatable, and they are false unless the offset is zero.
118 if (!Base) {
119 Result = !Value.Offset.isZero();
120 return true;
121 }
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000122
John McCall35542832010-05-07 21:34:32 +0000123 // We have a non-null base expression. These are generally known to
124 // be true, but if it'a decl-ref to a weak symbol it can be null at
125 // runtime.
126
127 Result = true;
128
129 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000130 if (!DeclRef)
131 return true;
132
John McCall35542832010-05-07 21:34:32 +0000133 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000134 const ValueDecl* Decl = DeclRef->getDecl();
135 if (Decl->hasAttr<WeakAttr>() ||
136 Decl->hasAttr<WeakRefAttr>() ||
137 Decl->hasAttr<WeakImportAttr>())
138 return false;
139
Eli Friedman5bc86102009-06-14 02:17:33 +0000140 return true;
141}
142
John McCallcd7a4452010-01-05 23:42:56 +0000143static bool HandleConversionToBool(const Expr* E, bool& Result,
144 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000145 if (E->getType()->isIntegralType()) {
146 APSInt IntResult;
147 if (!EvaluateInteger(E, IntResult, Info))
148 return false;
149 Result = IntResult != 0;
150 return true;
151 } else if (E->getType()->isRealFloatingType()) {
152 APFloat FloatResult(0.0);
153 if (!EvaluateFloat(E, FloatResult, Info))
154 return false;
155 Result = !FloatResult.isZero();
156 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000157 } else if (E->getType()->hasPointerRepresentation()) {
John McCallefdb83e2010-05-07 21:00:08 +0000158 LValue PointerResult;
Eli Friedman4efaa272008-11-12 09:44:48 +0000159 if (!EvaluatePointer(E, PointerResult, Info))
160 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +0000161 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +0000162 } else if (E->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +0000163 ComplexValue ComplexResult;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000164 if (!EvaluateComplex(E, ComplexResult, Info))
165 return false;
166 if (ComplexResult.isComplexFloat()) {
167 Result = !ComplexResult.getComplexFloatReal().isZero() ||
168 !ComplexResult.getComplexFloatImag().isZero();
169 } else {
170 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
171 ComplexResult.getComplexIntImag().getBoolValue();
172 }
173 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000174 }
175
176 return false;
177}
178
Mike Stump1eb44332009-09-09 15:08:12 +0000179static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000180 APFloat &Value, ASTContext &Ctx) {
181 unsigned DestWidth = Ctx.getIntWidth(DestType);
182 // Determine whether we are converting to unsigned or signed.
183 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000185 // FIXME: Warning for overflow.
186 uint64_t Space[4];
187 bool ignored;
188 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
189 llvm::APFloat::rmTowardZero, &ignored);
190 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
191}
192
Mike Stump1eb44332009-09-09 15:08:12 +0000193static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000194 APFloat &Value, ASTContext &Ctx) {
195 bool ignored;
196 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000198 APFloat::rmNearestTiesToEven, &ignored);
199 return Result;
200}
201
Mike Stump1eb44332009-09-09 15:08:12 +0000202static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000203 APSInt &Value, ASTContext &Ctx) {
204 unsigned DestWidth = Ctx.getIntWidth(DestType);
205 APSInt Result = Value;
206 // Figure out if this is a truncate, extend or noop cast.
207 // If the input is signed, do a sign extend, noop, or truncate.
208 Result.extOrTrunc(DestWidth);
209 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
210 return Result;
211}
212
Mike Stump1eb44332009-09-09 15:08:12 +0000213static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000214 APSInt &Value, ASTContext &Ctx) {
215
216 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
217 Result.convertFromAPInt(Value, Value.isSigned(),
218 APFloat::rmNearestTiesToEven);
219 return Result;
220}
221
Mike Stumpc4c90452009-10-27 22:09:17 +0000222namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000223class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000224 : public StmtVisitor<HasSideEffect, bool> {
225 EvalInfo &Info;
226public:
227
228 HasSideEffect(EvalInfo &info) : Info(info) {}
229
230 // Unhandled nodes conservatively default to having side effects.
231 bool VisitStmt(Stmt *S) {
232 return true;
233 }
234
235 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
236 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000237 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000238 return true;
239 return false;
240 }
241 // We don't want to evaluate BlockExprs multiple times, as they generate
242 // a ton of code.
243 bool VisitBlockExpr(BlockExpr *E) { return true; }
244 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
245 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
246 { return Visit(E->getInitializer()); }
247 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
248 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
249 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
250 bool VisitStringLiteral(StringLiteral *E) { return false; }
251 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
252 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
253 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000254 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000255 bool VisitChooseExpr(ChooseExpr *E)
256 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
257 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
258 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000259 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000260 bool VisitBinaryOperator(BinaryOperator *E)
261 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000262 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
263 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
264 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
265 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
266 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000267 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000268 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000269 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000270 }
271 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000272
273 // Has side effects if any element does.
274 bool VisitInitListExpr(InitListExpr *E) {
275 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
276 if (Visit(E->getInit(i))) return true;
277 return false;
278 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000279};
280
Mike Stumpc4c90452009-10-27 22:09:17 +0000281} // end anonymous namespace
282
Eli Friedman4efaa272008-11-12 09:44:48 +0000283//===----------------------------------------------------------------------===//
284// LValue Evaluation
285//===----------------------------------------------------------------------===//
286namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000287class LValueExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000288 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman4efaa272008-11-12 09:44:48 +0000289 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000290 LValue &Result;
291
292 bool Success(Expr *E) {
293 Result.Base = E;
294 Result.Offset = CharUnits::Zero();
295 return true;
296 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000297public:
Mike Stump1eb44332009-09-09 15:08:12 +0000298
John McCallefdb83e2010-05-07 21:00:08 +0000299 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
300 Info(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000301
John McCallefdb83e2010-05-07 21:00:08 +0000302 bool VisitStmt(Stmt *S) {
303 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000304 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000305
John McCallefdb83e2010-05-07 21:00:08 +0000306 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
307 bool VisitDeclRefExpr(DeclRefExpr *E);
308 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
309 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
310 bool VisitMemberExpr(MemberExpr *E);
311 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
312 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
313 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
314 bool VisitUnaryDeref(UnaryOperator *E);
315 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000316 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000317 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000318 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000319
John McCallefdb83e2010-05-07 21:00:08 +0000320 bool VisitCastExpr(CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000321 switch (E->getCastKind()) {
322 default:
John McCallefdb83e2010-05-07 21:00:08 +0000323 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000324
325 case CastExpr::CK_NoOp:
326 return Visit(E->getSubExpr());
327 }
328 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000329 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000330};
331} // end anonymous namespace
332
John McCallefdb83e2010-05-07 21:00:08 +0000333static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
334 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman4efaa272008-11-12 09:44:48 +0000335}
336
John McCallefdb83e2010-05-07 21:00:08 +0000337bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000338 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000339 return Success(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000340 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000341 if (!Info.AnyLValue && !VD->hasGlobalStorage())
John McCallefdb83e2010-05-07 21:00:08 +0000342 return false;
Eli Friedman50c39ea2009-05-27 06:04:58 +0000343 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000344 return Success(E);
Eli Friedmand933a012009-08-29 19:09:59 +0000345 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000346 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000347 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000348 }
349
John McCallefdb83e2010-05-07 21:00:08 +0000350 return false;
Anders Carlsson35873c42008-11-24 04:41:22 +0000351}
352
John McCallefdb83e2010-05-07 21:00:08 +0000353bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000354 if (!Info.AnyLValue && !E->isFileScope())
John McCallefdb83e2010-05-07 21:00:08 +0000355 return false;
356 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000357}
358
John McCallefdb83e2010-05-07 21:00:08 +0000359bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000360 QualType Ty;
361 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000362 if (!EvaluatePointer(E->getBase(), Result, Info))
363 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000364 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000365 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000366 if (!Visit(E->getBase()))
367 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000368 Ty = E->getBase()->getType();
369 }
370
Ted Kremenek6217b802009-07-29 21:53:49 +0000371 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000372 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000373
374 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
375 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000376 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000377
378 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000379 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000380
Eli Friedman4efaa272008-11-12 09:44:48 +0000381 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000382 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000383 for (RecordDecl::field_iterator Field = RD->field_begin(),
384 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000385 Field != FieldEnd; (void)++Field, ++i) {
386 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000387 break;
388 }
389
John McCallefdb83e2010-05-07 21:00:08 +0000390 Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8);
391 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000392}
393
John McCallefdb83e2010-05-07 21:00:08 +0000394bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000395 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000396 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlsson3068d112008-11-16 19:01:22 +0000398 APSInt Index;
399 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000400 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000401
Ken Dyck199c3d62010-01-11 17:06:35 +0000402 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000403 Result.Offset += Index.getSExtValue() * ElementSize;
404 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000405}
Eli Friedman4efaa272008-11-12 09:44:48 +0000406
John McCallefdb83e2010-05-07 21:00:08 +0000407bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
408 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000409}
410
Eli Friedman4efaa272008-11-12 09:44:48 +0000411//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412// Pointer Evaluation
413//===----------------------------------------------------------------------===//
414
Anders Carlssonc754aa62008-07-08 05:13:58 +0000415namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000416class PointerExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000417 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000418 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000419 LValue &Result;
420
421 bool Success(Expr *E) {
422 Result.Base = E;
423 Result.Offset = CharUnits::Zero();
424 return true;
425 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000426public:
Mike Stump1eb44332009-09-09 15:08:12 +0000427
John McCallefdb83e2010-05-07 21:00:08 +0000428 PointerExprEvaluator(EvalInfo &info, LValue &Result)
429 : Info(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000430
John McCallefdb83e2010-05-07 21:00:08 +0000431 bool VisitStmt(Stmt *S) {
432 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000433 }
434
John McCallefdb83e2010-05-07 21:00:08 +0000435 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000436
John McCallefdb83e2010-05-07 21:00:08 +0000437 bool VisitBinaryOperator(const BinaryOperator *E);
438 bool VisitCastExpr(CastExpr* E);
439 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman2217c872009-02-22 11:46:18 +0000440 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000441 bool VisitUnaryAddrOf(const UnaryOperator *E);
442 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
443 { return Success(E); }
444 bool VisitAddrLabelExpr(AddrLabelExpr *E)
445 { return Success(E); }
446 bool VisitCallExpr(CallExpr *E);
447 bool VisitBlockExpr(BlockExpr *E) {
Mike Stumpb83d2872009-02-19 22:01:56 +0000448 if (!E->hasBlockDeclRefExprs())
John McCallefdb83e2010-05-07 21:00:08 +0000449 return Success(E);
450 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000451 }
John McCallefdb83e2010-05-07 21:00:08 +0000452 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
453 { return Success((Expr*)0); }
454 bool VisitConditionalOperator(ConditionalOperator *E);
455 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000456 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallefdb83e2010-05-07 21:00:08 +0000457 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
458 { return Success((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000459 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000460};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000461} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000462
John McCallefdb83e2010-05-07 21:00:08 +0000463static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000464 assert(E->getType()->hasPointerRepresentation());
John McCallefdb83e2010-05-07 21:00:08 +0000465 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000466}
467
John McCallefdb83e2010-05-07 21:00:08 +0000468bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000469 if (E->getOpcode() != BinaryOperator::Add &&
470 E->getOpcode() != BinaryOperator::Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000471 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000473 const Expr *PExp = E->getLHS();
474 const Expr *IExp = E->getRHS();
475 if (IExp->getType()->isPointerType())
476 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
John McCallefdb83e2010-05-07 21:00:08 +0000478 if (!EvaluatePointer(PExp, Result, Info))
479 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
John McCallefdb83e2010-05-07 21:00:08 +0000481 llvm::APSInt Offset;
482 if (!EvaluateInteger(IExp, Offset, Info))
483 return false;
484 int64_t AdditionalOffset
485 = Offset.isSigned() ? Offset.getSExtValue()
486 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000487
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000488 // Compute the new offset in the appropriate width.
489
490 QualType PointeeType =
491 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000492 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000494 // Explicitly handle GNU void* and function pointer arithmetic extensions.
495 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000496 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000497 else
John McCallefdb83e2010-05-07 21:00:08 +0000498 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000499
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000500 if (E->getOpcode() == BinaryOperator::Add)
John McCallefdb83e2010-05-07 21:00:08 +0000501 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000502 else
John McCallefdb83e2010-05-07 21:00:08 +0000503 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000504
John McCallefdb83e2010-05-07 21:00:08 +0000505 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000506}
Eli Friedman4efaa272008-11-12 09:44:48 +0000507
John McCallefdb83e2010-05-07 21:00:08 +0000508bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
509 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000510}
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000512
John McCallefdb83e2010-05-07 21:00:08 +0000513bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000514 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000515
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000516 switch (E->getCastKind()) {
517 default:
518 break;
519
520 case CastExpr::CK_Unknown: {
521 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
522
523 // Check for pointer->pointer cast
524 if (SubExpr->getType()->isPointerType() ||
525 SubExpr->getType()->isObjCObjectPointerType() ||
526 SubExpr->getType()->isNullPtrType() ||
527 SubExpr->getType()->isBlockPointerType())
528 return Visit(SubExpr);
529
530 if (SubExpr->getType()->isIntegralType()) {
John McCallefdb83e2010-05-07 21:00:08 +0000531 APValue Value;
532 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000533 break;
534
John McCallefdb83e2010-05-07 21:00:08 +0000535 if (Value.isInt()) {
536 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
537 Result.Base = 0;
538 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
539 return true;
540 } else {
541 Result.Base = Value.getLValueBase();
542 Result.Offset = Value.getLValueOffset();
543 return true;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000544 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000545 }
546 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000549 case CastExpr::CK_NoOp:
550 case CastExpr::CK_BitCast:
551 case CastExpr::CK_AnyPointerToObjCPointerCast:
552 case CastExpr::CK_AnyPointerToBlockPointerCast:
553 return Visit(SubExpr);
554
555 case CastExpr::CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000556 APValue Value;
557 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000558 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000559
John McCallefdb83e2010-05-07 21:00:08 +0000560 if (Value.isInt()) {
561 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
562 Result.Base = 0;
563 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
564 return true;
565 } else {
566 // Cast is of an lvalue, no need to change value.
567 Result.Base = Value.getLValueBase();
568 Result.Offset = Value.getLValueOffset();
569 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000570 }
571 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000572 case CastExpr::CK_ArrayToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000573 case CastExpr::CK_FunctionToPointerDecay:
574 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000575 }
576
John McCallefdb83e2010-05-07 21:00:08 +0000577 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000578}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000579
John McCallefdb83e2010-05-07 21:00:08 +0000580bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000581 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000582 Builtin::BI__builtin___CFStringMakeConstantString ||
583 E->isBuiltinCall(Info.Ctx) ==
584 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000585 return Success(E);
586 return false;
Eli Friedman3941b182009-01-25 01:54:01 +0000587}
588
John McCallefdb83e2010-05-07 21:00:08 +0000589bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000590 bool BoolResult;
591 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000592 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000593
594 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCallefdb83e2010-05-07 21:00:08 +0000595 return Visit(EvalExpr);
Eli Friedman4efaa272008-11-12 09:44:48 +0000596}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000597
598//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000599// Vector Evaluation
600//===----------------------------------------------------------------------===//
601
602namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000603 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000604 : public StmtVisitor<VectorExprEvaluator, APValue> {
605 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000606 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000607 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Nate Begeman59b5da62009-01-18 03:20:47 +0000609 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Nate Begeman59b5da62009-01-18 03:20:47 +0000611 APValue VisitStmt(Stmt *S) {
612 return APValue();
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Eli Friedman91110ee2009-02-23 04:23:56 +0000615 APValue VisitParenExpr(ParenExpr *E)
616 { return Visit(E->getSubExpr()); }
617 APValue VisitUnaryExtension(const UnaryOperator *E)
618 { return Visit(E->getSubExpr()); }
619 APValue VisitUnaryPlus(const UnaryOperator *E)
620 { return Visit(E->getSubExpr()); }
621 APValue VisitUnaryReal(const UnaryOperator *E)
622 { return Visit(E->getSubExpr()); }
623 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
624 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000625 APValue VisitCastExpr(const CastExpr* E);
626 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
627 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000628 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000629 APValue VisitChooseExpr(const ChooseExpr *E)
630 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000631 APValue VisitUnaryImag(const UnaryOperator *E);
632 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000633 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000634 // shufflevector, ExtVectorElementExpr
635 // (Note that these require implementing conversions
636 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000637 };
638} // end anonymous namespace
639
640static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
641 if (!E->getType()->isVectorType())
642 return false;
643 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
644 return !Result.isUninit();
645}
646
647APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000648 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000649 QualType EltTy = VTy->getElementType();
650 unsigned NElts = VTy->getNumElements();
651 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Nate Begeman59b5da62009-01-18 03:20:47 +0000653 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000654 QualType SETy = SE->getType();
655 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000656
Nate Begemane8c9e922009-06-26 18:22:18 +0000657 // Check for vector->vector bitcast and scalar->vector splat.
658 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000659 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000660 } else if (SETy->isIntegerType()) {
661 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000662 if (!EvaluateInteger(SE, IntResult, Info))
663 return APValue();
664 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000665 } else if (SETy->isRealFloatingType()) {
666 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000667 if (!EvaluateFloat(SE, F, Info))
668 return APValue();
669 Result = APValue(F);
670 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000671 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000672
Nate Begemanc0b8b192009-07-01 07:50:47 +0000673 // For casts of a scalar to ExtVector, convert the scalar to the element type
674 // and splat it to all elements.
675 if (E->getType()->isExtVectorType()) {
676 if (EltTy->isIntegerType() && Result.isInt())
677 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
678 Info.Ctx));
679 else if (EltTy->isIntegerType())
680 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
681 Info.Ctx));
682 else if (EltTy->isRealFloatingType() && Result.isInt())
683 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
684 Info.Ctx));
685 else if (EltTy->isRealFloatingType())
686 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
687 Info.Ctx));
688 else
689 return APValue();
690
691 // Splat and create vector APValue.
692 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
693 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000694 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000695
696 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
697 // to the vector. To construct the APValue vector initializer, bitcast the
698 // initializing value to an APInt, and shift out the bits pertaining to each
699 // element.
700 APSInt Init;
701 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Nate Begemanc0b8b192009-07-01 07:50:47 +0000703 llvm::SmallVector<APValue, 4> Elts;
704 for (unsigned i = 0; i != NElts; ++i) {
705 APSInt Tmp = Init;
706 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Nate Begemanc0b8b192009-07-01 07:50:47 +0000708 if (EltTy->isIntegerType())
709 Elts.push_back(APValue(Tmp));
710 else if (EltTy->isRealFloatingType())
711 Elts.push_back(APValue(APFloat(Tmp)));
712 else
713 return APValue();
714
715 Init >>= EltWidth;
716 }
717 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000718}
719
Mike Stump1eb44332009-09-09 15:08:12 +0000720APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000721VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
722 return this->Visit(const_cast<Expr*>(E->getInitializer()));
723}
724
Mike Stump1eb44332009-09-09 15:08:12 +0000725APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000726VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000727 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000728 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000729 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Nate Begeman59b5da62009-01-18 03:20:47 +0000731 QualType EltTy = VT->getElementType();
732 llvm::SmallVector<APValue, 4> Elements;
733
Eli Friedman91110ee2009-02-23 04:23:56 +0000734 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000735 if (EltTy->isIntegerType()) {
736 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000737 if (i < NumInits) {
738 if (!EvaluateInteger(E->getInit(i), sInt, Info))
739 return APValue();
740 } else {
741 sInt = Info.Ctx.MakeIntValue(0, EltTy);
742 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000743 Elements.push_back(APValue(sInt));
744 } else {
745 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000746 if (i < NumInits) {
747 if (!EvaluateFloat(E->getInit(i), f, Info))
748 return APValue();
749 } else {
750 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
751 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000752 Elements.push_back(APValue(f));
753 }
754 }
755 return APValue(&Elements[0], Elements.size());
756}
757
Mike Stump1eb44332009-09-09 15:08:12 +0000758APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000759VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000760 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000761 QualType EltTy = VT->getElementType();
762 APValue ZeroElement;
763 if (EltTy->isIntegerType())
764 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
765 else
766 ZeroElement =
767 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
768
769 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
770 return APValue(&Elements[0], Elements.size());
771}
772
773APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
774 bool BoolResult;
775 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
776 return APValue();
777
778 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
779
780 APValue Result;
781 if (EvaluateVector(EvalExpr, Result, Info))
782 return Result;
783 return APValue();
784}
785
Eli Friedman91110ee2009-02-23 04:23:56 +0000786APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
787 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
788 Info.EvalResult.HasSideEffects = true;
789 return GetZeroVector(E->getType());
790}
791
Nate Begeman59b5da62009-01-18 03:20:47 +0000792//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000793// Integer Evaluation
794//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000795
796namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000797class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000798 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000799 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000800 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000801public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000802 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000803 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000804
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000805 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000806 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000807 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000808 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000809 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000810 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000811 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000812 return true;
813 }
814
Daniel Dunbar131eb432009-02-19 09:06:44 +0000815 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000816 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000817 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000818 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000819 Result = APValue(APSInt(I));
820 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000821 return true;
822 }
823
824 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000825 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000826 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000827 return true;
828 }
829
Anders Carlsson82206e22008-11-30 18:14:57 +0000830 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000831 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000832 if (Info.EvalResult.Diag == 0) {
833 Info.EvalResult.DiagLoc = L;
834 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000835 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000836 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000837 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Anders Carlssonc754aa62008-07-08 05:13:58 +0000840 //===--------------------------------------------------------------------===//
841 // Visitor Methods
842 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattner32fea9d2008-11-12 07:43:42 +0000844 bool VisitStmt(Stmt *) {
845 assert(0 && "This should be called on integers, stmts are not integers");
846 return false;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Chris Lattner32fea9d2008-11-12 07:43:42 +0000849 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000850 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattnerb542afe2008-07-11 19:10:17 +0000853 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000854
Chris Lattner4c4867e2008-07-12 00:38:25 +0000855 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000856 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000857 }
858 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000859 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000860 }
861 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000862 // Per gcc docs "this built-in function ignores top level
863 // qualifiers". We need to use the canonical version to properly
864 // be able to strip CRV qualifiers from the type.
865 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
866 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000867 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000868 T1.getUnqualifiedType()),
869 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000870 }
Eli Friedman04309752009-11-24 05:28:59 +0000871
872 bool CheckReferencedDecl(const Expr *E, const Decl *D);
873 bool VisitDeclRefExpr(const DeclRefExpr *E) {
874 return CheckReferencedDecl(E, E->getDecl());
875 }
876 bool VisitMemberExpr(const MemberExpr *E) {
877 if (CheckReferencedDecl(E, E->getMemberDecl())) {
878 // Conservatively assume a MemberExpr will have side-effects
879 Info.EvalResult.HasSideEffects = true;
880 return true;
881 }
882 return false;
883 }
884
Eli Friedmanc4a26382010-02-13 00:10:10 +0000885 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000886 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000887 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000888 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000889 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000890
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000891 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000892 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
893
Anders Carlsson3068d112008-11-16 19:01:22 +0000894 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000895 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Anders Carlsson3f704562008-12-21 22:39:40 +0000898 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000899 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Anders Carlsson3068d112008-11-16 19:01:22 +0000902 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000903 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000904 }
905
Eli Friedman664a1042009-02-27 04:45:43 +0000906 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
907 return Success(0, E);
908 }
909
Sebastian Redl64b45f72009-01-05 20:52:13 +0000910 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000911 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000912 }
913
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000914 bool VisitChooseExpr(const ChooseExpr *E) {
915 return Visit(E->getChosenSubExpr(Info.Ctx));
916 }
917
Eli Friedman722c7172009-02-28 03:59:05 +0000918 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000919 bool VisitUnaryImag(const UnaryOperator *E);
920
Chris Lattnerfcee0012008-07-11 21:24:13 +0000921private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000922 CharUnits GetAlignOfExpr(const Expr *E);
923 CharUnits GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000924 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000925};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000926} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000927
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000928static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000929 assert(E->getType()->isIntegralType());
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000930 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
931}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000932
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000933static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000934 assert(E->getType()->isIntegralType());
935
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000936 APValue Val;
937 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
938 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000939 Result = Val.getInt();
940 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000941}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000942
Eli Friedman04309752009-11-24 05:28:59 +0000943bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000944 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000945 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
946 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000947
948 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000949 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000950 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
951 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000952
953 if (isa<ParmVarDecl>(D))
954 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
955
Eli Friedman04309752009-11-24 05:28:59 +0000956 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000957 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000958 if (APValue *V = VD->getEvaluatedValue()) {
959 if (V->isInt())
960 return Success(V->getInt(), E);
961 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
962 }
963
964 if (VD->isEvaluatingValue())
965 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
966
967 VD->setEvaluatingValue();
968
Douglas Gregor78d15832009-05-26 18:54:04 +0000969 if (Visit(const_cast<Expr*>(Init))) {
970 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000971 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000972 return true;
973 }
974
Eli Friedmanc0131182009-12-03 20:31:57 +0000975 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000976 return false;
977 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000978 }
979 }
980
Chris Lattner4c4867e2008-07-12 00:38:25 +0000981 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000982 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000983}
984
Chris Lattnera4d55d82008-10-06 06:40:35 +0000985/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
986/// as GCC.
987static int EvaluateBuiltinClassifyType(const CallExpr *E) {
988 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000989 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000990 enum gcc_type_class {
991 no_type_class = -1,
992 void_type_class, integer_type_class, char_type_class,
993 enumeral_type_class, boolean_type_class,
994 pointer_type_class, reference_type_class, offset_type_class,
995 real_type_class, complex_type_class,
996 function_type_class, method_type_class,
997 record_type_class, union_type_class,
998 array_type_class, string_type_class,
999 lang_type_class
1000 };
Mike Stump1eb44332009-09-09 15:08:12 +00001001
1002 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001003 // ideal, however it is what gcc does.
1004 if (E->getNumArgs() == 0)
1005 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattnera4d55d82008-10-06 06:40:35 +00001007 QualType ArgTy = E->getArg(0)->getType();
1008 if (ArgTy->isVoidType())
1009 return void_type_class;
1010 else if (ArgTy->isEnumeralType())
1011 return enumeral_type_class;
1012 else if (ArgTy->isBooleanType())
1013 return boolean_type_class;
1014 else if (ArgTy->isCharType())
1015 return string_type_class; // gcc doesn't appear to use char_type_class
1016 else if (ArgTy->isIntegerType())
1017 return integer_type_class;
1018 else if (ArgTy->isPointerType())
1019 return pointer_type_class;
1020 else if (ArgTy->isReferenceType())
1021 return reference_type_class;
1022 else if (ArgTy->isRealType())
1023 return real_type_class;
1024 else if (ArgTy->isComplexType())
1025 return complex_type_class;
1026 else if (ArgTy->isFunctionType())
1027 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001028 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001029 return record_type_class;
1030 else if (ArgTy->isUnionType())
1031 return union_type_class;
1032 else if (ArgTy->isArrayType())
1033 return array_type_class;
1034 else if (ArgTy->isUnionType())
1035 return union_type_class;
1036 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1037 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1038 return -1;
1039}
1040
Eli Friedmanc4a26382010-02-13 00:10:10 +00001041bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001042 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001043 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001044 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001045
1046 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +00001047 const Expr *Arg = E->getArg(0)->IgnoreParens();
1048 Expr::EvalResult Base;
Eric Christopherb2aaf512010-01-19 22:58:35 +00001049
1050 // TODO: Perhaps we should let LLVM lower this?
Mike Stump660e6f72009-10-26 23:05:19 +00001051 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +00001052 && Base.Val.getKind() == APValue::LValue
1053 && !Base.HasSideEffects)
1054 if (const Expr *LVBase = Base.Val.getLValueBase())
1055 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
1056 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +00001057 if (!VD->getType()->isIncompleteType()
1058 && VD->getType()->isObjectType()
1059 && !VD->getType()->isVariablyModifiedType()
1060 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +00001061 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +00001062 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +00001063 if (!Offset.isNegative() && Offset <= Size)
1064 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +00001065 else
Ken Dyck199c3d62010-01-11 17:06:35 +00001066 Size = CharUnits::Zero();
1067 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +00001068 }
Mike Stump64eda9e2009-10-26 18:35:08 +00001069 }
1070 }
1071
Eric Christopherb2aaf512010-01-19 22:58:35 +00001072 // If evaluating the argument has side-effects we can't determine
1073 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001074 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001075 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001076 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001077 return Success(0, E);
1078 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001079
Mike Stump64eda9e2009-10-26 18:35:08 +00001080 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1081 }
1082
Chris Lattner019f4e82008-10-06 05:28:25 +00001083 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001084 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001086 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001087 // __builtin_constant_p always has one operand: it returns true if that
1088 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001089 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001090
1091 case Builtin::BI__builtin_eh_return_data_regno: {
1092 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1093 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1094 return Success(Operand, E);
1095 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001096
1097 case Builtin::BI__builtin_expect:
1098 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001099 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001100}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001101
Chris Lattnerb542afe2008-07-11 19:10:17 +00001102bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001103 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001104 if (!Visit(E->getRHS()))
1105 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001106
Eli Friedman33ef1452009-02-26 10:19:36 +00001107 // If we can't evaluate the LHS, it might have side effects;
1108 // conservatively mark it.
1109 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1110 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001111
Anders Carlsson027f62e2008-12-01 02:07:06 +00001112 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001113 }
1114
1115 if (E->isLogicalOp()) {
1116 // These need to be handled specially because the operands aren't
1117 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001118 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001120 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001121 // We were able to evaluate the LHS, see if we can get away with not
1122 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001123 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001124 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001125
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001126 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001127 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001128 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001129 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001130 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001131 }
1132 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001133 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001134 // We can't evaluate the LHS; however, sometimes the result
1135 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001136 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001137 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001138 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001139 // must have had side effects.
1140 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001141
1142 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001143 }
1144 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001145 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001146
Eli Friedmana6afa762008-11-13 06:09:17 +00001147 return false;
1148 }
1149
Anders Carlsson286f85e2008-11-16 07:17:21 +00001150 QualType LHSTy = E->getLHS()->getType();
1151 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001152
1153 if (LHSTy->isAnyComplexType()) {
1154 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001155 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001156
1157 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1158 return false;
1159
1160 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1161 return false;
1162
1163 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001164 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001165 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001166 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001167 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1168
Daniel Dunbar4087e242009-01-29 06:43:41 +00001169 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001170 return Success((CR_r == APFloat::cmpEqual &&
1171 CR_i == APFloat::cmpEqual), E);
1172 else {
1173 assert(E->getOpcode() == BinaryOperator::NE &&
1174 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001175 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001176 CR_r == APFloat::cmpLessThan ||
1177 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001178 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001179 CR_i == APFloat::cmpLessThan ||
1180 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001181 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001182 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001183 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001184 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1185 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1186 else {
1187 assert(E->getOpcode() == BinaryOperator::NE &&
1188 "Invalid compex comparison.");
1189 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1190 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1191 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001192 }
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Anders Carlsson286f85e2008-11-16 07:17:21 +00001195 if (LHSTy->isRealFloatingType() &&
1196 RHSTy->isRealFloatingType()) {
1197 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Anders Carlsson286f85e2008-11-16 07:17:21 +00001199 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1200 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Anders Carlsson286f85e2008-11-16 07:17:21 +00001202 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1203 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Anders Carlsson286f85e2008-11-16 07:17:21 +00001205 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001206
Anders Carlsson286f85e2008-11-16 07:17:21 +00001207 switch (E->getOpcode()) {
1208 default:
1209 assert(0 && "Invalid binary operator!");
1210 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001211 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001212 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001213 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001214 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001215 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001216 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001217 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001218 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001219 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001220 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001221 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001222 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001223 || CR == APFloat::cmpLessThan
1224 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001225 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001228 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1229 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001230 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001231 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1232 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001233
John McCallefdb83e2010-05-07 21:00:08 +00001234 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001235 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1236 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001237
Eli Friedman5bc86102009-06-14 02:17:33 +00001238 // Reject any bases from the normal codepath; we special-case comparisons
1239 // to null.
1240 if (LHSValue.getLValueBase()) {
1241 if (!E->isEqualityOp())
1242 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001243 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001244 return false;
1245 bool bres;
1246 if (!EvalPointerValueAsBool(LHSValue, bres))
1247 return false;
1248 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1249 } else if (RHSValue.getLValueBase()) {
1250 if (!E->isEqualityOp())
1251 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001252 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001253 return false;
1254 bool bres;
1255 if (!EvalPointerValueAsBool(RHSValue, bres))
1256 return false;
1257 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1258 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001259
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001260 if (E->getOpcode() == BinaryOperator::Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001261 QualType Type = E->getLHS()->getType();
1262 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001263
Ken Dycka7305832010-01-15 12:37:54 +00001264 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001265 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001266 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001267
Ken Dycka7305832010-01-15 12:37:54 +00001268 CharUnits Diff = LHSValue.getLValueOffset() -
1269 RHSValue.getLValueOffset();
1270 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001271 }
1272 bool Result;
1273 if (E->getOpcode() == BinaryOperator::EQ) {
1274 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001275 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001276 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1277 }
1278 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001279 }
1280 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001281 if (!LHSTy->isIntegralType() ||
1282 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001283 // We can't continue from here for non-integral types, and they
1284 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001285 return false;
1286 }
1287
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001288 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001289 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001290 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001291
Eli Friedman42edd0d2009-03-24 01:14:50 +00001292 APValue RHSVal;
1293 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001294 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001295
1296 // Handle cases like (unsigned long)&a + 4.
1297 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001298 CharUnits Offset = Result.getLValueOffset();
1299 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1300 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001301 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001302 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001303 else
Ken Dycka7305832010-01-15 12:37:54 +00001304 Offset -= AdditionalOffset;
1305 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001306 return true;
1307 }
1308
1309 // Handle cases like 4 + (unsigned long)&a
1310 if (E->getOpcode() == BinaryOperator::Add &&
1311 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001312 CharUnits Offset = RHSVal.getLValueOffset();
1313 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1314 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001315 return true;
1316 }
1317
1318 // All the following cases expect both operands to be an integer
1319 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001320 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001321
Eli Friedman42edd0d2009-03-24 01:14:50 +00001322 APSInt& RHS = RHSVal.getInt();
1323
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001324 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001325 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001326 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001327 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1328 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1329 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1330 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1331 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1332 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001333 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001334 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001335 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001336 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001337 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001338 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001339 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001340 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001341 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001342 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001343 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001344 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1345 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001346 }
1347 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001348 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001349 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1350 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001351 }
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001353 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1354 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1355 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1356 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1357 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1358 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001359 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001360}
1361
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001362bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001363 bool Cond;
1364 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001365 return false;
1366
Nuno Lopesa25bd552008-11-16 22:06:39 +00001367 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001368}
1369
Ken Dyck8b752f12010-01-27 17:10:57 +00001370CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001371 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1372 // the result is the size of the referenced type."
1373 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1374 // result shall be the alignment of the referenced type."
1375 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1376 T = Ref->getPointeeType();
1377
Chris Lattnere9feb472009-01-24 21:09:06 +00001378 // Get information about the alignment.
1379 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001380
Eli Friedman2be58612009-05-30 21:09:44 +00001381 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001382 return CharUnits::fromQuantity(
1383 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001384}
1385
Ken Dyck8b752f12010-01-27 17:10:57 +00001386CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001387 E = E->IgnoreParens();
1388
1389 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001391 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001392 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1393 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001394
Chris Lattneraf707ab2009-01-24 21:53:27 +00001395 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001396 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1397 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001398
Chris Lattnere9feb472009-01-24 21:09:06 +00001399 return GetAlignOfType(E->getType());
1400}
1401
1402
Sebastian Redl05189992008-11-11 17:56:53 +00001403/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1404/// expression's type.
1405bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001406 // Handle alignof separately.
1407 if (!E->isSizeOf()) {
1408 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001409 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001410 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001411 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001412 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001413
Sebastian Redl05189992008-11-11 17:56:53 +00001414 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001415 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1416 // the result is the size of the referenced type."
1417 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1418 // result shall be the alignment of the referenced type."
1419 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1420 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001421
Daniel Dunbar131eb432009-02-19 09:06:44 +00001422 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1423 // extension.
1424 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1425 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001426
Chris Lattnerfcee0012008-07-11 21:24:13 +00001427 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001428 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001429 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001430
Chris Lattnere9feb472009-01-24 21:09:06 +00001431 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001432 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001433}
1434
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001435bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1436 CharUnits Result;
1437 unsigned n = E->getNumComponents();
1438 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1439 if (n == 0)
1440 return false;
1441 QualType CurrentType = E->getTypeSourceInfo()->getType();
1442 for (unsigned i = 0; i != n; ++i) {
1443 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1444 switch (ON.getKind()) {
1445 case OffsetOfExpr::OffsetOfNode::Array: {
1446 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1447 APSInt IdxResult;
1448 if (!EvaluateInteger(Idx, IdxResult, Info))
1449 return false;
1450 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1451 if (!AT)
1452 return false;
1453 CurrentType = AT->getElementType();
1454 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1455 Result += IdxResult.getSExtValue() * ElementSize;
1456 break;
1457 }
1458
1459 case OffsetOfExpr::OffsetOfNode::Field: {
1460 FieldDecl *MemberDecl = ON.getField();
1461 const RecordType *RT = CurrentType->getAs<RecordType>();
1462 if (!RT)
1463 return false;
1464 RecordDecl *RD = RT->getDecl();
1465 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1466 unsigned i = 0;
1467 // FIXME: It would be nice if we didn't have to loop here!
1468 for (RecordDecl::field_iterator Field = RD->field_begin(),
1469 FieldEnd = RD->field_end();
1470 Field != FieldEnd; (void)++Field, ++i) {
1471 if (*Field == MemberDecl)
1472 break;
1473 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001474 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1475 Result += CharUnits::fromQuantity(
1476 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001477 CurrentType = MemberDecl->getType().getNonReferenceType();
1478 break;
1479 }
1480
1481 case OffsetOfExpr::OffsetOfNode::Identifier:
1482 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001483 return false;
1484
1485 case OffsetOfExpr::OffsetOfNode::Base: {
1486 CXXBaseSpecifier *BaseSpec = ON.getBase();
1487 if (BaseSpec->isVirtual())
1488 return false;
1489
1490 // Find the layout of the class whose base we are looking into.
1491 const RecordType *RT = CurrentType->getAs<RecordType>();
1492 if (!RT)
1493 return false;
1494 RecordDecl *RD = RT->getDecl();
1495 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1496
1497 // Find the base class itself.
1498 CurrentType = BaseSpec->getType();
1499 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1500 if (!BaseRT)
1501 return false;
1502
1503 // Add the offset to the base.
1504 Result += CharUnits::fromQuantity(
1505 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1506 / Info.Ctx.getCharWidth());
1507 break;
1508 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001509 }
1510 }
1511 return Success(Result.getQuantity(), E);
1512}
1513
Chris Lattnerb542afe2008-07-11 19:10:17 +00001514bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001515 // Special case unary operators that do not need their subexpression
1516 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001517 if (E->isOffsetOfOp()) {
1518 // The AST for offsetof is defined in such a way that we can just
1519 // directly Evaluate it as an l-value.
John McCallefdb83e2010-05-07 21:00:08 +00001520 LValue LV;
Eli Friedman35183ac2009-02-27 06:44:11 +00001521 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001522 return false;
Eli Friedman35183ac2009-02-27 06:44:11 +00001523 if (LV.getLValueBase())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001524 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001525 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001526 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001527
Eli Friedmana6afa762008-11-13 06:09:17 +00001528 if (E->getOpcode() == UnaryOperator::LNot) {
1529 // LNot's operand isn't necessarily an integer, so we handle it specially.
1530 bool bres;
1531 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1532 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001533 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001534 }
1535
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001536 // Only handle integral operations...
1537 if (!E->getSubExpr()->getType()->isIntegralType())
1538 return false;
1539
Chris Lattner87eae5e2008-07-11 22:52:41 +00001540 // Get the operand value into 'Result'.
1541 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001542 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001543
Chris Lattner75a48812008-07-11 22:15:16 +00001544 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001545 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001546 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1547 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001548 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001549 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001550 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1551 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001552 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001553 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001554 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001555 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001556 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001557 if (!Result.isInt()) return false;
1558 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001559 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001560 if (!Result.isInt()) return false;
1561 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001562 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001563}
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Chris Lattner732b2232008-07-12 01:15:53 +00001565/// HandleCast - This is used to evaluate implicit or explicit casts where the
1566/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001567bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001568 Expr *SubExpr = E->getSubExpr();
1569 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001570 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001571
Eli Friedman4efaa272008-11-12 09:44:48 +00001572 if (DestType->isBooleanType()) {
1573 bool BoolResult;
1574 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1575 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001576 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001577 }
1578
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001579 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001580 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001581 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001582 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001583
Eli Friedmanbe265702009-02-20 01:15:07 +00001584 if (!Result.isInt()) {
1585 // Only allow casts of lvalues if they are lossless.
1586 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1587 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001588
Daniel Dunbardd211642009-02-19 22:24:01 +00001589 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001590 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Chris Lattner732b2232008-07-12 01:15:53 +00001593 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001594 if (SrcType->isPointerType()) {
John McCallefdb83e2010-05-07 21:00:08 +00001595 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001596 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001597 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001598
Daniel Dunbardd211642009-02-19 22:24:01 +00001599 if (LV.getLValueBase()) {
1600 // Only allow based lvalue casts if they are lossless.
1601 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1602 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001603
John McCallefdb83e2010-05-07 21:00:08 +00001604 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001605 return true;
1606 }
1607
Ken Dycka7305832010-01-15 12:37:54 +00001608 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1609 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001610 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001611 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001612
Eli Friedmanbe265702009-02-20 01:15:07 +00001613 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1614 // This handles double-conversion cases, where there's both
1615 // an l-value promotion and an implicit conversion to int.
John McCallefdb83e2010-05-07 21:00:08 +00001616 LValue LV;
Eli Friedmanbe265702009-02-20 01:15:07 +00001617 if (!EvaluateLValue(SubExpr, LV, Info))
1618 return false;
1619
1620 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1621 return false;
1622
John McCallefdb83e2010-05-07 21:00:08 +00001623 LV.moveInto(Result);
Eli Friedmanbe265702009-02-20 01:15:07 +00001624 return true;
1625 }
1626
Eli Friedman1725f682009-04-22 19:23:09 +00001627 if (SrcType->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001628 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001629 if (!EvaluateComplex(SubExpr, C, Info))
1630 return false;
1631 if (C.isComplexFloat())
1632 return Success(HandleFloatToIntCast(DestType, SrcType,
1633 C.getComplexFloatReal(), Info.Ctx),
1634 E);
1635 else
1636 return Success(HandleIntToIntCast(DestType, SrcType,
1637 C.getComplexIntReal(), Info.Ctx), E);
1638 }
Eli Friedman2217c872009-02-22 11:46:18 +00001639 // FIXME: Handle vectors
1640
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001641 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001642 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001643
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001644 APFloat F(0.0);
1645 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001646 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001648 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001649}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001650
Eli Friedman722c7172009-02-28 03:59:05 +00001651bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1652 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001653 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001654 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1655 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1656 return Success(LV.getComplexIntReal(), E);
1657 }
1658
1659 return Visit(E->getSubExpr());
1660}
1661
Eli Friedman664a1042009-02-27 04:45:43 +00001662bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001663 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001664 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001665 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1666 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1667 return Success(LV.getComplexIntImag(), E);
1668 }
1669
Eli Friedman664a1042009-02-27 04:45:43 +00001670 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1671 Info.EvalResult.HasSideEffects = true;
1672 return Success(0, E);
1673}
1674
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001675//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001676// Float Evaluation
1677//===----------------------------------------------------------------------===//
1678
1679namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001680class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001681 : public StmtVisitor<FloatExprEvaluator, bool> {
1682 EvalInfo &Info;
1683 APFloat &Result;
1684public:
1685 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1686 : Info(info), Result(result) {}
1687
1688 bool VisitStmt(Stmt *S) {
1689 return false;
1690 }
1691
1692 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001693 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001694
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001695 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001696 bool VisitBinaryOperator(const BinaryOperator *E);
1697 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001698 bool VisitCastExpr(CastExpr *E);
1699 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001700 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001701
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001702 bool VisitChooseExpr(const ChooseExpr *E)
1703 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1704 bool VisitUnaryExtension(const UnaryOperator *E)
1705 { return Visit(E->getSubExpr()); }
John McCallabd3a852010-05-07 22:08:54 +00001706 bool VisitUnaryReal(const UnaryOperator *E);
1707 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001708
John McCallabd3a852010-05-07 22:08:54 +00001709 // FIXME: Missing: array subscript of vector, member of vector,
1710 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001711};
1712} // end anonymous namespace
1713
1714static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001715 assert(E->getType()->isRealFloatingType());
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001716 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1717}
1718
John McCalldb7b72a2010-02-28 13:00:19 +00001719static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1720 QualType ResultTy,
1721 const Expr *Arg,
1722 bool SNaN,
1723 llvm::APFloat &Result) {
1724 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1725 if (!S) return false;
1726
1727 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1728
1729 llvm::APInt fill;
1730
1731 // Treat empty strings as if they were zero.
1732 if (S->getString().empty())
1733 fill = llvm::APInt(32, 0);
1734 else if (S->getString().getAsInteger(0, fill))
1735 return false;
1736
1737 if (SNaN)
1738 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1739 else
1740 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1741 return true;
1742}
1743
Chris Lattner019f4e82008-10-06 05:28:25 +00001744bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001745 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001746 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001747 case Builtin::BI__builtin_huge_val:
1748 case Builtin::BI__builtin_huge_valf:
1749 case Builtin::BI__builtin_huge_vall:
1750 case Builtin::BI__builtin_inf:
1751 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001752 case Builtin::BI__builtin_infl: {
1753 const llvm::fltSemantics &Sem =
1754 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001755 Result = llvm::APFloat::getInf(Sem);
1756 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
John McCalldb7b72a2010-02-28 13:00:19 +00001759 case Builtin::BI__builtin_nans:
1760 case Builtin::BI__builtin_nansf:
1761 case Builtin::BI__builtin_nansl:
1762 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1763 true, Result);
1764
Chris Lattner9e621712008-10-06 06:31:58 +00001765 case Builtin::BI__builtin_nan:
1766 case Builtin::BI__builtin_nanf:
1767 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001768 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001769 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001770 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1771 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001772
1773 case Builtin::BI__builtin_fabs:
1774 case Builtin::BI__builtin_fabsf:
1775 case Builtin::BI__builtin_fabsl:
1776 if (!EvaluateFloat(E->getArg(0), Result, Info))
1777 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001779 if (Result.isNegative())
1780 Result.changeSign();
1781 return true;
1782
Mike Stump1eb44332009-09-09 15:08:12 +00001783 case Builtin::BI__builtin_copysign:
1784 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001785 case Builtin::BI__builtin_copysignl: {
1786 APFloat RHS(0.);
1787 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1788 !EvaluateFloat(E->getArg(1), RHS, Info))
1789 return false;
1790 Result.copySign(RHS);
1791 return true;
1792 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001793 }
1794}
1795
John McCallabd3a852010-05-07 22:08:54 +00001796bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1797 ComplexValue CV;
1798 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1799 return false;
1800 Result = CV.FloatReal;
1801 return true;
1802}
1803
1804bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
1805 ComplexValue CV;
1806 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1807 return false;
1808 Result = CV.FloatImag;
1809 return true;
1810}
1811
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001812bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001813 if (E->getOpcode() == UnaryOperator::Deref)
1814 return false;
1815
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001816 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1817 return false;
1818
1819 switch (E->getOpcode()) {
1820 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001821 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001822 return true;
1823 case UnaryOperator::Minus:
1824 Result.changeSign();
1825 return true;
1826 }
1827}
Chris Lattner019f4e82008-10-06 05:28:25 +00001828
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001829bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001830 if (E->getOpcode() == BinaryOperator::Comma) {
1831 if (!EvaluateFloat(E->getRHS(), Result, Info))
1832 return false;
1833
1834 // If we can't evaluate the LHS, it might have side effects;
1835 // conservatively mark it.
1836 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1837 Info.EvalResult.HasSideEffects = true;
1838
1839 return true;
1840 }
1841
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001842 // FIXME: Diagnostics? I really don't understand how the warnings
1843 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001844 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001845 if (!EvaluateFloat(E->getLHS(), Result, Info))
1846 return false;
1847 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1848 return false;
1849
1850 switch (E->getOpcode()) {
1851 default: return false;
1852 case BinaryOperator::Mul:
1853 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1854 return true;
1855 case BinaryOperator::Add:
1856 Result.add(RHS, APFloat::rmNearestTiesToEven);
1857 return true;
1858 case BinaryOperator::Sub:
1859 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1860 return true;
1861 case BinaryOperator::Div:
1862 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1863 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001864 }
1865}
1866
1867bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1868 Result = E->getValue();
1869 return true;
1870}
1871
Eli Friedman4efaa272008-11-12 09:44:48 +00001872bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1873 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Eli Friedman4efaa272008-11-12 09:44:48 +00001875 if (SubExpr->getType()->isIntegralType()) {
1876 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001877 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001878 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001879 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001880 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001881 return true;
1882 }
1883 if (SubExpr->getType()->isRealFloatingType()) {
1884 if (!Visit(SubExpr))
1885 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001886 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1887 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001888 return true;
1889 }
Eli Friedman2217c872009-02-22 11:46:18 +00001890 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001891
1892 return false;
1893}
1894
1895bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1896 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1897 return true;
1898}
1899
Eli Friedman67f85fc2009-12-04 02:12:53 +00001900bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1901 bool Cond;
1902 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1903 return false;
1904
1905 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1906}
1907
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001908//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001909// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001910//===----------------------------------------------------------------------===//
1911
1912namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001913class ComplexExprEvaluator
John McCallf4cf1a12010-05-07 17:22:02 +00001914 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001915 EvalInfo &Info;
John McCallf4cf1a12010-05-07 17:22:02 +00001916 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001918public:
John McCallf4cf1a12010-05-07 17:22:02 +00001919 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
1920 : Info(info), Result(Result) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001922 //===--------------------------------------------------------------------===//
1923 // Visitor Methods
1924 //===--------------------------------------------------------------------===//
1925
John McCallf4cf1a12010-05-07 17:22:02 +00001926 bool VisitStmt(Stmt *S) {
1927 return false;
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
John McCallf4cf1a12010-05-07 17:22:02 +00001930 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001931
John McCallf4cf1a12010-05-07 17:22:02 +00001932 bool VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001933 Expr* SubExpr = E->getSubExpr();
1934
1935 if (SubExpr->getType()->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001936 Result.makeComplexFloat();
1937 APFloat &Imag = Result.FloatImag;
1938 if (!EvaluateFloat(SubExpr, Imag, Info))
1939 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001940
John McCallf4cf1a12010-05-07 17:22:02 +00001941 Result.FloatReal = APFloat(Imag.getSemantics());
1942 return true;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001943 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001944 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001945 "Unexpected imaginary literal.");
1946
John McCallf4cf1a12010-05-07 17:22:02 +00001947 Result.makeComplexInt();
1948 APSInt &Imag = Result.IntImag;
1949 if (!EvaluateInteger(SubExpr, Imag, Info))
1950 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
John McCallf4cf1a12010-05-07 17:22:02 +00001952 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
1953 return true;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001954 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001955 }
1956
John McCallf4cf1a12010-05-07 17:22:02 +00001957 bool VisitCastExpr(CastExpr *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001958 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001959 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001960 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001961
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001962 if (SubType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001963 APFloat &Real = Result.FloatReal;
1964 if (!EvaluateFloat(SubExpr, Real, Info))
1965 return false;
Eli Friedman1725f682009-04-22 19:23:09 +00001966
1967 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001968 Result.makeComplexFloat();
1969 Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx);
1970 Result.FloatImag = APFloat(Real.getSemantics());
1971 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00001972 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00001973 Result.makeComplexInt();
1974 Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx);
1975 Result.IntImag = APSInt(Result.IntReal.getBitWidth(),
1976 !Result.IntReal.isSigned());
1977 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00001978 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001979 } else if (SubType->isIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001980 APSInt &Real = Result.IntReal;
1981 if (!EvaluateInteger(SubExpr, Real, Info))
1982 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001983
Eli Friedman1725f682009-04-22 19:23:09 +00001984 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001985 Result.makeComplexFloat();
1986 Result.FloatReal
1987 = HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx);
1988 Result.FloatImag = APFloat(Result.FloatReal.getSemantics());
1989 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00001990 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00001991 Result.makeComplexInt();
1992 Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx);
1993 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
1994 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00001995 }
John McCall183700f2009-09-21 23:43:11 +00001996 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001997 if (!Visit(SubExpr))
1998 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001999
2000 QualType SrcType = CT->getElementType();
2001
John McCallf4cf1a12010-05-07 17:22:02 +00002002 if (Result.isComplexFloat()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002003 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002004 Result.makeComplexFloat();
2005 Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType,
2006 Result.FloatReal,
2007 Info.Ctx);
2008 Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType,
2009 Result.FloatImag,
2010 Info.Ctx);
2011 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002012 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002013 Result.makeComplexInt();
2014 Result.IntReal = HandleFloatToIntCast(EltType, SrcType,
2015 Result.FloatReal,
2016 Info.Ctx);
2017 Result.IntImag = HandleFloatToIntCast(EltType, SrcType,
2018 Result.FloatImag,
2019 Info.Ctx);
2020 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002021 }
2022 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002023 assert(Result.isComplexInt() && "Invalid evaluate result.");
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002024 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002025 Result.makeComplexFloat();
2026 Result.FloatReal = HandleIntToFloatCast(EltType, SrcType,
2027 Result.IntReal,
2028 Info.Ctx);
2029 Result.FloatImag = HandleIntToFloatCast(EltType, SrcType,
2030 Result.IntImag,
2031 Info.Ctx);
2032 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002033 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002034 Result.makeComplexInt();
2035 Result.IntReal = HandleIntToIntCast(EltType, SrcType,
2036 Result.IntReal,
2037 Info.Ctx);
2038 Result.IntImag = HandleIntToIntCast(EltType, SrcType,
2039 Result.IntImag,
2040 Info.Ctx);
2041 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002042 }
2043 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002044 }
2045
2046 // FIXME: Handle more casts.
John McCallf4cf1a12010-05-07 17:22:02 +00002047 return false;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002048 }
Mike Stump1eb44332009-09-09 15:08:12 +00002049
John McCallf4cf1a12010-05-07 17:22:02 +00002050 bool VisitBinaryOperator(const BinaryOperator *E);
2051 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002052 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallf4cf1a12010-05-07 17:22:02 +00002053 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002054 { return Visit(E->getSubExpr()); }
2055 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00002056 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002057};
2058} // end anonymous namespace
2059
John McCallf4cf1a12010-05-07 17:22:02 +00002060static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2061 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002062 assert(E->getType()->isAnyComplexType());
John McCallf4cf1a12010-05-07 17:22:02 +00002063 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002064}
2065
John McCallf4cf1a12010-05-07 17:22:02 +00002066bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
2067 if (!Visit(E->getLHS()))
2068 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
John McCallf4cf1a12010-05-07 17:22:02 +00002070 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002071 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002072 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002073
Daniel Dunbar3f279872009-01-29 01:32:56 +00002074 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2075 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002076 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002077 default: return false;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002078 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002079 if (Result.isComplexFloat()) {
2080 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2081 APFloat::rmNearestTiesToEven);
2082 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2083 APFloat::rmNearestTiesToEven);
2084 } else {
2085 Result.getComplexIntReal() += RHS.getComplexIntReal();
2086 Result.getComplexIntImag() += RHS.getComplexIntImag();
2087 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002088 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002089 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002090 if (Result.isComplexFloat()) {
2091 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2092 APFloat::rmNearestTiesToEven);
2093 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2094 APFloat::rmNearestTiesToEven);
2095 } else {
2096 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2097 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2098 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002099 break;
2100 case BinaryOperator::Mul:
2101 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002102 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002103 APFloat &LHS_r = LHS.getComplexFloatReal();
2104 APFloat &LHS_i = LHS.getComplexFloatImag();
2105 APFloat &RHS_r = RHS.getComplexFloatReal();
2106 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Daniel Dunbar3f279872009-01-29 01:32:56 +00002108 APFloat Tmp = LHS_r;
2109 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2110 Result.getComplexFloatReal() = Tmp;
2111 Tmp = LHS_i;
2112 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2113 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2114
2115 Tmp = LHS_r;
2116 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2117 Result.getComplexFloatImag() = Tmp;
2118 Tmp = LHS_i;
2119 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2120 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2121 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002122 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002123 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002124 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2125 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002126 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002127 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2128 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2129 }
2130 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002131 }
2132
John McCallf4cf1a12010-05-07 17:22:02 +00002133 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002134}
2135
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002136//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002137// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002138//===----------------------------------------------------------------------===//
2139
John McCallefdb83e2010-05-07 21:00:08 +00002140static bool Evaluate(const Expr *E, EvalInfo &Info) {
2141 if (E->getType()->isVectorType()) {
2142 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002143 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002144 } else if (E->getType()->isIntegerType()) {
2145 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002146 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002147 } else if (E->getType()->hasPointerRepresentation()) {
2148 LValue LV;
2149 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002150 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002151 LV.moveInto(Info.EvalResult.Val);
2152 } else if (E->getType()->isRealFloatingType()) {
2153 llvm::APFloat F(0.0);
2154 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002155 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
John McCallefdb83e2010-05-07 21:00:08 +00002157 Info.EvalResult.Val = APValue(F);
2158 } else if (E->getType()->isAnyComplexType()) {
2159 ComplexValue C;
2160 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002161 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002162 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002163 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002164 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002165
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002166 return true;
2167}
2168
John McCallefdb83e2010-05-07 21:00:08 +00002169/// Evaluate - Return true if this is a constant which we can fold using
2170/// any crazy technique (that has nothing to do with language standards) that
2171/// we want to. If this function returns true, it returns the folded constant
2172/// in Result.
2173bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2174 EvalInfo Info(Ctx, Result);
2175 return ::Evaluate(this, Info);
2176}
2177
Mike Stump660e6f72009-10-26 23:05:19 +00002178bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
2179 EvalInfo Info(Ctx, Result, true);
John McCallefdb83e2010-05-07 21:00:08 +00002180 return ::Evaluate(this, Info);
Mike Stump660e6f72009-10-26 23:05:19 +00002181}
2182
John McCallcd7a4452010-01-05 23:42:56 +00002183bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2184 EvalResult Scratch;
2185 EvalInfo Info(Ctx, Scratch);
2186
2187 return HandleConversionToBool(this, Result, Info);
2188}
2189
Anders Carlsson1b782762009-04-10 04:54:13 +00002190bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2191 EvalInfo Info(Ctx, Result);
2192
John McCallefdb83e2010-05-07 21:00:08 +00002193 LValue LV;
2194 if (EvaluateLValue(this, LV, Info) && !Result.HasSideEffects) {
2195 LV.moveInto(Result.Val);
2196 return true;
2197 }
2198 return false;
Anders Carlsson1b782762009-04-10 04:54:13 +00002199}
2200
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002201bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2202 EvalInfo Info(Ctx, Result, true);
2203
John McCallefdb83e2010-05-07 21:00:08 +00002204 LValue LV;
2205 if (EvaluateLValue(this, LV, Info) && !Result.HasSideEffects) {
2206 LV.moveInto(Result.Val);
2207 return true;
2208 }
2209 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002210}
2211
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002212/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002213/// folded, but discard the result.
2214bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002215 EvalResult Result;
2216 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002217}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002218
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002219bool Expr::HasSideEffects(ASTContext &Ctx) const {
2220 Expr::EvalResult Result;
2221 EvalInfo Info(Ctx, Result);
2222 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2223}
2224
Anders Carlsson51fe9962008-11-22 21:04:56 +00002225APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002226 EvalResult EvalResult;
2227 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002228 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002229 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002230 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002231
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002232 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002233}
John McCalld905f5a2010-05-07 05:32:02 +00002234
2235/// isIntegerConstantExpr - this recursive routine will test if an expression is
2236/// an integer constant expression.
2237
2238/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2239/// comma, etc
2240///
2241/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2242/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2243/// cast+dereference.
2244
2245// CheckICE - This function does the fundamental ICE checking: the returned
2246// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2247// Note that to reduce code duplication, this helper does no evaluation
2248// itself; the caller checks whether the expression is evaluatable, and
2249// in the rare cases where CheckICE actually cares about the evaluated
2250// value, it calls into Evalute.
2251//
2252// Meanings of Val:
2253// 0: This expression is an ICE if it can be evaluated by Evaluate.
2254// 1: This expression is not an ICE, but if it isn't evaluated, it's
2255// a legal subexpression for an ICE. This return value is used to handle
2256// the comma operator in C99 mode.
2257// 2: This expression is not an ICE, and is not a legal subexpression for one.
2258
2259struct ICEDiag {
2260 unsigned Val;
2261 SourceLocation Loc;
2262
2263 public:
2264 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2265 ICEDiag() : Val(0) {}
2266};
2267
2268ICEDiag NoDiag() { return ICEDiag(); }
2269
2270static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2271 Expr::EvalResult EVResult;
2272 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2273 !EVResult.Val.isInt()) {
2274 return ICEDiag(2, E->getLocStart());
2275 }
2276 return NoDiag();
2277}
2278
2279static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2280 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
2281 if (!E->getType()->isIntegralType()) {
2282 return ICEDiag(2, E->getLocStart());
2283 }
2284
2285 switch (E->getStmtClass()) {
2286#define STMT(Node, Base) case Expr::Node##Class:
2287#define EXPR(Node, Base)
2288#include "clang/AST/StmtNodes.inc"
2289 case Expr::PredefinedExprClass:
2290 case Expr::FloatingLiteralClass:
2291 case Expr::ImaginaryLiteralClass:
2292 case Expr::StringLiteralClass:
2293 case Expr::ArraySubscriptExprClass:
2294 case Expr::MemberExprClass:
2295 case Expr::CompoundAssignOperatorClass:
2296 case Expr::CompoundLiteralExprClass:
2297 case Expr::ExtVectorElementExprClass:
2298 case Expr::InitListExprClass:
2299 case Expr::DesignatedInitExprClass:
2300 case Expr::ImplicitValueInitExprClass:
2301 case Expr::ParenListExprClass:
2302 case Expr::VAArgExprClass:
2303 case Expr::AddrLabelExprClass:
2304 case Expr::StmtExprClass:
2305 case Expr::CXXMemberCallExprClass:
2306 case Expr::CXXDynamicCastExprClass:
2307 case Expr::CXXTypeidExprClass:
2308 case Expr::CXXNullPtrLiteralExprClass:
2309 case Expr::CXXThisExprClass:
2310 case Expr::CXXThrowExprClass:
2311 case Expr::CXXNewExprClass:
2312 case Expr::CXXDeleteExprClass:
2313 case Expr::CXXPseudoDestructorExprClass:
2314 case Expr::UnresolvedLookupExprClass:
2315 case Expr::DependentScopeDeclRefExprClass:
2316 case Expr::CXXConstructExprClass:
2317 case Expr::CXXBindTemporaryExprClass:
2318 case Expr::CXXBindReferenceExprClass:
2319 case Expr::CXXExprWithTemporariesClass:
2320 case Expr::CXXTemporaryObjectExprClass:
2321 case Expr::CXXUnresolvedConstructExprClass:
2322 case Expr::CXXDependentScopeMemberExprClass:
2323 case Expr::UnresolvedMemberExprClass:
2324 case Expr::ObjCStringLiteralClass:
2325 case Expr::ObjCEncodeExprClass:
2326 case Expr::ObjCMessageExprClass:
2327 case Expr::ObjCSelectorExprClass:
2328 case Expr::ObjCProtocolExprClass:
2329 case Expr::ObjCIvarRefExprClass:
2330 case Expr::ObjCPropertyRefExprClass:
2331 case Expr::ObjCImplicitSetterGetterRefExprClass:
2332 case Expr::ObjCSuperExprClass:
2333 case Expr::ObjCIsaExprClass:
2334 case Expr::ShuffleVectorExprClass:
2335 case Expr::BlockExprClass:
2336 case Expr::BlockDeclRefExprClass:
2337 case Expr::NoStmtClass:
2338 return ICEDiag(2, E->getLocStart());
2339
2340 case Expr::GNUNullExprClass:
2341 // GCC considers the GNU __null value to be an integral constant expression.
2342 return NoDiag();
2343
2344 case Expr::ParenExprClass:
2345 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2346 case Expr::IntegerLiteralClass:
2347 case Expr::CharacterLiteralClass:
2348 case Expr::CXXBoolLiteralExprClass:
2349 case Expr::CXXZeroInitValueExprClass:
2350 case Expr::TypesCompatibleExprClass:
2351 case Expr::UnaryTypeTraitExprClass:
2352 return NoDiag();
2353 case Expr::CallExprClass:
2354 case Expr::CXXOperatorCallExprClass: {
2355 const CallExpr *CE = cast<CallExpr>(E);
2356 if (CE->isBuiltinCall(Ctx))
2357 return CheckEvalInICE(E, Ctx);
2358 return ICEDiag(2, E->getLocStart());
2359 }
2360 case Expr::DeclRefExprClass:
2361 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2362 return NoDiag();
2363 if (Ctx.getLangOptions().CPlusPlus &&
2364 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2365 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2366
2367 // Parameter variables are never constants. Without this check,
2368 // getAnyInitializer() can find a default argument, which leads
2369 // to chaos.
2370 if (isa<ParmVarDecl>(D))
2371 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2372
2373 // C++ 7.1.5.1p2
2374 // A variable of non-volatile const-qualified integral or enumeration
2375 // type initialized by an ICE can be used in ICEs.
2376 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2377 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2378 if (Quals.hasVolatile() || !Quals.hasConst())
2379 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2380
2381 // Look for a declaration of this variable that has an initializer.
2382 const VarDecl *ID = 0;
2383 const Expr *Init = Dcl->getAnyInitializer(ID);
2384 if (Init) {
2385 if (ID->isInitKnownICE()) {
2386 // We have already checked whether this subexpression is an
2387 // integral constant expression.
2388 if (ID->isInitICE())
2389 return NoDiag();
2390 else
2391 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2392 }
2393
2394 // It's an ICE whether or not the definition we found is
2395 // out-of-line. See DR 721 and the discussion in Clang PR
2396 // 6206 for details.
2397
2398 if (Dcl->isCheckingICE()) {
2399 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2400 }
2401
2402 Dcl->setCheckingICE();
2403 ICEDiag Result = CheckICE(Init, Ctx);
2404 // Cache the result of the ICE test.
2405 Dcl->setInitKnownICE(Result.Val == 0);
2406 return Result;
2407 }
2408 }
2409 }
2410 return ICEDiag(2, E->getLocStart());
2411 case Expr::UnaryOperatorClass: {
2412 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2413 switch (Exp->getOpcode()) {
2414 case UnaryOperator::PostInc:
2415 case UnaryOperator::PostDec:
2416 case UnaryOperator::PreInc:
2417 case UnaryOperator::PreDec:
2418 case UnaryOperator::AddrOf:
2419 case UnaryOperator::Deref:
2420 return ICEDiag(2, E->getLocStart());
2421 case UnaryOperator::Extension:
2422 case UnaryOperator::LNot:
2423 case UnaryOperator::Plus:
2424 case UnaryOperator::Minus:
2425 case UnaryOperator::Not:
2426 case UnaryOperator::Real:
2427 case UnaryOperator::Imag:
2428 return CheckICE(Exp->getSubExpr(), Ctx);
2429 case UnaryOperator::OffsetOf:
2430 break;
2431 }
2432
2433 // OffsetOf falls through here.
2434 }
2435 case Expr::OffsetOfExprClass: {
2436 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2437 // Evaluate matches the proposed gcc behavior for cases like
2438 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2439 // compliance: we should warn earlier for offsetof expressions with
2440 // array subscripts that aren't ICEs, and if the array subscripts
2441 // are ICEs, the value of the offsetof must be an integer constant.
2442 return CheckEvalInICE(E, Ctx);
2443 }
2444 case Expr::SizeOfAlignOfExprClass: {
2445 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
2446 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
2447 return ICEDiag(2, E->getLocStart());
2448 return NoDiag();
2449 }
2450 case Expr::BinaryOperatorClass: {
2451 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2452 switch (Exp->getOpcode()) {
2453 case BinaryOperator::PtrMemD:
2454 case BinaryOperator::PtrMemI:
2455 case BinaryOperator::Assign:
2456 case BinaryOperator::MulAssign:
2457 case BinaryOperator::DivAssign:
2458 case BinaryOperator::RemAssign:
2459 case BinaryOperator::AddAssign:
2460 case BinaryOperator::SubAssign:
2461 case BinaryOperator::ShlAssign:
2462 case BinaryOperator::ShrAssign:
2463 case BinaryOperator::AndAssign:
2464 case BinaryOperator::XorAssign:
2465 case BinaryOperator::OrAssign:
2466 return ICEDiag(2, E->getLocStart());
2467
2468 case BinaryOperator::Mul:
2469 case BinaryOperator::Div:
2470 case BinaryOperator::Rem:
2471 case BinaryOperator::Add:
2472 case BinaryOperator::Sub:
2473 case BinaryOperator::Shl:
2474 case BinaryOperator::Shr:
2475 case BinaryOperator::LT:
2476 case BinaryOperator::GT:
2477 case BinaryOperator::LE:
2478 case BinaryOperator::GE:
2479 case BinaryOperator::EQ:
2480 case BinaryOperator::NE:
2481 case BinaryOperator::And:
2482 case BinaryOperator::Xor:
2483 case BinaryOperator::Or:
2484 case BinaryOperator::Comma: {
2485 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2486 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2487 if (Exp->getOpcode() == BinaryOperator::Div ||
2488 Exp->getOpcode() == BinaryOperator::Rem) {
2489 // Evaluate gives an error for undefined Div/Rem, so make sure
2490 // we don't evaluate one.
2491 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
2492 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2493 if (REval == 0)
2494 return ICEDiag(1, E->getLocStart());
2495 if (REval.isSigned() && REval.isAllOnesValue()) {
2496 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2497 if (LEval.isMinSignedValue())
2498 return ICEDiag(1, E->getLocStart());
2499 }
2500 }
2501 }
2502 if (Exp->getOpcode() == BinaryOperator::Comma) {
2503 if (Ctx.getLangOptions().C99) {
2504 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2505 // if it isn't evaluated.
2506 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2507 return ICEDiag(1, E->getLocStart());
2508 } else {
2509 // In both C89 and C++, commas in ICEs are illegal.
2510 return ICEDiag(2, E->getLocStart());
2511 }
2512 }
2513 if (LHSResult.Val >= RHSResult.Val)
2514 return LHSResult;
2515 return RHSResult;
2516 }
2517 case BinaryOperator::LAnd:
2518 case BinaryOperator::LOr: {
2519 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2520 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2521 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2522 // Rare case where the RHS has a comma "side-effect"; we need
2523 // to actually check the condition to see whether the side
2524 // with the comma is evaluated.
2525 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
2526 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2527 return RHSResult;
2528 return NoDiag();
2529 }
2530
2531 if (LHSResult.Val >= RHSResult.Val)
2532 return LHSResult;
2533 return RHSResult;
2534 }
2535 }
2536 }
2537 case Expr::ImplicitCastExprClass:
2538 case Expr::CStyleCastExprClass:
2539 case Expr::CXXFunctionalCastExprClass:
2540 case Expr::CXXStaticCastExprClass:
2541 case Expr::CXXReinterpretCastExprClass:
2542 case Expr::CXXConstCastExprClass: {
2543 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2544 if (SubExpr->getType()->isIntegralType())
2545 return CheckICE(SubExpr, Ctx);
2546 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2547 return NoDiag();
2548 return ICEDiag(2, E->getLocStart());
2549 }
2550 case Expr::ConditionalOperatorClass: {
2551 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
2552 // If the condition (ignoring parens) is a __builtin_constant_p call,
2553 // then only the true side is actually considered in an integer constant
2554 // expression, and it is fully evaluated. This is an important GNU
2555 // extension. See GCC PR38377 for discussion.
2556 if (const CallExpr *CallCE
2557 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
2558 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
2559 Expr::EvalResult EVResult;
2560 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2561 !EVResult.Val.isInt()) {
2562 return ICEDiag(2, E->getLocStart());
2563 }
2564 return NoDiag();
2565 }
2566 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2567 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2568 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2569 if (CondResult.Val == 2)
2570 return CondResult;
2571 if (TrueResult.Val == 2)
2572 return TrueResult;
2573 if (FalseResult.Val == 2)
2574 return FalseResult;
2575 if (CondResult.Val == 1)
2576 return CondResult;
2577 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2578 return NoDiag();
2579 // Rare case where the diagnostics depend on which side is evaluated
2580 // Note that if we get here, CondResult is 0, and at least one of
2581 // TrueResult and FalseResult is non-zero.
2582 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
2583 return FalseResult;
2584 }
2585 return TrueResult;
2586 }
2587 case Expr::CXXDefaultArgExprClass:
2588 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
2589 case Expr::ChooseExprClass: {
2590 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
2591 }
2592 }
2593
2594 // Silence a GCC warning
2595 return ICEDiag(2, E->getLocStart());
2596}
2597
2598bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2599 SourceLocation *Loc, bool isEvaluated) const {
2600 ICEDiag d = CheckICE(this, Ctx);
2601 if (d.Val != 0) {
2602 if (Loc) *Loc = d.Loc;
2603 return false;
2604 }
2605 EvalResult EvalResult;
2606 if (!Evaluate(EvalResult, Ctx))
2607 llvm_unreachable("ICE cannot be evaluated!");
2608 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2609 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
2610 Result = EvalResult.Val.getInt();
2611 return true;
2612}