blob: 1a44cd02d9c16eecdc0e61db305b46c70d77f8bd [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"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000020#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000021#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000022#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlssonc44eec62008-07-03 04:20:39 +000025using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000028
Chris Lattner87eae5e2008-07-11 22:52:41 +000029/// EvalInfo - This is a private struct used by the evaluator to capture
30/// information about a subexpression as it is folded. It retains information
31/// about the AST context, but also maintains information about the folded
32/// expression.
33///
34/// If an expression could be evaluated, it is still possible it is not a C
35/// "integer constant expression" or constant expression. If not, this struct
36/// captures information about how and why not.
37///
38/// One bit of information passed *into* the request for constant folding
39/// indicates whether the subexpression is "evaluated" or not according to C
40/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
41/// evaluate the expression regardless of what the RHS is, but C only allows
42/// certain things in certain situations.
43struct EvalInfo {
44 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Anders Carlsson54da0492008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000048
Eli Friedmanb2f295c2009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000055};
56
57
Eli Friedman4efaa272008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
62 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000063static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000064static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000065
66//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000067// Misc utilities
68//===----------------------------------------------------------------------===//
69
Eli Friedman5bc86102009-06-14 02:17:33 +000070static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
71 // FIXME: Is this accurate for all kinds of bases? If not, what would
72 // the check look like?
Ken Dycka7305832010-01-15 12:37:54 +000073 Result = Value.getLValueBase() || !Value.getLValueOffset().isZero();
Eli Friedman5bc86102009-06-14 02:17:33 +000074 return true;
75}
76
John McCallcd7a4452010-01-05 23:42:56 +000077static bool HandleConversionToBool(const Expr* E, bool& Result,
78 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +000079 if (E->getType()->isIntegralType()) {
80 APSInt IntResult;
81 if (!EvaluateInteger(E, IntResult, Info))
82 return false;
83 Result = IntResult != 0;
84 return true;
85 } else if (E->getType()->isRealFloatingType()) {
86 APFloat FloatResult(0.0);
87 if (!EvaluateFloat(E, FloatResult, Info))
88 return false;
89 Result = !FloatResult.isZero();
90 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000091 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000092 APValue PointerResult;
93 if (!EvaluatePointer(E, PointerResult, Info))
94 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000095 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000096 } else if (E->getType()->isAnyComplexType()) {
97 APValue ComplexResult;
98 if (!EvaluateComplex(E, ComplexResult, Info))
99 return false;
100 if (ComplexResult.isComplexFloat()) {
101 Result = !ComplexResult.getComplexFloatReal().isZero() ||
102 !ComplexResult.getComplexFloatImag().isZero();
103 } else {
104 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
105 ComplexResult.getComplexIntImag().getBoolValue();
106 }
107 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000108 }
109
110 return false;
111}
112
Mike Stump1eb44332009-09-09 15:08:12 +0000113static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000114 APFloat &Value, ASTContext &Ctx) {
115 unsigned DestWidth = Ctx.getIntWidth(DestType);
116 // Determine whether we are converting to unsigned or signed.
117 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000119 // FIXME: Warning for overflow.
120 uint64_t Space[4];
121 bool ignored;
122 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
123 llvm::APFloat::rmTowardZero, &ignored);
124 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
125}
126
Mike Stump1eb44332009-09-09 15:08:12 +0000127static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000128 APFloat &Value, ASTContext &Ctx) {
129 bool ignored;
130 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000131 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000132 APFloat::rmNearestTiesToEven, &ignored);
133 return Result;
134}
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000137 APSInt &Value, ASTContext &Ctx) {
138 unsigned DestWidth = Ctx.getIntWidth(DestType);
139 APSInt Result = Value;
140 // Figure out if this is a truncate, extend or noop cast.
141 // If the input is signed, do a sign extend, noop, or truncate.
142 Result.extOrTrunc(DestWidth);
143 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
144 return Result;
145}
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000148 APSInt &Value, ASTContext &Ctx) {
149
150 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
151 Result.convertFromAPInt(Value, Value.isSigned(),
152 APFloat::rmNearestTiesToEven);
153 return Result;
154}
155
Mike Stumpc4c90452009-10-27 22:09:17 +0000156namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000157class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000158 : public StmtVisitor<HasSideEffect, bool> {
159 EvalInfo &Info;
160public:
161
162 HasSideEffect(EvalInfo &info) : Info(info) {}
163
164 // Unhandled nodes conservatively default to having side effects.
165 bool VisitStmt(Stmt *S) {
166 return true;
167 }
168
169 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
170 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000171 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000172 return true;
173 return false;
174 }
175 // We don't want to evaluate BlockExprs multiple times, as they generate
176 // a ton of code.
177 bool VisitBlockExpr(BlockExpr *E) { return true; }
178 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
179 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
180 { return Visit(E->getInitializer()); }
181 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
182 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
183 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
184 bool VisitStringLiteral(StringLiteral *E) { return false; }
185 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
186 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
187 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000188 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000189 bool VisitChooseExpr(ChooseExpr *E)
190 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
191 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
192 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000193 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000194 bool VisitBinaryOperator(BinaryOperator *E)
195 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000196 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
198 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
200 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000201 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000202 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000203 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000204 }
205 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
206};
207
Mike Stumpc4c90452009-10-27 22:09:17 +0000208} // end anonymous namespace
209
Eli Friedman4efaa272008-11-12 09:44:48 +0000210//===----------------------------------------------------------------------===//
211// LValue Evaluation
212//===----------------------------------------------------------------------===//
213namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000214class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 : public StmtVisitor<LValueExprEvaluator, APValue> {
216 EvalInfo &Info;
217public:
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Eli Friedman4efaa272008-11-12 09:44:48 +0000219 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
220
221 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000222 return APValue();
223 }
224
225 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000226 APValue VisitDeclRefExpr(DeclRefExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); }
231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000232 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000233 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000234 APValue VisitUnaryExtension(const UnaryOperator *E)
235 { return Visit(E->getSubExpr()); }
236 APValue VisitChooseExpr(const ChooseExpr *E)
237 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000238
239 APValue VisitCastExpr(CastExpr *E) {
240 switch (E->getCastKind()) {
241 default:
242 return APValue();
243
244 case CastExpr::CK_NoOp:
245 return Visit(E->getSubExpr());
246 }
247 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000248 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000249};
250} // end anonymous namespace
251
252static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
253 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
254 return Result.isLValue();
255}
256
Mike Stump1eb44332009-09-09 15:08:12 +0000257APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000258 if (isa<FunctionDecl>(E->getDecl())) {
Ken Dycka7305832010-01-15 12:37:54 +0000259 return APValue(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000260 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000261 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000262 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000263 if (!VD->getType()->isReferenceType())
Ken Dycka7305832010-01-15 12:37:54 +0000264 return APValue(E);
Eli Friedmand933a012009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000266 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000267 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000268 }
269
270 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000271}
272
Eli Friedman4efaa272008-11-12 09:44:48 +0000273APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000274 if (!Info.AnyLValue && !E->isFileScope())
275 return APValue();
Ken Dycka7305832010-01-15 12:37:54 +0000276 return APValue(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000277}
278
279APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
280 APValue result;
281 QualType Ty;
282 if (E->isArrow()) {
283 if (!EvaluatePointer(E->getBase(), result, Info))
284 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000285 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000286 } else {
287 result = Visit(E->getBase());
288 if (result.isUninit())
289 return APValue();
290 Ty = E->getBase()->getType();
291 }
292
Ted Kremenek6217b802009-07-29 21:53:49 +0000293 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000294 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000295
296 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
297 if (!FD) // FIXME: deal with other kinds of member expressions
298 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000299
300 if (FD->getType()->isReferenceType())
301 return APValue();
302
Eli Friedman4efaa272008-11-12 09:44:48 +0000303 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000304 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000305 for (RecordDecl::field_iterator Field = RD->field_begin(),
306 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000307 Field != FieldEnd; (void)++Field, ++i) {
308 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000309 break;
310 }
311
312 result.setLValue(result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000313 result.getLValueOffset() +
314 CharUnits::fromQuantity(RL.getFieldOffset(i) / 8));
Eli Friedman4efaa272008-11-12 09:44:48 +0000315
316 return result;
317}
318
Mike Stump1eb44332009-09-09 15:08:12 +0000319APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000320 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Anders Carlsson3068d112008-11-16 19:01:22 +0000322 if (!EvaluatePointer(E->getBase(), Result, Info))
323 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Anders Carlsson3068d112008-11-16 19:01:22 +0000325 APSInt Index;
326 if (!EvaluateInteger(E->getIdx(), Index, Info))
327 return APValue();
328
Ken Dyck199c3d62010-01-11 17:06:35 +0000329 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
Anders Carlsson3068d112008-11-16 19:01:22 +0000330
Ken Dyck199c3d62010-01-11 17:06:35 +0000331 CharUnits Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000332 Result.setLValue(Result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000333 Result.getLValueOffset() + Offset);
Anders Carlsson3068d112008-11-16 19:01:22 +0000334 return Result;
335}
Eli Friedman4efaa272008-11-12 09:44:48 +0000336
Mike Stump1eb44332009-09-09 15:08:12 +0000337APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000338 APValue Result;
339 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
340 return APValue();
341 return Result;
342}
343
Eli Friedman4efaa272008-11-12 09:44:48 +0000344//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000345// Pointer Evaluation
346//===----------------------------------------------------------------------===//
347
Anders Carlssonc754aa62008-07-08 05:13:58 +0000348namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000349class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000350 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000351 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000352public:
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner87eae5e2008-07-11 22:52:41 +0000354 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000355
Anders Carlsson2bad1682008-07-08 14:30:00 +0000356 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000357 return APValue();
358 }
359
360 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
361
Anders Carlsson650c92f2008-07-08 15:34:11 +0000362 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000363 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000364 APValue VisitUnaryExtension(const UnaryOperator *E)
365 { return Visit(E->getSubExpr()); }
366 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000367 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
Ken Dycka7305832010-01-15 12:37:54 +0000368 { return APValue(E); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000369 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000370 { return APValue(E); }
Eli Friedman3941b182009-01-25 01:54:01 +0000371 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000372 APValue VisitBlockExpr(BlockExpr *E) {
373 if (!E->hasBlockDeclRefExprs())
Ken Dycka7305832010-01-15 12:37:54 +0000374 return APValue(E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000375 return APValue();
376 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000377 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000378 { return APValue((Expr*)0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000379 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000380 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000381 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
382 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000383 { return APValue((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000384 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000385};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000386} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000387
Chris Lattner87eae5e2008-07-11 22:52:41 +0000388static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000389 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000390 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000391 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000392 return Result.isLValue();
393}
394
395APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
396 if (E->getOpcode() != BinaryOperator::Add &&
397 E->getOpcode() != BinaryOperator::Sub)
398 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000400 const Expr *PExp = E->getLHS();
401 const Expr *IExp = E->getRHS();
402 if (IExp->getType()->isPointerType())
403 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000405 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000406 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000407 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000409 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000410 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000411 return APValue();
412
Ted Kremenek6217b802009-07-29 21:53:49 +0000413 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Ken Dyck199c3d62010-01-11 17:06:35 +0000414 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000416 // Explicitly handle GNU void* and function pointer arithmetic extensions.
417 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Ken Dyck199c3d62010-01-11 17:06:35 +0000418 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000419 else
Ken Dyck199c3d62010-01-11 17:06:35 +0000420 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000421
Ken Dycka7305832010-01-15 12:37:54 +0000422 CharUnits Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000423
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000424 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000425 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000426 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000427 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
428
Ken Dycka7305832010-01-15 12:37:54 +0000429 return APValue(ResultLValue.getLValueBase(), Offset);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000430}
Eli Friedman4efaa272008-11-12 09:44:48 +0000431
Eli Friedman2217c872009-02-22 11:46:18 +0000432APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
433 APValue result;
434 if (EvaluateLValue(E->getSubExpr(), result, Info))
435 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000436 return APValue();
437}
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000439
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000440APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
441 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000442
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000443 switch (E->getCastKind()) {
444 default:
445 break;
446
447 case CastExpr::CK_Unknown: {
448 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
449
450 // Check for pointer->pointer cast
451 if (SubExpr->getType()->isPointerType() ||
452 SubExpr->getType()->isObjCObjectPointerType() ||
453 SubExpr->getType()->isNullPtrType() ||
454 SubExpr->getType()->isBlockPointerType())
455 return Visit(SubExpr);
456
457 if (SubExpr->getType()->isIntegralType()) {
458 APValue Result;
459 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
460 break;
461
462 if (Result.isInt()) {
463 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000464 return APValue(0,
465 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000466 }
467
468 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000469 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000470 }
471 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000474 case CastExpr::CK_NoOp:
475 case CastExpr::CK_BitCast:
476 case CastExpr::CK_AnyPointerToObjCPointerCast:
477 case CastExpr::CK_AnyPointerToBlockPointerCast:
478 return Visit(SubExpr);
479
480 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000481 APValue Result;
482 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000483 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000484
485 if (Result.isInt()) {
486 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000487 return APValue(0,
488 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000491 // Cast is of an lvalue, no need to change value.
492 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000493 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000494 case CastExpr::CK_ArrayToPointerDecay:
495 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000496 APValue Result;
497 if (EvaluateLValue(SubExpr, Result, Info))
498 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000499 break;
500 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000501 }
502
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000503 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000504}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000505
Eli Friedman3941b182009-01-25 01:54:01 +0000506APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000507 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000508 Builtin::BI__builtin___CFStringMakeConstantString ||
509 E->isBuiltinCall(Info.Ctx) ==
510 Builtin::BI__builtin___NSStringMakeConstantString)
Ken Dycka7305832010-01-15 12:37:54 +0000511 return APValue(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000512 return APValue();
513}
514
Eli Friedman4efaa272008-11-12 09:44:48 +0000515APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
516 bool BoolResult;
517 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
518 return APValue();
519
520 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
521
522 APValue Result;
523 if (EvaluatePointer(EvalExpr, Result, Info))
524 return Result;
525 return APValue();
526}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000527
528//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000529// Vector Evaluation
530//===----------------------------------------------------------------------===//
531
532namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000533 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000534 : public StmtVisitor<VectorExprEvaluator, APValue> {
535 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000536 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000537 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Nate Begeman59b5da62009-01-18 03:20:47 +0000539 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Nate Begeman59b5da62009-01-18 03:20:47 +0000541 APValue VisitStmt(Stmt *S) {
542 return APValue();
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Eli Friedman91110ee2009-02-23 04:23:56 +0000545 APValue VisitParenExpr(ParenExpr *E)
546 { return Visit(E->getSubExpr()); }
547 APValue VisitUnaryExtension(const UnaryOperator *E)
548 { return Visit(E->getSubExpr()); }
549 APValue VisitUnaryPlus(const UnaryOperator *E)
550 { return Visit(E->getSubExpr()); }
551 APValue VisitUnaryReal(const UnaryOperator *E)
552 { return Visit(E->getSubExpr()); }
553 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
554 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000555 APValue VisitCastExpr(const CastExpr* E);
556 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
557 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000558 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000559 APValue VisitChooseExpr(const ChooseExpr *E)
560 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000561 APValue VisitUnaryImag(const UnaryOperator *E);
562 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000563 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000564 // shufflevector, ExtVectorElementExpr
565 // (Note that these require implementing conversions
566 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000567 };
568} // end anonymous namespace
569
570static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
571 if (!E->getType()->isVectorType())
572 return false;
573 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
574 return !Result.isUninit();
575}
576
577APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000578 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000579 QualType EltTy = VTy->getElementType();
580 unsigned NElts = VTy->getNumElements();
581 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Nate Begeman59b5da62009-01-18 03:20:47 +0000583 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000584 QualType SETy = SE->getType();
585 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000586
Nate Begemane8c9e922009-06-26 18:22:18 +0000587 // Check for vector->vector bitcast and scalar->vector splat.
588 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000589 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000590 } else if (SETy->isIntegerType()) {
591 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000592 if (!EvaluateInteger(SE, IntResult, Info))
593 return APValue();
594 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000595 } else if (SETy->isRealFloatingType()) {
596 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000597 if (!EvaluateFloat(SE, F, Info))
598 return APValue();
599 Result = APValue(F);
600 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000601 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000602
Nate Begemanc0b8b192009-07-01 07:50:47 +0000603 // For casts of a scalar to ExtVector, convert the scalar to the element type
604 // and splat it to all elements.
605 if (E->getType()->isExtVectorType()) {
606 if (EltTy->isIntegerType() && Result.isInt())
607 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
608 Info.Ctx));
609 else if (EltTy->isIntegerType())
610 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
611 Info.Ctx));
612 else if (EltTy->isRealFloatingType() && Result.isInt())
613 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
614 Info.Ctx));
615 else if (EltTy->isRealFloatingType())
616 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
617 Info.Ctx));
618 else
619 return APValue();
620
621 // Splat and create vector APValue.
622 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
623 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000624 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000625
626 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
627 // to the vector. To construct the APValue vector initializer, bitcast the
628 // initializing value to an APInt, and shift out the bits pertaining to each
629 // element.
630 APSInt Init;
631 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Nate Begemanc0b8b192009-07-01 07:50:47 +0000633 llvm::SmallVector<APValue, 4> Elts;
634 for (unsigned i = 0; i != NElts; ++i) {
635 APSInt Tmp = Init;
636 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Nate Begemanc0b8b192009-07-01 07:50:47 +0000638 if (EltTy->isIntegerType())
639 Elts.push_back(APValue(Tmp));
640 else if (EltTy->isRealFloatingType())
641 Elts.push_back(APValue(APFloat(Tmp)));
642 else
643 return APValue();
644
645 Init >>= EltWidth;
646 }
647 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000648}
649
Mike Stump1eb44332009-09-09 15:08:12 +0000650APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000651VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
652 return this->Visit(const_cast<Expr*>(E->getInitializer()));
653}
654
Mike Stump1eb44332009-09-09 15:08:12 +0000655APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000656VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000657 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000658 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000659 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Nate Begeman59b5da62009-01-18 03:20:47 +0000661 QualType EltTy = VT->getElementType();
662 llvm::SmallVector<APValue, 4> Elements;
663
Eli Friedman91110ee2009-02-23 04:23:56 +0000664 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000665 if (EltTy->isIntegerType()) {
666 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000667 if (i < NumInits) {
668 if (!EvaluateInteger(E->getInit(i), sInt, Info))
669 return APValue();
670 } else {
671 sInt = Info.Ctx.MakeIntValue(0, EltTy);
672 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000673 Elements.push_back(APValue(sInt));
674 } else {
675 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000676 if (i < NumInits) {
677 if (!EvaluateFloat(E->getInit(i), f, Info))
678 return APValue();
679 } else {
680 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
681 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000682 Elements.push_back(APValue(f));
683 }
684 }
685 return APValue(&Elements[0], Elements.size());
686}
687
Mike Stump1eb44332009-09-09 15:08:12 +0000688APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000689VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000690 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000691 QualType EltTy = VT->getElementType();
692 APValue ZeroElement;
693 if (EltTy->isIntegerType())
694 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
695 else
696 ZeroElement =
697 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
698
699 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
700 return APValue(&Elements[0], Elements.size());
701}
702
703APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
704 bool BoolResult;
705 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
706 return APValue();
707
708 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
709
710 APValue Result;
711 if (EvaluateVector(EvalExpr, Result, Info))
712 return Result;
713 return APValue();
714}
715
Eli Friedman91110ee2009-02-23 04:23:56 +0000716APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
717 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
718 Info.EvalResult.HasSideEffects = true;
719 return GetZeroVector(E->getType());
720}
721
Nate Begeman59b5da62009-01-18 03:20:47 +0000722//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000723// Integer Evaluation
724//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000725
726namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000727class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000728 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000729 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000730 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000731public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000732 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000733 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000734
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000735 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000736 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000737 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000738 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000739 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000740 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000741 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000742 return true;
743 }
744
Daniel Dunbar131eb432009-02-19 09:06:44 +0000745 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000746 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000747 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000748 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000749 Result = APValue(APSInt(I));
750 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000751 return true;
752 }
753
754 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000755 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000756 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000757 return true;
758 }
759
Anders Carlsson82206e22008-11-30 18:14:57 +0000760 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000761 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000762 if (Info.EvalResult.Diag == 0) {
763 Info.EvalResult.DiagLoc = L;
764 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000765 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000766 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000767 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Anders Carlssonc754aa62008-07-08 05:13:58 +0000770 //===--------------------------------------------------------------------===//
771 // Visitor Methods
772 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner32fea9d2008-11-12 07:43:42 +0000774 bool VisitStmt(Stmt *) {
775 assert(0 && "This should be called on integers, stmts are not integers");
776 return false;
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattner32fea9d2008-11-12 07:43:42 +0000779 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000780 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnerb542afe2008-07-11 19:10:17 +0000783 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000784
Chris Lattner4c4867e2008-07-12 00:38:25 +0000785 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000786 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000787 }
788 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000789 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000790 }
791 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000792 // Per gcc docs "this built-in function ignores top level
793 // qualifiers". We need to use the canonical version to properly
794 // be able to strip CRV qualifiers from the type.
795 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
796 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000797 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000798 T1.getUnqualifiedType()),
799 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000800 }
Eli Friedman04309752009-11-24 05:28:59 +0000801
802 bool CheckReferencedDecl(const Expr *E, const Decl *D);
803 bool VisitDeclRefExpr(const DeclRefExpr *E) {
804 return CheckReferencedDecl(E, E->getDecl());
805 }
806 bool VisitMemberExpr(const MemberExpr *E) {
807 if (CheckReferencedDecl(E, E->getMemberDecl())) {
808 // Conservatively assume a MemberExpr will have side-effects
809 Info.EvalResult.HasSideEffects = true;
810 return true;
811 }
812 return false;
813 }
814
Eli Friedmanc4a26382010-02-13 00:10:10 +0000815 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000816 bool VisitBinaryOperator(const BinaryOperator *E);
817 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000818 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000819
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000820 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000821 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
822
Anders Carlsson3068d112008-11-16 19:01:22 +0000823 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000824 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Anders Carlsson3f704562008-12-21 22:39:40 +0000827 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000828 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Anders Carlsson3068d112008-11-16 19:01:22 +0000831 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000832 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000833 }
834
Eli Friedman664a1042009-02-27 04:45:43 +0000835 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
836 return Success(0, E);
837 }
838
Sebastian Redl64b45f72009-01-05 20:52:13 +0000839 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000840 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000841 }
842
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000843 bool VisitChooseExpr(const ChooseExpr *E) {
844 return Visit(E->getChosenSubExpr(Info.Ctx));
845 }
846
Eli Friedman722c7172009-02-28 03:59:05 +0000847 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000848 bool VisitUnaryImag(const UnaryOperator *E);
849
Chris Lattnerfcee0012008-07-11 21:24:13 +0000850private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000851 CharUnits GetAlignOfExpr(const Expr *E);
852 CharUnits GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000853 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000854};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000855} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000856
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000857static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000858 if (!E->getType()->isIntegralType())
859 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000861 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
862}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000863
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000864static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
865 APValue Val;
866 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
867 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000868 Result = Val.getInt();
869 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000870}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000871
Eli Friedman04309752009-11-24 05:28:59 +0000872bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000873 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000874 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
875 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000876
877 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000878 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000879 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
880 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000881
882 if (isa<ParmVarDecl>(D))
883 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
884
Eli Friedman04309752009-11-24 05:28:59 +0000885 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000886 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000887 if (APValue *V = VD->getEvaluatedValue()) {
888 if (V->isInt())
889 return Success(V->getInt(), E);
890 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
891 }
892
893 if (VD->isEvaluatingValue())
894 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
895
896 VD->setEvaluatingValue();
897
Douglas Gregor78d15832009-05-26 18:54:04 +0000898 if (Visit(const_cast<Expr*>(Init))) {
899 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000900 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000901 return true;
902 }
903
Eli Friedmanc0131182009-12-03 20:31:57 +0000904 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000905 return false;
906 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000907 }
908 }
909
Chris Lattner4c4867e2008-07-12 00:38:25 +0000910 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000911 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000912}
913
Chris Lattnera4d55d82008-10-06 06:40:35 +0000914/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
915/// as GCC.
916static int EvaluateBuiltinClassifyType(const CallExpr *E) {
917 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000918 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000919 enum gcc_type_class {
920 no_type_class = -1,
921 void_type_class, integer_type_class, char_type_class,
922 enumeral_type_class, boolean_type_class,
923 pointer_type_class, reference_type_class, offset_type_class,
924 real_type_class, complex_type_class,
925 function_type_class, method_type_class,
926 record_type_class, union_type_class,
927 array_type_class, string_type_class,
928 lang_type_class
929 };
Mike Stump1eb44332009-09-09 15:08:12 +0000930
931 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000932 // ideal, however it is what gcc does.
933 if (E->getNumArgs() == 0)
934 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattnera4d55d82008-10-06 06:40:35 +0000936 QualType ArgTy = E->getArg(0)->getType();
937 if (ArgTy->isVoidType())
938 return void_type_class;
939 else if (ArgTy->isEnumeralType())
940 return enumeral_type_class;
941 else if (ArgTy->isBooleanType())
942 return boolean_type_class;
943 else if (ArgTy->isCharType())
944 return string_type_class; // gcc doesn't appear to use char_type_class
945 else if (ArgTy->isIntegerType())
946 return integer_type_class;
947 else if (ArgTy->isPointerType())
948 return pointer_type_class;
949 else if (ArgTy->isReferenceType())
950 return reference_type_class;
951 else if (ArgTy->isRealType())
952 return real_type_class;
953 else if (ArgTy->isComplexType())
954 return complex_type_class;
955 else if (ArgTy->isFunctionType())
956 return function_type_class;
957 else if (ArgTy->isStructureType())
958 return record_type_class;
959 else if (ArgTy->isUnionType())
960 return union_type_class;
961 else if (ArgTy->isArrayType())
962 return array_type_class;
963 else if (ArgTy->isUnionType())
964 return union_type_class;
965 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
966 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
967 return -1;
968}
969
Eli Friedmanc4a26382010-02-13 00:10:10 +0000970bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000971 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000972 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000973 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000974
975 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000976 const Expr *Arg = E->getArg(0)->IgnoreParens();
977 Expr::EvalResult Base;
Eric Christopherb2aaf512010-01-19 22:58:35 +0000978
979 // TODO: Perhaps we should let LLVM lower this?
Mike Stump660e6f72009-10-26 23:05:19 +0000980 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000981 && Base.Val.getKind() == APValue::LValue
982 && !Base.HasSideEffects)
983 if (const Expr *LVBase = Base.Val.getLValueBase())
984 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
985 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000986 if (!VD->getType()->isIncompleteType()
987 && VD->getType()->isObjectType()
988 && !VD->getType()->isVariablyModifiedType()
989 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000990 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +0000991 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +0000992 if (!Offset.isNegative() && Offset <= Size)
993 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +0000994 else
Ken Dyck199c3d62010-01-11 17:06:35 +0000995 Size = CharUnits::Zero();
996 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +0000997 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000998 }
999 }
1000
Eric Christopherb2aaf512010-01-19 22:58:35 +00001001 // If evaluating the argument has side-effects we can't determine
1002 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001003 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001004 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001005 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001006 return Success(0, E);
1007 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001008
Mike Stump64eda9e2009-10-26 18:35:08 +00001009 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1010 }
1011
Chris Lattner019f4e82008-10-06 05:28:25 +00001012 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001013 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001015 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001016 // __builtin_constant_p always has one operand: it returns true if that
1017 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001018 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001019
1020 case Builtin::BI__builtin_eh_return_data_regno: {
1021 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1022 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1023 return Success(Operand, E);
1024 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001025
1026 case Builtin::BI__builtin_expect:
1027 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001028 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001029}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001030
Chris Lattnerb542afe2008-07-11 19:10:17 +00001031bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001032 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001033 if (!Visit(E->getRHS()))
1034 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001035
Eli Friedman33ef1452009-02-26 10:19:36 +00001036 // If we can't evaluate the LHS, it might have side effects;
1037 // conservatively mark it.
1038 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1039 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001040
Anders Carlsson027f62e2008-12-01 02:07:06 +00001041 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001042 }
1043
1044 if (E->isLogicalOp()) {
1045 // These need to be handled specially because the operands aren't
1046 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001047 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001049 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001050 // We were able to evaluate the LHS, see if we can get away with not
1051 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001052 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001053 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001054
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001055 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001056 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001057 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001058 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001059 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001060 }
1061 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001062 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001063 // We can't evaluate the LHS; however, sometimes the result
1064 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001065 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001066 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001067 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001068 // must have had side effects.
1069 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001070
1071 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001072 }
1073 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001074 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001075
Eli Friedmana6afa762008-11-13 06:09:17 +00001076 return false;
1077 }
1078
Anders Carlsson286f85e2008-11-16 07:17:21 +00001079 QualType LHSTy = E->getLHS()->getType();
1080 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001081
1082 if (LHSTy->isAnyComplexType()) {
1083 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1084 APValue LHS, RHS;
1085
1086 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1087 return false;
1088
1089 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1090 return false;
1091
1092 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001093 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001094 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001095 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001096 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1097
Daniel Dunbar4087e242009-01-29 06:43:41 +00001098 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001099 return Success((CR_r == APFloat::cmpEqual &&
1100 CR_i == APFloat::cmpEqual), E);
1101 else {
1102 assert(E->getOpcode() == BinaryOperator::NE &&
1103 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001104 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001105 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001106 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001107 CR_i == APFloat::cmpLessThan)), E);
1108 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001109 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001110 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001111 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1112 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1113 else {
1114 assert(E->getOpcode() == BinaryOperator::NE &&
1115 "Invalid compex comparison.");
1116 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1117 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1118 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001119 }
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Anders Carlsson286f85e2008-11-16 07:17:21 +00001122 if (LHSTy->isRealFloatingType() &&
1123 RHSTy->isRealFloatingType()) {
1124 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Anders Carlsson286f85e2008-11-16 07:17:21 +00001126 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1127 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Anders Carlsson286f85e2008-11-16 07:17:21 +00001129 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1130 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Anders Carlsson286f85e2008-11-16 07:17:21 +00001132 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001133
Anders Carlsson286f85e2008-11-16 07:17:21 +00001134 switch (E->getOpcode()) {
1135 default:
1136 assert(0 && "Invalid binary operator!");
1137 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001138 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001139 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001140 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001141 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001142 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001143 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001144 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001145 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001146 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001147 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001148 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001149 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001150 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001151 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001154 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1155 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001156 APValue LHSValue;
1157 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1158 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001159
Anders Carlsson3068d112008-11-16 19:01:22 +00001160 APValue RHSValue;
1161 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1162 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001163
Eli Friedman5bc86102009-06-14 02:17:33 +00001164 // Reject any bases from the normal codepath; we special-case comparisons
1165 // to null.
1166 if (LHSValue.getLValueBase()) {
1167 if (!E->isEqualityOp())
1168 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001169 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001170 return false;
1171 bool bres;
1172 if (!EvalPointerValueAsBool(LHSValue, bres))
1173 return false;
1174 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1175 } else if (RHSValue.getLValueBase()) {
1176 if (!E->isEqualityOp())
1177 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001178 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001179 return false;
1180 bool bres;
1181 if (!EvalPointerValueAsBool(RHSValue, bres))
1182 return false;
1183 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1184 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001185
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001186 if (E->getOpcode() == BinaryOperator::Sub) {
1187 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001188 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001189
Ken Dycka7305832010-01-15 12:37:54 +00001190 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001191 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001192 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001193
Ken Dycka7305832010-01-15 12:37:54 +00001194 CharUnits Diff = LHSValue.getLValueOffset() -
1195 RHSValue.getLValueOffset();
1196 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001197 }
1198 bool Result;
1199 if (E->getOpcode() == BinaryOperator::EQ) {
1200 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001201 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001202 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1203 }
1204 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001205 }
1206 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001207 if (!LHSTy->isIntegralType() ||
1208 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001209 // We can't continue from here for non-integral types, and they
1210 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001211 return false;
1212 }
1213
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001214 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001215 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001216 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001217
Eli Friedman42edd0d2009-03-24 01:14:50 +00001218 APValue RHSVal;
1219 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001220 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001221
1222 // Handle cases like (unsigned long)&a + 4.
1223 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001224 CharUnits Offset = Result.getLValueOffset();
1225 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1226 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001227 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001228 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001229 else
Ken Dycka7305832010-01-15 12:37:54 +00001230 Offset -= AdditionalOffset;
1231 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001232 return true;
1233 }
1234
1235 // Handle cases like 4 + (unsigned long)&a
1236 if (E->getOpcode() == BinaryOperator::Add &&
1237 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001238 CharUnits Offset = RHSVal.getLValueOffset();
1239 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1240 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001241 return true;
1242 }
1243
1244 // All the following cases expect both operands to be an integer
1245 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001246 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001247
Eli Friedman42edd0d2009-03-24 01:14:50 +00001248 APSInt& RHS = RHSVal.getInt();
1249
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001250 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001251 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001252 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001253 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1254 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1255 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1256 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1257 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1258 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001259 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001260 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001261 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001262 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001263 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001264 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001265 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001266 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001267 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001268 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001269 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001270 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1271 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001272 }
1273 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001274 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001275 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1276 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001279 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1280 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1281 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1282 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1283 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1284 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001285 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001286}
1287
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001288bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001289 bool Cond;
1290 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001291 return false;
1292
Nuno Lopesa25bd552008-11-16 22:06:39 +00001293 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001294}
1295
Ken Dyck8b752f12010-01-27 17:10:57 +00001296CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001297 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1298 // the result is the size of the referenced type."
1299 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1300 // result shall be the alignment of the referenced type."
1301 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1302 T = Ref->getPointeeType();
1303
Chris Lattnere9feb472009-01-24 21:09:06 +00001304 // Get information about the alignment.
1305 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001306
Eli Friedman2be58612009-05-30 21:09:44 +00001307 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001308 return CharUnits::fromQuantity(
1309 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001310}
1311
Ken Dyck8b752f12010-01-27 17:10:57 +00001312CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001313 E = E->IgnoreParens();
1314
1315 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001316 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001317 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001318 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1319 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001320
Chris Lattneraf707ab2009-01-24 21:53:27 +00001321 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001322 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1323 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001324
Chris Lattnere9feb472009-01-24 21:09:06 +00001325 return GetAlignOfType(E->getType());
1326}
1327
1328
Sebastian Redl05189992008-11-11 17:56:53 +00001329/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1330/// expression's type.
1331bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001332 // Handle alignof separately.
1333 if (!E->isSizeOf()) {
1334 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001335 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001336 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001337 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001338 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001339
Sebastian Redl05189992008-11-11 17:56:53 +00001340 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001341 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1342 // the result is the size of the referenced type."
1343 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1344 // result shall be the alignment of the referenced type."
1345 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1346 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001347
Daniel Dunbar131eb432009-02-19 09:06:44 +00001348 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1349 // extension.
1350 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1351 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001352
Chris Lattnerfcee0012008-07-11 21:24:13 +00001353 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001354 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001355 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001356
Chris Lattnere9feb472009-01-24 21:09:06 +00001357 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001358 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001359}
1360
Chris Lattnerb542afe2008-07-11 19:10:17 +00001361bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001362 // Special case unary operators that do not need their subexpression
1363 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001364 if (E->isOffsetOfOp()) {
1365 // The AST for offsetof is defined in such a way that we can just
1366 // directly Evaluate it as an l-value.
1367 APValue LV;
1368 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1369 return false;
1370 if (LV.getLValueBase())
1371 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001372 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001373 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001374
1375 if (E->getOpcode() == UnaryOperator::LNot) {
1376 // LNot's operand isn't necessarily an integer, so we handle it specially.
1377 bool bres;
1378 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1379 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001380 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001381 }
1382
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001383 // Only handle integral operations...
1384 if (!E->getSubExpr()->getType()->isIntegralType())
1385 return false;
1386
Chris Lattner87eae5e2008-07-11 22:52:41 +00001387 // Get the operand value into 'Result'.
1388 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001389 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001390
Chris Lattner75a48812008-07-11 22:15:16 +00001391 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001392 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001393 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1394 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001395 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001396 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001397 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1398 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001399 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001400 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001401 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001402 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001403 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001404 if (!Result.isInt()) return false;
1405 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001406 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001407 if (!Result.isInt()) return false;
1408 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001409 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001410}
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Chris Lattner732b2232008-07-12 01:15:53 +00001412/// HandleCast - This is used to evaluate implicit or explicit casts where the
1413/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001414bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001415 Expr *SubExpr = E->getSubExpr();
1416 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001417 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001418
Eli Friedman4efaa272008-11-12 09:44:48 +00001419 if (DestType->isBooleanType()) {
1420 bool BoolResult;
1421 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1422 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001423 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001424 }
1425
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001426 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001427 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001428 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001429 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001430
Eli Friedmanbe265702009-02-20 01:15:07 +00001431 if (!Result.isInt()) {
1432 // Only allow casts of lvalues if they are lossless.
1433 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1434 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001435
Daniel Dunbardd211642009-02-19 22:24:01 +00001436 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001437 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Chris Lattner732b2232008-07-12 01:15:53 +00001440 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001441 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001442 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001443 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001444 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001445
Daniel Dunbardd211642009-02-19 22:24:01 +00001446 if (LV.getLValueBase()) {
1447 // Only allow based lvalue casts if they are lossless.
1448 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1449 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001450
Daniel Dunbardd211642009-02-19 22:24:01 +00001451 Result = LV;
1452 return true;
1453 }
1454
Ken Dycka7305832010-01-15 12:37:54 +00001455 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1456 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001457 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001458 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001459
Eli Friedmanbe265702009-02-20 01:15:07 +00001460 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1461 // This handles double-conversion cases, where there's both
1462 // an l-value promotion and an implicit conversion to int.
1463 APValue LV;
1464 if (!EvaluateLValue(SubExpr, LV, Info))
1465 return false;
1466
1467 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1468 return false;
1469
1470 Result = LV;
1471 return true;
1472 }
1473
Eli Friedman1725f682009-04-22 19:23:09 +00001474 if (SrcType->isAnyComplexType()) {
1475 APValue C;
1476 if (!EvaluateComplex(SubExpr, C, Info))
1477 return false;
1478 if (C.isComplexFloat())
1479 return Success(HandleFloatToIntCast(DestType, SrcType,
1480 C.getComplexFloatReal(), Info.Ctx),
1481 E);
1482 else
1483 return Success(HandleIntToIntCast(DestType, SrcType,
1484 C.getComplexIntReal(), Info.Ctx), E);
1485 }
Eli Friedman2217c872009-02-22 11:46:18 +00001486 // FIXME: Handle vectors
1487
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001488 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001489 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001490
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001491 APFloat F(0.0);
1492 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001493 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001495 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001496}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001497
Eli Friedman722c7172009-02-28 03:59:05 +00001498bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1499 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1500 APValue LV;
1501 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1502 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1503 return Success(LV.getComplexIntReal(), E);
1504 }
1505
1506 return Visit(E->getSubExpr());
1507}
1508
Eli Friedman664a1042009-02-27 04:45:43 +00001509bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001510 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1511 APValue LV;
1512 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1513 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1514 return Success(LV.getComplexIntImag(), E);
1515 }
1516
Eli Friedman664a1042009-02-27 04:45:43 +00001517 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1518 Info.EvalResult.HasSideEffects = true;
1519 return Success(0, E);
1520}
1521
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001522//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001523// Float Evaluation
1524//===----------------------------------------------------------------------===//
1525
1526namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001527class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001528 : public StmtVisitor<FloatExprEvaluator, bool> {
1529 EvalInfo &Info;
1530 APFloat &Result;
1531public:
1532 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1533 : Info(info), Result(result) {}
1534
1535 bool VisitStmt(Stmt *S) {
1536 return false;
1537 }
1538
1539 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001540 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001541
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001542 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001543 bool VisitBinaryOperator(const BinaryOperator *E);
1544 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001545 bool VisitCastExpr(CastExpr *E);
1546 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001547 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001548
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001549 bool VisitChooseExpr(const ChooseExpr *E)
1550 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1551 bool VisitUnaryExtension(const UnaryOperator *E)
1552 { return Visit(E->getSubExpr()); }
1553
1554 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001555 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001556};
1557} // end anonymous namespace
1558
1559static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1560 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1561}
1562
Chris Lattner019f4e82008-10-06 05:28:25 +00001563bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001564 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001565 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001566 case Builtin::BI__builtin_huge_val:
1567 case Builtin::BI__builtin_huge_valf:
1568 case Builtin::BI__builtin_huge_vall:
1569 case Builtin::BI__builtin_inf:
1570 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001571 case Builtin::BI__builtin_infl: {
1572 const llvm::fltSemantics &Sem =
1573 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001574 Result = llvm::APFloat::getInf(Sem);
1575 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Chris Lattner9e621712008-10-06 06:31:58 +00001578 case Builtin::BI__builtin_nan:
1579 case Builtin::BI__builtin_nanf:
1580 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001581 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001582 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001583 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001584 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001585 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001586 const llvm::fltSemantics &Sem =
1587 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer33035802009-12-11 13:26:32 +00001588 unsigned Type = 0;
1589 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump4572bab2009-05-30 03:56:50 +00001590 return false;
Benjamin Kramer33035802009-12-11 13:26:32 +00001591 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner9e621712008-10-06 06:31:58 +00001592 return true;
1593 }
1594 }
1595 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001596
1597 case Builtin::BI__builtin_fabs:
1598 case Builtin::BI__builtin_fabsf:
1599 case Builtin::BI__builtin_fabsl:
1600 if (!EvaluateFloat(E->getArg(0), Result, Info))
1601 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001603 if (Result.isNegative())
1604 Result.changeSign();
1605 return true;
1606
Mike Stump1eb44332009-09-09 15:08:12 +00001607 case Builtin::BI__builtin_copysign:
1608 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001609 case Builtin::BI__builtin_copysignl: {
1610 APFloat RHS(0.);
1611 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1612 !EvaluateFloat(E->getArg(1), RHS, Info))
1613 return false;
1614 Result.copySign(RHS);
1615 return true;
1616 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001617 }
1618}
1619
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001620bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001621 if (E->getOpcode() == UnaryOperator::Deref)
1622 return false;
1623
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001624 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1625 return false;
1626
1627 switch (E->getOpcode()) {
1628 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001629 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001630 return true;
1631 case UnaryOperator::Minus:
1632 Result.changeSign();
1633 return true;
1634 }
1635}
Chris Lattner019f4e82008-10-06 05:28:25 +00001636
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001637bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001638 if (E->getOpcode() == BinaryOperator::Comma) {
1639 if (!EvaluateFloat(E->getRHS(), Result, Info))
1640 return false;
1641
1642 // If we can't evaluate the LHS, it might have side effects;
1643 // conservatively mark it.
1644 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1645 Info.EvalResult.HasSideEffects = true;
1646
1647 return true;
1648 }
1649
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001650 // FIXME: Diagnostics? I really don't understand how the warnings
1651 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001652 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001653 if (!EvaluateFloat(E->getLHS(), Result, Info))
1654 return false;
1655 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1656 return false;
1657
1658 switch (E->getOpcode()) {
1659 default: return false;
1660 case BinaryOperator::Mul:
1661 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1662 return true;
1663 case BinaryOperator::Add:
1664 Result.add(RHS, APFloat::rmNearestTiesToEven);
1665 return true;
1666 case BinaryOperator::Sub:
1667 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1668 return true;
1669 case BinaryOperator::Div:
1670 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1671 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001672 }
1673}
1674
1675bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1676 Result = E->getValue();
1677 return true;
1678}
1679
Eli Friedman4efaa272008-11-12 09:44:48 +00001680bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1681 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Eli Friedman4efaa272008-11-12 09:44:48 +00001683 if (SubExpr->getType()->isIntegralType()) {
1684 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001685 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001686 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001687 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001688 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001689 return true;
1690 }
1691 if (SubExpr->getType()->isRealFloatingType()) {
1692 if (!Visit(SubExpr))
1693 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001694 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1695 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001696 return true;
1697 }
Eli Friedman2217c872009-02-22 11:46:18 +00001698 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001699
1700 return false;
1701}
1702
1703bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1704 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1705 return true;
1706}
1707
Eli Friedman67f85fc2009-12-04 02:12:53 +00001708bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1709 bool Cond;
1710 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1711 return false;
1712
1713 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1714}
1715
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001716//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001717// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001718//===----------------------------------------------------------------------===//
1719
1720namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001721class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001722 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001723 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001725public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001726 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001728 //===--------------------------------------------------------------------===//
1729 // Visitor Methods
1730 //===--------------------------------------------------------------------===//
1731
1732 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001733 return APValue();
1734 }
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001736 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1737
1738 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001739 Expr* SubExpr = E->getSubExpr();
1740
1741 if (SubExpr->getType()->isRealFloatingType()) {
1742 APFloat Result(0.0);
1743
1744 if (!EvaluateFloat(SubExpr, Result, Info))
1745 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001746
1747 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001748 Result);
1749 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001750 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001751 "Unexpected imaginary literal.");
1752
1753 llvm::APSInt Result;
1754 if (!EvaluateInteger(SubExpr, Result, Info))
1755 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001757 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1758 Zero = 0;
1759 return APValue(Zero, Result);
1760 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001761 }
1762
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001763 APValue VisitCastExpr(CastExpr *E) {
1764 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001765 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001766 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001767
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001768 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001769 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001770
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001771 if (!EvaluateFloat(SubExpr, Result, Info))
1772 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001773
1774 if (EltType->isRealFloatingType()) {
1775 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001776 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001777 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1778 } else {
1779 llvm::APSInt IResult;
1780 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1781 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1782 Zero = 0;
1783 return APValue(IResult, Zero);
1784 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001785 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001786 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001787
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001788 if (!EvaluateInteger(SubExpr, Result, Info))
1789 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001790
Eli Friedman1725f682009-04-22 19:23:09 +00001791 if (EltType->isRealFloatingType()) {
1792 APFloat FResult =
1793 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001794 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001795 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1796 } else {
1797 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1798 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1799 Zero = 0;
1800 return APValue(Result, Zero);
1801 }
John McCall183700f2009-09-21 23:43:11 +00001802 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001803 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001804
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001805 if (!EvaluateComplex(SubExpr, Src, Info))
1806 return APValue();
1807
1808 QualType SrcType = CT->getElementType();
1809
1810 if (Src.isComplexFloat()) {
1811 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001812 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001813 Src.getComplexFloatReal(),
1814 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001815 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001816 Src.getComplexFloatImag(),
1817 Info.Ctx));
1818 } else {
1819 return APValue(HandleFloatToIntCast(EltType, SrcType,
1820 Src.getComplexFloatReal(),
1821 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001822 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001823 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001824 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001825 }
1826 } else {
1827 assert(Src.isComplexInt() && "Invalid evaluate result.");
1828 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001829 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001830 Src.getComplexIntReal(),
1831 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001832 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001833 Src.getComplexIntImag(),
1834 Info.Ctx));
1835 } else {
1836 return APValue(HandleIntToIntCast(EltType, SrcType,
1837 Src.getComplexIntReal(),
1838 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001839 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001840 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001841 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001842 }
1843 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001844 }
1845
1846 // FIXME: Handle more casts.
1847 return APValue();
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001850 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001851 APValue VisitChooseExpr(const ChooseExpr *E)
1852 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1853 APValue VisitUnaryExtension(const UnaryOperator *E)
1854 { return Visit(E->getSubExpr()); }
1855 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001856 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001857};
1858} // end anonymous namespace
1859
Mike Stump1eb44332009-09-09 15:08:12 +00001860static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001861 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1862 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001863 (&Result.getComplexFloatReal().getSemantics() ==
1864 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001865 "Invalid complex evaluation.");
1866 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001867}
1868
Mike Stump1eb44332009-09-09 15:08:12 +00001869APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001870 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001872 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001873 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001875 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001876 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001877
Daniel Dunbar3f279872009-01-29 01:32:56 +00001878 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1879 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001880 switch (E->getOpcode()) {
1881 default: return APValue();
1882 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001883 if (Result.isComplexFloat()) {
1884 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1885 APFloat::rmNearestTiesToEven);
1886 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1887 APFloat::rmNearestTiesToEven);
1888 } else {
1889 Result.getComplexIntReal() += RHS.getComplexIntReal();
1890 Result.getComplexIntImag() += RHS.getComplexIntImag();
1891 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001892 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001893 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001894 if (Result.isComplexFloat()) {
1895 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1896 APFloat::rmNearestTiesToEven);
1897 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1898 APFloat::rmNearestTiesToEven);
1899 } else {
1900 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1901 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1902 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001903 break;
1904 case BinaryOperator::Mul:
1905 if (Result.isComplexFloat()) {
1906 APValue LHS = Result;
1907 APFloat &LHS_r = LHS.getComplexFloatReal();
1908 APFloat &LHS_i = LHS.getComplexFloatImag();
1909 APFloat &RHS_r = RHS.getComplexFloatReal();
1910 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Daniel Dunbar3f279872009-01-29 01:32:56 +00001912 APFloat Tmp = LHS_r;
1913 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1914 Result.getComplexFloatReal() = Tmp;
1915 Tmp = LHS_i;
1916 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1917 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1918
1919 Tmp = LHS_r;
1920 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1921 Result.getComplexFloatImag() = Tmp;
1922 Tmp = LHS_i;
1923 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1924 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1925 } else {
1926 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001927 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001928 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1929 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001930 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001931 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1932 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1933 }
1934 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001935 }
1936
1937 return Result;
1938}
1939
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001940//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001941// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001942//===----------------------------------------------------------------------===//
1943
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001944/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001945/// any crazy technique (that has nothing to do with language standards) that
1946/// we want to. If this function returns true, it returns the folded constant
1947/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001948bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1949 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001950
Nate Begeman59b5da62009-01-18 03:20:47 +00001951 if (getType()->isVectorType()) {
1952 if (!EvaluateVector(this, Result.Val, Info))
1953 return false;
1954 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001955 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001956 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001957 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001958 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001959 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001960 } else if (getType()->isRealFloatingType()) {
1961 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001962 if (!EvaluateFloat(this, f, Info))
1963 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001965 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001966 } else if (getType()->isAnyComplexType()) {
1967 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001968 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001969 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001970 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001971
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001972 return true;
1973}
1974
Mike Stump660e6f72009-10-26 23:05:19 +00001975bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1976 EvalInfo Info(Ctx, Result, true);
1977
1978 if (getType()->isVectorType()) {
1979 if (!EvaluateVector(this, Result.Val, Info))
1980 return false;
1981 } else if (getType()->isIntegerType()) {
1982 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1983 return false;
1984 } else if (getType()->hasPointerRepresentation()) {
1985 if (!EvaluatePointer(this, Result.Val, Info))
1986 return false;
1987 } else if (getType()->isRealFloatingType()) {
1988 llvm::APFloat f(0.0);
1989 if (!EvaluateFloat(this, f, Info))
1990 return false;
1991
1992 Result.Val = APValue(f);
1993 } else if (getType()->isAnyComplexType()) {
1994 if (!EvaluateComplex(this, Result.Val, Info))
1995 return false;
1996 } else
1997 return false;
1998
1999 return true;
2000}
2001
John McCallcd7a4452010-01-05 23:42:56 +00002002bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2003 EvalResult Scratch;
2004 EvalInfo Info(Ctx, Scratch);
2005
2006 return HandleConversionToBool(this, Result, Info);
2007}
2008
Anders Carlsson1b782762009-04-10 04:54:13 +00002009bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2010 EvalInfo Info(Ctx, Result);
2011
2012 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2013}
2014
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002015bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2016 EvalInfo Info(Ctx, Result, true);
2017
2018 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2019}
2020
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002021/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002022/// folded, but discard the result.
2023bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002024 EvalResult Result;
2025 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002026}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002027
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002028bool Expr::HasSideEffects(ASTContext &Ctx) const {
2029 Expr::EvalResult Result;
2030 EvalInfo Info(Ctx, Result);
2031 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2032}
2033
Anders Carlsson51fe9962008-11-22 21:04:56 +00002034APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002035 EvalResult EvalResult;
2036 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002037 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002038 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002039 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002040
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002041 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002042}