blob: a0b2aa993faa746b4aab1d1a001e4c2e45939c54 [file] [log] [blame]
Chris Lattnere13042c2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlsson7a241ba2008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Ken Dyck40775002010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000019#include "clang/AST/ASTDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000020#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000021#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000022#include "llvm/ADT/SmallString.h"
Mike Stump2346cd22009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlsson7a241ba2008-07-03 04:20:39 +000025using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000028
Chris Lattnercdf34e72008-07-11 22:52:41 +000029/// EvalInfo - This is a private struct used by the evaluator to capture
30/// information about a subexpression as it is folded. It retains information
31/// about the AST context, but also maintains information about the folded
32/// expression.
33///
34/// If an expression could be evaluated, it is still possible it is not a C
35/// "integer constant expression" or constant expression. If not, this struct
36/// captures information about how and why not.
37///
38/// One bit of information passed *into* the request for constant folding
39/// indicates whether the subexpression is "evaluated" or not according to C
40/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
41/// evaluate the expression regardless of what the RHS is, but C only allows
42/// certain things in certain situations.
43struct EvalInfo {
44 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000045
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000048
Eli Friedman7d45c482009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattnercdf34e72008-07-11 22:52:41 +000055};
56
57
Eli Friedman9a156e52008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattner6c4d2552009-10-28 23:59:40 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
62 EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +000063static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +000064static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +000065
66//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +000067// Misc utilities
68//===----------------------------------------------------------------------===//
69
Eli Friedman334046a2009-06-14 02:17:33 +000070static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
71 // FIXME: Is this accurate for all kinds of bases? If not, what would
72 // the check look like?
Ken Dyck02990832010-01-15 12:37:54 +000073 Result = Value.getLValueBase() || !Value.getLValueOffset().isZero();
Eli Friedman334046a2009-06-14 02:17:33 +000074 return true;
75}
76
John McCall1be1c632010-01-05 23:42:56 +000077static bool HandleConversionToBool(const Expr* E, bool& Result,
78 EvalInfo &Info) {
Eli Friedman9a156e52008-11-12 09:44:48 +000079 if (E->getType()->isIntegralType()) {
80 APSInt IntResult;
81 if (!EvaluateInteger(E, IntResult, Info))
82 return false;
83 Result = IntResult != 0;
84 return true;
85 } else if (E->getType()->isRealFloatingType()) {
86 APFloat FloatResult(0.0);
87 if (!EvaluateFloat(E, FloatResult, Info))
88 return false;
89 Result = !FloatResult.isZero();
90 return true;
Eli Friedman64004332009-03-23 04:38:34 +000091 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000092 APValue PointerResult;
93 if (!EvaluatePointer(E, PointerResult, Info))
94 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000095 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +000096 } else if (E->getType()->isAnyComplexType()) {
97 APValue ComplexResult;
98 if (!EvaluateComplex(E, ComplexResult, Info))
99 return false;
100 if (ComplexResult.isComplexFloat()) {
101 Result = !ComplexResult.getComplexFloatReal().isZero() ||
102 !ComplexResult.getComplexFloatImag().isZero();
103 } else {
104 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
105 ComplexResult.getComplexIntImag().getBoolValue();
106 }
107 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000108 }
109
110 return false;
111}
112
Mike Stump11289f42009-09-09 15:08:12 +0000113static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000114 APFloat &Value, ASTContext &Ctx) {
115 unsigned DestWidth = Ctx.getIntWidth(DestType);
116 // Determine whether we are converting to unsigned or signed.
117 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000118
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000119 // FIXME: Warning for overflow.
120 uint64_t Space[4];
121 bool ignored;
122 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
123 llvm::APFloat::rmTowardZero, &ignored);
124 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
125}
126
Mike Stump11289f42009-09-09 15:08:12 +0000127static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000128 APFloat &Value, ASTContext &Ctx) {
129 bool ignored;
130 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000131 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000132 APFloat::rmNearestTiesToEven, &ignored);
133 return Result;
134}
135
Mike Stump11289f42009-09-09 15:08:12 +0000136static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000137 APSInt &Value, ASTContext &Ctx) {
138 unsigned DestWidth = Ctx.getIntWidth(DestType);
139 APSInt Result = Value;
140 // Figure out if this is a truncate, extend or noop cast.
141 // If the input is signed, do a sign extend, noop, or truncate.
142 Result.extOrTrunc(DestWidth);
143 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
144 return Result;
145}
146
Mike Stump11289f42009-09-09 15:08:12 +0000147static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000148 APSInt &Value, ASTContext &Ctx) {
149
150 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
151 Result.convertFromAPInt(Value, Value.isSigned(),
152 APFloat::rmNearestTiesToEven);
153 return Result;
154}
155
Mike Stump876387b2009-10-27 22:09:17 +0000156namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000157class HasSideEffect
Mike Stump876387b2009-10-27 22:09:17 +0000158 : public StmtVisitor<HasSideEffect, bool> {
159 EvalInfo &Info;
160public:
161
162 HasSideEffect(EvalInfo &info) : Info(info) {}
163
164 // Unhandled nodes conservatively default to having side effects.
165 bool VisitStmt(Stmt *S) {
166 return true;
167 }
168
169 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
170 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000171 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000172 return true;
173 return false;
174 }
175 // We don't want to evaluate BlockExprs multiple times, as they generate
176 // a ton of code.
177 bool VisitBlockExpr(BlockExpr *E) { return true; }
178 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
179 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
180 { return Visit(E->getInitializer()); }
181 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
182 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
183 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
184 bool VisitStringLiteral(StringLiteral *E) { return false; }
185 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
186 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
187 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stumpfa502902009-10-29 20:48:09 +0000188 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000189 bool VisitChooseExpr(ChooseExpr *E)
190 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
191 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
192 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000193 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000194 bool VisitBinaryOperator(BinaryOperator *E)
195 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000196 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
198 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
200 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000201 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000202 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000203 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000204 }
205 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
206};
207
Mike Stump876387b2009-10-27 22:09:17 +0000208} // end anonymous namespace
209
Eli Friedman9a156e52008-11-12 09:44:48 +0000210//===----------------------------------------------------------------------===//
211// LValue Evaluation
212//===----------------------------------------------------------------------===//
213namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000214class LValueExprEvaluator
Eli Friedman9a156e52008-11-12 09:44:48 +0000215 : public StmtVisitor<LValueExprEvaluator, APValue> {
216 EvalInfo &Info;
217public:
Mike Stump11289f42009-09-09 15:08:12 +0000218
Eli Friedman9a156e52008-11-12 09:44:48 +0000219 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
220
221 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000222 return APValue();
223 }
224
225 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000226 APValue VisitDeclRefExpr(DeclRefExpr *E);
Ken Dyck02990832010-01-15 12:37:54 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
Ken Dyck02990832010-01-15 12:37:54 +0000230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); }
231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000232 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000233 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000234 APValue VisitUnaryExtension(const UnaryOperator *E)
235 { return Visit(E->getSubExpr()); }
236 APValue VisitChooseExpr(const ChooseExpr *E)
237 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000238
239 APValue VisitCastExpr(CastExpr *E) {
240 switch (E->getCastKind()) {
241 default:
242 return APValue();
243
244 case CastExpr::CK_NoOp:
245 return Visit(E->getSubExpr());
246 }
247 }
Eli Friedman449fe542009-03-23 04:56:01 +0000248 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000249};
250} // end anonymous namespace
251
252static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
253 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
254 return Result.isLValue();
255}
256
Mike Stump11289f42009-09-09 15:08:12 +0000257APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000258 if (isa<FunctionDecl>(E->getDecl())) {
Ken Dyck02990832010-01-15 12:37:54 +0000259 return APValue(E);
Eli Friedman751aa72b72009-05-27 06:04:58 +0000260 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000261 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000262 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000263 if (!VD->getType()->isReferenceType())
Ken Dyck02990832010-01-15 12:37:54 +0000264 return APValue(E);
Eli Friedman9ab03192009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Sebastian Redl5ca79842010-02-01 20:16:42 +0000266 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregor0840cc02009-11-01 20:32:48 +0000267 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000268 }
269
270 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000271}
272
Eli Friedman9a156e52008-11-12 09:44:48 +0000273APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000274 if (!Info.AnyLValue && !E->isFileScope())
275 return APValue();
Ken Dyck02990832010-01-15 12:37:54 +0000276 return APValue(E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000277}
278
279APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
280 APValue result;
281 QualType Ty;
282 if (E->isArrow()) {
283 if (!EvaluatePointer(E->getBase(), result, Info))
284 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000285 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000286 } else {
287 result = Visit(E->getBase());
288 if (result.isUninit())
289 return APValue();
290 Ty = E->getBase()->getType();
291 }
292
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000293 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000294 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000295
296 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
297 if (!FD) // FIXME: deal with other kinds of member expressions
298 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000299
300 if (FD->getType()->isReferenceType())
301 return APValue();
302
Eli Friedman9a156e52008-11-12 09:44:48 +0000303 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000304 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000305 for (RecordDecl::field_iterator Field = RD->field_begin(),
306 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000307 Field != FieldEnd; (void)++Field, ++i) {
308 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000309 break;
310 }
311
312 result.setLValue(result.getLValueBase(),
Ken Dyck02990832010-01-15 12:37:54 +0000313 result.getLValueOffset() +
314 CharUnits::fromQuantity(RL.getFieldOffset(i) / 8));
Eli Friedman9a156e52008-11-12 09:44:48 +0000315
316 return result;
317}
318
Mike Stump11289f42009-09-09 15:08:12 +0000319APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000320 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000322 if (!EvaluatePointer(E->getBase(), Result, Info))
323 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000324
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000325 APSInt Index;
326 if (!EvaluateInteger(E->getIdx(), Index, Info))
327 return APValue();
328
Ken Dyck40775002010-01-11 17:06:35 +0000329 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000330
Ken Dyck40775002010-01-11 17:06:35 +0000331 CharUnits Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000332 Result.setLValue(Result.getLValueBase(),
Ken Dyck02990832010-01-15 12:37:54 +0000333 Result.getLValueOffset() + Offset);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000334 return Result;
335}
Eli Friedman9a156e52008-11-12 09:44:48 +0000336
Mike Stump11289f42009-09-09 15:08:12 +0000337APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000338 APValue Result;
339 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
340 return APValue();
341 return Result;
342}
343
Eli Friedman9a156e52008-11-12 09:44:48 +0000344//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000345// Pointer Evaluation
346//===----------------------------------------------------------------------===//
347
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000348namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000349class PointerExprEvaluator
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000350 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000351 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000352public:
Mike Stump11289f42009-09-09 15:08:12 +0000353
Chris Lattnercdf34e72008-07-11 22:52:41 +0000354 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000355
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000356 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000357 return APValue();
358 }
359
360 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
361
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000362 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman847a2bc2009-12-27 05:43:15 +0000363 APValue VisitCastExpr(CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000364 APValue VisitUnaryExtension(const UnaryOperator *E)
365 { return Visit(E->getSubExpr()); }
366 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000367 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
Ken Dyck02990832010-01-15 12:37:54 +0000368 { return APValue(E); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000369 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
Ken Dyck02990832010-01-15 12:37:54 +0000370 { return APValue(E); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000371 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000372 APValue VisitBlockExpr(BlockExpr *E) {
373 if (!E->hasBlockDeclRefExprs())
Ken Dyck02990832010-01-15 12:37:54 +0000374 return APValue(E);
Mike Stumpa6703322009-02-19 22:01:56 +0000375 return APValue();
376 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000377 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
Ken Dyck02990832010-01-15 12:37:54 +0000378 { return APValue((Expr*)0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000379 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000380 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000381 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
382 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
Ken Dyck02990832010-01-15 12:37:54 +0000383 { return APValue((Expr*)0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000384 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000385};
Chris Lattner05706e882008-07-11 18:11:29 +0000386} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000387
Chris Lattnercdf34e72008-07-11 22:52:41 +0000388static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000389 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000390 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000391 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000392 return Result.isLValue();
393}
394
395APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
396 if (E->getOpcode() != BinaryOperator::Add &&
397 E->getOpcode() != BinaryOperator::Sub)
398 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner05706e882008-07-11 18:11:29 +0000400 const Expr *PExp = E->getLHS();
401 const Expr *IExp = E->getRHS();
402 if (IExp->getType()->isPointerType())
403 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner05706e882008-07-11 18:11:29 +0000405 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000406 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000407 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner05706e882008-07-11 18:11:29 +0000409 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000410 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000411 return APValue();
412
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000413 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Ken Dyck40775002010-01-11 17:06:35 +0000414 CharUnits SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Anders Carlssonef56fba2009-02-19 04:55:58 +0000416 // Explicitly handle GNU void* and function pointer arithmetic extensions.
417 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Ken Dyck40775002010-01-11 17:06:35 +0000418 SizeOfPointee = CharUnits::One();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000419 else
Ken Dyck40775002010-01-11 17:06:35 +0000420 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman9a156e52008-11-12 09:44:48 +0000421
Ken Dyck02990832010-01-15 12:37:54 +0000422 CharUnits Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000423
Chris Lattner05706e882008-07-11 18:11:29 +0000424 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000425 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000426 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000427 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
428
Ken Dyck02990832010-01-15 12:37:54 +0000429 return APValue(ResultLValue.getLValueBase(), Offset);
Chris Lattner05706e882008-07-11 18:11:29 +0000430}
Eli Friedman9a156e52008-11-12 09:44:48 +0000431
Eli Friedmanc2b50172009-02-22 11:46:18 +0000432APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
433 APValue result;
434 if (EvaluateLValue(E->getSubExpr(), result, Info))
435 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000436 return APValue();
437}
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chris Lattner05706e882008-07-11 18:11:29 +0000439
Eli Friedman847a2bc2009-12-27 05:43:15 +0000440APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
441 Expr* SubExpr = E->getSubExpr();
Chris Lattner05706e882008-07-11 18:11:29 +0000442
Eli Friedman847a2bc2009-12-27 05:43:15 +0000443 switch (E->getCastKind()) {
444 default:
445 break;
446
447 case CastExpr::CK_Unknown: {
448 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
449
450 // Check for pointer->pointer cast
451 if (SubExpr->getType()->isPointerType() ||
452 SubExpr->getType()->isObjCObjectPointerType() ||
453 SubExpr->getType()->isNullPtrType() ||
454 SubExpr->getType()->isBlockPointerType())
455 return Visit(SubExpr);
456
457 if (SubExpr->getType()->isIntegralType()) {
458 APValue Result;
459 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
460 break;
461
462 if (Result.isInt()) {
463 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dyck02990832010-01-15 12:37:54 +0000464 return APValue(0,
465 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman847a2bc2009-12-27 05:43:15 +0000466 }
467
468 // Cast is of an lvalue, no need to change value.
Chris Lattner05706e882008-07-11 18:11:29 +0000469 return Result;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000470 }
471 break;
Chris Lattner05706e882008-07-11 18:11:29 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Eli Friedman847a2bc2009-12-27 05:43:15 +0000474 case CastExpr::CK_NoOp:
475 case CastExpr::CK_BitCast:
476 case CastExpr::CK_AnyPointerToObjCPointerCast:
477 case CastExpr::CK_AnyPointerToBlockPointerCast:
478 return Visit(SubExpr);
479
480 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbarce399542009-02-20 18:22:23 +0000481 APValue Result;
482 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000483 break;
Daniel Dunbarce399542009-02-20 18:22:23 +0000484
485 if (Result.isInt()) {
486 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dyck02990832010-01-15 12:37:54 +0000487 return APValue(0,
488 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattner05706e882008-07-11 18:11:29 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Daniel Dunbarce399542009-02-20 18:22:23 +0000491 // Cast is of an lvalue, no need to change value.
492 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000493 }
Eli Friedman847a2bc2009-12-27 05:43:15 +0000494 case CastExpr::CK_ArrayToPointerDecay:
495 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman9a156e52008-11-12 09:44:48 +0000496 APValue Result;
497 if (EvaluateLValue(SubExpr, Result, Info))
498 return Result;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000499 break;
500 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000501 }
502
Chris Lattner05706e882008-07-11 18:11:29 +0000503 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000504}
Chris Lattner05706e882008-07-11 18:11:29 +0000505
Eli Friedmanc69d4542009-01-25 01:54:01 +0000506APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000507 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall481e3a82010-01-23 02:40:42 +0000508 Builtin::BI__builtin___CFStringMakeConstantString ||
509 E->isBuiltinCall(Info.Ctx) ==
510 Builtin::BI__builtin___NSStringMakeConstantString)
Ken Dyck02990832010-01-15 12:37:54 +0000511 return APValue(E);
Eli Friedmanc69d4542009-01-25 01:54:01 +0000512 return APValue();
513}
514
Eli Friedman9a156e52008-11-12 09:44:48 +0000515APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
516 bool BoolResult;
517 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
518 return APValue();
519
520 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
521
522 APValue Result;
523 if (EvaluatePointer(EvalExpr, Result, Info))
524 return Result;
525 return APValue();
526}
Chris Lattner05706e882008-07-11 18:11:29 +0000527
528//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000529// Vector Evaluation
530//===----------------------------------------------------------------------===//
531
532namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000533 class VectorExprEvaluator
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000534 : public StmtVisitor<VectorExprEvaluator, APValue> {
535 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000536 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000537 public:
Mike Stump11289f42009-09-09 15:08:12 +0000538
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000539 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000540
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000541 APValue VisitStmt(Stmt *S) {
542 return APValue();
543 }
Mike Stump11289f42009-09-09 15:08:12 +0000544
Eli Friedman3ae59112009-02-23 04:23:56 +0000545 APValue VisitParenExpr(ParenExpr *E)
546 { return Visit(E->getSubExpr()); }
547 APValue VisitUnaryExtension(const UnaryOperator *E)
548 { return Visit(E->getSubExpr()); }
549 APValue VisitUnaryPlus(const UnaryOperator *E)
550 { return Visit(E->getSubExpr()); }
551 APValue VisitUnaryReal(const UnaryOperator *E)
552 { return Visit(E->getSubExpr()); }
553 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
554 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000555 APValue VisitCastExpr(const CastExpr* E);
556 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
557 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000558 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000559 APValue VisitChooseExpr(const ChooseExpr *E)
560 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000561 APValue VisitUnaryImag(const UnaryOperator *E);
562 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000563 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000564 // shufflevector, ExtVectorElementExpr
565 // (Note that these require implementing conversions
566 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000567 };
568} // end anonymous namespace
569
570static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
571 if (!E->getType()->isVectorType())
572 return false;
573 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
574 return !Result.isUninit();
575}
576
577APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000578 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000579 QualType EltTy = VTy->getElementType();
580 unsigned NElts = VTy->getNumElements();
581 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000583 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000584 QualType SETy = SE->getType();
585 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000586
Nate Begeman2ffd3842009-06-26 18:22:18 +0000587 // Check for vector->vector bitcast and scalar->vector splat.
588 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000589 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000590 } else if (SETy->isIntegerType()) {
591 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000592 if (!EvaluateInteger(SE, IntResult, Info))
593 return APValue();
594 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000595 } else if (SETy->isRealFloatingType()) {
596 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000597 if (!EvaluateFloat(SE, F, Info))
598 return APValue();
599 Result = APValue(F);
600 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000601 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000602
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000603 // For casts of a scalar to ExtVector, convert the scalar to the element type
604 // and splat it to all elements.
605 if (E->getType()->isExtVectorType()) {
606 if (EltTy->isIntegerType() && Result.isInt())
607 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
608 Info.Ctx));
609 else if (EltTy->isIntegerType())
610 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
611 Info.Ctx));
612 else if (EltTy->isRealFloatingType() && Result.isInt())
613 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
614 Info.Ctx));
615 else if (EltTy->isRealFloatingType())
616 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
617 Info.Ctx));
618 else
619 return APValue();
620
621 // Splat and create vector APValue.
622 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
623 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000624 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000625
626 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
627 // to the vector. To construct the APValue vector initializer, bitcast the
628 // initializing value to an APInt, and shift out the bits pertaining to each
629 // element.
630 APSInt Init;
631 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000632
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000633 llvm::SmallVector<APValue, 4> Elts;
634 for (unsigned i = 0; i != NElts; ++i) {
635 APSInt Tmp = Init;
636 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000638 if (EltTy->isIntegerType())
639 Elts.push_back(APValue(Tmp));
640 else if (EltTy->isRealFloatingType())
641 Elts.push_back(APValue(APFloat(Tmp)));
642 else
643 return APValue();
644
645 Init >>= EltWidth;
646 }
647 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000648}
649
Mike Stump11289f42009-09-09 15:08:12 +0000650APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000651VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
652 return this->Visit(const_cast<Expr*>(E->getInitializer()));
653}
654
Mike Stump11289f42009-09-09 15:08:12 +0000655APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000656VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000657 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000658 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000659 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000660
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000661 QualType EltTy = VT->getElementType();
662 llvm::SmallVector<APValue, 4> Elements;
663
Eli Friedman3ae59112009-02-23 04:23:56 +0000664 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000665 if (EltTy->isIntegerType()) {
666 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000667 if (i < NumInits) {
668 if (!EvaluateInteger(E->getInit(i), sInt, Info))
669 return APValue();
670 } else {
671 sInt = Info.Ctx.MakeIntValue(0, EltTy);
672 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000673 Elements.push_back(APValue(sInt));
674 } else {
675 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000676 if (i < NumInits) {
677 if (!EvaluateFloat(E->getInit(i), f, Info))
678 return APValue();
679 } else {
680 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
681 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000682 Elements.push_back(APValue(f));
683 }
684 }
685 return APValue(&Elements[0], Elements.size());
686}
687
Mike Stump11289f42009-09-09 15:08:12 +0000688APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000689VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000690 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000691 QualType EltTy = VT->getElementType();
692 APValue ZeroElement;
693 if (EltTy->isIntegerType())
694 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
695 else
696 ZeroElement =
697 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
698
699 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
700 return APValue(&Elements[0], Elements.size());
701}
702
703APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
704 bool BoolResult;
705 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
706 return APValue();
707
708 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
709
710 APValue Result;
711 if (EvaluateVector(EvalExpr, Result, Info))
712 return Result;
713 return APValue();
714}
715
Eli Friedman3ae59112009-02-23 04:23:56 +0000716APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
717 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
718 Info.EvalResult.HasSideEffects = true;
719 return GetZeroVector(E->getType());
720}
721
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000722//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000723// Integer Evaluation
724//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000725
726namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000727class IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000728 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000729 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000730 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000731public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000732 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000733 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000734
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000735 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000736 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000737 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000738 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000739 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000740 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000741 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000742 return true;
743 }
744
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000745 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000746 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000747 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000748 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000749 Result = APValue(APSInt(I));
750 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000751 return true;
752 }
753
754 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000755 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000756 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000757 return true;
758 }
759
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000760 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000761 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000762 if (Info.EvalResult.Diag == 0) {
763 Info.EvalResult.DiagLoc = L;
764 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000765 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000766 }
Chris Lattner99415702008-07-12 00:14:42 +0000767 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000768 }
Mike Stump11289f42009-09-09 15:08:12 +0000769
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000770 //===--------------------------------------------------------------------===//
771 // Visitor Methods
772 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000774 bool VisitStmt(Stmt *) {
775 assert(0 && "This should be called on integers, stmts are not integers");
776 return false;
777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000779 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000780 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnere13042c2008-07-11 19:10:17 +0000783 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000784
Chris Lattner7174bf32008-07-12 00:38:25 +0000785 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000786 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000787 }
788 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000789 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000790 }
791 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000792 // Per gcc docs "this built-in function ignores top level
793 // qualifiers". We need to use the canonical version to properly
794 // be able to strip CRV qualifiers from the type.
795 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
796 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000797 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000798 T1.getUnqualifiedType()),
799 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000800 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000801
802 bool CheckReferencedDecl(const Expr *E, const Decl *D);
803 bool VisitDeclRefExpr(const DeclRefExpr *E) {
804 return CheckReferencedDecl(E, E->getDecl());
805 }
806 bool VisitMemberExpr(const MemberExpr *E) {
807 if (CheckReferencedDecl(E, E->getMemberDecl())) {
808 // Conservatively assume a MemberExpr will have side-effects
809 Info.EvalResult.HasSideEffects = true;
810 return true;
811 }
812 return false;
813 }
814
Chris Lattner7174bf32008-07-12 00:38:25 +0000815 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000816 bool VisitBinaryOperator(const BinaryOperator *E);
817 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000818 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000819
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000820 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000821 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
822
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000823 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000824 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Anders Carlsson39def3a2008-12-21 22:39:40 +0000827 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000828 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000831 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000832 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000833 }
834
Eli Friedman4e7a2412009-02-27 04:45:43 +0000835 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
836 return Success(0, E);
837 }
838
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000839 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000840 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000841 }
842
Eli Friedman449fe542009-03-23 04:56:01 +0000843 bool VisitChooseExpr(const ChooseExpr *E) {
844 return Visit(E->getChosenSubExpr(Info.Ctx));
845 }
846
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000847 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000848 bool VisitUnaryImag(const UnaryOperator *E);
849
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000850private:
Ken Dyck160146e2010-01-27 17:10:57 +0000851 CharUnits GetAlignOfExpr(const Expr *E);
852 CharUnits GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000853 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000854};
Chris Lattner05706e882008-07-11 18:11:29 +0000855} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000856
Daniel Dunbarce399542009-02-20 18:22:23 +0000857static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000858 if (!E->getType()->isIntegralType())
859 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Daniel Dunbarce399542009-02-20 18:22:23 +0000861 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
862}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000863
Daniel Dunbarce399542009-02-20 18:22:23 +0000864static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
865 APValue Val;
866 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
867 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000868 Result = Val.getInt();
869 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000870}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000871
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000872bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000873 // Enums are integer constant exprs.
Eli Friedmanee275c82009-12-10 22:29:29 +0000874 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
875 return Success(ECD->getInitVal(), E);
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000876
877 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000878 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000879 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
880 == Qualifiers::Const) {
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000881 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000882 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000883 if (APValue *V = VD->getEvaluatedValue()) {
884 if (V->isInt())
885 return Success(V->getInt(), E);
886 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
887 }
888
889 if (VD->isEvaluatingValue())
890 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
891
892 VD->setEvaluatingValue();
893
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000894 if (Visit(const_cast<Expr*>(Init))) {
895 // Cache the evaluated value in the variable declaration.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000896 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000897 return true;
898 }
899
Eli Friedman1d6fb162009-12-03 20:31:57 +0000900 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000901 return false;
902 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000903 }
904 }
905
Chris Lattner7174bf32008-07-12 00:38:25 +0000906 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000907 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000908}
909
Chris Lattner86ee2862008-10-06 06:40:35 +0000910/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
911/// as GCC.
912static int EvaluateBuiltinClassifyType(const CallExpr *E) {
913 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000914 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000915 enum gcc_type_class {
916 no_type_class = -1,
917 void_type_class, integer_type_class, char_type_class,
918 enumeral_type_class, boolean_type_class,
919 pointer_type_class, reference_type_class, offset_type_class,
920 real_type_class, complex_type_class,
921 function_type_class, method_type_class,
922 record_type_class, union_type_class,
923 array_type_class, string_type_class,
924 lang_type_class
925 };
Mike Stump11289f42009-09-09 15:08:12 +0000926
927 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000928 // ideal, however it is what gcc does.
929 if (E->getNumArgs() == 0)
930 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000931
Chris Lattner86ee2862008-10-06 06:40:35 +0000932 QualType ArgTy = E->getArg(0)->getType();
933 if (ArgTy->isVoidType())
934 return void_type_class;
935 else if (ArgTy->isEnumeralType())
936 return enumeral_type_class;
937 else if (ArgTy->isBooleanType())
938 return boolean_type_class;
939 else if (ArgTy->isCharType())
940 return string_type_class; // gcc doesn't appear to use char_type_class
941 else if (ArgTy->isIntegerType())
942 return integer_type_class;
943 else if (ArgTy->isPointerType())
944 return pointer_type_class;
945 else if (ArgTy->isReferenceType())
946 return reference_type_class;
947 else if (ArgTy->isRealType())
948 return real_type_class;
949 else if (ArgTy->isComplexType())
950 return complex_type_class;
951 else if (ArgTy->isFunctionType())
952 return function_type_class;
953 else if (ArgTy->isStructureType())
954 return record_type_class;
955 else if (ArgTy->isUnionType())
956 return union_type_class;
957 else if (ArgTy->isArrayType())
958 return array_type_class;
959 else if (ArgTy->isUnionType())
960 return union_type_class;
961 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
962 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
963 return -1;
964}
965
Chris Lattner7174bf32008-07-12 00:38:25 +0000966bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000967 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000968 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000969 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000970
971 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000972 const Expr *Arg = E->getArg(0)->IgnoreParens();
973 Expr::EvalResult Base;
Eric Christopher99469702010-01-19 22:58:35 +0000974
975 // TODO: Perhaps we should let LLVM lower this?
Mike Stump5183a142009-10-26 23:05:19 +0000976 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000977 && Base.Val.getKind() == APValue::LValue
978 && !Base.HasSideEffects)
979 if (const Expr *LVBase = Base.Val.getLValueBase())
980 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
981 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000982 if (!VD->getType()->isIncompleteType()
983 && VD->getType()->isObjectType()
984 && !VD->getType()->isVariablyModifiedType()
985 && !VD->getType()->isDependentType()) {
Ken Dyck40775002010-01-11 17:06:35 +0000986 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dyck02990832010-01-15 12:37:54 +0000987 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck40775002010-01-11 17:06:35 +0000988 if (!Offset.isNegative() && Offset <= Size)
989 Size -= Offset;
Mike Stump5179f302009-10-28 21:22:24 +0000990 else
Ken Dyck40775002010-01-11 17:06:35 +0000991 Size = CharUnits::Zero();
992 return Success(Size.getQuantity(), E);
Mike Stump5179f302009-10-28 21:22:24 +0000993 }
Mike Stump722cedf2009-10-26 18:35:08 +0000994 }
995 }
996
Eric Christopher99469702010-01-19 22:58:35 +0000997 // If evaluating the argument has side-effects we can't determine
998 // the size of the object and lower it to unknown now.
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +0000999 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer0128f662010-01-03 18:18:37 +00001000 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattner4f105592009-11-03 19:48:51 +00001001 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +00001002 return Success(0, E);
1003 }
Mike Stump876387b2009-10-27 22:09:17 +00001004
Mike Stump722cedf2009-10-26 18:35:08 +00001005 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1006 }
1007
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001008 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001009 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +00001010
Anders Carlsson4c76e932008-11-24 04:21:33 +00001011 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001012 // __builtin_constant_p always has one operand: it returns true if that
1013 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001014 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +00001015
1016 case Builtin::BI__builtin_eh_return_data_regno: {
1017 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1018 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1019 return Success(Operand, E);
1020 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001021 }
Chris Lattner7174bf32008-07-12 00:38:25 +00001022}
Anders Carlsson4a3585b2008-07-08 15:34:11 +00001023
Chris Lattnere13042c2008-07-11 19:10:17 +00001024bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001025 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001026 if (!Visit(E->getRHS()))
1027 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001028
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001029 // If we can't evaluate the LHS, it might have side effects;
1030 // conservatively mark it.
1031 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1032 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001033
Anders Carlsson564730a2008-12-01 02:07:06 +00001034 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001035 }
1036
1037 if (E->isLogicalOp()) {
1038 // These need to be handled specially because the operands aren't
1039 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001040 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001041
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001042 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001043 // We were able to evaluate the LHS, see if we can get away with not
1044 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001045 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001046 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001047
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001048 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001049 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001050 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001051 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001052 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001053 }
1054 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001055 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001056 // We can't evaluate the LHS; however, sometimes the result
1057 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001058 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001059 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001060 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001061 // must have had side effects.
1062 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001063
1064 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001065 }
1066 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001067 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001068
Eli Friedman5a332ea2008-11-13 06:09:17 +00001069 return false;
1070 }
1071
Anders Carlssonacc79812008-11-16 07:17:21 +00001072 QualType LHSTy = E->getLHS()->getType();
1073 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001074
1075 if (LHSTy->isAnyComplexType()) {
1076 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1077 APValue LHS, RHS;
1078
1079 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1080 return false;
1081
1082 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1083 return false;
1084
1085 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001086 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001087 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001088 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001089 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1090
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001091 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001092 return Success((CR_r == APFloat::cmpEqual &&
1093 CR_i == APFloat::cmpEqual), E);
1094 else {
1095 assert(E->getOpcode() == BinaryOperator::NE &&
1096 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001097 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001098 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001099 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001100 CR_i == APFloat::cmpLessThan)), E);
1101 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001102 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001103 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001104 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1105 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1106 else {
1107 assert(E->getOpcode() == BinaryOperator::NE &&
1108 "Invalid compex comparison.");
1109 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1110 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1111 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001112 }
1113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Anders Carlssonacc79812008-11-16 07:17:21 +00001115 if (LHSTy->isRealFloatingType() &&
1116 RHSTy->isRealFloatingType()) {
1117 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001118
Anders Carlssonacc79812008-11-16 07:17:21 +00001119 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1120 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001121
Anders Carlssonacc79812008-11-16 07:17:21 +00001122 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1123 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001124
Anders Carlssonacc79812008-11-16 07:17:21 +00001125 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001126
Anders Carlssonacc79812008-11-16 07:17:21 +00001127 switch (E->getOpcode()) {
1128 default:
1129 assert(0 && "Invalid binary operator!");
1130 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001131 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001132 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001133 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001134 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001135 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001136 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001137 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001138 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001139 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001140 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001141 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001142 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001143 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001144 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Eli Friedmana38da572009-04-28 19:17:36 +00001147 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1148 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001149 APValue LHSValue;
1150 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1151 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001152
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001153 APValue RHSValue;
1154 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1155 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001156
Eli Friedman334046a2009-06-14 02:17:33 +00001157 // Reject any bases from the normal codepath; we special-case comparisons
1158 // to null.
1159 if (LHSValue.getLValueBase()) {
1160 if (!E->isEqualityOp())
1161 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001162 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001163 return false;
1164 bool bres;
1165 if (!EvalPointerValueAsBool(LHSValue, bres))
1166 return false;
1167 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1168 } else if (RHSValue.getLValueBase()) {
1169 if (!E->isEqualityOp())
1170 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001171 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman334046a2009-06-14 02:17:33 +00001172 return false;
1173 bool bres;
1174 if (!EvalPointerValueAsBool(RHSValue, bres))
1175 return false;
1176 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1177 }
Eli Friedman64004332009-03-23 04:38:34 +00001178
Eli Friedmana38da572009-04-28 19:17:36 +00001179 if (E->getOpcode() == BinaryOperator::Sub) {
1180 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001181 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001182
Ken Dyck02990832010-01-15 12:37:54 +00001183 CharUnits ElementSize = CharUnits::One();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001184 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dyck02990832010-01-15 12:37:54 +00001185 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedman64004332009-03-23 04:38:34 +00001186
Ken Dyck02990832010-01-15 12:37:54 +00001187 CharUnits Diff = LHSValue.getLValueOffset() -
1188 RHSValue.getLValueOffset();
1189 return Success(Diff / ElementSize, E);
Eli Friedmana38da572009-04-28 19:17:36 +00001190 }
1191 bool Result;
1192 if (E->getOpcode() == BinaryOperator::EQ) {
1193 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001194 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001195 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1196 }
1197 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001198 }
1199 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001200 if (!LHSTy->isIntegralType() ||
1201 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001202 // We can't continue from here for non-integral types, and they
1203 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001204 return false;
1205 }
1206
Anders Carlsson9c181652008-07-08 14:35:21 +00001207 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001208 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001209 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001210
Eli Friedman94c25c62009-03-24 01:14:50 +00001211 APValue RHSVal;
1212 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001213 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001214
1215 // Handle cases like (unsigned long)&a + 4.
1216 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001217 CharUnits Offset = Result.getLValueOffset();
1218 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1219 RHSVal.getInt().getZExtValue());
Eli Friedman94c25c62009-03-24 01:14:50 +00001220 if (E->getOpcode() == BinaryOperator::Add)
Ken Dyck02990832010-01-15 12:37:54 +00001221 Offset += AdditionalOffset;
Eli Friedman94c25c62009-03-24 01:14:50 +00001222 else
Ken Dyck02990832010-01-15 12:37:54 +00001223 Offset -= AdditionalOffset;
1224 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001225 return true;
1226 }
1227
1228 // Handle cases like 4 + (unsigned long)&a
1229 if (E->getOpcode() == BinaryOperator::Add &&
1230 RHSVal.isLValue() && Result.isInt()) {
Ken Dyck02990832010-01-15 12:37:54 +00001231 CharUnits Offset = RHSVal.getLValueOffset();
1232 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1233 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman94c25c62009-03-24 01:14:50 +00001234 return true;
1235 }
1236
1237 // All the following cases expect both operands to be an integer
1238 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001239 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001240
Eli Friedman94c25c62009-03-24 01:14:50 +00001241 APSInt& RHS = RHSVal.getInt();
1242
Anders Carlsson9c181652008-07-08 14:35:21 +00001243 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001244 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001245 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001246 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1247 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1248 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1249 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1250 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1251 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001252 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001253 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001254 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001255 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001256 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001257 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001258 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001259 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001260 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001261 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001262 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001263 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1264 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001265 }
1266 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001267 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001268 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1269 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001270 }
Mike Stump11289f42009-09-09 15:08:12 +00001271
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001272 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1273 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1274 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1275 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1276 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1277 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001278 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001279}
1280
Nuno Lopes42042612008-11-16 19:28:31 +00001281bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001282 bool Cond;
1283 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001284 return false;
1285
Nuno Lopes527b5a62008-11-16 22:06:39 +00001286 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001287}
1288
Ken Dyck160146e2010-01-27 17:10:57 +00001289CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001290 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1291 // the result is the size of the referenced type."
1292 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1293 // result shall be the alignment of the referenced type."
1294 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1295 T = Ref->getPointeeType();
1296
Chris Lattner24aeeab2009-01-24 21:09:06 +00001297 // Get information about the alignment.
1298 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001299
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001300 // __alignof is defined to return the preferred alignment.
Ken Dyck160146e2010-01-27 17:10:57 +00001301 return CharUnits::fromQuantity(
1302 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001303}
1304
Ken Dyck160146e2010-01-27 17:10:57 +00001305CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattner68061312009-01-24 21:53:27 +00001306 E = E->IgnoreParens();
1307
1308 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001309 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001310 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001311 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1312 /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001313
Chris Lattner68061312009-01-24 21:53:27 +00001314 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck160146e2010-01-27 17:10:57 +00001315 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1316 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001317
Chris Lattner24aeeab2009-01-24 21:09:06 +00001318 return GetAlignOfType(E->getType());
1319}
1320
1321
Sebastian Redl6f282892008-11-11 17:56:53 +00001322/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1323/// expression's type.
1324bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001325 // Handle alignof separately.
1326 if (!E->isSizeOf()) {
1327 if (E->isArgumentType())
Ken Dyck160146e2010-01-27 17:10:57 +00001328 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001329 else
Ken Dyck160146e2010-01-27 17:10:57 +00001330 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001331 }
Eli Friedman64004332009-03-23 04:38:34 +00001332
Sebastian Redl6f282892008-11-11 17:56:53 +00001333 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001334 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1335 // the result is the size of the referenced type."
1336 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1337 // result shall be the alignment of the referenced type."
1338 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1339 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001340
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001341 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1342 // extension.
1343 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1344 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001345
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001346 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001347 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001348 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001349
Chris Lattner24aeeab2009-01-24 21:09:06 +00001350 // Get information about the size.
Ken Dyck40775002010-01-11 17:06:35 +00001351 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001352}
1353
Chris Lattnere13042c2008-07-11 19:10:17 +00001354bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001355 // Special case unary operators that do not need their subexpression
1356 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001357 if (E->isOffsetOfOp()) {
1358 // The AST for offsetof is defined in such a way that we can just
1359 // directly Evaluate it as an l-value.
1360 APValue LV;
1361 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1362 return false;
1363 if (LV.getLValueBase())
1364 return false;
Ken Dyck02990832010-01-15 12:37:54 +00001365 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman988a16b2009-02-27 06:44:11 +00001366 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001367
1368 if (E->getOpcode() == UnaryOperator::LNot) {
1369 // LNot's operand isn't necessarily an integer, so we handle it specially.
1370 bool bres;
1371 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1372 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001373 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001374 }
1375
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001376 // Only handle integral operations...
1377 if (!E->getSubExpr()->getType()->isIntegralType())
1378 return false;
1379
Chris Lattnercdf34e72008-07-11 22:52:41 +00001380 // Get the operand value into 'Result'.
1381 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001382 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001383
Chris Lattnerf09ad162008-07-11 22:15:16 +00001384 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001385 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001386 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1387 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001388 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001389 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001390 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1391 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001392 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001393 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001394 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001395 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001396 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001397 if (!Result.isInt()) return false;
1398 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001399 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001400 if (!Result.isInt()) return false;
1401 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001402 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001403}
Mike Stump11289f42009-09-09 15:08:12 +00001404
Chris Lattner477c4be2008-07-12 01:15:53 +00001405/// HandleCast - This is used to evaluate implicit or explicit casts where the
1406/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001407bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001408 Expr *SubExpr = E->getSubExpr();
1409 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001410 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001411
Eli Friedman9a156e52008-11-12 09:44:48 +00001412 if (DestType->isBooleanType()) {
1413 bool BoolResult;
1414 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1415 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001416 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001417 }
1418
Anders Carlsson9c181652008-07-08 14:35:21 +00001419 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001420 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001421 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001422 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001423
Eli Friedman742421e2009-02-20 01:15:07 +00001424 if (!Result.isInt()) {
1425 // Only allow casts of lvalues if they are lossless.
1426 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1427 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001428
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001429 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001430 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Chris Lattner477c4be2008-07-12 01:15:53 +00001433 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001434 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001435 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001436 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001437 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001438
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001439 if (LV.getLValueBase()) {
1440 // Only allow based lvalue casts if they are lossless.
1441 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1442 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001443
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001444 Result = LV;
1445 return true;
1446 }
1447
Ken Dyck02990832010-01-15 12:37:54 +00001448 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1449 SrcType);
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001450 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001451 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001452
Eli Friedman742421e2009-02-20 01:15:07 +00001453 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1454 // This handles double-conversion cases, where there's both
1455 // an l-value promotion and an implicit conversion to int.
1456 APValue LV;
1457 if (!EvaluateLValue(SubExpr, LV, Info))
1458 return false;
1459
1460 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1461 return false;
1462
1463 Result = LV;
1464 return true;
1465 }
1466
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001467 if (SrcType->isAnyComplexType()) {
1468 APValue C;
1469 if (!EvaluateComplex(SubExpr, C, Info))
1470 return false;
1471 if (C.isComplexFloat())
1472 return Success(HandleFloatToIntCast(DestType, SrcType,
1473 C.getComplexFloatReal(), Info.Ctx),
1474 E);
1475 else
1476 return Success(HandleIntToIntCast(DestType, SrcType,
1477 C.getComplexIntReal(), Info.Ctx), E);
1478 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001479 // FIXME: Handle vectors
1480
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001481 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001482 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001483
Eli Friedman24c01542008-08-22 00:06:13 +00001484 APFloat F(0.0);
1485 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001486 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001487
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001488 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001489}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001490
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001491bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1492 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1493 APValue LV;
1494 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1495 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1496 return Success(LV.getComplexIntReal(), E);
1497 }
1498
1499 return Visit(E->getSubExpr());
1500}
1501
Eli Friedman4e7a2412009-02-27 04:45:43 +00001502bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001503 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1504 APValue LV;
1505 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1506 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1507 return Success(LV.getComplexIntImag(), E);
1508 }
1509
Eli Friedman4e7a2412009-02-27 04:45:43 +00001510 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1511 Info.EvalResult.HasSideEffects = true;
1512 return Success(0, E);
1513}
1514
Chris Lattner05706e882008-07-11 18:11:29 +00001515//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001516// Float Evaluation
1517//===----------------------------------------------------------------------===//
1518
1519namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001520class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001521 : public StmtVisitor<FloatExprEvaluator, bool> {
1522 EvalInfo &Info;
1523 APFloat &Result;
1524public:
1525 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1526 : Info(info), Result(result) {}
1527
1528 bool VisitStmt(Stmt *S) {
1529 return false;
1530 }
1531
1532 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001533 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001534
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001535 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001536 bool VisitBinaryOperator(const BinaryOperator *E);
1537 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001538 bool VisitCastExpr(CastExpr *E);
1539 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanf3da3342009-12-04 02:12:53 +00001540 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001541
Eli Friedman449fe542009-03-23 04:56:01 +00001542 bool VisitChooseExpr(const ChooseExpr *E)
1543 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1544 bool VisitUnaryExtension(const UnaryOperator *E)
1545 { return Visit(E->getSubExpr()); }
1546
1547 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedmanf3da3342009-12-04 02:12:53 +00001548 // member of vector, ImplicitValueInitExpr
Eli Friedman24c01542008-08-22 00:06:13 +00001549};
1550} // end anonymous namespace
1551
1552static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1553 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1554}
1555
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001556bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001557 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001558 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001559 case Builtin::BI__builtin_huge_val:
1560 case Builtin::BI__builtin_huge_valf:
1561 case Builtin::BI__builtin_huge_vall:
1562 case Builtin::BI__builtin_inf:
1563 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001564 case Builtin::BI__builtin_infl: {
1565 const llvm::fltSemantics &Sem =
1566 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001567 Result = llvm::APFloat::getInf(Sem);
1568 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001569 }
Mike Stump11289f42009-09-09 15:08:12 +00001570
Chris Lattner0b7282e2008-10-06 06:31:58 +00001571 case Builtin::BI__builtin_nan:
1572 case Builtin::BI__builtin_nanf:
1573 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001574 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001575 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001576 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001577 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001578 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001579 const llvm::fltSemantics &Sem =
1580 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001581 unsigned Type = 0;
1582 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump2346cd22009-05-30 03:56:50 +00001583 return false;
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001584 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001585 return true;
1586 }
1587 }
1588 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001589
1590 case Builtin::BI__builtin_fabs:
1591 case Builtin::BI__builtin_fabsf:
1592 case Builtin::BI__builtin_fabsl:
1593 if (!EvaluateFloat(E->getArg(0), Result, Info))
1594 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001596 if (Result.isNegative())
1597 Result.changeSign();
1598 return true;
1599
Mike Stump11289f42009-09-09 15:08:12 +00001600 case Builtin::BI__builtin_copysign:
1601 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001602 case Builtin::BI__builtin_copysignl: {
1603 APFloat RHS(0.);
1604 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1605 !EvaluateFloat(E->getArg(1), RHS, Info))
1606 return false;
1607 Result.copySign(RHS);
1608 return true;
1609 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001610 }
1611}
1612
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001613bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001614 if (E->getOpcode() == UnaryOperator::Deref)
1615 return false;
1616
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001617 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1618 return false;
1619
1620 switch (E->getOpcode()) {
1621 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001622 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001623 return true;
1624 case UnaryOperator::Minus:
1625 Result.changeSign();
1626 return true;
1627 }
1628}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001629
Eli Friedman24c01542008-08-22 00:06:13 +00001630bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001631 if (E->getOpcode() == BinaryOperator::Comma) {
1632 if (!EvaluateFloat(E->getRHS(), Result, Info))
1633 return false;
1634
1635 // If we can't evaluate the LHS, it might have side effects;
1636 // conservatively mark it.
1637 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1638 Info.EvalResult.HasSideEffects = true;
1639
1640 return true;
1641 }
1642
Eli Friedman24c01542008-08-22 00:06:13 +00001643 // FIXME: Diagnostics? I really don't understand how the warnings
1644 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001645 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001646 if (!EvaluateFloat(E->getLHS(), Result, Info))
1647 return false;
1648 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1649 return false;
1650
1651 switch (E->getOpcode()) {
1652 default: return false;
1653 case BinaryOperator::Mul:
1654 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1655 return true;
1656 case BinaryOperator::Add:
1657 Result.add(RHS, APFloat::rmNearestTiesToEven);
1658 return true;
1659 case BinaryOperator::Sub:
1660 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1661 return true;
1662 case BinaryOperator::Div:
1663 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1664 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001665 }
1666}
1667
1668bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1669 Result = E->getValue();
1670 return true;
1671}
1672
Eli Friedman9a156e52008-11-12 09:44:48 +00001673bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1674 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001675
Eli Friedman9a156e52008-11-12 09:44:48 +00001676 if (SubExpr->getType()->isIntegralType()) {
1677 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001678 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001679 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001680 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001681 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001682 return true;
1683 }
1684 if (SubExpr->getType()->isRealFloatingType()) {
1685 if (!Visit(SubExpr))
1686 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001687 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1688 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001689 return true;
1690 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001691 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001692
1693 return false;
1694}
1695
1696bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1697 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1698 return true;
1699}
1700
Eli Friedmanf3da3342009-12-04 02:12:53 +00001701bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1702 bool Cond;
1703 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1704 return false;
1705
1706 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1707}
1708
Eli Friedman24c01542008-08-22 00:06:13 +00001709//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001710// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001711//===----------------------------------------------------------------------===//
1712
1713namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001714class ComplexExprEvaluator
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001715 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001716 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001717
Anders Carlsson537969c2008-11-16 20:27:53 +00001718public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001719 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001720
Anders Carlsson537969c2008-11-16 20:27:53 +00001721 //===--------------------------------------------------------------------===//
1722 // Visitor Methods
1723 //===--------------------------------------------------------------------===//
1724
1725 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001726 return APValue();
1727 }
Mike Stump11289f42009-09-09 15:08:12 +00001728
Anders Carlsson537969c2008-11-16 20:27:53 +00001729 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1730
1731 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001732 Expr* SubExpr = E->getSubExpr();
1733
1734 if (SubExpr->getType()->isRealFloatingType()) {
1735 APFloat Result(0.0);
1736
1737 if (!EvaluateFloat(SubExpr, Result, Info))
1738 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001739
1740 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001741 Result);
1742 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001743 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001744 "Unexpected imaginary literal.");
1745
1746 llvm::APSInt Result;
1747 if (!EvaluateInteger(SubExpr, Result, Info))
1748 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001749
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001750 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1751 Zero = 0;
1752 return APValue(Zero, Result);
1753 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001754 }
1755
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001756 APValue VisitCastExpr(CastExpr *E) {
1757 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001758 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001759 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001760
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001761 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001762 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001763
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001764 if (!EvaluateFloat(SubExpr, Result, Info))
1765 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001766
1767 if (EltType->isRealFloatingType()) {
1768 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001769 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001770 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1771 } else {
1772 llvm::APSInt IResult;
1773 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1774 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1775 Zero = 0;
1776 return APValue(IResult, Zero);
1777 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001778 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001779 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001780
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001781 if (!EvaluateInteger(SubExpr, Result, Info))
1782 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001783
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001784 if (EltType->isRealFloatingType()) {
1785 APFloat FResult =
1786 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001787 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001788 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1789 } else {
1790 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1791 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1792 Zero = 0;
1793 return APValue(Result, Zero);
1794 }
John McCall9dd450b2009-09-21 23:43:11 +00001795 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001796 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001797
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001798 if (!EvaluateComplex(SubExpr, Src, Info))
1799 return APValue();
1800
1801 QualType SrcType = CT->getElementType();
1802
1803 if (Src.isComplexFloat()) {
1804 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001805 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001806 Src.getComplexFloatReal(),
1807 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001808 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001809 Src.getComplexFloatImag(),
1810 Info.Ctx));
1811 } else {
1812 return APValue(HandleFloatToIntCast(EltType, SrcType,
1813 Src.getComplexFloatReal(),
1814 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001815 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001816 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001817 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001818 }
1819 } else {
1820 assert(Src.isComplexInt() && "Invalid evaluate result.");
1821 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001822 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001823 Src.getComplexIntReal(),
1824 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001825 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001826 Src.getComplexIntImag(),
1827 Info.Ctx));
1828 } else {
1829 return APValue(HandleIntToIntCast(EltType, SrcType,
1830 Src.getComplexIntReal(),
1831 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001832 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001833 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001834 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001835 }
1836 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001837 }
1838
1839 // FIXME: Handle more casts.
1840 return APValue();
1841 }
Mike Stump11289f42009-09-09 15:08:12 +00001842
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001843 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001844 APValue VisitChooseExpr(const ChooseExpr *E)
1845 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1846 APValue VisitUnaryExtension(const UnaryOperator *E)
1847 { return Visit(E->getSubExpr()); }
1848 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001849 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001850};
1851} // end anonymous namespace
1852
Mike Stump11289f42009-09-09 15:08:12 +00001853static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001854 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1855 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001856 (&Result.getComplexFloatReal().getSemantics() ==
1857 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001858 "Invalid complex evaluation.");
1859 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001860}
1861
Mike Stump11289f42009-09-09 15:08:12 +00001862APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001863 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001864
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001865 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001866 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001867
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001868 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001869 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001870
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001871 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1872 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001873 switch (E->getOpcode()) {
1874 default: return APValue();
1875 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001876 if (Result.isComplexFloat()) {
1877 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1878 APFloat::rmNearestTiesToEven);
1879 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1880 APFloat::rmNearestTiesToEven);
1881 } else {
1882 Result.getComplexIntReal() += RHS.getComplexIntReal();
1883 Result.getComplexIntImag() += RHS.getComplexIntImag();
1884 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001885 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001886 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001887 if (Result.isComplexFloat()) {
1888 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1889 APFloat::rmNearestTiesToEven);
1890 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1891 APFloat::rmNearestTiesToEven);
1892 } else {
1893 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1894 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1895 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001896 break;
1897 case BinaryOperator::Mul:
1898 if (Result.isComplexFloat()) {
1899 APValue LHS = Result;
1900 APFloat &LHS_r = LHS.getComplexFloatReal();
1901 APFloat &LHS_i = LHS.getComplexFloatImag();
1902 APFloat &RHS_r = RHS.getComplexFloatReal();
1903 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001904
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001905 APFloat Tmp = LHS_r;
1906 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1907 Result.getComplexFloatReal() = Tmp;
1908 Tmp = LHS_i;
1909 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1910 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1911
1912 Tmp = LHS_r;
1913 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1914 Result.getComplexFloatImag() = Tmp;
1915 Tmp = LHS_i;
1916 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1917 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1918 } else {
1919 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001920 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001921 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1922 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001923 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001924 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1925 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1926 }
1927 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001928 }
1929
1930 return Result;
1931}
1932
Anders Carlsson537969c2008-11-16 20:27:53 +00001933//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001934// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001935//===----------------------------------------------------------------------===//
1936
Chris Lattner67d7b922008-11-16 21:24:15 +00001937/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001938/// any crazy technique (that has nothing to do with language standards) that
1939/// we want to. If this function returns true, it returns the folded constant
1940/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001941bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1942 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001943
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001944 if (getType()->isVectorType()) {
1945 if (!EvaluateVector(this, Result.Val, Info))
1946 return false;
1947 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001948 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001949 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001950 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001951 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001952 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001953 } else if (getType()->isRealFloatingType()) {
1954 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001955 if (!EvaluateFloat(this, f, Info))
1956 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001958 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001959 } else if (getType()->isAnyComplexType()) {
1960 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001961 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001962 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001963 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001964
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001965 return true;
1966}
1967
Mike Stump5183a142009-10-26 23:05:19 +00001968bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1969 EvalInfo Info(Ctx, Result, true);
1970
1971 if (getType()->isVectorType()) {
1972 if (!EvaluateVector(this, Result.Val, Info))
1973 return false;
1974 } else if (getType()->isIntegerType()) {
1975 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1976 return false;
1977 } else if (getType()->hasPointerRepresentation()) {
1978 if (!EvaluatePointer(this, Result.Val, Info))
1979 return false;
1980 } else if (getType()->isRealFloatingType()) {
1981 llvm::APFloat f(0.0);
1982 if (!EvaluateFloat(this, f, Info))
1983 return false;
1984
1985 Result.Val = APValue(f);
1986 } else if (getType()->isAnyComplexType()) {
1987 if (!EvaluateComplex(this, Result.Val, Info))
1988 return false;
1989 } else
1990 return false;
1991
1992 return true;
1993}
1994
John McCall1be1c632010-01-05 23:42:56 +00001995bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
1996 EvalResult Scratch;
1997 EvalInfo Info(Ctx, Scratch);
1998
1999 return HandleConversionToBool(this, Result, Info);
2000}
2001
Anders Carlsson43168122009-04-10 04:54:13 +00002002bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2003 EvalInfo Info(Ctx, Result);
2004
2005 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2006}
2007
Eli Friedman7d45c482009-09-13 10:17:44 +00002008bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2009 EvalInfo Info(Ctx, Result, true);
2010
2011 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2012}
2013
Chris Lattner67d7b922008-11-16 21:24:15 +00002014/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00002015/// folded, but discard the result.
2016bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00002017 EvalResult Result;
2018 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00002019}
Anders Carlsson59689ed2008-11-22 21:04:56 +00002020
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00002021bool Expr::HasSideEffects(ASTContext &Ctx) const {
2022 Expr::EvalResult Result;
2023 EvalInfo Info(Ctx, Result);
2024 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2025}
2026
Anders Carlsson59689ed2008-11-22 21:04:56 +00002027APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002028 EvalResult EvalResult;
2029 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00002030 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00002031 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002032 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00002033
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002034 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00002035}