blob: eeeeb5c836b815052e5271ac49166bd6364f04b5 [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
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000409 llvm::APSInt AdditionalOffset;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000410 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000411 return APValue();
412
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000413 // Compute the new offset in the appropriate width.
414
415 QualType PointeeType =
416 PExp->getType()->getAs<PointerType>()->getPointeeType();
417 llvm::APSInt SizeOfPointee(AdditionalOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000419 // Explicitly handle GNU void* and function pointer arithmetic extensions.
420 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000421 SizeOfPointee = 1;
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000422 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000423 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType).getQuantity();
Eli Friedman4efaa272008-11-12 09:44:48 +0000424
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000425 llvm::APSInt Offset(AdditionalOffset);
426 Offset = ResultLValue.getLValueOffset().getQuantity();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427 if (E->getOpcode() == BinaryOperator::Add)
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000428 Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000429 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000430 Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000431
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000432 // Sign extend prior to converting back to a char unit.
433 if (Offset.getBitWidth() < 64)
434 Offset.extend(64);
435 return APValue(ResultLValue.getLValueBase(),
436 CharUnits::fromQuantity(Offset.getLimitedValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000437}
Eli Friedman4efaa272008-11-12 09:44:48 +0000438
Eli Friedman2217c872009-02-22 11:46:18 +0000439APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
440 APValue result;
441 if (EvaluateLValue(E->getSubExpr(), result, Info))
442 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000443 return APValue();
444}
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000446
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000447APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
448 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000449
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000450 switch (E->getCastKind()) {
451 default:
452 break;
453
454 case CastExpr::CK_Unknown: {
455 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
456
457 // Check for pointer->pointer cast
458 if (SubExpr->getType()->isPointerType() ||
459 SubExpr->getType()->isObjCObjectPointerType() ||
460 SubExpr->getType()->isNullPtrType() ||
461 SubExpr->getType()->isBlockPointerType())
462 return Visit(SubExpr);
463
464 if (SubExpr->getType()->isIntegralType()) {
465 APValue Result;
466 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
467 break;
468
469 if (Result.isInt()) {
470 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000471 return APValue(0,
472 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000473 }
474
475 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000476 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000477 }
478 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000481 case CastExpr::CK_NoOp:
482 case CastExpr::CK_BitCast:
483 case CastExpr::CK_AnyPointerToObjCPointerCast:
484 case CastExpr::CK_AnyPointerToBlockPointerCast:
485 return Visit(SubExpr);
486
487 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000488 APValue Result;
489 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000490 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000491
492 if (Result.isInt()) {
493 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000494 return APValue(0,
495 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000498 // Cast is of an lvalue, no need to change value.
499 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000500 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000501 case CastExpr::CK_ArrayToPointerDecay:
502 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000503 APValue Result;
504 if (EvaluateLValue(SubExpr, Result, Info))
505 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000506 break;
507 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000508 }
509
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000510 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000511}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000512
Eli Friedman3941b182009-01-25 01:54:01 +0000513APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000514 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000515 Builtin::BI__builtin___CFStringMakeConstantString ||
516 E->isBuiltinCall(Info.Ctx) ==
517 Builtin::BI__builtin___NSStringMakeConstantString)
Ken Dycka7305832010-01-15 12:37:54 +0000518 return APValue(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000519 return APValue();
520}
521
Eli Friedman4efaa272008-11-12 09:44:48 +0000522APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
523 bool BoolResult;
524 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
525 return APValue();
526
527 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
528
529 APValue Result;
530 if (EvaluatePointer(EvalExpr, Result, Info))
531 return Result;
532 return APValue();
533}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000534
535//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000536// Vector Evaluation
537//===----------------------------------------------------------------------===//
538
539namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000540 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000541 : public StmtVisitor<VectorExprEvaluator, APValue> {
542 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000543 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000544 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Nate Begeman59b5da62009-01-18 03:20:47 +0000546 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Nate Begeman59b5da62009-01-18 03:20:47 +0000548 APValue VisitStmt(Stmt *S) {
549 return APValue();
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Eli Friedman91110ee2009-02-23 04:23:56 +0000552 APValue VisitParenExpr(ParenExpr *E)
553 { return Visit(E->getSubExpr()); }
554 APValue VisitUnaryExtension(const UnaryOperator *E)
555 { return Visit(E->getSubExpr()); }
556 APValue VisitUnaryPlus(const UnaryOperator *E)
557 { return Visit(E->getSubExpr()); }
558 APValue VisitUnaryReal(const UnaryOperator *E)
559 { return Visit(E->getSubExpr()); }
560 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
561 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000562 APValue VisitCastExpr(const CastExpr* E);
563 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
564 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000565 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000566 APValue VisitChooseExpr(const ChooseExpr *E)
567 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000568 APValue VisitUnaryImag(const UnaryOperator *E);
569 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000570 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000571 // shufflevector, ExtVectorElementExpr
572 // (Note that these require implementing conversions
573 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000574 };
575} // end anonymous namespace
576
577static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
578 if (!E->getType()->isVectorType())
579 return false;
580 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
581 return !Result.isUninit();
582}
583
584APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000585 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000586 QualType EltTy = VTy->getElementType();
587 unsigned NElts = VTy->getNumElements();
588 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Nate Begeman59b5da62009-01-18 03:20:47 +0000590 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000591 QualType SETy = SE->getType();
592 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000593
Nate Begemane8c9e922009-06-26 18:22:18 +0000594 // Check for vector->vector bitcast and scalar->vector splat.
595 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000596 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000597 } else if (SETy->isIntegerType()) {
598 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000599 if (!EvaluateInteger(SE, IntResult, Info))
600 return APValue();
601 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000602 } else if (SETy->isRealFloatingType()) {
603 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000604 if (!EvaluateFloat(SE, F, Info))
605 return APValue();
606 Result = APValue(F);
607 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000608 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000609
Nate Begemanc0b8b192009-07-01 07:50:47 +0000610 // For casts of a scalar to ExtVector, convert the scalar to the element type
611 // and splat it to all elements.
612 if (E->getType()->isExtVectorType()) {
613 if (EltTy->isIntegerType() && Result.isInt())
614 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
615 Info.Ctx));
616 else if (EltTy->isIntegerType())
617 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
618 Info.Ctx));
619 else if (EltTy->isRealFloatingType() && Result.isInt())
620 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
621 Info.Ctx));
622 else if (EltTy->isRealFloatingType())
623 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
624 Info.Ctx));
625 else
626 return APValue();
627
628 // Splat and create vector APValue.
629 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
630 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000631 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000632
633 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
634 // to the vector. To construct the APValue vector initializer, bitcast the
635 // initializing value to an APInt, and shift out the bits pertaining to each
636 // element.
637 APSInt Init;
638 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Nate Begemanc0b8b192009-07-01 07:50:47 +0000640 llvm::SmallVector<APValue, 4> Elts;
641 for (unsigned i = 0; i != NElts; ++i) {
642 APSInt Tmp = Init;
643 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Nate Begemanc0b8b192009-07-01 07:50:47 +0000645 if (EltTy->isIntegerType())
646 Elts.push_back(APValue(Tmp));
647 else if (EltTy->isRealFloatingType())
648 Elts.push_back(APValue(APFloat(Tmp)));
649 else
650 return APValue();
651
652 Init >>= EltWidth;
653 }
654 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000655}
656
Mike Stump1eb44332009-09-09 15:08:12 +0000657APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000658VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
659 return this->Visit(const_cast<Expr*>(E->getInitializer()));
660}
661
Mike Stump1eb44332009-09-09 15:08:12 +0000662APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000663VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000664 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000665 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000666 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Nate Begeman59b5da62009-01-18 03:20:47 +0000668 QualType EltTy = VT->getElementType();
669 llvm::SmallVector<APValue, 4> Elements;
670
Eli Friedman91110ee2009-02-23 04:23:56 +0000671 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000672 if (EltTy->isIntegerType()) {
673 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000674 if (i < NumInits) {
675 if (!EvaluateInteger(E->getInit(i), sInt, Info))
676 return APValue();
677 } else {
678 sInt = Info.Ctx.MakeIntValue(0, EltTy);
679 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000680 Elements.push_back(APValue(sInt));
681 } else {
682 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000683 if (i < NumInits) {
684 if (!EvaluateFloat(E->getInit(i), f, Info))
685 return APValue();
686 } else {
687 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
688 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000689 Elements.push_back(APValue(f));
690 }
691 }
692 return APValue(&Elements[0], Elements.size());
693}
694
Mike Stump1eb44332009-09-09 15:08:12 +0000695APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000696VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000697 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000698 QualType EltTy = VT->getElementType();
699 APValue ZeroElement;
700 if (EltTy->isIntegerType())
701 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
702 else
703 ZeroElement =
704 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
705
706 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
707 return APValue(&Elements[0], Elements.size());
708}
709
710APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
711 bool BoolResult;
712 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
713 return APValue();
714
715 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
716
717 APValue Result;
718 if (EvaluateVector(EvalExpr, Result, Info))
719 return Result;
720 return APValue();
721}
722
Eli Friedman91110ee2009-02-23 04:23:56 +0000723APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
724 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
725 Info.EvalResult.HasSideEffects = true;
726 return GetZeroVector(E->getType());
727}
728
Nate Begeman59b5da62009-01-18 03:20:47 +0000729//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000730// Integer Evaluation
731//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000732
733namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000734class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000735 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000736 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000737 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000738public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000739 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000740 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000741
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000742 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000743 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000744 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000745 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000746 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000747 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000748 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000749 return true;
750 }
751
Daniel Dunbar131eb432009-02-19 09:06:44 +0000752 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000753 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000754 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000755 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000756 Result = APValue(APSInt(I));
757 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000758 return true;
759 }
760
761 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000762 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000763 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000764 return true;
765 }
766
Anders Carlsson82206e22008-11-30 18:14:57 +0000767 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000768 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000769 if (Info.EvalResult.Diag == 0) {
770 Info.EvalResult.DiagLoc = L;
771 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000772 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000773 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000774 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Anders Carlssonc754aa62008-07-08 05:13:58 +0000777 //===--------------------------------------------------------------------===//
778 // Visitor Methods
779 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Chris Lattner32fea9d2008-11-12 07:43:42 +0000781 bool VisitStmt(Stmt *) {
782 assert(0 && "This should be called on integers, stmts are not integers");
783 return false;
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Chris Lattner32fea9d2008-11-12 07:43:42 +0000786 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000787 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattnerb542afe2008-07-11 19:10:17 +0000790 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000791
Chris Lattner4c4867e2008-07-12 00:38:25 +0000792 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000793 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000794 }
795 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000796 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000797 }
798 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000799 // Per gcc docs "this built-in function ignores top level
800 // qualifiers". We need to use the canonical version to properly
801 // be able to strip CRV qualifiers from the type.
802 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
803 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000804 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000805 T1.getUnqualifiedType()),
806 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000807 }
Eli Friedman04309752009-11-24 05:28:59 +0000808
809 bool CheckReferencedDecl(const Expr *E, const Decl *D);
810 bool VisitDeclRefExpr(const DeclRefExpr *E) {
811 return CheckReferencedDecl(E, E->getDecl());
812 }
813 bool VisitMemberExpr(const MemberExpr *E) {
814 if (CheckReferencedDecl(E, E->getMemberDecl())) {
815 // Conservatively assume a MemberExpr will have side-effects
816 Info.EvalResult.HasSideEffects = true;
817 return true;
818 }
819 return false;
820 }
821
Eli Friedmanc4a26382010-02-13 00:10:10 +0000822 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000823 bool VisitBinaryOperator(const BinaryOperator *E);
824 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000825 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000826
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000827 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000828 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
829
Anders Carlsson3068d112008-11-16 19:01:22 +0000830 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000831 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Anders Carlsson3f704562008-12-21 22:39:40 +0000834 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000835 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Anders Carlsson3068d112008-11-16 19:01:22 +0000838 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000839 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000840 }
841
Eli Friedman664a1042009-02-27 04:45:43 +0000842 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
843 return Success(0, E);
844 }
845
Sebastian Redl64b45f72009-01-05 20:52:13 +0000846 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000847 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000848 }
849
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000850 bool VisitChooseExpr(const ChooseExpr *E) {
851 return Visit(E->getChosenSubExpr(Info.Ctx));
852 }
853
Eli Friedman722c7172009-02-28 03:59:05 +0000854 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000855 bool VisitUnaryImag(const UnaryOperator *E);
856
Chris Lattnerfcee0012008-07-11 21:24:13 +0000857private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000858 CharUnits GetAlignOfExpr(const Expr *E);
859 CharUnits GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000860 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000861};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000862} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000863
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000864static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000865 if (!E->getType()->isIntegralType())
866 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000868 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
869}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000870
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000871static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
872 APValue Val;
873 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
874 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000875 Result = Val.getInt();
876 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000877}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000878
Eli Friedman04309752009-11-24 05:28:59 +0000879bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000880 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000881 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
882 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000883
884 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000885 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000886 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
887 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000888
889 if (isa<ParmVarDecl>(D))
890 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
891
Eli Friedman04309752009-11-24 05:28:59 +0000892 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000893 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000894 if (APValue *V = VD->getEvaluatedValue()) {
895 if (V->isInt())
896 return Success(V->getInt(), E);
897 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
898 }
899
900 if (VD->isEvaluatingValue())
901 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
902
903 VD->setEvaluatingValue();
904
Douglas Gregor78d15832009-05-26 18:54:04 +0000905 if (Visit(const_cast<Expr*>(Init))) {
906 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000907 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000908 return true;
909 }
910
Eli Friedmanc0131182009-12-03 20:31:57 +0000911 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000912 return false;
913 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000914 }
915 }
916
Chris Lattner4c4867e2008-07-12 00:38:25 +0000917 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000918 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000919}
920
Chris Lattnera4d55d82008-10-06 06:40:35 +0000921/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
922/// as GCC.
923static int EvaluateBuiltinClassifyType(const CallExpr *E) {
924 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000925 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000926 enum gcc_type_class {
927 no_type_class = -1,
928 void_type_class, integer_type_class, char_type_class,
929 enumeral_type_class, boolean_type_class,
930 pointer_type_class, reference_type_class, offset_type_class,
931 real_type_class, complex_type_class,
932 function_type_class, method_type_class,
933 record_type_class, union_type_class,
934 array_type_class, string_type_class,
935 lang_type_class
936 };
Mike Stump1eb44332009-09-09 15:08:12 +0000937
938 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000939 // ideal, however it is what gcc does.
940 if (E->getNumArgs() == 0)
941 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Chris Lattnera4d55d82008-10-06 06:40:35 +0000943 QualType ArgTy = E->getArg(0)->getType();
944 if (ArgTy->isVoidType())
945 return void_type_class;
946 else if (ArgTy->isEnumeralType())
947 return enumeral_type_class;
948 else if (ArgTy->isBooleanType())
949 return boolean_type_class;
950 else if (ArgTy->isCharType())
951 return string_type_class; // gcc doesn't appear to use char_type_class
952 else if (ArgTy->isIntegerType())
953 return integer_type_class;
954 else if (ArgTy->isPointerType())
955 return pointer_type_class;
956 else if (ArgTy->isReferenceType())
957 return reference_type_class;
958 else if (ArgTy->isRealType())
959 return real_type_class;
960 else if (ArgTy->isComplexType())
961 return complex_type_class;
962 else if (ArgTy->isFunctionType())
963 return function_type_class;
964 else if (ArgTy->isStructureType())
965 return record_type_class;
966 else if (ArgTy->isUnionType())
967 return union_type_class;
968 else if (ArgTy->isArrayType())
969 return array_type_class;
970 else if (ArgTy->isUnionType())
971 return union_type_class;
972 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
973 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
974 return -1;
975}
976
Eli Friedmanc4a26382010-02-13 00:10:10 +0000977bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000978 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000979 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000980 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000981
982 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000983 const Expr *Arg = E->getArg(0)->IgnoreParens();
984 Expr::EvalResult Base;
Eric Christopherb2aaf512010-01-19 22:58:35 +0000985
986 // TODO: Perhaps we should let LLVM lower this?
Mike Stump660e6f72009-10-26 23:05:19 +0000987 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000988 && Base.Val.getKind() == APValue::LValue
989 && !Base.HasSideEffects)
990 if (const Expr *LVBase = Base.Val.getLValueBase())
991 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
992 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000993 if (!VD->getType()->isIncompleteType()
994 && VD->getType()->isObjectType()
995 && !VD->getType()->isVariablyModifiedType()
996 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000997 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +0000998 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +0000999 if (!Offset.isNegative() && Offset <= Size)
1000 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +00001001 else
Ken Dyck199c3d62010-01-11 17:06:35 +00001002 Size = CharUnits::Zero();
1003 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +00001004 }
Mike Stump64eda9e2009-10-26 18:35:08 +00001005 }
1006 }
1007
Eric Christopherb2aaf512010-01-19 22:58:35 +00001008 // If evaluating the argument has side-effects we can't determine
1009 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001010 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001011 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001012 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001013 return Success(0, E);
1014 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001015
Mike Stump64eda9e2009-10-26 18:35:08 +00001016 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1017 }
1018
Chris Lattner019f4e82008-10-06 05:28:25 +00001019 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001020 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001022 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001023 // __builtin_constant_p always has one operand: it returns true if that
1024 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001025 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001026
1027 case Builtin::BI__builtin_eh_return_data_regno: {
1028 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1029 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1030 return Success(Operand, E);
1031 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001032
1033 case Builtin::BI__builtin_expect:
1034 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001035 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001036}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001037
Chris Lattnerb542afe2008-07-11 19:10:17 +00001038bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001039 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001040 if (!Visit(E->getRHS()))
1041 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001042
Eli Friedman33ef1452009-02-26 10:19:36 +00001043 // If we can't evaluate the LHS, it might have side effects;
1044 // conservatively mark it.
1045 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1046 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001047
Anders Carlsson027f62e2008-12-01 02:07:06 +00001048 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001049 }
1050
1051 if (E->isLogicalOp()) {
1052 // These need to be handled specially because the operands aren't
1053 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001054 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001056 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001057 // We were able to evaluate the LHS, see if we can get away with not
1058 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001059 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001060 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001061
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001062 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001063 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001064 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001065 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001066 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001067 }
1068 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001069 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001070 // We can't evaluate the LHS; however, sometimes the result
1071 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001072 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001073 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001074 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001075 // must have had side effects.
1076 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001077
1078 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001079 }
1080 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001081 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001082
Eli Friedmana6afa762008-11-13 06:09:17 +00001083 return false;
1084 }
1085
Anders Carlsson286f85e2008-11-16 07:17:21 +00001086 QualType LHSTy = E->getLHS()->getType();
1087 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001088
1089 if (LHSTy->isAnyComplexType()) {
1090 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1091 APValue LHS, RHS;
1092
1093 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1094 return false;
1095
1096 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1097 return false;
1098
1099 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001100 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001101 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001102 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001103 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1104
Daniel Dunbar4087e242009-01-29 06:43:41 +00001105 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001106 return Success((CR_r == APFloat::cmpEqual &&
1107 CR_i == APFloat::cmpEqual), E);
1108 else {
1109 assert(E->getOpcode() == BinaryOperator::NE &&
1110 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001111 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001112 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001113 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001114 CR_i == APFloat::cmpLessThan)), E);
1115 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001116 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001117 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001118 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1119 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1120 else {
1121 assert(E->getOpcode() == BinaryOperator::NE &&
1122 "Invalid compex comparison.");
1123 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1124 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1125 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001126 }
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Anders Carlsson286f85e2008-11-16 07:17:21 +00001129 if (LHSTy->isRealFloatingType() &&
1130 RHSTy->isRealFloatingType()) {
1131 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Anders Carlsson286f85e2008-11-16 07:17:21 +00001133 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1134 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Anders Carlsson286f85e2008-11-16 07:17:21 +00001136 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1137 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Anders Carlsson286f85e2008-11-16 07:17:21 +00001139 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001140
Anders Carlsson286f85e2008-11-16 07:17:21 +00001141 switch (E->getOpcode()) {
1142 default:
1143 assert(0 && "Invalid binary operator!");
1144 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001145 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001146 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001147 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001148 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001149 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001150 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001151 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001152 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001153 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001154 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001155 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001156 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001157 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001158 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001161 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1162 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001163 APValue LHSValue;
1164 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1165 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001166
Anders Carlsson3068d112008-11-16 19:01:22 +00001167 APValue RHSValue;
1168 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1169 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001170
Eli Friedman5bc86102009-06-14 02:17:33 +00001171 // Reject any bases from the normal codepath; we special-case comparisons
1172 // to null.
1173 if (LHSValue.getLValueBase()) {
1174 if (!E->isEqualityOp())
1175 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001176 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001177 return false;
1178 bool bres;
1179 if (!EvalPointerValueAsBool(LHSValue, bres))
1180 return false;
1181 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1182 } else if (RHSValue.getLValueBase()) {
1183 if (!E->isEqualityOp())
1184 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001185 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001186 return false;
1187 bool bres;
1188 if (!EvalPointerValueAsBool(RHSValue, bres))
1189 return false;
1190 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1191 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001192
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001193 if (E->getOpcode() == BinaryOperator::Sub) {
1194 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001195 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001196
Ken Dycka7305832010-01-15 12:37:54 +00001197 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001198 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001199 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001200
Ken Dycka7305832010-01-15 12:37:54 +00001201 CharUnits Diff = LHSValue.getLValueOffset() -
1202 RHSValue.getLValueOffset();
1203 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001204 }
1205 bool Result;
1206 if (E->getOpcode() == BinaryOperator::EQ) {
1207 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001208 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001209 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1210 }
1211 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001212 }
1213 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001214 if (!LHSTy->isIntegralType() ||
1215 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001216 // We can't continue from here for non-integral types, and they
1217 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001218 return false;
1219 }
1220
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001221 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001222 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001223 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001224
Eli Friedman42edd0d2009-03-24 01:14:50 +00001225 APValue RHSVal;
1226 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001227 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001228
1229 // Handle cases like (unsigned long)&a + 4.
1230 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001231 CharUnits Offset = Result.getLValueOffset();
1232 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1233 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001234 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001235 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001236 else
Ken Dycka7305832010-01-15 12:37:54 +00001237 Offset -= AdditionalOffset;
1238 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001239 return true;
1240 }
1241
1242 // Handle cases like 4 + (unsigned long)&a
1243 if (E->getOpcode() == BinaryOperator::Add &&
1244 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001245 CharUnits Offset = RHSVal.getLValueOffset();
1246 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1247 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001248 return true;
1249 }
1250
1251 // All the following cases expect both operands to be an integer
1252 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001253 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001254
Eli Friedman42edd0d2009-03-24 01:14:50 +00001255 APSInt& RHS = RHSVal.getInt();
1256
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001257 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001258 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001259 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001260 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1261 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1262 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1263 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1264 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1265 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001266 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001267 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001268 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001269 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001270 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001271 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001272 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001273 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001274 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001275 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001276 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001277 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1278 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001279 }
1280 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001281 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001282 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1283 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001284 }
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001286 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1287 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1288 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1289 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1290 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1291 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001292 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001293}
1294
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001295bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001296 bool Cond;
1297 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001298 return false;
1299
Nuno Lopesa25bd552008-11-16 22:06:39 +00001300 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001301}
1302
Ken Dyck8b752f12010-01-27 17:10:57 +00001303CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001304 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1305 // the result is the size of the referenced type."
1306 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1307 // result shall be the alignment of the referenced type."
1308 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1309 T = Ref->getPointeeType();
1310
Chris Lattnere9feb472009-01-24 21:09:06 +00001311 // Get information about the alignment.
1312 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001313
Eli Friedman2be58612009-05-30 21:09:44 +00001314 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001315 return CharUnits::fromQuantity(
1316 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001317}
1318
Ken Dyck8b752f12010-01-27 17:10:57 +00001319CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001320 E = E->IgnoreParens();
1321
1322 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001323 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001324 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001325 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1326 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001327
Chris Lattneraf707ab2009-01-24 21:53:27 +00001328 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001329 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1330 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001331
Chris Lattnere9feb472009-01-24 21:09:06 +00001332 return GetAlignOfType(E->getType());
1333}
1334
1335
Sebastian Redl05189992008-11-11 17:56:53 +00001336/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1337/// expression's type.
1338bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001339 // Handle alignof separately.
1340 if (!E->isSizeOf()) {
1341 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001342 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001343 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001344 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001345 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001346
Sebastian Redl05189992008-11-11 17:56:53 +00001347 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001348 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1349 // the result is the size of the referenced type."
1350 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1351 // result shall be the alignment of the referenced type."
1352 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1353 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001354
Daniel Dunbar131eb432009-02-19 09:06:44 +00001355 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1356 // extension.
1357 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1358 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001359
Chris Lattnerfcee0012008-07-11 21:24:13 +00001360 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001361 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001362 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001363
Chris Lattnere9feb472009-01-24 21:09:06 +00001364 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001365 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001366}
1367
Chris Lattnerb542afe2008-07-11 19:10:17 +00001368bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001369 // Special case unary operators that do not need their subexpression
1370 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001371 if (E->isOffsetOfOp()) {
1372 // The AST for offsetof is defined in such a way that we can just
1373 // directly Evaluate it as an l-value.
1374 APValue LV;
1375 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1376 return false;
1377 if (LV.getLValueBase())
1378 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001379 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001380 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001381
1382 if (E->getOpcode() == UnaryOperator::LNot) {
1383 // LNot's operand isn't necessarily an integer, so we handle it specially.
1384 bool bres;
1385 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1386 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001387 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001388 }
1389
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001390 // Only handle integral operations...
1391 if (!E->getSubExpr()->getType()->isIntegralType())
1392 return false;
1393
Chris Lattner87eae5e2008-07-11 22:52:41 +00001394 // Get the operand value into 'Result'.
1395 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001396 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001397
Chris Lattner75a48812008-07-11 22:15:16 +00001398 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001399 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001400 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1401 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001402 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001403 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001404 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1405 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001406 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001407 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001408 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001409 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001410 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001411 if (!Result.isInt()) return false;
1412 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001413 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001414 if (!Result.isInt()) return false;
1415 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001416 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001417}
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Chris Lattner732b2232008-07-12 01:15:53 +00001419/// HandleCast - This is used to evaluate implicit or explicit casts where the
1420/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001421bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001422 Expr *SubExpr = E->getSubExpr();
1423 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001424 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001425
Eli Friedman4efaa272008-11-12 09:44:48 +00001426 if (DestType->isBooleanType()) {
1427 bool BoolResult;
1428 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1429 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001430 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001431 }
1432
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001433 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001434 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001435 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001436 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001437
Eli Friedmanbe265702009-02-20 01:15:07 +00001438 if (!Result.isInt()) {
1439 // Only allow casts of lvalues if they are lossless.
1440 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1441 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001442
Daniel Dunbardd211642009-02-19 22:24:01 +00001443 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001444 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Chris Lattner732b2232008-07-12 01:15:53 +00001447 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001448 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001449 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001450 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001451 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001452
Daniel Dunbardd211642009-02-19 22:24:01 +00001453 if (LV.getLValueBase()) {
1454 // Only allow based lvalue casts if they are lossless.
1455 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1456 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001457
Daniel Dunbardd211642009-02-19 22:24:01 +00001458 Result = LV;
1459 return true;
1460 }
1461
Ken Dycka7305832010-01-15 12:37:54 +00001462 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1463 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001464 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001465 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001466
Eli Friedmanbe265702009-02-20 01:15:07 +00001467 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1468 // This handles double-conversion cases, where there's both
1469 // an l-value promotion and an implicit conversion to int.
1470 APValue LV;
1471 if (!EvaluateLValue(SubExpr, LV, Info))
1472 return false;
1473
1474 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1475 return false;
1476
1477 Result = LV;
1478 return true;
1479 }
1480
Eli Friedman1725f682009-04-22 19:23:09 +00001481 if (SrcType->isAnyComplexType()) {
1482 APValue C;
1483 if (!EvaluateComplex(SubExpr, C, Info))
1484 return false;
1485 if (C.isComplexFloat())
1486 return Success(HandleFloatToIntCast(DestType, SrcType,
1487 C.getComplexFloatReal(), Info.Ctx),
1488 E);
1489 else
1490 return Success(HandleIntToIntCast(DestType, SrcType,
1491 C.getComplexIntReal(), Info.Ctx), E);
1492 }
Eli Friedman2217c872009-02-22 11:46:18 +00001493 // FIXME: Handle vectors
1494
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001495 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001496 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001497
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001498 APFloat F(0.0);
1499 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001500 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001502 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001503}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001504
Eli Friedman722c7172009-02-28 03:59:05 +00001505bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1506 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1507 APValue LV;
1508 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1509 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1510 return Success(LV.getComplexIntReal(), E);
1511 }
1512
1513 return Visit(E->getSubExpr());
1514}
1515
Eli Friedman664a1042009-02-27 04:45:43 +00001516bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001517 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1518 APValue LV;
1519 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1520 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1521 return Success(LV.getComplexIntImag(), E);
1522 }
1523
Eli Friedman664a1042009-02-27 04:45:43 +00001524 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1525 Info.EvalResult.HasSideEffects = true;
1526 return Success(0, E);
1527}
1528
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001529//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001530// Float Evaluation
1531//===----------------------------------------------------------------------===//
1532
1533namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001534class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001535 : public StmtVisitor<FloatExprEvaluator, bool> {
1536 EvalInfo &Info;
1537 APFloat &Result;
1538public:
1539 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1540 : Info(info), Result(result) {}
1541
1542 bool VisitStmt(Stmt *S) {
1543 return false;
1544 }
1545
1546 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001547 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001548
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001549 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001550 bool VisitBinaryOperator(const BinaryOperator *E);
1551 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001552 bool VisitCastExpr(CastExpr *E);
1553 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001554 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001555
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001556 bool VisitChooseExpr(const ChooseExpr *E)
1557 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1558 bool VisitUnaryExtension(const UnaryOperator *E)
1559 { return Visit(E->getSubExpr()); }
1560
1561 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001562 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001563};
1564} // end anonymous namespace
1565
1566static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1567 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1568}
1569
John McCalldb7b72a2010-02-28 13:00:19 +00001570static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1571 QualType ResultTy,
1572 const Expr *Arg,
1573 bool SNaN,
1574 llvm::APFloat &Result) {
1575 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1576 if (!S) return false;
1577
1578 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1579
1580 llvm::APInt fill;
1581
1582 // Treat empty strings as if they were zero.
1583 if (S->getString().empty())
1584 fill = llvm::APInt(32, 0);
1585 else if (S->getString().getAsInteger(0, fill))
1586 return false;
1587
1588 if (SNaN)
1589 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1590 else
1591 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1592 return true;
1593}
1594
Chris Lattner019f4e82008-10-06 05:28:25 +00001595bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001596 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001597 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001598 case Builtin::BI__builtin_huge_val:
1599 case Builtin::BI__builtin_huge_valf:
1600 case Builtin::BI__builtin_huge_vall:
1601 case Builtin::BI__builtin_inf:
1602 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001603 case Builtin::BI__builtin_infl: {
1604 const llvm::fltSemantics &Sem =
1605 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001606 Result = llvm::APFloat::getInf(Sem);
1607 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001608 }
Mike Stump1eb44332009-09-09 15:08:12 +00001609
John McCalldb7b72a2010-02-28 13:00:19 +00001610 case Builtin::BI__builtin_nans:
1611 case Builtin::BI__builtin_nansf:
1612 case Builtin::BI__builtin_nansl:
1613 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1614 true, Result);
1615
Chris Lattner9e621712008-10-06 06:31:58 +00001616 case Builtin::BI__builtin_nan:
1617 case Builtin::BI__builtin_nanf:
1618 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001619 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001620 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001621 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1622 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001623
1624 case Builtin::BI__builtin_fabs:
1625 case Builtin::BI__builtin_fabsf:
1626 case Builtin::BI__builtin_fabsl:
1627 if (!EvaluateFloat(E->getArg(0), Result, Info))
1628 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001630 if (Result.isNegative())
1631 Result.changeSign();
1632 return true;
1633
Mike Stump1eb44332009-09-09 15:08:12 +00001634 case Builtin::BI__builtin_copysign:
1635 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001636 case Builtin::BI__builtin_copysignl: {
1637 APFloat RHS(0.);
1638 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1639 !EvaluateFloat(E->getArg(1), RHS, Info))
1640 return false;
1641 Result.copySign(RHS);
1642 return true;
1643 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001644 }
1645}
1646
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001647bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001648 if (E->getOpcode() == UnaryOperator::Deref)
1649 return false;
1650
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001651 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1652 return false;
1653
1654 switch (E->getOpcode()) {
1655 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001656 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001657 return true;
1658 case UnaryOperator::Minus:
1659 Result.changeSign();
1660 return true;
1661 }
1662}
Chris Lattner019f4e82008-10-06 05:28:25 +00001663
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001664bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001665 if (E->getOpcode() == BinaryOperator::Comma) {
1666 if (!EvaluateFloat(E->getRHS(), Result, Info))
1667 return false;
1668
1669 // If we can't evaluate the LHS, it might have side effects;
1670 // conservatively mark it.
1671 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1672 Info.EvalResult.HasSideEffects = true;
1673
1674 return true;
1675 }
1676
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001677 // FIXME: Diagnostics? I really don't understand how the warnings
1678 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001679 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001680 if (!EvaluateFloat(E->getLHS(), Result, Info))
1681 return false;
1682 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1683 return false;
1684
1685 switch (E->getOpcode()) {
1686 default: return false;
1687 case BinaryOperator::Mul:
1688 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1689 return true;
1690 case BinaryOperator::Add:
1691 Result.add(RHS, APFloat::rmNearestTiesToEven);
1692 return true;
1693 case BinaryOperator::Sub:
1694 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1695 return true;
1696 case BinaryOperator::Div:
1697 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1698 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001699 }
1700}
1701
1702bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1703 Result = E->getValue();
1704 return true;
1705}
1706
Eli Friedman4efaa272008-11-12 09:44:48 +00001707bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1708 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Eli Friedman4efaa272008-11-12 09:44:48 +00001710 if (SubExpr->getType()->isIntegralType()) {
1711 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001712 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001713 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001714 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001715 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001716 return true;
1717 }
1718 if (SubExpr->getType()->isRealFloatingType()) {
1719 if (!Visit(SubExpr))
1720 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001721 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1722 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001723 return true;
1724 }
Eli Friedman2217c872009-02-22 11:46:18 +00001725 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001726
1727 return false;
1728}
1729
1730bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1731 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1732 return true;
1733}
1734
Eli Friedman67f85fc2009-12-04 02:12:53 +00001735bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1736 bool Cond;
1737 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1738 return false;
1739
1740 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1741}
1742
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001743//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001744// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001745//===----------------------------------------------------------------------===//
1746
1747namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001748class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001749 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001750 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001752public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001753 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001755 //===--------------------------------------------------------------------===//
1756 // Visitor Methods
1757 //===--------------------------------------------------------------------===//
1758
1759 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001760 return APValue();
1761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001763 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1764
1765 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001766 Expr* SubExpr = E->getSubExpr();
1767
1768 if (SubExpr->getType()->isRealFloatingType()) {
1769 APFloat Result(0.0);
1770
1771 if (!EvaluateFloat(SubExpr, Result, Info))
1772 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001773
1774 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001775 Result);
1776 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001777 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001778 "Unexpected imaginary literal.");
1779
1780 llvm::APSInt Result;
1781 if (!EvaluateInteger(SubExpr, Result, Info))
1782 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001784 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1785 Zero = 0;
1786 return APValue(Zero, Result);
1787 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001788 }
1789
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001790 APValue VisitCastExpr(CastExpr *E) {
1791 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001792 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001793 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001794
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001795 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001796 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001797
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001798 if (!EvaluateFloat(SubExpr, Result, Info))
1799 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001800
1801 if (EltType->isRealFloatingType()) {
1802 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001803 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001804 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1805 } else {
1806 llvm::APSInt IResult;
1807 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1808 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1809 Zero = 0;
1810 return APValue(IResult, Zero);
1811 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001812 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001813 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001814
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001815 if (!EvaluateInteger(SubExpr, Result, Info))
1816 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001817
Eli Friedman1725f682009-04-22 19:23:09 +00001818 if (EltType->isRealFloatingType()) {
1819 APFloat FResult =
1820 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001821 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001822 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1823 } else {
1824 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1825 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1826 Zero = 0;
1827 return APValue(Result, Zero);
1828 }
John McCall183700f2009-09-21 23:43:11 +00001829 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001830 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001831
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001832 if (!EvaluateComplex(SubExpr, Src, Info))
1833 return APValue();
1834
1835 QualType SrcType = CT->getElementType();
1836
1837 if (Src.isComplexFloat()) {
1838 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001839 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001840 Src.getComplexFloatReal(),
1841 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001842 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001843 Src.getComplexFloatImag(),
1844 Info.Ctx));
1845 } else {
1846 return APValue(HandleFloatToIntCast(EltType, SrcType,
1847 Src.getComplexFloatReal(),
1848 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001849 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001850 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001851 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001852 }
1853 } else {
1854 assert(Src.isComplexInt() && "Invalid evaluate result.");
1855 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001856 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001857 Src.getComplexIntReal(),
1858 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001859 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001860 Src.getComplexIntImag(),
1861 Info.Ctx));
1862 } else {
1863 return APValue(HandleIntToIntCast(EltType, SrcType,
1864 Src.getComplexIntReal(),
1865 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001866 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001867 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001868 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001869 }
1870 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001871 }
1872
1873 // FIXME: Handle more casts.
1874 return APValue();
1875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001877 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001878 APValue VisitChooseExpr(const ChooseExpr *E)
1879 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1880 APValue VisitUnaryExtension(const UnaryOperator *E)
1881 { return Visit(E->getSubExpr()); }
1882 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001883 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001884};
1885} // end anonymous namespace
1886
Mike Stump1eb44332009-09-09 15:08:12 +00001887static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001888 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1889 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001890 (&Result.getComplexFloatReal().getSemantics() ==
1891 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001892 "Invalid complex evaluation.");
1893 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001894}
1895
Mike Stump1eb44332009-09-09 15:08:12 +00001896APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001897 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001899 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001900 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001902 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001903 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001904
Daniel Dunbar3f279872009-01-29 01:32:56 +00001905 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1906 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001907 switch (E->getOpcode()) {
1908 default: return APValue();
1909 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001910 if (Result.isComplexFloat()) {
1911 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1912 APFloat::rmNearestTiesToEven);
1913 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1914 APFloat::rmNearestTiesToEven);
1915 } else {
1916 Result.getComplexIntReal() += RHS.getComplexIntReal();
1917 Result.getComplexIntImag() += RHS.getComplexIntImag();
1918 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001919 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001920 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001921 if (Result.isComplexFloat()) {
1922 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1923 APFloat::rmNearestTiesToEven);
1924 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1925 APFloat::rmNearestTiesToEven);
1926 } else {
1927 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1928 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1929 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001930 break;
1931 case BinaryOperator::Mul:
1932 if (Result.isComplexFloat()) {
1933 APValue LHS = Result;
1934 APFloat &LHS_r = LHS.getComplexFloatReal();
1935 APFloat &LHS_i = LHS.getComplexFloatImag();
1936 APFloat &RHS_r = RHS.getComplexFloatReal();
1937 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Daniel Dunbar3f279872009-01-29 01:32:56 +00001939 APFloat Tmp = LHS_r;
1940 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1941 Result.getComplexFloatReal() = Tmp;
1942 Tmp = LHS_i;
1943 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1944 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1945
1946 Tmp = LHS_r;
1947 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1948 Result.getComplexFloatImag() = Tmp;
1949 Tmp = LHS_i;
1950 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1951 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1952 } else {
1953 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001954 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001955 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1956 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001957 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001958 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1959 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1960 }
1961 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001962 }
1963
1964 return Result;
1965}
1966
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001967//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001968// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001969//===----------------------------------------------------------------------===//
1970
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001971/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001972/// any crazy technique (that has nothing to do with language standards) that
1973/// we want to. If this function returns true, it returns the folded constant
1974/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001975bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1976 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001977
Nate Begeman59b5da62009-01-18 03:20:47 +00001978 if (getType()->isVectorType()) {
1979 if (!EvaluateVector(this, Result.Val, Info))
1980 return false;
1981 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001982 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001983 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001984 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001985 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001986 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001987 } else if (getType()->isRealFloatingType()) {
1988 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001989 if (!EvaluateFloat(this, f, Info))
1990 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001992 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001993 } else if (getType()->isAnyComplexType()) {
1994 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001995 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001996 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001997 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001998
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001999 return true;
2000}
2001
Mike Stump660e6f72009-10-26 23:05:19 +00002002bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
2003 EvalInfo Info(Ctx, Result, true);
2004
2005 if (getType()->isVectorType()) {
2006 if (!EvaluateVector(this, Result.Val, Info))
2007 return false;
2008 } else if (getType()->isIntegerType()) {
2009 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
2010 return false;
2011 } else if (getType()->hasPointerRepresentation()) {
2012 if (!EvaluatePointer(this, Result.Val, Info))
2013 return false;
2014 } else if (getType()->isRealFloatingType()) {
2015 llvm::APFloat f(0.0);
2016 if (!EvaluateFloat(this, f, Info))
2017 return false;
2018
2019 Result.Val = APValue(f);
2020 } else if (getType()->isAnyComplexType()) {
2021 if (!EvaluateComplex(this, Result.Val, Info))
2022 return false;
2023 } else
2024 return false;
2025
2026 return true;
2027}
2028
John McCallcd7a4452010-01-05 23:42:56 +00002029bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2030 EvalResult Scratch;
2031 EvalInfo Info(Ctx, Scratch);
2032
2033 return HandleConversionToBool(this, Result, Info);
2034}
2035
Anders Carlsson1b782762009-04-10 04:54:13 +00002036bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2037 EvalInfo Info(Ctx, Result);
2038
2039 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2040}
2041
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002042bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2043 EvalInfo Info(Ctx, Result, true);
2044
2045 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2046}
2047
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002048/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002049/// folded, but discard the result.
2050bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002051 EvalResult Result;
2052 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002053}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002054
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002055bool Expr::HasSideEffects(ASTContext &Ctx) const {
2056 Expr::EvalResult Result;
2057 EvalInfo Info(Ctx, Result);
2058 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2059}
2060
Anders Carlsson51fe9962008-11-22 21:04:56 +00002061APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002062 EvalResult EvalResult;
2063 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002064 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002065 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002066 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002067
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002068 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002069}