blob: cc86b243424f89692ff73aae0e82c898d6e7893d [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"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000022#include <cstring>
23
Anders Carlssonc44eec62008-07-03 04:20:39 +000024using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000025using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000026using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000027
Chris Lattner87eae5e2008-07-11 22:52:41 +000028/// EvalInfo - This is a private struct used by the evaluator to capture
29/// information about a subexpression as it is folded. It retains information
30/// about the AST context, but also maintains information about the folded
31/// expression.
32///
33/// If an expression could be evaluated, it is still possible it is not a C
34/// "integer constant expression" or constant expression. If not, this struct
35/// captures information about how and why not.
36///
37/// One bit of information passed *into* the request for constant folding
38/// indicates whether the subexpression is "evaluated" or not according to C
39/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
40/// evaluate the expression regardless of what the RHS is, but C only allows
41/// certain things in certain situations.
42struct EvalInfo {
43 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Anders Carlsson54da0492008-11-30 16:38:33 +000045 /// EvalResult - Contains information about the evaluation.
46 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000047
Eli Friedmanb2f295c2009-09-13 10:17:44 +000048 /// AnyLValue - Stack based LValue results are not discarded.
49 bool AnyLValue;
50
51 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
52 bool anylvalue = false)
53 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000054};
55
56
Eli Friedman4efaa272008-11-12 09:44:48 +000057static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000058static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
59static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +000060static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
61 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman5bc86102009-06-14 02:17:33 +000069static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
70 // FIXME: Is this accurate for all kinds of bases? If not, what would
71 // the check look like?
72 Result = Value.getLValueBase() || Value.getLValueOffset();
73 return true;
74}
75
John McCallcd7a4452010-01-05 23:42:56 +000076static bool HandleConversionToBool(const Expr* E, bool& Result,
77 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +000078 if (E->getType()->isIntegralType()) {
79 APSInt IntResult;
80 if (!EvaluateInteger(E, IntResult, Info))
81 return false;
82 Result = IntResult != 0;
83 return true;
84 } else if (E->getType()->isRealFloatingType()) {
85 APFloat FloatResult(0.0);
86 if (!EvaluateFloat(E, FloatResult, Info))
87 return false;
88 Result = !FloatResult.isZero();
89 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000090 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000091 APValue PointerResult;
92 if (!EvaluatePointer(E, PointerResult, Info))
93 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000094 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000095 } else if (E->getType()->isAnyComplexType()) {
96 APValue ComplexResult;
97 if (!EvaluateComplex(E, ComplexResult, Info))
98 return false;
99 if (ComplexResult.isComplexFloat()) {
100 Result = !ComplexResult.getComplexFloatReal().isZero() ||
101 !ComplexResult.getComplexFloatImag().isZero();
102 } else {
103 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
104 ComplexResult.getComplexIntImag().getBoolValue();
105 }
106 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000107 }
108
109 return false;
110}
111
Mike Stump1eb44332009-09-09 15:08:12 +0000112static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000113 APFloat &Value, ASTContext &Ctx) {
114 unsigned DestWidth = Ctx.getIntWidth(DestType);
115 // Determine whether we are converting to unsigned or signed.
116 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000118 // FIXME: Warning for overflow.
119 uint64_t Space[4];
120 bool ignored;
121 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
122 llvm::APFloat::rmTowardZero, &ignored);
123 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
124}
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000127 APFloat &Value, ASTContext &Ctx) {
128 bool ignored;
129 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000130 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000131 APFloat::rmNearestTiesToEven, &ignored);
132 return Result;
133}
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000136 APSInt &Value, ASTContext &Ctx) {
137 unsigned DestWidth = Ctx.getIntWidth(DestType);
138 APSInt Result = Value;
139 // Figure out if this is a truncate, extend or noop cast.
140 // If the input is signed, do a sign extend, noop, or truncate.
141 Result.extOrTrunc(DestWidth);
142 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
143 return Result;
144}
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000147 APSInt &Value, ASTContext &Ctx) {
148
149 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
150 Result.convertFromAPInt(Value, Value.isSigned(),
151 APFloat::rmNearestTiesToEven);
152 return Result;
153}
154
Mike Stumpc4c90452009-10-27 22:09:17 +0000155namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000156class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000157 : public StmtVisitor<HasSideEffect, bool> {
158 EvalInfo &Info;
159public:
160
161 HasSideEffect(EvalInfo &info) : Info(info) {}
162
163 // Unhandled nodes conservatively default to having side effects.
164 bool VisitStmt(Stmt *S) {
165 return true;
166 }
167
168 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
169 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000170 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000171 return true;
172 return false;
173 }
174 // We don't want to evaluate BlockExprs multiple times, as they generate
175 // a ton of code.
176 bool VisitBlockExpr(BlockExpr *E) { return true; }
177 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
178 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
179 { return Visit(E->getInitializer()); }
180 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
181 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
182 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
183 bool VisitStringLiteral(StringLiteral *E) { return false; }
184 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
185 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
186 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000187 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000188 bool VisitChooseExpr(ChooseExpr *E)
189 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
190 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
191 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000192 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000193 bool VisitBinaryOperator(BinaryOperator *E)
194 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000195 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000200 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000201 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000202 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000203 }
204 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
205};
206
Mike Stumpc4c90452009-10-27 22:09:17 +0000207} // end anonymous namespace
208
Eli Friedman4efaa272008-11-12 09:44:48 +0000209//===----------------------------------------------------------------------===//
210// LValue Evaluation
211//===----------------------------------------------------------------------===//
212namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000213class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000214 : public StmtVisitor<LValueExprEvaluator, APValue> {
215 EvalInfo &Info;
216public:
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Eli Friedman4efaa272008-11-12 09:44:48 +0000218 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
219
220 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000221 return APValue();
222 }
223
224 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000225 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000226 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
227 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
228 APValue VisitMemberExpr(MemberExpr *E);
229 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000230 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000231 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000232 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000233 APValue VisitUnaryExtension(const UnaryOperator *E)
234 { return Visit(E->getSubExpr()); }
235 APValue VisitChooseExpr(const ChooseExpr *E)
236 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000237
238 APValue VisitCastExpr(CastExpr *E) {
239 switch (E->getCastKind()) {
240 default:
241 return APValue();
242
243 case CastExpr::CK_NoOp:
244 return Visit(E->getSubExpr());
245 }
246 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000247 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000248};
249} // end anonymous namespace
250
251static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
252 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
253 return Result.isLValue();
254}
255
Mike Stump1eb44332009-09-09 15:08:12 +0000256APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000257 if (isa<FunctionDecl>(E->getDecl())) {
258 return APValue(E, 0);
259 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000260 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000261 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000262 if (!VD->getType()->isReferenceType())
263 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000264 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000265 const VarDecl *Def = 0;
266 if (const Expr *Init = VD->getDefinition(Def))
267 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();
276 return APValue(E, 0);
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(),
313 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
314
315 return result;
316}
317
Mike Stump1eb44332009-09-09 15:08:12 +0000318APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000319 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlsson3068d112008-11-16 19:01:22 +0000321 if (!EvaluatePointer(E->getBase(), Result, Info))
322 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Anders Carlsson3068d112008-11-16 19:01:22 +0000324 APSInt Index;
325 if (!EvaluateInteger(E->getIdx(), Index, Info))
326 return APValue();
327
328 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
329
330 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000332 Result.getLValueOffset() + Offset);
333 return Result;
334}
Eli Friedman4efaa272008-11-12 09:44:48 +0000335
Mike Stump1eb44332009-09-09 15:08:12 +0000336APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000337 APValue Result;
338 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
339 return APValue();
340 return Result;
341}
342
Eli Friedman4efaa272008-11-12 09:44:48 +0000343//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000344// Pointer Evaluation
345//===----------------------------------------------------------------------===//
346
Anders Carlssonc754aa62008-07-08 05:13:58 +0000347namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000348class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000349 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000350 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000351public:
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattner87eae5e2008-07-11 22:52:41 +0000353 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000354
Anders Carlsson2bad1682008-07-08 14:30:00 +0000355 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000356 return APValue();
357 }
358
359 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
360
Anders Carlsson650c92f2008-07-08 15:34:11 +0000361 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000362 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000363 APValue VisitUnaryExtension(const UnaryOperator *E)
364 { return Visit(E->getSubExpr()); }
365 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000366 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
367 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000368 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
369 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000370 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000371 APValue VisitBlockExpr(BlockExpr *E) {
372 if (!E->hasBlockDeclRefExprs())
373 return APValue(E, 0);
374 return APValue();
375 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000376 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
377 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000378 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000379 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000380 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
381 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
382 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000383 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000384};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000385} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000386
Chris Lattner87eae5e2008-07-11 22:52:41 +0000387static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000388 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000389 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000390 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000391 return Result.isLValue();
392}
393
394APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
395 if (E->getOpcode() != BinaryOperator::Add &&
396 E->getOpcode() != BinaryOperator::Sub)
397 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000399 const Expr *PExp = E->getLHS();
400 const Expr *IExp = E->getRHS();
401 if (IExp->getType()->isPointerType())
402 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000404 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000405 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000406 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000408 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000409 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000410 return APValue();
411
Ted Kremenek6217b802009-07-29 21:53:49 +0000412 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000413 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000415 // Explicitly handle GNU void* and function pointer arithmetic extensions.
416 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
417 SizeOfPointee = 1;
418 else
419 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000420
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000421 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000422
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000423 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000424 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000425 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000426 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
427
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000428 return APValue(ResultLValue.getLValueBase(), Offset);
429}
Eli Friedman4efaa272008-11-12 09:44:48 +0000430
Eli Friedman2217c872009-02-22 11:46:18 +0000431APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
432 APValue result;
433 if (EvaluateLValue(E->getSubExpr(), result, Info))
434 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000435 return APValue();
436}
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000438
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000439APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
440 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000441
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000442 switch (E->getCastKind()) {
443 default:
444 break;
445
446 case CastExpr::CK_Unknown: {
447 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
448
449 // Check for pointer->pointer cast
450 if (SubExpr->getType()->isPointerType() ||
451 SubExpr->getType()->isObjCObjectPointerType() ||
452 SubExpr->getType()->isNullPtrType() ||
453 SubExpr->getType()->isBlockPointerType())
454 return Visit(SubExpr);
455
456 if (SubExpr->getType()->isIntegralType()) {
457 APValue Result;
458 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
459 break;
460
461 if (Result.isInt()) {
462 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
463 return APValue(0, Result.getInt().getZExtValue());
464 }
465
466 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000467 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000468 }
469 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000472 case CastExpr::CK_NoOp:
473 case CastExpr::CK_BitCast:
474 case CastExpr::CK_AnyPointerToObjCPointerCast:
475 case CastExpr::CK_AnyPointerToBlockPointerCast:
476 return Visit(SubExpr);
477
478 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000479 APValue Result;
480 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000481 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000482
483 if (Result.isInt()) {
484 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
485 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000488 // Cast is of an lvalue, no need to change value.
489 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000490 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000491 case CastExpr::CK_ArrayToPointerDecay:
492 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000493 APValue Result;
494 if (EvaluateLValue(SubExpr, Result, Info))
495 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000496 break;
497 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000498 }
499
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000500 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000501}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000502
Eli Friedman3941b182009-01-25 01:54:01 +0000503APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000504 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000505 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000506 return APValue(E, 0);
507 return APValue();
508}
509
Eli Friedman4efaa272008-11-12 09:44:48 +0000510APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
511 bool BoolResult;
512 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
513 return APValue();
514
515 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
516
517 APValue Result;
518 if (EvaluatePointer(EvalExpr, Result, Info))
519 return Result;
520 return APValue();
521}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000522
523//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000524// Vector Evaluation
525//===----------------------------------------------------------------------===//
526
527namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000528 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000529 : public StmtVisitor<VectorExprEvaluator, APValue> {
530 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000531 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000532 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Nate Begeman59b5da62009-01-18 03:20:47 +0000534 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Nate Begeman59b5da62009-01-18 03:20:47 +0000536 APValue VisitStmt(Stmt *S) {
537 return APValue();
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Eli Friedman91110ee2009-02-23 04:23:56 +0000540 APValue VisitParenExpr(ParenExpr *E)
541 { return Visit(E->getSubExpr()); }
542 APValue VisitUnaryExtension(const UnaryOperator *E)
543 { return Visit(E->getSubExpr()); }
544 APValue VisitUnaryPlus(const UnaryOperator *E)
545 { return Visit(E->getSubExpr()); }
546 APValue VisitUnaryReal(const UnaryOperator *E)
547 { return Visit(E->getSubExpr()); }
548 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
549 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000550 APValue VisitCastExpr(const CastExpr* E);
551 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
552 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000553 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000554 APValue VisitChooseExpr(const ChooseExpr *E)
555 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000556 APValue VisitUnaryImag(const UnaryOperator *E);
557 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000558 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000559 // shufflevector, ExtVectorElementExpr
560 // (Note that these require implementing conversions
561 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000562 };
563} // end anonymous namespace
564
565static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
566 if (!E->getType()->isVectorType())
567 return false;
568 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
569 return !Result.isUninit();
570}
571
572APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000573 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000574 QualType EltTy = VTy->getElementType();
575 unsigned NElts = VTy->getNumElements();
576 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Nate Begeman59b5da62009-01-18 03:20:47 +0000578 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000579 QualType SETy = SE->getType();
580 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000581
Nate Begemane8c9e922009-06-26 18:22:18 +0000582 // Check for vector->vector bitcast and scalar->vector splat.
583 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000584 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000585 } else if (SETy->isIntegerType()) {
586 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000587 if (!EvaluateInteger(SE, IntResult, Info))
588 return APValue();
589 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000590 } else if (SETy->isRealFloatingType()) {
591 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000592 if (!EvaluateFloat(SE, F, Info))
593 return APValue();
594 Result = APValue(F);
595 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000596 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000597
Nate Begemanc0b8b192009-07-01 07:50:47 +0000598 // For casts of a scalar to ExtVector, convert the scalar to the element type
599 // and splat it to all elements.
600 if (E->getType()->isExtVectorType()) {
601 if (EltTy->isIntegerType() && Result.isInt())
602 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
603 Info.Ctx));
604 else if (EltTy->isIntegerType())
605 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
606 Info.Ctx));
607 else if (EltTy->isRealFloatingType() && Result.isInt())
608 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
609 Info.Ctx));
610 else if (EltTy->isRealFloatingType())
611 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
612 Info.Ctx));
613 else
614 return APValue();
615
616 // Splat and create vector APValue.
617 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
618 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000619 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000620
621 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
622 // to the vector. To construct the APValue vector initializer, bitcast the
623 // initializing value to an APInt, and shift out the bits pertaining to each
624 // element.
625 APSInt Init;
626 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Nate Begemanc0b8b192009-07-01 07:50:47 +0000628 llvm::SmallVector<APValue, 4> Elts;
629 for (unsigned i = 0; i != NElts; ++i) {
630 APSInt Tmp = Init;
631 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Nate Begemanc0b8b192009-07-01 07:50:47 +0000633 if (EltTy->isIntegerType())
634 Elts.push_back(APValue(Tmp));
635 else if (EltTy->isRealFloatingType())
636 Elts.push_back(APValue(APFloat(Tmp)));
637 else
638 return APValue();
639
640 Init >>= EltWidth;
641 }
642 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000643}
644
Mike Stump1eb44332009-09-09 15:08:12 +0000645APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000646VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
647 return this->Visit(const_cast<Expr*>(E->getInitializer()));
648}
649
Mike Stump1eb44332009-09-09 15:08:12 +0000650APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000651VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000652 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000653 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000654 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Nate Begeman59b5da62009-01-18 03:20:47 +0000656 QualType EltTy = VT->getElementType();
657 llvm::SmallVector<APValue, 4> Elements;
658
Eli Friedman91110ee2009-02-23 04:23:56 +0000659 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000660 if (EltTy->isIntegerType()) {
661 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000662 if (i < NumInits) {
663 if (!EvaluateInteger(E->getInit(i), sInt, Info))
664 return APValue();
665 } else {
666 sInt = Info.Ctx.MakeIntValue(0, EltTy);
667 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000668 Elements.push_back(APValue(sInt));
669 } else {
670 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000671 if (i < NumInits) {
672 if (!EvaluateFloat(E->getInit(i), f, Info))
673 return APValue();
674 } else {
675 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
676 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000677 Elements.push_back(APValue(f));
678 }
679 }
680 return APValue(&Elements[0], Elements.size());
681}
682
Mike Stump1eb44332009-09-09 15:08:12 +0000683APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000684VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000685 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000686 QualType EltTy = VT->getElementType();
687 APValue ZeroElement;
688 if (EltTy->isIntegerType())
689 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
690 else
691 ZeroElement =
692 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
693
694 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
695 return APValue(&Elements[0], Elements.size());
696}
697
698APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
699 bool BoolResult;
700 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
701 return APValue();
702
703 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
704
705 APValue Result;
706 if (EvaluateVector(EvalExpr, Result, Info))
707 return Result;
708 return APValue();
709}
710
Eli Friedman91110ee2009-02-23 04:23:56 +0000711APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
712 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
713 Info.EvalResult.HasSideEffects = true;
714 return GetZeroVector(E->getType());
715}
716
Nate Begeman59b5da62009-01-18 03:20:47 +0000717//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000718// Integer Evaluation
719//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000720
721namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000722class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000723 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000724 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000725 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000726public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000727 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000728 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000729
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000730 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000731 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000732 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000733 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000734 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000735 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000736 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000737 return true;
738 }
739
Daniel Dunbar131eb432009-02-19 09:06:44 +0000740 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000741 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000742 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000743 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000744 Result = APValue(APSInt(I));
745 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000746 return true;
747 }
748
749 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000750 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000751 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000752 return true;
753 }
754
Anders Carlsson82206e22008-11-30 18:14:57 +0000755 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000756 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000757 if (Info.EvalResult.Diag == 0) {
758 Info.EvalResult.DiagLoc = L;
759 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000760 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000761 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000762 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Anders Carlssonc754aa62008-07-08 05:13:58 +0000765 //===--------------------------------------------------------------------===//
766 // Visitor Methods
767 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattner32fea9d2008-11-12 07:43:42 +0000769 bool VisitStmt(Stmt *) {
770 assert(0 && "This should be called on integers, stmts are not integers");
771 return false;
772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner32fea9d2008-11-12 07:43:42 +0000774 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000775 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnerb542afe2008-07-11 19:10:17 +0000778 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000779
Chris Lattner4c4867e2008-07-12 00:38:25 +0000780 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000781 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000782 }
783 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000784 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000785 }
786 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000787 // Per gcc docs "this built-in function ignores top level
788 // qualifiers". We need to use the canonical version to properly
789 // be able to strip CRV qualifiers from the type.
790 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
791 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000792 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000793 T1.getUnqualifiedType()),
794 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000795 }
Eli Friedman04309752009-11-24 05:28:59 +0000796
797 bool CheckReferencedDecl(const Expr *E, const Decl *D);
798 bool VisitDeclRefExpr(const DeclRefExpr *E) {
799 return CheckReferencedDecl(E, E->getDecl());
800 }
801 bool VisitMemberExpr(const MemberExpr *E) {
802 if (CheckReferencedDecl(E, E->getMemberDecl())) {
803 // Conservatively assume a MemberExpr will have side-effects
804 Info.EvalResult.HasSideEffects = true;
805 return true;
806 }
807 return false;
808 }
809
Chris Lattner4c4867e2008-07-12 00:38:25 +0000810 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000811 bool VisitBinaryOperator(const BinaryOperator *E);
812 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000813 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000814
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000815 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000816 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
817
Anders Carlsson3068d112008-11-16 19:01:22 +0000818 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000819 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlsson3f704562008-12-21 22:39:40 +0000822 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000823 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anders Carlsson3068d112008-11-16 19:01:22 +0000826 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000827 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000828 }
829
Eli Friedman664a1042009-02-27 04:45:43 +0000830 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
831 return Success(0, E);
832 }
833
Sebastian Redl64b45f72009-01-05 20:52:13 +0000834 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000835 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000836 }
837
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000838 bool VisitChooseExpr(const ChooseExpr *E) {
839 return Visit(E->getChosenSubExpr(Info.Ctx));
840 }
841
Eli Friedman722c7172009-02-28 03:59:05 +0000842 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000843 bool VisitUnaryImag(const UnaryOperator *E);
844
Chris Lattnerfcee0012008-07-11 21:24:13 +0000845private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000846 unsigned GetAlignOfExpr(const Expr *E);
847 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000848 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000849};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000850} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000851
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000852static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000853 if (!E->getType()->isIntegralType())
854 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000856 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
857}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000858
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000859static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
860 APValue Val;
861 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
862 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000863 Result = Val.getInt();
864 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000865}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000866
Eli Friedman04309752009-11-24 05:28:59 +0000867bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000868 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000869 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
870 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000871
872 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000873 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000874 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
875 == Qualifiers::Const) {
Eli Friedman04309752009-11-24 05:28:59 +0000876 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000877 const VarDecl *Def = 0;
Eli Friedman04309752009-11-24 05:28:59 +0000878 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000879 if (APValue *V = VD->getEvaluatedValue()) {
880 if (V->isInt())
881 return Success(V->getInt(), E);
882 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
883 }
884
885 if (VD->isEvaluatingValue())
886 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
887
888 VD->setEvaluatingValue();
889
Douglas Gregor78d15832009-05-26 18:54:04 +0000890 if (Visit(const_cast<Expr*>(Init))) {
891 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000892 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000893 return true;
894 }
895
Eli Friedmanc0131182009-12-03 20:31:57 +0000896 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000897 return false;
898 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000899 }
900 }
901
Chris Lattner4c4867e2008-07-12 00:38:25 +0000902 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000903 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000904}
905
Chris Lattnera4d55d82008-10-06 06:40:35 +0000906/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
907/// as GCC.
908static int EvaluateBuiltinClassifyType(const CallExpr *E) {
909 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000910 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000911 enum gcc_type_class {
912 no_type_class = -1,
913 void_type_class, integer_type_class, char_type_class,
914 enumeral_type_class, boolean_type_class,
915 pointer_type_class, reference_type_class, offset_type_class,
916 real_type_class, complex_type_class,
917 function_type_class, method_type_class,
918 record_type_class, union_type_class,
919 array_type_class, string_type_class,
920 lang_type_class
921 };
Mike Stump1eb44332009-09-09 15:08:12 +0000922
923 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000924 // ideal, however it is what gcc does.
925 if (E->getNumArgs() == 0)
926 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattnera4d55d82008-10-06 06:40:35 +0000928 QualType ArgTy = E->getArg(0)->getType();
929 if (ArgTy->isVoidType())
930 return void_type_class;
931 else if (ArgTy->isEnumeralType())
932 return enumeral_type_class;
933 else if (ArgTy->isBooleanType())
934 return boolean_type_class;
935 else if (ArgTy->isCharType())
936 return string_type_class; // gcc doesn't appear to use char_type_class
937 else if (ArgTy->isIntegerType())
938 return integer_type_class;
939 else if (ArgTy->isPointerType())
940 return pointer_type_class;
941 else if (ArgTy->isReferenceType())
942 return reference_type_class;
943 else if (ArgTy->isRealType())
944 return real_type_class;
945 else if (ArgTy->isComplexType())
946 return complex_type_class;
947 else if (ArgTy->isFunctionType())
948 return function_type_class;
949 else if (ArgTy->isStructureType())
950 return record_type_class;
951 else if (ArgTy->isUnionType())
952 return union_type_class;
953 else if (ArgTy->isArrayType())
954 return array_type_class;
955 else if (ArgTy->isUnionType())
956 return union_type_class;
957 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
958 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
959 return -1;
960}
961
Chris Lattner4c4867e2008-07-12 00:38:25 +0000962bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000963 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000964 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000965 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000966
967 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000968 const Expr *Arg = E->getArg(0)->IgnoreParens();
969 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000970 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000971 && Base.Val.getKind() == APValue::LValue
972 && !Base.HasSideEffects)
973 if (const Expr *LVBase = Base.Val.getLValueBase())
974 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
975 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000976 if (!VD->getType()->isIncompleteType()
977 && VD->getType()->isObjectType()
978 && !VD->getType()->isVariablyModifiedType()
979 && !VD->getType()->isDependentType()) {
980 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
981 uint64_t Offset = Base.Val.getLValueOffset();
982 if (Offset <= Size)
983 Size -= Base.Val.getLValueOffset();
984 else
985 Size = 0;
986 return Success(Size, E);
987 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000988 }
989 }
990
Eric Christopherfee667f2009-12-23 03:49:37 +0000991 // TODO: Perhaps we should let LLVM lower this?
Fariborz Jahanian393c2472009-11-05 18:03:03 +0000992 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000993 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +0000994 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000995 return Success(0, E);
996 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000997
Mike Stump64eda9e2009-10-26 18:35:08 +0000998 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
999 }
1000
Chris Lattner019f4e82008-10-06 05:28:25 +00001001 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001002 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001004 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001005 // __builtin_constant_p always has one operand: it returns true if that
1006 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001007 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001008
1009 case Builtin::BI__builtin_eh_return_data_regno: {
1010 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1011 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1012 return Success(Operand, E);
1013 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001014 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001015}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001016
Chris Lattnerb542afe2008-07-11 19:10:17 +00001017bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001018 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001019 if (!Visit(E->getRHS()))
1020 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001021
Eli Friedman33ef1452009-02-26 10:19:36 +00001022 // If we can't evaluate the LHS, it might have side effects;
1023 // conservatively mark it.
1024 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1025 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001026
Anders Carlsson027f62e2008-12-01 02:07:06 +00001027 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001028 }
1029
1030 if (E->isLogicalOp()) {
1031 // These need to be handled specially because the operands aren't
1032 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001033 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001035 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001036 // We were able to evaluate the LHS, see if we can get away with not
1037 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001038 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001039 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001040
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001041 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001042 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001043 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001044 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001045 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001046 }
1047 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001048 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001049 // We can't evaluate the LHS; however, sometimes the result
1050 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001051 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001052 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001053 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001054 // must have had side effects.
1055 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001056
1057 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001058 }
1059 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001060 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001061
Eli Friedmana6afa762008-11-13 06:09:17 +00001062 return false;
1063 }
1064
Anders Carlsson286f85e2008-11-16 07:17:21 +00001065 QualType LHSTy = E->getLHS()->getType();
1066 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001067
1068 if (LHSTy->isAnyComplexType()) {
1069 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1070 APValue LHS, RHS;
1071
1072 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1073 return false;
1074
1075 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1076 return false;
1077
1078 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001079 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001080 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001081 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001082 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1083
Daniel Dunbar4087e242009-01-29 06:43:41 +00001084 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001085 return Success((CR_r == APFloat::cmpEqual &&
1086 CR_i == APFloat::cmpEqual), E);
1087 else {
1088 assert(E->getOpcode() == BinaryOperator::NE &&
1089 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001090 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001091 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001092 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001093 CR_i == APFloat::cmpLessThan)), E);
1094 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001095 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001096 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001097 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1098 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1099 else {
1100 assert(E->getOpcode() == BinaryOperator::NE &&
1101 "Invalid compex comparison.");
1102 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1103 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1104 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001105 }
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Anders Carlsson286f85e2008-11-16 07:17:21 +00001108 if (LHSTy->isRealFloatingType() &&
1109 RHSTy->isRealFloatingType()) {
1110 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Anders Carlsson286f85e2008-11-16 07:17:21 +00001112 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1113 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Anders Carlsson286f85e2008-11-16 07:17:21 +00001115 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1116 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Anders Carlsson286f85e2008-11-16 07:17:21 +00001118 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001119
Anders Carlsson286f85e2008-11-16 07:17:21 +00001120 switch (E->getOpcode()) {
1121 default:
1122 assert(0 && "Invalid binary operator!");
1123 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001124 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001125 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001126 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001127 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001128 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001129 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001130 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001131 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001132 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001133 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001134 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001135 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001136 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001137 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001140 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1141 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001142 APValue LHSValue;
1143 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1144 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001145
Anders Carlsson3068d112008-11-16 19:01:22 +00001146 APValue RHSValue;
1147 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1148 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001149
Eli Friedman5bc86102009-06-14 02:17:33 +00001150 // Reject any bases from the normal codepath; we special-case comparisons
1151 // to null.
1152 if (LHSValue.getLValueBase()) {
1153 if (!E->isEqualityOp())
1154 return false;
1155 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1156 return false;
1157 bool bres;
1158 if (!EvalPointerValueAsBool(LHSValue, bres))
1159 return false;
1160 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1161 } else if (RHSValue.getLValueBase()) {
1162 if (!E->isEqualityOp())
1163 return false;
1164 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1165 return false;
1166 bool bres;
1167 if (!EvalPointerValueAsBool(RHSValue, bres))
1168 return false;
1169 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1170 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001171
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001172 if (E->getOpcode() == BinaryOperator::Sub) {
1173 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001174 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001175
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001176 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001177 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1178 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001179
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001180 return Success(D, E);
1181 }
1182 bool Result;
1183 if (E->getOpcode() == BinaryOperator::EQ) {
1184 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001185 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001186 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1187 }
1188 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001189 }
1190 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001191 if (!LHSTy->isIntegralType() ||
1192 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001193 // We can't continue from here for non-integral types, and they
1194 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001195 return false;
1196 }
1197
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001198 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001199 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001200 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001201
Eli Friedman42edd0d2009-03-24 01:14:50 +00001202 APValue RHSVal;
1203 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001204 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001205
1206 // Handle cases like (unsigned long)&a + 4.
1207 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1208 uint64_t offset = Result.getLValueOffset();
1209 if (E->getOpcode() == BinaryOperator::Add)
1210 offset += RHSVal.getInt().getZExtValue();
1211 else
1212 offset -= RHSVal.getInt().getZExtValue();
1213 Result = APValue(Result.getLValueBase(), offset);
1214 return true;
1215 }
1216
1217 // Handle cases like 4 + (unsigned long)&a
1218 if (E->getOpcode() == BinaryOperator::Add &&
1219 RHSVal.isLValue() && Result.isInt()) {
1220 uint64_t offset = RHSVal.getLValueOffset();
1221 offset += Result.getInt().getZExtValue();
1222 Result = APValue(RHSVal.getLValueBase(), offset);
1223 return true;
1224 }
1225
1226 // All the following cases expect both operands to be an integer
1227 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001228 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001229
Eli Friedman42edd0d2009-03-24 01:14:50 +00001230 APSInt& RHS = RHSVal.getInt();
1231
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001232 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001233 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001234 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001235 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1236 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1237 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1238 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1239 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1240 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001241 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001242 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001243 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001244 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001245 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001246 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001247 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001248 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001249 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001250 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001251 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001252 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1253 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001254 }
1255 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001256 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001257 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1258 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001261 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1262 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1263 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1264 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1265 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1266 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001267 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001268}
1269
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001270bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001271 bool Cond;
1272 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001273 return false;
1274
Nuno Lopesa25bd552008-11-16 22:06:39 +00001275 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001276}
1277
Chris Lattneraf707ab2009-01-24 21:53:27 +00001278unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001279 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1280 // the result is the size of the referenced type."
1281 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1282 // result shall be the alignment of the referenced type."
1283 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1284 T = Ref->getPointeeType();
1285
Chris Lattnere9feb472009-01-24 21:09:06 +00001286 // Get information about the alignment.
1287 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001288
Eli Friedman2be58612009-05-30 21:09:44 +00001289 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001290 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001291}
1292
Chris Lattneraf707ab2009-01-24 21:53:27 +00001293unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1294 E = E->IgnoreParens();
1295
1296 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001297 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001298 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001299 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001300
Chris Lattneraf707ab2009-01-24 21:53:27 +00001301 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001302 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1303 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001304
Chris Lattnere9feb472009-01-24 21:09:06 +00001305 return GetAlignOfType(E->getType());
1306}
1307
1308
Sebastian Redl05189992008-11-11 17:56:53 +00001309/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1310/// expression's type.
1311bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001312 // Handle alignof separately.
1313 if (!E->isSizeOf()) {
1314 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001315 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001316 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001317 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001318 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001319
Sebastian Redl05189992008-11-11 17:56:53 +00001320 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001321 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1322 // the result is the size of the referenced type."
1323 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1324 // result shall be the alignment of the referenced type."
1325 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1326 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001327
Daniel Dunbar131eb432009-02-19 09:06:44 +00001328 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1329 // extension.
1330 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1331 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001332
Chris Lattnerfcee0012008-07-11 21:24:13 +00001333 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001334 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001335 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001336
Chris Lattnere9feb472009-01-24 21:09:06 +00001337 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001338 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001339 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001340}
1341
Chris Lattnerb542afe2008-07-11 19:10:17 +00001342bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001343 // Special case unary operators that do not need their subexpression
1344 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001345 if (E->isOffsetOfOp()) {
1346 // The AST for offsetof is defined in such a way that we can just
1347 // directly Evaluate it as an l-value.
1348 APValue LV;
1349 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1350 return false;
1351 if (LV.getLValueBase())
1352 return false;
1353 return Success(LV.getLValueOffset(), E);
1354 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001355
1356 if (E->getOpcode() == UnaryOperator::LNot) {
1357 // LNot's operand isn't necessarily an integer, so we handle it specially.
1358 bool bres;
1359 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1360 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001361 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001362 }
1363
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001364 // Only handle integral operations...
1365 if (!E->getSubExpr()->getType()->isIntegralType())
1366 return false;
1367
Chris Lattner87eae5e2008-07-11 22:52:41 +00001368 // Get the operand value into 'Result'.
1369 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001370 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001371
Chris Lattner75a48812008-07-11 22:15:16 +00001372 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001373 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001374 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1375 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001376 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001377 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001378 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1379 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001380 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001381 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001382 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001383 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001384 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001385 if (!Result.isInt()) return false;
1386 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001387 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001388 if (!Result.isInt()) return false;
1389 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001390 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001391}
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Chris Lattner732b2232008-07-12 01:15:53 +00001393/// HandleCast - This is used to evaluate implicit or explicit casts where the
1394/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001395bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001396 Expr *SubExpr = E->getSubExpr();
1397 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001398 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001399
Eli Friedman4efaa272008-11-12 09:44:48 +00001400 if (DestType->isBooleanType()) {
1401 bool BoolResult;
1402 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1403 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001404 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001405 }
1406
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001407 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001408 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001409 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001410 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001411
Eli Friedmanbe265702009-02-20 01:15:07 +00001412 if (!Result.isInt()) {
1413 // Only allow casts of lvalues if they are lossless.
1414 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1415 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001416
Daniel Dunbardd211642009-02-19 22:24:01 +00001417 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001418 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001419 }
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Chris Lattner732b2232008-07-12 01:15:53 +00001421 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001422 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001423 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001424 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001425 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001426
Daniel Dunbardd211642009-02-19 22:24:01 +00001427 if (LV.getLValueBase()) {
1428 // Only allow based lvalue casts if they are lossless.
1429 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1430 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001431
Daniel Dunbardd211642009-02-19 22:24:01 +00001432 Result = LV;
1433 return true;
1434 }
1435
1436 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1437 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001438 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001439
Eli Friedmanbe265702009-02-20 01:15:07 +00001440 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1441 // This handles double-conversion cases, where there's both
1442 // an l-value promotion and an implicit conversion to int.
1443 APValue LV;
1444 if (!EvaluateLValue(SubExpr, LV, Info))
1445 return false;
1446
1447 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1448 return false;
1449
1450 Result = LV;
1451 return true;
1452 }
1453
Eli Friedman1725f682009-04-22 19:23:09 +00001454 if (SrcType->isAnyComplexType()) {
1455 APValue C;
1456 if (!EvaluateComplex(SubExpr, C, Info))
1457 return false;
1458 if (C.isComplexFloat())
1459 return Success(HandleFloatToIntCast(DestType, SrcType,
1460 C.getComplexFloatReal(), Info.Ctx),
1461 E);
1462 else
1463 return Success(HandleIntToIntCast(DestType, SrcType,
1464 C.getComplexIntReal(), Info.Ctx), E);
1465 }
Eli Friedman2217c872009-02-22 11:46:18 +00001466 // FIXME: Handle vectors
1467
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001468 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001469 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001470
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001471 APFloat F(0.0);
1472 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001473 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001475 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001476}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001477
Eli Friedman722c7172009-02-28 03:59:05 +00001478bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1479 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1480 APValue LV;
1481 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1482 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1483 return Success(LV.getComplexIntReal(), E);
1484 }
1485
1486 return Visit(E->getSubExpr());
1487}
1488
Eli Friedman664a1042009-02-27 04:45:43 +00001489bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001490 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1491 APValue LV;
1492 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1493 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1494 return Success(LV.getComplexIntImag(), E);
1495 }
1496
Eli Friedman664a1042009-02-27 04:45:43 +00001497 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1498 Info.EvalResult.HasSideEffects = true;
1499 return Success(0, E);
1500}
1501
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001502//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001503// Float Evaluation
1504//===----------------------------------------------------------------------===//
1505
1506namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001507class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001508 : public StmtVisitor<FloatExprEvaluator, bool> {
1509 EvalInfo &Info;
1510 APFloat &Result;
1511public:
1512 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1513 : Info(info), Result(result) {}
1514
1515 bool VisitStmt(Stmt *S) {
1516 return false;
1517 }
1518
1519 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001520 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001521
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001522 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001523 bool VisitBinaryOperator(const BinaryOperator *E);
1524 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001525 bool VisitCastExpr(CastExpr *E);
1526 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001527 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001528
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001529 bool VisitChooseExpr(const ChooseExpr *E)
1530 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1531 bool VisitUnaryExtension(const UnaryOperator *E)
1532 { return Visit(E->getSubExpr()); }
1533
1534 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001535 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001536};
1537} // end anonymous namespace
1538
1539static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1540 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1541}
1542
Chris Lattner019f4e82008-10-06 05:28:25 +00001543bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001544 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001545 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001546 case Builtin::BI__builtin_huge_val:
1547 case Builtin::BI__builtin_huge_valf:
1548 case Builtin::BI__builtin_huge_vall:
1549 case Builtin::BI__builtin_inf:
1550 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001551 case Builtin::BI__builtin_infl: {
1552 const llvm::fltSemantics &Sem =
1553 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001554 Result = llvm::APFloat::getInf(Sem);
1555 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Chris Lattner9e621712008-10-06 06:31:58 +00001558 case Builtin::BI__builtin_nan:
1559 case Builtin::BI__builtin_nanf:
1560 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001561 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001562 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001563 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001564 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001565 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001566 const llvm::fltSemantics &Sem =
1567 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer33035802009-12-11 13:26:32 +00001568 unsigned Type = 0;
1569 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump4572bab2009-05-30 03:56:50 +00001570 return false;
Benjamin Kramer33035802009-12-11 13:26:32 +00001571 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner9e621712008-10-06 06:31:58 +00001572 return true;
1573 }
1574 }
1575 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001576
1577 case Builtin::BI__builtin_fabs:
1578 case Builtin::BI__builtin_fabsf:
1579 case Builtin::BI__builtin_fabsl:
1580 if (!EvaluateFloat(E->getArg(0), Result, Info))
1581 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001583 if (Result.isNegative())
1584 Result.changeSign();
1585 return true;
1586
Mike Stump1eb44332009-09-09 15:08:12 +00001587 case Builtin::BI__builtin_copysign:
1588 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001589 case Builtin::BI__builtin_copysignl: {
1590 APFloat RHS(0.);
1591 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1592 !EvaluateFloat(E->getArg(1), RHS, Info))
1593 return false;
1594 Result.copySign(RHS);
1595 return true;
1596 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001597 }
1598}
1599
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001600bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001601 if (E->getOpcode() == UnaryOperator::Deref)
1602 return false;
1603
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001604 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1605 return false;
1606
1607 switch (E->getOpcode()) {
1608 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001609 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001610 return true;
1611 case UnaryOperator::Minus:
1612 Result.changeSign();
1613 return true;
1614 }
1615}
Chris Lattner019f4e82008-10-06 05:28:25 +00001616
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001617bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001618 if (E->getOpcode() == BinaryOperator::Comma) {
1619 if (!EvaluateFloat(E->getRHS(), Result, Info))
1620 return false;
1621
1622 // If we can't evaluate the LHS, it might have side effects;
1623 // conservatively mark it.
1624 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1625 Info.EvalResult.HasSideEffects = true;
1626
1627 return true;
1628 }
1629
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001630 // FIXME: Diagnostics? I really don't understand how the warnings
1631 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001632 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001633 if (!EvaluateFloat(E->getLHS(), Result, Info))
1634 return false;
1635 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1636 return false;
1637
1638 switch (E->getOpcode()) {
1639 default: return false;
1640 case BinaryOperator::Mul:
1641 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1642 return true;
1643 case BinaryOperator::Add:
1644 Result.add(RHS, APFloat::rmNearestTiesToEven);
1645 return true;
1646 case BinaryOperator::Sub:
1647 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1648 return true;
1649 case BinaryOperator::Div:
1650 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1651 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001652 }
1653}
1654
1655bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1656 Result = E->getValue();
1657 return true;
1658}
1659
Eli Friedman4efaa272008-11-12 09:44:48 +00001660bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1661 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Eli Friedman4efaa272008-11-12 09:44:48 +00001663 if (SubExpr->getType()->isIntegralType()) {
1664 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001665 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001666 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001667 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001668 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001669 return true;
1670 }
1671 if (SubExpr->getType()->isRealFloatingType()) {
1672 if (!Visit(SubExpr))
1673 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001674 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1675 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001676 return true;
1677 }
Eli Friedman2217c872009-02-22 11:46:18 +00001678 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001679
1680 return false;
1681}
1682
1683bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1684 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1685 return true;
1686}
1687
Eli Friedman67f85fc2009-12-04 02:12:53 +00001688bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1689 bool Cond;
1690 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1691 return false;
1692
1693 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1694}
1695
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001696//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001697// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001698//===----------------------------------------------------------------------===//
1699
1700namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001701class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001702 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001703 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001705public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001706 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001708 //===--------------------------------------------------------------------===//
1709 // Visitor Methods
1710 //===--------------------------------------------------------------------===//
1711
1712 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001713 return APValue();
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001716 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1717
1718 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001719 Expr* SubExpr = E->getSubExpr();
1720
1721 if (SubExpr->getType()->isRealFloatingType()) {
1722 APFloat Result(0.0);
1723
1724 if (!EvaluateFloat(SubExpr, Result, Info))
1725 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001726
1727 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001728 Result);
1729 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001730 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001731 "Unexpected imaginary literal.");
1732
1733 llvm::APSInt Result;
1734 if (!EvaluateInteger(SubExpr, Result, Info))
1735 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001737 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1738 Zero = 0;
1739 return APValue(Zero, Result);
1740 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001741 }
1742
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001743 APValue VisitCastExpr(CastExpr *E) {
1744 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001745 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001746 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001747
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001748 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001749 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001750
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001751 if (!EvaluateFloat(SubExpr, Result, Info))
1752 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001753
1754 if (EltType->isRealFloatingType()) {
1755 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001756 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001757 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1758 } else {
1759 llvm::APSInt IResult;
1760 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1761 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1762 Zero = 0;
1763 return APValue(IResult, Zero);
1764 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001765 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001766 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001767
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001768 if (!EvaluateInteger(SubExpr, Result, Info))
1769 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001770
Eli Friedman1725f682009-04-22 19:23:09 +00001771 if (EltType->isRealFloatingType()) {
1772 APFloat FResult =
1773 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001774 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001775 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1776 } else {
1777 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1778 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1779 Zero = 0;
1780 return APValue(Result, Zero);
1781 }
John McCall183700f2009-09-21 23:43:11 +00001782 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001783 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001784
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001785 if (!EvaluateComplex(SubExpr, Src, Info))
1786 return APValue();
1787
1788 QualType SrcType = CT->getElementType();
1789
1790 if (Src.isComplexFloat()) {
1791 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001792 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001793 Src.getComplexFloatReal(),
1794 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001795 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001796 Src.getComplexFloatImag(),
1797 Info.Ctx));
1798 } else {
1799 return APValue(HandleFloatToIntCast(EltType, SrcType,
1800 Src.getComplexFloatReal(),
1801 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001802 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001803 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001804 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001805 }
1806 } else {
1807 assert(Src.isComplexInt() && "Invalid evaluate result.");
1808 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001809 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001810 Src.getComplexIntReal(),
1811 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001812 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001813 Src.getComplexIntImag(),
1814 Info.Ctx));
1815 } else {
1816 return APValue(HandleIntToIntCast(EltType, SrcType,
1817 Src.getComplexIntReal(),
1818 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001819 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001820 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001821 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001822 }
1823 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001824 }
1825
1826 // FIXME: Handle more casts.
1827 return APValue();
1828 }
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001830 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001831 APValue VisitChooseExpr(const ChooseExpr *E)
1832 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1833 APValue VisitUnaryExtension(const UnaryOperator *E)
1834 { return Visit(E->getSubExpr()); }
1835 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001836 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001837};
1838} // end anonymous namespace
1839
Mike Stump1eb44332009-09-09 15:08:12 +00001840static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001841 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1842 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001843 (&Result.getComplexFloatReal().getSemantics() ==
1844 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001845 "Invalid complex evaluation.");
1846 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001847}
1848
Mike Stump1eb44332009-09-09 15:08:12 +00001849APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001850 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001852 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001853 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001855 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001856 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001857
Daniel Dunbar3f279872009-01-29 01:32:56 +00001858 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1859 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001860 switch (E->getOpcode()) {
1861 default: return APValue();
1862 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001863 if (Result.isComplexFloat()) {
1864 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1865 APFloat::rmNearestTiesToEven);
1866 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1867 APFloat::rmNearestTiesToEven);
1868 } else {
1869 Result.getComplexIntReal() += RHS.getComplexIntReal();
1870 Result.getComplexIntImag() += RHS.getComplexIntImag();
1871 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001872 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001873 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001874 if (Result.isComplexFloat()) {
1875 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1876 APFloat::rmNearestTiesToEven);
1877 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1878 APFloat::rmNearestTiesToEven);
1879 } else {
1880 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1881 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1882 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001883 break;
1884 case BinaryOperator::Mul:
1885 if (Result.isComplexFloat()) {
1886 APValue LHS = Result;
1887 APFloat &LHS_r = LHS.getComplexFloatReal();
1888 APFloat &LHS_i = LHS.getComplexFloatImag();
1889 APFloat &RHS_r = RHS.getComplexFloatReal();
1890 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Daniel Dunbar3f279872009-01-29 01:32:56 +00001892 APFloat Tmp = LHS_r;
1893 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1894 Result.getComplexFloatReal() = Tmp;
1895 Tmp = LHS_i;
1896 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1897 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1898
1899 Tmp = LHS_r;
1900 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1901 Result.getComplexFloatImag() = Tmp;
1902 Tmp = LHS_i;
1903 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1904 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1905 } else {
1906 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001907 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001908 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1909 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001910 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001911 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1912 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1913 }
1914 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001915 }
1916
1917 return Result;
1918}
1919
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001920//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001921// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001922//===----------------------------------------------------------------------===//
1923
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001924/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001925/// any crazy technique (that has nothing to do with language standards) that
1926/// we want to. If this function returns true, it returns the folded constant
1927/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001928bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1929 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001930
Nate Begeman59b5da62009-01-18 03:20:47 +00001931 if (getType()->isVectorType()) {
1932 if (!EvaluateVector(this, Result.Val, Info))
1933 return false;
1934 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001935 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001936 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001937 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001938 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001939 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001940 } else if (getType()->isRealFloatingType()) {
1941 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001942 if (!EvaluateFloat(this, f, Info))
1943 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001945 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001946 } else if (getType()->isAnyComplexType()) {
1947 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001948 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001949 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001950 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001951
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001952 return true;
1953}
1954
Mike Stump660e6f72009-10-26 23:05:19 +00001955bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1956 EvalInfo Info(Ctx, Result, true);
1957
1958 if (getType()->isVectorType()) {
1959 if (!EvaluateVector(this, Result.Val, Info))
1960 return false;
1961 } else if (getType()->isIntegerType()) {
1962 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1963 return false;
1964 } else if (getType()->hasPointerRepresentation()) {
1965 if (!EvaluatePointer(this, Result.Val, Info))
1966 return false;
1967 } else if (getType()->isRealFloatingType()) {
1968 llvm::APFloat f(0.0);
1969 if (!EvaluateFloat(this, f, Info))
1970 return false;
1971
1972 Result.Val = APValue(f);
1973 } else if (getType()->isAnyComplexType()) {
1974 if (!EvaluateComplex(this, Result.Val, Info))
1975 return false;
1976 } else
1977 return false;
1978
1979 return true;
1980}
1981
John McCallcd7a4452010-01-05 23:42:56 +00001982bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
1983 EvalResult Scratch;
1984 EvalInfo Info(Ctx, Scratch);
1985
1986 return HandleConversionToBool(this, Result, Info);
1987}
1988
Anders Carlsson1b782762009-04-10 04:54:13 +00001989bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1990 EvalInfo Info(Ctx, Result);
1991
1992 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1993}
1994
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001995bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1996 EvalInfo Info(Ctx, Result, true);
1997
1998 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1999}
2000
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002001/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002002/// folded, but discard the result.
2003bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002004 EvalResult Result;
2005 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002006}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002007
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002008bool Expr::HasSideEffects(ASTContext &Ctx) const {
2009 Expr::EvalResult Result;
2010 EvalInfo Info(Ctx, Result);
2011 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2012}
2013
Anders Carlsson51fe9962008-11-22 21:04:56 +00002014APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002015 EvalResult EvalResult;
2016 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002017 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002018 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002019 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002020
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002021 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002022}