blob: 13831dc1f52cdb78d5a1ac8c0107b3fdbf9df631 [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 Friedmanee275c82009-12-10 22:29:29 +0000851 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
852 return Success(ECD->getInitVal(), E);
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000853
854 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000855 // In C, they can also be folded, although they are not ICEs.
Douglas Gregor0840cc02009-11-01 20:32:48 +0000856 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
857 == Qualifiers::Const) {
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000858 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +0000859 const VarDecl *Def = 0;
Eli Friedmanfb8a93f2009-11-24 05:28:59 +0000860 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000861 if (APValue *V = VD->getEvaluatedValue()) {
862 if (V->isInt())
863 return Success(V->getInt(), E);
864 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
865 }
866
867 if (VD->isEvaluatingValue())
868 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
869
870 VD->setEvaluatingValue();
871
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000872 if (Visit(const_cast<Expr*>(Init))) {
873 // Cache the evaluated value in the variable declaration.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000874 VD->setEvaluatedValue(Result);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000875 return true;
876 }
877
Eli Friedman1d6fb162009-12-03 20:31:57 +0000878 VD->setEvaluatedValue(APValue());
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000879 return false;
880 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000881 }
882 }
883
Chris Lattner7174bf32008-07-12 00:38:25 +0000884 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000885 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000886}
887
Chris Lattner86ee2862008-10-06 06:40:35 +0000888/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
889/// as GCC.
890static int EvaluateBuiltinClassifyType(const CallExpr *E) {
891 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000892 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000893 enum gcc_type_class {
894 no_type_class = -1,
895 void_type_class, integer_type_class, char_type_class,
896 enumeral_type_class, boolean_type_class,
897 pointer_type_class, reference_type_class, offset_type_class,
898 real_type_class, complex_type_class,
899 function_type_class, method_type_class,
900 record_type_class, union_type_class,
901 array_type_class, string_type_class,
902 lang_type_class
903 };
Mike Stump11289f42009-09-09 15:08:12 +0000904
905 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000906 // ideal, however it is what gcc does.
907 if (E->getNumArgs() == 0)
908 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Chris Lattner86ee2862008-10-06 06:40:35 +0000910 QualType ArgTy = E->getArg(0)->getType();
911 if (ArgTy->isVoidType())
912 return void_type_class;
913 else if (ArgTy->isEnumeralType())
914 return enumeral_type_class;
915 else if (ArgTy->isBooleanType())
916 return boolean_type_class;
917 else if (ArgTy->isCharType())
918 return string_type_class; // gcc doesn't appear to use char_type_class
919 else if (ArgTy->isIntegerType())
920 return integer_type_class;
921 else if (ArgTy->isPointerType())
922 return pointer_type_class;
923 else if (ArgTy->isReferenceType())
924 return reference_type_class;
925 else if (ArgTy->isRealType())
926 return real_type_class;
927 else if (ArgTy->isComplexType())
928 return complex_type_class;
929 else if (ArgTy->isFunctionType())
930 return function_type_class;
931 else if (ArgTy->isStructureType())
932 return record_type_class;
933 else if (ArgTy->isUnionType())
934 return union_type_class;
935 else if (ArgTy->isArrayType())
936 return array_type_class;
937 else if (ArgTy->isUnionType())
938 return union_type_class;
939 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
940 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
941 return -1;
942}
943
Chris Lattner7174bf32008-07-12 00:38:25 +0000944bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000945 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000946 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000947 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000948
949 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000950 const Expr *Arg = E->getArg(0)->IgnoreParens();
951 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000952 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000953 && Base.Val.getKind() == APValue::LValue
954 && !Base.HasSideEffects)
955 if (const Expr *LVBase = Base.Val.getLValueBase())
956 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
957 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump5179f302009-10-28 21:22:24 +0000958 if (!VD->getType()->isIncompleteType()
959 && VD->getType()->isObjectType()
960 && !VD->getType()->isVariablyModifiedType()
961 && !VD->getType()->isDependentType()) {
962 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
963 uint64_t Offset = Base.Val.getLValueOffset();
964 if (Offset <= Size)
965 Size -= Base.Val.getLValueOffset();
966 else
967 Size = 0;
968 return Success(Size, E);
969 }
Mike Stump722cedf2009-10-26 18:35:08 +0000970 }
971 }
972
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +0000973 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Mike Stump99f11f72009-10-26 18:57:47 +0000974 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Chris Lattner4f105592009-11-03 19:48:51 +0000975 return Success(-1ULL, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000976 return Success(0, E);
977 }
Mike Stump876387b2009-10-27 22:09:17 +0000978
Mike Stump722cedf2009-10-26 18:35:08 +0000979 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
980 }
981
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000982 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000983 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Anders Carlsson4c76e932008-11-24 04:21:33 +0000985 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000986 // __builtin_constant_p always has one operand: it returns true if that
987 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000988 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000989
990 case Builtin::BI__builtin_eh_return_data_regno: {
991 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
992 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
993 return Success(Operand, E);
994 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000995 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000996}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000997
Chris Lattnere13042c2008-07-11 19:10:17 +0000998bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000999 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +00001000 if (!Visit(E->getRHS()))
1001 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001002
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001003 // If we can't evaluate the LHS, it might have side effects;
1004 // conservatively mark it.
1005 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1006 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001007
Anders Carlsson564730a2008-12-01 02:07:06 +00001008 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001009 }
1010
1011 if (E->isLogicalOp()) {
1012 // These need to be handled specially because the operands aren't
1013 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001014 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001016 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +00001017 // We were able to evaluate the LHS, see if we can get away with not
1018 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +00001019 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001020 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001021
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001022 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001023 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001024 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001025 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001026 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001027 }
1028 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001029 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +00001030 // We can't evaluate the LHS; however, sometimes the result
1031 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +00001032 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001033 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001034 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +00001035 // must have had side effects.
1036 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001037
1038 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +00001039 }
1040 }
Anders Carlsson59689ed2008-11-22 21:04:56 +00001041 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001042
Eli Friedman5a332ea2008-11-13 06:09:17 +00001043 return false;
1044 }
1045
Anders Carlssonacc79812008-11-16 07:17:21 +00001046 QualType LHSTy = E->getLHS()->getType();
1047 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001048
1049 if (LHSTy->isAnyComplexType()) {
1050 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1051 APValue LHS, RHS;
1052
1053 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1054 return false;
1055
1056 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1057 return false;
1058
1059 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +00001060 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001061 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +00001062 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001063 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1064
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001065 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001066 return Success((CR_r == APFloat::cmpEqual &&
1067 CR_i == APFloat::cmpEqual), E);
1068 else {
1069 assert(E->getOpcode() == BinaryOperator::NE &&
1070 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +00001071 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001072 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +00001073 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001074 CR_i == APFloat::cmpLessThan)), E);
1075 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001076 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001077 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001078 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1079 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1080 else {
1081 assert(E->getOpcode() == BinaryOperator::NE &&
1082 "Invalid compex comparison.");
1083 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1084 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1085 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001086 }
1087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Anders Carlssonacc79812008-11-16 07:17:21 +00001089 if (LHSTy->isRealFloatingType() &&
1090 RHSTy->isRealFloatingType()) {
1091 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001092
Anders Carlssonacc79812008-11-16 07:17:21 +00001093 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1094 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001095
Anders Carlssonacc79812008-11-16 07:17:21 +00001096 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1097 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Anders Carlssonacc79812008-11-16 07:17:21 +00001099 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001100
Anders Carlssonacc79812008-11-16 07:17:21 +00001101 switch (E->getOpcode()) {
1102 default:
1103 assert(0 && "Invalid binary operator!");
1104 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001105 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001106 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001107 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001108 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001109 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001110 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001111 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001112 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001113 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001114 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001115 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001116 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001117 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001118 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Eli Friedmana38da572009-04-28 19:17:36 +00001121 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1122 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001123 APValue LHSValue;
1124 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1125 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001126
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001127 APValue RHSValue;
1128 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1129 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001130
Eli Friedman334046a2009-06-14 02:17:33 +00001131 // Reject any bases from the normal codepath; we special-case comparisons
1132 // to null.
1133 if (LHSValue.getLValueBase()) {
1134 if (!E->isEqualityOp())
1135 return false;
1136 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1137 return false;
1138 bool bres;
1139 if (!EvalPointerValueAsBool(LHSValue, bres))
1140 return false;
1141 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1142 } else if (RHSValue.getLValueBase()) {
1143 if (!E->isEqualityOp())
1144 return false;
1145 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1146 return false;
1147 bool bres;
1148 if (!EvalPointerValueAsBool(RHSValue, bres))
1149 return false;
1150 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1151 }
Eli Friedman64004332009-03-23 04:38:34 +00001152
Eli Friedmana38da572009-04-28 19:17:36 +00001153 if (E->getOpcode() == BinaryOperator::Sub) {
1154 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001155 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001156
Eli Friedmana38da572009-04-28 19:17:36 +00001157 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001158 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1159 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001160
Eli Friedmana38da572009-04-28 19:17:36 +00001161 return Success(D, E);
1162 }
1163 bool Result;
1164 if (E->getOpcode() == BinaryOperator::EQ) {
1165 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001166 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001167 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1168 }
1169 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001170 }
1171 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001172 if (!LHSTy->isIntegralType() ||
1173 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001174 // We can't continue from here for non-integral types, and they
1175 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001176 return false;
1177 }
1178
Anders Carlsson9c181652008-07-08 14:35:21 +00001179 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001180 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001181 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001182
Eli Friedman94c25c62009-03-24 01:14:50 +00001183 APValue RHSVal;
1184 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001185 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001186
1187 // Handle cases like (unsigned long)&a + 4.
1188 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1189 uint64_t offset = Result.getLValueOffset();
1190 if (E->getOpcode() == BinaryOperator::Add)
1191 offset += RHSVal.getInt().getZExtValue();
1192 else
1193 offset -= RHSVal.getInt().getZExtValue();
1194 Result = APValue(Result.getLValueBase(), offset);
1195 return true;
1196 }
1197
1198 // Handle cases like 4 + (unsigned long)&a
1199 if (E->getOpcode() == BinaryOperator::Add &&
1200 RHSVal.isLValue() && Result.isInt()) {
1201 uint64_t offset = RHSVal.getLValueOffset();
1202 offset += Result.getInt().getZExtValue();
1203 Result = APValue(RHSVal.getLValueBase(), offset);
1204 return true;
1205 }
1206
1207 // All the following cases expect both operands to be an integer
1208 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001209 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001210
Eli Friedman94c25c62009-03-24 01:14:50 +00001211 APSInt& RHS = RHSVal.getInt();
1212
Anders Carlsson9c181652008-07-08 14:35:21 +00001213 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001214 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001215 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001216 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1217 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1218 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1219 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1220 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1221 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001222 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001223 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001224 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001225 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001226 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001227 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001228 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001229 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001230 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001231 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001232 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001233 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1234 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001235 }
1236 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001237 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001238 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1239 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001242 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1243 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1244 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1245 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1246 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1247 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001248 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001249}
1250
Nuno Lopes42042612008-11-16 19:28:31 +00001251bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001252 bool Cond;
1253 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001254 return false;
1255
Nuno Lopes527b5a62008-11-16 22:06:39 +00001256 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001257}
1258
Chris Lattner68061312009-01-24 21:53:27 +00001259unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001260 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1261 // the result is the size of the referenced type."
1262 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1263 // result shall be the alignment of the referenced type."
1264 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1265 T = Ref->getPointeeType();
1266
Chris Lattner24aeeab2009-01-24 21:09:06 +00001267 // Get information about the alignment.
1268 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001269
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001270 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001271 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001272}
1273
Chris Lattner68061312009-01-24 21:53:27 +00001274unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1275 E = E->IgnoreParens();
1276
1277 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001278 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001279 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001280 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedman64004332009-03-23 04:38:34 +00001281
Chris Lattner68061312009-01-24 21:53:27 +00001282 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001283 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1284 /*RefAsPointee*/true);
Chris Lattner68061312009-01-24 21:53:27 +00001285
Chris Lattner24aeeab2009-01-24 21:09:06 +00001286 return GetAlignOfType(E->getType());
1287}
1288
1289
Sebastian Redl6f282892008-11-11 17:56:53 +00001290/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1291/// expression's type.
1292bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1293 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001294
Chris Lattner24aeeab2009-01-24 21:09:06 +00001295 // Handle alignof separately.
1296 if (!E->isSizeOf()) {
1297 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001298 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001299 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001300 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001301 }
Eli Friedman64004332009-03-23 04:38:34 +00001302
Sebastian Redl6f282892008-11-11 17:56:53 +00001303 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl22e2e5c2009-11-23 17:18:46 +00001304 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1305 // the result is the size of the referenced type."
1306 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1307 // result shall be the alignment of the referenced type."
1308 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1309 SrcTy = Ref->getPointeeType();
Sebastian Redl6f282892008-11-11 17:56:53 +00001310
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001311 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1312 // extension.
1313 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1314 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001315
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001316 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001317 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001318 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001319
Chris Lattner24aeeab2009-01-24 21:09:06 +00001320 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001321 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001322 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001323}
1324
Chris Lattnere13042c2008-07-11 19:10:17 +00001325bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001326 // Special case unary operators that do not need their subexpression
1327 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001328 if (E->isOffsetOfOp()) {
1329 // The AST for offsetof is defined in such a way that we can just
1330 // directly Evaluate it as an l-value.
1331 APValue LV;
1332 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1333 return false;
1334 if (LV.getLValueBase())
1335 return false;
1336 return Success(LV.getLValueOffset(), E);
1337 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001338
1339 if (E->getOpcode() == UnaryOperator::LNot) {
1340 // LNot's operand isn't necessarily an integer, so we handle it specially.
1341 bool bres;
1342 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1343 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001344 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001345 }
1346
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001347 // Only handle integral operations...
1348 if (!E->getSubExpr()->getType()->isIntegralType())
1349 return false;
1350
Chris Lattnercdf34e72008-07-11 22:52:41 +00001351 // Get the operand value into 'Result'.
1352 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001353 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001354
Chris Lattnerf09ad162008-07-11 22:15:16 +00001355 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001356 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001357 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1358 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001359 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001360 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001361 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1362 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001363 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001364 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001365 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001366 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001367 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001368 if (!Result.isInt()) return false;
1369 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001370 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001371 if (!Result.isInt()) return false;
1372 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001373 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001374}
Mike Stump11289f42009-09-09 15:08:12 +00001375
Chris Lattner477c4be2008-07-12 01:15:53 +00001376/// HandleCast - This is used to evaluate implicit or explicit casts where the
1377/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001378bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001379 Expr *SubExpr = E->getSubExpr();
1380 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001381 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001382
Eli Friedman9a156e52008-11-12 09:44:48 +00001383 if (DestType->isBooleanType()) {
1384 bool BoolResult;
1385 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1386 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001387 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001388 }
1389
Anders Carlsson9c181652008-07-08 14:35:21 +00001390 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001391 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001392 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001393 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001394
Eli Friedman742421e2009-02-20 01:15:07 +00001395 if (!Result.isInt()) {
1396 // Only allow casts of lvalues if they are lossless.
1397 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1398 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001399
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001400 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001401 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner477c4be2008-07-12 01:15:53 +00001404 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001405 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001406 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001407 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001408 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001409
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001410 if (LV.getLValueBase()) {
1411 // Only allow based lvalue casts if they are lossless.
1412 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1413 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001414
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001415 Result = LV;
1416 return true;
1417 }
1418
1419 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1420 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001421 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001422
Eli Friedman742421e2009-02-20 01:15:07 +00001423 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1424 // This handles double-conversion cases, where there's both
1425 // an l-value promotion and an implicit conversion to int.
1426 APValue LV;
1427 if (!EvaluateLValue(SubExpr, LV, Info))
1428 return false;
1429
1430 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1431 return false;
1432
1433 Result = LV;
1434 return true;
1435 }
1436
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001437 if (SrcType->isAnyComplexType()) {
1438 APValue C;
1439 if (!EvaluateComplex(SubExpr, C, Info))
1440 return false;
1441 if (C.isComplexFloat())
1442 return Success(HandleFloatToIntCast(DestType, SrcType,
1443 C.getComplexFloatReal(), Info.Ctx),
1444 E);
1445 else
1446 return Success(HandleIntToIntCast(DestType, SrcType,
1447 C.getComplexIntReal(), Info.Ctx), E);
1448 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001449 // FIXME: Handle vectors
1450
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001451 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001452 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001453
Eli Friedman24c01542008-08-22 00:06:13 +00001454 APFloat F(0.0);
1455 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001456 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001457
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001458 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001459}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001460
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001461bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1462 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1463 APValue LV;
1464 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1465 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1466 return Success(LV.getComplexIntReal(), E);
1467 }
1468
1469 return Visit(E->getSubExpr());
1470}
1471
Eli Friedman4e7a2412009-02-27 04:45:43 +00001472bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001473 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1474 APValue LV;
1475 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1476 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1477 return Success(LV.getComplexIntImag(), E);
1478 }
1479
Eli Friedman4e7a2412009-02-27 04:45:43 +00001480 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1481 Info.EvalResult.HasSideEffects = true;
1482 return Success(0, E);
1483}
1484
Chris Lattner05706e882008-07-11 18:11:29 +00001485//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001486// Float Evaluation
1487//===----------------------------------------------------------------------===//
1488
1489namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001490class FloatExprEvaluator
Eli Friedman24c01542008-08-22 00:06:13 +00001491 : public StmtVisitor<FloatExprEvaluator, bool> {
1492 EvalInfo &Info;
1493 APFloat &Result;
1494public:
1495 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1496 : Info(info), Result(result) {}
1497
1498 bool VisitStmt(Stmt *S) {
1499 return false;
1500 }
1501
1502 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001503 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001504
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001505 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001506 bool VisitBinaryOperator(const BinaryOperator *E);
1507 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001508 bool VisitCastExpr(CastExpr *E);
1509 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanf3da3342009-12-04 02:12:53 +00001510 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001511
Eli Friedman449fe542009-03-23 04:56:01 +00001512 bool VisitChooseExpr(const ChooseExpr *E)
1513 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1514 bool VisitUnaryExtension(const UnaryOperator *E)
1515 { return Visit(E->getSubExpr()); }
1516
1517 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedmanf3da3342009-12-04 02:12:53 +00001518 // member of vector, ImplicitValueInitExpr
Eli Friedman24c01542008-08-22 00:06:13 +00001519};
1520} // end anonymous namespace
1521
1522static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1523 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1524}
1525
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001526bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001527 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001528 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001529 case Builtin::BI__builtin_huge_val:
1530 case Builtin::BI__builtin_huge_valf:
1531 case Builtin::BI__builtin_huge_vall:
1532 case Builtin::BI__builtin_inf:
1533 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001534 case Builtin::BI__builtin_infl: {
1535 const llvm::fltSemantics &Sem =
1536 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001537 Result = llvm::APFloat::getInf(Sem);
1538 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner0b7282e2008-10-06 06:31:58 +00001541 case Builtin::BI__builtin_nan:
1542 case Builtin::BI__builtin_nanf:
1543 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001544 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001545 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001546 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001547 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001548 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001549 const llvm::fltSemantics &Sem =
1550 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001551 unsigned Type = 0;
1552 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump2346cd22009-05-30 03:56:50 +00001553 return false;
Benjamin Kramer8a4ad4a2009-12-11 13:26:32 +00001554 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001555 return true;
1556 }
1557 }
1558 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001559
1560 case Builtin::BI__builtin_fabs:
1561 case Builtin::BI__builtin_fabsf:
1562 case Builtin::BI__builtin_fabsl:
1563 if (!EvaluateFloat(E->getArg(0), Result, Info))
1564 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001566 if (Result.isNegative())
1567 Result.changeSign();
1568 return true;
1569
Mike Stump11289f42009-09-09 15:08:12 +00001570 case Builtin::BI__builtin_copysign:
1571 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001572 case Builtin::BI__builtin_copysignl: {
1573 APFloat RHS(0.);
1574 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1575 !EvaluateFloat(E->getArg(1), RHS, Info))
1576 return false;
1577 Result.copySign(RHS);
1578 return true;
1579 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001580 }
1581}
1582
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001583bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001584 if (E->getOpcode() == UnaryOperator::Deref)
1585 return false;
1586
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001587 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1588 return false;
1589
1590 switch (E->getOpcode()) {
1591 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001592 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001593 return true;
1594 case UnaryOperator::Minus:
1595 Result.changeSign();
1596 return true;
1597 }
1598}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001599
Eli Friedman24c01542008-08-22 00:06:13 +00001600bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman141fbf32009-11-16 04:25:37 +00001601 if (E->getOpcode() == BinaryOperator::Comma) {
1602 if (!EvaluateFloat(E->getRHS(), Result, Info))
1603 return false;
1604
1605 // If we can't evaluate the LHS, it might have side effects;
1606 // conservatively mark it.
1607 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1608 Info.EvalResult.HasSideEffects = true;
1609
1610 return true;
1611 }
1612
Eli Friedman24c01542008-08-22 00:06:13 +00001613 // FIXME: Diagnostics? I really don't understand how the warnings
1614 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001615 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001616 if (!EvaluateFloat(E->getLHS(), Result, Info))
1617 return false;
1618 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1619 return false;
1620
1621 switch (E->getOpcode()) {
1622 default: return false;
1623 case BinaryOperator::Mul:
1624 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1625 return true;
1626 case BinaryOperator::Add:
1627 Result.add(RHS, APFloat::rmNearestTiesToEven);
1628 return true;
1629 case BinaryOperator::Sub:
1630 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1631 return true;
1632 case BinaryOperator::Div:
1633 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1634 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001635 }
1636}
1637
1638bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1639 Result = E->getValue();
1640 return true;
1641}
1642
Eli Friedman9a156e52008-11-12 09:44:48 +00001643bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1644 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001645
Eli Friedman9a156e52008-11-12 09:44:48 +00001646 if (SubExpr->getType()->isIntegralType()) {
1647 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001648 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001649 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001650 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001651 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001652 return true;
1653 }
1654 if (SubExpr->getType()->isRealFloatingType()) {
1655 if (!Visit(SubExpr))
1656 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001657 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1658 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001659 return true;
1660 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001661 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001662
1663 return false;
1664}
1665
1666bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1667 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1668 return true;
1669}
1670
Eli Friedmanf3da3342009-12-04 02:12:53 +00001671bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1672 bool Cond;
1673 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1674 return false;
1675
1676 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1677}
1678
Eli Friedman24c01542008-08-22 00:06:13 +00001679//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001680// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001681//===----------------------------------------------------------------------===//
1682
1683namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +00001684class ComplexExprEvaluator
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001685 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001686 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001687
Anders Carlsson537969c2008-11-16 20:27:53 +00001688public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001689 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001690
Anders Carlsson537969c2008-11-16 20:27:53 +00001691 //===--------------------------------------------------------------------===//
1692 // Visitor Methods
1693 //===--------------------------------------------------------------------===//
1694
1695 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001696 return APValue();
1697 }
Mike Stump11289f42009-09-09 15:08:12 +00001698
Anders Carlsson537969c2008-11-16 20:27:53 +00001699 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1700
1701 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001702 Expr* SubExpr = E->getSubExpr();
1703
1704 if (SubExpr->getType()->isRealFloatingType()) {
1705 APFloat Result(0.0);
1706
1707 if (!EvaluateFloat(SubExpr, Result, Info))
1708 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001709
1710 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001711 Result);
1712 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001713 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001714 "Unexpected imaginary literal.");
1715
1716 llvm::APSInt Result;
1717 if (!EvaluateInteger(SubExpr, Result, Info))
1718 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001719
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001720 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1721 Zero = 0;
1722 return APValue(Zero, Result);
1723 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001724 }
1725
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001726 APValue VisitCastExpr(CastExpr *E) {
1727 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001728 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001729 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001730
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001731 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001732 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001733
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001734 if (!EvaluateFloat(SubExpr, Result, Info))
1735 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001736
1737 if (EltType->isRealFloatingType()) {
1738 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001739 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001740 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1741 } else {
1742 llvm::APSInt IResult;
1743 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1744 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1745 Zero = 0;
1746 return APValue(IResult, Zero);
1747 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001748 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001749 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001750
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001751 if (!EvaluateInteger(SubExpr, Result, Info))
1752 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001753
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001754 if (EltType->isRealFloatingType()) {
1755 APFloat FResult =
1756 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001757 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001758 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1759 } else {
1760 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1761 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1762 Zero = 0;
1763 return APValue(Result, Zero);
1764 }
John McCall9dd450b2009-09-21 23:43:11 +00001765 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001766 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001767
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001768 if (!EvaluateComplex(SubExpr, Src, Info))
1769 return APValue();
1770
1771 QualType SrcType = CT->getElementType();
1772
1773 if (Src.isComplexFloat()) {
1774 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001775 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001776 Src.getComplexFloatReal(),
1777 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001778 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001779 Src.getComplexFloatImag(),
1780 Info.Ctx));
1781 } else {
1782 return APValue(HandleFloatToIntCast(EltType, SrcType,
1783 Src.getComplexFloatReal(),
1784 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001785 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001786 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001787 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001788 }
1789 } else {
1790 assert(Src.isComplexInt() && "Invalid evaluate result.");
1791 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001792 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001793 Src.getComplexIntReal(),
1794 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001795 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001796 Src.getComplexIntImag(),
1797 Info.Ctx));
1798 } else {
1799 return APValue(HandleIntToIntCast(EltType, SrcType,
1800 Src.getComplexIntReal(),
1801 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001802 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001803 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001804 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001805 }
1806 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001807 }
1808
1809 // FIXME: Handle more casts.
1810 return APValue();
1811 }
Mike Stump11289f42009-09-09 15:08:12 +00001812
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001813 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001814 APValue VisitChooseExpr(const ChooseExpr *E)
1815 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1816 APValue VisitUnaryExtension(const UnaryOperator *E)
1817 { return Visit(E->getSubExpr()); }
1818 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001819 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001820};
1821} // end anonymous namespace
1822
Mike Stump11289f42009-09-09 15:08:12 +00001823static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001824 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1825 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001826 (&Result.getComplexFloatReal().getSemantics() ==
1827 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001828 "Invalid complex evaluation.");
1829 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001830}
1831
Mike Stump11289f42009-09-09 15:08:12 +00001832APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001833 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001834
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001835 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001836 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001837
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001838 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001839 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001840
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001841 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1842 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001843 switch (E->getOpcode()) {
1844 default: return APValue();
1845 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001846 if (Result.isComplexFloat()) {
1847 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1848 APFloat::rmNearestTiesToEven);
1849 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1850 APFloat::rmNearestTiesToEven);
1851 } else {
1852 Result.getComplexIntReal() += RHS.getComplexIntReal();
1853 Result.getComplexIntImag() += RHS.getComplexIntImag();
1854 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001855 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001856 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001857 if (Result.isComplexFloat()) {
1858 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1859 APFloat::rmNearestTiesToEven);
1860 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1861 APFloat::rmNearestTiesToEven);
1862 } else {
1863 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1864 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1865 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001866 break;
1867 case BinaryOperator::Mul:
1868 if (Result.isComplexFloat()) {
1869 APValue LHS = Result;
1870 APFloat &LHS_r = LHS.getComplexFloatReal();
1871 APFloat &LHS_i = LHS.getComplexFloatImag();
1872 APFloat &RHS_r = RHS.getComplexFloatReal();
1873 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001874
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001875 APFloat Tmp = LHS_r;
1876 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1877 Result.getComplexFloatReal() = Tmp;
1878 Tmp = LHS_i;
1879 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1880 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1881
1882 Tmp = LHS_r;
1883 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1884 Result.getComplexFloatImag() = Tmp;
1885 Tmp = LHS_i;
1886 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1887 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1888 } else {
1889 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001890 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001891 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1892 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001893 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001894 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1895 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1896 }
1897 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001898 }
1899
1900 return Result;
1901}
1902
Anders Carlsson537969c2008-11-16 20:27:53 +00001903//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001904// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001905//===----------------------------------------------------------------------===//
1906
Chris Lattner67d7b922008-11-16 21:24:15 +00001907/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001908/// any crazy technique (that has nothing to do with language standards) that
1909/// we want to. If this function returns true, it returns the folded constant
1910/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001911bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1912 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001913
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001914 if (getType()->isVectorType()) {
1915 if (!EvaluateVector(this, Result.Val, Info))
1916 return false;
1917 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001918 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001919 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001920 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001921 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001922 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001923 } else if (getType()->isRealFloatingType()) {
1924 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001925 if (!EvaluateFloat(this, f, Info))
1926 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001927
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001928 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001929 } else if (getType()->isAnyComplexType()) {
1930 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001931 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001932 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001933 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001934
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001935 return true;
1936}
1937
Mike Stump5183a142009-10-26 23:05:19 +00001938bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1939 EvalInfo Info(Ctx, Result, true);
1940
1941 if (getType()->isVectorType()) {
1942 if (!EvaluateVector(this, Result.Val, Info))
1943 return false;
1944 } else if (getType()->isIntegerType()) {
1945 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1946 return false;
1947 } else if (getType()->hasPointerRepresentation()) {
1948 if (!EvaluatePointer(this, Result.Val, Info))
1949 return false;
1950 } else if (getType()->isRealFloatingType()) {
1951 llvm::APFloat f(0.0);
1952 if (!EvaluateFloat(this, f, Info))
1953 return false;
1954
1955 Result.Val = APValue(f);
1956 } else if (getType()->isAnyComplexType()) {
1957 if (!EvaluateComplex(this, Result.Val, Info))
1958 return false;
1959 } else
1960 return false;
1961
1962 return true;
1963}
1964
Anders Carlsson43168122009-04-10 04:54:13 +00001965bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1966 EvalInfo Info(Ctx, Result);
1967
1968 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1969}
1970
Eli Friedman7d45c482009-09-13 10:17:44 +00001971bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1972 EvalInfo Info(Ctx, Result, true);
1973
1974 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1975}
1976
Chris Lattner67d7b922008-11-16 21:24:15 +00001977/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001978/// folded, but discard the result.
1979bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001980 EvalResult Result;
1981 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001982}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001983
Fariborz Jahanian4127b8e2009-11-05 18:03:03 +00001984bool Expr::HasSideEffects(ASTContext &Ctx) const {
1985 Expr::EvalResult Result;
1986 EvalInfo Info(Ctx, Result);
1987 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1988}
1989
Anders Carlsson59689ed2008-11-22 21:04:56 +00001990APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001991 EvalResult EvalResult;
1992 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001993 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001994 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001995 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001996
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001997 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001998}