blob: dc614018ec2b7b7b3f48f5819c4254af675c686e [file] [log] [blame]
Chris Lattnere13042c2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlsson7a241ba2008-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 Dyck40775002010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump2346cd22009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlsson7a241ba2008-07-03 04:20:39 +000027using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000030
Chris Lattnercdf34e72008-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 Stump11289f42009-09-09 15:08:12 +000047
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000048 /// EvalResult - Contains information about the evaluation.
49 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000050
John McCall95007602010-05-10 23:27:23 +000051 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult)
52 : Ctx(ctx), EvalResult(evalresult) {}
Chris Lattnercdf34e72008-07-11 22:52:41 +000053};
54
John McCall93d91dc2010-05-07 17:22:02 +000055namespace {
56 struct ComplexValue {
57 private:
58 bool IsInt;
59
60 public:
61 APSInt IntReal, IntImag;
62 APFloat FloatReal, FloatImag;
63
64 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
65
66 void makeComplexFloat() { IsInt = false; }
67 bool isComplexFloat() const { return !IsInt; }
68 APFloat &getComplexFloatReal() { return FloatReal; }
69 APFloat &getComplexFloatImag() { return FloatImag; }
70
71 void makeComplexInt() { IsInt = true; }
72 bool isComplexInt() const { return IsInt; }
73 APSInt &getComplexIntReal() { return IntReal; }
74 APSInt &getComplexIntImag() { return IntImag; }
75
76 void moveInto(APValue &v) {
77 if (isComplexFloat())
78 v = APValue(FloatReal, FloatImag);
79 else
80 v = APValue(IntReal, IntImag);
81 }
82 };
John McCall45d55e42010-05-07 21:00:08 +000083
84 struct LValue {
85 Expr *Base;
86 CharUnits Offset;
87
88 Expr *getLValueBase() { return Base; }
89 CharUnits getLValueOffset() { return Offset; }
90
91 void moveInto(APValue &v) {
92 v = APValue(Base, Offset);
93 }
94 };
John McCall93d91dc2010-05-07 17:22:02 +000095}
Chris Lattnercdf34e72008-07-11 22:52:41 +000096
John McCall45d55e42010-05-07 21:00:08 +000097static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
98static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-07-11 22:52:41 +000099static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattner6c4d2552009-10-28 23:59:40 +0000100static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
101 EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +0000102static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCall93d91dc2010-05-07 17:22:02 +0000103static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +0000104
105//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +0000106// Misc utilities
107//===----------------------------------------------------------------------===//
108
Abramo Bagnaraf8199452010-05-14 17:07:14 +0000109static bool IsGlobalLValue(const Expr* E) {
John McCall95007602010-05-10 23:27:23 +0000110 if (!E) return true;
111
112 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
113 if (isa<FunctionDecl>(DRE->getDecl()))
114 return true;
115 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
116 return VD->hasGlobalStorage();
117 return false;
118 }
119
120 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
121 return CLE->isFileScope();
122
123 return true;
124}
125
John McCall45d55e42010-05-07 21:00:08 +0000126static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
127 const Expr* Base = Value.Base;
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000128
John McCalleb3e4f32010-05-07 21:34:32 +0000129 // A null base expression indicates a null pointer. These are always
130 // evaluatable, and they are false unless the offset is zero.
131 if (!Base) {
132 Result = !Value.Offset.isZero();
133 return true;
134 }
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000135
John McCall95007602010-05-10 23:27:23 +0000136 // Require the base expression to be a global l-value.
Abramo Bagnaraf8199452010-05-14 17:07:14 +0000137 if (!IsGlobalLValue(Base)) return false;
John McCall95007602010-05-10 23:27:23 +0000138
John McCalleb3e4f32010-05-07 21:34:32 +0000139 // We have a non-null base expression. These are generally known to
140 // be true, but if it'a decl-ref to a weak symbol it can be null at
141 // runtime.
John McCalleb3e4f32010-05-07 21:34:32 +0000142 Result = true;
143
144 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000145 if (!DeclRef)
146 return true;
147
John McCalleb3e4f32010-05-07 21:34:32 +0000148 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000149 const ValueDecl* Decl = DeclRef->getDecl();
150 if (Decl->hasAttr<WeakAttr>() ||
151 Decl->hasAttr<WeakRefAttr>() ||
152 Decl->hasAttr<WeakImportAttr>())
153 return false;
154
Eli Friedman334046a2009-06-14 02:17:33 +0000155 return true;
156}
157
John McCall1be1c632010-01-05 23:42:56 +0000158static bool HandleConversionToBool(const Expr* E, bool& Result,
159 EvalInfo &Info) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000160 if (E->getType()->isIntegralType()) {
161 APSInt IntResult;
162 if (!EvaluateInteger(E, IntResult, Info))
163 return false;
164 Result = IntResult != 0;
165 return true;
166 } else if (E->getType()->isRealFloatingType()) {
167 APFloat FloatResult(0.0);
168 if (!EvaluateFloat(E, FloatResult, Info))
169 return false;
170 Result = !FloatResult.isZero();
171 return true;
Eli Friedman64004332009-03-23 04:38:34 +0000172 } else if (E->getType()->hasPointerRepresentation()) {
John McCall45d55e42010-05-07 21:00:08 +0000173 LValue PointerResult;
Eli Friedman9a156e52008-11-12 09:44:48 +0000174 if (!EvaluatePointer(E, PointerResult, Info))
175 return false;
Eli Friedman334046a2009-06-14 02:17:33 +0000176 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +0000177 } else if (E->getType()->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +0000178 ComplexValue ComplexResult;
Eli Friedman64004332009-03-23 04:38:34 +0000179 if (!EvaluateComplex(E, ComplexResult, Info))
180 return false;
181 if (ComplexResult.isComplexFloat()) {
182 Result = !ComplexResult.getComplexFloatReal().isZero() ||
183 !ComplexResult.getComplexFloatImag().isZero();
184 } else {
185 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
186 ComplexResult.getComplexIntImag().getBoolValue();
187 }
188 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000189 }
190
191 return false;
192}
193
Mike Stump11289f42009-09-09 15:08:12 +0000194static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000195 APFloat &Value, ASTContext &Ctx) {
196 unsigned DestWidth = Ctx.getIntWidth(DestType);
197 // Determine whether we are converting to unsigned or signed.
198 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000200 // FIXME: Warning for overflow.
201 uint64_t Space[4];
202 bool ignored;
203 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
204 llvm::APFloat::rmTowardZero, &ignored);
205 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
206}
207
Mike Stump11289f42009-09-09 15:08:12 +0000208static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000209 APFloat &Value, ASTContext &Ctx) {
210 bool ignored;
211 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000212 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000213 APFloat::rmNearestTiesToEven, &ignored);
214 return Result;
215}
216
Mike Stump11289f42009-09-09 15:08:12 +0000217static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000218 APSInt &Value, ASTContext &Ctx) {
219 unsigned DestWidth = Ctx.getIntWidth(DestType);
220 APSInt Result = Value;
221 // Figure out if this is a truncate, extend or noop cast.
222 // If the input is signed, do a sign extend, noop, or truncate.
223 Result.extOrTrunc(DestWidth);
224 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
225 return Result;
226}
227
Mike Stump11289f42009-09-09 15:08:12 +0000228static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000229 APSInt &Value, ASTContext &Ctx) {
230
231 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
232 Result.convertFromAPInt(Value, Value.isSigned(),
233 APFloat::rmNearestTiesToEven);
234 return Result;
235}
236
Mike Stump876387b2009-10-27 22:09:17 +0000237namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000238class HasSideEffect
Mike Stump876387b2009-10-27 22:09:17 +0000239 : public StmtVisitor<HasSideEffect, bool> {
240 EvalInfo &Info;
241public:
242
243 HasSideEffect(EvalInfo &info) : Info(info) {}
244
245 // Unhandled nodes conservatively default to having side effects.
246 bool VisitStmt(Stmt *S) {
247 return true;
248 }
249
250 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
251 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000252 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000253 return true;
254 return false;
255 }
256 // We don't want to evaluate BlockExprs multiple times, as they generate
257 // a ton of code.
258 bool VisitBlockExpr(BlockExpr *E) { return true; }
259 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
260 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
261 { return Visit(E->getInitializer()); }
262 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
263 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
264 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
265 bool VisitStringLiteral(StringLiteral *E) { return false; }
266 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
267 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
268 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stumpfa502902009-10-29 20:48:09 +0000269 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000270 bool VisitChooseExpr(ChooseExpr *E)
271 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
272 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
273 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000274 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000275 bool VisitBinaryOperator(BinaryOperator *E)
276 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000277 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
278 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
279 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
280 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
281 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000282 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000283 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000284 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000285 }
286 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattnera0679422010-04-13 17:34:23 +0000287
288 // Has side effects if any element does.
289 bool VisitInitListExpr(InitListExpr *E) {
290 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
291 if (Visit(E->getInit(i))) return true;
292 return false;
293 }
Mike Stump876387b2009-10-27 22:09:17 +0000294};
295
Mike Stump876387b2009-10-27 22:09:17 +0000296} // end anonymous namespace
297
Eli Friedman9a156e52008-11-12 09:44:48 +0000298//===----------------------------------------------------------------------===//
299// LValue Evaluation
300//===----------------------------------------------------------------------===//
301namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000302class LValueExprEvaluator
John McCall45d55e42010-05-07 21:00:08 +0000303 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman9a156e52008-11-12 09:44:48 +0000304 EvalInfo &Info;
John McCall45d55e42010-05-07 21:00:08 +0000305 LValue &Result;
306
307 bool Success(Expr *E) {
308 Result.Base = E;
309 Result.Offset = CharUnits::Zero();
310 return true;
311 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000312public:
Mike Stump11289f42009-09-09 15:08:12 +0000313
John McCall45d55e42010-05-07 21:00:08 +0000314 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
315 Info(info), Result(Result) {}
Eli Friedman9a156e52008-11-12 09:44:48 +0000316
John McCall45d55e42010-05-07 21:00:08 +0000317 bool VisitStmt(Stmt *S) {
318 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000319 }
Douglas Gregor882211c2010-04-28 22:16:22 +0000320
John McCall45d55e42010-05-07 21:00:08 +0000321 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
322 bool VisitDeclRefExpr(DeclRefExpr *E);
323 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
324 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
325 bool VisitMemberExpr(MemberExpr *E);
326 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
327 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
328 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
329 bool VisitUnaryDeref(UnaryOperator *E);
330 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman449fe542009-03-23 04:56:01 +0000331 { return Visit(E->getSubExpr()); }
John McCall45d55e42010-05-07 21:00:08 +0000332 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedman449fe542009-03-23 04:56:01 +0000333 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000334
John McCall45d55e42010-05-07 21:00:08 +0000335 bool VisitCastExpr(CastExpr *E) {
Anders Carlssonde55f642009-10-03 16:30:22 +0000336 switch (E->getCastKind()) {
337 default:
John McCall45d55e42010-05-07 21:00:08 +0000338 return false;
Anders Carlssonde55f642009-10-03 16:30:22 +0000339
340 case CastExpr::CK_NoOp:
341 return Visit(E->getSubExpr());
342 }
343 }
Eli Friedman449fe542009-03-23 04:56:01 +0000344 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000345};
346} // end anonymous namespace
347
John McCall45d55e42010-05-07 21:00:08 +0000348static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
349 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman9a156e52008-11-12 09:44:48 +0000350}
351
John McCall45d55e42010-05-07 21:00:08 +0000352bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000353 if (isa<FunctionDecl>(E->getDecl())) {
John McCall45d55e42010-05-07 21:00:08 +0000354 return Success(E);
Eli Friedman751aa72b72009-05-27 06:04:58 +0000355 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
356 if (!VD->getType()->isReferenceType())
John McCall45d55e42010-05-07 21:00:08 +0000357 return Success(E);
Chandler Carruthe299ba62010-05-16 09:32:51 +0000358 // Reference parameters can refer to anything even if they have an
359 // "initializer" in the form of a default argument.
360 if (isa<ParmVarDecl>(VD))
361 return false;
Eli Friedman9ab03192009-08-29 19:09:59 +0000362 // FIXME: Check whether VD might be overridden!
Sebastian Redl5ca79842010-02-01 20:16:42 +0000363 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregor0840cc02009-11-01 20:32:48 +0000364 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000365 }
366
John McCall45d55e42010-05-07 21:00:08 +0000367 return false;
Anders Carlssona42ee442008-11-24 04:41:22 +0000368}
369
John McCall45d55e42010-05-07 21:00:08 +0000370bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCall45d55e42010-05-07 21:00:08 +0000371 return Success(E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000372}
373
John McCall45d55e42010-05-07 21:00:08 +0000374bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000375 QualType Ty;
376 if (E->isArrow()) {
John McCall45d55e42010-05-07 21:00:08 +0000377 if (!EvaluatePointer(E->getBase(), Result, Info))
378 return false;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000379 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000380 } else {
John McCall45d55e42010-05-07 21:00:08 +0000381 if (!Visit(E->getBase()))
382 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000383 Ty = E->getBase()->getType();
384 }
385
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000386 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000387 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000388
389 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
390 if (!FD) // FIXME: deal with other kinds of member expressions
John McCall45d55e42010-05-07 21:00:08 +0000391 return false;
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000392
393 if (FD->getType()->isReferenceType())
John McCall45d55e42010-05-07 21:00:08 +0000394 return false;
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000395
Eli Friedman9a156e52008-11-12 09:44:48 +0000396 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000397 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000398 for (RecordDecl::field_iterator Field = RD->field_begin(),
399 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000400 Field != FieldEnd; (void)++Field, ++i) {
401 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000402 break;
403 }
404
John McCall45d55e42010-05-07 21:00:08 +0000405 Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8);
406 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000407}
408
John McCall45d55e42010-05-07 21:00:08 +0000409bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000410 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCall45d55e42010-05-07 21:00:08 +0000411 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000413 APSInt Index;
414 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCall45d55e42010-05-07 21:00:08 +0000415 return false;
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000416
Ken Dyck40775002010-01-11 17:06:35 +0000417 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCall45d55e42010-05-07 21:00:08 +0000418 Result.Offset += Index.getSExtValue() * ElementSize;
419 return true;
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000420}
Eli Friedman9a156e52008-11-12 09:44:48 +0000421
John McCall45d55e42010-05-07 21:00:08 +0000422bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
423 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000424}
425
Eli Friedman9a156e52008-11-12 09:44:48 +0000426//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000427// Pointer Evaluation
428//===----------------------------------------------------------------------===//
429
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000430namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000431class PointerExprEvaluator
John McCall45d55e42010-05-07 21:00:08 +0000432 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000433 EvalInfo &Info;
John McCall45d55e42010-05-07 21:00:08 +0000434 LValue &Result;
435
436 bool Success(Expr *E) {
437 Result.Base = E;
438 Result.Offset = CharUnits::Zero();
439 return true;
440 }
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000441public:
Mike Stump11289f42009-09-09 15:08:12 +0000442
John McCall45d55e42010-05-07 21:00:08 +0000443 PointerExprEvaluator(EvalInfo &info, LValue &Result)
444 : Info(info), Result(Result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000445
John McCall45d55e42010-05-07 21:00:08 +0000446 bool VisitStmt(Stmt *S) {
447 return false;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000448 }
449
John McCall45d55e42010-05-07 21:00:08 +0000450 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000451
John McCall45d55e42010-05-07 21:00:08 +0000452 bool VisitBinaryOperator(const BinaryOperator *E);
453 bool VisitCastExpr(CastExpr* E);
454 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanc2b50172009-02-22 11:46:18 +0000455 { return Visit(E->getSubExpr()); }
John McCall45d55e42010-05-07 21:00:08 +0000456 bool VisitUnaryAddrOf(const UnaryOperator *E);
457 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
458 { return Success(E); }
459 bool VisitAddrLabelExpr(AddrLabelExpr *E)
460 { return Success(E); }
461 bool VisitCallExpr(CallExpr *E);
462 bool VisitBlockExpr(BlockExpr *E) {
Mike Stumpa6703322009-02-19 22:01:56 +0000463 if (!E->hasBlockDeclRefExprs())
John McCall45d55e42010-05-07 21:00:08 +0000464 return Success(E);
465 return false;
Mike Stumpa6703322009-02-19 22:01:56 +0000466 }
John McCall45d55e42010-05-07 21:00:08 +0000467 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
468 { return Success((Expr*)0); }
469 bool VisitConditionalOperator(ConditionalOperator *E);
470 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000471 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCall45d55e42010-05-07 21:00:08 +0000472 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
473 { return Success((Expr*)0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000474 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000475};
Chris Lattner05706e882008-07-11 18:11:29 +0000476} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000477
John McCall45d55e42010-05-07 21:00:08 +0000478static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +0000479 assert(E->getType()->hasPointerRepresentation());
John McCall45d55e42010-05-07 21:00:08 +0000480 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000481}
482
John McCall45d55e42010-05-07 21:00:08 +0000483bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000484 if (E->getOpcode() != BinaryOperator::Add &&
485 E->getOpcode() != BinaryOperator::Sub)
John McCall45d55e42010-05-07 21:00:08 +0000486 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Chris Lattner05706e882008-07-11 18:11:29 +0000488 const Expr *PExp = E->getLHS();
489 const Expr *IExp = E->getRHS();
490 if (IExp->getType()->isPointerType())
491 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000492
John McCall45d55e42010-05-07 21:00:08 +0000493 if (!EvaluatePointer(PExp, Result, Info))
494 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000495
John McCall45d55e42010-05-07 21:00:08 +0000496 llvm::APSInt Offset;
497 if (!EvaluateInteger(IExp, Offset, Info))
498 return false;
499 int64_t AdditionalOffset
500 = Offset.isSigned() ? Offset.getSExtValue()
501 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000502
Daniel Dunbar4c43e312010-03-20 05:53:45 +0000503 // Compute the new offset in the appropriate width.
504
505 QualType PointeeType =
506 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCall45d55e42010-05-07 21:00:08 +0000507 CharUnits SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Anders Carlssonef56fba2009-02-19 04:55:58 +0000509 // Explicitly handle GNU void* and function pointer arithmetic extensions.
510 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCall45d55e42010-05-07 21:00:08 +0000511 SizeOfPointee = CharUnits::One();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000512 else
John McCall45d55e42010-05-07 21:00:08 +0000513 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman9a156e52008-11-12 09:44:48 +0000514
Chris Lattner05706e882008-07-11 18:11:29 +0000515 if (E->getOpcode() == BinaryOperator::Add)
John McCall45d55e42010-05-07 21:00:08 +0000516 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000517 else
John McCall45d55e42010-05-07 21:00:08 +0000518 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman9a156e52008-11-12 09:44:48 +0000519
John McCall45d55e42010-05-07 21:00:08 +0000520 return true;
Chris Lattner05706e882008-07-11 18:11:29 +0000521}
Eli Friedman9a156e52008-11-12 09:44:48 +0000522
John McCall45d55e42010-05-07 21:00:08 +0000523bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
524 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman9a156e52008-11-12 09:44:48 +0000525}
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner05706e882008-07-11 18:11:29 +0000527
John McCall45d55e42010-05-07 21:00:08 +0000528bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman847a2bc2009-12-27 05:43:15 +0000529 Expr* SubExpr = E->getSubExpr();
Chris Lattner05706e882008-07-11 18:11:29 +0000530
Eli Friedman847a2bc2009-12-27 05:43:15 +0000531 switch (E->getCastKind()) {
532 default:
533 break;
534
535 case CastExpr::CK_Unknown: {
536 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
537
538 // Check for pointer->pointer cast
539 if (SubExpr->getType()->isPointerType() ||
540 SubExpr->getType()->isObjCObjectPointerType() ||
541 SubExpr->getType()->isNullPtrType() ||
542 SubExpr->getType()->isBlockPointerType())
543 return Visit(SubExpr);
544
545 if (SubExpr->getType()->isIntegralType()) {
John McCall45d55e42010-05-07 21:00:08 +0000546 APValue Value;
547 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000548 break;
549
John McCall45d55e42010-05-07 21:00:08 +0000550 if (Value.isInt()) {
551 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
552 Result.Base = 0;
553 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
554 return true;
555 } else {
556 Result.Base = Value.getLValueBase();
557 Result.Offset = Value.getLValueOffset();
558 return true;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000559 }
Eli Friedman847a2bc2009-12-27 05:43:15 +0000560 }
561 break;
Chris Lattner05706e882008-07-11 18:11:29 +0000562 }
Mike Stump11289f42009-09-09 15:08:12 +0000563
Eli Friedman847a2bc2009-12-27 05:43:15 +0000564 case CastExpr::CK_NoOp:
565 case CastExpr::CK_BitCast:
566 case CastExpr::CK_AnyPointerToObjCPointerCast:
567 case CastExpr::CK_AnyPointerToBlockPointerCast:
568 return Visit(SubExpr);
569
570 case CastExpr::CK_IntegralToPointer: {
John McCall45d55e42010-05-07 21:00:08 +0000571 APValue Value;
572 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000573 break;
Daniel Dunbarce399542009-02-20 18:22:23 +0000574
John McCall45d55e42010-05-07 21:00:08 +0000575 if (Value.isInt()) {
576 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
577 Result.Base = 0;
578 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
579 return true;
580 } else {
581 // Cast is of an lvalue, no need to change value.
582 Result.Base = Value.getLValueBase();
583 Result.Offset = Value.getLValueOffset();
584 return true;
Chris Lattner05706e882008-07-11 18:11:29 +0000585 }
586 }
Eli Friedman847a2bc2009-12-27 05:43:15 +0000587 case CastExpr::CK_ArrayToPointerDecay:
John McCall45d55e42010-05-07 21:00:08 +0000588 case CastExpr::CK_FunctionToPointerDecay:
589 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman9a156e52008-11-12 09:44:48 +0000590 }
591
John McCall45d55e42010-05-07 21:00:08 +0000592 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000593}
Chris Lattner05706e882008-07-11 18:11:29 +0000594
John McCall45d55e42010-05-07 21:00:08 +0000595bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000596 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall481e3a82010-01-23 02:40:42 +0000597 Builtin::BI__builtin___CFStringMakeConstantString ||
598 E->isBuiltinCall(Info.Ctx) ==
599 Builtin::BI__builtin___NSStringMakeConstantString)
John McCall45d55e42010-05-07 21:00:08 +0000600 return Success(E);
601 return false;
Eli Friedmanc69d4542009-01-25 01:54:01 +0000602}
603
John McCall45d55e42010-05-07 21:00:08 +0000604bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000605 bool BoolResult;
606 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCall45d55e42010-05-07 21:00:08 +0000607 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000608
609 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCall45d55e42010-05-07 21:00:08 +0000610 return Visit(EvalExpr);
Eli Friedman9a156e52008-11-12 09:44:48 +0000611}
Chris Lattner05706e882008-07-11 18:11:29 +0000612
613//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000614// Vector Evaluation
615//===----------------------------------------------------------------------===//
616
617namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000618 class VectorExprEvaluator
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000619 : public StmtVisitor<VectorExprEvaluator, APValue> {
620 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000621 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000622 public:
Mike Stump11289f42009-09-09 15:08:12 +0000623
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000624 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000625
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000626 APValue VisitStmt(Stmt *S) {
627 return APValue();
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Eli Friedman3ae59112009-02-23 04:23:56 +0000630 APValue VisitParenExpr(ParenExpr *E)
631 { return Visit(E->getSubExpr()); }
632 APValue VisitUnaryExtension(const UnaryOperator *E)
633 { return Visit(E->getSubExpr()); }
634 APValue VisitUnaryPlus(const UnaryOperator *E)
635 { return Visit(E->getSubExpr()); }
636 APValue VisitUnaryReal(const UnaryOperator *E)
637 { return Visit(E->getSubExpr()); }
638 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
639 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000640 APValue VisitCastExpr(const CastExpr* E);
641 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
642 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000643 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000644 APValue VisitChooseExpr(const ChooseExpr *E)
645 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000646 APValue VisitUnaryImag(const UnaryOperator *E);
647 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000648 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000649 // shufflevector, ExtVectorElementExpr
650 // (Note that these require implementing conversions
651 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000652 };
653} // end anonymous namespace
654
655static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
656 if (!E->getType()->isVectorType())
657 return false;
658 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
659 return !Result.isUninit();
660}
661
662APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000663 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000664 QualType EltTy = VTy->getElementType();
665 unsigned NElts = VTy->getNumElements();
666 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000668 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000669 QualType SETy = SE->getType();
670 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000671
Nate Begeman2ffd3842009-06-26 18:22:18 +0000672 // Check for vector->vector bitcast and scalar->vector splat.
673 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000674 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000675 } else if (SETy->isIntegerType()) {
676 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000677 if (!EvaluateInteger(SE, IntResult, Info))
678 return APValue();
679 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000680 } else if (SETy->isRealFloatingType()) {
681 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000682 if (!EvaluateFloat(SE, F, Info))
683 return APValue();
684 Result = APValue(F);
685 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000686 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000687
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000688 // For casts of a scalar to ExtVector, convert the scalar to the element type
689 // and splat it to all elements.
690 if (E->getType()->isExtVectorType()) {
691 if (EltTy->isIntegerType() && Result.isInt())
692 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
693 Info.Ctx));
694 else if (EltTy->isIntegerType())
695 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
696 Info.Ctx));
697 else if (EltTy->isRealFloatingType() && Result.isInt())
698 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
699 Info.Ctx));
700 else if (EltTy->isRealFloatingType())
701 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
702 Info.Ctx));
703 else
704 return APValue();
705
706 // Splat and create vector APValue.
707 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
708 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000709 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000710
711 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
712 // to the vector. To construct the APValue vector initializer, bitcast the
713 // initializing value to an APInt, and shift out the bits pertaining to each
714 // element.
715 APSInt Init;
716 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000717
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000718 llvm::SmallVector<APValue, 4> Elts;
719 for (unsigned i = 0; i != NElts; ++i) {
720 APSInt Tmp = Init;
721 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000722
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000723 if (EltTy->isIntegerType())
724 Elts.push_back(APValue(Tmp));
725 else if (EltTy->isRealFloatingType())
726 Elts.push_back(APValue(APFloat(Tmp)));
727 else
728 return APValue();
729
730 Init >>= EltWidth;
731 }
732 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000733}
734
Mike Stump11289f42009-09-09 15:08:12 +0000735APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000736VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
737 return this->Visit(const_cast<Expr*>(E->getInitializer()));
738}
739
Mike Stump11289f42009-09-09 15:08:12 +0000740APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000741VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000742 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000743 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000744 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000745
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000746 QualType EltTy = VT->getElementType();
747 llvm::SmallVector<APValue, 4> Elements;
748
Eli Friedman3ae59112009-02-23 04:23:56 +0000749 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000750 if (EltTy->isIntegerType()) {
751 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000752 if (i < NumInits) {
753 if (!EvaluateInteger(E->getInit(i), sInt, Info))
754 return APValue();
755 } else {
756 sInt = Info.Ctx.MakeIntValue(0, EltTy);
757 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000758 Elements.push_back(APValue(sInt));
759 } else {
760 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000761 if (i < NumInits) {
762 if (!EvaluateFloat(E->getInit(i), f, Info))
763 return APValue();
764 } else {
765 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
766 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000767 Elements.push_back(APValue(f));
768 }
769 }
770 return APValue(&Elements[0], Elements.size());
771}
772
Mike Stump11289f42009-09-09 15:08:12 +0000773APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000774VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000775 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000776 QualType EltTy = VT->getElementType();
777 APValue ZeroElement;
778 if (EltTy->isIntegerType())
779 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
780 else
781 ZeroElement =
782 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
783
784 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
785 return APValue(&Elements[0], Elements.size());
786}
787
788APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
789 bool BoolResult;
790 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
791 return APValue();
792
793 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
794
795 APValue Result;
796 if (EvaluateVector(EvalExpr, Result, Info))
797 return Result;
798 return APValue();
799}
800
Eli Friedman3ae59112009-02-23 04:23:56 +0000801APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
802 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
803 Info.EvalResult.HasSideEffects = true;
804 return GetZeroVector(E->getType());
805}
806
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000807//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000808// Integer Evaluation
809//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000810
811namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000812class IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000813 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000814 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000815 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000816public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000817 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000818 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000819
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000820 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000821 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000822 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000823 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000824 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000825 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000826 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000827 return true;
828 }
829
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000830 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000831 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000832 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000833 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000834 Result = APValue(APSInt(I));
835 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000836 return true;
837 }
838
839 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000840 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000841 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000842 return true;
843 }
844
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000845 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000846 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000847 if (Info.EvalResult.Diag == 0) {
848 Info.EvalResult.DiagLoc = L;
849 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000850 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000851 }
Chris Lattner99415702008-07-12 00:14:42 +0000852 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000855 //===--------------------------------------------------------------------===//
856 // Visitor Methods
857 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000858
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000859 bool VisitStmt(Stmt *) {
860 assert(0 && "This should be called on integers, stmts are not integers");
861 return false;
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000864 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000865 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
Chris Lattnere13042c2008-07-11 19:10:17 +0000868 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000869
Chris Lattner7174bf32008-07-12 00:38:25 +0000870 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000871 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000872 }
873 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000874 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000875 }
876 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000877 // Per gcc docs "this built-in function ignores top level
878 // qualifiers". We need to use the canonical version to properly
879 // be able to strip CRV qualifiers from the type.
880 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
881 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000882 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000883 T1.getUnqualifiedType()),
884 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000885 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000886
887 bool CheckReferencedDecl(const Expr *E, const Decl *D);
888 bool VisitDeclRefExpr(const DeclRefExpr *E) {
889 return CheckReferencedDecl(E, E->getDecl());
890 }
891 bool VisitMemberExpr(const MemberExpr *E) {
892 if (CheckReferencedDecl(E, E->getMemberDecl())) {
893 // Conservatively assume a MemberExpr will have side-effects
894 Info.EvalResult.HasSideEffects = true;
895 return true;
896 }
897 return false;
898 }
899
Eli Friedmand5c93992010-02-13 00:10:10 +0000900 bool VisitCallExpr(CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000901 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor882211c2010-04-28 22:16:22 +0000902 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000903 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000904 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000905
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000906 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000907 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
908
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000909 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000910 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Anders Carlsson39def3a2008-12-21 22:39:40 +0000913 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000914 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000915 }
Mike Stump11289f42009-09-09 15:08:12 +0000916
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000917 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000918 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000919 }
920
Eli Friedman4e7a2412009-02-27 04:45:43 +0000921 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
922 return Success(0, E);
923 }
924
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000925 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000926 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000927 }
928
Eli Friedman449fe542009-03-23 04:56:01 +0000929 bool VisitChooseExpr(const ChooseExpr *E) {
930 return Visit(E->getChosenSubExpr(Info.Ctx));
931 }
932
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000933 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000934 bool VisitUnaryImag(const UnaryOperator *E);
935
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000936private:
Ken Dyck160146e2010-01-27 17:10:57 +0000937 CharUnits GetAlignOfExpr(const Expr *E);
938 CharUnits GetAlignOfType(QualType T);
John McCall95007602010-05-10 23:27:23 +0000939 static QualType GetObjectType(const Expr *E);
940 bool TryEvaluateBuiltinObjectSize(CallExpr *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000941 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000942};
Chris Lattner05706e882008-07-11 18:11:29 +0000943} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000944
Daniel Dunbarce399542009-02-20 18:22:23 +0000945static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +0000946 assert(E->getType()->isIntegralType());
Daniel Dunbarce399542009-02-20 18:22:23 +0000947 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
948}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000949
Daniel Dunbarce399542009-02-20 18:22:23 +0000950static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +0000951 assert(E->getType()->isIntegralType());
952
Daniel Dunbarce399542009-02-20 18:22:23 +0000953 APValue Val;
954 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
955 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000956 Result = Val.getInt();
957 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000958}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000959
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000960bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000961 // Enums are integer constant exprs.
Eli Friedmanee275c82009-12-10 22:29:29 +0000962 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
963 return Success(ECD->getInitVal(), E);
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000964
965 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000966 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000967 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
968 == Qualifiers::Const) {
Anders Carlssonb0695ef2010-02-03 21:58:41 +0000969
970 if (isa<ParmVarDecl>(D))
971 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
972
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000973 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000974 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000975 if (APValue *V = VD->getEvaluatedValue()) {
976 if (V->isInt())
977 return Success(V->getInt(), E);
978 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
979 }
980
981 if (VD->isEvaluatingValue())
982 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
983
984 VD->setEvaluatingValue();
985
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000986 if (Visit(const_cast<Expr*>(Init))) {
987 // Cache the evaluated value in the variable declaration.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000988 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000989 return true;
990 }
991
Eli Friedman1d6fb162009-12-03 20:31:57 +0000992 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000993 return false;
994 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000995 }
996 }
997
Chris Lattner7174bf32008-07-12 00:38:25 +0000998 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000999 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +00001000}
1001
Chris Lattner86ee2862008-10-06 06:40:35 +00001002/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1003/// as GCC.
1004static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1005 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001006 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +00001007 enum gcc_type_class {
1008 no_type_class = -1,
1009 void_type_class, integer_type_class, char_type_class,
1010 enumeral_type_class, boolean_type_class,
1011 pointer_type_class, reference_type_class, offset_type_class,
1012 real_type_class, complex_type_class,
1013 function_type_class, method_type_class,
1014 record_type_class, union_type_class,
1015 array_type_class, string_type_class,
1016 lang_type_class
1017 };
Mike Stump11289f42009-09-09 15:08:12 +00001018
1019 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +00001020 // ideal, however it is what gcc does.
1021 if (E->getNumArgs() == 0)
1022 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner86ee2862008-10-06 06:40:35 +00001024 QualType ArgTy = E->getArg(0)->getType();
1025 if (ArgTy->isVoidType())
1026 return void_type_class;
1027 else if (ArgTy->isEnumeralType())
1028 return enumeral_type_class;
1029 else if (ArgTy->isBooleanType())
1030 return boolean_type_class;
1031 else if (ArgTy->isCharType())
1032 return string_type_class; // gcc doesn't appear to use char_type_class
1033 else if (ArgTy->isIntegerType())
1034 return integer_type_class;
1035 else if (ArgTy->isPointerType())
1036 return pointer_type_class;
1037 else if (ArgTy->isReferenceType())
1038 return reference_type_class;
1039 else if (ArgTy->isRealType())
1040 return real_type_class;
1041 else if (ArgTy->isComplexType())
1042 return complex_type_class;
1043 else if (ArgTy->isFunctionType())
1044 return function_type_class;
Douglas Gregor8385a062010-04-26 21:31:17 +00001045 else if (ArgTy->isStructureOrClassType())
Chris Lattner86ee2862008-10-06 06:40:35 +00001046 return record_type_class;
1047 else if (ArgTy->isUnionType())
1048 return union_type_class;
1049 else if (ArgTy->isArrayType())
1050 return array_type_class;
1051 else if (ArgTy->isUnionType())
1052 return union_type_class;
1053 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1054 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1055 return -1;
1056}
1057
John McCall95007602010-05-10 23:27:23 +00001058/// Retrieves the "underlying object type" of the given expression,
1059/// as used by __builtin_object_size.
1060QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1061 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1062 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1063 return VD->getType();
1064 } else if (isa<CompoundLiteralExpr>(E)) {
1065 return E->getType();
1066 }
1067
1068 return QualType();
1069}
1070
1071bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
1072 // TODO: Perhaps we should let LLVM lower this?
1073 LValue Base;
1074 if (!EvaluatePointer(E->getArg(0), Base, Info))
1075 return false;
1076
1077 // If we can prove the base is null, lower to zero now.
1078 const Expr *LVBase = Base.getLValueBase();
1079 if (!LVBase) return Success(0, E);
1080
1081 QualType T = GetObjectType(LVBase);
1082 if (T.isNull() ||
1083 T->isIncompleteType() ||
1084 !T->isObjectType() ||
1085 T->isVariablyModifiedType() ||
1086 T->isDependentType())
1087 return false;
1088
1089 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1090 CharUnits Offset = Base.getLValueOffset();
1091
1092 if (!Offset.isNegative() && Offset <= Size)
1093 Size -= Offset;
1094 else
1095 Size = CharUnits::Zero();
1096 return Success(Size.getQuantity(), E);
1097}
1098
Eli Friedmand5c93992010-02-13 00:10:10 +00001099bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001100 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001101 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001102 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +00001103
1104 case Builtin::BI__builtin_object_size: {
John McCall95007602010-05-10 23:27:23 +00001105 if (TryEvaluateBuiltinObjectSize(E))
1106 return true;
Mike Stump722cedf2009-10-26 18:35:08 +00001107
Eric Christopher99469702010-01-19 22:58:35 +00001108 // If evaluating the argument has side-effects we can't determine
1109 // the size of the object and lower it to unknown now.
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00001110 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer0128f662010-01-03 18:18:37 +00001111 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattner4f105592009-11-03 19:48:51 +00001112 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +00001113 return Success(0, E);
1114 }
Mike Stump876387b2009-10-27 22:09:17 +00001115
Mike Stump722cedf2009-10-26 18:35:08 +00001116 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1117 }
1118
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001119 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001120 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +00001121
Anders Carlsson4c76e932008-11-24 04:21:33 +00001122 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001123 // __builtin_constant_p always has one operand: it returns true if that
1124 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001125 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +00001126
1127 case Builtin::BI__builtin_eh_return_data_regno: {
1128 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1129 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1130 return Success(Operand, E);
1131 }
Eli Friedmand5c93992010-02-13 00:10:10 +00001132
1133 case Builtin::BI__builtin_expect:
1134 return Visit(E->getArg(0));
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001135 }
Chris Lattner7174bf32008-07-12 00:38:25 +00001136}
Anders Carlsson4a3585b2008-07-08 15:34:11 +00001137
Chris Lattnere13042c2008-07-11 19:10:17 +00001138bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001139 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001140 if (!Visit(E->getRHS()))
1141 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001142
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001143 // If we can't evaluate the LHS, it might have side effects;
1144 // conservatively mark it.
1145 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1146 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001147
Anders Carlsson564730a2008-12-01 02:07:06 +00001148 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001149 }
1150
1151 if (E->isLogicalOp()) {
1152 // These need to be handled specially because the operands aren't
1153 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001154 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001156 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001157 // We were able to evaluate the LHS, see if we can get away with not
1158 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001159 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001160 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001161
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001162 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001163 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001164 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001165 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001166 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001167 }
1168 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001169 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001170 // We can't evaluate the LHS; however, sometimes the result
1171 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001172 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001173 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001174 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001175 // must have had side effects.
1176 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001177
1178 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001179 }
1180 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001181 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001182
Eli Friedman5a332ea2008-11-13 06:09:17 +00001183 return false;
1184 }
1185
Anders Carlssonacc79812008-11-16 07:17:21 +00001186 QualType LHSTy = E->getLHS()->getType();
1187 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001188
1189 if (LHSTy->isAnyComplexType()) {
1190 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCall93d91dc2010-05-07 17:22:02 +00001191 ComplexValue LHS, RHS;
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001192
1193 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1194 return false;
1195
1196 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1197 return false;
1198
1199 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001200 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001201 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001202 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001203 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1204
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001205 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001206 return Success((CR_r == APFloat::cmpEqual &&
1207 CR_i == APFloat::cmpEqual), E);
1208 else {
1209 assert(E->getOpcode() == BinaryOperator::NE &&
1210 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001211 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wang75c645c2010-04-29 05:53:29 +00001212 CR_r == APFloat::cmpLessThan ||
1213 CR_r == APFloat::cmpUnordered) ||
Mike Stump11289f42009-09-09 15:08:12 +00001214 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wang75c645c2010-04-29 05:53:29 +00001215 CR_i == APFloat::cmpLessThan ||
1216 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001217 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001218 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001219 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001220 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1221 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1222 else {
1223 assert(E->getOpcode() == BinaryOperator::NE &&
1224 "Invalid compex comparison.");
1225 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1226 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1227 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001228 }
1229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Anders Carlssonacc79812008-11-16 07:17:21 +00001231 if (LHSTy->isRealFloatingType() &&
1232 RHSTy->isRealFloatingType()) {
1233 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001234
Anders Carlssonacc79812008-11-16 07:17:21 +00001235 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1236 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001237
Anders Carlssonacc79812008-11-16 07:17:21 +00001238 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1239 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001240
Anders Carlssonacc79812008-11-16 07:17:21 +00001241 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001242
Anders Carlssonacc79812008-11-16 07:17:21 +00001243 switch (E->getOpcode()) {
1244 default:
1245 assert(0 && "Invalid binary operator!");
1246 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001247 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001248 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001249 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001250 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001251 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001252 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001253 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001254 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001255 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001256 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001257 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001258 return Success(CR == APFloat::cmpGreaterThan
Mon P Wang75c645c2010-04-29 05:53:29 +00001259 || CR == APFloat::cmpLessThan
1260 || CR == APFloat::cmpUnordered, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001261 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001262 }
Mike Stump11289f42009-09-09 15:08:12 +00001263
Eli Friedmana38da572009-04-28 19:17:36 +00001264 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1265 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
John McCall45d55e42010-05-07 21:00:08 +00001266 LValue LHSValue;
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001267 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1268 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001269
John McCall45d55e42010-05-07 21:00:08 +00001270 LValue RHSValue;
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001271 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1272 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001273
Eli Friedman334046a2009-06-14 02:17:33 +00001274 // Reject any bases from the normal codepath; we special-case comparisons
1275 // to null.
1276 if (LHSValue.getLValueBase()) {
1277 if (!E->isEqualityOp())
1278 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001279 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001280 return false;
1281 bool bres;
1282 if (!EvalPointerValueAsBool(LHSValue, bres))
1283 return false;
1284 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1285 } else if (RHSValue.getLValueBase()) {
1286 if (!E->isEqualityOp())
1287 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001288 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001289 return false;
1290 bool bres;
1291 if (!EvalPointerValueAsBool(RHSValue, bres))
1292 return false;
1293 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1294 }
Eli Friedman64004332009-03-23 04:38:34 +00001295
Eli Friedmana38da572009-04-28 19:17:36 +00001296 if (E->getOpcode() == BinaryOperator::Sub) {
Chris Lattner882bdf22010-04-20 17:13:14 +00001297 QualType Type = E->getLHS()->getType();
1298 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001299
Ken Dyck02990832010-01-15 12:37:54 +00001300 CharUnits ElementSize = CharUnits::One();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001301 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dyck02990832010-01-15 12:37:54 +00001302 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedman64004332009-03-23 04:38:34 +00001303
Ken Dyck02990832010-01-15 12:37:54 +00001304 CharUnits Diff = LHSValue.getLValueOffset() -
1305 RHSValue.getLValueOffset();
1306 return Success(Diff / ElementSize, E);
Eli Friedmana38da572009-04-28 19:17:36 +00001307 }
1308 bool Result;
1309 if (E->getOpcode() == BinaryOperator::EQ) {
1310 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001311 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001312 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1313 }
1314 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001315 }
1316 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001317 if (!LHSTy->isIntegralType() ||
1318 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001319 // We can't continue from here for non-integral types, and they
1320 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001321 return false;
1322 }
1323
Anders Carlsson9c181652008-07-08 14:35:21 +00001324 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001325 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001326 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001327
Eli Friedman94c25c62009-03-24 01:14:50 +00001328 APValue RHSVal;
1329 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001330 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001331
1332 // Handle cases like (unsigned long)&a + 4.
1333 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001334 CharUnits Offset = Result.getLValueOffset();
1335 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1336 RHSVal.getInt().getZExtValue());
Eli Friedman94c25c62009-03-24 01:14:50 +00001337 if (E->getOpcode() == BinaryOperator::Add)
Ken Dyck02990832010-01-15 12:37:54 +00001338 Offset += AdditionalOffset;
Eli Friedman94c25c62009-03-24 01:14:50 +00001339 else
Ken Dyck02990832010-01-15 12:37:54 +00001340 Offset -= AdditionalOffset;
1341 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001342 return true;
1343 }
1344
1345 // Handle cases like 4 + (unsigned long)&a
1346 if (E->getOpcode() == BinaryOperator::Add &&
1347 RHSVal.isLValue() && Result.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001348 CharUnits Offset = RHSVal.getLValueOffset();
1349 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1350 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001351 return true;
1352 }
1353
1354 // All the following cases expect both operands to be an integer
1355 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001356 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001357
Eli Friedman94c25c62009-03-24 01:14:50 +00001358 APSInt& RHS = RHSVal.getInt();
1359
Anders Carlsson9c181652008-07-08 14:35:21 +00001360 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001361 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001362 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001363 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1364 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1365 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1366 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1367 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1368 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001369 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001370 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001371 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001372 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001373 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001374 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001375 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001376 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001377 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001378 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001379 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001380 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1381 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001382 }
1383 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001384 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001385 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1386 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001389 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1390 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1391 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1392 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1393 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1394 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001395 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001396}
1397
Nuno Lopes42042612008-11-16 19:28:31 +00001398bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001399 bool Cond;
1400 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001401 return false;
1402
Nuno Lopes527b5a62008-11-16 22:06:39 +00001403 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001404}
1405
Ken Dyck160146e2010-01-27 17:10:57 +00001406CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001407 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1408 // the result is the size of the referenced type."
1409 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1410 // result shall be the alignment of the referenced type."
1411 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1412 T = Ref->getPointeeType();
1413
Chris Lattner24aeeab2009-01-24 21:09:06 +00001414 // Get information about the alignment.
1415 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001416
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001417 // __alignof is defined to return the preferred alignment.
Ken Dyck160146e2010-01-27 17:10:57 +00001418 return CharUnits::fromQuantity(
1419 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001420}
1421
Ken Dyck160146e2010-01-27 17:10:57 +00001422CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattner68061312009-01-24 21:53:27 +00001423 E = E->IgnoreParens();
1424
1425 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001426 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001427 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001428 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1429 /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001430
Chris Lattner68061312009-01-24 21:53:27 +00001431 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001432 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1433 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001434
Chris Lattner24aeeab2009-01-24 21:09:06 +00001435 return GetAlignOfType(E->getType());
1436}
1437
1438
Sebastian Redl6f282892008-11-11 17:56:53 +00001439/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1440/// expression's type.
1441bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001442 // Handle alignof separately.
1443 if (!E->isSizeOf()) {
1444 if (E->isArgumentType())
Ken Dyck160146e2010-01-27 17:10:57 +00001445 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001446 else
Ken Dyck160146e2010-01-27 17:10:57 +00001447 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001448 }
Eli Friedman64004332009-03-23 04:38:34 +00001449
Sebastian Redl6f282892008-11-11 17:56:53 +00001450 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001451 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1452 // the result is the size of the referenced type."
1453 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1454 // result shall be the alignment of the referenced type."
1455 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1456 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001457
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001458 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1459 // extension.
1460 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1461 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001462
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001463 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001464 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001465 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001466
Chris Lattner24aeeab2009-01-24 21:09:06 +00001467 // Get information about the size.
Ken Dyck40775002010-01-11 17:06:35 +00001468 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001469}
1470
Douglas Gregor882211c2010-04-28 22:16:22 +00001471bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1472 CharUnits Result;
1473 unsigned n = E->getNumComponents();
1474 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1475 if (n == 0)
1476 return false;
1477 QualType CurrentType = E->getTypeSourceInfo()->getType();
1478 for (unsigned i = 0; i != n; ++i) {
1479 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1480 switch (ON.getKind()) {
1481 case OffsetOfExpr::OffsetOfNode::Array: {
1482 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1483 APSInt IdxResult;
1484 if (!EvaluateInteger(Idx, IdxResult, Info))
1485 return false;
1486 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1487 if (!AT)
1488 return false;
1489 CurrentType = AT->getElementType();
1490 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1491 Result += IdxResult.getSExtValue() * ElementSize;
1492 break;
1493 }
1494
1495 case OffsetOfExpr::OffsetOfNode::Field: {
1496 FieldDecl *MemberDecl = ON.getField();
1497 const RecordType *RT = CurrentType->getAs<RecordType>();
1498 if (!RT)
1499 return false;
1500 RecordDecl *RD = RT->getDecl();
1501 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1502 unsigned i = 0;
1503 // FIXME: It would be nice if we didn't have to loop here!
1504 for (RecordDecl::field_iterator Field = RD->field_begin(),
1505 FieldEnd = RD->field_end();
1506 Field != FieldEnd; (void)++Field, ++i) {
1507 if (*Field == MemberDecl)
1508 break;
1509 }
Douglas Gregord1702062010-04-29 00:18:15 +00001510 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1511 Result += CharUnits::fromQuantity(
1512 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor882211c2010-04-28 22:16:22 +00001513 CurrentType = MemberDecl->getType().getNonReferenceType();
1514 break;
1515 }
1516
1517 case OffsetOfExpr::OffsetOfNode::Identifier:
1518 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregord1702062010-04-29 00:18:15 +00001519 return false;
1520
1521 case OffsetOfExpr::OffsetOfNode::Base: {
1522 CXXBaseSpecifier *BaseSpec = ON.getBase();
1523 if (BaseSpec->isVirtual())
1524 return false;
1525
1526 // Find the layout of the class whose base we are looking into.
1527 const RecordType *RT = CurrentType->getAs<RecordType>();
1528 if (!RT)
1529 return false;
1530 RecordDecl *RD = RT->getDecl();
1531 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1532
1533 // Find the base class itself.
1534 CurrentType = BaseSpec->getType();
1535 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1536 if (!BaseRT)
1537 return false;
1538
1539 // Add the offset to the base.
1540 Result += CharUnits::fromQuantity(
1541 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1542 / Info.Ctx.getCharWidth());
1543 break;
1544 }
Douglas Gregor882211c2010-04-28 22:16:22 +00001545 }
1546 }
1547 return Success(Result.getQuantity(), E);
1548}
1549
Chris Lattnere13042c2008-07-11 19:10:17 +00001550bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001551 // Special case unary operators that do not need their subexpression
1552 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001553 if (E->isOffsetOfOp()) {
1554 // The AST for offsetof is defined in such a way that we can just
1555 // directly Evaluate it as an l-value.
John McCall45d55e42010-05-07 21:00:08 +00001556 LValue LV;
Eli Friedman988a16b2009-02-27 06:44:11 +00001557 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
Douglas Gregor882211c2010-04-28 22:16:22 +00001558 return false;
Eli Friedman988a16b2009-02-27 06:44:11 +00001559 if (LV.getLValueBase())
Douglas Gregor882211c2010-04-28 22:16:22 +00001560 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001561 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman988a16b2009-02-27 06:44:11 +00001562 }
Douglas Gregor882211c2010-04-28 22:16:22 +00001563
Eli Friedman5a332ea2008-11-13 06:09:17 +00001564 if (E->getOpcode() == UnaryOperator::LNot) {
1565 // LNot's operand isn't necessarily an integer, so we handle it specially.
1566 bool bres;
1567 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1568 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001569 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001570 }
1571
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001572 // Only handle integral operations...
1573 if (!E->getSubExpr()->getType()->isIntegralType())
1574 return false;
1575
Chris Lattnercdf34e72008-07-11 22:52:41 +00001576 // Get the operand value into 'Result'.
1577 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001578 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001579
Chris Lattnerf09ad162008-07-11 22:15:16 +00001580 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001581 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001582 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1583 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001584 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001585 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001586 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1587 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001588 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001589 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001590 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001591 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001592 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001593 if (!Result.isInt()) return false;
1594 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001595 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001596 if (!Result.isInt()) return false;
1597 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001598 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001599}
Mike Stump11289f42009-09-09 15:08:12 +00001600
Chris Lattner477c4be2008-07-12 01:15:53 +00001601/// HandleCast - This is used to evaluate implicit or explicit casts where the
1602/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001603bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001604 Expr *SubExpr = E->getSubExpr();
1605 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001606 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001607
Eli Friedman9a156e52008-11-12 09:44:48 +00001608 if (DestType->isBooleanType()) {
1609 bool BoolResult;
1610 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1611 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001612 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001613 }
1614
Anders Carlsson9c181652008-07-08 14:35:21 +00001615 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001616 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001617 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001618 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001619
Eli Friedman742421e2009-02-20 01:15:07 +00001620 if (!Result.isInt()) {
1621 // Only allow casts of lvalues if they are lossless.
1622 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1623 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001624
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001625 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001626 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001627 }
Mike Stump11289f42009-09-09 15:08:12 +00001628
Chris Lattner477c4be2008-07-12 01:15:53 +00001629 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001630 if (SrcType->isPointerType()) {
John McCall45d55e42010-05-07 21:00:08 +00001631 LValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001632 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001633 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001634
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001635 if (LV.getLValueBase()) {
1636 // Only allow based lvalue casts if they are lossless.
1637 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1638 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001639
John McCall45d55e42010-05-07 21:00:08 +00001640 LV.moveInto(Result);
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001641 return true;
1642 }
1643
Ken Dyck02990832010-01-15 12:37:54 +00001644 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1645 SrcType);
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001646 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001647 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001648
Eli Friedman742421e2009-02-20 01:15:07 +00001649 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1650 // This handles double-conversion cases, where there's both
1651 // an l-value promotion and an implicit conversion to int.
John McCall45d55e42010-05-07 21:00:08 +00001652 LValue LV;
Eli Friedman742421e2009-02-20 01:15:07 +00001653 if (!EvaluateLValue(SubExpr, LV, Info))
1654 return false;
1655
1656 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1657 return false;
1658
John McCall45d55e42010-05-07 21:00:08 +00001659 LV.moveInto(Result);
Eli Friedman742421e2009-02-20 01:15:07 +00001660 return true;
1661 }
1662
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001663 if (SrcType->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001664 ComplexValue C;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001665 if (!EvaluateComplex(SubExpr, C, Info))
1666 return false;
1667 if (C.isComplexFloat())
1668 return Success(HandleFloatToIntCast(DestType, SrcType,
1669 C.getComplexFloatReal(), Info.Ctx),
1670 E);
1671 else
1672 return Success(HandleIntToIntCast(DestType, SrcType,
1673 C.getComplexIntReal(), Info.Ctx), E);
1674 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001675 // FIXME: Handle vectors
1676
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001677 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001678 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001679
Eli Friedman24c01542008-08-22 00:06:13 +00001680 APFloat F(0.0);
1681 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001682 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001683
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001684 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001685}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001686
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001687bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1688 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001689 ComplexValue LV;
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001690 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1691 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1692 return Success(LV.getComplexIntReal(), E);
1693 }
1694
1695 return Visit(E->getSubExpr());
1696}
1697
Eli Friedman4e7a2412009-02-27 04:45:43 +00001698bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001699 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001700 ComplexValue LV;
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001701 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1702 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1703 return Success(LV.getComplexIntImag(), E);
1704 }
1705
Eli Friedman4e7a2412009-02-27 04:45:43 +00001706 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1707 Info.EvalResult.HasSideEffects = true;
1708 return Success(0, E);
1709}
1710
Chris Lattner05706e882008-07-11 18:11:29 +00001711//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001712// Float Evaluation
1713//===----------------------------------------------------------------------===//
1714
1715namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001716class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001717 : public StmtVisitor<FloatExprEvaluator, bool> {
1718 EvalInfo &Info;
1719 APFloat &Result;
1720public:
1721 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1722 : Info(info), Result(result) {}
1723
1724 bool VisitStmt(Stmt *S) {
1725 return false;
1726 }
1727
1728 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001729 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001730
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001731 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001732 bool VisitBinaryOperator(const BinaryOperator *E);
1733 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001734 bool VisitCastExpr(CastExpr *E);
1735 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanf3da3342009-12-04 02:12:53 +00001736 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001737
Eli Friedman449fe542009-03-23 04:56:01 +00001738 bool VisitChooseExpr(const ChooseExpr *E)
1739 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1740 bool VisitUnaryExtension(const UnaryOperator *E)
1741 { return Visit(E->getSubExpr()); }
John McCallb1fb0d32010-05-07 22:08:54 +00001742 bool VisitUnaryReal(const UnaryOperator *E);
1743 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001744
John McCallb1fb0d32010-05-07 22:08:54 +00001745 // FIXME: Missing: array subscript of vector, member of vector,
1746 // ImplicitValueInitExpr
Eli Friedman24c01542008-08-22 00:06:13 +00001747};
1748} // end anonymous namespace
1749
1750static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +00001751 assert(E->getType()->isRealFloatingType());
Eli Friedman24c01542008-08-22 00:06:13 +00001752 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1753}
1754
John McCall16291492010-02-28 13:00:19 +00001755static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1756 QualType ResultTy,
1757 const Expr *Arg,
1758 bool SNaN,
1759 llvm::APFloat &Result) {
1760 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1761 if (!S) return false;
1762
1763 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1764
1765 llvm::APInt fill;
1766
1767 // Treat empty strings as if they were zero.
1768 if (S->getString().empty())
1769 fill = llvm::APInt(32, 0);
1770 else if (S->getString().getAsInteger(0, fill))
1771 return false;
1772
1773 if (SNaN)
1774 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1775 else
1776 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1777 return true;
1778}
1779
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001780bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001781 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001782 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001783 case Builtin::BI__builtin_huge_val:
1784 case Builtin::BI__builtin_huge_valf:
1785 case Builtin::BI__builtin_huge_vall:
1786 case Builtin::BI__builtin_inf:
1787 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001788 case Builtin::BI__builtin_infl: {
1789 const llvm::fltSemantics &Sem =
1790 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001791 Result = llvm::APFloat::getInf(Sem);
1792 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001793 }
Mike Stump11289f42009-09-09 15:08:12 +00001794
John McCall16291492010-02-28 13:00:19 +00001795 case Builtin::BI__builtin_nans:
1796 case Builtin::BI__builtin_nansf:
1797 case Builtin::BI__builtin_nansl:
1798 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1799 true, Result);
1800
Chris Lattner0b7282e2008-10-06 06:31:58 +00001801 case Builtin::BI__builtin_nan:
1802 case Builtin::BI__builtin_nanf:
1803 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001804 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001805 // can't constant fold it.
John McCall16291492010-02-28 13:00:19 +00001806 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1807 false, Result);
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001808
1809 case Builtin::BI__builtin_fabs:
1810 case Builtin::BI__builtin_fabsf:
1811 case Builtin::BI__builtin_fabsl:
1812 if (!EvaluateFloat(E->getArg(0), Result, Info))
1813 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001814
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001815 if (Result.isNegative())
1816 Result.changeSign();
1817 return true;
1818
Mike Stump11289f42009-09-09 15:08:12 +00001819 case Builtin::BI__builtin_copysign:
1820 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001821 case Builtin::BI__builtin_copysignl: {
1822 APFloat RHS(0.);
1823 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1824 !EvaluateFloat(E->getArg(1), RHS, Info))
1825 return false;
1826 Result.copySign(RHS);
1827 return true;
1828 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001829 }
1830}
1831
John McCallb1fb0d32010-05-07 22:08:54 +00001832bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1833 ComplexValue CV;
1834 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1835 return false;
1836 Result = CV.FloatReal;
1837 return true;
1838}
1839
1840bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
1841 ComplexValue CV;
1842 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1843 return false;
1844 Result = CV.FloatImag;
1845 return true;
1846}
1847
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001848bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001849 if (E->getOpcode() == UnaryOperator::Deref)
1850 return false;
1851
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001852 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1853 return false;
1854
1855 switch (E->getOpcode()) {
1856 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001857 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001858 return true;
1859 case UnaryOperator::Minus:
1860 Result.changeSign();
1861 return true;
1862 }
1863}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001864
Eli Friedman24c01542008-08-22 00:06:13 +00001865bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001866 if (E->getOpcode() == BinaryOperator::Comma) {
1867 if (!EvaluateFloat(E->getRHS(), Result, Info))
1868 return false;
1869
1870 // If we can't evaluate the LHS, it might have side effects;
1871 // conservatively mark it.
1872 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1873 Info.EvalResult.HasSideEffects = true;
1874
1875 return true;
1876 }
1877
Eli Friedman24c01542008-08-22 00:06:13 +00001878 // FIXME: Diagnostics? I really don't understand how the warnings
1879 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001880 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001881 if (!EvaluateFloat(E->getLHS(), Result, Info))
1882 return false;
1883 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1884 return false;
1885
1886 switch (E->getOpcode()) {
1887 default: return false;
1888 case BinaryOperator::Mul:
1889 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1890 return true;
1891 case BinaryOperator::Add:
1892 Result.add(RHS, APFloat::rmNearestTiesToEven);
1893 return true;
1894 case BinaryOperator::Sub:
1895 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1896 return true;
1897 case BinaryOperator::Div:
1898 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1899 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001900 }
1901}
1902
1903bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1904 Result = E->getValue();
1905 return true;
1906}
1907
Eli Friedman9a156e52008-11-12 09:44:48 +00001908bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1909 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001910
Eli Friedman9a156e52008-11-12 09:44:48 +00001911 if (SubExpr->getType()->isIntegralType()) {
1912 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001913 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001914 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001915 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001916 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001917 return true;
1918 }
1919 if (SubExpr->getType()->isRealFloatingType()) {
1920 if (!Visit(SubExpr))
1921 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001922 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1923 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001924 return true;
1925 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001926 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001927
1928 return false;
1929}
1930
1931bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1932 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1933 return true;
1934}
1935
Eli Friedmanf3da3342009-12-04 02:12:53 +00001936bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1937 bool Cond;
1938 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1939 return false;
1940
1941 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1942}
1943
Eli Friedman24c01542008-08-22 00:06:13 +00001944//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001945// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001946//===----------------------------------------------------------------------===//
1947
1948namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001949class ComplexExprEvaluator
John McCall93d91dc2010-05-07 17:22:02 +00001950 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001951 EvalInfo &Info;
John McCall93d91dc2010-05-07 17:22:02 +00001952 ComplexValue &Result;
Mike Stump11289f42009-09-09 15:08:12 +00001953
Anders Carlsson537969c2008-11-16 20:27:53 +00001954public:
John McCall93d91dc2010-05-07 17:22:02 +00001955 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
1956 : Info(info), Result(Result) {}
Mike Stump11289f42009-09-09 15:08:12 +00001957
Anders Carlsson537969c2008-11-16 20:27:53 +00001958 //===--------------------------------------------------------------------===//
1959 // Visitor Methods
1960 //===--------------------------------------------------------------------===//
1961
John McCall93d91dc2010-05-07 17:22:02 +00001962 bool VisitStmt(Stmt *S) {
1963 return false;
Anders Carlsson537969c2008-11-16 20:27:53 +00001964 }
Mike Stump11289f42009-09-09 15:08:12 +00001965
John McCall93d91dc2010-05-07 17:22:02 +00001966 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson537969c2008-11-16 20:27:53 +00001967
John McCall93d91dc2010-05-07 17:22:02 +00001968 bool VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001969 Expr* SubExpr = E->getSubExpr();
1970
1971 if (SubExpr->getType()->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001972 Result.makeComplexFloat();
1973 APFloat &Imag = Result.FloatImag;
1974 if (!EvaluateFloat(SubExpr, Imag, Info))
1975 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001976
John McCall93d91dc2010-05-07 17:22:02 +00001977 Result.FloatReal = APFloat(Imag.getSemantics());
1978 return true;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001979 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001980 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001981 "Unexpected imaginary literal.");
1982
John McCall93d91dc2010-05-07 17:22:02 +00001983 Result.makeComplexInt();
1984 APSInt &Imag = Result.IntImag;
1985 if (!EvaluateInteger(SubExpr, Imag, Info))
1986 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001987
John McCall93d91dc2010-05-07 17:22:02 +00001988 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
1989 return true;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001990 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001991 }
1992
John McCall93d91dc2010-05-07 17:22:02 +00001993 bool VisitCastExpr(CastExpr *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001994 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001995 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001996 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001997
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001998 if (SubType->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001999 APFloat &Real = Result.FloatReal;
2000 if (!EvaluateFloat(SubExpr, Real, Info))
2001 return false;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002002
2003 if (EltType->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00002004 Result.makeComplexFloat();
2005 Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx);
2006 Result.FloatImag = APFloat(Real.getSemantics());
2007 return true;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002008 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002009 Result.makeComplexInt();
2010 Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx);
2011 Result.IntImag = APSInt(Result.IntReal.getBitWidth(),
2012 !Result.IntReal.isSigned());
2013 return true;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002014 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002015 } else if (SubType->isIntegerType()) {
John McCall93d91dc2010-05-07 17:22:02 +00002016 APSInt &Real = Result.IntReal;
2017 if (!EvaluateInteger(SubExpr, Real, Info))
2018 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002019
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002020 if (EltType->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00002021 Result.makeComplexFloat();
2022 Result.FloatReal
2023 = HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx);
2024 Result.FloatImag = APFloat(Result.FloatReal.getSemantics());
2025 return true;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002026 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002027 Result.makeComplexInt();
2028 Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx);
2029 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2030 return true;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00002031 }
John McCall9dd450b2009-09-21 23:43:11 +00002032 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
John McCall93d91dc2010-05-07 17:22:02 +00002033 if (!Visit(SubExpr))
2034 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002035
2036 QualType SrcType = CT->getElementType();
2037
John McCall93d91dc2010-05-07 17:22:02 +00002038 if (Result.isComplexFloat()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002039 if (EltType->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00002040 Result.makeComplexFloat();
2041 Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType,
2042 Result.FloatReal,
2043 Info.Ctx);
2044 Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType,
2045 Result.FloatImag,
2046 Info.Ctx);
2047 return true;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002048 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002049 Result.makeComplexInt();
2050 Result.IntReal = HandleFloatToIntCast(EltType, SrcType,
2051 Result.FloatReal,
2052 Info.Ctx);
2053 Result.IntImag = HandleFloatToIntCast(EltType, SrcType,
2054 Result.FloatImag,
2055 Info.Ctx);
2056 return true;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002057 }
2058 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002059 assert(Result.isComplexInt() && "Invalid evaluate result.");
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002060 if (EltType->isRealFloatingType()) {
John McCall93d91dc2010-05-07 17:22:02 +00002061 Result.makeComplexFloat();
2062 Result.FloatReal = HandleIntToFloatCast(EltType, SrcType,
2063 Result.IntReal,
2064 Info.Ctx);
2065 Result.FloatImag = HandleIntToFloatCast(EltType, SrcType,
2066 Result.IntImag,
2067 Info.Ctx);
2068 return true;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002069 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002070 Result.makeComplexInt();
2071 Result.IntReal = HandleIntToIntCast(EltType, SrcType,
2072 Result.IntReal,
2073 Info.Ctx);
2074 Result.IntImag = HandleIntToIntCast(EltType, SrcType,
2075 Result.IntImag,
2076 Info.Ctx);
2077 return true;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00002078 }
2079 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002080 }
2081
2082 // FIXME: Handle more casts.
John McCall93d91dc2010-05-07 17:22:02 +00002083 return false;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002084 }
Mike Stump11289f42009-09-09 15:08:12 +00002085
John McCall93d91dc2010-05-07 17:22:02 +00002086 bool VisitBinaryOperator(const BinaryOperator *E);
2087 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedman449fe542009-03-23 04:56:01 +00002088 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCall93d91dc2010-05-07 17:22:02 +00002089 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman449fe542009-03-23 04:56:01 +00002090 { return Visit(E->getSubExpr()); }
2091 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00002092 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00002093};
2094} // end anonymous namespace
2095
John McCall93d91dc2010-05-07 17:22:02 +00002096static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2097 EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +00002098 assert(E->getType()->isAnyComplexType());
John McCall93d91dc2010-05-07 17:22:02 +00002099 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson537969c2008-11-16 20:27:53 +00002100}
2101
John McCall93d91dc2010-05-07 17:22:02 +00002102bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
2103 if (!Visit(E->getLHS()))
2104 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002105
John McCall93d91dc2010-05-07 17:22:02 +00002106 ComplexValue RHS;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002107 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCall93d91dc2010-05-07 17:22:02 +00002108 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002109
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002110 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2111 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002112 switch (E->getOpcode()) {
John McCall93d91dc2010-05-07 17:22:02 +00002113 default: return false;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002114 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002115 if (Result.isComplexFloat()) {
2116 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2117 APFloat::rmNearestTiesToEven);
2118 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2119 APFloat::rmNearestTiesToEven);
2120 } else {
2121 Result.getComplexIntReal() += RHS.getComplexIntReal();
2122 Result.getComplexIntImag() += RHS.getComplexIntImag();
2123 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002124 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002125 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002126 if (Result.isComplexFloat()) {
2127 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2128 APFloat::rmNearestTiesToEven);
2129 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2130 APFloat::rmNearestTiesToEven);
2131 } else {
2132 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2133 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2134 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002135 break;
2136 case BinaryOperator::Mul:
2137 if (Result.isComplexFloat()) {
John McCall93d91dc2010-05-07 17:22:02 +00002138 ComplexValue LHS = Result;
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002139 APFloat &LHS_r = LHS.getComplexFloatReal();
2140 APFloat &LHS_i = LHS.getComplexFloatImag();
2141 APFloat &RHS_r = RHS.getComplexFloatReal();
2142 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00002143
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002144 APFloat Tmp = LHS_r;
2145 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2146 Result.getComplexFloatReal() = Tmp;
2147 Tmp = LHS_i;
2148 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2149 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2150
2151 Tmp = LHS_r;
2152 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2153 Result.getComplexFloatImag() = Tmp;
2154 Tmp = LHS_i;
2155 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2156 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2157 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002158 ComplexValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00002159 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002160 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2161 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00002162 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002163 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2164 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2165 }
2166 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002167 }
2168
John McCall93d91dc2010-05-07 17:22:02 +00002169 return true;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002170}
2171
Anders Carlsson537969c2008-11-16 20:27:53 +00002172//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00002173// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00002174//===----------------------------------------------------------------------===//
2175
John McCall95007602010-05-10 23:27:23 +00002176/// Evaluate - Return true if this is a constant which we can fold using
2177/// any crazy technique (that has nothing to do with language standards) that
2178/// we want to. If this function returns true, it returns the folded constant
2179/// in Result.
2180bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2181 const Expr *E = this;
2182 EvalInfo Info(Ctx, Result);
John McCall45d55e42010-05-07 21:00:08 +00002183 if (E->getType()->isVectorType()) {
2184 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002185 return false;
John McCall45d55e42010-05-07 21:00:08 +00002186 } else if (E->getType()->isIntegerType()) {
2187 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002188 return false;
John McCall45d55e42010-05-07 21:00:08 +00002189 } else if (E->getType()->hasPointerRepresentation()) {
2190 LValue LV;
2191 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002192 return false;
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002193 if (!IsGlobalLValue(LV.Base))
John McCall95007602010-05-10 23:27:23 +00002194 return false;
John McCall45d55e42010-05-07 21:00:08 +00002195 LV.moveInto(Info.EvalResult.Val);
2196 } else if (E->getType()->isRealFloatingType()) {
2197 llvm::APFloat F(0.0);
2198 if (!EvaluateFloat(E, F, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002199 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002200
John McCall45d55e42010-05-07 21:00:08 +00002201 Info.EvalResult.Val = APValue(F);
2202 } else if (E->getType()->isAnyComplexType()) {
2203 ComplexValue C;
2204 if (!EvaluateComplex(E, C, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002205 return false;
John McCall45d55e42010-05-07 21:00:08 +00002206 C.moveInto(Info.EvalResult.Val);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002207 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00002208 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002209
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00002210 return true;
2211}
2212
John McCall1be1c632010-01-05 23:42:56 +00002213bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2214 EvalResult Scratch;
2215 EvalInfo Info(Ctx, Scratch);
2216
2217 return HandleConversionToBool(this, Result, Info);
2218}
2219
Anders Carlsson43168122009-04-10 04:54:13 +00002220bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2221 EvalInfo Info(Ctx, Result);
2222
John McCall45d55e42010-05-07 21:00:08 +00002223 LValue LV;
John McCall95007602010-05-10 23:27:23 +00002224 if (EvaluateLValue(this, LV, Info) &&
2225 !Result.HasSideEffects &&
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002226 IsGlobalLValue(LV.Base)) {
2227 LV.moveInto(Result.Val);
2228 return true;
2229 }
2230 return false;
2231}
2232
2233bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2234 EvalInfo Info(Ctx, Result);
2235
2236 LValue LV;
2237 if (EvaluateLValue(this, LV, Info)) {
John McCall45d55e42010-05-07 21:00:08 +00002238 LV.moveInto(Result.Val);
2239 return true;
2240 }
2241 return false;
Eli Friedman7d45c482009-09-13 10:17:44 +00002242}
2243
Chris Lattner67d7b922008-11-16 21:24:15 +00002244/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00002245/// folded, but discard the result.
2246bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00002247 EvalResult Result;
2248 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00002249}
Anders Carlsson59689ed2008-11-22 21:04:56 +00002250
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00002251bool Expr::HasSideEffects(ASTContext &Ctx) const {
2252 Expr::EvalResult Result;
2253 EvalInfo Info(Ctx, Result);
2254 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2255}
2256
Anders Carlsson59689ed2008-11-22 21:04:56 +00002257APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002258 EvalResult EvalResult;
2259 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00002260 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00002261 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002262 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00002263
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002264 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00002265}
John McCall864e3962010-05-07 05:32:02 +00002266
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002267 bool Expr::EvalResult::isGlobalLValue() const {
2268 assert(Val.isLValue());
2269 return IsGlobalLValue(Val.getLValueBase());
2270 }
2271
2272
John McCall864e3962010-05-07 05:32:02 +00002273/// isIntegerConstantExpr - this recursive routine will test if an expression is
2274/// an integer constant expression.
2275
2276/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2277/// comma, etc
2278///
2279/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2280/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2281/// cast+dereference.
2282
2283// CheckICE - This function does the fundamental ICE checking: the returned
2284// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2285// Note that to reduce code duplication, this helper does no evaluation
2286// itself; the caller checks whether the expression is evaluatable, and
2287// in the rare cases where CheckICE actually cares about the evaluated
2288// value, it calls into Evalute.
2289//
2290// Meanings of Val:
2291// 0: This expression is an ICE if it can be evaluated by Evaluate.
2292// 1: This expression is not an ICE, but if it isn't evaluated, it's
2293// a legal subexpression for an ICE. This return value is used to handle
2294// the comma operator in C99 mode.
2295// 2: This expression is not an ICE, and is not a legal subexpression for one.
2296
2297struct ICEDiag {
2298 unsigned Val;
2299 SourceLocation Loc;
2300
2301 public:
2302 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2303 ICEDiag() : Val(0) {}
2304};
2305
2306ICEDiag NoDiag() { return ICEDiag(); }
2307
2308static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2309 Expr::EvalResult EVResult;
2310 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2311 !EVResult.Val.isInt()) {
2312 return ICEDiag(2, E->getLocStart());
2313 }
2314 return NoDiag();
2315}
2316
2317static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2318 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
2319 if (!E->getType()->isIntegralType()) {
2320 return ICEDiag(2, E->getLocStart());
2321 }
2322
2323 switch (E->getStmtClass()) {
2324#define STMT(Node, Base) case Expr::Node##Class:
2325#define EXPR(Node, Base)
2326#include "clang/AST/StmtNodes.inc"
2327 case Expr::PredefinedExprClass:
2328 case Expr::FloatingLiteralClass:
2329 case Expr::ImaginaryLiteralClass:
2330 case Expr::StringLiteralClass:
2331 case Expr::ArraySubscriptExprClass:
2332 case Expr::MemberExprClass:
2333 case Expr::CompoundAssignOperatorClass:
2334 case Expr::CompoundLiteralExprClass:
2335 case Expr::ExtVectorElementExprClass:
2336 case Expr::InitListExprClass:
2337 case Expr::DesignatedInitExprClass:
2338 case Expr::ImplicitValueInitExprClass:
2339 case Expr::ParenListExprClass:
2340 case Expr::VAArgExprClass:
2341 case Expr::AddrLabelExprClass:
2342 case Expr::StmtExprClass:
2343 case Expr::CXXMemberCallExprClass:
2344 case Expr::CXXDynamicCastExprClass:
2345 case Expr::CXXTypeidExprClass:
2346 case Expr::CXXNullPtrLiteralExprClass:
2347 case Expr::CXXThisExprClass:
2348 case Expr::CXXThrowExprClass:
2349 case Expr::CXXNewExprClass:
2350 case Expr::CXXDeleteExprClass:
2351 case Expr::CXXPseudoDestructorExprClass:
2352 case Expr::UnresolvedLookupExprClass:
2353 case Expr::DependentScopeDeclRefExprClass:
2354 case Expr::CXXConstructExprClass:
2355 case Expr::CXXBindTemporaryExprClass:
2356 case Expr::CXXBindReferenceExprClass:
2357 case Expr::CXXExprWithTemporariesClass:
2358 case Expr::CXXTemporaryObjectExprClass:
2359 case Expr::CXXUnresolvedConstructExprClass:
2360 case Expr::CXXDependentScopeMemberExprClass:
2361 case Expr::UnresolvedMemberExprClass:
2362 case Expr::ObjCStringLiteralClass:
2363 case Expr::ObjCEncodeExprClass:
2364 case Expr::ObjCMessageExprClass:
2365 case Expr::ObjCSelectorExprClass:
2366 case Expr::ObjCProtocolExprClass:
2367 case Expr::ObjCIvarRefExprClass:
2368 case Expr::ObjCPropertyRefExprClass:
2369 case Expr::ObjCImplicitSetterGetterRefExprClass:
2370 case Expr::ObjCSuperExprClass:
2371 case Expr::ObjCIsaExprClass:
2372 case Expr::ShuffleVectorExprClass:
2373 case Expr::BlockExprClass:
2374 case Expr::BlockDeclRefExprClass:
2375 case Expr::NoStmtClass:
2376 return ICEDiag(2, E->getLocStart());
2377
2378 case Expr::GNUNullExprClass:
2379 // GCC considers the GNU __null value to be an integral constant expression.
2380 return NoDiag();
2381
2382 case Expr::ParenExprClass:
2383 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2384 case Expr::IntegerLiteralClass:
2385 case Expr::CharacterLiteralClass:
2386 case Expr::CXXBoolLiteralExprClass:
2387 case Expr::CXXZeroInitValueExprClass:
2388 case Expr::TypesCompatibleExprClass:
2389 case Expr::UnaryTypeTraitExprClass:
2390 return NoDiag();
2391 case Expr::CallExprClass:
2392 case Expr::CXXOperatorCallExprClass: {
2393 const CallExpr *CE = cast<CallExpr>(E);
2394 if (CE->isBuiltinCall(Ctx))
2395 return CheckEvalInICE(E, Ctx);
2396 return ICEDiag(2, E->getLocStart());
2397 }
2398 case Expr::DeclRefExprClass:
2399 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2400 return NoDiag();
2401 if (Ctx.getLangOptions().CPlusPlus &&
2402 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2403 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2404
2405 // Parameter variables are never constants. Without this check,
2406 // getAnyInitializer() can find a default argument, which leads
2407 // to chaos.
2408 if (isa<ParmVarDecl>(D))
2409 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2410
2411 // C++ 7.1.5.1p2
2412 // A variable of non-volatile const-qualified integral or enumeration
2413 // type initialized by an ICE can be used in ICEs.
2414 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2415 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2416 if (Quals.hasVolatile() || !Quals.hasConst())
2417 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2418
2419 // Look for a declaration of this variable that has an initializer.
2420 const VarDecl *ID = 0;
2421 const Expr *Init = Dcl->getAnyInitializer(ID);
2422 if (Init) {
2423 if (ID->isInitKnownICE()) {
2424 // We have already checked whether this subexpression is an
2425 // integral constant expression.
2426 if (ID->isInitICE())
2427 return NoDiag();
2428 else
2429 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2430 }
2431
2432 // It's an ICE whether or not the definition we found is
2433 // out-of-line. See DR 721 and the discussion in Clang PR
2434 // 6206 for details.
2435
2436 if (Dcl->isCheckingICE()) {
2437 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2438 }
2439
2440 Dcl->setCheckingICE();
2441 ICEDiag Result = CheckICE(Init, Ctx);
2442 // Cache the result of the ICE test.
2443 Dcl->setInitKnownICE(Result.Val == 0);
2444 return Result;
2445 }
2446 }
2447 }
2448 return ICEDiag(2, E->getLocStart());
2449 case Expr::UnaryOperatorClass: {
2450 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2451 switch (Exp->getOpcode()) {
2452 case UnaryOperator::PostInc:
2453 case UnaryOperator::PostDec:
2454 case UnaryOperator::PreInc:
2455 case UnaryOperator::PreDec:
2456 case UnaryOperator::AddrOf:
2457 case UnaryOperator::Deref:
2458 return ICEDiag(2, E->getLocStart());
2459 case UnaryOperator::Extension:
2460 case UnaryOperator::LNot:
2461 case UnaryOperator::Plus:
2462 case UnaryOperator::Minus:
2463 case UnaryOperator::Not:
2464 case UnaryOperator::Real:
2465 case UnaryOperator::Imag:
2466 return CheckICE(Exp->getSubExpr(), Ctx);
2467 case UnaryOperator::OffsetOf:
2468 break;
2469 }
2470
2471 // OffsetOf falls through here.
2472 }
2473 case Expr::OffsetOfExprClass: {
2474 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2475 // Evaluate matches the proposed gcc behavior for cases like
2476 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2477 // compliance: we should warn earlier for offsetof expressions with
2478 // array subscripts that aren't ICEs, and if the array subscripts
2479 // are ICEs, the value of the offsetof must be an integer constant.
2480 return CheckEvalInICE(E, Ctx);
2481 }
2482 case Expr::SizeOfAlignOfExprClass: {
2483 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
2484 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
2485 return ICEDiag(2, E->getLocStart());
2486 return NoDiag();
2487 }
2488 case Expr::BinaryOperatorClass: {
2489 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2490 switch (Exp->getOpcode()) {
2491 case BinaryOperator::PtrMemD:
2492 case BinaryOperator::PtrMemI:
2493 case BinaryOperator::Assign:
2494 case BinaryOperator::MulAssign:
2495 case BinaryOperator::DivAssign:
2496 case BinaryOperator::RemAssign:
2497 case BinaryOperator::AddAssign:
2498 case BinaryOperator::SubAssign:
2499 case BinaryOperator::ShlAssign:
2500 case BinaryOperator::ShrAssign:
2501 case BinaryOperator::AndAssign:
2502 case BinaryOperator::XorAssign:
2503 case BinaryOperator::OrAssign:
2504 return ICEDiag(2, E->getLocStart());
2505
2506 case BinaryOperator::Mul:
2507 case BinaryOperator::Div:
2508 case BinaryOperator::Rem:
2509 case BinaryOperator::Add:
2510 case BinaryOperator::Sub:
2511 case BinaryOperator::Shl:
2512 case BinaryOperator::Shr:
2513 case BinaryOperator::LT:
2514 case BinaryOperator::GT:
2515 case BinaryOperator::LE:
2516 case BinaryOperator::GE:
2517 case BinaryOperator::EQ:
2518 case BinaryOperator::NE:
2519 case BinaryOperator::And:
2520 case BinaryOperator::Xor:
2521 case BinaryOperator::Or:
2522 case BinaryOperator::Comma: {
2523 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2524 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2525 if (Exp->getOpcode() == BinaryOperator::Div ||
2526 Exp->getOpcode() == BinaryOperator::Rem) {
2527 // Evaluate gives an error for undefined Div/Rem, so make sure
2528 // we don't evaluate one.
2529 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
2530 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2531 if (REval == 0)
2532 return ICEDiag(1, E->getLocStart());
2533 if (REval.isSigned() && REval.isAllOnesValue()) {
2534 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2535 if (LEval.isMinSignedValue())
2536 return ICEDiag(1, E->getLocStart());
2537 }
2538 }
2539 }
2540 if (Exp->getOpcode() == BinaryOperator::Comma) {
2541 if (Ctx.getLangOptions().C99) {
2542 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2543 // if it isn't evaluated.
2544 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2545 return ICEDiag(1, E->getLocStart());
2546 } else {
2547 // In both C89 and C++, commas in ICEs are illegal.
2548 return ICEDiag(2, E->getLocStart());
2549 }
2550 }
2551 if (LHSResult.Val >= RHSResult.Val)
2552 return LHSResult;
2553 return RHSResult;
2554 }
2555 case BinaryOperator::LAnd:
2556 case BinaryOperator::LOr: {
2557 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2558 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2559 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2560 // Rare case where the RHS has a comma "side-effect"; we need
2561 // to actually check the condition to see whether the side
2562 // with the comma is evaluated.
2563 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
2564 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2565 return RHSResult;
2566 return NoDiag();
2567 }
2568
2569 if (LHSResult.Val >= RHSResult.Val)
2570 return LHSResult;
2571 return RHSResult;
2572 }
2573 }
2574 }
2575 case Expr::ImplicitCastExprClass:
2576 case Expr::CStyleCastExprClass:
2577 case Expr::CXXFunctionalCastExprClass:
2578 case Expr::CXXStaticCastExprClass:
2579 case Expr::CXXReinterpretCastExprClass:
2580 case Expr::CXXConstCastExprClass: {
2581 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2582 if (SubExpr->getType()->isIntegralType())
2583 return CheckICE(SubExpr, Ctx);
2584 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2585 return NoDiag();
2586 return ICEDiag(2, E->getLocStart());
2587 }
2588 case Expr::ConditionalOperatorClass: {
2589 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
2590 // If the condition (ignoring parens) is a __builtin_constant_p call,
2591 // then only the true side is actually considered in an integer constant
2592 // expression, and it is fully evaluated. This is an important GNU
2593 // extension. See GCC PR38377 for discussion.
2594 if (const CallExpr *CallCE
2595 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
2596 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
2597 Expr::EvalResult EVResult;
2598 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2599 !EVResult.Val.isInt()) {
2600 return ICEDiag(2, E->getLocStart());
2601 }
2602 return NoDiag();
2603 }
2604 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2605 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2606 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2607 if (CondResult.Val == 2)
2608 return CondResult;
2609 if (TrueResult.Val == 2)
2610 return TrueResult;
2611 if (FalseResult.Val == 2)
2612 return FalseResult;
2613 if (CondResult.Val == 1)
2614 return CondResult;
2615 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2616 return NoDiag();
2617 // Rare case where the diagnostics depend on which side is evaluated
2618 // Note that if we get here, CondResult is 0, and at least one of
2619 // TrueResult and FalseResult is non-zero.
2620 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
2621 return FalseResult;
2622 }
2623 return TrueResult;
2624 }
2625 case Expr::CXXDefaultArgExprClass:
2626 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
2627 case Expr::ChooseExprClass: {
2628 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
2629 }
2630 }
2631
2632 // Silence a GCC warning
2633 return ICEDiag(2, E->getLocStart());
2634}
2635
2636bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2637 SourceLocation *Loc, bool isEvaluated) const {
2638 ICEDiag d = CheckICE(this, Ctx);
2639 if (d.Val != 0) {
2640 if (Loc) *Loc = d.Loc;
2641 return false;
2642 }
2643 EvalResult EvalResult;
2644 if (!Evaluate(EvalResult, Ctx))
2645 llvm_unreachable("ICE cannot be evaluated!");
2646 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2647 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
2648 Result = EvalResult.Val.getInt();
2649 return true;
2650}