blob: b0af5f8c91dfcc8c6709dc1e5959fb1dddad7b65 [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 {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000105 const Expr *Base;
John McCallefdb83e2010-05-07 21:00:08 +0000106 CharUnits Offset;
107
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000108 const Expr *getLValueBase() { return Base; }
John McCallefdb83e2010-05-07 21:00:08 +0000109 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
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000265 : public ConstStmtVisitor<HasSideEffect, bool> {
Mike Stumpc4c90452009-10-27 22:09:17 +0000266 EvalInfo &Info;
267public:
268
269 HasSideEffect(EvalInfo &info) : Info(info) {}
270
271 // Unhandled nodes conservatively default to having side effects.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000272 bool VisitStmt(const Stmt *S) {
Mike Stumpc4c90452009-10-27 22:09:17 +0000273 return true;
274 }
275
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000276 bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
277 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
Peter Collingbournef111d932011-04-15 00:35:48 +0000278 return Visit(E->getResultExpr());
279 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000280 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000281 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000282 return true;
283 return false;
284 }
285 // We don't want to evaluate BlockExprs multiple times, as they generate
286 // a ton of code.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000287 bool VisitBlockExpr(const BlockExpr *E) { return true; }
288 bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
289 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
Mike Stumpc4c90452009-10-27 22:09:17 +0000290 { return Visit(E->getInitializer()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000291 bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
292 bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
293 bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
294 bool VisitStringLiteral(const StringLiteral *E) { return false; }
295 bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
296 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000297 { return false; }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000298 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000299 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000300 bool VisitChooseExpr(const ChooseExpr *E)
Mike Stumpc4c90452009-10-27 22:09:17 +0000301 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000302 bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
303 bool VisitBinAssign(const BinaryOperator *E) { return true; }
304 bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
305 bool VisitBinaryOperator(const BinaryOperator *E)
Mike Stump980ca222009-10-29 20:48:09 +0000306 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000307 bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
308 bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
309 bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
310 bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
311 bool VisitUnaryDeref(const UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000312 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000313 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000314 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000315 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000316 bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000317
318 // Has side effects if any element does.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000319 bool VisitInitListExpr(const InitListExpr *E) {
Chris Lattner363ff232010-04-13 17:34:23 +0000320 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
321 if (Visit(E->getInit(i))) return true;
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000322 if (const Expr *filler = E->getArrayFiller())
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +0000323 return Visit(filler);
Chris Lattner363ff232010-04-13 17:34:23 +0000324 return false;
325 }
Douglas Gregoree8aff02011-01-04 17:33:58 +0000326
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000327 bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000328};
329
John McCall56ca35d2011-02-17 10:25:35 +0000330class OpaqueValueEvaluation {
331 EvalInfo &info;
332 OpaqueValueExpr *opaqueValue;
333
334public:
335 OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
336 Expr *value)
337 : info(info), opaqueValue(opaqueValue) {
338
339 // If evaluation fails, fail immediately.
340 if (!Evaluate(info, value)) {
341 this->opaqueValue = 0;
342 return;
343 }
344 info.OpaqueValues[opaqueValue] = info.EvalResult.Val;
345 }
346
347 bool hasError() const { return opaqueValue == 0; }
348
349 ~OpaqueValueEvaluation() {
350 if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
351 }
352};
353
Mike Stumpc4c90452009-10-27 22:09:17 +0000354} // end anonymous namespace
355
Eli Friedman4efaa272008-11-12 09:44:48 +0000356//===----------------------------------------------------------------------===//
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000357// Generic Evaluation
358//===----------------------------------------------------------------------===//
359namespace {
360
361template <class Derived, typename RetTy=void>
362class ExprEvaluatorBase
363 : public ConstStmtVisitor<Derived, RetTy> {
364private:
365 RetTy DerivedSuccess(const APValue &V, const Expr *E) {
366 return static_cast<Derived*>(this)->Success(V, E);
367 }
368 RetTy DerivedError(const Expr *E) {
369 return static_cast<Derived*>(this)->Error(E);
370 }
371
372protected:
373 EvalInfo &Info;
374 typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
375 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
376
377public:
378 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
379
380 RetTy VisitStmt(const Stmt *) {
381 assert(0 && "Expression evaluator should not be called on stmts");
382 return DerivedError(0);
383 }
384 RetTy VisitExpr(const Expr *E) {
385 return DerivedError(E);
386 }
387
388 RetTy VisitParenExpr(const ParenExpr *E)
389 { return StmtVisitorTy::Visit(E->getSubExpr()); }
390 RetTy VisitUnaryExtension(const UnaryOperator *E)
391 { return StmtVisitorTy::Visit(E->getSubExpr()); }
392 RetTy VisitUnaryPlus(const UnaryOperator *E)
393 { return StmtVisitorTy::Visit(E->getSubExpr()); }
394 RetTy VisitChooseExpr(const ChooseExpr *E)
395 { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
396 RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
397 { return StmtVisitorTy::Visit(E->getResultExpr()); }
398
399 RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
400 OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());
401 if (opaque.hasError())
402 return DerivedError(E);
403
404 bool cond;
405 if (!HandleConversionToBool(E->getCond(), cond, Info))
406 return DerivedError(E);
407
408 return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr());
409 }
410
411 RetTy VisitConditionalOperator(const ConditionalOperator *E) {
412 bool BoolResult;
413 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
414 return DerivedError(E);
415
416 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
417 return StmtVisitorTy::Visit(EvalExpr);
418 }
419
420 RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
421 const APValue *value = Info.getOpaqueValue(E);
422 if (!value)
423 return (E->getSourceExpr() ? StmtVisitorTy::Visit(E->getSourceExpr())
424 : DerivedError(E));
425 return DerivedSuccess(*value, E);
426 }
427};
428
429}
430
431//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000432// LValue Evaluation
433//===----------------------------------------------------------------------===//
434namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000435class LValueExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000436 : public ExprEvaluatorBase<LValueExprEvaluator, bool> {
John McCallefdb83e2010-05-07 21:00:08 +0000437 LValue &Result;
438
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000439 bool Success(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000440 Result.Base = E;
441 Result.Offset = CharUnits::Zero();
442 return true;
443 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000444public:
Mike Stump1eb44332009-09-09 15:08:12 +0000445
John McCallefdb83e2010-05-07 21:00:08 +0000446 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000447 ExprEvaluatorBaseTy(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000448
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000449 bool Success(const APValue &V, const Expr *E) {
450 Result.setFrom(V);
451 return true;
452 }
453 bool Error(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000454 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000455 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000456
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000457 bool VisitDeclRefExpr(const DeclRefExpr *E);
458 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
459 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
460 bool VisitMemberExpr(const MemberExpr *E);
461 bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
462 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
463 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
464 bool VisitUnaryDeref(const UnaryOperator *E);
Anders Carlsson26bc2202009-10-03 16:30:22 +0000465
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000466 bool VisitCastExpr(const CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000467 switch (E->getCastKind()) {
468 default:
John McCallefdb83e2010-05-07 21:00:08 +0000469 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000470
John McCall2de56d12010-08-25 11:45:40 +0000471 case CK_NoOp:
Anders Carlsson26bc2202009-10-03 16:30:22 +0000472 return Visit(E->getSubExpr());
473 }
474 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000475 // FIXME: Missing: __real__, __imag__
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000476
Eli Friedman4efaa272008-11-12 09:44:48 +0000477};
478} // end anonymous namespace
479
John McCallefdb83e2010-05-07 21:00:08 +0000480static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000481 return LValueExprEvaluator(Info, Result).Visit(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000482}
483
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000484bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000485 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000486 return Success(E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000487 } else if (const VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000488 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000489 return Success(E);
Chandler Carruth761c94e2010-05-16 09:32:51 +0000490 // Reference parameters can refer to anything even if they have an
491 // "initializer" in the form of a default argument.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000492 if (!isa<ParmVarDecl>(VD))
493 // FIXME: Check whether VD might be overridden!
494 if (const Expr *Init = VD->getAnyInitializer())
495 return Visit(Init);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000496 }
497
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000498 return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
Anders Carlsson35873c42008-11-24 04:41:22 +0000499}
500
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000501bool
502LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000503 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000504}
505
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000506bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000507 QualType Ty;
508 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000509 if (!EvaluatePointer(E->getBase(), Result, Info))
510 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000511 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000512 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000513 if (!Visit(E->getBase()))
514 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000515 Ty = E->getBase()->getType();
516 }
517
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000518 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000519 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000520
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000521 const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Douglas Gregor86f19402008-12-20 23:49:58 +0000522 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000523 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000524
525 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000526 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000527
Eli Friedman4efaa272008-11-12 09:44:48 +0000528 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000529 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000530 for (RecordDecl::field_iterator Field = RD->field_begin(),
531 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000532 Field != FieldEnd; (void)++Field, ++i) {
533 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000534 break;
535 }
536
Ken Dyckfb1e3bc2011-01-18 01:56:16 +0000537 Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
John McCallefdb83e2010-05-07 21:00:08 +0000538 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000539}
540
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000541bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000542 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000543 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Anders Carlsson3068d112008-11-16 19:01:22 +0000545 APSInt Index;
546 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000547 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000548
Ken Dyck199c3d62010-01-11 17:06:35 +0000549 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000550 Result.Offset += Index.getSExtValue() * ElementSize;
551 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000552}
Eli Friedman4efaa272008-11-12 09:44:48 +0000553
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000554bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000555 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000556}
557
Eli Friedman4efaa272008-11-12 09:44:48 +0000558//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000559// Pointer Evaluation
560//===----------------------------------------------------------------------===//
561
Anders Carlssonc754aa62008-07-08 05:13:58 +0000562namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000563class PointerExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000564 : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
John McCallefdb83e2010-05-07 21:00:08 +0000565 LValue &Result;
566
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000567 bool Success(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000568 Result.Base = E;
569 Result.Offset = CharUnits::Zero();
570 return true;
571 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000572public:
Mike Stump1eb44332009-09-09 15:08:12 +0000573
John McCallefdb83e2010-05-07 21:00:08 +0000574 PointerExprEvaluator(EvalInfo &info, LValue &Result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000575 : ExprEvaluatorBaseTy(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000576
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000577 bool Success(const APValue &V, const Expr *E) {
578 Result.setFrom(V);
579 return true;
580 }
581 bool Error(const Stmt *S) {
John McCallefdb83e2010-05-07 21:00:08 +0000582 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000583 }
584
John McCallefdb83e2010-05-07 21:00:08 +0000585 bool VisitBinaryOperator(const BinaryOperator *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000586 bool VisitCastExpr(const CastExpr* E);
John McCallefdb83e2010-05-07 21:00:08 +0000587 bool VisitUnaryAddrOf(const UnaryOperator *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000588 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
John McCallefdb83e2010-05-07 21:00:08 +0000589 { return Success(E); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000590 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000591 { return Success(E); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000592 bool VisitCallExpr(const CallExpr *E);
593 bool VisitBlockExpr(const BlockExpr *E) {
John McCall469a1eb2011-02-02 13:00:07 +0000594 if (!E->getBlockDecl()->hasCaptures())
John McCallefdb83e2010-05-07 21:00:08 +0000595 return Success(E);
596 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000597 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000598 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000599 { return Success((Expr*)0); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000600 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000601 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000602
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000603 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000604};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000605} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000606
John McCallefdb83e2010-05-07 21:00:08 +0000607static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000608 assert(E->getType()->hasPointerRepresentation());
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000609 return PointerExprEvaluator(Info, Result).Visit(E);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000610}
611
John McCallefdb83e2010-05-07 21:00:08 +0000612bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000613 if (E->getOpcode() != BO_Add &&
614 E->getOpcode() != BO_Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000615 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000617 const Expr *PExp = E->getLHS();
618 const Expr *IExp = E->getRHS();
619 if (IExp->getType()->isPointerType())
620 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
John McCallefdb83e2010-05-07 21:00:08 +0000622 if (!EvaluatePointer(PExp, Result, Info))
623 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
John McCallefdb83e2010-05-07 21:00:08 +0000625 llvm::APSInt Offset;
626 if (!EvaluateInteger(IExp, Offset, Info))
627 return false;
628 int64_t AdditionalOffset
629 = Offset.isSigned() ? Offset.getSExtValue()
630 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000631
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000632 // Compute the new offset in the appropriate width.
633
634 QualType PointeeType =
635 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000636 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000638 // Explicitly handle GNU void* and function pointer arithmetic extensions.
639 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000640 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000641 else
John McCallefdb83e2010-05-07 21:00:08 +0000642 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000643
John McCall2de56d12010-08-25 11:45:40 +0000644 if (E->getOpcode() == BO_Add)
John McCallefdb83e2010-05-07 21:00:08 +0000645 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000646 else
John McCallefdb83e2010-05-07 21:00:08 +0000647 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000648
John McCallefdb83e2010-05-07 21:00:08 +0000649 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000650}
Eli Friedman4efaa272008-11-12 09:44:48 +0000651
John McCallefdb83e2010-05-07 21:00:08 +0000652bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
653 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000654}
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000656
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000657bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
658 const Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000659
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000660 switch (E->getCastKind()) {
661 default:
662 break;
663
John McCall2de56d12010-08-25 11:45:40 +0000664 case CK_NoOp:
665 case CK_BitCast:
John McCall2de56d12010-08-25 11:45:40 +0000666 case CK_AnyPointerToObjCPointerCast:
667 case CK_AnyPointerToBlockPointerCast:
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000668 return Visit(SubExpr);
669
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000670 case CK_DerivedToBase:
671 case CK_UncheckedDerivedToBase: {
672 LValue BaseLV;
673 if (!EvaluatePointer(E->getSubExpr(), BaseLV, Info))
674 return false;
675
676 // Now figure out the necessary offset to add to the baseLV to get from
677 // the derived class to the base class.
Ken Dyck7c7f8202011-01-26 02:17:08 +0000678 CharUnits Offset = CharUnits::Zero();
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000679
680 QualType Ty = E->getSubExpr()->getType();
681 const CXXRecordDecl *DerivedDecl =
682 Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl();
683
684 for (CastExpr::path_const_iterator PathI = E->path_begin(),
685 PathE = E->path_end(); PathI != PathE; ++PathI) {
686 const CXXBaseSpecifier *Base = *PathI;
687
688 // FIXME: If the base is virtual, we'd need to determine the type of the
689 // most derived class and we don't support that right now.
690 if (Base->isVirtual())
691 return false;
692
693 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
694 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
695
Ken Dyck7c7f8202011-01-26 02:17:08 +0000696 Offset += Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000697 DerivedDecl = BaseDecl;
698 }
699
700 Result.Base = BaseLV.getLValueBase();
Ken Dyck7c7f8202011-01-26 02:17:08 +0000701 Result.Offset = BaseLV.getLValueOffset() + Offset;
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000702 return true;
703 }
704
John McCall404cd162010-11-13 01:35:44 +0000705 case CK_NullToPointer: {
706 Result.Base = 0;
707 Result.Offset = CharUnits::Zero();
708 return true;
709 }
710
John McCall2de56d12010-08-25 11:45:40 +0000711 case CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000712 APValue Value;
713 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000714 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000715
John McCallefdb83e2010-05-07 21:00:08 +0000716 if (Value.isInt()) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000717 Value.getInt() = Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
John McCallefdb83e2010-05-07 21:00:08 +0000718 Result.Base = 0;
719 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
720 return true;
721 } else {
722 // Cast is of an lvalue, no need to change value.
723 Result.Base = Value.getLValueBase();
724 Result.Offset = Value.getLValueOffset();
725 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000726 }
727 }
John McCall2de56d12010-08-25 11:45:40 +0000728 case CK_ArrayToPointerDecay:
729 case CK_FunctionToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000730 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000731 }
732
John McCallefdb83e2010-05-07 21:00:08 +0000733 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000734}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000735
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000736bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000737 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000738 Builtin::BI__builtin___CFStringMakeConstantString ||
739 E->isBuiltinCall(Info.Ctx) ==
740 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000741 return Success(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000742
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000743 return ExprEvaluatorBaseTy::VisitCallExpr(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000744}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000745
746//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000747// Vector Evaluation
748//===----------------------------------------------------------------------===//
749
750namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000751 class VectorExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000752 : public ExprEvaluatorBase<VectorExprEvaluator, APValue> {
Eli Friedman91110ee2009-02-23 04:23:56 +0000753 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000754 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000756 VectorExprEvaluator(EvalInfo &info) : ExprEvaluatorBaseTy(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000758 APValue Success(const APValue &V, const Expr *E) { return V; }
759 APValue Error(const Expr *E) { return APValue(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Eli Friedman91110ee2009-02-23 04:23:56 +0000761 APValue VisitUnaryReal(const UnaryOperator *E)
762 { return Visit(E->getSubExpr()); }
763 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
764 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000765 APValue VisitCastExpr(const CastExpr* E);
766 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
767 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000768 APValue VisitUnaryImag(const UnaryOperator *E);
769 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000770 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000771 // shufflevector, ExtVectorElementExpr
772 // (Note that these require implementing conversions
773 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000774 };
775} // end anonymous namespace
776
777static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
778 if (!E->getType()->isVectorType())
779 return false;
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000780 Result = VectorExprEvaluator(Info).Visit(E);
Nate Begeman59b5da62009-01-18 03:20:47 +0000781 return !Result.isUninit();
782}
783
784APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000785 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000786 QualType EltTy = VTy->getElementType();
787 unsigned NElts = VTy->getNumElements();
788 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Nate Begeman59b5da62009-01-18 03:20:47 +0000790 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000791 QualType SETy = SE->getType();
Nate Begeman59b5da62009-01-18 03:20:47 +0000792
Eli Friedman46a52322011-03-25 00:43:55 +0000793 switch (E->getCastKind()) {
794 case CK_VectorSplat: {
795 APValue Result = APValue();
796 if (SETy->isIntegerType()) {
797 APSInt IntResult;
798 if (!EvaluateInteger(SE, IntResult, Info))
799 return APValue();
800 Result = APValue(IntResult);
801 } else if (SETy->isRealFloatingType()) {
802 APFloat F(0.0);
803 if (!EvaluateFloat(SE, F, Info))
804 return APValue();
805 Result = APValue(F);
806 } else {
Anders Carlsson0254e702011-03-25 11:22:47 +0000807 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000808 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000809
810 // Splat and create vector APValue.
811 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
812 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000813 }
Eli Friedman46a52322011-03-25 00:43:55 +0000814 case CK_BitCast: {
815 if (SETy->isVectorType())
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000816 return Visit(SE);
Nate Begemanc0b8b192009-07-01 07:50:47 +0000817
Eli Friedman46a52322011-03-25 00:43:55 +0000818 if (!SETy->isIntegerType())
Anders Carlsson0254e702011-03-25 11:22:47 +0000819 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Eli Friedman46a52322011-03-25 00:43:55 +0000821 APSInt Init;
822 if (!EvaluateInteger(SE, Init, Info))
Nate Begemanc0b8b192009-07-01 07:50:47 +0000823 return APValue();
824
Eli Friedman46a52322011-03-25 00:43:55 +0000825 assert((EltTy->isIntegerType() || EltTy->isRealFloatingType()) &&
826 "Vectors must be composed of ints or floats");
827
828 llvm::SmallVector<APValue, 4> Elts;
829 for (unsigned i = 0; i != NElts; ++i) {
830 APSInt Tmp = Init.extOrTrunc(EltWidth);
831
832 if (EltTy->isIntegerType())
833 Elts.push_back(APValue(Tmp));
834 else
835 Elts.push_back(APValue(APFloat(Tmp)));
836
837 Init >>= EltWidth;
838 }
839 return APValue(&Elts[0], Elts.size());
Nate Begemanc0b8b192009-07-01 07:50:47 +0000840 }
Eli Friedman46a52322011-03-25 00:43:55 +0000841 case CK_LValueToRValue:
842 case CK_NoOp:
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000843 return Visit(SE);
Eli Friedman46a52322011-03-25 00:43:55 +0000844 default:
Anders Carlsson0254e702011-03-25 11:22:47 +0000845 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000846 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000847}
848
Mike Stump1eb44332009-09-09 15:08:12 +0000849APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000850VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000851 return this->Visit(E->getInitializer());
Nate Begeman59b5da62009-01-18 03:20:47 +0000852}
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000855VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000856 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000857 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000858 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Nate Begeman59b5da62009-01-18 03:20:47 +0000860 QualType EltTy = VT->getElementType();
861 llvm::SmallVector<APValue, 4> Elements;
862
John McCalla7d6c222010-06-11 17:54:15 +0000863 // If a vector is initialized with a single element, that value
864 // becomes every element of the vector, not just the first.
865 // This is the behavior described in the IBM AltiVec documentation.
866 if (NumInits == 1) {
Tanya Lattnerb92ae0e2011-04-15 22:42:59 +0000867
868 // Handle the case where the vector is initialized by a another
869 // vector (OpenCL 6.1.6).
870 if (E->getInit(0)->getType()->isVectorType())
871 return this->Visit(const_cast<Expr*>(E->getInit(0)));
872
John McCalla7d6c222010-06-11 17:54:15 +0000873 APValue InitValue;
Nate Begeman59b5da62009-01-18 03:20:47 +0000874 if (EltTy->isIntegerType()) {
875 llvm::APSInt sInt(32);
John McCalla7d6c222010-06-11 17:54:15 +0000876 if (!EvaluateInteger(E->getInit(0), sInt, Info))
877 return APValue();
878 InitValue = APValue(sInt);
Nate Begeman59b5da62009-01-18 03:20:47 +0000879 } else {
880 llvm::APFloat f(0.0);
John McCalla7d6c222010-06-11 17:54:15 +0000881 if (!EvaluateFloat(E->getInit(0), f, Info))
882 return APValue();
883 InitValue = APValue(f);
884 }
885 for (unsigned i = 0; i < NumElements; i++) {
886 Elements.push_back(InitValue);
887 }
888 } else {
889 for (unsigned i = 0; i < NumElements; i++) {
890 if (EltTy->isIntegerType()) {
891 llvm::APSInt sInt(32);
892 if (i < NumInits) {
893 if (!EvaluateInteger(E->getInit(i), sInt, Info))
894 return APValue();
895 } else {
896 sInt = Info.Ctx.MakeIntValue(0, EltTy);
897 }
898 Elements.push_back(APValue(sInt));
Eli Friedman91110ee2009-02-23 04:23:56 +0000899 } else {
John McCalla7d6c222010-06-11 17:54:15 +0000900 llvm::APFloat f(0.0);
901 if (i < NumInits) {
902 if (!EvaluateFloat(E->getInit(i), f, Info))
903 return APValue();
904 } else {
905 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
906 }
907 Elements.push_back(APValue(f));
Eli Friedman91110ee2009-02-23 04:23:56 +0000908 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000909 }
910 }
911 return APValue(&Elements[0], Elements.size());
912}
913
Mike Stump1eb44332009-09-09 15:08:12 +0000914APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000915VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000916 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000917 QualType EltTy = VT->getElementType();
918 APValue ZeroElement;
919 if (EltTy->isIntegerType())
920 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
921 else
922 ZeroElement =
923 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
924
925 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
926 return APValue(&Elements[0], Elements.size());
927}
928
Eli Friedman91110ee2009-02-23 04:23:56 +0000929APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
930 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
931 Info.EvalResult.HasSideEffects = true;
932 return GetZeroVector(E->getType());
933}
934
Nate Begeman59b5da62009-01-18 03:20:47 +0000935//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000936// Integer Evaluation
937//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000938
939namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000940class IntExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000941 : public ExprEvaluatorBase<IntExprEvaluator, bool> {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000942 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000943public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000944 IntExprEvaluator(EvalInfo &info, APValue &result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000945 : ExprEvaluatorBaseTy(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000946
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000947 bool Success(const llvm::APSInt &SI, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000948 assert(E->getType()->isIntegralOrEnumerationType() &&
949 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000950 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000951 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000952 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000953 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000954 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000955 return true;
956 }
957
Daniel Dunbar131eb432009-02-19 09:06:44 +0000958 bool Success(const llvm::APInt &I, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000959 assert(E->getType()->isIntegralOrEnumerationType() &&
960 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000961 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000962 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000963 Result = APValue(APSInt(I));
964 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000965 return true;
966 }
967
968 bool Success(uint64_t Value, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000969 assert(E->getType()->isIntegralOrEnumerationType() &&
970 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000971 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000972 return true;
973 }
974
Ken Dyck4f3bc8f2011-03-11 02:13:43 +0000975 bool Success(CharUnits Size, const Expr *E) {
976 return Success(Size.getQuantity(), E);
977 }
978
979
Anders Carlsson82206e22008-11-30 18:14:57 +0000980 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000981 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000982 if (Info.EvalResult.Diag == 0) {
983 Info.EvalResult.DiagLoc = L;
984 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000985 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000986 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000987 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000990 bool Success(const APValue &V, const Expr *E) {
991 return Success(V.getInt(), E);
Chris Lattner32fea9d2008-11-12 07:43:42 +0000992 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000993 bool Error(const Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000994 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000997 //===--------------------------------------------------------------------===//
998 // Visitor Methods
999 //===--------------------------------------------------------------------===//
Anders Carlssonc754aa62008-07-08 05:13:58 +00001000
Chris Lattner4c4867e2008-07-12 00:38:25 +00001001 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001002 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001003 }
1004 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001005 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001006 }
Eli Friedman04309752009-11-24 05:28:59 +00001007
1008 bool CheckReferencedDecl(const Expr *E, const Decl *D);
1009 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001010 if (CheckReferencedDecl(E, E->getDecl()))
1011 return true;
1012
1013 return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
Eli Friedman04309752009-11-24 05:28:59 +00001014 }
1015 bool VisitMemberExpr(const MemberExpr *E) {
1016 if (CheckReferencedDecl(E, E->getMemberDecl())) {
1017 // Conservatively assume a MemberExpr will have side-effects
1018 Info.EvalResult.HasSideEffects = true;
1019 return true;
1020 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001021
1022 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
Eli Friedman04309752009-11-24 05:28:59 +00001023 }
1024
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001025 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001026 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001027 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001028 bool VisitUnaryOperator(const UnaryOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +00001029
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001030 bool VisitCastExpr(const CastExpr* E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001031 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Sebastian Redl05189992008-11-11 17:56:53 +00001032
Anders Carlsson3068d112008-11-16 19:01:22 +00001033 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001034 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Anders Carlsson3f704562008-12-21 22:39:40 +00001037 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001038 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +00001039 }
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregored8abf12010-07-08 06:14:04 +00001041 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001042 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001043 }
1044
Eli Friedman664a1042009-02-27 04:45:43 +00001045 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
1046 return Success(0, E);
1047 }
1048
Sebastian Redl64b45f72009-01-05 20:52:13 +00001049 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Sebastian Redl0dfd8482010-09-13 20:56:31 +00001050 return Success(E->getValue(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001051 }
1052
Francois Pichet6ad6f282010-12-07 00:08:36 +00001053 bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
1054 return Success(E->getValue(), E);
1055 }
1056
John Wiegley21ff2e52011-04-28 00:16:57 +00001057 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
1058 return Success(E->getValue(), E);
1059 }
1060
John Wiegley55262202011-04-25 06:54:41 +00001061 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
1062 return Success(E->getValue(), E);
1063 }
1064
Eli Friedman722c7172009-02-28 03:59:05 +00001065 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001066 bool VisitUnaryImag(const UnaryOperator *E);
1067
Sebastian Redl295995c2010-09-10 20:55:47 +00001068 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001069 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1070
Chris Lattnerfcee0012008-07-11 21:24:13 +00001071private:
Ken Dyck8b752f12010-01-27 17:10:57 +00001072 CharUnits GetAlignOfExpr(const Expr *E);
1073 CharUnits GetAlignOfType(QualType T);
John McCall42c8f872010-05-10 23:27:23 +00001074 static QualType GetObjectType(const Expr *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001075 bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001076 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001077};
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001078} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +00001079
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001080static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001081 assert(E->getType()->isIntegralOrEnumerationType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001082 return IntExprEvaluator(Info, Result).Visit(E);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001083}
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001084
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001085static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001086 assert(E->getType()->isIntegralOrEnumerationType());
John McCall7db7acb2010-05-07 05:46:35 +00001087
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001088 APValue Val;
1089 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
1090 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001091 Result = Val.getInt();
1092 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +00001093}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001094
Eli Friedman04309752009-11-24 05:28:59 +00001095bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001096 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +00001097 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
1098 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001099
1100 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +00001101 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001102 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
1103 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +00001104
1105 if (isa<ParmVarDecl>(D))
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001106 return false;
Anders Carlssonf6b60252010-02-03 21:58:41 +00001107
Eli Friedman04309752009-11-24 05:28:59 +00001108 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001109 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001110 if (APValue *V = VD->getEvaluatedValue()) {
1111 if (V->isInt())
1112 return Success(V->getInt(), E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001113 return false;
Eli Friedmanc0131182009-12-03 20:31:57 +00001114 }
1115
1116 if (VD->isEvaluatingValue())
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001117 return false;
Eli Friedmanc0131182009-12-03 20:31:57 +00001118
1119 VD->setEvaluatingValue();
1120
Eli Friedmana7dedf72010-09-06 00:10:32 +00001121 Expr::EvalResult EResult;
1122 if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
1123 EResult.Val.isInt()) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001124 // Cache the evaluated value in the variable declaration.
Eli Friedmana7dedf72010-09-06 00:10:32 +00001125 Result = EResult.Val;
Eli Friedmanc0131182009-12-03 20:31:57 +00001126 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +00001127 return true;
1128 }
1129
Eli Friedmanc0131182009-12-03 20:31:57 +00001130 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +00001131 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001132 }
1133 }
1134
Chris Lattner4c4867e2008-07-12 00:38:25 +00001135 // Otherwise, random variable references are not constants.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001136 return false;
Chris Lattner4c4867e2008-07-12 00:38:25 +00001137}
1138
Chris Lattnera4d55d82008-10-06 06:40:35 +00001139/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1140/// as GCC.
1141static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1142 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001143 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +00001144 enum gcc_type_class {
1145 no_type_class = -1,
1146 void_type_class, integer_type_class, char_type_class,
1147 enumeral_type_class, boolean_type_class,
1148 pointer_type_class, reference_type_class, offset_type_class,
1149 real_type_class, complex_type_class,
1150 function_type_class, method_type_class,
1151 record_type_class, union_type_class,
1152 array_type_class, string_type_class,
1153 lang_type_class
1154 };
Mike Stump1eb44332009-09-09 15:08:12 +00001155
1156 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001157 // ideal, however it is what gcc does.
1158 if (E->getNumArgs() == 0)
1159 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Chris Lattnera4d55d82008-10-06 06:40:35 +00001161 QualType ArgTy = E->getArg(0)->getType();
1162 if (ArgTy->isVoidType())
1163 return void_type_class;
1164 else if (ArgTy->isEnumeralType())
1165 return enumeral_type_class;
1166 else if (ArgTy->isBooleanType())
1167 return boolean_type_class;
1168 else if (ArgTy->isCharType())
1169 return string_type_class; // gcc doesn't appear to use char_type_class
1170 else if (ArgTy->isIntegerType())
1171 return integer_type_class;
1172 else if (ArgTy->isPointerType())
1173 return pointer_type_class;
1174 else if (ArgTy->isReferenceType())
1175 return reference_type_class;
1176 else if (ArgTy->isRealType())
1177 return real_type_class;
1178 else if (ArgTy->isComplexType())
1179 return complex_type_class;
1180 else if (ArgTy->isFunctionType())
1181 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001182 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001183 return record_type_class;
1184 else if (ArgTy->isUnionType())
1185 return union_type_class;
1186 else if (ArgTy->isArrayType())
1187 return array_type_class;
1188 else if (ArgTy->isUnionType())
1189 return union_type_class;
1190 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1191 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1192 return -1;
1193}
1194
John McCall42c8f872010-05-10 23:27:23 +00001195/// Retrieves the "underlying object type" of the given expression,
1196/// as used by __builtin_object_size.
1197QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1198 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1199 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1200 return VD->getType();
1201 } else if (isa<CompoundLiteralExpr>(E)) {
1202 return E->getType();
1203 }
1204
1205 return QualType();
1206}
1207
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001208bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
John McCall42c8f872010-05-10 23:27:23 +00001209 // TODO: Perhaps we should let LLVM lower this?
1210 LValue Base;
1211 if (!EvaluatePointer(E->getArg(0), Base, Info))
1212 return false;
1213
1214 // If we can prove the base is null, lower to zero now.
1215 const Expr *LVBase = Base.getLValueBase();
1216 if (!LVBase) return Success(0, E);
1217
1218 QualType T = GetObjectType(LVBase);
1219 if (T.isNull() ||
1220 T->isIncompleteType() ||
Eli Friedman13578692010-08-05 02:49:48 +00001221 T->isFunctionType() ||
John McCall42c8f872010-05-10 23:27:23 +00001222 T->isVariablyModifiedType() ||
1223 T->isDependentType())
1224 return false;
1225
1226 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1227 CharUnits Offset = Base.getLValueOffset();
1228
1229 if (!Offset.isNegative() && Offset <= Size)
1230 Size -= Offset;
1231 else
1232 Size = CharUnits::Zero();
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001233 return Success(Size, E);
John McCall42c8f872010-05-10 23:27:23 +00001234}
1235
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001236bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001237 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001238 default:
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001239 return ExprEvaluatorBaseTy::VisitCallExpr(E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001240
1241 case Builtin::BI__builtin_object_size: {
John McCall42c8f872010-05-10 23:27:23 +00001242 if (TryEvaluateBuiltinObjectSize(E))
1243 return true;
Mike Stump64eda9e2009-10-26 18:35:08 +00001244
Eric Christopherb2aaf512010-01-19 22:58:35 +00001245 // If evaluating the argument has side-effects we can't determine
1246 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001247 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001248 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001249 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001250 return Success(0, E);
1251 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001252
Mike Stump64eda9e2009-10-26 18:35:08 +00001253 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1254 }
1255
Chris Lattner019f4e82008-10-06 05:28:25 +00001256 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001257 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001259 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001260 // __builtin_constant_p always has one operand: it returns true if that
1261 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001262 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001263
1264 case Builtin::BI__builtin_eh_return_data_regno: {
1265 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1266 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1267 return Success(Operand, E);
1268 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001269
1270 case Builtin::BI__builtin_expect:
1271 return Visit(E->getArg(0));
Douglas Gregor5726d402010-09-10 06:27:15 +00001272
1273 case Builtin::BIstrlen:
1274 case Builtin::BI__builtin_strlen:
1275 // As an extension, we support strlen() and __builtin_strlen() as constant
1276 // expressions when the argument is a string literal.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001277 if (const StringLiteral *S
Douglas Gregor5726d402010-09-10 06:27:15 +00001278 = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
1279 // The string literal may have embedded null characters. Find the first
1280 // one and truncate there.
1281 llvm::StringRef Str = S->getString();
1282 llvm::StringRef::size_type Pos = Str.find(0);
1283 if (Pos != llvm::StringRef::npos)
1284 Str = Str.substr(0, Pos);
1285
1286 return Success(Str.size(), E);
1287 }
1288
1289 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +00001290 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001291}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001292
Chris Lattnerb542afe2008-07-11 19:10:17 +00001293bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001294 if (E->getOpcode() == BO_Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001295 if (!Visit(E->getRHS()))
1296 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001297
Eli Friedman33ef1452009-02-26 10:19:36 +00001298 // If we can't evaluate the LHS, it might have side effects;
1299 // conservatively mark it.
1300 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1301 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001302
Anders Carlsson027f62e2008-12-01 02:07:06 +00001303 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001304 }
1305
1306 if (E->isLogicalOp()) {
1307 // These need to be handled specially because the operands aren't
1308 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001309 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001311 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001312 // We were able to evaluate the LHS, see if we can get away with not
1313 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
John McCall2de56d12010-08-25 11:45:40 +00001314 if (lhsResult == (E->getOpcode() == BO_LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001315 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001316
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001317 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
John McCall2de56d12010-08-25 11:45:40 +00001318 if (E->getOpcode() == BO_LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001319 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001320 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001321 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001322 }
1323 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001324 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001325 // We can't evaluate the LHS; however, sometimes the result
1326 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
John McCall2de56d12010-08-25 11:45:40 +00001327 if (rhsResult == (E->getOpcode() == BO_LOr) ||
1328 !rhsResult == (E->getOpcode() == BO_LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001329 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001330 // must have had side effects.
1331 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001332
1333 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001334 }
1335 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001336 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001337
Eli Friedmana6afa762008-11-13 06:09:17 +00001338 return false;
1339 }
1340
Anders Carlsson286f85e2008-11-16 07:17:21 +00001341 QualType LHSTy = E->getLHS()->getType();
1342 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001343
1344 if (LHSTy->isAnyComplexType()) {
1345 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001346 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001347
1348 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1349 return false;
1350
1351 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1352 return false;
1353
1354 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001355 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001356 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001357 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001358 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1359
John McCall2de56d12010-08-25 11:45:40 +00001360 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001361 return Success((CR_r == APFloat::cmpEqual &&
1362 CR_i == APFloat::cmpEqual), E);
1363 else {
John McCall2de56d12010-08-25 11:45:40 +00001364 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001365 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001366 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001367 CR_r == APFloat::cmpLessThan ||
1368 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001369 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001370 CR_i == APFloat::cmpLessThan ||
1371 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001372 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001373 } else {
John McCall2de56d12010-08-25 11:45:40 +00001374 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001375 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1376 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1377 else {
John McCall2de56d12010-08-25 11:45:40 +00001378 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001379 "Invalid compex comparison.");
1380 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1381 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1382 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001383 }
1384 }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Anders Carlsson286f85e2008-11-16 07:17:21 +00001386 if (LHSTy->isRealFloatingType() &&
1387 RHSTy->isRealFloatingType()) {
1388 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Anders Carlsson286f85e2008-11-16 07:17:21 +00001390 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1391 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Anders Carlsson286f85e2008-11-16 07:17:21 +00001393 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1394 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Anders Carlsson286f85e2008-11-16 07:17:21 +00001396 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001397
Anders Carlsson286f85e2008-11-16 07:17:21 +00001398 switch (E->getOpcode()) {
1399 default:
1400 assert(0 && "Invalid binary operator!");
John McCall2de56d12010-08-25 11:45:40 +00001401 case BO_LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001402 return Success(CR == APFloat::cmpLessThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001403 case BO_GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001404 return Success(CR == APFloat::cmpGreaterThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001405 case BO_LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001406 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001407 case BO_GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001408 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001409 E);
John McCall2de56d12010-08-25 11:45:40 +00001410 case BO_EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001411 return Success(CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001412 case BO_NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001413 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001414 || CR == APFloat::cmpLessThan
1415 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001416 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001419 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001420 if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001421 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001422 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1423 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001424
John McCallefdb83e2010-05-07 21:00:08 +00001425 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001426 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1427 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001428
Eli Friedman5bc86102009-06-14 02:17:33 +00001429 // Reject any bases from the normal codepath; we special-case comparisons
1430 // to null.
1431 if (LHSValue.getLValueBase()) {
1432 if (!E->isEqualityOp())
1433 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001434 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001435 return false;
1436 bool bres;
1437 if (!EvalPointerValueAsBool(LHSValue, bres))
1438 return false;
John McCall2de56d12010-08-25 11:45:40 +00001439 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001440 } else if (RHSValue.getLValueBase()) {
1441 if (!E->isEqualityOp())
1442 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001443 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001444 return false;
1445 bool bres;
1446 if (!EvalPointerValueAsBool(RHSValue, bres))
1447 return false;
John McCall2de56d12010-08-25 11:45:40 +00001448 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001449 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001450
John McCall2de56d12010-08-25 11:45:40 +00001451 if (E->getOpcode() == BO_Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001452 QualType Type = E->getLHS()->getType();
1453 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001454
Ken Dycka7305832010-01-15 12:37:54 +00001455 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001456 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001457 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001458
Ken Dycka7305832010-01-15 12:37:54 +00001459 CharUnits Diff = LHSValue.getLValueOffset() -
1460 RHSValue.getLValueOffset();
1461 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001462 }
1463 bool Result;
John McCall2de56d12010-08-25 11:45:40 +00001464 if (E->getOpcode() == BO_EQ) {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001465 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001466 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001467 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1468 }
1469 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001470 }
1471 }
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001472 if (!LHSTy->isIntegralOrEnumerationType() ||
1473 !RHSTy->isIntegralOrEnumerationType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001474 // We can't continue from here for non-integral types, and they
1475 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001476 return false;
1477 }
1478
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001479 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001480 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001481 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001482
Eli Friedman42edd0d2009-03-24 01:14:50 +00001483 APValue RHSVal;
1484 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001485 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001486
1487 // Handle cases like (unsigned long)&a + 4.
1488 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001489 CharUnits Offset = Result.getLValueOffset();
1490 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1491 RHSVal.getInt().getZExtValue());
John McCall2de56d12010-08-25 11:45:40 +00001492 if (E->getOpcode() == BO_Add)
Ken Dycka7305832010-01-15 12:37:54 +00001493 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001494 else
Ken Dycka7305832010-01-15 12:37:54 +00001495 Offset -= AdditionalOffset;
1496 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001497 return true;
1498 }
1499
1500 // Handle cases like 4 + (unsigned long)&a
John McCall2de56d12010-08-25 11:45:40 +00001501 if (E->getOpcode() == BO_Add &&
Eli Friedman42edd0d2009-03-24 01:14:50 +00001502 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001503 CharUnits Offset = RHSVal.getLValueOffset();
1504 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1505 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001506 return true;
1507 }
1508
1509 // All the following cases expect both operands to be an integer
1510 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001511 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001512
Eli Friedman42edd0d2009-03-24 01:14:50 +00001513 APSInt& RHS = RHSVal.getInt();
1514
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001515 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001516 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001517 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001518 case BO_Mul: return Success(Result.getInt() * RHS, E);
1519 case BO_Add: return Success(Result.getInt() + RHS, E);
1520 case BO_Sub: return Success(Result.getInt() - RHS, E);
1521 case BO_And: return Success(Result.getInt() & RHS, E);
1522 case BO_Xor: return Success(Result.getInt() ^ RHS, E);
1523 case BO_Or: return Success(Result.getInt() | RHS, E);
1524 case BO_Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001525 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001526 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001527 return Success(Result.getInt() / RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001528 case BO_Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001529 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001530 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001531 return Success(Result.getInt() % RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001532 case BO_Shl: {
John McCall091f23f2010-11-09 22:22:12 +00001533 // During constant-folding, a negative shift is an opposite shift.
1534 if (RHS.isSigned() && RHS.isNegative()) {
1535 RHS = -RHS;
1536 goto shift_right;
1537 }
1538
1539 shift_left:
1540 unsigned SA
1541 = (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001542 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001543 }
John McCall2de56d12010-08-25 11:45:40 +00001544 case BO_Shr: {
John McCall091f23f2010-11-09 22:22:12 +00001545 // During constant-folding, a negative shift is an opposite shift.
1546 if (RHS.isSigned() && RHS.isNegative()) {
1547 RHS = -RHS;
1548 goto shift_left;
1549 }
1550
1551 shift_right:
Mike Stump1eb44332009-09-09 15:08:12 +00001552 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001553 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1554 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
John McCall2de56d12010-08-25 11:45:40 +00001557 case BO_LT: return Success(Result.getInt() < RHS, E);
1558 case BO_GT: return Success(Result.getInt() > RHS, E);
1559 case BO_LE: return Success(Result.getInt() <= RHS, E);
1560 case BO_GE: return Success(Result.getInt() >= RHS, E);
1561 case BO_EQ: return Success(Result.getInt() == RHS, E);
1562 case BO_NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001563 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001564}
1565
Ken Dyck8b752f12010-01-27 17:10:57 +00001566CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001567 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1568 // the result is the size of the referenced type."
1569 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1570 // result shall be the alignment of the referenced type."
1571 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1572 T = Ref->getPointeeType();
1573
Eli Friedman2be58612009-05-30 21:09:44 +00001574 // __alignof is defined to return the preferred alignment.
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001575 return Info.Ctx.toCharUnitsFromBits(
1576 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
Chris Lattnere9feb472009-01-24 21:09:06 +00001577}
1578
Ken Dyck8b752f12010-01-27 17:10:57 +00001579CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001580 E = E->IgnoreParens();
1581
1582 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001583 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001584 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001585 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1586 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001587
Chris Lattneraf707ab2009-01-24 21:53:27 +00001588 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001589 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1590 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001591
Chris Lattnere9feb472009-01-24 21:09:06 +00001592 return GetAlignOfType(E->getType());
1593}
1594
1595
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001596/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
1597/// a result as the expression's type.
1598bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
1599 const UnaryExprOrTypeTraitExpr *E) {
1600 switch(E->getKind()) {
1601 case UETT_AlignOf: {
Chris Lattnere9feb472009-01-24 21:09:06 +00001602 if (E->isArgumentType())
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001603 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001604 else
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001605 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001606 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001607
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001608 case UETT_VecStep: {
1609 QualType Ty = E->getTypeOfArgument();
Sebastian Redl05189992008-11-11 17:56:53 +00001610
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001611 if (Ty->isVectorType()) {
1612 unsigned n = Ty->getAs<VectorType>()->getNumElements();
Eli Friedmana1f47c42009-03-23 04:38:34 +00001613
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001614 // The vec_step built-in functions that take a 3-component
1615 // vector return 4. (OpenCL 1.1 spec 6.11.12)
1616 if (n == 3)
1617 n = 4;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001618
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001619 return Success(n, E);
1620 } else
1621 return Success(1, E);
1622 }
1623
1624 case UETT_SizeOf: {
1625 QualType SrcTy = E->getTypeOfArgument();
1626 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1627 // the result is the size of the referenced type."
1628 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1629 // result shall be the alignment of the referenced type."
1630 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1631 SrcTy = Ref->getPointeeType();
1632
1633 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1634 // extension.
1635 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1636 return Success(1, E);
1637
1638 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
1639 if (!SrcTy->isConstantSizeType())
1640 return false;
1641
1642 // Get information about the size.
1643 return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E);
1644 }
1645 }
1646
1647 llvm_unreachable("unknown expr/type trait");
1648 return false;
Chris Lattnerfcee0012008-07-11 21:24:13 +00001649}
1650
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001651bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001652 CharUnits Result;
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001653 unsigned n = OOE->getNumComponents();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001654 if (n == 0)
1655 return false;
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001656 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001657 for (unsigned i = 0; i != n; ++i) {
1658 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1659 switch (ON.getKind()) {
1660 case OffsetOfExpr::OffsetOfNode::Array: {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001661 const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001662 APSInt IdxResult;
1663 if (!EvaluateInteger(Idx, IdxResult, Info))
1664 return false;
1665 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1666 if (!AT)
1667 return false;
1668 CurrentType = AT->getElementType();
1669 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1670 Result += IdxResult.getSExtValue() * ElementSize;
1671 break;
1672 }
1673
1674 case OffsetOfExpr::OffsetOfNode::Field: {
1675 FieldDecl *MemberDecl = ON.getField();
1676 const RecordType *RT = CurrentType->getAs<RecordType>();
1677 if (!RT)
1678 return false;
1679 RecordDecl *RD = RT->getDecl();
1680 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
John McCallba4f5d52011-01-20 07:57:12 +00001681 unsigned i = MemberDecl->getFieldIndex();
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001682 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001683 Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001684 CurrentType = MemberDecl->getType().getNonReferenceType();
1685 break;
1686 }
1687
1688 case OffsetOfExpr::OffsetOfNode::Identifier:
1689 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001690 return false;
1691
1692 case OffsetOfExpr::OffsetOfNode::Base: {
1693 CXXBaseSpecifier *BaseSpec = ON.getBase();
1694 if (BaseSpec->isVirtual())
1695 return false;
1696
1697 // Find the layout of the class whose base we are looking into.
1698 const RecordType *RT = CurrentType->getAs<RecordType>();
1699 if (!RT)
1700 return false;
1701 RecordDecl *RD = RT->getDecl();
1702 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1703
1704 // Find the base class itself.
1705 CurrentType = BaseSpec->getType();
1706 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1707 if (!BaseRT)
1708 return false;
1709
1710 // Add the offset to the base.
Ken Dyck7c7f8202011-01-26 02:17:08 +00001711 Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001712 break;
1713 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001714 }
1715 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001716 return Success(Result, OOE);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001717}
1718
Chris Lattnerb542afe2008-07-11 19:10:17 +00001719bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001720 if (E->getOpcode() == UO_LNot) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001721 // LNot's operand isn't necessarily an integer, so we handle it specially.
1722 bool bres;
1723 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1724 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001725 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001726 }
1727
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001728 // Only handle integral operations...
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001729 if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001730 return false;
1731
Chris Lattner87eae5e2008-07-11 22:52:41 +00001732 // Get the operand value into 'Result'.
1733 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001734 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001735
Chris Lattner75a48812008-07-11 22:15:16 +00001736 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001737 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001738 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1739 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001740 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001741 case UO_Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001742 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1743 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001744 return true;
John McCall2de56d12010-08-25 11:45:40 +00001745 case UO_Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001746 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001747 return true;
John McCall2de56d12010-08-25 11:45:40 +00001748 case UO_Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001749 if (!Result.isInt()) return false;
1750 return Success(-Result.getInt(), E);
John McCall2de56d12010-08-25 11:45:40 +00001751 case UO_Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001752 if (!Result.isInt()) return false;
1753 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001754 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001755}
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Chris Lattner732b2232008-07-12 01:15:53 +00001757/// HandleCast - This is used to evaluate implicit or explicit casts where the
1758/// result type is integer.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001759bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1760 const Expr *SubExpr = E->getSubExpr();
Anders Carlsson82206e22008-11-30 18:14:57 +00001761 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001762 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001763
Eli Friedman46a52322011-03-25 00:43:55 +00001764 switch (E->getCastKind()) {
Eli Friedman46a52322011-03-25 00:43:55 +00001765 case CK_BaseToDerived:
1766 case CK_DerivedToBase:
1767 case CK_UncheckedDerivedToBase:
1768 case CK_Dynamic:
1769 case CK_ToUnion:
1770 case CK_ArrayToPointerDecay:
1771 case CK_FunctionToPointerDecay:
1772 case CK_NullToPointer:
1773 case CK_NullToMemberPointer:
1774 case CK_BaseToDerivedMemberPointer:
1775 case CK_DerivedToBaseMemberPointer:
1776 case CK_ConstructorConversion:
1777 case CK_IntegralToPointer:
1778 case CK_ToVoid:
1779 case CK_VectorSplat:
1780 case CK_IntegralToFloating:
1781 case CK_FloatingCast:
1782 case CK_AnyPointerToObjCPointerCast:
1783 case CK_AnyPointerToBlockPointerCast:
1784 case CK_ObjCObjectLValueCast:
1785 case CK_FloatingRealToComplex:
1786 case CK_FloatingComplexToReal:
1787 case CK_FloatingComplexCast:
1788 case CK_FloatingComplexToIntegralComplex:
1789 case CK_IntegralRealToComplex:
1790 case CK_IntegralComplexCast:
1791 case CK_IntegralComplexToFloatingComplex:
1792 llvm_unreachable("invalid cast kind for integral value");
1793
Eli Friedmane50c2972011-03-25 19:07:11 +00001794 case CK_BitCast:
Eli Friedman46a52322011-03-25 00:43:55 +00001795 case CK_Dependent:
1796 case CK_GetObjCProperty:
1797 case CK_LValueBitCast:
1798 case CK_UserDefinedConversion:
1799 return false;
1800
1801 case CK_LValueToRValue:
1802 case CK_NoOp:
1803 return Visit(E->getSubExpr());
1804
1805 case CK_MemberPointerToBoolean:
1806 case CK_PointerToBoolean:
1807 case CK_IntegralToBoolean:
1808 case CK_FloatingToBoolean:
1809 case CK_FloatingComplexToBoolean:
1810 case CK_IntegralComplexToBoolean: {
Eli Friedman4efaa272008-11-12 09:44:48 +00001811 bool BoolResult;
1812 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1813 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001814 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001815 }
1816
Eli Friedman46a52322011-03-25 00:43:55 +00001817 case CK_IntegralCast: {
Chris Lattner732b2232008-07-12 01:15:53 +00001818 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001819 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001820
Eli Friedmanbe265702009-02-20 01:15:07 +00001821 if (!Result.isInt()) {
1822 // Only allow casts of lvalues if they are lossless.
1823 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1824 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001825
Daniel Dunbardd211642009-02-19 22:24:01 +00001826 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001827 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001828 }
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Eli Friedman46a52322011-03-25 00:43:55 +00001830 case CK_PointerToIntegral: {
John McCallefdb83e2010-05-07 21:00:08 +00001831 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001832 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001833 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001834
Daniel Dunbardd211642009-02-19 22:24:01 +00001835 if (LV.getLValueBase()) {
1836 // Only allow based lvalue casts if they are lossless.
1837 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1838 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001839
John McCallefdb83e2010-05-07 21:00:08 +00001840 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001841 return true;
1842 }
1843
Ken Dycka7305832010-01-15 12:37:54 +00001844 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1845 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001846 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001847 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001848
Eli Friedman46a52322011-03-25 00:43:55 +00001849 case CK_IntegralComplexToReal: {
John McCallf4cf1a12010-05-07 17:22:02 +00001850 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001851 if (!EvaluateComplex(SubExpr, C, Info))
1852 return false;
Eli Friedman46a52322011-03-25 00:43:55 +00001853 return Success(C.getComplexIntReal(), E);
Eli Friedman1725f682009-04-22 19:23:09 +00001854 }
Eli Friedman2217c872009-02-22 11:46:18 +00001855
Eli Friedman46a52322011-03-25 00:43:55 +00001856 case CK_FloatingToIntegral: {
1857 APFloat F(0.0);
1858 if (!EvaluateFloat(SubExpr, F, Info))
1859 return false;
Chris Lattner732b2232008-07-12 01:15:53 +00001860
Eli Friedman46a52322011-03-25 00:43:55 +00001861 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
1862 }
1863 }
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Eli Friedman46a52322011-03-25 00:43:55 +00001865 llvm_unreachable("unknown cast resulting in integral value");
1866 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001867}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001868
Eli Friedman722c7172009-02-28 03:59:05 +00001869bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1870 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001871 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001872 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1873 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1874 return Success(LV.getComplexIntReal(), E);
1875 }
1876
1877 return Visit(E->getSubExpr());
1878}
1879
Eli Friedman664a1042009-02-27 04:45:43 +00001880bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001881 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001882 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001883 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1884 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1885 return Success(LV.getComplexIntImag(), E);
1886 }
1887
Eli Friedman664a1042009-02-27 04:45:43 +00001888 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1889 Info.EvalResult.HasSideEffects = true;
1890 return Success(0, E);
1891}
1892
Douglas Gregoree8aff02011-01-04 17:33:58 +00001893bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1894 return Success(E->getPackLength(), E);
1895}
1896
Sebastian Redl295995c2010-09-10 20:55:47 +00001897bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1898 return Success(E->getValue(), E);
1899}
1900
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001901//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001902// Float Evaluation
1903//===----------------------------------------------------------------------===//
1904
1905namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001906class FloatExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001907 : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001908 APFloat &Result;
1909public:
1910 FloatExprEvaluator(EvalInfo &info, APFloat &result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001911 : ExprEvaluatorBaseTy(info), Result(result) {}
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001912
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001913 bool Success(const APValue &V, const Expr *e) {
1914 Result = V.getFloat();
1915 return true;
1916 }
1917 bool Error(const Stmt *S) {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001918 return false;
1919 }
1920
Chris Lattner019f4e82008-10-06 05:28:25 +00001921 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001922
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001923 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001924 bool VisitBinaryOperator(const BinaryOperator *E);
1925 bool VisitFloatingLiteral(const FloatingLiteral *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001926 bool VisitCastExpr(const CastExpr *E);
1927 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001928
John McCallabd3a852010-05-07 22:08:54 +00001929 bool VisitUnaryReal(const UnaryOperator *E);
1930 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001931
John McCall189d6ef2010-10-09 01:34:31 +00001932 bool VisitDeclRefExpr(const DeclRefExpr *E);
1933
John McCallabd3a852010-05-07 22:08:54 +00001934 // FIXME: Missing: array subscript of vector, member of vector,
1935 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001936};
1937} // end anonymous namespace
1938
1939static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001940 assert(E->getType()->isRealFloatingType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001941 return FloatExprEvaluator(Info, Result).Visit(E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001942}
1943
Jay Foad4ba2a172011-01-12 09:06:06 +00001944static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
John McCalldb7b72a2010-02-28 13:00:19 +00001945 QualType ResultTy,
1946 const Expr *Arg,
1947 bool SNaN,
1948 llvm::APFloat &Result) {
1949 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1950 if (!S) return false;
1951
1952 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1953
1954 llvm::APInt fill;
1955
1956 // Treat empty strings as if they were zero.
1957 if (S->getString().empty())
1958 fill = llvm::APInt(32, 0);
1959 else if (S->getString().getAsInteger(0, fill))
1960 return false;
1961
1962 if (SNaN)
1963 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1964 else
1965 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1966 return true;
1967}
1968
Chris Lattner019f4e82008-10-06 05:28:25 +00001969bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001970 switch (E->isBuiltinCall(Info.Ctx)) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001971 default:
1972 return ExprEvaluatorBaseTy::VisitCallExpr(E);
1973
Chris Lattner019f4e82008-10-06 05:28:25 +00001974 case Builtin::BI__builtin_huge_val:
1975 case Builtin::BI__builtin_huge_valf:
1976 case Builtin::BI__builtin_huge_vall:
1977 case Builtin::BI__builtin_inf:
1978 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001979 case Builtin::BI__builtin_infl: {
1980 const llvm::fltSemantics &Sem =
1981 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001982 Result = llvm::APFloat::getInf(Sem);
1983 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
John McCalldb7b72a2010-02-28 13:00:19 +00001986 case Builtin::BI__builtin_nans:
1987 case Builtin::BI__builtin_nansf:
1988 case Builtin::BI__builtin_nansl:
1989 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1990 true, Result);
1991
Chris Lattner9e621712008-10-06 06:31:58 +00001992 case Builtin::BI__builtin_nan:
1993 case Builtin::BI__builtin_nanf:
1994 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001995 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001996 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001997 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1998 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001999
2000 case Builtin::BI__builtin_fabs:
2001 case Builtin::BI__builtin_fabsf:
2002 case Builtin::BI__builtin_fabsl:
2003 if (!EvaluateFloat(E->getArg(0), Result, Info))
2004 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002006 if (Result.isNegative())
2007 Result.changeSign();
2008 return true;
2009
Mike Stump1eb44332009-09-09 15:08:12 +00002010 case Builtin::BI__builtin_copysign:
2011 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002012 case Builtin::BI__builtin_copysignl: {
2013 APFloat RHS(0.);
2014 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
2015 !EvaluateFloat(E->getArg(1), RHS, Info))
2016 return false;
2017 Result.copySign(RHS);
2018 return true;
2019 }
Chris Lattner019f4e82008-10-06 05:28:25 +00002020 }
2021}
2022
John McCall189d6ef2010-10-09 01:34:31 +00002023bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002024 if (ExprEvaluatorBaseTy::VisitDeclRefExpr(E))
2025 return true;
2026
John McCall189d6ef2010-10-09 01:34:31 +00002027 const Decl *D = E->getDecl();
2028 if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false;
2029 const VarDecl *VD = cast<VarDecl>(D);
2030
2031 // Require the qualifiers to be const and not volatile.
2032 CanQualType T = Info.Ctx.getCanonicalType(E->getType());
2033 if (!T.isConstQualified() || T.isVolatileQualified())
2034 return false;
2035
2036 const Expr *Init = VD->getAnyInitializer();
2037 if (!Init) return false;
2038
2039 if (APValue *V = VD->getEvaluatedValue()) {
2040 if (V->isFloat()) {
2041 Result = V->getFloat();
2042 return true;
2043 }
2044 return false;
2045 }
2046
2047 if (VD->isEvaluatingValue())
2048 return false;
2049
2050 VD->setEvaluatingValue();
2051
2052 Expr::EvalResult InitResult;
2053 if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects &&
2054 InitResult.Val.isFloat()) {
2055 // Cache the evaluated value in the variable declaration.
2056 Result = InitResult.Val.getFloat();
2057 VD->setEvaluatedValue(InitResult.Val);
2058 return true;
2059 }
2060
2061 VD->setEvaluatedValue(APValue());
2062 return false;
2063}
2064
John McCallabd3a852010-05-07 22:08:54 +00002065bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002066 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2067 ComplexValue CV;
2068 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2069 return false;
2070 Result = CV.FloatReal;
2071 return true;
2072 }
2073
2074 return Visit(E->getSubExpr());
John McCallabd3a852010-05-07 22:08:54 +00002075}
2076
2077bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002078 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2079 ComplexValue CV;
2080 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2081 return false;
2082 Result = CV.FloatImag;
2083 return true;
2084 }
2085
2086 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
2087 Info.EvalResult.HasSideEffects = true;
2088 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
2089 Result = llvm::APFloat::getZero(Sem);
John McCallabd3a852010-05-07 22:08:54 +00002090 return true;
2091}
2092
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002093bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002094 if (E->getOpcode() == UO_Deref)
Nuno Lopesa468d342008-11-19 17:44:31 +00002095 return false;
2096
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002097 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
2098 return false;
2099
2100 switch (E->getOpcode()) {
2101 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002102 case UO_Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002103 return true;
John McCall2de56d12010-08-25 11:45:40 +00002104 case UO_Minus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002105 Result.changeSign();
2106 return true;
2107 }
2108}
Chris Lattner019f4e82008-10-06 05:28:25 +00002109
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002110bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002111 if (E->getOpcode() == BO_Comma) {
Eli Friedman7f92f032009-11-16 04:25:37 +00002112 if (!EvaluateFloat(E->getRHS(), Result, Info))
2113 return false;
2114
2115 // If we can't evaluate the LHS, it might have side effects;
2116 // conservatively mark it.
2117 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2118 Info.EvalResult.HasSideEffects = true;
2119
2120 return true;
2121 }
2122
Anders Carlsson96e93662010-10-31 01:21:47 +00002123 // We can't evaluate pointer-to-member operations.
2124 if (E->isPtrMemOp())
2125 return false;
2126
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002127 // FIXME: Diagnostics? I really don't understand how the warnings
2128 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002129 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002130 if (!EvaluateFloat(E->getLHS(), Result, Info))
2131 return false;
2132 if (!EvaluateFloat(E->getRHS(), RHS, Info))
2133 return false;
2134
2135 switch (E->getOpcode()) {
2136 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002137 case BO_Mul:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002138 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
2139 return true;
John McCall2de56d12010-08-25 11:45:40 +00002140 case BO_Add:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002141 Result.add(RHS, APFloat::rmNearestTiesToEven);
2142 return true;
John McCall2de56d12010-08-25 11:45:40 +00002143 case BO_Sub:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002144 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
2145 return true;
John McCall2de56d12010-08-25 11:45:40 +00002146 case BO_Div:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002147 Result.divide(RHS, APFloat::rmNearestTiesToEven);
2148 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002149 }
2150}
2151
2152bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
2153 Result = E->getValue();
2154 return true;
2155}
2156
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002157bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
2158 const Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Eli Friedman2a523ee2011-03-25 00:54:52 +00002160 switch (E->getCastKind()) {
2161 default:
2162 return false;
2163
2164 case CK_LValueToRValue:
2165 case CK_NoOp:
2166 return Visit(SubExpr);
2167
2168 case CK_IntegralToFloating: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002169 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00002170 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00002171 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002172 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002173 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002174 return true;
2175 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002176
2177 case CK_FloatingCast: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002178 if (!Visit(SubExpr))
2179 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002180 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
2181 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002182 return true;
2183 }
John McCallf3ea8cf2010-11-14 08:17:51 +00002184
Eli Friedman2a523ee2011-03-25 00:54:52 +00002185 case CK_FloatingComplexToReal: {
John McCallf3ea8cf2010-11-14 08:17:51 +00002186 ComplexValue V;
2187 if (!EvaluateComplex(SubExpr, V, Info))
2188 return false;
2189 Result = V.getComplexFloatReal();
2190 return true;
2191 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002192 }
Eli Friedman4efaa272008-11-12 09:44:48 +00002193
2194 return false;
2195}
2196
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002197bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +00002198 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
2199 return true;
2200}
2201
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002202//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002203// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002204//===----------------------------------------------------------------------===//
2205
2206namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00002207class ComplexExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002208 : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
John McCallf4cf1a12010-05-07 17:22:02 +00002209 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002211public:
John McCallf4cf1a12010-05-07 17:22:02 +00002212 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002213 : ExprEvaluatorBaseTy(info), Result(Result) {}
2214
2215 bool Success(const APValue &V, const Expr *e) {
2216 Result.setFrom(V);
2217 return true;
2218 }
2219 bool Error(const Expr *E) {
2220 return false;
2221 }
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002223 //===--------------------------------------------------------------------===//
2224 // Visitor Methods
2225 //===--------------------------------------------------------------------===//
2226
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002227 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002229 bool VisitCastExpr(const 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);
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002233 // FIXME Missing: ImplicitValueInitExpr
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002234};
2235} // end anonymous namespace
2236
John McCallf4cf1a12010-05-07 17:22:02 +00002237static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2238 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002239 assert(E->getType()->isAnyComplexType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002240 return ComplexExprEvaluator(Info, Result).Visit(E);
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002241}
2242
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002243bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
2244 const Expr* SubExpr = E->getSubExpr();
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002245
2246 if (SubExpr->getType()->isRealFloatingType()) {
2247 Result.makeComplexFloat();
2248 APFloat &Imag = Result.FloatImag;
2249 if (!EvaluateFloat(SubExpr, Imag, Info))
2250 return false;
2251
2252 Result.FloatReal = APFloat(Imag.getSemantics());
2253 return true;
2254 } else {
2255 assert(SubExpr->getType()->isIntegerType() &&
2256 "Unexpected imaginary literal.");
2257
2258 Result.makeComplexInt();
2259 APSInt &Imag = Result.IntImag;
2260 if (!EvaluateInteger(SubExpr, Imag, Info))
2261 return false;
2262
2263 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2264 return true;
2265 }
2266}
2267
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002268bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002269
John McCall8786da72010-12-14 17:51:41 +00002270 switch (E->getCastKind()) {
2271 case CK_BitCast:
John McCall8786da72010-12-14 17:51:41 +00002272 case CK_BaseToDerived:
2273 case CK_DerivedToBase:
2274 case CK_UncheckedDerivedToBase:
2275 case CK_Dynamic:
2276 case CK_ToUnion:
2277 case CK_ArrayToPointerDecay:
2278 case CK_FunctionToPointerDecay:
2279 case CK_NullToPointer:
2280 case CK_NullToMemberPointer:
2281 case CK_BaseToDerivedMemberPointer:
2282 case CK_DerivedToBaseMemberPointer:
2283 case CK_MemberPointerToBoolean:
2284 case CK_ConstructorConversion:
2285 case CK_IntegralToPointer:
2286 case CK_PointerToIntegral:
2287 case CK_PointerToBoolean:
2288 case CK_ToVoid:
2289 case CK_VectorSplat:
2290 case CK_IntegralCast:
2291 case CK_IntegralToBoolean:
2292 case CK_IntegralToFloating:
2293 case CK_FloatingToIntegral:
2294 case CK_FloatingToBoolean:
2295 case CK_FloatingCast:
2296 case CK_AnyPointerToObjCPointerCast:
2297 case CK_AnyPointerToBlockPointerCast:
2298 case CK_ObjCObjectLValueCast:
2299 case CK_FloatingComplexToReal:
2300 case CK_FloatingComplexToBoolean:
2301 case CK_IntegralComplexToReal:
2302 case CK_IntegralComplexToBoolean:
2303 llvm_unreachable("invalid cast kind for complex value");
John McCall2bb5d002010-11-13 09:02:35 +00002304
John McCall8786da72010-12-14 17:51:41 +00002305 case CK_LValueToRValue:
2306 case CK_NoOp:
2307 return Visit(E->getSubExpr());
2308
2309 case CK_Dependent:
2310 case CK_GetObjCProperty:
Eli Friedman46a52322011-03-25 00:43:55 +00002311 case CK_LValueBitCast:
John McCall8786da72010-12-14 17:51:41 +00002312 case CK_UserDefinedConversion:
2313 return false;
2314
2315 case CK_FloatingRealToComplex: {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002316 APFloat &Real = Result.FloatReal;
John McCall8786da72010-12-14 17:51:41 +00002317 if (!EvaluateFloat(E->getSubExpr(), Real, Info))
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002318 return false;
2319
John McCall8786da72010-12-14 17:51:41 +00002320 Result.makeComplexFloat();
2321 Result.FloatImag = APFloat(Real.getSemantics());
2322 return true;
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002323 }
2324
John McCall8786da72010-12-14 17:51:41 +00002325 case CK_FloatingComplexCast: {
2326 if (!Visit(E->getSubExpr()))
2327 return false;
2328
2329 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2330 QualType From
2331 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2332
2333 Result.FloatReal
2334 = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx);
2335 Result.FloatImag
2336 = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx);
2337 return true;
2338 }
2339
2340 case CK_FloatingComplexToIntegralComplex: {
2341 if (!Visit(E->getSubExpr()))
2342 return false;
2343
2344 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2345 QualType From
2346 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2347 Result.makeComplexInt();
2348 Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx);
2349 Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx);
2350 return true;
2351 }
2352
2353 case CK_IntegralRealToComplex: {
2354 APSInt &Real = Result.IntReal;
2355 if (!EvaluateInteger(E->getSubExpr(), Real, Info))
2356 return false;
2357
2358 Result.makeComplexInt();
2359 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2360 return true;
2361 }
2362
2363 case CK_IntegralComplexCast: {
2364 if (!Visit(E->getSubExpr()))
2365 return false;
2366
2367 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2368 QualType From
2369 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2370
2371 Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx);
2372 Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx);
2373 return true;
2374 }
2375
2376 case CK_IntegralComplexToFloatingComplex: {
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 Result.makeComplexFloat();
2384 Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx);
2385 Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx);
2386 return true;
2387 }
2388 }
2389
2390 llvm_unreachable("unknown cast resulting in complex value");
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002391 return false;
2392}
2393
John McCallf4cf1a12010-05-07 17:22:02 +00002394bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002395 if (E->getOpcode() == BO_Comma) {
2396 if (!Visit(E->getRHS()))
2397 return false;
2398
2399 // If we can't evaluate the LHS, it might have side effects;
2400 // conservatively mark it.
2401 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2402 Info.EvalResult.HasSideEffects = true;
2403
2404 return true;
2405 }
John McCallf4cf1a12010-05-07 17:22:02 +00002406 if (!Visit(E->getLHS()))
2407 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002408
John McCallf4cf1a12010-05-07 17:22:02 +00002409 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002410 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002411 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002412
Daniel Dunbar3f279872009-01-29 01:32:56 +00002413 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2414 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002415 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002416 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002417 case BO_Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002418 if (Result.isComplexFloat()) {
2419 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2420 APFloat::rmNearestTiesToEven);
2421 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2422 APFloat::rmNearestTiesToEven);
2423 } else {
2424 Result.getComplexIntReal() += RHS.getComplexIntReal();
2425 Result.getComplexIntImag() += RHS.getComplexIntImag();
2426 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002427 break;
John McCall2de56d12010-08-25 11:45:40 +00002428 case BO_Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002429 if (Result.isComplexFloat()) {
2430 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2431 APFloat::rmNearestTiesToEven);
2432 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2433 APFloat::rmNearestTiesToEven);
2434 } else {
2435 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2436 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2437 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002438 break;
John McCall2de56d12010-08-25 11:45:40 +00002439 case BO_Mul:
Daniel Dunbar3f279872009-01-29 01:32:56 +00002440 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002441 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002442 APFloat &LHS_r = LHS.getComplexFloatReal();
2443 APFloat &LHS_i = LHS.getComplexFloatImag();
2444 APFloat &RHS_r = RHS.getComplexFloatReal();
2445 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Daniel Dunbar3f279872009-01-29 01:32:56 +00002447 APFloat Tmp = LHS_r;
2448 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2449 Result.getComplexFloatReal() = Tmp;
2450 Tmp = LHS_i;
2451 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2452 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2453
2454 Tmp = LHS_r;
2455 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2456 Result.getComplexFloatImag() = Tmp;
2457 Tmp = LHS_i;
2458 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2459 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2460 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002461 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002462 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002463 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2464 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002465 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002466 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2467 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2468 }
2469 break;
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002470 case BO_Div:
2471 if (Result.isComplexFloat()) {
2472 ComplexValue LHS = Result;
2473 APFloat &LHS_r = LHS.getComplexFloatReal();
2474 APFloat &LHS_i = LHS.getComplexFloatImag();
2475 APFloat &RHS_r = RHS.getComplexFloatReal();
2476 APFloat &RHS_i = RHS.getComplexFloatImag();
2477 APFloat &Res_r = Result.getComplexFloatReal();
2478 APFloat &Res_i = Result.getComplexFloatImag();
2479
2480 APFloat Den = RHS_r;
2481 Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2482 APFloat Tmp = RHS_i;
2483 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2484 Den.add(Tmp, APFloat::rmNearestTiesToEven);
2485
2486 Res_r = LHS_r;
2487 Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2488 Tmp = LHS_i;
2489 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2490 Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
2491 Res_r.divide(Den, APFloat::rmNearestTiesToEven);
2492
2493 Res_i = LHS_i;
2494 Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2495 Tmp = LHS_r;
2496 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2497 Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
2498 Res_i.divide(Den, APFloat::rmNearestTiesToEven);
2499 } else {
2500 if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) {
2501 // FIXME: what about diagnostics?
2502 return false;
2503 }
2504 ComplexValue LHS = Result;
2505 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
2506 RHS.getComplexIntImag() * RHS.getComplexIntImag();
2507 Result.getComplexIntReal() =
2508 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
2509 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
2510 Result.getComplexIntImag() =
2511 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
2512 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
2513 }
2514 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002515 }
2516
John McCallf4cf1a12010-05-07 17:22:02 +00002517 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002518}
2519
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002520bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
2521 // Get the operand value into 'Result'.
2522 if (!Visit(E->getSubExpr()))
2523 return false;
2524
2525 switch (E->getOpcode()) {
2526 default:
2527 // FIXME: what about diagnostics?
2528 return false;
2529 case UO_Extension:
2530 return true;
2531 case UO_Plus:
2532 // The result is always just the subexpr.
2533 return true;
2534 case UO_Minus:
2535 if (Result.isComplexFloat()) {
2536 Result.getComplexFloatReal().changeSign();
2537 Result.getComplexFloatImag().changeSign();
2538 }
2539 else {
2540 Result.getComplexIntReal() = -Result.getComplexIntReal();
2541 Result.getComplexIntImag() = -Result.getComplexIntImag();
2542 }
2543 return true;
2544 case UO_Not:
2545 if (Result.isComplexFloat())
2546 Result.getComplexFloatImag().changeSign();
2547 else
2548 Result.getComplexIntImag() = -Result.getComplexIntImag();
2549 return true;
2550 }
2551}
2552
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002553//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002554// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002555//===----------------------------------------------------------------------===//
2556
John McCall56ca35d2011-02-17 10:25:35 +00002557static bool Evaluate(EvalInfo &Info, const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +00002558 if (E->getType()->isVectorType()) {
2559 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002560 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002561 } else if (E->getType()->isIntegerType()) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002562 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(E))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002563 return false;
John McCall56ca35d2011-02-17 10:25:35 +00002564 if (Info.EvalResult.Val.isLValue() &&
2565 !IsGlobalLValue(Info.EvalResult.Val.getLValueBase()))
John McCall0f2b6922010-07-07 05:08:32 +00002566 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002567 } else if (E->getType()->hasPointerRepresentation()) {
2568 LValue LV;
2569 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002570 return false;
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002571 if (!IsGlobalLValue(LV.Base))
John McCall42c8f872010-05-10 23:27:23 +00002572 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002573 LV.moveInto(Info.EvalResult.Val);
2574 } else if (E->getType()->isRealFloatingType()) {
2575 llvm::APFloat F(0.0);
2576 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002577 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002578
John McCallefdb83e2010-05-07 21:00:08 +00002579 Info.EvalResult.Val = APValue(F);
2580 } else if (E->getType()->isAnyComplexType()) {
2581 ComplexValue C;
2582 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002583 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002584 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002585 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002586 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002587
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002588 return true;
2589}
2590
John McCall56ca35d2011-02-17 10:25:35 +00002591/// Evaluate - Return true if this is a constant which we can fold using
2592/// any crazy technique (that has nothing to do with language standards) that
2593/// we want to. If this function returns true, it returns the folded constant
2594/// in Result.
2595bool Expr::Evaluate(EvalResult &Result, const ASTContext &Ctx) const {
2596 EvalInfo Info(Ctx, Result);
2597 return ::Evaluate(Info, this);
2598}
2599
Jay Foad4ba2a172011-01-12 09:06:06 +00002600bool Expr::EvaluateAsBooleanCondition(bool &Result,
2601 const ASTContext &Ctx) const {
John McCallcd7a4452010-01-05 23:42:56 +00002602 EvalResult Scratch;
2603 EvalInfo Info(Ctx, Scratch);
2604
2605 return HandleConversionToBool(this, Result, Info);
2606}
2607
Jay Foad4ba2a172011-01-12 09:06:06 +00002608bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
Anders Carlsson1b782762009-04-10 04:54:13 +00002609 EvalInfo Info(Ctx, Result);
2610
John McCallefdb83e2010-05-07 21:00:08 +00002611 LValue LV;
John McCall42c8f872010-05-10 23:27:23 +00002612 if (EvaluateLValue(this, LV, Info) &&
2613 !Result.HasSideEffects &&
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002614 IsGlobalLValue(LV.Base)) {
2615 LV.moveInto(Result.Val);
2616 return true;
2617 }
2618 return false;
2619}
2620
Jay Foad4ba2a172011-01-12 09:06:06 +00002621bool Expr::EvaluateAsAnyLValue(EvalResult &Result,
2622 const ASTContext &Ctx) const {
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002623 EvalInfo Info(Ctx, Result);
2624
2625 LValue LV;
2626 if (EvaluateLValue(this, LV, Info)) {
John McCallefdb83e2010-05-07 21:00:08 +00002627 LV.moveInto(Result.Val);
2628 return true;
2629 }
2630 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002631}
2632
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002633/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002634/// folded, but discard the result.
Jay Foad4ba2a172011-01-12 09:06:06 +00002635bool Expr::isEvaluatable(const ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002636 EvalResult Result;
2637 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002638}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002639
Jay Foad4ba2a172011-01-12 09:06:06 +00002640bool Expr::HasSideEffects(const ASTContext &Ctx) const {
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002641 Expr::EvalResult Result;
2642 EvalInfo Info(Ctx, Result);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002643 return HasSideEffect(Info).Visit(this);
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002644}
2645
Jay Foad4ba2a172011-01-12 09:06:06 +00002646APSInt Expr::EvaluateAsInt(const ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002647 EvalResult EvalResult;
2648 bool Result = Evaluate(EvalResult, Ctx);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00002649 (void)Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002650 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002651 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002652
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002653 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002654}
John McCalld905f5a2010-05-07 05:32:02 +00002655
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002656 bool Expr::EvalResult::isGlobalLValue() const {
2657 assert(Val.isLValue());
2658 return IsGlobalLValue(Val.getLValueBase());
2659 }
2660
2661
John McCalld905f5a2010-05-07 05:32:02 +00002662/// isIntegerConstantExpr - this recursive routine will test if an expression is
2663/// an integer constant expression.
2664
2665/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2666/// comma, etc
2667///
2668/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2669/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2670/// cast+dereference.
2671
2672// CheckICE - This function does the fundamental ICE checking: the returned
2673// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2674// Note that to reduce code duplication, this helper does no evaluation
2675// itself; the caller checks whether the expression is evaluatable, and
2676// in the rare cases where CheckICE actually cares about the evaluated
2677// value, it calls into Evalute.
2678//
2679// Meanings of Val:
2680// 0: This expression is an ICE if it can be evaluated by Evaluate.
2681// 1: This expression is not an ICE, but if it isn't evaluated, it's
2682// a legal subexpression for an ICE. This return value is used to handle
2683// the comma operator in C99 mode.
2684// 2: This expression is not an ICE, and is not a legal subexpression for one.
2685
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002686namespace {
2687
John McCalld905f5a2010-05-07 05:32:02 +00002688struct ICEDiag {
2689 unsigned Val;
2690 SourceLocation Loc;
2691
2692 public:
2693 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2694 ICEDiag() : Val(0) {}
2695};
2696
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002697}
2698
2699static ICEDiag NoDiag() { return ICEDiag(); }
John McCalld905f5a2010-05-07 05:32:02 +00002700
2701static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2702 Expr::EvalResult EVResult;
2703 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2704 !EVResult.Val.isInt()) {
2705 return ICEDiag(2, E->getLocStart());
2706 }
2707 return NoDiag();
2708}
2709
2710static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2711 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002712 if (!E->getType()->isIntegralOrEnumerationType()) {
John McCalld905f5a2010-05-07 05:32:02 +00002713 return ICEDiag(2, E->getLocStart());
2714 }
2715
2716 switch (E->getStmtClass()) {
John McCall63c00d72011-02-09 08:16:59 +00002717#define ABSTRACT_STMT(Node)
John McCalld905f5a2010-05-07 05:32:02 +00002718#define STMT(Node, Base) case Expr::Node##Class:
2719#define EXPR(Node, Base)
2720#include "clang/AST/StmtNodes.inc"
2721 case Expr::PredefinedExprClass:
2722 case Expr::FloatingLiteralClass:
2723 case Expr::ImaginaryLiteralClass:
2724 case Expr::StringLiteralClass:
2725 case Expr::ArraySubscriptExprClass:
2726 case Expr::MemberExprClass:
2727 case Expr::CompoundAssignOperatorClass:
2728 case Expr::CompoundLiteralExprClass:
2729 case Expr::ExtVectorElementExprClass:
2730 case Expr::InitListExprClass:
2731 case Expr::DesignatedInitExprClass:
2732 case Expr::ImplicitValueInitExprClass:
2733 case Expr::ParenListExprClass:
2734 case Expr::VAArgExprClass:
2735 case Expr::AddrLabelExprClass:
2736 case Expr::StmtExprClass:
2737 case Expr::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002738 case Expr::CUDAKernelCallExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002739 case Expr::CXXDynamicCastExprClass:
2740 case Expr::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002741 case Expr::CXXUuidofExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002742 case Expr::CXXNullPtrLiteralExprClass:
2743 case Expr::CXXThisExprClass:
2744 case Expr::CXXThrowExprClass:
2745 case Expr::CXXNewExprClass:
2746 case Expr::CXXDeleteExprClass:
2747 case Expr::CXXPseudoDestructorExprClass:
2748 case Expr::UnresolvedLookupExprClass:
2749 case Expr::DependentScopeDeclRefExprClass:
2750 case Expr::CXXConstructExprClass:
2751 case Expr::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002752 case Expr::ExprWithCleanupsClass:
John McCalld905f5a2010-05-07 05:32:02 +00002753 case Expr::CXXTemporaryObjectExprClass:
2754 case Expr::CXXUnresolvedConstructExprClass:
2755 case Expr::CXXDependentScopeMemberExprClass:
2756 case Expr::UnresolvedMemberExprClass:
2757 case Expr::ObjCStringLiteralClass:
2758 case Expr::ObjCEncodeExprClass:
2759 case Expr::ObjCMessageExprClass:
2760 case Expr::ObjCSelectorExprClass:
2761 case Expr::ObjCProtocolExprClass:
2762 case Expr::ObjCIvarRefExprClass:
2763 case Expr::ObjCPropertyRefExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002764 case Expr::ObjCIsaExprClass:
2765 case Expr::ShuffleVectorExprClass:
2766 case Expr::BlockExprClass:
2767 case Expr::BlockDeclRefExprClass:
2768 case Expr::NoStmtClass:
John McCall7cd7d1a2010-11-15 23:31:06 +00002769 case Expr::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +00002770 case Expr::PackExpansionExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +00002771 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002772 return ICEDiag(2, E->getLocStart());
2773
Douglas Gregoree8aff02011-01-04 17:33:58 +00002774 case Expr::SizeOfPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002775 case Expr::GNUNullExprClass:
2776 // GCC considers the GNU __null value to be an integral constant expression.
2777 return NoDiag();
2778
2779 case Expr::ParenExprClass:
2780 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00002781 case Expr::GenericSelectionExprClass:
2782 return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002783 case Expr::IntegerLiteralClass:
2784 case Expr::CharacterLiteralClass:
2785 case Expr::CXXBoolLiteralExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +00002786 case Expr::CXXScalarValueInitExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002787 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002788 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002789 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002790 case Expr::ExpressionTraitExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002791 case Expr::CXXNoexceptExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002792 return NoDiag();
2793 case Expr::CallExprClass:
Sean Hunt6cf75022010-08-30 17:47:05 +00002794 case Expr::CXXOperatorCallExprClass: {
John McCalld905f5a2010-05-07 05:32:02 +00002795 const CallExpr *CE = cast<CallExpr>(E);
2796 if (CE->isBuiltinCall(Ctx))
2797 return CheckEvalInICE(E, Ctx);
2798 return ICEDiag(2, E->getLocStart());
2799 }
2800 case Expr::DeclRefExprClass:
2801 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2802 return NoDiag();
2803 if (Ctx.getLangOptions().CPlusPlus &&
2804 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2805 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2806
2807 // Parameter variables are never constants. Without this check,
2808 // getAnyInitializer() can find a default argument, which leads
2809 // to chaos.
2810 if (isa<ParmVarDecl>(D))
2811 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2812
2813 // C++ 7.1.5.1p2
2814 // A variable of non-volatile const-qualified integral or enumeration
2815 // type initialized by an ICE can be used in ICEs.
2816 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2817 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2818 if (Quals.hasVolatile() || !Quals.hasConst())
2819 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2820
2821 // Look for a declaration of this variable that has an initializer.
2822 const VarDecl *ID = 0;
2823 const Expr *Init = Dcl->getAnyInitializer(ID);
2824 if (Init) {
2825 if (ID->isInitKnownICE()) {
2826 // We have already checked whether this subexpression is an
2827 // integral constant expression.
2828 if (ID->isInitICE())
2829 return NoDiag();
2830 else
2831 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2832 }
2833
2834 // It's an ICE whether or not the definition we found is
2835 // out-of-line. See DR 721 and the discussion in Clang PR
2836 // 6206 for details.
2837
2838 if (Dcl->isCheckingICE()) {
2839 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2840 }
2841
2842 Dcl->setCheckingICE();
2843 ICEDiag Result = CheckICE(Init, Ctx);
2844 // Cache the result of the ICE test.
2845 Dcl->setInitKnownICE(Result.Val == 0);
2846 return Result;
2847 }
2848 }
2849 }
2850 return ICEDiag(2, E->getLocStart());
2851 case Expr::UnaryOperatorClass: {
2852 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2853 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002854 case UO_PostInc:
2855 case UO_PostDec:
2856 case UO_PreInc:
2857 case UO_PreDec:
2858 case UO_AddrOf:
2859 case UO_Deref:
John McCalld905f5a2010-05-07 05:32:02 +00002860 return ICEDiag(2, E->getLocStart());
John McCall2de56d12010-08-25 11:45:40 +00002861 case UO_Extension:
2862 case UO_LNot:
2863 case UO_Plus:
2864 case UO_Minus:
2865 case UO_Not:
2866 case UO_Real:
2867 case UO_Imag:
John McCalld905f5a2010-05-07 05:32:02 +00002868 return CheckICE(Exp->getSubExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002869 }
2870
2871 // OffsetOf falls through here.
2872 }
2873 case Expr::OffsetOfExprClass: {
2874 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2875 // Evaluate matches the proposed gcc behavior for cases like
2876 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2877 // compliance: we should warn earlier for offsetof expressions with
2878 // array subscripts that aren't ICEs, and if the array subscripts
2879 // are ICEs, the value of the offsetof must be an integer constant.
2880 return CheckEvalInICE(E, Ctx);
2881 }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002882 case Expr::UnaryExprOrTypeTraitExprClass: {
2883 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
2884 if ((Exp->getKind() == UETT_SizeOf) &&
2885 Exp->getTypeOfArgument()->isVariableArrayType())
John McCalld905f5a2010-05-07 05:32:02 +00002886 return ICEDiag(2, E->getLocStart());
2887 return NoDiag();
2888 }
2889 case Expr::BinaryOperatorClass: {
2890 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2891 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002892 case BO_PtrMemD:
2893 case BO_PtrMemI:
2894 case BO_Assign:
2895 case BO_MulAssign:
2896 case BO_DivAssign:
2897 case BO_RemAssign:
2898 case BO_AddAssign:
2899 case BO_SubAssign:
2900 case BO_ShlAssign:
2901 case BO_ShrAssign:
2902 case BO_AndAssign:
2903 case BO_XorAssign:
2904 case BO_OrAssign:
John McCalld905f5a2010-05-07 05:32:02 +00002905 return ICEDiag(2, E->getLocStart());
2906
John McCall2de56d12010-08-25 11:45:40 +00002907 case BO_Mul:
2908 case BO_Div:
2909 case BO_Rem:
2910 case BO_Add:
2911 case BO_Sub:
2912 case BO_Shl:
2913 case BO_Shr:
2914 case BO_LT:
2915 case BO_GT:
2916 case BO_LE:
2917 case BO_GE:
2918 case BO_EQ:
2919 case BO_NE:
2920 case BO_And:
2921 case BO_Xor:
2922 case BO_Or:
2923 case BO_Comma: {
John McCalld905f5a2010-05-07 05:32:02 +00002924 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2925 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
John McCall2de56d12010-08-25 11:45:40 +00002926 if (Exp->getOpcode() == BO_Div ||
2927 Exp->getOpcode() == BO_Rem) {
John McCalld905f5a2010-05-07 05:32:02 +00002928 // Evaluate gives an error for undefined Div/Rem, so make sure
2929 // we don't evaluate one.
John McCall3b332ab2011-02-26 08:27:17 +00002930 if (LHSResult.Val == 0 && RHSResult.Val == 0) {
John McCalld905f5a2010-05-07 05:32:02 +00002931 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2932 if (REval == 0)
2933 return ICEDiag(1, E->getLocStart());
2934 if (REval.isSigned() && REval.isAllOnesValue()) {
2935 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2936 if (LEval.isMinSignedValue())
2937 return ICEDiag(1, E->getLocStart());
2938 }
2939 }
2940 }
John McCall2de56d12010-08-25 11:45:40 +00002941 if (Exp->getOpcode() == BO_Comma) {
John McCalld905f5a2010-05-07 05:32:02 +00002942 if (Ctx.getLangOptions().C99) {
2943 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2944 // if it isn't evaluated.
2945 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2946 return ICEDiag(1, E->getLocStart());
2947 } else {
2948 // In both C89 and C++, commas in ICEs are illegal.
2949 return ICEDiag(2, E->getLocStart());
2950 }
2951 }
2952 if (LHSResult.Val >= RHSResult.Val)
2953 return LHSResult;
2954 return RHSResult;
2955 }
John McCall2de56d12010-08-25 11:45:40 +00002956 case BO_LAnd:
2957 case BO_LOr: {
John McCalld905f5a2010-05-07 05:32:02 +00002958 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2959 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2960 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2961 // Rare case where the RHS has a comma "side-effect"; we need
2962 // to actually check the condition to see whether the side
2963 // with the comma is evaluated.
John McCall2de56d12010-08-25 11:45:40 +00002964 if ((Exp->getOpcode() == BO_LAnd) !=
John McCalld905f5a2010-05-07 05:32:02 +00002965 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2966 return RHSResult;
2967 return NoDiag();
2968 }
2969
2970 if (LHSResult.Val >= RHSResult.Val)
2971 return LHSResult;
2972 return RHSResult;
2973 }
2974 }
2975 }
2976 case Expr::ImplicitCastExprClass:
2977 case Expr::CStyleCastExprClass:
2978 case Expr::CXXFunctionalCastExprClass:
2979 case Expr::CXXStaticCastExprClass:
2980 case Expr::CXXReinterpretCastExprClass:
2981 case Expr::CXXConstCastExprClass: {
2982 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002983 if (SubExpr->getType()->isIntegralOrEnumerationType())
John McCalld905f5a2010-05-07 05:32:02 +00002984 return CheckICE(SubExpr, Ctx);
2985 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2986 return NoDiag();
2987 return ICEDiag(2, E->getLocStart());
2988 }
John McCall56ca35d2011-02-17 10:25:35 +00002989 case Expr::BinaryConditionalOperatorClass: {
2990 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
2991 ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
2992 if (CommonResult.Val == 2) return CommonResult;
2993 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2994 if (FalseResult.Val == 2) return FalseResult;
2995 if (CommonResult.Val == 1) return CommonResult;
2996 if (FalseResult.Val == 1 &&
2997 Exp->getCommon()->EvaluateAsInt(Ctx) == 0) return NoDiag();
2998 return FalseResult;
2999 }
John McCalld905f5a2010-05-07 05:32:02 +00003000 case Expr::ConditionalOperatorClass: {
3001 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
3002 // If the condition (ignoring parens) is a __builtin_constant_p call,
3003 // then only the true side is actually considered in an integer constant
3004 // expression, and it is fully evaluated. This is an important GNU
3005 // extension. See GCC PR38377 for discussion.
3006 if (const CallExpr *CallCE
3007 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
3008 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
3009 Expr::EvalResult EVResult;
3010 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
3011 !EVResult.Val.isInt()) {
3012 return ICEDiag(2, E->getLocStart());
3013 }
3014 return NoDiag();
3015 }
3016 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
3017 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
3018 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3019 if (CondResult.Val == 2)
3020 return CondResult;
3021 if (TrueResult.Val == 2)
3022 return TrueResult;
3023 if (FalseResult.Val == 2)
3024 return FalseResult;
3025 if (CondResult.Val == 1)
3026 return CondResult;
3027 if (TrueResult.Val == 0 && FalseResult.Val == 0)
3028 return NoDiag();
3029 // Rare case where the diagnostics depend on which side is evaluated
3030 // Note that if we get here, CondResult is 0, and at least one of
3031 // TrueResult and FalseResult is non-zero.
3032 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
3033 return FalseResult;
3034 }
3035 return TrueResult;
3036 }
3037 case Expr::CXXDefaultArgExprClass:
3038 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
3039 case Expr::ChooseExprClass: {
3040 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
3041 }
3042 }
3043
3044 // Silence a GCC warning
3045 return ICEDiag(2, E->getLocStart());
3046}
3047
3048bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
3049 SourceLocation *Loc, bool isEvaluated) const {
3050 ICEDiag d = CheckICE(this, Ctx);
3051 if (d.Val != 0) {
3052 if (Loc) *Loc = d.Loc;
3053 return false;
3054 }
3055 EvalResult EvalResult;
3056 if (!Evaluate(EvalResult, Ctx))
3057 llvm_unreachable("ICE cannot be evaluated!");
3058 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
3059 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
3060 Result = EvalResult.Val.getInt();
3061 return true;
3062}