blob: 15a9a06be864f86365a3af96ef3f4d4ca77dbae4 [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"
Mike Stump2346cd22009-05-30 03:56:50 +000022#include <cstring>
23
Anders Carlsson7a241ba2008-07-03 04:20:39 +000024using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000025using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000026using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000027
Chris Lattnercdf34e72008-07-11 22:52:41 +000028/// EvalInfo - This is a private struct used by the evaluator to capture
29/// information about a subexpression as it is folded. It retains information
30/// about the AST context, but also maintains information about the folded
31/// expression.
32///
33/// If an expression could be evaluated, it is still possible it is not a C
34/// "integer constant expression" or constant expression. If not, this struct
35/// captures information about how and why not.
36///
37/// One bit of information passed *into* the request for constant folding
38/// indicates whether the subexpression is "evaluated" or not according to C
39/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
40/// evaluate the expression regardless of what the RHS is, but C only allows
41/// certain things in certain situations.
42struct EvalInfo {
43 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000044
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000045 /// EvalResult - Contains information about the evaluation.
46 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000047
Eli Friedman7d45c482009-09-13 10:17:44 +000048 /// AnyLValue - Stack based LValue results are not discarded.
49 bool AnyLValue;
50
51 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
52 bool anylvalue = false)
53 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattnercdf34e72008-07-11 22:52:41 +000054};
55
56
Eli Friedman9a156e52008-11-12 09:44:48 +000057static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-07-11 22:52:41 +000058static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
59static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattner6c4d2552009-10-28 23:59:40 +000060static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
61 EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman334046a2009-06-14 02:17:33 +000069static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
70 // FIXME: Is this accurate for all kinds of bases? If not, what would
71 // the check look like?
72 Result = Value.getLValueBase() || Value.getLValueOffset();
73 return true;
74}
75
Eli Friedman9a156e52008-11-12 09:44:48 +000076static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
77 if (E->getType()->isIntegralType()) {
78 APSInt IntResult;
79 if (!EvaluateInteger(E, IntResult, Info))
80 return false;
81 Result = IntResult != 0;
82 return true;
83 } else if (E->getType()->isRealFloatingType()) {
84 APFloat FloatResult(0.0);
85 if (!EvaluateFloat(E, FloatResult, Info))
86 return false;
87 Result = !FloatResult.isZero();
88 return true;
Eli Friedman64004332009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-03-23 04:38:34 +000094 } else if (E->getType()->isAnyComplexType()) {
95 APValue ComplexResult;
96 if (!EvaluateComplex(E, ComplexResult, Info))
97 return false;
98 if (ComplexResult.isComplexFloat()) {
99 Result = !ComplexResult.getComplexFloatReal().isZero() ||
100 !ComplexResult.getComplexFloatImag().isZero();
101 } else {
102 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
103 ComplexResult.getComplexIntImag().getBoolValue();
104 }
105 return true;
Eli Friedman9a156e52008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000112 APFloat &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 // Determine whether we are converting to unsigned or signed.
115 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000116
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000117 // FIXME: Warning for overflow.
118 uint64_t Space[4];
119 bool ignored;
120 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
121 llvm::APFloat::rmTowardZero, &ignored);
122 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
123}
124
Mike Stump11289f42009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump11289f42009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000135 APSInt &Value, ASTContext &Ctx) {
136 unsigned DestWidth = Ctx.getIntWidth(DestType);
137 APSInt Result = Value;
138 // Figure out if this is a truncate, extend or noop cast.
139 // If the input is signed, do a sign extend, noop, or truncate.
140 Result.extOrTrunc(DestWidth);
141 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
142 return Result;
143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Mike Stump876387b2009-10-27 22:09:17 +0000154namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000155class HasSideEffect
Mike Stump876387b2009-10-27 22:09:17 +0000156 : public StmtVisitor<HasSideEffect, bool> {
157 EvalInfo &Info;
158public:
159
160 HasSideEffect(EvalInfo &info) : Info(info) {}
161
162 // Unhandled nodes conservatively default to having side effects.
163 bool VisitStmt(Stmt *S) {
164 return true;
165 }
166
167 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
168 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000169 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000170 return true;
171 return false;
172 }
173 // We don't want to evaluate BlockExprs multiple times, as they generate
174 // a ton of code.
175 bool VisitBlockExpr(BlockExpr *E) { return true; }
176 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
177 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
178 { return Visit(E->getInitializer()); }
179 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
180 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
181 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
182 bool VisitStringLiteral(StringLiteral *E) { return false; }
183 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
184 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
185 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stumpfa502902009-10-29 20:48:09 +0000186 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000187 bool VisitChooseExpr(ChooseExpr *E)
188 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
189 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
190 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stumpf3eb5ec2009-10-29 23:34:20 +0000191 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stumpfa502902009-10-29 20:48:09 +0000192 bool VisitBinaryOperator(BinaryOperator *E)
193 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stump876387b2009-10-27 22:09:17 +0000194 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
195 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stump53f9ded2009-11-03 23:25:48 +0000199 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stump876387b2009-10-27 22:09:17 +0000200 return true;
Mike Stumpfa502902009-10-29 20:48:09 +0000201 return Visit(E->getSubExpr());
Mike Stump876387b2009-10-27 22:09:17 +0000202 }
203 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
204};
205
Mike Stump876387b2009-10-27 22:09:17 +0000206} // end anonymous namespace
207
Eli Friedman9a156e52008-11-12 09:44:48 +0000208//===----------------------------------------------------------------------===//
209// LValue Evaluation
210//===----------------------------------------------------------------------===//
211namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000212class LValueExprEvaluator
Eli Friedman9a156e52008-11-12 09:44:48 +0000213 : public StmtVisitor<LValueExprEvaluator, APValue> {
214 EvalInfo &Info;
215public:
Mike Stump11289f42009-09-09 15:08:12 +0000216
Eli Friedman9a156e52008-11-12 09:44:48 +0000217 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
218
219 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000220 return APValue();
221 }
222
223 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000224 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000225 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000226 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
227 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
228 APValue VisitMemberExpr(MemberExpr *E);
229 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000230 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000231 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000232 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000233 APValue VisitUnaryExtension(const UnaryOperator *E)
234 { return Visit(E->getSubExpr()); }
235 APValue VisitChooseExpr(const ChooseExpr *E)
236 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000237
238 APValue VisitCastExpr(CastExpr *E) {
239 switch (E->getCastKind()) {
240 default:
241 return APValue();
242
243 case CastExpr::CK_NoOp:
244 return Visit(E->getSubExpr());
245 }
246 }
Eli Friedman449fe542009-03-23 04:56:01 +0000247 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000248};
249} // end anonymous namespace
250
251static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
252 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
253 return Result.isLValue();
254}
255
Mike Stump11289f42009-09-09 15:08:12 +0000256APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000257 if (isa<FunctionDecl>(E->getDecl())) {
258 return APValue(E, 0);
259 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000260 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000261 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000262 if (!VD->getType()->isReferenceType())
263 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000264 // FIXME: Check whether VD might be overridden!
Douglas Gregor0840cc02009-11-01 20:32:48 +0000265 const VarDecl *Def = 0;
266 if (const Expr *Init = VD->getDefinition(Def))
267 return Visit(const_cast<Expr *>(Init));
Eli Friedman751aa72b72009-05-27 06:04:58 +0000268 }
269
270 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000271}
272
Mike Stump11289f42009-09-09 15:08:12 +0000273APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000274 if (E->hasBlockDeclRefExprs())
275 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000276
Steve Naroffa0c32702009-04-16 19:02:57 +0000277 return APValue(E, 0);
278}
279
Eli Friedman9a156e52008-11-12 09:44:48 +0000280APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000281 if (!Info.AnyLValue && !E->isFileScope())
282 return APValue();
283 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000284}
285
286APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
287 APValue result;
288 QualType Ty;
289 if (E->isArrow()) {
290 if (!EvaluatePointer(E->getBase(), result, Info))
291 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000292 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000293 } else {
294 result = Visit(E->getBase());
295 if (result.isUninit())
296 return APValue();
297 Ty = E->getBase()->getType();
298 }
299
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000300 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000301 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000302
303 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
304 if (!FD) // FIXME: deal with other kinds of member expressions
305 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000306
307 if (FD->getType()->isReferenceType())
308 return APValue();
309
Eli Friedman9a156e52008-11-12 09:44:48 +0000310 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000311 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000312 for (RecordDecl::field_iterator Field = RD->field_begin(),
313 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000314 Field != FieldEnd; (void)++Field, ++i) {
315 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000316 break;
317 }
318
319 result.setLValue(result.getLValueBase(),
320 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
321
322 return result;
323}
324
Mike Stump11289f42009-09-09 15:08:12 +0000325APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000326 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000328 if (!EvaluatePointer(E->getBase(), Result, Info))
329 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000330
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000331 APSInt Index;
332 if (!EvaluateInteger(E->getIdx(), Index, Info))
333 return APValue();
334
335 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
336
337 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000338 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000339 Result.getLValueOffset() + Offset);
340 return Result;
341}
Eli Friedman9a156e52008-11-12 09:44:48 +0000342
Mike Stump11289f42009-09-09 15:08:12 +0000343APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000344 APValue Result;
345 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
346 return APValue();
347 return Result;
348}
349
Eli Friedman9a156e52008-11-12 09:44:48 +0000350//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000351// Pointer Evaluation
352//===----------------------------------------------------------------------===//
353
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000354namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000355class PointerExprEvaluator
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000356 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000357 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000358public:
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattnercdf34e72008-07-11 22:52:41 +0000360 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000361
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000362 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000363 return APValue();
364 }
365
366 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
367
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000368 APValue VisitBinaryOperator(const BinaryOperator *E);
369 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000370 APValue VisitUnaryExtension(const UnaryOperator *E)
371 { return Visit(E->getSubExpr()); }
372 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000373 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
374 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000375 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
376 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000377 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000378 APValue VisitBlockExpr(BlockExpr *E) {
379 if (!E->hasBlockDeclRefExprs())
380 return APValue(E, 0);
381 return APValue();
382 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000383 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
384 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000385 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000386 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000387 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
388 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
389 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000390 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000391};
Chris Lattner05706e882008-07-11 18:11:29 +0000392} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000393
Chris Lattnercdf34e72008-07-11 22:52:41 +0000394static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000395 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000396 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000397 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000398 return Result.isLValue();
399}
400
401APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
402 if (E->getOpcode() != BinaryOperator::Add &&
403 E->getOpcode() != BinaryOperator::Sub)
404 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner05706e882008-07-11 18:11:29 +0000406 const Expr *PExp = E->getLHS();
407 const Expr *IExp = E->getRHS();
408 if (IExp->getType()->isPointerType())
409 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner05706e882008-07-11 18:11:29 +0000411 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000412 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000413 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattner05706e882008-07-11 18:11:29 +0000415 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000416 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000417 return APValue();
418
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000419 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000420 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlssonef56fba2009-02-19 04:55:58 +0000422 // Explicitly handle GNU void* and function pointer arithmetic extensions.
423 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
424 SizeOfPointee = 1;
425 else
426 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000427
Chris Lattner05706e882008-07-11 18:11:29 +0000428 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000429
Chris Lattner05706e882008-07-11 18:11:29 +0000430 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000431 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000432 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000433 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
434
Chris Lattner05706e882008-07-11 18:11:29 +0000435 return APValue(ResultLValue.getLValueBase(), Offset);
436}
Eli Friedman9a156e52008-11-12 09:44:48 +0000437
Eli Friedmanc2b50172009-02-22 11:46:18 +0000438APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
439 APValue result;
440 if (EvaluateLValue(E->getSubExpr(), result, Info))
441 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000442 return APValue();
443}
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattner05706e882008-07-11 18:11:29 +0000445
Chris Lattnere13042c2008-07-11 19:10:17 +0000446APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000447 const Expr* SubExpr = E->getSubExpr();
448
449 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000450 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000451 SubExpr->getType()->isObjCObjectPointerType() ||
452 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000453 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000454 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000455 return Result;
456 return APValue();
457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Eli Friedmanbd840592008-07-27 05:46:18 +0000459 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000460 APValue Result;
461 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
462 return APValue();
463
464 if (Result.isInt()) {
465 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
466 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Daniel Dunbarce399542009-02-20 18:22:23 +0000469 // Cast is of an lvalue, no need to change value.
470 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000471 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000472
473 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000474 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000475 SubExpr->getType()->isArrayType()) {
476 APValue Result;
477 if (EvaluateLValue(SubExpr, Result, Info))
478 return Result;
479 return APValue();
480 }
481
Chris Lattner05706e882008-07-11 18:11:29 +0000482 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000483}
Chris Lattner05706e882008-07-11 18:11:29 +0000484
Eli Friedmanc69d4542009-01-25 01:54:01 +0000485APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000486 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000487 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000488 return APValue(E, 0);
489 return APValue();
490}
491
Eli Friedman9a156e52008-11-12 09:44:48 +0000492APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
493 bool BoolResult;
494 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
495 return APValue();
496
497 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
498
499 APValue Result;
500 if (EvaluatePointer(EvalExpr, Result, Info))
501 return Result;
502 return APValue();
503}
Chris Lattner05706e882008-07-11 18:11:29 +0000504
505//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000506// Vector Evaluation
507//===----------------------------------------------------------------------===//
508
509namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000510 class VectorExprEvaluator
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000511 : public StmtVisitor<VectorExprEvaluator, APValue> {
512 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000513 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000514 public:
Mike Stump11289f42009-09-09 15:08:12 +0000515
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000516 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000517
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000518 APValue VisitStmt(Stmt *S) {
519 return APValue();
520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Eli Friedman3ae59112009-02-23 04:23:56 +0000522 APValue VisitParenExpr(ParenExpr *E)
523 { return Visit(E->getSubExpr()); }
524 APValue VisitUnaryExtension(const UnaryOperator *E)
525 { return Visit(E->getSubExpr()); }
526 APValue VisitUnaryPlus(const UnaryOperator *E)
527 { return Visit(E->getSubExpr()); }
528 APValue VisitUnaryReal(const UnaryOperator *E)
529 { return Visit(E->getSubExpr()); }
530 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
531 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000532 APValue VisitCastExpr(const CastExpr* E);
533 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
534 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000535 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000536 APValue VisitChooseExpr(const ChooseExpr *E)
537 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000538 APValue VisitUnaryImag(const UnaryOperator *E);
539 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000540 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000541 // shufflevector, ExtVectorElementExpr
542 // (Note that these require implementing conversions
543 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000544 };
545} // end anonymous namespace
546
547static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
548 if (!E->getType()->isVectorType())
549 return false;
550 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
551 return !Result.isUninit();
552}
553
554APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000555 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000556 QualType EltTy = VTy->getElementType();
557 unsigned NElts = VTy->getNumElements();
558 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000560 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000561 QualType SETy = SE->getType();
562 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000563
Nate Begeman2ffd3842009-06-26 18:22:18 +0000564 // Check for vector->vector bitcast and scalar->vector splat.
565 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000566 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000567 } else if (SETy->isIntegerType()) {
568 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000569 if (!EvaluateInteger(SE, IntResult, Info))
570 return APValue();
571 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000572 } else if (SETy->isRealFloatingType()) {
573 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000574 if (!EvaluateFloat(SE, F, Info))
575 return APValue();
576 Result = APValue(F);
577 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000578 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000579
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000580 // For casts of a scalar to ExtVector, convert the scalar to the element type
581 // and splat it to all elements.
582 if (E->getType()->isExtVectorType()) {
583 if (EltTy->isIntegerType() && Result.isInt())
584 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
585 Info.Ctx));
586 else if (EltTy->isIntegerType())
587 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
588 Info.Ctx));
589 else if (EltTy->isRealFloatingType() && Result.isInt())
590 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
591 Info.Ctx));
592 else if (EltTy->isRealFloatingType())
593 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
594 Info.Ctx));
595 else
596 return APValue();
597
598 // Splat and create vector APValue.
599 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
600 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000601 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000602
603 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
604 // to the vector. To construct the APValue vector initializer, bitcast the
605 // initializing value to an APInt, and shift out the bits pertaining to each
606 // element.
607 APSInt Init;
608 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000609
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000610 llvm::SmallVector<APValue, 4> Elts;
611 for (unsigned i = 0; i != NElts; ++i) {
612 APSInt Tmp = Init;
613 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000615 if (EltTy->isIntegerType())
616 Elts.push_back(APValue(Tmp));
617 else if (EltTy->isRealFloatingType())
618 Elts.push_back(APValue(APFloat(Tmp)));
619 else
620 return APValue();
621
622 Init >>= EltWidth;
623 }
624 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000625}
626
Mike Stump11289f42009-09-09 15:08:12 +0000627APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000628VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
629 return this->Visit(const_cast<Expr*>(E->getInitializer()));
630}
631
Mike Stump11289f42009-09-09 15:08:12 +0000632APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000633VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000634 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000635 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000636 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000637
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000638 QualType EltTy = VT->getElementType();
639 llvm::SmallVector<APValue, 4> Elements;
640
Eli Friedman3ae59112009-02-23 04:23:56 +0000641 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000642 if (EltTy->isIntegerType()) {
643 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000644 if (i < NumInits) {
645 if (!EvaluateInteger(E->getInit(i), sInt, Info))
646 return APValue();
647 } else {
648 sInt = Info.Ctx.MakeIntValue(0, EltTy);
649 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000650 Elements.push_back(APValue(sInt));
651 } else {
652 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000653 if (i < NumInits) {
654 if (!EvaluateFloat(E->getInit(i), f, Info))
655 return APValue();
656 } else {
657 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
658 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000659 Elements.push_back(APValue(f));
660 }
661 }
662 return APValue(&Elements[0], Elements.size());
663}
664
Mike Stump11289f42009-09-09 15:08:12 +0000665APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000666VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000667 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000668 QualType EltTy = VT->getElementType();
669 APValue ZeroElement;
670 if (EltTy->isIntegerType())
671 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
672 else
673 ZeroElement =
674 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
675
676 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
677 return APValue(&Elements[0], Elements.size());
678}
679
680APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
681 bool BoolResult;
682 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
683 return APValue();
684
685 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
686
687 APValue Result;
688 if (EvaluateVector(EvalExpr, Result, Info))
689 return Result;
690 return APValue();
691}
692
Eli Friedman3ae59112009-02-23 04:23:56 +0000693APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
694 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
695 Info.EvalResult.HasSideEffects = true;
696 return GetZeroVector(E->getType());
697}
698
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000699//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000700// Integer Evaluation
701//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000702
703namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +0000704class IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000705 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000706 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000707 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000708public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000709 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000710 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000711
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000712 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000713 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000714 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000715 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000716 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000717 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000718 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000719 return true;
720 }
721
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000722 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000723 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000724 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000725 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000726 Result = APValue(APSInt(I));
727 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000728 return true;
729 }
730
731 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000732 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000733 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000734 return true;
735 }
736
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000737 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000738 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000739 if (Info.EvalResult.Diag == 0) {
740 Info.EvalResult.DiagLoc = L;
741 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000742 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000743 }
Chris Lattner99415702008-07-12 00:14:42 +0000744 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000747 //===--------------------------------------------------------------------===//
748 // Visitor Methods
749 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000751 bool VisitStmt(Stmt *) {
752 assert(0 && "This should be called on integers, stmts are not integers");
753 return false;
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000756 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000757 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattnere13042c2008-07-11 19:10:17 +0000760 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000761
Chris Lattner7174bf32008-07-12 00:38:25 +0000762 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000763 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000764 }
765 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000766 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000767 }
768 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000769 // Per gcc docs "this built-in function ignores top level
770 // qualifiers". We need to use the canonical version to properly
771 // be able to strip CRV qualifiers from the type.
772 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
773 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000774 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000775 T1.getUnqualifiedType()),
776 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000777 }
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000778
779 bool CheckReferencedDecl(const Expr *E, const Decl *D);
780 bool VisitDeclRefExpr(const DeclRefExpr *E) {
781 return CheckReferencedDecl(E, E->getDecl());
782 }
783 bool VisitMemberExpr(const MemberExpr *E) {
784 if (CheckReferencedDecl(E, E->getMemberDecl())) {
785 // Conservatively assume a MemberExpr will have side-effects
786 Info.EvalResult.HasSideEffects = true;
787 return true;
788 }
789 return false;
790 }
791
Chris Lattner7174bf32008-07-12 00:38:25 +0000792 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000793 bool VisitBinaryOperator(const BinaryOperator *E);
794 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000795 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000796
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000797 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000798 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
799
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000800 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000801 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000802 }
Mike Stump11289f42009-09-09 15:08:12 +0000803
Anders Carlsson39def3a2008-12-21 22:39:40 +0000804 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000805 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000806 }
Mike Stump11289f42009-09-09 15:08:12 +0000807
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000808 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000809 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000810 }
811
Eli Friedman4e7a2412009-02-27 04:45:43 +0000812 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
813 return Success(0, E);
814 }
815
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000816 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000817 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000818 }
819
Eli Friedman449fe542009-03-23 04:56:01 +0000820 bool VisitChooseExpr(const ChooseExpr *E) {
821 return Visit(E->getChosenSubExpr(Info.Ctx));
822 }
823
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000824 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000825 bool VisitUnaryImag(const UnaryOperator *E);
826
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000827private:
Chris Lattner68061312009-01-24 21:53:27 +0000828 unsigned GetAlignOfExpr(const Expr *E);
829 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000830 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000831};
Chris Lattner05706e882008-07-11 18:11:29 +0000832} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000833
Daniel Dunbarce399542009-02-20 18:22:23 +0000834static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000835 if (!E->getType()->isIntegralType())
836 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Daniel Dunbarce399542009-02-20 18:22:23 +0000838 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
839}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000840
Daniel Dunbarce399542009-02-20 18:22:23 +0000841static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
842 APValue Val;
843 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
844 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000845 Result = Val.getInt();
846 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000847}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000848
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000849bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner7174bf32008-07-12 00:38:25 +0000850 // Enums are integer constant exprs.
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000851 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000852 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000853 // signedness consistently; see PR3173.
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000854 APSInt SI = ECD->getInitVal();
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000855 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
856 // FIXME: This is an ugly hack around the fact that enums don't
857 // set their width (!?!) consistently; see PR3173.
858 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
859 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000860 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000861
862 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000863 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000864 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
865 == Qualifiers::Const) {
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000866 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +0000867 const VarDecl *Def = 0;
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000868 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000869 if (APValue *V = VD->getEvaluatedValue()) {
870 if (V->isInt())
871 return Success(V->getInt(), E);
872 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
873 }
874
875 if (VD->isEvaluatingValue())
876 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
877
878 VD->setEvaluatingValue();
879
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000880 if (Visit(const_cast<Expr*>(Init))) {
881 // Cache the evaluated value in the variable declaration.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000882 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000883 return true;
884 }
885
Eli Friedman1d6fb162009-12-03 20:31:57 +0000886 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000887 return false;
888 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000889 }
890 }
891
Chris Lattner7174bf32008-07-12 00:38:25 +0000892 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000893 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000894}
895
Chris Lattner86ee2862008-10-06 06:40:35 +0000896/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
897/// as GCC.
898static int EvaluateBuiltinClassifyType(const CallExpr *E) {
899 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000900 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000901 enum gcc_type_class {
902 no_type_class = -1,
903 void_type_class, integer_type_class, char_type_class,
904 enumeral_type_class, boolean_type_class,
905 pointer_type_class, reference_type_class, offset_type_class,
906 real_type_class, complex_type_class,
907 function_type_class, method_type_class,
908 record_type_class, union_type_class,
909 array_type_class, string_type_class,
910 lang_type_class
911 };
Mike Stump11289f42009-09-09 15:08:12 +0000912
913 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000914 // ideal, however it is what gcc does.
915 if (E->getNumArgs() == 0)
916 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner86ee2862008-10-06 06:40:35 +0000918 QualType ArgTy = E->getArg(0)->getType();
919 if (ArgTy->isVoidType())
920 return void_type_class;
921 else if (ArgTy->isEnumeralType())
922 return enumeral_type_class;
923 else if (ArgTy->isBooleanType())
924 return boolean_type_class;
925 else if (ArgTy->isCharType())
926 return string_type_class; // gcc doesn't appear to use char_type_class
927 else if (ArgTy->isIntegerType())
928 return integer_type_class;
929 else if (ArgTy->isPointerType())
930 return pointer_type_class;
931 else if (ArgTy->isReferenceType())
932 return reference_type_class;
933 else if (ArgTy->isRealType())
934 return real_type_class;
935 else if (ArgTy->isComplexType())
936 return complex_type_class;
937 else if (ArgTy->isFunctionType())
938 return function_type_class;
939 else if (ArgTy->isStructureType())
940 return record_type_class;
941 else if (ArgTy->isUnionType())
942 return union_type_class;
943 else if (ArgTy->isArrayType())
944 return array_type_class;
945 else if (ArgTy->isUnionType())
946 return union_type_class;
947 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
948 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
949 return -1;
950}
951
Chris Lattner7174bf32008-07-12 00:38:25 +0000952bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000953 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000954 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000955 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000956
957 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000958 const Expr *Arg = E->getArg(0)->IgnoreParens();
959 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000960 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000961 && Base.Val.getKind() == APValue::LValue
962 && !Base.HasSideEffects)
963 if (const Expr *LVBase = Base.Val.getLValueBase())
964 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
965 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000966 if (!VD->getType()->isIncompleteType()
967 && VD->getType()->isObjectType()
968 && !VD->getType()->isVariablyModifiedType()
969 && !VD->getType()->isDependentType()) {
970 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
971 uint64_t Offset = Base.Val.getLValueOffset();
972 if (Offset <= Size)
973 Size -= Base.Val.getLValueOffset();
974 else
975 Size = 0;
976 return Success(Size, E);
977 }
Mike Stump722cedf2009-10-26 18:35:08 +0000978 }
979 }
980
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +0000981 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000982 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Chris Lattner4f105592009-11-03 19:48:51 +0000983 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000984 return Success(0, E);
985 }
Mike Stump876387b2009-10-27 22:09:17 +0000986
Mike Stump722cedf2009-10-26 18:35:08 +0000987 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
988 }
989
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000990 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000991 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000992
Anders Carlsson4c76e932008-11-24 04:21:33 +0000993 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000994 // __builtin_constant_p always has one operand: it returns true if that
995 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000996 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000997
998 case Builtin::BI__builtin_eh_return_data_regno: {
999 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1000 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1001 return Success(Operand, E);
1002 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001003 }
Chris Lattner7174bf32008-07-12 00:38:25 +00001004}
Anders Carlsson4a3585b2008-07-08 15:34:11 +00001005
Chris Lattnere13042c2008-07-11 19:10:17 +00001006bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001007 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001008 if (!Visit(E->getRHS()))
1009 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001010
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001011 // If we can't evaluate the LHS, it might have side effects;
1012 // conservatively mark it.
1013 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1014 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001015
Anders Carlsson564730a2008-12-01 02:07:06 +00001016 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001017 }
1018
1019 if (E->isLogicalOp()) {
1020 // These need to be handled specially because the operands aren't
1021 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001022 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001024 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001025 // We were able to evaluate the LHS, see if we can get away with not
1026 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001027 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001028 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001029
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001030 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001031 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001032 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001033 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001034 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001035 }
1036 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001037 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001038 // We can't evaluate the LHS; however, sometimes the result
1039 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001040 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001041 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001042 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001043 // must have had side effects.
1044 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001045
1046 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001047 }
1048 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001049 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001050
Eli Friedman5a332ea2008-11-13 06:09:17 +00001051 return false;
1052 }
1053
Anders Carlssonacc79812008-11-16 07:17:21 +00001054 QualType LHSTy = E->getLHS()->getType();
1055 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001056
1057 if (LHSTy->isAnyComplexType()) {
1058 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1059 APValue LHS, RHS;
1060
1061 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1062 return false;
1063
1064 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1065 return false;
1066
1067 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001068 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001069 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001070 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001071 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1072
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001073 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001074 return Success((CR_r == APFloat::cmpEqual &&
1075 CR_i == APFloat::cmpEqual), E);
1076 else {
1077 assert(E->getOpcode() == BinaryOperator::NE &&
1078 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001079 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001080 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001081 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001082 CR_i == APFloat::cmpLessThan)), E);
1083 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001084 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001085 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001086 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1087 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1088 else {
1089 assert(E->getOpcode() == BinaryOperator::NE &&
1090 "Invalid compex comparison.");
1091 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1092 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1093 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001094 }
1095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Anders Carlssonacc79812008-11-16 07:17:21 +00001097 if (LHSTy->isRealFloatingType() &&
1098 RHSTy->isRealFloatingType()) {
1099 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Anders Carlssonacc79812008-11-16 07:17:21 +00001101 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1102 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001103
Anders Carlssonacc79812008-11-16 07:17:21 +00001104 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1105 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001106
Anders Carlssonacc79812008-11-16 07:17:21 +00001107 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001108
Anders Carlssonacc79812008-11-16 07:17:21 +00001109 switch (E->getOpcode()) {
1110 default:
1111 assert(0 && "Invalid binary operator!");
1112 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001113 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001114 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001115 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001116 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001117 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001118 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001119 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001120 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001121 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001122 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001123 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001124 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001125 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001126 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Eli Friedmana38da572009-04-28 19:17:36 +00001129 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1130 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001131 APValue LHSValue;
1132 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1133 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001134
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001135 APValue RHSValue;
1136 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1137 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001138
Eli Friedman334046a2009-06-14 02:17:33 +00001139 // Reject any bases from the normal codepath; we special-case comparisons
1140 // to null.
1141 if (LHSValue.getLValueBase()) {
1142 if (!E->isEqualityOp())
1143 return false;
1144 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1145 return false;
1146 bool bres;
1147 if (!EvalPointerValueAsBool(LHSValue, bres))
1148 return false;
1149 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1150 } else if (RHSValue.getLValueBase()) {
1151 if (!E->isEqualityOp())
1152 return false;
1153 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1154 return false;
1155 bool bres;
1156 if (!EvalPointerValueAsBool(RHSValue, bres))
1157 return false;
1158 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1159 }
Eli Friedman64004332009-03-23 04:38:34 +00001160
Eli Friedmana38da572009-04-28 19:17:36 +00001161 if (E->getOpcode() == BinaryOperator::Sub) {
1162 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001163 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001164
Eli Friedmana38da572009-04-28 19:17:36 +00001165 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001166 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1167 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001168
Eli Friedmana38da572009-04-28 19:17:36 +00001169 return Success(D, E);
1170 }
1171 bool Result;
1172 if (E->getOpcode() == BinaryOperator::EQ) {
1173 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001174 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001175 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1176 }
1177 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001178 }
1179 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001180 if (!LHSTy->isIntegralType() ||
1181 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001182 // We can't continue from here for non-integral types, and they
1183 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001184 return false;
1185 }
1186
Anders Carlsson9c181652008-07-08 14:35:21 +00001187 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001188 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001189 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001190
Eli Friedman94c25c62009-03-24 01:14:50 +00001191 APValue RHSVal;
1192 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001193 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001194
1195 // Handle cases like (unsigned long)&a + 4.
1196 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1197 uint64_t offset = Result.getLValueOffset();
1198 if (E->getOpcode() == BinaryOperator::Add)
1199 offset += RHSVal.getInt().getZExtValue();
1200 else
1201 offset -= RHSVal.getInt().getZExtValue();
1202 Result = APValue(Result.getLValueBase(), offset);
1203 return true;
1204 }
1205
1206 // Handle cases like 4 + (unsigned long)&a
1207 if (E->getOpcode() == BinaryOperator::Add &&
1208 RHSVal.isLValue() && Result.isInt()) {
1209 uint64_t offset = RHSVal.getLValueOffset();
1210 offset += Result.getInt().getZExtValue();
1211 Result = APValue(RHSVal.getLValueBase(), offset);
1212 return true;
1213 }
1214
1215 // All the following cases expect both operands to be an integer
1216 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001217 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001218
Eli Friedman94c25c62009-03-24 01:14:50 +00001219 APSInt& RHS = RHSVal.getInt();
1220
Anders Carlsson9c181652008-07-08 14:35:21 +00001221 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001222 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001223 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001224 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1225 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1226 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1227 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1228 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1229 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001230 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001231 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001232 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001233 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001234 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001235 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001236 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001237 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001238 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001239 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001240 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001241 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1242 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001243 }
1244 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001245 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001246 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1247 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001248 }
Mike Stump11289f42009-09-09 15:08:12 +00001249
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001250 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1251 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1252 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1253 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1254 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1255 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001256 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001257}
1258
Nuno Lopes42042612008-11-16 19:28:31 +00001259bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001260 bool Cond;
1261 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001262 return false;
1263
Nuno Lopes527b5a62008-11-16 22:06:39 +00001264 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001265}
1266
Chris Lattner68061312009-01-24 21:53:27 +00001267unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001268 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1269 // the result is the size of the referenced type."
1270 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1271 // result shall be the alignment of the referenced type."
1272 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1273 T = Ref->getPointeeType();
1274
Chris Lattner24aeeab2009-01-24 21:09:06 +00001275 // Get information about the alignment.
1276 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001277
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001278 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001279 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001280}
1281
Chris Lattner68061312009-01-24 21:53:27 +00001282unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1283 E = E->IgnoreParens();
1284
1285 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001286 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001287 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001288 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001289
Chris Lattner68061312009-01-24 21:53:27 +00001290 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001291 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1292 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001293
Chris Lattner24aeeab2009-01-24 21:09:06 +00001294 return GetAlignOfType(E->getType());
1295}
1296
1297
Sebastian Redl6f282892008-11-11 17:56:53 +00001298/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1299/// expression's type.
1300bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1301 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001302
Chris Lattner24aeeab2009-01-24 21:09:06 +00001303 // Handle alignof separately.
1304 if (!E->isSizeOf()) {
1305 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001306 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001307 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001308 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001309 }
Eli Friedman64004332009-03-23 04:38:34 +00001310
Sebastian Redl6f282892008-11-11 17:56:53 +00001311 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001312 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1313 // the result is the size of the referenced type."
1314 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1315 // result shall be the alignment of the referenced type."
1316 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1317 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001318
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001319 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1320 // extension.
1321 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1322 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001323
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001324 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001325 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001326 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001327
Chris Lattner24aeeab2009-01-24 21:09:06 +00001328 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001329 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001330 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001331}
1332
Chris Lattnere13042c2008-07-11 19:10:17 +00001333bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001334 // Special case unary operators that do not need their subexpression
1335 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001336 if (E->isOffsetOfOp()) {
1337 // The AST for offsetof is defined in such a way that we can just
1338 // directly Evaluate it as an l-value.
1339 APValue LV;
1340 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1341 return false;
1342 if (LV.getLValueBase())
1343 return false;
1344 return Success(LV.getLValueOffset(), E);
1345 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001346
1347 if (E->getOpcode() == UnaryOperator::LNot) {
1348 // LNot's operand isn't necessarily an integer, so we handle it specially.
1349 bool bres;
1350 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1351 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001352 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001353 }
1354
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001355 // Only handle integral operations...
1356 if (!E->getSubExpr()->getType()->isIntegralType())
1357 return false;
1358
Chris Lattnercdf34e72008-07-11 22:52:41 +00001359 // Get the operand value into 'Result'.
1360 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001361 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001362
Chris Lattnerf09ad162008-07-11 22:15:16 +00001363 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001364 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001365 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1366 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001367 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001368 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001369 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1370 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001371 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001372 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001373 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001374 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001375 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001376 if (!Result.isInt()) return false;
1377 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001378 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001379 if (!Result.isInt()) return false;
1380 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001381 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001382}
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner477c4be2008-07-12 01:15:53 +00001384/// HandleCast - This is used to evaluate implicit or explicit casts where the
1385/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001386bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001387 Expr *SubExpr = E->getSubExpr();
1388 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001389 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001390
Eli Friedman9a156e52008-11-12 09:44:48 +00001391 if (DestType->isBooleanType()) {
1392 bool BoolResult;
1393 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1394 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001395 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001396 }
1397
Anders Carlsson9c181652008-07-08 14:35:21 +00001398 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001399 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001400 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001401 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001402
Eli Friedman742421e2009-02-20 01:15:07 +00001403 if (!Result.isInt()) {
1404 // Only allow casts of lvalues if they are lossless.
1405 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1406 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001407
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001408 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001409 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001410 }
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattner477c4be2008-07-12 01:15:53 +00001412 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001413 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001414 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001415 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001416 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001417
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001418 if (LV.getLValueBase()) {
1419 // Only allow based lvalue casts if they are lossless.
1420 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1421 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001422
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001423 Result = LV;
1424 return true;
1425 }
1426
1427 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1428 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001429 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001430
Eli Friedman742421e2009-02-20 01:15:07 +00001431 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1432 // This handles double-conversion cases, where there's both
1433 // an l-value promotion and an implicit conversion to int.
1434 APValue LV;
1435 if (!EvaluateLValue(SubExpr, LV, Info))
1436 return false;
1437
1438 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1439 return false;
1440
1441 Result = LV;
1442 return true;
1443 }
1444
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001445 if (SrcType->isAnyComplexType()) {
1446 APValue C;
1447 if (!EvaluateComplex(SubExpr, C, Info))
1448 return false;
1449 if (C.isComplexFloat())
1450 return Success(HandleFloatToIntCast(DestType, SrcType,
1451 C.getComplexFloatReal(), Info.Ctx),
1452 E);
1453 else
1454 return Success(HandleIntToIntCast(DestType, SrcType,
1455 C.getComplexIntReal(), Info.Ctx), E);
1456 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001457 // FIXME: Handle vectors
1458
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001459 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001460 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001461
Eli Friedman24c01542008-08-22 00:06:13 +00001462 APFloat F(0.0);
1463 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001464 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001465
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001466 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001467}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001468
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001469bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1470 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1471 APValue LV;
1472 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1473 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1474 return Success(LV.getComplexIntReal(), E);
1475 }
1476
1477 return Visit(E->getSubExpr());
1478}
1479
Eli Friedman4e7a2412009-02-27 04:45:43 +00001480bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001481 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1482 APValue LV;
1483 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1484 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1485 return Success(LV.getComplexIntImag(), E);
1486 }
1487
Eli Friedman4e7a2412009-02-27 04:45:43 +00001488 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1489 Info.EvalResult.HasSideEffects = true;
1490 return Success(0, E);
1491}
1492
Chris Lattner05706e882008-07-11 18:11:29 +00001493//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001494// Float Evaluation
1495//===----------------------------------------------------------------------===//
1496
1497namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001498class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001499 : public StmtVisitor<FloatExprEvaluator, bool> {
1500 EvalInfo &Info;
1501 APFloat &Result;
1502public:
1503 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1504 : Info(info), Result(result) {}
1505
1506 bool VisitStmt(Stmt *S) {
1507 return false;
1508 }
1509
1510 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001511 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001512
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001513 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001514 bool VisitBinaryOperator(const BinaryOperator *E);
1515 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001516 bool VisitCastExpr(CastExpr *E);
1517 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001518
Eli Friedman449fe542009-03-23 04:56:01 +00001519 bool VisitChooseExpr(const ChooseExpr *E)
1520 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1521 bool VisitUnaryExtension(const UnaryOperator *E)
1522 { return Visit(E->getSubExpr()); }
1523
1524 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1525 // member of vector, ImplicitValueInitExpr,
Eli Friedman141fbf32009-11-16 04:25:37 +00001526 // conditional ?:
Eli Friedman24c01542008-08-22 00:06:13 +00001527};
1528} // end anonymous namespace
1529
1530static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1531 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1532}
1533
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001534bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001535 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001536 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001537 case Builtin::BI__builtin_huge_val:
1538 case Builtin::BI__builtin_huge_valf:
1539 case Builtin::BI__builtin_huge_vall:
1540 case Builtin::BI__builtin_inf:
1541 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001542 case Builtin::BI__builtin_infl: {
1543 const llvm::fltSemantics &Sem =
1544 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001545 Result = llvm::APFloat::getInf(Sem);
1546 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001547 }
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner0b7282e2008-10-06 06:31:58 +00001549 case Builtin::BI__builtin_nan:
1550 case Builtin::BI__builtin_nanf:
1551 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001552 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001553 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001554 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001555 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001556 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001557 const llvm::fltSemantics &Sem =
1558 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001559 llvm::SmallString<16> s;
1560 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1561 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001562 long l;
1563 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001564 l = strtol(&s[0], &endp, 0);
1565 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001566 return false;
1567 unsigned type = (unsigned int)l;;
1568 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001569 return true;
1570 }
1571 }
1572 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001573
1574 case Builtin::BI__builtin_fabs:
1575 case Builtin::BI__builtin_fabsf:
1576 case Builtin::BI__builtin_fabsl:
1577 if (!EvaluateFloat(E->getArg(0), Result, Info))
1578 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001579
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001580 if (Result.isNegative())
1581 Result.changeSign();
1582 return true;
1583
Mike Stump11289f42009-09-09 15:08:12 +00001584 case Builtin::BI__builtin_copysign:
1585 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001586 case Builtin::BI__builtin_copysignl: {
1587 APFloat RHS(0.);
1588 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1589 !EvaluateFloat(E->getArg(1), RHS, Info))
1590 return false;
1591 Result.copySign(RHS);
1592 return true;
1593 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001594 }
1595}
1596
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001597bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001598 if (E->getOpcode() == UnaryOperator::Deref)
1599 return false;
1600
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001601 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1602 return false;
1603
1604 switch (E->getOpcode()) {
1605 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001606 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001607 return true;
1608 case UnaryOperator::Minus:
1609 Result.changeSign();
1610 return true;
1611 }
1612}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001613
Eli Friedman24c01542008-08-22 00:06:13 +00001614bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001615 if (E->getOpcode() == BinaryOperator::Comma) {
1616 if (!EvaluateFloat(E->getRHS(), Result, Info))
1617 return false;
1618
1619 // If we can't evaluate the LHS, it might have side effects;
1620 // conservatively mark it.
1621 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1622 Info.EvalResult.HasSideEffects = true;
1623
1624 return true;
1625 }
1626
Eli Friedman24c01542008-08-22 00:06:13 +00001627 // FIXME: Diagnostics? I really don't understand how the warnings
1628 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001629 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001630 if (!EvaluateFloat(E->getLHS(), Result, Info))
1631 return false;
1632 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1633 return false;
1634
1635 switch (E->getOpcode()) {
1636 default: return false;
1637 case BinaryOperator::Mul:
1638 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1639 return true;
1640 case BinaryOperator::Add:
1641 Result.add(RHS, APFloat::rmNearestTiesToEven);
1642 return true;
1643 case BinaryOperator::Sub:
1644 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1645 return true;
1646 case BinaryOperator::Div:
1647 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1648 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001649 }
1650}
1651
1652bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1653 Result = E->getValue();
1654 return true;
1655}
1656
Eli Friedman9a156e52008-11-12 09:44:48 +00001657bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1658 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001659
Eli Friedman9a156e52008-11-12 09:44:48 +00001660 if (SubExpr->getType()->isIntegralType()) {
1661 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001662 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001663 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001664 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001665 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001666 return true;
1667 }
1668 if (SubExpr->getType()->isRealFloatingType()) {
1669 if (!Visit(SubExpr))
1670 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001671 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1672 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001673 return true;
1674 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001675 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001676
1677 return false;
1678}
1679
1680bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1681 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1682 return true;
1683}
1684
Eli Friedman24c01542008-08-22 00:06:13 +00001685//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001686// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001687//===----------------------------------------------------------------------===//
1688
1689namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001690class ComplexExprEvaluator
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001691 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001692 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001693
Anders Carlsson537969c2008-11-16 20:27:53 +00001694public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001695 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001696
Anders Carlsson537969c2008-11-16 20:27:53 +00001697 //===--------------------------------------------------------------------===//
1698 // Visitor Methods
1699 //===--------------------------------------------------------------------===//
1700
1701 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001702 return APValue();
1703 }
Mike Stump11289f42009-09-09 15:08:12 +00001704
Anders Carlsson537969c2008-11-16 20:27:53 +00001705 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1706
1707 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001708 Expr* SubExpr = E->getSubExpr();
1709
1710 if (SubExpr->getType()->isRealFloatingType()) {
1711 APFloat Result(0.0);
1712
1713 if (!EvaluateFloat(SubExpr, Result, Info))
1714 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001715
1716 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001717 Result);
1718 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001719 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001720 "Unexpected imaginary literal.");
1721
1722 llvm::APSInt Result;
1723 if (!EvaluateInteger(SubExpr, Result, Info))
1724 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001725
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001726 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1727 Zero = 0;
1728 return APValue(Zero, Result);
1729 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001730 }
1731
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001732 APValue VisitCastExpr(CastExpr *E) {
1733 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001734 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001735 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001736
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001737 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001738 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001739
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001740 if (!EvaluateFloat(SubExpr, Result, Info))
1741 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001742
1743 if (EltType->isRealFloatingType()) {
1744 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001745 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001746 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1747 } else {
1748 llvm::APSInt IResult;
1749 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1750 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1751 Zero = 0;
1752 return APValue(IResult, Zero);
1753 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001754 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001755 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001756
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001757 if (!EvaluateInteger(SubExpr, Result, Info))
1758 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001759
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001760 if (EltType->isRealFloatingType()) {
1761 APFloat FResult =
1762 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001763 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001764 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1765 } else {
1766 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1767 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1768 Zero = 0;
1769 return APValue(Result, Zero);
1770 }
John McCall9dd450b2009-09-21 23:43:11 +00001771 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001772 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001773
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001774 if (!EvaluateComplex(SubExpr, Src, Info))
1775 return APValue();
1776
1777 QualType SrcType = CT->getElementType();
1778
1779 if (Src.isComplexFloat()) {
1780 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001781 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001782 Src.getComplexFloatReal(),
1783 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001784 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001785 Src.getComplexFloatImag(),
1786 Info.Ctx));
1787 } else {
1788 return APValue(HandleFloatToIntCast(EltType, SrcType,
1789 Src.getComplexFloatReal(),
1790 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001791 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001792 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001793 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001794 }
1795 } else {
1796 assert(Src.isComplexInt() && "Invalid evaluate result.");
1797 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001798 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001799 Src.getComplexIntReal(),
1800 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001801 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001802 Src.getComplexIntImag(),
1803 Info.Ctx));
1804 } else {
1805 return APValue(HandleIntToIntCast(EltType, SrcType,
1806 Src.getComplexIntReal(),
1807 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001808 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001809 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001810 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001811 }
1812 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001813 }
1814
1815 // FIXME: Handle more casts.
1816 return APValue();
1817 }
Mike Stump11289f42009-09-09 15:08:12 +00001818
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001819 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001820 APValue VisitChooseExpr(const ChooseExpr *E)
1821 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1822 APValue VisitUnaryExtension(const UnaryOperator *E)
1823 { return Visit(E->getSubExpr()); }
1824 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001825 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001826};
1827} // end anonymous namespace
1828
Mike Stump11289f42009-09-09 15:08:12 +00001829static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001830 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1831 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001832 (&Result.getComplexFloatReal().getSemantics() ==
1833 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001834 "Invalid complex evaluation.");
1835 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001836}
1837
Mike Stump11289f42009-09-09 15:08:12 +00001838APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001839 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001840
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001841 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001842 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001843
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001844 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001845 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001846
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001847 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1848 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001849 switch (E->getOpcode()) {
1850 default: return APValue();
1851 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001852 if (Result.isComplexFloat()) {
1853 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1854 APFloat::rmNearestTiesToEven);
1855 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1856 APFloat::rmNearestTiesToEven);
1857 } else {
1858 Result.getComplexIntReal() += RHS.getComplexIntReal();
1859 Result.getComplexIntImag() += RHS.getComplexIntImag();
1860 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001861 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001862 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001863 if (Result.isComplexFloat()) {
1864 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1865 APFloat::rmNearestTiesToEven);
1866 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1867 APFloat::rmNearestTiesToEven);
1868 } else {
1869 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1870 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1871 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001872 break;
1873 case BinaryOperator::Mul:
1874 if (Result.isComplexFloat()) {
1875 APValue LHS = Result;
1876 APFloat &LHS_r = LHS.getComplexFloatReal();
1877 APFloat &LHS_i = LHS.getComplexFloatImag();
1878 APFloat &RHS_r = RHS.getComplexFloatReal();
1879 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001880
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001881 APFloat Tmp = LHS_r;
1882 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1883 Result.getComplexFloatReal() = Tmp;
1884 Tmp = LHS_i;
1885 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1886 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1887
1888 Tmp = LHS_r;
1889 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1890 Result.getComplexFloatImag() = Tmp;
1891 Tmp = LHS_i;
1892 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1893 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1894 } else {
1895 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001896 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001897 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1898 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001899 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001900 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1901 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1902 }
1903 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001904 }
1905
1906 return Result;
1907}
1908
Anders Carlsson537969c2008-11-16 20:27:53 +00001909//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001910// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001911//===----------------------------------------------------------------------===//
1912
Chris Lattner67d7b922008-11-16 21:24:15 +00001913/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001914/// any crazy technique (that has nothing to do with language standards) that
1915/// we want to. If this function returns true, it returns the folded constant
1916/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001917bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1918 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001919
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001920 if (getType()->isVectorType()) {
1921 if (!EvaluateVector(this, Result.Val, Info))
1922 return false;
1923 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001924 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001925 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001926 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001927 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001928 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001929 } else if (getType()->isRealFloatingType()) {
1930 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001931 if (!EvaluateFloat(this, f, Info))
1932 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001933
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001934 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001935 } else if (getType()->isAnyComplexType()) {
1936 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001937 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001938 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001939 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001940
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001941 return true;
1942}
1943
Mike Stump5183a142009-10-26 23:05:19 +00001944bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1945 EvalInfo Info(Ctx, Result, true);
1946
1947 if (getType()->isVectorType()) {
1948 if (!EvaluateVector(this, Result.Val, Info))
1949 return false;
1950 } else if (getType()->isIntegerType()) {
1951 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1952 return false;
1953 } else if (getType()->hasPointerRepresentation()) {
1954 if (!EvaluatePointer(this, Result.Val, Info))
1955 return false;
1956 } else if (getType()->isRealFloatingType()) {
1957 llvm::APFloat f(0.0);
1958 if (!EvaluateFloat(this, f, Info))
1959 return false;
1960
1961 Result.Val = APValue(f);
1962 } else if (getType()->isAnyComplexType()) {
1963 if (!EvaluateComplex(this, Result.Val, Info))
1964 return false;
1965 } else
1966 return false;
1967
1968 return true;
1969}
1970
Anders Carlsson43168122009-04-10 04:54:13 +00001971bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1972 EvalInfo Info(Ctx, Result);
1973
1974 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1975}
1976
Eli Friedman7d45c482009-09-13 10:17:44 +00001977bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1978 EvalInfo Info(Ctx, Result, true);
1979
1980 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1981}
1982
Chris Lattner67d7b922008-11-16 21:24:15 +00001983/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001984/// folded, but discard the result.
1985bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001986 EvalResult Result;
1987 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001988}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001989
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00001990bool Expr::HasSideEffects(ASTContext &Ctx) const {
1991 Expr::EvalResult Result;
1992 EvalInfo Info(Ctx, Result);
1993 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1994}
1995
Anders Carlsson59689ed2008-11-22 21:04:56 +00001996APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001997 EvalResult EvalResult;
1998 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001999 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00002000 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002001 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00002002
Anders Carlsson6736d1a22008-12-19 20:58:05 +00002003 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00002004}