blob: cdd7efaaf555561c1608710ba167d95468f30353 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
John McCallf4cf1a12010-05-07 17:22:02 +000045namespace {
Benjamin Kramerc54061a2011-03-04 13:12:48 +000046 struct EvalInfo {
47 const ASTContext &Ctx;
48
49 /// EvalResult - Contains information about the evaluation.
50 Expr::EvalResult &EvalResult;
51
52 typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy;
53 MapTy OpaqueValues;
54 const APValue *getOpaqueValue(const OpaqueValueExpr *e) const {
55 MapTy::const_iterator i = OpaqueValues.find(e);
56 if (i == OpaqueValues.end()) return 0;
57 return &i->second;
58 }
59
60 EvalInfo(const ASTContext &ctx, Expr::EvalResult &evalresult)
61 : Ctx(ctx), EvalResult(evalresult) {}
62 };
63
John McCallf4cf1a12010-05-07 17:22:02 +000064 struct ComplexValue {
65 private:
66 bool IsInt;
67
68 public:
69 APSInt IntReal, IntImag;
70 APFloat FloatReal, FloatImag;
71
72 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
73
74 void makeComplexFloat() { IsInt = false; }
75 bool isComplexFloat() const { return !IsInt; }
76 APFloat &getComplexFloatReal() { return FloatReal; }
77 APFloat &getComplexFloatImag() { return FloatImag; }
78
79 void makeComplexInt() { IsInt = true; }
80 bool isComplexInt() const { return IsInt; }
81 APSInt &getComplexIntReal() { return IntReal; }
82 APSInt &getComplexIntImag() { return IntImag; }
83
John McCall56ca35d2011-02-17 10:25:35 +000084 void moveInto(APValue &v) const {
John McCallf4cf1a12010-05-07 17:22:02 +000085 if (isComplexFloat())
86 v = APValue(FloatReal, FloatImag);
87 else
88 v = APValue(IntReal, IntImag);
89 }
John McCall56ca35d2011-02-17 10:25:35 +000090 void setFrom(const APValue &v) {
91 assert(v.isComplexFloat() || v.isComplexInt());
92 if (v.isComplexFloat()) {
93 makeComplexFloat();
94 FloatReal = v.getComplexFloatReal();
95 FloatImag = v.getComplexFloatImag();
96 } else {
97 makeComplexInt();
98 IntReal = v.getComplexIntReal();
99 IntImag = v.getComplexIntImag();
100 }
101 }
John McCallf4cf1a12010-05-07 17:22:02 +0000102 };
John McCallefdb83e2010-05-07 21:00:08 +0000103
104 struct LValue {
105 Expr *Base;
106 CharUnits Offset;
107
108 Expr *getLValueBase() { return Base; }
109 CharUnits getLValueOffset() { return Offset; }
110
John McCall56ca35d2011-02-17 10:25:35 +0000111 void moveInto(APValue &v) const {
John McCallefdb83e2010-05-07 21:00:08 +0000112 v = APValue(Base, Offset);
113 }
John McCall56ca35d2011-02-17 10:25:35 +0000114 void setFrom(const APValue &v) {
115 assert(v.isLValue());
116 Base = v.getLValueBase();
117 Offset = v.getLValueOffset();
118 }
John McCallefdb83e2010-05-07 21:00:08 +0000119 };
John McCallf4cf1a12010-05-07 17:22:02 +0000120}
Chris Lattner87eae5e2008-07-11 22:52:41 +0000121
John McCall56ca35d2011-02-17 10:25:35 +0000122static bool Evaluate(EvalInfo &info, const Expr *E);
John McCallefdb83e2010-05-07 21:00:08 +0000123static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
124static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000125static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +0000126static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
127 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000128static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCallf4cf1a12010-05-07 17:22:02 +0000129static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000130
131//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000132// Misc utilities
133//===----------------------------------------------------------------------===//
134
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000135static bool IsGlobalLValue(const Expr* E) {
John McCall42c8f872010-05-10 23:27:23 +0000136 if (!E) return true;
137
138 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
139 if (isa<FunctionDecl>(DRE->getDecl()))
140 return true;
141 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
142 return VD->hasGlobalStorage();
143 return false;
144 }
145
146 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
147 return CLE->isFileScope();
148
149 return true;
150}
151
John McCallefdb83e2010-05-07 21:00:08 +0000152static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
153 const Expr* Base = Value.Base;
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000154
John McCall35542832010-05-07 21:34:32 +0000155 // A null base expression indicates a null pointer. These are always
156 // evaluatable, and they are false unless the offset is zero.
157 if (!Base) {
158 Result = !Value.Offset.isZero();
159 return true;
160 }
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000161
John McCall42c8f872010-05-10 23:27:23 +0000162 // Require the base expression to be a global l-value.
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000163 if (!IsGlobalLValue(Base)) return false;
John McCall42c8f872010-05-10 23:27:23 +0000164
John McCall35542832010-05-07 21:34:32 +0000165 // We have a non-null base expression. These are generally known to
166 // be true, but if it'a decl-ref to a weak symbol it can be null at
167 // runtime.
John McCall35542832010-05-07 21:34:32 +0000168 Result = true;
169
170 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000171 if (!DeclRef)
172 return true;
173
John McCall35542832010-05-07 21:34:32 +0000174 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000175 const ValueDecl* Decl = DeclRef->getDecl();
176 if (Decl->hasAttr<WeakAttr>() ||
177 Decl->hasAttr<WeakRefAttr>() ||
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000178 Decl->isWeakImported())
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000179 return false;
180
Eli Friedman5bc86102009-06-14 02:17:33 +0000181 return true;
182}
183
John McCallcd7a4452010-01-05 23:42:56 +0000184static bool HandleConversionToBool(const Expr* E, bool& Result,
185 EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000186 if (E->getType()->isIntegralOrEnumerationType()) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000187 APSInt IntResult;
188 if (!EvaluateInteger(E, IntResult, Info))
189 return false;
190 Result = IntResult != 0;
191 return true;
192 } else if (E->getType()->isRealFloatingType()) {
193 APFloat FloatResult(0.0);
194 if (!EvaluateFloat(E, FloatResult, Info))
195 return false;
196 Result = !FloatResult.isZero();
197 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000198 } else if (E->getType()->hasPointerRepresentation()) {
John McCallefdb83e2010-05-07 21:00:08 +0000199 LValue PointerResult;
Eli Friedman4efaa272008-11-12 09:44:48 +0000200 if (!EvaluatePointer(E, PointerResult, Info))
201 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +0000202 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +0000203 } else if (E->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +0000204 ComplexValue ComplexResult;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000205 if (!EvaluateComplex(E, ComplexResult, Info))
206 return false;
207 if (ComplexResult.isComplexFloat()) {
208 Result = !ComplexResult.getComplexFloatReal().isZero() ||
209 !ComplexResult.getComplexFloatImag().isZero();
210 } else {
211 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
212 ComplexResult.getComplexIntImag().getBoolValue();
213 }
214 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 }
216
217 return false;
218}
219
Mike Stump1eb44332009-09-09 15:08:12 +0000220static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000221 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000222 unsigned DestWidth = Ctx.getIntWidth(DestType);
223 // Determine whether we are converting to unsigned or signed.
224 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000226 // FIXME: Warning for overflow.
227 uint64_t Space[4];
228 bool ignored;
229 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
230 llvm::APFloat::rmTowardZero, &ignored);
231 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
232}
233
Mike Stump1eb44332009-09-09 15:08:12 +0000234static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000235 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000236 bool ignored;
237 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000239 APFloat::rmNearestTiesToEven, &ignored);
240 return Result;
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000244 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000245 unsigned DestWidth = Ctx.getIntWidth(DestType);
246 APSInt Result = Value;
247 // Figure out if this is a truncate, extend or noop cast.
248 // If the input is signed, do a sign extend, noop, or truncate.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000249 Result = Result.extOrTrunc(DestWidth);
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000250 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
251 return Result;
252}
253
Mike Stump1eb44332009-09-09 15:08:12 +0000254static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000255 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000256
257 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
258 Result.convertFromAPInt(Value, Value.isSigned(),
259 APFloat::rmNearestTiesToEven);
260 return Result;
261}
262
Mike Stumpc4c90452009-10-27 22:09:17 +0000263namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000264class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000265 : public StmtVisitor<HasSideEffect, bool> {
266 EvalInfo &Info;
267public:
268
269 HasSideEffect(EvalInfo &info) : Info(info) {}
270
271 // Unhandled nodes conservatively default to having side effects.
272 bool VisitStmt(Stmt *S) {
273 return true;
274 }
275
276 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
277 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000278 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000279 return true;
280 return false;
281 }
282 // We don't want to evaluate BlockExprs multiple times, as they generate
283 // a ton of code.
284 bool VisitBlockExpr(BlockExpr *E) { return true; }
285 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
286 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
287 { return Visit(E->getInitializer()); }
288 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
289 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
290 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
291 bool VisitStringLiteral(StringLiteral *E) { return false; }
292 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000293 bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E)
294 { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000295 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000296 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000297 bool VisitChooseExpr(ChooseExpr *E)
298 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
299 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
300 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000301 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000302 bool VisitBinaryOperator(BinaryOperator *E)
303 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000304 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
305 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
306 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
307 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
308 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000309 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000310 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000311 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000312 }
313 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000314
315 // Has side effects if any element does.
316 bool VisitInitListExpr(InitListExpr *E) {
317 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
318 if (Visit(E->getInit(i))) return true;
319 return false;
320 }
Douglas Gregoree8aff02011-01-04 17:33:58 +0000321
322 bool VisitSizeOfPackExpr(SizeOfPackExpr *) { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000323};
324
John McCall56ca35d2011-02-17 10:25:35 +0000325class OpaqueValueEvaluation {
326 EvalInfo &info;
327 OpaqueValueExpr *opaqueValue;
328
329public:
330 OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
331 Expr *value)
332 : info(info), opaqueValue(opaqueValue) {
333
334 // If evaluation fails, fail immediately.
335 if (!Evaluate(info, value)) {
336 this->opaqueValue = 0;
337 return;
338 }
339 info.OpaqueValues[opaqueValue] = info.EvalResult.Val;
340 }
341
342 bool hasError() const { return opaqueValue == 0; }
343
344 ~OpaqueValueEvaluation() {
345 if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
346 }
347};
348
Mike Stumpc4c90452009-10-27 22:09:17 +0000349} // end anonymous namespace
350
Eli Friedman4efaa272008-11-12 09:44:48 +0000351//===----------------------------------------------------------------------===//
352// LValue Evaluation
353//===----------------------------------------------------------------------===//
354namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000355class LValueExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000356 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman4efaa272008-11-12 09:44:48 +0000357 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000358 LValue &Result;
359
360 bool Success(Expr *E) {
361 Result.Base = E;
362 Result.Offset = CharUnits::Zero();
363 return true;
364 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000365public:
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCallefdb83e2010-05-07 21:00:08 +0000367 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
368 Info(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000369
John McCallefdb83e2010-05-07 21:00:08 +0000370 bool VisitStmt(Stmt *S) {
371 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000372 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000373
John McCallefdb83e2010-05-07 21:00:08 +0000374 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
375 bool VisitDeclRefExpr(DeclRefExpr *E);
376 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
377 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
378 bool VisitMemberExpr(MemberExpr *E);
379 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
380 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
381 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
382 bool VisitUnaryDeref(UnaryOperator *E);
383 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000384 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000385 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000386 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000387
John McCallefdb83e2010-05-07 21:00:08 +0000388 bool VisitCastExpr(CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000389 switch (E->getCastKind()) {
390 default:
John McCallefdb83e2010-05-07 21:00:08 +0000391 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000392
John McCall2de56d12010-08-25 11:45:40 +0000393 case CK_NoOp:
Anders Carlsson26bc2202009-10-03 16:30:22 +0000394 return Visit(E->getSubExpr());
395 }
396 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000397 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000398};
399} // end anonymous namespace
400
John McCallefdb83e2010-05-07 21:00:08 +0000401static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
402 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman4efaa272008-11-12 09:44:48 +0000403}
404
John McCallefdb83e2010-05-07 21:00:08 +0000405bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000406 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000407 return Success(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000408 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
409 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000410 return Success(E);
Chandler Carruth761c94e2010-05-16 09:32:51 +0000411 // Reference parameters can refer to anything even if they have an
412 // "initializer" in the form of a default argument.
413 if (isa<ParmVarDecl>(VD))
414 return false;
Eli Friedmand933a012009-08-29 19:09:59 +0000415 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000416 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000417 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000418 }
419
John McCallefdb83e2010-05-07 21:00:08 +0000420 return false;
Anders Carlsson35873c42008-11-24 04:41:22 +0000421}
422
John McCallefdb83e2010-05-07 21:00:08 +0000423bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000424 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000425}
426
John McCallefdb83e2010-05-07 21:00:08 +0000427bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000428 QualType Ty;
429 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000430 if (!EvaluatePointer(E->getBase(), Result, Info))
431 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000432 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000433 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000434 if (!Visit(E->getBase()))
435 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000436 Ty = E->getBase()->getType();
437 }
438
Ted Kremenek6217b802009-07-29 21:53:49 +0000439 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000440 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000441
442 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
443 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000444 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000445
446 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000447 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000448
Eli Friedman4efaa272008-11-12 09:44:48 +0000449 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000450 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000451 for (RecordDecl::field_iterator Field = RD->field_begin(),
452 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000453 Field != FieldEnd; (void)++Field, ++i) {
454 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000455 break;
456 }
457
Ken Dyckfb1e3bc2011-01-18 01:56:16 +0000458 Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
John McCallefdb83e2010-05-07 21:00:08 +0000459 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000460}
461
John McCallefdb83e2010-05-07 21:00:08 +0000462bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000463 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000464 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Anders Carlsson3068d112008-11-16 19:01:22 +0000466 APSInt Index;
467 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000468 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000469
Ken Dyck199c3d62010-01-11 17:06:35 +0000470 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000471 Result.Offset += Index.getSExtValue() * ElementSize;
472 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000473}
Eli Friedman4efaa272008-11-12 09:44:48 +0000474
John McCallefdb83e2010-05-07 21:00:08 +0000475bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
476 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000477}
478
Eli Friedman4efaa272008-11-12 09:44:48 +0000479//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000480// Pointer Evaluation
481//===----------------------------------------------------------------------===//
482
Anders Carlssonc754aa62008-07-08 05:13:58 +0000483namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000484class PointerExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000485 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000486 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000487 LValue &Result;
488
489 bool Success(Expr *E) {
490 Result.Base = E;
491 Result.Offset = CharUnits::Zero();
492 return true;
493 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000494public:
Mike Stump1eb44332009-09-09 15:08:12 +0000495
John McCallefdb83e2010-05-07 21:00:08 +0000496 PointerExprEvaluator(EvalInfo &info, LValue &Result)
497 : Info(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000498
John McCallefdb83e2010-05-07 21:00:08 +0000499 bool VisitStmt(Stmt *S) {
500 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000501 }
502
John McCallefdb83e2010-05-07 21:00:08 +0000503 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000504
John McCallefdb83e2010-05-07 21:00:08 +0000505 bool VisitBinaryOperator(const BinaryOperator *E);
506 bool VisitCastExpr(CastExpr* E);
507 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman2217c872009-02-22 11:46:18 +0000508 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000509 bool VisitUnaryAddrOf(const UnaryOperator *E);
510 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
511 { return Success(E); }
512 bool VisitAddrLabelExpr(AddrLabelExpr *E)
513 { return Success(E); }
514 bool VisitCallExpr(CallExpr *E);
515 bool VisitBlockExpr(BlockExpr *E) {
John McCall469a1eb2011-02-02 13:00:07 +0000516 if (!E->getBlockDecl()->hasCaptures())
John McCallefdb83e2010-05-07 21:00:08 +0000517 return Success(E);
518 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000519 }
John McCallefdb83e2010-05-07 21:00:08 +0000520 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
521 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000522 bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
John McCallefdb83e2010-05-07 21:00:08 +0000523 bool VisitConditionalOperator(ConditionalOperator *E);
524 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000525 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallefdb83e2010-05-07 21:00:08 +0000526 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
527 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000528
529 bool VisitOpaqueValueExpr(OpaqueValueExpr *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000530 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000531};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000532} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000533
John McCallefdb83e2010-05-07 21:00:08 +0000534static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000535 assert(E->getType()->hasPointerRepresentation());
John McCallefdb83e2010-05-07 21:00:08 +0000536 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000537}
538
John McCallefdb83e2010-05-07 21:00:08 +0000539bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000540 if (E->getOpcode() != BO_Add &&
541 E->getOpcode() != BO_Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000542 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000544 const Expr *PExp = E->getLHS();
545 const Expr *IExp = E->getRHS();
546 if (IExp->getType()->isPointerType())
547 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
John McCallefdb83e2010-05-07 21:00:08 +0000549 if (!EvaluatePointer(PExp, Result, Info))
550 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
John McCallefdb83e2010-05-07 21:00:08 +0000552 llvm::APSInt Offset;
553 if (!EvaluateInteger(IExp, Offset, Info))
554 return false;
555 int64_t AdditionalOffset
556 = Offset.isSigned() ? Offset.getSExtValue()
557 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000558
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000559 // Compute the new offset in the appropriate width.
560
561 QualType PointeeType =
562 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000563 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000565 // Explicitly handle GNU void* and function pointer arithmetic extensions.
566 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000567 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000568 else
John McCallefdb83e2010-05-07 21:00:08 +0000569 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000570
John McCall2de56d12010-08-25 11:45:40 +0000571 if (E->getOpcode() == BO_Add)
John McCallefdb83e2010-05-07 21:00:08 +0000572 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000573 else
John McCallefdb83e2010-05-07 21:00:08 +0000574 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000575
John McCallefdb83e2010-05-07 21:00:08 +0000576 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000577}
Eli Friedman4efaa272008-11-12 09:44:48 +0000578
John McCallefdb83e2010-05-07 21:00:08 +0000579bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
580 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000581}
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000583
John McCallefdb83e2010-05-07 21:00:08 +0000584bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000585 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000586
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000587 switch (E->getCastKind()) {
588 default:
589 break;
590
John McCall2de56d12010-08-25 11:45:40 +0000591 case CK_NoOp:
592 case CK_BitCast:
John McCall2de56d12010-08-25 11:45:40 +0000593 case CK_AnyPointerToObjCPointerCast:
594 case CK_AnyPointerToBlockPointerCast:
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000595 return Visit(SubExpr);
596
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000597 case CK_DerivedToBase:
598 case CK_UncheckedDerivedToBase: {
599 LValue BaseLV;
600 if (!EvaluatePointer(E->getSubExpr(), BaseLV, Info))
601 return false;
602
603 // Now figure out the necessary offset to add to the baseLV to get from
604 // the derived class to the base class.
Ken Dyck7c7f8202011-01-26 02:17:08 +0000605 CharUnits Offset = CharUnits::Zero();
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000606
607 QualType Ty = E->getSubExpr()->getType();
608 const CXXRecordDecl *DerivedDecl =
609 Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl();
610
611 for (CastExpr::path_const_iterator PathI = E->path_begin(),
612 PathE = E->path_end(); PathI != PathE; ++PathI) {
613 const CXXBaseSpecifier *Base = *PathI;
614
615 // FIXME: If the base is virtual, we'd need to determine the type of the
616 // most derived class and we don't support that right now.
617 if (Base->isVirtual())
618 return false;
619
620 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
621 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
622
Ken Dyck7c7f8202011-01-26 02:17:08 +0000623 Offset += Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000624 DerivedDecl = BaseDecl;
625 }
626
627 Result.Base = BaseLV.getLValueBase();
Ken Dyck7c7f8202011-01-26 02:17:08 +0000628 Result.Offset = BaseLV.getLValueOffset() + Offset;
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000629 return true;
630 }
631
John McCall404cd162010-11-13 01:35:44 +0000632 case CK_NullToPointer: {
633 Result.Base = 0;
634 Result.Offset = CharUnits::Zero();
635 return true;
636 }
637
John McCall2de56d12010-08-25 11:45:40 +0000638 case CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000639 APValue Value;
640 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000641 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000642
John McCallefdb83e2010-05-07 21:00:08 +0000643 if (Value.isInt()) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000644 Value.getInt() = Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
John McCallefdb83e2010-05-07 21:00:08 +0000645 Result.Base = 0;
646 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
647 return true;
648 } else {
649 // Cast is of an lvalue, no need to change value.
650 Result.Base = Value.getLValueBase();
651 Result.Offset = Value.getLValueOffset();
652 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000653 }
654 }
John McCall2de56d12010-08-25 11:45:40 +0000655 case CK_ArrayToPointerDecay:
656 case CK_FunctionToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000657 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000658 }
659
John McCallefdb83e2010-05-07 21:00:08 +0000660 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000661}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000662
John McCallefdb83e2010-05-07 21:00:08 +0000663bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000664 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000665 Builtin::BI__builtin___CFStringMakeConstantString ||
666 E->isBuiltinCall(Info.Ctx) ==
667 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000668 return Success(E);
669 return false;
Eli Friedman3941b182009-01-25 01:54:01 +0000670}
671
John McCall56ca35d2011-02-17 10:25:35 +0000672bool PointerExprEvaluator::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
673 const APValue *value = Info.getOpaqueValue(e);
674 if (!value)
675 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
676 Result.setFrom(*value);
677 return true;
678}
679
680bool PointerExprEvaluator::
681VisitBinaryConditionalOperator(BinaryConditionalOperator *e) {
682 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
683 if (opaque.hasError()) return false;
684
685 bool cond;
686 if (!HandleConversionToBool(e->getCond(), cond, Info))
687 return false;
688
689 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
690}
691
John McCallefdb83e2010-05-07 21:00:08 +0000692bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000693 bool BoolResult;
694 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000695 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000696
697 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCallefdb83e2010-05-07 21:00:08 +0000698 return Visit(EvalExpr);
Eli Friedman4efaa272008-11-12 09:44:48 +0000699}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000700
701//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000702// Vector Evaluation
703//===----------------------------------------------------------------------===//
704
705namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000706 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000707 : public StmtVisitor<VectorExprEvaluator, APValue> {
708 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000709 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000710 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Nate Begeman59b5da62009-01-18 03:20:47 +0000712 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Nate Begeman59b5da62009-01-18 03:20:47 +0000714 APValue VisitStmt(Stmt *S) {
715 return APValue();
716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Eli Friedman91110ee2009-02-23 04:23:56 +0000718 APValue VisitParenExpr(ParenExpr *E)
719 { return Visit(E->getSubExpr()); }
720 APValue VisitUnaryExtension(const UnaryOperator *E)
721 { return Visit(E->getSubExpr()); }
722 APValue VisitUnaryPlus(const UnaryOperator *E)
723 { return Visit(E->getSubExpr()); }
724 APValue VisitUnaryReal(const UnaryOperator *E)
725 { return Visit(E->getSubExpr()); }
726 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
727 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000728 APValue VisitCastExpr(const CastExpr* E);
729 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
730 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000731 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000732 APValue VisitChooseExpr(const ChooseExpr *E)
733 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000734 APValue VisitUnaryImag(const UnaryOperator *E);
735 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000736 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000737 // shufflevector, ExtVectorElementExpr
738 // (Note that these require implementing conversions
739 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000740 };
741} // end anonymous namespace
742
743static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
744 if (!E->getType()->isVectorType())
745 return false;
746 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
747 return !Result.isUninit();
748}
749
750APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000751 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000752 QualType EltTy = VTy->getElementType();
753 unsigned NElts = VTy->getNumElements();
754 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Nate Begeman59b5da62009-01-18 03:20:47 +0000756 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000757 QualType SETy = SE->getType();
Nate Begeman59b5da62009-01-18 03:20:47 +0000758
Eli Friedman46a52322011-03-25 00:43:55 +0000759 switch (E->getCastKind()) {
760 case CK_VectorSplat: {
761 APValue Result = APValue();
762 if (SETy->isIntegerType()) {
763 APSInt IntResult;
764 if (!EvaluateInteger(SE, IntResult, Info))
765 return APValue();
766 Result = APValue(IntResult);
767 } else if (SETy->isRealFloatingType()) {
768 APFloat F(0.0);
769 if (!EvaluateFloat(SE, F, Info))
770 return APValue();
771 Result = APValue(F);
772 } else {
Anders Carlsson0254e702011-03-25 11:22:47 +0000773 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000774 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000775
776 // Splat and create vector APValue.
777 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
778 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000779 }
Eli Friedman46a52322011-03-25 00:43:55 +0000780 case CK_BitCast: {
781 if (SETy->isVectorType())
782 return Visit(const_cast<Expr*>(SE));
Nate Begemanc0b8b192009-07-01 07:50:47 +0000783
Eli Friedman46a52322011-03-25 00:43:55 +0000784 if (!SETy->isIntegerType())
Anders Carlsson0254e702011-03-25 11:22:47 +0000785 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Eli Friedman46a52322011-03-25 00:43:55 +0000787 APSInt Init;
788 if (!EvaluateInteger(SE, Init, Info))
Nate Begemanc0b8b192009-07-01 07:50:47 +0000789 return APValue();
790
Eli Friedman46a52322011-03-25 00:43:55 +0000791 assert((EltTy->isIntegerType() || EltTy->isRealFloatingType()) &&
792 "Vectors must be composed of ints or floats");
793
794 llvm::SmallVector<APValue, 4> Elts;
795 for (unsigned i = 0; i != NElts; ++i) {
796 APSInt Tmp = Init.extOrTrunc(EltWidth);
797
798 if (EltTy->isIntegerType())
799 Elts.push_back(APValue(Tmp));
800 else
801 Elts.push_back(APValue(APFloat(Tmp)));
802
803 Init >>= EltWidth;
804 }
805 return APValue(&Elts[0], Elts.size());
Nate Begemanc0b8b192009-07-01 07:50:47 +0000806 }
Eli Friedman46a52322011-03-25 00:43:55 +0000807 case CK_LValueToRValue:
808 case CK_NoOp:
809 return Visit(const_cast<Expr*>(SE));
810 default:
Anders Carlsson0254e702011-03-25 11:22:47 +0000811 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000812 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000813}
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000816VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
817 return this->Visit(const_cast<Expr*>(E->getInitializer()));
818}
819
Mike Stump1eb44332009-09-09 15:08:12 +0000820APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000821VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000822 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000823 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000824 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Nate Begeman59b5da62009-01-18 03:20:47 +0000826 QualType EltTy = VT->getElementType();
827 llvm::SmallVector<APValue, 4> Elements;
828
John McCalla7d6c222010-06-11 17:54:15 +0000829 // If a vector is initialized with a single element, that value
830 // becomes every element of the vector, not just the first.
831 // This is the behavior described in the IBM AltiVec documentation.
832 if (NumInits == 1) {
833 APValue InitValue;
Nate Begeman59b5da62009-01-18 03:20:47 +0000834 if (EltTy->isIntegerType()) {
835 llvm::APSInt sInt(32);
John McCalla7d6c222010-06-11 17:54:15 +0000836 if (!EvaluateInteger(E->getInit(0), sInt, Info))
837 return APValue();
838 InitValue = APValue(sInt);
Nate Begeman59b5da62009-01-18 03:20:47 +0000839 } else {
840 llvm::APFloat f(0.0);
John McCalla7d6c222010-06-11 17:54:15 +0000841 if (!EvaluateFloat(E->getInit(0), f, Info))
842 return APValue();
843 InitValue = APValue(f);
844 }
845 for (unsigned i = 0; i < NumElements; i++) {
846 Elements.push_back(InitValue);
847 }
848 } else {
849 for (unsigned i = 0; i < NumElements; i++) {
850 if (EltTy->isIntegerType()) {
851 llvm::APSInt sInt(32);
852 if (i < NumInits) {
853 if (!EvaluateInteger(E->getInit(i), sInt, Info))
854 return APValue();
855 } else {
856 sInt = Info.Ctx.MakeIntValue(0, EltTy);
857 }
858 Elements.push_back(APValue(sInt));
Eli Friedman91110ee2009-02-23 04:23:56 +0000859 } else {
John McCalla7d6c222010-06-11 17:54:15 +0000860 llvm::APFloat f(0.0);
861 if (i < NumInits) {
862 if (!EvaluateFloat(E->getInit(i), f, Info))
863 return APValue();
864 } else {
865 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
866 }
867 Elements.push_back(APValue(f));
Eli Friedman91110ee2009-02-23 04:23:56 +0000868 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000869 }
870 }
871 return APValue(&Elements[0], Elements.size());
872}
873
Mike Stump1eb44332009-09-09 15:08:12 +0000874APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000875VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000876 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000877 QualType EltTy = VT->getElementType();
878 APValue ZeroElement;
879 if (EltTy->isIntegerType())
880 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
881 else
882 ZeroElement =
883 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
884
885 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
886 return APValue(&Elements[0], Elements.size());
887}
888
889APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
890 bool BoolResult;
891 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
892 return APValue();
893
894 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
895
896 APValue Result;
897 if (EvaluateVector(EvalExpr, Result, Info))
898 return Result;
899 return APValue();
900}
901
Eli Friedman91110ee2009-02-23 04:23:56 +0000902APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
903 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
904 Info.EvalResult.HasSideEffects = true;
905 return GetZeroVector(E->getType());
906}
907
Nate Begeman59b5da62009-01-18 03:20:47 +0000908//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000909// Integer Evaluation
910//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000911
912namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000913class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000914 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000915 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000916 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000917public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000918 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000919 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000920
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000921 bool Success(const llvm::APSInt &SI, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000922 assert(E->getType()->isIntegralOrEnumerationType() &&
923 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000924 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000925 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000926 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000927 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000928 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000929 return true;
930 }
931
Daniel Dunbar131eb432009-02-19 09:06:44 +0000932 bool Success(const llvm::APInt &I, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000933 assert(E->getType()->isIntegralOrEnumerationType() &&
934 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000935 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000936 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000937 Result = APValue(APSInt(I));
938 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000939 return true;
940 }
941
942 bool Success(uint64_t Value, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000943 assert(E->getType()->isIntegralOrEnumerationType() &&
944 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000945 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000946 return true;
947 }
948
Ken Dyck4f3bc8f2011-03-11 02:13:43 +0000949 bool Success(CharUnits Size, const Expr *E) {
950 return Success(Size.getQuantity(), E);
951 }
952
953
Anders Carlsson82206e22008-11-30 18:14:57 +0000954 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000955 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000956 if (Info.EvalResult.Diag == 0) {
957 Info.EvalResult.DiagLoc = L;
958 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000959 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000960 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000961 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Anders Carlssonc754aa62008-07-08 05:13:58 +0000964 //===--------------------------------------------------------------------===//
965 // Visitor Methods
966 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner32fea9d2008-11-12 07:43:42 +0000968 bool VisitStmt(Stmt *) {
969 assert(0 && "This should be called on integers, stmts are not integers");
970 return false;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner32fea9d2008-11-12 07:43:42 +0000973 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000974 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattnerb542afe2008-07-11 19:10:17 +0000977 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000978
Chris Lattner4c4867e2008-07-12 00:38:25 +0000979 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000980 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000981 }
982 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000983 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000984 }
Eli Friedman04309752009-11-24 05:28:59 +0000985
John McCall56ca35d2011-02-17 10:25:35 +0000986 bool VisitOpaqueValueExpr(OpaqueValueExpr *e) {
987 const APValue *value = Info.getOpaqueValue(e);
988 if (!value) {
989 if (e->getSourceExpr()) return Visit(e->getSourceExpr());
990 return Error(e->getExprLoc(), diag::note_invalid_subexpr_in_ice, e);
991 }
992 return Success(value->getInt(), e);
993 }
994
Eli Friedman04309752009-11-24 05:28:59 +0000995 bool CheckReferencedDecl(const Expr *E, const Decl *D);
996 bool VisitDeclRefExpr(const DeclRefExpr *E) {
997 return CheckReferencedDecl(E, E->getDecl());
998 }
999 bool VisitMemberExpr(const MemberExpr *E) {
1000 if (CheckReferencedDecl(E, E->getMemberDecl())) {
1001 // Conservatively assume a MemberExpr will have side-effects
1002 Info.EvalResult.HasSideEffects = true;
1003 return true;
1004 }
1005 return false;
1006 }
1007
Eli Friedmanc4a26382010-02-13 00:10:10 +00001008 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001009 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001010 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001011 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001012 bool VisitConditionalOperator(const ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00001013 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +00001014
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001015 bool VisitCastExpr(CastExpr* E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001016 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Sebastian Redl05189992008-11-11 17:56:53 +00001017
Anders Carlsson3068d112008-11-16 19:01:22 +00001018 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001019 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Anders Carlsson3f704562008-12-21 22:39:40 +00001022 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001023 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +00001024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregored8abf12010-07-08 06:14:04 +00001026 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001027 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001028 }
1029
Eli Friedman664a1042009-02-27 04:45:43 +00001030 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
1031 return Success(0, E);
1032 }
1033
Sebastian Redl64b45f72009-01-05 20:52:13 +00001034 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Sebastian Redl0dfd8482010-09-13 20:56:31 +00001035 return Success(E->getValue(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001036 }
1037
Francois Pichet6ad6f282010-12-07 00:08:36 +00001038 bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
1039 return Success(E->getValue(), E);
1040 }
1041
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001042 bool VisitChooseExpr(const ChooseExpr *E) {
1043 return Visit(E->getChosenSubExpr(Info.Ctx));
1044 }
1045
Eli Friedman722c7172009-02-28 03:59:05 +00001046 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001047 bool VisitUnaryImag(const UnaryOperator *E);
1048
Sebastian Redl295995c2010-09-10 20:55:47 +00001049 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001050 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1051
Chris Lattnerfcee0012008-07-11 21:24:13 +00001052private:
Ken Dyck8b752f12010-01-27 17:10:57 +00001053 CharUnits GetAlignOfExpr(const Expr *E);
1054 CharUnits GetAlignOfType(QualType T);
John McCall42c8f872010-05-10 23:27:23 +00001055 static QualType GetObjectType(const Expr *E);
1056 bool TryEvaluateBuiltinObjectSize(CallExpr *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001057 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001058};
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001059} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +00001060
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001061static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001062 assert(E->getType()->isIntegralOrEnumerationType());
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001063 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1064}
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001065
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001066static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001067 assert(E->getType()->isIntegralOrEnumerationType());
John McCall7db7acb2010-05-07 05:46:35 +00001068
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001069 APValue Val;
1070 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
1071 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001072 Result = Val.getInt();
1073 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +00001074}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001075
Eli Friedman04309752009-11-24 05:28:59 +00001076bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001077 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +00001078 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
1079 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001080
1081 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +00001082 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001083 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
1084 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +00001085
1086 if (isa<ParmVarDecl>(D))
1087 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1088
Eli Friedman04309752009-11-24 05:28:59 +00001089 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001090 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001091 if (APValue *V = VD->getEvaluatedValue()) {
1092 if (V->isInt())
1093 return Success(V->getInt(), E);
1094 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1095 }
1096
1097 if (VD->isEvaluatingValue())
1098 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1099
1100 VD->setEvaluatingValue();
1101
Eli Friedmana7dedf72010-09-06 00:10:32 +00001102 Expr::EvalResult EResult;
1103 if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
1104 EResult.Val.isInt()) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001105 // Cache the evaluated value in the variable declaration.
Eli Friedmana7dedf72010-09-06 00:10:32 +00001106 Result = EResult.Val;
Eli Friedmanc0131182009-12-03 20:31:57 +00001107 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +00001108 return true;
1109 }
1110
Eli Friedmanc0131182009-12-03 20:31:57 +00001111 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +00001112 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001113 }
1114 }
1115
Chris Lattner4c4867e2008-07-12 00:38:25 +00001116 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001117 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001118}
1119
Chris Lattnera4d55d82008-10-06 06:40:35 +00001120/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1121/// as GCC.
1122static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1123 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001124 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +00001125 enum gcc_type_class {
1126 no_type_class = -1,
1127 void_type_class, integer_type_class, char_type_class,
1128 enumeral_type_class, boolean_type_class,
1129 pointer_type_class, reference_type_class, offset_type_class,
1130 real_type_class, complex_type_class,
1131 function_type_class, method_type_class,
1132 record_type_class, union_type_class,
1133 array_type_class, string_type_class,
1134 lang_type_class
1135 };
Mike Stump1eb44332009-09-09 15:08:12 +00001136
1137 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001138 // ideal, however it is what gcc does.
1139 if (E->getNumArgs() == 0)
1140 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Chris Lattnera4d55d82008-10-06 06:40:35 +00001142 QualType ArgTy = E->getArg(0)->getType();
1143 if (ArgTy->isVoidType())
1144 return void_type_class;
1145 else if (ArgTy->isEnumeralType())
1146 return enumeral_type_class;
1147 else if (ArgTy->isBooleanType())
1148 return boolean_type_class;
1149 else if (ArgTy->isCharType())
1150 return string_type_class; // gcc doesn't appear to use char_type_class
1151 else if (ArgTy->isIntegerType())
1152 return integer_type_class;
1153 else if (ArgTy->isPointerType())
1154 return pointer_type_class;
1155 else if (ArgTy->isReferenceType())
1156 return reference_type_class;
1157 else if (ArgTy->isRealType())
1158 return real_type_class;
1159 else if (ArgTy->isComplexType())
1160 return complex_type_class;
1161 else if (ArgTy->isFunctionType())
1162 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001163 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001164 return record_type_class;
1165 else if (ArgTy->isUnionType())
1166 return union_type_class;
1167 else if (ArgTy->isArrayType())
1168 return array_type_class;
1169 else if (ArgTy->isUnionType())
1170 return union_type_class;
1171 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1172 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1173 return -1;
1174}
1175
John McCall42c8f872010-05-10 23:27:23 +00001176/// Retrieves the "underlying object type" of the given expression,
1177/// as used by __builtin_object_size.
1178QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1179 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1180 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1181 return VD->getType();
1182 } else if (isa<CompoundLiteralExpr>(E)) {
1183 return E->getType();
1184 }
1185
1186 return QualType();
1187}
1188
1189bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
1190 // TODO: Perhaps we should let LLVM lower this?
1191 LValue Base;
1192 if (!EvaluatePointer(E->getArg(0), Base, Info))
1193 return false;
1194
1195 // If we can prove the base is null, lower to zero now.
1196 const Expr *LVBase = Base.getLValueBase();
1197 if (!LVBase) return Success(0, E);
1198
1199 QualType T = GetObjectType(LVBase);
1200 if (T.isNull() ||
1201 T->isIncompleteType() ||
Eli Friedman13578692010-08-05 02:49:48 +00001202 T->isFunctionType() ||
John McCall42c8f872010-05-10 23:27:23 +00001203 T->isVariablyModifiedType() ||
1204 T->isDependentType())
1205 return false;
1206
1207 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1208 CharUnits Offset = Base.getLValueOffset();
1209
1210 if (!Offset.isNegative() && Offset <= Size)
1211 Size -= Offset;
1212 else
1213 Size = CharUnits::Zero();
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001214 return Success(Size, E);
John McCall42c8f872010-05-10 23:27:23 +00001215}
1216
Eli Friedmanc4a26382010-02-13 00:10:10 +00001217bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001218 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001219 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001220 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001221
1222 case Builtin::BI__builtin_object_size: {
John McCall42c8f872010-05-10 23:27:23 +00001223 if (TryEvaluateBuiltinObjectSize(E))
1224 return true;
Mike Stump64eda9e2009-10-26 18:35:08 +00001225
Eric Christopherb2aaf512010-01-19 22:58:35 +00001226 // If evaluating the argument has side-effects we can't determine
1227 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001228 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001229 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001230 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001231 return Success(0, E);
1232 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001233
Mike Stump64eda9e2009-10-26 18:35:08 +00001234 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1235 }
1236
Chris Lattner019f4e82008-10-06 05:28:25 +00001237 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001238 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001240 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001241 // __builtin_constant_p always has one operand: it returns true if that
1242 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001243 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001244
1245 case Builtin::BI__builtin_eh_return_data_regno: {
1246 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1247 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1248 return Success(Operand, E);
1249 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001250
1251 case Builtin::BI__builtin_expect:
1252 return Visit(E->getArg(0));
Douglas Gregor5726d402010-09-10 06:27:15 +00001253
1254 case Builtin::BIstrlen:
1255 case Builtin::BI__builtin_strlen:
1256 // As an extension, we support strlen() and __builtin_strlen() as constant
1257 // expressions when the argument is a string literal.
1258 if (StringLiteral *S
1259 = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
1260 // The string literal may have embedded null characters. Find the first
1261 // one and truncate there.
1262 llvm::StringRef Str = S->getString();
1263 llvm::StringRef::size_type Pos = Str.find(0);
1264 if (Pos != llvm::StringRef::npos)
1265 Str = Str.substr(0, Pos);
1266
1267 return Success(Str.size(), E);
1268 }
1269
1270 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +00001271 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001272}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001273
Chris Lattnerb542afe2008-07-11 19:10:17 +00001274bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001275 if (E->getOpcode() == BO_Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001276 if (!Visit(E->getRHS()))
1277 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001278
Eli Friedman33ef1452009-02-26 10:19:36 +00001279 // If we can't evaluate the LHS, it might have side effects;
1280 // conservatively mark it.
1281 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1282 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001283
Anders Carlsson027f62e2008-12-01 02:07:06 +00001284 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001285 }
1286
1287 if (E->isLogicalOp()) {
1288 // These need to be handled specially because the operands aren't
1289 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001290 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001292 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001293 // We were able to evaluate the LHS, see if we can get away with not
1294 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
John McCall2de56d12010-08-25 11:45:40 +00001295 if (lhsResult == (E->getOpcode() == BO_LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001296 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001297
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001298 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
John McCall2de56d12010-08-25 11:45:40 +00001299 if (E->getOpcode() == BO_LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001300 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001301 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001302 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001303 }
1304 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001305 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001306 // We can't evaluate the LHS; however, sometimes the result
1307 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
John McCall2de56d12010-08-25 11:45:40 +00001308 if (rhsResult == (E->getOpcode() == BO_LOr) ||
1309 !rhsResult == (E->getOpcode() == BO_LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001310 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001311 // must have had side effects.
1312 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001313
1314 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001315 }
1316 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001317 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001318
Eli Friedmana6afa762008-11-13 06:09:17 +00001319 return false;
1320 }
1321
Anders Carlsson286f85e2008-11-16 07:17:21 +00001322 QualType LHSTy = E->getLHS()->getType();
1323 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001324
1325 if (LHSTy->isAnyComplexType()) {
1326 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001327 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001328
1329 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1330 return false;
1331
1332 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1333 return false;
1334
1335 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001336 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001337 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001338 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001339 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1340
John McCall2de56d12010-08-25 11:45:40 +00001341 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001342 return Success((CR_r == APFloat::cmpEqual &&
1343 CR_i == APFloat::cmpEqual), E);
1344 else {
John McCall2de56d12010-08-25 11:45:40 +00001345 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001346 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001347 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001348 CR_r == APFloat::cmpLessThan ||
1349 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001350 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001351 CR_i == APFloat::cmpLessThan ||
1352 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001353 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001354 } else {
John McCall2de56d12010-08-25 11:45:40 +00001355 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001356 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1357 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1358 else {
John McCall2de56d12010-08-25 11:45:40 +00001359 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001360 "Invalid compex comparison.");
1361 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1362 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1363 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001364 }
1365 }
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Anders Carlsson286f85e2008-11-16 07:17:21 +00001367 if (LHSTy->isRealFloatingType() &&
1368 RHSTy->isRealFloatingType()) {
1369 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Anders Carlsson286f85e2008-11-16 07:17:21 +00001371 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1372 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Anders Carlsson286f85e2008-11-16 07:17:21 +00001374 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1375 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Anders Carlsson286f85e2008-11-16 07:17:21 +00001377 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001378
Anders Carlsson286f85e2008-11-16 07:17:21 +00001379 switch (E->getOpcode()) {
1380 default:
1381 assert(0 && "Invalid binary operator!");
John McCall2de56d12010-08-25 11:45:40 +00001382 case BO_LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001383 return Success(CR == APFloat::cmpLessThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001384 case BO_GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001385 return Success(CR == APFloat::cmpGreaterThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001386 case BO_LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001387 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001388 case BO_GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001389 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001390 E);
John McCall2de56d12010-08-25 11:45:40 +00001391 case BO_EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001392 return Success(CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001393 case BO_NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001394 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001395 || CR == APFloat::cmpLessThan
1396 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001397 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001400 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001401 if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001402 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001403 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1404 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001405
John McCallefdb83e2010-05-07 21:00:08 +00001406 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001407 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1408 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001409
Eli Friedman5bc86102009-06-14 02:17:33 +00001410 // Reject any bases from the normal codepath; we special-case comparisons
1411 // to null.
1412 if (LHSValue.getLValueBase()) {
1413 if (!E->isEqualityOp())
1414 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001415 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001416 return false;
1417 bool bres;
1418 if (!EvalPointerValueAsBool(LHSValue, bres))
1419 return false;
John McCall2de56d12010-08-25 11:45:40 +00001420 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001421 } else if (RHSValue.getLValueBase()) {
1422 if (!E->isEqualityOp())
1423 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001424 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001425 return false;
1426 bool bres;
1427 if (!EvalPointerValueAsBool(RHSValue, bres))
1428 return false;
John McCall2de56d12010-08-25 11:45:40 +00001429 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001430 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001431
John McCall2de56d12010-08-25 11:45:40 +00001432 if (E->getOpcode() == BO_Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001433 QualType Type = E->getLHS()->getType();
1434 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001435
Ken Dycka7305832010-01-15 12:37:54 +00001436 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001437 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001438 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001439
Ken Dycka7305832010-01-15 12:37:54 +00001440 CharUnits Diff = LHSValue.getLValueOffset() -
1441 RHSValue.getLValueOffset();
1442 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001443 }
1444 bool Result;
John McCall2de56d12010-08-25 11:45:40 +00001445 if (E->getOpcode() == BO_EQ) {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001446 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001447 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001448 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1449 }
1450 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001451 }
1452 }
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001453 if (!LHSTy->isIntegralOrEnumerationType() ||
1454 !RHSTy->isIntegralOrEnumerationType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001455 // We can't continue from here for non-integral types, and they
1456 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001457 return false;
1458 }
1459
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001460 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001461 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001462 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001463
Eli Friedman42edd0d2009-03-24 01:14:50 +00001464 APValue RHSVal;
1465 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001466 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001467
1468 // Handle cases like (unsigned long)&a + 4.
1469 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001470 CharUnits Offset = Result.getLValueOffset();
1471 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1472 RHSVal.getInt().getZExtValue());
John McCall2de56d12010-08-25 11:45:40 +00001473 if (E->getOpcode() == BO_Add)
Ken Dycka7305832010-01-15 12:37:54 +00001474 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001475 else
Ken Dycka7305832010-01-15 12:37:54 +00001476 Offset -= AdditionalOffset;
1477 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001478 return true;
1479 }
1480
1481 // Handle cases like 4 + (unsigned long)&a
John McCall2de56d12010-08-25 11:45:40 +00001482 if (E->getOpcode() == BO_Add &&
Eli Friedman42edd0d2009-03-24 01:14:50 +00001483 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001484 CharUnits Offset = RHSVal.getLValueOffset();
1485 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1486 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001487 return true;
1488 }
1489
1490 // All the following cases expect both operands to be an integer
1491 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001492 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001493
Eli Friedman42edd0d2009-03-24 01:14:50 +00001494 APSInt& RHS = RHSVal.getInt();
1495
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001496 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001497 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001498 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001499 case BO_Mul: return Success(Result.getInt() * RHS, E);
1500 case BO_Add: return Success(Result.getInt() + RHS, E);
1501 case BO_Sub: return Success(Result.getInt() - RHS, E);
1502 case BO_And: return Success(Result.getInt() & RHS, E);
1503 case BO_Xor: return Success(Result.getInt() ^ RHS, E);
1504 case BO_Or: return Success(Result.getInt() | RHS, E);
1505 case BO_Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001506 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001507 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001508 return Success(Result.getInt() / RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001509 case BO_Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001510 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001511 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001512 return Success(Result.getInt() % RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001513 case BO_Shl: {
John McCall091f23f2010-11-09 22:22:12 +00001514 // During constant-folding, a negative shift is an opposite shift.
1515 if (RHS.isSigned() && RHS.isNegative()) {
1516 RHS = -RHS;
1517 goto shift_right;
1518 }
1519
1520 shift_left:
1521 unsigned SA
1522 = (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001523 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001524 }
John McCall2de56d12010-08-25 11:45:40 +00001525 case BO_Shr: {
John McCall091f23f2010-11-09 22:22:12 +00001526 // During constant-folding, a negative shift is an opposite shift.
1527 if (RHS.isSigned() && RHS.isNegative()) {
1528 RHS = -RHS;
1529 goto shift_left;
1530 }
1531
1532 shift_right:
Mike Stump1eb44332009-09-09 15:08:12 +00001533 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001534 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1535 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
John McCall2de56d12010-08-25 11:45:40 +00001538 case BO_LT: return Success(Result.getInt() < RHS, E);
1539 case BO_GT: return Success(Result.getInt() > RHS, E);
1540 case BO_LE: return Success(Result.getInt() <= RHS, E);
1541 case BO_GE: return Success(Result.getInt() >= RHS, E);
1542 case BO_EQ: return Success(Result.getInt() == RHS, E);
1543 case BO_NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001544 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001545}
1546
John McCall56ca35d2011-02-17 10:25:35 +00001547bool IntExprEvaluator::
1548VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) {
1549 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
1550 if (opaque.hasError()) return false;
1551
1552 bool cond;
1553 if (!HandleConversionToBool(e->getCond(), cond, Info))
1554 return false;
1555
1556 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
1557}
1558
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001559bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001560 bool Cond;
1561 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001562 return false;
1563
Nuno Lopesa25bd552008-11-16 22:06:39 +00001564 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001565}
1566
Ken Dyck8b752f12010-01-27 17:10:57 +00001567CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001568 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1569 // the result is the size of the referenced type."
1570 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1571 // result shall be the alignment of the referenced type."
1572 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1573 T = Ref->getPointeeType();
1574
Eli Friedman2be58612009-05-30 21:09:44 +00001575 // __alignof is defined to return the preferred alignment.
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001576 return Info.Ctx.toCharUnitsFromBits(
1577 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
Chris Lattnere9feb472009-01-24 21:09:06 +00001578}
1579
Ken Dyck8b752f12010-01-27 17:10:57 +00001580CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001581 E = E->IgnoreParens();
1582
1583 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001584 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001585 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001586 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1587 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001588
Chris Lattneraf707ab2009-01-24 21:53:27 +00001589 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001590 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1591 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001592
Chris Lattnere9feb472009-01-24 21:09:06 +00001593 return GetAlignOfType(E->getType());
1594}
1595
1596
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001597/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
1598/// a result as the expression's type.
1599bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
1600 const UnaryExprOrTypeTraitExpr *E) {
1601 switch(E->getKind()) {
1602 case UETT_AlignOf: {
Chris Lattnere9feb472009-01-24 21:09:06 +00001603 if (E->isArgumentType())
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001604 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001605 else
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001606 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001607 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001608
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001609 case UETT_VecStep: {
1610 QualType Ty = E->getTypeOfArgument();
Sebastian Redl05189992008-11-11 17:56:53 +00001611
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001612 if (Ty->isVectorType()) {
1613 unsigned n = Ty->getAs<VectorType>()->getNumElements();
Eli Friedmana1f47c42009-03-23 04:38:34 +00001614
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001615 // The vec_step built-in functions that take a 3-component
1616 // vector return 4. (OpenCL 1.1 spec 6.11.12)
1617 if (n == 3)
1618 n = 4;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001619
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001620 return Success(n, E);
1621 } else
1622 return Success(1, E);
1623 }
1624
1625 case UETT_SizeOf: {
1626 QualType SrcTy = E->getTypeOfArgument();
1627 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1628 // the result is the size of the referenced type."
1629 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1630 // result shall be the alignment of the referenced type."
1631 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1632 SrcTy = Ref->getPointeeType();
1633
1634 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1635 // extension.
1636 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1637 return Success(1, E);
1638
1639 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
1640 if (!SrcTy->isConstantSizeType())
1641 return false;
1642
1643 // Get information about the size.
1644 return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E);
1645 }
1646 }
1647
1648 llvm_unreachable("unknown expr/type trait");
1649 return false;
Chris Lattnerfcee0012008-07-11 21:24:13 +00001650}
1651
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001652bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1653 CharUnits Result;
1654 unsigned n = E->getNumComponents();
1655 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1656 if (n == 0)
1657 return false;
1658 QualType CurrentType = E->getTypeSourceInfo()->getType();
1659 for (unsigned i = 0; i != n; ++i) {
1660 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1661 switch (ON.getKind()) {
1662 case OffsetOfExpr::OffsetOfNode::Array: {
1663 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1664 APSInt IdxResult;
1665 if (!EvaluateInteger(Idx, IdxResult, Info))
1666 return false;
1667 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1668 if (!AT)
1669 return false;
1670 CurrentType = AT->getElementType();
1671 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1672 Result += IdxResult.getSExtValue() * ElementSize;
1673 break;
1674 }
1675
1676 case OffsetOfExpr::OffsetOfNode::Field: {
1677 FieldDecl *MemberDecl = ON.getField();
1678 const RecordType *RT = CurrentType->getAs<RecordType>();
1679 if (!RT)
1680 return false;
1681 RecordDecl *RD = RT->getDecl();
1682 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
John McCallba4f5d52011-01-20 07:57:12 +00001683 unsigned i = MemberDecl->getFieldIndex();
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001684 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001685 Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001686 CurrentType = MemberDecl->getType().getNonReferenceType();
1687 break;
1688 }
1689
1690 case OffsetOfExpr::OffsetOfNode::Identifier:
1691 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001692 return false;
1693
1694 case OffsetOfExpr::OffsetOfNode::Base: {
1695 CXXBaseSpecifier *BaseSpec = ON.getBase();
1696 if (BaseSpec->isVirtual())
1697 return false;
1698
1699 // Find the layout of the class whose base we are looking into.
1700 const RecordType *RT = CurrentType->getAs<RecordType>();
1701 if (!RT)
1702 return false;
1703 RecordDecl *RD = RT->getDecl();
1704 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1705
1706 // Find the base class itself.
1707 CurrentType = BaseSpec->getType();
1708 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1709 if (!BaseRT)
1710 return false;
1711
1712 // Add the offset to the base.
Ken Dyck7c7f8202011-01-26 02:17:08 +00001713 Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001714 break;
1715 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001716 }
1717 }
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001718 return Success(Result, E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001719}
1720
Chris Lattnerb542afe2008-07-11 19:10:17 +00001721bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001722 if (E->getOpcode() == UO_LNot) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001723 // LNot's operand isn't necessarily an integer, so we handle it specially.
1724 bool bres;
1725 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1726 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001727 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001728 }
1729
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001730 // Only handle integral operations...
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001731 if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001732 return false;
1733
Chris Lattner87eae5e2008-07-11 22:52:41 +00001734 // Get the operand value into 'Result'.
1735 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001736 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001737
Chris Lattner75a48812008-07-11 22:15:16 +00001738 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001739 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001740 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1741 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001742 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001743 case UO_Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001744 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1745 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001746 return true;
John McCall2de56d12010-08-25 11:45:40 +00001747 case UO_Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001748 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001749 return true;
John McCall2de56d12010-08-25 11:45:40 +00001750 case UO_Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001751 if (!Result.isInt()) return false;
1752 return Success(-Result.getInt(), E);
John McCall2de56d12010-08-25 11:45:40 +00001753 case UO_Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001754 if (!Result.isInt()) return false;
1755 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001756 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001757}
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Chris Lattner732b2232008-07-12 01:15:53 +00001759/// HandleCast - This is used to evaluate implicit or explicit casts where the
1760/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001761bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001762 Expr *SubExpr = E->getSubExpr();
1763 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001764 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001765
Eli Friedman46a52322011-03-25 00:43:55 +00001766 switch (E->getCastKind()) {
Eli Friedman46a52322011-03-25 00:43:55 +00001767 case CK_BaseToDerived:
1768 case CK_DerivedToBase:
1769 case CK_UncheckedDerivedToBase:
1770 case CK_Dynamic:
1771 case CK_ToUnion:
1772 case CK_ArrayToPointerDecay:
1773 case CK_FunctionToPointerDecay:
1774 case CK_NullToPointer:
1775 case CK_NullToMemberPointer:
1776 case CK_BaseToDerivedMemberPointer:
1777 case CK_DerivedToBaseMemberPointer:
1778 case CK_ConstructorConversion:
1779 case CK_IntegralToPointer:
1780 case CK_ToVoid:
1781 case CK_VectorSplat:
1782 case CK_IntegralToFloating:
1783 case CK_FloatingCast:
1784 case CK_AnyPointerToObjCPointerCast:
1785 case CK_AnyPointerToBlockPointerCast:
1786 case CK_ObjCObjectLValueCast:
1787 case CK_FloatingRealToComplex:
1788 case CK_FloatingComplexToReal:
1789 case CK_FloatingComplexCast:
1790 case CK_FloatingComplexToIntegralComplex:
1791 case CK_IntegralRealToComplex:
1792 case CK_IntegralComplexCast:
1793 case CK_IntegralComplexToFloatingComplex:
1794 llvm_unreachable("invalid cast kind for integral value");
1795
Eli Friedmane50c2972011-03-25 19:07:11 +00001796 case CK_BitCast:
Eli Friedman46a52322011-03-25 00:43:55 +00001797 case CK_Dependent:
1798 case CK_GetObjCProperty:
1799 case CK_LValueBitCast:
1800 case CK_UserDefinedConversion:
1801 return false;
1802
1803 case CK_LValueToRValue:
1804 case CK_NoOp:
1805 return Visit(E->getSubExpr());
1806
1807 case CK_MemberPointerToBoolean:
1808 case CK_PointerToBoolean:
1809 case CK_IntegralToBoolean:
1810 case CK_FloatingToBoolean:
1811 case CK_FloatingComplexToBoolean:
1812 case CK_IntegralComplexToBoolean: {
Eli Friedman4efaa272008-11-12 09:44:48 +00001813 bool BoolResult;
1814 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1815 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001816 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001817 }
1818
Eli Friedman46a52322011-03-25 00:43:55 +00001819 case CK_IntegralCast: {
Chris Lattner732b2232008-07-12 01:15:53 +00001820 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001821 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001822
Eli Friedmanbe265702009-02-20 01:15:07 +00001823 if (!Result.isInt()) {
1824 // Only allow casts of lvalues if they are lossless.
1825 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1826 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001827
Daniel Dunbardd211642009-02-19 22:24:01 +00001828 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001829 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001830 }
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Eli Friedman46a52322011-03-25 00:43:55 +00001832 case CK_PointerToIntegral: {
John McCallefdb83e2010-05-07 21:00:08 +00001833 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001834 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001835 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001836
Daniel Dunbardd211642009-02-19 22:24:01 +00001837 if (LV.getLValueBase()) {
1838 // Only allow based lvalue casts if they are lossless.
1839 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1840 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001841
John McCallefdb83e2010-05-07 21:00:08 +00001842 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001843 return true;
1844 }
1845
Ken Dycka7305832010-01-15 12:37:54 +00001846 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1847 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001848 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001849 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001850
Eli Friedman46a52322011-03-25 00:43:55 +00001851 case CK_IntegralComplexToReal: {
John McCallf4cf1a12010-05-07 17:22:02 +00001852 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001853 if (!EvaluateComplex(SubExpr, C, Info))
1854 return false;
Eli Friedman46a52322011-03-25 00:43:55 +00001855 return Success(C.getComplexIntReal(), E);
Eli Friedman1725f682009-04-22 19:23:09 +00001856 }
Eli Friedman2217c872009-02-22 11:46:18 +00001857
Eli Friedman46a52322011-03-25 00:43:55 +00001858 case CK_FloatingToIntegral: {
1859 APFloat F(0.0);
1860 if (!EvaluateFloat(SubExpr, F, Info))
1861 return false;
Chris Lattner732b2232008-07-12 01:15:53 +00001862
Eli Friedman46a52322011-03-25 00:43:55 +00001863 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
1864 }
1865 }
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Eli Friedman46a52322011-03-25 00:43:55 +00001867 llvm_unreachable("unknown cast resulting in integral value");
1868 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001869}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001870
Eli Friedman722c7172009-02-28 03:59:05 +00001871bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1872 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001873 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001874 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1875 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1876 return Success(LV.getComplexIntReal(), E);
1877 }
1878
1879 return Visit(E->getSubExpr());
1880}
1881
Eli Friedman664a1042009-02-27 04:45:43 +00001882bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001883 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001884 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001885 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1886 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1887 return Success(LV.getComplexIntImag(), E);
1888 }
1889
Eli Friedman664a1042009-02-27 04:45:43 +00001890 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1891 Info.EvalResult.HasSideEffects = true;
1892 return Success(0, E);
1893}
1894
Douglas Gregoree8aff02011-01-04 17:33:58 +00001895bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1896 return Success(E->getPackLength(), E);
1897}
1898
Sebastian Redl295995c2010-09-10 20:55:47 +00001899bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1900 return Success(E->getValue(), E);
1901}
1902
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001903//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001904// Float Evaluation
1905//===----------------------------------------------------------------------===//
1906
1907namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001908class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001909 : public StmtVisitor<FloatExprEvaluator, bool> {
1910 EvalInfo &Info;
1911 APFloat &Result;
1912public:
1913 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1914 : Info(info), Result(result) {}
1915
1916 bool VisitStmt(Stmt *S) {
1917 return false;
1918 }
1919
1920 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
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);
Eli Friedman4efaa272008-11-12 09:44:48 +00001926 bool VisitCastExpr(CastExpr *E);
Douglas Gregored8abf12010-07-08 06:14:04 +00001927 bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001928 bool VisitConditionalOperator(ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00001929 bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001930
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001931 bool VisitChooseExpr(const ChooseExpr *E)
1932 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1933 bool VisitUnaryExtension(const UnaryOperator *E)
1934 { return Visit(E->getSubExpr()); }
John McCallabd3a852010-05-07 22:08:54 +00001935 bool VisitUnaryReal(const UnaryOperator *E);
1936 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001937
John McCall189d6ef2010-10-09 01:34:31 +00001938 bool VisitDeclRefExpr(const DeclRefExpr *E);
1939
John McCall56ca35d2011-02-17 10:25:35 +00001940 bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) {
1941 const APValue *value = Info.getOpaqueValue(e);
1942 if (!value)
1943 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
1944 Result = value->getFloat();
1945 return true;
1946 }
1947
John McCallabd3a852010-05-07 22:08:54 +00001948 // FIXME: Missing: array subscript of vector, member of vector,
1949 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001950};
1951} // end anonymous namespace
1952
1953static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001954 assert(E->getType()->isRealFloatingType());
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001955 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1956}
1957
Jay Foad4ba2a172011-01-12 09:06:06 +00001958static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
John McCalldb7b72a2010-02-28 13:00:19 +00001959 QualType ResultTy,
1960 const Expr *Arg,
1961 bool SNaN,
1962 llvm::APFloat &Result) {
1963 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1964 if (!S) return false;
1965
1966 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1967
1968 llvm::APInt fill;
1969
1970 // Treat empty strings as if they were zero.
1971 if (S->getString().empty())
1972 fill = llvm::APInt(32, 0);
1973 else if (S->getString().getAsInteger(0, fill))
1974 return false;
1975
1976 if (SNaN)
1977 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1978 else
1979 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1980 return true;
1981}
1982
Chris Lattner019f4e82008-10-06 05:28:25 +00001983bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001984 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001985 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001986 case Builtin::BI__builtin_huge_val:
1987 case Builtin::BI__builtin_huge_valf:
1988 case Builtin::BI__builtin_huge_vall:
1989 case Builtin::BI__builtin_inf:
1990 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001991 case Builtin::BI__builtin_infl: {
1992 const llvm::fltSemantics &Sem =
1993 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001994 Result = llvm::APFloat::getInf(Sem);
1995 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001996 }
Mike Stump1eb44332009-09-09 15:08:12 +00001997
John McCalldb7b72a2010-02-28 13:00:19 +00001998 case Builtin::BI__builtin_nans:
1999 case Builtin::BI__builtin_nansf:
2000 case Builtin::BI__builtin_nansl:
2001 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
2002 true, Result);
2003
Chris Lattner9e621712008-10-06 06:31:58 +00002004 case Builtin::BI__builtin_nan:
2005 case Builtin::BI__builtin_nanf:
2006 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00002007 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00002008 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00002009 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
2010 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002011
2012 case Builtin::BI__builtin_fabs:
2013 case Builtin::BI__builtin_fabsf:
2014 case Builtin::BI__builtin_fabsl:
2015 if (!EvaluateFloat(E->getArg(0), Result, Info))
2016 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002018 if (Result.isNegative())
2019 Result.changeSign();
2020 return true;
2021
Mike Stump1eb44332009-09-09 15:08:12 +00002022 case Builtin::BI__builtin_copysign:
2023 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002024 case Builtin::BI__builtin_copysignl: {
2025 APFloat RHS(0.);
2026 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
2027 !EvaluateFloat(E->getArg(1), RHS, Info))
2028 return false;
2029 Result.copySign(RHS);
2030 return true;
2031 }
Chris Lattner019f4e82008-10-06 05:28:25 +00002032 }
2033}
2034
John McCall189d6ef2010-10-09 01:34:31 +00002035bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
2036 const Decl *D = E->getDecl();
2037 if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false;
2038 const VarDecl *VD = cast<VarDecl>(D);
2039
2040 // Require the qualifiers to be const and not volatile.
2041 CanQualType T = Info.Ctx.getCanonicalType(E->getType());
2042 if (!T.isConstQualified() || T.isVolatileQualified())
2043 return false;
2044
2045 const Expr *Init = VD->getAnyInitializer();
2046 if (!Init) return false;
2047
2048 if (APValue *V = VD->getEvaluatedValue()) {
2049 if (V->isFloat()) {
2050 Result = V->getFloat();
2051 return true;
2052 }
2053 return false;
2054 }
2055
2056 if (VD->isEvaluatingValue())
2057 return false;
2058
2059 VD->setEvaluatingValue();
2060
2061 Expr::EvalResult InitResult;
2062 if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects &&
2063 InitResult.Val.isFloat()) {
2064 // Cache the evaluated value in the variable declaration.
2065 Result = InitResult.Val.getFloat();
2066 VD->setEvaluatedValue(InitResult.Val);
2067 return true;
2068 }
2069
2070 VD->setEvaluatedValue(APValue());
2071 return false;
2072}
2073
John McCallabd3a852010-05-07 22:08:54 +00002074bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002075 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2076 ComplexValue CV;
2077 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2078 return false;
2079 Result = CV.FloatReal;
2080 return true;
2081 }
2082
2083 return Visit(E->getSubExpr());
John McCallabd3a852010-05-07 22:08:54 +00002084}
2085
2086bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002087 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2088 ComplexValue CV;
2089 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2090 return false;
2091 Result = CV.FloatImag;
2092 return true;
2093 }
2094
2095 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
2096 Info.EvalResult.HasSideEffects = true;
2097 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
2098 Result = llvm::APFloat::getZero(Sem);
John McCallabd3a852010-05-07 22:08:54 +00002099 return true;
2100}
2101
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002102bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002103 if (E->getOpcode() == UO_Deref)
Nuno Lopesa468d342008-11-19 17:44:31 +00002104 return false;
2105
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002106 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
2107 return false;
2108
2109 switch (E->getOpcode()) {
2110 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002111 case UO_Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002112 return true;
John McCall2de56d12010-08-25 11:45:40 +00002113 case UO_Minus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002114 Result.changeSign();
2115 return true;
2116 }
2117}
Chris Lattner019f4e82008-10-06 05:28:25 +00002118
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002119bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002120 if (E->getOpcode() == BO_Comma) {
Eli Friedman7f92f032009-11-16 04:25:37 +00002121 if (!EvaluateFloat(E->getRHS(), Result, Info))
2122 return false;
2123
2124 // If we can't evaluate the LHS, it might have side effects;
2125 // conservatively mark it.
2126 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2127 Info.EvalResult.HasSideEffects = true;
2128
2129 return true;
2130 }
2131
Anders Carlsson96e93662010-10-31 01:21:47 +00002132 // We can't evaluate pointer-to-member operations.
2133 if (E->isPtrMemOp())
2134 return false;
2135
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002136 // FIXME: Diagnostics? I really don't understand how the warnings
2137 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002138 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002139 if (!EvaluateFloat(E->getLHS(), Result, Info))
2140 return false;
2141 if (!EvaluateFloat(E->getRHS(), RHS, Info))
2142 return false;
2143
2144 switch (E->getOpcode()) {
2145 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002146 case BO_Mul:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002147 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
2148 return true;
John McCall2de56d12010-08-25 11:45:40 +00002149 case BO_Add:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002150 Result.add(RHS, APFloat::rmNearestTiesToEven);
2151 return true;
John McCall2de56d12010-08-25 11:45:40 +00002152 case BO_Sub:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002153 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
2154 return true;
John McCall2de56d12010-08-25 11:45:40 +00002155 case BO_Div:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002156 Result.divide(RHS, APFloat::rmNearestTiesToEven);
2157 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002158 }
2159}
2160
2161bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
2162 Result = E->getValue();
2163 return true;
2164}
2165
Eli Friedman4efaa272008-11-12 09:44:48 +00002166bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
2167 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Eli Friedman2a523ee2011-03-25 00:54:52 +00002169 switch (E->getCastKind()) {
2170 default:
2171 return false;
2172
2173 case CK_LValueToRValue:
2174 case CK_NoOp:
2175 return Visit(SubExpr);
2176
2177 case CK_IntegralToFloating: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002178 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00002179 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00002180 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002181 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002182 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002183 return true;
2184 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002185
2186 case CK_FloatingCast: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002187 if (!Visit(SubExpr))
2188 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002189 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
2190 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002191 return true;
2192 }
John McCallf3ea8cf2010-11-14 08:17:51 +00002193
Eli Friedman2a523ee2011-03-25 00:54:52 +00002194 case CK_FloatingComplexToReal: {
John McCallf3ea8cf2010-11-14 08:17:51 +00002195 ComplexValue V;
2196 if (!EvaluateComplex(SubExpr, V, Info))
2197 return false;
2198 Result = V.getComplexFloatReal();
2199 return true;
2200 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002201 }
Eli Friedman4efaa272008-11-12 09:44:48 +00002202
2203 return false;
2204}
2205
Douglas Gregored8abf12010-07-08 06:14:04 +00002206bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +00002207 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
2208 return true;
2209}
2210
John McCall56ca35d2011-02-17 10:25:35 +00002211bool FloatExprEvaluator::
2212VisitBinaryConditionalOperator(BinaryConditionalOperator *e) {
2213 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
2214 if (opaque.hasError()) return false;
2215
2216 bool cond;
2217 if (!HandleConversionToBool(e->getCond(), cond, Info))
2218 return false;
2219
2220 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
2221}
2222
Eli Friedman67f85fc2009-12-04 02:12:53 +00002223bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
2224 bool Cond;
2225 if (!HandleConversionToBool(E->getCond(), Cond, Info))
2226 return false;
2227
2228 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
2229}
2230
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002231//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002232// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002233//===----------------------------------------------------------------------===//
2234
2235namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00002236class ComplexExprEvaluator
John McCallf4cf1a12010-05-07 17:22:02 +00002237 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002238 EvalInfo &Info;
John McCallf4cf1a12010-05-07 17:22:02 +00002239 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002241public:
John McCallf4cf1a12010-05-07 17:22:02 +00002242 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
2243 : Info(info), Result(Result) {}
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002245 //===--------------------------------------------------------------------===//
2246 // Visitor Methods
2247 //===--------------------------------------------------------------------===//
2248
John McCallf4cf1a12010-05-07 17:22:02 +00002249 bool VisitStmt(Stmt *S) {
2250 return false;
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002251 }
Mike Stump1eb44332009-09-09 15:08:12 +00002252
John McCallf4cf1a12010-05-07 17:22:02 +00002253 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002254
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002255 bool VisitImaginaryLiteral(ImaginaryLiteral *E);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002256
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002257 bool VisitCastExpr(CastExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002258
John McCallf4cf1a12010-05-07 17:22:02 +00002259 bool VisitBinaryOperator(const BinaryOperator *E);
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002260 bool VisitUnaryOperator(const UnaryOperator *E);
2261 bool VisitConditionalOperator(const ConditionalOperator *E);
John McCall56ca35d2011-02-17 10:25:35 +00002262 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E);
John McCallf4cf1a12010-05-07 17:22:02 +00002263 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002264 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallf4cf1a12010-05-07 17:22:02 +00002265 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002266 { return Visit(E->getSubExpr()); }
John McCall56ca35d2011-02-17 10:25:35 +00002267 bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) {
2268 const APValue *value = Info.getOpaqueValue(e);
2269 if (!value)
2270 return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false);
2271 Result.setFrom(*value);
2272 return true;
2273 }
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002274 // FIXME Missing: ImplicitValueInitExpr
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002275};
2276} // end anonymous namespace
2277
John McCallf4cf1a12010-05-07 17:22:02 +00002278static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2279 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002280 assert(E->getType()->isAnyComplexType());
John McCallf4cf1a12010-05-07 17:22:02 +00002281 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002282}
2283
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002284bool ComplexExprEvaluator::VisitImaginaryLiteral(ImaginaryLiteral *E) {
2285 Expr* SubExpr = E->getSubExpr();
2286
2287 if (SubExpr->getType()->isRealFloatingType()) {
2288 Result.makeComplexFloat();
2289 APFloat &Imag = Result.FloatImag;
2290 if (!EvaluateFloat(SubExpr, Imag, Info))
2291 return false;
2292
2293 Result.FloatReal = APFloat(Imag.getSemantics());
2294 return true;
2295 } else {
2296 assert(SubExpr->getType()->isIntegerType() &&
2297 "Unexpected imaginary literal.");
2298
2299 Result.makeComplexInt();
2300 APSInt &Imag = Result.IntImag;
2301 if (!EvaluateInteger(SubExpr, Imag, Info))
2302 return false;
2303
2304 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2305 return true;
2306 }
2307}
2308
2309bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002310
John McCall8786da72010-12-14 17:51:41 +00002311 switch (E->getCastKind()) {
2312 case CK_BitCast:
John McCall8786da72010-12-14 17:51:41 +00002313 case CK_BaseToDerived:
2314 case CK_DerivedToBase:
2315 case CK_UncheckedDerivedToBase:
2316 case CK_Dynamic:
2317 case CK_ToUnion:
2318 case CK_ArrayToPointerDecay:
2319 case CK_FunctionToPointerDecay:
2320 case CK_NullToPointer:
2321 case CK_NullToMemberPointer:
2322 case CK_BaseToDerivedMemberPointer:
2323 case CK_DerivedToBaseMemberPointer:
2324 case CK_MemberPointerToBoolean:
2325 case CK_ConstructorConversion:
2326 case CK_IntegralToPointer:
2327 case CK_PointerToIntegral:
2328 case CK_PointerToBoolean:
2329 case CK_ToVoid:
2330 case CK_VectorSplat:
2331 case CK_IntegralCast:
2332 case CK_IntegralToBoolean:
2333 case CK_IntegralToFloating:
2334 case CK_FloatingToIntegral:
2335 case CK_FloatingToBoolean:
2336 case CK_FloatingCast:
2337 case CK_AnyPointerToObjCPointerCast:
2338 case CK_AnyPointerToBlockPointerCast:
2339 case CK_ObjCObjectLValueCast:
2340 case CK_FloatingComplexToReal:
2341 case CK_FloatingComplexToBoolean:
2342 case CK_IntegralComplexToReal:
2343 case CK_IntegralComplexToBoolean:
2344 llvm_unreachable("invalid cast kind for complex value");
John McCall2bb5d002010-11-13 09:02:35 +00002345
John McCall8786da72010-12-14 17:51:41 +00002346 case CK_LValueToRValue:
2347 case CK_NoOp:
2348 return Visit(E->getSubExpr());
2349
2350 case CK_Dependent:
2351 case CK_GetObjCProperty:
Eli Friedman46a52322011-03-25 00:43:55 +00002352 case CK_LValueBitCast:
John McCall8786da72010-12-14 17:51:41 +00002353 case CK_UserDefinedConversion:
2354 return false;
2355
2356 case CK_FloatingRealToComplex: {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002357 APFloat &Real = Result.FloatReal;
John McCall8786da72010-12-14 17:51:41 +00002358 if (!EvaluateFloat(E->getSubExpr(), Real, Info))
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002359 return false;
2360
John McCall8786da72010-12-14 17:51:41 +00002361 Result.makeComplexFloat();
2362 Result.FloatImag = APFloat(Real.getSemantics());
2363 return true;
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002364 }
2365
John McCall8786da72010-12-14 17:51:41 +00002366 case CK_FloatingComplexCast: {
2367 if (!Visit(E->getSubExpr()))
2368 return false;
2369
2370 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2371 QualType From
2372 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2373
2374 Result.FloatReal
2375 = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx);
2376 Result.FloatImag
2377 = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx);
2378 return true;
2379 }
2380
2381 case CK_FloatingComplexToIntegralComplex: {
2382 if (!Visit(E->getSubExpr()))
2383 return false;
2384
2385 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2386 QualType From
2387 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2388 Result.makeComplexInt();
2389 Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx);
2390 Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx);
2391 return true;
2392 }
2393
2394 case CK_IntegralRealToComplex: {
2395 APSInt &Real = Result.IntReal;
2396 if (!EvaluateInteger(E->getSubExpr(), Real, Info))
2397 return false;
2398
2399 Result.makeComplexInt();
2400 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2401 return true;
2402 }
2403
2404 case CK_IntegralComplexCast: {
2405 if (!Visit(E->getSubExpr()))
2406 return false;
2407
2408 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2409 QualType From
2410 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2411
2412 Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx);
2413 Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx);
2414 return true;
2415 }
2416
2417 case CK_IntegralComplexToFloatingComplex: {
2418 if (!Visit(E->getSubExpr()))
2419 return false;
2420
2421 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2422 QualType From
2423 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2424 Result.makeComplexFloat();
2425 Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx);
2426 Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx);
2427 return true;
2428 }
2429 }
2430
2431 llvm_unreachable("unknown cast resulting in complex value");
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002432 return false;
2433}
2434
John McCallf4cf1a12010-05-07 17:22:02 +00002435bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002436 if (E->getOpcode() == BO_Comma) {
2437 if (!Visit(E->getRHS()))
2438 return false;
2439
2440 // If we can't evaluate the LHS, it might have side effects;
2441 // conservatively mark it.
2442 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2443 Info.EvalResult.HasSideEffects = true;
2444
2445 return true;
2446 }
John McCallf4cf1a12010-05-07 17:22:02 +00002447 if (!Visit(E->getLHS()))
2448 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002449
John McCallf4cf1a12010-05-07 17:22:02 +00002450 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002451 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002452 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002453
Daniel Dunbar3f279872009-01-29 01:32:56 +00002454 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2455 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002456 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002457 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002458 case BO_Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002459 if (Result.isComplexFloat()) {
2460 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2461 APFloat::rmNearestTiesToEven);
2462 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2463 APFloat::rmNearestTiesToEven);
2464 } else {
2465 Result.getComplexIntReal() += RHS.getComplexIntReal();
2466 Result.getComplexIntImag() += RHS.getComplexIntImag();
2467 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002468 break;
John McCall2de56d12010-08-25 11:45:40 +00002469 case BO_Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002470 if (Result.isComplexFloat()) {
2471 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2472 APFloat::rmNearestTiesToEven);
2473 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2474 APFloat::rmNearestTiesToEven);
2475 } else {
2476 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2477 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2478 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002479 break;
John McCall2de56d12010-08-25 11:45:40 +00002480 case BO_Mul:
Daniel Dunbar3f279872009-01-29 01:32:56 +00002481 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002482 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002483 APFloat &LHS_r = LHS.getComplexFloatReal();
2484 APFloat &LHS_i = LHS.getComplexFloatImag();
2485 APFloat &RHS_r = RHS.getComplexFloatReal();
2486 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002487
Daniel Dunbar3f279872009-01-29 01:32:56 +00002488 APFloat Tmp = LHS_r;
2489 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2490 Result.getComplexFloatReal() = Tmp;
2491 Tmp = LHS_i;
2492 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2493 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2494
2495 Tmp = LHS_r;
2496 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2497 Result.getComplexFloatImag() = Tmp;
2498 Tmp = LHS_i;
2499 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2500 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2501 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002502 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002503 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002504 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2505 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002506 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002507 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2508 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2509 }
2510 break;
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002511 case BO_Div:
2512 if (Result.isComplexFloat()) {
2513 ComplexValue LHS = Result;
2514 APFloat &LHS_r = LHS.getComplexFloatReal();
2515 APFloat &LHS_i = LHS.getComplexFloatImag();
2516 APFloat &RHS_r = RHS.getComplexFloatReal();
2517 APFloat &RHS_i = RHS.getComplexFloatImag();
2518 APFloat &Res_r = Result.getComplexFloatReal();
2519 APFloat &Res_i = Result.getComplexFloatImag();
2520
2521 APFloat Den = RHS_r;
2522 Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2523 APFloat Tmp = RHS_i;
2524 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2525 Den.add(Tmp, APFloat::rmNearestTiesToEven);
2526
2527 Res_r = LHS_r;
2528 Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2529 Tmp = LHS_i;
2530 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2531 Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
2532 Res_r.divide(Den, APFloat::rmNearestTiesToEven);
2533
2534 Res_i = LHS_i;
2535 Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2536 Tmp = LHS_r;
2537 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2538 Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
2539 Res_i.divide(Den, APFloat::rmNearestTiesToEven);
2540 } else {
2541 if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) {
2542 // FIXME: what about diagnostics?
2543 return false;
2544 }
2545 ComplexValue LHS = Result;
2546 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
2547 RHS.getComplexIntImag() * RHS.getComplexIntImag();
2548 Result.getComplexIntReal() =
2549 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
2550 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
2551 Result.getComplexIntImag() =
2552 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
2553 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
2554 }
2555 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002556 }
2557
John McCallf4cf1a12010-05-07 17:22:02 +00002558 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002559}
2560
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002561bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
2562 // Get the operand value into 'Result'.
2563 if (!Visit(E->getSubExpr()))
2564 return false;
2565
2566 switch (E->getOpcode()) {
2567 default:
2568 // FIXME: what about diagnostics?
2569 return false;
2570 case UO_Extension:
2571 return true;
2572 case UO_Plus:
2573 // The result is always just the subexpr.
2574 return true;
2575 case UO_Minus:
2576 if (Result.isComplexFloat()) {
2577 Result.getComplexFloatReal().changeSign();
2578 Result.getComplexFloatImag().changeSign();
2579 }
2580 else {
2581 Result.getComplexIntReal() = -Result.getComplexIntReal();
2582 Result.getComplexIntImag() = -Result.getComplexIntImag();
2583 }
2584 return true;
2585 case UO_Not:
2586 if (Result.isComplexFloat())
2587 Result.getComplexFloatImag().changeSign();
2588 else
2589 Result.getComplexIntImag() = -Result.getComplexIntImag();
2590 return true;
2591 }
2592}
2593
John McCall56ca35d2011-02-17 10:25:35 +00002594bool ComplexExprEvaluator::
2595VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) {
2596 OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon());
2597 if (opaque.hasError()) return false;
2598
2599 bool cond;
2600 if (!HandleConversionToBool(e->getCond(), cond, Info))
2601 return false;
2602
2603 return Visit(cond ? e->getTrueExpr() : e->getFalseExpr());
2604}
2605
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002606bool ComplexExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
2607 bool Cond;
2608 if (!HandleConversionToBool(E->getCond(), Cond, Info))
2609 return false;
2610
2611 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
2612}
2613
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002614//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002615// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002616//===----------------------------------------------------------------------===//
2617
John McCall56ca35d2011-02-17 10:25:35 +00002618static bool Evaluate(EvalInfo &Info, const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +00002619 if (E->getType()->isVectorType()) {
2620 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002621 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002622 } else if (E->getType()->isIntegerType()) {
2623 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002624 return false;
John McCall56ca35d2011-02-17 10:25:35 +00002625 if (Info.EvalResult.Val.isLValue() &&
2626 !IsGlobalLValue(Info.EvalResult.Val.getLValueBase()))
John McCall0f2b6922010-07-07 05:08:32 +00002627 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002628 } else if (E->getType()->hasPointerRepresentation()) {
2629 LValue LV;
2630 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002631 return false;
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002632 if (!IsGlobalLValue(LV.Base))
John McCall42c8f872010-05-10 23:27:23 +00002633 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002634 LV.moveInto(Info.EvalResult.Val);
2635 } else if (E->getType()->isRealFloatingType()) {
2636 llvm::APFloat F(0.0);
2637 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002638 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002639
John McCallefdb83e2010-05-07 21:00:08 +00002640 Info.EvalResult.Val = APValue(F);
2641 } else if (E->getType()->isAnyComplexType()) {
2642 ComplexValue C;
2643 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002644 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002645 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002646 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002647 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002648
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002649 return true;
2650}
2651
John McCall56ca35d2011-02-17 10:25:35 +00002652/// Evaluate - Return true if this is a constant which we can fold using
2653/// any crazy technique (that has nothing to do with language standards) that
2654/// we want to. If this function returns true, it returns the folded constant
2655/// in Result.
2656bool Expr::Evaluate(EvalResult &Result, const ASTContext &Ctx) const {
2657 EvalInfo Info(Ctx, Result);
2658 return ::Evaluate(Info, this);
2659}
2660
Jay Foad4ba2a172011-01-12 09:06:06 +00002661bool Expr::EvaluateAsBooleanCondition(bool &Result,
2662 const ASTContext &Ctx) const {
John McCallcd7a4452010-01-05 23:42:56 +00002663 EvalResult Scratch;
2664 EvalInfo Info(Ctx, Scratch);
2665
2666 return HandleConversionToBool(this, Result, Info);
2667}
2668
Jay Foad4ba2a172011-01-12 09:06:06 +00002669bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
Anders Carlsson1b782762009-04-10 04:54:13 +00002670 EvalInfo Info(Ctx, Result);
2671
John McCallefdb83e2010-05-07 21:00:08 +00002672 LValue LV;
John McCall42c8f872010-05-10 23:27:23 +00002673 if (EvaluateLValue(this, LV, Info) &&
2674 !Result.HasSideEffects &&
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002675 IsGlobalLValue(LV.Base)) {
2676 LV.moveInto(Result.Val);
2677 return true;
2678 }
2679 return false;
2680}
2681
Jay Foad4ba2a172011-01-12 09:06:06 +00002682bool Expr::EvaluateAsAnyLValue(EvalResult &Result,
2683 const ASTContext &Ctx) const {
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002684 EvalInfo Info(Ctx, Result);
2685
2686 LValue LV;
2687 if (EvaluateLValue(this, LV, Info)) {
John McCallefdb83e2010-05-07 21:00:08 +00002688 LV.moveInto(Result.Val);
2689 return true;
2690 }
2691 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002692}
2693
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002694/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002695/// folded, but discard the result.
Jay Foad4ba2a172011-01-12 09:06:06 +00002696bool Expr::isEvaluatable(const ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002697 EvalResult Result;
2698 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002699}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002700
Jay Foad4ba2a172011-01-12 09:06:06 +00002701bool Expr::HasSideEffects(const ASTContext &Ctx) const {
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002702 Expr::EvalResult Result;
2703 EvalInfo Info(Ctx, Result);
2704 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2705}
2706
Jay Foad4ba2a172011-01-12 09:06:06 +00002707APSInt Expr::EvaluateAsInt(const ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002708 EvalResult EvalResult;
2709 bool Result = Evaluate(EvalResult, Ctx);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00002710 (void)Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002711 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002712 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002713
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002714 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002715}
John McCalld905f5a2010-05-07 05:32:02 +00002716
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002717 bool Expr::EvalResult::isGlobalLValue() const {
2718 assert(Val.isLValue());
2719 return IsGlobalLValue(Val.getLValueBase());
2720 }
2721
2722
John McCalld905f5a2010-05-07 05:32:02 +00002723/// isIntegerConstantExpr - this recursive routine will test if an expression is
2724/// an integer constant expression.
2725
2726/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2727/// comma, etc
2728///
2729/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2730/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2731/// cast+dereference.
2732
2733// CheckICE - This function does the fundamental ICE checking: the returned
2734// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2735// Note that to reduce code duplication, this helper does no evaluation
2736// itself; the caller checks whether the expression is evaluatable, and
2737// in the rare cases where CheckICE actually cares about the evaluated
2738// value, it calls into Evalute.
2739//
2740// Meanings of Val:
2741// 0: This expression is an ICE if it can be evaluated by Evaluate.
2742// 1: This expression is not an ICE, but if it isn't evaluated, it's
2743// a legal subexpression for an ICE. This return value is used to handle
2744// the comma operator in C99 mode.
2745// 2: This expression is not an ICE, and is not a legal subexpression for one.
2746
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002747namespace {
2748
John McCalld905f5a2010-05-07 05:32:02 +00002749struct ICEDiag {
2750 unsigned Val;
2751 SourceLocation Loc;
2752
2753 public:
2754 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2755 ICEDiag() : Val(0) {}
2756};
2757
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002758}
2759
2760static ICEDiag NoDiag() { return ICEDiag(); }
John McCalld905f5a2010-05-07 05:32:02 +00002761
2762static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2763 Expr::EvalResult EVResult;
2764 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2765 !EVResult.Val.isInt()) {
2766 return ICEDiag(2, E->getLocStart());
2767 }
2768 return NoDiag();
2769}
2770
2771static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2772 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002773 if (!E->getType()->isIntegralOrEnumerationType()) {
John McCalld905f5a2010-05-07 05:32:02 +00002774 return ICEDiag(2, E->getLocStart());
2775 }
2776
2777 switch (E->getStmtClass()) {
John McCall63c00d72011-02-09 08:16:59 +00002778#define ABSTRACT_STMT(Node)
John McCalld905f5a2010-05-07 05:32:02 +00002779#define STMT(Node, Base) case Expr::Node##Class:
2780#define EXPR(Node, Base)
2781#include "clang/AST/StmtNodes.inc"
2782 case Expr::PredefinedExprClass:
2783 case Expr::FloatingLiteralClass:
2784 case Expr::ImaginaryLiteralClass:
2785 case Expr::StringLiteralClass:
2786 case Expr::ArraySubscriptExprClass:
2787 case Expr::MemberExprClass:
2788 case Expr::CompoundAssignOperatorClass:
2789 case Expr::CompoundLiteralExprClass:
2790 case Expr::ExtVectorElementExprClass:
2791 case Expr::InitListExprClass:
2792 case Expr::DesignatedInitExprClass:
2793 case Expr::ImplicitValueInitExprClass:
2794 case Expr::ParenListExprClass:
2795 case Expr::VAArgExprClass:
2796 case Expr::AddrLabelExprClass:
2797 case Expr::StmtExprClass:
2798 case Expr::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002799 case Expr::CUDAKernelCallExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002800 case Expr::CXXDynamicCastExprClass:
2801 case Expr::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002802 case Expr::CXXUuidofExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002803 case Expr::CXXNullPtrLiteralExprClass:
2804 case Expr::CXXThisExprClass:
2805 case Expr::CXXThrowExprClass:
2806 case Expr::CXXNewExprClass:
2807 case Expr::CXXDeleteExprClass:
2808 case Expr::CXXPseudoDestructorExprClass:
2809 case Expr::UnresolvedLookupExprClass:
2810 case Expr::DependentScopeDeclRefExprClass:
2811 case Expr::CXXConstructExprClass:
2812 case Expr::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002813 case Expr::ExprWithCleanupsClass:
John McCalld905f5a2010-05-07 05:32:02 +00002814 case Expr::CXXTemporaryObjectExprClass:
2815 case Expr::CXXUnresolvedConstructExprClass:
2816 case Expr::CXXDependentScopeMemberExprClass:
2817 case Expr::UnresolvedMemberExprClass:
2818 case Expr::ObjCStringLiteralClass:
2819 case Expr::ObjCEncodeExprClass:
2820 case Expr::ObjCMessageExprClass:
2821 case Expr::ObjCSelectorExprClass:
2822 case Expr::ObjCProtocolExprClass:
2823 case Expr::ObjCIvarRefExprClass:
2824 case Expr::ObjCPropertyRefExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002825 case Expr::ObjCIsaExprClass:
2826 case Expr::ShuffleVectorExprClass:
2827 case Expr::BlockExprClass:
2828 case Expr::BlockDeclRefExprClass:
2829 case Expr::NoStmtClass:
John McCall7cd7d1a2010-11-15 23:31:06 +00002830 case Expr::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +00002831 case Expr::PackExpansionExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +00002832 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002833 return ICEDiag(2, E->getLocStart());
2834
Douglas Gregoree8aff02011-01-04 17:33:58 +00002835 case Expr::SizeOfPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002836 case Expr::GNUNullExprClass:
2837 // GCC considers the GNU __null value to be an integral constant expression.
2838 return NoDiag();
2839
2840 case Expr::ParenExprClass:
2841 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2842 case Expr::IntegerLiteralClass:
2843 case Expr::CharacterLiteralClass:
2844 case Expr::CXXBoolLiteralExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +00002845 case Expr::CXXScalarValueInitExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002846 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002847 case Expr::BinaryTypeTraitExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002848 case Expr::CXXNoexceptExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002849 return NoDiag();
2850 case Expr::CallExprClass:
Sean Hunt6cf75022010-08-30 17:47:05 +00002851 case Expr::CXXOperatorCallExprClass: {
John McCalld905f5a2010-05-07 05:32:02 +00002852 const CallExpr *CE = cast<CallExpr>(E);
2853 if (CE->isBuiltinCall(Ctx))
2854 return CheckEvalInICE(E, Ctx);
2855 return ICEDiag(2, E->getLocStart());
2856 }
2857 case Expr::DeclRefExprClass:
2858 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2859 return NoDiag();
2860 if (Ctx.getLangOptions().CPlusPlus &&
2861 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2862 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2863
2864 // Parameter variables are never constants. Without this check,
2865 // getAnyInitializer() can find a default argument, which leads
2866 // to chaos.
2867 if (isa<ParmVarDecl>(D))
2868 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2869
2870 // C++ 7.1.5.1p2
2871 // A variable of non-volatile const-qualified integral or enumeration
2872 // type initialized by an ICE can be used in ICEs.
2873 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2874 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2875 if (Quals.hasVolatile() || !Quals.hasConst())
2876 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2877
2878 // Look for a declaration of this variable that has an initializer.
2879 const VarDecl *ID = 0;
2880 const Expr *Init = Dcl->getAnyInitializer(ID);
2881 if (Init) {
2882 if (ID->isInitKnownICE()) {
2883 // We have already checked whether this subexpression is an
2884 // integral constant expression.
2885 if (ID->isInitICE())
2886 return NoDiag();
2887 else
2888 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2889 }
2890
2891 // It's an ICE whether or not the definition we found is
2892 // out-of-line. See DR 721 and the discussion in Clang PR
2893 // 6206 for details.
2894
2895 if (Dcl->isCheckingICE()) {
2896 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2897 }
2898
2899 Dcl->setCheckingICE();
2900 ICEDiag Result = CheckICE(Init, Ctx);
2901 // Cache the result of the ICE test.
2902 Dcl->setInitKnownICE(Result.Val == 0);
2903 return Result;
2904 }
2905 }
2906 }
2907 return ICEDiag(2, E->getLocStart());
2908 case Expr::UnaryOperatorClass: {
2909 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2910 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002911 case UO_PostInc:
2912 case UO_PostDec:
2913 case UO_PreInc:
2914 case UO_PreDec:
2915 case UO_AddrOf:
2916 case UO_Deref:
John McCalld905f5a2010-05-07 05:32:02 +00002917 return ICEDiag(2, E->getLocStart());
John McCall2de56d12010-08-25 11:45:40 +00002918 case UO_Extension:
2919 case UO_LNot:
2920 case UO_Plus:
2921 case UO_Minus:
2922 case UO_Not:
2923 case UO_Real:
2924 case UO_Imag:
John McCalld905f5a2010-05-07 05:32:02 +00002925 return CheckICE(Exp->getSubExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002926 }
2927
2928 // OffsetOf falls through here.
2929 }
2930 case Expr::OffsetOfExprClass: {
2931 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2932 // Evaluate matches the proposed gcc behavior for cases like
2933 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2934 // compliance: we should warn earlier for offsetof expressions with
2935 // array subscripts that aren't ICEs, and if the array subscripts
2936 // are ICEs, the value of the offsetof must be an integer constant.
2937 return CheckEvalInICE(E, Ctx);
2938 }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002939 case Expr::UnaryExprOrTypeTraitExprClass: {
2940 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
2941 if ((Exp->getKind() == UETT_SizeOf) &&
2942 Exp->getTypeOfArgument()->isVariableArrayType())
John McCalld905f5a2010-05-07 05:32:02 +00002943 return ICEDiag(2, E->getLocStart());
2944 return NoDiag();
2945 }
2946 case Expr::BinaryOperatorClass: {
2947 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2948 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002949 case BO_PtrMemD:
2950 case BO_PtrMemI:
2951 case BO_Assign:
2952 case BO_MulAssign:
2953 case BO_DivAssign:
2954 case BO_RemAssign:
2955 case BO_AddAssign:
2956 case BO_SubAssign:
2957 case BO_ShlAssign:
2958 case BO_ShrAssign:
2959 case BO_AndAssign:
2960 case BO_XorAssign:
2961 case BO_OrAssign:
John McCalld905f5a2010-05-07 05:32:02 +00002962 return ICEDiag(2, E->getLocStart());
2963
John McCall2de56d12010-08-25 11:45:40 +00002964 case BO_Mul:
2965 case BO_Div:
2966 case BO_Rem:
2967 case BO_Add:
2968 case BO_Sub:
2969 case BO_Shl:
2970 case BO_Shr:
2971 case BO_LT:
2972 case BO_GT:
2973 case BO_LE:
2974 case BO_GE:
2975 case BO_EQ:
2976 case BO_NE:
2977 case BO_And:
2978 case BO_Xor:
2979 case BO_Or:
2980 case BO_Comma: {
John McCalld905f5a2010-05-07 05:32:02 +00002981 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2982 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
John McCall2de56d12010-08-25 11:45:40 +00002983 if (Exp->getOpcode() == BO_Div ||
2984 Exp->getOpcode() == BO_Rem) {
John McCalld905f5a2010-05-07 05:32:02 +00002985 // Evaluate gives an error for undefined Div/Rem, so make sure
2986 // we don't evaluate one.
John McCall3b332ab2011-02-26 08:27:17 +00002987 if (LHSResult.Val == 0 && RHSResult.Val == 0) {
John McCalld905f5a2010-05-07 05:32:02 +00002988 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2989 if (REval == 0)
2990 return ICEDiag(1, E->getLocStart());
2991 if (REval.isSigned() && REval.isAllOnesValue()) {
2992 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2993 if (LEval.isMinSignedValue())
2994 return ICEDiag(1, E->getLocStart());
2995 }
2996 }
2997 }
John McCall2de56d12010-08-25 11:45:40 +00002998 if (Exp->getOpcode() == BO_Comma) {
John McCalld905f5a2010-05-07 05:32:02 +00002999 if (Ctx.getLangOptions().C99) {
3000 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
3001 // if it isn't evaluated.
3002 if (LHSResult.Val == 0 && RHSResult.Val == 0)
3003 return ICEDiag(1, E->getLocStart());
3004 } else {
3005 // In both C89 and C++, commas in ICEs are illegal.
3006 return ICEDiag(2, E->getLocStart());
3007 }
3008 }
3009 if (LHSResult.Val >= RHSResult.Val)
3010 return LHSResult;
3011 return RHSResult;
3012 }
John McCall2de56d12010-08-25 11:45:40 +00003013 case BO_LAnd:
3014 case BO_LOr: {
John McCalld905f5a2010-05-07 05:32:02 +00003015 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
3016 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
3017 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
3018 // Rare case where the RHS has a comma "side-effect"; we need
3019 // to actually check the condition to see whether the side
3020 // with the comma is evaluated.
John McCall2de56d12010-08-25 11:45:40 +00003021 if ((Exp->getOpcode() == BO_LAnd) !=
John McCalld905f5a2010-05-07 05:32:02 +00003022 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
3023 return RHSResult;
3024 return NoDiag();
3025 }
3026
3027 if (LHSResult.Val >= RHSResult.Val)
3028 return LHSResult;
3029 return RHSResult;
3030 }
3031 }
3032 }
3033 case Expr::ImplicitCastExprClass:
3034 case Expr::CStyleCastExprClass:
3035 case Expr::CXXFunctionalCastExprClass:
3036 case Expr::CXXStaticCastExprClass:
3037 case Expr::CXXReinterpretCastExprClass:
3038 case Expr::CXXConstCastExprClass: {
3039 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003040 if (SubExpr->getType()->isIntegralOrEnumerationType())
John McCalld905f5a2010-05-07 05:32:02 +00003041 return CheckICE(SubExpr, Ctx);
3042 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
3043 return NoDiag();
3044 return ICEDiag(2, E->getLocStart());
3045 }
John McCall56ca35d2011-02-17 10:25:35 +00003046 case Expr::BinaryConditionalOperatorClass: {
3047 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
3048 ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
3049 if (CommonResult.Val == 2) return CommonResult;
3050 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3051 if (FalseResult.Val == 2) return FalseResult;
3052 if (CommonResult.Val == 1) return CommonResult;
3053 if (FalseResult.Val == 1 &&
3054 Exp->getCommon()->EvaluateAsInt(Ctx) == 0) return NoDiag();
3055 return FalseResult;
3056 }
John McCalld905f5a2010-05-07 05:32:02 +00003057 case Expr::ConditionalOperatorClass: {
3058 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
3059 // If the condition (ignoring parens) is a __builtin_constant_p call,
3060 // then only the true side is actually considered in an integer constant
3061 // expression, and it is fully evaluated. This is an important GNU
3062 // extension. See GCC PR38377 for discussion.
3063 if (const CallExpr *CallCE
3064 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
3065 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
3066 Expr::EvalResult EVResult;
3067 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
3068 !EVResult.Val.isInt()) {
3069 return ICEDiag(2, E->getLocStart());
3070 }
3071 return NoDiag();
3072 }
3073 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
3074 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
3075 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3076 if (CondResult.Val == 2)
3077 return CondResult;
3078 if (TrueResult.Val == 2)
3079 return TrueResult;
3080 if (FalseResult.Val == 2)
3081 return FalseResult;
3082 if (CondResult.Val == 1)
3083 return CondResult;
3084 if (TrueResult.Val == 0 && FalseResult.Val == 0)
3085 return NoDiag();
3086 // Rare case where the diagnostics depend on which side is evaluated
3087 // Note that if we get here, CondResult is 0, and at least one of
3088 // TrueResult and FalseResult is non-zero.
3089 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
3090 return FalseResult;
3091 }
3092 return TrueResult;
3093 }
3094 case Expr::CXXDefaultArgExprClass:
3095 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
3096 case Expr::ChooseExprClass: {
3097 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
3098 }
3099 }
3100
3101 // Silence a GCC warning
3102 return ICEDiag(2, E->getLocStart());
3103}
3104
3105bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
3106 SourceLocation *Loc, bool isEvaluated) const {
3107 ICEDiag d = CheckICE(this, Ctx);
3108 if (d.Val != 0) {
3109 if (Loc) *Loc = d.Loc;
3110 return false;
3111 }
3112 EvalResult EvalResult;
3113 if (!Evaluate(EvalResult, Ctx))
3114 llvm_unreachable("ICE cannot be evaluated!");
3115 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
3116 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
3117 Result = EvalResult.Val.getInt();
3118 return true;
3119}