blob: 9c31ac936d4fc020142186670907364ce51a77f2 [file] [log] [blame]
Chris Lattnere13042c2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlsson7a241ba2008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Anders Carlsson0a1707c2008-07-08 05:13:58 +000022#include "llvm/Support/Compiler.h"
Mike Stump2346cd22009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlsson7a241ba2008-07-03 04:20:39 +000025using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000028
Chris Lattnercdf34e72008-07-11 22:52:41 +000029/// EvalInfo - This is a private struct used by the evaluator to capture
30/// information about a subexpression as it is folded. It retains information
31/// about the AST context, but also maintains information about the folded
32/// expression.
33///
34/// If an expression could be evaluated, it is still possible it is not a C
35/// "integer constant expression" or constant expression. If not, this struct
36/// captures information about how and why not.
37///
38/// One bit of information passed *into* the request for constant folding
39/// indicates whether the subexpression is "evaluated" or not according to C
40/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
41/// evaluate the expression regardless of what the RHS is, but C only allows
42/// certain things in certain situations.
43struct EvalInfo {
44 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000045
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000048
Eli Friedman7d45c482009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattnercdf34e72008-07-11 22:52:41 +000055};
56
57
Eli Friedman9a156e52008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattner6c4d2552009-10-28 23:59:40 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
62 EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +000063static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +000064static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +000065
66//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +000067// Misc utilities
68//===----------------------------------------------------------------------===//
69
Eli Friedman334046a2009-06-14 02:17:33 +000070static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
71 // FIXME: Is this accurate for all kinds of bases? If not, what would
72 // the check look like?
73 Result = Value.getLValueBase() || Value.getLValueOffset();
74 return true;
75}
76
Eli Friedman9a156e52008-11-12 09:44:48 +000077static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
78 if (E->getType()->isIntegralType()) {
79 APSInt IntResult;
80 if (!EvaluateInteger(E, IntResult, Info))
81 return false;
82 Result = IntResult != 0;
83 return true;
84 } else if (E->getType()->isRealFloatingType()) {
85 APFloat FloatResult(0.0);
86 if (!EvaluateFloat(E, FloatResult, Info))
87 return false;
88 Result = !FloatResult.isZero();
89 return true;
Eli Friedman64004332009-03-23 04:38:34 +000090 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000091 APValue PointerResult;
92 if (!EvaluatePointer(E, PointerResult, Info))
93 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000094 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +000095 } else if (E->getType()->isAnyComplexType()) {
96 APValue ComplexResult;
97 if (!EvaluateComplex(E, ComplexResult, Info))
98 return false;
99 if (ComplexResult.isComplexFloat()) {
100 Result = !ComplexResult.getComplexFloatReal().isZero() ||
101 !ComplexResult.getComplexFloatImag().isZero();
102 } else {
103 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
104 ComplexResult.getComplexIntImag().getBoolValue();
105 }
106 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000107 }
108
109 return false;
110}
111
Mike Stump11289f42009-09-09 15:08:12 +0000112static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000113 APFloat &Value, ASTContext &Ctx) {
114 unsigned DestWidth = Ctx.getIntWidth(DestType);
115 // Determine whether we are converting to unsigned or signed.
116 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000118 // FIXME: Warning for overflow.
119 uint64_t Space[4];
120 bool ignored;
121 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
122 llvm::APFloat::rmTowardZero, &ignored);
123 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
124}
125
Mike Stump11289f42009-09-09 15:08:12 +0000126static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000127 APFloat &Value, ASTContext &Ctx) {
128 bool ignored;
129 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000130 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000131 APFloat::rmNearestTiesToEven, &ignored);
132 return Result;
133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000136 APSInt &Value, ASTContext &Ctx) {
137 unsigned DestWidth = Ctx.getIntWidth(DestType);
138 APSInt Result = Value;
139 // Figure out if this is a truncate, extend or noop cast.
140 // If the input is signed, do a sign extend, noop, or truncate.
141 Result.extOrTrunc(DestWidth);
142 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
143 return Result;
144}
145
Mike Stump11289f42009-09-09 15:08:12 +0000146static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000147 APSInt &Value, ASTContext &Ctx) {
148
149 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
150 Result.convertFromAPInt(Value, Value.isSigned(),
151 APFloat::rmNearestTiesToEven);
152 return Result;
153}
154
Mike Stump876387b2009-10-27 22:09:17 +0000155namespace {
156class VISIBILITY_HIDDEN HasSideEffect
157 : public StmtVisitor<HasSideEffect, bool> {
158 EvalInfo &Info;
159public:
160
161 HasSideEffect(EvalInfo &info) : Info(info) {}
162
163 // Unhandled nodes conservatively default to having side effects.
164 bool VisitStmt(Stmt *S) {
165 return true;
166 }
167
168 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
169 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000170 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000171 return true;
172 return false;
173 }
174 // We don't want to evaluate BlockExprs multiple times, as they generate
175 // a ton of code.
176 bool VisitBlockExpr(BlockExpr *E) { return true; }
177 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
178 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
179 { return Visit(E->getInitializer()); }
180 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
181 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
182 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
183 bool VisitStringLiteral(StringLiteral *E) { return false; }
184 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
185 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
186 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stumpfa502902009-10-29 20:48:09 +0000187 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000188 bool VisitChooseExpr(ChooseExpr *E)
189 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
190 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
191 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000192 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000193 bool VisitBinaryOperator(BinaryOperator *E)
194 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000195 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000200 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000201 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000202 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000203 }
204 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
205};
206
Mike Stump876387b2009-10-27 22:09:17 +0000207} // end anonymous namespace
208
Eli Friedman9a156e52008-11-12 09:44:48 +0000209//===----------------------------------------------------------------------===//
210// LValue Evaluation
211//===----------------------------------------------------------------------===//
212namespace {
213class VISIBILITY_HIDDEN LValueExprEvaluator
214 : public StmtVisitor<LValueExprEvaluator, APValue> {
215 EvalInfo &Info;
216public:
Mike Stump11289f42009-09-09 15:08:12 +0000217
Eli Friedman9a156e52008-11-12 09:44:48 +0000218 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
219
220 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000221 return APValue();
222 }
223
224 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000225 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000226 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000232 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000233 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000234 APValue VisitUnaryExtension(const UnaryOperator *E)
235 { return Visit(E->getSubExpr()); }
236 APValue VisitChooseExpr(const ChooseExpr *E)
237 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000238
239 APValue VisitCastExpr(CastExpr *E) {
240 switch (E->getCastKind()) {
241 default:
242 return APValue();
243
244 case CastExpr::CK_NoOp:
245 return Visit(E->getSubExpr());
246 }
247 }
Eli Friedman449fe542009-03-23 04:56:01 +0000248 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000249};
250} // end anonymous namespace
251
252static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
253 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
254 return Result.isLValue();
255}
256
Mike Stump11289f42009-09-09 15:08:12 +0000257APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000258 if (isa<FunctionDecl>(E->getDecl())) {
259 return APValue(E, 0);
260 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000261 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000262 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000263 if (!VD->getType()->isReferenceType())
264 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Douglas Gregor0840cc02009-11-01 20:32:48 +0000266 const VarDecl *Def = 0;
267 if (const Expr *Init = VD->getDefinition(Def))
268 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000269 }
270
271 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000272}
273
Mike Stump11289f42009-09-09 15:08:12 +0000274APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000275 if (E->hasBlockDeclRefExprs())
276 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000277
Steve Naroffa0c32702009-04-16 19:02:57 +0000278 return APValue(E, 0);
279}
280
Eli Friedman9a156e52008-11-12 09:44:48 +0000281APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000282 if (!Info.AnyLValue && !E->isFileScope())
283 return APValue();
284 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000285}
286
287APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
288 APValue result;
289 QualType Ty;
290 if (E->isArrow()) {
291 if (!EvaluatePointer(E->getBase(), result, Info))
292 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000293 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000294 } else {
295 result = Visit(E->getBase());
296 if (result.isUninit())
297 return APValue();
298 Ty = E->getBase()->getType();
299 }
300
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000301 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000302 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000303
304 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
305 if (!FD) // FIXME: deal with other kinds of member expressions
306 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000307
308 if (FD->getType()->isReferenceType())
309 return APValue();
310
Eli Friedman9a156e52008-11-12 09:44:48 +0000311 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000312 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000313 for (RecordDecl::field_iterator Field = RD->field_begin(),
314 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000315 Field != FieldEnd; (void)++Field, ++i) {
316 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000317 break;
318 }
319
320 result.setLValue(result.getLValueBase(),
321 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
322
323 return result;
324}
325
Mike Stump11289f42009-09-09 15:08:12 +0000326APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000327 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000328
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000329 if (!EvaluatePointer(E->getBase(), Result, Info))
330 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000331
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000332 APSInt Index;
333 if (!EvaluateInteger(E->getIdx(), Index, Info))
334 return APValue();
335
336 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
337
338 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000339 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000340 Result.getLValueOffset() + Offset);
341 return Result;
342}
Eli Friedman9a156e52008-11-12 09:44:48 +0000343
Mike Stump11289f42009-09-09 15:08:12 +0000344APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000345 APValue Result;
346 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
347 return APValue();
348 return Result;
349}
350
Eli Friedman9a156e52008-11-12 09:44:48 +0000351//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000352// Pointer Evaluation
353//===----------------------------------------------------------------------===//
354
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000355namespace {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000356class VISIBILITY_HIDDEN PointerExprEvaluator
357 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000358 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000359public:
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnercdf34e72008-07-11 22:52:41 +0000361 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000362
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000363 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000364 return APValue();
365 }
366
367 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
368
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000369 APValue VisitBinaryOperator(const BinaryOperator *E);
370 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000371 APValue VisitUnaryExtension(const UnaryOperator *E)
372 { return Visit(E->getSubExpr()); }
373 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000374 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
375 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000376 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
377 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000378 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000379 APValue VisitBlockExpr(BlockExpr *E) {
380 if (!E->hasBlockDeclRefExprs())
381 return APValue(E, 0);
382 return APValue();
383 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000384 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
385 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000386 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000387 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000388 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
389 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
390 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000391 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000392};
Chris Lattner05706e882008-07-11 18:11:29 +0000393} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000394
Chris Lattnercdf34e72008-07-11 22:52:41 +0000395static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000396 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000397 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000398 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000399 return Result.isLValue();
400}
401
402APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
403 if (E->getOpcode() != BinaryOperator::Add &&
404 E->getOpcode() != BinaryOperator::Sub)
405 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner05706e882008-07-11 18:11:29 +0000407 const Expr *PExp = E->getLHS();
408 const Expr *IExp = E->getRHS();
409 if (IExp->getType()->isPointerType())
410 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner05706e882008-07-11 18:11:29 +0000412 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000413 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000414 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner05706e882008-07-11 18:11:29 +0000416 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000417 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000418 return APValue();
419
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000420 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000421 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlssonef56fba2009-02-19 04:55:58 +0000423 // Explicitly handle GNU void* and function pointer arithmetic extensions.
424 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
425 SizeOfPointee = 1;
426 else
427 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000428
Chris Lattner05706e882008-07-11 18:11:29 +0000429 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000430
Chris Lattner05706e882008-07-11 18:11:29 +0000431 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000432 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000433 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000434 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
435
Chris Lattner05706e882008-07-11 18:11:29 +0000436 return APValue(ResultLValue.getLValueBase(), Offset);
437}
Eli Friedman9a156e52008-11-12 09:44:48 +0000438
Eli Friedmanc2b50172009-02-22 11:46:18 +0000439APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
440 APValue result;
441 if (EvaluateLValue(E->getSubExpr(), result, Info))
442 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000443 return APValue();
444}
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner05706e882008-07-11 18:11:29 +0000446
Chris Lattnere13042c2008-07-11 19:10:17 +0000447APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000448 const Expr* SubExpr = E->getSubExpr();
449
450 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000451 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000452 SubExpr->getType()->isObjCObjectPointerType() ||
453 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000454 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000455 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000456 return Result;
457 return APValue();
458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Eli Friedmanbd840592008-07-27 05:46:18 +0000460 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000461 APValue Result;
462 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
463 return APValue();
464
465 if (Result.isInt()) {
466 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
467 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000468 }
Mike Stump11289f42009-09-09 15:08:12 +0000469
Daniel Dunbarce399542009-02-20 18:22:23 +0000470 // Cast is of an lvalue, no need to change value.
471 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000472 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000473
474 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000475 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000476 SubExpr->getType()->isArrayType()) {
477 APValue Result;
478 if (EvaluateLValue(SubExpr, Result, Info))
479 return Result;
480 return APValue();
481 }
482
Chris Lattner05706e882008-07-11 18:11:29 +0000483 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000484}
Chris Lattner05706e882008-07-11 18:11:29 +0000485
Eli Friedmanc69d4542009-01-25 01:54:01 +0000486APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000487 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000488 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000489 return APValue(E, 0);
490 return APValue();
491}
492
Eli Friedman9a156e52008-11-12 09:44:48 +0000493APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
494 bool BoolResult;
495 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
496 return APValue();
497
498 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
499
500 APValue Result;
501 if (EvaluatePointer(EvalExpr, Result, Info))
502 return Result;
503 return APValue();
504}
Chris Lattner05706e882008-07-11 18:11:29 +0000505
506//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000507// Vector Evaluation
508//===----------------------------------------------------------------------===//
509
510namespace {
511 class VISIBILITY_HIDDEN VectorExprEvaluator
512 : public StmtVisitor<VectorExprEvaluator, APValue> {
513 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000514 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000515 public:
Mike Stump11289f42009-09-09 15:08:12 +0000516
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000517 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000518
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000519 APValue VisitStmt(Stmt *S) {
520 return APValue();
521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Eli Friedman3ae59112009-02-23 04:23:56 +0000523 APValue VisitParenExpr(ParenExpr *E)
524 { return Visit(E->getSubExpr()); }
525 APValue VisitUnaryExtension(const UnaryOperator *E)
526 { return Visit(E->getSubExpr()); }
527 APValue VisitUnaryPlus(const UnaryOperator *E)
528 { return Visit(E->getSubExpr()); }
529 APValue VisitUnaryReal(const UnaryOperator *E)
530 { return Visit(E->getSubExpr()); }
531 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
532 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000533 APValue VisitCastExpr(const CastExpr* E);
534 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
535 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000536 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000537 APValue VisitChooseExpr(const ChooseExpr *E)
538 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000539 APValue VisitUnaryImag(const UnaryOperator *E);
540 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000541 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000542 // shufflevector, ExtVectorElementExpr
543 // (Note that these require implementing conversions
544 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000545 };
546} // end anonymous namespace
547
548static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
549 if (!E->getType()->isVectorType())
550 return false;
551 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
552 return !Result.isUninit();
553}
554
555APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000556 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000557 QualType EltTy = VTy->getElementType();
558 unsigned NElts = VTy->getNumElements();
559 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000560
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000561 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000562 QualType SETy = SE->getType();
563 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000564
Nate Begeman2ffd3842009-06-26 18:22:18 +0000565 // Check for vector->vector bitcast and scalar->vector splat.
566 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000567 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000568 } else if (SETy->isIntegerType()) {
569 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000570 if (!EvaluateInteger(SE, IntResult, Info))
571 return APValue();
572 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000573 } else if (SETy->isRealFloatingType()) {
574 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000575 if (!EvaluateFloat(SE, F, Info))
576 return APValue();
577 Result = APValue(F);
578 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000579 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000580
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000581 // For casts of a scalar to ExtVector, convert the scalar to the element type
582 // and splat it to all elements.
583 if (E->getType()->isExtVectorType()) {
584 if (EltTy->isIntegerType() && Result.isInt())
585 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
586 Info.Ctx));
587 else if (EltTy->isIntegerType())
588 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
589 Info.Ctx));
590 else if (EltTy->isRealFloatingType() && Result.isInt())
591 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
592 Info.Ctx));
593 else if (EltTy->isRealFloatingType())
594 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
595 Info.Ctx));
596 else
597 return APValue();
598
599 // Splat and create vector APValue.
600 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
601 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000602 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000603
604 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
605 // to the vector. To construct the APValue vector initializer, bitcast the
606 // initializing value to an APInt, and shift out the bits pertaining to each
607 // element.
608 APSInt Init;
609 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000610
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000611 llvm::SmallVector<APValue, 4> Elts;
612 for (unsigned i = 0; i != NElts; ++i) {
613 APSInt Tmp = Init;
614 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000615
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000616 if (EltTy->isIntegerType())
617 Elts.push_back(APValue(Tmp));
618 else if (EltTy->isRealFloatingType())
619 Elts.push_back(APValue(APFloat(Tmp)));
620 else
621 return APValue();
622
623 Init >>= EltWidth;
624 }
625 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000626}
627
Mike Stump11289f42009-09-09 15:08:12 +0000628APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000629VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
630 return this->Visit(const_cast<Expr*>(E->getInitializer()));
631}
632
Mike Stump11289f42009-09-09 15:08:12 +0000633APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000634VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000635 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000636 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000637 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000638
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000639 QualType EltTy = VT->getElementType();
640 llvm::SmallVector<APValue, 4> Elements;
641
Eli Friedman3ae59112009-02-23 04:23:56 +0000642 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000643 if (EltTy->isIntegerType()) {
644 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000645 if (i < NumInits) {
646 if (!EvaluateInteger(E->getInit(i), sInt, Info))
647 return APValue();
648 } else {
649 sInt = Info.Ctx.MakeIntValue(0, EltTy);
650 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000651 Elements.push_back(APValue(sInt));
652 } else {
653 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000654 if (i < NumInits) {
655 if (!EvaluateFloat(E->getInit(i), f, Info))
656 return APValue();
657 } else {
658 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
659 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000660 Elements.push_back(APValue(f));
661 }
662 }
663 return APValue(&Elements[0], Elements.size());
664}
665
Mike Stump11289f42009-09-09 15:08:12 +0000666APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000667VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000668 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000669 QualType EltTy = VT->getElementType();
670 APValue ZeroElement;
671 if (EltTy->isIntegerType())
672 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
673 else
674 ZeroElement =
675 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
676
677 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
678 return APValue(&Elements[0], Elements.size());
679}
680
681APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
682 bool BoolResult;
683 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
684 return APValue();
685
686 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
687
688 APValue Result;
689 if (EvaluateVector(EvalExpr, Result, Info))
690 return Result;
691 return APValue();
692}
693
Eli Friedman3ae59112009-02-23 04:23:56 +0000694APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
695 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
696 Info.EvalResult.HasSideEffects = true;
697 return GetZeroVector(E->getType());
698}
699
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000700//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000701// Integer Evaluation
702//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000703
704namespace {
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000705class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000706 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000707 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000708 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000709public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000710 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000711 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000712
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000713 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000714 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000715 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000716 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000717 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000718 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000719 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000720 return true;
721 }
722
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000723 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000724 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000725 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000726 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000727 Result = APValue(APSInt(I));
728 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000729 return true;
730 }
731
732 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000733 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000734 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000735 return true;
736 }
737
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000738 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000739 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000740 if (Info.EvalResult.Diag == 0) {
741 Info.EvalResult.DiagLoc = L;
742 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000743 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000744 }
Chris Lattner99415702008-07-12 00:14:42 +0000745 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000748 //===--------------------------------------------------------------------===//
749 // Visitor Methods
750 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000752 bool VisitStmt(Stmt *) {
753 assert(0 && "This should be called on integers, stmts are not integers");
754 return false;
755 }
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000757 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000758 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnere13042c2008-07-11 19:10:17 +0000761 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000762
Chris Lattner7174bf32008-07-12 00:38:25 +0000763 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000764 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000765 }
766 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000767 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000768 }
769 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000770 // Per gcc docs "this built-in function ignores top level
771 // qualifiers". We need to use the canonical version to properly
772 // be able to strip CRV qualifiers from the type.
773 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
774 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000775 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000776 T1.getUnqualifiedType()),
777 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000778 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000779
780 bool CheckReferencedDecl(const Expr *E, const Decl *D);
781 bool VisitDeclRefExpr(const DeclRefExpr *E) {
782 return CheckReferencedDecl(E, E->getDecl());
783 }
784 bool VisitMemberExpr(const MemberExpr *E) {
785 if (CheckReferencedDecl(E, E->getMemberDecl())) {
786 // Conservatively assume a MemberExpr will have side-effects
787 Info.EvalResult.HasSideEffects = true;
788 return true;
789 }
790 return false;
791 }
792
Chris Lattner7174bf32008-07-12 00:38:25 +0000793 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000794 bool VisitBinaryOperator(const BinaryOperator *E);
795 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000796 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000797
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000798 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000799 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
800
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000801 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000802 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000803 }
Mike Stump11289f42009-09-09 15:08:12 +0000804
Anders Carlsson39def3a2008-12-21 22:39:40 +0000805 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000806 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000809 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000810 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000811 }
812
Eli Friedman4e7a2412009-02-27 04:45:43 +0000813 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
814 return Success(0, E);
815 }
816
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000817 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000818 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000819 }
820
Eli Friedman449fe542009-03-23 04:56:01 +0000821 bool VisitChooseExpr(const ChooseExpr *E) {
822 return Visit(E->getChosenSubExpr(Info.Ctx));
823 }
824
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000825 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000826 bool VisitUnaryImag(const UnaryOperator *E);
827
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000828private:
Chris Lattner68061312009-01-24 21:53:27 +0000829 unsigned GetAlignOfExpr(const Expr *E);
830 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000831 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000832};
Chris Lattner05706e882008-07-11 18:11:29 +0000833} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000834
Daniel Dunbarce399542009-02-20 18:22:23 +0000835static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000836 if (!E->getType()->isIntegralType())
837 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000838
Daniel Dunbarce399542009-02-20 18:22:23 +0000839 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
840}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000841
Daniel Dunbarce399542009-02-20 18:22:23 +0000842static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
843 APValue Val;
844 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
845 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000846 Result = Val.getInt();
847 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000848}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000849
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000850bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000851 // Enums are integer constant exprs.
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000852 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000853 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000854 // signedness consistently; see PR3173.
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000855 APSInt SI = ECD->getInitVal();
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000856 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
857 // FIXME: This is an ugly hack around the fact that enums don't
858 // set their width (!?!) consistently; see PR3173.
859 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
860 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000861 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000862
863 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000864 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000865 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
866 == Qualifiers::Const) {
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000867 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +0000868 const VarDecl *Def = 0;
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000869 if (const Expr *Init = VD->getDefinition(Def)) {
870 if (APValue *V = VD->getEvaluatedValue())
Douglas Gregor0840cc02009-11-01 20:32:48 +0000871 return Success(V->getInt(), E);
872
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000873 if (Visit(const_cast<Expr*>(Init))) {
874 // Cache the evaluated value in the variable declaration.
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000875 VD->setEvaluatedValue(Info.Ctx, Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000876 return true;
877 }
878
879 return false;
880 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000881 }
882 }
883
Chris Lattner7174bf32008-07-12 00:38:25 +0000884 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000885 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000886}
887
Chris Lattner86ee2862008-10-06 06:40:35 +0000888/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
889/// as GCC.
890static int EvaluateBuiltinClassifyType(const CallExpr *E) {
891 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000892 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000893 enum gcc_type_class {
894 no_type_class = -1,
895 void_type_class, integer_type_class, char_type_class,
896 enumeral_type_class, boolean_type_class,
897 pointer_type_class, reference_type_class, offset_type_class,
898 real_type_class, complex_type_class,
899 function_type_class, method_type_class,
900 record_type_class, union_type_class,
901 array_type_class, string_type_class,
902 lang_type_class
903 };
Mike Stump11289f42009-09-09 15:08:12 +0000904
905 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000906 // ideal, however it is what gcc does.
907 if (E->getNumArgs() == 0)
908 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Chris Lattner86ee2862008-10-06 06:40:35 +0000910 QualType ArgTy = E->getArg(0)->getType();
911 if (ArgTy->isVoidType())
912 return void_type_class;
913 else if (ArgTy->isEnumeralType())
914 return enumeral_type_class;
915 else if (ArgTy->isBooleanType())
916 return boolean_type_class;
917 else if (ArgTy->isCharType())
918 return string_type_class; // gcc doesn't appear to use char_type_class
919 else if (ArgTy->isIntegerType())
920 return integer_type_class;
921 else if (ArgTy->isPointerType())
922 return pointer_type_class;
923 else if (ArgTy->isReferenceType())
924 return reference_type_class;
925 else if (ArgTy->isRealType())
926 return real_type_class;
927 else if (ArgTy->isComplexType())
928 return complex_type_class;
929 else if (ArgTy->isFunctionType())
930 return function_type_class;
931 else if (ArgTy->isStructureType())
932 return record_type_class;
933 else if (ArgTy->isUnionType())
934 return union_type_class;
935 else if (ArgTy->isArrayType())
936 return array_type_class;
937 else if (ArgTy->isUnionType())
938 return union_type_class;
939 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
940 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
941 return -1;
942}
943
Chris Lattner7174bf32008-07-12 00:38:25 +0000944bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000945 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000946 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000947 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000948
949 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000950 const Expr *Arg = E->getArg(0)->IgnoreParens();
951 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000952 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000953 && Base.Val.getKind() == APValue::LValue
954 && !Base.HasSideEffects)
955 if (const Expr *LVBase = Base.Val.getLValueBase())
956 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
957 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000958 if (!VD->getType()->isIncompleteType()
959 && VD->getType()->isObjectType()
960 && !VD->getType()->isVariablyModifiedType()
961 && !VD->getType()->isDependentType()) {
962 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
963 uint64_t Offset = Base.Val.getLValueOffset();
964 if (Offset <= Size)
965 Size -= Base.Val.getLValueOffset();
966 else
967 Size = 0;
968 return Success(Size, E);
969 }
Mike Stump722cedf2009-10-26 18:35:08 +0000970 }
971 }
972
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +0000973 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000974 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Chris Lattner4f105592009-11-03 19:48:51 +0000975 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000976 return Success(0, E);
977 }
Mike Stump876387b2009-10-27 22:09:17 +0000978
Mike Stump722cedf2009-10-26 18:35:08 +0000979 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
980 }
981
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000982 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000983 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Anders Carlsson4c76e932008-11-24 04:21:33 +0000985 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000986 // __builtin_constant_p always has one operand: it returns true if that
987 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000988 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000989
990 case Builtin::BI__builtin_eh_return_data_regno: {
991 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
992 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
993 return Success(Operand, E);
994 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000995 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000996}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000997
Chris Lattnere13042c2008-07-11 19:10:17 +0000998bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000999 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001000 if (!Visit(E->getRHS()))
1001 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001002
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001003 // If we can't evaluate the LHS, it might have side effects;
1004 // conservatively mark it.
1005 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1006 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001007
Anders Carlsson564730a2008-12-01 02:07:06 +00001008 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001009 }
1010
1011 if (E->isLogicalOp()) {
1012 // These need to be handled specially because the operands aren't
1013 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001014 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001016 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001017 // We were able to evaluate the LHS, see if we can get away with not
1018 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001019 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001020 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001021
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001022 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001023 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001024 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001025 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001026 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001027 }
1028 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001029 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001030 // We can't evaluate the LHS; however, sometimes the result
1031 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001032 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001033 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001034 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001035 // must have had side effects.
1036 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001037
1038 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001039 }
1040 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001041 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001042
Eli Friedman5a332ea2008-11-13 06:09:17 +00001043 return false;
1044 }
1045
Anders Carlssonacc79812008-11-16 07:17:21 +00001046 QualType LHSTy = E->getLHS()->getType();
1047 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001048
1049 if (LHSTy->isAnyComplexType()) {
1050 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1051 APValue LHS, RHS;
1052
1053 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1054 return false;
1055
1056 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1057 return false;
1058
1059 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001060 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001061 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001062 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001063 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1064
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001065 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001066 return Success((CR_r == APFloat::cmpEqual &&
1067 CR_i == APFloat::cmpEqual), E);
1068 else {
1069 assert(E->getOpcode() == BinaryOperator::NE &&
1070 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001071 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001072 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001073 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001074 CR_i == APFloat::cmpLessThan)), E);
1075 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001076 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001077 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001078 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1079 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1080 else {
1081 assert(E->getOpcode() == BinaryOperator::NE &&
1082 "Invalid compex comparison.");
1083 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1084 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1085 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001086 }
1087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Anders Carlssonacc79812008-11-16 07:17:21 +00001089 if (LHSTy->isRealFloatingType() &&
1090 RHSTy->isRealFloatingType()) {
1091 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001092
Anders Carlssonacc79812008-11-16 07:17:21 +00001093 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1094 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001095
Anders Carlssonacc79812008-11-16 07:17:21 +00001096 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1097 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Anders Carlssonacc79812008-11-16 07:17:21 +00001099 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001100
Anders Carlssonacc79812008-11-16 07:17:21 +00001101 switch (E->getOpcode()) {
1102 default:
1103 assert(0 && "Invalid binary operator!");
1104 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001105 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001106 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001107 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001108 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001109 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001110 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001111 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001112 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001113 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001114 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001115 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001116 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001117 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001118 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Eli Friedmana38da572009-04-28 19:17:36 +00001121 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1122 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001123 APValue LHSValue;
1124 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1125 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001126
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001127 APValue RHSValue;
1128 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1129 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001130
Eli Friedman334046a2009-06-14 02:17:33 +00001131 // Reject any bases from the normal codepath; we special-case comparisons
1132 // to null.
1133 if (LHSValue.getLValueBase()) {
1134 if (!E->isEqualityOp())
1135 return false;
1136 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1137 return false;
1138 bool bres;
1139 if (!EvalPointerValueAsBool(LHSValue, bres))
1140 return false;
1141 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1142 } else if (RHSValue.getLValueBase()) {
1143 if (!E->isEqualityOp())
1144 return false;
1145 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1146 return false;
1147 bool bres;
1148 if (!EvalPointerValueAsBool(RHSValue, bres))
1149 return false;
1150 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1151 }
Eli Friedman64004332009-03-23 04:38:34 +00001152
Eli Friedmana38da572009-04-28 19:17:36 +00001153 if (E->getOpcode() == BinaryOperator::Sub) {
1154 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001155 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001156
Eli Friedmana38da572009-04-28 19:17:36 +00001157 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001158 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1159 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001160
Eli Friedmana38da572009-04-28 19:17:36 +00001161 return Success(D, E);
1162 }
1163 bool Result;
1164 if (E->getOpcode() == BinaryOperator::EQ) {
1165 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001166 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001167 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1168 }
1169 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001170 }
1171 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001172 if (!LHSTy->isIntegralType() ||
1173 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001174 // We can't continue from here for non-integral types, and they
1175 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001176 return false;
1177 }
1178
Anders Carlsson9c181652008-07-08 14:35:21 +00001179 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001180 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001181 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001182
Eli Friedman94c25c62009-03-24 01:14:50 +00001183 APValue RHSVal;
1184 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001185 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001186
1187 // Handle cases like (unsigned long)&a + 4.
1188 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1189 uint64_t offset = Result.getLValueOffset();
1190 if (E->getOpcode() == BinaryOperator::Add)
1191 offset += RHSVal.getInt().getZExtValue();
1192 else
1193 offset -= RHSVal.getInt().getZExtValue();
1194 Result = APValue(Result.getLValueBase(), offset);
1195 return true;
1196 }
1197
1198 // Handle cases like 4 + (unsigned long)&a
1199 if (E->getOpcode() == BinaryOperator::Add &&
1200 RHSVal.isLValue() && Result.isInt()) {
1201 uint64_t offset = RHSVal.getLValueOffset();
1202 offset += Result.getInt().getZExtValue();
1203 Result = APValue(RHSVal.getLValueBase(), offset);
1204 return true;
1205 }
1206
1207 // All the following cases expect both operands to be an integer
1208 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001209 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001210
Eli Friedman94c25c62009-03-24 01:14:50 +00001211 APSInt& RHS = RHSVal.getInt();
1212
Anders Carlsson9c181652008-07-08 14:35:21 +00001213 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001214 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001215 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001216 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1217 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1218 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1219 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1220 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1221 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001222 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001223 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001224 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001225 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001226 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001227 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001228 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001229 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001230 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001231 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001232 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001233 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1234 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001235 }
1236 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001237 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001238 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1239 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001242 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1243 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1244 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1245 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1246 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1247 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001248 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001249}
1250
Nuno Lopes42042612008-11-16 19:28:31 +00001251bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001252 bool Cond;
1253 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001254 return false;
1255
Nuno Lopes527b5a62008-11-16 22:06:39 +00001256 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001257}
1258
Chris Lattner68061312009-01-24 21:53:27 +00001259unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001260 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1261 // the result is the size of the referenced type."
1262 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1263 // result shall be the alignment of the referenced type."
1264 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1265 T = Ref->getPointeeType();
1266
Chris Lattner24aeeab2009-01-24 21:09:06 +00001267 // Get information about the alignment.
1268 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001269
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001270 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001271 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001272}
1273
Chris Lattner68061312009-01-24 21:53:27 +00001274unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1275 E = E->IgnoreParens();
1276
1277 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001278 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001279 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001280 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001281
Chris Lattner68061312009-01-24 21:53:27 +00001282 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001283 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1284 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001285
Chris Lattner24aeeab2009-01-24 21:09:06 +00001286 return GetAlignOfType(E->getType());
1287}
1288
1289
Sebastian Redl6f282892008-11-11 17:56:53 +00001290/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1291/// expression's type.
1292bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1293 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001294
Chris Lattner24aeeab2009-01-24 21:09:06 +00001295 // Handle alignof separately.
1296 if (!E->isSizeOf()) {
1297 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001298 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001299 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001300 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001301 }
Eli Friedman64004332009-03-23 04:38:34 +00001302
Sebastian Redl6f282892008-11-11 17:56:53 +00001303 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001304 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1305 // the result is the size of the referenced type."
1306 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1307 // result shall be the alignment of the referenced type."
1308 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1309 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001310
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001311 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1312 // extension.
1313 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1314 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001315
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001316 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001317 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001318 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001319
Chris Lattner24aeeab2009-01-24 21:09:06 +00001320 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001321 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001322 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001323}
1324
Chris Lattnere13042c2008-07-11 19:10:17 +00001325bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001326 // Special case unary operators that do not need their subexpression
1327 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001328 if (E->isOffsetOfOp()) {
1329 // The AST for offsetof is defined in such a way that we can just
1330 // directly Evaluate it as an l-value.
1331 APValue LV;
1332 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1333 return false;
1334 if (LV.getLValueBase())
1335 return false;
1336 return Success(LV.getLValueOffset(), E);
1337 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001338
1339 if (E->getOpcode() == UnaryOperator::LNot) {
1340 // LNot's operand isn't necessarily an integer, so we handle it specially.
1341 bool bres;
1342 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1343 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001344 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001345 }
1346
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001347 // Only handle integral operations...
1348 if (!E->getSubExpr()->getType()->isIntegralType())
1349 return false;
1350
Chris Lattnercdf34e72008-07-11 22:52:41 +00001351 // Get the operand value into 'Result'.
1352 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001353 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001354
Chris Lattnerf09ad162008-07-11 22:15:16 +00001355 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001356 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001357 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1358 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001359 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001360 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001361 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1362 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001363 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001364 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001365 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001366 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001367 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001368 if (!Result.isInt()) return false;
1369 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001370 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001371 if (!Result.isInt()) return false;
1372 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001373 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001374}
Mike Stump11289f42009-09-09 15:08:12 +00001375
Chris Lattner477c4be2008-07-12 01:15:53 +00001376/// HandleCast - This is used to evaluate implicit or explicit casts where the
1377/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001378bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001379 Expr *SubExpr = E->getSubExpr();
1380 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001381 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001382
Eli Friedman9a156e52008-11-12 09:44:48 +00001383 if (DestType->isBooleanType()) {
1384 bool BoolResult;
1385 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1386 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001387 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001388 }
1389
Anders Carlsson9c181652008-07-08 14:35:21 +00001390 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001391 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001392 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001393 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001394
Eli Friedman742421e2009-02-20 01:15:07 +00001395 if (!Result.isInt()) {
1396 // Only allow casts of lvalues if they are lossless.
1397 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1398 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001399
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001400 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001401 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner477c4be2008-07-12 01:15:53 +00001404 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001405 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001406 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001407 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001408 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001409
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001410 if (LV.getLValueBase()) {
1411 // Only allow based lvalue casts if they are lossless.
1412 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1413 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001414
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001415 Result = LV;
1416 return true;
1417 }
1418
1419 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1420 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001421 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001422
Eli Friedman742421e2009-02-20 01:15:07 +00001423 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1424 // This handles double-conversion cases, where there's both
1425 // an l-value promotion and an implicit conversion to int.
1426 APValue LV;
1427 if (!EvaluateLValue(SubExpr, LV, Info))
1428 return false;
1429
1430 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1431 return false;
1432
1433 Result = LV;
1434 return true;
1435 }
1436
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001437 if (SrcType->isAnyComplexType()) {
1438 APValue C;
1439 if (!EvaluateComplex(SubExpr, C, Info))
1440 return false;
1441 if (C.isComplexFloat())
1442 return Success(HandleFloatToIntCast(DestType, SrcType,
1443 C.getComplexFloatReal(), Info.Ctx),
1444 E);
1445 else
1446 return Success(HandleIntToIntCast(DestType, SrcType,
1447 C.getComplexIntReal(), Info.Ctx), E);
1448 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001449 // FIXME: Handle vectors
1450
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001451 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001452 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001453
Eli Friedman24c01542008-08-22 00:06:13 +00001454 APFloat F(0.0);
1455 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001456 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001457
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001458 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001459}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001460
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001461bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1462 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1463 APValue LV;
1464 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1465 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1466 return Success(LV.getComplexIntReal(), E);
1467 }
1468
1469 return Visit(E->getSubExpr());
1470}
1471
Eli Friedman4e7a2412009-02-27 04:45:43 +00001472bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001473 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1474 APValue LV;
1475 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1476 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1477 return Success(LV.getComplexIntImag(), E);
1478 }
1479
Eli Friedman4e7a2412009-02-27 04:45:43 +00001480 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1481 Info.EvalResult.HasSideEffects = true;
1482 return Success(0, E);
1483}
1484
Chris Lattner05706e882008-07-11 18:11:29 +00001485//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001486// Float Evaluation
1487//===----------------------------------------------------------------------===//
1488
1489namespace {
1490class VISIBILITY_HIDDEN FloatExprEvaluator
1491 : public StmtVisitor<FloatExprEvaluator, bool> {
1492 EvalInfo &Info;
1493 APFloat &Result;
1494public:
1495 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1496 : Info(info), Result(result) {}
1497
1498 bool VisitStmt(Stmt *S) {
1499 return false;
1500 }
1501
1502 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001503 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001504
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001505 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001506 bool VisitBinaryOperator(const BinaryOperator *E);
1507 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001508 bool VisitCastExpr(CastExpr *E);
1509 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001510
Eli Friedman449fe542009-03-23 04:56:01 +00001511 bool VisitChooseExpr(const ChooseExpr *E)
1512 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1513 bool VisitUnaryExtension(const UnaryOperator *E)
1514 { return Visit(E->getSubExpr()); }
1515
1516 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1517 // member of vector, ImplicitValueInitExpr,
Eli Friedman141fbf32009-11-16 04:25:37 +00001518 // conditional ?:
Eli Friedman24c01542008-08-22 00:06:13 +00001519};
1520} // end anonymous namespace
1521
1522static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1523 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1524}
1525
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001526bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001527 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001528 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001529 case Builtin::BI__builtin_huge_val:
1530 case Builtin::BI__builtin_huge_valf:
1531 case Builtin::BI__builtin_huge_vall:
1532 case Builtin::BI__builtin_inf:
1533 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001534 case Builtin::BI__builtin_infl: {
1535 const llvm::fltSemantics &Sem =
1536 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001537 Result = llvm::APFloat::getInf(Sem);
1538 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner0b7282e2008-10-06 06:31:58 +00001541 case Builtin::BI__builtin_nan:
1542 case Builtin::BI__builtin_nanf:
1543 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001544 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001545 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001546 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001547 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001548 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001549 const llvm::fltSemantics &Sem =
1550 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001551 llvm::SmallString<16> s;
1552 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1553 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001554 long l;
1555 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001556 l = strtol(&s[0], &endp, 0);
1557 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001558 return false;
1559 unsigned type = (unsigned int)l;;
1560 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001561 return true;
1562 }
1563 }
1564 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001565
1566 case Builtin::BI__builtin_fabs:
1567 case Builtin::BI__builtin_fabsf:
1568 case Builtin::BI__builtin_fabsl:
1569 if (!EvaluateFloat(E->getArg(0), Result, Info))
1570 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001571
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001572 if (Result.isNegative())
1573 Result.changeSign();
1574 return true;
1575
Mike Stump11289f42009-09-09 15:08:12 +00001576 case Builtin::BI__builtin_copysign:
1577 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001578 case Builtin::BI__builtin_copysignl: {
1579 APFloat RHS(0.);
1580 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1581 !EvaluateFloat(E->getArg(1), RHS, Info))
1582 return false;
1583 Result.copySign(RHS);
1584 return true;
1585 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001586 }
1587}
1588
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001589bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001590 if (E->getOpcode() == UnaryOperator::Deref)
1591 return false;
1592
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001593 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1594 return false;
1595
1596 switch (E->getOpcode()) {
1597 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001598 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001599 return true;
1600 case UnaryOperator::Minus:
1601 Result.changeSign();
1602 return true;
1603 }
1604}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001605
Eli Friedman24c01542008-08-22 00:06:13 +00001606bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001607 if (E->getOpcode() == BinaryOperator::Comma) {
1608 if (!EvaluateFloat(E->getRHS(), Result, Info))
1609 return false;
1610
1611 // If we can't evaluate the LHS, it might have side effects;
1612 // conservatively mark it.
1613 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1614 Info.EvalResult.HasSideEffects = true;
1615
1616 return true;
1617 }
1618
Eli Friedman24c01542008-08-22 00:06:13 +00001619 // FIXME: Diagnostics? I really don't understand how the warnings
1620 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001621 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001622 if (!EvaluateFloat(E->getLHS(), Result, Info))
1623 return false;
1624 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1625 return false;
1626
1627 switch (E->getOpcode()) {
1628 default: return false;
1629 case BinaryOperator::Mul:
1630 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1631 return true;
1632 case BinaryOperator::Add:
1633 Result.add(RHS, APFloat::rmNearestTiesToEven);
1634 return true;
1635 case BinaryOperator::Sub:
1636 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1637 return true;
1638 case BinaryOperator::Div:
1639 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1640 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001641 }
1642}
1643
1644bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1645 Result = E->getValue();
1646 return true;
1647}
1648
Eli Friedman9a156e52008-11-12 09:44:48 +00001649bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1650 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001651
Eli Friedman9a156e52008-11-12 09:44:48 +00001652 if (SubExpr->getType()->isIntegralType()) {
1653 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001654 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001655 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001656 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001657 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001658 return true;
1659 }
1660 if (SubExpr->getType()->isRealFloatingType()) {
1661 if (!Visit(SubExpr))
1662 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001663 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1664 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001665 return true;
1666 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001667 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001668
1669 return false;
1670}
1671
1672bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1673 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1674 return true;
1675}
1676
Eli Friedman24c01542008-08-22 00:06:13 +00001677//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001678// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001679//===----------------------------------------------------------------------===//
1680
1681namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001682class VISIBILITY_HIDDEN ComplexExprEvaluator
1683 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001684 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001685
Anders Carlsson537969c2008-11-16 20:27:53 +00001686public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001687 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001688
Anders Carlsson537969c2008-11-16 20:27:53 +00001689 //===--------------------------------------------------------------------===//
1690 // Visitor Methods
1691 //===--------------------------------------------------------------------===//
1692
1693 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001694 return APValue();
1695 }
Mike Stump11289f42009-09-09 15:08:12 +00001696
Anders Carlsson537969c2008-11-16 20:27:53 +00001697 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1698
1699 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001700 Expr* SubExpr = E->getSubExpr();
1701
1702 if (SubExpr->getType()->isRealFloatingType()) {
1703 APFloat Result(0.0);
1704
1705 if (!EvaluateFloat(SubExpr, Result, Info))
1706 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001707
1708 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001709 Result);
1710 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001711 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001712 "Unexpected imaginary literal.");
1713
1714 llvm::APSInt Result;
1715 if (!EvaluateInteger(SubExpr, Result, Info))
1716 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001717
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001718 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1719 Zero = 0;
1720 return APValue(Zero, Result);
1721 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001722 }
1723
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001724 APValue VisitCastExpr(CastExpr *E) {
1725 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001726 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001727 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001728
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001729 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001730 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001731
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001732 if (!EvaluateFloat(SubExpr, Result, Info))
1733 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001734
1735 if (EltType->isRealFloatingType()) {
1736 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001737 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001738 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1739 } else {
1740 llvm::APSInt IResult;
1741 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1742 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1743 Zero = 0;
1744 return APValue(IResult, Zero);
1745 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001746 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001747 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001748
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001749 if (!EvaluateInteger(SubExpr, Result, Info))
1750 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001751
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001752 if (EltType->isRealFloatingType()) {
1753 APFloat FResult =
1754 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001755 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001756 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1757 } else {
1758 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1759 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1760 Zero = 0;
1761 return APValue(Result, Zero);
1762 }
John McCall9dd450b2009-09-21 23:43:11 +00001763 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001764 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001765
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001766 if (!EvaluateComplex(SubExpr, Src, Info))
1767 return APValue();
1768
1769 QualType SrcType = CT->getElementType();
1770
1771 if (Src.isComplexFloat()) {
1772 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001773 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001774 Src.getComplexFloatReal(),
1775 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001776 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001777 Src.getComplexFloatImag(),
1778 Info.Ctx));
1779 } else {
1780 return APValue(HandleFloatToIntCast(EltType, SrcType,
1781 Src.getComplexFloatReal(),
1782 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001783 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001784 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001785 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001786 }
1787 } else {
1788 assert(Src.isComplexInt() && "Invalid evaluate result.");
1789 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001790 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001791 Src.getComplexIntReal(),
1792 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001793 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001794 Src.getComplexIntImag(),
1795 Info.Ctx));
1796 } else {
1797 return APValue(HandleIntToIntCast(EltType, SrcType,
1798 Src.getComplexIntReal(),
1799 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001800 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001801 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001802 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001803 }
1804 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001805 }
1806
1807 // FIXME: Handle more casts.
1808 return APValue();
1809 }
Mike Stump11289f42009-09-09 15:08:12 +00001810
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001811 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001812 APValue VisitChooseExpr(const ChooseExpr *E)
1813 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1814 APValue VisitUnaryExtension(const UnaryOperator *E)
1815 { return Visit(E->getSubExpr()); }
1816 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001817 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001818};
1819} // end anonymous namespace
1820
Mike Stump11289f42009-09-09 15:08:12 +00001821static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001822 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1823 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001824 (&Result.getComplexFloatReal().getSemantics() ==
1825 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001826 "Invalid complex evaluation.");
1827 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001828}
1829
Mike Stump11289f42009-09-09 15:08:12 +00001830APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001831 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001832
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001833 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001834 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001835
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001836 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001837 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001838
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001839 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1840 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001841 switch (E->getOpcode()) {
1842 default: return APValue();
1843 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001844 if (Result.isComplexFloat()) {
1845 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1846 APFloat::rmNearestTiesToEven);
1847 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1848 APFloat::rmNearestTiesToEven);
1849 } else {
1850 Result.getComplexIntReal() += RHS.getComplexIntReal();
1851 Result.getComplexIntImag() += RHS.getComplexIntImag();
1852 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001853 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001854 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001855 if (Result.isComplexFloat()) {
1856 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1857 APFloat::rmNearestTiesToEven);
1858 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1859 APFloat::rmNearestTiesToEven);
1860 } else {
1861 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1862 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1863 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001864 break;
1865 case BinaryOperator::Mul:
1866 if (Result.isComplexFloat()) {
1867 APValue LHS = Result;
1868 APFloat &LHS_r = LHS.getComplexFloatReal();
1869 APFloat &LHS_i = LHS.getComplexFloatImag();
1870 APFloat &RHS_r = RHS.getComplexFloatReal();
1871 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001872
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001873 APFloat Tmp = LHS_r;
1874 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1875 Result.getComplexFloatReal() = Tmp;
1876 Tmp = LHS_i;
1877 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1878 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1879
1880 Tmp = LHS_r;
1881 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1882 Result.getComplexFloatImag() = Tmp;
1883 Tmp = LHS_i;
1884 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1885 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1886 } else {
1887 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001888 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001889 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1890 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001891 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001892 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1893 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1894 }
1895 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001896 }
1897
1898 return Result;
1899}
1900
Anders Carlsson537969c2008-11-16 20:27:53 +00001901//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001902// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001903//===----------------------------------------------------------------------===//
1904
Chris Lattner67d7b922008-11-16 21:24:15 +00001905/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001906/// any crazy technique (that has nothing to do with language standards) that
1907/// we want to. If this function returns true, it returns the folded constant
1908/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001909bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1910 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001911
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001912 if (getType()->isVectorType()) {
1913 if (!EvaluateVector(this, Result.Val, Info))
1914 return false;
1915 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001916 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001917 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001918 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001919 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001920 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001921 } else if (getType()->isRealFloatingType()) {
1922 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001923 if (!EvaluateFloat(this, f, Info))
1924 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001925
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001926 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001927 } else if (getType()->isAnyComplexType()) {
1928 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001929 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001930 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001931 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001932
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001933 return true;
1934}
1935
Mike Stump5183a142009-10-26 23:05:19 +00001936bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1937 EvalInfo Info(Ctx, Result, true);
1938
1939 if (getType()->isVectorType()) {
1940 if (!EvaluateVector(this, Result.Val, Info))
1941 return false;
1942 } else if (getType()->isIntegerType()) {
1943 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1944 return false;
1945 } else if (getType()->hasPointerRepresentation()) {
1946 if (!EvaluatePointer(this, Result.Val, Info))
1947 return false;
1948 } else if (getType()->isRealFloatingType()) {
1949 llvm::APFloat f(0.0);
1950 if (!EvaluateFloat(this, f, Info))
1951 return false;
1952
1953 Result.Val = APValue(f);
1954 } else if (getType()->isAnyComplexType()) {
1955 if (!EvaluateComplex(this, Result.Val, Info))
1956 return false;
1957 } else
1958 return false;
1959
1960 return true;
1961}
1962
Anders Carlsson43168122009-04-10 04:54:13 +00001963bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1964 EvalInfo Info(Ctx, Result);
1965
1966 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1967}
1968
Eli Friedman7d45c482009-09-13 10:17:44 +00001969bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1970 EvalInfo Info(Ctx, Result, true);
1971
1972 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1973}
1974
Chris Lattner67d7b922008-11-16 21:24:15 +00001975/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001976/// folded, but discard the result.
1977bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001978 EvalResult Result;
1979 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001980}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001981
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00001982bool Expr::HasSideEffects(ASTContext &Ctx) const {
1983 Expr::EvalResult Result;
1984 EvalInfo Info(Ctx, Result);
1985 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1986}
1987
Anders Carlsson59689ed2008-11-22 21:04:56 +00001988APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001989 EvalResult EvalResult;
1990 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001991 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001992 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001993 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001994
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001995 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001996}