blob: 4494896d2c450e98ef48a1da70cdd80bb8ec2a10 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000022#include "llvm/Support/Compiler.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
Mike Stump1eb44332009-09-09 15:08:12 +000049 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Eli Friedman33ef1452009-02-26 10:19:36 +000050 EvalResult(evalresult) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000051};
52
53
Eli Friedman4efaa272008-11-12 09:44:48 +000054static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000055static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
56static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000057static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000058static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000059static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000060
61//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000062// Misc utilities
63//===----------------------------------------------------------------------===//
64
Eli Friedman5bc86102009-06-14 02:17:33 +000065static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
66 // FIXME: Is this accurate for all kinds of bases? If not, what would
67 // the check look like?
68 Result = Value.getLValueBase() || Value.getLValueOffset();
69 return true;
70}
71
Eli Friedman4efaa272008-11-12 09:44:48 +000072static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
73 if (E->getType()->isIntegralType()) {
74 APSInt IntResult;
75 if (!EvaluateInteger(E, IntResult, Info))
76 return false;
77 Result = IntResult != 0;
78 return true;
79 } else if (E->getType()->isRealFloatingType()) {
80 APFloat FloatResult(0.0);
81 if (!EvaluateFloat(E, FloatResult, Info))
82 return false;
83 Result = !FloatResult.isZero();
84 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000085 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000086 APValue PointerResult;
87 if (!EvaluatePointer(E, PointerResult, Info))
88 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000089 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000090 } else if (E->getType()->isAnyComplexType()) {
91 APValue ComplexResult;
92 if (!EvaluateComplex(E, ComplexResult, Info))
93 return false;
94 if (ComplexResult.isComplexFloat()) {
95 Result = !ComplexResult.getComplexFloatReal().isZero() ||
96 !ComplexResult.getComplexFloatImag().isZero();
97 } else {
98 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
99 ComplexResult.getComplexIntImag().getBoolValue();
100 }
101 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000102 }
103
104 return false;
105}
106
Mike Stump1eb44332009-09-09 15:08:12 +0000107static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000108 APFloat &Value, ASTContext &Ctx) {
109 unsigned DestWidth = Ctx.getIntWidth(DestType);
110 // Determine whether we are converting to unsigned or signed.
111 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000113 // FIXME: Warning for overflow.
114 uint64_t Space[4];
115 bool ignored;
116 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
117 llvm::APFloat::rmTowardZero, &ignored);
118 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000122 APFloat &Value, ASTContext &Ctx) {
123 bool ignored;
124 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000126 APFloat::rmNearestTiesToEven, &ignored);
127 return Result;
128}
129
Mike Stump1eb44332009-09-09 15:08:12 +0000130static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000131 APSInt &Value, ASTContext &Ctx) {
132 unsigned DestWidth = Ctx.getIntWidth(DestType);
133 APSInt Result = Value;
134 // Figure out if this is a truncate, extend or noop cast.
135 // If the input is signed, do a sign extend, noop, or truncate.
136 Result.extOrTrunc(DestWidth);
137 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
138 return Result;
139}
140
Mike Stump1eb44332009-09-09 15:08:12 +0000141static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000142 APSInt &Value, ASTContext &Ctx) {
143
144 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
145 Result.convertFromAPInt(Value, Value.isSigned(),
146 APFloat::rmNearestTiesToEven);
147 return Result;
148}
149
Eli Friedman4efaa272008-11-12 09:44:48 +0000150//===----------------------------------------------------------------------===//
151// LValue Evaluation
152//===----------------------------------------------------------------------===//
153namespace {
154class VISIBILITY_HIDDEN LValueExprEvaluator
155 : public StmtVisitor<LValueExprEvaluator, APValue> {
156 EvalInfo &Info;
157public:
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Eli Friedman4efaa272008-11-12 09:44:48 +0000159 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
160
161 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000162 return APValue();
163 }
164
165 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000166 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000167 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000168 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
169 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
170 APValue VisitMemberExpr(MemberExpr *E);
171 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000172 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000173 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000174 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000175 APValue VisitUnaryExtension(const UnaryOperator *E)
176 { return Visit(E->getSubExpr()); }
177 APValue VisitChooseExpr(const ChooseExpr *E)
178 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
179 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000180};
181} // end anonymous namespace
182
183static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
184 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
185 return Result.isLValue();
186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000189 if (isa<FunctionDecl>(E->getDecl())) {
190 return APValue(E, 0);
191 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmand933a012009-08-29 19:09:59 +0000192 if (!VD->hasGlobalStorage())
193 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000194 if (!VD->getType()->isReferenceType())
195 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000196 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000197 if (VD->getInit())
198 return Visit(VD->getInit());
199 }
200
201 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000202}
203
Mike Stump1eb44332009-09-09 15:08:12 +0000204APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000205 if (E->hasBlockDeclRefExprs())
206 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Steve Naroff3aaa4822009-04-16 19:02:57 +0000208 return APValue(E, 0);
209}
210
Eli Friedman4efaa272008-11-12 09:44:48 +0000211APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
212 if (E->isFileScope())
213 return APValue(E, 0);
214 return APValue();
215}
216
217APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
218 APValue result;
219 QualType Ty;
220 if (E->isArrow()) {
221 if (!EvaluatePointer(E->getBase(), result, Info))
222 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000223 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000224 } else {
225 result = Visit(E->getBase());
226 if (result.isUninit())
227 return APValue();
228 Ty = E->getBase()->getType();
229 }
230
Ted Kremenek6217b802009-07-29 21:53:49 +0000231 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000232 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000233
234 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
235 if (!FD) // FIXME: deal with other kinds of member expressions
236 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000237
238 if (FD->getType()->isReferenceType())
239 return APValue();
240
Eli Friedman4efaa272008-11-12 09:44:48 +0000241 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000242 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000243 for (RecordDecl::field_iterator Field = RD->field_begin(),
244 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000245 Field != FieldEnd; (void)++Field, ++i) {
246 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000247 break;
248 }
249
250 result.setLValue(result.getLValueBase(),
251 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
252
253 return result;
254}
255
Mike Stump1eb44332009-09-09 15:08:12 +0000256APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000257 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlsson3068d112008-11-16 19:01:22 +0000259 if (!EvaluatePointer(E->getBase(), Result, Info))
260 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlsson3068d112008-11-16 19:01:22 +0000262 APSInt Index;
263 if (!EvaluateInteger(E->getIdx(), Index, Info))
264 return APValue();
265
266 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
267
268 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000269 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000270 Result.getLValueOffset() + Offset);
271 return Result;
272}
Eli Friedman4efaa272008-11-12 09:44:48 +0000273
Mike Stump1eb44332009-09-09 15:08:12 +0000274APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000275 APValue Result;
276 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
277 return APValue();
278 return Result;
279}
280
Eli Friedman4efaa272008-11-12 09:44:48 +0000281//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000282// Pointer Evaluation
283//===----------------------------------------------------------------------===//
284
Anders Carlssonc754aa62008-07-08 05:13:58 +0000285namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000286class VISIBILITY_HIDDEN PointerExprEvaluator
287 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000288 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000289public:
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner87eae5e2008-07-11 22:52:41 +0000291 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000292
Anders Carlsson2bad1682008-07-08 14:30:00 +0000293 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000294 return APValue();
295 }
296
297 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
298
Anders Carlsson650c92f2008-07-08 15:34:11 +0000299 APValue VisitBinaryOperator(const BinaryOperator *E);
300 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000301 APValue VisitUnaryExtension(const UnaryOperator *E)
302 { return Visit(E->getSubExpr()); }
303 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000304 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
305 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000306 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
307 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000308 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000309 APValue VisitBlockExpr(BlockExpr *E) {
310 if (!E->hasBlockDeclRefExprs())
311 return APValue(E, 0);
312 return APValue();
313 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000314 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
315 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000316 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000317 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000318 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
319 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
320 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000321 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000322};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000323} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000324
Chris Lattner87eae5e2008-07-11 22:52:41 +0000325static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000326 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000327 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000328 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000329 return Result.isLValue();
330}
331
332APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
333 if (E->getOpcode() != BinaryOperator::Add &&
334 E->getOpcode() != BinaryOperator::Sub)
335 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000337 const Expr *PExp = E->getLHS();
338 const Expr *IExp = E->getRHS();
339 if (IExp->getType()->isPointerType())
340 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000342 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000343 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000344 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000346 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000347 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000348 return APValue();
349
Ted Kremenek6217b802009-07-29 21:53:49 +0000350 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000351 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000353 // Explicitly handle GNU void* and function pointer arithmetic extensions.
354 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
355 SizeOfPointee = 1;
356 else
357 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000358
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000359 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000360
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000361 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000362 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000363 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000364 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
365
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000366 return APValue(ResultLValue.getLValueBase(), Offset);
367}
Eli Friedman4efaa272008-11-12 09:44:48 +0000368
Eli Friedman2217c872009-02-22 11:46:18 +0000369APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
370 APValue result;
371 if (EvaluateLValue(E->getSubExpr(), result, Info))
372 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000373 return APValue();
374}
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000376
Chris Lattnerb542afe2008-07-11 19:10:17 +0000377APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000378 const Expr* SubExpr = E->getSubExpr();
379
380 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000381 if (SubExpr->getType()->isPointerType() ||
382 SubExpr->getType()->isObjCObjectPointerType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000383 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000384 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000385 return Result;
386 return APValue();
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000389 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000390 APValue Result;
391 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
392 return APValue();
393
394 if (Result.isInt()) {
395 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
396 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000399 // Cast is of an lvalue, no need to change value.
400 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000401 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000402
403 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000404 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000405 SubExpr->getType()->isArrayType()) {
406 APValue Result;
407 if (EvaluateLValue(SubExpr, Result, Info))
408 return Result;
409 return APValue();
410 }
411
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000413}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000414
Eli Friedman3941b182009-01-25 01:54:01 +0000415APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000416 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000417 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000418 return APValue(E, 0);
419 return APValue();
420}
421
Eli Friedman4efaa272008-11-12 09:44:48 +0000422APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
423 bool BoolResult;
424 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
425 return APValue();
426
427 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
428
429 APValue Result;
430 if (EvaluatePointer(EvalExpr, Result, Info))
431 return Result;
432 return APValue();
433}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000434
435//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000436// Vector Evaluation
437//===----------------------------------------------------------------------===//
438
439namespace {
440 class VISIBILITY_HIDDEN VectorExprEvaluator
441 : public StmtVisitor<VectorExprEvaluator, APValue> {
442 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000443 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000444 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Nate Begeman59b5da62009-01-18 03:20:47 +0000446 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Nate Begeman59b5da62009-01-18 03:20:47 +0000448 APValue VisitStmt(Stmt *S) {
449 return APValue();
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Eli Friedman91110ee2009-02-23 04:23:56 +0000452 APValue VisitParenExpr(ParenExpr *E)
453 { return Visit(E->getSubExpr()); }
454 APValue VisitUnaryExtension(const UnaryOperator *E)
455 { return Visit(E->getSubExpr()); }
456 APValue VisitUnaryPlus(const UnaryOperator *E)
457 { return Visit(E->getSubExpr()); }
458 APValue VisitUnaryReal(const UnaryOperator *E)
459 { return Visit(E->getSubExpr()); }
460 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
461 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000462 APValue VisitCastExpr(const CastExpr* E);
463 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
464 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000465 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000466 APValue VisitChooseExpr(const ChooseExpr *E)
467 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000468 APValue VisitUnaryImag(const UnaryOperator *E);
469 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000470 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000471 // shufflevector, ExtVectorElementExpr
472 // (Note that these require implementing conversions
473 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000474 };
475} // end anonymous namespace
476
477static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
478 if (!E->getType()->isVectorType())
479 return false;
480 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
481 return !Result.isUninit();
482}
483
484APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
Nate Begemanc0b8b192009-07-01 07:50:47 +0000485 const VectorType *VTy = E->getType()->getAsVectorType();
486 QualType EltTy = VTy->getElementType();
487 unsigned NElts = VTy->getNumElements();
488 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Nate Begeman59b5da62009-01-18 03:20:47 +0000490 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000491 QualType SETy = SE->getType();
492 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000493
Nate Begemane8c9e922009-06-26 18:22:18 +0000494 // Check for vector->vector bitcast and scalar->vector splat.
495 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000496 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000497 } else if (SETy->isIntegerType()) {
498 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000499 if (!EvaluateInteger(SE, IntResult, Info))
500 return APValue();
501 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000502 } else if (SETy->isRealFloatingType()) {
503 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000504 if (!EvaluateFloat(SE, F, Info))
505 return APValue();
506 Result = APValue(F);
507 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000508 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000509
Nate Begemanc0b8b192009-07-01 07:50:47 +0000510 // For casts of a scalar to ExtVector, convert the scalar to the element type
511 // and splat it to all elements.
512 if (E->getType()->isExtVectorType()) {
513 if (EltTy->isIntegerType() && Result.isInt())
514 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
515 Info.Ctx));
516 else if (EltTy->isIntegerType())
517 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
518 Info.Ctx));
519 else if (EltTy->isRealFloatingType() && Result.isInt())
520 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
521 Info.Ctx));
522 else if (EltTy->isRealFloatingType())
523 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
524 Info.Ctx));
525 else
526 return APValue();
527
528 // Splat and create vector APValue.
529 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
530 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000531 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000532
533 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
534 // to the vector. To construct the APValue vector initializer, bitcast the
535 // initializing value to an APInt, and shift out the bits pertaining to each
536 // element.
537 APSInt Init;
538 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Nate Begemanc0b8b192009-07-01 07:50:47 +0000540 llvm::SmallVector<APValue, 4> Elts;
541 for (unsigned i = 0; i != NElts; ++i) {
542 APSInt Tmp = Init;
543 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Nate Begemanc0b8b192009-07-01 07:50:47 +0000545 if (EltTy->isIntegerType())
546 Elts.push_back(APValue(Tmp));
547 else if (EltTy->isRealFloatingType())
548 Elts.push_back(APValue(APFloat(Tmp)));
549 else
550 return APValue();
551
552 Init >>= EltWidth;
553 }
554 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000555}
556
Mike Stump1eb44332009-09-09 15:08:12 +0000557APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000558VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
559 return this->Visit(const_cast<Expr*>(E->getInitializer()));
560}
561
Mike Stump1eb44332009-09-09 15:08:12 +0000562APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000563VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
564 const VectorType *VT = E->getType()->getAsVectorType();
565 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000566 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Nate Begeman59b5da62009-01-18 03:20:47 +0000568 QualType EltTy = VT->getElementType();
569 llvm::SmallVector<APValue, 4> Elements;
570
Eli Friedman91110ee2009-02-23 04:23:56 +0000571 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000572 if (EltTy->isIntegerType()) {
573 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000574 if (i < NumInits) {
575 if (!EvaluateInteger(E->getInit(i), sInt, Info))
576 return APValue();
577 } else {
578 sInt = Info.Ctx.MakeIntValue(0, EltTy);
579 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000580 Elements.push_back(APValue(sInt));
581 } else {
582 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000583 if (i < NumInits) {
584 if (!EvaluateFloat(E->getInit(i), f, Info))
585 return APValue();
586 } else {
587 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
588 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000589 Elements.push_back(APValue(f));
590 }
591 }
592 return APValue(&Elements[0], Elements.size());
593}
594
Mike Stump1eb44332009-09-09 15:08:12 +0000595APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000596VectorExprEvaluator::GetZeroVector(QualType T) {
597 const VectorType *VT = T->getAsVectorType();
598 QualType EltTy = VT->getElementType();
599 APValue ZeroElement;
600 if (EltTy->isIntegerType())
601 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
602 else
603 ZeroElement =
604 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
605
606 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
607 return APValue(&Elements[0], Elements.size());
608}
609
610APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
611 bool BoolResult;
612 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
613 return APValue();
614
615 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
616
617 APValue Result;
618 if (EvaluateVector(EvalExpr, Result, Info))
619 return Result;
620 return APValue();
621}
622
Eli Friedman91110ee2009-02-23 04:23:56 +0000623APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
624 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
625 Info.EvalResult.HasSideEffects = true;
626 return GetZeroVector(E->getType());
627}
628
Nate Begeman59b5da62009-01-18 03:20:47 +0000629//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000630// Integer Evaluation
631//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000632
633namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000634class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000635 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000636 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000637 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000638public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000639 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000640 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000641
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000642 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000643 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000644 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000645 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000646 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000647 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000648 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000649 return true;
650 }
651
Daniel Dunbar131eb432009-02-19 09:06:44 +0000652 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000653 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000654 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000655 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000656 Result = APValue(APSInt(I));
657 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000658 return true;
659 }
660
661 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000662 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000663 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000664 return true;
665 }
666
Anders Carlsson82206e22008-11-30 18:14:57 +0000667 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000668 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000669 if (Info.EvalResult.Diag == 0) {
670 Info.EvalResult.DiagLoc = L;
671 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000672 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000673 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000674 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anders Carlssonc754aa62008-07-08 05:13:58 +0000677 //===--------------------------------------------------------------------===//
678 // Visitor Methods
679 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner32fea9d2008-11-12 07:43:42 +0000681 bool VisitStmt(Stmt *) {
682 assert(0 && "This should be called on integers, stmts are not integers");
683 return false;
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Chris Lattner32fea9d2008-11-12 07:43:42 +0000686 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000687 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattnerb542afe2008-07-11 19:10:17 +0000690 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000691
Chris Lattner4c4867e2008-07-12 00:38:25 +0000692 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000693 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000694 }
695 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000696 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000697 }
698 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000699 // Per gcc docs "this built-in function ignores top level
700 // qualifiers". We need to use the canonical version to properly
701 // be able to strip CRV qualifiers from the type.
702 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
703 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000704 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000705 T1.getUnqualifiedType()),
706 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000707 }
708 bool VisitDeclRefExpr(const DeclRefExpr *E);
709 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000710 bool VisitBinaryOperator(const BinaryOperator *E);
711 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000712 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000713
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000714 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000715 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
716
Anders Carlsson3068d112008-11-16 19:01:22 +0000717 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000718 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Anders Carlsson3f704562008-12-21 22:39:40 +0000721 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000722 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Anders Carlsson3068d112008-11-16 19:01:22 +0000725 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000726 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000727 }
728
Eli Friedman664a1042009-02-27 04:45:43 +0000729 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
730 return Success(0, E);
731 }
732
Sebastian Redl64b45f72009-01-05 20:52:13 +0000733 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000734 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000735 }
736
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000737 bool VisitChooseExpr(const ChooseExpr *E) {
738 return Visit(E->getChosenSubExpr(Info.Ctx));
739 }
740
Eli Friedman722c7172009-02-28 03:59:05 +0000741 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000742 bool VisitUnaryImag(const UnaryOperator *E);
743
Chris Lattnerfcee0012008-07-11 21:24:13 +0000744private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000745 unsigned GetAlignOfExpr(const Expr *E);
746 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000747 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000748};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000749} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000750
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000751static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000752 if (!E->getType()->isIntegralType())
753 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000755 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
756}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000757
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000758static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
759 APValue Val;
760 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
761 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000762 Result = Val.getInt();
763 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000764}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000765
Chris Lattner4c4867e2008-07-12 00:38:25 +0000766bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
767 // Enums are integer constant exprs.
768 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000769 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000770 // signedness consistently; see PR3173.
771 APSInt SI = D->getInitVal();
772 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
773 // FIXME: This is an ugly hack around the fact that enums don't
774 // set their width (!?!) consistently; see PR3173.
775 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
776 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000777 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000778
779 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000780 // In C, they can also be folded, although they are not ICEs.
781 if (E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000782 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000783 if (APValue *V = D->getEvaluatedValue())
784 return Success(V->getInt(), E);
785 if (const Expr *Init = D->getInit()) {
786 if (Visit(const_cast<Expr*>(Init))) {
787 // Cache the evaluated value in the variable declaration.
788 D->setEvaluatedValue(Info.Ctx, Result);
789 return true;
790 }
791
792 return false;
793 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000794 }
795 }
796
Chris Lattner4c4867e2008-07-12 00:38:25 +0000797 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000798 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000799}
800
Chris Lattnera4d55d82008-10-06 06:40:35 +0000801/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
802/// as GCC.
803static int EvaluateBuiltinClassifyType(const CallExpr *E) {
804 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000805 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000806 enum gcc_type_class {
807 no_type_class = -1,
808 void_type_class, integer_type_class, char_type_class,
809 enumeral_type_class, boolean_type_class,
810 pointer_type_class, reference_type_class, offset_type_class,
811 real_type_class, complex_type_class,
812 function_type_class, method_type_class,
813 record_type_class, union_type_class,
814 array_type_class, string_type_class,
815 lang_type_class
816 };
Mike Stump1eb44332009-09-09 15:08:12 +0000817
818 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000819 // ideal, however it is what gcc does.
820 if (E->getNumArgs() == 0)
821 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnera4d55d82008-10-06 06:40:35 +0000823 QualType ArgTy = E->getArg(0)->getType();
824 if (ArgTy->isVoidType())
825 return void_type_class;
826 else if (ArgTy->isEnumeralType())
827 return enumeral_type_class;
828 else if (ArgTy->isBooleanType())
829 return boolean_type_class;
830 else if (ArgTy->isCharType())
831 return string_type_class; // gcc doesn't appear to use char_type_class
832 else if (ArgTy->isIntegerType())
833 return integer_type_class;
834 else if (ArgTy->isPointerType())
835 return pointer_type_class;
836 else if (ArgTy->isReferenceType())
837 return reference_type_class;
838 else if (ArgTy->isRealType())
839 return real_type_class;
840 else if (ArgTy->isComplexType())
841 return complex_type_class;
842 else if (ArgTy->isFunctionType())
843 return function_type_class;
844 else if (ArgTy->isStructureType())
845 return record_type_class;
846 else if (ArgTy->isUnionType())
847 return union_type_class;
848 else if (ArgTy->isArrayType())
849 return array_type_class;
850 else if (ArgTy->isUnionType())
851 return union_type_class;
852 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
853 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
854 return -1;
855}
856
Chris Lattner4c4867e2008-07-12 00:38:25 +0000857bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000858 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000859 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000860 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000861 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000862 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000864 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000865 // __builtin_constant_p always has one operand: it returns true if that
866 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000867 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000868 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000869}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000870
Chris Lattnerb542afe2008-07-11 19:10:17 +0000871bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000872 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000873 if (!Visit(E->getRHS()))
874 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000875
Eli Friedman33ef1452009-02-26 10:19:36 +0000876 // If we can't evaluate the LHS, it might have side effects;
877 // conservatively mark it.
878 if (!E->getLHS()->isEvaluatable(Info.Ctx))
879 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000880
Anders Carlsson027f62e2008-12-01 02:07:06 +0000881 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000882 }
883
884 if (E->isLogicalOp()) {
885 // These need to be handled specially because the operands aren't
886 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000887 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000889 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000890 // We were able to evaluate the LHS, see if we can get away with not
891 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000892 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000893 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000894
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000895 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000896 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000897 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000898 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000899 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000900 }
901 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000902 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000903 // We can't evaluate the LHS; however, sometimes the result
904 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000905 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000906 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000907 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000908 // must have had side effects.
909 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000910
911 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000912 }
913 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000914 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000915
Eli Friedmana6afa762008-11-13 06:09:17 +0000916 return false;
917 }
918
Anders Carlsson286f85e2008-11-16 07:17:21 +0000919 QualType LHSTy = E->getLHS()->getType();
920 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000921
922 if (LHSTy->isAnyComplexType()) {
923 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
924 APValue LHS, RHS;
925
926 if (!EvaluateComplex(E->getLHS(), LHS, Info))
927 return false;
928
929 if (!EvaluateComplex(E->getRHS(), RHS, Info))
930 return false;
931
932 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000933 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000934 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000935 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000936 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
937
Daniel Dunbar4087e242009-01-29 06:43:41 +0000938 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000939 return Success((CR_r == APFloat::cmpEqual &&
940 CR_i == APFloat::cmpEqual), E);
941 else {
942 assert(E->getOpcode() == BinaryOperator::NE &&
943 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000944 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000945 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000946 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000947 CR_i == APFloat::cmpLessThan)), E);
948 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000949 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000950 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000951 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
952 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
953 else {
954 assert(E->getOpcode() == BinaryOperator::NE &&
955 "Invalid compex comparison.");
956 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
957 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
958 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000959 }
960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Anders Carlsson286f85e2008-11-16 07:17:21 +0000962 if (LHSTy->isRealFloatingType() &&
963 RHSTy->isRealFloatingType()) {
964 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Anders Carlsson286f85e2008-11-16 07:17:21 +0000966 if (!EvaluateFloat(E->getRHS(), RHS, Info))
967 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Anders Carlsson286f85e2008-11-16 07:17:21 +0000969 if (!EvaluateFloat(E->getLHS(), LHS, Info))
970 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Anders Carlsson286f85e2008-11-16 07:17:21 +0000972 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000973
Anders Carlsson286f85e2008-11-16 07:17:21 +0000974 switch (E->getOpcode()) {
975 default:
976 assert(0 && "Invalid binary operator!");
977 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000978 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000979 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000980 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000981 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000982 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000983 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000984 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +0000985 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000986 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000987 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000988 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +0000989 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +0000990 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000991 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000994 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
995 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000996 APValue LHSValue;
997 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
998 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000999
Anders Carlsson3068d112008-11-16 19:01:22 +00001000 APValue RHSValue;
1001 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1002 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001003
Eli Friedman5bc86102009-06-14 02:17:33 +00001004 // Reject any bases from the normal codepath; we special-case comparisons
1005 // to null.
1006 if (LHSValue.getLValueBase()) {
1007 if (!E->isEqualityOp())
1008 return false;
1009 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1010 return false;
1011 bool bres;
1012 if (!EvalPointerValueAsBool(LHSValue, bres))
1013 return false;
1014 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1015 } else if (RHSValue.getLValueBase()) {
1016 if (!E->isEqualityOp())
1017 return false;
1018 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1019 return false;
1020 bool bres;
1021 if (!EvalPointerValueAsBool(RHSValue, bres))
1022 return false;
1023 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1024 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001025
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001026 if (E->getOpcode() == BinaryOperator::Sub) {
1027 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001028 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001029
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001030 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001031 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1032 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001033
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001034 return Success(D, E);
1035 }
1036 bool Result;
1037 if (E->getOpcode() == BinaryOperator::EQ) {
1038 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001039 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001040 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1041 }
1042 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001043 }
1044 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001045 if (!LHSTy->isIntegralType() ||
1046 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001047 // We can't continue from here for non-integral types, and they
1048 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001049 return false;
1050 }
1051
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001052 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001053 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001054 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001055
Eli Friedman42edd0d2009-03-24 01:14:50 +00001056 APValue RHSVal;
1057 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001058 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001059
1060 // Handle cases like (unsigned long)&a + 4.
1061 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1062 uint64_t offset = Result.getLValueOffset();
1063 if (E->getOpcode() == BinaryOperator::Add)
1064 offset += RHSVal.getInt().getZExtValue();
1065 else
1066 offset -= RHSVal.getInt().getZExtValue();
1067 Result = APValue(Result.getLValueBase(), offset);
1068 return true;
1069 }
1070
1071 // Handle cases like 4 + (unsigned long)&a
1072 if (E->getOpcode() == BinaryOperator::Add &&
1073 RHSVal.isLValue() && Result.isInt()) {
1074 uint64_t offset = RHSVal.getLValueOffset();
1075 offset += Result.getInt().getZExtValue();
1076 Result = APValue(RHSVal.getLValueBase(), offset);
1077 return true;
1078 }
1079
1080 // All the following cases expect both operands to be an integer
1081 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001082 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001083
Eli Friedman42edd0d2009-03-24 01:14:50 +00001084 APSInt& RHS = RHSVal.getInt();
1085
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001086 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001087 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001088 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001089 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1090 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1091 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1092 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1093 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1094 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001095 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001096 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001097 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001098 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001099 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001100 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001101 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001102 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001103 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001104 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001105 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001106 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1107 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001108 }
1109 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001110 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001111 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1112 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001115 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1116 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1117 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1118 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1119 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1120 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001121 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001122}
1123
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001124bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001125 bool Cond;
1126 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001127 return false;
1128
Nuno Lopesa25bd552008-11-16 22:06:39 +00001129 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001130}
1131
Chris Lattneraf707ab2009-01-24 21:53:27 +00001132unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001133 // Get information about the alignment.
1134 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001135
Eli Friedman2be58612009-05-30 21:09:44 +00001136 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001137 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001138}
1139
Chris Lattneraf707ab2009-01-24 21:53:27 +00001140unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1141 E = E->IgnoreParens();
1142
1143 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001144 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001145 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001146 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001147
Chris Lattneraf707ab2009-01-24 21:53:27 +00001148 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001149 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001150
Chris Lattnere9feb472009-01-24 21:09:06 +00001151 return GetAlignOfType(E->getType());
1152}
1153
1154
Sebastian Redl05189992008-11-11 17:56:53 +00001155/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1156/// expression's type.
1157bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1158 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001159
Chris Lattnere9feb472009-01-24 21:09:06 +00001160 // Handle alignof separately.
1161 if (!E->isSizeOf()) {
1162 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001163 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001164 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001165 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001166 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001167
Sebastian Redl05189992008-11-11 17:56:53 +00001168 QualType SrcTy = E->getTypeOfArgument();
1169
Daniel Dunbar131eb432009-02-19 09:06:44 +00001170 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1171 // extension.
1172 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1173 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001174
Chris Lattnerfcee0012008-07-11 21:24:13 +00001175 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001176 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001177 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001178
Chris Lattnere9feb472009-01-24 21:09:06 +00001179 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001180 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001181 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001182}
1183
Chris Lattnerb542afe2008-07-11 19:10:17 +00001184bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001185 // Special case unary operators that do not need their subexpression
1186 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001187 if (E->isOffsetOfOp()) {
1188 // The AST for offsetof is defined in such a way that we can just
1189 // directly Evaluate it as an l-value.
1190 APValue LV;
1191 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1192 return false;
1193 if (LV.getLValueBase())
1194 return false;
1195 return Success(LV.getLValueOffset(), E);
1196 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001197
1198 if (E->getOpcode() == UnaryOperator::LNot) {
1199 // LNot's operand isn't necessarily an integer, so we handle it specially.
1200 bool bres;
1201 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1202 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001203 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001204 }
1205
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001206 // Only handle integral operations...
1207 if (!E->getSubExpr()->getType()->isIntegralType())
1208 return false;
1209
Chris Lattner87eae5e2008-07-11 22:52:41 +00001210 // Get the operand value into 'Result'.
1211 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001212 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001213
Chris Lattner75a48812008-07-11 22:15:16 +00001214 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001215 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001216 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1217 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001218 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001219 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001220 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1221 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001222 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001223 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001224 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001225 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001226 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001227 if (!Result.isInt()) return false;
1228 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001229 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001230 if (!Result.isInt()) return false;
1231 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001232 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001233}
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattner732b2232008-07-12 01:15:53 +00001235/// HandleCast - This is used to evaluate implicit or explicit casts where the
1236/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001237bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001238 Expr *SubExpr = E->getSubExpr();
1239 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001240 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001241
Eli Friedman4efaa272008-11-12 09:44:48 +00001242 if (DestType->isBooleanType()) {
1243 bool BoolResult;
1244 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1245 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001246 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001247 }
1248
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001249 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001250 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001251 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001252 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001253
Eli Friedmanbe265702009-02-20 01:15:07 +00001254 if (!Result.isInt()) {
1255 // Only allow casts of lvalues if they are lossless.
1256 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1257 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001258
Daniel Dunbardd211642009-02-19 22:24:01 +00001259 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001260 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001261 }
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Chris Lattner732b2232008-07-12 01:15:53 +00001263 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001264 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001265 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001266 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001267 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001268
Daniel Dunbardd211642009-02-19 22:24:01 +00001269 if (LV.getLValueBase()) {
1270 // Only allow based lvalue casts if they are lossless.
1271 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1272 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001273
Daniel Dunbardd211642009-02-19 22:24:01 +00001274 Result = LV;
1275 return true;
1276 }
1277
1278 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1279 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001280 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001281
Eli Friedmanbe265702009-02-20 01:15:07 +00001282 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1283 // This handles double-conversion cases, where there's both
1284 // an l-value promotion and an implicit conversion to int.
1285 APValue LV;
1286 if (!EvaluateLValue(SubExpr, LV, Info))
1287 return false;
1288
1289 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1290 return false;
1291
1292 Result = LV;
1293 return true;
1294 }
1295
Eli Friedman1725f682009-04-22 19:23:09 +00001296 if (SrcType->isAnyComplexType()) {
1297 APValue C;
1298 if (!EvaluateComplex(SubExpr, C, Info))
1299 return false;
1300 if (C.isComplexFloat())
1301 return Success(HandleFloatToIntCast(DestType, SrcType,
1302 C.getComplexFloatReal(), Info.Ctx),
1303 E);
1304 else
1305 return Success(HandleIntToIntCast(DestType, SrcType,
1306 C.getComplexIntReal(), Info.Ctx), E);
1307 }
Eli Friedman2217c872009-02-22 11:46:18 +00001308 // FIXME: Handle vectors
1309
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001310 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001311 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001312
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001313 APFloat F(0.0);
1314 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001315 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001317 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001318}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001319
Eli Friedman722c7172009-02-28 03:59:05 +00001320bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1321 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1322 APValue LV;
1323 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1324 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1325 return Success(LV.getComplexIntReal(), E);
1326 }
1327
1328 return Visit(E->getSubExpr());
1329}
1330
Eli Friedman664a1042009-02-27 04:45:43 +00001331bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001332 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1333 APValue LV;
1334 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1335 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1336 return Success(LV.getComplexIntImag(), E);
1337 }
1338
Eli Friedman664a1042009-02-27 04:45:43 +00001339 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1340 Info.EvalResult.HasSideEffects = true;
1341 return Success(0, E);
1342}
1343
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001344//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001345// Float Evaluation
1346//===----------------------------------------------------------------------===//
1347
1348namespace {
1349class VISIBILITY_HIDDEN FloatExprEvaluator
1350 : public StmtVisitor<FloatExprEvaluator, bool> {
1351 EvalInfo &Info;
1352 APFloat &Result;
1353public:
1354 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1355 : Info(info), Result(result) {}
1356
1357 bool VisitStmt(Stmt *S) {
1358 return false;
1359 }
1360
1361 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001362 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001363
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001364 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001365 bool VisitBinaryOperator(const BinaryOperator *E);
1366 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001367 bool VisitCastExpr(CastExpr *E);
1368 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001369
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001370 bool VisitChooseExpr(const ChooseExpr *E)
1371 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1372 bool VisitUnaryExtension(const UnaryOperator *E)
1373 { return Visit(E->getSubExpr()); }
1374
1375 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1376 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001377 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001378};
1379} // end anonymous namespace
1380
1381static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1382 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1383}
1384
Chris Lattner019f4e82008-10-06 05:28:25 +00001385bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001386 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001387 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001388 case Builtin::BI__builtin_huge_val:
1389 case Builtin::BI__builtin_huge_valf:
1390 case Builtin::BI__builtin_huge_vall:
1391 case Builtin::BI__builtin_inf:
1392 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001393 case Builtin::BI__builtin_infl: {
1394 const llvm::fltSemantics &Sem =
1395 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001396 Result = llvm::APFloat::getInf(Sem);
1397 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Chris Lattner9e621712008-10-06 06:31:58 +00001400 case Builtin::BI__builtin_nan:
1401 case Builtin::BI__builtin_nanf:
1402 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001403 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001404 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001405 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001406 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001407 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001408 const llvm::fltSemantics &Sem =
1409 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001410 llvm::SmallString<16> s;
1411 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1412 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001413 long l;
1414 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001415 l = strtol(&s[0], &endp, 0);
1416 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001417 return false;
1418 unsigned type = (unsigned int)l;;
1419 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001420 return true;
1421 }
1422 }
1423 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001424
1425 case Builtin::BI__builtin_fabs:
1426 case Builtin::BI__builtin_fabsf:
1427 case Builtin::BI__builtin_fabsl:
1428 if (!EvaluateFloat(E->getArg(0), Result, Info))
1429 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001431 if (Result.isNegative())
1432 Result.changeSign();
1433 return true;
1434
Mike Stump1eb44332009-09-09 15:08:12 +00001435 case Builtin::BI__builtin_copysign:
1436 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001437 case Builtin::BI__builtin_copysignl: {
1438 APFloat RHS(0.);
1439 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1440 !EvaluateFloat(E->getArg(1), RHS, Info))
1441 return false;
1442 Result.copySign(RHS);
1443 return true;
1444 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001445 }
1446}
1447
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001448bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001449 if (E->getOpcode() == UnaryOperator::Deref)
1450 return false;
1451
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001452 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1453 return false;
1454
1455 switch (E->getOpcode()) {
1456 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001457 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001458 return true;
1459 case UnaryOperator::Minus:
1460 Result.changeSign();
1461 return true;
1462 }
1463}
Chris Lattner019f4e82008-10-06 05:28:25 +00001464
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001465bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1466 // FIXME: Diagnostics? I really don't understand how the warnings
1467 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001468 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001469 if (!EvaluateFloat(E->getLHS(), Result, Info))
1470 return false;
1471 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1472 return false;
1473
1474 switch (E->getOpcode()) {
1475 default: return false;
1476 case BinaryOperator::Mul:
1477 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1478 return true;
1479 case BinaryOperator::Add:
1480 Result.add(RHS, APFloat::rmNearestTiesToEven);
1481 return true;
1482 case BinaryOperator::Sub:
1483 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1484 return true;
1485 case BinaryOperator::Div:
1486 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1487 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001488 }
1489}
1490
1491bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1492 Result = E->getValue();
1493 return true;
1494}
1495
Eli Friedman4efaa272008-11-12 09:44:48 +00001496bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1497 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Eli Friedman4efaa272008-11-12 09:44:48 +00001499 if (SubExpr->getType()->isIntegralType()) {
1500 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001501 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001502 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001503 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001504 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001505 return true;
1506 }
1507 if (SubExpr->getType()->isRealFloatingType()) {
1508 if (!Visit(SubExpr))
1509 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001510 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1511 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001512 return true;
1513 }
Eli Friedman2217c872009-02-22 11:46:18 +00001514 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001515
1516 return false;
1517}
1518
1519bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1520 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1521 return true;
1522}
1523
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001524//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001525// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001526//===----------------------------------------------------------------------===//
1527
1528namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001529class VISIBILITY_HIDDEN ComplexExprEvaluator
1530 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001531 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001533public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001534 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001536 //===--------------------------------------------------------------------===//
1537 // Visitor Methods
1538 //===--------------------------------------------------------------------===//
1539
1540 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001541 return APValue();
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001544 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1545
1546 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001547 Expr* SubExpr = E->getSubExpr();
1548
1549 if (SubExpr->getType()->isRealFloatingType()) {
1550 APFloat Result(0.0);
1551
1552 if (!EvaluateFloat(SubExpr, Result, Info))
1553 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001554
1555 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001556 Result);
1557 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001558 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001559 "Unexpected imaginary literal.");
1560
1561 llvm::APSInt Result;
1562 if (!EvaluateInteger(SubExpr, Result, Info))
1563 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001565 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1566 Zero = 0;
1567 return APValue(Zero, Result);
1568 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001569 }
1570
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001571 APValue VisitCastExpr(CastExpr *E) {
1572 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001573 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1574 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001575
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001576 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001577 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001578
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001579 if (!EvaluateFloat(SubExpr, Result, Info))
1580 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001581
1582 if (EltType->isRealFloatingType()) {
1583 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001584 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001585 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1586 } else {
1587 llvm::APSInt IResult;
1588 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1589 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1590 Zero = 0;
1591 return APValue(IResult, Zero);
1592 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001593 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001594 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001595
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001596 if (!EvaluateInteger(SubExpr, Result, Info))
1597 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001598
Eli Friedman1725f682009-04-22 19:23:09 +00001599 if (EltType->isRealFloatingType()) {
1600 APFloat FResult =
1601 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001602 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001603 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1604 } else {
1605 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1606 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1607 Zero = 0;
1608 return APValue(Result, Zero);
1609 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001610 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1611 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001612
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001613 if (!EvaluateComplex(SubExpr, Src, Info))
1614 return APValue();
1615
1616 QualType SrcType = CT->getElementType();
1617
1618 if (Src.isComplexFloat()) {
1619 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001620 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001621 Src.getComplexFloatReal(),
1622 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001623 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001624 Src.getComplexFloatImag(),
1625 Info.Ctx));
1626 } else {
1627 return APValue(HandleFloatToIntCast(EltType, SrcType,
1628 Src.getComplexFloatReal(),
1629 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001630 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001631 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001632 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001633 }
1634 } else {
1635 assert(Src.isComplexInt() && "Invalid evaluate result.");
1636 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001637 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001638 Src.getComplexIntReal(),
1639 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001640 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001641 Src.getComplexIntImag(),
1642 Info.Ctx));
1643 } else {
1644 return APValue(HandleIntToIntCast(EltType, SrcType,
1645 Src.getComplexIntReal(),
1646 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001647 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001648 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001649 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001650 }
1651 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001652 }
1653
1654 // FIXME: Handle more casts.
1655 return APValue();
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001658 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001659 APValue VisitChooseExpr(const ChooseExpr *E)
1660 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1661 APValue VisitUnaryExtension(const UnaryOperator *E)
1662 { return Visit(E->getSubExpr()); }
1663 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001664 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001665};
1666} // end anonymous namespace
1667
Mike Stump1eb44332009-09-09 15:08:12 +00001668static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001669 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1670 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001671 (&Result.getComplexFloatReal().getSemantics() ==
1672 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001673 "Invalid complex evaluation.");
1674 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001675}
1676
Mike Stump1eb44332009-09-09 15:08:12 +00001677APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001678 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001680 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001681 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001683 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001684 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001685
Daniel Dunbar3f279872009-01-29 01:32:56 +00001686 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1687 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001688 switch (E->getOpcode()) {
1689 default: return APValue();
1690 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001691 if (Result.isComplexFloat()) {
1692 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1693 APFloat::rmNearestTiesToEven);
1694 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1695 APFloat::rmNearestTiesToEven);
1696 } else {
1697 Result.getComplexIntReal() += RHS.getComplexIntReal();
1698 Result.getComplexIntImag() += RHS.getComplexIntImag();
1699 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001700 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001701 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001702 if (Result.isComplexFloat()) {
1703 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1704 APFloat::rmNearestTiesToEven);
1705 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1706 APFloat::rmNearestTiesToEven);
1707 } else {
1708 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1709 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1710 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001711 break;
1712 case BinaryOperator::Mul:
1713 if (Result.isComplexFloat()) {
1714 APValue LHS = Result;
1715 APFloat &LHS_r = LHS.getComplexFloatReal();
1716 APFloat &LHS_i = LHS.getComplexFloatImag();
1717 APFloat &RHS_r = RHS.getComplexFloatReal();
1718 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Daniel Dunbar3f279872009-01-29 01:32:56 +00001720 APFloat Tmp = LHS_r;
1721 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1722 Result.getComplexFloatReal() = Tmp;
1723 Tmp = LHS_i;
1724 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1725 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1726
1727 Tmp = LHS_r;
1728 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1729 Result.getComplexFloatImag() = Tmp;
1730 Tmp = LHS_i;
1731 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1732 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1733 } else {
1734 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001735 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001736 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1737 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001738 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001739 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1740 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1741 }
1742 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001743 }
1744
1745 return Result;
1746}
1747
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001748//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001749// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001750//===----------------------------------------------------------------------===//
1751
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001752/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001753/// any crazy technique (that has nothing to do with language standards) that
1754/// we want to. If this function returns true, it returns the folded constant
1755/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001756bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1757 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001758
Nate Begeman59b5da62009-01-18 03:20:47 +00001759 if (getType()->isVectorType()) {
1760 if (!EvaluateVector(this, Result.Val, Info))
1761 return false;
1762 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001763 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001764 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001765 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001766 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001767 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001768 } else if (getType()->isRealFloatingType()) {
1769 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001770 if (!EvaluateFloat(this, f, Info))
1771 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001773 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001774 } else if (getType()->isAnyComplexType()) {
1775 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001776 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001777 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001778 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001779
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001780 return true;
1781}
1782
Anders Carlsson1b782762009-04-10 04:54:13 +00001783bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1784 EvalInfo Info(Ctx, Result);
1785
1786 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1787}
1788
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001789/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001790/// folded, but discard the result.
1791bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001792 EvalResult Result;
1793 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001794}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001795
1796APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001797 EvalResult EvalResult;
1798 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001799 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001800 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001801 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001802
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001803 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001804}