blob: c9aff6408fa940c7e2178cfe2a56fd0303ca1c03 [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"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000020#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000021#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000022#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlssonc44eec62008-07-03 04:20:39 +000025using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000028
Chris Lattner87eae5e2008-07-11 22:52:41 +000029/// EvalInfo - This is a private struct used by the evaluator to capture
30/// information about a subexpression as it is folded. It retains information
31/// about the AST context, but also maintains information about the folded
32/// expression.
33///
34/// If an expression could be evaluated, it is still possible it is not a C
35/// "integer constant expression" or constant expression. If not, this struct
36/// captures information about how and why not.
37///
38/// One bit of information passed *into* the request for constant folding
39/// indicates whether the subexpression is "evaluated" or not according to C
40/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
41/// evaluate the expression regardless of what the RHS is, but C only allows
42/// certain things in certain situations.
43struct EvalInfo {
44 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Anders Carlsson54da0492008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000048
Eli Friedmanb2f295c2009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000055};
56
57
Eli Friedman4efaa272008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
62 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000063static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000064static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000065
66//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000067// Misc utilities
68//===----------------------------------------------------------------------===//
69
Eli Friedman5bc86102009-06-14 02:17:33 +000070static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
71 // FIXME: Is this accurate for all kinds of bases? If not, what would
72 // the check look like?
Ken Dycka7305832010-01-15 12:37:54 +000073 Result = Value.getLValueBase() || !Value.getLValueOffset().isZero();
Eli Friedman5bc86102009-06-14 02:17:33 +000074 return true;
75}
76
John McCallcd7a4452010-01-05 23:42:56 +000077static bool HandleConversionToBool(const Expr* E, bool& Result,
78 EvalInfo &Info) {
Eli Friedman4efaa272008-11-12 09:44:48 +000079 if (E->getType()->isIntegralType()) {
80 APSInt IntResult;
81 if (!EvaluateInteger(E, IntResult, Info))
82 return false;
83 Result = IntResult != 0;
84 return true;
85 } else if (E->getType()->isRealFloatingType()) {
86 APFloat FloatResult(0.0);
87 if (!EvaluateFloat(E, FloatResult, Info))
88 return false;
89 Result = !FloatResult.isZero();
90 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000091 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000092 APValue PointerResult;
93 if (!EvaluatePointer(E, PointerResult, Info))
94 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000095 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000096 } else if (E->getType()->isAnyComplexType()) {
97 APValue ComplexResult;
98 if (!EvaluateComplex(E, ComplexResult, Info))
99 return false;
100 if (ComplexResult.isComplexFloat()) {
101 Result = !ComplexResult.getComplexFloatReal().isZero() ||
102 !ComplexResult.getComplexFloatImag().isZero();
103 } else {
104 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
105 ComplexResult.getComplexIntImag().getBoolValue();
106 }
107 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000108 }
109
110 return false;
111}
112
Mike Stump1eb44332009-09-09 15:08:12 +0000113static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000114 APFloat &Value, ASTContext &Ctx) {
115 unsigned DestWidth = Ctx.getIntWidth(DestType);
116 // Determine whether we are converting to unsigned or signed.
117 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000119 // FIXME: Warning for overflow.
120 uint64_t Space[4];
121 bool ignored;
122 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
123 llvm::APFloat::rmTowardZero, &ignored);
124 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
125}
126
Mike Stump1eb44332009-09-09 15:08:12 +0000127static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000128 APFloat &Value, ASTContext &Ctx) {
129 bool ignored;
130 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000131 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000132 APFloat::rmNearestTiesToEven, &ignored);
133 return Result;
134}
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000137 APSInt &Value, ASTContext &Ctx) {
138 unsigned DestWidth = Ctx.getIntWidth(DestType);
139 APSInt Result = Value;
140 // Figure out if this is a truncate, extend or noop cast.
141 // If the input is signed, do a sign extend, noop, or truncate.
142 Result.extOrTrunc(DestWidth);
143 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
144 return Result;
145}
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000148 APSInt &Value, ASTContext &Ctx) {
149
150 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
151 Result.convertFromAPInt(Value, Value.isSigned(),
152 APFloat::rmNearestTiesToEven);
153 return Result;
154}
155
Mike Stumpc4c90452009-10-27 22:09:17 +0000156namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000157class HasSideEffect
Mike Stumpc4c90452009-10-27 22:09:17 +0000158 : public StmtVisitor<HasSideEffect, bool> {
159 EvalInfo &Info;
160public:
161
162 HasSideEffect(EvalInfo &info) : Info(info) {}
163
164 // Unhandled nodes conservatively default to having side effects.
165 bool VisitStmt(Stmt *S) {
166 return true;
167 }
168
169 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
170 bool VisitDeclRefExpr(DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000171 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000172 return true;
173 return false;
174 }
175 // We don't want to evaluate BlockExprs multiple times, as they generate
176 // a ton of code.
177 bool VisitBlockExpr(BlockExpr *E) { return true; }
178 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
179 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
180 { return Visit(E->getInitializer()); }
181 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
182 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
183 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
184 bool VisitStringLiteral(StringLiteral *E) { return false; }
185 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
186 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
187 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000188 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000189 bool VisitChooseExpr(ChooseExpr *E)
190 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
191 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
192 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000193 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000194 bool VisitBinaryOperator(BinaryOperator *E)
195 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000196 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
198 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
200 bool VisitUnaryDeref(UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000201 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000202 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000203 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000204 }
205 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000206
207 // Has side effects if any element does.
208 bool VisitInitListExpr(InitListExpr *E) {
209 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
210 if (Visit(E->getInit(i))) return true;
211 return false;
212 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000213};
214
Mike Stumpc4c90452009-10-27 22:09:17 +0000215} // end anonymous namespace
216
Eli Friedman4efaa272008-11-12 09:44:48 +0000217//===----------------------------------------------------------------------===//
218// LValue Evaluation
219//===----------------------------------------------------------------------===//
220namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000221class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000222 : public StmtVisitor<LValueExprEvaluator, APValue> {
223 EvalInfo &Info;
224public:
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Eli Friedman4efaa272008-11-12 09:44:48 +0000226 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
227
228 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000229 return APValue();
230 }
231
232 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000233 APValue VisitDeclRefExpr(DeclRefExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000234 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000235 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
236 APValue VisitMemberExpr(MemberExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000237 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); }
238 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000239 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000240 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000241 APValue VisitUnaryExtension(const UnaryOperator *E)
242 { return Visit(E->getSubExpr()); }
243 APValue VisitChooseExpr(const ChooseExpr *E)
244 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000245
246 APValue VisitCastExpr(CastExpr *E) {
247 switch (E->getCastKind()) {
248 default:
249 return APValue();
250
251 case CastExpr::CK_NoOp:
252 return Visit(E->getSubExpr());
253 }
254 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000255 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000256};
257} // end anonymous namespace
258
259static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
260 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
261 return Result.isLValue();
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000265 if (isa<FunctionDecl>(E->getDecl())) {
Ken Dycka7305832010-01-15 12:37:54 +0000266 return APValue(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000267 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000268 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000269 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000270 if (!VD->getType()->isReferenceType())
Ken Dycka7305832010-01-15 12:37:54 +0000271 return APValue(E);
Eli Friedmand933a012009-08-29 19:09:59 +0000272 // FIXME: Check whether VD might be overridden!
Sebastian Redl31310a22010-02-01 20:16:42 +0000273 if (const Expr *Init = VD->getAnyInitializer())
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000274 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000275 }
276
277 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000278}
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();
Ken Dycka7305832010-01-15 12:37:54 +0000283 return APValue(E);
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(),
Ken Dycka7305832010-01-15 12:37:54 +0000320 result.getLValueOffset() +
321 CharUnits::fromQuantity(RL.getFieldOffset(i) / 8));
Eli Friedman4efaa272008-11-12 09:44:48 +0000322
323 return result;
324}
325
Mike Stump1eb44332009-09-09 15:08:12 +0000326APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000327 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlsson3068d112008-11-16 19:01:22 +0000329 if (!EvaluatePointer(E->getBase(), Result, Info))
330 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlsson3068d112008-11-16 19:01:22 +0000332 APSInt Index;
333 if (!EvaluateInteger(E->getIdx(), Index, Info))
334 return APValue();
335
Ken Dyck199c3d62010-01-11 17:06:35 +0000336 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
Anders Carlsson3068d112008-11-16 19:01:22 +0000337
Ken Dyck199c3d62010-01-11 17:06:35 +0000338 CharUnits Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000339 Result.setLValue(Result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000340 Result.getLValueOffset() + Offset);
Anders Carlsson3068d112008-11-16 19:01:22 +0000341 return Result;
342}
Eli Friedman4efaa272008-11-12 09:44:48 +0000343
Mike Stump1eb44332009-09-09 15:08:12 +0000344APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000345 APValue Result;
346 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
347 return APValue();
348 return Result;
349}
350
Eli Friedman4efaa272008-11-12 09:44:48 +0000351//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000352// Pointer Evaluation
353//===----------------------------------------------------------------------===//
354
Anders Carlssonc754aa62008-07-08 05:13:58 +0000355namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000356class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000357 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000358 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000359public:
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner87eae5e2008-07-11 22:52:41 +0000361 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000362
Anders Carlsson2bad1682008-07-08 14:30:00 +0000363 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000364 return APValue();
365 }
366
367 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
368
Anders Carlsson650c92f2008-07-08 15:34:11 +0000369 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000370 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000371 APValue VisitUnaryExtension(const UnaryOperator *E)
372 { return Visit(E->getSubExpr()); }
373 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000374 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
Ken Dycka7305832010-01-15 12:37:54 +0000375 { return APValue(E); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000376 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000377 { return APValue(E); }
Eli Friedman3941b182009-01-25 01:54:01 +0000378 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000379 APValue VisitBlockExpr(BlockExpr *E) {
380 if (!E->hasBlockDeclRefExprs())
Ken Dycka7305832010-01-15 12:37:54 +0000381 return APValue(E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000382 return APValue();
383 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000384 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000385 { return APValue((Expr*)0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000386 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000387 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000388 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
389 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000390 { return APValue((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000391 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000392};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000394
Chris Lattner87eae5e2008-07-11 22:52:41 +0000395static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000396 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000397 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000398 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000399 return Result.isLValue();
400}
401
402APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
403 if (E->getOpcode() != BinaryOperator::Add &&
404 E->getOpcode() != BinaryOperator::Sub)
405 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000407 const Expr *PExp = E->getLHS();
408 const Expr *IExp = E->getRHS();
409 if (IExp->getType()->isPointerType())
410 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000413 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000414 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000416 llvm::APSInt AdditionalOffset;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000417 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000418 return APValue();
419
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000420 // Compute the new offset in the appropriate width.
421
422 QualType PointeeType =
423 PExp->getType()->getAs<PointerType>()->getPointeeType();
424 llvm::APSInt SizeOfPointee(AdditionalOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000426 // Explicitly handle GNU void* and function pointer arithmetic extensions.
427 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000428 SizeOfPointee = 1;
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000429 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000430 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType).getQuantity();
Eli Friedman4efaa272008-11-12 09:44:48 +0000431
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000432 llvm::APSInt Offset(AdditionalOffset);
433 Offset = ResultLValue.getLValueOffset().getQuantity();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000434 if (E->getOpcode() == BinaryOperator::Add)
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000435 Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000436 else
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000437 Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000438
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000439 // Sign extend prior to converting back to a char unit.
440 if (Offset.getBitWidth() < 64)
441 Offset.extend(64);
442 return APValue(ResultLValue.getLValueBase(),
443 CharUnits::fromQuantity(Offset.getLimitedValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000444}
Eli Friedman4efaa272008-11-12 09:44:48 +0000445
Eli Friedman2217c872009-02-22 11:46:18 +0000446APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
447 APValue result;
448 if (EvaluateLValue(E->getSubExpr(), result, Info))
449 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000450 return APValue();
451}
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000453
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000454APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
455 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000456
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000457 switch (E->getCastKind()) {
458 default:
459 break;
460
461 case CastExpr::CK_Unknown: {
462 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
463
464 // Check for pointer->pointer cast
465 if (SubExpr->getType()->isPointerType() ||
466 SubExpr->getType()->isObjCObjectPointerType() ||
467 SubExpr->getType()->isNullPtrType() ||
468 SubExpr->getType()->isBlockPointerType())
469 return Visit(SubExpr);
470
471 if (SubExpr->getType()->isIntegralType()) {
472 APValue Result;
473 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
474 break;
475
476 if (Result.isInt()) {
477 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000478 return APValue(0,
479 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000480 }
481
482 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000483 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000484 }
485 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000488 case CastExpr::CK_NoOp:
489 case CastExpr::CK_BitCast:
490 case CastExpr::CK_AnyPointerToObjCPointerCast:
491 case CastExpr::CK_AnyPointerToBlockPointerCast:
492 return Visit(SubExpr);
493
494 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000495 APValue Result;
496 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000497 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000498
499 if (Result.isInt()) {
500 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000501 return APValue(0,
502 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000505 // Cast is of an lvalue, no need to change value.
506 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000507 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000508 case CastExpr::CK_ArrayToPointerDecay:
509 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000510 APValue Result;
511 if (EvaluateLValue(SubExpr, Result, Info))
512 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000513 break;
514 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000515 }
516
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000517 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000518}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000519
Eli Friedman3941b182009-01-25 01:54:01 +0000520APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000521 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000522 Builtin::BI__builtin___CFStringMakeConstantString ||
523 E->isBuiltinCall(Info.Ctx) ==
524 Builtin::BI__builtin___NSStringMakeConstantString)
Ken Dycka7305832010-01-15 12:37:54 +0000525 return APValue(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000526 return APValue();
527}
528
Eli Friedman4efaa272008-11-12 09:44:48 +0000529APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
530 bool BoolResult;
531 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
532 return APValue();
533
534 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
535
536 APValue Result;
537 if (EvaluatePointer(EvalExpr, Result, Info))
538 return Result;
539 return APValue();
540}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000541
542//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000543// Vector Evaluation
544//===----------------------------------------------------------------------===//
545
546namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000547 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000548 : public StmtVisitor<VectorExprEvaluator, APValue> {
549 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000550 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000551 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Nate Begeman59b5da62009-01-18 03:20:47 +0000553 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Nate Begeman59b5da62009-01-18 03:20:47 +0000555 APValue VisitStmt(Stmt *S) {
556 return APValue();
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Eli Friedman91110ee2009-02-23 04:23:56 +0000559 APValue VisitParenExpr(ParenExpr *E)
560 { return Visit(E->getSubExpr()); }
561 APValue VisitUnaryExtension(const UnaryOperator *E)
562 { return Visit(E->getSubExpr()); }
563 APValue VisitUnaryPlus(const UnaryOperator *E)
564 { return Visit(E->getSubExpr()); }
565 APValue VisitUnaryReal(const UnaryOperator *E)
566 { return Visit(E->getSubExpr()); }
567 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
568 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000569 APValue VisitCastExpr(const CastExpr* E);
570 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
571 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000572 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000573 APValue VisitChooseExpr(const ChooseExpr *E)
574 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000575 APValue VisitUnaryImag(const UnaryOperator *E);
576 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000577 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000578 // shufflevector, ExtVectorElementExpr
579 // (Note that these require implementing conversions
580 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000581 };
582} // end anonymous namespace
583
584static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
585 if (!E->getType()->isVectorType())
586 return false;
587 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
588 return !Result.isUninit();
589}
590
591APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000592 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000593 QualType EltTy = VTy->getElementType();
594 unsigned NElts = VTy->getNumElements();
595 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Nate Begeman59b5da62009-01-18 03:20:47 +0000597 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000598 QualType SETy = SE->getType();
599 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000600
Nate Begemane8c9e922009-06-26 18:22:18 +0000601 // Check for vector->vector bitcast and scalar->vector splat.
602 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000603 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000604 } else if (SETy->isIntegerType()) {
605 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000606 if (!EvaluateInteger(SE, IntResult, Info))
607 return APValue();
608 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000609 } else if (SETy->isRealFloatingType()) {
610 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000611 if (!EvaluateFloat(SE, F, Info))
612 return APValue();
613 Result = APValue(F);
614 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000615 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000616
Nate Begemanc0b8b192009-07-01 07:50:47 +0000617 // For casts of a scalar to ExtVector, convert the scalar to the element type
618 // and splat it to all elements.
619 if (E->getType()->isExtVectorType()) {
620 if (EltTy->isIntegerType() && Result.isInt())
621 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
622 Info.Ctx));
623 else if (EltTy->isIntegerType())
624 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
625 Info.Ctx));
626 else if (EltTy->isRealFloatingType() && Result.isInt())
627 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
628 Info.Ctx));
629 else if (EltTy->isRealFloatingType())
630 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
631 Info.Ctx));
632 else
633 return APValue();
634
635 // Splat and create vector APValue.
636 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
637 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000638 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000639
640 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
641 // to the vector. To construct the APValue vector initializer, bitcast the
642 // initializing value to an APInt, and shift out the bits pertaining to each
643 // element.
644 APSInt Init;
645 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Nate Begemanc0b8b192009-07-01 07:50:47 +0000647 llvm::SmallVector<APValue, 4> Elts;
648 for (unsigned i = 0; i != NElts; ++i) {
649 APSInt Tmp = Init;
650 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Nate Begemanc0b8b192009-07-01 07:50:47 +0000652 if (EltTy->isIntegerType())
653 Elts.push_back(APValue(Tmp));
654 else if (EltTy->isRealFloatingType())
655 Elts.push_back(APValue(APFloat(Tmp)));
656 else
657 return APValue();
658
659 Init >>= EltWidth;
660 }
661 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000662}
663
Mike Stump1eb44332009-09-09 15:08:12 +0000664APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000665VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
666 return this->Visit(const_cast<Expr*>(E->getInitializer()));
667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000670VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000671 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000672 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000673 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Nate Begeman59b5da62009-01-18 03:20:47 +0000675 QualType EltTy = VT->getElementType();
676 llvm::SmallVector<APValue, 4> Elements;
677
Eli Friedman91110ee2009-02-23 04:23:56 +0000678 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000679 if (EltTy->isIntegerType()) {
680 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000681 if (i < NumInits) {
682 if (!EvaluateInteger(E->getInit(i), sInt, Info))
683 return APValue();
684 } else {
685 sInt = Info.Ctx.MakeIntValue(0, EltTy);
686 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000687 Elements.push_back(APValue(sInt));
688 } else {
689 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000690 if (i < NumInits) {
691 if (!EvaluateFloat(E->getInit(i), f, Info))
692 return APValue();
693 } else {
694 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
695 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000696 Elements.push_back(APValue(f));
697 }
698 }
699 return APValue(&Elements[0], Elements.size());
700}
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000703VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000704 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000705 QualType EltTy = VT->getElementType();
706 APValue ZeroElement;
707 if (EltTy->isIntegerType())
708 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
709 else
710 ZeroElement =
711 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
712
713 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
714 return APValue(&Elements[0], Elements.size());
715}
716
717APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
718 bool BoolResult;
719 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
720 return APValue();
721
722 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
723
724 APValue Result;
725 if (EvaluateVector(EvalExpr, Result, Info))
726 return Result;
727 return APValue();
728}
729
Eli Friedman91110ee2009-02-23 04:23:56 +0000730APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
731 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
732 Info.EvalResult.HasSideEffects = true;
733 return GetZeroVector(E->getType());
734}
735
Nate Begeman59b5da62009-01-18 03:20:47 +0000736//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000737// Integer Evaluation
738//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000739
740namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000741class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000742 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000743 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000744 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000745public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000746 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000747 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000748
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000749 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000750 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000751 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000752 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000753 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000754 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000755 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000756 return true;
757 }
758
Daniel Dunbar131eb432009-02-19 09:06:44 +0000759 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000760 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000761 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000762 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000763 Result = APValue(APSInt(I));
764 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000765 return true;
766 }
767
768 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000769 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000770 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000771 return true;
772 }
773
Anders Carlsson82206e22008-11-30 18:14:57 +0000774 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000775 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000776 if (Info.EvalResult.Diag == 0) {
777 Info.EvalResult.DiagLoc = L;
778 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000779 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000780 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000781 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Anders Carlssonc754aa62008-07-08 05:13:58 +0000784 //===--------------------------------------------------------------------===//
785 // Visitor Methods
786 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Chris Lattner32fea9d2008-11-12 07:43:42 +0000788 bool VisitStmt(Stmt *) {
789 assert(0 && "This should be called on integers, stmts are not integers");
790 return false;
791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattner32fea9d2008-11-12 07:43:42 +0000793 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000794 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Chris Lattnerb542afe2008-07-11 19:10:17 +0000797 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000798
Chris Lattner4c4867e2008-07-12 00:38:25 +0000799 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000800 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000801 }
802 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000803 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000804 }
805 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000806 // Per gcc docs "this built-in function ignores top level
807 // qualifiers". We need to use the canonical version to properly
808 // be able to strip CRV qualifiers from the type.
809 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
810 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000811 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000812 T1.getUnqualifiedType()),
813 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000814 }
Eli Friedman04309752009-11-24 05:28:59 +0000815
816 bool CheckReferencedDecl(const Expr *E, const Decl *D);
817 bool VisitDeclRefExpr(const DeclRefExpr *E) {
818 return CheckReferencedDecl(E, E->getDecl());
819 }
820 bool VisitMemberExpr(const MemberExpr *E) {
821 if (CheckReferencedDecl(E, E->getMemberDecl())) {
822 // Conservatively assume a MemberExpr will have side-effects
823 Info.EvalResult.HasSideEffects = true;
824 return true;
825 }
826 return false;
827 }
828
Eli Friedmanc4a26382010-02-13 00:10:10 +0000829 bool VisitCallExpr(CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000830 bool VisitBinaryOperator(const BinaryOperator *E);
831 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000832 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000833
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000834 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000835 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
836
Anders Carlsson3068d112008-11-16 19:01:22 +0000837 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000838 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Anders Carlsson3f704562008-12-21 22:39:40 +0000841 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000842 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Anders Carlsson3068d112008-11-16 19:01:22 +0000845 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000846 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000847 }
848
Eli Friedman664a1042009-02-27 04:45:43 +0000849 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
850 return Success(0, E);
851 }
852
Sebastian Redl64b45f72009-01-05 20:52:13 +0000853 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000854 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000855 }
856
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000857 bool VisitChooseExpr(const ChooseExpr *E) {
858 return Visit(E->getChosenSubExpr(Info.Ctx));
859 }
860
Eli Friedman722c7172009-02-28 03:59:05 +0000861 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000862 bool VisitUnaryImag(const UnaryOperator *E);
863
Chris Lattnerfcee0012008-07-11 21:24:13 +0000864private:
Ken Dyck8b752f12010-01-27 17:10:57 +0000865 CharUnits GetAlignOfExpr(const Expr *E);
866 CharUnits GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000867 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000868};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000869} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000870
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000871static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000872 if (!E->getType()->isIntegralType())
873 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000875 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
876}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000877
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000878static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
879 APValue Val;
880 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
881 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000882 Result = Val.getInt();
883 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000884}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000885
Eli Friedman04309752009-11-24 05:28:59 +0000886bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000887 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000888 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
889 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000890
891 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000892 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000893 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
894 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +0000895
896 if (isa<ParmVarDecl>(D))
897 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
898
Eli Friedman04309752009-11-24 05:28:59 +0000899 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000900 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000901 if (APValue *V = VD->getEvaluatedValue()) {
902 if (V->isInt())
903 return Success(V->getInt(), E);
904 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
905 }
906
907 if (VD->isEvaluatingValue())
908 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
909
910 VD->setEvaluatingValue();
911
Douglas Gregor78d15832009-05-26 18:54:04 +0000912 if (Visit(const_cast<Expr*>(Init))) {
913 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000914 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000915 return true;
916 }
917
Eli Friedmanc0131182009-12-03 20:31:57 +0000918 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000919 return false;
920 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000921 }
922 }
923
Chris Lattner4c4867e2008-07-12 00:38:25 +0000924 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000925 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000926}
927
Chris Lattnera4d55d82008-10-06 06:40:35 +0000928/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
929/// as GCC.
930static int EvaluateBuiltinClassifyType(const CallExpr *E) {
931 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000932 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000933 enum gcc_type_class {
934 no_type_class = -1,
935 void_type_class, integer_type_class, char_type_class,
936 enumeral_type_class, boolean_type_class,
937 pointer_type_class, reference_type_class, offset_type_class,
938 real_type_class, complex_type_class,
939 function_type_class, method_type_class,
940 record_type_class, union_type_class,
941 array_type_class, string_type_class,
942 lang_type_class
943 };
Mike Stump1eb44332009-09-09 15:08:12 +0000944
945 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000946 // ideal, however it is what gcc does.
947 if (E->getNumArgs() == 0)
948 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Chris Lattnera4d55d82008-10-06 06:40:35 +0000950 QualType ArgTy = E->getArg(0)->getType();
951 if (ArgTy->isVoidType())
952 return void_type_class;
953 else if (ArgTy->isEnumeralType())
954 return enumeral_type_class;
955 else if (ArgTy->isBooleanType())
956 return boolean_type_class;
957 else if (ArgTy->isCharType())
958 return string_type_class; // gcc doesn't appear to use char_type_class
959 else if (ArgTy->isIntegerType())
960 return integer_type_class;
961 else if (ArgTy->isPointerType())
962 return pointer_type_class;
963 else if (ArgTy->isReferenceType())
964 return reference_type_class;
965 else if (ArgTy->isRealType())
966 return real_type_class;
967 else if (ArgTy->isComplexType())
968 return complex_type_class;
969 else if (ArgTy->isFunctionType())
970 return function_type_class;
971 else if (ArgTy->isStructureType())
972 return record_type_class;
973 else if (ArgTy->isUnionType())
974 return union_type_class;
975 else if (ArgTy->isArrayType())
976 return array_type_class;
977 else if (ArgTy->isUnionType())
978 return union_type_class;
979 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
980 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
981 return -1;
982}
983
Eli Friedmanc4a26382010-02-13 00:10:10 +0000984bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000985 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000986 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000987 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000988
989 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000990 const Expr *Arg = E->getArg(0)->IgnoreParens();
991 Expr::EvalResult Base;
Eric Christopherb2aaf512010-01-19 22:58:35 +0000992
993 // TODO: Perhaps we should let LLVM lower this?
Mike Stump660e6f72009-10-26 23:05:19 +0000994 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000995 && Base.Val.getKind() == APValue::LValue
996 && !Base.HasSideEffects)
997 if (const Expr *LVBase = Base.Val.getLValueBase())
998 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
999 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +00001000 if (!VD->getType()->isIncompleteType()
1001 && VD->getType()->isObjectType()
1002 && !VD->getType()->isVariablyModifiedType()
1003 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +00001004 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +00001005 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +00001006 if (!Offset.isNegative() && Offset <= Size)
1007 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +00001008 else
Ken Dyck199c3d62010-01-11 17:06:35 +00001009 Size = CharUnits::Zero();
1010 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +00001011 }
Mike Stump64eda9e2009-10-26 18:35:08 +00001012 }
1013 }
1014
Eric Christopherb2aaf512010-01-19 22:58:35 +00001015 // If evaluating the argument has side-effects we can't determine
1016 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001017 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001018 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001019 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001020 return Success(0, E);
1021 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001022
Mike Stump64eda9e2009-10-26 18:35:08 +00001023 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1024 }
1025
Chris Lattner019f4e82008-10-06 05:28:25 +00001026 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001027 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001029 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001030 // __builtin_constant_p always has one operand: it returns true if that
1031 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001032 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001033
1034 case Builtin::BI__builtin_eh_return_data_regno: {
1035 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1036 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1037 return Success(Operand, E);
1038 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001039
1040 case Builtin::BI__builtin_expect:
1041 return Visit(E->getArg(0));
Chris Lattner019f4e82008-10-06 05:28:25 +00001042 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001043}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001044
Chris Lattnerb542afe2008-07-11 19:10:17 +00001045bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001046 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001047 if (!Visit(E->getRHS()))
1048 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001049
Eli Friedman33ef1452009-02-26 10:19:36 +00001050 // If we can't evaluate the LHS, it might have side effects;
1051 // conservatively mark it.
1052 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1053 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001054
Anders Carlsson027f62e2008-12-01 02:07:06 +00001055 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001056 }
1057
1058 if (E->isLogicalOp()) {
1059 // These need to be handled specially because the operands aren't
1060 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001061 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001063 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001064 // We were able to evaluate the LHS, see if we can get away with not
1065 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001066 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001067 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001068
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001069 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001070 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001071 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001072 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001073 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001074 }
1075 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001076 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001077 // We can't evaluate the LHS; however, sometimes the result
1078 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001079 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001080 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001081 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001082 // must have had side effects.
1083 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001084
1085 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001086 }
1087 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001088 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001089
Eli Friedmana6afa762008-11-13 06:09:17 +00001090 return false;
1091 }
1092
Anders Carlsson286f85e2008-11-16 07:17:21 +00001093 QualType LHSTy = E->getLHS()->getType();
1094 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001095
1096 if (LHSTy->isAnyComplexType()) {
1097 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1098 APValue LHS, RHS;
1099
1100 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1101 return false;
1102
1103 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1104 return false;
1105
1106 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001107 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001108 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001109 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001110 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1111
Daniel Dunbar4087e242009-01-29 06:43:41 +00001112 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001113 return Success((CR_r == APFloat::cmpEqual &&
1114 CR_i == APFloat::cmpEqual), E);
1115 else {
1116 assert(E->getOpcode() == BinaryOperator::NE &&
1117 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001118 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001119 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001120 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001121 CR_i == APFloat::cmpLessThan)), E);
1122 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001123 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001124 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001125 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1126 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1127 else {
1128 assert(E->getOpcode() == BinaryOperator::NE &&
1129 "Invalid compex comparison.");
1130 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1131 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1132 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001133 }
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Anders Carlsson286f85e2008-11-16 07:17:21 +00001136 if (LHSTy->isRealFloatingType() &&
1137 RHSTy->isRealFloatingType()) {
1138 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Anders Carlsson286f85e2008-11-16 07:17:21 +00001140 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1141 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Anders Carlsson286f85e2008-11-16 07:17:21 +00001143 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1144 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Anders Carlsson286f85e2008-11-16 07:17:21 +00001146 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001147
Anders Carlsson286f85e2008-11-16 07:17:21 +00001148 switch (E->getOpcode()) {
1149 default:
1150 assert(0 && "Invalid binary operator!");
1151 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001152 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001153 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001154 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001155 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001156 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001157 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001158 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001159 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001160 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001161 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001162 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001163 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001164 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001165 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001168 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1169 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001170 APValue LHSValue;
1171 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1172 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001173
Anders Carlsson3068d112008-11-16 19:01:22 +00001174 APValue RHSValue;
1175 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1176 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001177
Eli Friedman5bc86102009-06-14 02:17:33 +00001178 // Reject any bases from the normal codepath; we special-case comparisons
1179 // to null.
1180 if (LHSValue.getLValueBase()) {
1181 if (!E->isEqualityOp())
1182 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001183 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001184 return false;
1185 bool bres;
1186 if (!EvalPointerValueAsBool(LHSValue, bres))
1187 return false;
1188 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1189 } else if (RHSValue.getLValueBase()) {
1190 if (!E->isEqualityOp())
1191 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001192 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001193 return false;
1194 bool bres;
1195 if (!EvalPointerValueAsBool(RHSValue, bres))
1196 return false;
1197 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1198 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001199
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001200 if (E->getOpcode() == BinaryOperator::Sub) {
1201 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001202 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001203
Ken Dycka7305832010-01-15 12:37:54 +00001204 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001205 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001206 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001207
Ken Dycka7305832010-01-15 12:37:54 +00001208 CharUnits Diff = LHSValue.getLValueOffset() -
1209 RHSValue.getLValueOffset();
1210 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001211 }
1212 bool Result;
1213 if (E->getOpcode() == BinaryOperator::EQ) {
1214 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001215 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001216 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1217 }
1218 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001219 }
1220 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001221 if (!LHSTy->isIntegralType() ||
1222 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001223 // We can't continue from here for non-integral types, and they
1224 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001225 return false;
1226 }
1227
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001228 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001229 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001230 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001231
Eli Friedman42edd0d2009-03-24 01:14:50 +00001232 APValue RHSVal;
1233 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001234 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001235
1236 // Handle cases like (unsigned long)&a + 4.
1237 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001238 CharUnits Offset = Result.getLValueOffset();
1239 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1240 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001241 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001242 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001243 else
Ken Dycka7305832010-01-15 12:37:54 +00001244 Offset -= AdditionalOffset;
1245 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001246 return true;
1247 }
1248
1249 // Handle cases like 4 + (unsigned long)&a
1250 if (E->getOpcode() == BinaryOperator::Add &&
1251 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001252 CharUnits Offset = RHSVal.getLValueOffset();
1253 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1254 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001255 return true;
1256 }
1257
1258 // All the following cases expect both operands to be an integer
1259 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001260 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001261
Eli Friedman42edd0d2009-03-24 01:14:50 +00001262 APSInt& RHS = RHSVal.getInt();
1263
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001264 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001265 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001266 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001267 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1268 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1269 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1270 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1271 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1272 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001273 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001274 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001275 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001276 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001277 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001278 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001279 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001280 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001281 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001282 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001283 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001284 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1285 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001286 }
1287 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001288 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001289 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1290 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001293 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1294 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1295 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1296 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1297 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1298 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001299 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001300}
1301
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001302bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001303 bool Cond;
1304 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001305 return false;
1306
Nuno Lopesa25bd552008-11-16 22:06:39 +00001307 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001308}
1309
Ken Dyck8b752f12010-01-27 17:10:57 +00001310CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001311 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1312 // the result is the size of the referenced type."
1313 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1314 // result shall be the alignment of the referenced type."
1315 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1316 T = Ref->getPointeeType();
1317
Chris Lattnere9feb472009-01-24 21:09:06 +00001318 // Get information about the alignment.
1319 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001320
Eli Friedman2be58612009-05-30 21:09:44 +00001321 // __alignof is defined to return the preferred alignment.
Ken Dyck8b752f12010-01-27 17:10:57 +00001322 return CharUnits::fromQuantity(
1323 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
Chris Lattnere9feb472009-01-24 21:09:06 +00001324}
1325
Ken Dyck8b752f12010-01-27 17:10:57 +00001326CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001327 E = E->IgnoreParens();
1328
1329 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001330 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001331 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001332 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1333 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001334
Chris Lattneraf707ab2009-01-24 21:53:27 +00001335 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001336 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1337 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001338
Chris Lattnere9feb472009-01-24 21:09:06 +00001339 return GetAlignOfType(E->getType());
1340}
1341
1342
Sebastian Redl05189992008-11-11 17:56:53 +00001343/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1344/// expression's type.
1345bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001346 // Handle alignof separately.
1347 if (!E->isSizeOf()) {
1348 if (E->isArgumentType())
Ken Dyck8b752f12010-01-27 17:10:57 +00001349 return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001350 else
Ken Dyck8b752f12010-01-27 17:10:57 +00001351 return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001352 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001353
Sebastian Redl05189992008-11-11 17:56:53 +00001354 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001355 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1356 // the result is the size of the referenced type."
1357 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1358 // result shall be the alignment of the referenced type."
1359 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1360 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001361
Daniel Dunbar131eb432009-02-19 09:06:44 +00001362 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1363 // extension.
1364 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1365 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001366
Chris Lattnerfcee0012008-07-11 21:24:13 +00001367 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001368 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001369 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001370
Chris Lattnere9feb472009-01-24 21:09:06 +00001371 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001372 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001373}
1374
Chris Lattnerb542afe2008-07-11 19:10:17 +00001375bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001376 // Special case unary operators that do not need their subexpression
1377 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001378 if (E->isOffsetOfOp()) {
1379 // The AST for offsetof is defined in such a way that we can just
1380 // directly Evaluate it as an l-value.
1381 APValue LV;
1382 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1383 return false;
1384 if (LV.getLValueBase())
1385 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001386 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001387 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001388
1389 if (E->getOpcode() == UnaryOperator::LNot) {
1390 // LNot's operand isn't necessarily an integer, so we handle it specially.
1391 bool bres;
1392 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1393 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001394 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001395 }
1396
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001397 // Only handle integral operations...
1398 if (!E->getSubExpr()->getType()->isIntegralType())
1399 return false;
1400
Chris Lattner87eae5e2008-07-11 22:52:41 +00001401 // Get the operand value into 'Result'.
1402 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001403 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001404
Chris Lattner75a48812008-07-11 22:15:16 +00001405 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001406 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001407 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1408 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001409 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001410 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001411 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1412 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001413 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001414 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001415 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001416 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001417 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001418 if (!Result.isInt()) return false;
1419 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001420 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001421 if (!Result.isInt()) return false;
1422 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001423 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001424}
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattner732b2232008-07-12 01:15:53 +00001426/// HandleCast - This is used to evaluate implicit or explicit casts where the
1427/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001428bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001429 Expr *SubExpr = E->getSubExpr();
1430 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001431 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001432
Eli Friedman4efaa272008-11-12 09:44:48 +00001433 if (DestType->isBooleanType()) {
1434 bool BoolResult;
1435 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1436 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001437 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001438 }
1439
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001440 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001441 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001442 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001443 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001444
Eli Friedmanbe265702009-02-20 01:15:07 +00001445 if (!Result.isInt()) {
1446 // Only allow casts of lvalues if they are lossless.
1447 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1448 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001449
Daniel Dunbardd211642009-02-19 22:24:01 +00001450 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001451 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001452 }
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Chris Lattner732b2232008-07-12 01:15:53 +00001454 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001455 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001456 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001457 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001458 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001459
Daniel Dunbardd211642009-02-19 22:24:01 +00001460 if (LV.getLValueBase()) {
1461 // Only allow based lvalue casts if they are lossless.
1462 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1463 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001464
Daniel Dunbardd211642009-02-19 22:24:01 +00001465 Result = LV;
1466 return true;
1467 }
1468
Ken Dycka7305832010-01-15 12:37:54 +00001469 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1470 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001471 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001472 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001473
Eli Friedmanbe265702009-02-20 01:15:07 +00001474 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1475 // This handles double-conversion cases, where there's both
1476 // an l-value promotion and an implicit conversion to int.
1477 APValue LV;
1478 if (!EvaluateLValue(SubExpr, LV, Info))
1479 return false;
1480
1481 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1482 return false;
1483
1484 Result = LV;
1485 return true;
1486 }
1487
Eli Friedman1725f682009-04-22 19:23:09 +00001488 if (SrcType->isAnyComplexType()) {
1489 APValue C;
1490 if (!EvaluateComplex(SubExpr, C, Info))
1491 return false;
1492 if (C.isComplexFloat())
1493 return Success(HandleFloatToIntCast(DestType, SrcType,
1494 C.getComplexFloatReal(), Info.Ctx),
1495 E);
1496 else
1497 return Success(HandleIntToIntCast(DestType, SrcType,
1498 C.getComplexIntReal(), Info.Ctx), E);
1499 }
Eli Friedman2217c872009-02-22 11:46:18 +00001500 // FIXME: Handle vectors
1501
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001502 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001503 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001504
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001505 APFloat F(0.0);
1506 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001507 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001509 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001510}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001511
Eli Friedman722c7172009-02-28 03:59:05 +00001512bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1513 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1514 APValue LV;
1515 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1516 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1517 return Success(LV.getComplexIntReal(), E);
1518 }
1519
1520 return Visit(E->getSubExpr());
1521}
1522
Eli Friedman664a1042009-02-27 04:45:43 +00001523bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001524 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1525 APValue LV;
1526 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1527 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1528 return Success(LV.getComplexIntImag(), E);
1529 }
1530
Eli Friedman664a1042009-02-27 04:45:43 +00001531 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1532 Info.EvalResult.HasSideEffects = true;
1533 return Success(0, E);
1534}
1535
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001536//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001537// Float Evaluation
1538//===----------------------------------------------------------------------===//
1539
1540namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001541class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001542 : public StmtVisitor<FloatExprEvaluator, bool> {
1543 EvalInfo &Info;
1544 APFloat &Result;
1545public:
1546 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1547 : Info(info), Result(result) {}
1548
1549 bool VisitStmt(Stmt *S) {
1550 return false;
1551 }
1552
1553 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001554 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001555
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001556 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001557 bool VisitBinaryOperator(const BinaryOperator *E);
1558 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001559 bool VisitCastExpr(CastExpr *E);
1560 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001561 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001562
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001563 bool VisitChooseExpr(const ChooseExpr *E)
1564 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1565 bool VisitUnaryExtension(const UnaryOperator *E)
1566 { return Visit(E->getSubExpr()); }
1567
1568 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001569 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001570};
1571} // end anonymous namespace
1572
1573static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1574 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1575}
1576
John McCalldb7b72a2010-02-28 13:00:19 +00001577static bool TryEvaluateBuiltinNaN(ASTContext &Context,
1578 QualType ResultTy,
1579 const Expr *Arg,
1580 bool SNaN,
1581 llvm::APFloat &Result) {
1582 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1583 if (!S) return false;
1584
1585 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1586
1587 llvm::APInt fill;
1588
1589 // Treat empty strings as if they were zero.
1590 if (S->getString().empty())
1591 fill = llvm::APInt(32, 0);
1592 else if (S->getString().getAsInteger(0, fill))
1593 return false;
1594
1595 if (SNaN)
1596 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1597 else
1598 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1599 return true;
1600}
1601
Chris Lattner019f4e82008-10-06 05:28:25 +00001602bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001603 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001604 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001605 case Builtin::BI__builtin_huge_val:
1606 case Builtin::BI__builtin_huge_valf:
1607 case Builtin::BI__builtin_huge_vall:
1608 case Builtin::BI__builtin_inf:
1609 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001610 case Builtin::BI__builtin_infl: {
1611 const llvm::fltSemantics &Sem =
1612 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001613 Result = llvm::APFloat::getInf(Sem);
1614 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
John McCalldb7b72a2010-02-28 13:00:19 +00001617 case Builtin::BI__builtin_nans:
1618 case Builtin::BI__builtin_nansf:
1619 case Builtin::BI__builtin_nansl:
1620 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1621 true, Result);
1622
Chris Lattner9e621712008-10-06 06:31:58 +00001623 case Builtin::BI__builtin_nan:
1624 case Builtin::BI__builtin_nanf:
1625 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001626 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001627 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001628 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1629 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001630
1631 case Builtin::BI__builtin_fabs:
1632 case Builtin::BI__builtin_fabsf:
1633 case Builtin::BI__builtin_fabsl:
1634 if (!EvaluateFloat(E->getArg(0), Result, Info))
1635 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001637 if (Result.isNegative())
1638 Result.changeSign();
1639 return true;
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641 case Builtin::BI__builtin_copysign:
1642 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001643 case Builtin::BI__builtin_copysignl: {
1644 APFloat RHS(0.);
1645 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1646 !EvaluateFloat(E->getArg(1), RHS, Info))
1647 return false;
1648 Result.copySign(RHS);
1649 return true;
1650 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001651 }
1652}
1653
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001654bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001655 if (E->getOpcode() == UnaryOperator::Deref)
1656 return false;
1657
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001658 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1659 return false;
1660
1661 switch (E->getOpcode()) {
1662 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001663 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001664 return true;
1665 case UnaryOperator::Minus:
1666 Result.changeSign();
1667 return true;
1668 }
1669}
Chris Lattner019f4e82008-10-06 05:28:25 +00001670
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001671bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001672 if (E->getOpcode() == BinaryOperator::Comma) {
1673 if (!EvaluateFloat(E->getRHS(), Result, Info))
1674 return false;
1675
1676 // If we can't evaluate the LHS, it might have side effects;
1677 // conservatively mark it.
1678 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1679 Info.EvalResult.HasSideEffects = true;
1680
1681 return true;
1682 }
1683
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001684 // FIXME: Diagnostics? I really don't understand how the warnings
1685 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001686 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001687 if (!EvaluateFloat(E->getLHS(), Result, Info))
1688 return false;
1689 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1690 return false;
1691
1692 switch (E->getOpcode()) {
1693 default: return false;
1694 case BinaryOperator::Mul:
1695 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1696 return true;
1697 case BinaryOperator::Add:
1698 Result.add(RHS, APFloat::rmNearestTiesToEven);
1699 return true;
1700 case BinaryOperator::Sub:
1701 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1702 return true;
1703 case BinaryOperator::Div:
1704 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1705 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001706 }
1707}
1708
1709bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1710 Result = E->getValue();
1711 return true;
1712}
1713
Eli Friedman4efaa272008-11-12 09:44:48 +00001714bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1715 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Eli Friedman4efaa272008-11-12 09:44:48 +00001717 if (SubExpr->getType()->isIntegralType()) {
1718 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001719 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001720 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001721 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001722 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001723 return true;
1724 }
1725 if (SubExpr->getType()->isRealFloatingType()) {
1726 if (!Visit(SubExpr))
1727 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001728 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1729 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001730 return true;
1731 }
Eli Friedman2217c872009-02-22 11:46:18 +00001732 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001733
1734 return false;
1735}
1736
1737bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1738 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1739 return true;
1740}
1741
Eli Friedman67f85fc2009-12-04 02:12:53 +00001742bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1743 bool Cond;
1744 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1745 return false;
1746
1747 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1748}
1749
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001750//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001751// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001752//===----------------------------------------------------------------------===//
1753
1754namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001755class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001756 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001757 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001759public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001760 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001762 //===--------------------------------------------------------------------===//
1763 // Visitor Methods
1764 //===--------------------------------------------------------------------===//
1765
1766 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001767 return APValue();
1768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001770 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1771
1772 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001773 Expr* SubExpr = E->getSubExpr();
1774
1775 if (SubExpr->getType()->isRealFloatingType()) {
1776 APFloat Result(0.0);
1777
1778 if (!EvaluateFloat(SubExpr, Result, Info))
1779 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001780
1781 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001782 Result);
1783 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001784 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001785 "Unexpected imaginary literal.");
1786
1787 llvm::APSInt Result;
1788 if (!EvaluateInteger(SubExpr, Result, Info))
1789 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001791 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1792 Zero = 0;
1793 return APValue(Zero, Result);
1794 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001795 }
1796
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001797 APValue VisitCastExpr(CastExpr *E) {
1798 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001799 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001800 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001801
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001802 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001803 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001804
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001805 if (!EvaluateFloat(SubExpr, Result, Info))
1806 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001807
1808 if (EltType->isRealFloatingType()) {
1809 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001810 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001811 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1812 } else {
1813 llvm::APSInt IResult;
1814 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1815 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1816 Zero = 0;
1817 return APValue(IResult, Zero);
1818 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001819 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001820 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001821
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001822 if (!EvaluateInteger(SubExpr, Result, Info))
1823 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001824
Eli Friedman1725f682009-04-22 19:23:09 +00001825 if (EltType->isRealFloatingType()) {
1826 APFloat FResult =
1827 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001828 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001829 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1830 } else {
1831 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1832 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1833 Zero = 0;
1834 return APValue(Result, Zero);
1835 }
John McCall183700f2009-09-21 23:43:11 +00001836 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001837 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001838
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001839 if (!EvaluateComplex(SubExpr, Src, Info))
1840 return APValue();
1841
1842 QualType SrcType = CT->getElementType();
1843
1844 if (Src.isComplexFloat()) {
1845 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001846 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001847 Src.getComplexFloatReal(),
1848 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001849 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001850 Src.getComplexFloatImag(),
1851 Info.Ctx));
1852 } else {
1853 return APValue(HandleFloatToIntCast(EltType, SrcType,
1854 Src.getComplexFloatReal(),
1855 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001856 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001857 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001858 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001859 }
1860 } else {
1861 assert(Src.isComplexInt() && "Invalid evaluate result.");
1862 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001863 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001864 Src.getComplexIntReal(),
1865 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001866 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001867 Src.getComplexIntImag(),
1868 Info.Ctx));
1869 } else {
1870 return APValue(HandleIntToIntCast(EltType, SrcType,
1871 Src.getComplexIntReal(),
1872 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001873 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001874 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001875 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001876 }
1877 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001878 }
1879
1880 // FIXME: Handle more casts.
1881 return APValue();
1882 }
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001884 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001885 APValue VisitChooseExpr(const ChooseExpr *E)
1886 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1887 APValue VisitUnaryExtension(const UnaryOperator *E)
1888 { return Visit(E->getSubExpr()); }
1889 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001890 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001891};
1892} // end anonymous namespace
1893
Mike Stump1eb44332009-09-09 15:08:12 +00001894static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001895 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1896 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001897 (&Result.getComplexFloatReal().getSemantics() ==
1898 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001899 "Invalid complex evaluation.");
1900 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001901}
1902
Mike Stump1eb44332009-09-09 15:08:12 +00001903APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001904 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001906 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001907 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001909 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001910 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001911
Daniel Dunbar3f279872009-01-29 01:32:56 +00001912 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1913 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001914 switch (E->getOpcode()) {
1915 default: return APValue();
1916 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001917 if (Result.isComplexFloat()) {
1918 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1919 APFloat::rmNearestTiesToEven);
1920 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1921 APFloat::rmNearestTiesToEven);
1922 } else {
1923 Result.getComplexIntReal() += RHS.getComplexIntReal();
1924 Result.getComplexIntImag() += RHS.getComplexIntImag();
1925 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001926 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001927 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001928 if (Result.isComplexFloat()) {
1929 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1930 APFloat::rmNearestTiesToEven);
1931 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1932 APFloat::rmNearestTiesToEven);
1933 } else {
1934 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1935 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1936 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001937 break;
1938 case BinaryOperator::Mul:
1939 if (Result.isComplexFloat()) {
1940 APValue LHS = Result;
1941 APFloat &LHS_r = LHS.getComplexFloatReal();
1942 APFloat &LHS_i = LHS.getComplexFloatImag();
1943 APFloat &RHS_r = RHS.getComplexFloatReal();
1944 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Daniel Dunbar3f279872009-01-29 01:32:56 +00001946 APFloat Tmp = LHS_r;
1947 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1948 Result.getComplexFloatReal() = Tmp;
1949 Tmp = LHS_i;
1950 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1951 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1952
1953 Tmp = LHS_r;
1954 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1955 Result.getComplexFloatImag() = Tmp;
1956 Tmp = LHS_i;
1957 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1958 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1959 } else {
1960 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001961 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001962 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1963 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001964 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001965 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1966 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1967 }
1968 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001969 }
1970
1971 return Result;
1972}
1973
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001974//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001975// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001976//===----------------------------------------------------------------------===//
1977
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001978/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001979/// any crazy technique (that has nothing to do with language standards) that
1980/// we want to. If this function returns true, it returns the folded constant
1981/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001982bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1983 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001984
Nate Begeman59b5da62009-01-18 03:20:47 +00001985 if (getType()->isVectorType()) {
1986 if (!EvaluateVector(this, Result.Val, Info))
1987 return false;
1988 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001989 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001990 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001991 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001992 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001993 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001994 } else if (getType()->isRealFloatingType()) {
1995 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001996 if (!EvaluateFloat(this, f, Info))
1997 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001999 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002000 } else if (getType()->isAnyComplexType()) {
2001 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002002 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002003 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002004 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002005
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002006 return true;
2007}
2008
Mike Stump660e6f72009-10-26 23:05:19 +00002009bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
2010 EvalInfo Info(Ctx, Result, true);
2011
2012 if (getType()->isVectorType()) {
2013 if (!EvaluateVector(this, Result.Val, Info))
2014 return false;
2015 } else if (getType()->isIntegerType()) {
2016 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
2017 return false;
2018 } else if (getType()->hasPointerRepresentation()) {
2019 if (!EvaluatePointer(this, Result.Val, Info))
2020 return false;
2021 } else if (getType()->isRealFloatingType()) {
2022 llvm::APFloat f(0.0);
2023 if (!EvaluateFloat(this, f, Info))
2024 return false;
2025
2026 Result.Val = APValue(f);
2027 } else if (getType()->isAnyComplexType()) {
2028 if (!EvaluateComplex(this, Result.Val, Info))
2029 return false;
2030 } else
2031 return false;
2032
2033 return true;
2034}
2035
John McCallcd7a4452010-01-05 23:42:56 +00002036bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
2037 EvalResult Scratch;
2038 EvalInfo Info(Ctx, Scratch);
2039
2040 return HandleConversionToBool(this, Result, Info);
2041}
2042
Anders Carlsson1b782762009-04-10 04:54:13 +00002043bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
2044 EvalInfo Info(Ctx, Result);
2045
2046 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2047}
2048
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002049bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2050 EvalInfo Info(Ctx, Result, true);
2051
2052 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2053}
2054
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002055/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002056/// folded, but discard the result.
2057bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002058 EvalResult Result;
2059 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002060}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002061
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002062bool Expr::HasSideEffects(ASTContext &Ctx) const {
2063 Expr::EvalResult Result;
2064 EvalInfo Info(Ctx, Result);
2065 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2066}
2067
Anders Carlsson51fe9962008-11-22 21:04:56 +00002068APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002069 EvalResult EvalResult;
2070 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002071 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002072 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002073 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002074
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002075 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002076}