blob: 1c2b76eede680ddc91e7c2702ba943652a1d0969 [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 ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001122 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001123 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001124 CR_i == APFloat::cmpLessThan)), E);
1125 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001126 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001127 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001128 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1129 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1130 else {
1131 assert(E->getOpcode() == BinaryOperator::NE &&
1132 "Invalid compex comparison.");
1133 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1134 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1135 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001136 }
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Anders Carlsson286f85e2008-11-16 07:17:21 +00001139 if (LHSTy->isRealFloatingType() &&
1140 RHSTy->isRealFloatingType()) {
1141 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Anders Carlsson286f85e2008-11-16 07:17:21 +00001143 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1144 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Anders Carlsson286f85e2008-11-16 07:17:21 +00001146 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1147 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Anders Carlsson286f85e2008-11-16 07:17:21 +00001149 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001150
Anders Carlsson286f85e2008-11-16 07:17:21 +00001151 switch (E->getOpcode()) {
1152 default:
1153 assert(0 && "Invalid binary operator!");
1154 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001155 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001156 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001157 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001158 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001159 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001160 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001161 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001162 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001163 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001164 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001165 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001166 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001167 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001168 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001169 }
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001171 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1172 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001173 APValue LHSValue;
1174 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1175 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001176
Anders Carlsson3068d112008-11-16 19:01:22 +00001177 APValue RHSValue;
1178 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1179 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001180
Eli Friedman5bc86102009-06-14 02:17:33 +00001181 // Reject any bases from the normal codepath; we special-case comparisons
1182 // to null.
1183 if (LHSValue.getLValueBase()) {
1184 if (!E->isEqualityOp())
1185 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001186 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001187 return false;
1188 bool bres;
1189 if (!EvalPointerValueAsBool(LHSValue, bres))
1190 return false;
1191 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1192 } else if (RHSValue.getLValueBase()) {
1193 if (!E->isEqualityOp())
1194 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001195 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001196 return false;
1197 bool bres;
1198 if (!EvalPointerValueAsBool(RHSValue, bres))
1199 return false;
1200 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1201 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001202
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001203 if (E->getOpcode() == BinaryOperator::Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001204 QualType Type = E->getLHS()->getType();
1205 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001206
Ken Dycka7305832010-01-15 12:37:54 +00001207 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001208 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001209 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001210
Ken Dycka7305832010-01-15 12:37:54 +00001211 CharUnits Diff = LHSValue.getLValueOffset() -
1212 RHSValue.getLValueOffset();
1213 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001214 }
1215 bool Result;
1216 if (E->getOpcode() == BinaryOperator::EQ) {
1217 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001218 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001219 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1220 }
1221 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001222 }
1223 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001224 if (!LHSTy->isIntegralType() ||
1225 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001226 // We can't continue from here for non-integral types, and they
1227 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001228 return false;
1229 }
1230
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001231 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001232 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001233 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001234
Eli Friedman42edd0d2009-03-24 01:14:50 +00001235 APValue RHSVal;
1236 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001237 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001238
1239 // Handle cases like (unsigned long)&a + 4.
1240 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001241 CharUnits Offset = Result.getLValueOffset();
1242 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1243 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001244 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001245 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001246 else
Ken Dycka7305832010-01-15 12:37:54 +00001247 Offset -= AdditionalOffset;
1248 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001249 return true;
1250 }
1251
1252 // Handle cases like 4 + (unsigned long)&a
1253 if (E->getOpcode() == BinaryOperator::Add &&
1254 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001255 CharUnits Offset = RHSVal.getLValueOffset();
1256 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1257 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001258 return true;
1259 }
1260
1261 // All the following cases expect both operands to be an integer
1262 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001263 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001264
Eli Friedman42edd0d2009-03-24 01:14:50 +00001265 APSInt& RHS = RHSVal.getInt();
1266
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001267 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001268 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001269 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001270 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1271 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1272 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1273 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1274 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1275 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001276 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001277 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001278 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001279 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001280 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001281 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001282 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001283 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001284 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001285 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001286 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001287 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1288 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001289 }
1290 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001291 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001292 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1293 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001296 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1297 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1298 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1299 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1300 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1301 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001302 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001303}
1304
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001305bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001306 bool Cond;
1307 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001308 return false;
1309
Nuno Lopesa25bd552008-11-16 22:06:39 +00001310 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001311}
1312
Ken Dyck8b752f12010-01-27 17:10:57 +00001313CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001314 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1315 // the result is the size of the referenced type."
1316 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1317 // result shall be the alignment of the referenced type."
1318 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1319 T = Ref->getPointeeType();
1320
Chris Lattnere9feb472009-01-24 21:09:06 +00001321 // Get information about the alignment.
1322 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001323
Eli Friedman2be58612009-05-30 21:09:44 +00001324 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001325 return CharUnits::fromQuantity(
1326 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001327}
1328
Ken Dyck8b752f12010-01-27 17:10:57 +00001329CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001330 E = E->IgnoreParens();
1331
1332 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001333 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001334 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001335 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1336 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001337
Chris Lattneraf707ab2009-01-24 21:53:27 +00001338 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001339 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1340 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001341
Chris Lattnere9feb472009-01-24 21:09:06 +00001342 return GetAlignOfType(E->getType());
1343}
1344
1345
Sebastian Redl05189992008-11-11 17:56:53 +00001346/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1347/// expression's type.
1348bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001349 // Handle alignof separately.
1350 if (!E->isSizeOf()) {
1351 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001352 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001353 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001354 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001355 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001356
Sebastian Redl05189992008-11-11 17:56:53 +00001357 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001358 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1359 // the result is the size of the referenced type."
1360 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1361 // result shall be the alignment of the referenced type."
1362 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1363 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001364
Daniel Dunbar131eb432009-02-19 09:06:44 +00001365 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1366 // extension.
1367 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1368 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001369
Chris Lattnerfcee0012008-07-11 21:24:13 +00001370 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001371 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001372 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001373
Chris Lattnere9feb472009-01-24 21:09:06 +00001374 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001375 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001376}
1377
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001378bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1379 CharUnits Result;
1380 unsigned n = E->getNumComponents();
1381 OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
1382 if (n == 0)
1383 return false;
1384 QualType CurrentType = E->getTypeSourceInfo()->getType();
1385 for (unsigned i = 0; i != n; ++i) {
1386 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1387 switch (ON.getKind()) {
1388 case OffsetOfExpr::OffsetOfNode::Array: {
1389 Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
1390 APSInt IdxResult;
1391 if (!EvaluateInteger(Idx, IdxResult, Info))
1392 return false;
1393 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1394 if (!AT)
1395 return false;
1396 CurrentType = AT->getElementType();
1397 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1398 Result += IdxResult.getSExtValue() * ElementSize;
1399 break;
1400 }
1401
1402 case OffsetOfExpr::OffsetOfNode::Field: {
1403 FieldDecl *MemberDecl = ON.getField();
1404 const RecordType *RT = CurrentType->getAs<RecordType>();
1405 if (!RT)
1406 return false;
1407 RecordDecl *RD = RT->getDecl();
1408 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1409 unsigned i = 0;
1410 // FIXME: It would be nice if we didn't have to loop here!
1411 for (RecordDecl::field_iterator Field = RD->field_begin(),
1412 FieldEnd = RD->field_end();
1413 Field != FieldEnd; (void)++Field, ++i) {
1414 if (*Field == MemberDecl)
1415 break;
1416 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001417 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1418 Result += CharUnits::fromQuantity(
1419 RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001420 CurrentType = MemberDecl->getType().getNonReferenceType();
1421 break;
1422 }
1423
1424 case OffsetOfExpr::OffsetOfNode::Identifier:
1425 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001426 return false;
1427
1428 case OffsetOfExpr::OffsetOfNode::Base: {
1429 CXXBaseSpecifier *BaseSpec = ON.getBase();
1430 if (BaseSpec->isVirtual())
1431 return false;
1432
1433 // Find the layout of the class whose base we are looking into.
1434 const RecordType *RT = CurrentType->getAs<RecordType>();
1435 if (!RT)
1436 return false;
1437 RecordDecl *RD = RT->getDecl();
1438 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1439
1440 // Find the base class itself.
1441 CurrentType = BaseSpec->getType();
1442 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1443 if (!BaseRT)
1444 return false;
1445
1446 // Add the offset to the base.
1447 Result += CharUnits::fromQuantity(
1448 RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
1449 / Info.Ctx.getCharWidth());
1450 break;
1451 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001452 }
1453 }
1454 return Success(Result.getQuantity(), E);
1455}
1456
Chris Lattnerb542afe2008-07-11 19:10:17 +00001457bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001458 // Special case unary operators that do not need their subexpression
1459 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001460 if (E->isOffsetOfOp()) {
1461 // The AST for offsetof is defined in such a way that we can just
1462 // directly Evaluate it as an l-value.
1463 APValue LV;
1464 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001465 return false;
Eli Friedman35183ac2009-02-27 06:44:11 +00001466 if (LV.getLValueBase())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001467 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001468 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001469 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001470
Eli Friedmana6afa762008-11-13 06:09:17 +00001471 if (E->getOpcode() == UnaryOperator::LNot) {
1472 // LNot's operand isn't necessarily an integer, so we handle it specially.
1473 bool bres;
1474 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1475 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001476 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001477 }
1478
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001479 // Only handle integral operations...
1480 if (!E->getSubExpr()->getType()->isIntegralType())
1481 return false;
1482
Chris Lattner87eae5e2008-07-11 22:52:41 +00001483 // Get the operand value into 'Result'.
1484 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001485 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001486
Chris Lattner75a48812008-07-11 22:15:16 +00001487 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001488 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001489 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1490 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001491 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001492 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001493 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1494 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001495 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001496 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001497 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001498 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001499 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001500 if (!Result.isInt()) return false;
1501 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001502 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001503 if (!Result.isInt()) return false;
1504 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001505 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001506}
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Chris Lattner732b2232008-07-12 01:15:53 +00001508/// HandleCast - This is used to evaluate implicit or explicit casts where the
1509/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001510bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001511 Expr *SubExpr = E->getSubExpr();
1512 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001513 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001514
Eli Friedman4efaa272008-11-12 09:44:48 +00001515 if (DestType->isBooleanType()) {
1516 bool BoolResult;
1517 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1518 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001519 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001520 }
1521
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001522 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001523 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001524 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001525 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001526
Eli Friedmanbe265702009-02-20 01:15:07 +00001527 if (!Result.isInt()) {
1528 // Only allow casts of lvalues if they are lossless.
1529 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1530 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001531
Daniel Dunbardd211642009-02-19 22:24:01 +00001532 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001533 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Chris Lattner732b2232008-07-12 01:15:53 +00001536 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001537 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001538 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001539 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001540 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001541
Daniel Dunbardd211642009-02-19 22:24:01 +00001542 if (LV.getLValueBase()) {
1543 // Only allow based lvalue casts if they are lossless.
1544 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1545 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001546
Daniel Dunbardd211642009-02-19 22:24:01 +00001547 Result = LV;
1548 return true;
1549 }
1550
Ken Dycka7305832010-01-15 12:37:54 +00001551 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1552 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001553 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001554 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001555
Eli Friedmanbe265702009-02-20 01:15:07 +00001556 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1557 // This handles double-conversion cases, where there's both
1558 // an l-value promotion and an implicit conversion to int.
1559 APValue LV;
1560 if (!EvaluateLValue(SubExpr, LV, Info))
1561 return false;
1562
1563 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1564 return false;
1565
1566 Result = LV;
1567 return true;
1568 }
1569
Eli Friedman1725f682009-04-22 19:23:09 +00001570 if (SrcType->isAnyComplexType()) {
1571 APValue C;
1572 if (!EvaluateComplex(SubExpr, C, Info))
1573 return false;
1574 if (C.isComplexFloat())
1575 return Success(HandleFloatToIntCast(DestType, SrcType,
1576 C.getComplexFloatReal(), Info.Ctx),
1577 E);
1578 else
1579 return Success(HandleIntToIntCast(DestType, SrcType,
1580 C.getComplexIntReal(), Info.Ctx), E);
1581 }
Eli Friedman2217c872009-02-22 11:46:18 +00001582 // FIXME: Handle vectors
1583
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001584 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001585 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001586
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001587 APFloat F(0.0);
1588 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001589 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001591 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001592}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001593
Eli Friedman722c7172009-02-28 03:59:05 +00001594bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1595 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1596 APValue LV;
1597 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1598 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1599 return Success(LV.getComplexIntReal(), E);
1600 }
1601
1602 return Visit(E->getSubExpr());
1603}
1604
Eli Friedman664a1042009-02-27 04:45:43 +00001605bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001606 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1607 APValue LV;
1608 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1609 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1610 return Success(LV.getComplexIntImag(), E);
1611 }
1612
Eli Friedman664a1042009-02-27 04:45:43 +00001613 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1614 Info.EvalResult.HasSideEffects = true;
1615 return Success(0, E);
1616}
1617
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001618//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001619// Float Evaluation
1620//===----------------------------------------------------------------------===//
1621
1622namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001623class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001624 : public StmtVisitor<FloatExprEvaluator, bool> {
1625 EvalInfo &Info;
1626 APFloat &Result;
1627public:
1628 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1629 : Info(info), Result(result) {}
1630
1631 bool VisitStmt(Stmt *S) {
1632 return false;
1633 }
1634
1635 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001636 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001637
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001638 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001639 bool VisitBinaryOperator(const BinaryOperator *E);
1640 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001641 bool VisitCastExpr(CastExpr *E);
1642 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001643 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001644
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001645 bool VisitChooseExpr(const ChooseExpr *E)
1646 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1647 bool VisitUnaryExtension(const UnaryOperator *E)
1648 { return Visit(E->getSubExpr()); }
1649
1650 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001651 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001652};
1653} // end anonymous namespace
1654
1655static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1656 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1657}
1658
John McCalldb7b72a2010-02-28 13:00:19 +00001659static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1660 QualType ResultTy,
1661 const Expr *Arg,
1662 bool SNaN,
1663 llvm::APFloat &Result) {
1664 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1665 if (!S) return false;
1666
1667 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1668
1669 llvm::APInt fill;
1670
1671 // Treat empty strings as if they were zero.
1672 if (S->getString().empty())
1673 fill = llvm::APInt(32, 0);
1674 else if (S->getString().getAsInteger(0, fill))
1675 return false;
1676
1677 if (SNaN)
1678 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1679 else
1680 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1681 return true;
1682}
1683
Chris Lattner019f4e82008-10-06 05:28:25 +00001684bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001685 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001686 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001687 case Builtin::BI__builtin_huge_val:
1688 case Builtin::BI__builtin_huge_valf:
1689 case Builtin::BI__builtin_huge_vall:
1690 case Builtin::BI__builtin_inf:
1691 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001692 case Builtin::BI__builtin_infl: {
1693 const llvm::fltSemantics &Sem =
1694 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001695 Result = llvm::APFloat::getInf(Sem);
1696 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
John McCalldb7b72a2010-02-28 13:00:19 +00001699 case Builtin::BI__builtin_nans:
1700 case Builtin::BI__builtin_nansf:
1701 case Builtin::BI__builtin_nansl:
1702 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1703 true, Result);
1704
Chris Lattner9e621712008-10-06 06:31:58 +00001705 case Builtin::BI__builtin_nan:
1706 case Builtin::BI__builtin_nanf:
1707 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001708 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001709 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001710 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1711 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001712
1713 case Builtin::BI__builtin_fabs:
1714 case Builtin::BI__builtin_fabsf:
1715 case Builtin::BI__builtin_fabsl:
1716 if (!EvaluateFloat(E->getArg(0), Result, Info))
1717 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001719 if (Result.isNegative())
1720 Result.changeSign();
1721 return true;
1722
Mike Stump1eb44332009-09-09 15:08:12 +00001723 case Builtin::BI__builtin_copysign:
1724 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001725 case Builtin::BI__builtin_copysignl: {
1726 APFloat RHS(0.);
1727 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1728 !EvaluateFloat(E->getArg(1), RHS, Info))
1729 return false;
1730 Result.copySign(RHS);
1731 return true;
1732 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001733 }
1734}
1735
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001736bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001737 if (E->getOpcode() == UnaryOperator::Deref)
1738 return false;
1739
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001740 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1741 return false;
1742
1743 switch (E->getOpcode()) {
1744 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001745 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001746 return true;
1747 case UnaryOperator::Minus:
1748 Result.changeSign();
1749 return true;
1750 }
1751}
Chris Lattner019f4e82008-10-06 05:28:25 +00001752
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001753bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001754 if (E->getOpcode() == BinaryOperator::Comma) {
1755 if (!EvaluateFloat(E->getRHS(), Result, Info))
1756 return false;
1757
1758 // If we can't evaluate the LHS, it might have side effects;
1759 // conservatively mark it.
1760 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1761 Info.EvalResult.HasSideEffects = true;
1762
1763 return true;
1764 }
1765
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001766 // FIXME: Diagnostics? I really don't understand how the warnings
1767 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001768 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001769 if (!EvaluateFloat(E->getLHS(), Result, Info))
1770 return false;
1771 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1772 return false;
1773
1774 switch (E->getOpcode()) {
1775 default: return false;
1776 case BinaryOperator::Mul:
1777 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1778 return true;
1779 case BinaryOperator::Add:
1780 Result.add(RHS, APFloat::rmNearestTiesToEven);
1781 return true;
1782 case BinaryOperator::Sub:
1783 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1784 return true;
1785 case BinaryOperator::Div:
1786 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1787 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001788 }
1789}
1790
1791bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1792 Result = E->getValue();
1793 return true;
1794}
1795
Eli Friedman4efaa272008-11-12 09:44:48 +00001796bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1797 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Eli Friedman4efaa272008-11-12 09:44:48 +00001799 if (SubExpr->getType()->isIntegralType()) {
1800 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001801 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001802 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001803 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001804 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001805 return true;
1806 }
1807 if (SubExpr->getType()->isRealFloatingType()) {
1808 if (!Visit(SubExpr))
1809 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001810 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1811 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001812 return true;
1813 }
Eli Friedman2217c872009-02-22 11:46:18 +00001814 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001815
1816 return false;
1817}
1818
1819bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1820 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1821 return true;
1822}
1823
Eli Friedman67f85fc2009-12-04 02:12:53 +00001824bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1825 bool Cond;
1826 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1827 return false;
1828
1829 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1830}
1831
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001832//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001833// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001834//===----------------------------------------------------------------------===//
1835
1836namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001837class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001838 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001839 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001841public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001842 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001844 //===--------------------------------------------------------------------===//
1845 // Visitor Methods
1846 //===--------------------------------------------------------------------===//
1847
1848 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001849 return APValue();
1850 }
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001852 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1853
1854 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001855 Expr* SubExpr = E->getSubExpr();
1856
1857 if (SubExpr->getType()->isRealFloatingType()) {
1858 APFloat Result(0.0);
1859
1860 if (!EvaluateFloat(SubExpr, Result, Info))
1861 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001862
1863 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001864 Result);
1865 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001866 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001867 "Unexpected imaginary literal.");
1868
1869 llvm::APSInt Result;
1870 if (!EvaluateInteger(SubExpr, Result, Info))
1871 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001873 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1874 Zero = 0;
1875 return APValue(Zero, Result);
1876 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001877 }
1878
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001879 APValue VisitCastExpr(CastExpr *E) {
1880 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001881 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001882 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001883
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001884 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001885 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001886
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001887 if (!EvaluateFloat(SubExpr, Result, Info))
1888 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001889
1890 if (EltType->isRealFloatingType()) {
1891 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001892 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001893 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1894 } else {
1895 llvm::APSInt IResult;
1896 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1897 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1898 Zero = 0;
1899 return APValue(IResult, Zero);
1900 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001901 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001902 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001903
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001904 if (!EvaluateInteger(SubExpr, Result, Info))
1905 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001906
Eli Friedman1725f682009-04-22 19:23:09 +00001907 if (EltType->isRealFloatingType()) {
1908 APFloat FResult =
1909 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001910 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001911 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1912 } else {
1913 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1914 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1915 Zero = 0;
1916 return APValue(Result, Zero);
1917 }
John McCall183700f2009-09-21 23:43:11 +00001918 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001919 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001920
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001921 if (!EvaluateComplex(SubExpr, Src, Info))
1922 return APValue();
1923
1924 QualType SrcType = CT->getElementType();
1925
1926 if (Src.isComplexFloat()) {
1927 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001928 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001929 Src.getComplexFloatReal(),
1930 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001931 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001932 Src.getComplexFloatImag(),
1933 Info.Ctx));
1934 } else {
1935 return APValue(HandleFloatToIntCast(EltType, SrcType,
1936 Src.getComplexFloatReal(),
1937 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001938 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001939 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001940 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001941 }
1942 } else {
1943 assert(Src.isComplexInt() && "Invalid evaluate result.");
1944 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001945 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001946 Src.getComplexIntReal(),
1947 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001948 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001949 Src.getComplexIntImag(),
1950 Info.Ctx));
1951 } else {
1952 return APValue(HandleIntToIntCast(EltType, SrcType,
1953 Src.getComplexIntReal(),
1954 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001955 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001956 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001957 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001958 }
1959 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001960 }
1961
1962 // FIXME: Handle more casts.
1963 return APValue();
1964 }
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001966 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001967 APValue VisitChooseExpr(const ChooseExpr *E)
1968 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1969 APValue VisitUnaryExtension(const UnaryOperator *E)
1970 { return Visit(E->getSubExpr()); }
1971 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001972 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001973};
1974} // end anonymous namespace
1975
Mike Stump1eb44332009-09-09 15:08:12 +00001976static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001977 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1978 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001979 (&Result.getComplexFloatReal().getSemantics() ==
1980 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001981 "Invalid complex evaluation.");
1982 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001983}
1984
Mike Stump1eb44332009-09-09 15:08:12 +00001985APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001986 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001988 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001989 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001991 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001992 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001993
Daniel Dunbar3f279872009-01-29 01:32:56 +00001994 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1995 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001996 switch (E->getOpcode()) {
1997 default: return APValue();
1998 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001999 if (Result.isComplexFloat()) {
2000 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2001 APFloat::rmNearestTiesToEven);
2002 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2003 APFloat::rmNearestTiesToEven);
2004 } else {
2005 Result.getComplexIntReal() += RHS.getComplexIntReal();
2006 Result.getComplexIntImag() += RHS.getComplexIntImag();
2007 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002008 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002009 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002010 if (Result.isComplexFloat()) {
2011 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2012 APFloat::rmNearestTiesToEven);
2013 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2014 APFloat::rmNearestTiesToEven);
2015 } else {
2016 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2017 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2018 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002019 break;
2020 case BinaryOperator::Mul:
2021 if (Result.isComplexFloat()) {
2022 APValue LHS = Result;
2023 APFloat &LHS_r = LHS.getComplexFloatReal();
2024 APFloat &LHS_i = LHS.getComplexFloatImag();
2025 APFloat &RHS_r = RHS.getComplexFloatReal();
2026 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Daniel Dunbar3f279872009-01-29 01:32:56 +00002028 APFloat Tmp = LHS_r;
2029 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2030 Result.getComplexFloatReal() = Tmp;
2031 Tmp = LHS_i;
2032 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2033 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2034
2035 Tmp = LHS_r;
2036 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2037 Result.getComplexFloatImag() = Tmp;
2038 Tmp = LHS_i;
2039 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2040 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2041 } else {
2042 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002043 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002044 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2045 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002046 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002047 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2048 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2049 }
2050 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002051 }
2052
2053 return Result;
2054}
2055
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002056//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002057// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002058//===----------------------------------------------------------------------===//
2059
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002060/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00002061/// any crazy technique (that has nothing to do with language standards) that
2062/// we want to. If this function returns true, it returns the folded constant
2063/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002064bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
2065 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00002066
Nate Begeman59b5da62009-01-18 03:20:47 +00002067 if (getType()->isVectorType()) {
2068 if (!EvaluateVector(this, Result.Val, Info))
2069 return false;
2070 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00002071 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002072 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00002073 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002074 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002075 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002076 } else if (getType()->isRealFloatingType()) {
2077 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002078 if (!EvaluateFloat(this, f, Info))
2079 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002081 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002082 } else if (getType()->isAnyComplexType()) {
2083 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002084 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002085 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002086 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002087
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002088 return true;
2089}
2090
Mike Stump660e6f72009-10-26 23:05:19 +00002091bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
2092 EvalInfo Info(Ctx, Result, true);
2093
2094 if (getType()->isVectorType()) {
2095 if (!EvaluateVector(this, Result.Val, Info))
2096 return false;
2097 } else if (getType()->isIntegerType()) {
2098 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
2099 return false;
2100 } else if (getType()->hasPointerRepresentation()) {
2101 if (!EvaluatePointer(this, Result.Val, Info))
2102 return false;
2103 } else if (getType()->isRealFloatingType()) {
2104 llvm::APFloat f(0.0);
2105 if (!EvaluateFloat(this, f, Info))
2106 return false;
2107
2108 Result.Val = APValue(f);
2109 } else if (getType()->isAnyComplexType()) {
2110 if (!EvaluateComplex(this, Result.Val, Info))
2111 return false;
2112 } else
2113 return false;
2114
2115 return true;
2116}
2117
John McCallcd7a4452010-01-05 23:42:56 +00002118bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2119 EvalResult Scratch;
2120 EvalInfo Info(Ctx, Scratch);
2121
2122 return HandleConversionToBool(this, Result, Info);
2123}
2124
Anders Carlsson1b782762009-04-10 04:54:13 +00002125bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2126 EvalInfo Info(Ctx, Result);
2127
2128 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2129}
2130
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002131bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2132 EvalInfo Info(Ctx, Result, true);
2133
2134 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2135}
2136
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002137/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002138/// folded, but discard the result.
2139bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002140 EvalResult Result;
2141 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002142}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002143
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002144bool Expr::HasSideEffects(ASTContext &Ctx) const {
2145 Expr::EvalResult Result;
2146 EvalInfo Info(Ctx, Result);
2147 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2148}
2149
Anders Carlsson51fe9962008-11-22 21:04:56 +00002150APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002151 EvalResult EvalResult;
2152 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002153 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002154 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002155 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002156
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002157 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002158}