blob: 6ddeba99a737db9faac93923b0e59a99d0ece812 [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)
Mike Stumpfa502902009-10-29 20:48:09 +0000187 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000188 bool VisitChooseExpr(ChooseExpr *E)
189 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
190 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
191 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000192 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000193 bool VisitBinaryOperator(BinaryOperator *E)
194 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000195 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryDeref(UnaryOperator *E) {
200 if (E->getType().isVolatileQualified())
201 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000202 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000203 }
204 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
205};
206
207bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
208 Expr::EvalResult Result;
209 EvalInfo Info(Ctx, Result);
210
211 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
212}
213
214} // end anonymous namespace
215
Eli Friedman9a156e52008-11-12 09:44:48 +0000216//===----------------------------------------------------------------------===//
217// LValue Evaluation
218//===----------------------------------------------------------------------===//
219namespace {
220class VISIBILITY_HIDDEN LValueExprEvaluator
221 : public StmtVisitor<LValueExprEvaluator, APValue> {
222 EvalInfo &Info;
223public:
Mike Stump11289f42009-09-09 15:08:12 +0000224
Eli Friedman9a156e52008-11-12 09:44:48 +0000225 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
226
227 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000228 return APValue();
229 }
230
231 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000232 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000233 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000234 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
235 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
236 APValue VisitMemberExpr(MemberExpr *E);
237 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000238 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000239 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000240 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000241 APValue VisitUnaryExtension(const UnaryOperator *E)
242 { return Visit(E->getSubExpr()); }
243 APValue VisitChooseExpr(const ChooseExpr *E)
244 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000245
246 APValue VisitCastExpr(CastExpr *E) {
247 switch (E->getCastKind()) {
248 default:
249 return APValue();
250
251 case CastExpr::CK_NoOp:
252 return Visit(E->getSubExpr());
253 }
254 }
Eli Friedman449fe542009-03-23 04:56:01 +0000255 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000256};
257} // end anonymous namespace
258
259static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
260 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
261 return Result.isLValue();
262}
263
Mike Stump11289f42009-09-09 15:08:12 +0000264APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000265 if (isa<FunctionDecl>(E->getDecl())) {
266 return APValue(E, 0);
267 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000268 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000269 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000270 if (!VD->getType()->isReferenceType())
271 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000272 // FIXME: Check whether VD might be overridden!
Eli Friedman751aa72b72009-05-27 06:04:58 +0000273 if (VD->getInit())
274 return Visit(VD->getInit());
275 }
276
277 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000278}
279
Mike Stump11289f42009-09-09 15:08:12 +0000280APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000281 if (E->hasBlockDeclRefExprs())
282 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000283
Steve Naroffa0c32702009-04-16 19:02:57 +0000284 return APValue(E, 0);
285}
286
Eli Friedman9a156e52008-11-12 09:44:48 +0000287APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000288 if (!Info.AnyLValue && !E->isFileScope())
289 return APValue();
290 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000291}
292
293APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
294 APValue result;
295 QualType Ty;
296 if (E->isArrow()) {
297 if (!EvaluatePointer(E->getBase(), result, Info))
298 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000299 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000300 } else {
301 result = Visit(E->getBase());
302 if (result.isUninit())
303 return APValue();
304 Ty = E->getBase()->getType();
305 }
306
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000307 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000308 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000309
310 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
311 if (!FD) // FIXME: deal with other kinds of member expressions
312 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000313
314 if (FD->getType()->isReferenceType())
315 return APValue();
316
Eli Friedman9a156e52008-11-12 09:44:48 +0000317 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000318 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000319 for (RecordDecl::field_iterator Field = RD->field_begin(),
320 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000321 Field != FieldEnd; (void)++Field, ++i) {
322 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000323 break;
324 }
325
326 result.setLValue(result.getLValueBase(),
327 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
328
329 return result;
330}
331
Mike Stump11289f42009-09-09 15:08:12 +0000332APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000333 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000335 if (!EvaluatePointer(E->getBase(), Result, Info))
336 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000338 APSInt Index;
339 if (!EvaluateInteger(E->getIdx(), Index, Info))
340 return APValue();
341
342 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
343
344 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000345 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000346 Result.getLValueOffset() + Offset);
347 return Result;
348}
Eli Friedman9a156e52008-11-12 09:44:48 +0000349
Mike Stump11289f42009-09-09 15:08:12 +0000350APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000351 APValue Result;
352 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
353 return APValue();
354 return Result;
355}
356
Eli Friedman9a156e52008-11-12 09:44:48 +0000357//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000358// Pointer Evaluation
359//===----------------------------------------------------------------------===//
360
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000361namespace {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000362class VISIBILITY_HIDDEN PointerExprEvaluator
363 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000364 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000365public:
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattnercdf34e72008-07-11 22:52:41 +0000367 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000368
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000369 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000370 return APValue();
371 }
372
373 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
374
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000375 APValue VisitBinaryOperator(const BinaryOperator *E);
376 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000377 APValue VisitUnaryExtension(const UnaryOperator *E)
378 { return Visit(E->getSubExpr()); }
379 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000380 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
381 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000382 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
383 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000384 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000385 APValue VisitBlockExpr(BlockExpr *E) {
386 if (!E->hasBlockDeclRefExprs())
387 return APValue(E, 0);
388 return APValue();
389 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000390 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
391 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000392 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000393 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000394 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
395 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
396 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000397 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000398};
Chris Lattner05706e882008-07-11 18:11:29 +0000399} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000400
Chris Lattnercdf34e72008-07-11 22:52:41 +0000401static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000402 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000403 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000404 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000405 return Result.isLValue();
406}
407
408APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
409 if (E->getOpcode() != BinaryOperator::Add &&
410 E->getOpcode() != BinaryOperator::Sub)
411 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner05706e882008-07-11 18:11:29 +0000413 const Expr *PExp = E->getLHS();
414 const Expr *IExp = E->getRHS();
415 if (IExp->getType()->isPointerType())
416 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner05706e882008-07-11 18:11:29 +0000418 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000419 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000420 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner05706e882008-07-11 18:11:29 +0000422 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000423 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000424 return APValue();
425
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000426 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000427 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlssonef56fba2009-02-19 04:55:58 +0000429 // Explicitly handle GNU void* and function pointer arithmetic extensions.
430 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
431 SizeOfPointee = 1;
432 else
433 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000434
Chris Lattner05706e882008-07-11 18:11:29 +0000435 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000436
Chris Lattner05706e882008-07-11 18:11:29 +0000437 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000438 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000439 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000440 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
441
Chris Lattner05706e882008-07-11 18:11:29 +0000442 return APValue(ResultLValue.getLValueBase(), Offset);
443}
Eli Friedman9a156e52008-11-12 09:44:48 +0000444
Eli Friedmanc2b50172009-02-22 11:46:18 +0000445APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
446 APValue result;
447 if (EvaluateLValue(E->getSubExpr(), result, Info))
448 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000449 return APValue();
450}
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner05706e882008-07-11 18:11:29 +0000452
Chris Lattnere13042c2008-07-11 19:10:17 +0000453APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000454 const Expr* SubExpr = E->getSubExpr();
455
456 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000457 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000458 SubExpr->getType()->isObjCObjectPointerType() ||
459 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000460 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000461 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000462 return Result;
463 return APValue();
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Eli Friedmanbd840592008-07-27 05:46:18 +0000466 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000467 APValue Result;
468 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
469 return APValue();
470
471 if (Result.isInt()) {
472 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
473 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Daniel Dunbarce399542009-02-20 18:22:23 +0000476 // Cast is of an lvalue, no need to change value.
477 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000478 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000479
480 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000481 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000482 SubExpr->getType()->isArrayType()) {
483 APValue Result;
484 if (EvaluateLValue(SubExpr, Result, Info))
485 return Result;
486 return APValue();
487 }
488
Chris Lattner05706e882008-07-11 18:11:29 +0000489 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000490}
Chris Lattner05706e882008-07-11 18:11:29 +0000491
Eli Friedmanc69d4542009-01-25 01:54:01 +0000492APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000493 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000494 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000495 return APValue(E, 0);
496 return APValue();
497}
498
Eli Friedman9a156e52008-11-12 09:44:48 +0000499APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
500 bool BoolResult;
501 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
502 return APValue();
503
504 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
505
506 APValue Result;
507 if (EvaluatePointer(EvalExpr, Result, Info))
508 return Result;
509 return APValue();
510}
Chris Lattner05706e882008-07-11 18:11:29 +0000511
512//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000513// Vector Evaluation
514//===----------------------------------------------------------------------===//
515
516namespace {
517 class VISIBILITY_HIDDEN VectorExprEvaluator
518 : public StmtVisitor<VectorExprEvaluator, APValue> {
519 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000520 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000521 public:
Mike Stump11289f42009-09-09 15:08:12 +0000522
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000523 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000524
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000525 APValue VisitStmt(Stmt *S) {
526 return APValue();
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Eli Friedman3ae59112009-02-23 04:23:56 +0000529 APValue VisitParenExpr(ParenExpr *E)
530 { return Visit(E->getSubExpr()); }
531 APValue VisitUnaryExtension(const UnaryOperator *E)
532 { return Visit(E->getSubExpr()); }
533 APValue VisitUnaryPlus(const UnaryOperator *E)
534 { return Visit(E->getSubExpr()); }
535 APValue VisitUnaryReal(const UnaryOperator *E)
536 { return Visit(E->getSubExpr()); }
537 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
538 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000539 APValue VisitCastExpr(const CastExpr* E);
540 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
541 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000542 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000543 APValue VisitChooseExpr(const ChooseExpr *E)
544 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000545 APValue VisitUnaryImag(const UnaryOperator *E);
546 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000547 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000548 // shufflevector, ExtVectorElementExpr
549 // (Note that these require implementing conversions
550 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000551 };
552} // end anonymous namespace
553
554static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
555 if (!E->getType()->isVectorType())
556 return false;
557 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
558 return !Result.isUninit();
559}
560
561APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000562 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000563 QualType EltTy = VTy->getElementType();
564 unsigned NElts = VTy->getNumElements();
565 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000566
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000567 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000568 QualType SETy = SE->getType();
569 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000570
Nate Begeman2ffd3842009-06-26 18:22:18 +0000571 // Check for vector->vector bitcast and scalar->vector splat.
572 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000573 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000574 } else if (SETy->isIntegerType()) {
575 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000576 if (!EvaluateInteger(SE, IntResult, Info))
577 return APValue();
578 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000579 } else if (SETy->isRealFloatingType()) {
580 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000581 if (!EvaluateFloat(SE, F, Info))
582 return APValue();
583 Result = APValue(F);
584 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000585 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000586
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000587 // For casts of a scalar to ExtVector, convert the scalar to the element type
588 // and splat it to all elements.
589 if (E->getType()->isExtVectorType()) {
590 if (EltTy->isIntegerType() && Result.isInt())
591 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
592 Info.Ctx));
593 else if (EltTy->isIntegerType())
594 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
595 Info.Ctx));
596 else if (EltTy->isRealFloatingType() && Result.isInt())
597 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
598 Info.Ctx));
599 else if (EltTy->isRealFloatingType())
600 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
601 Info.Ctx));
602 else
603 return APValue();
604
605 // Splat and create vector APValue.
606 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
607 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000608 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000609
610 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
611 // to the vector. To construct the APValue vector initializer, bitcast the
612 // initializing value to an APInt, and shift out the bits pertaining to each
613 // element.
614 APSInt Init;
615 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000616
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000617 llvm::SmallVector<APValue, 4> Elts;
618 for (unsigned i = 0; i != NElts; ++i) {
619 APSInt Tmp = Init;
620 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000622 if (EltTy->isIntegerType())
623 Elts.push_back(APValue(Tmp));
624 else if (EltTy->isRealFloatingType())
625 Elts.push_back(APValue(APFloat(Tmp)));
626 else
627 return APValue();
628
629 Init >>= EltWidth;
630 }
631 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000632}
633
Mike Stump11289f42009-09-09 15:08:12 +0000634APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000635VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
636 return this->Visit(const_cast<Expr*>(E->getInitializer()));
637}
638
Mike Stump11289f42009-09-09 15:08:12 +0000639APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000640VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000641 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000642 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000643 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000644
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000645 QualType EltTy = VT->getElementType();
646 llvm::SmallVector<APValue, 4> Elements;
647
Eli Friedman3ae59112009-02-23 04:23:56 +0000648 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000649 if (EltTy->isIntegerType()) {
650 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000651 if (i < NumInits) {
652 if (!EvaluateInteger(E->getInit(i), sInt, Info))
653 return APValue();
654 } else {
655 sInt = Info.Ctx.MakeIntValue(0, EltTy);
656 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000657 Elements.push_back(APValue(sInt));
658 } else {
659 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000660 if (i < NumInits) {
661 if (!EvaluateFloat(E->getInit(i), f, Info))
662 return APValue();
663 } else {
664 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
665 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000666 Elements.push_back(APValue(f));
667 }
668 }
669 return APValue(&Elements[0], Elements.size());
670}
671
Mike Stump11289f42009-09-09 15:08:12 +0000672APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000673VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000674 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000675 QualType EltTy = VT->getElementType();
676 APValue ZeroElement;
677 if (EltTy->isIntegerType())
678 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
679 else
680 ZeroElement =
681 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
682
683 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
684 return APValue(&Elements[0], Elements.size());
685}
686
687APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
688 bool BoolResult;
689 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
690 return APValue();
691
692 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
693
694 APValue Result;
695 if (EvaluateVector(EvalExpr, Result, Info))
696 return Result;
697 return APValue();
698}
699
Eli Friedman3ae59112009-02-23 04:23:56 +0000700APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
701 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
702 Info.EvalResult.HasSideEffects = true;
703 return GetZeroVector(E->getType());
704}
705
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000706//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000707// Integer Evaluation
708//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000709
710namespace {
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000711class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000712 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000713 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000714 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000715public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000716 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000717 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000718
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000719 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000720 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000721 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000722 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000723 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000724 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000725 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000726 return true;
727 }
728
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000729 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000730 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000731 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000732 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000733 Result = APValue(APSInt(I));
734 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000735 return true;
736 }
737
738 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000739 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000740 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000741 return true;
742 }
743
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000744 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000745 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000746 if (Info.EvalResult.Diag == 0) {
747 Info.EvalResult.DiagLoc = L;
748 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000749 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000750 }
Chris Lattner99415702008-07-12 00:14:42 +0000751 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000752 }
Mike Stump11289f42009-09-09 15:08:12 +0000753
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000754 //===--------------------------------------------------------------------===//
755 // Visitor Methods
756 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000758 bool VisitStmt(Stmt *) {
759 assert(0 && "This should be called on integers, stmts are not integers");
760 return false;
761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000763 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000764 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattnere13042c2008-07-11 19:10:17 +0000767 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000768
Chris Lattner7174bf32008-07-12 00:38:25 +0000769 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000770 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000771 }
772 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000773 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000774 }
775 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000776 // Per gcc docs "this built-in function ignores top level
777 // qualifiers". We need to use the canonical version to properly
778 // be able to strip CRV qualifiers from the type.
779 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
780 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000781 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000782 T1.getUnqualifiedType()),
783 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000784 }
785 bool VisitDeclRefExpr(const DeclRefExpr *E);
786 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000787 bool VisitBinaryOperator(const BinaryOperator *E);
788 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000789 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000790
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000791 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000792 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
793
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000794 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000795 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000796 }
Mike Stump11289f42009-09-09 15:08:12 +0000797
Anders Carlsson39def3a2008-12-21 22:39:40 +0000798 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000799 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000800 }
Mike Stump11289f42009-09-09 15:08:12 +0000801
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000802 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000803 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000804 }
805
Eli Friedman4e7a2412009-02-27 04:45:43 +0000806 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
807 return Success(0, E);
808 }
809
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000810 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000811 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000812 }
813
Eli Friedman449fe542009-03-23 04:56:01 +0000814 bool VisitChooseExpr(const ChooseExpr *E) {
815 return Visit(E->getChosenSubExpr(Info.Ctx));
816 }
817
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000818 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000819 bool VisitUnaryImag(const UnaryOperator *E);
820
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000821private:
Chris Lattner68061312009-01-24 21:53:27 +0000822 unsigned GetAlignOfExpr(const Expr *E);
823 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000824 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000825};
Chris Lattner05706e882008-07-11 18:11:29 +0000826} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000827
Daniel Dunbarce399542009-02-20 18:22:23 +0000828static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000829 if (!E->getType()->isIntegralType())
830 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000831
Daniel Dunbarce399542009-02-20 18:22:23 +0000832 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
833}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000834
Daniel Dunbarce399542009-02-20 18:22:23 +0000835static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
836 APValue Val;
837 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
838 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000839 Result = Val.getInt();
840 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000841}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000842
Chris Lattner7174bf32008-07-12 00:38:25 +0000843bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
844 // Enums are integer constant exprs.
845 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000846 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000847 // signedness consistently; see PR3173.
848 APSInt SI = D->getInitVal();
849 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
850 // FIXME: This is an ugly hack around the fact that enums don't
851 // set their width (!?!) consistently; see PR3173.
852 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
853 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000854 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000855
856 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000857 // In C, they can also be folded, although they are not ICEs.
John McCall8ccfcb52009-09-24 19:53:00 +0000858 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000859 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000860 if (APValue *V = D->getEvaluatedValue())
861 return Success(V->getInt(), E);
862 if (const Expr *Init = D->getInit()) {
863 if (Visit(const_cast<Expr*>(Init))) {
864 // Cache the evaluated value in the variable declaration.
865 D->setEvaluatedValue(Info.Ctx, Result);
866 return true;
867 }
868
869 return false;
870 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000871 }
872 }
873
Chris Lattner7174bf32008-07-12 00:38:25 +0000874 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000875 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000876}
877
Chris Lattner86ee2862008-10-06 06:40:35 +0000878/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
879/// as GCC.
880static int EvaluateBuiltinClassifyType(const CallExpr *E) {
881 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000882 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000883 enum gcc_type_class {
884 no_type_class = -1,
885 void_type_class, integer_type_class, char_type_class,
886 enumeral_type_class, boolean_type_class,
887 pointer_type_class, reference_type_class, offset_type_class,
888 real_type_class, complex_type_class,
889 function_type_class, method_type_class,
890 record_type_class, union_type_class,
891 array_type_class, string_type_class,
892 lang_type_class
893 };
Mike Stump11289f42009-09-09 15:08:12 +0000894
895 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000896 // ideal, however it is what gcc does.
897 if (E->getNumArgs() == 0)
898 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattner86ee2862008-10-06 06:40:35 +0000900 QualType ArgTy = E->getArg(0)->getType();
901 if (ArgTy->isVoidType())
902 return void_type_class;
903 else if (ArgTy->isEnumeralType())
904 return enumeral_type_class;
905 else if (ArgTy->isBooleanType())
906 return boolean_type_class;
907 else if (ArgTy->isCharType())
908 return string_type_class; // gcc doesn't appear to use char_type_class
909 else if (ArgTy->isIntegerType())
910 return integer_type_class;
911 else if (ArgTy->isPointerType())
912 return pointer_type_class;
913 else if (ArgTy->isReferenceType())
914 return reference_type_class;
915 else if (ArgTy->isRealType())
916 return real_type_class;
917 else if (ArgTy->isComplexType())
918 return complex_type_class;
919 else if (ArgTy->isFunctionType())
920 return function_type_class;
921 else if (ArgTy->isStructureType())
922 return record_type_class;
923 else if (ArgTy->isUnionType())
924 return union_type_class;
925 else if (ArgTy->isArrayType())
926 return array_type_class;
927 else if (ArgTy->isUnionType())
928 return union_type_class;
929 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
930 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
931 return -1;
932}
933
Chris Lattner7174bf32008-07-12 00:38:25 +0000934bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000935 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000936 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000937 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000938
939 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000940 const Expr *Arg = E->getArg(0)->IgnoreParens();
941 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000942 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000943 && Base.Val.getKind() == APValue::LValue
944 && !Base.HasSideEffects)
945 if (const Expr *LVBase = Base.Val.getLValueBase())
946 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
947 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000948 if (!VD->getType()->isIncompleteType()
949 && VD->getType()->isObjectType()
950 && !VD->getType()->isVariablyModifiedType()
951 && !VD->getType()->isDependentType()) {
952 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
953 uint64_t Offset = Base.Val.getLValueOffset();
954 if (Offset <= Size)
955 Size -= Base.Val.getLValueOffset();
956 else
957 Size = 0;
958 return Success(Size, E);
959 }
Mike Stump722cedf2009-10-26 18:35:08 +0000960 }
961 }
962
Mike Stump876387b2009-10-27 22:09:17 +0000963 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000964 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump722cedf2009-10-26 18:35:08 +0000965 return Success(-1, E);
966 return Success(0, E);
967 }
Mike Stump876387b2009-10-27 22:09:17 +0000968
Mike Stump722cedf2009-10-26 18:35:08 +0000969 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
970 }
971
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000972 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000973 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000974
Anders Carlsson4c76e932008-11-24 04:21:33 +0000975 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000976 // __builtin_constant_p always has one operand: it returns true if that
977 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000978 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000979
980 case Builtin::BI__builtin_eh_return_data_regno: {
981 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
982 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
983 return Success(Operand, E);
984 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000985 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000986}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000987
Chris Lattnere13042c2008-07-11 19:10:17 +0000988bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000989 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +0000990 if (!Visit(E->getRHS()))
991 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +0000992
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000993 // If we can't evaluate the LHS, it might have side effects;
994 // conservatively mark it.
995 if (!E->getLHS()->isEvaluatable(Info.Ctx))
996 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000997
Anders Carlsson564730a2008-12-01 02:07:06 +0000998 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000999 }
1000
1001 if (E->isLogicalOp()) {
1002 // These need to be handled specially because the operands aren't
1003 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001004 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001005
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001006 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001007 // We were able to evaluate the LHS, see if we can get away with not
1008 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001009 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001010 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001011
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001012 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001013 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001014 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001015 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001016 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001017 }
1018 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001019 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001020 // We can't evaluate the LHS; however, sometimes the result
1021 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001022 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001023 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001024 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001025 // must have had side effects.
1026 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001027
1028 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001029 }
1030 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001031 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001032
Eli Friedman5a332ea2008-11-13 06:09:17 +00001033 return false;
1034 }
1035
Anders Carlssonacc79812008-11-16 07:17:21 +00001036 QualType LHSTy = E->getLHS()->getType();
1037 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001038
1039 if (LHSTy->isAnyComplexType()) {
1040 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1041 APValue LHS, RHS;
1042
1043 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1044 return false;
1045
1046 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1047 return false;
1048
1049 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001050 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001051 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001052 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001053 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1054
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001055 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001056 return Success((CR_r == APFloat::cmpEqual &&
1057 CR_i == APFloat::cmpEqual), E);
1058 else {
1059 assert(E->getOpcode() == BinaryOperator::NE &&
1060 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001061 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001062 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001063 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001064 CR_i == APFloat::cmpLessThan)), E);
1065 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001066 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001067 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001068 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1069 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1070 else {
1071 assert(E->getOpcode() == BinaryOperator::NE &&
1072 "Invalid compex comparison.");
1073 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1074 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1075 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001076 }
1077 }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Anders Carlssonacc79812008-11-16 07:17:21 +00001079 if (LHSTy->isRealFloatingType() &&
1080 RHSTy->isRealFloatingType()) {
1081 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001082
Anders Carlssonacc79812008-11-16 07:17:21 +00001083 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1084 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001085
Anders Carlssonacc79812008-11-16 07:17:21 +00001086 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1087 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001088
Anders Carlssonacc79812008-11-16 07:17:21 +00001089 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001090
Anders Carlssonacc79812008-11-16 07:17:21 +00001091 switch (E->getOpcode()) {
1092 default:
1093 assert(0 && "Invalid binary operator!");
1094 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001095 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001096 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001097 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001098 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001099 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001100 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001101 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001102 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001103 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001104 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001105 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001106 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001107 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001108 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Eli Friedmana38da572009-04-28 19:17:36 +00001111 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1112 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001113 APValue LHSValue;
1114 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1115 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001116
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001117 APValue RHSValue;
1118 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1119 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001120
Eli Friedman334046a2009-06-14 02:17:33 +00001121 // Reject any bases from the normal codepath; we special-case comparisons
1122 // to null.
1123 if (LHSValue.getLValueBase()) {
1124 if (!E->isEqualityOp())
1125 return false;
1126 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1127 return false;
1128 bool bres;
1129 if (!EvalPointerValueAsBool(LHSValue, bres))
1130 return false;
1131 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1132 } else if (RHSValue.getLValueBase()) {
1133 if (!E->isEqualityOp())
1134 return false;
1135 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1136 return false;
1137 bool bres;
1138 if (!EvalPointerValueAsBool(RHSValue, bres))
1139 return false;
1140 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1141 }
Eli Friedman64004332009-03-23 04:38:34 +00001142
Eli Friedmana38da572009-04-28 19:17:36 +00001143 if (E->getOpcode() == BinaryOperator::Sub) {
1144 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001145 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001146
Eli Friedmana38da572009-04-28 19:17:36 +00001147 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001148 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1149 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001150
Eli Friedmana38da572009-04-28 19:17:36 +00001151 return Success(D, E);
1152 }
1153 bool Result;
1154 if (E->getOpcode() == BinaryOperator::EQ) {
1155 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001156 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001157 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1158 }
1159 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001160 }
1161 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001162 if (!LHSTy->isIntegralType() ||
1163 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001164 // We can't continue from here for non-integral types, and they
1165 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001166 return false;
1167 }
1168
Anders Carlsson9c181652008-07-08 14:35:21 +00001169 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001170 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001171 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001172
Eli Friedman94c25c62009-03-24 01:14:50 +00001173 APValue RHSVal;
1174 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001175 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001176
1177 // Handle cases like (unsigned long)&a + 4.
1178 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1179 uint64_t offset = Result.getLValueOffset();
1180 if (E->getOpcode() == BinaryOperator::Add)
1181 offset += RHSVal.getInt().getZExtValue();
1182 else
1183 offset -= RHSVal.getInt().getZExtValue();
1184 Result = APValue(Result.getLValueBase(), offset);
1185 return true;
1186 }
1187
1188 // Handle cases like 4 + (unsigned long)&a
1189 if (E->getOpcode() == BinaryOperator::Add &&
1190 RHSVal.isLValue() && Result.isInt()) {
1191 uint64_t offset = RHSVal.getLValueOffset();
1192 offset += Result.getInt().getZExtValue();
1193 Result = APValue(RHSVal.getLValueBase(), offset);
1194 return true;
1195 }
1196
1197 // All the following cases expect both operands to be an integer
1198 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001199 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001200
Eli Friedman94c25c62009-03-24 01:14:50 +00001201 APSInt& RHS = RHSVal.getInt();
1202
Anders Carlsson9c181652008-07-08 14:35:21 +00001203 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001204 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001205 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001206 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1207 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1208 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1209 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1210 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1211 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001212 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001213 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001214 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001215 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001216 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001217 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001218 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001219 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001220 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001221 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001222 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001223 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1224 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001225 }
1226 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001227 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001228 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1229 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001230 }
Mike Stump11289f42009-09-09 15:08:12 +00001231
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001232 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1233 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1234 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1235 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1236 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1237 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001238 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001239}
1240
Nuno Lopes42042612008-11-16 19:28:31 +00001241bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001242 bool Cond;
1243 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001244 return false;
1245
Nuno Lopes527b5a62008-11-16 22:06:39 +00001246 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001247}
1248
Chris Lattner68061312009-01-24 21:53:27 +00001249unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001250 // Get information about the alignment.
1251 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001252
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001253 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001254 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001255}
1256
Chris Lattner68061312009-01-24 21:53:27 +00001257unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1258 E = E->IgnoreParens();
1259
1260 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001261 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001262 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001263 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedman64004332009-03-23 04:38:34 +00001264
Chris Lattner68061312009-01-24 21:53:27 +00001265 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001266 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattner68061312009-01-24 21:53:27 +00001267
Chris Lattner24aeeab2009-01-24 21:09:06 +00001268 return GetAlignOfType(E->getType());
1269}
1270
1271
Sebastian Redl6f282892008-11-11 17:56:53 +00001272/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1273/// expression's type.
1274bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1275 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001276
Chris Lattner24aeeab2009-01-24 21:09:06 +00001277 // Handle alignof separately.
1278 if (!E->isSizeOf()) {
1279 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001280 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001281 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001282 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001283 }
Eli Friedman64004332009-03-23 04:38:34 +00001284
Sebastian Redl6f282892008-11-11 17:56:53 +00001285 QualType SrcTy = E->getTypeOfArgument();
1286
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001287 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1288 // extension.
1289 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1290 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001291
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001292 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001293 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001294 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001295
Chris Lattner24aeeab2009-01-24 21:09:06 +00001296 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001297 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001298 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001299}
1300
Chris Lattnere13042c2008-07-11 19:10:17 +00001301bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001302 // Special case unary operators that do not need their subexpression
1303 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001304 if (E->isOffsetOfOp()) {
1305 // The AST for offsetof is defined in such a way that we can just
1306 // directly Evaluate it as an l-value.
1307 APValue LV;
1308 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1309 return false;
1310 if (LV.getLValueBase())
1311 return false;
1312 return Success(LV.getLValueOffset(), E);
1313 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001314
1315 if (E->getOpcode() == UnaryOperator::LNot) {
1316 // LNot's operand isn't necessarily an integer, so we handle it specially.
1317 bool bres;
1318 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1319 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001320 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001321 }
1322
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001323 // Only handle integral operations...
1324 if (!E->getSubExpr()->getType()->isIntegralType())
1325 return false;
1326
Chris Lattnercdf34e72008-07-11 22:52:41 +00001327 // Get the operand value into 'Result'.
1328 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001329 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001330
Chris Lattnerf09ad162008-07-11 22:15:16 +00001331 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001332 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001333 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1334 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001335 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001336 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001337 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1338 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001339 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001340 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001341 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001342 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001343 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001344 if (!Result.isInt()) return false;
1345 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001346 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001347 if (!Result.isInt()) return false;
1348 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001349 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001350}
Mike Stump11289f42009-09-09 15:08:12 +00001351
Chris Lattner477c4be2008-07-12 01:15:53 +00001352/// HandleCast - This is used to evaluate implicit or explicit casts where the
1353/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001354bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001355 Expr *SubExpr = E->getSubExpr();
1356 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001357 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001358
Eli Friedman9a156e52008-11-12 09:44:48 +00001359 if (DestType->isBooleanType()) {
1360 bool BoolResult;
1361 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1362 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001363 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001364 }
1365
Anders Carlsson9c181652008-07-08 14:35:21 +00001366 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001367 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001368 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001369 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001370
Eli Friedman742421e2009-02-20 01:15:07 +00001371 if (!Result.isInt()) {
1372 // Only allow casts of lvalues if they are lossless.
1373 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1374 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001375
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001376 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001377 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner477c4be2008-07-12 01:15:53 +00001380 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001381 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001382 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001383 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001384 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001385
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001386 if (LV.getLValueBase()) {
1387 // Only allow based lvalue casts if they are lossless.
1388 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1389 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001390
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001391 Result = LV;
1392 return true;
1393 }
1394
1395 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1396 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001397 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001398
Eli Friedman742421e2009-02-20 01:15:07 +00001399 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1400 // This handles double-conversion cases, where there's both
1401 // an l-value promotion and an implicit conversion to int.
1402 APValue LV;
1403 if (!EvaluateLValue(SubExpr, LV, Info))
1404 return false;
1405
1406 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1407 return false;
1408
1409 Result = LV;
1410 return true;
1411 }
1412
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001413 if (SrcType->isAnyComplexType()) {
1414 APValue C;
1415 if (!EvaluateComplex(SubExpr, C, Info))
1416 return false;
1417 if (C.isComplexFloat())
1418 return Success(HandleFloatToIntCast(DestType, SrcType,
1419 C.getComplexFloatReal(), Info.Ctx),
1420 E);
1421 else
1422 return Success(HandleIntToIntCast(DestType, SrcType,
1423 C.getComplexIntReal(), Info.Ctx), E);
1424 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001425 // FIXME: Handle vectors
1426
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001427 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001428 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001429
Eli Friedman24c01542008-08-22 00:06:13 +00001430 APFloat F(0.0);
1431 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001432 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001433
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001434 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001435}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001436
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001437bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1438 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1439 APValue LV;
1440 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1441 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1442 return Success(LV.getComplexIntReal(), E);
1443 }
1444
1445 return Visit(E->getSubExpr());
1446}
1447
Eli Friedman4e7a2412009-02-27 04:45:43 +00001448bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001449 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1450 APValue LV;
1451 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1452 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1453 return Success(LV.getComplexIntImag(), E);
1454 }
1455
Eli Friedman4e7a2412009-02-27 04:45:43 +00001456 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1457 Info.EvalResult.HasSideEffects = true;
1458 return Success(0, E);
1459}
1460
Chris Lattner05706e882008-07-11 18:11:29 +00001461//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001462// Float Evaluation
1463//===----------------------------------------------------------------------===//
1464
1465namespace {
1466class VISIBILITY_HIDDEN FloatExprEvaluator
1467 : public StmtVisitor<FloatExprEvaluator, bool> {
1468 EvalInfo &Info;
1469 APFloat &Result;
1470public:
1471 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1472 : Info(info), Result(result) {}
1473
1474 bool VisitStmt(Stmt *S) {
1475 return false;
1476 }
1477
1478 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001479 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001480
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001481 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001482 bool VisitBinaryOperator(const BinaryOperator *E);
1483 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001484 bool VisitCastExpr(CastExpr *E);
1485 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001486
Eli Friedman449fe542009-03-23 04:56:01 +00001487 bool VisitChooseExpr(const ChooseExpr *E)
1488 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1489 bool VisitUnaryExtension(const UnaryOperator *E)
1490 { return Visit(E->getSubExpr()); }
1491
1492 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1493 // member of vector, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001494 // conditional ?:, comma
Eli Friedman24c01542008-08-22 00:06:13 +00001495};
1496} // end anonymous namespace
1497
1498static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1499 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1500}
1501
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001502bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001503 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001504 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001505 case Builtin::BI__builtin_huge_val:
1506 case Builtin::BI__builtin_huge_valf:
1507 case Builtin::BI__builtin_huge_vall:
1508 case Builtin::BI__builtin_inf:
1509 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001510 case Builtin::BI__builtin_infl: {
1511 const llvm::fltSemantics &Sem =
1512 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001513 Result = llvm::APFloat::getInf(Sem);
1514 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001515 }
Mike Stump11289f42009-09-09 15:08:12 +00001516
Chris Lattner0b7282e2008-10-06 06:31:58 +00001517 case Builtin::BI__builtin_nan:
1518 case Builtin::BI__builtin_nanf:
1519 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001520 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001521 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001522 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001523 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001524 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001525 const llvm::fltSemantics &Sem =
1526 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001527 llvm::SmallString<16> s;
1528 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1529 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001530 long l;
1531 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001532 l = strtol(&s[0], &endp, 0);
1533 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001534 return false;
1535 unsigned type = (unsigned int)l;;
1536 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001537 return true;
1538 }
1539 }
1540 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001541
1542 case Builtin::BI__builtin_fabs:
1543 case Builtin::BI__builtin_fabsf:
1544 case Builtin::BI__builtin_fabsl:
1545 if (!EvaluateFloat(E->getArg(0), Result, Info))
1546 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001547
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001548 if (Result.isNegative())
1549 Result.changeSign();
1550 return true;
1551
Mike Stump11289f42009-09-09 15:08:12 +00001552 case Builtin::BI__builtin_copysign:
1553 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001554 case Builtin::BI__builtin_copysignl: {
1555 APFloat RHS(0.);
1556 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1557 !EvaluateFloat(E->getArg(1), RHS, Info))
1558 return false;
1559 Result.copySign(RHS);
1560 return true;
1561 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001562 }
1563}
1564
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001565bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001566 if (E->getOpcode() == UnaryOperator::Deref)
1567 return false;
1568
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001569 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1570 return false;
1571
1572 switch (E->getOpcode()) {
1573 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001574 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001575 return true;
1576 case UnaryOperator::Minus:
1577 Result.changeSign();
1578 return true;
1579 }
1580}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001581
Eli Friedman24c01542008-08-22 00:06:13 +00001582bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1583 // FIXME: Diagnostics? I really don't understand how the warnings
1584 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001585 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001586 if (!EvaluateFloat(E->getLHS(), Result, Info))
1587 return false;
1588 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1589 return false;
1590
1591 switch (E->getOpcode()) {
1592 default: return false;
1593 case BinaryOperator::Mul:
1594 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1595 return true;
1596 case BinaryOperator::Add:
1597 Result.add(RHS, APFloat::rmNearestTiesToEven);
1598 return true;
1599 case BinaryOperator::Sub:
1600 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1601 return true;
1602 case BinaryOperator::Div:
1603 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1604 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001605 }
1606}
1607
1608bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1609 Result = E->getValue();
1610 return true;
1611}
1612
Eli Friedman9a156e52008-11-12 09:44:48 +00001613bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1614 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001615
Eli Friedman9a156e52008-11-12 09:44:48 +00001616 if (SubExpr->getType()->isIntegralType()) {
1617 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001618 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001619 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001620 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001621 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001622 return true;
1623 }
1624 if (SubExpr->getType()->isRealFloatingType()) {
1625 if (!Visit(SubExpr))
1626 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001627 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1628 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001629 return true;
1630 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001631 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001632
1633 return false;
1634}
1635
1636bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1637 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1638 return true;
1639}
1640
Eli Friedman24c01542008-08-22 00:06:13 +00001641//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001642// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001643//===----------------------------------------------------------------------===//
1644
1645namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001646class VISIBILITY_HIDDEN ComplexExprEvaluator
1647 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001648 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001649
Anders Carlsson537969c2008-11-16 20:27:53 +00001650public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001651 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001652
Anders Carlsson537969c2008-11-16 20:27:53 +00001653 //===--------------------------------------------------------------------===//
1654 // Visitor Methods
1655 //===--------------------------------------------------------------------===//
1656
1657 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001658 return APValue();
1659 }
Mike Stump11289f42009-09-09 15:08:12 +00001660
Anders Carlsson537969c2008-11-16 20:27:53 +00001661 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1662
1663 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001664 Expr* SubExpr = E->getSubExpr();
1665
1666 if (SubExpr->getType()->isRealFloatingType()) {
1667 APFloat Result(0.0);
1668
1669 if (!EvaluateFloat(SubExpr, Result, Info))
1670 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001671
1672 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001673 Result);
1674 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001675 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001676 "Unexpected imaginary literal.");
1677
1678 llvm::APSInt Result;
1679 if (!EvaluateInteger(SubExpr, Result, Info))
1680 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001681
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001682 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1683 Zero = 0;
1684 return APValue(Zero, Result);
1685 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001686 }
1687
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001688 APValue VisitCastExpr(CastExpr *E) {
1689 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001690 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001691 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001692
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001693 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001694 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001695
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001696 if (!EvaluateFloat(SubExpr, Result, Info))
1697 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001698
1699 if (EltType->isRealFloatingType()) {
1700 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001701 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001702 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1703 } else {
1704 llvm::APSInt IResult;
1705 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1706 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1707 Zero = 0;
1708 return APValue(IResult, Zero);
1709 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001710 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001711 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001712
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001713 if (!EvaluateInteger(SubExpr, Result, Info))
1714 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001715
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001716 if (EltType->isRealFloatingType()) {
1717 APFloat FResult =
1718 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001719 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001720 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1721 } else {
1722 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1723 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1724 Zero = 0;
1725 return APValue(Result, Zero);
1726 }
John McCall9dd450b2009-09-21 23:43:11 +00001727 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001728 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001729
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001730 if (!EvaluateComplex(SubExpr, Src, Info))
1731 return APValue();
1732
1733 QualType SrcType = CT->getElementType();
1734
1735 if (Src.isComplexFloat()) {
1736 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001737 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001738 Src.getComplexFloatReal(),
1739 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001740 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001741 Src.getComplexFloatImag(),
1742 Info.Ctx));
1743 } else {
1744 return APValue(HandleFloatToIntCast(EltType, SrcType,
1745 Src.getComplexFloatReal(),
1746 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001747 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001748 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001749 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001750 }
1751 } else {
1752 assert(Src.isComplexInt() && "Invalid evaluate result.");
1753 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001754 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001755 Src.getComplexIntReal(),
1756 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001757 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001758 Src.getComplexIntImag(),
1759 Info.Ctx));
1760 } else {
1761 return APValue(HandleIntToIntCast(EltType, SrcType,
1762 Src.getComplexIntReal(),
1763 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001764 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001765 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001766 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001767 }
1768 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001769 }
1770
1771 // FIXME: Handle more casts.
1772 return APValue();
1773 }
Mike Stump11289f42009-09-09 15:08:12 +00001774
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001775 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001776 APValue VisitChooseExpr(const ChooseExpr *E)
1777 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1778 APValue VisitUnaryExtension(const UnaryOperator *E)
1779 { return Visit(E->getSubExpr()); }
1780 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001781 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001782};
1783} // end anonymous namespace
1784
Mike Stump11289f42009-09-09 15:08:12 +00001785static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001786 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1787 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001788 (&Result.getComplexFloatReal().getSemantics() ==
1789 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001790 "Invalid complex evaluation.");
1791 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001792}
1793
Mike Stump11289f42009-09-09 15:08:12 +00001794APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001795 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001796
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001797 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001798 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001799
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001800 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001801 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001802
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001803 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1804 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001805 switch (E->getOpcode()) {
1806 default: return APValue();
1807 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001808 if (Result.isComplexFloat()) {
1809 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1810 APFloat::rmNearestTiesToEven);
1811 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1812 APFloat::rmNearestTiesToEven);
1813 } else {
1814 Result.getComplexIntReal() += RHS.getComplexIntReal();
1815 Result.getComplexIntImag() += RHS.getComplexIntImag();
1816 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001817 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001818 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001819 if (Result.isComplexFloat()) {
1820 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1821 APFloat::rmNearestTiesToEven);
1822 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1823 APFloat::rmNearestTiesToEven);
1824 } else {
1825 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1826 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1827 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001828 break;
1829 case BinaryOperator::Mul:
1830 if (Result.isComplexFloat()) {
1831 APValue LHS = Result;
1832 APFloat &LHS_r = LHS.getComplexFloatReal();
1833 APFloat &LHS_i = LHS.getComplexFloatImag();
1834 APFloat &RHS_r = RHS.getComplexFloatReal();
1835 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001836
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001837 APFloat Tmp = LHS_r;
1838 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1839 Result.getComplexFloatReal() = Tmp;
1840 Tmp = LHS_i;
1841 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1842 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1843
1844 Tmp = LHS_r;
1845 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1846 Result.getComplexFloatImag() = Tmp;
1847 Tmp = LHS_i;
1848 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1849 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1850 } else {
1851 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001852 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001853 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1854 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001855 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001856 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1857 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1858 }
1859 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001860 }
1861
1862 return Result;
1863}
1864
Anders Carlsson537969c2008-11-16 20:27:53 +00001865//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001866// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001867//===----------------------------------------------------------------------===//
1868
Chris Lattner67d7b922008-11-16 21:24:15 +00001869/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001870/// any crazy technique (that has nothing to do with language standards) that
1871/// we want to. If this function returns true, it returns the folded constant
1872/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001873bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1874 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001875
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001876 if (getType()->isVectorType()) {
1877 if (!EvaluateVector(this, Result.Val, Info))
1878 return false;
1879 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001880 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001881 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001882 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001883 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001884 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001885 } else if (getType()->isRealFloatingType()) {
1886 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001887 if (!EvaluateFloat(this, f, Info))
1888 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001889
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001890 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001891 } else if (getType()->isAnyComplexType()) {
1892 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001893 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001894 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001895 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001896
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001897 return true;
1898}
1899
Mike Stump5183a142009-10-26 23:05:19 +00001900bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1901 EvalInfo Info(Ctx, Result, true);
1902
1903 if (getType()->isVectorType()) {
1904 if (!EvaluateVector(this, Result.Val, Info))
1905 return false;
1906 } else if (getType()->isIntegerType()) {
1907 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1908 return false;
1909 } else if (getType()->hasPointerRepresentation()) {
1910 if (!EvaluatePointer(this, Result.Val, Info))
1911 return false;
1912 } else if (getType()->isRealFloatingType()) {
1913 llvm::APFloat f(0.0);
1914 if (!EvaluateFloat(this, f, Info))
1915 return false;
1916
1917 Result.Val = APValue(f);
1918 } else if (getType()->isAnyComplexType()) {
1919 if (!EvaluateComplex(this, Result.Val, Info))
1920 return false;
1921 } else
1922 return false;
1923
1924 return true;
1925}
1926
Anders Carlsson43168122009-04-10 04:54:13 +00001927bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1928 EvalInfo Info(Ctx, Result);
1929
1930 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1931}
1932
Eli Friedman7d45c482009-09-13 10:17:44 +00001933bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1934 EvalInfo Info(Ctx, Result, true);
1935
1936 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1937}
1938
Chris Lattner67d7b922008-11-16 21:24:15 +00001939/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001940/// folded, but discard the result.
1941bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001942 EvalResult Result;
1943 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001944}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001945
1946APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001947 EvalResult EvalResult;
1948 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001949 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001950 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001951 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001952
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001953 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001954}