blob: a20e1cc6f56b42799fcaa042081bbb94b4b67f88 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-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 Carlsson19cc4ab2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000022#include <cstring>
23
Anders Carlssonc44eec62008-07-03 04:20:39 +000024using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000025using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000026using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000027
Chris Lattner87eae5e2008-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 Stump1eb44332009-09-09 15:08:12 +000044
Anders Carlsson54da0492008-11-30 16:38:33 +000045 /// EvalResult - Contains information about the evaluation.
46 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000047
Eli Friedmanb2f295c2009-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 Lattner87eae5e2008-07-11 22:52:41 +000054};
55
56
Eli Friedman4efaa272008-11-12 09:44:48 +000057static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-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 Lattnerd9becd12009-10-28 23:59:40 +000060static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
61 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman5bc86102009-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 Friedman4efaa272008-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 Friedmana1f47c42009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-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 Friedman4efaa272008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-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 Stump1eb44332009-09-09 15:08:12 +0000116
Daniel Dunbara2cfd342009-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 Stump1eb44332009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump1eb44332009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-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 Stump1eb44332009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-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 Stumpc4c90452009-10-27 22:09:17 +0000154namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000155class HasSideEffect
Mike Stumpc4c90452009-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 Stumpdf317bf2009-11-03 23:25:48 +0000169 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-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 Stump980ca222009-10-29 20:48:09 +0000186 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-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 Stump3f0147e2009-10-29 23:34:20 +0000191 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000192 bool VisitBinaryOperator(BinaryOperator *E)
193 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-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 Stumpdf317bf2009-11-03 23:25:48 +0000199 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000200 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000201 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000202 }
203 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
204};
205
Mike Stumpc4c90452009-10-27 22:09:17 +0000206} // end anonymous namespace
207
Eli Friedman4efaa272008-11-12 09:44:48 +0000208//===----------------------------------------------------------------------===//
209// LValue Evaluation
210//===----------------------------------------------------------------------===//
211namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000212class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000213 : public StmtVisitor<LValueExprEvaluator, APValue> {
214 EvalInfo &Info;
215public:
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Eli Friedman4efaa272008-11-12 09:44:48 +0000217 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
218
219 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000220 return APValue();
221 }
222
223 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000224 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000225 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-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 Lattnereaf2bb82009-02-24 22:18:39 +0000230 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000231 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000232 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-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 Carlsson26bc2202009-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 Friedmanba98d6b2009-03-23 04:56:01 +0000247 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-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 Stump1eb44332009-09-09 15:08:12 +0000256APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-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 Friedmanb2f295c2009-09-13 10:17:44 +0000260 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000261 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000262 if (!VD->getType()->isReferenceType())
263 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000264 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-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 Friedman50c39ea2009-05-27 06:04:58 +0000268 }
269
270 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000274 if (E->hasBlockDeclRefExprs())
275 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Steve Naroff3aaa4822009-04-16 19:02:57 +0000277 return APValue(E, 0);
278}
279
Eli Friedman4efaa272008-11-12 09:44:48 +0000280APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000281 if (!Info.AnyLValue && !E->isFileScope())
282 return APValue();
283 return APValue(E, 0);
Eli Friedman4efaa272008-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 Kremenek6217b802009-07-29 21:53:49 +0000292 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-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 Kremenek6217b802009-07-29 21:53:49 +0000300 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000301 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-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 Friedman2be58612009-05-30 21:09:44 +0000306
307 if (FD->getType()->isReferenceType())
308 return APValue();
309
Eli Friedman4efaa272008-11-12 09:44:48 +0000310 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000311 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000312 for (RecordDecl::field_iterator Field = RD->field_begin(),
313 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000314 Field != FieldEnd; (void)++Field, ++i) {
315 if (*Field == FD)
Eli Friedman4efaa272008-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 Stump1eb44332009-09-09 15:08:12 +0000325APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000326 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Anders Carlsson3068d112008-11-16 19:01:22 +0000328 if (!EvaluatePointer(E->getBase(), Result, Info))
329 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlsson3068d112008-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 Stump1eb44332009-09-09 15:08:12 +0000338 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000339 Result.getLValueOffset() + Offset);
340 return Result;
341}
Eli Friedman4efaa272008-11-12 09:44:48 +0000342
Mike Stump1eb44332009-09-09 15:08:12 +0000343APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000344 APValue Result;
345 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
346 return APValue();
347 return Result;
348}
349
Eli Friedman4efaa272008-11-12 09:44:48 +0000350//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000351// Pointer Evaluation
352//===----------------------------------------------------------------------===//
353
Anders Carlssonc754aa62008-07-08 05:13:58 +0000354namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000355class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000356 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000357 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000358public:
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner87eae5e2008-07-11 22:52:41 +0000360 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000361
Anders Carlsson2bad1682008-07-08 14:30:00 +0000362 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000363 return APValue();
364 }
365
366 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
367
Anders Carlsson650c92f2008-07-08 15:34:11 +0000368 APValue VisitBinaryOperator(const BinaryOperator *E);
369 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000370 APValue VisitUnaryExtension(const UnaryOperator *E)
371 { return Visit(E->getSubExpr()); }
372 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000373 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
374 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000375 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
376 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000377 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000378 APValue VisitBlockExpr(BlockExpr *E) {
379 if (!E->hasBlockDeclRefExprs())
380 return APValue(E, 0);
381 return APValue();
382 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000383 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
384 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000385 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000386 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000387 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
388 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
389 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000390 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000391};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000392} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000393
Chris Lattner87eae5e2008-07-11 22:52:41 +0000394static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000395 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000396 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000397 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-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 Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnerf5eeb052008-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 Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000411 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000412 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000413 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000415 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000416 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000417 return APValue();
418
Ted Kremenek6217b802009-07-29 21:53:49 +0000419 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000420 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson4d4c50d2009-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 Friedman4efaa272008-11-12 09:44:48 +0000427
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000428 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000429
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000430 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000431 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000432 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000433 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
434
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000435 return APValue(ResultLValue.getLValueBase(), Offset);
436}
Eli Friedman4efaa272008-11-12 09:44:48 +0000437
Eli Friedman2217c872009-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 Friedman4efaa272008-11-12 09:44:48 +0000442 return APValue();
443}
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000445
Chris Lattnerb542afe2008-07-11 19:10:17 +0000446APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000447 const Expr* SubExpr = E->getSubExpr();
448
449 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000450 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000451 SubExpr->getType()->isObjCObjectPointerType() ||
452 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000453 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000454 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000455 return Result;
456 return APValue();
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000459 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-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 Lattnerf5eeb052008-07-11 18:11:29 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000469 // Cast is of an lvalue, no need to change value.
470 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000471 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000472
473 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000474 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-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 Lattnerf5eeb052008-07-11 18:11:29 +0000482 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000483}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000484
Eli Friedman3941b182009-01-25 01:54:01 +0000485APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000487 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000488 return APValue(E, 0);
489 return APValue();
490}
491
Eli Friedman4efaa272008-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 Lattnerf5eeb052008-07-11 18:11:29 +0000504
505//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000506// Vector Evaluation
507//===----------------------------------------------------------------------===//
508
509namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000510 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000511 : public StmtVisitor<VectorExprEvaluator, APValue> {
512 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000513 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000514 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Nate Begeman59b5da62009-01-18 03:20:47 +0000516 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Nate Begeman59b5da62009-01-18 03:20:47 +0000518 APValue VisitStmt(Stmt *S) {
519 return APValue();
520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Eli Friedman91110ee2009-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 Begeman59b5da62009-01-18 03:20:47 +0000532 APValue VisitCastExpr(const CastExpr* E);
533 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
534 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000535 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000536 APValue VisitChooseExpr(const ChooseExpr *E)
537 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000538 APValue VisitUnaryImag(const UnaryOperator *E);
539 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000540 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000541 // shufflevector, ExtVectorElementExpr
542 // (Note that these require implementing conversions
543 // between vector types.)
Nate Begeman59b5da62009-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 McCall183700f2009-09-21 23:43:11 +0000555 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000556 QualType EltTy = VTy->getElementType();
557 unsigned NElts = VTy->getNumElements();
558 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Nate Begeman59b5da62009-01-18 03:20:47 +0000560 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000561 QualType SETy = SE->getType();
562 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000563
Nate Begemane8c9e922009-06-26 18:22:18 +0000564 // Check for vector->vector bitcast and scalar->vector splat.
565 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000566 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000567 } else if (SETy->isIntegerType()) {
568 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000569 if (!EvaluateInteger(SE, IntResult, Info))
570 return APValue();
571 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000572 } else if (SETy->isRealFloatingType()) {
573 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000574 if (!EvaluateFloat(SE, F, Info))
575 return APValue();
576 Result = APValue(F);
577 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000578 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000579
Nate Begemanc0b8b192009-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 Begemane8c9e922009-06-26 18:22:18 +0000601 }
Nate Begemanc0b8b192009-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 Stump1eb44332009-09-09 15:08:12 +0000609
Nate Begemanc0b8b192009-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 Stump1eb44332009-09-09 15:08:12 +0000614
Nate Begemanc0b8b192009-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 Begeman59b5da62009-01-18 03:20:47 +0000625}
626
Mike Stump1eb44332009-09-09 15:08:12 +0000627APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000628VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
629 return this->Visit(const_cast<Expr*>(E->getInitializer()));
630}
631
Mike Stump1eb44332009-09-09 15:08:12 +0000632APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000633VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000634 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000635 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000636 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Nate Begeman59b5da62009-01-18 03:20:47 +0000638 QualType EltTy = VT->getElementType();
639 llvm::SmallVector<APValue, 4> Elements;
640
Eli Friedman91110ee2009-02-23 04:23:56 +0000641 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000642 if (EltTy->isIntegerType()) {
643 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-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 Begeman59b5da62009-01-18 03:20:47 +0000650 Elements.push_back(APValue(sInt));
651 } else {
652 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-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 Begeman59b5da62009-01-18 03:20:47 +0000659 Elements.push_back(APValue(f));
660 }
661 }
662 return APValue(&Elements[0], Elements.size());
663}
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000666VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000667 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-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 Friedman91110ee2009-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 Begeman59b5da62009-01-18 03:20:47 +0000699//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000700// Integer Evaluation
701//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000702
703namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000704class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000705 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000706 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000707 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000708public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000709 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000710 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000711
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000712 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000713 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000714 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000715 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000716 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000717 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000718 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000719 return true;
720 }
721
Daniel Dunbar131eb432009-02-19 09:06:44 +0000722 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000723 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000724 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000725 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000726 Result = APValue(APSInt(I));
727 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000728 return true;
729 }
730
731 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000732 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000733 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000734 return true;
735 }
736
Anders Carlsson82206e22008-11-30 18:14:57 +0000737 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000738 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000739 if (Info.EvalResult.Diag == 0) {
740 Info.EvalResult.DiagLoc = L;
741 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000742 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000743 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000744 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Anders Carlssonc754aa62008-07-08 05:13:58 +0000747 //===--------------------------------------------------------------------===//
748 // Visitor Methods
749 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattner32fea9d2008-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 Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner32fea9d2008-11-12 07:43:42 +0000756 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000757 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnerb542afe2008-07-11 19:10:17 +0000760 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000761
Chris Lattner4c4867e2008-07-12 00:38:25 +0000762 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000763 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000764 }
765 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000766 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000767 }
768 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-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 Stump1eb44332009-09-09 15:08:12 +0000774 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000775 T1.getUnqualifiedType()),
776 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000777 }
Eli Friedman04309752009-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 Lattner4c4867e2008-07-12 00:38:25 +0000792 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000793 bool VisitBinaryOperator(const BinaryOperator *E);
794 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000795 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000796
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000797 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000798 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
799
Anders Carlsson3068d112008-11-16 19:01:22 +0000800 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000801 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Anders Carlsson3f704562008-12-21 22:39:40 +0000804 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000805 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson3068d112008-11-16 19:01:22 +0000808 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000809 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000810 }
811
Eli Friedman664a1042009-02-27 04:45:43 +0000812 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
813 return Success(0, E);
814 }
815
Sebastian Redl64b45f72009-01-05 20:52:13 +0000816 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000817 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000818 }
819
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000820 bool VisitChooseExpr(const ChooseExpr *E) {
821 return Visit(E->getChosenSubExpr(Info.Ctx));
822 }
823
Eli Friedman722c7172009-02-28 03:59:05 +0000824 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000825 bool VisitUnaryImag(const UnaryOperator *E);
826
Chris Lattnerfcee0012008-07-11 21:24:13 +0000827private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000828 unsigned GetAlignOfExpr(const Expr *E);
829 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000830 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000831};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000832} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000833
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000834static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000835 if (!E->getType()->isIntegralType())
836 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000838 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
839}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000840
Daniel Dunbar69ab26a2009-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 Dunbar30c37f42009-02-19 20:17:33 +0000845 Result = Val.getInt();
846 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000847}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000848
Eli Friedman04309752009-11-24 05:28:59 +0000849bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000850 // Enums are integer constant exprs.
Eli Friedman04309752009-11-24 05:28:59 +0000851 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000852 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000853 // signedness consistently; see PR3173.
Eli Friedman04309752009-11-24 05:28:59 +0000854 APSInt SI = ECD->getInitVal();
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000855 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
856 // FIXME: This is an ugly hack around the fact that enums don't
857 // set their width (!?!) consistently; see PR3173.
858 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
859 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000860 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000861
862 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000863 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000864 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
865 == Qualifiers::Const) {
Eli Friedman04309752009-11-24 05:28:59 +0000866 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000867 const VarDecl *Def = 0;
Eli Friedman04309752009-11-24 05:28:59 +0000868 if (const Expr *Init = VD->getDefinition(Def)) {
869 if (APValue *V = VD->getEvaluatedValue())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000870 return Success(V->getInt(), E);
871
Douglas Gregor78d15832009-05-26 18:54:04 +0000872 if (Visit(const_cast<Expr*>(Init))) {
873 // Cache the evaluated value in the variable declaration.
Eli Friedman04309752009-11-24 05:28:59 +0000874 VD->setEvaluatedValue(Info.Ctx, Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000875 return true;
876 }
877
878 return false;
879 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000880 }
881 }
882
Chris Lattner4c4867e2008-07-12 00:38:25 +0000883 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000884 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000885}
886
Chris Lattnera4d55d82008-10-06 06:40:35 +0000887/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
888/// as GCC.
889static int EvaluateBuiltinClassifyType(const CallExpr *E) {
890 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000891 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000892 enum gcc_type_class {
893 no_type_class = -1,
894 void_type_class, integer_type_class, char_type_class,
895 enumeral_type_class, boolean_type_class,
896 pointer_type_class, reference_type_class, offset_type_class,
897 real_type_class, complex_type_class,
898 function_type_class, method_type_class,
899 record_type_class, union_type_class,
900 array_type_class, string_type_class,
901 lang_type_class
902 };
Mike Stump1eb44332009-09-09 15:08:12 +0000903
904 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000905 // ideal, however it is what gcc does.
906 if (E->getNumArgs() == 0)
907 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Chris Lattnera4d55d82008-10-06 06:40:35 +0000909 QualType ArgTy = E->getArg(0)->getType();
910 if (ArgTy->isVoidType())
911 return void_type_class;
912 else if (ArgTy->isEnumeralType())
913 return enumeral_type_class;
914 else if (ArgTy->isBooleanType())
915 return boolean_type_class;
916 else if (ArgTy->isCharType())
917 return string_type_class; // gcc doesn't appear to use char_type_class
918 else if (ArgTy->isIntegerType())
919 return integer_type_class;
920 else if (ArgTy->isPointerType())
921 return pointer_type_class;
922 else if (ArgTy->isReferenceType())
923 return reference_type_class;
924 else if (ArgTy->isRealType())
925 return real_type_class;
926 else if (ArgTy->isComplexType())
927 return complex_type_class;
928 else if (ArgTy->isFunctionType())
929 return function_type_class;
930 else if (ArgTy->isStructureType())
931 return record_type_class;
932 else if (ArgTy->isUnionType())
933 return union_type_class;
934 else if (ArgTy->isArrayType())
935 return array_type_class;
936 else if (ArgTy->isUnionType())
937 return union_type_class;
938 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
939 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
940 return -1;
941}
942
Chris Lattner4c4867e2008-07-12 00:38:25 +0000943bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000944 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000945 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000946 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000947
948 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000949 const Expr *Arg = E->getArg(0)->IgnoreParens();
950 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000951 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000952 && Base.Val.getKind() == APValue::LValue
953 && !Base.HasSideEffects)
954 if (const Expr *LVBase = Base.Val.getLValueBase())
955 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
956 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000957 if (!VD->getType()->isIncompleteType()
958 && VD->getType()->isObjectType()
959 && !VD->getType()->isVariablyModifiedType()
960 && !VD->getType()->isDependentType()) {
961 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
962 uint64_t Offset = Base.Val.getLValueOffset();
963 if (Offset <= Size)
964 Size -= Base.Val.getLValueOffset();
965 else
966 Size = 0;
967 return Success(Size, E);
968 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000969 }
970 }
971
Fariborz Jahanian393c2472009-11-05 18:03:03 +0000972 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Mike Stump4fc87582009-10-26 18:57:47 +0000973 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Chris Lattnercf184652009-11-03 19:48:51 +0000974 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000975 return Success(0, E);
976 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000977
Mike Stump64eda9e2009-10-26 18:35:08 +0000978 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
979 }
980
Chris Lattner019f4e82008-10-06 05:28:25 +0000981 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000982 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000984 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000985 // __builtin_constant_p always has one operand: it returns true if that
986 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000987 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000988
989 case Builtin::BI__builtin_eh_return_data_regno: {
990 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
991 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
992 return Success(Operand, E);
993 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000994 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000995}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000996
Chris Lattnerb542afe2008-07-11 19:10:17 +0000997bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000998 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000999 if (!Visit(E->getRHS()))
1000 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001001
Eli Friedman33ef1452009-02-26 10:19:36 +00001002 // If we can't evaluate the LHS, it might have side effects;
1003 // conservatively mark it.
1004 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1005 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001006
Anders Carlsson027f62e2008-12-01 02:07:06 +00001007 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001008 }
1009
1010 if (E->isLogicalOp()) {
1011 // These need to be handled specially because the operands aren't
1012 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001013 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001015 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001016 // We were able to evaluate the LHS, see if we can get away with not
1017 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001018 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001019 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001020
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001021 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001022 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001023 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001024 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001025 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001026 }
1027 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001028 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001029 // We can't evaluate the LHS; however, sometimes the result
1030 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001031 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001032 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001033 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001034 // must have had side effects.
1035 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001036
1037 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001038 }
1039 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001040 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001041
Eli Friedmana6afa762008-11-13 06:09:17 +00001042 return false;
1043 }
1044
Anders Carlsson286f85e2008-11-16 07:17:21 +00001045 QualType LHSTy = E->getLHS()->getType();
1046 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001047
1048 if (LHSTy->isAnyComplexType()) {
1049 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1050 APValue LHS, RHS;
1051
1052 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1053 return false;
1054
1055 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1056 return false;
1057
1058 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001059 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001060 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001061 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001062 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1063
Daniel Dunbar4087e242009-01-29 06:43:41 +00001064 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001065 return Success((CR_r == APFloat::cmpEqual &&
1066 CR_i == APFloat::cmpEqual), E);
1067 else {
1068 assert(E->getOpcode() == BinaryOperator::NE &&
1069 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001070 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001071 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001072 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001073 CR_i == APFloat::cmpLessThan)), E);
1074 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001075 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001076 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001077 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1078 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1079 else {
1080 assert(E->getOpcode() == BinaryOperator::NE &&
1081 "Invalid compex comparison.");
1082 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1083 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1084 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001085 }
1086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Anders Carlsson286f85e2008-11-16 07:17:21 +00001088 if (LHSTy->isRealFloatingType() &&
1089 RHSTy->isRealFloatingType()) {
1090 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Anders Carlsson286f85e2008-11-16 07:17:21 +00001092 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1093 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Anders Carlsson286f85e2008-11-16 07:17:21 +00001095 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1096 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Anders Carlsson286f85e2008-11-16 07:17:21 +00001098 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001099
Anders Carlsson286f85e2008-11-16 07:17:21 +00001100 switch (E->getOpcode()) {
1101 default:
1102 assert(0 && "Invalid binary operator!");
1103 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001104 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001105 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001106 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001107 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001108 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001109 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001110 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001111 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001112 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001113 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001114 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001115 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001116 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001117 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001120 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1121 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001122 APValue LHSValue;
1123 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1124 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001125
Anders Carlsson3068d112008-11-16 19:01:22 +00001126 APValue RHSValue;
1127 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1128 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001129
Eli Friedman5bc86102009-06-14 02:17:33 +00001130 // Reject any bases from the normal codepath; we special-case comparisons
1131 // to null.
1132 if (LHSValue.getLValueBase()) {
1133 if (!E->isEqualityOp())
1134 return false;
1135 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1136 return false;
1137 bool bres;
1138 if (!EvalPointerValueAsBool(LHSValue, bres))
1139 return false;
1140 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1141 } else if (RHSValue.getLValueBase()) {
1142 if (!E->isEqualityOp())
1143 return false;
1144 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1145 return false;
1146 bool bres;
1147 if (!EvalPointerValueAsBool(RHSValue, bres))
1148 return false;
1149 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1150 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001151
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001152 if (E->getOpcode() == BinaryOperator::Sub) {
1153 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001154 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001155
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001156 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001157 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1158 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001159
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001160 return Success(D, E);
1161 }
1162 bool Result;
1163 if (E->getOpcode() == BinaryOperator::EQ) {
1164 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001165 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001166 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1167 }
1168 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001169 }
1170 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001171 if (!LHSTy->isIntegralType() ||
1172 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001173 // We can't continue from here for non-integral types, and they
1174 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001175 return false;
1176 }
1177
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001178 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001179 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001180 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001181
Eli Friedman42edd0d2009-03-24 01:14:50 +00001182 APValue RHSVal;
1183 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001184 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001185
1186 // Handle cases like (unsigned long)&a + 4.
1187 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1188 uint64_t offset = Result.getLValueOffset();
1189 if (E->getOpcode() == BinaryOperator::Add)
1190 offset += RHSVal.getInt().getZExtValue();
1191 else
1192 offset -= RHSVal.getInt().getZExtValue();
1193 Result = APValue(Result.getLValueBase(), offset);
1194 return true;
1195 }
1196
1197 // Handle cases like 4 + (unsigned long)&a
1198 if (E->getOpcode() == BinaryOperator::Add &&
1199 RHSVal.isLValue() && Result.isInt()) {
1200 uint64_t offset = RHSVal.getLValueOffset();
1201 offset += Result.getInt().getZExtValue();
1202 Result = APValue(RHSVal.getLValueBase(), offset);
1203 return true;
1204 }
1205
1206 // All the following cases expect both operands to be an integer
1207 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001208 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001209
Eli Friedman42edd0d2009-03-24 01:14:50 +00001210 APSInt& RHS = RHSVal.getInt();
1211
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001212 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001213 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001214 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001215 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1216 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1217 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1218 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1219 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1220 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001221 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001222 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001223 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001224 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001225 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001226 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001227 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001228 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001229 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001230 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001231 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001232 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1233 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001234 }
1235 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001236 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001237 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1238 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001241 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1242 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1243 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1244 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1245 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1246 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001247 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001248}
1249
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001250bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001251 bool Cond;
1252 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001253 return false;
1254
Nuno Lopesa25bd552008-11-16 22:06:39 +00001255 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001256}
1257
Chris Lattneraf707ab2009-01-24 21:53:27 +00001258unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001259 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1260 // the result is the size of the referenced type."
1261 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1262 // result shall be the alignment of the referenced type."
1263 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1264 T = Ref->getPointeeType();
1265
Chris Lattnere9feb472009-01-24 21:09:06 +00001266 // Get information about the alignment.
1267 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001268
Eli Friedman2be58612009-05-30 21:09:44 +00001269 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001270 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001271}
1272
Chris Lattneraf707ab2009-01-24 21:53:27 +00001273unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1274 E = E->IgnoreParens();
1275
1276 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001277 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001278 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001279 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001280
Chris Lattneraf707ab2009-01-24 21:53:27 +00001281 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001282 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1283 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001284
Chris Lattnere9feb472009-01-24 21:09:06 +00001285 return GetAlignOfType(E->getType());
1286}
1287
1288
Sebastian Redl05189992008-11-11 17:56:53 +00001289/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1290/// expression's type.
1291bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1292 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001293
Chris Lattnere9feb472009-01-24 21:09:06 +00001294 // Handle alignof separately.
1295 if (!E->isSizeOf()) {
1296 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001297 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001298 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001299 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001300 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001301
Sebastian Redl05189992008-11-11 17:56:53 +00001302 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001303 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1304 // the result is the size of the referenced type."
1305 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1306 // result shall be the alignment of the referenced type."
1307 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1308 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001309
Daniel Dunbar131eb432009-02-19 09:06:44 +00001310 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1311 // extension.
1312 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1313 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001314
Chris Lattnerfcee0012008-07-11 21:24:13 +00001315 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001316 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001317 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001318
Chris Lattnere9feb472009-01-24 21:09:06 +00001319 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001320 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001321 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001322}
1323
Chris Lattnerb542afe2008-07-11 19:10:17 +00001324bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001325 // Special case unary operators that do not need their subexpression
1326 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001327 if (E->isOffsetOfOp()) {
1328 // The AST for offsetof is defined in such a way that we can just
1329 // directly Evaluate it as an l-value.
1330 APValue LV;
1331 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1332 return false;
1333 if (LV.getLValueBase())
1334 return false;
1335 return Success(LV.getLValueOffset(), E);
1336 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001337
1338 if (E->getOpcode() == UnaryOperator::LNot) {
1339 // LNot's operand isn't necessarily an integer, so we handle it specially.
1340 bool bres;
1341 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1342 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001343 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001344 }
1345
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001346 // Only handle integral operations...
1347 if (!E->getSubExpr()->getType()->isIntegralType())
1348 return false;
1349
Chris Lattner87eae5e2008-07-11 22:52:41 +00001350 // Get the operand value into 'Result'.
1351 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001352 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001353
Chris Lattner75a48812008-07-11 22:15:16 +00001354 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001355 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001356 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1357 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001358 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001359 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001360 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1361 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001362 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001363 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001365 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001366 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001367 if (!Result.isInt()) return false;
1368 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001369 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001370 if (!Result.isInt()) return false;
1371 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001372 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001373}
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Chris Lattner732b2232008-07-12 01:15:53 +00001375/// HandleCast - This is used to evaluate implicit or explicit casts where the
1376/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001377bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001378 Expr *SubExpr = E->getSubExpr();
1379 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001380 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001381
Eli Friedman4efaa272008-11-12 09:44:48 +00001382 if (DestType->isBooleanType()) {
1383 bool BoolResult;
1384 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1385 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001386 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001387 }
1388
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001389 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001390 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001391 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001392 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001393
Eli Friedmanbe265702009-02-20 01:15:07 +00001394 if (!Result.isInt()) {
1395 // Only allow casts of lvalues if they are lossless.
1396 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1397 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001398
Daniel Dunbardd211642009-02-19 22:24:01 +00001399 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001400 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001401 }
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Chris Lattner732b2232008-07-12 01:15:53 +00001403 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001404 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001405 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001406 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001407 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001408
Daniel Dunbardd211642009-02-19 22:24:01 +00001409 if (LV.getLValueBase()) {
1410 // Only allow based lvalue casts if they are lossless.
1411 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1412 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001413
Daniel Dunbardd211642009-02-19 22:24:01 +00001414 Result = LV;
1415 return true;
1416 }
1417
1418 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1419 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001420 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001421
Eli Friedmanbe265702009-02-20 01:15:07 +00001422 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1423 // This handles double-conversion cases, where there's both
1424 // an l-value promotion and an implicit conversion to int.
1425 APValue LV;
1426 if (!EvaluateLValue(SubExpr, LV, Info))
1427 return false;
1428
1429 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1430 return false;
1431
1432 Result = LV;
1433 return true;
1434 }
1435
Eli Friedman1725f682009-04-22 19:23:09 +00001436 if (SrcType->isAnyComplexType()) {
1437 APValue C;
1438 if (!EvaluateComplex(SubExpr, C, Info))
1439 return false;
1440 if (C.isComplexFloat())
1441 return Success(HandleFloatToIntCast(DestType, SrcType,
1442 C.getComplexFloatReal(), Info.Ctx),
1443 E);
1444 else
1445 return Success(HandleIntToIntCast(DestType, SrcType,
1446 C.getComplexIntReal(), Info.Ctx), E);
1447 }
Eli Friedman2217c872009-02-22 11:46:18 +00001448 // FIXME: Handle vectors
1449
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001450 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001451 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001452
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001453 APFloat F(0.0);
1454 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001455 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001457 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001458}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001459
Eli Friedman722c7172009-02-28 03:59:05 +00001460bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1461 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1462 APValue LV;
1463 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1464 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1465 return Success(LV.getComplexIntReal(), E);
1466 }
1467
1468 return Visit(E->getSubExpr());
1469}
1470
Eli Friedman664a1042009-02-27 04:45:43 +00001471bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001472 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1473 APValue LV;
1474 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1475 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1476 return Success(LV.getComplexIntImag(), E);
1477 }
1478
Eli Friedman664a1042009-02-27 04:45:43 +00001479 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1480 Info.EvalResult.HasSideEffects = true;
1481 return Success(0, E);
1482}
1483
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001484//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001485// Float Evaluation
1486//===----------------------------------------------------------------------===//
1487
1488namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001489class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001490 : public StmtVisitor<FloatExprEvaluator, bool> {
1491 EvalInfo &Info;
1492 APFloat &Result;
1493public:
1494 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1495 : Info(info), Result(result) {}
1496
1497 bool VisitStmt(Stmt *S) {
1498 return false;
1499 }
1500
1501 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001502 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001503
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001504 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001505 bool VisitBinaryOperator(const BinaryOperator *E);
1506 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001507 bool VisitCastExpr(CastExpr *E);
1508 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001509
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001510 bool VisitChooseExpr(const ChooseExpr *E)
1511 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1512 bool VisitUnaryExtension(const UnaryOperator *E)
1513 { return Visit(E->getSubExpr()); }
1514
1515 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1516 // member of vector, ImplicitValueInitExpr,
Eli Friedman7f92f032009-11-16 04:25:37 +00001517 // conditional ?:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001518};
1519} // end anonymous namespace
1520
1521static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1522 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1523}
1524
Chris Lattner019f4e82008-10-06 05:28:25 +00001525bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001526 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001527 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001528 case Builtin::BI__builtin_huge_val:
1529 case Builtin::BI__builtin_huge_valf:
1530 case Builtin::BI__builtin_huge_vall:
1531 case Builtin::BI__builtin_inf:
1532 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001533 case Builtin::BI__builtin_infl: {
1534 const llvm::fltSemantics &Sem =
1535 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001536 Result = llvm::APFloat::getInf(Sem);
1537 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Chris Lattner9e621712008-10-06 06:31:58 +00001540 case Builtin::BI__builtin_nan:
1541 case Builtin::BI__builtin_nanf:
1542 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001543 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001544 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001545 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001546 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001547 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001548 const llvm::fltSemantics &Sem =
1549 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001550 llvm::SmallString<16> s;
1551 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1552 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001553 long l;
1554 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001555 l = strtol(&s[0], &endp, 0);
1556 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001557 return false;
1558 unsigned type = (unsigned int)l;;
1559 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001560 return true;
1561 }
1562 }
1563 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001564
1565 case Builtin::BI__builtin_fabs:
1566 case Builtin::BI__builtin_fabsf:
1567 case Builtin::BI__builtin_fabsl:
1568 if (!EvaluateFloat(E->getArg(0), Result, Info))
1569 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001571 if (Result.isNegative())
1572 Result.changeSign();
1573 return true;
1574
Mike Stump1eb44332009-09-09 15:08:12 +00001575 case Builtin::BI__builtin_copysign:
1576 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001577 case Builtin::BI__builtin_copysignl: {
1578 APFloat RHS(0.);
1579 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1580 !EvaluateFloat(E->getArg(1), RHS, Info))
1581 return false;
1582 Result.copySign(RHS);
1583 return true;
1584 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001585 }
1586}
1587
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001588bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001589 if (E->getOpcode() == UnaryOperator::Deref)
1590 return false;
1591
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001592 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1593 return false;
1594
1595 switch (E->getOpcode()) {
1596 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001597 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001598 return true;
1599 case UnaryOperator::Minus:
1600 Result.changeSign();
1601 return true;
1602 }
1603}
Chris Lattner019f4e82008-10-06 05:28:25 +00001604
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001605bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001606 if (E->getOpcode() == BinaryOperator::Comma) {
1607 if (!EvaluateFloat(E->getRHS(), Result, Info))
1608 return false;
1609
1610 // If we can't evaluate the LHS, it might have side effects;
1611 // conservatively mark it.
1612 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1613 Info.EvalResult.HasSideEffects = true;
1614
1615 return true;
1616 }
1617
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001618 // FIXME: Diagnostics? I really don't understand how the warnings
1619 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001620 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001621 if (!EvaluateFloat(E->getLHS(), Result, Info))
1622 return false;
1623 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1624 return false;
1625
1626 switch (E->getOpcode()) {
1627 default: return false;
1628 case BinaryOperator::Mul:
1629 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1630 return true;
1631 case BinaryOperator::Add:
1632 Result.add(RHS, APFloat::rmNearestTiesToEven);
1633 return true;
1634 case BinaryOperator::Sub:
1635 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1636 return true;
1637 case BinaryOperator::Div:
1638 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1639 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001640 }
1641}
1642
1643bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1644 Result = E->getValue();
1645 return true;
1646}
1647
Eli Friedman4efaa272008-11-12 09:44:48 +00001648bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1649 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Eli Friedman4efaa272008-11-12 09:44:48 +00001651 if (SubExpr->getType()->isIntegralType()) {
1652 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001653 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001654 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001655 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001656 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001657 return true;
1658 }
1659 if (SubExpr->getType()->isRealFloatingType()) {
1660 if (!Visit(SubExpr))
1661 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001662 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1663 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001664 return true;
1665 }
Eli Friedman2217c872009-02-22 11:46:18 +00001666 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001667
1668 return false;
1669}
1670
1671bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1672 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1673 return true;
1674}
1675
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001676//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001677// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001678//===----------------------------------------------------------------------===//
1679
1680namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001681class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001682 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001683 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001685public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001686 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001688 //===--------------------------------------------------------------------===//
1689 // Visitor Methods
1690 //===--------------------------------------------------------------------===//
1691
1692 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001693 return APValue();
1694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001696 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1697
1698 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001699 Expr* SubExpr = E->getSubExpr();
1700
1701 if (SubExpr->getType()->isRealFloatingType()) {
1702 APFloat Result(0.0);
1703
1704 if (!EvaluateFloat(SubExpr, Result, Info))
1705 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001706
1707 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001708 Result);
1709 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001710 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001711 "Unexpected imaginary literal.");
1712
1713 llvm::APSInt Result;
1714 if (!EvaluateInteger(SubExpr, Result, Info))
1715 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001717 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1718 Zero = 0;
1719 return APValue(Zero, Result);
1720 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001721 }
1722
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001723 APValue VisitCastExpr(CastExpr *E) {
1724 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001725 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001726 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001727
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001728 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001729 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001730
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001731 if (!EvaluateFloat(SubExpr, Result, Info))
1732 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001733
1734 if (EltType->isRealFloatingType()) {
1735 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001736 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001737 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1738 } else {
1739 llvm::APSInt IResult;
1740 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1741 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1742 Zero = 0;
1743 return APValue(IResult, Zero);
1744 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001745 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001746 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001747
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001748 if (!EvaluateInteger(SubExpr, Result, Info))
1749 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001750
Eli Friedman1725f682009-04-22 19:23:09 +00001751 if (EltType->isRealFloatingType()) {
1752 APFloat FResult =
1753 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001754 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001755 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1756 } else {
1757 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1758 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1759 Zero = 0;
1760 return APValue(Result, Zero);
1761 }
John McCall183700f2009-09-21 23:43:11 +00001762 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001763 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001764
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001765 if (!EvaluateComplex(SubExpr, Src, Info))
1766 return APValue();
1767
1768 QualType SrcType = CT->getElementType();
1769
1770 if (Src.isComplexFloat()) {
1771 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001772 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001773 Src.getComplexFloatReal(),
1774 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001775 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001776 Src.getComplexFloatImag(),
1777 Info.Ctx));
1778 } else {
1779 return APValue(HandleFloatToIntCast(EltType, SrcType,
1780 Src.getComplexFloatReal(),
1781 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001782 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001783 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001784 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001785 }
1786 } else {
1787 assert(Src.isComplexInt() && "Invalid evaluate result.");
1788 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001789 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001790 Src.getComplexIntReal(),
1791 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001792 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001793 Src.getComplexIntImag(),
1794 Info.Ctx));
1795 } else {
1796 return APValue(HandleIntToIntCast(EltType, SrcType,
1797 Src.getComplexIntReal(),
1798 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001799 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001800 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001801 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001802 }
1803 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001804 }
1805
1806 // FIXME: Handle more casts.
1807 return APValue();
1808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001810 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001811 APValue VisitChooseExpr(const ChooseExpr *E)
1812 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1813 APValue VisitUnaryExtension(const UnaryOperator *E)
1814 { return Visit(E->getSubExpr()); }
1815 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001816 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001817};
1818} // end anonymous namespace
1819
Mike Stump1eb44332009-09-09 15:08:12 +00001820static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001821 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1822 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001823 (&Result.getComplexFloatReal().getSemantics() ==
1824 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001825 "Invalid complex evaluation.");
1826 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001827}
1828
Mike Stump1eb44332009-09-09 15:08:12 +00001829APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001830 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001832 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001833 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001835 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001836 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001837
Daniel Dunbar3f279872009-01-29 01:32:56 +00001838 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1839 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001840 switch (E->getOpcode()) {
1841 default: return APValue();
1842 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001843 if (Result.isComplexFloat()) {
1844 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1845 APFloat::rmNearestTiesToEven);
1846 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1847 APFloat::rmNearestTiesToEven);
1848 } else {
1849 Result.getComplexIntReal() += RHS.getComplexIntReal();
1850 Result.getComplexIntImag() += RHS.getComplexIntImag();
1851 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001852 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001853 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001854 if (Result.isComplexFloat()) {
1855 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1856 APFloat::rmNearestTiesToEven);
1857 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1858 APFloat::rmNearestTiesToEven);
1859 } else {
1860 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1861 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1862 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001863 break;
1864 case BinaryOperator::Mul:
1865 if (Result.isComplexFloat()) {
1866 APValue LHS = Result;
1867 APFloat &LHS_r = LHS.getComplexFloatReal();
1868 APFloat &LHS_i = LHS.getComplexFloatImag();
1869 APFloat &RHS_r = RHS.getComplexFloatReal();
1870 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Daniel Dunbar3f279872009-01-29 01:32:56 +00001872 APFloat Tmp = LHS_r;
1873 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1874 Result.getComplexFloatReal() = Tmp;
1875 Tmp = LHS_i;
1876 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1877 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1878
1879 Tmp = LHS_r;
1880 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1881 Result.getComplexFloatImag() = Tmp;
1882 Tmp = LHS_i;
1883 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1884 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1885 } else {
1886 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001887 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001888 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1889 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001890 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001891 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1892 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1893 }
1894 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001895 }
1896
1897 return Result;
1898}
1899
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001900//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001901// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001902//===----------------------------------------------------------------------===//
1903
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001904/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001905/// any crazy technique (that has nothing to do with language standards) that
1906/// we want to. If this function returns true, it returns the folded constant
1907/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001908bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1909 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001910
Nate Begeman59b5da62009-01-18 03:20:47 +00001911 if (getType()->isVectorType()) {
1912 if (!EvaluateVector(this, Result.Val, Info))
1913 return false;
1914 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001915 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001916 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001917 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001918 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001919 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001920 } else if (getType()->isRealFloatingType()) {
1921 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001922 if (!EvaluateFloat(this, f, Info))
1923 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001925 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001926 } else if (getType()->isAnyComplexType()) {
1927 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001928 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001929 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001930 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001931
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001932 return true;
1933}
1934
Mike Stump660e6f72009-10-26 23:05:19 +00001935bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1936 EvalInfo Info(Ctx, Result, true);
1937
1938 if (getType()->isVectorType()) {
1939 if (!EvaluateVector(this, Result.Val, Info))
1940 return false;
1941 } else if (getType()->isIntegerType()) {
1942 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1943 return false;
1944 } else if (getType()->hasPointerRepresentation()) {
1945 if (!EvaluatePointer(this, Result.Val, Info))
1946 return false;
1947 } else if (getType()->isRealFloatingType()) {
1948 llvm::APFloat f(0.0);
1949 if (!EvaluateFloat(this, f, Info))
1950 return false;
1951
1952 Result.Val = APValue(f);
1953 } else if (getType()->isAnyComplexType()) {
1954 if (!EvaluateComplex(this, Result.Val, Info))
1955 return false;
1956 } else
1957 return false;
1958
1959 return true;
1960}
1961
Anders Carlsson1b782762009-04-10 04:54:13 +00001962bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1963 EvalInfo Info(Ctx, Result);
1964
1965 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1966}
1967
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001968bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1969 EvalInfo Info(Ctx, Result, true);
1970
1971 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1972}
1973
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001974/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001975/// folded, but discard the result.
1976bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001977 EvalResult Result;
1978 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001979}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001980
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001981bool Expr::HasSideEffects(ASTContext &Ctx) const {
1982 Expr::EvalResult Result;
1983 EvalInfo Info(Ctx, Result);
1984 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1985}
1986
Anders Carlsson51fe9962008-11-22 21:04:56 +00001987APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001988 EvalResult EvalResult;
1989 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001990 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001991 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001992 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001993
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001994 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001995}