blob: b408a3f61583f59e2ac206fcfab560d189abb888 [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"
Anders Carlsson15b73de2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Anders Carlsson0a1707c2008-07-08 05:13:58 +000022#include "llvm/Support/Compiler.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
Eli Friedman9a156e52008-11-12 09:44:48 +000077static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
78 if (E->getType()->isIntegralType()) {
79 APSInt IntResult;
80 if (!EvaluateInteger(E, IntResult, Info))
81 return false;
82 Result = IntResult != 0;
83 return true;
84 } else if (E->getType()->isRealFloatingType()) {
85 APFloat FloatResult(0.0);
86 if (!EvaluateFloat(E, FloatResult, Info))
87 return false;
88 Result = !FloatResult.isZero();
89 return true;
Eli Friedman64004332009-03-23 04:38:34 +000090 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000091 APValue PointerResult;
92 if (!EvaluatePointer(E, PointerResult, Info))
93 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000094 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +000095 } else if (E->getType()->isAnyComplexType()) {
96 APValue ComplexResult;
97 if (!EvaluateComplex(E, ComplexResult, Info))
98 return false;
99 if (ComplexResult.isComplexFloat()) {
100 Result = !ComplexResult.getComplexFloatReal().isZero() ||
101 !ComplexResult.getComplexFloatImag().isZero();
102 } else {
103 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
104 ComplexResult.getComplexIntImag().getBoolValue();
105 }
106 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000107 }
108
109 return false;
110}
111
Mike Stump11289f42009-09-09 15:08:12 +0000112static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000113 APFloat &Value, ASTContext &Ctx) {
114 unsigned DestWidth = Ctx.getIntWidth(DestType);
115 // Determine whether we are converting to unsigned or signed.
116 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000118 // FIXME: Warning for overflow.
119 uint64_t Space[4];
120 bool ignored;
121 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
122 llvm::APFloat::rmTowardZero, &ignored);
123 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
124}
125
Mike Stump11289f42009-09-09 15:08:12 +0000126static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000127 APFloat &Value, ASTContext &Ctx) {
128 bool ignored;
129 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000130 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000131 APFloat::rmNearestTiesToEven, &ignored);
132 return Result;
133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000136 APSInt &Value, ASTContext &Ctx) {
137 unsigned DestWidth = Ctx.getIntWidth(DestType);
138 APSInt Result = Value;
139 // Figure out if this is a truncate, extend or noop cast.
140 // If the input is signed, do a sign extend, noop, or truncate.
141 Result.extOrTrunc(DestWidth);
142 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
143 return Result;
144}
145
Mike Stump11289f42009-09-09 15:08:12 +0000146static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000147 APSInt &Value, ASTContext &Ctx) {
148
149 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
150 Result.convertFromAPInt(Value, Value.isSigned(),
151 APFloat::rmNearestTiesToEven);
152 return Result;
153}
154
Mike Stump876387b2009-10-27 22:09:17 +0000155namespace {
156class VISIBILITY_HIDDEN HasSideEffect
157 : public StmtVisitor<HasSideEffect, bool> {
158 EvalInfo &Info;
159public:
160
161 HasSideEffect(EvalInfo &info) : Info(info) {}
162
163 // Unhandled nodes conservatively default to having side effects.
164 bool VisitStmt(Stmt *S) {
165 return true;
166 }
167
168 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
169 bool VisitDeclRefExpr(DeclRefExpr *E) {
170 if (E->getType().isVolatileQualified())
171 return true;
172 return false;
173 }
174 // We don't want to evaluate BlockExprs multiple times, as they generate
175 // a ton of code.
176 bool VisitBlockExpr(BlockExpr *E) { return true; }
177 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
178 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
179 { return Visit(E->getInitializer()); }
180 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
181 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
182 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
183 bool VisitStringLiteral(StringLiteral *E) { return false; }
184 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
185 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
186 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
187 { return Visit(E->getLHS()) && Visit(E->getRHS()); }
188 bool VisitChooseExpr(ChooseExpr *E)
189 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
190 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
191 bool VisitBinAssign(BinaryOperator *E) { return true; }
192 bool VisitCompoundAssign(BinaryOperator *E) { return true; }
193 bool VisitBinaryOperator(BinaryOperator *E) { return false; }
194 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
195 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryDeref(UnaryOperator *E) {
199 if (E->getType().isVolatileQualified())
200 return true;
201 return false;
202 }
203 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
204};
205
206bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
207 Expr::EvalResult Result;
208 EvalInfo Info(Ctx, Result);
209
210 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
211}
212
213} // end anonymous namespace
214
Eli Friedman9a156e52008-11-12 09:44:48 +0000215//===----------------------------------------------------------------------===//
216// LValue Evaluation
217//===----------------------------------------------------------------------===//
218namespace {
219class VISIBILITY_HIDDEN LValueExprEvaluator
220 : public StmtVisitor<LValueExprEvaluator, APValue> {
221 EvalInfo &Info;
222public:
Mike Stump11289f42009-09-09 15:08:12 +0000223
Eli Friedman9a156e52008-11-12 09:44:48 +0000224 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
225
226 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000227 return APValue();
228 }
229
230 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000231 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000232 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000233 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
234 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
235 APValue VisitMemberExpr(MemberExpr *E);
236 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000237 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000238 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000239 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000240 APValue VisitUnaryExtension(const UnaryOperator *E)
241 { return Visit(E->getSubExpr()); }
242 APValue VisitChooseExpr(const ChooseExpr *E)
243 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000244
245 APValue VisitCastExpr(CastExpr *E) {
246 switch (E->getCastKind()) {
247 default:
248 return APValue();
249
250 case CastExpr::CK_NoOp:
251 return Visit(E->getSubExpr());
252 }
253 }
Eli Friedman449fe542009-03-23 04:56:01 +0000254 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000255};
256} // end anonymous namespace
257
258static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
259 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
260 return Result.isLValue();
261}
262
Mike Stump11289f42009-09-09 15:08:12 +0000263APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000264 if (isa<FunctionDecl>(E->getDecl())) {
265 return APValue(E, 0);
266 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000267 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000268 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000269 if (!VD->getType()->isReferenceType())
270 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000271 // FIXME: Check whether VD might be overridden!
Eli Friedman751aa72b72009-05-27 06:04:58 +0000272 if (VD->getInit())
273 return Visit(VD->getInit());
274 }
275
276 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000277}
278
Mike Stump11289f42009-09-09 15:08:12 +0000279APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000280 if (E->hasBlockDeclRefExprs())
281 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000282
Steve Naroffa0c32702009-04-16 19:02:57 +0000283 return APValue(E, 0);
284}
285
Eli Friedman9a156e52008-11-12 09:44:48 +0000286APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000287 if (!Info.AnyLValue && !E->isFileScope())
288 return APValue();
289 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000290}
291
292APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
293 APValue result;
294 QualType Ty;
295 if (E->isArrow()) {
296 if (!EvaluatePointer(E->getBase(), result, Info))
297 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000298 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000299 } else {
300 result = Visit(E->getBase());
301 if (result.isUninit())
302 return APValue();
303 Ty = E->getBase()->getType();
304 }
305
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000306 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000307 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000308
309 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
310 if (!FD) // FIXME: deal with other kinds of member expressions
311 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000312
313 if (FD->getType()->isReferenceType())
314 return APValue();
315
Eli Friedman9a156e52008-11-12 09:44:48 +0000316 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000317 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000318 for (RecordDecl::field_iterator Field = RD->field_begin(),
319 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000320 Field != FieldEnd; (void)++Field, ++i) {
321 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000322 break;
323 }
324
325 result.setLValue(result.getLValueBase(),
326 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
327
328 return result;
329}
330
Mike Stump11289f42009-09-09 15:08:12 +0000331APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000332 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000334 if (!EvaluatePointer(E->getBase(), Result, Info))
335 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000336
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000337 APSInt Index;
338 if (!EvaluateInteger(E->getIdx(), Index, Info))
339 return APValue();
340
341 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
342
343 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000344 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000345 Result.getLValueOffset() + Offset);
346 return Result;
347}
Eli Friedman9a156e52008-11-12 09:44:48 +0000348
Mike Stump11289f42009-09-09 15:08:12 +0000349APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000350 APValue Result;
351 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
352 return APValue();
353 return Result;
354}
355
Eli Friedman9a156e52008-11-12 09:44:48 +0000356//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000357// Pointer Evaluation
358//===----------------------------------------------------------------------===//
359
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000360namespace {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000361class VISIBILITY_HIDDEN PointerExprEvaluator
362 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000363 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000364public:
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattnercdf34e72008-07-11 22:52:41 +0000366 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000367
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000368 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000369 return APValue();
370 }
371
372 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
373
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000374 APValue VisitBinaryOperator(const BinaryOperator *E);
375 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000376 APValue VisitUnaryExtension(const UnaryOperator *E)
377 { return Visit(E->getSubExpr()); }
378 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000379 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
380 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000381 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
382 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000383 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000384 APValue VisitBlockExpr(BlockExpr *E) {
385 if (!E->hasBlockDeclRefExprs())
386 return APValue(E, 0);
387 return APValue();
388 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000389 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
390 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000391 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000392 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000393 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
394 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
395 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000396 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000397};
Chris Lattner05706e882008-07-11 18:11:29 +0000398} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000399
Chris Lattnercdf34e72008-07-11 22:52:41 +0000400static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000401 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000402 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000403 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000404 return Result.isLValue();
405}
406
407APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
408 if (E->getOpcode() != BinaryOperator::Add &&
409 E->getOpcode() != BinaryOperator::Sub)
410 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner05706e882008-07-11 18:11:29 +0000412 const Expr *PExp = E->getLHS();
413 const Expr *IExp = E->getRHS();
414 if (IExp->getType()->isPointerType())
415 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner05706e882008-07-11 18:11:29 +0000417 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000418 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000419 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattner05706e882008-07-11 18:11:29 +0000421 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000422 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000423 return APValue();
424
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000425 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000426 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlssonef56fba2009-02-19 04:55:58 +0000428 // Explicitly handle GNU void* and function pointer arithmetic extensions.
429 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
430 SizeOfPointee = 1;
431 else
432 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000433
Chris Lattner05706e882008-07-11 18:11:29 +0000434 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000435
Chris Lattner05706e882008-07-11 18:11:29 +0000436 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000437 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000438 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000439 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
440
Chris Lattner05706e882008-07-11 18:11:29 +0000441 return APValue(ResultLValue.getLValueBase(), Offset);
442}
Eli Friedman9a156e52008-11-12 09:44:48 +0000443
Eli Friedmanc2b50172009-02-22 11:46:18 +0000444APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
445 APValue result;
446 if (EvaluateLValue(E->getSubExpr(), result, Info))
447 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000448 return APValue();
449}
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattner05706e882008-07-11 18:11:29 +0000451
Chris Lattnere13042c2008-07-11 19:10:17 +0000452APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000453 const Expr* SubExpr = E->getSubExpr();
454
455 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000456 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000457 SubExpr->getType()->isObjCObjectPointerType() ||
458 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000459 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000460 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000461 return Result;
462 return APValue();
463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Eli Friedmanbd840592008-07-27 05:46:18 +0000465 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000466 APValue Result;
467 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
468 return APValue();
469
470 if (Result.isInt()) {
471 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
472 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Daniel Dunbarce399542009-02-20 18:22:23 +0000475 // Cast is of an lvalue, no need to change value.
476 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000477 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000478
479 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000480 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000481 SubExpr->getType()->isArrayType()) {
482 APValue Result;
483 if (EvaluateLValue(SubExpr, Result, Info))
484 return Result;
485 return APValue();
486 }
487
Chris Lattner05706e882008-07-11 18:11:29 +0000488 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000489}
Chris Lattner05706e882008-07-11 18:11:29 +0000490
Eli Friedmanc69d4542009-01-25 01:54:01 +0000491APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000492 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000493 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000494 return APValue(E, 0);
495 return APValue();
496}
497
Eli Friedman9a156e52008-11-12 09:44:48 +0000498APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
499 bool BoolResult;
500 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
501 return APValue();
502
503 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
504
505 APValue Result;
506 if (EvaluatePointer(EvalExpr, Result, Info))
507 return Result;
508 return APValue();
509}
Chris Lattner05706e882008-07-11 18:11:29 +0000510
511//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000512// Vector Evaluation
513//===----------------------------------------------------------------------===//
514
515namespace {
516 class VISIBILITY_HIDDEN VectorExprEvaluator
517 : public StmtVisitor<VectorExprEvaluator, APValue> {
518 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000519 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000520 public:
Mike Stump11289f42009-09-09 15:08:12 +0000521
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000522 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000523
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000524 APValue VisitStmt(Stmt *S) {
525 return APValue();
526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Eli Friedman3ae59112009-02-23 04:23:56 +0000528 APValue VisitParenExpr(ParenExpr *E)
529 { return Visit(E->getSubExpr()); }
530 APValue VisitUnaryExtension(const UnaryOperator *E)
531 { return Visit(E->getSubExpr()); }
532 APValue VisitUnaryPlus(const UnaryOperator *E)
533 { return Visit(E->getSubExpr()); }
534 APValue VisitUnaryReal(const UnaryOperator *E)
535 { return Visit(E->getSubExpr()); }
536 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
537 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000538 APValue VisitCastExpr(const CastExpr* E);
539 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
540 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000541 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000542 APValue VisitChooseExpr(const ChooseExpr *E)
543 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000544 APValue VisitUnaryImag(const UnaryOperator *E);
545 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000546 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000547 // shufflevector, ExtVectorElementExpr
548 // (Note that these require implementing conversions
549 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000550 };
551} // end anonymous namespace
552
553static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
554 if (!E->getType()->isVectorType())
555 return false;
556 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
557 return !Result.isUninit();
558}
559
560APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000561 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000562 QualType EltTy = VTy->getElementType();
563 unsigned NElts = VTy->getNumElements();
564 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000565
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000566 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000567 QualType SETy = SE->getType();
568 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000569
Nate Begeman2ffd3842009-06-26 18:22:18 +0000570 // Check for vector->vector bitcast and scalar->vector splat.
571 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000572 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000573 } else if (SETy->isIntegerType()) {
574 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000575 if (!EvaluateInteger(SE, IntResult, Info))
576 return APValue();
577 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000578 } else if (SETy->isRealFloatingType()) {
579 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000580 if (!EvaluateFloat(SE, F, Info))
581 return APValue();
582 Result = APValue(F);
583 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000584 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000585
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000586 // For casts of a scalar to ExtVector, convert the scalar to the element type
587 // and splat it to all elements.
588 if (E->getType()->isExtVectorType()) {
589 if (EltTy->isIntegerType() && Result.isInt())
590 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
591 Info.Ctx));
592 else if (EltTy->isIntegerType())
593 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
594 Info.Ctx));
595 else if (EltTy->isRealFloatingType() && Result.isInt())
596 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
597 Info.Ctx));
598 else if (EltTy->isRealFloatingType())
599 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
600 Info.Ctx));
601 else
602 return APValue();
603
604 // Splat and create vector APValue.
605 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
606 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000607 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000608
609 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
610 // to the vector. To construct the APValue vector initializer, bitcast the
611 // initializing value to an APInt, and shift out the bits pertaining to each
612 // element.
613 APSInt Init;
614 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000615
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000616 llvm::SmallVector<APValue, 4> Elts;
617 for (unsigned i = 0; i != NElts; ++i) {
618 APSInt Tmp = Init;
619 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000621 if (EltTy->isIntegerType())
622 Elts.push_back(APValue(Tmp));
623 else if (EltTy->isRealFloatingType())
624 Elts.push_back(APValue(APFloat(Tmp)));
625 else
626 return APValue();
627
628 Init >>= EltWidth;
629 }
630 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000631}
632
Mike Stump11289f42009-09-09 15:08:12 +0000633APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000634VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
635 return this->Visit(const_cast<Expr*>(E->getInitializer()));
636}
637
Mike Stump11289f42009-09-09 15:08:12 +0000638APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000639VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000640 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000641 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000642 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000643
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000644 QualType EltTy = VT->getElementType();
645 llvm::SmallVector<APValue, 4> Elements;
646
Eli Friedman3ae59112009-02-23 04:23:56 +0000647 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000648 if (EltTy->isIntegerType()) {
649 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000650 if (i < NumInits) {
651 if (!EvaluateInteger(E->getInit(i), sInt, Info))
652 return APValue();
653 } else {
654 sInt = Info.Ctx.MakeIntValue(0, EltTy);
655 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000656 Elements.push_back(APValue(sInt));
657 } else {
658 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000659 if (i < NumInits) {
660 if (!EvaluateFloat(E->getInit(i), f, Info))
661 return APValue();
662 } else {
663 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
664 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000665 Elements.push_back(APValue(f));
666 }
667 }
668 return APValue(&Elements[0], Elements.size());
669}
670
Mike Stump11289f42009-09-09 15:08:12 +0000671APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000672VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000673 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000674 QualType EltTy = VT->getElementType();
675 APValue ZeroElement;
676 if (EltTy->isIntegerType())
677 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
678 else
679 ZeroElement =
680 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
681
682 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
683 return APValue(&Elements[0], Elements.size());
684}
685
686APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
687 bool BoolResult;
688 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
689 return APValue();
690
691 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
692
693 APValue Result;
694 if (EvaluateVector(EvalExpr, Result, Info))
695 return Result;
696 return APValue();
697}
698
Eli Friedman3ae59112009-02-23 04:23:56 +0000699APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
700 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
701 Info.EvalResult.HasSideEffects = true;
702 return GetZeroVector(E->getType());
703}
704
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000705//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000706// Integer Evaluation
707//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000708
709namespace {
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000710class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000711 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000712 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000713 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000714public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000715 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000716 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000717
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000718 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000719 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000720 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000721 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000722 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000723 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000724 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000725 return true;
726 }
727
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000728 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000729 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000730 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000731 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000732 Result = APValue(APSInt(I));
733 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000734 return true;
735 }
736
737 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000738 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000739 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000740 return true;
741 }
742
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000743 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000744 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000745 if (Info.EvalResult.Diag == 0) {
746 Info.EvalResult.DiagLoc = L;
747 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000748 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000749 }
Chris Lattner99415702008-07-12 00:14:42 +0000750 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000751 }
Mike Stump11289f42009-09-09 15:08:12 +0000752
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000753 //===--------------------------------------------------------------------===//
754 // Visitor Methods
755 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000757 bool VisitStmt(Stmt *) {
758 assert(0 && "This should be called on integers, stmts are not integers");
759 return false;
760 }
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000762 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000763 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattnere13042c2008-07-11 19:10:17 +0000766 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000767
Chris Lattner7174bf32008-07-12 00:38:25 +0000768 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000769 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000770 }
771 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000772 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000773 }
774 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000775 // Per gcc docs "this built-in function ignores top level
776 // qualifiers". We need to use the canonical version to properly
777 // be able to strip CRV qualifiers from the type.
778 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
779 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000780 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000781 T1.getUnqualifiedType()),
782 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000783 }
784 bool VisitDeclRefExpr(const DeclRefExpr *E);
785 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000786 bool VisitBinaryOperator(const BinaryOperator *E);
787 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000788 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000789
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000790 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000791 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
792
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000793 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000794 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Anders Carlsson39def3a2008-12-21 22:39:40 +0000797 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000798 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000801 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000802 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000803 }
804
Eli Friedman4e7a2412009-02-27 04:45:43 +0000805 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
806 return Success(0, E);
807 }
808
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000809 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000810 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000811 }
812
Eli Friedman449fe542009-03-23 04:56:01 +0000813 bool VisitChooseExpr(const ChooseExpr *E) {
814 return Visit(E->getChosenSubExpr(Info.Ctx));
815 }
816
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000817 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000818 bool VisitUnaryImag(const UnaryOperator *E);
819
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000820private:
Chris Lattner68061312009-01-24 21:53:27 +0000821 unsigned GetAlignOfExpr(const Expr *E);
822 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000823 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000824};
Chris Lattner05706e882008-07-11 18:11:29 +0000825} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000826
Daniel Dunbarce399542009-02-20 18:22:23 +0000827static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000828 if (!E->getType()->isIntegralType())
829 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Daniel Dunbarce399542009-02-20 18:22:23 +0000831 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
832}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000833
Daniel Dunbarce399542009-02-20 18:22:23 +0000834static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
835 APValue Val;
836 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
837 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000838 Result = Val.getInt();
839 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000840}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000841
Chris Lattner7174bf32008-07-12 00:38:25 +0000842bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
843 // Enums are integer constant exprs.
844 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000845 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000846 // signedness consistently; see PR3173.
847 APSInt SI = D->getInitVal();
848 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
849 // FIXME: This is an ugly hack around the fact that enums don't
850 // set their width (!?!) consistently; see PR3173.
851 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
852 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000853 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000854
855 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000856 // In C, they can also be folded, although they are not ICEs.
John McCall8ccfcb52009-09-24 19:53:00 +0000857 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000858 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000859 if (APValue *V = D->getEvaluatedValue())
860 return Success(V->getInt(), E);
861 if (const Expr *Init = D->getInit()) {
862 if (Visit(const_cast<Expr*>(Init))) {
863 // Cache the evaluated value in the variable declaration.
864 D->setEvaluatedValue(Info.Ctx, Result);
865 return true;
866 }
867
868 return false;
869 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000870 }
871 }
872
Chris Lattner7174bf32008-07-12 00:38:25 +0000873 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000874 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000875}
876
Chris Lattner86ee2862008-10-06 06:40:35 +0000877/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
878/// as GCC.
879static int EvaluateBuiltinClassifyType(const CallExpr *E) {
880 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000881 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000882 enum gcc_type_class {
883 no_type_class = -1,
884 void_type_class, integer_type_class, char_type_class,
885 enumeral_type_class, boolean_type_class,
886 pointer_type_class, reference_type_class, offset_type_class,
887 real_type_class, complex_type_class,
888 function_type_class, method_type_class,
889 record_type_class, union_type_class,
890 array_type_class, string_type_class,
891 lang_type_class
892 };
Mike Stump11289f42009-09-09 15:08:12 +0000893
894 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000895 // ideal, however it is what gcc does.
896 if (E->getNumArgs() == 0)
897 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000898
Chris Lattner86ee2862008-10-06 06:40:35 +0000899 QualType ArgTy = E->getArg(0)->getType();
900 if (ArgTy->isVoidType())
901 return void_type_class;
902 else if (ArgTy->isEnumeralType())
903 return enumeral_type_class;
904 else if (ArgTy->isBooleanType())
905 return boolean_type_class;
906 else if (ArgTy->isCharType())
907 return string_type_class; // gcc doesn't appear to use char_type_class
908 else if (ArgTy->isIntegerType())
909 return integer_type_class;
910 else if (ArgTy->isPointerType())
911 return pointer_type_class;
912 else if (ArgTy->isReferenceType())
913 return reference_type_class;
914 else if (ArgTy->isRealType())
915 return real_type_class;
916 else if (ArgTy->isComplexType())
917 return complex_type_class;
918 else if (ArgTy->isFunctionType())
919 return function_type_class;
920 else if (ArgTy->isStructureType())
921 return record_type_class;
922 else if (ArgTy->isUnionType())
923 return union_type_class;
924 else if (ArgTy->isArrayType())
925 return array_type_class;
926 else if (ArgTy->isUnionType())
927 return union_type_class;
928 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
929 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
930 return -1;
931}
932
Chris Lattner7174bf32008-07-12 00:38:25 +0000933bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000934 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000935 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000936 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000937
938 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000939 const Expr *Arg = E->getArg(0)->IgnoreParens();
940 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000941 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000942 && Base.Val.getKind() == APValue::LValue
943 && !Base.HasSideEffects)
944 if (const Expr *LVBase = Base.Val.getLValueBase())
945 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
946 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000947 if (!VD->getType()->isIncompleteType()
948 && VD->getType()->isObjectType()
949 && !VD->getType()->isVariablyModifiedType()
950 && !VD->getType()->isDependentType()) {
951 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
952 uint64_t Offset = Base.Val.getLValueOffset();
953 if (Offset <= Size)
954 Size -= Base.Val.getLValueOffset();
955 else
956 Size = 0;
957 return Success(Size, E);
958 }
Mike Stump722cedf2009-10-26 18:35:08 +0000959 }
960 }
961
Mike Stump876387b2009-10-27 22:09:17 +0000962 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000963 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump722cedf2009-10-26 18:35:08 +0000964 return Success(-1, E);
965 return Success(0, E);
966 }
Mike Stump876387b2009-10-27 22:09:17 +0000967
Mike Stump722cedf2009-10-26 18:35:08 +0000968 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
969 }
970
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000971 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000972 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000973
Anders Carlsson4c76e932008-11-24 04:21:33 +0000974 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000975 // __builtin_constant_p always has one operand: it returns true if that
976 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000977 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000978
979 case Builtin::BI__builtin_eh_return_data_regno: {
980 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
981 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
982 return Success(Operand, E);
983 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000984 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000985}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000986
Chris Lattnere13042c2008-07-11 19:10:17 +0000987bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000988 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +0000989 if (!Visit(E->getRHS()))
990 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +0000991
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000992 // If we can't evaluate the LHS, it might have side effects;
993 // conservatively mark it.
994 if (!E->getLHS()->isEvaluatable(Info.Ctx))
995 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000996
Anders Carlsson564730a2008-12-01 02:07:06 +0000997 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000998 }
999
1000 if (E->isLogicalOp()) {
1001 // These need to be handled specially because the operands aren't
1002 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001003 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001004
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001005 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001006 // We were able to evaluate the LHS, see if we can get away with not
1007 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001008 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001009 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001010
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001011 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001012 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001013 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001014 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001015 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001016 }
1017 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001018 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001019 // We can't evaluate the LHS; however, sometimes the result
1020 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001021 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001022 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001023 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001024 // must have had side effects.
1025 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001026
1027 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001028 }
1029 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001030 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001031
Eli Friedman5a332ea2008-11-13 06:09:17 +00001032 return false;
1033 }
1034
Anders Carlssonacc79812008-11-16 07:17:21 +00001035 QualType LHSTy = E->getLHS()->getType();
1036 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001037
1038 if (LHSTy->isAnyComplexType()) {
1039 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1040 APValue LHS, RHS;
1041
1042 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1043 return false;
1044
1045 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1046 return false;
1047
1048 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001049 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001050 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001051 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001052 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1053
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001054 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001055 return Success((CR_r == APFloat::cmpEqual &&
1056 CR_i == APFloat::cmpEqual), E);
1057 else {
1058 assert(E->getOpcode() == BinaryOperator::NE &&
1059 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001060 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001061 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001062 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001063 CR_i == APFloat::cmpLessThan)), E);
1064 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001065 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001066 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001067 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1068 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1069 else {
1070 assert(E->getOpcode() == BinaryOperator::NE &&
1071 "Invalid compex comparison.");
1072 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1073 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1074 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001075 }
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Anders Carlssonacc79812008-11-16 07:17:21 +00001078 if (LHSTy->isRealFloatingType() &&
1079 RHSTy->isRealFloatingType()) {
1080 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001081
Anders Carlssonacc79812008-11-16 07:17:21 +00001082 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1083 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001084
Anders Carlssonacc79812008-11-16 07:17:21 +00001085 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1086 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Anders Carlssonacc79812008-11-16 07:17:21 +00001088 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001089
Anders Carlssonacc79812008-11-16 07:17:21 +00001090 switch (E->getOpcode()) {
1091 default:
1092 assert(0 && "Invalid binary operator!");
1093 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001094 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001095 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001096 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001097 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001098 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001099 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001100 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001101 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001102 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001103 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001104 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001105 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001106 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001107 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001108 }
Mike Stump11289f42009-09-09 15:08:12 +00001109
Eli Friedmana38da572009-04-28 19:17:36 +00001110 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1111 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001112 APValue LHSValue;
1113 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1114 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001115
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001116 APValue RHSValue;
1117 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1118 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001119
Eli Friedman334046a2009-06-14 02:17:33 +00001120 // Reject any bases from the normal codepath; we special-case comparisons
1121 // to null.
1122 if (LHSValue.getLValueBase()) {
1123 if (!E->isEqualityOp())
1124 return false;
1125 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1126 return false;
1127 bool bres;
1128 if (!EvalPointerValueAsBool(LHSValue, bres))
1129 return false;
1130 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1131 } else if (RHSValue.getLValueBase()) {
1132 if (!E->isEqualityOp())
1133 return false;
1134 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1135 return false;
1136 bool bres;
1137 if (!EvalPointerValueAsBool(RHSValue, bres))
1138 return false;
1139 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1140 }
Eli Friedman64004332009-03-23 04:38:34 +00001141
Eli Friedmana38da572009-04-28 19:17:36 +00001142 if (E->getOpcode() == BinaryOperator::Sub) {
1143 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001144 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001145
Eli Friedmana38da572009-04-28 19:17:36 +00001146 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001147 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1148 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001149
Eli Friedmana38da572009-04-28 19:17:36 +00001150 return Success(D, E);
1151 }
1152 bool Result;
1153 if (E->getOpcode() == BinaryOperator::EQ) {
1154 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001155 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001156 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1157 }
1158 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001159 }
1160 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001161 if (!LHSTy->isIntegralType() ||
1162 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001163 // We can't continue from here for non-integral types, and they
1164 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001165 return false;
1166 }
1167
Anders Carlsson9c181652008-07-08 14:35:21 +00001168 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001169 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001170 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001171
Eli Friedman94c25c62009-03-24 01:14:50 +00001172 APValue RHSVal;
1173 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001174 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001175
1176 // Handle cases like (unsigned long)&a + 4.
1177 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1178 uint64_t offset = Result.getLValueOffset();
1179 if (E->getOpcode() == BinaryOperator::Add)
1180 offset += RHSVal.getInt().getZExtValue();
1181 else
1182 offset -= RHSVal.getInt().getZExtValue();
1183 Result = APValue(Result.getLValueBase(), offset);
1184 return true;
1185 }
1186
1187 // Handle cases like 4 + (unsigned long)&a
1188 if (E->getOpcode() == BinaryOperator::Add &&
1189 RHSVal.isLValue() && Result.isInt()) {
1190 uint64_t offset = RHSVal.getLValueOffset();
1191 offset += Result.getInt().getZExtValue();
1192 Result = APValue(RHSVal.getLValueBase(), offset);
1193 return true;
1194 }
1195
1196 // All the following cases expect both operands to be an integer
1197 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001198 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001199
Eli Friedman94c25c62009-03-24 01:14:50 +00001200 APSInt& RHS = RHSVal.getInt();
1201
Anders Carlsson9c181652008-07-08 14:35:21 +00001202 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001203 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001204 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001205 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1206 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1207 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1208 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1209 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1210 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001211 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001212 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001213 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001214 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001215 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001216 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001217 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001218 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001219 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001220 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001221 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001222 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1223 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001224 }
1225 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001226 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001227 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1228 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001231 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1232 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1233 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1234 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1235 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1236 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001237 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001238}
1239
Nuno Lopes42042612008-11-16 19:28:31 +00001240bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001241 bool Cond;
1242 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001243 return false;
1244
Nuno Lopes527b5a62008-11-16 22:06:39 +00001245 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001246}
1247
Chris Lattner68061312009-01-24 21:53:27 +00001248unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001249 // Get information about the alignment.
1250 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001251
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001252 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001253 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001254}
1255
Chris Lattner68061312009-01-24 21:53:27 +00001256unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1257 E = E->IgnoreParens();
1258
1259 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001260 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001261 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001262 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedman64004332009-03-23 04:38:34 +00001263
Chris Lattner68061312009-01-24 21:53:27 +00001264 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001265 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattner68061312009-01-24 21:53:27 +00001266
Chris Lattner24aeeab2009-01-24 21:09:06 +00001267 return GetAlignOfType(E->getType());
1268}
1269
1270
Sebastian Redl6f282892008-11-11 17:56:53 +00001271/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1272/// expression's type.
1273bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1274 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001275
Chris Lattner24aeeab2009-01-24 21:09:06 +00001276 // Handle alignof separately.
1277 if (!E->isSizeOf()) {
1278 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001279 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001280 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001281 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001282 }
Eli Friedman64004332009-03-23 04:38:34 +00001283
Sebastian Redl6f282892008-11-11 17:56:53 +00001284 QualType SrcTy = E->getTypeOfArgument();
1285
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001286 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1287 // extension.
1288 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1289 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001290
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001291 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001292 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001293 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001294
Chris Lattner24aeeab2009-01-24 21:09:06 +00001295 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001296 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001297 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001298}
1299
Chris Lattnere13042c2008-07-11 19:10:17 +00001300bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001301 // Special case unary operators that do not need their subexpression
1302 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001303 if (E->isOffsetOfOp()) {
1304 // The AST for offsetof is defined in such a way that we can just
1305 // directly Evaluate it as an l-value.
1306 APValue LV;
1307 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1308 return false;
1309 if (LV.getLValueBase())
1310 return false;
1311 return Success(LV.getLValueOffset(), E);
1312 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001313
1314 if (E->getOpcode() == UnaryOperator::LNot) {
1315 // LNot's operand isn't necessarily an integer, so we handle it specially.
1316 bool bres;
1317 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1318 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001319 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001320 }
1321
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001322 // Only handle integral operations...
1323 if (!E->getSubExpr()->getType()->isIntegralType())
1324 return false;
1325
Chris Lattnercdf34e72008-07-11 22:52:41 +00001326 // Get the operand value into 'Result'.
1327 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001328 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001329
Chris Lattnerf09ad162008-07-11 22:15:16 +00001330 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001331 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001332 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1333 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001334 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001335 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001336 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1337 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001338 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001339 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001340 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001341 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001342 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001343 if (!Result.isInt()) return false;
1344 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001345 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001346 if (!Result.isInt()) return false;
1347 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001348 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001349}
Mike Stump11289f42009-09-09 15:08:12 +00001350
Chris Lattner477c4be2008-07-12 01:15:53 +00001351/// HandleCast - This is used to evaluate implicit or explicit casts where the
1352/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001353bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001354 Expr *SubExpr = E->getSubExpr();
1355 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001356 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001357
Eli Friedman9a156e52008-11-12 09:44:48 +00001358 if (DestType->isBooleanType()) {
1359 bool BoolResult;
1360 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1361 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001362 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001363 }
1364
Anders Carlsson9c181652008-07-08 14:35:21 +00001365 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001366 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001367 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001368 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001369
Eli Friedman742421e2009-02-20 01:15:07 +00001370 if (!Result.isInt()) {
1371 // Only allow casts of lvalues if they are lossless.
1372 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1373 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001374
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001375 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001376 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattner477c4be2008-07-12 01:15:53 +00001379 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001380 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001381 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001382 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001383 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001384
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001385 if (LV.getLValueBase()) {
1386 // Only allow based lvalue casts if they are lossless.
1387 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1388 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001389
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001390 Result = LV;
1391 return true;
1392 }
1393
1394 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1395 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001396 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001397
Eli Friedman742421e2009-02-20 01:15:07 +00001398 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1399 // This handles double-conversion cases, where there's both
1400 // an l-value promotion and an implicit conversion to int.
1401 APValue LV;
1402 if (!EvaluateLValue(SubExpr, LV, Info))
1403 return false;
1404
1405 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1406 return false;
1407
1408 Result = LV;
1409 return true;
1410 }
1411
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001412 if (SrcType->isAnyComplexType()) {
1413 APValue C;
1414 if (!EvaluateComplex(SubExpr, C, Info))
1415 return false;
1416 if (C.isComplexFloat())
1417 return Success(HandleFloatToIntCast(DestType, SrcType,
1418 C.getComplexFloatReal(), Info.Ctx),
1419 E);
1420 else
1421 return Success(HandleIntToIntCast(DestType, SrcType,
1422 C.getComplexIntReal(), Info.Ctx), E);
1423 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001424 // FIXME: Handle vectors
1425
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001426 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001427 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001428
Eli Friedman24c01542008-08-22 00:06:13 +00001429 APFloat F(0.0);
1430 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001431 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001432
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001433 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001434}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001435
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001436bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1437 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1438 APValue LV;
1439 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1440 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1441 return Success(LV.getComplexIntReal(), E);
1442 }
1443
1444 return Visit(E->getSubExpr());
1445}
1446
Eli Friedman4e7a2412009-02-27 04:45:43 +00001447bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001448 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1449 APValue LV;
1450 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1451 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1452 return Success(LV.getComplexIntImag(), E);
1453 }
1454
Eli Friedman4e7a2412009-02-27 04:45:43 +00001455 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1456 Info.EvalResult.HasSideEffects = true;
1457 return Success(0, E);
1458}
1459
Chris Lattner05706e882008-07-11 18:11:29 +00001460//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001461// Float Evaluation
1462//===----------------------------------------------------------------------===//
1463
1464namespace {
1465class VISIBILITY_HIDDEN FloatExprEvaluator
1466 : public StmtVisitor<FloatExprEvaluator, bool> {
1467 EvalInfo &Info;
1468 APFloat &Result;
1469public:
1470 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1471 : Info(info), Result(result) {}
1472
1473 bool VisitStmt(Stmt *S) {
1474 return false;
1475 }
1476
1477 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001478 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001479
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001480 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001481 bool VisitBinaryOperator(const BinaryOperator *E);
1482 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001483 bool VisitCastExpr(CastExpr *E);
1484 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001485
Eli Friedman449fe542009-03-23 04:56:01 +00001486 bool VisitChooseExpr(const ChooseExpr *E)
1487 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1488 bool VisitUnaryExtension(const UnaryOperator *E)
1489 { return Visit(E->getSubExpr()); }
1490
1491 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1492 // member of vector, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001493 // conditional ?:, comma
Eli Friedman24c01542008-08-22 00:06:13 +00001494};
1495} // end anonymous namespace
1496
1497static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1498 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1499}
1500
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001501bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001502 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001503 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001504 case Builtin::BI__builtin_huge_val:
1505 case Builtin::BI__builtin_huge_valf:
1506 case Builtin::BI__builtin_huge_vall:
1507 case Builtin::BI__builtin_inf:
1508 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001509 case Builtin::BI__builtin_infl: {
1510 const llvm::fltSemantics &Sem =
1511 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001512 Result = llvm::APFloat::getInf(Sem);
1513 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001514 }
Mike Stump11289f42009-09-09 15:08:12 +00001515
Chris Lattner0b7282e2008-10-06 06:31:58 +00001516 case Builtin::BI__builtin_nan:
1517 case Builtin::BI__builtin_nanf:
1518 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001519 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001520 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001521 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001522 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001523 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001524 const llvm::fltSemantics &Sem =
1525 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001526 llvm::SmallString<16> s;
1527 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1528 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001529 long l;
1530 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001531 l = strtol(&s[0], &endp, 0);
1532 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001533 return false;
1534 unsigned type = (unsigned int)l;;
1535 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001536 return true;
1537 }
1538 }
1539 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001540
1541 case Builtin::BI__builtin_fabs:
1542 case Builtin::BI__builtin_fabsf:
1543 case Builtin::BI__builtin_fabsl:
1544 if (!EvaluateFloat(E->getArg(0), Result, Info))
1545 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001547 if (Result.isNegative())
1548 Result.changeSign();
1549 return true;
1550
Mike Stump11289f42009-09-09 15:08:12 +00001551 case Builtin::BI__builtin_copysign:
1552 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001553 case Builtin::BI__builtin_copysignl: {
1554 APFloat RHS(0.);
1555 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1556 !EvaluateFloat(E->getArg(1), RHS, Info))
1557 return false;
1558 Result.copySign(RHS);
1559 return true;
1560 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001561 }
1562}
1563
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001564bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001565 if (E->getOpcode() == UnaryOperator::Deref)
1566 return false;
1567
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001568 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1569 return false;
1570
1571 switch (E->getOpcode()) {
1572 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001573 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001574 return true;
1575 case UnaryOperator::Minus:
1576 Result.changeSign();
1577 return true;
1578 }
1579}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001580
Eli Friedman24c01542008-08-22 00:06:13 +00001581bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1582 // FIXME: Diagnostics? I really don't understand how the warnings
1583 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001584 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001585 if (!EvaluateFloat(E->getLHS(), Result, Info))
1586 return false;
1587 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1588 return false;
1589
1590 switch (E->getOpcode()) {
1591 default: return false;
1592 case BinaryOperator::Mul:
1593 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1594 return true;
1595 case BinaryOperator::Add:
1596 Result.add(RHS, APFloat::rmNearestTiesToEven);
1597 return true;
1598 case BinaryOperator::Sub:
1599 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1600 return true;
1601 case BinaryOperator::Div:
1602 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1603 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001604 }
1605}
1606
1607bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1608 Result = E->getValue();
1609 return true;
1610}
1611
Eli Friedman9a156e52008-11-12 09:44:48 +00001612bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1613 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001614
Eli Friedman9a156e52008-11-12 09:44:48 +00001615 if (SubExpr->getType()->isIntegralType()) {
1616 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001617 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001618 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001619 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001620 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001621 return true;
1622 }
1623 if (SubExpr->getType()->isRealFloatingType()) {
1624 if (!Visit(SubExpr))
1625 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001626 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1627 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001628 return true;
1629 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001630 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001631
1632 return false;
1633}
1634
1635bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1636 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1637 return true;
1638}
1639
Eli Friedman24c01542008-08-22 00:06:13 +00001640//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001641// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001642//===----------------------------------------------------------------------===//
1643
1644namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001645class VISIBILITY_HIDDEN ComplexExprEvaluator
1646 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001647 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001648
Anders Carlsson537969c2008-11-16 20:27:53 +00001649public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001650 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001651
Anders Carlsson537969c2008-11-16 20:27:53 +00001652 //===--------------------------------------------------------------------===//
1653 // Visitor Methods
1654 //===--------------------------------------------------------------------===//
1655
1656 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001657 return APValue();
1658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
Anders Carlsson537969c2008-11-16 20:27:53 +00001660 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1661
1662 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001663 Expr* SubExpr = E->getSubExpr();
1664
1665 if (SubExpr->getType()->isRealFloatingType()) {
1666 APFloat Result(0.0);
1667
1668 if (!EvaluateFloat(SubExpr, Result, Info))
1669 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001670
1671 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001672 Result);
1673 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001674 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001675 "Unexpected imaginary literal.");
1676
1677 llvm::APSInt Result;
1678 if (!EvaluateInteger(SubExpr, Result, Info))
1679 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001680
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001681 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1682 Zero = 0;
1683 return APValue(Zero, Result);
1684 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001685 }
1686
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001687 APValue VisitCastExpr(CastExpr *E) {
1688 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001689 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001690 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001691
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001692 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001693 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001694
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001695 if (!EvaluateFloat(SubExpr, Result, Info))
1696 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001697
1698 if (EltType->isRealFloatingType()) {
1699 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001700 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001701 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1702 } else {
1703 llvm::APSInt IResult;
1704 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1705 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1706 Zero = 0;
1707 return APValue(IResult, Zero);
1708 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001709 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001710 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001711
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001712 if (!EvaluateInteger(SubExpr, Result, Info))
1713 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001714
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001715 if (EltType->isRealFloatingType()) {
1716 APFloat FResult =
1717 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001718 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001719 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1720 } else {
1721 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1722 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1723 Zero = 0;
1724 return APValue(Result, Zero);
1725 }
John McCall9dd450b2009-09-21 23:43:11 +00001726 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001727 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001728
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001729 if (!EvaluateComplex(SubExpr, Src, Info))
1730 return APValue();
1731
1732 QualType SrcType = CT->getElementType();
1733
1734 if (Src.isComplexFloat()) {
1735 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001736 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001737 Src.getComplexFloatReal(),
1738 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001739 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001740 Src.getComplexFloatImag(),
1741 Info.Ctx));
1742 } else {
1743 return APValue(HandleFloatToIntCast(EltType, SrcType,
1744 Src.getComplexFloatReal(),
1745 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001746 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001747 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001748 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001749 }
1750 } else {
1751 assert(Src.isComplexInt() && "Invalid evaluate result.");
1752 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001753 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001754 Src.getComplexIntReal(),
1755 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001756 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001757 Src.getComplexIntImag(),
1758 Info.Ctx));
1759 } else {
1760 return APValue(HandleIntToIntCast(EltType, SrcType,
1761 Src.getComplexIntReal(),
1762 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001763 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001764 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001765 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001766 }
1767 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001768 }
1769
1770 // FIXME: Handle more casts.
1771 return APValue();
1772 }
Mike Stump11289f42009-09-09 15:08:12 +00001773
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001774 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001775 APValue VisitChooseExpr(const ChooseExpr *E)
1776 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1777 APValue VisitUnaryExtension(const UnaryOperator *E)
1778 { return Visit(E->getSubExpr()); }
1779 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001780 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001781};
1782} // end anonymous namespace
1783
Mike Stump11289f42009-09-09 15:08:12 +00001784static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001785 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1786 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001787 (&Result.getComplexFloatReal().getSemantics() ==
1788 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001789 "Invalid complex evaluation.");
1790 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001791}
1792
Mike Stump11289f42009-09-09 15:08:12 +00001793APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001794 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001795
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001796 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001797 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001798
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001799 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001800 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001801
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001802 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1803 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001804 switch (E->getOpcode()) {
1805 default: return APValue();
1806 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001807 if (Result.isComplexFloat()) {
1808 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1809 APFloat::rmNearestTiesToEven);
1810 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1811 APFloat::rmNearestTiesToEven);
1812 } else {
1813 Result.getComplexIntReal() += RHS.getComplexIntReal();
1814 Result.getComplexIntImag() += RHS.getComplexIntImag();
1815 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001816 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001817 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001818 if (Result.isComplexFloat()) {
1819 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1820 APFloat::rmNearestTiesToEven);
1821 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1822 APFloat::rmNearestTiesToEven);
1823 } else {
1824 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1825 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1826 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001827 break;
1828 case BinaryOperator::Mul:
1829 if (Result.isComplexFloat()) {
1830 APValue LHS = Result;
1831 APFloat &LHS_r = LHS.getComplexFloatReal();
1832 APFloat &LHS_i = LHS.getComplexFloatImag();
1833 APFloat &RHS_r = RHS.getComplexFloatReal();
1834 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001835
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001836 APFloat Tmp = LHS_r;
1837 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1838 Result.getComplexFloatReal() = Tmp;
1839 Tmp = LHS_i;
1840 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1841 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1842
1843 Tmp = LHS_r;
1844 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1845 Result.getComplexFloatImag() = Tmp;
1846 Tmp = LHS_i;
1847 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1848 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1849 } else {
1850 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001851 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001852 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1853 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001854 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001855 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1856 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1857 }
1858 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001859 }
1860
1861 return Result;
1862}
1863
Anders Carlsson537969c2008-11-16 20:27:53 +00001864//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001865// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001866//===----------------------------------------------------------------------===//
1867
Chris Lattner67d7b922008-11-16 21:24:15 +00001868/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001869/// any crazy technique (that has nothing to do with language standards) that
1870/// we want to. If this function returns true, it returns the folded constant
1871/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001872bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1873 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001874
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001875 if (getType()->isVectorType()) {
1876 if (!EvaluateVector(this, Result.Val, Info))
1877 return false;
1878 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001879 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001880 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001881 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001882 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001883 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001884 } else if (getType()->isRealFloatingType()) {
1885 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001886 if (!EvaluateFloat(this, f, Info))
1887 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001888
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001889 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001890 } else if (getType()->isAnyComplexType()) {
1891 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001892 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001893 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001894 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001895
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001896 return true;
1897}
1898
Mike Stump5183a142009-10-26 23:05:19 +00001899bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1900 EvalInfo Info(Ctx, Result, true);
1901
1902 if (getType()->isVectorType()) {
1903 if (!EvaluateVector(this, Result.Val, Info))
1904 return false;
1905 } else if (getType()->isIntegerType()) {
1906 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1907 return false;
1908 } else if (getType()->hasPointerRepresentation()) {
1909 if (!EvaluatePointer(this, Result.Val, Info))
1910 return false;
1911 } else if (getType()->isRealFloatingType()) {
1912 llvm::APFloat f(0.0);
1913 if (!EvaluateFloat(this, f, Info))
1914 return false;
1915
1916 Result.Val = APValue(f);
1917 } else if (getType()->isAnyComplexType()) {
1918 if (!EvaluateComplex(this, Result.Val, Info))
1919 return false;
1920 } else
1921 return false;
1922
1923 return true;
1924}
1925
Anders Carlsson43168122009-04-10 04:54:13 +00001926bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1927 EvalInfo Info(Ctx, Result);
1928
1929 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1930}
1931
Eli Friedman7d45c482009-09-13 10:17:44 +00001932bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1933 EvalInfo Info(Ctx, Result, true);
1934
1935 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1936}
1937
Chris Lattner67d7b922008-11-16 21:24:15 +00001938/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001939/// folded, but discard the result.
1940bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001941 EvalResult Result;
1942 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001943}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001944
1945APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001946 EvalResult EvalResult;
1947 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001948 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001949 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001950 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001951
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001952 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001953}