blob: c1a42d88fffa8287e4b8db524597b41b79d10834 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
45struct EvalInfo {
46 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson54da0492008-11-30 16:38:33 +000048 /// EvalResult - Contains information about the evaluation.
49 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000050
Eli Friedmanb2f295c2009-09-13 10:17:44 +000051 /// AnyLValue - Stack based LValue results are not discarded.
52 bool AnyLValue;
53
54 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
55 bool anylvalue = false)
56 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000057};
58
59
Eli Friedman4efaa272008-11-12 09:44:48 +000060static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000061static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
62static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +000063static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
64 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000065static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000066static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000067
68//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000069// Misc utilities
70//===----------------------------------------------------------------------===//
71
Eli Friedman5bc86102009-06-14 02:17:33 +000072static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
73 // FIXME: Is this accurate for all kinds of bases? If not, what would
74 // the check look like?
Ken Dycka7305832010-01-15 12:37:54 +000075 Result = Value.getLValueBase() || !Value.getLValueOffset().isZero();
Eli Friedman5bc86102009-06-14 02:17:33 +000076 return true;
77}
78
John McCallcd7a4452010-01-05 23:42:56 +000079static bool HandleConversionToBool(const Expr* E, bool& Result,
80 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +000081 if (E->getType()->isIntegralType()) {
82 APSInt IntResult;
83 if (!EvaluateInteger(E, IntResult, Info))
84 return false;
85 Result = IntResult != 0;
86 return true;
87 } else if (E->getType()->isRealFloatingType()) {
88 APFloat FloatResult(0.0);
89 if (!EvaluateFloat(E, FloatResult, Info))
90 return false;
91 Result = !FloatResult.isZero();
92 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000093 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000094 APValue PointerResult;
95 if (!EvaluatePointer(E, PointerResult, Info))
96 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000097 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000098 } else if (E->getType()->isAnyComplexType()) {
99 APValue ComplexResult;
100 if (!EvaluateComplex(E, ComplexResult, Info))
101 return false;
102 if (ComplexResult.isComplexFloat()) {
103 Result = !ComplexResult.getComplexFloatReal().isZero() ||
104 !ComplexResult.getComplexFloatImag().isZero();
105 } else {
106 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
107 ComplexResult.getComplexIntImag().getBoolValue();
108 }
109 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000110 }
111
112 return false;
113}
114
Mike Stump1eb44332009-09-09 15:08:12 +0000115static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000116 APFloat &Value, ASTContext &Ctx) {
117 unsigned DestWidth = Ctx.getIntWidth(DestType);
118 // Determine whether we are converting to unsigned or signed.
119 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000121 // FIXME: Warning for overflow.
122 uint64_t Space[4];
123 bool ignored;
124 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
125 llvm::APFloat::rmTowardZero, &ignored);
126 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
127}
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000130 APFloat &Value, ASTContext &Ctx) {
131 bool ignored;
132 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000133 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000134 APFloat::rmNearestTiesToEven, &ignored);
135 return Result;
136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000139 APSInt &Value, ASTContext &Ctx) {
140 unsigned DestWidth = Ctx.getIntWidth(DestType);
141 APSInt Result = Value;
142 // Figure out if this is a truncate, extend or noop cast.
143 // If the input is signed, do a sign extend, noop, or truncate.
144 Result.extOrTrunc(DestWidth);
145 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
146 return Result;
147}
148
Mike Stump1eb44332009-09-09 15:08:12 +0000149static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000150 APSInt &Value, ASTContext &Ctx) {
151
152 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
153 Result.convertFromAPInt(Value, Value.isSigned(),
154 APFloat::rmNearestTiesToEven);
155 return Result;
156}
157
Mike Stumpc4c90452009-10-27 22:09:17 +0000158namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000159class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000160 : public StmtVisitor<HasSideEffect, bool> {
161 EvalInfo &Info;
162public:
163
164 HasSideEffect(EvalInfo &info) : Info(info) {}
165
166 // Unhandled nodes conservatively default to having side effects.
167 bool VisitStmt(Stmt *S) {
168 return true;
169 }
170
171 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
172 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000173 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000174 return true;
175 return false;
176 }
177 // We don't want to evaluate BlockExprs multiple times, as they generate
178 // a ton of code.
179 bool VisitBlockExpr(BlockExpr *E) { return true; }
180 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
181 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
182 { return Visit(E->getInitializer()); }
183 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
184 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
185 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
186 bool VisitStringLiteral(StringLiteral *E) { return false; }
187 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
188 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
189 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000190 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000191 bool VisitChooseExpr(ChooseExpr *E)
192 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
193 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
194 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000195 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000196 bool VisitBinaryOperator(BinaryOperator *E)
197 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000198 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
199 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
200 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
201 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
202 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000203 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000204 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000205 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000206 }
207 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000208
209 // Has side effects if any element does.
210 bool VisitInitListExpr(InitListExpr *E) {
211 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
212 if (Visit(E->getInit(i))) return true;
213 return false;
214 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000215};
216
Mike Stumpc4c90452009-10-27 22:09:17 +0000217} // end anonymous namespace
218
Eli Friedman4efaa272008-11-12 09:44:48 +0000219//===----------------------------------------------------------------------===//
220// LValue Evaluation
221//===----------------------------------------------------------------------===//
222namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000223class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000224 : public StmtVisitor<LValueExprEvaluator, APValue> {
225 EvalInfo &Info;
226public:
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
229
230 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000231 return APValue();
232 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000233
Eli Friedman4efaa272008-11-12 09:44:48 +0000234 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000235 APValue VisitDeclRefExpr(DeclRefExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000236 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000237 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
238 APValue VisitMemberExpr(MemberExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000239 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); }
240 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000241 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000242 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000243 APValue VisitUnaryExtension(const UnaryOperator *E)
244 { return Visit(E->getSubExpr()); }
245 APValue VisitChooseExpr(const ChooseExpr *E)
246 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000247
248 APValue VisitCastExpr(CastExpr *E) {
249 switch (E->getCastKind()) {
250 default:
251 return APValue();
252
253 case CastExpr::CK_NoOp:
254 return Visit(E->getSubExpr());
255 }
256 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000257 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000258};
259} // end anonymous namespace
260
261static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
262 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
263 return Result.isLValue();
264}
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000267 if (isa<FunctionDecl>(E->getDecl())) {
Ken Dycka7305832010-01-15 12:37:54 +0000268 return APValue(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000269 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000270 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000271 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000272 if (!VD->getType()->isReferenceType())
Ken Dycka7305832010-01-15 12:37:54 +0000273 return APValue(E);
Eli Friedmand933a012009-08-29 19:09:59 +0000274 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000275 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000276 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000277 }
278
279 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000280}
281
Eli Friedman4efaa272008-11-12 09:44:48 +0000282APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000283 if (!Info.AnyLValue && !E->isFileScope())
284 return APValue();
Ken Dycka7305832010-01-15 12:37:54 +0000285 return APValue(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000286}
287
288APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
289 APValue result;
290 QualType Ty;
291 if (E->isArrow()) {
292 if (!EvaluatePointer(E->getBase(), result, Info))
293 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000294 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000295 } else {
296 result = Visit(E->getBase());
297 if (result.isUninit())
298 return APValue();
299 Ty = E->getBase()->getType();
300 }
301
Ted Kremenek6217b802009-07-29 21:53:49 +0000302 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000303 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000304
305 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
306 if (!FD) // FIXME: deal with other kinds of member expressions
307 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000308
309 if (FD->getType()->isReferenceType())
310 return APValue();
311
Eli Friedman4efaa272008-11-12 09:44:48 +0000312 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000313 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000314 for (RecordDecl::field_iterator Field = RD->field_begin(),
315 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000316 Field != FieldEnd; (void)++Field, ++i) {
317 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000318 break;
319 }
320
321 result.setLValue(result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000322 result.getLValueOffset() +
323 CharUnits::fromQuantity(RL.getFieldOffset(i) / 8));
Eli Friedman4efaa272008-11-12 09:44:48 +0000324
325 return result;
326}
327
Mike Stump1eb44332009-09-09 15:08:12 +0000328APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000329 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlsson3068d112008-11-16 19:01:22 +0000331 if (!EvaluatePointer(E->getBase(), Result, Info))
332 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Anders Carlsson3068d112008-11-16 19:01:22 +0000334 APSInt Index;
335 if (!EvaluateInteger(E->getIdx(), Index, Info))
336 return APValue();
337
Ken Dyck199c3d62010-01-11 17:06:35 +0000338 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
Anders Carlsson3068d112008-11-16 19:01:22 +0000339
Ken Dyck199c3d62010-01-11 17:06:35 +0000340 CharUnits Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000341 Result.setLValue(Result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000342 Result.getLValueOffset() + Offset);
Anders Carlsson3068d112008-11-16 19:01:22 +0000343 return Result;
344}
Eli Friedman4efaa272008-11-12 09:44:48 +0000345
Mike Stump1eb44332009-09-09 15:08:12 +0000346APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000347 APValue Result;
348 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
349 return APValue();
350 return Result;
351}
352
Eli Friedman4efaa272008-11-12 09:44:48 +0000353//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000354// Pointer Evaluation
355//===----------------------------------------------------------------------===//
356
Anders Carlssonc754aa62008-07-08 05:13:58 +0000357namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000358class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000359 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000360 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000361public:
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattner87eae5e2008-07-11 22:52:41 +0000363 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000364
Anders Carlsson2bad1682008-07-08 14:30:00 +0000365 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000366 return APValue();
367 }
368
369 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
370
Anders Carlsson650c92f2008-07-08 15:34:11 +0000371 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000372 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000373 APValue VisitUnaryExtension(const UnaryOperator *E)
374 { return Visit(E->getSubExpr()); }
375 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000376 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
Ken Dycka7305832010-01-15 12:37:54 +0000377 { return APValue(E); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000378 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000379 { return APValue(E); }
Eli Friedman3941b182009-01-25 01:54:01 +0000380 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000381 APValue VisitBlockExpr(BlockExpr *E) {
382 if (!E->hasBlockDeclRefExprs())
Ken Dycka7305832010-01-15 12:37:54 +0000383 return APValue(E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000384 return APValue();
385 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000386 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000387 { return APValue((Expr*)0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000388 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000389 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000390 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
391 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000392 { return APValue((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000393 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000394};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000395} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000396
Chris Lattner87eae5e2008-07-11 22:52:41 +0000397static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000398 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000399 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000400 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000401 return Result.isLValue();
402}
403
404APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
405 if (E->getOpcode() != BinaryOperator::Add &&
406 E->getOpcode() != BinaryOperator::Sub)
407 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000409 const Expr *PExp = E->getLHS();
410 const Expr *IExp = E->getRHS();
411 if (IExp->getType()->isPointerType())
412 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000414 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000415 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000418 llvm::APSInt AdditionalOffset;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000419 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000420 return APValue();
421
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000422 // Compute the new offset in the appropriate width.
423
424 QualType PointeeType =
425 PExp->getType()->getAs<PointerType>()->getPointeeType();
426 llvm::APSInt SizeOfPointee(AdditionalOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000428 // Explicitly handle GNU void* and function pointer arithmetic extensions.
429 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000430 SizeOfPointee = 1;
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000431 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000432 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType).getQuantity();
Eli Friedman4efaa272008-11-12 09:44:48 +0000433
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000434 llvm::APSInt Offset(AdditionalOffset);
435 Offset = ResultLValue.getLValueOffset().getQuantity();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000436 if (E->getOpcode() == BinaryOperator::Add)
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000437 Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000438 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000439 Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000440
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000441 // Sign extend prior to converting back to a char unit.
442 if (Offset.getBitWidth() < 64)
443 Offset.extend(64);
444 return APValue(ResultLValue.getLValueBase(),
445 CharUnits::fromQuantity(Offset.getLimitedValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000446}
Eli Friedman4efaa272008-11-12 09:44:48 +0000447
Eli Friedman2217c872009-02-22 11:46:18 +0000448APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
449 APValue result;
450 if (EvaluateLValue(E->getSubExpr(), result, Info))
451 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000452 return APValue();
453}
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000455
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000456APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
457 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000458
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000459 switch (E->getCastKind()) {
460 default:
461 break;
462
463 case CastExpr::CK_Unknown: {
464 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
465
466 // Check for pointer->pointer cast
467 if (SubExpr->getType()->isPointerType() ||
468 SubExpr->getType()->isObjCObjectPointerType() ||
469 SubExpr->getType()->isNullPtrType() ||
470 SubExpr->getType()->isBlockPointerType())
471 return Visit(SubExpr);
472
473 if (SubExpr->getType()->isIntegralType()) {
474 APValue Result;
475 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
476 break;
477
478 if (Result.isInt()) {
479 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000480 return APValue(0,
481 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000482 }
483
484 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000485 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000486 }
487 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000490 case CastExpr::CK_NoOp:
491 case CastExpr::CK_BitCast:
492 case CastExpr::CK_AnyPointerToObjCPointerCast:
493 case CastExpr::CK_AnyPointerToBlockPointerCast:
494 return Visit(SubExpr);
495
496 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000497 APValue Result;
498 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000499 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000500
501 if (Result.isInt()) {
502 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000503 return APValue(0,
504 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000507 // Cast is of an lvalue, no need to change value.
508 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000509 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000510 case CastExpr::CK_ArrayToPointerDecay:
511 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000512 APValue Result;
513 if (EvaluateLValue(SubExpr, Result, Info))
514 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000515 break;
516 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000517 }
518
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000519 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000520}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000521
Eli Friedman3941b182009-01-25 01:54:01 +0000522APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000524 Builtin::BI__builtin___CFStringMakeConstantString ||
525 E->isBuiltinCall(Info.Ctx) ==
526 Builtin::BI__builtin___NSStringMakeConstantString)
Ken Dycka7305832010-01-15 12:37:54 +0000527 return APValue(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000528 return APValue();
529}
530
Eli Friedman4efaa272008-11-12 09:44:48 +0000531APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
532 bool BoolResult;
533 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
534 return APValue();
535
536 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
537
538 APValue Result;
539 if (EvaluatePointer(EvalExpr, Result, Info))
540 return Result;
541 return APValue();
542}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000543
544//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000545// Vector Evaluation
546//===----------------------------------------------------------------------===//
547
548namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000549 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000550 : public StmtVisitor<VectorExprEvaluator, APValue> {
551 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000552 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000553 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Nate Begeman59b5da62009-01-18 03:20:47 +0000555 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Nate Begeman59b5da62009-01-18 03:20:47 +0000557 APValue VisitStmt(Stmt *S) {
558 return APValue();
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Eli Friedman91110ee2009-02-23 04:23:56 +0000561 APValue VisitParenExpr(ParenExpr *E)
562 { return Visit(E->getSubExpr()); }
563 APValue VisitUnaryExtension(const UnaryOperator *E)
564 { return Visit(E->getSubExpr()); }
565 APValue VisitUnaryPlus(const UnaryOperator *E)
566 { return Visit(E->getSubExpr()); }
567 APValue VisitUnaryReal(const UnaryOperator *E)
568 { return Visit(E->getSubExpr()); }
569 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
570 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000571 APValue VisitCastExpr(const CastExpr* E);
572 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
573 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000574 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000575 APValue VisitChooseExpr(const ChooseExpr *E)
576 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000577 APValue VisitUnaryImag(const UnaryOperator *E);
578 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000579 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000580 // shufflevector, ExtVectorElementExpr
581 // (Note that these require implementing conversions
582 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000583 };
584} // end anonymous namespace
585
586static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
587 if (!E->getType()->isVectorType())
588 return false;
589 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
590 return !Result.isUninit();
591}
592
593APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000594 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000595 QualType EltTy = VTy->getElementType();
596 unsigned NElts = VTy->getNumElements();
597 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Nate Begeman59b5da62009-01-18 03:20:47 +0000599 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000600 QualType SETy = SE->getType();
601 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000602
Nate Begemane8c9e922009-06-26 18:22:18 +0000603 // Check for vector->vector bitcast and scalar->vector splat.
604 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000605 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000606 } else if (SETy->isIntegerType()) {
607 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000608 if (!EvaluateInteger(SE, IntResult, Info))
609 return APValue();
610 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000611 } else if (SETy->isRealFloatingType()) {
612 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000613 if (!EvaluateFloat(SE, F, Info))
614 return APValue();
615 Result = APValue(F);
616 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000617 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000618
Nate Begemanc0b8b192009-07-01 07:50:47 +0000619 // For casts of a scalar to ExtVector, convert the scalar to the element type
620 // and splat it to all elements.
621 if (E->getType()->isExtVectorType()) {
622 if (EltTy->isIntegerType() && Result.isInt())
623 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
624 Info.Ctx));
625 else if (EltTy->isIntegerType())
626 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
627 Info.Ctx));
628 else if (EltTy->isRealFloatingType() && Result.isInt())
629 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
630 Info.Ctx));
631 else if (EltTy->isRealFloatingType())
632 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
633 Info.Ctx));
634 else
635 return APValue();
636
637 // Splat and create vector APValue.
638 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
639 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000640 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000641
642 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
643 // to the vector. To construct the APValue vector initializer, bitcast the
644 // initializing value to an APInt, and shift out the bits pertaining to each
645 // element.
646 APSInt Init;
647 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Nate Begemanc0b8b192009-07-01 07:50:47 +0000649 llvm::SmallVector<APValue, 4> Elts;
650 for (unsigned i = 0; i != NElts; ++i) {
651 APSInt Tmp = Init;
652 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Nate Begemanc0b8b192009-07-01 07:50:47 +0000654 if (EltTy->isIntegerType())
655 Elts.push_back(APValue(Tmp));
656 else if (EltTy->isRealFloatingType())
657 Elts.push_back(APValue(APFloat(Tmp)));
658 else
659 return APValue();
660
661 Init >>= EltWidth;
662 }
663 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000664}
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000667VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
668 return this->Visit(const_cast<Expr*>(E->getInitializer()));
669}
670
Mike Stump1eb44332009-09-09 15:08:12 +0000671APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000672VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000673 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000674 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000675 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Nate Begeman59b5da62009-01-18 03:20:47 +0000677 QualType EltTy = VT->getElementType();
678 llvm::SmallVector<APValue, 4> Elements;
679
Eli Friedman91110ee2009-02-23 04:23:56 +0000680 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000681 if (EltTy->isIntegerType()) {
682 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000683 if (i < NumInits) {
684 if (!EvaluateInteger(E->getInit(i), sInt, Info))
685 return APValue();
686 } else {
687 sInt = Info.Ctx.MakeIntValue(0, EltTy);
688 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000689 Elements.push_back(APValue(sInt));
690 } else {
691 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000692 if (i < NumInits) {
693 if (!EvaluateFloat(E->getInit(i), f, Info))
694 return APValue();
695 } else {
696 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
697 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000698 Elements.push_back(APValue(f));
699 }
700 }
701 return APValue(&Elements[0], Elements.size());
702}
703
Mike Stump1eb44332009-09-09 15:08:12 +0000704APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000705VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000706 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000707 QualType EltTy = VT->getElementType();
708 APValue ZeroElement;
709 if (EltTy->isIntegerType())
710 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
711 else
712 ZeroElement =
713 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
714
715 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
716 return APValue(&Elements[0], Elements.size());
717}
718
719APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
720 bool BoolResult;
721 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
722 return APValue();
723
724 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
725
726 APValue Result;
727 if (EvaluateVector(EvalExpr, Result, Info))
728 return Result;
729 return APValue();
730}
731
Eli Friedman91110ee2009-02-23 04:23:56 +0000732APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
733 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
734 Info.EvalResult.HasSideEffects = true;
735 return GetZeroVector(E->getType());
736}
737
Nate Begeman59b5da62009-01-18 03:20:47 +0000738//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000739// Integer Evaluation
740//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000741
742namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000743class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000744 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000745 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000746 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000747public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000748 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000749 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000750
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000751 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000752 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000753 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000754 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000755 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000756 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000757 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000758 return true;
759 }
760
Daniel Dunbar131eb432009-02-19 09:06:44 +0000761 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000762 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000763 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000764 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000765 Result = APValue(APSInt(I));
766 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000767 return true;
768 }
769
770 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000771 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000772 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000773 return true;
774 }
775
Anders Carlsson82206e22008-11-30 18:14:57 +0000776 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000777 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000778 if (Info.EvalResult.Diag == 0) {
779 Info.EvalResult.DiagLoc = L;
780 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000781 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000782 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000783 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Anders Carlssonc754aa62008-07-08 05:13:58 +0000786 //===--------------------------------------------------------------------===//
787 // Visitor Methods
788 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner32fea9d2008-11-12 07:43:42 +0000790 bool VisitStmt(Stmt *) {
791 assert(0 && "This should be called on integers, stmts are not integers");
792 return false;
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattner32fea9d2008-11-12 07:43:42 +0000795 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000796 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerb542afe2008-07-11 19:10:17 +0000799 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000800
Chris Lattner4c4867e2008-07-12 00:38:25 +0000801 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000802 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000803 }
804 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000805 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000806 }
807 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000808 // Per gcc docs "this built-in function ignores top level
809 // qualifiers". We need to use the canonical version to properly
810 // be able to strip CRV qualifiers from the type.
811 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
812 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000813 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000814 T1.getUnqualifiedType()),
815 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000816 }
Eli Friedman04309752009-11-24 05:28:59 +0000817
818 bool CheckReferencedDecl(const Expr *E, const Decl *D);
819 bool VisitDeclRefExpr(const DeclRefExpr *E) {
820 return CheckReferencedDecl(E, E->getDecl());
821 }
822 bool VisitMemberExpr(const MemberExpr *E) {
823 if (CheckReferencedDecl(E, E->getMemberDecl())) {
824 // Conservatively assume a MemberExpr will have side-effects
825 Info.EvalResult.HasSideEffects = true;
826 return true;
827 }
828 return false;
829 }
830
Eli Friedmanc4a26382010-02-13 00:10:10 +0000831 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000832 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000833 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000834 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000835 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000836
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000837 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000838 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
839
Anders Carlsson3068d112008-11-16 19:01:22 +0000840 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000841 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Anders Carlsson3f704562008-12-21 22:39:40 +0000844 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000845 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Anders Carlsson3068d112008-11-16 19:01:22 +0000848 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000849 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000850 }
851
Eli Friedman664a1042009-02-27 04:45:43 +0000852 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
853 return Success(0, E);
854 }
855
Sebastian Redl64b45f72009-01-05 20:52:13 +0000856 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000857 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000858 }
859
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000860 bool VisitChooseExpr(const ChooseExpr *E) {
861 return Visit(E->getChosenSubExpr(Info.Ctx));
862 }
863
Eli Friedman722c7172009-02-28 03:59:05 +0000864 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000865 bool VisitUnaryImag(const UnaryOperator *E);
866
Chris Lattnerfcee0012008-07-11 21:24:13 +0000867private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000868 CharUnits GetAlignOfExpr(const Expr *E);
869 CharUnits GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000870 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000871};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000872} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000873
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000874static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000875 if (!E->getType()->isIntegralType())
876 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000878 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
879}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000880
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000881static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
882 APValue Val;
883 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
884 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000885 Result = Val.getInt();
886 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000887}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000888
Eli Friedman04309752009-11-24 05:28:59 +0000889bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000890 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000891 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
892 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000893
894 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000895 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000896 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
897 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000898
899 if (isa<ParmVarDecl>(D))
900 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
901
Eli Friedman04309752009-11-24 05:28:59 +0000902 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000903 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000904 if (APValue *V = VD->getEvaluatedValue()) {
905 if (V->isInt())
906 return Success(V->getInt(), E);
907 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
908 }
909
910 if (VD->isEvaluatingValue())
911 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
912
913 VD->setEvaluatingValue();
914
Douglas Gregor78d15832009-05-26 18:54:04 +0000915 if (Visit(const_cast<Expr*>(Init))) {
916 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000917 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000918 return true;
919 }
920
Eli Friedmanc0131182009-12-03 20:31:57 +0000921 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000922 return false;
923 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000924 }
925 }
926
Chris Lattner4c4867e2008-07-12 00:38:25 +0000927 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000928 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000929}
930
Chris Lattnera4d55d82008-10-06 06:40:35 +0000931/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
932/// as GCC.
933static int EvaluateBuiltinClassifyType(const CallExpr *E) {
934 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000935 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000936 enum gcc_type_class {
937 no_type_class = -1,
938 void_type_class, integer_type_class, char_type_class,
939 enumeral_type_class, boolean_type_class,
940 pointer_type_class, reference_type_class, offset_type_class,
941 real_type_class, complex_type_class,
942 function_type_class, method_type_class,
943 record_type_class, union_type_class,
944 array_type_class, string_type_class,
945 lang_type_class
946 };
Mike Stump1eb44332009-09-09 15:08:12 +0000947
948 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000949 // ideal, however it is what gcc does.
950 if (E->getNumArgs() == 0)
951 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattnera4d55d82008-10-06 06:40:35 +0000953 QualType ArgTy = E->getArg(0)->getType();
954 if (ArgTy->isVoidType())
955 return void_type_class;
956 else if (ArgTy->isEnumeralType())
957 return enumeral_type_class;
958 else if (ArgTy->isBooleanType())
959 return boolean_type_class;
960 else if (ArgTy->isCharType())
961 return string_type_class; // gcc doesn't appear to use char_type_class
962 else if (ArgTy->isIntegerType())
963 return integer_type_class;
964 else if (ArgTy->isPointerType())
965 return pointer_type_class;
966 else if (ArgTy->isReferenceType())
967 return reference_type_class;
968 else if (ArgTy->isRealType())
969 return real_type_class;
970 else if (ArgTy->isComplexType())
971 return complex_type_class;
972 else if (ArgTy->isFunctionType())
973 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +0000974 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +0000975 return record_type_class;
976 else if (ArgTy->isUnionType())
977 return union_type_class;
978 else if (ArgTy->isArrayType())
979 return array_type_class;
980 else if (ArgTy->isUnionType())
981 return union_type_class;
982 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
983 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
984 return -1;
985}
986
Eli Friedmanc4a26382010-02-13 00:10:10 +0000987bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000988 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000989 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000990 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000991
992 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000993 const Expr *Arg = E->getArg(0)->IgnoreParens();
994 Expr::EvalResult Base;
Eric Christopherb2aaf512010-01-19 22:58:35 +0000995
996 // TODO: Perhaps we should let LLVM lower this?
Mike Stump660e6f72009-10-26 23:05:19 +0000997 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000998 && Base.Val.getKind() == APValue::LValue
999 && !Base.HasSideEffects)
1000 if (const Expr *LVBase = Base.Val.getLValueBase())
1001 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
1002 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +00001003 if (!VD->getType()->isIncompleteType()
1004 && VD->getType()->isObjectType()
1005 && !VD->getType()->isVariablyModifiedType()
1006 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +00001007 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +00001008 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +00001009 if (!Offset.isNegative() && Offset <= Size)
1010 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +00001011 else
Ken Dyck199c3d62010-01-11 17:06:35 +00001012 Size = CharUnits::Zero();
1013 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +00001014 }
Mike Stump64eda9e2009-10-26 18:35:08 +00001015 }
1016 }
1017
Eric Christopherb2aaf512010-01-19 22:58:35 +00001018 // If evaluating the argument has side-effects we can't determine
1019 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001020 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001021 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001022 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001023 return Success(0, E);
1024 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001025
Mike Stump64eda9e2009-10-26 18:35:08 +00001026 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1027 }
1028
Chris Lattner019f4e82008-10-06 05:28:25 +00001029 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001030 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001032 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001033 // __builtin_constant_p always has one operand: it returns true if that
1034 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001035 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001036
1037 case Builtin::BI__builtin_eh_return_data_regno: {
1038 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1039 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1040 return Success(Operand, E);
1041 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001042
1043 case Builtin::BI__builtin_expect:
1044 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001045 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001046}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001047
Chris Lattnerb542afe2008-07-11 19:10:17 +00001048bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001049 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001050 if (!Visit(E->getRHS()))
1051 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001052
Eli Friedman33ef1452009-02-26 10:19:36 +00001053 // If we can't evaluate the LHS, it might have side effects;
1054 // conservatively mark it.
1055 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1056 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001057
Anders Carlsson027f62e2008-12-01 02:07:06 +00001058 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001059 }
1060
1061 if (E->isLogicalOp()) {
1062 // These need to be handled specially because the operands aren't
1063 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001064 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001066 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001067 // We were able to evaluate the LHS, see if we can get away with not
1068 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001069 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001070 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001071
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001072 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001073 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001074 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001075 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001076 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001077 }
1078 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001079 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001080 // We can't evaluate the LHS; however, sometimes the result
1081 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001082 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001083 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001084 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001085 // must have had side effects.
1086 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001087
1088 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001089 }
1090 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001091 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001092
Eli Friedmana6afa762008-11-13 06:09:17 +00001093 return false;
1094 }
1095
Anders Carlsson286f85e2008-11-16 07:17:21 +00001096 QualType LHSTy = E->getLHS()->getType();
1097 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001098
1099 if (LHSTy->isAnyComplexType()) {
1100 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1101 APValue LHS, RHS;
1102
1103 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1104 return false;
1105
1106 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1107 return false;
1108
1109 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001110 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001111 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001112 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001113 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1114
Daniel Dunbar4087e242009-01-29 06:43:41 +00001115 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001116 return Success((CR_r == APFloat::cmpEqual &&
1117 CR_i == APFloat::cmpEqual), E);
1118 else {
1119 assert(E->getOpcode() == BinaryOperator::NE &&
1120 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001121 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001122 CR_r == APFloat::cmpLessThan ||
1123 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001124 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001125 CR_i == APFloat::cmpLessThan ||
1126 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001127 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001128 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001129 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001130 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1131 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1132 else {
1133 assert(E->getOpcode() == BinaryOperator::NE &&
1134 "Invalid compex comparison.");
1135 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1136 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1137 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001138 }
1139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Anders Carlsson286f85e2008-11-16 07:17:21 +00001141 if (LHSTy->isRealFloatingType() &&
1142 RHSTy->isRealFloatingType()) {
1143 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Anders Carlsson286f85e2008-11-16 07:17:21 +00001145 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1146 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Anders Carlsson286f85e2008-11-16 07:17:21 +00001148 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1149 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Anders Carlsson286f85e2008-11-16 07:17:21 +00001151 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001152
Anders Carlsson286f85e2008-11-16 07:17:21 +00001153 switch (E->getOpcode()) {
1154 default:
1155 assert(0 && "Invalid binary operator!");
1156 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001157 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001158 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001159 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001160 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001161 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001162 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001163 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001164 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001165 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001166 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001167 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001168 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001169 || CR == APFloat::cmpLessThan
1170 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001171 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001174 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1175 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001176 APValue LHSValue;
1177 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1178 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001179
Anders Carlsson3068d112008-11-16 19:01:22 +00001180 APValue RHSValue;
1181 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1182 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001183
Eli Friedman5bc86102009-06-14 02:17:33 +00001184 // Reject any bases from the normal codepath; we special-case comparisons
1185 // to null.
1186 if (LHSValue.getLValueBase()) {
1187 if (!E->isEqualityOp())
1188 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001189 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001190 return false;
1191 bool bres;
1192 if (!EvalPointerValueAsBool(LHSValue, bres))
1193 return false;
1194 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1195 } else if (RHSValue.getLValueBase()) {
1196 if (!E->isEqualityOp())
1197 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001198 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001199 return false;
1200 bool bres;
1201 if (!EvalPointerValueAsBool(RHSValue, bres))
1202 return false;
1203 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1204 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001205
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001206 if (E->getOpcode() == BinaryOperator::Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001207 QualType Type = E->getLHS()->getType();
1208 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001209
Ken Dycka7305832010-01-15 12:37:54 +00001210 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001211 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001212 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001213
Ken Dycka7305832010-01-15 12:37:54 +00001214 CharUnits Diff = LHSValue.getLValueOffset() -
1215 RHSValue.getLValueOffset();
1216 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001217 }
1218 bool Result;
1219 if (E->getOpcode() == BinaryOperator::EQ) {
1220 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001221 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001222 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1223 }
1224 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001225 }
1226 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001227 if (!LHSTy->isIntegralType() ||
1228 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001229 // We can't continue from here for non-integral types, and they
1230 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001231 return false;
1232 }
1233
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001234 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001235 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001236 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001237
Eli Friedman42edd0d2009-03-24 01:14:50 +00001238 APValue RHSVal;
1239 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001240 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001241
1242 // Handle cases like (unsigned long)&a + 4.
1243 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001244 CharUnits Offset = Result.getLValueOffset();
1245 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1246 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001247 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001248 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001249 else
Ken Dycka7305832010-01-15 12:37:54 +00001250 Offset -= AdditionalOffset;
1251 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001252 return true;
1253 }
1254
1255 // Handle cases like 4 + (unsigned long)&a
1256 if (E->getOpcode() == BinaryOperator::Add &&
1257 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001258 CharUnits Offset = RHSVal.getLValueOffset();
1259 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1260 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001261 return true;
1262 }
1263
1264 // All the following cases expect both operands to be an integer
1265 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001266 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001267
Eli Friedman42edd0d2009-03-24 01:14:50 +00001268 APSInt& RHS = RHSVal.getInt();
1269
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001270 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001271 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001272 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001273 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1274 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1275 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1276 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1277 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1278 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001279 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001280 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001281 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001282 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001283 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001284 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001285 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001286 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001287 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001288 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001289 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001290 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1291 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001292 }
1293 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001294 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001295 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1296 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001299 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1300 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1301 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1302 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1303 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1304 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001305 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001306}
1307
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001308bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001309 bool Cond;
1310 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001311 return false;
1312
Nuno Lopesa25bd552008-11-16 22:06:39 +00001313 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001314}
1315
Ken Dyck8b752f12010-01-27 17:10:57 +00001316CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001317 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1318 // the result is the size of the referenced type."
1319 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1320 // result shall be the alignment of the referenced type."
1321 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1322 T = Ref->getPointeeType();
1323
Chris Lattnere9feb472009-01-24 21:09:06 +00001324 // Get information about the alignment.
1325 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001326
Eli Friedman2be58612009-05-30 21:09:44 +00001327 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001328 return CharUnits::fromQuantity(
1329 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001330}
1331
Ken Dyck8b752f12010-01-27 17:10:57 +00001332CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001333 E = E->IgnoreParens();
1334
1335 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001336 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001337 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001338 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1339 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001340
Chris Lattneraf707ab2009-01-24 21:53:27 +00001341 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001342 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1343 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001344
Chris Lattnere9feb472009-01-24 21:09:06 +00001345 return GetAlignOfType(E->getType());
1346}
1347
1348
Sebastian Redl05189992008-11-11 17:56:53 +00001349/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1350/// expression's type.
1351bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001352 // Handle alignof separately.
1353 if (!E->isSizeOf()) {
1354 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001355 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001356 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001357 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001358 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001359
Sebastian Redl05189992008-11-11 17:56:53 +00001360 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001361 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1362 // the result is the size of the referenced type."
1363 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1364 // result shall be the alignment of the referenced type."
1365 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1366 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001367
Daniel Dunbar131eb432009-02-19 09:06:44 +00001368 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1369 // extension.
1370 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1371 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001372
Chris Lattnerfcee0012008-07-11 21:24:13 +00001373 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001374 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001375 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001376
Chris Lattnere9feb472009-01-24 21:09:06 +00001377 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001378 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001379}
1380
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001381bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1382 CharUnits Result;
1383 unsigned n = E->getNumComponents();
1384 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1385 if (n == 0)
1386 return false;
1387 QualType CurrentType = E->getTypeSourceInfo()->getType();
1388 for (unsigned i = 0; i != n; ++i) {
1389 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1390 switch (ON.getKind()) {
1391 case OffsetOfExpr::OffsetOfNode::Array: {
1392 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1393 APSInt IdxResult;
1394 if (!EvaluateInteger(Idx, IdxResult, Info))
1395 return false;
1396 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1397 if (!AT)
1398 return false;
1399 CurrentType = AT->getElementType();
1400 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1401 Result += IdxResult.getSExtValue() * ElementSize;
1402 break;
1403 }
1404
1405 case OffsetOfExpr::OffsetOfNode::Field: {
1406 FieldDecl *MemberDecl = ON.getField();
1407 const RecordType *RT = CurrentType->getAs<RecordType>();
1408 if (!RT)
1409 return false;
1410 RecordDecl *RD = RT->getDecl();
1411 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1412 unsigned i = 0;
1413 // FIXME: It would be nice if we didn't have to loop here!
1414 for (RecordDecl::field_iterator Field = RD->field_begin(),
1415 FieldEnd = RD->field_end();
1416 Field != FieldEnd; (void)++Field, ++i) {
1417 if (*Field == MemberDecl)
1418 break;
1419 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001420 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1421 Result += CharUnits::fromQuantity(
1422 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001423 CurrentType = MemberDecl->getType().getNonReferenceType();
1424 break;
1425 }
1426
1427 case OffsetOfExpr::OffsetOfNode::Identifier:
1428 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001429 return false;
1430
1431 case OffsetOfExpr::OffsetOfNode::Base: {
1432 CXXBaseSpecifier *BaseSpec = ON.getBase();
1433 if (BaseSpec->isVirtual())
1434 return false;
1435
1436 // Find the layout of the class whose base we are looking into.
1437 const RecordType *RT = CurrentType->getAs<RecordType>();
1438 if (!RT)
1439 return false;
1440 RecordDecl *RD = RT->getDecl();
1441 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1442
1443 // Find the base class itself.
1444 CurrentType = BaseSpec->getType();
1445 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1446 if (!BaseRT)
1447 return false;
1448
1449 // Add the offset to the base.
1450 Result += CharUnits::fromQuantity(
1451 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1452 / Info.Ctx.getCharWidth());
1453 break;
1454 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001455 }
1456 }
1457 return Success(Result.getQuantity(), E);
1458}
1459
Chris Lattnerb542afe2008-07-11 19:10:17 +00001460bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001461 // Special case unary operators that do not need their subexpression
1462 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001463 if (E->isOffsetOfOp()) {
1464 // The AST for offsetof is defined in such a way that we can just
1465 // directly Evaluate it as an l-value.
1466 APValue LV;
1467 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001468 return false;
Eli Friedman35183ac2009-02-27 06:44:11 +00001469 if (LV.getLValueBase())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001470 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001471 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001472 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001473
Eli Friedmana6afa762008-11-13 06:09:17 +00001474 if (E->getOpcode() == UnaryOperator::LNot) {
1475 // LNot's operand isn't necessarily an integer, so we handle it specially.
1476 bool bres;
1477 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1478 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001479 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001480 }
1481
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001482 // Only handle integral operations...
1483 if (!E->getSubExpr()->getType()->isIntegralType())
1484 return false;
1485
Chris Lattner87eae5e2008-07-11 22:52:41 +00001486 // Get the operand value into 'Result'.
1487 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001488 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001489
Chris Lattner75a48812008-07-11 22:15:16 +00001490 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001491 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001492 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1493 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001494 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001495 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001496 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1497 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001498 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001499 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001500 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001501 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001502 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001503 if (!Result.isInt()) return false;
1504 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001505 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001506 if (!Result.isInt()) return false;
1507 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001508 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001509}
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattner732b2232008-07-12 01:15:53 +00001511/// HandleCast - This is used to evaluate implicit or explicit casts where the
1512/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001513bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001514 Expr *SubExpr = E->getSubExpr();
1515 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001516 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001517
Eli Friedman4efaa272008-11-12 09:44:48 +00001518 if (DestType->isBooleanType()) {
1519 bool BoolResult;
1520 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1521 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001522 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001523 }
1524
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001525 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001526 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001527 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001528 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001529
Eli Friedmanbe265702009-02-20 01:15:07 +00001530 if (!Result.isInt()) {
1531 // Only allow casts of lvalues if they are lossless.
1532 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1533 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001534
Daniel Dunbardd211642009-02-19 22:24:01 +00001535 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001536 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Chris Lattner732b2232008-07-12 01:15:53 +00001539 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001540 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001541 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001542 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001543 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001544
Daniel Dunbardd211642009-02-19 22:24:01 +00001545 if (LV.getLValueBase()) {
1546 // Only allow based lvalue casts if they are lossless.
1547 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1548 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001549
Daniel Dunbardd211642009-02-19 22:24:01 +00001550 Result = LV;
1551 return true;
1552 }
1553
Ken Dycka7305832010-01-15 12:37:54 +00001554 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1555 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001556 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001557 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001558
Eli Friedmanbe265702009-02-20 01:15:07 +00001559 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1560 // This handles double-conversion cases, where there's both
1561 // an l-value promotion and an implicit conversion to int.
1562 APValue LV;
1563 if (!EvaluateLValue(SubExpr, LV, Info))
1564 return false;
1565
1566 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1567 return false;
1568
1569 Result = LV;
1570 return true;
1571 }
1572
Eli Friedman1725f682009-04-22 19:23:09 +00001573 if (SrcType->isAnyComplexType()) {
1574 APValue C;
1575 if (!EvaluateComplex(SubExpr, C, Info))
1576 return false;
1577 if (C.isComplexFloat())
1578 return Success(HandleFloatToIntCast(DestType, SrcType,
1579 C.getComplexFloatReal(), Info.Ctx),
1580 E);
1581 else
1582 return Success(HandleIntToIntCast(DestType, SrcType,
1583 C.getComplexIntReal(), Info.Ctx), E);
1584 }
Eli Friedman2217c872009-02-22 11:46:18 +00001585 // FIXME: Handle vectors
1586
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001587 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001588 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001589
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001590 APFloat F(0.0);
1591 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001592 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001594 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001595}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001596
Eli Friedman722c7172009-02-28 03:59:05 +00001597bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1598 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1599 APValue LV;
1600 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1601 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1602 return Success(LV.getComplexIntReal(), E);
1603 }
1604
1605 return Visit(E->getSubExpr());
1606}
1607
Eli Friedman664a1042009-02-27 04:45:43 +00001608bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001609 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1610 APValue LV;
1611 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1612 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1613 return Success(LV.getComplexIntImag(), E);
1614 }
1615
Eli Friedman664a1042009-02-27 04:45:43 +00001616 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1617 Info.EvalResult.HasSideEffects = true;
1618 return Success(0, E);
1619}
1620
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001621//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001622// Float Evaluation
1623//===----------------------------------------------------------------------===//
1624
1625namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001626class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001627 : public StmtVisitor<FloatExprEvaluator, bool> {
1628 EvalInfo &Info;
1629 APFloat &Result;
1630public:
1631 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1632 : Info(info), Result(result) {}
1633
1634 bool VisitStmt(Stmt *S) {
1635 return false;
1636 }
1637
1638 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001639 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001640
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001641 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001642 bool VisitBinaryOperator(const BinaryOperator *E);
1643 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001644 bool VisitCastExpr(CastExpr *E);
1645 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001646 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001647
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001648 bool VisitChooseExpr(const ChooseExpr *E)
1649 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1650 bool VisitUnaryExtension(const UnaryOperator *E)
1651 { return Visit(E->getSubExpr()); }
1652
1653 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001654 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001655};
1656} // end anonymous namespace
1657
1658static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1659 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1660}
1661
John McCalldb7b72a2010-02-28 13:00:19 +00001662static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1663 QualType ResultTy,
1664 const Expr *Arg,
1665 bool SNaN,
1666 llvm::APFloat &Result) {
1667 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1668 if (!S) return false;
1669
1670 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1671
1672 llvm::APInt fill;
1673
1674 // Treat empty strings as if they were zero.
1675 if (S->getString().empty())
1676 fill = llvm::APInt(32, 0);
1677 else if (S->getString().getAsInteger(0, fill))
1678 return false;
1679
1680 if (SNaN)
1681 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1682 else
1683 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1684 return true;
1685}
1686
Chris Lattner019f4e82008-10-06 05:28:25 +00001687bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001688 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001689 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001690 case Builtin::BI__builtin_huge_val:
1691 case Builtin::BI__builtin_huge_valf:
1692 case Builtin::BI__builtin_huge_vall:
1693 case Builtin::BI__builtin_inf:
1694 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001695 case Builtin::BI__builtin_infl: {
1696 const llvm::fltSemantics &Sem =
1697 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001698 Result = llvm::APFloat::getInf(Sem);
1699 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001700 }
Mike Stump1eb44332009-09-09 15:08:12 +00001701
John McCalldb7b72a2010-02-28 13:00:19 +00001702 case Builtin::BI__builtin_nans:
1703 case Builtin::BI__builtin_nansf:
1704 case Builtin::BI__builtin_nansl:
1705 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1706 true, Result);
1707
Chris Lattner9e621712008-10-06 06:31:58 +00001708 case Builtin::BI__builtin_nan:
1709 case Builtin::BI__builtin_nanf:
1710 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001711 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001712 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001713 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1714 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001715
1716 case Builtin::BI__builtin_fabs:
1717 case Builtin::BI__builtin_fabsf:
1718 case Builtin::BI__builtin_fabsl:
1719 if (!EvaluateFloat(E->getArg(0), Result, Info))
1720 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001722 if (Result.isNegative())
1723 Result.changeSign();
1724 return true;
1725
Mike Stump1eb44332009-09-09 15:08:12 +00001726 case Builtin::BI__builtin_copysign:
1727 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001728 case Builtin::BI__builtin_copysignl: {
1729 APFloat RHS(0.);
1730 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1731 !EvaluateFloat(E->getArg(1), RHS, Info))
1732 return false;
1733 Result.copySign(RHS);
1734 return true;
1735 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001736 }
1737}
1738
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001739bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001740 if (E->getOpcode() == UnaryOperator::Deref)
1741 return false;
1742
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001743 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1744 return false;
1745
1746 switch (E->getOpcode()) {
1747 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001748 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001749 return true;
1750 case UnaryOperator::Minus:
1751 Result.changeSign();
1752 return true;
1753 }
1754}
Chris Lattner019f4e82008-10-06 05:28:25 +00001755
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001756bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001757 if (E->getOpcode() == BinaryOperator::Comma) {
1758 if (!EvaluateFloat(E->getRHS(), Result, Info))
1759 return false;
1760
1761 // If we can't evaluate the LHS, it might have side effects;
1762 // conservatively mark it.
1763 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1764 Info.EvalResult.HasSideEffects = true;
1765
1766 return true;
1767 }
1768
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001769 // FIXME: Diagnostics? I really don't understand how the warnings
1770 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001771 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001772 if (!EvaluateFloat(E->getLHS(), Result, Info))
1773 return false;
1774 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1775 return false;
1776
1777 switch (E->getOpcode()) {
1778 default: return false;
1779 case BinaryOperator::Mul:
1780 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1781 return true;
1782 case BinaryOperator::Add:
1783 Result.add(RHS, APFloat::rmNearestTiesToEven);
1784 return true;
1785 case BinaryOperator::Sub:
1786 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1787 return true;
1788 case BinaryOperator::Div:
1789 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1790 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001791 }
1792}
1793
1794bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1795 Result = E->getValue();
1796 return true;
1797}
1798
Eli Friedman4efaa272008-11-12 09:44:48 +00001799bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1800 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Eli Friedman4efaa272008-11-12 09:44:48 +00001802 if (SubExpr->getType()->isIntegralType()) {
1803 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001804 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001805 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001806 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001807 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001808 return true;
1809 }
1810 if (SubExpr->getType()->isRealFloatingType()) {
1811 if (!Visit(SubExpr))
1812 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001813 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1814 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001815 return true;
1816 }
Eli Friedman2217c872009-02-22 11:46:18 +00001817 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001818
1819 return false;
1820}
1821
1822bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1823 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1824 return true;
1825}
1826
Eli Friedman67f85fc2009-12-04 02:12:53 +00001827bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1828 bool Cond;
1829 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1830 return false;
1831
1832 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1833}
1834
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001835//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001836// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001837//===----------------------------------------------------------------------===//
1838
1839namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001840class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001841 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001842 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001844public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001845 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001847 //===--------------------------------------------------------------------===//
1848 // Visitor Methods
1849 //===--------------------------------------------------------------------===//
1850
1851 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001852 return APValue();
1853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001855 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1856
1857 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001858 Expr* SubExpr = E->getSubExpr();
1859
1860 if (SubExpr->getType()->isRealFloatingType()) {
1861 APFloat Result(0.0);
1862
1863 if (!EvaluateFloat(SubExpr, Result, Info))
1864 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001865
1866 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001867 Result);
1868 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001869 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001870 "Unexpected imaginary literal.");
1871
1872 llvm::APSInt Result;
1873 if (!EvaluateInteger(SubExpr, Result, Info))
1874 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001876 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1877 Zero = 0;
1878 return APValue(Zero, Result);
1879 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001880 }
1881
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001882 APValue VisitCastExpr(CastExpr *E) {
1883 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001884 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001885 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001886
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001887 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001888 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001889
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001890 if (!EvaluateFloat(SubExpr, Result, Info))
1891 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001892
1893 if (EltType->isRealFloatingType()) {
1894 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001895 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001896 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1897 } else {
1898 llvm::APSInt IResult;
1899 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1900 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1901 Zero = 0;
1902 return APValue(IResult, Zero);
1903 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001904 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001905 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001906
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001907 if (!EvaluateInteger(SubExpr, Result, Info))
1908 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001909
Eli Friedman1725f682009-04-22 19:23:09 +00001910 if (EltType->isRealFloatingType()) {
1911 APFloat FResult =
1912 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001913 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001914 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1915 } else {
1916 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1917 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1918 Zero = 0;
1919 return APValue(Result, Zero);
1920 }
John McCall183700f2009-09-21 23:43:11 +00001921 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001922 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001923
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001924 if (!EvaluateComplex(SubExpr, Src, Info))
1925 return APValue();
1926
1927 QualType SrcType = CT->getElementType();
1928
1929 if (Src.isComplexFloat()) {
1930 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001931 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001932 Src.getComplexFloatReal(),
1933 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001934 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001935 Src.getComplexFloatImag(),
1936 Info.Ctx));
1937 } else {
1938 return APValue(HandleFloatToIntCast(EltType, SrcType,
1939 Src.getComplexFloatReal(),
1940 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001941 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001942 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001943 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001944 }
1945 } else {
1946 assert(Src.isComplexInt() && "Invalid evaluate result.");
1947 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001948 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001949 Src.getComplexIntReal(),
1950 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001951 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001952 Src.getComplexIntImag(),
1953 Info.Ctx));
1954 } else {
1955 return APValue(HandleIntToIntCast(EltType, SrcType,
1956 Src.getComplexIntReal(),
1957 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001958 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001959 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001960 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001961 }
1962 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001963 }
1964
1965 // FIXME: Handle more casts.
1966 return APValue();
1967 }
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001969 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001970 APValue VisitChooseExpr(const ChooseExpr *E)
1971 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1972 APValue VisitUnaryExtension(const UnaryOperator *E)
1973 { return Visit(E->getSubExpr()); }
1974 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001975 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001976};
1977} // end anonymous namespace
1978
Mike Stump1eb44332009-09-09 15:08:12 +00001979static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001980 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1981 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001982 (&Result.getComplexFloatReal().getSemantics() ==
1983 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001984 "Invalid complex evaluation.");
1985 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001986}
1987
Mike Stump1eb44332009-09-09 15:08:12 +00001988APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001989 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001991 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001992 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001994 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001995 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001996
Daniel Dunbar3f279872009-01-29 01:32:56 +00001997 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1998 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001999 switch (E->getOpcode()) {
2000 default: return APValue();
2001 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002002 if (Result.isComplexFloat()) {
2003 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2004 APFloat::rmNearestTiesToEven);
2005 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2006 APFloat::rmNearestTiesToEven);
2007 } else {
2008 Result.getComplexIntReal() += RHS.getComplexIntReal();
2009 Result.getComplexIntImag() += RHS.getComplexIntImag();
2010 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002011 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002012 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002013 if (Result.isComplexFloat()) {
2014 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2015 APFloat::rmNearestTiesToEven);
2016 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2017 APFloat::rmNearestTiesToEven);
2018 } else {
2019 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2020 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2021 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002022 break;
2023 case BinaryOperator::Mul:
2024 if (Result.isComplexFloat()) {
2025 APValue LHS = Result;
2026 APFloat &LHS_r = LHS.getComplexFloatReal();
2027 APFloat &LHS_i = LHS.getComplexFloatImag();
2028 APFloat &RHS_r = RHS.getComplexFloatReal();
2029 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002030
Daniel Dunbar3f279872009-01-29 01:32:56 +00002031 APFloat Tmp = LHS_r;
2032 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2033 Result.getComplexFloatReal() = Tmp;
2034 Tmp = LHS_i;
2035 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2036 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2037
2038 Tmp = LHS_r;
2039 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2040 Result.getComplexFloatImag() = Tmp;
2041 Tmp = LHS_i;
2042 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2043 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2044 } else {
2045 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002046 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002047 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2048 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002049 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002050 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2051 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2052 }
2053 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002054 }
2055
2056 return Result;
2057}
2058
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002059//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002060// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002061//===----------------------------------------------------------------------===//
2062
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002063/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00002064/// any crazy technique (that has nothing to do with language standards) that
2065/// we want to. If this function returns true, it returns the folded constant
2066/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002067bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2068 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00002069
Nate Begeman59b5da62009-01-18 03:20:47 +00002070 if (getType()->isVectorType()) {
2071 if (!EvaluateVector(this, Result.Val, Info))
2072 return false;
2073 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00002074 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002075 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00002076 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002077 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002078 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002079 } else if (getType()->isRealFloatingType()) {
2080 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002081 if (!EvaluateFloat(this, f, Info))
2082 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002084 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002085 } else if (getType()->isAnyComplexType()) {
2086 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002087 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002088 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002089 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002090
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002091 return true;
2092}
2093
Mike Stump660e6f72009-10-26 23:05:19 +00002094bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
2095 EvalInfo Info(Ctx, Result, true);
2096
2097 if (getType()->isVectorType()) {
2098 if (!EvaluateVector(this, Result.Val, Info))
2099 return false;
2100 } else if (getType()->isIntegerType()) {
2101 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
2102 return false;
2103 } else if (getType()->hasPointerRepresentation()) {
2104 if (!EvaluatePointer(this, Result.Val, Info))
2105 return false;
2106 } else if (getType()->isRealFloatingType()) {
2107 llvm::APFloat f(0.0);
2108 if (!EvaluateFloat(this, f, Info))
2109 return false;
2110
2111 Result.Val = APValue(f);
2112 } else if (getType()->isAnyComplexType()) {
2113 if (!EvaluateComplex(this, Result.Val, Info))
2114 return false;
2115 } else
2116 return false;
2117
2118 return true;
2119}
2120
John McCallcd7a4452010-01-05 23:42:56 +00002121bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2122 EvalResult Scratch;
2123 EvalInfo Info(Ctx, Scratch);
2124
2125 return HandleConversionToBool(this, Result, Info);
2126}
2127
Anders Carlsson1b782762009-04-10 04:54:13 +00002128bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2129 EvalInfo Info(Ctx, Result);
2130
2131 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2132}
2133
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002134bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2135 EvalInfo Info(Ctx, Result, true);
2136
2137 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2138}
2139
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002140/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002141/// folded, but discard the result.
2142bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002143 EvalResult Result;
2144 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002145}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002146
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002147bool Expr::HasSideEffects(ASTContext &Ctx) const {
2148 Expr::EvalResult Result;
2149 EvalInfo Info(Ctx, Result);
2150 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2151}
2152
Anders Carlsson51fe9962008-11-22 21:04:56 +00002153APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002154 EvalResult EvalResult;
2155 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002156 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002157 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002158 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002159
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002160 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002161}