blob: 5123aaa7ca79fd69b4be4495bca65ede17bbe6bc [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
Eli Friedmanb2f295c2009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000055};
56
57
Eli Friedman4efaa272008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman5bc86102009-06-14 02:17:33 +000069static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
70 // FIXME: Is this accurate for all kinds of bases? If not, what would
71 // the check look like?
72 Result = Value.getLValueBase() || Value.getLValueOffset();
73 return true;
74}
75
Eli Friedman4efaa272008-11-12 09:44:48 +000076static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
77 if (E->getType()->isIntegralType()) {
78 APSInt IntResult;
79 if (!EvaluateInteger(E, IntResult, Info))
80 return false;
81 Result = IntResult != 0;
82 return true;
83 } else if (E->getType()->isRealFloatingType()) {
84 APFloat FloatResult(0.0);
85 if (!EvaluateFloat(E, FloatResult, Info))
86 return false;
87 Result = !FloatResult.isZero();
88 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000094 } else if (E->getType()->isAnyComplexType()) {
95 APValue ComplexResult;
96 if (!EvaluateComplex(E, ComplexResult, Info))
97 return false;
98 if (ComplexResult.isComplexFloat()) {
99 Result = !ComplexResult.getComplexFloatReal().isZero() ||
100 !ComplexResult.getComplexFloatImag().isZero();
101 } else {
102 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
103 ComplexResult.getComplexIntImag().getBoolValue();
104 }
105 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000112 APFloat &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 // Determine whether we are converting to unsigned or signed.
115 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000117 // FIXME: Warning for overflow.
118 uint64_t Space[4];
119 bool ignored;
120 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
121 llvm::APFloat::rmTowardZero, &ignored);
122 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump1eb44332009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000135 APSInt &Value, ASTContext &Ctx) {
136 unsigned DestWidth = Ctx.getIntWidth(DestType);
137 APSInt Result = Value;
138 // Figure out if this is a truncate, extend or noop cast.
139 // If the input is signed, do a sign extend, noop, or truncate.
140 Result.extOrTrunc(DestWidth);
141 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
142 return Result;
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Eli Friedman4efaa272008-11-12 09:44:48 +0000154//===----------------------------------------------------------------------===//
155// LValue Evaluation
156//===----------------------------------------------------------------------===//
157namespace {
158class VISIBILITY_HIDDEN LValueExprEvaluator
159 : public StmtVisitor<LValueExprEvaluator, APValue> {
160 EvalInfo &Info;
161public:
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Eli Friedman4efaa272008-11-12 09:44:48 +0000163 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
164
165 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000166 return APValue();
167 }
168
169 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000170 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000171 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000172 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
173 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
174 APValue VisitMemberExpr(MemberExpr *E);
175 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000176 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000177 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000178 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000179 APValue VisitUnaryExtension(const UnaryOperator *E)
180 { return Visit(E->getSubExpr()); }
181 APValue VisitChooseExpr(const ChooseExpr *E)
182 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
183 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000184};
185} // end anonymous namespace
186
187static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
188 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
189 return Result.isLValue();
190}
191
Mike Stump1eb44332009-09-09 15:08:12 +0000192APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000193 if (isa<FunctionDecl>(E->getDecl())) {
194 return APValue(E, 0);
195 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000196 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000197 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000198 if (!VD->getType()->isReferenceType())
199 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000200 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000201 if (VD->getInit())
202 return Visit(VD->getInit());
203 }
204
205 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000209 if (E->hasBlockDeclRefExprs())
210 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Steve Naroff3aaa4822009-04-16 19:02:57 +0000212 return APValue(E, 0);
213}
214
Eli Friedman4efaa272008-11-12 09:44:48 +0000215APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000216 if (!Info.AnyLValue && !E->isFileScope())
217 return APValue();
218 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000219}
220
221APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
222 APValue result;
223 QualType Ty;
224 if (E->isArrow()) {
225 if (!EvaluatePointer(E->getBase(), result, Info))
226 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000227 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 } else {
229 result = Visit(E->getBase());
230 if (result.isUninit())
231 return APValue();
232 Ty = E->getBase()->getType();
233 }
234
Ted Kremenek6217b802009-07-29 21:53:49 +0000235 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000236 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000237
238 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
239 if (!FD) // FIXME: deal with other kinds of member expressions
240 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000241
242 if (FD->getType()->isReferenceType())
243 return APValue();
244
Eli Friedman4efaa272008-11-12 09:44:48 +0000245 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000246 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000247 for (RecordDecl::field_iterator Field = RD->field_begin(),
248 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000249 Field != FieldEnd; (void)++Field, ++i) {
250 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000251 break;
252 }
253
254 result.setLValue(result.getLValueBase(),
255 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
256
257 return result;
258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000261 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson3068d112008-11-16 19:01:22 +0000263 if (!EvaluatePointer(E->getBase(), Result, Info))
264 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Anders Carlsson3068d112008-11-16 19:01:22 +0000266 APSInt Index;
267 if (!EvaluateInteger(E->getIdx(), Index, Info))
268 return APValue();
269
270 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
271
272 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000274 Result.getLValueOffset() + Offset);
275 return Result;
276}
Eli Friedman4efaa272008-11-12 09:44:48 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000279 APValue Result;
280 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
281 return APValue();
282 return Result;
283}
284
Eli Friedman4efaa272008-11-12 09:44:48 +0000285//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000286// Pointer Evaluation
287//===----------------------------------------------------------------------===//
288
Anders Carlssonc754aa62008-07-08 05:13:58 +0000289namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000290class VISIBILITY_HIDDEN PointerExprEvaluator
291 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000292 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000293public:
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner87eae5e2008-07-11 22:52:41 +0000295 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000296
Anders Carlsson2bad1682008-07-08 14:30:00 +0000297 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000298 return APValue();
299 }
300
301 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
302
Anders Carlsson650c92f2008-07-08 15:34:11 +0000303 APValue VisitBinaryOperator(const BinaryOperator *E);
304 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000305 APValue VisitUnaryExtension(const UnaryOperator *E)
306 { return Visit(E->getSubExpr()); }
307 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000308 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
309 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000310 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
311 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000312 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000313 APValue VisitBlockExpr(BlockExpr *E) {
314 if (!E->hasBlockDeclRefExprs())
315 return APValue(E, 0);
316 return APValue();
317 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000318 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
319 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000320 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000321 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000322 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
323 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
324 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000325 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000326};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000327} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000328
Chris Lattner87eae5e2008-07-11 22:52:41 +0000329static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000330 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000331 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000332 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000333 return Result.isLValue();
334}
335
336APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
337 if (E->getOpcode() != BinaryOperator::Add &&
338 E->getOpcode() != BinaryOperator::Sub)
339 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 const Expr *PExp = E->getLHS();
342 const Expr *IExp = E->getRHS();
343 if (IExp->getType()->isPointerType())
344 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000346 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000347 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000348 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000350 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000351 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000352 return APValue();
353
Ted Kremenek6217b802009-07-29 21:53:49 +0000354 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000355 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000357 // Explicitly handle GNU void* and function pointer arithmetic extensions.
358 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
359 SizeOfPointee = 1;
360 else
361 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000362
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000363 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000364
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000365 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000366 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000367 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000368 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
369
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000370 return APValue(ResultLValue.getLValueBase(), Offset);
371}
Eli Friedman4efaa272008-11-12 09:44:48 +0000372
Eli Friedman2217c872009-02-22 11:46:18 +0000373APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
374 APValue result;
375 if (EvaluateLValue(E->getSubExpr(), result, Info))
376 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000377 return APValue();
378}
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000380
Chris Lattnerb542afe2008-07-11 19:10:17 +0000381APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000382 const Expr* SubExpr = E->getSubExpr();
383
384 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000385 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000386 SubExpr->getType()->isObjCObjectPointerType() ||
387 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000388 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000389 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000390 return Result;
391 return APValue();
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000394 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000395 APValue Result;
396 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
397 return APValue();
398
399 if (Result.isInt()) {
400 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
401 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000404 // Cast is of an lvalue, no need to change value.
405 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000406 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000407
408 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000409 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000410 SubExpr->getType()->isArrayType()) {
411 APValue Result;
412 if (EvaluateLValue(SubExpr, Result, Info))
413 return Result;
414 return APValue();
415 }
416
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000417 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000418}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000419
Eli Friedman3941b182009-01-25 01:54:01 +0000420APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000421 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000422 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000423 return APValue(E, 0);
424 return APValue();
425}
426
Eli Friedman4efaa272008-11-12 09:44:48 +0000427APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
428 bool BoolResult;
429 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
430 return APValue();
431
432 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
433
434 APValue Result;
435 if (EvaluatePointer(EvalExpr, Result, Info))
436 return Result;
437 return APValue();
438}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000439
440//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000441// Vector Evaluation
442//===----------------------------------------------------------------------===//
443
444namespace {
445 class VISIBILITY_HIDDEN VectorExprEvaluator
446 : public StmtVisitor<VectorExprEvaluator, APValue> {
447 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000448 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000449 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Nate Begeman59b5da62009-01-18 03:20:47 +0000451 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Nate Begeman59b5da62009-01-18 03:20:47 +0000453 APValue VisitStmt(Stmt *S) {
454 return APValue();
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Eli Friedman91110ee2009-02-23 04:23:56 +0000457 APValue VisitParenExpr(ParenExpr *E)
458 { return Visit(E->getSubExpr()); }
459 APValue VisitUnaryExtension(const UnaryOperator *E)
460 { return Visit(E->getSubExpr()); }
461 APValue VisitUnaryPlus(const UnaryOperator *E)
462 { return Visit(E->getSubExpr()); }
463 APValue VisitUnaryReal(const UnaryOperator *E)
464 { return Visit(E->getSubExpr()); }
465 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
466 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000467 APValue VisitCastExpr(const CastExpr* E);
468 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
469 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000470 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000471 APValue VisitChooseExpr(const ChooseExpr *E)
472 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000473 APValue VisitUnaryImag(const UnaryOperator *E);
474 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000475 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000476 // shufflevector, ExtVectorElementExpr
477 // (Note that these require implementing conversions
478 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000479 };
480} // end anonymous namespace
481
482static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
483 if (!E->getType()->isVectorType())
484 return false;
485 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
486 return !Result.isUninit();
487}
488
489APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000490 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000491 QualType EltTy = VTy->getElementType();
492 unsigned NElts = VTy->getNumElements();
493 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Nate Begeman59b5da62009-01-18 03:20:47 +0000495 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000496 QualType SETy = SE->getType();
497 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000498
Nate Begemane8c9e922009-06-26 18:22:18 +0000499 // Check for vector->vector bitcast and scalar->vector splat.
500 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000501 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000502 } else if (SETy->isIntegerType()) {
503 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000504 if (!EvaluateInteger(SE, IntResult, Info))
505 return APValue();
506 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000507 } else if (SETy->isRealFloatingType()) {
508 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000509 if (!EvaluateFloat(SE, F, Info))
510 return APValue();
511 Result = APValue(F);
512 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000513 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000514
Nate Begemanc0b8b192009-07-01 07:50:47 +0000515 // For casts of a scalar to ExtVector, convert the scalar to the element type
516 // and splat it to all elements.
517 if (E->getType()->isExtVectorType()) {
518 if (EltTy->isIntegerType() && Result.isInt())
519 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
520 Info.Ctx));
521 else if (EltTy->isIntegerType())
522 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
523 Info.Ctx));
524 else if (EltTy->isRealFloatingType() && Result.isInt())
525 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
526 Info.Ctx));
527 else if (EltTy->isRealFloatingType())
528 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
529 Info.Ctx));
530 else
531 return APValue();
532
533 // Splat and create vector APValue.
534 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
535 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000536 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000537
538 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
539 // to the vector. To construct the APValue vector initializer, bitcast the
540 // initializing value to an APInt, and shift out the bits pertaining to each
541 // element.
542 APSInt Init;
543 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Nate Begemanc0b8b192009-07-01 07:50:47 +0000545 llvm::SmallVector<APValue, 4> Elts;
546 for (unsigned i = 0; i != NElts; ++i) {
547 APSInt Tmp = Init;
548 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Nate Begemanc0b8b192009-07-01 07:50:47 +0000550 if (EltTy->isIntegerType())
551 Elts.push_back(APValue(Tmp));
552 else if (EltTy->isRealFloatingType())
553 Elts.push_back(APValue(APFloat(Tmp)));
554 else
555 return APValue();
556
557 Init >>= EltWidth;
558 }
559 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000560}
561
Mike Stump1eb44332009-09-09 15:08:12 +0000562APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000563VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
564 return this->Visit(const_cast<Expr*>(E->getInitializer()));
565}
566
Mike Stump1eb44332009-09-09 15:08:12 +0000567APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000568VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000569 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000570 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000571 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Nate Begeman59b5da62009-01-18 03:20:47 +0000573 QualType EltTy = VT->getElementType();
574 llvm::SmallVector<APValue, 4> Elements;
575
Eli Friedman91110ee2009-02-23 04:23:56 +0000576 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000577 if (EltTy->isIntegerType()) {
578 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000579 if (i < NumInits) {
580 if (!EvaluateInteger(E->getInit(i), sInt, Info))
581 return APValue();
582 } else {
583 sInt = Info.Ctx.MakeIntValue(0, EltTy);
584 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000585 Elements.push_back(APValue(sInt));
586 } else {
587 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000588 if (i < NumInits) {
589 if (!EvaluateFloat(E->getInit(i), f, Info))
590 return APValue();
591 } else {
592 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
593 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000594 Elements.push_back(APValue(f));
595 }
596 }
597 return APValue(&Elements[0], Elements.size());
598}
599
Mike Stump1eb44332009-09-09 15:08:12 +0000600APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000601VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000602 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000603 QualType EltTy = VT->getElementType();
604 APValue ZeroElement;
605 if (EltTy->isIntegerType())
606 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
607 else
608 ZeroElement =
609 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
610
611 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
612 return APValue(&Elements[0], Elements.size());
613}
614
615APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
616 bool BoolResult;
617 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
618 return APValue();
619
620 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
621
622 APValue Result;
623 if (EvaluateVector(EvalExpr, Result, Info))
624 return Result;
625 return APValue();
626}
627
Eli Friedman91110ee2009-02-23 04:23:56 +0000628APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
629 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
630 Info.EvalResult.HasSideEffects = true;
631 return GetZeroVector(E->getType());
632}
633
Nate Begeman59b5da62009-01-18 03:20:47 +0000634//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000635// Integer Evaluation
636//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000637
638namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000639class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000640 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000641 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000642 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000643public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000644 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000645 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000646
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000647 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000648 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000649 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000650 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000651 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000652 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000653 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000654 return true;
655 }
656
Daniel Dunbar131eb432009-02-19 09:06:44 +0000657 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000658 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000659 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000660 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000661 Result = APValue(APSInt(I));
662 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000663 return true;
664 }
665
666 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000667 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000668 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000669 return true;
670 }
671
Anders Carlsson82206e22008-11-30 18:14:57 +0000672 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000673 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000674 if (Info.EvalResult.Diag == 0) {
675 Info.EvalResult.DiagLoc = L;
676 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000677 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000678 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000679 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000680 }
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Anders Carlssonc754aa62008-07-08 05:13:58 +0000682 //===--------------------------------------------------------------------===//
683 // Visitor Methods
684 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Chris Lattner32fea9d2008-11-12 07:43:42 +0000686 bool VisitStmt(Stmt *) {
687 assert(0 && "This should be called on integers, stmts are not integers");
688 return false;
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattner32fea9d2008-11-12 07:43:42 +0000691 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000692 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattnerb542afe2008-07-11 19:10:17 +0000695 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000696
Chris Lattner4c4867e2008-07-12 00:38:25 +0000697 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000698 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000699 }
700 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000701 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000702 }
703 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000704 // Per gcc docs "this built-in function ignores top level
705 // qualifiers". We need to use the canonical version to properly
706 // be able to strip CRV qualifiers from the type.
707 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
708 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000709 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000710 T1.getUnqualifiedType()),
711 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000712 }
713 bool VisitDeclRefExpr(const DeclRefExpr *E);
714 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000715 bool VisitBinaryOperator(const BinaryOperator *E);
716 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000717 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000718
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000719 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000720 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
721
Anders Carlsson3068d112008-11-16 19:01:22 +0000722 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000723 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Anders Carlsson3f704562008-12-21 22:39:40 +0000726 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000727 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlsson3068d112008-11-16 19:01:22 +0000730 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000731 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000732 }
733
Eli Friedman664a1042009-02-27 04:45:43 +0000734 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
735 return Success(0, E);
736 }
737
Sebastian Redl64b45f72009-01-05 20:52:13 +0000738 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000739 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000740 }
741
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000742 bool VisitChooseExpr(const ChooseExpr *E) {
743 return Visit(E->getChosenSubExpr(Info.Ctx));
744 }
745
Eli Friedman722c7172009-02-28 03:59:05 +0000746 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000747 bool VisitUnaryImag(const UnaryOperator *E);
748
Chris Lattnerfcee0012008-07-11 21:24:13 +0000749private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000750 unsigned GetAlignOfExpr(const Expr *E);
751 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000752 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000753};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000754} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000755
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000756static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000757 if (!E->getType()->isIntegralType())
758 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000760 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
761}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000762
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000763static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
764 APValue Val;
765 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
766 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000767 Result = Val.getInt();
768 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000769}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000770
Chris Lattner4c4867e2008-07-12 00:38:25 +0000771bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
772 // Enums are integer constant exprs.
773 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000774 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000775 // signedness consistently; see PR3173.
776 APSInt SI = D->getInitVal();
777 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
778 // FIXME: This is an ugly hack around the fact that enums don't
779 // set their width (!?!) consistently; see PR3173.
780 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
781 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000782 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000783
784 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000785 // In C, they can also be folded, although they are not ICEs.
786 if (E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000787 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000788 if (APValue *V = D->getEvaluatedValue())
789 return Success(V->getInt(), E);
790 if (const Expr *Init = D->getInit()) {
791 if (Visit(const_cast<Expr*>(Init))) {
792 // Cache the evaluated value in the variable declaration.
793 D->setEvaluatedValue(Info.Ctx, Result);
794 return true;
795 }
796
797 return false;
798 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000799 }
800 }
801
Chris Lattner4c4867e2008-07-12 00:38:25 +0000802 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000803 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000804}
805
Chris Lattnera4d55d82008-10-06 06:40:35 +0000806/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
807/// as GCC.
808static int EvaluateBuiltinClassifyType(const CallExpr *E) {
809 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000810 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000811 enum gcc_type_class {
812 no_type_class = -1,
813 void_type_class, integer_type_class, char_type_class,
814 enumeral_type_class, boolean_type_class,
815 pointer_type_class, reference_type_class, offset_type_class,
816 real_type_class, complex_type_class,
817 function_type_class, method_type_class,
818 record_type_class, union_type_class,
819 array_type_class, string_type_class,
820 lang_type_class
821 };
Mike Stump1eb44332009-09-09 15:08:12 +0000822
823 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000824 // ideal, however it is what gcc does.
825 if (E->getNumArgs() == 0)
826 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattnera4d55d82008-10-06 06:40:35 +0000828 QualType ArgTy = E->getArg(0)->getType();
829 if (ArgTy->isVoidType())
830 return void_type_class;
831 else if (ArgTy->isEnumeralType())
832 return enumeral_type_class;
833 else if (ArgTy->isBooleanType())
834 return boolean_type_class;
835 else if (ArgTy->isCharType())
836 return string_type_class; // gcc doesn't appear to use char_type_class
837 else if (ArgTy->isIntegerType())
838 return integer_type_class;
839 else if (ArgTy->isPointerType())
840 return pointer_type_class;
841 else if (ArgTy->isReferenceType())
842 return reference_type_class;
843 else if (ArgTy->isRealType())
844 return real_type_class;
845 else if (ArgTy->isComplexType())
846 return complex_type_class;
847 else if (ArgTy->isFunctionType())
848 return function_type_class;
849 else if (ArgTy->isStructureType())
850 return record_type_class;
851 else if (ArgTy->isUnionType())
852 return union_type_class;
853 else if (ArgTy->isArrayType())
854 return array_type_class;
855 else if (ArgTy->isUnionType())
856 return union_type_class;
857 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
858 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
859 return -1;
860}
861
Chris Lattner4c4867e2008-07-12 00:38:25 +0000862bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000863 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000864 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000865 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000866 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000867 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000869 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000870 // __builtin_constant_p always has one operand: it returns true if that
871 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000872 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000873
874 case Builtin::BI__builtin_eh_return_data_regno: {
875 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
876 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
877 return Success(Operand, E);
878 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000879 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000880}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000881
Chris Lattnerb542afe2008-07-11 19:10:17 +0000882bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000883 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000884 if (!Visit(E->getRHS()))
885 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000886
Eli Friedman33ef1452009-02-26 10:19:36 +0000887 // If we can't evaluate the LHS, it might have side effects;
888 // conservatively mark it.
889 if (!E->getLHS()->isEvaluatable(Info.Ctx))
890 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000891
Anders Carlsson027f62e2008-12-01 02:07:06 +0000892 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000893 }
894
895 if (E->isLogicalOp()) {
896 // These need to be handled specially because the operands aren't
897 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000898 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000900 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000901 // We were able to evaluate the LHS, see if we can get away with not
902 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000903 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000904 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000905
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000906 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000907 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000908 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000909 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000910 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000911 }
912 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000913 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000914 // We can't evaluate the LHS; however, sometimes the result
915 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000916 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000917 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000918 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000919 // must have had side effects.
920 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000921
922 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000923 }
924 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000925 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000926
Eli Friedmana6afa762008-11-13 06:09:17 +0000927 return false;
928 }
929
Anders Carlsson286f85e2008-11-16 07:17:21 +0000930 QualType LHSTy = E->getLHS()->getType();
931 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000932
933 if (LHSTy->isAnyComplexType()) {
934 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
935 APValue LHS, RHS;
936
937 if (!EvaluateComplex(E->getLHS(), LHS, Info))
938 return false;
939
940 if (!EvaluateComplex(E->getRHS(), RHS, Info))
941 return false;
942
943 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000944 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000945 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000946 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000947 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
948
Daniel Dunbar4087e242009-01-29 06:43:41 +0000949 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000950 return Success((CR_r == APFloat::cmpEqual &&
951 CR_i == APFloat::cmpEqual), E);
952 else {
953 assert(E->getOpcode() == BinaryOperator::NE &&
954 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000955 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000956 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000957 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000958 CR_i == APFloat::cmpLessThan)), E);
959 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000960 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000961 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000962 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
963 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
964 else {
965 assert(E->getOpcode() == BinaryOperator::NE &&
966 "Invalid compex comparison.");
967 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
968 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
969 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000970 }
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Anders Carlsson286f85e2008-11-16 07:17:21 +0000973 if (LHSTy->isRealFloatingType() &&
974 RHSTy->isRealFloatingType()) {
975 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Anders Carlsson286f85e2008-11-16 07:17:21 +0000977 if (!EvaluateFloat(E->getRHS(), RHS, Info))
978 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Anders Carlsson286f85e2008-11-16 07:17:21 +0000980 if (!EvaluateFloat(E->getLHS(), LHS, Info))
981 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Anders Carlsson286f85e2008-11-16 07:17:21 +0000983 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000984
Anders Carlsson286f85e2008-11-16 07:17:21 +0000985 switch (E->getOpcode()) {
986 default:
987 assert(0 && "Invalid binary operator!");
988 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000989 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000990 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000991 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000992 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000993 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000994 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000995 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +0000996 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000997 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000998 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000999 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001000 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001001 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001002 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001005 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1006 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001007 APValue LHSValue;
1008 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1009 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001010
Anders Carlsson3068d112008-11-16 19:01:22 +00001011 APValue RHSValue;
1012 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1013 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001014
Eli Friedman5bc86102009-06-14 02:17:33 +00001015 // Reject any bases from the normal codepath; we special-case comparisons
1016 // to null.
1017 if (LHSValue.getLValueBase()) {
1018 if (!E->isEqualityOp())
1019 return false;
1020 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1021 return false;
1022 bool bres;
1023 if (!EvalPointerValueAsBool(LHSValue, bres))
1024 return false;
1025 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1026 } else if (RHSValue.getLValueBase()) {
1027 if (!E->isEqualityOp())
1028 return false;
1029 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1030 return false;
1031 bool bres;
1032 if (!EvalPointerValueAsBool(RHSValue, bres))
1033 return false;
1034 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1035 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001036
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001037 if (E->getOpcode() == BinaryOperator::Sub) {
1038 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001039 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001040
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001041 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001042 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1043 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001044
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001045 return Success(D, E);
1046 }
1047 bool Result;
1048 if (E->getOpcode() == BinaryOperator::EQ) {
1049 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001050 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001051 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1052 }
1053 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001054 }
1055 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001056 if (!LHSTy->isIntegralType() ||
1057 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001058 // We can't continue from here for non-integral types, and they
1059 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001060 return false;
1061 }
1062
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001063 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001064 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001065 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001066
Eli Friedman42edd0d2009-03-24 01:14:50 +00001067 APValue RHSVal;
1068 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001069 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001070
1071 // Handle cases like (unsigned long)&a + 4.
1072 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1073 uint64_t offset = Result.getLValueOffset();
1074 if (E->getOpcode() == BinaryOperator::Add)
1075 offset += RHSVal.getInt().getZExtValue();
1076 else
1077 offset -= RHSVal.getInt().getZExtValue();
1078 Result = APValue(Result.getLValueBase(), offset);
1079 return true;
1080 }
1081
1082 // Handle cases like 4 + (unsigned long)&a
1083 if (E->getOpcode() == BinaryOperator::Add &&
1084 RHSVal.isLValue() && Result.isInt()) {
1085 uint64_t offset = RHSVal.getLValueOffset();
1086 offset += Result.getInt().getZExtValue();
1087 Result = APValue(RHSVal.getLValueBase(), offset);
1088 return true;
1089 }
1090
1091 // All the following cases expect both operands to be an integer
1092 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001093 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001094
Eli Friedman42edd0d2009-03-24 01:14:50 +00001095 APSInt& RHS = RHSVal.getInt();
1096
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001097 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001098 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001099 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001100 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1101 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1102 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1103 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1104 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1105 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001106 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001107 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001108 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001109 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001110 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001111 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001112 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001113 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001114 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001115 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001116 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001117 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1118 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001119 }
1120 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001121 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001122 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1123 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001126 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1127 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1128 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1129 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1130 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1131 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001132 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001133}
1134
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001135bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001136 bool Cond;
1137 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001138 return false;
1139
Nuno Lopesa25bd552008-11-16 22:06:39 +00001140 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001141}
1142
Chris Lattneraf707ab2009-01-24 21:53:27 +00001143unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001144 // Get information about the alignment.
1145 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001146
Eli Friedman2be58612009-05-30 21:09:44 +00001147 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001148 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001149}
1150
Chris Lattneraf707ab2009-01-24 21:53:27 +00001151unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1152 E = E->IgnoreParens();
1153
1154 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001155 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001156 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001157 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001158
Chris Lattneraf707ab2009-01-24 21:53:27 +00001159 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001160 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001161
Chris Lattnere9feb472009-01-24 21:09:06 +00001162 return GetAlignOfType(E->getType());
1163}
1164
1165
Sebastian Redl05189992008-11-11 17:56:53 +00001166/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1167/// expression's type.
1168bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1169 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001170
Chris Lattnere9feb472009-01-24 21:09:06 +00001171 // Handle alignof separately.
1172 if (!E->isSizeOf()) {
1173 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001174 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001175 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001176 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001177 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001178
Sebastian Redl05189992008-11-11 17:56:53 +00001179 QualType SrcTy = E->getTypeOfArgument();
1180
Daniel Dunbar131eb432009-02-19 09:06:44 +00001181 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1182 // extension.
1183 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1184 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001185
Chris Lattnerfcee0012008-07-11 21:24:13 +00001186 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001187 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001188 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001189
Chris Lattnere9feb472009-01-24 21:09:06 +00001190 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001191 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001192 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001193}
1194
Chris Lattnerb542afe2008-07-11 19:10:17 +00001195bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001196 // Special case unary operators that do not need their subexpression
1197 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001198 if (E->isOffsetOfOp()) {
1199 // The AST for offsetof is defined in such a way that we can just
1200 // directly Evaluate it as an l-value.
1201 APValue LV;
1202 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1203 return false;
1204 if (LV.getLValueBase())
1205 return false;
1206 return Success(LV.getLValueOffset(), E);
1207 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001208
1209 if (E->getOpcode() == UnaryOperator::LNot) {
1210 // LNot's operand isn't necessarily an integer, so we handle it specially.
1211 bool bres;
1212 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1213 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001214 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001215 }
1216
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001217 // Only handle integral operations...
1218 if (!E->getSubExpr()->getType()->isIntegralType())
1219 return false;
1220
Chris Lattner87eae5e2008-07-11 22:52:41 +00001221 // Get the operand value into 'Result'.
1222 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001223 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001224
Chris Lattner75a48812008-07-11 22:15:16 +00001225 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001226 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001227 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1228 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001229 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001230 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001231 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1232 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001233 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001234 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001235 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001236 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001237 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001238 if (!Result.isInt()) return false;
1239 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001240 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001241 if (!Result.isInt()) return false;
1242 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001243 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001244}
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattner732b2232008-07-12 01:15:53 +00001246/// HandleCast - This is used to evaluate implicit or explicit casts where the
1247/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001248bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001249 Expr *SubExpr = E->getSubExpr();
1250 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001251 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001252
Eli Friedman4efaa272008-11-12 09:44:48 +00001253 if (DestType->isBooleanType()) {
1254 bool BoolResult;
1255 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1256 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001257 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001258 }
1259
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001260 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001261 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001262 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001263 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001264
Eli Friedmanbe265702009-02-20 01:15:07 +00001265 if (!Result.isInt()) {
1266 // Only allow casts of lvalues if they are lossless.
1267 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1268 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001269
Daniel Dunbardd211642009-02-19 22:24:01 +00001270 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001271 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattner732b2232008-07-12 01:15:53 +00001274 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001275 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001276 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001277 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001278 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001279
Daniel Dunbardd211642009-02-19 22:24:01 +00001280 if (LV.getLValueBase()) {
1281 // Only allow based lvalue casts if they are lossless.
1282 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1283 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001284
Daniel Dunbardd211642009-02-19 22:24:01 +00001285 Result = LV;
1286 return true;
1287 }
1288
1289 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1290 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001291 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001292
Eli Friedmanbe265702009-02-20 01:15:07 +00001293 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1294 // This handles double-conversion cases, where there's both
1295 // an l-value promotion and an implicit conversion to int.
1296 APValue LV;
1297 if (!EvaluateLValue(SubExpr, LV, Info))
1298 return false;
1299
1300 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1301 return false;
1302
1303 Result = LV;
1304 return true;
1305 }
1306
Eli Friedman1725f682009-04-22 19:23:09 +00001307 if (SrcType->isAnyComplexType()) {
1308 APValue C;
1309 if (!EvaluateComplex(SubExpr, C, Info))
1310 return false;
1311 if (C.isComplexFloat())
1312 return Success(HandleFloatToIntCast(DestType, SrcType,
1313 C.getComplexFloatReal(), Info.Ctx),
1314 E);
1315 else
1316 return Success(HandleIntToIntCast(DestType, SrcType,
1317 C.getComplexIntReal(), Info.Ctx), E);
1318 }
Eli Friedman2217c872009-02-22 11:46:18 +00001319 // FIXME: Handle vectors
1320
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001321 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001322 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001323
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001324 APFloat F(0.0);
1325 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001326 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001328 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001329}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001330
Eli Friedman722c7172009-02-28 03:59:05 +00001331bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1332 if (E->getSubExpr()->getType()->isAnyComplexType()) {
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.getComplexIntReal(), E);
1337 }
1338
1339 return Visit(E->getSubExpr());
1340}
1341
Eli Friedman664a1042009-02-27 04:45:43 +00001342bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001343 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1344 APValue LV;
1345 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1346 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1347 return Success(LV.getComplexIntImag(), E);
1348 }
1349
Eli Friedman664a1042009-02-27 04:45:43 +00001350 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1351 Info.EvalResult.HasSideEffects = true;
1352 return Success(0, E);
1353}
1354
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001355//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001356// Float Evaluation
1357//===----------------------------------------------------------------------===//
1358
1359namespace {
1360class VISIBILITY_HIDDEN FloatExprEvaluator
1361 : public StmtVisitor<FloatExprEvaluator, bool> {
1362 EvalInfo &Info;
1363 APFloat &Result;
1364public:
1365 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1366 : Info(info), Result(result) {}
1367
1368 bool VisitStmt(Stmt *S) {
1369 return false;
1370 }
1371
1372 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001373 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001374
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001375 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001376 bool VisitBinaryOperator(const BinaryOperator *E);
1377 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001378 bool VisitCastExpr(CastExpr *E);
1379 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001380
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001381 bool VisitChooseExpr(const ChooseExpr *E)
1382 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1383 bool VisitUnaryExtension(const UnaryOperator *E)
1384 { return Visit(E->getSubExpr()); }
1385
1386 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1387 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001388 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001389};
1390} // end anonymous namespace
1391
1392static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1393 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1394}
1395
Chris Lattner019f4e82008-10-06 05:28:25 +00001396bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001397 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001398 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001399 case Builtin::BI__builtin_huge_val:
1400 case Builtin::BI__builtin_huge_valf:
1401 case Builtin::BI__builtin_huge_vall:
1402 case Builtin::BI__builtin_inf:
1403 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001404 case Builtin::BI__builtin_infl: {
1405 const llvm::fltSemantics &Sem =
1406 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001407 Result = llvm::APFloat::getInf(Sem);
1408 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Chris Lattner9e621712008-10-06 06:31:58 +00001411 case Builtin::BI__builtin_nan:
1412 case Builtin::BI__builtin_nanf:
1413 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001414 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001415 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001416 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001417 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001418 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001419 const llvm::fltSemantics &Sem =
1420 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001421 llvm::SmallString<16> s;
1422 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1423 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001424 long l;
1425 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001426 l = strtol(&s[0], &endp, 0);
1427 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001428 return false;
1429 unsigned type = (unsigned int)l;;
1430 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001431 return true;
1432 }
1433 }
1434 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001435
1436 case Builtin::BI__builtin_fabs:
1437 case Builtin::BI__builtin_fabsf:
1438 case Builtin::BI__builtin_fabsl:
1439 if (!EvaluateFloat(E->getArg(0), Result, Info))
1440 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001442 if (Result.isNegative())
1443 Result.changeSign();
1444 return true;
1445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 case Builtin::BI__builtin_copysign:
1447 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001448 case Builtin::BI__builtin_copysignl: {
1449 APFloat RHS(0.);
1450 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1451 !EvaluateFloat(E->getArg(1), RHS, Info))
1452 return false;
1453 Result.copySign(RHS);
1454 return true;
1455 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001456 }
1457}
1458
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001459bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001460 if (E->getOpcode() == UnaryOperator::Deref)
1461 return false;
1462
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001463 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1464 return false;
1465
1466 switch (E->getOpcode()) {
1467 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001468 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001469 return true;
1470 case UnaryOperator::Minus:
1471 Result.changeSign();
1472 return true;
1473 }
1474}
Chris Lattner019f4e82008-10-06 05:28:25 +00001475
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001476bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1477 // FIXME: Diagnostics? I really don't understand how the warnings
1478 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001479 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001480 if (!EvaluateFloat(E->getLHS(), Result, Info))
1481 return false;
1482 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1483 return false;
1484
1485 switch (E->getOpcode()) {
1486 default: return false;
1487 case BinaryOperator::Mul:
1488 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1489 return true;
1490 case BinaryOperator::Add:
1491 Result.add(RHS, APFloat::rmNearestTiesToEven);
1492 return true;
1493 case BinaryOperator::Sub:
1494 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1495 return true;
1496 case BinaryOperator::Div:
1497 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1498 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001499 }
1500}
1501
1502bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1503 Result = E->getValue();
1504 return true;
1505}
1506
Eli Friedman4efaa272008-11-12 09:44:48 +00001507bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1508 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Eli Friedman4efaa272008-11-12 09:44:48 +00001510 if (SubExpr->getType()->isIntegralType()) {
1511 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001512 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001513 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001514 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001515 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001516 return true;
1517 }
1518 if (SubExpr->getType()->isRealFloatingType()) {
1519 if (!Visit(SubExpr))
1520 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001521 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1522 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001523 return true;
1524 }
Eli Friedman2217c872009-02-22 11:46:18 +00001525 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001526
1527 return false;
1528}
1529
1530bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1531 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1532 return true;
1533}
1534
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001535//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001536// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001537//===----------------------------------------------------------------------===//
1538
1539namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001540class VISIBILITY_HIDDEN ComplexExprEvaluator
1541 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001542 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001544public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001545 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001547 //===--------------------------------------------------------------------===//
1548 // Visitor Methods
1549 //===--------------------------------------------------------------------===//
1550
1551 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001552 return APValue();
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001555 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1556
1557 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001558 Expr* SubExpr = E->getSubExpr();
1559
1560 if (SubExpr->getType()->isRealFloatingType()) {
1561 APFloat Result(0.0);
1562
1563 if (!EvaluateFloat(SubExpr, Result, Info))
1564 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001565
1566 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001567 Result);
1568 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001569 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001570 "Unexpected imaginary literal.");
1571
1572 llvm::APSInt Result;
1573 if (!EvaluateInteger(SubExpr, Result, Info))
1574 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001576 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1577 Zero = 0;
1578 return APValue(Zero, Result);
1579 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001580 }
1581
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001582 APValue VisitCastExpr(CastExpr *E) {
1583 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001584 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001585 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001586
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001587 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001588 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001589
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001590 if (!EvaluateFloat(SubExpr, Result, Info))
1591 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001592
1593 if (EltType->isRealFloatingType()) {
1594 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001595 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001596 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1597 } else {
1598 llvm::APSInt IResult;
1599 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1600 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1601 Zero = 0;
1602 return APValue(IResult, Zero);
1603 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001604 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001605 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001606
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001607 if (!EvaluateInteger(SubExpr, Result, Info))
1608 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001609
Eli Friedman1725f682009-04-22 19:23:09 +00001610 if (EltType->isRealFloatingType()) {
1611 APFloat FResult =
1612 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001613 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001614 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1615 } else {
1616 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1617 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1618 Zero = 0;
1619 return APValue(Result, Zero);
1620 }
John McCall183700f2009-09-21 23:43:11 +00001621 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001622 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001623
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001624 if (!EvaluateComplex(SubExpr, Src, Info))
1625 return APValue();
1626
1627 QualType SrcType = CT->getElementType();
1628
1629 if (Src.isComplexFloat()) {
1630 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001631 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001632 Src.getComplexFloatReal(),
1633 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001634 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001635 Src.getComplexFloatImag(),
1636 Info.Ctx));
1637 } else {
1638 return APValue(HandleFloatToIntCast(EltType, SrcType,
1639 Src.getComplexFloatReal(),
1640 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001641 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001642 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001643 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001644 }
1645 } else {
1646 assert(Src.isComplexInt() && "Invalid evaluate result.");
1647 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001648 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001649 Src.getComplexIntReal(),
1650 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001651 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001652 Src.getComplexIntImag(),
1653 Info.Ctx));
1654 } else {
1655 return APValue(HandleIntToIntCast(EltType, SrcType,
1656 Src.getComplexIntReal(),
1657 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001658 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001659 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001660 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001661 }
1662 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001663 }
1664
1665 // FIXME: Handle more casts.
1666 return APValue();
1667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001669 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001670 APValue VisitChooseExpr(const ChooseExpr *E)
1671 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1672 APValue VisitUnaryExtension(const UnaryOperator *E)
1673 { return Visit(E->getSubExpr()); }
1674 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001675 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001676};
1677} // end anonymous namespace
1678
Mike Stump1eb44332009-09-09 15:08:12 +00001679static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001680 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1681 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001682 (&Result.getComplexFloatReal().getSemantics() ==
1683 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001684 "Invalid complex evaluation.");
1685 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001686}
1687
Mike Stump1eb44332009-09-09 15:08:12 +00001688APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001689 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001691 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001692 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001694 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001695 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001696
Daniel Dunbar3f279872009-01-29 01:32:56 +00001697 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1698 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001699 switch (E->getOpcode()) {
1700 default: return APValue();
1701 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001702 if (Result.isComplexFloat()) {
1703 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1704 APFloat::rmNearestTiesToEven);
1705 Result.getComplexFloatImag().add(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;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001712 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001713 if (Result.isComplexFloat()) {
1714 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1715 APFloat::rmNearestTiesToEven);
1716 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1717 APFloat::rmNearestTiesToEven);
1718 } else {
1719 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1720 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1721 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001722 break;
1723 case BinaryOperator::Mul:
1724 if (Result.isComplexFloat()) {
1725 APValue LHS = Result;
1726 APFloat &LHS_r = LHS.getComplexFloatReal();
1727 APFloat &LHS_i = LHS.getComplexFloatImag();
1728 APFloat &RHS_r = RHS.getComplexFloatReal();
1729 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Daniel Dunbar3f279872009-01-29 01:32:56 +00001731 APFloat Tmp = LHS_r;
1732 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1733 Result.getComplexFloatReal() = Tmp;
1734 Tmp = LHS_i;
1735 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1736 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1737
1738 Tmp = LHS_r;
1739 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1740 Result.getComplexFloatImag() = Tmp;
1741 Tmp = LHS_i;
1742 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1743 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1744 } else {
1745 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001746 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001747 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1748 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001749 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001750 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1751 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1752 }
1753 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001754 }
1755
1756 return Result;
1757}
1758
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001759//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001760// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001761//===----------------------------------------------------------------------===//
1762
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001763/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001764/// any crazy technique (that has nothing to do with language standards) that
1765/// we want to. If this function returns true, it returns the folded constant
1766/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001767bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1768 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001769
Nate Begeman59b5da62009-01-18 03:20:47 +00001770 if (getType()->isVectorType()) {
1771 if (!EvaluateVector(this, Result.Val, Info))
1772 return false;
1773 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001774 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001775 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001776 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001777 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001778 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001779 } else if (getType()->isRealFloatingType()) {
1780 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001781 if (!EvaluateFloat(this, f, Info))
1782 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001784 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001785 } else if (getType()->isAnyComplexType()) {
1786 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001787 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001788 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001789 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001790
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001791 return true;
1792}
1793
Anders Carlsson1b782762009-04-10 04:54:13 +00001794bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1795 EvalInfo Info(Ctx, Result);
1796
1797 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1798}
1799
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001800bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1801 EvalInfo Info(Ctx, Result, true);
1802
1803 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1804}
1805
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001806/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001807/// folded, but discard the result.
1808bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001809 EvalResult Result;
1810 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001811}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001812
1813APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001814 EvalResult EvalResult;
1815 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001816 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001817 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001818 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001819
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001820 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001821}