blob: 7347f5a43e17b76863f8c6cb86f2c322c6c47be8 [file] [log] [blame]
Chris Lattnere13042c2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlsson7a241ba2008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck40775002010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump2346cd22009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlsson7a241ba2008-07-03 04:20:39 +000027using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000030
Chris Lattnercdf34e72008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
45struct EvalInfo {
46 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000047
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000048 /// EvalResult - Contains information about the evaluation.
49 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000050
John McCall95007602010-05-10 23:27:23 +000051 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult)
52 : Ctx(ctx), EvalResult(evalresult) {}
Chris Lattnercdf34e72008-07-11 22:52:41 +000053};
54
John McCall93d91dc2010-05-07 17:22:02 +000055namespace {
56 struct ComplexValue {
57 private:
58 bool IsInt;
59
60 public:
61 APSInt IntReal, IntImag;
62 APFloat FloatReal, FloatImag;
63
64 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
65
66 void makeComplexFloat() { IsInt = false; }
67 bool isComplexFloat() const { return !IsInt; }
68 APFloat &getComplexFloatReal() { return FloatReal; }
69 APFloat &getComplexFloatImag() { return FloatImag; }
70
71 void makeComplexInt() { IsInt = true; }
72 bool isComplexInt() const { return IsInt; }
73 APSInt &getComplexIntReal() { return IntReal; }
74 APSInt &getComplexIntImag() { return IntImag; }
75
76 void moveInto(APValue &v) {
77 if (isComplexFloat())
78 v = APValue(FloatReal, FloatImag);
79 else
80 v = APValue(IntReal, IntImag);
81 }
82 };
John McCall45d55e42010-05-07 21:00:08 +000083
84 struct LValue {
85 Expr *Base;
86 CharUnits Offset;
87
88 Expr *getLValueBase() { return Base; }
89 CharUnits getLValueOffset() { return Offset; }
90
91 void moveInto(APValue &v) {
92 v = APValue(Base, Offset);
93 }
94 };
John McCall93d91dc2010-05-07 17:22:02 +000095}
Chris Lattnercdf34e72008-07-11 22:52:41 +000096
John McCall45d55e42010-05-07 21:00:08 +000097static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
98static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-07-11 22:52:41 +000099static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattner6c4d2552009-10-28 23:59:40 +0000100static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
101 EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +0000102static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCall93d91dc2010-05-07 17:22:02 +0000103static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +0000104
105//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +0000106// Misc utilities
107//===----------------------------------------------------------------------===//
108
Abramo Bagnaraf8199452010-05-14 17:07:14 +0000109static bool IsGlobalLValue(const Expr* E) {
John McCall95007602010-05-10 23:27:23 +0000110 if (!E) return true;
111
112 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
113 if (isa<FunctionDecl>(DRE->getDecl()))
114 return true;
115 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
116 return VD->hasGlobalStorage();
117 return false;
118 }
119
120 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
121 return CLE->isFileScope();
122
123 return true;
124}
125
John McCall45d55e42010-05-07 21:00:08 +0000126static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
127 const Expr* Base = Value.Base;
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000128
John McCalleb3e4f32010-05-07 21:34:32 +0000129 // A null base expression indicates a null pointer. These are always
130 // evaluatable, and they are false unless the offset is zero.
131 if (!Base) {
132 Result = !Value.Offset.isZero();
133 return true;
134 }
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000135
John McCall95007602010-05-10 23:27:23 +0000136 // Require the base expression to be a global l-value.
Abramo Bagnaraf8199452010-05-14 17:07:14 +0000137 if (!IsGlobalLValue(Base)) return false;
John McCall95007602010-05-10 23:27:23 +0000138
John McCalleb3e4f32010-05-07 21:34:32 +0000139 // We have a non-null base expression. These are generally known to
140 // be true, but if it'a decl-ref to a weak symbol it can be null at
141 // runtime.
John McCalleb3e4f32010-05-07 21:34:32 +0000142 Result = true;
143
144 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000145 if (!DeclRef)
146 return true;
147
John McCalleb3e4f32010-05-07 21:34:32 +0000148 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa1f9cc12010-05-07 15:18:43 +0000149 const ValueDecl* Decl = DeclRef->getDecl();
150 if (Decl->hasAttr<WeakAttr>() ||
151 Decl->hasAttr<WeakRefAttr>() ||
152 Decl->hasAttr<WeakImportAttr>())
153 return false;
154
Eli Friedman334046a2009-06-14 02:17:33 +0000155 return true;
156}
157
John McCall1be1c632010-01-05 23:42:56 +0000158static bool HandleConversionToBool(const Expr* E, bool& Result,
159 EvalInfo &Info) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000160 if (E->getType()->isIntegralOrEnumerationType()) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000161 APSInt IntResult;
162 if (!EvaluateInteger(E, IntResult, Info))
163 return false;
164 Result = IntResult != 0;
165 return true;
166 } else if (E->getType()->isRealFloatingType()) {
167 APFloat FloatResult(0.0);
168 if (!EvaluateFloat(E, FloatResult, Info))
169 return false;
170 Result = !FloatResult.isZero();
171 return true;
Eli Friedman64004332009-03-23 04:38:34 +0000172 } else if (E->getType()->hasPointerRepresentation()) {
John McCall45d55e42010-05-07 21:00:08 +0000173 LValue PointerResult;
Eli Friedman9a156e52008-11-12 09:44:48 +0000174 if (!EvaluatePointer(E, PointerResult, Info))
175 return false;
Eli Friedman334046a2009-06-14 02:17:33 +0000176 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +0000177 } else if (E->getType()->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +0000178 ComplexValue ComplexResult;
Eli Friedman64004332009-03-23 04:38:34 +0000179 if (!EvaluateComplex(E, ComplexResult, Info))
180 return false;
181 if (ComplexResult.isComplexFloat()) {
182 Result = !ComplexResult.getComplexFloatReal().isZero() ||
183 !ComplexResult.getComplexFloatImag().isZero();
184 } else {
185 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
186 ComplexResult.getComplexIntImag().getBoolValue();
187 }
188 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000189 }
190
191 return false;
192}
193
Mike Stump11289f42009-09-09 15:08:12 +0000194static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000195 APFloat &Value, ASTContext &Ctx) {
196 unsigned DestWidth = Ctx.getIntWidth(DestType);
197 // Determine whether we are converting to unsigned or signed.
198 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000200 // FIXME: Warning for overflow.
201 uint64_t Space[4];
202 bool ignored;
203 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
204 llvm::APFloat::rmTowardZero, &ignored);
205 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
206}
207
Mike Stump11289f42009-09-09 15:08:12 +0000208static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000209 APFloat &Value, ASTContext &Ctx) {
210 bool ignored;
211 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000212 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000213 APFloat::rmNearestTiesToEven, &ignored);
214 return Result;
215}
216
Mike Stump11289f42009-09-09 15:08:12 +0000217static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000218 APSInt &Value, ASTContext &Ctx) {
219 unsigned DestWidth = Ctx.getIntWidth(DestType);
220 APSInt Result = Value;
221 // Figure out if this is a truncate, extend or noop cast.
222 // If the input is signed, do a sign extend, noop, or truncate.
223 Result.extOrTrunc(DestWidth);
224 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
225 return Result;
226}
227
Mike Stump11289f42009-09-09 15:08:12 +0000228static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000229 APSInt &Value, ASTContext &Ctx) {
230
231 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
232 Result.convertFromAPInt(Value, Value.isSigned(),
233 APFloat::rmNearestTiesToEven);
234 return Result;
235}
236
Mike Stump876387b2009-10-27 22:09:17 +0000237namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000238class HasSideEffect
Mike Stump876387b2009-10-27 22:09:17 +0000239 : public StmtVisitor<HasSideEffect, bool> {
240 EvalInfo &Info;
241public:
242
243 HasSideEffect(EvalInfo &info) : Info(info) {}
244
245 // Unhandled nodes conservatively default to having side effects.
246 bool VisitStmt(Stmt *S) {
247 return true;
248 }
249
250 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
251 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000252 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000253 return true;
254 return false;
255 }
256 // We don't want to evaluate BlockExprs multiple times, as they generate
257 // a ton of code.
258 bool VisitBlockExpr(BlockExpr *E) { return true; }
259 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
260 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
261 { return Visit(E->getInitializer()); }
262 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
263 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
264 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
265 bool VisitStringLiteral(StringLiteral *E) { return false; }
266 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
267 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
268 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stumpfa502902009-10-29 20:48:09 +0000269 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000270 bool VisitChooseExpr(ChooseExpr *E)
271 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
272 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
273 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000274 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000275 bool VisitBinaryOperator(BinaryOperator *E)
276 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000277 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
278 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
279 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
280 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
281 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000282 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000283 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000284 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000285 }
286 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattnera0679422010-04-13 17:34:23 +0000287
288 // Has side effects if any element does.
289 bool VisitInitListExpr(InitListExpr *E) {
290 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
291 if (Visit(E->getInit(i))) return true;
292 return false;
293 }
Mike Stump876387b2009-10-27 22:09:17 +0000294};
295
Mike Stump876387b2009-10-27 22:09:17 +0000296} // end anonymous namespace
297
Eli Friedman9a156e52008-11-12 09:44:48 +0000298//===----------------------------------------------------------------------===//
299// LValue Evaluation
300//===----------------------------------------------------------------------===//
301namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000302class LValueExprEvaluator
John McCall45d55e42010-05-07 21:00:08 +0000303 : public StmtVisitor<LValueExprEvaluator, bool> {
Eli Friedman9a156e52008-11-12 09:44:48 +0000304 EvalInfo &Info;
John McCall45d55e42010-05-07 21:00:08 +0000305 LValue &Result;
306
307 bool Success(Expr *E) {
308 Result.Base = E;
309 Result.Offset = CharUnits::Zero();
310 return true;
311 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000312public:
Mike Stump11289f42009-09-09 15:08:12 +0000313
John McCall45d55e42010-05-07 21:00:08 +0000314 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
315 Info(info), Result(Result) {}
Eli Friedman9a156e52008-11-12 09:44:48 +0000316
John McCall45d55e42010-05-07 21:00:08 +0000317 bool VisitStmt(Stmt *S) {
318 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000319 }
Douglas Gregor882211c2010-04-28 22:16:22 +0000320
John McCall45d55e42010-05-07 21:00:08 +0000321 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
322 bool VisitDeclRefExpr(DeclRefExpr *E);
323 bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
324 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
325 bool VisitMemberExpr(MemberExpr *E);
326 bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
327 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
328 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
329 bool VisitUnaryDeref(UnaryOperator *E);
330 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman449fe542009-03-23 04:56:01 +0000331 { return Visit(E->getSubExpr()); }
John McCall45d55e42010-05-07 21:00:08 +0000332 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedman449fe542009-03-23 04:56:01 +0000333 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000334
John McCall45d55e42010-05-07 21:00:08 +0000335 bool VisitCastExpr(CastExpr *E) {
Anders Carlssonde55f642009-10-03 16:30:22 +0000336 switch (E->getCastKind()) {
337 default:
John McCall45d55e42010-05-07 21:00:08 +0000338 return false;
Anders Carlssonde55f642009-10-03 16:30:22 +0000339
John McCalle3027922010-08-25 11:45:40 +0000340 case CK_NoOp:
Anders Carlssonde55f642009-10-03 16:30:22 +0000341 return Visit(E->getSubExpr());
342 }
343 }
Eli Friedman449fe542009-03-23 04:56:01 +0000344 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000345};
346} // end anonymous namespace
347
John McCall45d55e42010-05-07 21:00:08 +0000348static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
349 return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Eli Friedman9a156e52008-11-12 09:44:48 +0000350}
351
John McCall45d55e42010-05-07 21:00:08 +0000352bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000353 if (isa<FunctionDecl>(E->getDecl())) {
John McCall45d55e42010-05-07 21:00:08 +0000354 return Success(E);
Eli Friedman751aa72b72009-05-27 06:04:58 +0000355 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
356 if (!VD->getType()->isReferenceType())
John McCall45d55e42010-05-07 21:00:08 +0000357 return Success(E);
Chandler Carruthe299ba62010-05-16 09:32:51 +0000358 // Reference parameters can refer to anything even if they have an
359 // "initializer" in the form of a default argument.
360 if (isa<ParmVarDecl>(VD))
361 return false;
Eli Friedman9ab03192009-08-29 19:09:59 +0000362 // FIXME: Check whether VD might be overridden!
Sebastian Redl5ca79842010-02-01 20:16:42 +0000363 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregor0840cc02009-11-01 20:32:48 +0000364 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000365 }
366
John McCall45d55e42010-05-07 21:00:08 +0000367 return false;
Anders Carlssona42ee442008-11-24 04:41:22 +0000368}
369
John McCall45d55e42010-05-07 21:00:08 +0000370bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCall45d55e42010-05-07 21:00:08 +0000371 return Success(E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000372}
373
John McCall45d55e42010-05-07 21:00:08 +0000374bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000375 QualType Ty;
376 if (E->isArrow()) {
John McCall45d55e42010-05-07 21:00:08 +0000377 if (!EvaluatePointer(E->getBase(), Result, Info))
378 return false;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000379 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000380 } else {
John McCall45d55e42010-05-07 21:00:08 +0000381 if (!Visit(E->getBase()))
382 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000383 Ty = E->getBase()->getType();
384 }
385
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000386 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000387 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000388
389 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
390 if (!FD) // FIXME: deal with other kinds of member expressions
John McCall45d55e42010-05-07 21:00:08 +0000391 return false;
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000392
393 if (FD->getType()->isReferenceType())
John McCall45d55e42010-05-07 21:00:08 +0000394 return false;
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000395
Eli Friedman9a156e52008-11-12 09:44:48 +0000396 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000397 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000398 for (RecordDecl::field_iterator Field = RD->field_begin(),
399 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000400 Field != FieldEnd; (void)++Field, ++i) {
401 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000402 break;
403 }
404
John McCall45d55e42010-05-07 21:00:08 +0000405 Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8);
406 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000407}
408
John McCall45d55e42010-05-07 21:00:08 +0000409bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000410 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCall45d55e42010-05-07 21:00:08 +0000411 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000413 APSInt Index;
414 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCall45d55e42010-05-07 21:00:08 +0000415 return false;
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000416
Ken Dyck40775002010-01-11 17:06:35 +0000417 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCall45d55e42010-05-07 21:00:08 +0000418 Result.Offset += Index.getSExtValue() * ElementSize;
419 return true;
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000420}
Eli Friedman9a156e52008-11-12 09:44:48 +0000421
John McCall45d55e42010-05-07 21:00:08 +0000422bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
423 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000424}
425
Eli Friedman9a156e52008-11-12 09:44:48 +0000426//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000427// Pointer Evaluation
428//===----------------------------------------------------------------------===//
429
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000430namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000431class PointerExprEvaluator
John McCall45d55e42010-05-07 21:00:08 +0000432 : public StmtVisitor<PointerExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000433 EvalInfo &Info;
John McCall45d55e42010-05-07 21:00:08 +0000434 LValue &Result;
435
436 bool Success(Expr *E) {
437 Result.Base = E;
438 Result.Offset = CharUnits::Zero();
439 return true;
440 }
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000441public:
Mike Stump11289f42009-09-09 15:08:12 +0000442
John McCall45d55e42010-05-07 21:00:08 +0000443 PointerExprEvaluator(EvalInfo &info, LValue &Result)
444 : Info(info), Result(Result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000445
John McCall45d55e42010-05-07 21:00:08 +0000446 bool VisitStmt(Stmt *S) {
447 return false;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000448 }
449
John McCall45d55e42010-05-07 21:00:08 +0000450 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000451
John McCall45d55e42010-05-07 21:00:08 +0000452 bool VisitBinaryOperator(const BinaryOperator *E);
453 bool VisitCastExpr(CastExpr* E);
454 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedmanc2b50172009-02-22 11:46:18 +0000455 { return Visit(E->getSubExpr()); }
John McCall45d55e42010-05-07 21:00:08 +0000456 bool VisitUnaryAddrOf(const UnaryOperator *E);
457 bool VisitObjCStringLiteral(ObjCStringLiteral *E)
458 { return Success(E); }
459 bool VisitAddrLabelExpr(AddrLabelExpr *E)
460 { return Success(E); }
461 bool VisitCallExpr(CallExpr *E);
462 bool VisitBlockExpr(BlockExpr *E) {
Mike Stumpa6703322009-02-19 22:01:56 +0000463 if (!E->hasBlockDeclRefExprs())
John McCall45d55e42010-05-07 21:00:08 +0000464 return Success(E);
465 return false;
Mike Stumpa6703322009-02-19 22:01:56 +0000466 }
John McCall45d55e42010-05-07 21:00:08 +0000467 bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
468 { return Success((Expr*)0); }
469 bool VisitConditionalOperator(ConditionalOperator *E);
470 bool VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000471 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCall45d55e42010-05-07 21:00:08 +0000472 bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
473 { return Success((Expr*)0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000474 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000475};
Chris Lattner05706e882008-07-11 18:11:29 +0000476} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000477
John McCall45d55e42010-05-07 21:00:08 +0000478static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +0000479 assert(E->getType()->hasPointerRepresentation());
John McCall45d55e42010-05-07 21:00:08 +0000480 return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000481}
482
John McCall45d55e42010-05-07 21:00:08 +0000483bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCalle3027922010-08-25 11:45:40 +0000484 if (E->getOpcode() != BO_Add &&
485 E->getOpcode() != BO_Sub)
John McCall45d55e42010-05-07 21:00:08 +0000486 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Chris Lattner05706e882008-07-11 18:11:29 +0000488 const Expr *PExp = E->getLHS();
489 const Expr *IExp = E->getRHS();
490 if (IExp->getType()->isPointerType())
491 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000492
John McCall45d55e42010-05-07 21:00:08 +0000493 if (!EvaluatePointer(PExp, Result, Info))
494 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000495
John McCall45d55e42010-05-07 21:00:08 +0000496 llvm::APSInt Offset;
497 if (!EvaluateInteger(IExp, Offset, Info))
498 return false;
499 int64_t AdditionalOffset
500 = Offset.isSigned() ? Offset.getSExtValue()
501 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000502
Daniel Dunbar4c43e312010-03-20 05:53:45 +0000503 // Compute the new offset in the appropriate width.
504
505 QualType PointeeType =
506 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCall45d55e42010-05-07 21:00:08 +0000507 CharUnits SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Anders Carlssonef56fba2009-02-19 04:55:58 +0000509 // Explicitly handle GNU void* and function pointer arithmetic extensions.
510 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCall45d55e42010-05-07 21:00:08 +0000511 SizeOfPointee = CharUnits::One();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000512 else
John McCall45d55e42010-05-07 21:00:08 +0000513 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman9a156e52008-11-12 09:44:48 +0000514
John McCalle3027922010-08-25 11:45:40 +0000515 if (E->getOpcode() == BO_Add)
John McCall45d55e42010-05-07 21:00:08 +0000516 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000517 else
John McCall45d55e42010-05-07 21:00:08 +0000518 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman9a156e52008-11-12 09:44:48 +0000519
John McCall45d55e42010-05-07 21:00:08 +0000520 return true;
Chris Lattner05706e882008-07-11 18:11:29 +0000521}
Eli Friedman9a156e52008-11-12 09:44:48 +0000522
John McCall45d55e42010-05-07 21:00:08 +0000523bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
524 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman9a156e52008-11-12 09:44:48 +0000525}
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner05706e882008-07-11 18:11:29 +0000527
John McCall45d55e42010-05-07 21:00:08 +0000528bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
Eli Friedman847a2bc2009-12-27 05:43:15 +0000529 Expr* SubExpr = E->getSubExpr();
Chris Lattner05706e882008-07-11 18:11:29 +0000530
Eli Friedman847a2bc2009-12-27 05:43:15 +0000531 switch (E->getCastKind()) {
532 default:
533 break;
534
John McCalle3027922010-08-25 11:45:40 +0000535 case CK_Unknown: {
Eli Friedman847a2bc2009-12-27 05:43:15 +0000536 // 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
Douglas Gregorb90df602010-06-16 00:17:44 +0000545 if (SubExpr->getType()->isIntegralOrEnumerationType()) {
John McCall45d55e42010-05-07 21:00:08 +0000546 APValue Value;
547 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000548 break;
549
John McCall45d55e42010-05-07 21:00:08 +0000550 if (Value.isInt()) {
551 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
552 Result.Base = 0;
553 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
554 return true;
555 } else {
556 Result.Base = Value.getLValueBase();
557 Result.Offset = Value.getLValueOffset();
558 return true;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000559 }
Eli Friedman847a2bc2009-12-27 05:43:15 +0000560 }
561 break;
Chris Lattner05706e882008-07-11 18:11:29 +0000562 }
Mike Stump11289f42009-09-09 15:08:12 +0000563
John McCalle3027922010-08-25 11:45:40 +0000564 case CK_NoOp:
565 case CK_BitCast:
566 case CK_LValueBitCast:
567 case CK_AnyPointerToObjCPointerCast:
568 case CK_AnyPointerToBlockPointerCast:
Eli Friedman847a2bc2009-12-27 05:43:15 +0000569 return Visit(SubExpr);
570
John McCalle3027922010-08-25 11:45:40 +0000571 case CK_IntegralToPointer: {
John McCall45d55e42010-05-07 21:00:08 +0000572 APValue Value;
573 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000574 break;
Daniel Dunbarce399542009-02-20 18:22:23 +0000575
John McCall45d55e42010-05-07 21:00:08 +0000576 if (Value.isInt()) {
577 Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
578 Result.Base = 0;
579 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
580 return true;
581 } else {
582 // Cast is of an lvalue, no need to change value.
583 Result.Base = Value.getLValueBase();
584 Result.Offset = Value.getLValueOffset();
585 return true;
Chris Lattner05706e882008-07-11 18:11:29 +0000586 }
587 }
John McCalle3027922010-08-25 11:45:40 +0000588 case CK_ArrayToPointerDecay:
589 case CK_FunctionToPointerDecay:
John McCall45d55e42010-05-07 21:00:08 +0000590 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman9a156e52008-11-12 09:44:48 +0000591 }
592
John McCall45d55e42010-05-07 21:00:08 +0000593 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000594}
Chris Lattner05706e882008-07-11 18:11:29 +0000595
John McCall45d55e42010-05-07 21:00:08 +0000596bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000597 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall481e3a82010-01-23 02:40:42 +0000598 Builtin::BI__builtin___CFStringMakeConstantString ||
599 E->isBuiltinCall(Info.Ctx) ==
600 Builtin::BI__builtin___NSStringMakeConstantString)
John McCall45d55e42010-05-07 21:00:08 +0000601 return Success(E);
602 return false;
Eli Friedmanc69d4542009-01-25 01:54:01 +0000603}
604
John McCall45d55e42010-05-07 21:00:08 +0000605bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000606 bool BoolResult;
607 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
John McCall45d55e42010-05-07 21:00:08 +0000608 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +0000609
610 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
John McCall45d55e42010-05-07 21:00:08 +0000611 return Visit(EvalExpr);
Eli Friedman9a156e52008-11-12 09:44:48 +0000612}
Chris Lattner05706e882008-07-11 18:11:29 +0000613
614//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000615// Vector Evaluation
616//===----------------------------------------------------------------------===//
617
618namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000619 class VectorExprEvaluator
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000620 : public StmtVisitor<VectorExprEvaluator, APValue> {
621 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000622 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000623 public:
Mike Stump11289f42009-09-09 15:08:12 +0000624
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000625 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000626
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000627 APValue VisitStmt(Stmt *S) {
628 return APValue();
629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
Eli Friedman3ae59112009-02-23 04:23:56 +0000631 APValue VisitParenExpr(ParenExpr *E)
632 { return Visit(E->getSubExpr()); }
633 APValue VisitUnaryExtension(const UnaryOperator *E)
634 { return Visit(E->getSubExpr()); }
635 APValue VisitUnaryPlus(const UnaryOperator *E)
636 { return Visit(E->getSubExpr()); }
637 APValue VisitUnaryReal(const UnaryOperator *E)
638 { return Visit(E->getSubExpr()); }
639 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
640 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000641 APValue VisitCastExpr(const CastExpr* E);
642 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
643 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000644 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000645 APValue VisitChooseExpr(const ChooseExpr *E)
646 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000647 APValue VisitUnaryImag(const UnaryOperator *E);
648 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000649 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000650 // shufflevector, ExtVectorElementExpr
651 // (Note that these require implementing conversions
652 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000653 };
654} // end anonymous namespace
655
656static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
657 if (!E->getType()->isVectorType())
658 return false;
659 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
660 return !Result.isUninit();
661}
662
663APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000664 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000665 QualType EltTy = VTy->getElementType();
666 unsigned NElts = VTy->getNumElements();
667 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000669 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000670 QualType SETy = SE->getType();
671 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000672
Nate Begeman2ffd3842009-06-26 18:22:18 +0000673 // Check for vector->vector bitcast and scalar->vector splat.
674 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000675 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000676 } else if (SETy->isIntegerType()) {
677 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000678 if (!EvaluateInteger(SE, IntResult, Info))
679 return APValue();
680 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000681 } else if (SETy->isRealFloatingType()) {
682 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000683 if (!EvaluateFloat(SE, F, Info))
684 return APValue();
685 Result = APValue(F);
686 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000687 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000688
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000689 // For casts of a scalar to ExtVector, convert the scalar to the element type
690 // and splat it to all elements.
691 if (E->getType()->isExtVectorType()) {
692 if (EltTy->isIntegerType() && Result.isInt())
693 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
694 Info.Ctx));
695 else if (EltTy->isIntegerType())
696 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
697 Info.Ctx));
698 else if (EltTy->isRealFloatingType() && Result.isInt())
699 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
700 Info.Ctx));
701 else if (EltTy->isRealFloatingType())
702 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
703 Info.Ctx));
704 else
705 return APValue();
706
707 // Splat and create vector APValue.
708 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
709 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000710 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000711
712 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
713 // to the vector. To construct the APValue vector initializer, bitcast the
714 // initializing value to an APInt, and shift out the bits pertaining to each
715 // element.
716 APSInt Init;
717 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000718
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000719 llvm::SmallVector<APValue, 4> Elts;
720 for (unsigned i = 0; i != NElts; ++i) {
721 APSInt Tmp = Init;
722 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000724 if (EltTy->isIntegerType())
725 Elts.push_back(APValue(Tmp));
726 else if (EltTy->isRealFloatingType())
727 Elts.push_back(APValue(APFloat(Tmp)));
728 else
729 return APValue();
730
731 Init >>= EltWidth;
732 }
733 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000734}
735
Mike Stump11289f42009-09-09 15:08:12 +0000736APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000737VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
738 return this->Visit(const_cast<Expr*>(E->getInitializer()));
739}
740
Mike Stump11289f42009-09-09 15:08:12 +0000741APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000742VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000743 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000744 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000745 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000746
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000747 QualType EltTy = VT->getElementType();
748 llvm::SmallVector<APValue, 4> Elements;
749
John McCall875679e2010-06-11 17:54:15 +0000750 // If a vector is initialized with a single element, that value
751 // becomes every element of the vector, not just the first.
752 // This is the behavior described in the IBM AltiVec documentation.
753 if (NumInits == 1) {
754 APValue InitValue;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000755 if (EltTy->isIntegerType()) {
756 llvm::APSInt sInt(32);
John McCall875679e2010-06-11 17:54:15 +0000757 if (!EvaluateInteger(E->getInit(0), sInt, Info))
758 return APValue();
759 InitValue = APValue(sInt);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000760 } else {
761 llvm::APFloat f(0.0);
John McCall875679e2010-06-11 17:54:15 +0000762 if (!EvaluateFloat(E->getInit(0), f, Info))
763 return APValue();
764 InitValue = APValue(f);
765 }
766 for (unsigned i = 0; i < NumElements; i++) {
767 Elements.push_back(InitValue);
768 }
769 } else {
770 for (unsigned i = 0; i < NumElements; i++) {
771 if (EltTy->isIntegerType()) {
772 llvm::APSInt sInt(32);
773 if (i < NumInits) {
774 if (!EvaluateInteger(E->getInit(i), sInt, Info))
775 return APValue();
776 } else {
777 sInt = Info.Ctx.MakeIntValue(0, EltTy);
778 }
779 Elements.push_back(APValue(sInt));
Eli Friedman3ae59112009-02-23 04:23:56 +0000780 } else {
John McCall875679e2010-06-11 17:54:15 +0000781 llvm::APFloat f(0.0);
782 if (i < NumInits) {
783 if (!EvaluateFloat(E->getInit(i), f, Info))
784 return APValue();
785 } else {
786 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
787 }
788 Elements.push_back(APValue(f));
Eli Friedman3ae59112009-02-23 04:23:56 +0000789 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000790 }
791 }
792 return APValue(&Elements[0], Elements.size());
793}
794
Mike Stump11289f42009-09-09 15:08:12 +0000795APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000796VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000797 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000798 QualType EltTy = VT->getElementType();
799 APValue ZeroElement;
800 if (EltTy->isIntegerType())
801 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
802 else
803 ZeroElement =
804 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
805
806 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
807 return APValue(&Elements[0], Elements.size());
808}
809
810APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
811 bool BoolResult;
812 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
813 return APValue();
814
815 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
816
817 APValue Result;
818 if (EvaluateVector(EvalExpr, Result, Info))
819 return Result;
820 return APValue();
821}
822
Eli Friedman3ae59112009-02-23 04:23:56 +0000823APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
824 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
825 Info.EvalResult.HasSideEffects = true;
826 return GetZeroVector(E->getType());
827}
828
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000829//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000830// Integer Evaluation
831//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000832
833namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000834class IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000835 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000836 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000837 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000838public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000839 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000840 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000841
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000842 bool Success(const llvm::APSInt &SI, const Expr *E) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000843 assert(E->getType()->isIntegralOrEnumerationType() &&
844 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000845 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000846 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000847 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000848 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000849 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000850 return true;
851 }
852
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000853 bool Success(const llvm::APInt &I, const Expr *E) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000854 assert(E->getType()->isIntegralOrEnumerationType() &&
855 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000856 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000857 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000858 Result = APValue(APSInt(I));
859 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000860 return true;
861 }
862
863 bool Success(uint64_t Value, const Expr *E) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000864 assert(E->getType()->isIntegralOrEnumerationType() &&
865 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000866 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000867 return true;
868 }
869
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000870 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000871 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000872 if (Info.EvalResult.Diag == 0) {
873 Info.EvalResult.DiagLoc = L;
874 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000875 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000876 }
Chris Lattner99415702008-07-12 00:14:42 +0000877 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000878 }
Mike Stump11289f42009-09-09 15:08:12 +0000879
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000880 //===--------------------------------------------------------------------===//
881 // Visitor Methods
882 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000884 bool VisitStmt(Stmt *) {
885 assert(0 && "This should be called on integers, stmts are not integers");
886 return false;
887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000889 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000890 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000891 }
Mike Stump11289f42009-09-09 15:08:12 +0000892
Chris Lattnere13042c2008-07-11 19:10:17 +0000893 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000894
Chris Lattner7174bf32008-07-12 00:38:25 +0000895 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000896 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000897 }
898 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000899 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000900 }
901 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000902 // Per gcc docs "this built-in function ignores top level
903 // qualifiers". We need to use the canonical version to properly
904 // be able to strip CRV qualifiers from the type.
905 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
906 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000907 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000908 T1.getUnqualifiedType()),
909 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000910 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000911
912 bool CheckReferencedDecl(const Expr *E, const Decl *D);
913 bool VisitDeclRefExpr(const DeclRefExpr *E) {
914 return CheckReferencedDecl(E, E->getDecl());
915 }
916 bool VisitMemberExpr(const MemberExpr *E) {
917 if (CheckReferencedDecl(E, E->getMemberDecl())) {
918 // Conservatively assume a MemberExpr will have side-effects
919 Info.EvalResult.HasSideEffects = true;
920 return true;
921 }
922 return false;
923 }
924
Eli Friedmand5c93992010-02-13 00:10:10 +0000925 bool VisitCallExpr(CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000926 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor882211c2010-04-28 22:16:22 +0000927 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000928 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000929 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000930
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000931 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000932 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
933
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000934 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000935 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000936 }
Mike Stump11289f42009-09-09 15:08:12 +0000937
Anders Carlsson39def3a2008-12-21 22:39:40 +0000938 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000939 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000940 }
Mike Stump11289f42009-09-09 15:08:12 +0000941
Douglas Gregor747eb782010-07-08 06:14:04 +0000942 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000943 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000944 }
945
Eli Friedman4e7a2412009-02-27 04:45:43 +0000946 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
947 return Success(0, E);
948 }
949
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000950 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000951 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000952 }
953
Eli Friedman449fe542009-03-23 04:56:01 +0000954 bool VisitChooseExpr(const ChooseExpr *E) {
955 return Visit(E->getChosenSubExpr(Info.Ctx));
956 }
957
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000958 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000959 bool VisitUnaryImag(const UnaryOperator *E);
960
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000961private:
Ken Dyck160146e2010-01-27 17:10:57 +0000962 CharUnits GetAlignOfExpr(const Expr *E);
963 CharUnits GetAlignOfType(QualType T);
John McCall95007602010-05-10 23:27:23 +0000964 static QualType GetObjectType(const Expr *E);
965 bool TryEvaluateBuiltinObjectSize(CallExpr *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000966 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000967};
Chris Lattner05706e882008-07-11 18:11:29 +0000968} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000969
Daniel Dunbarce399542009-02-20 18:22:23 +0000970static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000971 assert(E->getType()->isIntegralOrEnumerationType());
Daniel Dunbarce399542009-02-20 18:22:23 +0000972 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
973}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000974
Daniel Dunbarce399542009-02-20 18:22:23 +0000975static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000976 assert(E->getType()->isIntegralOrEnumerationType());
John McCallf0c4f352010-05-07 05:46:35 +0000977
Daniel Dunbarce399542009-02-20 18:22:23 +0000978 APValue Val;
979 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
980 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000981 Result = Val.getInt();
982 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000983}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000984
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000985bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000986 // Enums are integer constant exprs.
Eli Friedmanee275c82009-12-10 22:29:29 +0000987 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
988 return Success(ECD->getInitVal(), E);
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000989
990 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000991 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000992 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
993 == Qualifiers::Const) {
Anders Carlssonb0695ef2010-02-03 21:58:41 +0000994
995 if (isa<ParmVarDecl>(D))
996 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
997
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000998 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000999 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +00001000 if (APValue *V = VD->getEvaluatedValue()) {
1001 if (V->isInt())
1002 return Success(V->getInt(), E);
1003 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1004 }
1005
1006 if (VD->isEvaluatingValue())
1007 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1008
1009 VD->setEvaluatingValue();
1010
Eli Friedman0b1fbd12010-09-06 00:10:32 +00001011 Expr::EvalResult EResult;
1012 if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
1013 EResult.Val.isInt()) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001014 // Cache the evaluated value in the variable declaration.
Eli Friedman0b1fbd12010-09-06 00:10:32 +00001015 Result = EResult.Val;
Eli Friedman1d6fb162009-12-03 20:31:57 +00001016 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001017 return true;
1018 }
1019
Eli Friedman1d6fb162009-12-03 20:31:57 +00001020 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001021 return false;
1022 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +00001023 }
1024 }
1025
Chris Lattner7174bf32008-07-12 00:38:25 +00001026 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001027 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +00001028}
1029
Chris Lattner86ee2862008-10-06 06:40:35 +00001030/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1031/// as GCC.
1032static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1033 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001034 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +00001035 enum gcc_type_class {
1036 no_type_class = -1,
1037 void_type_class, integer_type_class, char_type_class,
1038 enumeral_type_class, boolean_type_class,
1039 pointer_type_class, reference_type_class, offset_type_class,
1040 real_type_class, complex_type_class,
1041 function_type_class, method_type_class,
1042 record_type_class, union_type_class,
1043 array_type_class, string_type_class,
1044 lang_type_class
1045 };
Mike Stump11289f42009-09-09 15:08:12 +00001046
1047 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +00001048 // ideal, however it is what gcc does.
1049 if (E->getNumArgs() == 0)
1050 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +00001051
Chris Lattner86ee2862008-10-06 06:40:35 +00001052 QualType ArgTy = E->getArg(0)->getType();
1053 if (ArgTy->isVoidType())
1054 return void_type_class;
1055 else if (ArgTy->isEnumeralType())
1056 return enumeral_type_class;
1057 else if (ArgTy->isBooleanType())
1058 return boolean_type_class;
1059 else if (ArgTy->isCharType())
1060 return string_type_class; // gcc doesn't appear to use char_type_class
1061 else if (ArgTy->isIntegerType())
1062 return integer_type_class;
1063 else if (ArgTy->isPointerType())
1064 return pointer_type_class;
1065 else if (ArgTy->isReferenceType())
1066 return reference_type_class;
1067 else if (ArgTy->isRealType())
1068 return real_type_class;
1069 else if (ArgTy->isComplexType())
1070 return complex_type_class;
1071 else if (ArgTy->isFunctionType())
1072 return function_type_class;
Douglas Gregor8385a062010-04-26 21:31:17 +00001073 else if (ArgTy->isStructureOrClassType())
Chris Lattner86ee2862008-10-06 06:40:35 +00001074 return record_type_class;
1075 else if (ArgTy->isUnionType())
1076 return union_type_class;
1077 else if (ArgTy->isArrayType())
1078 return array_type_class;
1079 else if (ArgTy->isUnionType())
1080 return union_type_class;
1081 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1082 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1083 return -1;
1084}
1085
John McCall95007602010-05-10 23:27:23 +00001086/// Retrieves the "underlying object type" of the given expression,
1087/// as used by __builtin_object_size.
1088QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1089 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1090 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1091 return VD->getType();
1092 } else if (isa<CompoundLiteralExpr>(E)) {
1093 return E->getType();
1094 }
1095
1096 return QualType();
1097}
1098
1099bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
1100 // TODO: Perhaps we should let LLVM lower this?
1101 LValue Base;
1102 if (!EvaluatePointer(E->getArg(0), Base, Info))
1103 return false;
1104
1105 // If we can prove the base is null, lower to zero now.
1106 const Expr *LVBase = Base.getLValueBase();
1107 if (!LVBase) return Success(0, E);
1108
1109 QualType T = GetObjectType(LVBase);
1110 if (T.isNull() ||
1111 T->isIncompleteType() ||
Eli Friedmana170cd62010-08-05 02:49:48 +00001112 T->isFunctionType() ||
John McCall95007602010-05-10 23:27:23 +00001113 T->isVariablyModifiedType() ||
1114 T->isDependentType())
1115 return false;
1116
1117 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1118 CharUnits Offset = Base.getLValueOffset();
1119
1120 if (!Offset.isNegative() && Offset <= Size)
1121 Size -= Offset;
1122 else
1123 Size = CharUnits::Zero();
1124 return Success(Size.getQuantity(), E);
1125}
1126
Eli Friedmand5c93992010-02-13 00:10:10 +00001127bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001128 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001129 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001130 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +00001131
1132 case Builtin::BI__builtin_object_size: {
John McCall95007602010-05-10 23:27:23 +00001133 if (TryEvaluateBuiltinObjectSize(E))
1134 return true;
Mike Stump722cedf2009-10-26 18:35:08 +00001135
Eric Christopher99469702010-01-19 22:58:35 +00001136 // If evaluating the argument has side-effects we can't determine
1137 // the size of the object and lower it to unknown now.
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00001138 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer0128f662010-01-03 18:18:37 +00001139 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattner4f105592009-11-03 19:48:51 +00001140 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +00001141 return Success(0, E);
1142 }
Mike Stump876387b2009-10-27 22:09:17 +00001143
Mike Stump722cedf2009-10-26 18:35:08 +00001144 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1145 }
1146
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001147 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001148 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +00001149
Anders Carlsson4c76e932008-11-24 04:21:33 +00001150 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001151 // __builtin_constant_p always has one operand: it returns true if that
1152 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001153 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +00001154
1155 case Builtin::BI__builtin_eh_return_data_regno: {
1156 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1157 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1158 return Success(Operand, E);
1159 }
Eli Friedmand5c93992010-02-13 00:10:10 +00001160
1161 case Builtin::BI__builtin_expect:
1162 return Visit(E->getArg(0));
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001163 }
Chris Lattner7174bf32008-07-12 00:38:25 +00001164}
Anders Carlsson4a3585b2008-07-08 15:34:11 +00001165
Chris Lattnere13042c2008-07-11 19:10:17 +00001166bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCalle3027922010-08-25 11:45:40 +00001167 if (E->getOpcode() == BO_Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001168 if (!Visit(E->getRHS()))
1169 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001170
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001171 // If we can't evaluate the LHS, it might have side effects;
1172 // conservatively mark it.
1173 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1174 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001175
Anders Carlsson564730a2008-12-01 02:07:06 +00001176 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001177 }
1178
1179 if (E->isLogicalOp()) {
1180 // These need to be handled specially because the operands aren't
1181 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001182 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001183
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001184 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001185 // We were able to evaluate the LHS, see if we can get away with not
1186 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
John McCalle3027922010-08-25 11:45:40 +00001187 if (lhsResult == (E->getOpcode() == BO_LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001188 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001189
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001190 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
John McCalle3027922010-08-25 11:45:40 +00001191 if (E->getOpcode() == BO_LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001192 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001193 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001194 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001195 }
1196 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001197 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001198 // We can't evaluate the LHS; however, sometimes the result
1199 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
John McCalle3027922010-08-25 11:45:40 +00001200 if (rhsResult == (E->getOpcode() == BO_LOr) ||
1201 !rhsResult == (E->getOpcode() == BO_LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001202 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001203 // must have had side effects.
1204 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001205
1206 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001207 }
1208 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001209 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001210
Eli Friedman5a332ea2008-11-13 06:09:17 +00001211 return false;
1212 }
1213
Anders Carlssonacc79812008-11-16 07:17:21 +00001214 QualType LHSTy = E->getLHS()->getType();
1215 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001216
1217 if (LHSTy->isAnyComplexType()) {
1218 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCall93d91dc2010-05-07 17:22:02 +00001219 ComplexValue LHS, RHS;
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001220
1221 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1222 return false;
1223
1224 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1225 return false;
1226
1227 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001228 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001229 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001230 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001231 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1232
John McCalle3027922010-08-25 11:45:40 +00001233 if (E->getOpcode() == BO_EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001234 return Success((CR_r == APFloat::cmpEqual &&
1235 CR_i == APFloat::cmpEqual), E);
1236 else {
John McCalle3027922010-08-25 11:45:40 +00001237 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001238 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001239 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wang75c645c2010-04-29 05:53:29 +00001240 CR_r == APFloat::cmpLessThan ||
1241 CR_r == APFloat::cmpUnordered) ||
Mike Stump11289f42009-09-09 15:08:12 +00001242 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wang75c645c2010-04-29 05:53:29 +00001243 CR_i == APFloat::cmpLessThan ||
1244 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001245 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001246 } else {
John McCalle3027922010-08-25 11:45:40 +00001247 if (E->getOpcode() == BO_EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001248 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1249 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1250 else {
John McCalle3027922010-08-25 11:45:40 +00001251 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001252 "Invalid compex comparison.");
1253 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1254 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1255 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001256 }
1257 }
Mike Stump11289f42009-09-09 15:08:12 +00001258
Anders Carlssonacc79812008-11-16 07:17:21 +00001259 if (LHSTy->isRealFloatingType() &&
1260 RHSTy->isRealFloatingType()) {
1261 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001262
Anders Carlssonacc79812008-11-16 07:17:21 +00001263 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1264 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Anders Carlssonacc79812008-11-16 07:17:21 +00001266 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1267 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001268
Anders Carlssonacc79812008-11-16 07:17:21 +00001269 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001270
Anders Carlssonacc79812008-11-16 07:17:21 +00001271 switch (E->getOpcode()) {
1272 default:
1273 assert(0 && "Invalid binary operator!");
John McCalle3027922010-08-25 11:45:40 +00001274 case BO_LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001275 return Success(CR == APFloat::cmpLessThan, E);
John McCalle3027922010-08-25 11:45:40 +00001276 case BO_GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001277 return Success(CR == APFloat::cmpGreaterThan, E);
John McCalle3027922010-08-25 11:45:40 +00001278 case BO_LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001279 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
John McCalle3027922010-08-25 11:45:40 +00001280 case BO_GE:
Mike Stump11289f42009-09-09 15:08:12 +00001281 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001282 E);
John McCalle3027922010-08-25 11:45:40 +00001283 case BO_EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001284 return Success(CR == APFloat::cmpEqual, E);
John McCalle3027922010-08-25 11:45:40 +00001285 case BO_NE:
Mike Stump11289f42009-09-09 15:08:12 +00001286 return Success(CR == APFloat::cmpGreaterThan
Mon P Wang75c645c2010-04-29 05:53:29 +00001287 || CR == APFloat::cmpLessThan
1288 || CR == APFloat::cmpUnordered, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001289 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001290 }
Mike Stump11289f42009-09-09 15:08:12 +00001291
Eli Friedmana38da572009-04-28 19:17:36 +00001292 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
John McCalle3027922010-08-25 11:45:40 +00001293 if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
John McCall45d55e42010-05-07 21:00:08 +00001294 LValue LHSValue;
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001295 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1296 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001297
John McCall45d55e42010-05-07 21:00:08 +00001298 LValue RHSValue;
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001299 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1300 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001301
Eli Friedman334046a2009-06-14 02:17:33 +00001302 // Reject any bases from the normal codepath; we special-case comparisons
1303 // to null.
1304 if (LHSValue.getLValueBase()) {
1305 if (!E->isEqualityOp())
1306 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001307 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001308 return false;
1309 bool bres;
1310 if (!EvalPointerValueAsBool(LHSValue, bres))
1311 return false;
John McCalle3027922010-08-25 11:45:40 +00001312 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman334046a2009-06-14 02:17:33 +00001313 } else if (RHSValue.getLValueBase()) {
1314 if (!E->isEqualityOp())
1315 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001316 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001317 return false;
1318 bool bres;
1319 if (!EvalPointerValueAsBool(RHSValue, bres))
1320 return false;
John McCalle3027922010-08-25 11:45:40 +00001321 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman334046a2009-06-14 02:17:33 +00001322 }
Eli Friedman64004332009-03-23 04:38:34 +00001323
John McCalle3027922010-08-25 11:45:40 +00001324 if (E->getOpcode() == BO_Sub) {
Chris Lattner882bdf22010-04-20 17:13:14 +00001325 QualType Type = E->getLHS()->getType();
1326 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001327
Ken Dyck02990832010-01-15 12:37:54 +00001328 CharUnits ElementSize = CharUnits::One();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001329 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dyck02990832010-01-15 12:37:54 +00001330 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedman64004332009-03-23 04:38:34 +00001331
Ken Dyck02990832010-01-15 12:37:54 +00001332 CharUnits Diff = LHSValue.getLValueOffset() -
1333 RHSValue.getLValueOffset();
1334 return Success(Diff / ElementSize, E);
Eli Friedmana38da572009-04-28 19:17:36 +00001335 }
1336 bool Result;
John McCalle3027922010-08-25 11:45:40 +00001337 if (E->getOpcode() == BO_EQ) {
Eli Friedmana38da572009-04-28 19:17:36 +00001338 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001339 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001340 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1341 }
1342 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001343 }
1344 }
Douglas Gregorb90df602010-06-16 00:17:44 +00001345 if (!LHSTy->isIntegralOrEnumerationType() ||
1346 !RHSTy->isIntegralOrEnumerationType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001347 // We can't continue from here for non-integral types, and they
1348 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001349 return false;
1350 }
1351
Anders Carlsson9c181652008-07-08 14:35:21 +00001352 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001353 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001354 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001355
Eli Friedman94c25c62009-03-24 01:14:50 +00001356 APValue RHSVal;
1357 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001358 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001359
1360 // Handle cases like (unsigned long)&a + 4.
1361 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001362 CharUnits Offset = Result.getLValueOffset();
1363 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1364 RHSVal.getInt().getZExtValue());
John McCalle3027922010-08-25 11:45:40 +00001365 if (E->getOpcode() == BO_Add)
Ken Dyck02990832010-01-15 12:37:54 +00001366 Offset += AdditionalOffset;
Eli Friedman94c25c62009-03-24 01:14:50 +00001367 else
Ken Dyck02990832010-01-15 12:37:54 +00001368 Offset -= AdditionalOffset;
1369 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001370 return true;
1371 }
1372
1373 // Handle cases like 4 + (unsigned long)&a
John McCalle3027922010-08-25 11:45:40 +00001374 if (E->getOpcode() == BO_Add &&
Eli Friedman94c25c62009-03-24 01:14:50 +00001375 RHSVal.isLValue() && Result.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001376 CharUnits Offset = RHSVal.getLValueOffset();
1377 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1378 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001379 return true;
1380 }
1381
1382 // All the following cases expect both operands to be an integer
1383 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001384 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001385
Eli Friedman94c25c62009-03-24 01:14:50 +00001386 APSInt& RHS = RHSVal.getInt();
1387
Anders Carlsson9c181652008-07-08 14:35:21 +00001388 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001389 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001390 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCalle3027922010-08-25 11:45:40 +00001391 case BO_Mul: return Success(Result.getInt() * RHS, E);
1392 case BO_Add: return Success(Result.getInt() + RHS, E);
1393 case BO_Sub: return Success(Result.getInt() - RHS, E);
1394 case BO_And: return Success(Result.getInt() & RHS, E);
1395 case BO_Xor: return Success(Result.getInt() ^ RHS, E);
1396 case BO_Or: return Success(Result.getInt() | RHS, E);
1397 case BO_Div:
Chris Lattner99415702008-07-12 00:14:42 +00001398 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001399 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001400 return Success(Result.getInt() / RHS, E);
John McCalle3027922010-08-25 11:45:40 +00001401 case BO_Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001402 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001403 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001404 return Success(Result.getInt() % RHS, E);
John McCalle3027922010-08-25 11:45:40 +00001405 case BO_Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001406 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001407 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001408 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1409 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001410 }
John McCalle3027922010-08-25 11:45:40 +00001411 case BO_Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001412 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001413 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1414 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
John McCalle3027922010-08-25 11:45:40 +00001417 case BO_LT: return Success(Result.getInt() < RHS, E);
1418 case BO_GT: return Success(Result.getInt() > RHS, E);
1419 case BO_LE: return Success(Result.getInt() <= RHS, E);
1420 case BO_GE: return Success(Result.getInt() >= RHS, E);
1421 case BO_EQ: return Success(Result.getInt() == RHS, E);
1422 case BO_NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001423 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001424}
1425
Nuno Lopes42042612008-11-16 19:28:31 +00001426bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001427 bool Cond;
1428 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001429 return false;
1430
Nuno Lopes527b5a62008-11-16 22:06:39 +00001431 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001432}
1433
Ken Dyck160146e2010-01-27 17:10:57 +00001434CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001435 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1436 // the result is the size of the referenced type."
1437 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1438 // result shall be the alignment of the referenced type."
1439 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1440 T = Ref->getPointeeType();
1441
Chris Lattner24aeeab2009-01-24 21:09:06 +00001442 // Get information about the alignment.
1443 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001444
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001445 // __alignof is defined to return the preferred alignment.
Ken Dyck160146e2010-01-27 17:10:57 +00001446 return CharUnits::fromQuantity(
1447 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001448}
1449
Ken Dyck160146e2010-01-27 17:10:57 +00001450CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattner68061312009-01-24 21:53:27 +00001451 E = E->IgnoreParens();
1452
1453 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001454 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001455 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001456 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1457 /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001458
Chris Lattner68061312009-01-24 21:53:27 +00001459 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001460 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1461 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001462
Chris Lattner24aeeab2009-01-24 21:09:06 +00001463 return GetAlignOfType(E->getType());
1464}
1465
1466
Sebastian Redl6f282892008-11-11 17:56:53 +00001467/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1468/// expression's type.
1469bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001470 // Handle alignof separately.
1471 if (!E->isSizeOf()) {
1472 if (E->isArgumentType())
Ken Dyck160146e2010-01-27 17:10:57 +00001473 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001474 else
Ken Dyck160146e2010-01-27 17:10:57 +00001475 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001476 }
Eli Friedman64004332009-03-23 04:38:34 +00001477
Sebastian Redl6f282892008-11-11 17:56:53 +00001478 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001479 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1480 // the result is the size of the referenced type."
1481 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1482 // result shall be the alignment of the referenced type."
1483 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1484 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001485
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001486 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1487 // extension.
1488 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1489 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001490
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001491 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001492 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001493 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001494
Chris Lattner24aeeab2009-01-24 21:09:06 +00001495 // Get information about the size.
Ken Dyck40775002010-01-11 17:06:35 +00001496 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001497}
1498
Douglas Gregor882211c2010-04-28 22:16:22 +00001499bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1500 CharUnits Result;
1501 unsigned n = E->getNumComponents();
1502 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1503 if (n == 0)
1504 return false;
1505 QualType CurrentType = E->getTypeSourceInfo()->getType();
1506 for (unsigned i = 0; i != n; ++i) {
1507 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1508 switch (ON.getKind()) {
1509 case OffsetOfExpr::OffsetOfNode::Array: {
1510 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1511 APSInt IdxResult;
1512 if (!EvaluateInteger(Idx, IdxResult, Info))
1513 return false;
1514 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1515 if (!AT)
1516 return false;
1517 CurrentType = AT->getElementType();
1518 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1519 Result += IdxResult.getSExtValue() * ElementSize;
1520 break;
1521 }
1522
1523 case OffsetOfExpr::OffsetOfNode::Field: {
1524 FieldDecl *MemberDecl = ON.getField();
1525 const RecordType *RT = CurrentType->getAs<RecordType>();
1526 if (!RT)
1527 return false;
1528 RecordDecl *RD = RT->getDecl();
1529 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1530 unsigned i = 0;
1531 // FIXME: It would be nice if we didn't have to loop here!
1532 for (RecordDecl::field_iterator Field = RD->field_begin(),
1533 FieldEnd = RD->field_end();
1534 Field != FieldEnd; (void)++Field, ++i) {
1535 if (*Field == MemberDecl)
1536 break;
1537 }
Douglas Gregord1702062010-04-29 00:18:15 +00001538 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1539 Result += CharUnits::fromQuantity(
1540 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor882211c2010-04-28 22:16:22 +00001541 CurrentType = MemberDecl->getType().getNonReferenceType();
1542 break;
1543 }
1544
1545 case OffsetOfExpr::OffsetOfNode::Identifier:
1546 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregord1702062010-04-29 00:18:15 +00001547 return false;
1548
1549 case OffsetOfExpr::OffsetOfNode::Base: {
1550 CXXBaseSpecifier *BaseSpec = ON.getBase();
1551 if (BaseSpec->isVirtual())
1552 return false;
1553
1554 // Find the layout of the class whose base we are looking into.
1555 const RecordType *RT = CurrentType->getAs<RecordType>();
1556 if (!RT)
1557 return false;
1558 RecordDecl *RD = RT->getDecl();
1559 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1560
1561 // Find the base class itself.
1562 CurrentType = BaseSpec->getType();
1563 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1564 if (!BaseRT)
1565 return false;
1566
1567 // Add the offset to the base.
1568 Result += CharUnits::fromQuantity(
1569 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1570 / Info.Ctx.getCharWidth());
1571 break;
1572 }
Douglas Gregor882211c2010-04-28 22:16:22 +00001573 }
1574 }
1575 return Success(Result.getQuantity(), E);
1576}
1577
Chris Lattnere13042c2008-07-11 19:10:17 +00001578bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCalle3027922010-08-25 11:45:40 +00001579 if (E->getOpcode() == UO_LNot) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001580 // LNot's operand isn't necessarily an integer, so we handle it specially.
1581 bool bres;
1582 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1583 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001584 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001585 }
1586
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001587 // Only handle integral operations...
Douglas Gregorb90df602010-06-16 00:17:44 +00001588 if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001589 return false;
1590
Chris Lattnercdf34e72008-07-11 22:52:41 +00001591 // Get the operand value into 'Result'.
1592 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001593 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001594
Chris Lattnerf09ad162008-07-11 22:15:16 +00001595 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001596 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001597 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1598 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001599 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCalle3027922010-08-25 11:45:40 +00001600 case UO_Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001601 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1602 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001603 return true;
John McCalle3027922010-08-25 11:45:40 +00001604 case UO_Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001605 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001606 return true;
John McCalle3027922010-08-25 11:45:40 +00001607 case UO_Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001608 if (!Result.isInt()) return false;
1609 return Success(-Result.getInt(), E);
John McCalle3027922010-08-25 11:45:40 +00001610 case UO_Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001611 if (!Result.isInt()) return false;
1612 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001613 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001614}
Mike Stump11289f42009-09-09 15:08:12 +00001615
Chris Lattner477c4be2008-07-12 01:15:53 +00001616/// HandleCast - This is used to evaluate implicit or explicit casts where the
1617/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001618bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001619 Expr *SubExpr = E->getSubExpr();
1620 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001621 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001622
Eli Friedman9a156e52008-11-12 09:44:48 +00001623 if (DestType->isBooleanType()) {
1624 bool BoolResult;
1625 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1626 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001627 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001628 }
1629
Anders Carlsson9c181652008-07-08 14:35:21 +00001630 // Handle simple integer->integer casts.
Douglas Gregorb90df602010-06-16 00:17:44 +00001631 if (SrcType->isIntegralOrEnumerationType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001632 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001633 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001634
Eli Friedman742421e2009-02-20 01:15:07 +00001635 if (!Result.isInt()) {
1636 // Only allow casts of lvalues if they are lossless.
1637 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1638 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001639
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001640 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001641 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Chris Lattner477c4be2008-07-12 01:15:53 +00001644 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001645 if (SrcType->isPointerType()) {
John McCall45d55e42010-05-07 21:00:08 +00001646 LValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001647 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001648 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001649
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001650 if (LV.getLValueBase()) {
1651 // Only allow based lvalue casts if they are lossless.
1652 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1653 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001654
John McCall45d55e42010-05-07 21:00:08 +00001655 LV.moveInto(Result);
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001656 return true;
1657 }
1658
Ken Dyck02990832010-01-15 12:37:54 +00001659 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1660 SrcType);
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001661 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001662 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001663
Eli Friedman742421e2009-02-20 01:15:07 +00001664 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1665 // This handles double-conversion cases, where there's both
1666 // an l-value promotion and an implicit conversion to int.
John McCall45d55e42010-05-07 21:00:08 +00001667 LValue LV;
Eli Friedman742421e2009-02-20 01:15:07 +00001668 if (!EvaluateLValue(SubExpr, LV, Info))
1669 return false;
1670
1671 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1672 return false;
1673
John McCall45d55e42010-05-07 21:00:08 +00001674 LV.moveInto(Result);
Eli Friedman742421e2009-02-20 01:15:07 +00001675 return true;
1676 }
1677
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001678 if (SrcType->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001679 ComplexValue C;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001680 if (!EvaluateComplex(SubExpr, C, Info))
1681 return false;
1682 if (C.isComplexFloat())
1683 return Success(HandleFloatToIntCast(DestType, SrcType,
1684 C.getComplexFloatReal(), Info.Ctx),
1685 E);
1686 else
1687 return Success(HandleIntToIntCast(DestType, SrcType,
1688 C.getComplexIntReal(), Info.Ctx), E);
1689 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001690 // FIXME: Handle vectors
1691
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001692 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001693 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001694
Eli Friedman24c01542008-08-22 00:06:13 +00001695 APFloat F(0.0);
1696 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001697 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001698
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001699 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001700}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001701
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001702bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1703 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001704 ComplexValue LV;
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001705 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1706 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1707 return Success(LV.getComplexIntReal(), E);
1708 }
1709
1710 return Visit(E->getSubExpr());
1711}
1712
Eli Friedman4e7a2412009-02-27 04:45:43 +00001713bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001714 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCall93d91dc2010-05-07 17:22:02 +00001715 ComplexValue LV;
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001716 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1717 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1718 return Success(LV.getComplexIntImag(), E);
1719 }
1720
Eli Friedman4e7a2412009-02-27 04:45:43 +00001721 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1722 Info.EvalResult.HasSideEffects = true;
1723 return Success(0, E);
1724}
1725
Chris Lattner05706e882008-07-11 18:11:29 +00001726//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001727// Float Evaluation
1728//===----------------------------------------------------------------------===//
1729
1730namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001731class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001732 : public StmtVisitor<FloatExprEvaluator, bool> {
1733 EvalInfo &Info;
1734 APFloat &Result;
1735public:
1736 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1737 : Info(info), Result(result) {}
1738
1739 bool VisitStmt(Stmt *S) {
1740 return false;
1741 }
1742
1743 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001744 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001745
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001746 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001747 bool VisitBinaryOperator(const BinaryOperator *E);
1748 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001749 bool VisitCastExpr(CastExpr *E);
Douglas Gregor747eb782010-07-08 06:14:04 +00001750 bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Eli Friedmanf3da3342009-12-04 02:12:53 +00001751 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001752
Eli Friedman449fe542009-03-23 04:56:01 +00001753 bool VisitChooseExpr(const ChooseExpr *E)
1754 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1755 bool VisitUnaryExtension(const UnaryOperator *E)
1756 { return Visit(E->getSubExpr()); }
John McCallb1fb0d32010-05-07 22:08:54 +00001757 bool VisitUnaryReal(const UnaryOperator *E);
1758 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001759
John McCallb1fb0d32010-05-07 22:08:54 +00001760 // FIXME: Missing: array subscript of vector, member of vector,
1761 // ImplicitValueInitExpr
Eli Friedman24c01542008-08-22 00:06:13 +00001762};
1763} // end anonymous namespace
1764
1765static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +00001766 assert(E->getType()->isRealFloatingType());
Eli Friedman24c01542008-08-22 00:06:13 +00001767 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1768}
1769
John McCall16291492010-02-28 13:00:19 +00001770static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1771 QualType ResultTy,
1772 const Expr *Arg,
1773 bool SNaN,
1774 llvm::APFloat &Result) {
1775 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1776 if (!S) return false;
1777
1778 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1779
1780 llvm::APInt fill;
1781
1782 // Treat empty strings as if they were zero.
1783 if (S->getString().empty())
1784 fill = llvm::APInt(32, 0);
1785 else if (S->getString().getAsInteger(0, fill))
1786 return false;
1787
1788 if (SNaN)
1789 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1790 else
1791 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1792 return true;
1793}
1794
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001795bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001796 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001797 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001798 case Builtin::BI__builtin_huge_val:
1799 case Builtin::BI__builtin_huge_valf:
1800 case Builtin::BI__builtin_huge_vall:
1801 case Builtin::BI__builtin_inf:
1802 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001803 case Builtin::BI__builtin_infl: {
1804 const llvm::fltSemantics &Sem =
1805 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001806 Result = llvm::APFloat::getInf(Sem);
1807 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001808 }
Mike Stump11289f42009-09-09 15:08:12 +00001809
John McCall16291492010-02-28 13:00:19 +00001810 case Builtin::BI__builtin_nans:
1811 case Builtin::BI__builtin_nansf:
1812 case Builtin::BI__builtin_nansl:
1813 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1814 true, Result);
1815
Chris Lattner0b7282e2008-10-06 06:31:58 +00001816 case Builtin::BI__builtin_nan:
1817 case Builtin::BI__builtin_nanf:
1818 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001819 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001820 // can't constant fold it.
John McCall16291492010-02-28 13:00:19 +00001821 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1822 false, Result);
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001823
1824 case Builtin::BI__builtin_fabs:
1825 case Builtin::BI__builtin_fabsf:
1826 case Builtin::BI__builtin_fabsl:
1827 if (!EvaluateFloat(E->getArg(0), Result, Info))
1828 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001829
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001830 if (Result.isNegative())
1831 Result.changeSign();
1832 return true;
1833
Mike Stump11289f42009-09-09 15:08:12 +00001834 case Builtin::BI__builtin_copysign:
1835 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001836 case Builtin::BI__builtin_copysignl: {
1837 APFloat RHS(0.);
1838 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1839 !EvaluateFloat(E->getArg(1), RHS, Info))
1840 return false;
1841 Result.copySign(RHS);
1842 return true;
1843 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001844 }
1845}
1846
John McCallb1fb0d32010-05-07 22:08:54 +00001847bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Eli Friedman95719532010-08-14 20:52:13 +00001848 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1849 ComplexValue CV;
1850 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1851 return false;
1852 Result = CV.FloatReal;
1853 return true;
1854 }
1855
1856 return Visit(E->getSubExpr());
John McCallb1fb0d32010-05-07 22:08:54 +00001857}
1858
1859bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman95719532010-08-14 20:52:13 +00001860 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1861 ComplexValue CV;
1862 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
1863 return false;
1864 Result = CV.FloatImag;
1865 return true;
1866 }
1867
1868 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1869 Info.EvalResult.HasSideEffects = true;
1870 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
1871 Result = llvm::APFloat::getZero(Sem);
John McCallb1fb0d32010-05-07 22:08:54 +00001872 return true;
1873}
1874
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001875bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCalle3027922010-08-25 11:45:40 +00001876 if (E->getOpcode() == UO_Deref)
Nuno Lopes0e33c682008-11-19 17:44:31 +00001877 return false;
1878
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001879 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1880 return false;
1881
1882 switch (E->getOpcode()) {
1883 default: return false;
John McCalle3027922010-08-25 11:45:40 +00001884 case UO_Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001885 return true;
John McCalle3027922010-08-25 11:45:40 +00001886 case UO_Minus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001887 Result.changeSign();
1888 return true;
1889 }
1890}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001891
Eli Friedman24c01542008-08-22 00:06:13 +00001892bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCalle3027922010-08-25 11:45:40 +00001893 if (E->getOpcode() == BO_Comma) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001894 if (!EvaluateFloat(E->getRHS(), Result, Info))
1895 return false;
1896
1897 // If we can't evaluate the LHS, it might have side effects;
1898 // conservatively mark it.
1899 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1900 Info.EvalResult.HasSideEffects = true;
1901
1902 return true;
1903 }
1904
Eli Friedman24c01542008-08-22 00:06:13 +00001905 // FIXME: Diagnostics? I really don't understand how the warnings
1906 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001907 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001908 if (!EvaluateFloat(E->getLHS(), Result, Info))
1909 return false;
1910 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1911 return false;
1912
1913 switch (E->getOpcode()) {
1914 default: return false;
John McCalle3027922010-08-25 11:45:40 +00001915 case BO_Mul:
Eli Friedman24c01542008-08-22 00:06:13 +00001916 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1917 return true;
John McCalle3027922010-08-25 11:45:40 +00001918 case BO_Add:
Eli Friedman24c01542008-08-22 00:06:13 +00001919 Result.add(RHS, APFloat::rmNearestTiesToEven);
1920 return true;
John McCalle3027922010-08-25 11:45:40 +00001921 case BO_Sub:
Eli Friedman24c01542008-08-22 00:06:13 +00001922 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1923 return true;
John McCalle3027922010-08-25 11:45:40 +00001924 case BO_Div:
Eli Friedman24c01542008-08-22 00:06:13 +00001925 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1926 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001927 }
1928}
1929
1930bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1931 Result = E->getValue();
1932 return true;
1933}
1934
Eli Friedman9a156e52008-11-12 09:44:48 +00001935bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1936 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001937
Douglas Gregorb90df602010-06-16 00:17:44 +00001938 if (SubExpr->getType()->isIntegralOrEnumerationType()) {
Eli Friedman9a156e52008-11-12 09:44:48 +00001939 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001940 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001941 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001942 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001943 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001944 return true;
1945 }
1946 if (SubExpr->getType()->isRealFloatingType()) {
1947 if (!Visit(SubExpr))
1948 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001949 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1950 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001951 return true;
1952 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001953 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001954
1955 return false;
1956}
1957
Douglas Gregor747eb782010-07-08 06:14:04 +00001958bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Eli Friedman9a156e52008-11-12 09:44:48 +00001959 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1960 return true;
1961}
1962
Eli Friedmanf3da3342009-12-04 02:12:53 +00001963bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1964 bool Cond;
1965 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1966 return false;
1967
1968 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1969}
1970
Eli Friedman24c01542008-08-22 00:06:13 +00001971//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001972// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001973//===----------------------------------------------------------------------===//
1974
1975namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001976class ComplexExprEvaluator
John McCall93d91dc2010-05-07 17:22:02 +00001977 : public StmtVisitor<ComplexExprEvaluator, bool> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001978 EvalInfo &Info;
John McCall93d91dc2010-05-07 17:22:02 +00001979 ComplexValue &Result;
Mike Stump11289f42009-09-09 15:08:12 +00001980
Anders Carlsson537969c2008-11-16 20:27:53 +00001981public:
John McCall93d91dc2010-05-07 17:22:02 +00001982 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
1983 : Info(info), Result(Result) {}
Mike Stump11289f42009-09-09 15:08:12 +00001984
Anders Carlsson537969c2008-11-16 20:27:53 +00001985 //===--------------------------------------------------------------------===//
1986 // Visitor Methods
1987 //===--------------------------------------------------------------------===//
1988
John McCall93d91dc2010-05-07 17:22:02 +00001989 bool VisitStmt(Stmt *S) {
1990 return false;
Anders Carlsson537969c2008-11-16 20:27:53 +00001991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
John McCall93d91dc2010-05-07 17:22:02 +00001993 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson537969c2008-11-16 20:27:53 +00001994
Eli Friedmanc3e9df32010-08-16 23:27:44 +00001995 bool VisitImaginaryLiteral(ImaginaryLiteral *E);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001996
Eli Friedmanc3e9df32010-08-16 23:27:44 +00001997 bool VisitCastExpr(CastExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +00001998
John McCall93d91dc2010-05-07 17:22:02 +00001999 bool VisitBinaryOperator(const BinaryOperator *E);
2000 bool VisitChooseExpr(const ChooseExpr *E)
Eli Friedman449fe542009-03-23 04:56:01 +00002001 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
John McCall93d91dc2010-05-07 17:22:02 +00002002 bool VisitUnaryExtension(const UnaryOperator *E)
Eli Friedman449fe542009-03-23 04:56:01 +00002003 { return Visit(E->getSubExpr()); }
2004 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00002005 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00002006};
2007} // end anonymous namespace
2008
John McCall93d91dc2010-05-07 17:22:02 +00002009static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2010 EvalInfo &Info) {
John McCallf0c4f352010-05-07 05:46:35 +00002011 assert(E->getType()->isAnyComplexType());
John McCall93d91dc2010-05-07 17:22:02 +00002012 return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson537969c2008-11-16 20:27:53 +00002013}
2014
Eli Friedmanc3e9df32010-08-16 23:27:44 +00002015bool ComplexExprEvaluator::VisitImaginaryLiteral(ImaginaryLiteral *E) {
2016 Expr* SubExpr = E->getSubExpr();
2017
2018 if (SubExpr->getType()->isRealFloatingType()) {
2019 Result.makeComplexFloat();
2020 APFloat &Imag = Result.FloatImag;
2021 if (!EvaluateFloat(SubExpr, Imag, Info))
2022 return false;
2023
2024 Result.FloatReal = APFloat(Imag.getSemantics());
2025 return true;
2026 } else {
2027 assert(SubExpr->getType()->isIntegerType() &&
2028 "Unexpected imaginary literal.");
2029
2030 Result.makeComplexInt();
2031 APSInt &Imag = Result.IntImag;
2032 if (!EvaluateInteger(SubExpr, Imag, Info))
2033 return false;
2034
2035 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2036 return true;
2037 }
2038}
2039
2040bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) {
2041 Expr* SubExpr = E->getSubExpr();
2042 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
2043 QualType SubType = SubExpr->getType();
2044
2045 if (SubType->isRealFloatingType()) {
2046 APFloat &Real = Result.FloatReal;
2047 if (!EvaluateFloat(SubExpr, Real, Info))
2048 return false;
2049
2050 if (EltType->isRealFloatingType()) {
2051 Result.makeComplexFloat();
2052 Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx);
2053 Result.FloatImag = APFloat(Real.getSemantics());
2054 return true;
2055 } else {
2056 Result.makeComplexInt();
2057 Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx);
2058 Result.IntImag = APSInt(Result.IntReal.getBitWidth(),
2059 !Result.IntReal.isSigned());
2060 return true;
2061 }
2062 } else if (SubType->isIntegerType()) {
2063 APSInt &Real = Result.IntReal;
2064 if (!EvaluateInteger(SubExpr, Real, Info))
2065 return false;
2066
2067 if (EltType->isRealFloatingType()) {
2068 Result.makeComplexFloat();
2069 Result.FloatReal
2070 = HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx);
2071 Result.FloatImag = APFloat(Result.FloatReal.getSemantics());
2072 return true;
2073 } else {
2074 Result.makeComplexInt();
2075 Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx);
2076 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2077 return true;
2078 }
2079 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
2080 if (!Visit(SubExpr))
2081 return false;
2082
2083 QualType SrcType = CT->getElementType();
2084
2085 if (Result.isComplexFloat()) {
2086 if (EltType->isRealFloatingType()) {
2087 Result.makeComplexFloat();
2088 Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType,
2089 Result.FloatReal,
2090 Info.Ctx);
2091 Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType,
2092 Result.FloatImag,
2093 Info.Ctx);
2094 return true;
2095 } else {
2096 Result.makeComplexInt();
2097 Result.IntReal = HandleFloatToIntCast(EltType, SrcType,
2098 Result.FloatReal,
2099 Info.Ctx);
2100 Result.IntImag = HandleFloatToIntCast(EltType, SrcType,
2101 Result.FloatImag,
2102 Info.Ctx);
2103 return true;
2104 }
2105 } else {
2106 assert(Result.isComplexInt() && "Invalid evaluate result.");
2107 if (EltType->isRealFloatingType()) {
2108 Result.makeComplexFloat();
2109 Result.FloatReal = HandleIntToFloatCast(EltType, SrcType,
2110 Result.IntReal,
2111 Info.Ctx);
2112 Result.FloatImag = HandleIntToFloatCast(EltType, SrcType,
2113 Result.IntImag,
2114 Info.Ctx);
2115 return true;
2116 } else {
2117 Result.makeComplexInt();
2118 Result.IntReal = HandleIntToIntCast(EltType, SrcType,
2119 Result.IntReal,
2120 Info.Ctx);
2121 Result.IntImag = HandleIntToIntCast(EltType, SrcType,
2122 Result.IntImag,
2123 Info.Ctx);
2124 return true;
2125 }
2126 }
2127 }
2128
2129 // FIXME: Handle more casts.
2130 return false;
2131}
2132
John McCall93d91dc2010-05-07 17:22:02 +00002133bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
2134 if (!Visit(E->getLHS()))
2135 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002136
John McCall93d91dc2010-05-07 17:22:02 +00002137 ComplexValue RHS;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002138 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCall93d91dc2010-05-07 17:22:02 +00002139 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002140
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002141 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2142 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002143 switch (E->getOpcode()) {
John McCall93d91dc2010-05-07 17:22:02 +00002144 default: return false;
John McCalle3027922010-08-25 11:45:40 +00002145 case BO_Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002146 if (Result.isComplexFloat()) {
2147 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2148 APFloat::rmNearestTiesToEven);
2149 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2150 APFloat::rmNearestTiesToEven);
2151 } else {
2152 Result.getComplexIntReal() += RHS.getComplexIntReal();
2153 Result.getComplexIntImag() += RHS.getComplexIntImag();
2154 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002155 break;
John McCalle3027922010-08-25 11:45:40 +00002156 case BO_Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002157 if (Result.isComplexFloat()) {
2158 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2159 APFloat::rmNearestTiesToEven);
2160 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2161 APFloat::rmNearestTiesToEven);
2162 } else {
2163 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2164 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2165 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002166 break;
John McCalle3027922010-08-25 11:45:40 +00002167 case BO_Mul:
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002168 if (Result.isComplexFloat()) {
John McCall93d91dc2010-05-07 17:22:02 +00002169 ComplexValue LHS = Result;
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002170 APFloat &LHS_r = LHS.getComplexFloatReal();
2171 APFloat &LHS_i = LHS.getComplexFloatImag();
2172 APFloat &RHS_r = RHS.getComplexFloatReal();
2173 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00002174
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002175 APFloat Tmp = LHS_r;
2176 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2177 Result.getComplexFloatReal() = Tmp;
2178 Tmp = LHS_i;
2179 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2180 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2181
2182 Tmp = LHS_r;
2183 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2184 Result.getComplexFloatImag() = Tmp;
2185 Tmp = LHS_i;
2186 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2187 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2188 } else {
John McCall93d91dc2010-05-07 17:22:02 +00002189 ComplexValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00002190 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002191 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2192 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00002193 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00002194 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2195 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2196 }
2197 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002198 }
2199
John McCall93d91dc2010-05-07 17:22:02 +00002200 return true;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00002201}
2202
Anders Carlsson537969c2008-11-16 20:27:53 +00002203//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00002204// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00002205//===----------------------------------------------------------------------===//
2206
John McCall95007602010-05-10 23:27:23 +00002207/// Evaluate - Return true if this is a constant which we can fold using
2208/// any crazy technique (that has nothing to do with language standards) that
2209/// we want to. If this function returns true, it returns the folded constant
2210/// in Result.
2211bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2212 const Expr *E = this;
2213 EvalInfo Info(Ctx, Result);
John McCall45d55e42010-05-07 21:00:08 +00002214 if (E->getType()->isVectorType()) {
2215 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002216 return false;
John McCall45d55e42010-05-07 21:00:08 +00002217 } else if (E->getType()->isIntegerType()) {
2218 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002219 return false;
John McCall11086fc2010-07-07 05:08:32 +00002220 if (Result.Val.isLValue() && !IsGlobalLValue(Result.Val.getLValueBase()))
2221 return false;
John McCall45d55e42010-05-07 21:00:08 +00002222 } else if (E->getType()->hasPointerRepresentation()) {
2223 LValue LV;
2224 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002225 return false;
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002226 if (!IsGlobalLValue(LV.Base))
John McCall95007602010-05-10 23:27:23 +00002227 return false;
John McCall45d55e42010-05-07 21:00:08 +00002228 LV.moveInto(Info.EvalResult.Val);
2229 } else if (E->getType()->isRealFloatingType()) {
2230 llvm::APFloat F(0.0);
2231 if (!EvaluateFloat(E, F, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002232 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002233
John McCall45d55e42010-05-07 21:00:08 +00002234 Info.EvalResult.Val = APValue(F);
2235 } else if (E->getType()->isAnyComplexType()) {
2236 ComplexValue C;
2237 if (!EvaluateComplex(E, C, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002238 return false;
John McCall45d55e42010-05-07 21:00:08 +00002239 C.moveInto(Info.EvalResult.Val);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00002240 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00002241 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00002242
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00002243 return true;
2244}
2245
John McCall1be1c632010-01-05 23:42:56 +00002246bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2247 EvalResult Scratch;
2248 EvalInfo Info(Ctx, Scratch);
2249
2250 return HandleConversionToBool(this, Result, Info);
2251}
2252
Anders Carlsson43168122009-04-10 04:54:13 +00002253bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2254 EvalInfo Info(Ctx, Result);
2255
John McCall45d55e42010-05-07 21:00:08 +00002256 LValue LV;
John McCall95007602010-05-10 23:27:23 +00002257 if (EvaluateLValue(this, LV, Info) &&
2258 !Result.HasSideEffects &&
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002259 IsGlobalLValue(LV.Base)) {
2260 LV.moveInto(Result.Val);
2261 return true;
2262 }
2263 return false;
2264}
2265
2266bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2267 EvalInfo Info(Ctx, Result);
2268
2269 LValue LV;
2270 if (EvaluateLValue(this, LV, Info)) {
John McCall45d55e42010-05-07 21:00:08 +00002271 LV.moveInto(Result.Val);
2272 return true;
2273 }
2274 return false;
Eli Friedman7d45c482009-09-13 10:17:44 +00002275}
2276
Chris Lattner67d7b922008-11-16 21:24:15 +00002277/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00002278/// folded, but discard the result.
2279bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00002280 EvalResult Result;
2281 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00002282}
Anders Carlsson59689ed2008-11-22 21:04:56 +00002283
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00002284bool Expr::HasSideEffects(ASTContext &Ctx) const {
2285 Expr::EvalResult Result;
2286 EvalInfo Info(Ctx, Result);
2287 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2288}
2289
Anders Carlsson59689ed2008-11-22 21:04:56 +00002290APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002291 EvalResult EvalResult;
2292 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00002293 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00002294 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002295 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00002296
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002297 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00002298}
John McCall864e3962010-05-07 05:32:02 +00002299
Abramo Bagnaraf8199452010-05-14 17:07:14 +00002300 bool Expr::EvalResult::isGlobalLValue() const {
2301 assert(Val.isLValue());
2302 return IsGlobalLValue(Val.getLValueBase());
2303 }
2304
2305
John McCall864e3962010-05-07 05:32:02 +00002306/// isIntegerConstantExpr - this recursive routine will test if an expression is
2307/// an integer constant expression.
2308
2309/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2310/// comma, etc
2311///
2312/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2313/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2314/// cast+dereference.
2315
2316// CheckICE - This function does the fundamental ICE checking: the returned
2317// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2318// Note that to reduce code duplication, this helper does no evaluation
2319// itself; the caller checks whether the expression is evaluatable, and
2320// in the rare cases where CheckICE actually cares about the evaluated
2321// value, it calls into Evalute.
2322//
2323// Meanings of Val:
2324// 0: This expression is an ICE if it can be evaluated by Evaluate.
2325// 1: This expression is not an ICE, but if it isn't evaluated, it's
2326// a legal subexpression for an ICE. This return value is used to handle
2327// the comma operator in C99 mode.
2328// 2: This expression is not an ICE, and is not a legal subexpression for one.
2329
Dan Gohman28ade552010-07-26 21:25:24 +00002330namespace {
2331
John McCall864e3962010-05-07 05:32:02 +00002332struct ICEDiag {
2333 unsigned Val;
2334 SourceLocation Loc;
2335
2336 public:
2337 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2338 ICEDiag() : Val(0) {}
2339};
2340
Dan Gohman28ade552010-07-26 21:25:24 +00002341}
2342
2343static ICEDiag NoDiag() { return ICEDiag(); }
John McCall864e3962010-05-07 05:32:02 +00002344
2345static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2346 Expr::EvalResult EVResult;
2347 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2348 !EVResult.Val.isInt()) {
2349 return ICEDiag(2, E->getLocStart());
2350 }
2351 return NoDiag();
2352}
2353
2354static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2355 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Douglas Gregorb90df602010-06-16 00:17:44 +00002356 if (!E->getType()->isIntegralOrEnumerationType()) {
John McCall864e3962010-05-07 05:32:02 +00002357 return ICEDiag(2, E->getLocStart());
2358 }
2359
2360 switch (E->getStmtClass()) {
2361#define STMT(Node, Base) case Expr::Node##Class:
2362#define EXPR(Node, Base)
2363#include "clang/AST/StmtNodes.inc"
2364 case Expr::PredefinedExprClass:
2365 case Expr::FloatingLiteralClass:
2366 case Expr::ImaginaryLiteralClass:
2367 case Expr::StringLiteralClass:
2368 case Expr::ArraySubscriptExprClass:
2369 case Expr::MemberExprClass:
2370 case Expr::CompoundAssignOperatorClass:
2371 case Expr::CompoundLiteralExprClass:
2372 case Expr::ExtVectorElementExprClass:
2373 case Expr::InitListExprClass:
2374 case Expr::DesignatedInitExprClass:
2375 case Expr::ImplicitValueInitExprClass:
2376 case Expr::ParenListExprClass:
2377 case Expr::VAArgExprClass:
2378 case Expr::AddrLabelExprClass:
2379 case Expr::StmtExprClass:
2380 case Expr::CXXMemberCallExprClass:
2381 case Expr::CXXDynamicCastExprClass:
2382 case Expr::CXXTypeidExprClass:
2383 case Expr::CXXNullPtrLiteralExprClass:
2384 case Expr::CXXThisExprClass:
2385 case Expr::CXXThrowExprClass:
2386 case Expr::CXXNewExprClass:
2387 case Expr::CXXDeleteExprClass:
2388 case Expr::CXXPseudoDestructorExprClass:
2389 case Expr::UnresolvedLookupExprClass:
2390 case Expr::DependentScopeDeclRefExprClass:
2391 case Expr::CXXConstructExprClass:
2392 case Expr::CXXBindTemporaryExprClass:
John McCall864e3962010-05-07 05:32:02 +00002393 case Expr::CXXExprWithTemporariesClass:
2394 case Expr::CXXTemporaryObjectExprClass:
2395 case Expr::CXXUnresolvedConstructExprClass:
2396 case Expr::CXXDependentScopeMemberExprClass:
2397 case Expr::UnresolvedMemberExprClass:
2398 case Expr::ObjCStringLiteralClass:
2399 case Expr::ObjCEncodeExprClass:
2400 case Expr::ObjCMessageExprClass:
2401 case Expr::ObjCSelectorExprClass:
2402 case Expr::ObjCProtocolExprClass:
2403 case Expr::ObjCIvarRefExprClass:
2404 case Expr::ObjCPropertyRefExprClass:
2405 case Expr::ObjCImplicitSetterGetterRefExprClass:
2406 case Expr::ObjCSuperExprClass:
2407 case Expr::ObjCIsaExprClass:
2408 case Expr::ShuffleVectorExprClass:
2409 case Expr::BlockExprClass:
2410 case Expr::BlockDeclRefExprClass:
2411 case Expr::NoStmtClass:
2412 return ICEDiag(2, E->getLocStart());
2413
2414 case Expr::GNUNullExprClass:
2415 // GCC considers the GNU __null value to be an integral constant expression.
2416 return NoDiag();
2417
2418 case Expr::ParenExprClass:
2419 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
2420 case Expr::IntegerLiteralClass:
2421 case Expr::CharacterLiteralClass:
2422 case Expr::CXXBoolLiteralExprClass:
Douglas Gregor747eb782010-07-08 06:14:04 +00002423 case Expr::CXXScalarValueInitExprClass:
John McCall864e3962010-05-07 05:32:02 +00002424 case Expr::TypesCompatibleExprClass:
2425 case Expr::UnaryTypeTraitExprClass:
2426 return NoDiag();
2427 case Expr::CallExprClass:
Alexis Hunt3b791862010-08-30 17:47:05 +00002428 case Expr::CXXOperatorCallExprClass: {
John McCall864e3962010-05-07 05:32:02 +00002429 const CallExpr *CE = cast<CallExpr>(E);
2430 if (CE->isBuiltinCall(Ctx))
2431 return CheckEvalInICE(E, Ctx);
2432 return ICEDiag(2, E->getLocStart());
2433 }
2434 case Expr::DeclRefExprClass:
2435 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2436 return NoDiag();
2437 if (Ctx.getLangOptions().CPlusPlus &&
2438 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2439 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2440
2441 // Parameter variables are never constants. Without this check,
2442 // getAnyInitializer() can find a default argument, which leads
2443 // to chaos.
2444 if (isa<ParmVarDecl>(D))
2445 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2446
2447 // C++ 7.1.5.1p2
2448 // A variable of non-volatile const-qualified integral or enumeration
2449 // type initialized by an ICE can be used in ICEs.
2450 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2451 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2452 if (Quals.hasVolatile() || !Quals.hasConst())
2453 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2454
2455 // Look for a declaration of this variable that has an initializer.
2456 const VarDecl *ID = 0;
2457 const Expr *Init = Dcl->getAnyInitializer(ID);
2458 if (Init) {
2459 if (ID->isInitKnownICE()) {
2460 // We have already checked whether this subexpression is an
2461 // integral constant expression.
2462 if (ID->isInitICE())
2463 return NoDiag();
2464 else
2465 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2466 }
2467
2468 // It's an ICE whether or not the definition we found is
2469 // out-of-line. See DR 721 and the discussion in Clang PR
2470 // 6206 for details.
2471
2472 if (Dcl->isCheckingICE()) {
2473 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2474 }
2475
2476 Dcl->setCheckingICE();
2477 ICEDiag Result = CheckICE(Init, Ctx);
2478 // Cache the result of the ICE test.
2479 Dcl->setInitKnownICE(Result.Val == 0);
2480 return Result;
2481 }
2482 }
2483 }
2484 return ICEDiag(2, E->getLocStart());
2485 case Expr::UnaryOperatorClass: {
2486 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2487 switch (Exp->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002488 case UO_PostInc:
2489 case UO_PostDec:
2490 case UO_PreInc:
2491 case UO_PreDec:
2492 case UO_AddrOf:
2493 case UO_Deref:
John McCall864e3962010-05-07 05:32:02 +00002494 return ICEDiag(2, E->getLocStart());
John McCalle3027922010-08-25 11:45:40 +00002495 case UO_Extension:
2496 case UO_LNot:
2497 case UO_Plus:
2498 case UO_Minus:
2499 case UO_Not:
2500 case UO_Real:
2501 case UO_Imag:
John McCall864e3962010-05-07 05:32:02 +00002502 return CheckICE(Exp->getSubExpr(), Ctx);
John McCall864e3962010-05-07 05:32:02 +00002503 }
2504
2505 // OffsetOf falls through here.
2506 }
2507 case Expr::OffsetOfExprClass: {
2508 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2509 // Evaluate matches the proposed gcc behavior for cases like
2510 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2511 // compliance: we should warn earlier for offsetof expressions with
2512 // array subscripts that aren't ICEs, and if the array subscripts
2513 // are ICEs, the value of the offsetof must be an integer constant.
2514 return CheckEvalInICE(E, Ctx);
2515 }
2516 case Expr::SizeOfAlignOfExprClass: {
2517 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
2518 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
2519 return ICEDiag(2, E->getLocStart());
2520 return NoDiag();
2521 }
2522 case Expr::BinaryOperatorClass: {
2523 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2524 switch (Exp->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002525 case BO_PtrMemD:
2526 case BO_PtrMemI:
2527 case BO_Assign:
2528 case BO_MulAssign:
2529 case BO_DivAssign:
2530 case BO_RemAssign:
2531 case BO_AddAssign:
2532 case BO_SubAssign:
2533 case BO_ShlAssign:
2534 case BO_ShrAssign:
2535 case BO_AndAssign:
2536 case BO_XorAssign:
2537 case BO_OrAssign:
John McCall864e3962010-05-07 05:32:02 +00002538 return ICEDiag(2, E->getLocStart());
2539
John McCalle3027922010-08-25 11:45:40 +00002540 case BO_Mul:
2541 case BO_Div:
2542 case BO_Rem:
2543 case BO_Add:
2544 case BO_Sub:
2545 case BO_Shl:
2546 case BO_Shr:
2547 case BO_LT:
2548 case BO_GT:
2549 case BO_LE:
2550 case BO_GE:
2551 case BO_EQ:
2552 case BO_NE:
2553 case BO_And:
2554 case BO_Xor:
2555 case BO_Or:
2556 case BO_Comma: {
John McCall864e3962010-05-07 05:32:02 +00002557 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2558 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
John McCalle3027922010-08-25 11:45:40 +00002559 if (Exp->getOpcode() == BO_Div ||
2560 Exp->getOpcode() == BO_Rem) {
John McCall864e3962010-05-07 05:32:02 +00002561 // Evaluate gives an error for undefined Div/Rem, so make sure
2562 // we don't evaluate one.
2563 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
2564 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2565 if (REval == 0)
2566 return ICEDiag(1, E->getLocStart());
2567 if (REval.isSigned() && REval.isAllOnesValue()) {
2568 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2569 if (LEval.isMinSignedValue())
2570 return ICEDiag(1, E->getLocStart());
2571 }
2572 }
2573 }
John McCalle3027922010-08-25 11:45:40 +00002574 if (Exp->getOpcode() == BO_Comma) {
John McCall864e3962010-05-07 05:32:02 +00002575 if (Ctx.getLangOptions().C99) {
2576 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2577 // if it isn't evaluated.
2578 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2579 return ICEDiag(1, E->getLocStart());
2580 } else {
2581 // In both C89 and C++, commas in ICEs are illegal.
2582 return ICEDiag(2, E->getLocStart());
2583 }
2584 }
2585 if (LHSResult.Val >= RHSResult.Val)
2586 return LHSResult;
2587 return RHSResult;
2588 }
John McCalle3027922010-08-25 11:45:40 +00002589 case BO_LAnd:
2590 case BO_LOr: {
John McCall864e3962010-05-07 05:32:02 +00002591 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2592 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2593 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2594 // Rare case where the RHS has a comma "side-effect"; we need
2595 // to actually check the condition to see whether the side
2596 // with the comma is evaluated.
John McCalle3027922010-08-25 11:45:40 +00002597 if ((Exp->getOpcode() == BO_LAnd) !=
John McCall864e3962010-05-07 05:32:02 +00002598 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2599 return RHSResult;
2600 return NoDiag();
2601 }
2602
2603 if (LHSResult.Val >= RHSResult.Val)
2604 return LHSResult;
2605 return RHSResult;
2606 }
2607 }
2608 }
2609 case Expr::ImplicitCastExprClass:
2610 case Expr::CStyleCastExprClass:
2611 case Expr::CXXFunctionalCastExprClass:
2612 case Expr::CXXStaticCastExprClass:
2613 case Expr::CXXReinterpretCastExprClass:
2614 case Expr::CXXConstCastExprClass: {
2615 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
Douglas Gregorb90df602010-06-16 00:17:44 +00002616 if (SubExpr->getType()->isIntegralOrEnumerationType())
John McCall864e3962010-05-07 05:32:02 +00002617 return CheckICE(SubExpr, Ctx);
2618 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2619 return NoDiag();
2620 return ICEDiag(2, E->getLocStart());
2621 }
2622 case Expr::ConditionalOperatorClass: {
2623 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
2624 // If the condition (ignoring parens) is a __builtin_constant_p call,
2625 // then only the true side is actually considered in an integer constant
2626 // expression, and it is fully evaluated. This is an important GNU
2627 // extension. See GCC PR38377 for discussion.
2628 if (const CallExpr *CallCE
2629 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
2630 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
2631 Expr::EvalResult EVResult;
2632 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2633 !EVResult.Val.isInt()) {
2634 return ICEDiag(2, E->getLocStart());
2635 }
2636 return NoDiag();
2637 }
2638 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2639 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2640 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2641 if (CondResult.Val == 2)
2642 return CondResult;
2643 if (TrueResult.Val == 2)
2644 return TrueResult;
2645 if (FalseResult.Val == 2)
2646 return FalseResult;
2647 if (CondResult.Val == 1)
2648 return CondResult;
2649 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2650 return NoDiag();
2651 // Rare case where the diagnostics depend on which side is evaluated
2652 // Note that if we get here, CondResult is 0, and at least one of
2653 // TrueResult and FalseResult is non-zero.
2654 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
2655 return FalseResult;
2656 }
2657 return TrueResult;
2658 }
2659 case Expr::CXXDefaultArgExprClass:
2660 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
2661 case Expr::ChooseExprClass: {
2662 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
2663 }
2664 }
2665
2666 // Silence a GCC warning
2667 return ICEDiag(2, E->getLocStart());
2668}
2669
2670bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2671 SourceLocation *Loc, bool isEvaluated) const {
2672 ICEDiag d = CheckICE(this, Ctx);
2673 if (d.Val != 0) {
2674 if (Loc) *Loc = d.Loc;
2675 return false;
2676 }
2677 EvalResult EvalResult;
2678 if (!Evaluate(EvalResult, Ctx))
2679 llvm_unreachable("ICE cannot be evaluated!");
2680 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2681 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
2682 Result = EvalResult.Val.getInt();
2683 return true;
2684}