blob: a4ca2c4db6766e633362944abb984c253090e973 [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);
Daniel Dunbarce399542009-02-20 18:22:23 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman334046a2009-06-14 02:17:33 +000069static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
70 // FIXME: Is this accurate for all kinds of bases? If not, what would
71 // the check look like?
72 Result = Value.getLValueBase() || Value.getLValueOffset();
73 return true;
74}
75
Eli Friedman9a156e52008-11-12 09:44:48 +000076static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
77 if (E->getType()->isIntegralType()) {
78 APSInt IntResult;
79 if (!EvaluateInteger(E, IntResult, Info))
80 return false;
81 Result = IntResult != 0;
82 return true;
83 } else if (E->getType()->isRealFloatingType()) {
84 APFloat FloatResult(0.0);
85 if (!EvaluateFloat(E, FloatResult, Info))
86 return false;
87 Result = !FloatResult.isZero();
88 return true;
Eli Friedman64004332009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +000094 } else if (E->getType()->isAnyComplexType()) {
95 APValue ComplexResult;
96 if (!EvaluateComplex(E, ComplexResult, Info))
97 return false;
98 if (ComplexResult.isComplexFloat()) {
99 Result = !ComplexResult.getComplexFloatReal().isZero() ||
100 !ComplexResult.getComplexFloatImag().isZero();
101 } else {
102 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
103 ComplexResult.getComplexIntImag().getBoolValue();
104 }
105 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000112 APFloat &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 // Determine whether we are converting to unsigned or signed.
115 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000116
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000117 // FIXME: Warning for overflow.
118 uint64_t Space[4];
119 bool ignored;
120 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
121 llvm::APFloat::rmTowardZero, &ignored);
122 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
123}
124
Mike Stump11289f42009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump11289f42009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000135 APSInt &Value, ASTContext &Ctx) {
136 unsigned DestWidth = Ctx.getIntWidth(DestType);
137 APSInt Result = Value;
138 // Figure out if this is a truncate, extend or noop cast.
139 // If the input is signed, do a sign extend, noop, or truncate.
140 Result.extOrTrunc(DestWidth);
141 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
142 return Result;
143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Mike Stump876387b2009-10-27 22:09:17 +0000154namespace {
155class VISIBILITY_HIDDEN HasSideEffect
156 : public StmtVisitor<HasSideEffect, bool> {
157 EvalInfo &Info;
158public:
159
160 HasSideEffect(EvalInfo &info) : Info(info) {}
161
162 // Unhandled nodes conservatively default to having side effects.
163 bool VisitStmt(Stmt *S) {
164 return true;
165 }
166
167 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
168 bool VisitDeclRefExpr(DeclRefExpr *E) {
169 if (E->getType().isVolatileQualified())
170 return true;
171 return false;
172 }
173 // We don't want to evaluate BlockExprs multiple times, as they generate
174 // a ton of code.
175 bool VisitBlockExpr(BlockExpr *E) { return true; }
176 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
177 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
178 { return Visit(E->getInitializer()); }
179 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
180 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
181 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
182 bool VisitStringLiteral(StringLiteral *E) { return false; }
183 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
184 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
185 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
186 { return Visit(E->getLHS()) && Visit(E->getRHS()); }
187 bool VisitChooseExpr(ChooseExpr *E)
188 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
189 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
190 bool VisitBinAssign(BinaryOperator *E) { return true; }
191 bool VisitCompoundAssign(BinaryOperator *E) { return true; }
192 bool VisitBinaryOperator(BinaryOperator *E) { return false; }
193 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
194 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
195 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
197 bool VisitUnaryDeref(UnaryOperator *E) {
198 if (E->getType().isVolatileQualified())
199 return true;
200 return false;
201 }
202 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
203};
204
205bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
206 Expr::EvalResult Result;
207 EvalInfo Info(Ctx, Result);
208
209 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
210}
211
212} // end anonymous namespace
213
Eli Friedman9a156e52008-11-12 09:44:48 +0000214//===----------------------------------------------------------------------===//
215// LValue Evaluation
216//===----------------------------------------------------------------------===//
217namespace {
218class VISIBILITY_HIDDEN LValueExprEvaluator
219 : public StmtVisitor<LValueExprEvaluator, APValue> {
220 EvalInfo &Info;
221public:
Mike Stump11289f42009-09-09 15:08:12 +0000222
Eli Friedman9a156e52008-11-12 09:44:48 +0000223 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
224
225 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000226 return APValue();
227 }
228
229 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000230 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000231 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000232 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
233 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
234 APValue VisitMemberExpr(MemberExpr *E);
235 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000236 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000237 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000238 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000239 APValue VisitUnaryExtension(const UnaryOperator *E)
240 { return Visit(E->getSubExpr()); }
241 APValue VisitChooseExpr(const ChooseExpr *E)
242 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000243
244 APValue VisitCastExpr(CastExpr *E) {
245 switch (E->getCastKind()) {
246 default:
247 return APValue();
248
249 case CastExpr::CK_NoOp:
250 return Visit(E->getSubExpr());
251 }
252 }
Eli Friedman449fe542009-03-23 04:56:01 +0000253 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000254};
255} // end anonymous namespace
256
257static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
258 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
259 return Result.isLValue();
260}
261
Mike Stump11289f42009-09-09 15:08:12 +0000262APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000263 if (isa<FunctionDecl>(E->getDecl())) {
264 return APValue(E, 0);
265 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000266 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000267 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000268 if (!VD->getType()->isReferenceType())
269 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000270 // FIXME: Check whether VD might be overridden!
Eli Friedman751aa72b72009-05-27 06:04:58 +0000271 if (VD->getInit())
272 return Visit(VD->getInit());
273 }
274
275 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000276}
277
Mike Stump11289f42009-09-09 15:08:12 +0000278APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000279 if (E->hasBlockDeclRefExprs())
280 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Steve Naroffa0c32702009-04-16 19:02:57 +0000282 return APValue(E, 0);
283}
284
Eli Friedman9a156e52008-11-12 09:44:48 +0000285APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000286 if (!Info.AnyLValue && !E->isFileScope())
287 return APValue();
288 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000289}
290
291APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
292 APValue result;
293 QualType Ty;
294 if (E->isArrow()) {
295 if (!EvaluatePointer(E->getBase(), result, Info))
296 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000297 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000298 } else {
299 result = Visit(E->getBase());
300 if (result.isUninit())
301 return APValue();
302 Ty = E->getBase()->getType();
303 }
304
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000305 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000306 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000307
308 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
309 if (!FD) // FIXME: deal with other kinds of member expressions
310 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000311
312 if (FD->getType()->isReferenceType())
313 return APValue();
314
Eli Friedman9a156e52008-11-12 09:44:48 +0000315 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000316 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000317 for (RecordDecl::field_iterator Field = RD->field_begin(),
318 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000319 Field != FieldEnd; (void)++Field, ++i) {
320 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000321 break;
322 }
323
324 result.setLValue(result.getLValueBase(),
325 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
326
327 return result;
328}
329
Mike Stump11289f42009-09-09 15:08:12 +0000330APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000331 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000332
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000333 if (!EvaluatePointer(E->getBase(), Result, Info))
334 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000335
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000336 APSInt Index;
337 if (!EvaluateInteger(E->getIdx(), Index, Info))
338 return APValue();
339
340 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
341
342 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000343 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000344 Result.getLValueOffset() + Offset);
345 return Result;
346}
Eli Friedman9a156e52008-11-12 09:44:48 +0000347
Mike Stump11289f42009-09-09 15:08:12 +0000348APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000349 APValue Result;
350 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
351 return APValue();
352 return Result;
353}
354
Eli Friedman9a156e52008-11-12 09:44:48 +0000355//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000356// Pointer Evaluation
357//===----------------------------------------------------------------------===//
358
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000359namespace {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000360class VISIBILITY_HIDDEN PointerExprEvaluator
361 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000362 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000363public:
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnercdf34e72008-07-11 22:52:41 +0000365 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000366
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000367 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000368 return APValue();
369 }
370
371 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
372
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000373 APValue VisitBinaryOperator(const BinaryOperator *E);
374 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000375 APValue VisitUnaryExtension(const UnaryOperator *E)
376 { return Visit(E->getSubExpr()); }
377 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000378 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
379 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000380 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
381 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000382 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000383 APValue VisitBlockExpr(BlockExpr *E) {
384 if (!E->hasBlockDeclRefExprs())
385 return APValue(E, 0);
386 return APValue();
387 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000388 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
389 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000390 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000391 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000392 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
393 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
394 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000395 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000396};
Chris Lattner05706e882008-07-11 18:11:29 +0000397} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000398
Chris Lattnercdf34e72008-07-11 22:52:41 +0000399static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000400 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000401 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000402 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000403 return Result.isLValue();
404}
405
406APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
407 if (E->getOpcode() != BinaryOperator::Add &&
408 E->getOpcode() != BinaryOperator::Sub)
409 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner05706e882008-07-11 18:11:29 +0000411 const Expr *PExp = E->getLHS();
412 const Expr *IExp = E->getRHS();
413 if (IExp->getType()->isPointerType())
414 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner05706e882008-07-11 18:11:29 +0000416 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000417 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000418 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner05706e882008-07-11 18:11:29 +0000420 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000421 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000422 return APValue();
423
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000424 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000425 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Anders Carlssonef56fba2009-02-19 04:55:58 +0000427 // Explicitly handle GNU void* and function pointer arithmetic extensions.
428 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
429 SizeOfPointee = 1;
430 else
431 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000432
Chris Lattner05706e882008-07-11 18:11:29 +0000433 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000434
Chris Lattner05706e882008-07-11 18:11:29 +0000435 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000436 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000437 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000438 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
439
Chris Lattner05706e882008-07-11 18:11:29 +0000440 return APValue(ResultLValue.getLValueBase(), Offset);
441}
Eli Friedman9a156e52008-11-12 09:44:48 +0000442
Eli Friedmanc2b50172009-02-22 11:46:18 +0000443APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
444 APValue result;
445 if (EvaluateLValue(E->getSubExpr(), result, Info))
446 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000447 return APValue();
448}
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattner05706e882008-07-11 18:11:29 +0000450
Chris Lattnere13042c2008-07-11 19:10:17 +0000451APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000452 const Expr* SubExpr = E->getSubExpr();
453
454 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000455 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000456 SubExpr->getType()->isObjCObjectPointerType() ||
457 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000458 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000459 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000460 return Result;
461 return APValue();
462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Eli Friedmanbd840592008-07-27 05:46:18 +0000464 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000465 APValue Result;
466 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
467 return APValue();
468
469 if (Result.isInt()) {
470 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
471 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Daniel Dunbarce399542009-02-20 18:22:23 +0000474 // Cast is of an lvalue, no need to change value.
475 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000476 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000477
478 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000479 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000480 SubExpr->getType()->isArrayType()) {
481 APValue Result;
482 if (EvaluateLValue(SubExpr, Result, Info))
483 return Result;
484 return APValue();
485 }
486
Chris Lattner05706e882008-07-11 18:11:29 +0000487 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000488}
Chris Lattner05706e882008-07-11 18:11:29 +0000489
Eli Friedmanc69d4542009-01-25 01:54:01 +0000490APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000491 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000492 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000493 return APValue(E, 0);
494 return APValue();
495}
496
Eli Friedman9a156e52008-11-12 09:44:48 +0000497APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
498 bool BoolResult;
499 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
500 return APValue();
501
502 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
503
504 APValue Result;
505 if (EvaluatePointer(EvalExpr, Result, Info))
506 return Result;
507 return APValue();
508}
Chris Lattner05706e882008-07-11 18:11:29 +0000509
510//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000511// Vector Evaluation
512//===----------------------------------------------------------------------===//
513
514namespace {
515 class VISIBILITY_HIDDEN VectorExprEvaluator
516 : public StmtVisitor<VectorExprEvaluator, APValue> {
517 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000518 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000519 public:
Mike Stump11289f42009-09-09 15:08:12 +0000520
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000521 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000522
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000523 APValue VisitStmt(Stmt *S) {
524 return APValue();
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Eli Friedman3ae59112009-02-23 04:23:56 +0000527 APValue VisitParenExpr(ParenExpr *E)
528 { return Visit(E->getSubExpr()); }
529 APValue VisitUnaryExtension(const UnaryOperator *E)
530 { return Visit(E->getSubExpr()); }
531 APValue VisitUnaryPlus(const UnaryOperator *E)
532 { return Visit(E->getSubExpr()); }
533 APValue VisitUnaryReal(const UnaryOperator *E)
534 { return Visit(E->getSubExpr()); }
535 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
536 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000537 APValue VisitCastExpr(const CastExpr* E);
538 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
539 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000540 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000541 APValue VisitChooseExpr(const ChooseExpr *E)
542 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000543 APValue VisitUnaryImag(const UnaryOperator *E);
544 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000545 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000546 // shufflevector, ExtVectorElementExpr
547 // (Note that these require implementing conversions
548 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000549 };
550} // end anonymous namespace
551
552static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
553 if (!E->getType()->isVectorType())
554 return false;
555 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
556 return !Result.isUninit();
557}
558
559APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000560 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000561 QualType EltTy = VTy->getElementType();
562 unsigned NElts = VTy->getNumElements();
563 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000564
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000565 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000566 QualType SETy = SE->getType();
567 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000568
Nate Begeman2ffd3842009-06-26 18:22:18 +0000569 // Check for vector->vector bitcast and scalar->vector splat.
570 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000571 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000572 } else if (SETy->isIntegerType()) {
573 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000574 if (!EvaluateInteger(SE, IntResult, Info))
575 return APValue();
576 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000577 } else if (SETy->isRealFloatingType()) {
578 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000579 if (!EvaluateFloat(SE, F, Info))
580 return APValue();
581 Result = APValue(F);
582 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000583 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000584
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000585 // For casts of a scalar to ExtVector, convert the scalar to the element type
586 // and splat it to all elements.
587 if (E->getType()->isExtVectorType()) {
588 if (EltTy->isIntegerType() && Result.isInt())
589 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
590 Info.Ctx));
591 else if (EltTy->isIntegerType())
592 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
593 Info.Ctx));
594 else if (EltTy->isRealFloatingType() && Result.isInt())
595 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
596 Info.Ctx));
597 else if (EltTy->isRealFloatingType())
598 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
599 Info.Ctx));
600 else
601 return APValue();
602
603 // Splat and create vector APValue.
604 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
605 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000606 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000607
608 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
609 // to the vector. To construct the APValue vector initializer, bitcast the
610 // initializing value to an APInt, and shift out the bits pertaining to each
611 // element.
612 APSInt Init;
613 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000614
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000615 llvm::SmallVector<APValue, 4> Elts;
616 for (unsigned i = 0; i != NElts; ++i) {
617 APSInt Tmp = Init;
618 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000620 if (EltTy->isIntegerType())
621 Elts.push_back(APValue(Tmp));
622 else if (EltTy->isRealFloatingType())
623 Elts.push_back(APValue(APFloat(Tmp)));
624 else
625 return APValue();
626
627 Init >>= EltWidth;
628 }
629 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000630}
631
Mike Stump11289f42009-09-09 15:08:12 +0000632APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000633VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
634 return this->Visit(const_cast<Expr*>(E->getInitializer()));
635}
636
Mike Stump11289f42009-09-09 15:08:12 +0000637APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000638VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000639 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000640 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000641 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000642
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000643 QualType EltTy = VT->getElementType();
644 llvm::SmallVector<APValue, 4> Elements;
645
Eli Friedman3ae59112009-02-23 04:23:56 +0000646 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000647 if (EltTy->isIntegerType()) {
648 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000649 if (i < NumInits) {
650 if (!EvaluateInteger(E->getInit(i), sInt, Info))
651 return APValue();
652 } else {
653 sInt = Info.Ctx.MakeIntValue(0, EltTy);
654 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000655 Elements.push_back(APValue(sInt));
656 } else {
657 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000658 if (i < NumInits) {
659 if (!EvaluateFloat(E->getInit(i), f, Info))
660 return APValue();
661 } else {
662 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
663 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000664 Elements.push_back(APValue(f));
665 }
666 }
667 return APValue(&Elements[0], Elements.size());
668}
669
Mike Stump11289f42009-09-09 15:08:12 +0000670APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000671VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000672 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000673 QualType EltTy = VT->getElementType();
674 APValue ZeroElement;
675 if (EltTy->isIntegerType())
676 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
677 else
678 ZeroElement =
679 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
680
681 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
682 return APValue(&Elements[0], Elements.size());
683}
684
685APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
686 bool BoolResult;
687 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
688 return APValue();
689
690 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
691
692 APValue Result;
693 if (EvaluateVector(EvalExpr, Result, Info))
694 return Result;
695 return APValue();
696}
697
Eli Friedman3ae59112009-02-23 04:23:56 +0000698APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
699 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
700 Info.EvalResult.HasSideEffects = true;
701 return GetZeroVector(E->getType());
702}
703
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000704//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000705// Integer Evaluation
706//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000707
708namespace {
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000709class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000710 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000711 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000712 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000713public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000714 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000715 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000716
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000717 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000718 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000719 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000720 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000721 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000722 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000723 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000724 return true;
725 }
726
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000727 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000728 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000729 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000730 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000731 Result = APValue(APSInt(I));
732 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000733 return true;
734 }
735
736 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000737 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000738 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000739 return true;
740 }
741
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000742 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000743 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000744 if (Info.EvalResult.Diag == 0) {
745 Info.EvalResult.DiagLoc = L;
746 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000747 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000748 }
Chris Lattner99415702008-07-12 00:14:42 +0000749 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000752 //===--------------------------------------------------------------------===//
753 // Visitor Methods
754 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000756 bool VisitStmt(Stmt *) {
757 assert(0 && "This should be called on integers, stmts are not integers");
758 return false;
759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000761 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000762 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattnere13042c2008-07-11 19:10:17 +0000765 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000766
Chris Lattner7174bf32008-07-12 00:38:25 +0000767 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000768 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000769 }
770 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000771 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000772 }
773 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000774 // Per gcc docs "this built-in function ignores top level
775 // qualifiers". We need to use the canonical version to properly
776 // be able to strip CRV qualifiers from the type.
777 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
778 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000779 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000780 T1.getUnqualifiedType()),
781 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000782 }
783 bool VisitDeclRefExpr(const DeclRefExpr *E);
784 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000785 bool VisitBinaryOperator(const BinaryOperator *E);
786 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000787 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000788
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000789 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000790 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
791
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000792 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000793 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000794 }
Mike Stump11289f42009-09-09 15:08:12 +0000795
Anders Carlsson39def3a2008-12-21 22:39:40 +0000796 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000797 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000798 }
Mike Stump11289f42009-09-09 15:08:12 +0000799
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000800 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000801 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000802 }
803
Eli Friedman4e7a2412009-02-27 04:45:43 +0000804 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
805 return Success(0, E);
806 }
807
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000808 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000809 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000810 }
811
Eli Friedman449fe542009-03-23 04:56:01 +0000812 bool VisitChooseExpr(const ChooseExpr *E) {
813 return Visit(E->getChosenSubExpr(Info.Ctx));
814 }
815
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000816 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000817 bool VisitUnaryImag(const UnaryOperator *E);
818
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000819private:
Chris Lattner68061312009-01-24 21:53:27 +0000820 unsigned GetAlignOfExpr(const Expr *E);
821 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000822 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000823};
Chris Lattner05706e882008-07-11 18:11:29 +0000824} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000825
Daniel Dunbarce399542009-02-20 18:22:23 +0000826static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000827 if (!E->getType()->isIntegralType())
828 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Daniel Dunbarce399542009-02-20 18:22:23 +0000830 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
831}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000832
Daniel Dunbarce399542009-02-20 18:22:23 +0000833static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
834 APValue Val;
835 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
836 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000837 Result = Val.getInt();
838 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000839}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000840
Chris Lattner7174bf32008-07-12 00:38:25 +0000841bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
842 // Enums are integer constant exprs.
843 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000844 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000845 // signedness consistently; see PR3173.
846 APSInt SI = D->getInitVal();
847 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
848 // FIXME: This is an ugly hack around the fact that enums don't
849 // set their width (!?!) consistently; see PR3173.
850 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
851 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000852 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000853
854 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000855 // In C, they can also be folded, although they are not ICEs.
John McCall8ccfcb52009-09-24 19:53:00 +0000856 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000857 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000858 if (APValue *V = D->getEvaluatedValue())
859 return Success(V->getInt(), E);
860 if (const Expr *Init = D->getInit()) {
861 if (Visit(const_cast<Expr*>(Init))) {
862 // Cache the evaluated value in the variable declaration.
863 D->setEvaluatedValue(Info.Ctx, Result);
864 return true;
865 }
866
867 return false;
868 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000869 }
870 }
871
Chris Lattner7174bf32008-07-12 00:38:25 +0000872 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000873 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000874}
875
Chris Lattner86ee2862008-10-06 06:40:35 +0000876/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
877/// as GCC.
878static int EvaluateBuiltinClassifyType(const CallExpr *E) {
879 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000880 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000881 enum gcc_type_class {
882 no_type_class = -1,
883 void_type_class, integer_type_class, char_type_class,
884 enumeral_type_class, boolean_type_class,
885 pointer_type_class, reference_type_class, offset_type_class,
886 real_type_class, complex_type_class,
887 function_type_class, method_type_class,
888 record_type_class, union_type_class,
889 array_type_class, string_type_class,
890 lang_type_class
891 };
Mike Stump11289f42009-09-09 15:08:12 +0000892
893 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000894 // ideal, however it is what gcc does.
895 if (E->getNumArgs() == 0)
896 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner86ee2862008-10-06 06:40:35 +0000898 QualType ArgTy = E->getArg(0)->getType();
899 if (ArgTy->isVoidType())
900 return void_type_class;
901 else if (ArgTy->isEnumeralType())
902 return enumeral_type_class;
903 else if (ArgTy->isBooleanType())
904 return boolean_type_class;
905 else if (ArgTy->isCharType())
906 return string_type_class; // gcc doesn't appear to use char_type_class
907 else if (ArgTy->isIntegerType())
908 return integer_type_class;
909 else if (ArgTy->isPointerType())
910 return pointer_type_class;
911 else if (ArgTy->isReferenceType())
912 return reference_type_class;
913 else if (ArgTy->isRealType())
914 return real_type_class;
915 else if (ArgTy->isComplexType())
916 return complex_type_class;
917 else if (ArgTy->isFunctionType())
918 return function_type_class;
919 else if (ArgTy->isStructureType())
920 return record_type_class;
921 else if (ArgTy->isUnionType())
922 return union_type_class;
923 else if (ArgTy->isArrayType())
924 return array_type_class;
925 else if (ArgTy->isUnionType())
926 return union_type_class;
927 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
928 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
929 return -1;
930}
931
Chris Lattner7174bf32008-07-12 00:38:25 +0000932bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000933 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000934 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000935 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000936
937 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000938 const Expr *Arg = E->getArg(0)->IgnoreParens();
939 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000940 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000941 && Base.Val.getKind() == APValue::LValue
942 && !Base.HasSideEffects)
943 if (const Expr *LVBase = Base.Val.getLValueBase())
944 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
945 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000946 if (!VD->getType()->isIncompleteType()
947 && VD->getType()->isObjectType()
948 && !VD->getType()->isVariablyModifiedType()
949 && !VD->getType()->isDependentType()) {
950 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
951 uint64_t Offset = Base.Val.getLValueOffset();
952 if (Offset <= Size)
953 Size -= Base.Val.getLValueOffset();
954 else
955 Size = 0;
956 return Success(Size, E);
957 }
Mike Stump722cedf2009-10-26 18:35:08 +0000958 }
959 }
960
Mike Stump876387b2009-10-27 22:09:17 +0000961 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000962 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump722cedf2009-10-26 18:35:08 +0000963 return Success(-1, E);
964 return Success(0, E);
965 }
Mike Stump876387b2009-10-27 22:09:17 +0000966
Mike Stump722cedf2009-10-26 18:35:08 +0000967 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
968 }
969
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000970 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000971 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000972
Anders Carlsson4c76e932008-11-24 04:21:33 +0000973 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000974 // __builtin_constant_p always has one operand: it returns true if that
975 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000976 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000977
978 case Builtin::BI__builtin_eh_return_data_regno: {
979 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
980 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
981 return Success(Operand, E);
982 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000983 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000984}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000985
Chris Lattnere13042c2008-07-11 19:10:17 +0000986bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000987 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +0000988 if (!Visit(E->getRHS()))
989 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +0000990
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000991 // If we can't evaluate the LHS, it might have side effects;
992 // conservatively mark it.
993 if (!E->getLHS()->isEvaluatable(Info.Ctx))
994 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000995
Anders Carlsson564730a2008-12-01 02:07:06 +0000996 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000997 }
998
999 if (E->isLogicalOp()) {
1000 // These need to be handled specially because the operands aren't
1001 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001002 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001004 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001005 // We were able to evaluate the LHS, see if we can get away with not
1006 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001007 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001008 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001009
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001010 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001011 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001012 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001013 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001014 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001015 }
1016 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001017 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001018 // We can't evaluate the LHS; however, sometimes the result
1019 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001020 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001021 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001022 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001023 // must have had side effects.
1024 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001025
1026 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001027 }
1028 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001029 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001030
Eli Friedman5a332ea2008-11-13 06:09:17 +00001031 return false;
1032 }
1033
Anders Carlssonacc79812008-11-16 07:17:21 +00001034 QualType LHSTy = E->getLHS()->getType();
1035 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001036
1037 if (LHSTy->isAnyComplexType()) {
1038 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1039 APValue LHS, RHS;
1040
1041 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1042 return false;
1043
1044 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1045 return false;
1046
1047 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001048 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001049 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001050 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001051 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1052
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001053 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001054 return Success((CR_r == APFloat::cmpEqual &&
1055 CR_i == APFloat::cmpEqual), E);
1056 else {
1057 assert(E->getOpcode() == BinaryOperator::NE &&
1058 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001059 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001060 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001061 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001062 CR_i == APFloat::cmpLessThan)), E);
1063 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001064 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001065 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001066 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1067 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1068 else {
1069 assert(E->getOpcode() == BinaryOperator::NE &&
1070 "Invalid compex comparison.");
1071 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1072 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1073 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001074 }
1075 }
Mike Stump11289f42009-09-09 15:08:12 +00001076
Anders Carlssonacc79812008-11-16 07:17:21 +00001077 if (LHSTy->isRealFloatingType() &&
1078 RHSTy->isRealFloatingType()) {
1079 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001080
Anders Carlssonacc79812008-11-16 07:17:21 +00001081 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1082 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001083
Anders Carlssonacc79812008-11-16 07:17:21 +00001084 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1085 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001086
Anders Carlssonacc79812008-11-16 07:17:21 +00001087 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001088
Anders Carlssonacc79812008-11-16 07:17:21 +00001089 switch (E->getOpcode()) {
1090 default:
1091 assert(0 && "Invalid binary operator!");
1092 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001093 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001094 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001095 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001096 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001097 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001098 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001099 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001100 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001101 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001102 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001103 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001104 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001105 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001106 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001107 }
Mike Stump11289f42009-09-09 15:08:12 +00001108
Eli Friedmana38da572009-04-28 19:17:36 +00001109 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1110 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001111 APValue LHSValue;
1112 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1113 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001114
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001115 APValue RHSValue;
1116 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1117 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001118
Eli Friedman334046a2009-06-14 02:17:33 +00001119 // Reject any bases from the normal codepath; we special-case comparisons
1120 // to null.
1121 if (LHSValue.getLValueBase()) {
1122 if (!E->isEqualityOp())
1123 return false;
1124 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1125 return false;
1126 bool bres;
1127 if (!EvalPointerValueAsBool(LHSValue, bres))
1128 return false;
1129 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1130 } else if (RHSValue.getLValueBase()) {
1131 if (!E->isEqualityOp())
1132 return false;
1133 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1134 return false;
1135 bool bres;
1136 if (!EvalPointerValueAsBool(RHSValue, bres))
1137 return false;
1138 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1139 }
Eli Friedman64004332009-03-23 04:38:34 +00001140
Eli Friedmana38da572009-04-28 19:17:36 +00001141 if (E->getOpcode() == BinaryOperator::Sub) {
1142 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001143 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001144
Eli Friedmana38da572009-04-28 19:17:36 +00001145 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001146 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1147 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001148
Eli Friedmana38da572009-04-28 19:17:36 +00001149 return Success(D, E);
1150 }
1151 bool Result;
1152 if (E->getOpcode() == BinaryOperator::EQ) {
1153 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001154 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001155 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1156 }
1157 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001158 }
1159 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001160 if (!LHSTy->isIntegralType() ||
1161 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001162 // We can't continue from here for non-integral types, and they
1163 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001164 return false;
1165 }
1166
Anders Carlsson9c181652008-07-08 14:35:21 +00001167 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001168 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001169 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001170
Eli Friedman94c25c62009-03-24 01:14:50 +00001171 APValue RHSVal;
1172 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001173 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001174
1175 // Handle cases like (unsigned long)&a + 4.
1176 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1177 uint64_t offset = Result.getLValueOffset();
1178 if (E->getOpcode() == BinaryOperator::Add)
1179 offset += RHSVal.getInt().getZExtValue();
1180 else
1181 offset -= RHSVal.getInt().getZExtValue();
1182 Result = APValue(Result.getLValueBase(), offset);
1183 return true;
1184 }
1185
1186 // Handle cases like 4 + (unsigned long)&a
1187 if (E->getOpcode() == BinaryOperator::Add &&
1188 RHSVal.isLValue() && Result.isInt()) {
1189 uint64_t offset = RHSVal.getLValueOffset();
1190 offset += Result.getInt().getZExtValue();
1191 Result = APValue(RHSVal.getLValueBase(), offset);
1192 return true;
1193 }
1194
1195 // All the following cases expect both operands to be an integer
1196 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001197 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001198
Eli Friedman94c25c62009-03-24 01:14:50 +00001199 APSInt& RHS = RHSVal.getInt();
1200
Anders Carlsson9c181652008-07-08 14:35:21 +00001201 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001202 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001203 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001204 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1205 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1206 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1207 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1208 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1209 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001210 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001211 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001212 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001213 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001214 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001215 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001216 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001217 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001218 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001219 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001220 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001221 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1222 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001223 }
1224 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001225 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001226 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1227 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001228 }
Mike Stump11289f42009-09-09 15:08:12 +00001229
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001230 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1231 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1232 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1233 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1234 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1235 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001236 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001237}
1238
Nuno Lopes42042612008-11-16 19:28:31 +00001239bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001240 bool Cond;
1241 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001242 return false;
1243
Nuno Lopes527b5a62008-11-16 22:06:39 +00001244 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001245}
1246
Chris Lattner68061312009-01-24 21:53:27 +00001247unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001248 // Get information about the alignment.
1249 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001250
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001251 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001252 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001253}
1254
Chris Lattner68061312009-01-24 21:53:27 +00001255unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1256 E = E->IgnoreParens();
1257
1258 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001259 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001260 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001261 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedman64004332009-03-23 04:38:34 +00001262
Chris Lattner68061312009-01-24 21:53:27 +00001263 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001264 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattner68061312009-01-24 21:53:27 +00001265
Chris Lattner24aeeab2009-01-24 21:09:06 +00001266 return GetAlignOfType(E->getType());
1267}
1268
1269
Sebastian Redl6f282892008-11-11 17:56:53 +00001270/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1271/// expression's type.
1272bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1273 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001274
Chris Lattner24aeeab2009-01-24 21:09:06 +00001275 // Handle alignof separately.
1276 if (!E->isSizeOf()) {
1277 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001278 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001279 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001280 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001281 }
Eli Friedman64004332009-03-23 04:38:34 +00001282
Sebastian Redl6f282892008-11-11 17:56:53 +00001283 QualType SrcTy = E->getTypeOfArgument();
1284
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001285 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1286 // extension.
1287 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1288 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001289
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001290 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001291 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001292 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001293
Chris Lattner24aeeab2009-01-24 21:09:06 +00001294 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001295 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001296 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001297}
1298
Chris Lattnere13042c2008-07-11 19:10:17 +00001299bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001300 // Special case unary operators that do not need their subexpression
1301 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001302 if (E->isOffsetOfOp()) {
1303 // The AST for offsetof is defined in such a way that we can just
1304 // directly Evaluate it as an l-value.
1305 APValue LV;
1306 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1307 return false;
1308 if (LV.getLValueBase())
1309 return false;
1310 return Success(LV.getLValueOffset(), E);
1311 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001312
1313 if (E->getOpcode() == UnaryOperator::LNot) {
1314 // LNot's operand isn't necessarily an integer, so we handle it specially.
1315 bool bres;
1316 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1317 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001318 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001319 }
1320
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001321 // Only handle integral operations...
1322 if (!E->getSubExpr()->getType()->isIntegralType())
1323 return false;
1324
Chris Lattnercdf34e72008-07-11 22:52:41 +00001325 // Get the operand value into 'Result'.
1326 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001327 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001328
Chris Lattnerf09ad162008-07-11 22:15:16 +00001329 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001330 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001331 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1332 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001333 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001334 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001335 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1336 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001337 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001338 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001339 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001340 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001341 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001342 if (!Result.isInt()) return false;
1343 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001344 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001345 if (!Result.isInt()) return false;
1346 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001347 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001348}
Mike Stump11289f42009-09-09 15:08:12 +00001349
Chris Lattner477c4be2008-07-12 01:15:53 +00001350/// HandleCast - This is used to evaluate implicit or explicit casts where the
1351/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001352bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001353 Expr *SubExpr = E->getSubExpr();
1354 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001355 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001356
Eli Friedman9a156e52008-11-12 09:44:48 +00001357 if (DestType->isBooleanType()) {
1358 bool BoolResult;
1359 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1360 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001361 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001362 }
1363
Anders Carlsson9c181652008-07-08 14:35:21 +00001364 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001365 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001366 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001367 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001368
Eli Friedman742421e2009-02-20 01:15:07 +00001369 if (!Result.isInt()) {
1370 // Only allow casts of lvalues if they are lossless.
1371 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1372 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001373
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001374 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001375 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001376 }
Mike Stump11289f42009-09-09 15:08:12 +00001377
Chris Lattner477c4be2008-07-12 01:15:53 +00001378 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001379 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001380 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001381 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001382 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001383
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001384 if (LV.getLValueBase()) {
1385 // Only allow based lvalue casts if they are lossless.
1386 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1387 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001388
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001389 Result = LV;
1390 return true;
1391 }
1392
1393 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1394 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001395 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001396
Eli Friedman742421e2009-02-20 01:15:07 +00001397 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1398 // This handles double-conversion cases, where there's both
1399 // an l-value promotion and an implicit conversion to int.
1400 APValue LV;
1401 if (!EvaluateLValue(SubExpr, LV, Info))
1402 return false;
1403
1404 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1405 return false;
1406
1407 Result = LV;
1408 return true;
1409 }
1410
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001411 if (SrcType->isAnyComplexType()) {
1412 APValue C;
1413 if (!EvaluateComplex(SubExpr, C, Info))
1414 return false;
1415 if (C.isComplexFloat())
1416 return Success(HandleFloatToIntCast(DestType, SrcType,
1417 C.getComplexFloatReal(), Info.Ctx),
1418 E);
1419 else
1420 return Success(HandleIntToIntCast(DestType, SrcType,
1421 C.getComplexIntReal(), Info.Ctx), E);
1422 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001423 // FIXME: Handle vectors
1424
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001425 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001426 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001427
Eli Friedman24c01542008-08-22 00:06:13 +00001428 APFloat F(0.0);
1429 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001430 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001431
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001432 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001433}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001434
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001435bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1436 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1437 APValue LV;
1438 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1439 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1440 return Success(LV.getComplexIntReal(), E);
1441 }
1442
1443 return Visit(E->getSubExpr());
1444}
1445
Eli Friedman4e7a2412009-02-27 04:45:43 +00001446bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001447 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1448 APValue LV;
1449 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1450 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1451 return Success(LV.getComplexIntImag(), E);
1452 }
1453
Eli Friedman4e7a2412009-02-27 04:45:43 +00001454 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1455 Info.EvalResult.HasSideEffects = true;
1456 return Success(0, E);
1457}
1458
Chris Lattner05706e882008-07-11 18:11:29 +00001459//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001460// Float Evaluation
1461//===----------------------------------------------------------------------===//
1462
1463namespace {
1464class VISIBILITY_HIDDEN FloatExprEvaluator
1465 : public StmtVisitor<FloatExprEvaluator, bool> {
1466 EvalInfo &Info;
1467 APFloat &Result;
1468public:
1469 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1470 : Info(info), Result(result) {}
1471
1472 bool VisitStmt(Stmt *S) {
1473 return false;
1474 }
1475
1476 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001477 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001478
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001479 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001480 bool VisitBinaryOperator(const BinaryOperator *E);
1481 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001482 bool VisitCastExpr(CastExpr *E);
1483 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001484
Eli Friedman449fe542009-03-23 04:56:01 +00001485 bool VisitChooseExpr(const ChooseExpr *E)
1486 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1487 bool VisitUnaryExtension(const UnaryOperator *E)
1488 { return Visit(E->getSubExpr()); }
1489
1490 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1491 // member of vector, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001492 // conditional ?:, comma
Eli Friedman24c01542008-08-22 00:06:13 +00001493};
1494} // end anonymous namespace
1495
1496static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1497 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1498}
1499
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001500bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001501 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001502 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001503 case Builtin::BI__builtin_huge_val:
1504 case Builtin::BI__builtin_huge_valf:
1505 case Builtin::BI__builtin_huge_vall:
1506 case Builtin::BI__builtin_inf:
1507 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001508 case Builtin::BI__builtin_infl: {
1509 const llvm::fltSemantics &Sem =
1510 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001511 Result = llvm::APFloat::getInf(Sem);
1512 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001513 }
Mike Stump11289f42009-09-09 15:08:12 +00001514
Chris Lattner0b7282e2008-10-06 06:31:58 +00001515 case Builtin::BI__builtin_nan:
1516 case Builtin::BI__builtin_nanf:
1517 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001518 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001519 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001520 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001521 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001522 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001523 const llvm::fltSemantics &Sem =
1524 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001525 llvm::SmallString<16> s;
1526 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1527 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001528 long l;
1529 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001530 l = strtol(&s[0], &endp, 0);
1531 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001532 return false;
1533 unsigned type = (unsigned int)l;;
1534 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001535 return true;
1536 }
1537 }
1538 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001539
1540 case Builtin::BI__builtin_fabs:
1541 case Builtin::BI__builtin_fabsf:
1542 case Builtin::BI__builtin_fabsl:
1543 if (!EvaluateFloat(E->getArg(0), Result, Info))
1544 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001546 if (Result.isNegative())
1547 Result.changeSign();
1548 return true;
1549
Mike Stump11289f42009-09-09 15:08:12 +00001550 case Builtin::BI__builtin_copysign:
1551 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001552 case Builtin::BI__builtin_copysignl: {
1553 APFloat RHS(0.);
1554 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1555 !EvaluateFloat(E->getArg(1), RHS, Info))
1556 return false;
1557 Result.copySign(RHS);
1558 return true;
1559 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001560 }
1561}
1562
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001563bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001564 if (E->getOpcode() == UnaryOperator::Deref)
1565 return false;
1566
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001567 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1568 return false;
1569
1570 switch (E->getOpcode()) {
1571 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001572 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001573 return true;
1574 case UnaryOperator::Minus:
1575 Result.changeSign();
1576 return true;
1577 }
1578}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001579
Eli Friedman24c01542008-08-22 00:06:13 +00001580bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1581 // FIXME: Diagnostics? I really don't understand how the warnings
1582 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001583 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001584 if (!EvaluateFloat(E->getLHS(), Result, Info))
1585 return false;
1586 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1587 return false;
1588
1589 switch (E->getOpcode()) {
1590 default: return false;
1591 case BinaryOperator::Mul:
1592 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1593 return true;
1594 case BinaryOperator::Add:
1595 Result.add(RHS, APFloat::rmNearestTiesToEven);
1596 return true;
1597 case BinaryOperator::Sub:
1598 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1599 return true;
1600 case BinaryOperator::Div:
1601 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1602 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001603 }
1604}
1605
1606bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1607 Result = E->getValue();
1608 return true;
1609}
1610
Eli Friedman9a156e52008-11-12 09:44:48 +00001611bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1612 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001613
Eli Friedman9a156e52008-11-12 09:44:48 +00001614 if (SubExpr->getType()->isIntegralType()) {
1615 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001616 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001617 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001618 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001619 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001620 return true;
1621 }
1622 if (SubExpr->getType()->isRealFloatingType()) {
1623 if (!Visit(SubExpr))
1624 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001625 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1626 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001627 return true;
1628 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001629 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001630
1631 return false;
1632}
1633
1634bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1635 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1636 return true;
1637}
1638
Eli Friedman24c01542008-08-22 00:06:13 +00001639//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001640// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001641//===----------------------------------------------------------------------===//
1642
1643namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001644class VISIBILITY_HIDDEN ComplexExprEvaluator
1645 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001646 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001647
Anders Carlsson537969c2008-11-16 20:27:53 +00001648public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001649 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001650
Anders Carlsson537969c2008-11-16 20:27:53 +00001651 //===--------------------------------------------------------------------===//
1652 // Visitor Methods
1653 //===--------------------------------------------------------------------===//
1654
1655 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001656 return APValue();
1657 }
Mike Stump11289f42009-09-09 15:08:12 +00001658
Anders Carlsson537969c2008-11-16 20:27:53 +00001659 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1660
1661 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001662 Expr* SubExpr = E->getSubExpr();
1663
1664 if (SubExpr->getType()->isRealFloatingType()) {
1665 APFloat Result(0.0);
1666
1667 if (!EvaluateFloat(SubExpr, Result, Info))
1668 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001669
1670 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001671 Result);
1672 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001673 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001674 "Unexpected imaginary literal.");
1675
1676 llvm::APSInt Result;
1677 if (!EvaluateInteger(SubExpr, Result, Info))
1678 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001679
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001680 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1681 Zero = 0;
1682 return APValue(Zero, Result);
1683 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001684 }
1685
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001686 APValue VisitCastExpr(CastExpr *E) {
1687 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001688 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001689 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001690
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001691 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001692 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001693
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001694 if (!EvaluateFloat(SubExpr, Result, Info))
1695 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001696
1697 if (EltType->isRealFloatingType()) {
1698 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001699 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001700 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1701 } else {
1702 llvm::APSInt IResult;
1703 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1704 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1705 Zero = 0;
1706 return APValue(IResult, Zero);
1707 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001708 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001709 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001710
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001711 if (!EvaluateInteger(SubExpr, Result, Info))
1712 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001713
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001714 if (EltType->isRealFloatingType()) {
1715 APFloat FResult =
1716 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001717 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001718 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1719 } else {
1720 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1721 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1722 Zero = 0;
1723 return APValue(Result, Zero);
1724 }
John McCall9dd450b2009-09-21 23:43:11 +00001725 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001726 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001727
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001728 if (!EvaluateComplex(SubExpr, Src, Info))
1729 return APValue();
1730
1731 QualType SrcType = CT->getElementType();
1732
1733 if (Src.isComplexFloat()) {
1734 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001735 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001736 Src.getComplexFloatReal(),
1737 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001738 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001739 Src.getComplexFloatImag(),
1740 Info.Ctx));
1741 } else {
1742 return APValue(HandleFloatToIntCast(EltType, SrcType,
1743 Src.getComplexFloatReal(),
1744 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001745 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001746 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001747 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001748 }
1749 } else {
1750 assert(Src.isComplexInt() && "Invalid evaluate result.");
1751 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001752 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001753 Src.getComplexIntReal(),
1754 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001755 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001756 Src.getComplexIntImag(),
1757 Info.Ctx));
1758 } else {
1759 return APValue(HandleIntToIntCast(EltType, SrcType,
1760 Src.getComplexIntReal(),
1761 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001762 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001763 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001764 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001765 }
1766 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001767 }
1768
1769 // FIXME: Handle more casts.
1770 return APValue();
1771 }
Mike Stump11289f42009-09-09 15:08:12 +00001772
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001773 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001774 APValue VisitChooseExpr(const ChooseExpr *E)
1775 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1776 APValue VisitUnaryExtension(const UnaryOperator *E)
1777 { return Visit(E->getSubExpr()); }
1778 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001779 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001780};
1781} // end anonymous namespace
1782
Mike Stump11289f42009-09-09 15:08:12 +00001783static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001784 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1785 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001786 (&Result.getComplexFloatReal().getSemantics() ==
1787 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001788 "Invalid complex evaluation.");
1789 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001790}
1791
Mike Stump11289f42009-09-09 15:08:12 +00001792APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001793 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001794
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001795 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001796 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001797
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001798 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001799 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001800
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001801 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1802 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001803 switch (E->getOpcode()) {
1804 default: return APValue();
1805 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001806 if (Result.isComplexFloat()) {
1807 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1808 APFloat::rmNearestTiesToEven);
1809 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1810 APFloat::rmNearestTiesToEven);
1811 } else {
1812 Result.getComplexIntReal() += RHS.getComplexIntReal();
1813 Result.getComplexIntImag() += RHS.getComplexIntImag();
1814 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001815 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001816 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001817 if (Result.isComplexFloat()) {
1818 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1819 APFloat::rmNearestTiesToEven);
1820 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1821 APFloat::rmNearestTiesToEven);
1822 } else {
1823 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1824 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1825 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001826 break;
1827 case BinaryOperator::Mul:
1828 if (Result.isComplexFloat()) {
1829 APValue LHS = Result;
1830 APFloat &LHS_r = LHS.getComplexFloatReal();
1831 APFloat &LHS_i = LHS.getComplexFloatImag();
1832 APFloat &RHS_r = RHS.getComplexFloatReal();
1833 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001834
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001835 APFloat Tmp = LHS_r;
1836 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1837 Result.getComplexFloatReal() = Tmp;
1838 Tmp = LHS_i;
1839 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1840 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1841
1842 Tmp = LHS_r;
1843 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1844 Result.getComplexFloatImag() = Tmp;
1845 Tmp = LHS_i;
1846 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1847 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1848 } else {
1849 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001850 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001851 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1852 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001853 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001854 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1855 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1856 }
1857 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001858 }
1859
1860 return Result;
1861}
1862
Anders Carlsson537969c2008-11-16 20:27:53 +00001863//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001864// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001865//===----------------------------------------------------------------------===//
1866
Chris Lattner67d7b922008-11-16 21:24:15 +00001867/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001868/// any crazy technique (that has nothing to do with language standards) that
1869/// we want to. If this function returns true, it returns the folded constant
1870/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001871bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1872 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001873
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001874 if (getType()->isVectorType()) {
1875 if (!EvaluateVector(this, Result.Val, Info))
1876 return false;
1877 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001878 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001879 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001880 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001881 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001882 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001883 } else if (getType()->isRealFloatingType()) {
1884 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001885 if (!EvaluateFloat(this, f, Info))
1886 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001887
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001888 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001889 } else if (getType()->isAnyComplexType()) {
1890 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001891 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001892 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001893 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001894
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001895 return true;
1896}
1897
Mike Stump5183a142009-10-26 23:05:19 +00001898bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1899 EvalInfo Info(Ctx, Result, true);
1900
1901 if (getType()->isVectorType()) {
1902 if (!EvaluateVector(this, Result.Val, Info))
1903 return false;
1904 } else if (getType()->isIntegerType()) {
1905 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1906 return false;
1907 } else if (getType()->hasPointerRepresentation()) {
1908 if (!EvaluatePointer(this, Result.Val, Info))
1909 return false;
1910 } else if (getType()->isRealFloatingType()) {
1911 llvm::APFloat f(0.0);
1912 if (!EvaluateFloat(this, f, Info))
1913 return false;
1914
1915 Result.Val = APValue(f);
1916 } else if (getType()->isAnyComplexType()) {
1917 if (!EvaluateComplex(this, Result.Val, Info))
1918 return false;
1919 } else
1920 return false;
1921
1922 return true;
1923}
1924
Anders Carlsson43168122009-04-10 04:54:13 +00001925bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1926 EvalInfo Info(Ctx, Result);
1927
1928 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1929}
1930
Eli Friedman7d45c482009-09-13 10:17:44 +00001931bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1932 EvalInfo Info(Ctx, Result, true);
1933
1934 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1935}
1936
Chris Lattner67d7b922008-11-16 21:24:15 +00001937/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001938/// folded, but discard the result.
1939bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001940 EvalResult Result;
1941 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001942}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001943
1944APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001945 EvalResult EvalResult;
1946 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001947 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001948 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001949 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001950
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001951 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001952}