blob: 345c7fa8c077ad868ad76db455f33ba7b74c3c5f [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
John McCallf4cf1a12010-05-07 17:22:02 +000045namespace {
Benjamin Kramerc54061a2011-03-04 13:12:48 +000046 struct EvalInfo {
47 const ASTContext &Ctx;
48
49 /// EvalResult - Contains information about the evaluation.
50 Expr::EvalResult &EvalResult;
51
52 typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy;
53 MapTy OpaqueValues;
54 const APValue *getOpaqueValue(const OpaqueValueExpr *e) const {
55 MapTy::const_iterator i = OpaqueValues.find(e);
56 if (i == OpaqueValues.end()) return 0;
57 return &i->second;
58 }
59
60 EvalInfo(const ASTContext &ctx, Expr::EvalResult &evalresult)
61 : Ctx(ctx), EvalResult(evalresult) {}
62 };
63
John McCallf4cf1a12010-05-07 17:22:02 +000064 struct ComplexValue {
65 private:
66 bool IsInt;
67
68 public:
69 APSInt IntReal, IntImag;
70 APFloat FloatReal, FloatImag;
71
72 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
73
74 void makeComplexFloat() { IsInt = false; }
75 bool isComplexFloat() const { return !IsInt; }
76 APFloat &getComplexFloatReal() { return FloatReal; }
77 APFloat &getComplexFloatImag() { return FloatImag; }
78
79 void makeComplexInt() { IsInt = true; }
80 bool isComplexInt() const { return IsInt; }
81 APSInt &getComplexIntReal() { return IntReal; }
82 APSInt &getComplexIntImag() { return IntImag; }
83
John McCall56ca35d2011-02-17 10:25:35 +000084 void moveInto(APValue &v) const {
John McCallf4cf1a12010-05-07 17:22:02 +000085 if (isComplexFloat())
86 v = APValue(FloatReal, FloatImag);
87 else
88 v = APValue(IntReal, IntImag);
89 }
John McCall56ca35d2011-02-17 10:25:35 +000090 void setFrom(const APValue &v) {
91 assert(v.isComplexFloat() || v.isComplexInt());
92 if (v.isComplexFloat()) {
93 makeComplexFloat();
94 FloatReal = v.getComplexFloatReal();
95 FloatImag = v.getComplexFloatImag();
96 } else {
97 makeComplexInt();
98 IntReal = v.getComplexIntReal();
99 IntImag = v.getComplexIntImag();
100 }
101 }
John McCallf4cf1a12010-05-07 17:22:02 +0000102 };
John McCallefdb83e2010-05-07 21:00:08 +0000103
104 struct LValue {
105 Expr *Base;
106 CharUnits Offset;
107
108 Expr *getLValueBase() { return Base; }
109 CharUnits getLValueOffset() { return Offset; }
110
John McCall56ca35d2011-02-17 10:25:35 +0000111 void moveInto(APValue &v) const {
John McCallefdb83e2010-05-07 21:00:08 +0000112 v = APValue(Base, Offset);
113 }
John McCall56ca35d2011-02-17 10:25:35 +0000114 void setFrom(const APValue &v) {
115 assert(v.isLValue());
116 Base = v.getLValueBase();
117 Offset = v.getLValueOffset();
118 }
John McCallefdb83e2010-05-07 21:00:08 +0000119 };
John McCallf4cf1a12010-05-07 17:22:02 +0000120}
Chris Lattner87eae5e2008-07-11 22:52:41 +0000121
John McCall56ca35d2011-02-17 10:25:35 +0000122static bool Evaluate(EvalInfo &info, const Expr *E);
John McCallefdb83e2010-05-07 21:00:08 +0000123static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
124static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000125static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +0000126static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
127 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000128static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCallf4cf1a12010-05-07 17:22:02 +0000129static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000130
131//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000132// Misc utilities
133//===----------------------------------------------------------------------===//
134
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000135static bool IsGlobalLValue(const Expr* E) {
John McCall42c8f872010-05-10 23:27:23 +0000136 if (!E) return true;
137
138 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
139 if (isa<FunctionDecl>(DRE->getDecl()))
140 return true;
141 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
142 return VD->hasGlobalStorage();
143 return false;
144 }
145
146 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
147 return CLE->isFileScope();
148
149 return true;
150}
151
John McCallefdb83e2010-05-07 21:00:08 +0000152static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
153 const Expr* Base = Value.Base;
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000154
John McCall35542832010-05-07 21:34:32 +0000155 // A null base expression indicates a null pointer. These are always
156 // evaluatable, and they are false unless the offset is zero.
157 if (!Base) {
158 Result = !Value.Offset.isZero();
159 return true;
160 }
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000161
John McCall42c8f872010-05-10 23:27:23 +0000162 // Require the base expression to be a global l-value.
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000163 if (!IsGlobalLValue(Base)) return false;
John McCall42c8f872010-05-10 23:27:23 +0000164
John McCall35542832010-05-07 21:34:32 +0000165 // We have a non-null base expression. These are generally known to
166 // be true, but if it'a decl-ref to a weak symbol it can be null at
167 // runtime.
John McCall35542832010-05-07 21:34:32 +0000168 Result = true;
169
170 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000171 if (!DeclRef)
172 return true;
173
John McCall35542832010-05-07 21:34:32 +0000174 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000175 const ValueDecl* Decl = DeclRef->getDecl();
176 if (Decl->hasAttr<WeakAttr>() ||
177 Decl->hasAttr<WeakRefAttr>() ||
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000178 Decl->isWeakImported())
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000179 return false;
180
Eli Friedman5bc86102009-06-14 02:17:33 +0000181 return true;
182}
183
John McCallcd7a4452010-01-05 23:42:56 +0000184static bool HandleConversionToBool(const Expr* E, bool& Result,
185 EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000186 if (E->getType()->isIntegralOrEnumerationType()) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000187 APSInt IntResult;
188 if (!EvaluateInteger(E, IntResult, Info))
189 return false;
190 Result = IntResult != 0;
191 return true;
192 } else if (E->getType()->isRealFloatingType()) {
193 APFloat FloatResult(0.0);
194 if (!EvaluateFloat(E, FloatResult, Info))
195 return false;
196 Result = !FloatResult.isZero();
197 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000198 } else if (E->getType()->hasPointerRepresentation()) {
John McCallefdb83e2010-05-07 21:00:08 +0000199 LValue PointerResult;
Eli Friedman4efaa272008-11-12 09:44:48 +0000200 if (!EvaluatePointer(E, PointerResult, Info))
201 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +0000202 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +0000203 } else if (E->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +0000204 ComplexValue ComplexResult;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000205 if (!EvaluateComplex(E, ComplexResult, Info))
206 return false;
207 if (ComplexResult.isComplexFloat()) {
208 Result = !ComplexResult.getComplexFloatReal().isZero() ||
209 !ComplexResult.getComplexFloatImag().isZero();
210 } else {
211 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
212 ComplexResult.getComplexIntImag().getBoolValue();
213 }
214 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 }
216
217 return false;
218}
219
Mike Stump1eb44332009-09-09 15:08:12 +0000220static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000221 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000222 unsigned DestWidth = Ctx.getIntWidth(DestType);
223 // Determine whether we are converting to unsigned or signed.
224 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000226 // FIXME: Warning for overflow.
227 uint64_t Space[4];
228 bool ignored;
229 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
230 llvm::APFloat::rmTowardZero, &ignored);
231 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
232}
233
Mike Stump1eb44332009-09-09 15:08:12 +0000234static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000235 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000236 bool ignored;
237 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000239 APFloat::rmNearestTiesToEven, &ignored);
240 return Result;
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000244 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000245 unsigned DestWidth = Ctx.getIntWidth(DestType);
246 APSInt Result = Value;
247 // Figure out if this is a truncate, extend or noop cast.
248 // If the input is signed, do a sign extend, noop, or truncate.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000249 Result = Result.extOrTrunc(DestWidth);
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000250 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
251 return Result;
252}
253
Mike Stump1eb44332009-09-09 15:08:12 +0000254static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000255 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000256
257 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
258 Result.convertFromAPInt(Value, Value.isSigned(),
259 APFloat::rmNearestTiesToEven);
260 return Result;
261}
262
Mike Stumpc4c90452009-10-27 22:09:17 +0000263namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000264class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000265 : public StmtVisitor<HasSideEffect, bool> {
266 EvalInfo &Info;
267public:
268
269 HasSideEffect(EvalInfo &info) : Info(info) {}
270
271 // Unhandled nodes conservatively default to having side effects.
272 bool VisitStmt(Stmt *S) {
273 return true;
274 }
275
276 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
277 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000278 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000279 return true;
280 return false;
281 }
282 // We don't want to evaluate BlockExprs multiple times, as they generate
283 // a ton of code.
284 bool VisitBlockExpr(BlockExpr *E) { return true; }
285 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
286 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
287 { return Visit(E->getInitializer()); }
288 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
289 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
290 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
291 bool VisitStringLiteral(StringLiteral *E) { return false; }
292 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000293 bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E)
294 { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000295 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000296 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000297 bool VisitChooseExpr(ChooseExpr *E)
298 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
299 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
300 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000301 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000302 bool VisitBinaryOperator(BinaryOperator *E)
303 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000304 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
305 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
306 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
307 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
308 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000309 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000310 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000311 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000312 }
313 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000314
315 // Has side effects if any element does.
316 bool VisitInitListExpr(InitListExpr *E) {
317 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
318 if (Visit(E->getInit(i))) return true;
319 return false;
320 }
Douglas Gregoree8aff02011-01-04 17:33:58 +0000321
322 bool VisitSizeOfPackExpr(SizeOfPackExpr *) { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000323};
324
John McCall56ca35d2011-02-17 10:25:35 +0000325class OpaqueValueEvaluation {
326 EvalInfo &info;
327 OpaqueValueExpr *opaqueValue;
328
329public:
330 OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
331 Expr *value)
332 : info(info), opaqueValue(opaqueValue) {
333
334 // If evaluation fails, fail immediately.
335 if (!Evaluate(info, value)) {
336 this->opaqueValue = 0;
337 return;
338 }
339 info.OpaqueValues[opaqueValue] = info.EvalResult.Val;
340 }
341
342 bool hasError() const { return opaqueValue == 0; }
343
344 ~OpaqueValueEvaluation() {
345 if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
346 }
347};
348
Mike Stumpc4c90452009-10-27 22:09:17 +0000349} // end anonymous namespace
350
Eli Friedman4efaa272008-11-12 09:44:48 +0000351//===----------------------------------------------------------------------===//
352// LValue Evaluation
353//===----------------------------------------------------------------------===//
354namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000355class LValueExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000356 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman4efaa272008-11-12 09:44:48 +0000357 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000358 LValue &Result;
359
360 bool Success(Expr *E) {
361 Result.Base = E;
362 Result.Offset = CharUnits::Zero();
363 return true;
364 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000365public:
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCallefdb83e2010-05-07 21:00:08 +0000367 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
368 Info(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000369
John McCallefdb83e2010-05-07 21:00:08 +0000370 bool VisitStmt(Stmt *S) {
371 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000372 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000373
John McCallefdb83e2010-05-07 21:00:08 +0000374 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
375 bool VisitDeclRefExpr(DeclRefExpr *E);
376 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
377 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
378 bool VisitMemberExpr(MemberExpr *E);
379 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
380 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
381 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
382 bool VisitUnaryDeref(UnaryOperator *E);
383 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000384 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000385 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000386 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000387
John McCallefdb83e2010-05-07 21:00:08 +0000388 bool VisitCastExpr(CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000389 switch (E->getCastKind()) {
390 default:
John McCallefdb83e2010-05-07 21:00:08 +0000391 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000392
John McCall2de56d12010-08-25 11:45:40 +0000393 case CK_NoOp:
Anders Carlsson26bc2202009-10-03 16:30:22 +0000394 return Visit(E->getSubExpr());
395 }
396 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000397 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000398};
399} // end anonymous namespace
400
John McCallefdb83e2010-05-07 21:00:08 +0000401static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
402 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman4efaa272008-11-12 09:44:48 +0000403}
404
John McCallefdb83e2010-05-07 21:00:08 +0000405bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000406 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000407 return Success(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000408 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
409 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000410 return Success(E);
Chandler Carruth761c94e2010-05-16 09:32:51 +0000411 // Reference parameters can refer to anything even if they have an
412 // "initializer" in the form of a default argument.
413 if (isa<ParmVarDecl>(VD))
414 return false;
Eli Friedmand933a012009-08-29 19:09:59 +0000415 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000416 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000417 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000418 }
419
John McCallefdb83e2010-05-07 21:00:08 +0000420 return false;
Anders Carlsson35873c42008-11-24 04:41:22 +0000421}
422
John McCallefdb83e2010-05-07 21:00:08 +0000423bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000424 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000425}
426
John McCallefdb83e2010-05-07 21:00:08 +0000427bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000428 QualType Ty;
429 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000430 if (!EvaluatePointer(E->getBase(), Result, Info))
431 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000432 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000433 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000434 if (!Visit(E->getBase()))
435 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000436 Ty = E->getBase()->getType();
437 }
438
Ted Kremenek6217b802009-07-29 21:53:49 +0000439 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000440 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000441
442 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
443 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000444 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000445
446 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000447 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000448
Eli Friedman4efaa272008-11-12 09:44:48 +0000449 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000450 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000451 for (RecordDecl::field_iterator Field = RD->field_begin(),
452 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000453 Field != FieldEnd; (void)++Field, ++i) {
454 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000455 break;
456 }
457
Ken Dyckfb1e3bc2011-01-18 01:56:16 +0000458 Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
John McCallefdb83e2010-05-07 21:00:08 +0000459 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000460}
461
John McCallefdb83e2010-05-07 21:00:08 +0000462bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000463 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000464 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Anders Carlsson3068d112008-11-16 19:01:22 +0000466 APSInt Index;
467 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000468 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000469
Ken Dyck199c3d62010-01-11 17:06:35 +0000470 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000471 Result.Offset += Index.getSExtValue() * ElementSize;
472 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000473}
Eli Friedman4efaa272008-11-12 09:44:48 +0000474
John McCallefdb83e2010-05-07 21:00:08 +0000475bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
476 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000477}
478
Eli Friedman4efaa272008-11-12 09:44:48 +0000479//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000480// Pointer Evaluation
481//===----------------------------------------------------------------------===//
482
Anders Carlssonc754aa62008-07-08 05:13:58 +0000483namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000484class PointerExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000485 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000486 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000487 LValue &Result;
488
489 bool Success(Expr *E) {
490 Result.Base = E;
491 Result.Offset = CharUnits::Zero();
492 return true;
493 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000494public:
Mike Stump1eb44332009-09-09 15:08:12 +0000495
John McCallefdb83e2010-05-07 21:00:08 +0000496 PointerExprEvaluator(EvalInfo &info, LValue &Result)
497 : Info(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000498
John McCallefdb83e2010-05-07 21:00:08 +0000499 bool VisitStmt(Stmt *S) {
500 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000501 }
502
John McCallefdb83e2010-05-07 21:00:08 +0000503 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000504
John McCallefdb83e2010-05-07 21:00:08 +0000505 bool VisitBinaryOperator(const BinaryOperator *E);
506 bool VisitCastExpr(CastExpr* E);
507 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman2217c872009-02-22 11:46:18 +0000508 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000509 bool VisitUnaryAddrOf(const UnaryOperator *E);
510 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
511 { return Success(E); }
512 bool VisitAddrLabelExpr(AddrLabelExpr *E)
513 { return Success(E); }
514 bool VisitCallExpr(CallExpr *E);
515 bool VisitBlockExpr(BlockExpr *E) {
John McCall469a1eb2011-02-02 13:00:07 +0000516 if (!E->getBlockDecl()->hasCaptures())
John McCallefdb83e2010-05-07 21:00:08 +0000517 return Success(E);
518 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000519 }
John McCallefdb83e2010-05-07 21:00:08 +0000520 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
521 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000522 bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
John McCallefdb83e2010-05-07 21:00:08 +0000523 bool VisitConditionalOperator(ConditionalOperator *E);
524 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000525 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallefdb83e2010-05-07 21:00:08 +0000526 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
527 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000528
529 bool VisitOpaqueValueExpr(OpaqueValueExpr *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000530 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000531};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000532} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000533
John McCallefdb83e2010-05-07 21:00:08 +0000534static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000535 assert(E->getType()->hasPointerRepresentation());
John McCallefdb83e2010-05-07 21:00:08 +0000536 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000537}
538
John McCallefdb83e2010-05-07 21:00:08 +0000539bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000540 if (E->getOpcode() != BO_Add &&
541 E->getOpcode() != BO_Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000542 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000544 const Expr *PExp = E->getLHS();
545 const Expr *IExp = E->getRHS();
546 if (IExp->getType()->isPointerType())
547 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
John McCallefdb83e2010-05-07 21:00:08 +0000549 if (!EvaluatePointer(PExp, Result, Info))
550 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
John McCallefdb83e2010-05-07 21:00:08 +0000552 llvm::APSInt Offset;
553 if (!EvaluateInteger(IExp, Offset, Info))
554 return false;
555 int64_t AdditionalOffset
556 = Offset.isSigned() ? Offset.getSExtValue()
557 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000558
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000559 // Compute the new offset in the appropriate width.
560
561 QualType PointeeType =
562 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000563 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000565 // Explicitly handle GNU void* and function pointer arithmetic extensions.
566 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000567 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000568 else
John McCallefdb83e2010-05-07 21:00:08 +0000569 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000570
John McCall2de56d12010-08-25 11:45:40 +0000571 if (E->getOpcode() == BO_Add)
John McCallefdb83e2010-05-07 21:00:08 +0000572 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000573 else
John McCallefdb83e2010-05-07 21:00:08 +0000574 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000575
John McCallefdb83e2010-05-07 21:00:08 +0000576 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000577}
Eli Friedman4efaa272008-11-12 09:44:48 +0000578
John McCallefdb83e2010-05-07 21:00:08 +0000579bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
580 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000581}
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000583
John McCallefdb83e2010-05-07 21:00:08 +0000584bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000585 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000586
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000587 switch (E->getCastKind()) {
588 default:
589 break;
590
John McCall2de56d12010-08-25 11:45:40 +0000591 case CK_NoOp:
592 case CK_BitCast:
593 case CK_LValueBitCast:
594 case CK_AnyPointerToObjCPointerCast:
595 case CK_AnyPointerToBlockPointerCast:
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000596 return Visit(SubExpr);
597
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000598 case CK_DerivedToBase:
599 case CK_UncheckedDerivedToBase: {
600 LValue BaseLV;
601 if (!EvaluatePointer(E->getSubExpr(), BaseLV, Info))
602 return false;
603
604 // Now figure out the necessary offset to add to the baseLV to get from
605 // the derived class to the base class.
Ken Dyck7c7f8202011-01-26 02:17:08 +0000606 CharUnits Offset = CharUnits::Zero();
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000607
608 QualType Ty = E->getSubExpr()->getType();
609 const CXXRecordDecl *DerivedDecl =
610 Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl();
611
612 for (CastExpr::path_const_iterator PathI = E->path_begin(),
613 PathE = E->path_end(); PathI != PathE; ++PathI) {
614 const CXXBaseSpecifier *Base = *PathI;
615
616 // FIXME: If the base is virtual, we'd need to determine the type of the
617 // most derived class and we don't support that right now.
618 if (Base->isVirtual())
619 return false;
620
621 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
622 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
623
Ken Dyck7c7f8202011-01-26 02:17:08 +0000624 Offset += Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000625 DerivedDecl = BaseDecl;
626 }
627
628 Result.Base = BaseLV.getLValueBase();
Ken Dyck7c7f8202011-01-26 02:17:08 +0000629 Result.Offset = BaseLV.getLValueOffset() + Offset;
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000630 return true;
631 }
632
John McCall404cd162010-11-13 01:35:44 +0000633 case CK_NullToPointer: {
634 Result.Base = 0;
635 Result.Offset = CharUnits::Zero();
636 return true;
637 }
638
John McCall2de56d12010-08-25 11:45:40 +0000639 case CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000640 APValue Value;
641 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000642 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000643
John McCallefdb83e2010-05-07 21:00:08 +0000644 if (Value.isInt()) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000645 Value.getInt() = Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
John McCallefdb83e2010-05-07 21:00:08 +0000646 Result.Base = 0;
647 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
648 return true;
649 } else {
650 // Cast is of an lvalue, no need to change value.
651 Result.Base = Value.getLValueBase();
652 Result.Offset = Value.getLValueOffset();
653 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000654 }
655 }
John McCall2de56d12010-08-25 11:45:40 +0000656 case CK_ArrayToPointerDecay:
657 case CK_FunctionToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000658 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000659 }
660
John McCallefdb83e2010-05-07 21:00:08 +0000661 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000662}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000663
John McCallefdb83e2010-05-07 21:00:08 +0000664bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000665 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000666 Builtin::BI__builtin___CFStringMakeConstantString ||
667 E->isBuiltinCall(Info.Ctx) ==
668 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000669 return Success(E);
670 return false;
Eli Friedman3941b182009-01-25 01:54:01 +0000671}
672
John McCall56ca35d2011-02-17 10:25:35 +0000673bool PointerExprEvaluator::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
674 const APValue *value = Info.getOpaqueValue(e);
675 if (!value)
676 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
677 Result.setFrom(*value);
678 return true;
679}
680
681bool PointerExprEvaluator::
682VisitBinaryConditionalOperator(BinaryConditionalOperator *e) {
683 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
684 if (opaque.hasError()) return false;
685
686 bool cond;
687 if (!HandleConversionToBool(e->getCond(), cond, Info))
688 return false;
689
690 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
691}
692
John McCallefdb83e2010-05-07 21:00:08 +0000693bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000694 bool BoolResult;
695 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000696 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000697
698 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCallefdb83e2010-05-07 21:00:08 +0000699 return Visit(EvalExpr);
Eli Friedman4efaa272008-11-12 09:44:48 +0000700}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000701
702//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000703// Vector Evaluation
704//===----------------------------------------------------------------------===//
705
706namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000707 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000708 : public StmtVisitor<VectorExprEvaluator, APValue> {
709 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000710 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000711 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Nate Begeman59b5da62009-01-18 03:20:47 +0000713 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Nate Begeman59b5da62009-01-18 03:20:47 +0000715 APValue VisitStmt(Stmt *S) {
716 return APValue();
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Eli Friedman91110ee2009-02-23 04:23:56 +0000719 APValue VisitParenExpr(ParenExpr *E)
720 { return Visit(E->getSubExpr()); }
721 APValue VisitUnaryExtension(const UnaryOperator *E)
722 { return Visit(E->getSubExpr()); }
723 APValue VisitUnaryPlus(const UnaryOperator *E)
724 { return Visit(E->getSubExpr()); }
725 APValue VisitUnaryReal(const UnaryOperator *E)
726 { return Visit(E->getSubExpr()); }
727 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
728 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000729 APValue VisitCastExpr(const CastExpr* E);
730 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
731 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000732 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000733 APValue VisitChooseExpr(const ChooseExpr *E)
734 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000735 APValue VisitUnaryImag(const UnaryOperator *E);
736 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000737 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000738 // shufflevector, ExtVectorElementExpr
739 // (Note that these require implementing conversions
740 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000741 };
742} // end anonymous namespace
743
744static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
745 if (!E->getType()->isVectorType())
746 return false;
747 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
748 return !Result.isUninit();
749}
750
751APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000752 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000753 QualType EltTy = VTy->getElementType();
754 unsigned NElts = VTy->getNumElements();
755 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Nate Begeman59b5da62009-01-18 03:20:47 +0000757 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000758 QualType SETy = SE->getType();
759 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000760
Nate Begemane8c9e922009-06-26 18:22:18 +0000761 // Check for vector->vector bitcast and scalar->vector splat.
762 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000763 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000764 } else if (SETy->isIntegerType()) {
765 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000766 if (!EvaluateInteger(SE, IntResult, Info))
767 return APValue();
768 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000769 } else if (SETy->isRealFloatingType()) {
770 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000771 if (!EvaluateFloat(SE, F, Info))
772 return APValue();
773 Result = APValue(F);
774 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000775 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000776
Nate Begemanc0b8b192009-07-01 07:50:47 +0000777 // For casts of a scalar to ExtVector, convert the scalar to the element type
778 // and splat it to all elements.
779 if (E->getType()->isExtVectorType()) {
780 if (EltTy->isIntegerType() && Result.isInt())
781 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
782 Info.Ctx));
783 else if (EltTy->isIntegerType())
784 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
785 Info.Ctx));
786 else if (EltTy->isRealFloatingType() && Result.isInt())
787 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
788 Info.Ctx));
789 else if (EltTy->isRealFloatingType())
790 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
791 Info.Ctx));
792 else
793 return APValue();
794
795 // Splat and create vector APValue.
796 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
797 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000798 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000799
800 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
801 // to the vector. To construct the APValue vector initializer, bitcast the
802 // initializing value to an APInt, and shift out the bits pertaining to each
803 // element.
804 APSInt Init;
805 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Nate Begemanc0b8b192009-07-01 07:50:47 +0000807 llvm::SmallVector<APValue, 4> Elts;
808 for (unsigned i = 0; i != NElts; ++i) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000809 APSInt Tmp = Init.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Nate Begemanc0b8b192009-07-01 07:50:47 +0000811 if (EltTy->isIntegerType())
812 Elts.push_back(APValue(Tmp));
813 else if (EltTy->isRealFloatingType())
814 Elts.push_back(APValue(APFloat(Tmp)));
815 else
816 return APValue();
817
818 Init >>= EltWidth;
819 }
820 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000821}
822
Mike Stump1eb44332009-09-09 15:08:12 +0000823APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000824VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
825 return this->Visit(const_cast<Expr*>(E->getInitializer()));
826}
827
Mike Stump1eb44332009-09-09 15:08:12 +0000828APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000829VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000830 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000831 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000832 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Nate Begeman59b5da62009-01-18 03:20:47 +0000834 QualType EltTy = VT->getElementType();
835 llvm::SmallVector<APValue, 4> Elements;
836
John McCalla7d6c222010-06-11 17:54:15 +0000837 // If a vector is initialized with a single element, that value
838 // becomes every element of the vector, not just the first.
839 // This is the behavior described in the IBM AltiVec documentation.
840 if (NumInits == 1) {
841 APValue InitValue;
Nate Begeman59b5da62009-01-18 03:20:47 +0000842 if (EltTy->isIntegerType()) {
843 llvm::APSInt sInt(32);
John McCalla7d6c222010-06-11 17:54:15 +0000844 if (!EvaluateInteger(E->getInit(0), sInt, Info))
845 return APValue();
846 InitValue = APValue(sInt);
Nate Begeman59b5da62009-01-18 03:20:47 +0000847 } else {
848 llvm::APFloat f(0.0);
John McCalla7d6c222010-06-11 17:54:15 +0000849 if (!EvaluateFloat(E->getInit(0), f, Info))
850 return APValue();
851 InitValue = APValue(f);
852 }
853 for (unsigned i = 0; i < NumElements; i++) {
854 Elements.push_back(InitValue);
855 }
856 } else {
857 for (unsigned i = 0; i < NumElements; i++) {
858 if (EltTy->isIntegerType()) {
859 llvm::APSInt sInt(32);
860 if (i < NumInits) {
861 if (!EvaluateInteger(E->getInit(i), sInt, Info))
862 return APValue();
863 } else {
864 sInt = Info.Ctx.MakeIntValue(0, EltTy);
865 }
866 Elements.push_back(APValue(sInt));
Eli Friedman91110ee2009-02-23 04:23:56 +0000867 } else {
John McCalla7d6c222010-06-11 17:54:15 +0000868 llvm::APFloat f(0.0);
869 if (i < NumInits) {
870 if (!EvaluateFloat(E->getInit(i), f, Info))
871 return APValue();
872 } else {
873 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
874 }
875 Elements.push_back(APValue(f));
Eli Friedman91110ee2009-02-23 04:23:56 +0000876 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000877 }
878 }
879 return APValue(&Elements[0], Elements.size());
880}
881
Mike Stump1eb44332009-09-09 15:08:12 +0000882APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000883VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000884 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000885 QualType EltTy = VT->getElementType();
886 APValue ZeroElement;
887 if (EltTy->isIntegerType())
888 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
889 else
890 ZeroElement =
891 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
892
893 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
894 return APValue(&Elements[0], Elements.size());
895}
896
897APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
898 bool BoolResult;
899 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
900 return APValue();
901
902 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
903
904 APValue Result;
905 if (EvaluateVector(EvalExpr, Result, Info))
906 return Result;
907 return APValue();
908}
909
Eli Friedman91110ee2009-02-23 04:23:56 +0000910APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
911 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
912 Info.EvalResult.HasSideEffects = true;
913 return GetZeroVector(E->getType());
914}
915
Nate Begeman59b5da62009-01-18 03:20:47 +0000916//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000917// Integer Evaluation
918//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000919
920namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000921class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000922 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000923 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000924 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000925public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000926 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000927 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000928
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000929 bool Success(const llvm::APSInt &SI, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000930 assert(E->getType()->isIntegralOrEnumerationType() &&
931 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000932 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000933 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000934 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000935 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000936 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000937 return true;
938 }
939
Daniel Dunbar131eb432009-02-19 09:06:44 +0000940 bool Success(const llvm::APInt &I, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000941 assert(E->getType()->isIntegralOrEnumerationType() &&
942 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000943 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000944 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000945 Result = APValue(APSInt(I));
946 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000947 return true;
948 }
949
950 bool Success(uint64_t Value, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000951 assert(E->getType()->isIntegralOrEnumerationType() &&
952 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000953 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000954 return true;
955 }
956
Ken Dyck4f3bc8f2011-03-11 02:13:43 +0000957 bool Success(CharUnits Size, const Expr *E) {
958 return Success(Size.getQuantity(), E);
959 }
960
961
Anders Carlsson82206e22008-11-30 18:14:57 +0000962 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000963 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000964 if (Info.EvalResult.Diag == 0) {
965 Info.EvalResult.DiagLoc = L;
966 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000967 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000968 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000969 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Anders Carlssonc754aa62008-07-08 05:13:58 +0000972 //===--------------------------------------------------------------------===//
973 // Visitor Methods
974 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner32fea9d2008-11-12 07:43:42 +0000976 bool VisitStmt(Stmt *) {
977 assert(0 && "This should be called on integers, stmts are not integers");
978 return false;
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Chris Lattner32fea9d2008-11-12 07:43:42 +0000981 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000982 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnerb542afe2008-07-11 19:10:17 +0000985 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000986
Chris Lattner4c4867e2008-07-12 00:38:25 +0000987 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000988 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000989 }
990 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000991 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000992 }
Eli Friedman04309752009-11-24 05:28:59 +0000993
John McCall56ca35d2011-02-17 10:25:35 +0000994 bool VisitOpaqueValueExpr(OpaqueValueExpr *e) {
995 const APValue *value = Info.getOpaqueValue(e);
996 if (!value) {
997 if (e->getSourceExpr()) return Visit(e->getSourceExpr());
998 return Error(e->getExprLoc(), diag::note_invalid_subexpr_in_ice, e);
999 }
1000 return Success(value->getInt(), e);
1001 }
1002
Eli Friedman04309752009-11-24 05:28:59 +00001003 bool CheckReferencedDecl(const Expr *E, const Decl *D);
1004 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1005 return CheckReferencedDecl(E, E->getDecl());
1006 }
1007 bool VisitMemberExpr(const MemberExpr *E) {
1008 if (CheckReferencedDecl(E, E->getMemberDecl())) {
1009 // Conservatively assume a MemberExpr will have side-effects
1010 Info.EvalResult.HasSideEffects = true;
1011 return true;
1012 }
1013 return false;
1014 }
1015
Eli Friedmanc4a26382010-02-13 00:10:10 +00001016 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001017 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001018 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001019 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001020 bool VisitConditionalOperator(const ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00001021 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +00001022
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001023 bool VisitCastExpr(CastExpr* E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001024 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Sebastian Redl05189992008-11-11 17:56:53 +00001025
Anders Carlsson3068d112008-11-16 19:01:22 +00001026 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001027 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Anders Carlsson3f704562008-12-21 22:39:40 +00001030 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001031 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregored8abf12010-07-08 06:14:04 +00001034 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001035 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001036 }
1037
Eli Friedman664a1042009-02-27 04:45:43 +00001038 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
1039 return Success(0, E);
1040 }
1041
Sebastian Redl64b45f72009-01-05 20:52:13 +00001042 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Sebastian Redl0dfd8482010-09-13 20:56:31 +00001043 return Success(E->getValue(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001044 }
1045
Francois Pichet6ad6f282010-12-07 00:08:36 +00001046 bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
1047 return Success(E->getValue(), E);
1048 }
1049
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001050 bool VisitChooseExpr(const ChooseExpr *E) {
1051 return Visit(E->getChosenSubExpr(Info.Ctx));
1052 }
1053
Eli Friedman722c7172009-02-28 03:59:05 +00001054 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001055 bool VisitUnaryImag(const UnaryOperator *E);
1056
Sebastian Redl295995c2010-09-10 20:55:47 +00001057 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001058 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1059
Chris Lattnerfcee0012008-07-11 21:24:13 +00001060private:
Ken Dyck8b752f12010-01-27 17:10:57 +00001061 CharUnits GetAlignOfExpr(const Expr *E);
1062 CharUnits GetAlignOfType(QualType T);
John McCall42c8f872010-05-10 23:27:23 +00001063 static QualType GetObjectType(const Expr *E);
1064 bool TryEvaluateBuiltinObjectSize(CallExpr *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001065 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001066};
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001067} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +00001068
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001069static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001070 assert(E->getType()->isIntegralOrEnumerationType());
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001071 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1072}
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001073
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001074static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001075 assert(E->getType()->isIntegralOrEnumerationType());
John McCall7db7acb2010-05-07 05:46:35 +00001076
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001077 APValue Val;
1078 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
1079 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001080 Result = Val.getInt();
1081 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +00001082}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001083
Eli Friedman04309752009-11-24 05:28:59 +00001084bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001085 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +00001086 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
1087 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001088
1089 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +00001090 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001091 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
1092 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +00001093
1094 if (isa<ParmVarDecl>(D))
1095 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1096
Eli Friedman04309752009-11-24 05:28:59 +00001097 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001098 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001099 if (APValue *V = VD->getEvaluatedValue()) {
1100 if (V->isInt())
1101 return Success(V->getInt(), E);
1102 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1103 }
1104
1105 if (VD->isEvaluatingValue())
1106 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1107
1108 VD->setEvaluatingValue();
1109
Eli Friedmana7dedf72010-09-06 00:10:32 +00001110 Expr::EvalResult EResult;
1111 if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
1112 EResult.Val.isInt()) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001113 // Cache the evaluated value in the variable declaration.
Eli Friedmana7dedf72010-09-06 00:10:32 +00001114 Result = EResult.Val;
Eli Friedmanc0131182009-12-03 20:31:57 +00001115 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +00001116 return true;
1117 }
1118
Eli Friedmanc0131182009-12-03 20:31:57 +00001119 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +00001120 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001121 }
1122 }
1123
Chris Lattner4c4867e2008-07-12 00:38:25 +00001124 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001125 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001126}
1127
Chris Lattnera4d55d82008-10-06 06:40:35 +00001128/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1129/// as GCC.
1130static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1131 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001132 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +00001133 enum gcc_type_class {
1134 no_type_class = -1,
1135 void_type_class, integer_type_class, char_type_class,
1136 enumeral_type_class, boolean_type_class,
1137 pointer_type_class, reference_type_class, offset_type_class,
1138 real_type_class, complex_type_class,
1139 function_type_class, method_type_class,
1140 record_type_class, union_type_class,
1141 array_type_class, string_type_class,
1142 lang_type_class
1143 };
Mike Stump1eb44332009-09-09 15:08:12 +00001144
1145 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001146 // ideal, however it is what gcc does.
1147 if (E->getNumArgs() == 0)
1148 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Chris Lattnera4d55d82008-10-06 06:40:35 +00001150 QualType ArgTy = E->getArg(0)->getType();
1151 if (ArgTy->isVoidType())
1152 return void_type_class;
1153 else if (ArgTy->isEnumeralType())
1154 return enumeral_type_class;
1155 else if (ArgTy->isBooleanType())
1156 return boolean_type_class;
1157 else if (ArgTy->isCharType())
1158 return string_type_class; // gcc doesn't appear to use char_type_class
1159 else if (ArgTy->isIntegerType())
1160 return integer_type_class;
1161 else if (ArgTy->isPointerType())
1162 return pointer_type_class;
1163 else if (ArgTy->isReferenceType())
1164 return reference_type_class;
1165 else if (ArgTy->isRealType())
1166 return real_type_class;
1167 else if (ArgTy->isComplexType())
1168 return complex_type_class;
1169 else if (ArgTy->isFunctionType())
1170 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001171 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001172 return record_type_class;
1173 else if (ArgTy->isUnionType())
1174 return union_type_class;
1175 else if (ArgTy->isArrayType())
1176 return array_type_class;
1177 else if (ArgTy->isUnionType())
1178 return union_type_class;
1179 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1180 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1181 return -1;
1182}
1183
John McCall42c8f872010-05-10 23:27:23 +00001184/// Retrieves the "underlying object type" of the given expression,
1185/// as used by __builtin_object_size.
1186QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1187 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1188 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1189 return VD->getType();
1190 } else if (isa<CompoundLiteralExpr>(E)) {
1191 return E->getType();
1192 }
1193
1194 return QualType();
1195}
1196
1197bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
1198 // TODO: Perhaps we should let LLVM lower this?
1199 LValue Base;
1200 if (!EvaluatePointer(E->getArg(0), Base, Info))
1201 return false;
1202
1203 // If we can prove the base is null, lower to zero now.
1204 const Expr *LVBase = Base.getLValueBase();
1205 if (!LVBase) return Success(0, E);
1206
1207 QualType T = GetObjectType(LVBase);
1208 if (T.isNull() ||
1209 T->isIncompleteType() ||
Eli Friedman13578692010-08-05 02:49:48 +00001210 T->isFunctionType() ||
John McCall42c8f872010-05-10 23:27:23 +00001211 T->isVariablyModifiedType() ||
1212 T->isDependentType())
1213 return false;
1214
1215 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1216 CharUnits Offset = Base.getLValueOffset();
1217
1218 if (!Offset.isNegative() && Offset <= Size)
1219 Size -= Offset;
1220 else
1221 Size = CharUnits::Zero();
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001222 return Success(Size, E);
John McCall42c8f872010-05-10 23:27:23 +00001223}
1224
Eli Friedmanc4a26382010-02-13 00:10:10 +00001225bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001226 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001227 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001228 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001229
1230 case Builtin::BI__builtin_object_size: {
John McCall42c8f872010-05-10 23:27:23 +00001231 if (TryEvaluateBuiltinObjectSize(E))
1232 return true;
Mike Stump64eda9e2009-10-26 18:35:08 +00001233
Eric Christopherb2aaf512010-01-19 22:58:35 +00001234 // If evaluating the argument has side-effects we can't determine
1235 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001236 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001237 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001238 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001239 return Success(0, E);
1240 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001241
Mike Stump64eda9e2009-10-26 18:35:08 +00001242 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1243 }
1244
Chris Lattner019f4e82008-10-06 05:28:25 +00001245 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001246 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001248 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001249 // __builtin_constant_p always has one operand: it returns true if that
1250 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001251 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001252
1253 case Builtin::BI__builtin_eh_return_data_regno: {
1254 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1255 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1256 return Success(Operand, E);
1257 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001258
1259 case Builtin::BI__builtin_expect:
1260 return Visit(E->getArg(0));
Douglas Gregor5726d402010-09-10 06:27:15 +00001261
1262 case Builtin::BIstrlen:
1263 case Builtin::BI__builtin_strlen:
1264 // As an extension, we support strlen() and __builtin_strlen() as constant
1265 // expressions when the argument is a string literal.
1266 if (StringLiteral *S
1267 = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
1268 // The string literal may have embedded null characters. Find the first
1269 // one and truncate there.
1270 llvm::StringRef Str = S->getString();
1271 llvm::StringRef::size_type Pos = Str.find(0);
1272 if (Pos != llvm::StringRef::npos)
1273 Str = Str.substr(0, Pos);
1274
1275 return Success(Str.size(), E);
1276 }
1277
1278 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +00001279 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001280}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001281
Chris Lattnerb542afe2008-07-11 19:10:17 +00001282bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001283 if (E->getOpcode() == BO_Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001284 if (!Visit(E->getRHS()))
1285 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001286
Eli Friedman33ef1452009-02-26 10:19:36 +00001287 // If we can't evaluate the LHS, it might have side effects;
1288 // conservatively mark it.
1289 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1290 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001291
Anders Carlsson027f62e2008-12-01 02:07:06 +00001292 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001293 }
1294
1295 if (E->isLogicalOp()) {
1296 // These need to be handled specially because the operands aren't
1297 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001298 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001300 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001301 // We were able to evaluate the LHS, see if we can get away with not
1302 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
John McCall2de56d12010-08-25 11:45:40 +00001303 if (lhsResult == (E->getOpcode() == BO_LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001304 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001305
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001306 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
John McCall2de56d12010-08-25 11:45:40 +00001307 if (E->getOpcode() == BO_LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001308 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001309 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001310 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001311 }
1312 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001313 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001314 // We can't evaluate the LHS; however, sometimes the result
1315 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
John McCall2de56d12010-08-25 11:45:40 +00001316 if (rhsResult == (E->getOpcode() == BO_LOr) ||
1317 !rhsResult == (E->getOpcode() == BO_LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001318 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001319 // must have had side effects.
1320 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001321
1322 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001323 }
1324 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001325 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001326
Eli Friedmana6afa762008-11-13 06:09:17 +00001327 return false;
1328 }
1329
Anders Carlsson286f85e2008-11-16 07:17:21 +00001330 QualType LHSTy = E->getLHS()->getType();
1331 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001332
1333 if (LHSTy->isAnyComplexType()) {
1334 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001335 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001336
1337 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1338 return false;
1339
1340 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1341 return false;
1342
1343 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001344 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001345 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001346 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001347 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1348
John McCall2de56d12010-08-25 11:45:40 +00001349 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001350 return Success((CR_r == APFloat::cmpEqual &&
1351 CR_i == APFloat::cmpEqual), E);
1352 else {
John McCall2de56d12010-08-25 11:45:40 +00001353 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001354 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001355 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001356 CR_r == APFloat::cmpLessThan ||
1357 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001358 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001359 CR_i == APFloat::cmpLessThan ||
1360 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001361 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001362 } else {
John McCall2de56d12010-08-25 11:45:40 +00001363 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001364 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1365 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1366 else {
John McCall2de56d12010-08-25 11:45:40 +00001367 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001368 "Invalid compex comparison.");
1369 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1370 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1371 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001372 }
1373 }
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Anders Carlsson286f85e2008-11-16 07:17:21 +00001375 if (LHSTy->isRealFloatingType() &&
1376 RHSTy->isRealFloatingType()) {
1377 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Anders Carlsson286f85e2008-11-16 07:17:21 +00001379 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1380 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Anders Carlsson286f85e2008-11-16 07:17:21 +00001382 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1383 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Anders Carlsson286f85e2008-11-16 07:17:21 +00001385 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001386
Anders Carlsson286f85e2008-11-16 07:17:21 +00001387 switch (E->getOpcode()) {
1388 default:
1389 assert(0 && "Invalid binary operator!");
John McCall2de56d12010-08-25 11:45:40 +00001390 case BO_LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001391 return Success(CR == APFloat::cmpLessThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001392 case BO_GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001393 return Success(CR == APFloat::cmpGreaterThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001394 case BO_LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001395 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001396 case BO_GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001397 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001398 E);
John McCall2de56d12010-08-25 11:45:40 +00001399 case BO_EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001400 return Success(CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001401 case BO_NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001402 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001403 || CR == APFloat::cmpLessThan
1404 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001405 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001408 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001409 if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001410 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001411 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1412 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001413
John McCallefdb83e2010-05-07 21:00:08 +00001414 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001415 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1416 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001417
Eli Friedman5bc86102009-06-14 02:17:33 +00001418 // Reject any bases from the normal codepath; we special-case comparisons
1419 // to null.
1420 if (LHSValue.getLValueBase()) {
1421 if (!E->isEqualityOp())
1422 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001423 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001424 return false;
1425 bool bres;
1426 if (!EvalPointerValueAsBool(LHSValue, bres))
1427 return false;
John McCall2de56d12010-08-25 11:45:40 +00001428 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001429 } else if (RHSValue.getLValueBase()) {
1430 if (!E->isEqualityOp())
1431 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001432 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001433 return false;
1434 bool bres;
1435 if (!EvalPointerValueAsBool(RHSValue, bres))
1436 return false;
John McCall2de56d12010-08-25 11:45:40 +00001437 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001438 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001439
John McCall2de56d12010-08-25 11:45:40 +00001440 if (E->getOpcode() == BO_Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001441 QualType Type = E->getLHS()->getType();
1442 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001443
Ken Dycka7305832010-01-15 12:37:54 +00001444 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001445 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001446 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001447
Ken Dycka7305832010-01-15 12:37:54 +00001448 CharUnits Diff = LHSValue.getLValueOffset() -
1449 RHSValue.getLValueOffset();
1450 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001451 }
1452 bool Result;
John McCall2de56d12010-08-25 11:45:40 +00001453 if (E->getOpcode() == BO_EQ) {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001454 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001455 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001456 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1457 }
1458 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001459 }
1460 }
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001461 if (!LHSTy->isIntegralOrEnumerationType() ||
1462 !RHSTy->isIntegralOrEnumerationType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001463 // We can't continue from here for non-integral types, and they
1464 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001465 return false;
1466 }
1467
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001468 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001469 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001470 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001471
Eli Friedman42edd0d2009-03-24 01:14:50 +00001472 APValue RHSVal;
1473 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001474 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001475
1476 // Handle cases like (unsigned long)&a + 4.
1477 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001478 CharUnits Offset = Result.getLValueOffset();
1479 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1480 RHSVal.getInt().getZExtValue());
John McCall2de56d12010-08-25 11:45:40 +00001481 if (E->getOpcode() == BO_Add)
Ken Dycka7305832010-01-15 12:37:54 +00001482 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001483 else
Ken Dycka7305832010-01-15 12:37:54 +00001484 Offset -= AdditionalOffset;
1485 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001486 return true;
1487 }
1488
1489 // Handle cases like 4 + (unsigned long)&a
John McCall2de56d12010-08-25 11:45:40 +00001490 if (E->getOpcode() == BO_Add &&
Eli Friedman42edd0d2009-03-24 01:14:50 +00001491 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001492 CharUnits Offset = RHSVal.getLValueOffset();
1493 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1494 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001495 return true;
1496 }
1497
1498 // All the following cases expect both operands to be an integer
1499 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001500 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001501
Eli Friedman42edd0d2009-03-24 01:14:50 +00001502 APSInt& RHS = RHSVal.getInt();
1503
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001504 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001505 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001506 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001507 case BO_Mul: return Success(Result.getInt() * RHS, E);
1508 case BO_Add: return Success(Result.getInt() + RHS, E);
1509 case BO_Sub: return Success(Result.getInt() - RHS, E);
1510 case BO_And: return Success(Result.getInt() & RHS, E);
1511 case BO_Xor: return Success(Result.getInt() ^ RHS, E);
1512 case BO_Or: return Success(Result.getInt() | RHS, E);
1513 case BO_Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001514 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001515 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001516 return Success(Result.getInt() / RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001517 case BO_Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001518 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001519 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001520 return Success(Result.getInt() % RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001521 case BO_Shl: {
John McCall091f23f2010-11-09 22:22:12 +00001522 // During constant-folding, a negative shift is an opposite shift.
1523 if (RHS.isSigned() && RHS.isNegative()) {
1524 RHS = -RHS;
1525 goto shift_right;
1526 }
1527
1528 shift_left:
1529 unsigned SA
1530 = (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001531 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001532 }
John McCall2de56d12010-08-25 11:45:40 +00001533 case BO_Shr: {
John McCall091f23f2010-11-09 22:22:12 +00001534 // During constant-folding, a negative shift is an opposite shift.
1535 if (RHS.isSigned() && RHS.isNegative()) {
1536 RHS = -RHS;
1537 goto shift_left;
1538 }
1539
1540 shift_right:
Mike Stump1eb44332009-09-09 15:08:12 +00001541 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001542 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1543 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
John McCall2de56d12010-08-25 11:45:40 +00001546 case BO_LT: return Success(Result.getInt() < RHS, E);
1547 case BO_GT: return Success(Result.getInt() > RHS, E);
1548 case BO_LE: return Success(Result.getInt() <= RHS, E);
1549 case BO_GE: return Success(Result.getInt() >= RHS, E);
1550 case BO_EQ: return Success(Result.getInt() == RHS, E);
1551 case BO_NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001552 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001553}
1554
John McCall56ca35d2011-02-17 10:25:35 +00001555bool IntExprEvaluator::
1556VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) {
1557 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
1558 if (opaque.hasError()) return false;
1559
1560 bool cond;
1561 if (!HandleConversionToBool(e->getCond(), cond, Info))
1562 return false;
1563
1564 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
1565}
1566
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001567bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001568 bool Cond;
1569 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001570 return false;
1571
Nuno Lopesa25bd552008-11-16 22:06:39 +00001572 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001573}
1574
Ken Dyck8b752f12010-01-27 17:10:57 +00001575CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001576 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1577 // the result is the size of the referenced type."
1578 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1579 // result shall be the alignment of the referenced type."
1580 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1581 T = Ref->getPointeeType();
1582
Eli Friedman2be58612009-05-30 21:09:44 +00001583 // __alignof is defined to return the preferred alignment.
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001584 return Info.Ctx.toCharUnitsFromBits(
1585 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
Chris Lattnere9feb472009-01-24 21:09:06 +00001586}
1587
Ken Dyck8b752f12010-01-27 17:10:57 +00001588CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001589 E = E->IgnoreParens();
1590
1591 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001592 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001593 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001594 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1595 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001596
Chris Lattneraf707ab2009-01-24 21:53:27 +00001597 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001598 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1599 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001600
Chris Lattnere9feb472009-01-24 21:09:06 +00001601 return GetAlignOfType(E->getType());
1602}
1603
1604
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001605/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
1606/// a result as the expression's type.
1607bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
1608 const UnaryExprOrTypeTraitExpr *E) {
1609 switch(E->getKind()) {
1610 case UETT_AlignOf: {
Chris Lattnere9feb472009-01-24 21:09:06 +00001611 if (E->isArgumentType())
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001612 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001613 else
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001614 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001615 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001616
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001617 case UETT_VecStep: {
1618 QualType Ty = E->getTypeOfArgument();
Sebastian Redl05189992008-11-11 17:56:53 +00001619
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001620 if (Ty->isVectorType()) {
1621 unsigned n = Ty->getAs<VectorType>()->getNumElements();
Eli Friedmana1f47c42009-03-23 04:38:34 +00001622
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001623 // The vec_step built-in functions that take a 3-component
1624 // vector return 4. (OpenCL 1.1 spec 6.11.12)
1625 if (n == 3)
1626 n = 4;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001627
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001628 return Success(n, E);
1629 } else
1630 return Success(1, E);
1631 }
1632
1633 case UETT_SizeOf: {
1634 QualType SrcTy = E->getTypeOfArgument();
1635 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1636 // the result is the size of the referenced type."
1637 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1638 // result shall be the alignment of the referenced type."
1639 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1640 SrcTy = Ref->getPointeeType();
1641
1642 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1643 // extension.
1644 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1645 return Success(1, E);
1646
1647 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
1648 if (!SrcTy->isConstantSizeType())
1649 return false;
1650
1651 // Get information about the size.
1652 return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E);
1653 }
1654 }
1655
1656 llvm_unreachable("unknown expr/type trait");
1657 return false;
Chris Lattnerfcee0012008-07-11 21:24:13 +00001658}
1659
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001660bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1661 CharUnits Result;
1662 unsigned n = E->getNumComponents();
1663 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1664 if (n == 0)
1665 return false;
1666 QualType CurrentType = E->getTypeSourceInfo()->getType();
1667 for (unsigned i = 0; i != n; ++i) {
1668 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1669 switch (ON.getKind()) {
1670 case OffsetOfExpr::OffsetOfNode::Array: {
1671 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1672 APSInt IdxResult;
1673 if (!EvaluateInteger(Idx, IdxResult, Info))
1674 return false;
1675 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1676 if (!AT)
1677 return false;
1678 CurrentType = AT->getElementType();
1679 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1680 Result += IdxResult.getSExtValue() * ElementSize;
1681 break;
1682 }
1683
1684 case OffsetOfExpr::OffsetOfNode::Field: {
1685 FieldDecl *MemberDecl = ON.getField();
1686 const RecordType *RT = CurrentType->getAs<RecordType>();
1687 if (!RT)
1688 return false;
1689 RecordDecl *RD = RT->getDecl();
1690 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
John McCallba4f5d52011-01-20 07:57:12 +00001691 unsigned i = MemberDecl->getFieldIndex();
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001692 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001693 Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001694 CurrentType = MemberDecl->getType().getNonReferenceType();
1695 break;
1696 }
1697
1698 case OffsetOfExpr::OffsetOfNode::Identifier:
1699 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001700 return false;
1701
1702 case OffsetOfExpr::OffsetOfNode::Base: {
1703 CXXBaseSpecifier *BaseSpec = ON.getBase();
1704 if (BaseSpec->isVirtual())
1705 return false;
1706
1707 // Find the layout of the class whose base we are looking into.
1708 const RecordType *RT = CurrentType->getAs<RecordType>();
1709 if (!RT)
1710 return false;
1711 RecordDecl *RD = RT->getDecl();
1712 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1713
1714 // Find the base class itself.
1715 CurrentType = BaseSpec->getType();
1716 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1717 if (!BaseRT)
1718 return false;
1719
1720 // Add the offset to the base.
Ken Dyck7c7f8202011-01-26 02:17:08 +00001721 Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001722 break;
1723 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001724 }
1725 }
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001726 return Success(Result, E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001727}
1728
Chris Lattnerb542afe2008-07-11 19:10:17 +00001729bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001730 if (E->getOpcode() == UO_LNot) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001731 // LNot's operand isn't necessarily an integer, so we handle it specially.
1732 bool bres;
1733 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1734 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001735 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001736 }
1737
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001738 // Only handle integral operations...
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001739 if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001740 return false;
1741
Chris Lattner87eae5e2008-07-11 22:52:41 +00001742 // Get the operand value into 'Result'.
1743 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001744 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001745
Chris Lattner75a48812008-07-11 22:15:16 +00001746 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001747 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001748 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1749 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001750 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001751 case UO_Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001752 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1753 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001754 return true;
John McCall2de56d12010-08-25 11:45:40 +00001755 case UO_Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001756 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001757 return true;
John McCall2de56d12010-08-25 11:45:40 +00001758 case UO_Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001759 if (!Result.isInt()) return false;
1760 return Success(-Result.getInt(), E);
John McCall2de56d12010-08-25 11:45:40 +00001761 case UO_Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001762 if (!Result.isInt()) return false;
1763 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001764 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001765}
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Chris Lattner732b2232008-07-12 01:15:53 +00001767/// HandleCast - This is used to evaluate implicit or explicit casts where the
1768/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001769bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001770 Expr *SubExpr = E->getSubExpr();
1771 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001772 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001773
Eli Friedman4efaa272008-11-12 09:44:48 +00001774 if (DestType->isBooleanType()) {
1775 bool BoolResult;
1776 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1777 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001778 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001779 }
1780
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001781 // Handle simple integer->integer casts.
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001782 if (SrcType->isIntegralOrEnumerationType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001783 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001784 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001785
Eli Friedmanbe265702009-02-20 01:15:07 +00001786 if (!Result.isInt()) {
1787 // Only allow casts of lvalues if they are lossless.
1788 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1789 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001790
Daniel Dunbardd211642009-02-19 22:24:01 +00001791 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001792 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Chris Lattner732b2232008-07-12 01:15:53 +00001795 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001796 if (SrcType->isPointerType()) {
John McCallefdb83e2010-05-07 21:00:08 +00001797 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001798 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001799 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001800
Daniel Dunbardd211642009-02-19 22:24:01 +00001801 if (LV.getLValueBase()) {
1802 // Only allow based lvalue casts if they are lossless.
1803 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1804 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001805
John McCallefdb83e2010-05-07 21:00:08 +00001806 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001807 return true;
1808 }
1809
Ken Dycka7305832010-01-15 12:37:54 +00001810 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1811 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001812 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001813 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001814
Eli Friedmanbe265702009-02-20 01:15:07 +00001815 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1816 // This handles double-conversion cases, where there's both
1817 // an l-value promotion and an implicit conversion to int.
John McCallefdb83e2010-05-07 21:00:08 +00001818 LValue LV;
Eli Friedmanbe265702009-02-20 01:15:07 +00001819 if (!EvaluateLValue(SubExpr, LV, Info))
1820 return false;
1821
1822 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1823 return false;
1824
John McCallefdb83e2010-05-07 21:00:08 +00001825 LV.moveInto(Result);
Eli Friedmanbe265702009-02-20 01:15:07 +00001826 return true;
1827 }
1828
Eli Friedman1725f682009-04-22 19:23:09 +00001829 if (SrcType->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001830 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001831 if (!EvaluateComplex(SubExpr, C, Info))
1832 return false;
1833 if (C.isComplexFloat())
1834 return Success(HandleFloatToIntCast(DestType, SrcType,
1835 C.getComplexFloatReal(), Info.Ctx),
1836 E);
1837 else
1838 return Success(HandleIntToIntCast(DestType, SrcType,
1839 C.getComplexIntReal(), Info.Ctx), E);
1840 }
Eli Friedman2217c872009-02-22 11:46:18 +00001841 // FIXME: Handle vectors
1842
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001843 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001844 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001845
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001846 APFloat F(0.0);
1847 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001848 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001850 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001851}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001852
Eli Friedman722c7172009-02-28 03:59:05 +00001853bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1854 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001855 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001856 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1857 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1858 return Success(LV.getComplexIntReal(), E);
1859 }
1860
1861 return Visit(E->getSubExpr());
1862}
1863
Eli Friedman664a1042009-02-27 04:45:43 +00001864bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001865 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001866 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001867 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1868 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1869 return Success(LV.getComplexIntImag(), E);
1870 }
1871
Eli Friedman664a1042009-02-27 04:45:43 +00001872 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1873 Info.EvalResult.HasSideEffects = true;
1874 return Success(0, E);
1875}
1876
Douglas Gregoree8aff02011-01-04 17:33:58 +00001877bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1878 return Success(E->getPackLength(), E);
1879}
1880
Sebastian Redl295995c2010-09-10 20:55:47 +00001881bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1882 return Success(E->getValue(), E);
1883}
1884
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001885//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001886// Float Evaluation
1887//===----------------------------------------------------------------------===//
1888
1889namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001890class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001891 : public StmtVisitor<FloatExprEvaluator, bool> {
1892 EvalInfo &Info;
1893 APFloat &Result;
1894public:
1895 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1896 : Info(info), Result(result) {}
1897
1898 bool VisitStmt(Stmt *S) {
1899 return false;
1900 }
1901
1902 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001903 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001904
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001905 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001906 bool VisitBinaryOperator(const BinaryOperator *E);
1907 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001908 bool VisitCastExpr(CastExpr *E);
Douglas Gregored8abf12010-07-08 06:14:04 +00001909 bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001910 bool VisitConditionalOperator(ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00001911 bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001912
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001913 bool VisitChooseExpr(const ChooseExpr *E)
1914 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1915 bool VisitUnaryExtension(const UnaryOperator *E)
1916 { return Visit(E->getSubExpr()); }
John McCallabd3a852010-05-07 22:08:54 +00001917 bool VisitUnaryReal(const UnaryOperator *E);
1918 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001919
John McCall189d6ef2010-10-09 01:34:31 +00001920 bool VisitDeclRefExpr(const DeclRefExpr *E);
1921
John McCall56ca35d2011-02-17 10:25:35 +00001922 bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) {
1923 const APValue *value = Info.getOpaqueValue(e);
1924 if (!value)
1925 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
1926 Result = value->getFloat();
1927 return true;
1928 }
1929
John McCallabd3a852010-05-07 22:08:54 +00001930 // FIXME: Missing: array subscript of vector, member of vector,
1931 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001932};
1933} // end anonymous namespace
1934
1935static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001936 assert(E->getType()->isRealFloatingType());
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001937 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1938}
1939
Jay Foad4ba2a172011-01-12 09:06:06 +00001940static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
John McCalldb7b72a2010-02-28 13:00:19 +00001941 QualType ResultTy,
1942 const Expr *Arg,
1943 bool SNaN,
1944 llvm::APFloat &Result) {
1945 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1946 if (!S) return false;
1947
1948 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1949
1950 llvm::APInt fill;
1951
1952 // Treat empty strings as if they were zero.
1953 if (S->getString().empty())
1954 fill = llvm::APInt(32, 0);
1955 else if (S->getString().getAsInteger(0, fill))
1956 return false;
1957
1958 if (SNaN)
1959 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1960 else
1961 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1962 return true;
1963}
1964
Chris Lattner019f4e82008-10-06 05:28:25 +00001965bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001966 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001967 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001968 case Builtin::BI__builtin_huge_val:
1969 case Builtin::BI__builtin_huge_valf:
1970 case Builtin::BI__builtin_huge_vall:
1971 case Builtin::BI__builtin_inf:
1972 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001973 case Builtin::BI__builtin_infl: {
1974 const llvm::fltSemantics &Sem =
1975 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001976 Result = llvm::APFloat::getInf(Sem);
1977 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001978 }
Mike Stump1eb44332009-09-09 15:08:12 +00001979
John McCalldb7b72a2010-02-28 13:00:19 +00001980 case Builtin::BI__builtin_nans:
1981 case Builtin::BI__builtin_nansf:
1982 case Builtin::BI__builtin_nansl:
1983 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1984 true, Result);
1985
Chris Lattner9e621712008-10-06 06:31:58 +00001986 case Builtin::BI__builtin_nan:
1987 case Builtin::BI__builtin_nanf:
1988 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001989 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001990 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001991 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1992 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001993
1994 case Builtin::BI__builtin_fabs:
1995 case Builtin::BI__builtin_fabsf:
1996 case Builtin::BI__builtin_fabsl:
1997 if (!EvaluateFloat(E->getArg(0), Result, Info))
1998 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002000 if (Result.isNegative())
2001 Result.changeSign();
2002 return true;
2003
Mike Stump1eb44332009-09-09 15:08:12 +00002004 case Builtin::BI__builtin_copysign:
2005 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002006 case Builtin::BI__builtin_copysignl: {
2007 APFloat RHS(0.);
2008 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
2009 !EvaluateFloat(E->getArg(1), RHS, Info))
2010 return false;
2011 Result.copySign(RHS);
2012 return true;
2013 }
Chris Lattner019f4e82008-10-06 05:28:25 +00002014 }
2015}
2016
John McCall189d6ef2010-10-09 01:34:31 +00002017bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
2018 const Decl *D = E->getDecl();
2019 if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false;
2020 const VarDecl *VD = cast<VarDecl>(D);
2021
2022 // Require the qualifiers to be const and not volatile.
2023 CanQualType T = Info.Ctx.getCanonicalType(E->getType());
2024 if (!T.isConstQualified() || T.isVolatileQualified())
2025 return false;
2026
2027 const Expr *Init = VD->getAnyInitializer();
2028 if (!Init) return false;
2029
2030 if (APValue *V = VD->getEvaluatedValue()) {
2031 if (V->isFloat()) {
2032 Result = V->getFloat();
2033 return true;
2034 }
2035 return false;
2036 }
2037
2038 if (VD->isEvaluatingValue())
2039 return false;
2040
2041 VD->setEvaluatingValue();
2042
2043 Expr::EvalResult InitResult;
2044 if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects &&
2045 InitResult.Val.isFloat()) {
2046 // Cache the evaluated value in the variable declaration.
2047 Result = InitResult.Val.getFloat();
2048 VD->setEvaluatedValue(InitResult.Val);
2049 return true;
2050 }
2051
2052 VD->setEvaluatedValue(APValue());
2053 return false;
2054}
2055
John McCallabd3a852010-05-07 22:08:54 +00002056bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002057 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2058 ComplexValue CV;
2059 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2060 return false;
2061 Result = CV.FloatReal;
2062 return true;
2063 }
2064
2065 return Visit(E->getSubExpr());
John McCallabd3a852010-05-07 22:08:54 +00002066}
2067
2068bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002069 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2070 ComplexValue CV;
2071 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2072 return false;
2073 Result = CV.FloatImag;
2074 return true;
2075 }
2076
2077 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
2078 Info.EvalResult.HasSideEffects = true;
2079 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
2080 Result = llvm::APFloat::getZero(Sem);
John McCallabd3a852010-05-07 22:08:54 +00002081 return true;
2082}
2083
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002084bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002085 if (E->getOpcode() == UO_Deref)
Nuno Lopesa468d342008-11-19 17:44:31 +00002086 return false;
2087
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002088 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
2089 return false;
2090
2091 switch (E->getOpcode()) {
2092 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002093 case UO_Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002094 return true;
John McCall2de56d12010-08-25 11:45:40 +00002095 case UO_Minus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002096 Result.changeSign();
2097 return true;
2098 }
2099}
Chris Lattner019f4e82008-10-06 05:28:25 +00002100
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002101bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002102 if (E->getOpcode() == BO_Comma) {
Eli Friedman7f92f032009-11-16 04:25:37 +00002103 if (!EvaluateFloat(E->getRHS(), Result, Info))
2104 return false;
2105
2106 // If we can't evaluate the LHS, it might have side effects;
2107 // conservatively mark it.
2108 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2109 Info.EvalResult.HasSideEffects = true;
2110
2111 return true;
2112 }
2113
Anders Carlsson96e93662010-10-31 01:21:47 +00002114 // We can't evaluate pointer-to-member operations.
2115 if (E->isPtrMemOp())
2116 return false;
2117
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002118 // FIXME: Diagnostics? I really don't understand how the warnings
2119 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002120 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002121 if (!EvaluateFloat(E->getLHS(), Result, Info))
2122 return false;
2123 if (!EvaluateFloat(E->getRHS(), RHS, Info))
2124 return false;
2125
2126 switch (E->getOpcode()) {
2127 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002128 case BO_Mul:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002129 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
2130 return true;
John McCall2de56d12010-08-25 11:45:40 +00002131 case BO_Add:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002132 Result.add(RHS, APFloat::rmNearestTiesToEven);
2133 return true;
John McCall2de56d12010-08-25 11:45:40 +00002134 case BO_Sub:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002135 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
2136 return true;
John McCall2de56d12010-08-25 11:45:40 +00002137 case BO_Div:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002138 Result.divide(RHS, APFloat::rmNearestTiesToEven);
2139 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002140 }
2141}
2142
2143bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
2144 Result = E->getValue();
2145 return true;
2146}
2147
Eli Friedman4efaa272008-11-12 09:44:48 +00002148bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
2149 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002151 if (SubExpr->getType()->isIntegralOrEnumerationType()) {
Eli Friedman4efaa272008-11-12 09:44:48 +00002152 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00002153 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00002154 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002155 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002156 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002157 return true;
2158 }
2159 if (SubExpr->getType()->isRealFloatingType()) {
2160 if (!Visit(SubExpr))
2161 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002162 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
2163 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002164 return true;
2165 }
John McCallf3ea8cf2010-11-14 08:17:51 +00002166
2167 if (E->getCastKind() == CK_FloatingComplexToReal) {
2168 ComplexValue V;
2169 if (!EvaluateComplex(SubExpr, V, Info))
2170 return false;
2171 Result = V.getComplexFloatReal();
2172 return true;
2173 }
Eli Friedman4efaa272008-11-12 09:44:48 +00002174
2175 return false;
2176}
2177
Douglas Gregored8abf12010-07-08 06:14:04 +00002178bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +00002179 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
2180 return true;
2181}
2182
John McCall56ca35d2011-02-17 10:25:35 +00002183bool FloatExprEvaluator::
2184VisitBinaryConditionalOperator(BinaryConditionalOperator *e) {
2185 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
2186 if (opaque.hasError()) return false;
2187
2188 bool cond;
2189 if (!HandleConversionToBool(e->getCond(), cond, Info))
2190 return false;
2191
2192 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
2193}
2194
Eli Friedman67f85fc2009-12-04 02:12:53 +00002195bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
2196 bool Cond;
2197 if (!HandleConversionToBool(E->getCond(), Cond, Info))
2198 return false;
2199
2200 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
2201}
2202
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002203//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002204// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002205//===----------------------------------------------------------------------===//
2206
2207namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00002208class ComplexExprEvaluator
John McCallf4cf1a12010-05-07 17:22:02 +00002209 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002210 EvalInfo &Info;
John McCallf4cf1a12010-05-07 17:22:02 +00002211 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002213public:
John McCallf4cf1a12010-05-07 17:22:02 +00002214 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
2215 : Info(info), Result(Result) {}
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002217 //===--------------------------------------------------------------------===//
2218 // Visitor Methods
2219 //===--------------------------------------------------------------------===//
2220
John McCallf4cf1a12010-05-07 17:22:02 +00002221 bool VisitStmt(Stmt *S) {
2222 return false;
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002223 }
Mike Stump1eb44332009-09-09 15:08:12 +00002224
John McCallf4cf1a12010-05-07 17:22:02 +00002225 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002226
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002227 bool VisitImaginaryLiteral(ImaginaryLiteral *E);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002228
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002229 bool VisitCastExpr(CastExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002230
John McCallf4cf1a12010-05-07 17:22:02 +00002231 bool VisitBinaryOperator(const BinaryOperator *E);
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002232 bool VisitUnaryOperator(const UnaryOperator *E);
2233 bool VisitConditionalOperator(const ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00002234 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E);
John McCallf4cf1a12010-05-07 17:22:02 +00002235 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002236 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallf4cf1a12010-05-07 17:22:02 +00002237 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002238 { return Visit(E->getSubExpr()); }
John McCall56ca35d2011-02-17 10:25:35 +00002239 bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) {
2240 const APValue *value = Info.getOpaqueValue(e);
2241 if (!value)
2242 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
2243 Result.setFrom(*value);
2244 return true;
2245 }
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002246 // FIXME Missing: ImplicitValueInitExpr
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002247};
2248} // end anonymous namespace
2249
John McCallf4cf1a12010-05-07 17:22:02 +00002250static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2251 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002252 assert(E->getType()->isAnyComplexType());
John McCallf4cf1a12010-05-07 17:22:02 +00002253 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002254}
2255
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002256bool ComplexExprEvaluator::VisitImaginaryLiteral(ImaginaryLiteral *E) {
2257 Expr* SubExpr = E->getSubExpr();
2258
2259 if (SubExpr->getType()->isRealFloatingType()) {
2260 Result.makeComplexFloat();
2261 APFloat &Imag = Result.FloatImag;
2262 if (!EvaluateFloat(SubExpr, Imag, Info))
2263 return false;
2264
2265 Result.FloatReal = APFloat(Imag.getSemantics());
2266 return true;
2267 } else {
2268 assert(SubExpr->getType()->isIntegerType() &&
2269 "Unexpected imaginary literal.");
2270
2271 Result.makeComplexInt();
2272 APSInt &Imag = Result.IntImag;
2273 if (!EvaluateInteger(SubExpr, Imag, Info))
2274 return false;
2275
2276 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2277 return true;
2278 }
2279}
2280
2281bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002282
John McCall8786da72010-12-14 17:51:41 +00002283 switch (E->getCastKind()) {
2284 case CK_BitCast:
2285 case CK_LValueBitCast:
2286 case CK_BaseToDerived:
2287 case CK_DerivedToBase:
2288 case CK_UncheckedDerivedToBase:
2289 case CK_Dynamic:
2290 case CK_ToUnion:
2291 case CK_ArrayToPointerDecay:
2292 case CK_FunctionToPointerDecay:
2293 case CK_NullToPointer:
2294 case CK_NullToMemberPointer:
2295 case CK_BaseToDerivedMemberPointer:
2296 case CK_DerivedToBaseMemberPointer:
2297 case CK_MemberPointerToBoolean:
2298 case CK_ConstructorConversion:
2299 case CK_IntegralToPointer:
2300 case CK_PointerToIntegral:
2301 case CK_PointerToBoolean:
2302 case CK_ToVoid:
2303 case CK_VectorSplat:
2304 case CK_IntegralCast:
2305 case CK_IntegralToBoolean:
2306 case CK_IntegralToFloating:
2307 case CK_FloatingToIntegral:
2308 case CK_FloatingToBoolean:
2309 case CK_FloatingCast:
2310 case CK_AnyPointerToObjCPointerCast:
2311 case CK_AnyPointerToBlockPointerCast:
2312 case CK_ObjCObjectLValueCast:
2313 case CK_FloatingComplexToReal:
2314 case CK_FloatingComplexToBoolean:
2315 case CK_IntegralComplexToReal:
2316 case CK_IntegralComplexToBoolean:
2317 llvm_unreachable("invalid cast kind for complex value");
John McCall2bb5d002010-11-13 09:02:35 +00002318
John McCall8786da72010-12-14 17:51:41 +00002319 case CK_LValueToRValue:
2320 case CK_NoOp:
2321 return Visit(E->getSubExpr());
2322
2323 case CK_Dependent:
2324 case CK_GetObjCProperty:
2325 case CK_UserDefinedConversion:
2326 return false;
2327
2328 case CK_FloatingRealToComplex: {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002329 APFloat &Real = Result.FloatReal;
John McCall8786da72010-12-14 17:51:41 +00002330 if (!EvaluateFloat(E->getSubExpr(), Real, Info))
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002331 return false;
2332
John McCall8786da72010-12-14 17:51:41 +00002333 Result.makeComplexFloat();
2334 Result.FloatImag = APFloat(Real.getSemantics());
2335 return true;
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002336 }
2337
John McCall8786da72010-12-14 17:51:41 +00002338 case CK_FloatingComplexCast: {
2339 if (!Visit(E->getSubExpr()))
2340 return false;
2341
2342 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2343 QualType From
2344 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2345
2346 Result.FloatReal
2347 = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx);
2348 Result.FloatImag
2349 = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx);
2350 return true;
2351 }
2352
2353 case CK_FloatingComplexToIntegralComplex: {
2354 if (!Visit(E->getSubExpr()))
2355 return false;
2356
2357 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2358 QualType From
2359 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2360 Result.makeComplexInt();
2361 Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx);
2362 Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx);
2363 return true;
2364 }
2365
2366 case CK_IntegralRealToComplex: {
2367 APSInt &Real = Result.IntReal;
2368 if (!EvaluateInteger(E->getSubExpr(), Real, Info))
2369 return false;
2370
2371 Result.makeComplexInt();
2372 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2373 return true;
2374 }
2375
2376 case CK_IntegralComplexCast: {
2377 if (!Visit(E->getSubExpr()))
2378 return false;
2379
2380 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2381 QualType From
2382 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2383
2384 Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx);
2385 Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx);
2386 return true;
2387 }
2388
2389 case CK_IntegralComplexToFloatingComplex: {
2390 if (!Visit(E->getSubExpr()))
2391 return false;
2392
2393 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2394 QualType From
2395 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2396 Result.makeComplexFloat();
2397 Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx);
2398 Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx);
2399 return true;
2400 }
2401 }
2402
2403 llvm_unreachable("unknown cast resulting in complex value");
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002404 return false;
2405}
2406
John McCallf4cf1a12010-05-07 17:22:02 +00002407bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002408 if (E->getOpcode() == BO_Comma) {
2409 if (!Visit(E->getRHS()))
2410 return false;
2411
2412 // If we can't evaluate the LHS, it might have side effects;
2413 // conservatively mark it.
2414 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2415 Info.EvalResult.HasSideEffects = true;
2416
2417 return true;
2418 }
John McCallf4cf1a12010-05-07 17:22:02 +00002419 if (!Visit(E->getLHS()))
2420 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002421
John McCallf4cf1a12010-05-07 17:22:02 +00002422 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002423 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002424 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002425
Daniel Dunbar3f279872009-01-29 01:32:56 +00002426 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2427 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002428 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002429 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002430 case BO_Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002431 if (Result.isComplexFloat()) {
2432 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2433 APFloat::rmNearestTiesToEven);
2434 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2435 APFloat::rmNearestTiesToEven);
2436 } else {
2437 Result.getComplexIntReal() += RHS.getComplexIntReal();
2438 Result.getComplexIntImag() += RHS.getComplexIntImag();
2439 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002440 break;
John McCall2de56d12010-08-25 11:45:40 +00002441 case BO_Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002442 if (Result.isComplexFloat()) {
2443 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2444 APFloat::rmNearestTiesToEven);
2445 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2446 APFloat::rmNearestTiesToEven);
2447 } else {
2448 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2449 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2450 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002451 break;
John McCall2de56d12010-08-25 11:45:40 +00002452 case BO_Mul:
Daniel Dunbar3f279872009-01-29 01:32:56 +00002453 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002454 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002455 APFloat &LHS_r = LHS.getComplexFloatReal();
2456 APFloat &LHS_i = LHS.getComplexFloatImag();
2457 APFloat &RHS_r = RHS.getComplexFloatReal();
2458 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Daniel Dunbar3f279872009-01-29 01:32:56 +00002460 APFloat Tmp = LHS_r;
2461 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2462 Result.getComplexFloatReal() = Tmp;
2463 Tmp = LHS_i;
2464 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2465 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2466
2467 Tmp = LHS_r;
2468 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2469 Result.getComplexFloatImag() = Tmp;
2470 Tmp = LHS_i;
2471 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2472 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2473 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002474 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002475 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002476 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2477 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002478 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002479 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2480 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2481 }
2482 break;
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002483 case BO_Div:
2484 if (Result.isComplexFloat()) {
2485 ComplexValue LHS = Result;
2486 APFloat &LHS_r = LHS.getComplexFloatReal();
2487 APFloat &LHS_i = LHS.getComplexFloatImag();
2488 APFloat &RHS_r = RHS.getComplexFloatReal();
2489 APFloat &RHS_i = RHS.getComplexFloatImag();
2490 APFloat &Res_r = Result.getComplexFloatReal();
2491 APFloat &Res_i = Result.getComplexFloatImag();
2492
2493 APFloat Den = RHS_r;
2494 Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2495 APFloat Tmp = RHS_i;
2496 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2497 Den.add(Tmp, APFloat::rmNearestTiesToEven);
2498
2499 Res_r = LHS_r;
2500 Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2501 Tmp = LHS_i;
2502 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2503 Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
2504 Res_r.divide(Den, APFloat::rmNearestTiesToEven);
2505
2506 Res_i = LHS_i;
2507 Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2508 Tmp = LHS_r;
2509 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2510 Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
2511 Res_i.divide(Den, APFloat::rmNearestTiesToEven);
2512 } else {
2513 if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) {
2514 // FIXME: what about diagnostics?
2515 return false;
2516 }
2517 ComplexValue LHS = Result;
2518 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
2519 RHS.getComplexIntImag() * RHS.getComplexIntImag();
2520 Result.getComplexIntReal() =
2521 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
2522 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
2523 Result.getComplexIntImag() =
2524 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
2525 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
2526 }
2527 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002528 }
2529
John McCallf4cf1a12010-05-07 17:22:02 +00002530 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002531}
2532
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002533bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
2534 // Get the operand value into 'Result'.
2535 if (!Visit(E->getSubExpr()))
2536 return false;
2537
2538 switch (E->getOpcode()) {
2539 default:
2540 // FIXME: what about diagnostics?
2541 return false;
2542 case UO_Extension:
2543 return true;
2544 case UO_Plus:
2545 // The result is always just the subexpr.
2546 return true;
2547 case UO_Minus:
2548 if (Result.isComplexFloat()) {
2549 Result.getComplexFloatReal().changeSign();
2550 Result.getComplexFloatImag().changeSign();
2551 }
2552 else {
2553 Result.getComplexIntReal() = -Result.getComplexIntReal();
2554 Result.getComplexIntImag() = -Result.getComplexIntImag();
2555 }
2556 return true;
2557 case UO_Not:
2558 if (Result.isComplexFloat())
2559 Result.getComplexFloatImag().changeSign();
2560 else
2561 Result.getComplexIntImag() = -Result.getComplexIntImag();
2562 return true;
2563 }
2564}
2565
John McCall56ca35d2011-02-17 10:25:35 +00002566bool ComplexExprEvaluator::
2567VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) {
2568 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
2569 if (opaque.hasError()) return false;
2570
2571 bool cond;
2572 if (!HandleConversionToBool(e->getCond(), cond, Info))
2573 return false;
2574
2575 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
2576}
2577
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002578bool ComplexExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
2579 bool Cond;
2580 if (!HandleConversionToBool(E->getCond(), Cond, Info))
2581 return false;
2582
2583 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
2584}
2585
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002586//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002587// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002588//===----------------------------------------------------------------------===//
2589
John McCall56ca35d2011-02-17 10:25:35 +00002590static bool Evaluate(EvalInfo &Info, const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +00002591 if (E->getType()->isVectorType()) {
2592 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002593 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002594 } else if (E->getType()->isIntegerType()) {
2595 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002596 return false;
John McCall56ca35d2011-02-17 10:25:35 +00002597 if (Info.EvalResult.Val.isLValue() &&
2598 !IsGlobalLValue(Info.EvalResult.Val.getLValueBase()))
John McCall0f2b6922010-07-07 05:08:32 +00002599 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002600 } else if (E->getType()->hasPointerRepresentation()) {
2601 LValue LV;
2602 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002603 return false;
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002604 if (!IsGlobalLValue(LV.Base))
John McCall42c8f872010-05-10 23:27:23 +00002605 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002606 LV.moveInto(Info.EvalResult.Val);
2607 } else if (E->getType()->isRealFloatingType()) {
2608 llvm::APFloat F(0.0);
2609 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002610 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002611
John McCallefdb83e2010-05-07 21:00:08 +00002612 Info.EvalResult.Val = APValue(F);
2613 } else if (E->getType()->isAnyComplexType()) {
2614 ComplexValue C;
2615 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002616 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002617 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002618 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002619 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002620
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002621 return true;
2622}
2623
John McCall56ca35d2011-02-17 10:25:35 +00002624/// Evaluate - Return true if this is a constant which we can fold using
2625/// any crazy technique (that has nothing to do with language standards) that
2626/// we want to. If this function returns true, it returns the folded constant
2627/// in Result.
2628bool Expr::Evaluate(EvalResult &Result, const ASTContext &Ctx) const {
2629 EvalInfo Info(Ctx, Result);
2630 return ::Evaluate(Info, this);
2631}
2632
Jay Foad4ba2a172011-01-12 09:06:06 +00002633bool Expr::EvaluateAsBooleanCondition(bool &Result,
2634 const ASTContext &Ctx) const {
John McCallcd7a4452010-01-05 23:42:56 +00002635 EvalResult Scratch;
2636 EvalInfo Info(Ctx, Scratch);
2637
2638 return HandleConversionToBool(this, Result, Info);
2639}
2640
Jay Foad4ba2a172011-01-12 09:06:06 +00002641bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
Anders Carlsson1b782762009-04-10 04:54:13 +00002642 EvalInfo Info(Ctx, Result);
2643
John McCallefdb83e2010-05-07 21:00:08 +00002644 LValue LV;
John McCall42c8f872010-05-10 23:27:23 +00002645 if (EvaluateLValue(this, LV, Info) &&
2646 !Result.HasSideEffects &&
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002647 IsGlobalLValue(LV.Base)) {
2648 LV.moveInto(Result.Val);
2649 return true;
2650 }
2651 return false;
2652}
2653
Jay Foad4ba2a172011-01-12 09:06:06 +00002654bool Expr::EvaluateAsAnyLValue(EvalResult &Result,
2655 const ASTContext &Ctx) const {
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002656 EvalInfo Info(Ctx, Result);
2657
2658 LValue LV;
2659 if (EvaluateLValue(this, LV, Info)) {
John McCallefdb83e2010-05-07 21:00:08 +00002660 LV.moveInto(Result.Val);
2661 return true;
2662 }
2663 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002664}
2665
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002666/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002667/// folded, but discard the result.
Jay Foad4ba2a172011-01-12 09:06:06 +00002668bool Expr::isEvaluatable(const ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002669 EvalResult Result;
2670 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002671}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002672
Jay Foad4ba2a172011-01-12 09:06:06 +00002673bool Expr::HasSideEffects(const ASTContext &Ctx) const {
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002674 Expr::EvalResult Result;
2675 EvalInfo Info(Ctx, Result);
2676 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2677}
2678
Jay Foad4ba2a172011-01-12 09:06:06 +00002679APSInt Expr::EvaluateAsInt(const ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002680 EvalResult EvalResult;
2681 bool Result = Evaluate(EvalResult, Ctx);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00002682 (void)Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002683 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002684 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002685
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002686 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002687}
John McCalld905f5a2010-05-07 05:32:02 +00002688
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002689 bool Expr::EvalResult::isGlobalLValue() const {
2690 assert(Val.isLValue());
2691 return IsGlobalLValue(Val.getLValueBase());
2692 }
2693
2694
John McCalld905f5a2010-05-07 05:32:02 +00002695/// isIntegerConstantExpr - this recursive routine will test if an expression is
2696/// an integer constant expression.
2697
2698/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2699/// comma, etc
2700///
2701/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2702/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2703/// cast+dereference.
2704
2705// CheckICE - This function does the fundamental ICE checking: the returned
2706// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2707// Note that to reduce code duplication, this helper does no evaluation
2708// itself; the caller checks whether the expression is evaluatable, and
2709// in the rare cases where CheckICE actually cares about the evaluated
2710// value, it calls into Evalute.
2711//
2712// Meanings of Val:
2713// 0: This expression is an ICE if it can be evaluated by Evaluate.
2714// 1: This expression is not an ICE, but if it isn't evaluated, it's
2715// a legal subexpression for an ICE. This return value is used to handle
2716// the comma operator in C99 mode.
2717// 2: This expression is not an ICE, and is not a legal subexpression for one.
2718
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002719namespace {
2720
John McCalld905f5a2010-05-07 05:32:02 +00002721struct ICEDiag {
2722 unsigned Val;
2723 SourceLocation Loc;
2724
2725 public:
2726 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2727 ICEDiag() : Val(0) {}
2728};
2729
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002730}
2731
2732static ICEDiag NoDiag() { return ICEDiag(); }
John McCalld905f5a2010-05-07 05:32:02 +00002733
2734static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2735 Expr::EvalResult EVResult;
2736 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2737 !EVResult.Val.isInt()) {
2738 return ICEDiag(2, E->getLocStart());
2739 }
2740 return NoDiag();
2741}
2742
2743static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2744 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002745 if (!E->getType()->isIntegralOrEnumerationType()) {
John McCalld905f5a2010-05-07 05:32:02 +00002746 return ICEDiag(2, E->getLocStart());
2747 }
2748
2749 switch (E->getStmtClass()) {
John McCall63c00d72011-02-09 08:16:59 +00002750#define ABSTRACT_STMT(Node)
John McCalld905f5a2010-05-07 05:32:02 +00002751#define STMT(Node, Base) case Expr::Node##Class:
2752#define EXPR(Node, Base)
2753#include "clang/AST/StmtNodes.inc"
2754 case Expr::PredefinedExprClass:
2755 case Expr::FloatingLiteralClass:
2756 case Expr::ImaginaryLiteralClass:
2757 case Expr::StringLiteralClass:
2758 case Expr::ArraySubscriptExprClass:
2759 case Expr::MemberExprClass:
2760 case Expr::CompoundAssignOperatorClass:
2761 case Expr::CompoundLiteralExprClass:
2762 case Expr::ExtVectorElementExprClass:
2763 case Expr::InitListExprClass:
2764 case Expr::DesignatedInitExprClass:
2765 case Expr::ImplicitValueInitExprClass:
2766 case Expr::ParenListExprClass:
2767 case Expr::VAArgExprClass:
2768 case Expr::AddrLabelExprClass:
2769 case Expr::StmtExprClass:
2770 case Expr::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002771 case Expr::CUDAKernelCallExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002772 case Expr::CXXDynamicCastExprClass:
2773 case Expr::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002774 case Expr::CXXUuidofExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002775 case Expr::CXXNullPtrLiteralExprClass:
2776 case Expr::CXXThisExprClass:
2777 case Expr::CXXThrowExprClass:
2778 case Expr::CXXNewExprClass:
2779 case Expr::CXXDeleteExprClass:
2780 case Expr::CXXPseudoDestructorExprClass:
2781 case Expr::UnresolvedLookupExprClass:
2782 case Expr::DependentScopeDeclRefExprClass:
2783 case Expr::CXXConstructExprClass:
2784 case Expr::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002785 case Expr::ExprWithCleanupsClass:
John McCalld905f5a2010-05-07 05:32:02 +00002786 case Expr::CXXTemporaryObjectExprClass:
2787 case Expr::CXXUnresolvedConstructExprClass:
2788 case Expr::CXXDependentScopeMemberExprClass:
2789 case Expr::UnresolvedMemberExprClass:
2790 case Expr::ObjCStringLiteralClass:
2791 case Expr::ObjCEncodeExprClass:
2792 case Expr::ObjCMessageExprClass:
2793 case Expr::ObjCSelectorExprClass:
2794 case Expr::ObjCProtocolExprClass:
2795 case Expr::ObjCIvarRefExprClass:
2796 case Expr::ObjCPropertyRefExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002797 case Expr::ObjCIsaExprClass:
2798 case Expr::ShuffleVectorExprClass:
2799 case Expr::BlockExprClass:
2800 case Expr::BlockDeclRefExprClass:
2801 case Expr::NoStmtClass:
John McCall7cd7d1a2010-11-15 23:31:06 +00002802 case Expr::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +00002803 case Expr::PackExpansionExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +00002804 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002805 return ICEDiag(2, E->getLocStart());
2806
Douglas Gregoree8aff02011-01-04 17:33:58 +00002807 case Expr::SizeOfPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002808 case Expr::GNUNullExprClass:
2809 // GCC considers the GNU __null value to be an integral constant expression.
2810 return NoDiag();
2811
2812 case Expr::ParenExprClass:
2813 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2814 case Expr::IntegerLiteralClass:
2815 case Expr::CharacterLiteralClass:
2816 case Expr::CXXBoolLiteralExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +00002817 case Expr::CXXScalarValueInitExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002818 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002819 case Expr::BinaryTypeTraitExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002820 case Expr::CXXNoexceptExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002821 return NoDiag();
2822 case Expr::CallExprClass:
Sean Hunt6cf75022010-08-30 17:47:05 +00002823 case Expr::CXXOperatorCallExprClass: {
John McCalld905f5a2010-05-07 05:32:02 +00002824 const CallExpr *CE = cast<CallExpr>(E);
2825 if (CE->isBuiltinCall(Ctx))
2826 return CheckEvalInICE(E, Ctx);
2827 return ICEDiag(2, E->getLocStart());
2828 }
2829 case Expr::DeclRefExprClass:
2830 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2831 return NoDiag();
2832 if (Ctx.getLangOptions().CPlusPlus &&
2833 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2834 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2835
2836 // Parameter variables are never constants. Without this check,
2837 // getAnyInitializer() can find a default argument, which leads
2838 // to chaos.
2839 if (isa<ParmVarDecl>(D))
2840 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2841
2842 // C++ 7.1.5.1p2
2843 // A variable of non-volatile const-qualified integral or enumeration
2844 // type initialized by an ICE can be used in ICEs.
2845 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2846 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2847 if (Quals.hasVolatile() || !Quals.hasConst())
2848 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2849
2850 // Look for a declaration of this variable that has an initializer.
2851 const VarDecl *ID = 0;
2852 const Expr *Init = Dcl->getAnyInitializer(ID);
2853 if (Init) {
2854 if (ID->isInitKnownICE()) {
2855 // We have already checked whether this subexpression is an
2856 // integral constant expression.
2857 if (ID->isInitICE())
2858 return NoDiag();
2859 else
2860 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2861 }
2862
2863 // It's an ICE whether or not the definition we found is
2864 // out-of-line. See DR 721 and the discussion in Clang PR
2865 // 6206 for details.
2866
2867 if (Dcl->isCheckingICE()) {
2868 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2869 }
2870
2871 Dcl->setCheckingICE();
2872 ICEDiag Result = CheckICE(Init, Ctx);
2873 // Cache the result of the ICE test.
2874 Dcl->setInitKnownICE(Result.Val == 0);
2875 return Result;
2876 }
2877 }
2878 }
2879 return ICEDiag(2, E->getLocStart());
2880 case Expr::UnaryOperatorClass: {
2881 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2882 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002883 case UO_PostInc:
2884 case UO_PostDec:
2885 case UO_PreInc:
2886 case UO_PreDec:
2887 case UO_AddrOf:
2888 case UO_Deref:
John McCalld905f5a2010-05-07 05:32:02 +00002889 return ICEDiag(2, E->getLocStart());
John McCall2de56d12010-08-25 11:45:40 +00002890 case UO_Extension:
2891 case UO_LNot:
2892 case UO_Plus:
2893 case UO_Minus:
2894 case UO_Not:
2895 case UO_Real:
2896 case UO_Imag:
John McCalld905f5a2010-05-07 05:32:02 +00002897 return CheckICE(Exp->getSubExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002898 }
2899
2900 // OffsetOf falls through here.
2901 }
2902 case Expr::OffsetOfExprClass: {
2903 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2904 // Evaluate matches the proposed gcc behavior for cases like
2905 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2906 // compliance: we should warn earlier for offsetof expressions with
2907 // array subscripts that aren't ICEs, and if the array subscripts
2908 // are ICEs, the value of the offsetof must be an integer constant.
2909 return CheckEvalInICE(E, Ctx);
2910 }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002911 case Expr::UnaryExprOrTypeTraitExprClass: {
2912 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
2913 if ((Exp->getKind() == UETT_SizeOf) &&
2914 Exp->getTypeOfArgument()->isVariableArrayType())
John McCalld905f5a2010-05-07 05:32:02 +00002915 return ICEDiag(2, E->getLocStart());
2916 return NoDiag();
2917 }
2918 case Expr::BinaryOperatorClass: {
2919 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2920 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002921 case BO_PtrMemD:
2922 case BO_PtrMemI:
2923 case BO_Assign:
2924 case BO_MulAssign:
2925 case BO_DivAssign:
2926 case BO_RemAssign:
2927 case BO_AddAssign:
2928 case BO_SubAssign:
2929 case BO_ShlAssign:
2930 case BO_ShrAssign:
2931 case BO_AndAssign:
2932 case BO_XorAssign:
2933 case BO_OrAssign:
John McCalld905f5a2010-05-07 05:32:02 +00002934 return ICEDiag(2, E->getLocStart());
2935
John McCall2de56d12010-08-25 11:45:40 +00002936 case BO_Mul:
2937 case BO_Div:
2938 case BO_Rem:
2939 case BO_Add:
2940 case BO_Sub:
2941 case BO_Shl:
2942 case BO_Shr:
2943 case BO_LT:
2944 case BO_GT:
2945 case BO_LE:
2946 case BO_GE:
2947 case BO_EQ:
2948 case BO_NE:
2949 case BO_And:
2950 case BO_Xor:
2951 case BO_Or:
2952 case BO_Comma: {
John McCalld905f5a2010-05-07 05:32:02 +00002953 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2954 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
John McCall2de56d12010-08-25 11:45:40 +00002955 if (Exp->getOpcode() == BO_Div ||
2956 Exp->getOpcode() == BO_Rem) {
John McCalld905f5a2010-05-07 05:32:02 +00002957 // Evaluate gives an error for undefined Div/Rem, so make sure
2958 // we don't evaluate one.
John McCall3b332ab2011-02-26 08:27:17 +00002959 if (LHSResult.Val == 0 && RHSResult.Val == 0) {
John McCalld905f5a2010-05-07 05:32:02 +00002960 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2961 if (REval == 0)
2962 return ICEDiag(1, E->getLocStart());
2963 if (REval.isSigned() && REval.isAllOnesValue()) {
2964 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2965 if (LEval.isMinSignedValue())
2966 return ICEDiag(1, E->getLocStart());
2967 }
2968 }
2969 }
John McCall2de56d12010-08-25 11:45:40 +00002970 if (Exp->getOpcode() == BO_Comma) {
John McCalld905f5a2010-05-07 05:32:02 +00002971 if (Ctx.getLangOptions().C99) {
2972 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2973 // if it isn't evaluated.
2974 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2975 return ICEDiag(1, E->getLocStart());
2976 } else {
2977 // In both C89 and C++, commas in ICEs are illegal.
2978 return ICEDiag(2, E->getLocStart());
2979 }
2980 }
2981 if (LHSResult.Val >= RHSResult.Val)
2982 return LHSResult;
2983 return RHSResult;
2984 }
John McCall2de56d12010-08-25 11:45:40 +00002985 case BO_LAnd:
2986 case BO_LOr: {
John McCalld905f5a2010-05-07 05:32:02 +00002987 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2988 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2989 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2990 // Rare case where the RHS has a comma "side-effect"; we need
2991 // to actually check the condition to see whether the side
2992 // with the comma is evaluated.
John McCall2de56d12010-08-25 11:45:40 +00002993 if ((Exp->getOpcode() == BO_LAnd) !=
John McCalld905f5a2010-05-07 05:32:02 +00002994 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2995 return RHSResult;
2996 return NoDiag();
2997 }
2998
2999 if (LHSResult.Val >= RHSResult.Val)
3000 return LHSResult;
3001 return RHSResult;
3002 }
3003 }
3004 }
3005 case Expr::ImplicitCastExprClass:
3006 case Expr::CStyleCastExprClass:
3007 case Expr::CXXFunctionalCastExprClass:
3008 case Expr::CXXStaticCastExprClass:
3009 case Expr::CXXReinterpretCastExprClass:
3010 case Expr::CXXConstCastExprClass: {
3011 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003012 if (SubExpr->getType()->isIntegralOrEnumerationType())
John McCalld905f5a2010-05-07 05:32:02 +00003013 return CheckICE(SubExpr, Ctx);
3014 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
3015 return NoDiag();
3016 return ICEDiag(2, E->getLocStart());
3017 }
John McCall56ca35d2011-02-17 10:25:35 +00003018 case Expr::BinaryConditionalOperatorClass: {
3019 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
3020 ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
3021 if (CommonResult.Val == 2) return CommonResult;
3022 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3023 if (FalseResult.Val == 2) return FalseResult;
3024 if (CommonResult.Val == 1) return CommonResult;
3025 if (FalseResult.Val == 1 &&
3026 Exp->getCommon()->EvaluateAsInt(Ctx) == 0) return NoDiag();
3027 return FalseResult;
3028 }
John McCalld905f5a2010-05-07 05:32:02 +00003029 case Expr::ConditionalOperatorClass: {
3030 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
3031 // If the condition (ignoring parens) is a __builtin_constant_p call,
3032 // then only the true side is actually considered in an integer constant
3033 // expression, and it is fully evaluated. This is an important GNU
3034 // extension. See GCC PR38377 for discussion.
3035 if (const CallExpr *CallCE
3036 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
3037 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
3038 Expr::EvalResult EVResult;
3039 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
3040 !EVResult.Val.isInt()) {
3041 return ICEDiag(2, E->getLocStart());
3042 }
3043 return NoDiag();
3044 }
3045 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
3046 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
3047 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3048 if (CondResult.Val == 2)
3049 return CondResult;
3050 if (TrueResult.Val == 2)
3051 return TrueResult;
3052 if (FalseResult.Val == 2)
3053 return FalseResult;
3054 if (CondResult.Val == 1)
3055 return CondResult;
3056 if (TrueResult.Val == 0 && FalseResult.Val == 0)
3057 return NoDiag();
3058 // Rare case where the diagnostics depend on which side is evaluated
3059 // Note that if we get here, CondResult is 0, and at least one of
3060 // TrueResult and FalseResult is non-zero.
3061 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
3062 return FalseResult;
3063 }
3064 return TrueResult;
3065 }
3066 case Expr::CXXDefaultArgExprClass:
3067 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
3068 case Expr::ChooseExprClass: {
3069 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
3070 }
3071 }
3072
3073 // Silence a GCC warning
3074 return ICEDiag(2, E->getLocStart());
3075}
3076
3077bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
3078 SourceLocation *Loc, bool isEvaluated) const {
3079 ICEDiag d = CheckICE(this, Ctx);
3080 if (d.Val != 0) {
3081 if (Loc) *Loc = d.Loc;
3082 return false;
3083 }
3084 EvalResult EvalResult;
3085 if (!Evaluate(EvalResult, Ctx))
3086 llvm_unreachable("ICE cannot be evaluated!");
3087 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
3088 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
3089 Result = EvalResult.Val.getInt();
3090 return true;
3091}