blob: c40feb4c28080366b5c01322a9d93cdcf9a7e728 [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?
73 Result = Value.getLValueBase() || Value.getLValueOffset();
74 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);
Eli Friedman9a156e52008-11-12 09:44:48 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
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())) {
259 return APValue(E, 0);
260 } 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())
264 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Douglas Gregor0840cc02009-11-01 20:32:48 +0000266 const VarDecl *Def = 0;
267 if (const Expr *Init = VD->getDefinition(Def))
268 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000269 }
270
271 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000272}
273
Eli Friedman9a156e52008-11-12 09:44:48 +0000274APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000275 if (!Info.AnyLValue && !E->isFileScope())
276 return APValue();
277 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000278}
279
280APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
281 APValue result;
282 QualType Ty;
283 if (E->isArrow()) {
284 if (!EvaluatePointer(E->getBase(), result, Info))
285 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000286 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000287 } else {
288 result = Visit(E->getBase());
289 if (result.isUninit())
290 return APValue();
291 Ty = E->getBase()->getType();
292 }
293
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000294 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000295 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000296
297 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
298 if (!FD) // FIXME: deal with other kinds of member expressions
299 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000300
301 if (FD->getType()->isReferenceType())
302 return APValue();
303
Eli Friedman9a156e52008-11-12 09:44:48 +0000304 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000305 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000306 for (RecordDecl::field_iterator Field = RD->field_begin(),
307 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000308 Field != FieldEnd; (void)++Field, ++i) {
309 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000310 break;
311 }
312
313 result.setLValue(result.getLValueBase(),
314 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
315
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 Dyck40775002010-01-11 17:06:35 +0000333 Result.getLValueOffset() + Offset.getQuantity());
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)
368 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000369 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
370 { return APValue(E, 0); }
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())
374 return APValue(E, 0);
375 return APValue();
376 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000377 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
378 { return APValue((Expr*)0, 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)
383 { return APValue((Expr*)0, 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 Dyck40775002010-01-11 17:06:35 +0000422 CharUnits Offset = CharUnits::fromQuantity(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 Dyck40775002010-01-11 17:06:35 +0000429 return APValue(ResultLValue.getLValueBase(), Offset.getQuantity());
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()));
464 return APValue(0, Result.getInt().getZExtValue());
465 }
466
467 // Cast is of an lvalue, no need to change value.
Chris Lattner05706e882008-07-11 18:11:29 +0000468 return Result;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000469 }
470 break;
Chris Lattner05706e882008-07-11 18:11:29 +0000471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Eli Friedman847a2bc2009-12-27 05:43:15 +0000473 case CastExpr::CK_NoOp:
474 case CastExpr::CK_BitCast:
475 case CastExpr::CK_AnyPointerToObjCPointerCast:
476 case CastExpr::CK_AnyPointerToBlockPointerCast:
477 return Visit(SubExpr);
478
479 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbarce399542009-02-20 18:22:23 +0000480 APValue Result;
481 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman847a2bc2009-12-27 05:43:15 +0000482 break;
Daniel Dunbarce399542009-02-20 18:22:23 +0000483
484 if (Result.isInt()) {
485 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
486 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Daniel Dunbarce399542009-02-20 18:22:23 +0000489 // Cast is of an lvalue, no need to change value.
490 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000491 }
Eli Friedman847a2bc2009-12-27 05:43:15 +0000492 case CastExpr::CK_ArrayToPointerDecay:
493 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman9a156e52008-11-12 09:44:48 +0000494 APValue Result;
495 if (EvaluateLValue(SubExpr, Result, Info))
496 return Result;
Eli Friedman847a2bc2009-12-27 05:43:15 +0000497 break;
498 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000499 }
500
Chris Lattner05706e882008-07-11 18:11:29 +0000501 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000502}
Chris Lattner05706e882008-07-11 18:11:29 +0000503
Eli Friedmanc69d4542009-01-25 01:54:01 +0000504APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000505 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000506 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000507 return APValue(E, 0);
508 return APValue();
509}
510
Eli Friedman9a156e52008-11-12 09:44:48 +0000511APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
512 bool BoolResult;
513 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
514 return APValue();
515
516 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
517
518 APValue Result;
519 if (EvaluatePointer(EvalExpr, Result, Info))
520 return Result;
521 return APValue();
522}
Chris Lattner05706e882008-07-11 18:11:29 +0000523
524//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000525// Vector Evaluation
526//===----------------------------------------------------------------------===//
527
528namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000529 class VectorExprEvaluator
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000530 : public StmtVisitor<VectorExprEvaluator, APValue> {
531 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000532 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000533 public:
Mike Stump11289f42009-09-09 15:08:12 +0000534
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000535 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000536
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000537 APValue VisitStmt(Stmt *S) {
538 return APValue();
539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Eli Friedman3ae59112009-02-23 04:23:56 +0000541 APValue VisitParenExpr(ParenExpr *E)
542 { return Visit(E->getSubExpr()); }
543 APValue VisitUnaryExtension(const UnaryOperator *E)
544 { return Visit(E->getSubExpr()); }
545 APValue VisitUnaryPlus(const UnaryOperator *E)
546 { return Visit(E->getSubExpr()); }
547 APValue VisitUnaryReal(const UnaryOperator *E)
548 { return Visit(E->getSubExpr()); }
549 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
550 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000551 APValue VisitCastExpr(const CastExpr* E);
552 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
553 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000554 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000555 APValue VisitChooseExpr(const ChooseExpr *E)
556 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000557 APValue VisitUnaryImag(const UnaryOperator *E);
558 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000559 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000560 // shufflevector, ExtVectorElementExpr
561 // (Note that these require implementing conversions
562 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000563 };
564} // end anonymous namespace
565
566static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
567 if (!E->getType()->isVectorType())
568 return false;
569 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
570 return !Result.isUninit();
571}
572
573APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000574 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000575 QualType EltTy = VTy->getElementType();
576 unsigned NElts = VTy->getNumElements();
577 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000578
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000579 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000580 QualType SETy = SE->getType();
581 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000582
Nate Begeman2ffd3842009-06-26 18:22:18 +0000583 // Check for vector->vector bitcast and scalar->vector splat.
584 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000585 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000586 } else if (SETy->isIntegerType()) {
587 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000588 if (!EvaluateInteger(SE, IntResult, Info))
589 return APValue();
590 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000591 } else if (SETy->isRealFloatingType()) {
592 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000593 if (!EvaluateFloat(SE, F, Info))
594 return APValue();
595 Result = APValue(F);
596 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000597 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000598
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000599 // For casts of a scalar to ExtVector, convert the scalar to the element type
600 // and splat it to all elements.
601 if (E->getType()->isExtVectorType()) {
602 if (EltTy->isIntegerType() && Result.isInt())
603 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
604 Info.Ctx));
605 else if (EltTy->isIntegerType())
606 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
607 Info.Ctx));
608 else if (EltTy->isRealFloatingType() && Result.isInt())
609 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
610 Info.Ctx));
611 else if (EltTy->isRealFloatingType())
612 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
613 Info.Ctx));
614 else
615 return APValue();
616
617 // Splat and create vector APValue.
618 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
619 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000620 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000621
622 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
623 // to the vector. To construct the APValue vector initializer, bitcast the
624 // initializing value to an APInt, and shift out the bits pertaining to each
625 // element.
626 APSInt Init;
627 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000628
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000629 llvm::SmallVector<APValue, 4> Elts;
630 for (unsigned i = 0; i != NElts; ++i) {
631 APSInt Tmp = Init;
632 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000633
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000634 if (EltTy->isIntegerType())
635 Elts.push_back(APValue(Tmp));
636 else if (EltTy->isRealFloatingType())
637 Elts.push_back(APValue(APFloat(Tmp)));
638 else
639 return APValue();
640
641 Init >>= EltWidth;
642 }
643 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000644}
645
Mike Stump11289f42009-09-09 15:08:12 +0000646APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000647VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
648 return this->Visit(const_cast<Expr*>(E->getInitializer()));
649}
650
Mike Stump11289f42009-09-09 15:08:12 +0000651APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000652VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000653 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000654 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000655 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000656
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000657 QualType EltTy = VT->getElementType();
658 llvm::SmallVector<APValue, 4> Elements;
659
Eli Friedman3ae59112009-02-23 04:23:56 +0000660 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000661 if (EltTy->isIntegerType()) {
662 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000663 if (i < NumInits) {
664 if (!EvaluateInteger(E->getInit(i), sInt, Info))
665 return APValue();
666 } else {
667 sInt = Info.Ctx.MakeIntValue(0, EltTy);
668 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000669 Elements.push_back(APValue(sInt));
670 } else {
671 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000672 if (i < NumInits) {
673 if (!EvaluateFloat(E->getInit(i), f, Info))
674 return APValue();
675 } else {
676 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
677 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000678 Elements.push_back(APValue(f));
679 }
680 }
681 return APValue(&Elements[0], Elements.size());
682}
683
Mike Stump11289f42009-09-09 15:08:12 +0000684APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000685VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000686 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000687 QualType EltTy = VT->getElementType();
688 APValue ZeroElement;
689 if (EltTy->isIntegerType())
690 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
691 else
692 ZeroElement =
693 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
694
695 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
696 return APValue(&Elements[0], Elements.size());
697}
698
699APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
700 bool BoolResult;
701 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
702 return APValue();
703
704 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
705
706 APValue Result;
707 if (EvaluateVector(EvalExpr, Result, Info))
708 return Result;
709 return APValue();
710}
711
Eli Friedman3ae59112009-02-23 04:23:56 +0000712APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
713 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
714 Info.EvalResult.HasSideEffects = true;
715 return GetZeroVector(E->getType());
716}
717
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000718//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000719// Integer Evaluation
720//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000721
722namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000723class IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000724 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000725 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000726 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000727public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000728 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000729 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000730
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000731 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000732 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000733 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000734 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000735 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000736 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000737 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000738 return true;
739 }
740
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000741 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000742 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000743 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000744 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000745 Result = APValue(APSInt(I));
746 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000747 return true;
748 }
749
750 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000751 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000752 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000753 return true;
754 }
755
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000756 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000757 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000758 if (Info.EvalResult.Diag == 0) {
759 Info.EvalResult.DiagLoc = L;
760 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000761 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000762 }
Chris Lattner99415702008-07-12 00:14:42 +0000763 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000766 //===--------------------------------------------------------------------===//
767 // Visitor Methods
768 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000769
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000770 bool VisitStmt(Stmt *) {
771 assert(0 && "This should be called on integers, stmts are not integers");
772 return false;
773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000775 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000776 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattnere13042c2008-07-11 19:10:17 +0000779 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000780
Chris Lattner7174bf32008-07-12 00:38:25 +0000781 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000782 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000783 }
784 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000785 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000786 }
787 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000788 // Per gcc docs "this built-in function ignores top level
789 // qualifiers". We need to use the canonical version to properly
790 // be able to strip CRV qualifiers from the type.
791 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
792 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000793 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000794 T1.getUnqualifiedType()),
795 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000796 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000797
798 bool CheckReferencedDecl(const Expr *E, const Decl *D);
799 bool VisitDeclRefExpr(const DeclRefExpr *E) {
800 return CheckReferencedDecl(E, E->getDecl());
801 }
802 bool VisitMemberExpr(const MemberExpr *E) {
803 if (CheckReferencedDecl(E, E->getMemberDecl())) {
804 // Conservatively assume a MemberExpr will have side-effects
805 Info.EvalResult.HasSideEffects = true;
806 return true;
807 }
808 return false;
809 }
810
Chris Lattner7174bf32008-07-12 00:38:25 +0000811 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000812 bool VisitBinaryOperator(const BinaryOperator *E);
813 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000814 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000815
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000816 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000817 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
818
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000819 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000820 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000821 }
Mike Stump11289f42009-09-09 15:08:12 +0000822
Anders Carlsson39def3a2008-12-21 22:39:40 +0000823 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000824 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000827 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000828 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000829 }
830
Eli Friedman4e7a2412009-02-27 04:45:43 +0000831 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
832 return Success(0, E);
833 }
834
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000835 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000836 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000837 }
838
Eli Friedman449fe542009-03-23 04:56:01 +0000839 bool VisitChooseExpr(const ChooseExpr *E) {
840 return Visit(E->getChosenSubExpr(Info.Ctx));
841 }
842
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000843 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000844 bool VisitUnaryImag(const UnaryOperator *E);
845
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000846private:
Chris Lattner68061312009-01-24 21:53:27 +0000847 unsigned GetAlignOfExpr(const Expr *E);
848 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000849 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000850};
Chris Lattner05706e882008-07-11 18:11:29 +0000851} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000852
Daniel Dunbarce399542009-02-20 18:22:23 +0000853static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000854 if (!E->getType()->isIntegralType())
855 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000856
Daniel Dunbarce399542009-02-20 18:22:23 +0000857 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
858}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000859
Daniel Dunbarce399542009-02-20 18:22:23 +0000860static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
861 APValue Val;
862 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
863 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000864 Result = Val.getInt();
865 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000866}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000867
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000868bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000869 // Enums are integer constant exprs.
Eli Friedmanee275c82009-12-10 22:29:29 +0000870 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
871 return Success(ECD->getInitVal(), E);
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000872
873 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000874 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000875 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
876 == Qualifiers::Const) {
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000877 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +0000878 const VarDecl *Def = 0;
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000879 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000880 if (APValue *V = VD->getEvaluatedValue()) {
881 if (V->isInt())
882 return Success(V->getInt(), E);
883 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
884 }
885
886 if (VD->isEvaluatingValue())
887 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
888
889 VD->setEvaluatingValue();
890
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000891 if (Visit(const_cast<Expr*>(Init))) {
892 // Cache the evaluated value in the variable declaration.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000893 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000894 return true;
895 }
896
Eli Friedman1d6fb162009-12-03 20:31:57 +0000897 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000898 return false;
899 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000900 }
901 }
902
Chris Lattner7174bf32008-07-12 00:38:25 +0000903 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000904 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000905}
906
Chris Lattner86ee2862008-10-06 06:40:35 +0000907/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
908/// as GCC.
909static int EvaluateBuiltinClassifyType(const CallExpr *E) {
910 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000911 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000912 enum gcc_type_class {
913 no_type_class = -1,
914 void_type_class, integer_type_class, char_type_class,
915 enumeral_type_class, boolean_type_class,
916 pointer_type_class, reference_type_class, offset_type_class,
917 real_type_class, complex_type_class,
918 function_type_class, method_type_class,
919 record_type_class, union_type_class,
920 array_type_class, string_type_class,
921 lang_type_class
922 };
Mike Stump11289f42009-09-09 15:08:12 +0000923
924 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000925 // ideal, however it is what gcc does.
926 if (E->getNumArgs() == 0)
927 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattner86ee2862008-10-06 06:40:35 +0000929 QualType ArgTy = E->getArg(0)->getType();
930 if (ArgTy->isVoidType())
931 return void_type_class;
932 else if (ArgTy->isEnumeralType())
933 return enumeral_type_class;
934 else if (ArgTy->isBooleanType())
935 return boolean_type_class;
936 else if (ArgTy->isCharType())
937 return string_type_class; // gcc doesn't appear to use char_type_class
938 else if (ArgTy->isIntegerType())
939 return integer_type_class;
940 else if (ArgTy->isPointerType())
941 return pointer_type_class;
942 else if (ArgTy->isReferenceType())
943 return reference_type_class;
944 else if (ArgTy->isRealType())
945 return real_type_class;
946 else if (ArgTy->isComplexType())
947 return complex_type_class;
948 else if (ArgTy->isFunctionType())
949 return function_type_class;
950 else if (ArgTy->isStructureType())
951 return record_type_class;
952 else if (ArgTy->isUnionType())
953 return union_type_class;
954 else if (ArgTy->isArrayType())
955 return array_type_class;
956 else if (ArgTy->isUnionType())
957 return union_type_class;
958 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
959 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
960 return -1;
961}
962
Chris Lattner7174bf32008-07-12 00:38:25 +0000963bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000964 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000965 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000966 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000967
968 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000969 const Expr *Arg = E->getArg(0)->IgnoreParens();
970 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000971 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000972 && Base.Val.getKind() == APValue::LValue
973 && !Base.HasSideEffects)
974 if (const Expr *LVBase = Base.Val.getLValueBase())
975 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
976 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000977 if (!VD->getType()->isIncompleteType()
978 && VD->getType()->isObjectType()
979 && !VD->getType()->isVariablyModifiedType()
980 && !VD->getType()->isDependentType()) {
Ken Dyck40775002010-01-11 17:06:35 +0000981 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
982 CharUnits Offset =
983 CharUnits::fromQuantity(Base.Val.getLValueOffset());
984 if (!Offset.isNegative() && Offset <= Size)
985 Size -= Offset;
Mike Stump5179f302009-10-28 21:22:24 +0000986 else
Ken Dyck40775002010-01-11 17:06:35 +0000987 Size = CharUnits::Zero();
988 return Success(Size.getQuantity(), E);
Mike Stump5179f302009-10-28 21:22:24 +0000989 }
Mike Stump722cedf2009-10-26 18:35:08 +0000990 }
991 }
992
Eric Christopherc8791562009-12-23 03:49:37 +0000993 // TODO: Perhaps we should let LLVM lower this?
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +0000994 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer0128f662010-01-03 18:18:37 +0000995 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattner4f105592009-11-03 19:48:51 +0000996 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000997 return Success(0, E);
998 }
Mike Stump876387b2009-10-27 22:09:17 +0000999
Mike Stump722cedf2009-10-26 18:35:08 +00001000 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1001 }
1002
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001003 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001004 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +00001005
Anders Carlsson4c76e932008-11-24 04:21:33 +00001006 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001007 // __builtin_constant_p always has one operand: it returns true if that
1008 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001009 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +00001010
1011 case Builtin::BI__builtin_eh_return_data_regno: {
1012 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1013 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1014 return Success(Operand, E);
1015 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001016 }
Chris Lattner7174bf32008-07-12 00:38:25 +00001017}
Anders Carlsson4a3585b2008-07-08 15:34:11 +00001018
Chris Lattnere13042c2008-07-11 19:10:17 +00001019bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001020 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001021 if (!Visit(E->getRHS()))
1022 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001023
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001024 // If we can't evaluate the LHS, it might have side effects;
1025 // conservatively mark it.
1026 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1027 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001028
Anders Carlsson564730a2008-12-01 02:07:06 +00001029 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001030 }
1031
1032 if (E->isLogicalOp()) {
1033 // These need to be handled specially because the operands aren't
1034 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001035 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001036
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001037 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001038 // We were able to evaluate the LHS, see if we can get away with not
1039 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001040 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001041 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001042
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001043 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001044 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001045 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001046 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001047 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001048 }
1049 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001050 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001051 // We can't evaluate the LHS; however, sometimes the result
1052 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001053 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001054 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001055 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001056 // must have had side effects.
1057 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001058
1059 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001060 }
1061 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001062 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001063
Eli Friedman5a332ea2008-11-13 06:09:17 +00001064 return false;
1065 }
1066
Anders Carlssonacc79812008-11-16 07:17:21 +00001067 QualType LHSTy = E->getLHS()->getType();
1068 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001069
1070 if (LHSTy->isAnyComplexType()) {
1071 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1072 APValue LHS, RHS;
1073
1074 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1075 return false;
1076
1077 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1078 return false;
1079
1080 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001081 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001082 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001083 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001084 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1085
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001086 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001087 return Success((CR_r == APFloat::cmpEqual &&
1088 CR_i == APFloat::cmpEqual), E);
1089 else {
1090 assert(E->getOpcode() == BinaryOperator::NE &&
1091 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001092 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001093 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001094 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001095 CR_i == APFloat::cmpLessThan)), E);
1096 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001097 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001098 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001099 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1100 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1101 else {
1102 assert(E->getOpcode() == BinaryOperator::NE &&
1103 "Invalid compex comparison.");
1104 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1105 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1106 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001107 }
1108 }
Mike Stump11289f42009-09-09 15:08:12 +00001109
Anders Carlssonacc79812008-11-16 07:17:21 +00001110 if (LHSTy->isRealFloatingType() &&
1111 RHSTy->isRealFloatingType()) {
1112 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001113
Anders Carlssonacc79812008-11-16 07:17:21 +00001114 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1115 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001116
Anders Carlssonacc79812008-11-16 07:17:21 +00001117 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1118 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Anders Carlssonacc79812008-11-16 07:17:21 +00001120 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001121
Anders Carlssonacc79812008-11-16 07:17:21 +00001122 switch (E->getOpcode()) {
1123 default:
1124 assert(0 && "Invalid binary operator!");
1125 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001126 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001127 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001128 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001129 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001130 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001131 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001132 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001133 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001134 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001135 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001136 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001137 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001138 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001139 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001140 }
Mike Stump11289f42009-09-09 15:08:12 +00001141
Eli Friedmana38da572009-04-28 19:17:36 +00001142 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1143 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001144 APValue LHSValue;
1145 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1146 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001147
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001148 APValue RHSValue;
1149 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1150 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001151
Eli Friedman334046a2009-06-14 02:17:33 +00001152 // Reject any bases from the normal codepath; we special-case comparisons
1153 // to null.
1154 if (LHSValue.getLValueBase()) {
1155 if (!E->isEqualityOp())
1156 return false;
1157 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1158 return false;
1159 bool bres;
1160 if (!EvalPointerValueAsBool(LHSValue, bres))
1161 return false;
1162 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1163 } else if (RHSValue.getLValueBase()) {
1164 if (!E->isEqualityOp())
1165 return false;
1166 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1167 return false;
1168 bool bres;
1169 if (!EvalPointerValueAsBool(RHSValue, bres))
1170 return false;
1171 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1172 }
Eli Friedman64004332009-03-23 04:38:34 +00001173
Eli Friedmana38da572009-04-28 19:17:36 +00001174 if (E->getOpcode() == BinaryOperator::Sub) {
1175 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001176 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001177
Eli Friedmana38da572009-04-28 19:17:36 +00001178 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001179 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dyck40775002010-01-11 17:06:35 +00001180 D /= Info.Ctx.getTypeSizeInChars(ElementType).getQuantity();
Eli Friedman64004332009-03-23 04:38:34 +00001181
Eli Friedmana38da572009-04-28 19:17:36 +00001182 return Success(D, E);
1183 }
1184 bool Result;
1185 if (E->getOpcode() == BinaryOperator::EQ) {
1186 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001187 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001188 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1189 }
1190 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001191 }
1192 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001193 if (!LHSTy->isIntegralType() ||
1194 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001195 // We can't continue from here for non-integral types, and they
1196 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001197 return false;
1198 }
1199
Anders Carlsson9c181652008-07-08 14:35:21 +00001200 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001201 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001202 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001203
Eli Friedman94c25c62009-03-24 01:14:50 +00001204 APValue RHSVal;
1205 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001206 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001207
1208 // Handle cases like (unsigned long)&a + 4.
1209 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1210 uint64_t offset = Result.getLValueOffset();
1211 if (E->getOpcode() == BinaryOperator::Add)
1212 offset += RHSVal.getInt().getZExtValue();
1213 else
1214 offset -= RHSVal.getInt().getZExtValue();
1215 Result = APValue(Result.getLValueBase(), offset);
1216 return true;
1217 }
1218
1219 // Handle cases like 4 + (unsigned long)&a
1220 if (E->getOpcode() == BinaryOperator::Add &&
1221 RHSVal.isLValue() && Result.isInt()) {
1222 uint64_t offset = RHSVal.getLValueOffset();
1223 offset += Result.getInt().getZExtValue();
1224 Result = APValue(RHSVal.getLValueBase(), offset);
1225 return true;
1226 }
1227
1228 // All the following cases expect both operands to be an integer
1229 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001230 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001231
Eli Friedman94c25c62009-03-24 01:14:50 +00001232 APSInt& RHS = RHSVal.getInt();
1233
Anders Carlsson9c181652008-07-08 14:35:21 +00001234 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001235 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001236 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001237 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1238 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1239 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1240 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1241 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1242 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001243 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001244 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001245 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001246 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001247 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001248 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001249 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001250 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001251 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001252 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001253 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001254 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1255 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001256 }
1257 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001258 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001259 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1260 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001263 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1264 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1265 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1266 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1267 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1268 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001269 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001270}
1271
Nuno Lopes42042612008-11-16 19:28:31 +00001272bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001273 bool Cond;
1274 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001275 return false;
1276
Nuno Lopes527b5a62008-11-16 22:06:39 +00001277 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001278}
1279
Chris Lattner68061312009-01-24 21:53:27 +00001280unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001281 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1282 // the result is the size of the referenced type."
1283 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1284 // result shall be the alignment of the referenced type."
1285 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1286 T = Ref->getPointeeType();
1287
Chris Lattner24aeeab2009-01-24 21:09:06 +00001288 // Get information about the alignment.
1289 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001290
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001291 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001292 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001293}
1294
Chris Lattner68061312009-01-24 21:53:27 +00001295unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1296 E = E->IgnoreParens();
1297
1298 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001299 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001300 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001301 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001302
Chris Lattner68061312009-01-24 21:53:27 +00001303 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001304 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1305 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001306
Chris Lattner24aeeab2009-01-24 21:09:06 +00001307 return GetAlignOfType(E->getType());
1308}
1309
1310
Sebastian Redl6f282892008-11-11 17:56:53 +00001311/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1312/// expression's type.
1313bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001314 // Handle alignof separately.
1315 if (!E->isSizeOf()) {
1316 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001317 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001318 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001319 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001320 }
Eli Friedman64004332009-03-23 04:38:34 +00001321
Sebastian Redl6f282892008-11-11 17:56:53 +00001322 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001323 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1324 // the result is the size of the referenced type."
1325 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1326 // result shall be the alignment of the referenced type."
1327 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1328 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001329
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001330 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1331 // extension.
1332 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1333 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001334
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001335 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001336 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001337 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001338
Chris Lattner24aeeab2009-01-24 21:09:06 +00001339 // Get information about the size.
Ken Dyck40775002010-01-11 17:06:35 +00001340 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001341}
1342
Chris Lattnere13042c2008-07-11 19:10:17 +00001343bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001344 // Special case unary operators that do not need their subexpression
1345 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001346 if (E->isOffsetOfOp()) {
1347 // The AST for offsetof is defined in such a way that we can just
1348 // directly Evaluate it as an l-value.
1349 APValue LV;
1350 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1351 return false;
1352 if (LV.getLValueBase())
1353 return false;
1354 return Success(LV.getLValueOffset(), E);
1355 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001356
1357 if (E->getOpcode() == UnaryOperator::LNot) {
1358 // LNot's operand isn't necessarily an integer, so we handle it specially.
1359 bool bres;
1360 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1361 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001362 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001363 }
1364
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001365 // Only handle integral operations...
1366 if (!E->getSubExpr()->getType()->isIntegralType())
1367 return false;
1368
Chris Lattnercdf34e72008-07-11 22:52:41 +00001369 // Get the operand value into 'Result'.
1370 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001371 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001372
Chris Lattnerf09ad162008-07-11 22:15:16 +00001373 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001374 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001375 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1376 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001377 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001378 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001379 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1380 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001381 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001382 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001383 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001384 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001385 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001386 if (!Result.isInt()) return false;
1387 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001388 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001389 if (!Result.isInt()) return false;
1390 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001391 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001392}
Mike Stump11289f42009-09-09 15:08:12 +00001393
Chris Lattner477c4be2008-07-12 01:15:53 +00001394/// HandleCast - This is used to evaluate implicit or explicit casts where the
1395/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001396bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001397 Expr *SubExpr = E->getSubExpr();
1398 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001399 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001400
Eli Friedman9a156e52008-11-12 09:44:48 +00001401 if (DestType->isBooleanType()) {
1402 bool BoolResult;
1403 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1404 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001405 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001406 }
1407
Anders Carlsson9c181652008-07-08 14:35:21 +00001408 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001409 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001410 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001411 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001412
Eli Friedman742421e2009-02-20 01:15:07 +00001413 if (!Result.isInt()) {
1414 // Only allow casts of lvalues if they are lossless.
1415 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1416 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001417
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001418 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001419 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001420 }
Mike Stump11289f42009-09-09 15:08:12 +00001421
Chris Lattner477c4be2008-07-12 01:15:53 +00001422 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001423 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001424 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001425 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001426 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001427
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001428 if (LV.getLValueBase()) {
1429 // Only allow based lvalue casts if they are lossless.
1430 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1431 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001432
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001433 Result = LV;
1434 return true;
1435 }
1436
1437 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1438 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001439 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001440
Eli Friedman742421e2009-02-20 01:15:07 +00001441 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1442 // This handles double-conversion cases, where there's both
1443 // an l-value promotion and an implicit conversion to int.
1444 APValue LV;
1445 if (!EvaluateLValue(SubExpr, LV, Info))
1446 return false;
1447
1448 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1449 return false;
1450
1451 Result = LV;
1452 return true;
1453 }
1454
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001455 if (SrcType->isAnyComplexType()) {
1456 APValue C;
1457 if (!EvaluateComplex(SubExpr, C, Info))
1458 return false;
1459 if (C.isComplexFloat())
1460 return Success(HandleFloatToIntCast(DestType, SrcType,
1461 C.getComplexFloatReal(), Info.Ctx),
1462 E);
1463 else
1464 return Success(HandleIntToIntCast(DestType, SrcType,
1465 C.getComplexIntReal(), Info.Ctx), E);
1466 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001467 // FIXME: Handle vectors
1468
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001469 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001470 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001471
Eli Friedman24c01542008-08-22 00:06:13 +00001472 APFloat F(0.0);
1473 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001474 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001475
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001476 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001477}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001478
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001479bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1480 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1481 APValue LV;
1482 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1483 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1484 return Success(LV.getComplexIntReal(), E);
1485 }
1486
1487 return Visit(E->getSubExpr());
1488}
1489
Eli Friedman4e7a2412009-02-27 04:45:43 +00001490bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001491 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1492 APValue LV;
1493 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1494 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1495 return Success(LV.getComplexIntImag(), E);
1496 }
1497
Eli Friedman4e7a2412009-02-27 04:45:43 +00001498 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1499 Info.EvalResult.HasSideEffects = true;
1500 return Success(0, E);
1501}
1502
Chris Lattner05706e882008-07-11 18:11:29 +00001503//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001504// Float Evaluation
1505//===----------------------------------------------------------------------===//
1506
1507namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001508class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001509 : public StmtVisitor<FloatExprEvaluator, bool> {
1510 EvalInfo &Info;
1511 APFloat &Result;
1512public:
1513 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1514 : Info(info), Result(result) {}
1515
1516 bool VisitStmt(Stmt *S) {
1517 return false;
1518 }
1519
1520 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001521 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001522
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001523 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001524 bool VisitBinaryOperator(const BinaryOperator *E);
1525 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001526 bool VisitCastExpr(CastExpr *E);
1527 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanf3da3342009-12-04 02:12:53 +00001528 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001529
Eli Friedman449fe542009-03-23 04:56:01 +00001530 bool VisitChooseExpr(const ChooseExpr *E)
1531 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1532 bool VisitUnaryExtension(const UnaryOperator *E)
1533 { return Visit(E->getSubExpr()); }
1534
1535 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedmanf3da3342009-12-04 02:12:53 +00001536 // member of vector, ImplicitValueInitExpr
Eli Friedman24c01542008-08-22 00:06:13 +00001537};
1538} // end anonymous namespace
1539
1540static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1541 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1542}
1543
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001544bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001545 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001546 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001547 case Builtin::BI__builtin_huge_val:
1548 case Builtin::BI__builtin_huge_valf:
1549 case Builtin::BI__builtin_huge_vall:
1550 case Builtin::BI__builtin_inf:
1551 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001552 case Builtin::BI__builtin_infl: {
1553 const llvm::fltSemantics &Sem =
1554 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001555 Result = llvm::APFloat::getInf(Sem);
1556 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001557 }
Mike Stump11289f42009-09-09 15:08:12 +00001558
Chris Lattner0b7282e2008-10-06 06:31:58 +00001559 case Builtin::BI__builtin_nan:
1560 case Builtin::BI__builtin_nanf:
1561 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001562 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001563 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001564 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001565 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001566 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001567 const llvm::fltSemantics &Sem =
1568 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001569 unsigned Type = 0;
1570 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump2346cd22009-05-30 03:56:50 +00001571 return false;
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001572 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001573 return true;
1574 }
1575 }
1576 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001577
1578 case Builtin::BI__builtin_fabs:
1579 case Builtin::BI__builtin_fabsf:
1580 case Builtin::BI__builtin_fabsl:
1581 if (!EvaluateFloat(E->getArg(0), Result, Info))
1582 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001583
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001584 if (Result.isNegative())
1585 Result.changeSign();
1586 return true;
1587
Mike Stump11289f42009-09-09 15:08:12 +00001588 case Builtin::BI__builtin_copysign:
1589 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001590 case Builtin::BI__builtin_copysignl: {
1591 APFloat RHS(0.);
1592 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1593 !EvaluateFloat(E->getArg(1), RHS, Info))
1594 return false;
1595 Result.copySign(RHS);
1596 return true;
1597 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001598 }
1599}
1600
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001601bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001602 if (E->getOpcode() == UnaryOperator::Deref)
1603 return false;
1604
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001605 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1606 return false;
1607
1608 switch (E->getOpcode()) {
1609 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001610 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001611 return true;
1612 case UnaryOperator::Minus:
1613 Result.changeSign();
1614 return true;
1615 }
1616}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001617
Eli Friedman24c01542008-08-22 00:06:13 +00001618bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001619 if (E->getOpcode() == BinaryOperator::Comma) {
1620 if (!EvaluateFloat(E->getRHS(), Result, Info))
1621 return false;
1622
1623 // If we can't evaluate the LHS, it might have side effects;
1624 // conservatively mark it.
1625 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1626 Info.EvalResult.HasSideEffects = true;
1627
1628 return true;
1629 }
1630
Eli Friedman24c01542008-08-22 00:06:13 +00001631 // FIXME: Diagnostics? I really don't understand how the warnings
1632 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001633 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001634 if (!EvaluateFloat(E->getLHS(), Result, Info))
1635 return false;
1636 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1637 return false;
1638
1639 switch (E->getOpcode()) {
1640 default: return false;
1641 case BinaryOperator::Mul:
1642 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1643 return true;
1644 case BinaryOperator::Add:
1645 Result.add(RHS, APFloat::rmNearestTiesToEven);
1646 return true;
1647 case BinaryOperator::Sub:
1648 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1649 return true;
1650 case BinaryOperator::Div:
1651 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1652 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001653 }
1654}
1655
1656bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1657 Result = E->getValue();
1658 return true;
1659}
1660
Eli Friedman9a156e52008-11-12 09:44:48 +00001661bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1662 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001663
Eli Friedman9a156e52008-11-12 09:44:48 +00001664 if (SubExpr->getType()->isIntegralType()) {
1665 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001666 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001667 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001668 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001669 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001670 return true;
1671 }
1672 if (SubExpr->getType()->isRealFloatingType()) {
1673 if (!Visit(SubExpr))
1674 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001675 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1676 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001677 return true;
1678 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001679 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001680
1681 return false;
1682}
1683
1684bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1685 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1686 return true;
1687}
1688
Eli Friedmanf3da3342009-12-04 02:12:53 +00001689bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1690 bool Cond;
1691 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1692 return false;
1693
1694 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1695}
1696
Eli Friedman24c01542008-08-22 00:06:13 +00001697//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001698// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001699//===----------------------------------------------------------------------===//
1700
1701namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001702class ComplexExprEvaluator
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001703 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001704 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001705
Anders Carlsson537969c2008-11-16 20:27:53 +00001706public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001707 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001708
Anders Carlsson537969c2008-11-16 20:27:53 +00001709 //===--------------------------------------------------------------------===//
1710 // Visitor Methods
1711 //===--------------------------------------------------------------------===//
1712
1713 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001714 return APValue();
1715 }
Mike Stump11289f42009-09-09 15:08:12 +00001716
Anders Carlsson537969c2008-11-16 20:27:53 +00001717 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1718
1719 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001720 Expr* SubExpr = E->getSubExpr();
1721
1722 if (SubExpr->getType()->isRealFloatingType()) {
1723 APFloat Result(0.0);
1724
1725 if (!EvaluateFloat(SubExpr, Result, Info))
1726 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001727
1728 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001729 Result);
1730 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001731 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001732 "Unexpected imaginary literal.");
1733
1734 llvm::APSInt Result;
1735 if (!EvaluateInteger(SubExpr, Result, Info))
1736 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001737
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001738 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1739 Zero = 0;
1740 return APValue(Zero, Result);
1741 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001742 }
1743
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001744 APValue VisitCastExpr(CastExpr *E) {
1745 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001746 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001747 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001748
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001749 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001750 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001751
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001752 if (!EvaluateFloat(SubExpr, Result, Info))
1753 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001754
1755 if (EltType->isRealFloatingType()) {
1756 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001757 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001758 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1759 } else {
1760 llvm::APSInt IResult;
1761 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1762 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1763 Zero = 0;
1764 return APValue(IResult, Zero);
1765 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001766 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001767 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001768
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001769 if (!EvaluateInteger(SubExpr, Result, Info))
1770 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001771
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001772 if (EltType->isRealFloatingType()) {
1773 APFloat FResult =
1774 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001775 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001776 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1777 } else {
1778 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1779 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1780 Zero = 0;
1781 return APValue(Result, Zero);
1782 }
John McCall9dd450b2009-09-21 23:43:11 +00001783 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001784 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001785
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001786 if (!EvaluateComplex(SubExpr, Src, Info))
1787 return APValue();
1788
1789 QualType SrcType = CT->getElementType();
1790
1791 if (Src.isComplexFloat()) {
1792 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001793 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001794 Src.getComplexFloatReal(),
1795 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001796 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001797 Src.getComplexFloatImag(),
1798 Info.Ctx));
1799 } else {
1800 return APValue(HandleFloatToIntCast(EltType, SrcType,
1801 Src.getComplexFloatReal(),
1802 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001803 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001804 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001805 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001806 }
1807 } else {
1808 assert(Src.isComplexInt() && "Invalid evaluate result.");
1809 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001810 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001811 Src.getComplexIntReal(),
1812 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001813 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001814 Src.getComplexIntImag(),
1815 Info.Ctx));
1816 } else {
1817 return APValue(HandleIntToIntCast(EltType, SrcType,
1818 Src.getComplexIntReal(),
1819 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001820 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001821 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001822 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001823 }
1824 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001825 }
1826
1827 // FIXME: Handle more casts.
1828 return APValue();
1829 }
Mike Stump11289f42009-09-09 15:08:12 +00001830
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001831 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001832 APValue VisitChooseExpr(const ChooseExpr *E)
1833 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1834 APValue VisitUnaryExtension(const UnaryOperator *E)
1835 { return Visit(E->getSubExpr()); }
1836 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001837 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001838};
1839} // end anonymous namespace
1840
Mike Stump11289f42009-09-09 15:08:12 +00001841static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001842 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1843 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001844 (&Result.getComplexFloatReal().getSemantics() ==
1845 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001846 "Invalid complex evaluation.");
1847 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001848}
1849
Mike Stump11289f42009-09-09 15:08:12 +00001850APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001851 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001852
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001853 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001854 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001855
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001856 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001857 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001858
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001859 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1860 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001861 switch (E->getOpcode()) {
1862 default: return APValue();
1863 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001864 if (Result.isComplexFloat()) {
1865 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1866 APFloat::rmNearestTiesToEven);
1867 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1868 APFloat::rmNearestTiesToEven);
1869 } else {
1870 Result.getComplexIntReal() += RHS.getComplexIntReal();
1871 Result.getComplexIntImag() += RHS.getComplexIntImag();
1872 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001873 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001874 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001875 if (Result.isComplexFloat()) {
1876 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1877 APFloat::rmNearestTiesToEven);
1878 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1879 APFloat::rmNearestTiesToEven);
1880 } else {
1881 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1882 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1883 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001884 break;
1885 case BinaryOperator::Mul:
1886 if (Result.isComplexFloat()) {
1887 APValue LHS = Result;
1888 APFloat &LHS_r = LHS.getComplexFloatReal();
1889 APFloat &LHS_i = LHS.getComplexFloatImag();
1890 APFloat &RHS_r = RHS.getComplexFloatReal();
1891 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001892
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001893 APFloat Tmp = LHS_r;
1894 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1895 Result.getComplexFloatReal() = Tmp;
1896 Tmp = LHS_i;
1897 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1898 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1899
1900 Tmp = LHS_r;
1901 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1902 Result.getComplexFloatImag() = Tmp;
1903 Tmp = LHS_i;
1904 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1905 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1906 } else {
1907 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001908 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001909 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1910 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001911 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001912 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1913 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1914 }
1915 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001916 }
1917
1918 return Result;
1919}
1920
Anders Carlsson537969c2008-11-16 20:27:53 +00001921//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001922// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001923//===----------------------------------------------------------------------===//
1924
Chris Lattner67d7b922008-11-16 21:24:15 +00001925/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001926/// any crazy technique (that has nothing to do with language standards) that
1927/// we want to. If this function returns true, it returns the folded constant
1928/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001929bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1930 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001931
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001932 if (getType()->isVectorType()) {
1933 if (!EvaluateVector(this, Result.Val, Info))
1934 return false;
1935 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001936 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001937 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001938 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001939 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001940 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001941 } else if (getType()->isRealFloatingType()) {
1942 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001943 if (!EvaluateFloat(this, f, Info))
1944 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001945
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001946 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001947 } else if (getType()->isAnyComplexType()) {
1948 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001949 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001950 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001951 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001952
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001953 return true;
1954}
1955
Mike Stump5183a142009-10-26 23:05:19 +00001956bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1957 EvalInfo Info(Ctx, Result, true);
1958
1959 if (getType()->isVectorType()) {
1960 if (!EvaluateVector(this, Result.Val, Info))
1961 return false;
1962 } else if (getType()->isIntegerType()) {
1963 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1964 return false;
1965 } else if (getType()->hasPointerRepresentation()) {
1966 if (!EvaluatePointer(this, Result.Val, Info))
1967 return false;
1968 } else if (getType()->isRealFloatingType()) {
1969 llvm::APFloat f(0.0);
1970 if (!EvaluateFloat(this, f, Info))
1971 return false;
1972
1973 Result.Val = APValue(f);
1974 } else if (getType()->isAnyComplexType()) {
1975 if (!EvaluateComplex(this, Result.Val, Info))
1976 return false;
1977 } else
1978 return false;
1979
1980 return true;
1981}
1982
John McCall1be1c632010-01-05 23:42:56 +00001983bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
1984 EvalResult Scratch;
1985 EvalInfo Info(Ctx, Scratch);
1986
1987 return HandleConversionToBool(this, Result, Info);
1988}
1989
Anders Carlsson43168122009-04-10 04:54:13 +00001990bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1991 EvalInfo Info(Ctx, Result);
1992
1993 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1994}
1995
Eli Friedman7d45c482009-09-13 10:17:44 +00001996bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1997 EvalInfo Info(Ctx, Result, true);
1998
1999 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2000}
2001
Chris Lattner67d7b922008-11-16 21:24:15 +00002002/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00002003/// folded, but discard the result.
2004bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00002005 EvalResult Result;
2006 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00002007}
Anders Carlsson59689ed2008-11-22 21:04:56 +00002008
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00002009bool Expr::HasSideEffects(ASTContext &Ctx) const {
2010 Expr::EvalResult Result;
2011 EvalInfo Info(Ctx, Result);
2012 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2013}
2014
Anders Carlsson59689ed2008-11-22 21:04:56 +00002015APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002016 EvalResult EvalResult;
2017 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00002018 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00002019 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002020 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00002021
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002022 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00002023}