blob: dfff2099cbd6d183fa7cfae8f551bba6e1005882 [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()); }
206};
207
Mike Stumpc4c90452009-10-27 22:09:17 +0000208} // end anonymous namespace
209
Eli Friedman4efaa272008-11-12 09:44:48 +0000210//===----------------------------------------------------------------------===//
211// LValue Evaluation
212//===----------------------------------------------------------------------===//
213namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000214class LValueExprEvaluator
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 : public StmtVisitor<LValueExprEvaluator, APValue> {
216 EvalInfo &Info;
217public:
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Eli Friedman4efaa272008-11-12 09:44:48 +0000219 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
220
221 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000222 return APValue();
223 }
224
225 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000226 APValue VisitDeclRefExpr(DeclRefExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
Ken Dycka7305832010-01-15 12:37:54 +0000230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); }
231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000232 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000233 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000234 APValue VisitUnaryExtension(const UnaryOperator *E)
235 { return Visit(E->getSubExpr()); }
236 APValue VisitChooseExpr(const ChooseExpr *E)
237 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000238
239 APValue VisitCastExpr(CastExpr *E) {
240 switch (E->getCastKind()) {
241 default:
242 return APValue();
243
244 case CastExpr::CK_NoOp:
245 return Visit(E->getSubExpr());
246 }
247 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000248 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000249};
250} // end anonymous namespace
251
252static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
253 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
254 return Result.isLValue();
255}
256
Mike Stump1eb44332009-09-09 15:08:12 +0000257APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000258 if (isa<FunctionDecl>(E->getDecl())) {
Ken Dycka7305832010-01-15 12:37:54 +0000259 return APValue(E);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000260 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000261 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000262 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000263 if (!VD->getType()->isReferenceType())
Ken Dycka7305832010-01-15 12:37:54 +0000264 return APValue(E);
Eli Friedmand933a012009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000266 const VarDecl *Def = 0;
267 if (const Expr *Init = VD->getDefinition(Def))
268 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000269 }
270
271 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000272}
273
Eli Friedman4efaa272008-11-12 09:44:48 +0000274APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000275 if (!Info.AnyLValue && !E->isFileScope())
276 return APValue();
Ken Dycka7305832010-01-15 12:37:54 +0000277 return APValue(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000278}
279
280APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
281 APValue result;
282 QualType Ty;
283 if (E->isArrow()) {
284 if (!EvaluatePointer(E->getBase(), result, Info))
285 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000286 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000287 } else {
288 result = Visit(E->getBase());
289 if (result.isUninit())
290 return APValue();
291 Ty = E->getBase()->getType();
292 }
293
Ted Kremenek6217b802009-07-29 21:53:49 +0000294 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000295 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000296
297 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
298 if (!FD) // FIXME: deal with other kinds of member expressions
299 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000300
301 if (FD->getType()->isReferenceType())
302 return APValue();
303
Eli Friedman4efaa272008-11-12 09:44:48 +0000304 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000305 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000306 for (RecordDecl::field_iterator Field = RD->field_begin(),
307 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000308 Field != FieldEnd; (void)++Field, ++i) {
309 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000310 break;
311 }
312
313 result.setLValue(result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000314 result.getLValueOffset() +
315 CharUnits::fromQuantity(RL.getFieldOffset(i) / 8));
Eli Friedman4efaa272008-11-12 09:44:48 +0000316
317 return result;
318}
319
Mike Stump1eb44332009-09-09 15:08:12 +0000320APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000321 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Anders Carlsson3068d112008-11-16 19:01:22 +0000323 if (!EvaluatePointer(E->getBase(), Result, Info))
324 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlsson3068d112008-11-16 19:01:22 +0000326 APSInt Index;
327 if (!EvaluateInteger(E->getIdx(), Index, Info))
328 return APValue();
329
Ken Dyck199c3d62010-01-11 17:06:35 +0000330 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
Anders Carlsson3068d112008-11-16 19:01:22 +0000331
Ken Dyck199c3d62010-01-11 17:06:35 +0000332 CharUnits Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000333 Result.setLValue(Result.getLValueBase(),
Ken Dycka7305832010-01-15 12:37:54 +0000334 Result.getLValueOffset() + Offset);
Anders Carlsson3068d112008-11-16 19:01:22 +0000335 return Result;
336}
Eli Friedman4efaa272008-11-12 09:44:48 +0000337
Mike Stump1eb44332009-09-09 15:08:12 +0000338APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000339 APValue Result;
340 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
341 return APValue();
342 return Result;
343}
344
Eli Friedman4efaa272008-11-12 09:44:48 +0000345//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000346// Pointer Evaluation
347//===----------------------------------------------------------------------===//
348
Anders Carlssonc754aa62008-07-08 05:13:58 +0000349namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000350class PointerExprEvaluator
Anders Carlsson2bad1682008-07-08 14:30:00 +0000351 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000352 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000353public:
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner87eae5e2008-07-11 22:52:41 +0000355 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000356
Anders Carlsson2bad1682008-07-08 14:30:00 +0000357 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000358 return APValue();
359 }
360
361 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
362
Anders Carlsson650c92f2008-07-08 15:34:11 +0000363 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000364 APValue VisitCastExpr(CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000365 APValue VisitUnaryExtension(const UnaryOperator *E)
366 { return Visit(E->getSubExpr()); }
367 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000368 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
Ken Dycka7305832010-01-15 12:37:54 +0000369 { return APValue(E); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000370 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000371 { return APValue(E); }
Eli Friedman3941b182009-01-25 01:54:01 +0000372 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000373 APValue VisitBlockExpr(BlockExpr *E) {
374 if (!E->hasBlockDeclRefExprs())
Ken Dycka7305832010-01-15 12:37:54 +0000375 return APValue(E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000376 return APValue();
377 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000378 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000379 { return APValue((Expr*)0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000380 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000381 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000382 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
383 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
Ken Dycka7305832010-01-15 12:37:54 +0000384 { return APValue((Expr*)0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000385 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000386};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000387} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000388
Chris Lattner87eae5e2008-07-11 22:52:41 +0000389static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000390 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000391 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000392 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393 return Result.isLValue();
394}
395
396APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
397 if (E->getOpcode() != BinaryOperator::Add &&
398 E->getOpcode() != BinaryOperator::Sub)
399 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000401 const Expr *PExp = E->getLHS();
402 const Expr *IExp = E->getRHS();
403 if (IExp->getType()->isPointerType())
404 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000406 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000407 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000408 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000410 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000411 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412 return APValue();
413
Ted Kremenek6217b802009-07-29 21:53:49 +0000414 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Ken Dyck199c3d62010-01-11 17:06:35 +0000415 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000417 // Explicitly handle GNU void* and function pointer arithmetic extensions.
418 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
Ken Dyck199c3d62010-01-11 17:06:35 +0000419 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000420 else
Ken Dyck199c3d62010-01-11 17:06:35 +0000421 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000422
Ken Dycka7305832010-01-15 12:37:54 +0000423 CharUnits Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000424
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000425 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000426 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000428 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
429
Ken Dycka7305832010-01-15 12:37:54 +0000430 return APValue(ResultLValue.getLValueBase(), Offset);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000431}
Eli Friedman4efaa272008-11-12 09:44:48 +0000432
Eli Friedman2217c872009-02-22 11:46:18 +0000433APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
434 APValue result;
435 if (EvaluateLValue(E->getSubExpr(), result, Info))
436 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000437 return APValue();
438}
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000440
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000441APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
442 Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000443
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000444 switch (E->getCastKind()) {
445 default:
446 break;
447
448 case CastExpr::CK_Unknown: {
449 // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
450
451 // Check for pointer->pointer cast
452 if (SubExpr->getType()->isPointerType() ||
453 SubExpr->getType()->isObjCObjectPointerType() ||
454 SubExpr->getType()->isNullPtrType() ||
455 SubExpr->getType()->isBlockPointerType())
456 return Visit(SubExpr);
457
458 if (SubExpr->getType()->isIntegralType()) {
459 APValue Result;
460 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
461 break;
462
463 if (Result.isInt()) {
464 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000465 return APValue(0,
466 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000467 }
468
469 // Cast is of an lvalue, no need to change value.
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000470 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000471 }
472 break;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000475 case CastExpr::CK_NoOp:
476 case CastExpr::CK_BitCast:
477 case CastExpr::CK_AnyPointerToObjCPointerCast:
478 case CastExpr::CK_AnyPointerToBlockPointerCast:
479 return Visit(SubExpr);
480
481 case CastExpr::CK_IntegralToPointer: {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000482 APValue Result;
483 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000484 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000485
486 if (Result.isInt()) {
487 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Ken Dycka7305832010-01-15 12:37:54 +0000488 return APValue(0,
489 CharUnits::fromQuantity(Result.getInt().getZExtValue()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000492 // Cast is of an lvalue, no need to change value.
493 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000494 }
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000495 case CastExpr::CK_ArrayToPointerDecay:
496 case CastExpr::CK_FunctionToPointerDecay: {
Eli Friedman4efaa272008-11-12 09:44:48 +0000497 APValue Result;
498 if (EvaluateLValue(SubExpr, Result, Info))
499 return Result;
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000500 break;
501 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000502 }
503
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000504 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000505}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000506
Eli Friedman3941b182009-01-25 01:54:01 +0000507APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000508 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000509 Builtin::BI__builtin___CFStringMakeConstantString)
Ken Dycka7305832010-01-15 12:37:54 +0000510 return APValue(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000511 return APValue();
512}
513
Eli Friedman4efaa272008-11-12 09:44:48 +0000514APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
515 bool BoolResult;
516 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
517 return APValue();
518
519 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
520
521 APValue Result;
522 if (EvaluatePointer(EvalExpr, Result, Info))
523 return Result;
524 return APValue();
525}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000526
527//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000528// Vector Evaluation
529//===----------------------------------------------------------------------===//
530
531namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000532 class VectorExprEvaluator
Nate Begeman59b5da62009-01-18 03:20:47 +0000533 : public StmtVisitor<VectorExprEvaluator, APValue> {
534 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000535 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000536 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Nate Begeman59b5da62009-01-18 03:20:47 +0000538 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Nate Begeman59b5da62009-01-18 03:20:47 +0000540 APValue VisitStmt(Stmt *S) {
541 return APValue();
542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Eli Friedman91110ee2009-02-23 04:23:56 +0000544 APValue VisitParenExpr(ParenExpr *E)
545 { return Visit(E->getSubExpr()); }
546 APValue VisitUnaryExtension(const UnaryOperator *E)
547 { return Visit(E->getSubExpr()); }
548 APValue VisitUnaryPlus(const UnaryOperator *E)
549 { return Visit(E->getSubExpr()); }
550 APValue VisitUnaryReal(const UnaryOperator *E)
551 { return Visit(E->getSubExpr()); }
552 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
553 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000554 APValue VisitCastExpr(const CastExpr* E);
555 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
556 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000557 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000558 APValue VisitChooseExpr(const ChooseExpr *E)
559 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000560 APValue VisitUnaryImag(const UnaryOperator *E);
561 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000562 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000563 // shufflevector, ExtVectorElementExpr
564 // (Note that these require implementing conversions
565 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000566 };
567} // end anonymous namespace
568
569static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
570 if (!E->getType()->isVectorType())
571 return false;
572 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
573 return !Result.isUninit();
574}
575
576APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000577 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000578 QualType EltTy = VTy->getElementType();
579 unsigned NElts = VTy->getNumElements();
580 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Nate Begeman59b5da62009-01-18 03:20:47 +0000582 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000583 QualType SETy = SE->getType();
584 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000585
Nate Begemane8c9e922009-06-26 18:22:18 +0000586 // Check for vector->vector bitcast and scalar->vector splat.
587 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000588 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000589 } else if (SETy->isIntegerType()) {
590 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000591 if (!EvaluateInteger(SE, IntResult, Info))
592 return APValue();
593 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000594 } else if (SETy->isRealFloatingType()) {
595 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000596 if (!EvaluateFloat(SE, F, Info))
597 return APValue();
598 Result = APValue(F);
599 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000600 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000601
Nate Begemanc0b8b192009-07-01 07:50:47 +0000602 // For casts of a scalar to ExtVector, convert the scalar to the element type
603 // and splat it to all elements.
604 if (E->getType()->isExtVectorType()) {
605 if (EltTy->isIntegerType() && Result.isInt())
606 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
607 Info.Ctx));
608 else if (EltTy->isIntegerType())
609 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
610 Info.Ctx));
611 else if (EltTy->isRealFloatingType() && Result.isInt())
612 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
613 Info.Ctx));
614 else if (EltTy->isRealFloatingType())
615 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
616 Info.Ctx));
617 else
618 return APValue();
619
620 // Splat and create vector APValue.
621 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
622 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000623 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000624
625 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
626 // to the vector. To construct the APValue vector initializer, bitcast the
627 // initializing value to an APInt, and shift out the bits pertaining to each
628 // element.
629 APSInt Init;
630 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Nate Begemanc0b8b192009-07-01 07:50:47 +0000632 llvm::SmallVector<APValue, 4> Elts;
633 for (unsigned i = 0; i != NElts; ++i) {
634 APSInt Tmp = Init;
635 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Nate Begemanc0b8b192009-07-01 07:50:47 +0000637 if (EltTy->isIntegerType())
638 Elts.push_back(APValue(Tmp));
639 else if (EltTy->isRealFloatingType())
640 Elts.push_back(APValue(APFloat(Tmp)));
641 else
642 return APValue();
643
644 Init >>= EltWidth;
645 }
646 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000647}
648
Mike Stump1eb44332009-09-09 15:08:12 +0000649APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000650VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
651 return this->Visit(const_cast<Expr*>(E->getInitializer()));
652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000655VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000656 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000657 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000658 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Nate Begeman59b5da62009-01-18 03:20:47 +0000660 QualType EltTy = VT->getElementType();
661 llvm::SmallVector<APValue, 4> Elements;
662
Eli Friedman91110ee2009-02-23 04:23:56 +0000663 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000664 if (EltTy->isIntegerType()) {
665 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000666 if (i < NumInits) {
667 if (!EvaluateInteger(E->getInit(i), sInt, Info))
668 return APValue();
669 } else {
670 sInt = Info.Ctx.MakeIntValue(0, EltTy);
671 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000672 Elements.push_back(APValue(sInt));
673 } else {
674 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000675 if (i < NumInits) {
676 if (!EvaluateFloat(E->getInit(i), f, Info))
677 return APValue();
678 } else {
679 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
680 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000681 Elements.push_back(APValue(f));
682 }
683 }
684 return APValue(&Elements[0], Elements.size());
685}
686
Mike Stump1eb44332009-09-09 15:08:12 +0000687APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000688VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000689 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000690 QualType EltTy = VT->getElementType();
691 APValue ZeroElement;
692 if (EltTy->isIntegerType())
693 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
694 else
695 ZeroElement =
696 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
697
698 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
699 return APValue(&Elements[0], Elements.size());
700}
701
702APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
703 bool BoolResult;
704 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
705 return APValue();
706
707 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
708
709 APValue Result;
710 if (EvaluateVector(EvalExpr, Result, Info))
711 return Result;
712 return APValue();
713}
714
Eli Friedman91110ee2009-02-23 04:23:56 +0000715APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
716 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
717 Info.EvalResult.HasSideEffects = true;
718 return GetZeroVector(E->getType());
719}
720
Nate Begeman59b5da62009-01-18 03:20:47 +0000721//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000722// Integer Evaluation
723//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000724
725namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000726class IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000727 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000728 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000729 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000730public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000731 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000732 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000733
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000734 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000735 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000736 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000737 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000738 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000739 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000740 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000741 return true;
742 }
743
Daniel Dunbar131eb432009-02-19 09:06:44 +0000744 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000745 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000746 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000747 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000748 Result = APValue(APSInt(I));
749 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000750 return true;
751 }
752
753 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000754 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000755 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000756 return true;
757 }
758
Anders Carlsson82206e22008-11-30 18:14:57 +0000759 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000760 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000761 if (Info.EvalResult.Diag == 0) {
762 Info.EvalResult.DiagLoc = L;
763 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000764 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000765 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000766 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Anders Carlssonc754aa62008-07-08 05:13:58 +0000769 //===--------------------------------------------------------------------===//
770 // Visitor Methods
771 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner32fea9d2008-11-12 07:43:42 +0000773 bool VisitStmt(Stmt *) {
774 assert(0 && "This should be called on integers, stmts are not integers");
775 return false;
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattner32fea9d2008-11-12 07:43:42 +0000778 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000779 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chris Lattnerb542afe2008-07-11 19:10:17 +0000782 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000783
Chris Lattner4c4867e2008-07-12 00:38:25 +0000784 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000785 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000786 }
787 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000788 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000789 }
790 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000791 // Per gcc docs "this built-in function ignores top level
792 // qualifiers". We need to use the canonical version to properly
793 // be able to strip CRV qualifiers from the type.
794 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
795 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000796 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000797 T1.getUnqualifiedType()),
798 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000799 }
Eli Friedman04309752009-11-24 05:28:59 +0000800
801 bool CheckReferencedDecl(const Expr *E, const Decl *D);
802 bool VisitDeclRefExpr(const DeclRefExpr *E) {
803 return CheckReferencedDecl(E, E->getDecl());
804 }
805 bool VisitMemberExpr(const MemberExpr *E) {
806 if (CheckReferencedDecl(E, E->getMemberDecl())) {
807 // Conservatively assume a MemberExpr will have side-effects
808 Info.EvalResult.HasSideEffects = true;
809 return true;
810 }
811 return false;
812 }
813
Chris Lattner4c4867e2008-07-12 00:38:25 +0000814 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000815 bool VisitBinaryOperator(const BinaryOperator *E);
816 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000817 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000818
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000819 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000820 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
821
Anders Carlsson3068d112008-11-16 19:01:22 +0000822 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000823 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anders Carlsson3f704562008-12-21 22:39:40 +0000826 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000827 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Anders Carlsson3068d112008-11-16 19:01:22 +0000830 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000831 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000832 }
833
Eli Friedman664a1042009-02-27 04:45:43 +0000834 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
835 return Success(0, E);
836 }
837
Sebastian Redl64b45f72009-01-05 20:52:13 +0000838 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000839 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000840 }
841
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000842 bool VisitChooseExpr(const ChooseExpr *E) {
843 return Visit(E->getChosenSubExpr(Info.Ctx));
844 }
845
Eli Friedman722c7172009-02-28 03:59:05 +0000846 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000847 bool VisitUnaryImag(const UnaryOperator *E);
848
Chris Lattnerfcee0012008-07-11 21:24:13 +0000849private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000850 unsigned GetAlignOfExpr(const Expr *E);
851 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000852 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000853};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000854} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000855
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000856static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000857 if (!E->getType()->isIntegralType())
858 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000860 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
861}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000862
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000863static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
864 APValue Val;
865 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
866 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000867 Result = Val.getInt();
868 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000869}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000870
Eli Friedman04309752009-11-24 05:28:59 +0000871bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000872 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +0000873 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
874 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000875
876 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000877 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000878 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
879 == Qualifiers::Const) {
Eli Friedman04309752009-11-24 05:28:59 +0000880 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000881 const VarDecl *Def = 0;
Eli Friedman04309752009-11-24 05:28:59 +0000882 if (const Expr *Init = VD->getDefinition(Def)) {
Eli Friedmanc0131182009-12-03 20:31:57 +0000883 if (APValue *V = VD->getEvaluatedValue()) {
884 if (V->isInt())
885 return Success(V->getInt(), E);
886 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
887 }
888
889 if (VD->isEvaluatingValue())
890 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
891
892 VD->setEvaluatingValue();
893
Douglas Gregor78d15832009-05-26 18:54:04 +0000894 if (Visit(const_cast<Expr*>(Init))) {
895 // Cache the evaluated value in the variable declaration.
Eli Friedmanc0131182009-12-03 20:31:57 +0000896 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +0000897 return true;
898 }
899
Eli Friedmanc0131182009-12-03 20:31:57 +0000900 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +0000901 return false;
902 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000903 }
904 }
905
Chris Lattner4c4867e2008-07-12 00:38:25 +0000906 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000907 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000908}
909
Chris Lattnera4d55d82008-10-06 06:40:35 +0000910/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
911/// as GCC.
912static int EvaluateBuiltinClassifyType(const CallExpr *E) {
913 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000914 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000915 enum gcc_type_class {
916 no_type_class = -1,
917 void_type_class, integer_type_class, char_type_class,
918 enumeral_type_class, boolean_type_class,
919 pointer_type_class, reference_type_class, offset_type_class,
920 real_type_class, complex_type_class,
921 function_type_class, method_type_class,
922 record_type_class, union_type_class,
923 array_type_class, string_type_class,
924 lang_type_class
925 };
Mike Stump1eb44332009-09-09 15:08:12 +0000926
927 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000928 // ideal, however it is what gcc does.
929 if (E->getNumArgs() == 0)
930 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattnera4d55d82008-10-06 06:40:35 +0000932 QualType ArgTy = E->getArg(0)->getType();
933 if (ArgTy->isVoidType())
934 return void_type_class;
935 else if (ArgTy->isEnumeralType())
936 return enumeral_type_class;
937 else if (ArgTy->isBooleanType())
938 return boolean_type_class;
939 else if (ArgTy->isCharType())
940 return string_type_class; // gcc doesn't appear to use char_type_class
941 else if (ArgTy->isIntegerType())
942 return integer_type_class;
943 else if (ArgTy->isPointerType())
944 return pointer_type_class;
945 else if (ArgTy->isReferenceType())
946 return reference_type_class;
947 else if (ArgTy->isRealType())
948 return real_type_class;
949 else if (ArgTy->isComplexType())
950 return complex_type_class;
951 else if (ArgTy->isFunctionType())
952 return function_type_class;
953 else if (ArgTy->isStructureType())
954 return record_type_class;
955 else if (ArgTy->isUnionType())
956 return union_type_class;
957 else if (ArgTy->isArrayType())
958 return array_type_class;
959 else if (ArgTy->isUnionType())
960 return union_type_class;
961 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
962 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
963 return -1;
964}
965
Chris Lattner4c4867e2008-07-12 00:38:25 +0000966bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000967 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000968 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000969 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000970
971 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000972 const Expr *Arg = E->getArg(0)->IgnoreParens();
973 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000974 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000975 && Base.Val.getKind() == APValue::LValue
976 && !Base.HasSideEffects)
977 if (const Expr *LVBase = Base.Val.getLValueBase())
978 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
979 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000980 if (!VD->getType()->isIncompleteType()
981 && VD->getType()->isObjectType()
982 && !VD->getType()->isVariablyModifiedType()
983 && !VD->getType()->isDependentType()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000984 CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType());
Ken Dycka7305832010-01-15 12:37:54 +0000985 CharUnits Offset = Base.Val.getLValueOffset();
Ken Dyck199c3d62010-01-11 17:06:35 +0000986 if (!Offset.isNegative() && Offset <= Size)
987 Size -= Offset;
Mike Stump460d1382009-10-28 21:22:24 +0000988 else
Ken Dyck199c3d62010-01-11 17:06:35 +0000989 Size = CharUnits::Zero();
990 return Success(Size.getQuantity(), E);
Mike Stump460d1382009-10-28 21:22:24 +0000991 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000992 }
993 }
994
Eric Christopherfee667f2009-12-23 03:49:37 +0000995 // TODO: Perhaps we should let LLVM lower this?
Fariborz Jahanian393c2472009-11-05 18:03:03 +0000996 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000997 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +0000998 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000999 return Success(0, E);
1000 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001001
Mike Stump64eda9e2009-10-26 18:35:08 +00001002 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1003 }
1004
Chris Lattner019f4e82008-10-06 05:28:25 +00001005 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001006 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001008 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001009 // __builtin_constant_p always has one operand: it returns true if that
1010 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001011 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001012
1013 case Builtin::BI__builtin_eh_return_data_regno: {
1014 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1015 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1016 return Success(Operand, E);
1017 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001018 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001019}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001020
Chris Lattnerb542afe2008-07-11 19:10:17 +00001021bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001022 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001023 if (!Visit(E->getRHS()))
1024 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001025
Eli Friedman33ef1452009-02-26 10:19:36 +00001026 // If we can't evaluate the LHS, it might have side effects;
1027 // conservatively mark it.
1028 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1029 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001030
Anders Carlsson027f62e2008-12-01 02:07:06 +00001031 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001032 }
1033
1034 if (E->isLogicalOp()) {
1035 // These need to be handled specially because the operands aren't
1036 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001037 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001039 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001040 // We were able to evaluate the LHS, see if we can get away with not
1041 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001042 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001043 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001044
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001045 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001046 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001047 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001048 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001049 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001050 }
1051 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001052 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001053 // We can't evaluate the LHS; however, sometimes the result
1054 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001055 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001056 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001057 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001058 // must have had side effects.
1059 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001060
1061 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001062 }
1063 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001064 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001065
Eli Friedmana6afa762008-11-13 06:09:17 +00001066 return false;
1067 }
1068
Anders Carlsson286f85e2008-11-16 07:17:21 +00001069 QualType LHSTy = E->getLHS()->getType();
1070 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001071
1072 if (LHSTy->isAnyComplexType()) {
1073 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1074 APValue LHS, RHS;
1075
1076 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1077 return false;
1078
1079 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1080 return false;
1081
1082 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001083 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001084 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001085 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001086 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1087
Daniel Dunbar4087e242009-01-29 06:43:41 +00001088 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001089 return Success((CR_r == APFloat::cmpEqual &&
1090 CR_i == APFloat::cmpEqual), E);
1091 else {
1092 assert(E->getOpcode() == BinaryOperator::NE &&
1093 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001094 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001095 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001096 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001097 CR_i == APFloat::cmpLessThan)), E);
1098 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001099 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001100 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001101 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1102 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1103 else {
1104 assert(E->getOpcode() == BinaryOperator::NE &&
1105 "Invalid compex comparison.");
1106 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1107 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1108 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001109 }
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Anders Carlsson286f85e2008-11-16 07:17:21 +00001112 if (LHSTy->isRealFloatingType() &&
1113 RHSTy->isRealFloatingType()) {
1114 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Anders Carlsson286f85e2008-11-16 07:17:21 +00001116 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1117 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Anders Carlsson286f85e2008-11-16 07:17:21 +00001119 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1120 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Anders Carlsson286f85e2008-11-16 07:17:21 +00001122 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001123
Anders Carlsson286f85e2008-11-16 07:17:21 +00001124 switch (E->getOpcode()) {
1125 default:
1126 assert(0 && "Invalid binary operator!");
1127 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001128 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001129 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001130 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001131 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001132 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001133 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001134 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001135 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001136 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001137 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001138 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001139 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001140 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001141 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001144 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1145 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001146 APValue LHSValue;
1147 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1148 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001149
Anders Carlsson3068d112008-11-16 19:01:22 +00001150 APValue RHSValue;
1151 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1152 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001153
Eli Friedman5bc86102009-06-14 02:17:33 +00001154 // Reject any bases from the normal codepath; we special-case comparisons
1155 // to null.
1156 if (LHSValue.getLValueBase()) {
1157 if (!E->isEqualityOp())
1158 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001159 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001160 return false;
1161 bool bres;
1162 if (!EvalPointerValueAsBool(LHSValue, bres))
1163 return false;
1164 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1165 } else if (RHSValue.getLValueBase()) {
1166 if (!E->isEqualityOp())
1167 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001168 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001169 return false;
1170 bool bres;
1171 if (!EvalPointerValueAsBool(RHSValue, bres))
1172 return false;
1173 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1174 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001175
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001176 if (E->getOpcode() == BinaryOperator::Sub) {
1177 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001178 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001179
Ken Dycka7305832010-01-15 12:37:54 +00001180 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001181 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001182 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001183
Ken Dycka7305832010-01-15 12:37:54 +00001184 CharUnits Diff = LHSValue.getLValueOffset() -
1185 RHSValue.getLValueOffset();
1186 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001187 }
1188 bool Result;
1189 if (E->getOpcode() == BinaryOperator::EQ) {
1190 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001191 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001192 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1193 }
1194 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001195 }
1196 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001197 if (!LHSTy->isIntegralType() ||
1198 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001199 // We can't continue from here for non-integral types, and they
1200 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001201 return false;
1202 }
1203
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001204 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001205 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001206 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001207
Eli Friedman42edd0d2009-03-24 01:14:50 +00001208 APValue RHSVal;
1209 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001210 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001211
1212 // Handle cases like (unsigned long)&a + 4.
1213 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001214 CharUnits Offset = Result.getLValueOffset();
1215 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1216 RHSVal.getInt().getZExtValue());
Eli Friedman42edd0d2009-03-24 01:14:50 +00001217 if (E->getOpcode() == BinaryOperator::Add)
Ken Dycka7305832010-01-15 12:37:54 +00001218 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001219 else
Ken Dycka7305832010-01-15 12:37:54 +00001220 Offset -= AdditionalOffset;
1221 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001222 return true;
1223 }
1224
1225 // Handle cases like 4 + (unsigned long)&a
1226 if (E->getOpcode() == BinaryOperator::Add &&
1227 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001228 CharUnits Offset = RHSVal.getLValueOffset();
1229 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1230 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001231 return true;
1232 }
1233
1234 // All the following cases expect both operands to be an integer
1235 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001236 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001237
Eli Friedman42edd0d2009-03-24 01:14:50 +00001238 APSInt& RHS = RHSVal.getInt();
1239
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001240 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001241 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001242 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001243 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1244 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1245 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1246 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1247 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1248 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001249 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001250 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001251 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001252 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001253 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001254 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001255 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001256 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001257 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001258 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001259 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001260 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1261 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001262 }
1263 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001264 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001265 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1266 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001269 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1270 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1271 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1272 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1273 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1274 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001275 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001276}
1277
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001278bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001279 bool Cond;
1280 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001281 return false;
1282
Nuno Lopesa25bd552008-11-16 22:06:39 +00001283 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001284}
1285
Chris Lattneraf707ab2009-01-24 21:53:27 +00001286unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001287 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1288 // the result is the size of the referenced type."
1289 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1290 // result shall be the alignment of the referenced type."
1291 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1292 T = Ref->getPointeeType();
1293
Chris Lattnere9feb472009-01-24 21:09:06 +00001294 // Get information about the alignment.
1295 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001296
Eli Friedman2be58612009-05-30 21:09:44 +00001297 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001298 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001299}
1300
Chris Lattneraf707ab2009-01-24 21:53:27 +00001301unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1302 E = E->IgnoreParens();
1303
1304 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001305 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001306 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001307 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001308
Chris Lattneraf707ab2009-01-24 21:53:27 +00001309 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001310 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1311 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001312
Chris Lattnere9feb472009-01-24 21:09:06 +00001313 return GetAlignOfType(E->getType());
1314}
1315
1316
Sebastian Redl05189992008-11-11 17:56:53 +00001317/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1318/// expression's type.
1319bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001320 // Handle alignof separately.
1321 if (!E->isSizeOf()) {
1322 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001323 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001324 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001325 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001326 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001327
Sebastian Redl05189992008-11-11 17:56:53 +00001328 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001329 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1330 // the result is the size of the referenced type."
1331 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1332 // result shall be the alignment of the referenced type."
1333 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1334 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001335
Daniel Dunbar131eb432009-02-19 09:06:44 +00001336 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1337 // extension.
1338 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1339 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001340
Chris Lattnerfcee0012008-07-11 21:24:13 +00001341 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001342 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001343 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001344
Chris Lattnere9feb472009-01-24 21:09:06 +00001345 // Get information about the size.
Ken Dyck199c3d62010-01-11 17:06:35 +00001346 return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001347}
1348
Chris Lattnerb542afe2008-07-11 19:10:17 +00001349bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001350 // Special case unary operators that do not need their subexpression
1351 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001352 if (E->isOffsetOfOp()) {
1353 // The AST for offsetof is defined in such a way that we can just
1354 // directly Evaluate it as an l-value.
1355 APValue LV;
1356 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1357 return false;
1358 if (LV.getLValueBase())
1359 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001360 return Success(LV.getLValueOffset().getQuantity(), E);
Eli Friedman35183ac2009-02-27 06:44:11 +00001361 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001362
1363 if (E->getOpcode() == UnaryOperator::LNot) {
1364 // LNot's operand isn't necessarily an integer, so we handle it specially.
1365 bool bres;
1366 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1367 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001368 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001369 }
1370
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001371 // Only handle integral operations...
1372 if (!E->getSubExpr()->getType()->isIntegralType())
1373 return false;
1374
Chris Lattner87eae5e2008-07-11 22:52:41 +00001375 // Get the operand value into 'Result'.
1376 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001377 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001378
Chris Lattner75a48812008-07-11 22:15:16 +00001379 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001380 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001381 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1382 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001383 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001384 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001385 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1386 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001387 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001388 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001389 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001390 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001391 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001392 if (!Result.isInt()) return false;
1393 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001394 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001395 if (!Result.isInt()) return false;
1396 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001397 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001398}
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Chris Lattner732b2232008-07-12 01:15:53 +00001400/// HandleCast - This is used to evaluate implicit or explicit casts where the
1401/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001402bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001403 Expr *SubExpr = E->getSubExpr();
1404 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001405 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001406
Eli Friedman4efaa272008-11-12 09:44:48 +00001407 if (DestType->isBooleanType()) {
1408 bool BoolResult;
1409 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1410 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001411 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001412 }
1413
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001414 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001415 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001416 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001417 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001418
Eli Friedmanbe265702009-02-20 01:15:07 +00001419 if (!Result.isInt()) {
1420 // Only allow casts of lvalues if they are lossless.
1421 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1422 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001423
Daniel Dunbardd211642009-02-19 22:24:01 +00001424 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001425 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001426 }
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Chris Lattner732b2232008-07-12 01:15:53 +00001428 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001429 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001430 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001431 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001432 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001433
Daniel Dunbardd211642009-02-19 22:24:01 +00001434 if (LV.getLValueBase()) {
1435 // Only allow based lvalue casts if they are lossless.
1436 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1437 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001438
Daniel Dunbardd211642009-02-19 22:24:01 +00001439 Result = LV;
1440 return true;
1441 }
1442
Ken Dycka7305832010-01-15 12:37:54 +00001443 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1444 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001445 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001446 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001447
Eli Friedmanbe265702009-02-20 01:15:07 +00001448 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1449 // This handles double-conversion cases, where there's both
1450 // an l-value promotion and an implicit conversion to int.
1451 APValue LV;
1452 if (!EvaluateLValue(SubExpr, LV, Info))
1453 return false;
1454
1455 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1456 return false;
1457
1458 Result = LV;
1459 return true;
1460 }
1461
Eli Friedman1725f682009-04-22 19:23:09 +00001462 if (SrcType->isAnyComplexType()) {
1463 APValue C;
1464 if (!EvaluateComplex(SubExpr, C, Info))
1465 return false;
1466 if (C.isComplexFloat())
1467 return Success(HandleFloatToIntCast(DestType, SrcType,
1468 C.getComplexFloatReal(), Info.Ctx),
1469 E);
1470 else
1471 return Success(HandleIntToIntCast(DestType, SrcType,
1472 C.getComplexIntReal(), Info.Ctx), E);
1473 }
Eli Friedman2217c872009-02-22 11:46:18 +00001474 // FIXME: Handle vectors
1475
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001476 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001477 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001478
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001479 APFloat F(0.0);
1480 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001481 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001483 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001484}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001485
Eli Friedman722c7172009-02-28 03:59:05 +00001486bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1487 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1488 APValue LV;
1489 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1490 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1491 return Success(LV.getComplexIntReal(), E);
1492 }
1493
1494 return Visit(E->getSubExpr());
1495}
1496
Eli Friedman664a1042009-02-27 04:45:43 +00001497bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001498 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1499 APValue LV;
1500 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1501 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1502 return Success(LV.getComplexIntImag(), E);
1503 }
1504
Eli Friedman664a1042009-02-27 04:45:43 +00001505 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1506 Info.EvalResult.HasSideEffects = true;
1507 return Success(0, E);
1508}
1509
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001510//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001511// Float Evaluation
1512//===----------------------------------------------------------------------===//
1513
1514namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001515class FloatExprEvaluator
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001516 : public StmtVisitor<FloatExprEvaluator, bool> {
1517 EvalInfo &Info;
1518 APFloat &Result;
1519public:
1520 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1521 : Info(info), Result(result) {}
1522
1523 bool VisitStmt(Stmt *S) {
1524 return false;
1525 }
1526
1527 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001528 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001529
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001530 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001531 bool VisitBinaryOperator(const BinaryOperator *E);
1532 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001533 bool VisitCastExpr(CastExpr *E);
1534 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman67f85fc2009-12-04 02:12:53 +00001535 bool VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001536
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001537 bool VisitChooseExpr(const ChooseExpr *E)
1538 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1539 bool VisitUnaryExtension(const UnaryOperator *E)
1540 { return Visit(E->getSubExpr()); }
1541
1542 // FIXME: Missing: __real__/__imag__, array subscript of vector,
Eli Friedman67f85fc2009-12-04 02:12:53 +00001543 // member of vector, ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001544};
1545} // end anonymous namespace
1546
1547static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1548 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1549}
1550
Chris Lattner019f4e82008-10-06 05:28:25 +00001551bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001552 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001553 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001554 case Builtin::BI__builtin_huge_val:
1555 case Builtin::BI__builtin_huge_valf:
1556 case Builtin::BI__builtin_huge_vall:
1557 case Builtin::BI__builtin_inf:
1558 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001559 case Builtin::BI__builtin_infl: {
1560 const llvm::fltSemantics &Sem =
1561 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001562 Result = llvm::APFloat::getInf(Sem);
1563 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Chris Lattner9e621712008-10-06 06:31:58 +00001566 case Builtin::BI__builtin_nan:
1567 case Builtin::BI__builtin_nanf:
1568 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001569 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001570 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001571 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001572 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001573 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001574 const llvm::fltSemantics &Sem =
1575 Info.Ctx.getFloatTypeSemantics(E->getType());
Benjamin Kramer33035802009-12-11 13:26:32 +00001576 unsigned Type = 0;
1577 if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
Mike Stump4572bab2009-05-30 03:56:50 +00001578 return false;
Benjamin Kramer33035802009-12-11 13:26:32 +00001579 Result = llvm::APFloat::getNaN(Sem, false, Type);
Chris Lattner9e621712008-10-06 06:31:58 +00001580 return true;
1581 }
1582 }
1583 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001584
1585 case Builtin::BI__builtin_fabs:
1586 case Builtin::BI__builtin_fabsf:
1587 case Builtin::BI__builtin_fabsl:
1588 if (!EvaluateFloat(E->getArg(0), Result, Info))
1589 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001591 if (Result.isNegative())
1592 Result.changeSign();
1593 return true;
1594
Mike Stump1eb44332009-09-09 15:08:12 +00001595 case Builtin::BI__builtin_copysign:
1596 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001597 case Builtin::BI__builtin_copysignl: {
1598 APFloat RHS(0.);
1599 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1600 !EvaluateFloat(E->getArg(1), RHS, Info))
1601 return false;
1602 Result.copySign(RHS);
1603 return true;
1604 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001605 }
1606}
1607
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001608bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001609 if (E->getOpcode() == UnaryOperator::Deref)
1610 return false;
1611
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001612 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1613 return false;
1614
1615 switch (E->getOpcode()) {
1616 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001617 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001618 return true;
1619 case UnaryOperator::Minus:
1620 Result.changeSign();
1621 return true;
1622 }
1623}
Chris Lattner019f4e82008-10-06 05:28:25 +00001624
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001625bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001626 if (E->getOpcode() == BinaryOperator::Comma) {
1627 if (!EvaluateFloat(E->getRHS(), Result, Info))
1628 return false;
1629
1630 // If we can't evaluate the LHS, it might have side effects;
1631 // conservatively mark it.
1632 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1633 Info.EvalResult.HasSideEffects = true;
1634
1635 return true;
1636 }
1637
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001638 // FIXME: Diagnostics? I really don't understand how the warnings
1639 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001640 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001641 if (!EvaluateFloat(E->getLHS(), Result, Info))
1642 return false;
1643 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1644 return false;
1645
1646 switch (E->getOpcode()) {
1647 default: return false;
1648 case BinaryOperator::Mul:
1649 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1650 return true;
1651 case BinaryOperator::Add:
1652 Result.add(RHS, APFloat::rmNearestTiesToEven);
1653 return true;
1654 case BinaryOperator::Sub:
1655 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1656 return true;
1657 case BinaryOperator::Div:
1658 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1659 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001660 }
1661}
1662
1663bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1664 Result = E->getValue();
1665 return true;
1666}
1667
Eli Friedman4efaa272008-11-12 09:44:48 +00001668bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1669 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Eli Friedman4efaa272008-11-12 09:44:48 +00001671 if (SubExpr->getType()->isIntegralType()) {
1672 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001673 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001674 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001675 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001676 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001677 return true;
1678 }
1679 if (SubExpr->getType()->isRealFloatingType()) {
1680 if (!Visit(SubExpr))
1681 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001682 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1683 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001684 return true;
1685 }
Eli Friedman2217c872009-02-22 11:46:18 +00001686 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001687
1688 return false;
1689}
1690
1691bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1692 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1693 return true;
1694}
1695
Eli Friedman67f85fc2009-12-04 02:12:53 +00001696bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
1697 bool Cond;
1698 if (!HandleConversionToBool(E->getCond(), Cond, Info))
1699 return false;
1700
1701 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
1702}
1703
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001704//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001705// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001706//===----------------------------------------------------------------------===//
1707
1708namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001709class ComplexExprEvaluator
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001710 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001711 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001713public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001714 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001716 //===--------------------------------------------------------------------===//
1717 // Visitor Methods
1718 //===--------------------------------------------------------------------===//
1719
1720 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001721 return APValue();
1722 }
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001724 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1725
1726 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001727 Expr* SubExpr = E->getSubExpr();
1728
1729 if (SubExpr->getType()->isRealFloatingType()) {
1730 APFloat Result(0.0);
1731
1732 if (!EvaluateFloat(SubExpr, Result, Info))
1733 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001734
1735 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001736 Result);
1737 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001738 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001739 "Unexpected imaginary literal.");
1740
1741 llvm::APSInt Result;
1742 if (!EvaluateInteger(SubExpr, Result, Info))
1743 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001745 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1746 Zero = 0;
1747 return APValue(Zero, Result);
1748 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001749 }
1750
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001751 APValue VisitCastExpr(CastExpr *E) {
1752 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001753 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001754 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001755
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001756 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001757 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001758
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001759 if (!EvaluateFloat(SubExpr, Result, Info))
1760 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001761
1762 if (EltType->isRealFloatingType()) {
1763 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001764 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001765 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1766 } else {
1767 llvm::APSInt IResult;
1768 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1769 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1770 Zero = 0;
1771 return APValue(IResult, Zero);
1772 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001773 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001774 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001775
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001776 if (!EvaluateInteger(SubExpr, Result, Info))
1777 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001778
Eli Friedman1725f682009-04-22 19:23:09 +00001779 if (EltType->isRealFloatingType()) {
1780 APFloat FResult =
1781 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001782 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001783 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1784 } else {
1785 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1786 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1787 Zero = 0;
1788 return APValue(Result, Zero);
1789 }
John McCall183700f2009-09-21 23:43:11 +00001790 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001791 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001792
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001793 if (!EvaluateComplex(SubExpr, Src, Info))
1794 return APValue();
1795
1796 QualType SrcType = CT->getElementType();
1797
1798 if (Src.isComplexFloat()) {
1799 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001800 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001801 Src.getComplexFloatReal(),
1802 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001803 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001804 Src.getComplexFloatImag(),
1805 Info.Ctx));
1806 } else {
1807 return APValue(HandleFloatToIntCast(EltType, SrcType,
1808 Src.getComplexFloatReal(),
1809 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001810 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001811 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001812 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001813 }
1814 } else {
1815 assert(Src.isComplexInt() && "Invalid evaluate result.");
1816 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001817 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001818 Src.getComplexIntReal(),
1819 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001820 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001821 Src.getComplexIntImag(),
1822 Info.Ctx));
1823 } else {
1824 return APValue(HandleIntToIntCast(EltType, SrcType,
1825 Src.getComplexIntReal(),
1826 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001827 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001828 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001829 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001830 }
1831 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001832 }
1833
1834 // FIXME: Handle more casts.
1835 return APValue();
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001838 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001839 APValue VisitChooseExpr(const ChooseExpr *E)
1840 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1841 APValue VisitUnaryExtension(const UnaryOperator *E)
1842 { return Visit(E->getSubExpr()); }
1843 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001844 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001845};
1846} // end anonymous namespace
1847
Mike Stump1eb44332009-09-09 15:08:12 +00001848static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001849 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1850 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001851 (&Result.getComplexFloatReal().getSemantics() ==
1852 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001853 "Invalid complex evaluation.");
1854 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001855}
1856
Mike Stump1eb44332009-09-09 15:08:12 +00001857APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001858 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001860 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001861 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001863 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001864 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001865
Daniel Dunbar3f279872009-01-29 01:32:56 +00001866 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1867 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001868 switch (E->getOpcode()) {
1869 default: return APValue();
1870 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001871 if (Result.isComplexFloat()) {
1872 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1873 APFloat::rmNearestTiesToEven);
1874 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1875 APFloat::rmNearestTiesToEven);
1876 } else {
1877 Result.getComplexIntReal() += RHS.getComplexIntReal();
1878 Result.getComplexIntImag() += RHS.getComplexIntImag();
1879 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001880 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001881 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001882 if (Result.isComplexFloat()) {
1883 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1884 APFloat::rmNearestTiesToEven);
1885 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1886 APFloat::rmNearestTiesToEven);
1887 } else {
1888 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1889 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1890 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001891 break;
1892 case BinaryOperator::Mul:
1893 if (Result.isComplexFloat()) {
1894 APValue LHS = Result;
1895 APFloat &LHS_r = LHS.getComplexFloatReal();
1896 APFloat &LHS_i = LHS.getComplexFloatImag();
1897 APFloat &RHS_r = RHS.getComplexFloatReal();
1898 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Daniel Dunbar3f279872009-01-29 01:32:56 +00001900 APFloat Tmp = LHS_r;
1901 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1902 Result.getComplexFloatReal() = Tmp;
1903 Tmp = LHS_i;
1904 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1905 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1906
1907 Tmp = LHS_r;
1908 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1909 Result.getComplexFloatImag() = Tmp;
1910 Tmp = LHS_i;
1911 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1912 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1913 } else {
1914 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001915 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001916 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1917 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001918 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001919 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1920 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1921 }
1922 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001923 }
1924
1925 return Result;
1926}
1927
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001928//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001929// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001930//===----------------------------------------------------------------------===//
1931
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001932/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001933/// any crazy technique (that has nothing to do with language standards) that
1934/// we want to. If this function returns true, it returns the folded constant
1935/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001936bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1937 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001938
Nate Begeman59b5da62009-01-18 03:20:47 +00001939 if (getType()->isVectorType()) {
1940 if (!EvaluateVector(this, Result.Val, Info))
1941 return false;
1942 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001943 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001944 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001945 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001946 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001947 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001948 } else if (getType()->isRealFloatingType()) {
1949 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001950 if (!EvaluateFloat(this, f, Info))
1951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001953 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001954 } else if (getType()->isAnyComplexType()) {
1955 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001956 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001957 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001958 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001959
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001960 return true;
1961}
1962
Mike Stump660e6f72009-10-26 23:05:19 +00001963bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1964 EvalInfo Info(Ctx, Result, true);
1965
1966 if (getType()->isVectorType()) {
1967 if (!EvaluateVector(this, Result.Val, Info))
1968 return false;
1969 } else if (getType()->isIntegerType()) {
1970 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1971 return false;
1972 } else if (getType()->hasPointerRepresentation()) {
1973 if (!EvaluatePointer(this, Result.Val, Info))
1974 return false;
1975 } else if (getType()->isRealFloatingType()) {
1976 llvm::APFloat f(0.0);
1977 if (!EvaluateFloat(this, f, Info))
1978 return false;
1979
1980 Result.Val = APValue(f);
1981 } else if (getType()->isAnyComplexType()) {
1982 if (!EvaluateComplex(this, Result.Val, Info))
1983 return false;
1984 } else
1985 return false;
1986
1987 return true;
1988}
1989
John McCallcd7a4452010-01-05 23:42:56 +00001990bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
1991 EvalResult Scratch;
1992 EvalInfo Info(Ctx, Scratch);
1993
1994 return HandleConversionToBool(this, Result, Info);
1995}
1996
Anders Carlsson1b782762009-04-10 04:54:13 +00001997bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1998 EvalInfo Info(Ctx, Result);
1999
2000 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2001}
2002
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002003bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
2004 EvalInfo Info(Ctx, Result, true);
2005
2006 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
2007}
2008
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002009/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002010/// folded, but discard the result.
2011bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002012 EvalResult Result;
2013 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002014}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002015
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002016bool Expr::HasSideEffects(ASTContext &Ctx) const {
2017 Expr::EvalResult Result;
2018 EvalInfo Info(Ctx, Result);
2019 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
2020}
2021
Anders Carlsson51fe9962008-11-22 21:04:56 +00002022APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002023 EvalResult EvalResult;
2024 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00002025 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002026 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002027 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002028
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002029 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002030}