blob: d49a8216f3ac7914af1ef7a1663031d49ecbb680 [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
Eli Friedman4efaa272008-11-12 09:44:48 +000076static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
77 if (E->getType()->isIntegralType()) {
78 APSInt IntResult;
79 if (!EvaluateInteger(E, IntResult, Info))
80 return false;
81 Result = IntResult != 0;
82 return true;
83 } else if (E->getType()->isRealFloatingType()) {
84 APFloat FloatResult(0.0);
85 if (!EvaluateFloat(E, FloatResult, Info))
86 return false;
87 Result = !FloatResult.isZero();
88 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000094 } else if (E->getType()->isAnyComplexType()) {
95 APValue ComplexResult;
96 if (!EvaluateComplex(E, ComplexResult, Info))
97 return false;
98 if (ComplexResult.isComplexFloat()) {
99 Result = !ComplexResult.getComplexFloatReal().isZero() ||
100 !ComplexResult.getComplexFloatImag().isZero();
101 } else {
102 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
103 ComplexResult.getComplexIntImag().getBoolValue();
104 }
105 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000112 APFloat &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 // Determine whether we are converting to unsigned or signed.
115 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000117 // FIXME: Warning for overflow.
118 uint64_t Space[4];
119 bool ignored;
120 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
121 llvm::APFloat::rmTowardZero, &ignored);
122 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump1eb44332009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000135 APSInt &Value, ASTContext &Ctx) {
136 unsigned DestWidth = Ctx.getIntWidth(DestType);
137 APSInt Result = Value;
138 // Figure out if this is a truncate, extend or noop cast.
139 // If the input is signed, do a sign extend, noop, or truncate.
140 Result.extOrTrunc(DestWidth);
141 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
142 return Result;
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Mike Stumpc4c90452009-10-27 22:09:17 +0000154namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000155class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000156 : public StmtVisitor<HasSideEffect, bool> {
157 EvalInfo &Info;
158public:
159
160 HasSideEffect(EvalInfo &info) : Info(info) {}
161
162 // Unhandled nodes conservatively default to having side effects.
163 bool VisitStmt(Stmt *S) {
164 return true;
165 }
166
167 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
168 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000169 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000170 return true;
171 return false;
172 }
173 // We don't want to evaluate BlockExprs multiple times, as they generate
174 // a ton of code.
175 bool VisitBlockExpr(BlockExpr *E) { return true; }
176 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
177 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
178 { return Visit(E->getInitializer()); }
179 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
180 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
181 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
182 bool VisitStringLiteral(StringLiteral *E) { return false; }
183 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
184 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
185 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000186 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000187 bool VisitChooseExpr(ChooseExpr *E)
188 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
189 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
190 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000191 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000192 bool VisitBinaryOperator(BinaryOperator *E)
193 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000194 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
195 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000199 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000200 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000201 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000202 }
203 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
204};
205
Mike Stumpc4c90452009-10-27 22:09:17 +0000206} // end anonymous namespace
207
Eli Friedman4efaa272008-11-12 09:44:48 +0000208//===----------------------------------------------------------------------===//
209// LValue Evaluation
210//===----------------------------------------------------------------------===//
211namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000212class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000213 : public StmtVisitor<LValueExprEvaluator, APValue> {
214 EvalInfo &Info;
215public:
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Eli Friedman4efaa272008-11-12 09:44:48 +0000217 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
218
219 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000220 return APValue();
221 }
222
223 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000224 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000225 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
226 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
227 APValue VisitMemberExpr(MemberExpr *E);
228 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000229 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000230 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000231 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000232 APValue VisitUnaryExtension(const UnaryOperator *E)
233 { return Visit(E->getSubExpr()); }
234 APValue VisitChooseExpr(const ChooseExpr *E)
235 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000236
237 APValue VisitCastExpr(CastExpr *E) {
238 switch (E->getCastKind()) {
239 default:
240 return APValue();
241
242 case CastExpr::CK_NoOp:
243 return Visit(E->getSubExpr());
244 }
245 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000246 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000247};
248} // end anonymous namespace
249
250static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
251 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
252 return Result.isLValue();
253}
254
Mike Stump1eb44332009-09-09 15:08:12 +0000255APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000256 if (isa<FunctionDecl>(E->getDecl())) {
257 return APValue(E, 0);
258 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000259 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000260 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000261 if (!VD->getType()->isReferenceType())
262 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000263 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000264 const VarDecl *Def = 0;
265 if (const Expr *Init = VD->getDefinition(Def))
266 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000267 }
268
269 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000270}
271
Eli Friedman4efaa272008-11-12 09:44:48 +0000272APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000273 if (!Info.AnyLValue && !E->isFileScope())
274 return APValue();
275 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000276}
277
278APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
279 APValue result;
280 QualType Ty;
281 if (E->isArrow()) {
282 if (!EvaluatePointer(E->getBase(), result, Info))
283 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000284 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000285 } else {
286 result = Visit(E->getBase());
287 if (result.isUninit())
288 return APValue();
289 Ty = E->getBase()->getType();
290 }
291
Ted Kremenek6217b802009-07-29 21:53:49 +0000292 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000293 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000294
295 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
296 if (!FD) // FIXME: deal with other kinds of member expressions
297 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000298
299 if (FD->getType()->isReferenceType())
300 return APValue();
301
Eli Friedman4efaa272008-11-12 09:44:48 +0000302 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000303 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000304 for (RecordDecl::field_iterator Field = RD->field_begin(),
305 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000306 Field != FieldEnd; (void)++Field, ++i) {
307 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000308 break;
309 }
310
311 result.setLValue(result.getLValueBase(),
312 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
313
314 return result;
315}
316
Mike Stump1eb44332009-09-09 15:08:12 +0000317APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000318 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Anders Carlsson3068d112008-11-16 19:01:22 +0000320 if (!EvaluatePointer(E->getBase(), Result, Info))
321 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Anders Carlsson3068d112008-11-16 19:01:22 +0000323 APSInt Index;
324 if (!EvaluateInteger(E->getIdx(), Index, Info))
325 return APValue();
326
327 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
328
329 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000330 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000331 Result.getLValueOffset() + Offset);
332 return Result;
333}
Eli Friedman4efaa272008-11-12 09:44:48 +0000334
Mike Stump1eb44332009-09-09 15:08:12 +0000335APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000336 APValue Result;
337 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
338 return APValue();
339 return Result;
340}
341
Eli Friedman4efaa272008-11-12 09:44:48 +0000342//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000343// Pointer Evaluation
344//===----------------------------------------------------------------------===//
345
Anders Carlssonc754aa62008-07-08 05:13:58 +0000346namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000347class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000348 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000349 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000350public:
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner87eae5e2008-07-11 22:52:41 +0000352 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000353
Anders Carlsson2bad1682008-07-08 14:30:00 +0000354 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000355 return APValue();
356 }
357
358 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
359
Anders Carlsson650c92f2008-07-08 15:34:11 +0000360 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000361 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000362 APValue VisitUnaryExtension(const UnaryOperator *E)
363 { return Visit(E->getSubExpr()); }
364 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000365 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
366 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000367 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
368 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000369 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000370 APValue VisitBlockExpr(BlockExpr *E) {
371 if (!E->hasBlockDeclRefExprs())
372 return APValue(E, 0);
373 return APValue();
374 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000375 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
376 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000377 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000378 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000379 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
380 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
381 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000382 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000383};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000384} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000385
Chris Lattner87eae5e2008-07-11 22:52:41 +0000386static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000387 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000388 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000389 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000390 return Result.isLValue();
391}
392
393APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
394 if (E->getOpcode() != BinaryOperator::Add &&
395 E->getOpcode() != BinaryOperator::Sub)
396 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000398 const Expr *PExp = E->getLHS();
399 const Expr *IExp = E->getRHS();
400 if (IExp->getType()->isPointerType())
401 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000403 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000404 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000405 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000407 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000408 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000409 return APValue();
410
Ted Kremenek6217b802009-07-29 21:53:49 +0000411 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000412 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000414 // Explicitly handle GNU void* and function pointer arithmetic extensions.
415 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
416 SizeOfPointee = 1;
417 else
418 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000419
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000420 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000421
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000422 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000423 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000424 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000425 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
426
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427 return APValue(ResultLValue.getLValueBase(), Offset);
428}
Eli Friedman4efaa272008-11-12 09:44:48 +0000429
Eli Friedman2217c872009-02-22 11:46:18 +0000430APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
431 APValue result;
432 if (EvaluateLValue(E->getSubExpr(), result, Info))
433 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000434 return APValue();
435}
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000437
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000438APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
439 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000440
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000441 switch (E->getCastKind()) {
442 default:
443 break;
444
445 case CastExpr::CK_Unknown: {
446 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
447
448 // Check for pointer->pointer cast
449 if (SubExpr->getType()->isPointerType() ||
450 SubExpr->getType()->isObjCObjectPointerType() ||
451 SubExpr->getType()->isNullPtrType() ||
452 SubExpr->getType()->isBlockPointerType())
453 return Visit(SubExpr);
454
455 if (SubExpr->getType()->isIntegralType()) {
456 APValue Result;
457 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
458 break;
459
460 if (Result.isInt()) {
461 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
462 return APValue(0, Result.getInt().getZExtValue());
463 }
464
465 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000466 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000467 }
468 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000471 case CastExpr::CK_NoOp:
472 case CastExpr::CK_BitCast:
473 case CastExpr::CK_AnyPointerToObjCPointerCast:
474 case CastExpr::CK_AnyPointerToBlockPointerCast:
475 return Visit(SubExpr);
476
477 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000478 APValue Result;
479 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000480 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000481
482 if (Result.isInt()) {
483 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
484 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000487 // Cast is of an lvalue, no need to change value.
488 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000489 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000490 case CastExpr::CK_ArrayToPointerDecay:
491 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000492 APValue Result;
493 if (EvaluateLValue(SubExpr, Result, Info))
494 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000495 break;
496 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000497 }
498
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000499 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000500}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000501
Eli Friedman3941b182009-01-25 01:54:01 +0000502APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000503 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000504 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000505 return APValue(E, 0);
506 return APValue();
507}
508
Eli Friedman4efaa272008-11-12 09:44:48 +0000509APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
510 bool BoolResult;
511 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
512 return APValue();
513
514 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
515
516 APValue Result;
517 if (EvaluatePointer(EvalExpr, Result, Info))
518 return Result;
519 return APValue();
520}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000521
522//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000523// Vector Evaluation
524//===----------------------------------------------------------------------===//
525
526namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000527 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000528 : public StmtVisitor<VectorExprEvaluator, APValue> {
529 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000530 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000531 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Nate Begeman59b5da62009-01-18 03:20:47 +0000533 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Nate Begeman59b5da62009-01-18 03:20:47 +0000535 APValue VisitStmt(Stmt *S) {
536 return APValue();
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Eli Friedman91110ee2009-02-23 04:23:56 +0000539 APValue VisitParenExpr(ParenExpr *E)
540 { return Visit(E->getSubExpr()); }
541 APValue VisitUnaryExtension(const UnaryOperator *E)
542 { return Visit(E->getSubExpr()); }
543 APValue VisitUnaryPlus(const UnaryOperator *E)
544 { return Visit(E->getSubExpr()); }
545 APValue VisitUnaryReal(const UnaryOperator *E)
546 { return Visit(E->getSubExpr()); }
547 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
548 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000549 APValue VisitCastExpr(const CastExpr* E);
550 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
551 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000552 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000553 APValue VisitChooseExpr(const ChooseExpr *E)
554 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000555 APValue VisitUnaryImag(const UnaryOperator *E);
556 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000557 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000558 // shufflevector, ExtVectorElementExpr
559 // (Note that these require implementing conversions
560 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000561 };
562} // end anonymous namespace
563
564static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
565 if (!E->getType()->isVectorType())
566 return false;
567 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
568 return !Result.isUninit();
569}
570
571APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000572 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000573 QualType EltTy = VTy->getElementType();
574 unsigned NElts = VTy->getNumElements();
575 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Nate Begeman59b5da62009-01-18 03:20:47 +0000577 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000578 QualType SETy = SE->getType();
579 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000580
Nate Begemane8c9e922009-06-26 18:22:18 +0000581 // Check for vector->vector bitcast and scalar->vector splat.
582 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000583 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000584 } else if (SETy->isIntegerType()) {
585 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000586 if (!EvaluateInteger(SE, IntResult, Info))
587 return APValue();
588 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000589 } else if (SETy->isRealFloatingType()) {
590 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000591 if (!EvaluateFloat(SE, F, Info))
592 return APValue();
593 Result = APValue(F);
594 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000595 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000596
Nate Begemanc0b8b192009-07-01 07:50:47 +0000597 // For casts of a scalar to ExtVector, convert the scalar to the element type
598 // and splat it to all elements.
599 if (E->getType()->isExtVectorType()) {
600 if (EltTy->isIntegerType() && Result.isInt())
601 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
602 Info.Ctx));
603 else if (EltTy->isIntegerType())
604 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
605 Info.Ctx));
606 else if (EltTy->isRealFloatingType() && Result.isInt())
607 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
608 Info.Ctx));
609 else if (EltTy->isRealFloatingType())
610 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
611 Info.Ctx));
612 else
613 return APValue();
614
615 // Splat and create vector APValue.
616 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
617 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000618 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000619
620 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
621 // to the vector. To construct the APValue vector initializer, bitcast the
622 // initializing value to an APInt, and shift out the bits pertaining to each
623 // element.
624 APSInt Init;
625 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Nate Begemanc0b8b192009-07-01 07:50:47 +0000627 llvm::SmallVector<APValue, 4> Elts;
628 for (unsigned i = 0; i != NElts; ++i) {
629 APSInt Tmp = Init;
630 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Nate Begemanc0b8b192009-07-01 07:50:47 +0000632 if (EltTy->isIntegerType())
633 Elts.push_back(APValue(Tmp));
634 else if (EltTy->isRealFloatingType())
635 Elts.push_back(APValue(APFloat(Tmp)));
636 else
637 return APValue();
638
639 Init >>= EltWidth;
640 }
641 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000642}
643
Mike Stump1eb44332009-09-09 15:08:12 +0000644APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000645VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
646 return this->Visit(const_cast<Expr*>(E->getInitializer()));
647}
648
Mike Stump1eb44332009-09-09 15:08:12 +0000649APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000650VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000651 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000652 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000653 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Nate Begeman59b5da62009-01-18 03:20:47 +0000655 QualType EltTy = VT->getElementType();
656 llvm::SmallVector<APValue, 4> Elements;
657
Eli Friedman91110ee2009-02-23 04:23:56 +0000658 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000659 if (EltTy->isIntegerType()) {
660 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000661 if (i < NumInits) {
662 if (!EvaluateInteger(E->getInit(i), sInt, Info))
663 return APValue();
664 } else {
665 sInt = Info.Ctx.MakeIntValue(0, EltTy);
666 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000667 Elements.push_back(APValue(sInt));
668 } else {
669 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000670 if (i < NumInits) {
671 if (!EvaluateFloat(E->getInit(i), f, Info))
672 return APValue();
673 } else {
674 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
675 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000676 Elements.push_back(APValue(f));
677 }
678 }
679 return APValue(&Elements[0], Elements.size());
680}
681
Mike Stump1eb44332009-09-09 15:08:12 +0000682APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000683VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000684 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000685 QualType EltTy = VT->getElementType();
686 APValue ZeroElement;
687 if (EltTy->isIntegerType())
688 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
689 else
690 ZeroElement =
691 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
692
693 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
694 return APValue(&Elements[0], Elements.size());
695}
696
697APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
698 bool BoolResult;
699 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
700 return APValue();
701
702 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
703
704 APValue Result;
705 if (EvaluateVector(EvalExpr, Result, Info))
706 return Result;
707 return APValue();
708}
709
Eli Friedman91110ee2009-02-23 04:23:56 +0000710APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
711 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
712 Info.EvalResult.HasSideEffects = true;
713 return GetZeroVector(E->getType());
714}
715
Nate Begeman59b5da62009-01-18 03:20:47 +0000716//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000717// Integer Evaluation
718//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000719
720namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000721class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000722 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000723 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000724 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000725public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000726 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000727 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000728
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000729 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000730 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000731 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000732 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000733 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000734 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000735 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000736 return true;
737 }
738
Daniel Dunbar131eb432009-02-19 09:06:44 +0000739 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000740 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000741 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000742 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000743 Result = APValue(APSInt(I));
744 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000745 return true;
746 }
747
748 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000749 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000750 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000751 return true;
752 }
753
Anders Carlsson82206e22008-11-30 18:14:57 +0000754 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000755 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000756 if (Info.EvalResult.Diag == 0) {
757 Info.EvalResult.DiagLoc = L;
758 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000759 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000760 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000761 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Anders Carlssonc754aa62008-07-08 05:13:58 +0000764 //===--------------------------------------------------------------------===//
765 // Visitor Methods
766 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattner32fea9d2008-11-12 07:43:42 +0000768 bool VisitStmt(Stmt *) {
769 assert(0 && "This should be called on integers, stmts are not integers");
770 return false;
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner32fea9d2008-11-12 07:43:42 +0000773 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000774 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnerb542afe2008-07-11 19:10:17 +0000777 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000778
Chris Lattner4c4867e2008-07-12 00:38:25 +0000779 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000780 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000781 }
782 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000783 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000784 }
785 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000786 // Per gcc docs "this built-in function ignores top level
787 // qualifiers". We need to use the canonical version to properly
788 // be able to strip CRV qualifiers from the type.
789 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
790 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000791 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000792 T1.getUnqualifiedType()),
793 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000794 }
Eli Friedman04309752009-11-24 05:28:59 +0000795
796 bool CheckReferencedDecl(const Expr *E, const Decl *D);
797 bool VisitDeclRefExpr(const DeclRefExpr *E) {
798 return CheckReferencedDecl(E, E->getDecl());
799 }
800 bool VisitMemberExpr(const MemberExpr *E) {
801 if (CheckReferencedDecl(E, E->getMemberDecl())) {
802 // Conservatively assume a MemberExpr will have side-effects
803 Info.EvalResult.HasSideEffects = true;
804 return true;
805 }
806 return false;
807 }
808
Chris Lattner4c4867e2008-07-12 00:38:25 +0000809 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000810 bool VisitBinaryOperator(const BinaryOperator *E);
811 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000812 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000813
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000814 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000815 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
816
Anders Carlsson3068d112008-11-16 19:01:22 +0000817 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000818 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Anders Carlsson3f704562008-12-21 22:39:40 +0000821 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000822 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Anders Carlsson3068d112008-11-16 19:01:22 +0000825 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000826 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000827 }
828
Eli Friedman664a1042009-02-27 04:45:43 +0000829 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
830 return Success(0, E);
831 }
832
Sebastian Redl64b45f72009-01-05 20:52:13 +0000833 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000834 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000835 }
836
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000837 bool VisitChooseExpr(const ChooseExpr *E) {
838 return Visit(E->getChosenSubExpr(Info.Ctx));
839 }
840
Eli Friedman722c7172009-02-28 03:59:05 +0000841 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000842 bool VisitUnaryImag(const UnaryOperator *E);
843
Chris Lattnerfcee0012008-07-11 21:24:13 +0000844private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000845 unsigned GetAlignOfExpr(const Expr *E);
846 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000847 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000848};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000849} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000850
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000851static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000852 if (!E->getType()->isIntegralType())
853 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000855 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
856}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000857
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000858static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
859 APValue Val;
860 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
861 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000862 Result = Val.getInt();
863 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000864}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000865
Eli Friedman04309752009-11-24 05:28:59 +0000866bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000867 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000868 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
869 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000870
871 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000872 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000873 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
874 == Qualifiers::Const) {
Eli Friedman04309752009-11-24 05:28:59 +0000875 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000876 const VarDecl *Def = 0;
Eli Friedman04309752009-11-24 05:28:59 +0000877 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000878 if (APValue *V = VD->getEvaluatedValue()) {
879 if (V->isInt())
880 return Success(V->getInt(), E);
881 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
882 }
883
884 if (VD->isEvaluatingValue())
885 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
886
887 VD->setEvaluatingValue();
888
Douglas Gregor78d15832009-05-26 18:54:04 +0000889 if (Visit(const_cast<Expr*>(Init))) {
890 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000891 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000892 return true;
893 }
894
Eli Friedmanc0131182009-12-03 20:31:57 +0000895 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000896 return false;
897 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000898 }
899 }
900
Chris Lattner4c4867e2008-07-12 00:38:25 +0000901 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000902 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000903}
904
Chris Lattnera4d55d82008-10-06 06:40:35 +0000905/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
906/// as GCC.
907static int EvaluateBuiltinClassifyType(const CallExpr *E) {
908 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000909 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000910 enum gcc_type_class {
911 no_type_class = -1,
912 void_type_class, integer_type_class, char_type_class,
913 enumeral_type_class, boolean_type_class,
914 pointer_type_class, reference_type_class, offset_type_class,
915 real_type_class, complex_type_class,
916 function_type_class, method_type_class,
917 record_type_class, union_type_class,
918 array_type_class, string_type_class,
919 lang_type_class
920 };
Mike Stump1eb44332009-09-09 15:08:12 +0000921
922 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000923 // ideal, however it is what gcc does.
924 if (E->getNumArgs() == 0)
925 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattnera4d55d82008-10-06 06:40:35 +0000927 QualType ArgTy = E->getArg(0)->getType();
928 if (ArgTy->isVoidType())
929 return void_type_class;
930 else if (ArgTy->isEnumeralType())
931 return enumeral_type_class;
932 else if (ArgTy->isBooleanType())
933 return boolean_type_class;
934 else if (ArgTy->isCharType())
935 return string_type_class; // gcc doesn't appear to use char_type_class
936 else if (ArgTy->isIntegerType())
937 return integer_type_class;
938 else if (ArgTy->isPointerType())
939 return pointer_type_class;
940 else if (ArgTy->isReferenceType())
941 return reference_type_class;
942 else if (ArgTy->isRealType())
943 return real_type_class;
944 else if (ArgTy->isComplexType())
945 return complex_type_class;
946 else if (ArgTy->isFunctionType())
947 return function_type_class;
948 else if (ArgTy->isStructureType())
949 return record_type_class;
950 else if (ArgTy->isUnionType())
951 return union_type_class;
952 else if (ArgTy->isArrayType())
953 return array_type_class;
954 else if (ArgTy->isUnionType())
955 return union_type_class;
956 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
957 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
958 return -1;
959}
960
Chris Lattner4c4867e2008-07-12 00:38:25 +0000961bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000962 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000963 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000964 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000965
966 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000967 const Expr *Arg = E->getArg(0)->IgnoreParens();
968 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000969 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000970 && Base.Val.getKind() == APValue::LValue
971 && !Base.HasSideEffects)
972 if (const Expr *LVBase = Base.Val.getLValueBase())
973 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
974 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000975 if (!VD->getType()->isIncompleteType()
976 && VD->getType()->isObjectType()
977 && !VD->getType()->isVariablyModifiedType()
978 && !VD->getType()->isDependentType()) {
979 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
980 uint64_t Offset = Base.Val.getLValueOffset();
981 if (Offset <= Size)
982 Size -= Base.Val.getLValueOffset();
983 else
984 Size = 0;
985 return Success(Size, E);
986 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000987 }
988 }
989
Eric Christopherfee667f2009-12-23 03:49:37 +0000990 // TODO: Perhaps we should let LLVM lower this?
Fariborz Jahanian393c2472009-11-05 18:03:03 +0000991 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000992 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +0000993 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000994 return Success(0, E);
995 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000996
Mike Stump64eda9e2009-10-26 18:35:08 +0000997 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
998 }
999
Chris Lattner019f4e82008-10-06 05:28:25 +00001000 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001001 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001003 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001004 // __builtin_constant_p always has one operand: it returns true if that
1005 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001006 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001007
1008 case Builtin::BI__builtin_eh_return_data_regno: {
1009 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1010 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1011 return Success(Operand, E);
1012 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001013 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001014}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001015
Chris Lattnerb542afe2008-07-11 19:10:17 +00001016bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001017 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001018 if (!Visit(E->getRHS()))
1019 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001020
Eli Friedman33ef1452009-02-26 10:19:36 +00001021 // If we can't evaluate the LHS, it might have side effects;
1022 // conservatively mark it.
1023 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1024 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001025
Anders Carlsson027f62e2008-12-01 02:07:06 +00001026 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001027 }
1028
1029 if (E->isLogicalOp()) {
1030 // These need to be handled specially because the operands aren't
1031 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001032 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001034 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001035 // We were able to evaluate the LHS, see if we can get away with not
1036 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001037 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001038 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001039
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001040 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001041 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001042 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001043 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001044 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001045 }
1046 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001047 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001048 // We can't evaluate the LHS; however, sometimes the result
1049 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001050 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001051 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001052 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001053 // must have had side effects.
1054 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001055
1056 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001057 }
1058 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001059 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001060
Eli Friedmana6afa762008-11-13 06:09:17 +00001061 return false;
1062 }
1063
Anders Carlsson286f85e2008-11-16 07:17:21 +00001064 QualType LHSTy = E->getLHS()->getType();
1065 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001066
1067 if (LHSTy->isAnyComplexType()) {
1068 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1069 APValue LHS, RHS;
1070
1071 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1072 return false;
1073
1074 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1075 return false;
1076
1077 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001078 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001079 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001080 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001081 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1082
Daniel Dunbar4087e242009-01-29 06:43:41 +00001083 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001084 return Success((CR_r == APFloat::cmpEqual &&
1085 CR_i == APFloat::cmpEqual), E);
1086 else {
1087 assert(E->getOpcode() == BinaryOperator::NE &&
1088 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001089 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001090 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001091 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001092 CR_i == APFloat::cmpLessThan)), E);
1093 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001094 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001095 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001096 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1097 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1098 else {
1099 assert(E->getOpcode() == BinaryOperator::NE &&
1100 "Invalid compex comparison.");
1101 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1102 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1103 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001104 }
1105 }
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Anders Carlsson286f85e2008-11-16 07:17:21 +00001107 if (LHSTy->isRealFloatingType() &&
1108 RHSTy->isRealFloatingType()) {
1109 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Anders Carlsson286f85e2008-11-16 07:17:21 +00001111 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1112 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Anders Carlsson286f85e2008-11-16 07:17:21 +00001114 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1115 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Anders Carlsson286f85e2008-11-16 07:17:21 +00001117 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001118
Anders Carlsson286f85e2008-11-16 07:17:21 +00001119 switch (E->getOpcode()) {
1120 default:
1121 assert(0 && "Invalid binary operator!");
1122 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001123 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001124 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001125 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001126 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001127 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001128 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001129 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001130 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001131 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001132 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001133 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001134 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001135 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001136 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001139 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1140 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001141 APValue LHSValue;
1142 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1143 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001144
Anders Carlsson3068d112008-11-16 19:01:22 +00001145 APValue RHSValue;
1146 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1147 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001148
Eli Friedman5bc86102009-06-14 02:17:33 +00001149 // Reject any bases from the normal codepath; we special-case comparisons
1150 // to null.
1151 if (LHSValue.getLValueBase()) {
1152 if (!E->isEqualityOp())
1153 return false;
1154 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1155 return false;
1156 bool bres;
1157 if (!EvalPointerValueAsBool(LHSValue, bres))
1158 return false;
1159 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1160 } else if (RHSValue.getLValueBase()) {
1161 if (!E->isEqualityOp())
1162 return false;
1163 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1164 return false;
1165 bool bres;
1166 if (!EvalPointerValueAsBool(RHSValue, bres))
1167 return false;
1168 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1169 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001170
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001171 if (E->getOpcode() == BinaryOperator::Sub) {
1172 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001173 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001174
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001175 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001176 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1177 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001178
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001179 return Success(D, E);
1180 }
1181 bool Result;
1182 if (E->getOpcode() == BinaryOperator::EQ) {
1183 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001184 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001185 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1186 }
1187 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001188 }
1189 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001190 if (!LHSTy->isIntegralType() ||
1191 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001192 // We can't continue from here for non-integral types, and they
1193 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001194 return false;
1195 }
1196
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001197 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001198 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001199 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001200
Eli Friedman42edd0d2009-03-24 01:14:50 +00001201 APValue RHSVal;
1202 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001203 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001204
1205 // Handle cases like (unsigned long)&a + 4.
1206 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1207 uint64_t offset = Result.getLValueOffset();
1208 if (E->getOpcode() == BinaryOperator::Add)
1209 offset += RHSVal.getInt().getZExtValue();
1210 else
1211 offset -= RHSVal.getInt().getZExtValue();
1212 Result = APValue(Result.getLValueBase(), offset);
1213 return true;
1214 }
1215
1216 // Handle cases like 4 + (unsigned long)&a
1217 if (E->getOpcode() == BinaryOperator::Add &&
1218 RHSVal.isLValue() && Result.isInt()) {
1219 uint64_t offset = RHSVal.getLValueOffset();
1220 offset += Result.getInt().getZExtValue();
1221 Result = APValue(RHSVal.getLValueBase(), offset);
1222 return true;
1223 }
1224
1225 // All the following cases expect both operands to be an integer
1226 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001227 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001228
Eli Friedman42edd0d2009-03-24 01:14:50 +00001229 APSInt& RHS = RHSVal.getInt();
1230
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001231 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001232 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001233 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001234 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1235 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1236 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1237 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1238 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1239 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001240 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001241 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001242 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001243 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001244 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001245 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001246 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001247 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001248 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001249 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001250 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001251 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1252 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001253 }
1254 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001255 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001256 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1257 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001260 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1261 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1262 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1263 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1264 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1265 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001266 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001267}
1268
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001269bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001270 bool Cond;
1271 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001272 return false;
1273
Nuno Lopesa25bd552008-11-16 22:06:39 +00001274 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001275}
1276
Chris Lattneraf707ab2009-01-24 21:53:27 +00001277unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001278 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1279 // the result is the size of the referenced type."
1280 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1281 // result shall be the alignment of the referenced type."
1282 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1283 T = Ref->getPointeeType();
1284
Chris Lattnere9feb472009-01-24 21:09:06 +00001285 // Get information about the alignment.
1286 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001287
Eli Friedman2be58612009-05-30 21:09:44 +00001288 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001289 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001290}
1291
Chris Lattneraf707ab2009-01-24 21:53:27 +00001292unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1293 E = E->IgnoreParens();
1294
1295 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001296 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001297 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001298 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001299
Chris Lattneraf707ab2009-01-24 21:53:27 +00001300 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001301 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1302 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001303
Chris Lattnere9feb472009-01-24 21:09:06 +00001304 return GetAlignOfType(E->getType());
1305}
1306
1307
Sebastian Redl05189992008-11-11 17:56:53 +00001308/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1309/// expression's type.
1310bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001311 // Handle alignof separately.
1312 if (!E->isSizeOf()) {
1313 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001314 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001315 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001316 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001317 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001318
Sebastian Redl05189992008-11-11 17:56:53 +00001319 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001320 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1321 // the result is the size of the referenced type."
1322 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1323 // result shall be the alignment of the referenced type."
1324 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1325 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001326
Daniel Dunbar131eb432009-02-19 09:06:44 +00001327 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1328 // extension.
1329 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1330 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001331
Chris Lattnerfcee0012008-07-11 21:24:13 +00001332 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001333 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001334 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001335
Chris Lattnere9feb472009-01-24 21:09:06 +00001336 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001337 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001338 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001339}
1340
Chris Lattnerb542afe2008-07-11 19:10:17 +00001341bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001342 // Special case unary operators that do not need their subexpression
1343 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001344 if (E->isOffsetOfOp()) {
1345 // The AST for offsetof is defined in such a way that we can just
1346 // directly Evaluate it as an l-value.
1347 APValue LV;
1348 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1349 return false;
1350 if (LV.getLValueBase())
1351 return false;
1352 return Success(LV.getLValueOffset(), E);
1353 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001354
1355 if (E->getOpcode() == UnaryOperator::LNot) {
1356 // LNot's operand isn't necessarily an integer, so we handle it specially.
1357 bool bres;
1358 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1359 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001360 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001361 }
1362
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001363 // Only handle integral operations...
1364 if (!E->getSubExpr()->getType()->isIntegralType())
1365 return false;
1366
Chris Lattner87eae5e2008-07-11 22:52:41 +00001367 // Get the operand value into 'Result'.
1368 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001369 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001370
Chris Lattner75a48812008-07-11 22:15:16 +00001371 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001372 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001373 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1374 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001375 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001376 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001377 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1378 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001379 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001380 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001381 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001382 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001383 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001384 if (!Result.isInt()) return false;
1385 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001386 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001387 if (!Result.isInt()) return false;
1388 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001389 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001390}
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Chris Lattner732b2232008-07-12 01:15:53 +00001392/// HandleCast - This is used to evaluate implicit or explicit casts where the
1393/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001394bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001395 Expr *SubExpr = E->getSubExpr();
1396 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001397 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001398
Eli Friedman4efaa272008-11-12 09:44:48 +00001399 if (DestType->isBooleanType()) {
1400 bool BoolResult;
1401 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1402 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001403 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001404 }
1405
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001406 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001407 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001408 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001409 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001410
Eli Friedmanbe265702009-02-20 01:15:07 +00001411 if (!Result.isInt()) {
1412 // Only allow casts of lvalues if they are lossless.
1413 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1414 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001415
Daniel Dunbardd211642009-02-19 22:24:01 +00001416 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001417 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Chris Lattner732b2232008-07-12 01:15:53 +00001420 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001421 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001422 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001423 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001424 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001425
Daniel Dunbardd211642009-02-19 22:24:01 +00001426 if (LV.getLValueBase()) {
1427 // Only allow based lvalue casts if they are lossless.
1428 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1429 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001430
Daniel Dunbardd211642009-02-19 22:24:01 +00001431 Result = LV;
1432 return true;
1433 }
1434
1435 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1436 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001437 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001438
Eli Friedmanbe265702009-02-20 01:15:07 +00001439 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1440 // This handles double-conversion cases, where there's both
1441 // an l-value promotion and an implicit conversion to int.
1442 APValue LV;
1443 if (!EvaluateLValue(SubExpr, LV, Info))
1444 return false;
1445
1446 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1447 return false;
1448
1449 Result = LV;
1450 return true;
1451 }
1452
Eli Friedman1725f682009-04-22 19:23:09 +00001453 if (SrcType->isAnyComplexType()) {
1454 APValue C;
1455 if (!EvaluateComplex(SubExpr, C, Info))
1456 return false;
1457 if (C.isComplexFloat())
1458 return Success(HandleFloatToIntCast(DestType, SrcType,
1459 C.getComplexFloatReal(), Info.Ctx),
1460 E);
1461 else
1462 return Success(HandleIntToIntCast(DestType, SrcType,
1463 C.getComplexIntReal(), Info.Ctx), E);
1464 }
Eli Friedman2217c872009-02-22 11:46:18 +00001465 // FIXME: Handle vectors
1466
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001467 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001468 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001469
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001470 APFloat F(0.0);
1471 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001472 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001474 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001475}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001476
Eli Friedman722c7172009-02-28 03:59:05 +00001477bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1478 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1479 APValue LV;
1480 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1481 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1482 return Success(LV.getComplexIntReal(), E);
1483 }
1484
1485 return Visit(E->getSubExpr());
1486}
1487
Eli Friedman664a1042009-02-27 04:45:43 +00001488bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001489 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1490 APValue LV;
1491 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1492 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1493 return Success(LV.getComplexIntImag(), E);
1494 }
1495
Eli Friedman664a1042009-02-27 04:45:43 +00001496 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1497 Info.EvalResult.HasSideEffects = true;
1498 return Success(0, E);
1499}
1500
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001501//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001502// Float Evaluation
1503//===----------------------------------------------------------------------===//
1504
1505namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001506class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001507 : public StmtVisitor<FloatExprEvaluator, bool> {
1508 EvalInfo &Info;
1509 APFloat &Result;
1510public:
1511 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1512 : Info(info), Result(result) {}
1513
1514 bool VisitStmt(Stmt *S) {
1515 return false;
1516 }
1517
1518 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001519 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001520
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001521 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001522 bool VisitBinaryOperator(const BinaryOperator *E);
1523 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001524 bool VisitCastExpr(CastExpr *E);
1525 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001526 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001527
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001528 bool VisitChooseExpr(const ChooseExpr *E)
1529 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1530 bool VisitUnaryExtension(const UnaryOperator *E)
1531 { return Visit(E->getSubExpr()); }
1532
1533 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001534 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001535};
1536} // end anonymous namespace
1537
1538static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1539 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1540}
1541
Chris Lattner019f4e82008-10-06 05:28:25 +00001542bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001543 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001544 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001545 case Builtin::BI__builtin_huge_val:
1546 case Builtin::BI__builtin_huge_valf:
1547 case Builtin::BI__builtin_huge_vall:
1548 case Builtin::BI__builtin_inf:
1549 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001550 case Builtin::BI__builtin_infl: {
1551 const llvm::fltSemantics &Sem =
1552 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001553 Result = llvm::APFloat::getInf(Sem);
1554 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattner9e621712008-10-06 06:31:58 +00001557 case Builtin::BI__builtin_nan:
1558 case Builtin::BI__builtin_nanf:
1559 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001560 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001561 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001562 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001563 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001564 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001565 const llvm::fltSemantics &Sem =
1566 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer33035802009-12-11 13:26:32 +00001567 unsigned Type = 0;
1568 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump4572bab2009-05-30 03:56:50 +00001569 return false;
Benjamin Kramer33035802009-12-11 13:26:32 +00001570 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner9e621712008-10-06 06:31:58 +00001571 return true;
1572 }
1573 }
1574 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001575
1576 case Builtin::BI__builtin_fabs:
1577 case Builtin::BI__builtin_fabsf:
1578 case Builtin::BI__builtin_fabsl:
1579 if (!EvaluateFloat(E->getArg(0), Result, Info))
1580 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001582 if (Result.isNegative())
1583 Result.changeSign();
1584 return true;
1585
Mike Stump1eb44332009-09-09 15:08:12 +00001586 case Builtin::BI__builtin_copysign:
1587 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001588 case Builtin::BI__builtin_copysignl: {
1589 APFloat RHS(0.);
1590 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1591 !EvaluateFloat(E->getArg(1), RHS, Info))
1592 return false;
1593 Result.copySign(RHS);
1594 return true;
1595 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001596 }
1597}
1598
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001599bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001600 if (E->getOpcode() == UnaryOperator::Deref)
1601 return false;
1602
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001603 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1604 return false;
1605
1606 switch (E->getOpcode()) {
1607 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001608 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001609 return true;
1610 case UnaryOperator::Minus:
1611 Result.changeSign();
1612 return true;
1613 }
1614}
Chris Lattner019f4e82008-10-06 05:28:25 +00001615
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001616bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001617 if (E->getOpcode() == BinaryOperator::Comma) {
1618 if (!EvaluateFloat(E->getRHS(), Result, Info))
1619 return false;
1620
1621 // If we can't evaluate the LHS, it might have side effects;
1622 // conservatively mark it.
1623 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1624 Info.EvalResult.HasSideEffects = true;
1625
1626 return true;
1627 }
1628
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001629 // FIXME: Diagnostics? I really don't understand how the warnings
1630 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001631 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001632 if (!EvaluateFloat(E->getLHS(), Result, Info))
1633 return false;
1634 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1635 return false;
1636
1637 switch (E->getOpcode()) {
1638 default: return false;
1639 case BinaryOperator::Mul:
1640 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1641 return true;
1642 case BinaryOperator::Add:
1643 Result.add(RHS, APFloat::rmNearestTiesToEven);
1644 return true;
1645 case BinaryOperator::Sub:
1646 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1647 return true;
1648 case BinaryOperator::Div:
1649 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1650 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001651 }
1652}
1653
1654bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1655 Result = E->getValue();
1656 return true;
1657}
1658
Eli Friedman4efaa272008-11-12 09:44:48 +00001659bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1660 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Eli Friedman4efaa272008-11-12 09:44:48 +00001662 if (SubExpr->getType()->isIntegralType()) {
1663 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001664 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001665 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001666 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001667 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001668 return true;
1669 }
1670 if (SubExpr->getType()->isRealFloatingType()) {
1671 if (!Visit(SubExpr))
1672 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001673 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1674 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001675 return true;
1676 }
Eli Friedman2217c872009-02-22 11:46:18 +00001677 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001678
1679 return false;
1680}
1681
1682bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1683 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1684 return true;
1685}
1686
Eli Friedman67f85fc2009-12-04 02:12:53 +00001687bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1688 bool Cond;
1689 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1690 return false;
1691
1692 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1693}
1694
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001695//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001696// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001697//===----------------------------------------------------------------------===//
1698
1699namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001700class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001701 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001702 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001704public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001705 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001707 //===--------------------------------------------------------------------===//
1708 // Visitor Methods
1709 //===--------------------------------------------------------------------===//
1710
1711 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001712 return APValue();
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001715 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1716
1717 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001718 Expr* SubExpr = E->getSubExpr();
1719
1720 if (SubExpr->getType()->isRealFloatingType()) {
1721 APFloat Result(0.0);
1722
1723 if (!EvaluateFloat(SubExpr, Result, Info))
1724 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001725
1726 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001727 Result);
1728 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001729 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001730 "Unexpected imaginary literal.");
1731
1732 llvm::APSInt Result;
1733 if (!EvaluateInteger(SubExpr, Result, Info))
1734 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001736 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1737 Zero = 0;
1738 return APValue(Zero, Result);
1739 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001740 }
1741
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001742 APValue VisitCastExpr(CastExpr *E) {
1743 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001744 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001745 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001746
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001747 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001748 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001749
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001750 if (!EvaluateFloat(SubExpr, Result, Info))
1751 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001752
1753 if (EltType->isRealFloatingType()) {
1754 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001755 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001756 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1757 } else {
1758 llvm::APSInt IResult;
1759 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1760 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1761 Zero = 0;
1762 return APValue(IResult, Zero);
1763 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001764 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001765 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001766
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001767 if (!EvaluateInteger(SubExpr, Result, Info))
1768 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001769
Eli Friedman1725f682009-04-22 19:23:09 +00001770 if (EltType->isRealFloatingType()) {
1771 APFloat FResult =
1772 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001773 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001774 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1775 } else {
1776 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1777 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1778 Zero = 0;
1779 return APValue(Result, Zero);
1780 }
John McCall183700f2009-09-21 23:43:11 +00001781 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001782 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001783
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001784 if (!EvaluateComplex(SubExpr, Src, Info))
1785 return APValue();
1786
1787 QualType SrcType = CT->getElementType();
1788
1789 if (Src.isComplexFloat()) {
1790 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001791 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001792 Src.getComplexFloatReal(),
1793 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001794 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001795 Src.getComplexFloatImag(),
1796 Info.Ctx));
1797 } else {
1798 return APValue(HandleFloatToIntCast(EltType, SrcType,
1799 Src.getComplexFloatReal(),
1800 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001801 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001802 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001803 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001804 }
1805 } else {
1806 assert(Src.isComplexInt() && "Invalid evaluate result.");
1807 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001808 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001809 Src.getComplexIntReal(),
1810 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001811 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001812 Src.getComplexIntImag(),
1813 Info.Ctx));
1814 } else {
1815 return APValue(HandleIntToIntCast(EltType, SrcType,
1816 Src.getComplexIntReal(),
1817 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001818 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001819 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001820 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001821 }
1822 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001823 }
1824
1825 // FIXME: Handle more casts.
1826 return APValue();
1827 }
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001829 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001830 APValue VisitChooseExpr(const ChooseExpr *E)
1831 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1832 APValue VisitUnaryExtension(const UnaryOperator *E)
1833 { return Visit(E->getSubExpr()); }
1834 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001835 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001836};
1837} // end anonymous namespace
1838
Mike Stump1eb44332009-09-09 15:08:12 +00001839static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001840 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1841 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001842 (&Result.getComplexFloatReal().getSemantics() ==
1843 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001844 "Invalid complex evaluation.");
1845 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001846}
1847
Mike Stump1eb44332009-09-09 15:08:12 +00001848APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001849 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001851 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001852 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001854 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001855 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001856
Daniel Dunbar3f279872009-01-29 01:32:56 +00001857 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1858 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001859 switch (E->getOpcode()) {
1860 default: return APValue();
1861 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001862 if (Result.isComplexFloat()) {
1863 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1864 APFloat::rmNearestTiesToEven);
1865 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1866 APFloat::rmNearestTiesToEven);
1867 } else {
1868 Result.getComplexIntReal() += RHS.getComplexIntReal();
1869 Result.getComplexIntImag() += RHS.getComplexIntImag();
1870 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001871 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001872 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001873 if (Result.isComplexFloat()) {
1874 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1875 APFloat::rmNearestTiesToEven);
1876 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1877 APFloat::rmNearestTiesToEven);
1878 } else {
1879 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1880 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1881 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001882 break;
1883 case BinaryOperator::Mul:
1884 if (Result.isComplexFloat()) {
1885 APValue LHS = Result;
1886 APFloat &LHS_r = LHS.getComplexFloatReal();
1887 APFloat &LHS_i = LHS.getComplexFloatImag();
1888 APFloat &RHS_r = RHS.getComplexFloatReal();
1889 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Daniel Dunbar3f279872009-01-29 01:32:56 +00001891 APFloat Tmp = LHS_r;
1892 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1893 Result.getComplexFloatReal() = Tmp;
1894 Tmp = LHS_i;
1895 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1896 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1897
1898 Tmp = LHS_r;
1899 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1900 Result.getComplexFloatImag() = Tmp;
1901 Tmp = LHS_i;
1902 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1903 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1904 } else {
1905 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001906 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001907 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1908 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001909 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001910 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1911 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1912 }
1913 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001914 }
1915
1916 return Result;
1917}
1918
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001919//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001920// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001921//===----------------------------------------------------------------------===//
1922
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001923/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001924/// any crazy technique (that has nothing to do with language standards) that
1925/// we want to. If this function returns true, it returns the folded constant
1926/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001927bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1928 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001929
Nate Begeman59b5da62009-01-18 03:20:47 +00001930 if (getType()->isVectorType()) {
1931 if (!EvaluateVector(this, Result.Val, Info))
1932 return false;
1933 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001934 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001935 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001936 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001937 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001938 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001939 } else if (getType()->isRealFloatingType()) {
1940 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001941 if (!EvaluateFloat(this, f, Info))
1942 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001944 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001945 } else if (getType()->isAnyComplexType()) {
1946 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001947 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001948 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001949 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001950
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001951 return true;
1952}
1953
Mike Stump660e6f72009-10-26 23:05:19 +00001954bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1955 EvalInfo Info(Ctx, Result, true);
1956
1957 if (getType()->isVectorType()) {
1958 if (!EvaluateVector(this, Result.Val, Info))
1959 return false;
1960 } else if (getType()->isIntegerType()) {
1961 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1962 return false;
1963 } else if (getType()->hasPointerRepresentation()) {
1964 if (!EvaluatePointer(this, Result.Val, Info))
1965 return false;
1966 } else if (getType()->isRealFloatingType()) {
1967 llvm::APFloat f(0.0);
1968 if (!EvaluateFloat(this, f, Info))
1969 return false;
1970
1971 Result.Val = APValue(f);
1972 } else if (getType()->isAnyComplexType()) {
1973 if (!EvaluateComplex(this, Result.Val, Info))
1974 return false;
1975 } else
1976 return false;
1977
1978 return true;
1979}
1980
Anders Carlsson1b782762009-04-10 04:54:13 +00001981bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1982 EvalInfo Info(Ctx, Result);
1983
1984 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1985}
1986
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001987bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1988 EvalInfo Info(Ctx, Result, true);
1989
1990 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1991}
1992
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001993/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001994/// folded, but discard the result.
1995bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001996 EvalResult Result;
1997 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001998}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001999
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002000bool Expr::HasSideEffects(ASTContext &Ctx) const {
2001 Expr::EvalResult Result;
2002 EvalInfo Info(Ctx, Result);
2003 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2004}
2005
Anders Carlsson51fe9962008-11-22 21:04:56 +00002006APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002007 EvalResult EvalResult;
2008 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002009 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002010 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002011 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002012
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002013 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002014}