blob: d97d6256167daf96823021cd747f599c00f29762 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
45struct EvalInfo {
46 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson54da0492008-11-30 16:38:33 +000048 /// EvalResult - Contains information about the evaluation.
49 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000050
John McCall42c8f872010-05-10 23:27:23 +000051 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult)
52 : Ctx(ctx), EvalResult(evalresult) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000053};
54
John McCallf4cf1a12010-05-07 17:22:02 +000055namespace {
56 struct ComplexValue {
57 private:
58 bool IsInt;
59
60 public:
61 APSInt IntReal, IntImag;
62 APFloat FloatReal, FloatImag;
63
64 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
65
66 void makeComplexFloat() { IsInt = false; }
67 bool isComplexFloat() const { return !IsInt; }
68 APFloat &getComplexFloatReal() { return FloatReal; }
69 APFloat &getComplexFloatImag() { return FloatImag; }
70
71 void makeComplexInt() { IsInt = true; }
72 bool isComplexInt() const { return IsInt; }
73 APSInt &getComplexIntReal() { return IntReal; }
74 APSInt &getComplexIntImag() { return IntImag; }
75
76 void moveInto(APValue &v) {
77 if (isComplexFloat())
78 v = APValue(FloatReal, FloatImag);
79 else
80 v = APValue(IntReal, IntImag);
81 }
82 };
John McCallefdb83e2010-05-07 21:00:08 +000083
84 struct LValue {
85 Expr *Base;
86 CharUnits Offset;
87
88 Expr *getLValueBase() { return Base; }
89 CharUnits getLValueOffset() { return Offset; }
90
91 void moveInto(APValue &v) {
92 v = APValue(Base, Offset);
93 }
94 };
John McCallf4cf1a12010-05-07 17:22:02 +000095}
Chris Lattner87eae5e2008-07-11 22:52:41 +000096
John McCallefdb83e2010-05-07 21:00:08 +000097static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
98static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000099static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +0000100static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
101 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000102static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCallf4cf1a12010-05-07 17:22:02 +0000103static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000104
105//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000106// Misc utilities
107//===----------------------------------------------------------------------===//
108
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000109static bool IsGlobalLValue(const Expr* E) {
John McCall42c8f872010-05-10 23:27:23 +0000110 if (!E) return true;
111
112 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
113 if (isa<FunctionDecl>(DRE->getDecl()))
114 return true;
115 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
116 return VD->hasGlobalStorage();
117 return false;
118 }
119
120 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
121 return CLE->isFileScope();
122
123 return true;
124}
125
John McCallefdb83e2010-05-07 21:00:08 +0000126static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
127 const Expr* Base = Value.Base;
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000128
John McCall35542832010-05-07 21:34:32 +0000129 // A null base expression indicates a null pointer. These are always
130 // evaluatable, and they are false unless the offset is zero.
131 if (!Base) {
132 Result = !Value.Offset.isZero();
133 return true;
134 }
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000135
John McCall42c8f872010-05-10 23:27:23 +0000136 // Require the base expression to be a global l-value.
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000137 if (!IsGlobalLValue(Base)) return false;
John McCall42c8f872010-05-10 23:27:23 +0000138
John McCall35542832010-05-07 21:34:32 +0000139 // We have a non-null base expression. These are generally known to
140 // be true, but if it'a decl-ref to a weak symbol it can be null at
141 // runtime.
John McCall35542832010-05-07 21:34:32 +0000142 Result = true;
143
144 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000145 if (!DeclRef)
146 return true;
147
John McCall35542832010-05-07 21:34:32 +0000148 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000149 const ValueDecl* Decl = DeclRef->getDecl();
150 if (Decl->hasAttr<WeakAttr>() ||
151 Decl->hasAttr<WeakRefAttr>() ||
152 Decl->hasAttr<WeakImportAttr>())
153 return false;
154
Eli Friedman5bc86102009-06-14 02:17:33 +0000155 return true;
156}
157
John McCallcd7a4452010-01-05 23:42:56 +0000158static bool HandleConversionToBool(const Expr* E, bool& Result,
159 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000160 if (E->getType()->isIntegralType()) {
161 APSInt IntResult;
162 if (!EvaluateInteger(E, IntResult, Info))
163 return false;
164 Result = IntResult != 0;
165 return true;
166 } else if (E->getType()->isRealFloatingType()) {
167 APFloat FloatResult(0.0);
168 if (!EvaluateFloat(E, FloatResult, Info))
169 return false;
170 Result = !FloatResult.isZero();
171 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000172 } else if (E->getType()->hasPointerRepresentation()) {
John McCallefdb83e2010-05-07 21:00:08 +0000173 LValue PointerResult;
Eli Friedman4efaa272008-11-12 09:44:48 +0000174 if (!EvaluatePointer(E, PointerResult, Info))
175 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +0000176 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +0000177 } else if (E->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +0000178 ComplexValue ComplexResult;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000179 if (!EvaluateComplex(E, ComplexResult, Info))
180 return false;
181 if (ComplexResult.isComplexFloat()) {
182 Result = !ComplexResult.getComplexFloatReal().isZero() ||
183 !ComplexResult.getComplexFloatImag().isZero();
184 } else {
185 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
186 ComplexResult.getComplexIntImag().getBoolValue();
187 }
188 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000189 }
190
191 return false;
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000195 APFloat &Value, ASTContext &Ctx) {
196 unsigned DestWidth = Ctx.getIntWidth(DestType);
197 // Determine whether we are converting to unsigned or signed.
198 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000200 // FIXME: Warning for overflow.
201 uint64_t Space[4];
202 bool ignored;
203 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
204 llvm::APFloat::rmTowardZero, &ignored);
205 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000209 APFloat &Value, ASTContext &Ctx) {
210 bool ignored;
211 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000212 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000213 APFloat::rmNearestTiesToEven, &ignored);
214 return Result;
215}
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000218 APSInt &Value, ASTContext &Ctx) {
219 unsigned DestWidth = Ctx.getIntWidth(DestType);
220 APSInt Result = Value;
221 // Figure out if this is a truncate, extend or noop cast.
222 // If the input is signed, do a sign extend, noop, or truncate.
223 Result.extOrTrunc(DestWidth);
224 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
225 return Result;
226}
227
Mike Stump1eb44332009-09-09 15:08:12 +0000228static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000229 APSInt &Value, ASTContext &Ctx) {
230
231 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
232 Result.convertFromAPInt(Value, Value.isSigned(),
233 APFloat::rmNearestTiesToEven);
234 return Result;
235}
236
Mike Stumpc4c90452009-10-27 22:09:17 +0000237namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000238class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000239 : public StmtVisitor<HasSideEffect, bool> {
240 EvalInfo &Info;
241public:
242
243 HasSideEffect(EvalInfo &info) : Info(info) {}
244
245 // Unhandled nodes conservatively default to having side effects.
246 bool VisitStmt(Stmt *S) {
247 return true;
248 }
249
250 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
251 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000252 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000253 return true;
254 return false;
255 }
256 // We don't want to evaluate BlockExprs multiple times, as they generate
257 // a ton of code.
258 bool VisitBlockExpr(BlockExpr *E) { return true; }
259 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
260 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
261 { return Visit(E->getInitializer()); }
262 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
263 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
264 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
265 bool VisitStringLiteral(StringLiteral *E) { return false; }
266 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
267 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
268 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000269 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000270 bool VisitChooseExpr(ChooseExpr *E)
271 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
272 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
273 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000274 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000275 bool VisitBinaryOperator(BinaryOperator *E)
276 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000277 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
278 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
279 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
280 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
281 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000282 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000283 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000284 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000285 }
286 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000287
288 // Has side effects if any element does.
289 bool VisitInitListExpr(InitListExpr *E) {
290 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
291 if (Visit(E->getInit(i))) return true;
292 return false;
293 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000294};
295
Mike Stumpc4c90452009-10-27 22:09:17 +0000296} // end anonymous namespace
297
Eli Friedman4efaa272008-11-12 09:44:48 +0000298//===----------------------------------------------------------------------===//
299// LValue Evaluation
300//===----------------------------------------------------------------------===//
301namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000302class LValueExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000303 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman4efaa272008-11-12 09:44:48 +0000304 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000305 LValue &Result;
306
307 bool Success(Expr *E) {
308 Result.Base = E;
309 Result.Offset = CharUnits::Zero();
310 return true;
311 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000312public:
Mike Stump1eb44332009-09-09 15:08:12 +0000313
John McCallefdb83e2010-05-07 21:00:08 +0000314 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
315 Info(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000316
John McCallefdb83e2010-05-07 21:00:08 +0000317 bool VisitStmt(Stmt *S) {
318 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000319 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000320
John McCallefdb83e2010-05-07 21:00:08 +0000321 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
322 bool VisitDeclRefExpr(DeclRefExpr *E);
323 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
324 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
325 bool VisitMemberExpr(MemberExpr *E);
326 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
327 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
328 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
329 bool VisitUnaryDeref(UnaryOperator *E);
330 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000331 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000332 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000333 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000334
John McCallefdb83e2010-05-07 21:00:08 +0000335 bool VisitCastExpr(CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000336 switch (E->getCastKind()) {
337 default:
John McCallefdb83e2010-05-07 21:00:08 +0000338 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000339
340 case CastExpr::CK_NoOp:
341 return Visit(E->getSubExpr());
342 }
343 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000344 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000345};
346} // end anonymous namespace
347
John McCallefdb83e2010-05-07 21:00:08 +0000348static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
349 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman4efaa272008-11-12 09:44:48 +0000350}
351
John McCallefdb83e2010-05-07 21:00:08 +0000352bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000353 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000354 return Success(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000355 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
356 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000357 return Success(E);
Chandler Carruth761c94e2010-05-16 09:32:51 +0000358 // Reference parameters can refer to anything even if they have an
359 // "initializer" in the form of a default argument.
360 if (isa<ParmVarDecl>(VD))
361 return false;
Eli Friedmand933a012009-08-29 19:09:59 +0000362 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000363 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000364 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000365 }
366
John McCallefdb83e2010-05-07 21:00:08 +0000367 return false;
Anders Carlsson35873c42008-11-24 04:41:22 +0000368}
369
John McCallefdb83e2010-05-07 21:00:08 +0000370bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000371 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000372}
373
John McCallefdb83e2010-05-07 21:00:08 +0000374bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000375 QualType Ty;
376 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000377 if (!EvaluatePointer(E->getBase(), Result, Info))
378 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000379 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000380 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000381 if (!Visit(E->getBase()))
382 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000383 Ty = E->getBase()->getType();
384 }
385
Ted Kremenek6217b802009-07-29 21:53:49 +0000386 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000387 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000388
389 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
390 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000391 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000392
393 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000394 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000395
Eli Friedman4efaa272008-11-12 09:44:48 +0000396 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000397 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000398 for (RecordDecl::field_iterator Field = RD->field_begin(),
399 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000400 Field != FieldEnd; (void)++Field, ++i) {
401 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000402 break;
403 }
404
John McCallefdb83e2010-05-07 21:00:08 +0000405 Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8);
406 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000407}
408
John McCallefdb83e2010-05-07 21:00:08 +0000409bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000410 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000411 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson3068d112008-11-16 19:01:22 +0000413 APSInt Index;
414 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000415 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000416
Ken Dyck199c3d62010-01-11 17:06:35 +0000417 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000418 Result.Offset += Index.getSExtValue() * ElementSize;
419 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000420}
Eli Friedman4efaa272008-11-12 09:44:48 +0000421
John McCallefdb83e2010-05-07 21:00:08 +0000422bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
423 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000424}
425
Eli Friedman4efaa272008-11-12 09:44:48 +0000426//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427// Pointer Evaluation
428//===----------------------------------------------------------------------===//
429
Anders Carlssonc754aa62008-07-08 05:13:58 +0000430namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000431class PointerExprEvaluator
John McCallefdb83e2010-05-07 21:00:08 +0000432 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000433 EvalInfo &Info;
John McCallefdb83e2010-05-07 21:00:08 +0000434 LValue &Result;
435
436 bool Success(Expr *E) {
437 Result.Base = E;
438 Result.Offset = CharUnits::Zero();
439 return true;
440 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000441public:
Mike Stump1eb44332009-09-09 15:08:12 +0000442
John McCallefdb83e2010-05-07 21:00:08 +0000443 PointerExprEvaluator(EvalInfo &info, LValue &Result)
444 : Info(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000445
John McCallefdb83e2010-05-07 21:00:08 +0000446 bool VisitStmt(Stmt *S) {
447 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000448 }
449
John McCallefdb83e2010-05-07 21:00:08 +0000450 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000451
John McCallefdb83e2010-05-07 21:00:08 +0000452 bool VisitBinaryOperator(const BinaryOperator *E);
453 bool VisitCastExpr(CastExpr* E);
454 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman2217c872009-02-22 11:46:18 +0000455 { return Visit(E->getSubExpr()); }
John McCallefdb83e2010-05-07 21:00:08 +0000456 bool VisitUnaryAddrOf(const UnaryOperator *E);
457 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
458 { return Success(E); }
459 bool VisitAddrLabelExpr(AddrLabelExpr *E)
460 { return Success(E); }
461 bool VisitCallExpr(CallExpr *E);
462 bool VisitBlockExpr(BlockExpr *E) {
Mike Stumpb83d2872009-02-19 22:01:56 +0000463 if (!E->hasBlockDeclRefExprs())
John McCallefdb83e2010-05-07 21:00:08 +0000464 return Success(E);
465 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000466 }
John McCallefdb83e2010-05-07 21:00:08 +0000467 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
468 { return Success((Expr*)0); }
469 bool VisitConditionalOperator(ConditionalOperator *E);
470 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000471 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallefdb83e2010-05-07 21:00:08 +0000472 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
473 { return Success((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000474 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000475};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000476} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000477
John McCallefdb83e2010-05-07 21:00:08 +0000478static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000479 assert(E->getType()->hasPointerRepresentation());
John McCallefdb83e2010-05-07 21:00:08 +0000480 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000481}
482
John McCallefdb83e2010-05-07 21:00:08 +0000483bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000484 if (E->getOpcode() != BinaryOperator::Add &&
485 E->getOpcode() != BinaryOperator::Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000486 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000488 const Expr *PExp = E->getLHS();
489 const Expr *IExp = E->getRHS();
490 if (IExp->getType()->isPointerType())
491 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
John McCallefdb83e2010-05-07 21:00:08 +0000493 if (!EvaluatePointer(PExp, Result, Info))
494 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
John McCallefdb83e2010-05-07 21:00:08 +0000496 llvm::APSInt Offset;
497 if (!EvaluateInteger(IExp, Offset, Info))
498 return false;
499 int64_t AdditionalOffset
500 = Offset.isSigned() ? Offset.getSExtValue()
501 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000502
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000503 // Compute the new offset in the appropriate width.
504
505 QualType PointeeType =
506 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000507 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000509 // Explicitly handle GNU void* and function pointer arithmetic extensions.
510 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000511 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000512 else
John McCallefdb83e2010-05-07 21:00:08 +0000513 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000514
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000515 if (E->getOpcode() == BinaryOperator::Add)
John McCallefdb83e2010-05-07 21:00:08 +0000516 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000517 else
John McCallefdb83e2010-05-07 21:00:08 +0000518 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000519
John McCallefdb83e2010-05-07 21:00:08 +0000520 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000521}
Eli Friedman4efaa272008-11-12 09:44:48 +0000522
John McCallefdb83e2010-05-07 21:00:08 +0000523bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
524 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000525}
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000527
John McCallefdb83e2010-05-07 21:00:08 +0000528bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000529 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000530
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000531 switch (E->getCastKind()) {
532 default:
533 break;
534
535 case CastExpr::CK_Unknown: {
536 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
537
538 // Check for pointer->pointer cast
539 if (SubExpr->getType()->isPointerType() ||
540 SubExpr->getType()->isObjCObjectPointerType() ||
541 SubExpr->getType()->isNullPtrType() ||
542 SubExpr->getType()->isBlockPointerType())
543 return Visit(SubExpr);
544
545 if (SubExpr->getType()->isIntegralType()) {
John McCallefdb83e2010-05-07 21:00:08 +0000546 APValue Value;
547 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000548 break;
549
John McCallefdb83e2010-05-07 21:00:08 +0000550 if (Value.isInt()) {
551 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
552 Result.Base = 0;
553 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
554 return true;
555 } else {
556 Result.Base = Value.getLValueBase();
557 Result.Offset = Value.getLValueOffset();
558 return true;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000559 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000560 }
561 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000564 case CastExpr::CK_NoOp:
565 case CastExpr::CK_BitCast:
566 case CastExpr::CK_AnyPointerToObjCPointerCast:
567 case CastExpr::CK_AnyPointerToBlockPointerCast:
568 return Visit(SubExpr);
569
570 case CastExpr::CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000571 APValue Value;
572 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000573 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000574
John McCallefdb83e2010-05-07 21:00:08 +0000575 if (Value.isInt()) {
576 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
577 Result.Base = 0;
578 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
579 return true;
580 } else {
581 // Cast is of an lvalue, no need to change value.
582 Result.Base = Value.getLValueBase();
583 Result.Offset = Value.getLValueOffset();
584 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000585 }
586 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000587 case CastExpr::CK_ArrayToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000588 case CastExpr::CK_FunctionToPointerDecay:
589 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000590 }
591
John McCallefdb83e2010-05-07 21:00:08 +0000592 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000593}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000594
John McCallefdb83e2010-05-07 21:00:08 +0000595bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000596 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000597 Builtin::BI__builtin___CFStringMakeConstantString ||
598 E->isBuiltinCall(Info.Ctx) ==
599 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000600 return Success(E);
601 return false;
Eli Friedman3941b182009-01-25 01:54:01 +0000602}
603
John McCallefdb83e2010-05-07 21:00:08 +0000604bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000605 bool BoolResult;
606 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000607 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000608
609 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCallefdb83e2010-05-07 21:00:08 +0000610 return Visit(EvalExpr);
Eli Friedman4efaa272008-11-12 09:44:48 +0000611}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000612
613//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000614// Vector Evaluation
615//===----------------------------------------------------------------------===//
616
617namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000618 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000619 : public StmtVisitor<VectorExprEvaluator, APValue> {
620 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000621 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000622 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Nate Begeman59b5da62009-01-18 03:20:47 +0000624 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Nate Begeman59b5da62009-01-18 03:20:47 +0000626 APValue VisitStmt(Stmt *S) {
627 return APValue();
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Eli Friedman91110ee2009-02-23 04:23:56 +0000630 APValue VisitParenExpr(ParenExpr *E)
631 { return Visit(E->getSubExpr()); }
632 APValue VisitUnaryExtension(const UnaryOperator *E)
633 { return Visit(E->getSubExpr()); }
634 APValue VisitUnaryPlus(const UnaryOperator *E)
635 { return Visit(E->getSubExpr()); }
636 APValue VisitUnaryReal(const UnaryOperator *E)
637 { return Visit(E->getSubExpr()); }
638 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
639 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000640 APValue VisitCastExpr(const CastExpr* E);
641 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
642 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000643 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000644 APValue VisitChooseExpr(const ChooseExpr *E)
645 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000646 APValue VisitUnaryImag(const UnaryOperator *E);
647 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000648 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000649 // shufflevector, ExtVectorElementExpr
650 // (Note that these require implementing conversions
651 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000652 };
653} // end anonymous namespace
654
655static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
656 if (!E->getType()->isVectorType())
657 return false;
658 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
659 return !Result.isUninit();
660}
661
662APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000663 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000664 QualType EltTy = VTy->getElementType();
665 unsigned NElts = VTy->getNumElements();
666 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Nate Begeman59b5da62009-01-18 03:20:47 +0000668 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000669 QualType SETy = SE->getType();
670 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000671
Nate Begemane8c9e922009-06-26 18:22:18 +0000672 // Check for vector->vector bitcast and scalar->vector splat.
673 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000674 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000675 } else if (SETy->isIntegerType()) {
676 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000677 if (!EvaluateInteger(SE, IntResult, Info))
678 return APValue();
679 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000680 } else if (SETy->isRealFloatingType()) {
681 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000682 if (!EvaluateFloat(SE, F, Info))
683 return APValue();
684 Result = APValue(F);
685 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000686 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000687
Nate Begemanc0b8b192009-07-01 07:50:47 +0000688 // For casts of a scalar to ExtVector, convert the scalar to the element type
689 // and splat it to all elements.
690 if (E->getType()->isExtVectorType()) {
691 if (EltTy->isIntegerType() && Result.isInt())
692 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
693 Info.Ctx));
694 else if (EltTy->isIntegerType())
695 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
696 Info.Ctx));
697 else if (EltTy->isRealFloatingType() && Result.isInt())
698 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
699 Info.Ctx));
700 else if (EltTy->isRealFloatingType())
701 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
702 Info.Ctx));
703 else
704 return APValue();
705
706 // Splat and create vector APValue.
707 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
708 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000709 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000710
711 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
712 // to the vector. To construct the APValue vector initializer, bitcast the
713 // initializing value to an APInt, and shift out the bits pertaining to each
714 // element.
715 APSInt Init;
716 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Nate Begemanc0b8b192009-07-01 07:50:47 +0000718 llvm::SmallVector<APValue, 4> Elts;
719 for (unsigned i = 0; i != NElts; ++i) {
720 APSInt Tmp = Init;
721 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Nate Begemanc0b8b192009-07-01 07:50:47 +0000723 if (EltTy->isIntegerType())
724 Elts.push_back(APValue(Tmp));
725 else if (EltTy->isRealFloatingType())
726 Elts.push_back(APValue(APFloat(Tmp)));
727 else
728 return APValue();
729
730 Init >>= EltWidth;
731 }
732 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000733}
734
Mike Stump1eb44332009-09-09 15:08:12 +0000735APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000736VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
737 return this->Visit(const_cast<Expr*>(E->getInitializer()));
738}
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000741VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000742 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000743 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000744 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Nate Begeman59b5da62009-01-18 03:20:47 +0000746 QualType EltTy = VT->getElementType();
747 llvm::SmallVector<APValue, 4> Elements;
748
John McCalla7d6c222010-06-11 17:54:15 +0000749 // If a vector is initialized with a single element, that value
750 // becomes every element of the vector, not just the first.
751 // This is the behavior described in the IBM AltiVec documentation.
752 if (NumInits == 1) {
753 APValue InitValue;
Nate Begeman59b5da62009-01-18 03:20:47 +0000754 if (EltTy->isIntegerType()) {
755 llvm::APSInt sInt(32);
John McCalla7d6c222010-06-11 17:54:15 +0000756 if (!EvaluateInteger(E->getInit(0), sInt, Info))
757 return APValue();
758 InitValue = APValue(sInt);
Nate Begeman59b5da62009-01-18 03:20:47 +0000759 } else {
760 llvm::APFloat f(0.0);
John McCalla7d6c222010-06-11 17:54:15 +0000761 if (!EvaluateFloat(E->getInit(0), f, Info))
762 return APValue();
763 InitValue = APValue(f);
764 }
765 for (unsigned i = 0; i < NumElements; i++) {
766 Elements.push_back(InitValue);
767 }
768 } else {
769 for (unsigned i = 0; i < NumElements; i++) {
770 if (EltTy->isIntegerType()) {
771 llvm::APSInt sInt(32);
772 if (i < NumInits) {
773 if (!EvaluateInteger(E->getInit(i), sInt, Info))
774 return APValue();
775 } else {
776 sInt = Info.Ctx.MakeIntValue(0, EltTy);
777 }
778 Elements.push_back(APValue(sInt));
Eli Friedman91110ee2009-02-23 04:23:56 +0000779 } else {
John McCalla7d6c222010-06-11 17:54:15 +0000780 llvm::APFloat f(0.0);
781 if (i < NumInits) {
782 if (!EvaluateFloat(E->getInit(i), f, Info))
783 return APValue();
784 } else {
785 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
786 }
787 Elements.push_back(APValue(f));
Eli Friedman91110ee2009-02-23 04:23:56 +0000788 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000789 }
790 }
791 return APValue(&Elements[0], Elements.size());
792}
793
Mike Stump1eb44332009-09-09 15:08:12 +0000794APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000795VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000796 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000797 QualType EltTy = VT->getElementType();
798 APValue ZeroElement;
799 if (EltTy->isIntegerType())
800 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
801 else
802 ZeroElement =
803 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
804
805 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
806 return APValue(&Elements[0], Elements.size());
807}
808
809APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
810 bool BoolResult;
811 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
812 return APValue();
813
814 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
815
816 APValue Result;
817 if (EvaluateVector(EvalExpr, Result, Info))
818 return Result;
819 return APValue();
820}
821
Eli Friedman91110ee2009-02-23 04:23:56 +0000822APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
823 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
824 Info.EvalResult.HasSideEffects = true;
825 return GetZeroVector(E->getType());
826}
827
Nate Begeman59b5da62009-01-18 03:20:47 +0000828//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000829// Integer Evaluation
830//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000831
832namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000833class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000834 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000835 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000836 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000837public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000838 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000839 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000840
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000841 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000842 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000843 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000844 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000845 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000846 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000847 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000848 return true;
849 }
850
Daniel Dunbar131eb432009-02-19 09:06:44 +0000851 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000852 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000853 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000854 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000855 Result = APValue(APSInt(I));
856 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000857 return true;
858 }
859
860 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000861 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000862 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000863 return true;
864 }
865
Anders Carlsson82206e22008-11-30 18:14:57 +0000866 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000867 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000868 if (Info.EvalResult.Diag == 0) {
869 Info.EvalResult.DiagLoc = L;
870 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000871 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000872 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000873 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Anders Carlssonc754aa62008-07-08 05:13:58 +0000876 //===--------------------------------------------------------------------===//
877 // Visitor Methods
878 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Chris Lattner32fea9d2008-11-12 07:43:42 +0000880 bool VisitStmt(Stmt *) {
881 assert(0 && "This should be called on integers, stmts are not integers");
882 return false;
883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner32fea9d2008-11-12 07:43:42 +0000885 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000886 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattnerb542afe2008-07-11 19:10:17 +0000889 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000890
Chris Lattner4c4867e2008-07-12 00:38:25 +0000891 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000892 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000893 }
894 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000895 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000896 }
897 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000898 // Per gcc docs "this built-in function ignores top level
899 // qualifiers". We need to use the canonical version to properly
900 // be able to strip CRV qualifiers from the type.
901 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
902 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000903 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000904 T1.getUnqualifiedType()),
905 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000906 }
Eli Friedman04309752009-11-24 05:28:59 +0000907
908 bool CheckReferencedDecl(const Expr *E, const Decl *D);
909 bool VisitDeclRefExpr(const DeclRefExpr *E) {
910 return CheckReferencedDecl(E, E->getDecl());
911 }
912 bool VisitMemberExpr(const MemberExpr *E) {
913 if (CheckReferencedDecl(E, E->getMemberDecl())) {
914 // Conservatively assume a MemberExpr will have side-effects
915 Info.EvalResult.HasSideEffects = true;
916 return true;
917 }
918 return false;
919 }
920
Eli Friedmanc4a26382010-02-13 00:10:10 +0000921 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000922 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000923 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000924 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000925 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000926
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000927 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000928 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
929
Anders Carlsson3068d112008-11-16 19:01:22 +0000930 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000931 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Anders Carlsson3f704562008-12-21 22:39:40 +0000934 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000935 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000936 }
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Anders Carlsson3068d112008-11-16 19:01:22 +0000938 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000939 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000940 }
941
Eli Friedman664a1042009-02-27 04:45:43 +0000942 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
943 return Success(0, E);
944 }
945
Sebastian Redl64b45f72009-01-05 20:52:13 +0000946 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000947 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000948 }
949
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000950 bool VisitChooseExpr(const ChooseExpr *E) {
951 return Visit(E->getChosenSubExpr(Info.Ctx));
952 }
953
Eli Friedman722c7172009-02-28 03:59:05 +0000954 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000955 bool VisitUnaryImag(const UnaryOperator *E);
956
Chris Lattnerfcee0012008-07-11 21:24:13 +0000957private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000958 CharUnits GetAlignOfExpr(const Expr *E);
959 CharUnits GetAlignOfType(QualType T);
John McCall42c8f872010-05-10 23:27:23 +0000960 static QualType GetObjectType(const Expr *E);
961 bool TryEvaluateBuiltinObjectSize(CallExpr *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000962 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000963};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000964} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000965
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000966static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000967 assert(E->getType()->isIntegralType());
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000968 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
969}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000970
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000971static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000972 assert(E->getType()->isIntegralType());
973
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000974 APValue Val;
975 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
976 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000977 Result = Val.getInt();
978 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000979}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000980
Eli Friedman04309752009-11-24 05:28:59 +0000981bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000982 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000983 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
984 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000985
986 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000987 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000988 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
989 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000990
991 if (isa<ParmVarDecl>(D))
992 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
993
Eli Friedman04309752009-11-24 05:28:59 +0000994 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000995 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000996 if (APValue *V = VD->getEvaluatedValue()) {
997 if (V->isInt())
998 return Success(V->getInt(), E);
999 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1000 }
1001
1002 if (VD->isEvaluatingValue())
1003 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1004
1005 VD->setEvaluatingValue();
1006
Douglas Gregor78d15832009-05-26 18:54:04 +00001007 if (Visit(const_cast<Expr*>(Init))) {
1008 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +00001009 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +00001010 return true;
1011 }
1012
Eli Friedmanc0131182009-12-03 20:31:57 +00001013 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +00001014 return false;
1015 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001016 }
1017 }
1018
Chris Lattner4c4867e2008-07-12 00:38:25 +00001019 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001020 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001021}
1022
Chris Lattnera4d55d82008-10-06 06:40:35 +00001023/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1024/// as GCC.
1025static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1026 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001027 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +00001028 enum gcc_type_class {
1029 no_type_class = -1,
1030 void_type_class, integer_type_class, char_type_class,
1031 enumeral_type_class, boolean_type_class,
1032 pointer_type_class, reference_type_class, offset_type_class,
1033 real_type_class, complex_type_class,
1034 function_type_class, method_type_class,
1035 record_type_class, union_type_class,
1036 array_type_class, string_type_class,
1037 lang_type_class
1038 };
Mike Stump1eb44332009-09-09 15:08:12 +00001039
1040 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001041 // ideal, however it is what gcc does.
1042 if (E->getNumArgs() == 0)
1043 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattnera4d55d82008-10-06 06:40:35 +00001045 QualType ArgTy = E->getArg(0)->getType();
1046 if (ArgTy->isVoidType())
1047 return void_type_class;
1048 else if (ArgTy->isEnumeralType())
1049 return enumeral_type_class;
1050 else if (ArgTy->isBooleanType())
1051 return boolean_type_class;
1052 else if (ArgTy->isCharType())
1053 return string_type_class; // gcc doesn't appear to use char_type_class
1054 else if (ArgTy->isIntegerType())
1055 return integer_type_class;
1056 else if (ArgTy->isPointerType())
1057 return pointer_type_class;
1058 else if (ArgTy->isReferenceType())
1059 return reference_type_class;
1060 else if (ArgTy->isRealType())
1061 return real_type_class;
1062 else if (ArgTy->isComplexType())
1063 return complex_type_class;
1064 else if (ArgTy->isFunctionType())
1065 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001066 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001067 return record_type_class;
1068 else if (ArgTy->isUnionType())
1069 return union_type_class;
1070 else if (ArgTy->isArrayType())
1071 return array_type_class;
1072 else if (ArgTy->isUnionType())
1073 return union_type_class;
1074 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1075 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1076 return -1;
1077}
1078
John McCall42c8f872010-05-10 23:27:23 +00001079/// Retrieves the "underlying object type" of the given expression,
1080/// as used by __builtin_object_size.
1081QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1082 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1083 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1084 return VD->getType();
1085 } else if (isa<CompoundLiteralExpr>(E)) {
1086 return E->getType();
1087 }
1088
1089 return QualType();
1090}
1091
1092bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
1093 // TODO: Perhaps we should let LLVM lower this?
1094 LValue Base;
1095 if (!EvaluatePointer(E->getArg(0), Base, Info))
1096 return false;
1097
1098 // If we can prove the base is null, lower to zero now.
1099 const Expr *LVBase = Base.getLValueBase();
1100 if (!LVBase) return Success(0, E);
1101
1102 QualType T = GetObjectType(LVBase);
1103 if (T.isNull() ||
1104 T->isIncompleteType() ||
1105 !T->isObjectType() ||
1106 T->isVariablyModifiedType() ||
1107 T->isDependentType())
1108 return false;
1109
1110 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1111 CharUnits Offset = Base.getLValueOffset();
1112
1113 if (!Offset.isNegative() && Offset <= Size)
1114 Size -= Offset;
1115 else
1116 Size = CharUnits::Zero();
1117 return Success(Size.getQuantity(), E);
1118}
1119
Eli Friedmanc4a26382010-02-13 00:10:10 +00001120bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001121 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001122 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001123 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001124
1125 case Builtin::BI__builtin_object_size: {
John McCall42c8f872010-05-10 23:27:23 +00001126 if (TryEvaluateBuiltinObjectSize(E))
1127 return true;
Mike Stump64eda9e2009-10-26 18:35:08 +00001128
Eric Christopherb2aaf512010-01-19 22:58:35 +00001129 // If evaluating the argument has side-effects we can't determine
1130 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001131 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001132 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001133 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001134 return Success(0, E);
1135 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001136
Mike Stump64eda9e2009-10-26 18:35:08 +00001137 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1138 }
1139
Chris Lattner019f4e82008-10-06 05:28:25 +00001140 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001141 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001143 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001144 // __builtin_constant_p always has one operand: it returns true if that
1145 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001146 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001147
1148 case Builtin::BI__builtin_eh_return_data_regno: {
1149 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1150 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1151 return Success(Operand, E);
1152 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001153
1154 case Builtin::BI__builtin_expect:
1155 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001156 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001157}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001158
Chris Lattnerb542afe2008-07-11 19:10:17 +00001159bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001160 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001161 if (!Visit(E->getRHS()))
1162 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001163
Eli Friedman33ef1452009-02-26 10:19:36 +00001164 // If we can't evaluate the LHS, it might have side effects;
1165 // conservatively mark it.
1166 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1167 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001168
Anders Carlsson027f62e2008-12-01 02:07:06 +00001169 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001170 }
1171
1172 if (E->isLogicalOp()) {
1173 // These need to be handled specially because the operands aren't
1174 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001175 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001177 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001178 // We were able to evaluate the LHS, see if we can get away with not
1179 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001180 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001181 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001182
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001183 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001184 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001185 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001186 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001187 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001188 }
1189 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001190 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001191 // We can't evaluate the LHS; however, sometimes the result
1192 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001193 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001194 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001195 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001196 // must have had side effects.
1197 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001198
1199 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001200 }
1201 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001202 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001203
Eli Friedmana6afa762008-11-13 06:09:17 +00001204 return false;
1205 }
1206
Anders Carlsson286f85e2008-11-16 07:17:21 +00001207 QualType LHSTy = E->getLHS()->getType();
1208 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001209
1210 if (LHSTy->isAnyComplexType()) {
1211 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001212 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001213
1214 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1215 return false;
1216
1217 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1218 return false;
1219
1220 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001221 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001222 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001223 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001224 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1225
Daniel Dunbar4087e242009-01-29 06:43:41 +00001226 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001227 return Success((CR_r == APFloat::cmpEqual &&
1228 CR_i == APFloat::cmpEqual), E);
1229 else {
1230 assert(E->getOpcode() == BinaryOperator::NE &&
1231 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001232 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001233 CR_r == APFloat::cmpLessThan ||
1234 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001235 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001236 CR_i == APFloat::cmpLessThan ||
1237 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001238 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001239 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001240 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001241 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1242 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1243 else {
1244 assert(E->getOpcode() == BinaryOperator::NE &&
1245 "Invalid compex comparison.");
1246 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1247 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1248 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001249 }
1250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Anders Carlsson286f85e2008-11-16 07:17:21 +00001252 if (LHSTy->isRealFloatingType() &&
1253 RHSTy->isRealFloatingType()) {
1254 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Anders Carlsson286f85e2008-11-16 07:17:21 +00001256 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1257 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Anders Carlsson286f85e2008-11-16 07:17:21 +00001259 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1260 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Anders Carlsson286f85e2008-11-16 07:17:21 +00001262 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001263
Anders Carlsson286f85e2008-11-16 07:17:21 +00001264 switch (E->getOpcode()) {
1265 default:
1266 assert(0 && "Invalid binary operator!");
1267 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001268 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001269 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001270 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001271 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001272 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001273 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001274 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001275 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001276 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001277 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001278 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001279 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001280 || CR == APFloat::cmpLessThan
1281 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001282 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001285 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1286 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001287 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001288 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1289 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001290
John McCallefdb83e2010-05-07 21:00:08 +00001291 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001292 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1293 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001294
Eli Friedman5bc86102009-06-14 02:17:33 +00001295 // Reject any bases from the normal codepath; we special-case comparisons
1296 // to null.
1297 if (LHSValue.getLValueBase()) {
1298 if (!E->isEqualityOp())
1299 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001300 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001301 return false;
1302 bool bres;
1303 if (!EvalPointerValueAsBool(LHSValue, bres))
1304 return false;
1305 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1306 } else if (RHSValue.getLValueBase()) {
1307 if (!E->isEqualityOp())
1308 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001309 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001310 return false;
1311 bool bres;
1312 if (!EvalPointerValueAsBool(RHSValue, bres))
1313 return false;
1314 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1315 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001316
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001317 if (E->getOpcode() == BinaryOperator::Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001318 QualType Type = E->getLHS()->getType();
1319 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001320
Ken Dycka7305832010-01-15 12:37:54 +00001321 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001322 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001323 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001324
Ken Dycka7305832010-01-15 12:37:54 +00001325 CharUnits Diff = LHSValue.getLValueOffset() -
1326 RHSValue.getLValueOffset();
1327 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001328 }
1329 bool Result;
1330 if (E->getOpcode() == BinaryOperator::EQ) {
1331 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001332 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001333 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1334 }
1335 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001336 }
1337 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001338 if (!LHSTy->isIntegralType() ||
1339 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001340 // We can't continue from here for non-integral types, and they
1341 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001342 return false;
1343 }
1344
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001345 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001346 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001347 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001348
Eli Friedman42edd0d2009-03-24 01:14:50 +00001349 APValue RHSVal;
1350 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001351 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001352
1353 // Handle cases like (unsigned long)&a + 4.
1354 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001355 CharUnits Offset = Result.getLValueOffset();
1356 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1357 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001358 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001359 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001360 else
Ken Dycka7305832010-01-15 12:37:54 +00001361 Offset -= AdditionalOffset;
1362 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001363 return true;
1364 }
1365
1366 // Handle cases like 4 + (unsigned long)&a
1367 if (E->getOpcode() == BinaryOperator::Add &&
1368 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001369 CharUnits Offset = RHSVal.getLValueOffset();
1370 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1371 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001372 return true;
1373 }
1374
1375 // All the following cases expect both operands to be an integer
1376 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001377 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001378
Eli Friedman42edd0d2009-03-24 01:14:50 +00001379 APSInt& RHS = RHSVal.getInt();
1380
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001381 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001382 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001383 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001384 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1385 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1386 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1387 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1388 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1389 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001390 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001391 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001392 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001393 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001394 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001395 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001396 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001397 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001398 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001399 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001400 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001401 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1402 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001403 }
1404 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001405 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001406 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1407 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001410 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1411 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1412 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1413 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1414 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1415 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001416 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001417}
1418
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001419bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001420 bool Cond;
1421 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001422 return false;
1423
Nuno Lopesa25bd552008-11-16 22:06:39 +00001424 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001425}
1426
Ken Dyck8b752f12010-01-27 17:10:57 +00001427CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001428 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1429 // the result is the size of the referenced type."
1430 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1431 // result shall be the alignment of the referenced type."
1432 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1433 T = Ref->getPointeeType();
1434
Chris Lattnere9feb472009-01-24 21:09:06 +00001435 // Get information about the alignment.
1436 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001437
Eli Friedman2be58612009-05-30 21:09:44 +00001438 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001439 return CharUnits::fromQuantity(
1440 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001441}
1442
Ken Dyck8b752f12010-01-27 17:10:57 +00001443CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001444 E = E->IgnoreParens();
1445
1446 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001447 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001448 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001449 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1450 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001451
Chris Lattneraf707ab2009-01-24 21:53:27 +00001452 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001453 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1454 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001455
Chris Lattnere9feb472009-01-24 21:09:06 +00001456 return GetAlignOfType(E->getType());
1457}
1458
1459
Sebastian Redl05189992008-11-11 17:56:53 +00001460/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1461/// expression's type.
1462bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001463 // Handle alignof separately.
1464 if (!E->isSizeOf()) {
1465 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001466 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001467 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001468 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001469 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001470
Sebastian Redl05189992008-11-11 17:56:53 +00001471 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001472 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1473 // the result is the size of the referenced type."
1474 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1475 // result shall be the alignment of the referenced type."
1476 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1477 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001478
Daniel Dunbar131eb432009-02-19 09:06:44 +00001479 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1480 // extension.
1481 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1482 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001483
Chris Lattnerfcee0012008-07-11 21:24:13 +00001484 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001485 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001486 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001487
Chris Lattnere9feb472009-01-24 21:09:06 +00001488 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001489 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001490}
1491
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001492bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1493 CharUnits Result;
1494 unsigned n = E->getNumComponents();
1495 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1496 if (n == 0)
1497 return false;
1498 QualType CurrentType = E->getTypeSourceInfo()->getType();
1499 for (unsigned i = 0; i != n; ++i) {
1500 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1501 switch (ON.getKind()) {
1502 case OffsetOfExpr::OffsetOfNode::Array: {
1503 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1504 APSInt IdxResult;
1505 if (!EvaluateInteger(Idx, IdxResult, Info))
1506 return false;
1507 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1508 if (!AT)
1509 return false;
1510 CurrentType = AT->getElementType();
1511 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1512 Result += IdxResult.getSExtValue() * ElementSize;
1513 break;
1514 }
1515
1516 case OffsetOfExpr::OffsetOfNode::Field: {
1517 FieldDecl *MemberDecl = ON.getField();
1518 const RecordType *RT = CurrentType->getAs<RecordType>();
1519 if (!RT)
1520 return false;
1521 RecordDecl *RD = RT->getDecl();
1522 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1523 unsigned i = 0;
1524 // FIXME: It would be nice if we didn't have to loop here!
1525 for (RecordDecl::field_iterator Field = RD->field_begin(),
1526 FieldEnd = RD->field_end();
1527 Field != FieldEnd; (void)++Field, ++i) {
1528 if (*Field == MemberDecl)
1529 break;
1530 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001531 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1532 Result += CharUnits::fromQuantity(
1533 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001534 CurrentType = MemberDecl->getType().getNonReferenceType();
1535 break;
1536 }
1537
1538 case OffsetOfExpr::OffsetOfNode::Identifier:
1539 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001540 return false;
1541
1542 case OffsetOfExpr::OffsetOfNode::Base: {
1543 CXXBaseSpecifier *BaseSpec = ON.getBase();
1544 if (BaseSpec->isVirtual())
1545 return false;
1546
1547 // Find the layout of the class whose base we are looking into.
1548 const RecordType *RT = CurrentType->getAs<RecordType>();
1549 if (!RT)
1550 return false;
1551 RecordDecl *RD = RT->getDecl();
1552 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1553
1554 // Find the base class itself.
1555 CurrentType = BaseSpec->getType();
1556 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1557 if (!BaseRT)
1558 return false;
1559
1560 // Add the offset to the base.
1561 Result += CharUnits::fromQuantity(
1562 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1563 / Info.Ctx.getCharWidth());
1564 break;
1565 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001566 }
1567 }
1568 return Success(Result.getQuantity(), E);
1569}
1570
Chris Lattnerb542afe2008-07-11 19:10:17 +00001571bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001572 // Special case unary operators that do not need their subexpression
1573 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001574 if (E->isOffsetOfOp()) {
1575 // The AST for offsetof is defined in such a way that we can just
1576 // directly Evaluate it as an l-value.
John McCallefdb83e2010-05-07 21:00:08 +00001577 LValue LV;
Eli Friedman35183ac2009-02-27 06:44:11 +00001578 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001579 return false;
Eli Friedman35183ac2009-02-27 06:44:11 +00001580 if (LV.getLValueBase())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001581 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001582 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001583 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001584
Eli Friedmana6afa762008-11-13 06:09:17 +00001585 if (E->getOpcode() == UnaryOperator::LNot) {
1586 // LNot's operand isn't necessarily an integer, so we handle it specially.
1587 bool bres;
1588 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1589 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001590 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001591 }
1592
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001593 // Only handle integral operations...
1594 if (!E->getSubExpr()->getType()->isIntegralType())
1595 return false;
1596
Chris Lattner87eae5e2008-07-11 22:52:41 +00001597 // Get the operand value into 'Result'.
1598 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001599 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001600
Chris Lattner75a48812008-07-11 22:15:16 +00001601 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001602 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001603 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1604 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001605 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001606 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001607 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1608 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001609 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001610 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001611 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001612 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001613 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001614 if (!Result.isInt()) return false;
1615 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001616 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001617 if (!Result.isInt()) return false;
1618 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001619 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001620}
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Chris Lattner732b2232008-07-12 01:15:53 +00001622/// HandleCast - This is used to evaluate implicit or explicit casts where the
1623/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001624bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001625 Expr *SubExpr = E->getSubExpr();
1626 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001627 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001628
Eli Friedman4efaa272008-11-12 09:44:48 +00001629 if (DestType->isBooleanType()) {
1630 bool BoolResult;
1631 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1632 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001633 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001634 }
1635
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001636 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001637 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001638 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001639 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001640
Eli Friedmanbe265702009-02-20 01:15:07 +00001641 if (!Result.isInt()) {
1642 // Only allow casts of lvalues if they are lossless.
1643 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1644 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001645
Daniel Dunbardd211642009-02-19 22:24:01 +00001646 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001647 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001648 }
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Chris Lattner732b2232008-07-12 01:15:53 +00001650 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001651 if (SrcType->isPointerType()) {
John McCallefdb83e2010-05-07 21:00:08 +00001652 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001653 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001654 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001655
Daniel Dunbardd211642009-02-19 22:24:01 +00001656 if (LV.getLValueBase()) {
1657 // Only allow based lvalue casts if they are lossless.
1658 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1659 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001660
John McCallefdb83e2010-05-07 21:00:08 +00001661 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001662 return true;
1663 }
1664
Ken Dycka7305832010-01-15 12:37:54 +00001665 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1666 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001667 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001668 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001669
Eli Friedmanbe265702009-02-20 01:15:07 +00001670 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1671 // This handles double-conversion cases, where there's both
1672 // an l-value promotion and an implicit conversion to int.
John McCallefdb83e2010-05-07 21:00:08 +00001673 LValue LV;
Eli Friedmanbe265702009-02-20 01:15:07 +00001674 if (!EvaluateLValue(SubExpr, LV, Info))
1675 return false;
1676
1677 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1678 return false;
1679
John McCallefdb83e2010-05-07 21:00:08 +00001680 LV.moveInto(Result);
Eli Friedmanbe265702009-02-20 01:15:07 +00001681 return true;
1682 }
1683
Eli Friedman1725f682009-04-22 19:23:09 +00001684 if (SrcType->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001685 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001686 if (!EvaluateComplex(SubExpr, C, Info))
1687 return false;
1688 if (C.isComplexFloat())
1689 return Success(HandleFloatToIntCast(DestType, SrcType,
1690 C.getComplexFloatReal(), Info.Ctx),
1691 E);
1692 else
1693 return Success(HandleIntToIntCast(DestType, SrcType,
1694 C.getComplexIntReal(), Info.Ctx), E);
1695 }
Eli Friedman2217c872009-02-22 11:46:18 +00001696 // FIXME: Handle vectors
1697
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001698 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001699 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001700
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001701 APFloat F(0.0);
1702 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001703 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001705 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001706}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001707
Eli Friedman722c7172009-02-28 03:59:05 +00001708bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1709 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001710 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001711 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1712 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1713 return Success(LV.getComplexIntReal(), E);
1714 }
1715
1716 return Visit(E->getSubExpr());
1717}
1718
Eli Friedman664a1042009-02-27 04:45:43 +00001719bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001720 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001721 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001722 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1723 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1724 return Success(LV.getComplexIntImag(), E);
1725 }
1726
Eli Friedman664a1042009-02-27 04:45:43 +00001727 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1728 Info.EvalResult.HasSideEffects = true;
1729 return Success(0, E);
1730}
1731
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001732//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001733// Float Evaluation
1734//===----------------------------------------------------------------------===//
1735
1736namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001737class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001738 : public StmtVisitor<FloatExprEvaluator, bool> {
1739 EvalInfo &Info;
1740 APFloat &Result;
1741public:
1742 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1743 : Info(info), Result(result) {}
1744
1745 bool VisitStmt(Stmt *S) {
1746 return false;
1747 }
1748
1749 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001750 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001751
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001752 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001753 bool VisitBinaryOperator(const BinaryOperator *E);
1754 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001755 bool VisitCastExpr(CastExpr *E);
1756 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001757 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001758
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001759 bool VisitChooseExpr(const ChooseExpr *E)
1760 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1761 bool VisitUnaryExtension(const UnaryOperator *E)
1762 { return Visit(E->getSubExpr()); }
John McCallabd3a852010-05-07 22:08:54 +00001763 bool VisitUnaryReal(const UnaryOperator *E);
1764 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001765
John McCallabd3a852010-05-07 22:08:54 +00001766 // FIXME: Missing: array subscript of vector, member of vector,
1767 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001768};
1769} // end anonymous namespace
1770
1771static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001772 assert(E->getType()->isRealFloatingType());
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001773 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1774}
1775
John McCalldb7b72a2010-02-28 13:00:19 +00001776static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1777 QualType ResultTy,
1778 const Expr *Arg,
1779 bool SNaN,
1780 llvm::APFloat &Result) {
1781 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1782 if (!S) return false;
1783
1784 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1785
1786 llvm::APInt fill;
1787
1788 // Treat empty strings as if they were zero.
1789 if (S->getString().empty())
1790 fill = llvm::APInt(32, 0);
1791 else if (S->getString().getAsInteger(0, fill))
1792 return false;
1793
1794 if (SNaN)
1795 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1796 else
1797 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1798 return true;
1799}
1800
Chris Lattner019f4e82008-10-06 05:28:25 +00001801bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001802 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001803 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001804 case Builtin::BI__builtin_huge_val:
1805 case Builtin::BI__builtin_huge_valf:
1806 case Builtin::BI__builtin_huge_vall:
1807 case Builtin::BI__builtin_inf:
1808 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001809 case Builtin::BI__builtin_infl: {
1810 const llvm::fltSemantics &Sem =
1811 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001812 Result = llvm::APFloat::getInf(Sem);
1813 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
John McCalldb7b72a2010-02-28 13:00:19 +00001816 case Builtin::BI__builtin_nans:
1817 case Builtin::BI__builtin_nansf:
1818 case Builtin::BI__builtin_nansl:
1819 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1820 true, Result);
1821
Chris Lattner9e621712008-10-06 06:31:58 +00001822 case Builtin::BI__builtin_nan:
1823 case Builtin::BI__builtin_nanf:
1824 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001825 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001826 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001827 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1828 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001829
1830 case Builtin::BI__builtin_fabs:
1831 case Builtin::BI__builtin_fabsf:
1832 case Builtin::BI__builtin_fabsl:
1833 if (!EvaluateFloat(E->getArg(0), Result, Info))
1834 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001836 if (Result.isNegative())
1837 Result.changeSign();
1838 return true;
1839
Mike Stump1eb44332009-09-09 15:08:12 +00001840 case Builtin::BI__builtin_copysign:
1841 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001842 case Builtin::BI__builtin_copysignl: {
1843 APFloat RHS(0.);
1844 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1845 !EvaluateFloat(E->getArg(1), RHS, Info))
1846 return false;
1847 Result.copySign(RHS);
1848 return true;
1849 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001850 }
1851}
1852
John McCallabd3a852010-05-07 22:08:54 +00001853bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1854 ComplexValue CV;
1855 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1856 return false;
1857 Result = CV.FloatReal;
1858 return true;
1859}
1860
1861bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
1862 ComplexValue CV;
1863 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1864 return false;
1865 Result = CV.FloatImag;
1866 return true;
1867}
1868
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001869bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001870 if (E->getOpcode() == UnaryOperator::Deref)
1871 return false;
1872
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001873 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1874 return false;
1875
1876 switch (E->getOpcode()) {
1877 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001878 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001879 return true;
1880 case UnaryOperator::Minus:
1881 Result.changeSign();
1882 return true;
1883 }
1884}
Chris Lattner019f4e82008-10-06 05:28:25 +00001885
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001886bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001887 if (E->getOpcode() == BinaryOperator::Comma) {
1888 if (!EvaluateFloat(E->getRHS(), Result, Info))
1889 return false;
1890
1891 // If we can't evaluate the LHS, it might have side effects;
1892 // conservatively mark it.
1893 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1894 Info.EvalResult.HasSideEffects = true;
1895
1896 return true;
1897 }
1898
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001899 // FIXME: Diagnostics? I really don't understand how the warnings
1900 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001901 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001902 if (!EvaluateFloat(E->getLHS(), Result, Info))
1903 return false;
1904 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1905 return false;
1906
1907 switch (E->getOpcode()) {
1908 default: return false;
1909 case BinaryOperator::Mul:
1910 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1911 return true;
1912 case BinaryOperator::Add:
1913 Result.add(RHS, APFloat::rmNearestTiesToEven);
1914 return true;
1915 case BinaryOperator::Sub:
1916 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1917 return true;
1918 case BinaryOperator::Div:
1919 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1920 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001921 }
1922}
1923
1924bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1925 Result = E->getValue();
1926 return true;
1927}
1928
Eli Friedman4efaa272008-11-12 09:44:48 +00001929bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1930 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Eli Friedman4efaa272008-11-12 09:44:48 +00001932 if (SubExpr->getType()->isIntegralType()) {
1933 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001934 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001935 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001936 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001937 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001938 return true;
1939 }
1940 if (SubExpr->getType()->isRealFloatingType()) {
1941 if (!Visit(SubExpr))
1942 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001943 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1944 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001945 return true;
1946 }
Eli Friedman2217c872009-02-22 11:46:18 +00001947 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001948
1949 return false;
1950}
1951
1952bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1953 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1954 return true;
1955}
1956
Eli Friedman67f85fc2009-12-04 02:12:53 +00001957bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1958 bool Cond;
1959 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1960 return false;
1961
1962 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1963}
1964
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001965//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001966// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001967//===----------------------------------------------------------------------===//
1968
1969namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001970class ComplexExprEvaluator
John McCallf4cf1a12010-05-07 17:22:02 +00001971 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001972 EvalInfo &Info;
John McCallf4cf1a12010-05-07 17:22:02 +00001973 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001975public:
John McCallf4cf1a12010-05-07 17:22:02 +00001976 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
1977 : Info(info), Result(Result) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001979 //===--------------------------------------------------------------------===//
1980 // Visitor Methods
1981 //===--------------------------------------------------------------------===//
1982
John McCallf4cf1a12010-05-07 17:22:02 +00001983 bool VisitStmt(Stmt *S) {
1984 return false;
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
John McCallf4cf1a12010-05-07 17:22:02 +00001987 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001988
John McCallf4cf1a12010-05-07 17:22:02 +00001989 bool VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001990 Expr* SubExpr = E->getSubExpr();
1991
1992 if (SubExpr->getType()->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001993 Result.makeComplexFloat();
1994 APFloat &Imag = Result.FloatImag;
1995 if (!EvaluateFloat(SubExpr, Imag, Info))
1996 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001997
John McCallf4cf1a12010-05-07 17:22:02 +00001998 Result.FloatReal = APFloat(Imag.getSemantics());
1999 return true;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002000 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002001 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002002 "Unexpected imaginary literal.");
2003
John McCallf4cf1a12010-05-07 17:22:02 +00002004 Result.makeComplexInt();
2005 APSInt &Imag = Result.IntImag;
2006 if (!EvaluateInteger(SubExpr, Imag, Info))
2007 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
John McCallf4cf1a12010-05-07 17:22:02 +00002009 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2010 return true;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002011 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002012 }
2013
John McCallf4cf1a12010-05-07 17:22:02 +00002014 bool VisitCastExpr(CastExpr *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002015 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00002016 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002017 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002018
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002019 if (SubType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002020 APFloat &Real = Result.FloatReal;
2021 if (!EvaluateFloat(SubExpr, Real, Info))
2022 return false;
Eli Friedman1725f682009-04-22 19:23:09 +00002023
2024 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002025 Result.makeComplexFloat();
2026 Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx);
2027 Result.FloatImag = APFloat(Real.getSemantics());
2028 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00002029 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002030 Result.makeComplexInt();
2031 Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx);
2032 Result.IntImag = APSInt(Result.IntReal.getBitWidth(),
2033 !Result.IntReal.isSigned());
2034 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00002035 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002036 } else if (SubType->isIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002037 APSInt &Real = Result.IntReal;
2038 if (!EvaluateInteger(SubExpr, Real, Info))
2039 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002040
Eli Friedman1725f682009-04-22 19:23:09 +00002041 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002042 Result.makeComplexFloat();
2043 Result.FloatReal
2044 = HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx);
2045 Result.FloatImag = APFloat(Result.FloatReal.getSemantics());
2046 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00002047 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002048 Result.makeComplexInt();
2049 Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx);
2050 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2051 return true;
Eli Friedman1725f682009-04-22 19:23:09 +00002052 }
John McCall183700f2009-09-21 23:43:11 +00002053 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002054 if (!Visit(SubExpr))
2055 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002056
2057 QualType SrcType = CT->getElementType();
2058
John McCallf4cf1a12010-05-07 17:22:02 +00002059 if (Result.isComplexFloat()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002060 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002061 Result.makeComplexFloat();
2062 Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType,
2063 Result.FloatReal,
2064 Info.Ctx);
2065 Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType,
2066 Result.FloatImag,
2067 Info.Ctx);
2068 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002069 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002070 Result.makeComplexInt();
2071 Result.IntReal = HandleFloatToIntCast(EltType, SrcType,
2072 Result.FloatReal,
2073 Info.Ctx);
2074 Result.IntImag = HandleFloatToIntCast(EltType, SrcType,
2075 Result.FloatImag,
2076 Info.Ctx);
2077 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002078 }
2079 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002080 assert(Result.isComplexInt() && "Invalid evaluate result.");
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002081 if (EltType->isRealFloatingType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002082 Result.makeComplexFloat();
2083 Result.FloatReal = HandleIntToFloatCast(EltType, SrcType,
2084 Result.IntReal,
2085 Info.Ctx);
2086 Result.FloatImag = HandleIntToFloatCast(EltType, SrcType,
2087 Result.IntImag,
2088 Info.Ctx);
2089 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002090 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002091 Result.makeComplexInt();
2092 Result.IntReal = HandleIntToIntCast(EltType, SrcType,
2093 Result.IntReal,
2094 Info.Ctx);
2095 Result.IntImag = HandleIntToIntCast(EltType, SrcType,
2096 Result.IntImag,
2097 Info.Ctx);
2098 return true;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002099 }
2100 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002101 }
2102
2103 // FIXME: Handle more casts.
John McCallf4cf1a12010-05-07 17:22:02 +00002104 return false;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002105 }
Mike Stump1eb44332009-09-09 15:08:12 +00002106
John McCallf4cf1a12010-05-07 17:22:02 +00002107 bool VisitBinaryOperator(const BinaryOperator *E);
2108 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002109 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCallf4cf1a12010-05-07 17:22:02 +00002110 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanba98d6b2009-03-23 04:56:01 +00002111 { return Visit(E->getSubExpr()); }
2112 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00002113 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002114};
2115} // end anonymous namespace
2116
John McCallf4cf1a12010-05-07 17:22:02 +00002117static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2118 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002119 assert(E->getType()->isAnyComplexType());
John McCallf4cf1a12010-05-07 17:22:02 +00002120 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002121}
2122
John McCallf4cf1a12010-05-07 17:22:02 +00002123bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
2124 if (!Visit(E->getLHS()))
2125 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002126
John McCallf4cf1a12010-05-07 17:22:02 +00002127 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002128 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002129 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002130
Daniel Dunbar3f279872009-01-29 01:32:56 +00002131 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2132 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002133 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002134 default: return false;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002135 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002136 if (Result.isComplexFloat()) {
2137 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2138 APFloat::rmNearestTiesToEven);
2139 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2140 APFloat::rmNearestTiesToEven);
2141 } else {
2142 Result.getComplexIntReal() += RHS.getComplexIntReal();
2143 Result.getComplexIntImag() += RHS.getComplexIntImag();
2144 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002145 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002146 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002147 if (Result.isComplexFloat()) {
2148 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2149 APFloat::rmNearestTiesToEven);
2150 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2151 APFloat::rmNearestTiesToEven);
2152 } else {
2153 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2154 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2155 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002156 break;
2157 case BinaryOperator::Mul:
2158 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002159 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002160 APFloat &LHS_r = LHS.getComplexFloatReal();
2161 APFloat &LHS_i = LHS.getComplexFloatImag();
2162 APFloat &RHS_r = RHS.getComplexFloatReal();
2163 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Daniel Dunbar3f279872009-01-29 01:32:56 +00002165 APFloat Tmp = LHS_r;
2166 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2167 Result.getComplexFloatReal() = Tmp;
2168 Tmp = LHS_i;
2169 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2170 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2171
2172 Tmp = LHS_r;
2173 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2174 Result.getComplexFloatImag() = Tmp;
2175 Tmp = LHS_i;
2176 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2177 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2178 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002179 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002180 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002181 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2182 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002183 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002184 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2185 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2186 }
2187 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002188 }
2189
John McCallf4cf1a12010-05-07 17:22:02 +00002190 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002191}
2192
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002193//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002194// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002195//===----------------------------------------------------------------------===//
2196
John McCall42c8f872010-05-10 23:27:23 +00002197/// Evaluate - Return true if this is a constant which we can fold using
2198/// any crazy technique (that has nothing to do with language standards) that
2199/// we want to. If this function returns true, it returns the folded constant
2200/// in Result.
2201bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2202 const Expr *E = this;
2203 EvalInfo Info(Ctx, Result);
John McCallefdb83e2010-05-07 21:00:08 +00002204 if (E->getType()->isVectorType()) {
2205 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002206 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002207 } else if (E->getType()->isIntegerType()) {
2208 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002209 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002210 } else if (E->getType()->hasPointerRepresentation()) {
2211 LValue LV;
2212 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002213 return false;
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002214 if (!IsGlobalLValue(LV.Base))
John McCall42c8f872010-05-10 23:27:23 +00002215 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002216 LV.moveInto(Info.EvalResult.Val);
2217 } else if (E->getType()->isRealFloatingType()) {
2218 llvm::APFloat F(0.0);
2219 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002220 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002221
John McCallefdb83e2010-05-07 21:00:08 +00002222 Info.EvalResult.Val = APValue(F);
2223 } else if (E->getType()->isAnyComplexType()) {
2224 ComplexValue C;
2225 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002226 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002227 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002228 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002229 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002230
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002231 return true;
2232}
2233
John McCallcd7a4452010-01-05 23:42:56 +00002234bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2235 EvalResult Scratch;
2236 EvalInfo Info(Ctx, Scratch);
2237
2238 return HandleConversionToBool(this, Result, Info);
2239}
2240
Anders Carlsson1b782762009-04-10 04:54:13 +00002241bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2242 EvalInfo Info(Ctx, Result);
2243
John McCallefdb83e2010-05-07 21:00:08 +00002244 LValue LV;
John McCall42c8f872010-05-10 23:27:23 +00002245 if (EvaluateLValue(this, LV, Info) &&
2246 !Result.HasSideEffects &&
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002247 IsGlobalLValue(LV.Base)) {
2248 LV.moveInto(Result.Val);
2249 return true;
2250 }
2251 return false;
2252}
2253
2254bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2255 EvalInfo Info(Ctx, Result);
2256
2257 LValue LV;
2258 if (EvaluateLValue(this, LV, Info)) {
John McCallefdb83e2010-05-07 21:00:08 +00002259 LV.moveInto(Result.Val);
2260 return true;
2261 }
2262 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002263}
2264
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002265/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002266/// folded, but discard the result.
2267bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002268 EvalResult Result;
2269 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002270}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002271
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002272bool Expr::HasSideEffects(ASTContext &Ctx) const {
2273 Expr::EvalResult Result;
2274 EvalInfo Info(Ctx, Result);
2275 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2276}
2277
Anders Carlsson51fe9962008-11-22 21:04:56 +00002278APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002279 EvalResult EvalResult;
2280 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002281 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002282 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002283 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002284
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002285 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002286}
John McCalld905f5a2010-05-07 05:32:02 +00002287
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002288 bool Expr::EvalResult::isGlobalLValue() const {
2289 assert(Val.isLValue());
2290 return IsGlobalLValue(Val.getLValueBase());
2291 }
2292
2293
John McCalld905f5a2010-05-07 05:32:02 +00002294/// isIntegerConstantExpr - this recursive routine will test if an expression is
2295/// an integer constant expression.
2296
2297/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2298/// comma, etc
2299///
2300/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2301/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2302/// cast+dereference.
2303
2304// CheckICE - This function does the fundamental ICE checking: the returned
2305// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2306// Note that to reduce code duplication, this helper does no evaluation
2307// itself; the caller checks whether the expression is evaluatable, and
2308// in the rare cases where CheckICE actually cares about the evaluated
2309// value, it calls into Evalute.
2310//
2311// Meanings of Val:
2312// 0: This expression is an ICE if it can be evaluated by Evaluate.
2313// 1: This expression is not an ICE, but if it isn't evaluated, it's
2314// a legal subexpression for an ICE. This return value is used to handle
2315// the comma operator in C99 mode.
2316// 2: This expression is not an ICE, and is not a legal subexpression for one.
2317
2318struct ICEDiag {
2319 unsigned Val;
2320 SourceLocation Loc;
2321
2322 public:
2323 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2324 ICEDiag() : Val(0) {}
2325};
2326
2327ICEDiag NoDiag() { return ICEDiag(); }
2328
2329static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2330 Expr::EvalResult EVResult;
2331 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2332 !EVResult.Val.isInt()) {
2333 return ICEDiag(2, E->getLocStart());
2334 }
2335 return NoDiag();
2336}
2337
2338static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2339 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
2340 if (!E->getType()->isIntegralType()) {
2341 return ICEDiag(2, E->getLocStart());
2342 }
2343
2344 switch (E->getStmtClass()) {
2345#define STMT(Node, Base) case Expr::Node##Class:
2346#define EXPR(Node, Base)
2347#include "clang/AST/StmtNodes.inc"
2348 case Expr::PredefinedExprClass:
2349 case Expr::FloatingLiteralClass:
2350 case Expr::ImaginaryLiteralClass:
2351 case Expr::StringLiteralClass:
2352 case Expr::ArraySubscriptExprClass:
2353 case Expr::MemberExprClass:
2354 case Expr::CompoundAssignOperatorClass:
2355 case Expr::CompoundLiteralExprClass:
2356 case Expr::ExtVectorElementExprClass:
2357 case Expr::InitListExprClass:
2358 case Expr::DesignatedInitExprClass:
2359 case Expr::ImplicitValueInitExprClass:
2360 case Expr::ParenListExprClass:
2361 case Expr::VAArgExprClass:
2362 case Expr::AddrLabelExprClass:
2363 case Expr::StmtExprClass:
2364 case Expr::CXXMemberCallExprClass:
2365 case Expr::CXXDynamicCastExprClass:
2366 case Expr::CXXTypeidExprClass:
2367 case Expr::CXXNullPtrLiteralExprClass:
2368 case Expr::CXXThisExprClass:
2369 case Expr::CXXThrowExprClass:
2370 case Expr::CXXNewExprClass:
2371 case Expr::CXXDeleteExprClass:
2372 case Expr::CXXPseudoDestructorExprClass:
2373 case Expr::UnresolvedLookupExprClass:
2374 case Expr::DependentScopeDeclRefExprClass:
2375 case Expr::CXXConstructExprClass:
2376 case Expr::CXXBindTemporaryExprClass:
2377 case Expr::CXXBindReferenceExprClass:
2378 case Expr::CXXExprWithTemporariesClass:
2379 case Expr::CXXTemporaryObjectExprClass:
2380 case Expr::CXXUnresolvedConstructExprClass:
2381 case Expr::CXXDependentScopeMemberExprClass:
2382 case Expr::UnresolvedMemberExprClass:
2383 case Expr::ObjCStringLiteralClass:
2384 case Expr::ObjCEncodeExprClass:
2385 case Expr::ObjCMessageExprClass:
2386 case Expr::ObjCSelectorExprClass:
2387 case Expr::ObjCProtocolExprClass:
2388 case Expr::ObjCIvarRefExprClass:
2389 case Expr::ObjCPropertyRefExprClass:
2390 case Expr::ObjCImplicitSetterGetterRefExprClass:
2391 case Expr::ObjCSuperExprClass:
2392 case Expr::ObjCIsaExprClass:
2393 case Expr::ShuffleVectorExprClass:
2394 case Expr::BlockExprClass:
2395 case Expr::BlockDeclRefExprClass:
2396 case Expr::NoStmtClass:
2397 return ICEDiag(2, E->getLocStart());
2398
2399 case Expr::GNUNullExprClass:
2400 // GCC considers the GNU __null value to be an integral constant expression.
2401 return NoDiag();
2402
2403 case Expr::ParenExprClass:
2404 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2405 case Expr::IntegerLiteralClass:
2406 case Expr::CharacterLiteralClass:
2407 case Expr::CXXBoolLiteralExprClass:
2408 case Expr::CXXZeroInitValueExprClass:
2409 case Expr::TypesCompatibleExprClass:
2410 case Expr::UnaryTypeTraitExprClass:
2411 return NoDiag();
2412 case Expr::CallExprClass:
2413 case Expr::CXXOperatorCallExprClass: {
2414 const CallExpr *CE = cast<CallExpr>(E);
2415 if (CE->isBuiltinCall(Ctx))
2416 return CheckEvalInICE(E, Ctx);
2417 return ICEDiag(2, E->getLocStart());
2418 }
2419 case Expr::DeclRefExprClass:
2420 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2421 return NoDiag();
2422 if (Ctx.getLangOptions().CPlusPlus &&
2423 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2424 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2425
2426 // Parameter variables are never constants. Without this check,
2427 // getAnyInitializer() can find a default argument, which leads
2428 // to chaos.
2429 if (isa<ParmVarDecl>(D))
2430 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2431
2432 // C++ 7.1.5.1p2
2433 // A variable of non-volatile const-qualified integral or enumeration
2434 // type initialized by an ICE can be used in ICEs.
2435 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2436 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2437 if (Quals.hasVolatile() || !Quals.hasConst())
2438 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2439
2440 // Look for a declaration of this variable that has an initializer.
2441 const VarDecl *ID = 0;
2442 const Expr *Init = Dcl->getAnyInitializer(ID);
2443 if (Init) {
2444 if (ID->isInitKnownICE()) {
2445 // We have already checked whether this subexpression is an
2446 // integral constant expression.
2447 if (ID->isInitICE())
2448 return NoDiag();
2449 else
2450 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2451 }
2452
2453 // It's an ICE whether or not the definition we found is
2454 // out-of-line. See DR 721 and the discussion in Clang PR
2455 // 6206 for details.
2456
2457 if (Dcl->isCheckingICE()) {
2458 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2459 }
2460
2461 Dcl->setCheckingICE();
2462 ICEDiag Result = CheckICE(Init, Ctx);
2463 // Cache the result of the ICE test.
2464 Dcl->setInitKnownICE(Result.Val == 0);
2465 return Result;
2466 }
2467 }
2468 }
2469 return ICEDiag(2, E->getLocStart());
2470 case Expr::UnaryOperatorClass: {
2471 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2472 switch (Exp->getOpcode()) {
2473 case UnaryOperator::PostInc:
2474 case UnaryOperator::PostDec:
2475 case UnaryOperator::PreInc:
2476 case UnaryOperator::PreDec:
2477 case UnaryOperator::AddrOf:
2478 case UnaryOperator::Deref:
2479 return ICEDiag(2, E->getLocStart());
2480 case UnaryOperator::Extension:
2481 case UnaryOperator::LNot:
2482 case UnaryOperator::Plus:
2483 case UnaryOperator::Minus:
2484 case UnaryOperator::Not:
2485 case UnaryOperator::Real:
2486 case UnaryOperator::Imag:
2487 return CheckICE(Exp->getSubExpr(), Ctx);
2488 case UnaryOperator::OffsetOf:
2489 break;
2490 }
2491
2492 // OffsetOf falls through here.
2493 }
2494 case Expr::OffsetOfExprClass: {
2495 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2496 // Evaluate matches the proposed gcc behavior for cases like
2497 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2498 // compliance: we should warn earlier for offsetof expressions with
2499 // array subscripts that aren't ICEs, and if the array subscripts
2500 // are ICEs, the value of the offsetof must be an integer constant.
2501 return CheckEvalInICE(E, Ctx);
2502 }
2503 case Expr::SizeOfAlignOfExprClass: {
2504 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
2505 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
2506 return ICEDiag(2, E->getLocStart());
2507 return NoDiag();
2508 }
2509 case Expr::BinaryOperatorClass: {
2510 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2511 switch (Exp->getOpcode()) {
2512 case BinaryOperator::PtrMemD:
2513 case BinaryOperator::PtrMemI:
2514 case BinaryOperator::Assign:
2515 case BinaryOperator::MulAssign:
2516 case BinaryOperator::DivAssign:
2517 case BinaryOperator::RemAssign:
2518 case BinaryOperator::AddAssign:
2519 case BinaryOperator::SubAssign:
2520 case BinaryOperator::ShlAssign:
2521 case BinaryOperator::ShrAssign:
2522 case BinaryOperator::AndAssign:
2523 case BinaryOperator::XorAssign:
2524 case BinaryOperator::OrAssign:
2525 return ICEDiag(2, E->getLocStart());
2526
2527 case BinaryOperator::Mul:
2528 case BinaryOperator::Div:
2529 case BinaryOperator::Rem:
2530 case BinaryOperator::Add:
2531 case BinaryOperator::Sub:
2532 case BinaryOperator::Shl:
2533 case BinaryOperator::Shr:
2534 case BinaryOperator::LT:
2535 case BinaryOperator::GT:
2536 case BinaryOperator::LE:
2537 case BinaryOperator::GE:
2538 case BinaryOperator::EQ:
2539 case BinaryOperator::NE:
2540 case BinaryOperator::And:
2541 case BinaryOperator::Xor:
2542 case BinaryOperator::Or:
2543 case BinaryOperator::Comma: {
2544 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2545 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2546 if (Exp->getOpcode() == BinaryOperator::Div ||
2547 Exp->getOpcode() == BinaryOperator::Rem) {
2548 // Evaluate gives an error for undefined Div/Rem, so make sure
2549 // we don't evaluate one.
2550 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
2551 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2552 if (REval == 0)
2553 return ICEDiag(1, E->getLocStart());
2554 if (REval.isSigned() && REval.isAllOnesValue()) {
2555 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2556 if (LEval.isMinSignedValue())
2557 return ICEDiag(1, E->getLocStart());
2558 }
2559 }
2560 }
2561 if (Exp->getOpcode() == BinaryOperator::Comma) {
2562 if (Ctx.getLangOptions().C99) {
2563 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2564 // if it isn't evaluated.
2565 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2566 return ICEDiag(1, E->getLocStart());
2567 } else {
2568 // In both C89 and C++, commas in ICEs are illegal.
2569 return ICEDiag(2, E->getLocStart());
2570 }
2571 }
2572 if (LHSResult.Val >= RHSResult.Val)
2573 return LHSResult;
2574 return RHSResult;
2575 }
2576 case BinaryOperator::LAnd:
2577 case BinaryOperator::LOr: {
2578 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2579 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2580 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2581 // Rare case where the RHS has a comma "side-effect"; we need
2582 // to actually check the condition to see whether the side
2583 // with the comma is evaluated.
2584 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
2585 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2586 return RHSResult;
2587 return NoDiag();
2588 }
2589
2590 if (LHSResult.Val >= RHSResult.Val)
2591 return LHSResult;
2592 return RHSResult;
2593 }
2594 }
2595 }
2596 case Expr::ImplicitCastExprClass:
2597 case Expr::CStyleCastExprClass:
2598 case Expr::CXXFunctionalCastExprClass:
2599 case Expr::CXXStaticCastExprClass:
2600 case Expr::CXXReinterpretCastExprClass:
2601 case Expr::CXXConstCastExprClass: {
2602 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2603 if (SubExpr->getType()->isIntegralType())
2604 return CheckICE(SubExpr, Ctx);
2605 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2606 return NoDiag();
2607 return ICEDiag(2, E->getLocStart());
2608 }
2609 case Expr::ConditionalOperatorClass: {
2610 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
2611 // If the condition (ignoring parens) is a __builtin_constant_p call,
2612 // then only the true side is actually considered in an integer constant
2613 // expression, and it is fully evaluated. This is an important GNU
2614 // extension. See GCC PR38377 for discussion.
2615 if (const CallExpr *CallCE
2616 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
2617 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
2618 Expr::EvalResult EVResult;
2619 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2620 !EVResult.Val.isInt()) {
2621 return ICEDiag(2, E->getLocStart());
2622 }
2623 return NoDiag();
2624 }
2625 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2626 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2627 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2628 if (CondResult.Val == 2)
2629 return CondResult;
2630 if (TrueResult.Val == 2)
2631 return TrueResult;
2632 if (FalseResult.Val == 2)
2633 return FalseResult;
2634 if (CondResult.Val == 1)
2635 return CondResult;
2636 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2637 return NoDiag();
2638 // Rare case where the diagnostics depend on which side is evaluated
2639 // Note that if we get here, CondResult is 0, and at least one of
2640 // TrueResult and FalseResult is non-zero.
2641 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
2642 return FalseResult;
2643 }
2644 return TrueResult;
2645 }
2646 case Expr::CXXDefaultArgExprClass:
2647 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
2648 case Expr::ChooseExprClass: {
2649 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
2650 }
2651 }
2652
2653 // Silence a GCC warning
2654 return ICEDiag(2, E->getLocStart());
2655}
2656
2657bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2658 SourceLocation *Loc, bool isEvaluated) const {
2659 ICEDiag d = CheckICE(this, Ctx);
2660 if (d.Val != 0) {
2661 if (Loc) *Loc = d.Loc;
2662 return false;
2663 }
2664 EvalResult EvalResult;
2665 if (!Evaluate(EvalResult, Ctx))
2666 llvm_unreachable("ICE cannot be evaluated!");
2667 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2668 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
2669 Result = EvalResult.Val.getInt();
2670 return true;
2671}