blob: 9c34547db1adc04b7dfd5cb6ef878c1f3aabdd1e [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 Lattner019f4e82008-10-06 05:28:25 +0000873 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000874}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000875
Chris Lattnerb542afe2008-07-11 19:10:17 +0000876bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000877 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000878 if (!Visit(E->getRHS()))
879 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000880
Eli Friedman33ef1452009-02-26 10:19:36 +0000881 // If we can't evaluate the LHS, it might have side effects;
882 // conservatively mark it.
883 if (!E->getLHS()->isEvaluatable(Info.Ctx))
884 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000885
Anders Carlsson027f62e2008-12-01 02:07:06 +0000886 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000887 }
888
889 if (E->isLogicalOp()) {
890 // These need to be handled specially because the operands aren't
891 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000892 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000894 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000895 // We were able to evaluate the LHS, see if we can get away with not
896 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000897 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000898 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000899
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000900 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000901 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000902 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000903 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000904 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000905 }
906 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000907 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000908 // We can't evaluate the LHS; however, sometimes the result
909 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000910 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000911 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000912 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000913 // must have had side effects.
914 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000915
916 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000917 }
918 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000919 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000920
Eli Friedmana6afa762008-11-13 06:09:17 +0000921 return false;
922 }
923
Anders Carlsson286f85e2008-11-16 07:17:21 +0000924 QualType LHSTy = E->getLHS()->getType();
925 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000926
927 if (LHSTy->isAnyComplexType()) {
928 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
929 APValue LHS, RHS;
930
931 if (!EvaluateComplex(E->getLHS(), LHS, Info))
932 return false;
933
934 if (!EvaluateComplex(E->getRHS(), RHS, Info))
935 return false;
936
937 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000938 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000939 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000940 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000941 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
942
Daniel Dunbar4087e242009-01-29 06:43:41 +0000943 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000944 return Success((CR_r == APFloat::cmpEqual &&
945 CR_i == APFloat::cmpEqual), E);
946 else {
947 assert(E->getOpcode() == BinaryOperator::NE &&
948 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000949 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000950 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000951 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000952 CR_i == APFloat::cmpLessThan)), E);
953 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000954 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000955 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000956 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
957 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
958 else {
959 assert(E->getOpcode() == BinaryOperator::NE &&
960 "Invalid compex comparison.");
961 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
962 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
963 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000964 }
965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Anders Carlsson286f85e2008-11-16 07:17:21 +0000967 if (LHSTy->isRealFloatingType() &&
968 RHSTy->isRealFloatingType()) {
969 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Anders Carlsson286f85e2008-11-16 07:17:21 +0000971 if (!EvaluateFloat(E->getRHS(), RHS, Info))
972 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Anders Carlsson286f85e2008-11-16 07:17:21 +0000974 if (!EvaluateFloat(E->getLHS(), LHS, Info))
975 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Anders Carlsson286f85e2008-11-16 07:17:21 +0000977 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000978
Anders Carlsson286f85e2008-11-16 07:17:21 +0000979 switch (E->getOpcode()) {
980 default:
981 assert(0 && "Invalid binary operator!");
982 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000983 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000984 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000985 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000986 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000987 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000988 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000989 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +0000990 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000991 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000992 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000993 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +0000994 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +0000995 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000996 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000999 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1000 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001001 APValue LHSValue;
1002 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1003 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001004
Anders Carlsson3068d112008-11-16 19:01:22 +00001005 APValue RHSValue;
1006 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1007 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001008
Eli Friedman5bc86102009-06-14 02:17:33 +00001009 // Reject any bases from the normal codepath; we special-case comparisons
1010 // to null.
1011 if (LHSValue.getLValueBase()) {
1012 if (!E->isEqualityOp())
1013 return false;
1014 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1015 return false;
1016 bool bres;
1017 if (!EvalPointerValueAsBool(LHSValue, bres))
1018 return false;
1019 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1020 } else if (RHSValue.getLValueBase()) {
1021 if (!E->isEqualityOp())
1022 return false;
1023 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1024 return false;
1025 bool bres;
1026 if (!EvalPointerValueAsBool(RHSValue, bres))
1027 return false;
1028 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1029 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001030
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001031 if (E->getOpcode() == BinaryOperator::Sub) {
1032 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001033 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001034
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001035 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001036 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1037 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001038
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001039 return Success(D, E);
1040 }
1041 bool Result;
1042 if (E->getOpcode() == BinaryOperator::EQ) {
1043 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001044 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001045 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1046 }
1047 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001048 }
1049 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001050 if (!LHSTy->isIntegralType() ||
1051 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001052 // We can't continue from here for non-integral types, and they
1053 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001054 return false;
1055 }
1056
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001057 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001058 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001059 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001060
Eli Friedman42edd0d2009-03-24 01:14:50 +00001061 APValue RHSVal;
1062 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001063 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001064
1065 // Handle cases like (unsigned long)&a + 4.
1066 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1067 uint64_t offset = Result.getLValueOffset();
1068 if (E->getOpcode() == BinaryOperator::Add)
1069 offset += RHSVal.getInt().getZExtValue();
1070 else
1071 offset -= RHSVal.getInt().getZExtValue();
1072 Result = APValue(Result.getLValueBase(), offset);
1073 return true;
1074 }
1075
1076 // Handle cases like 4 + (unsigned long)&a
1077 if (E->getOpcode() == BinaryOperator::Add &&
1078 RHSVal.isLValue() && Result.isInt()) {
1079 uint64_t offset = RHSVal.getLValueOffset();
1080 offset += Result.getInt().getZExtValue();
1081 Result = APValue(RHSVal.getLValueBase(), offset);
1082 return true;
1083 }
1084
1085 // All the following cases expect both operands to be an integer
1086 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001087 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001088
Eli Friedman42edd0d2009-03-24 01:14:50 +00001089 APSInt& RHS = RHSVal.getInt();
1090
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001091 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001092 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001093 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001094 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1095 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1096 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1097 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1098 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1099 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001100 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001101 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001102 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001103 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001104 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001105 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001106 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001107 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001108 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001109 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001110 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001111 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1112 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001113 }
1114 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001115 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001116 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1117 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001120 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1121 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1122 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1123 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1124 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1125 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001126 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001127}
1128
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001129bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001130 bool Cond;
1131 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001132 return false;
1133
Nuno Lopesa25bd552008-11-16 22:06:39 +00001134 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001135}
1136
Chris Lattneraf707ab2009-01-24 21:53:27 +00001137unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001138 // Get information about the alignment.
1139 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001140
Eli Friedman2be58612009-05-30 21:09:44 +00001141 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001142 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001143}
1144
Chris Lattneraf707ab2009-01-24 21:53:27 +00001145unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1146 E = E->IgnoreParens();
1147
1148 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001149 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001150 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001151 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001152
Chris Lattneraf707ab2009-01-24 21:53:27 +00001153 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001154 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001155
Chris Lattnere9feb472009-01-24 21:09:06 +00001156 return GetAlignOfType(E->getType());
1157}
1158
1159
Sebastian Redl05189992008-11-11 17:56:53 +00001160/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1161/// expression's type.
1162bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1163 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001164
Chris Lattnere9feb472009-01-24 21:09:06 +00001165 // Handle alignof separately.
1166 if (!E->isSizeOf()) {
1167 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001168 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001169 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001170 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001171 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001172
Sebastian Redl05189992008-11-11 17:56:53 +00001173 QualType SrcTy = E->getTypeOfArgument();
1174
Daniel Dunbar131eb432009-02-19 09:06:44 +00001175 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1176 // extension.
1177 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1178 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001179
Chris Lattnerfcee0012008-07-11 21:24:13 +00001180 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001181 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001182 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001183
Chris Lattnere9feb472009-01-24 21:09:06 +00001184 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001185 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001186 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001187}
1188
Chris Lattnerb542afe2008-07-11 19:10:17 +00001189bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001190 // Special case unary operators that do not need their subexpression
1191 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001192 if (E->isOffsetOfOp()) {
1193 // The AST for offsetof is defined in such a way that we can just
1194 // directly Evaluate it as an l-value.
1195 APValue LV;
1196 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1197 return false;
1198 if (LV.getLValueBase())
1199 return false;
1200 return Success(LV.getLValueOffset(), E);
1201 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001202
1203 if (E->getOpcode() == UnaryOperator::LNot) {
1204 // LNot's operand isn't necessarily an integer, so we handle it specially.
1205 bool bres;
1206 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1207 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001208 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001209 }
1210
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001211 // Only handle integral operations...
1212 if (!E->getSubExpr()->getType()->isIntegralType())
1213 return false;
1214
Chris Lattner87eae5e2008-07-11 22:52:41 +00001215 // Get the operand value into 'Result'.
1216 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001217 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001218
Chris Lattner75a48812008-07-11 22:15:16 +00001219 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001220 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001221 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1222 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001223 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001224 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001225 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1226 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001227 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001228 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001229 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001230 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001231 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001232 if (!Result.isInt()) return false;
1233 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001234 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001235 if (!Result.isInt()) return false;
1236 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001237 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001238}
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Chris Lattner732b2232008-07-12 01:15:53 +00001240/// HandleCast - This is used to evaluate implicit or explicit casts where the
1241/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001242bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001243 Expr *SubExpr = E->getSubExpr();
1244 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001245 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001246
Eli Friedman4efaa272008-11-12 09:44:48 +00001247 if (DestType->isBooleanType()) {
1248 bool BoolResult;
1249 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1250 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001251 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001252 }
1253
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001254 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001255 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001256 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001257 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001258
Eli Friedmanbe265702009-02-20 01:15:07 +00001259 if (!Result.isInt()) {
1260 // Only allow casts of lvalues if they are lossless.
1261 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1262 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001263
Daniel Dunbardd211642009-02-19 22:24:01 +00001264 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001265 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Chris Lattner732b2232008-07-12 01:15:53 +00001268 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001269 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001270 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001271 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001272 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001273
Daniel Dunbardd211642009-02-19 22:24:01 +00001274 if (LV.getLValueBase()) {
1275 // Only allow based lvalue casts if they are lossless.
1276 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1277 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001278
Daniel Dunbardd211642009-02-19 22:24:01 +00001279 Result = LV;
1280 return true;
1281 }
1282
1283 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1284 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001285 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001286
Eli Friedmanbe265702009-02-20 01:15:07 +00001287 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1288 // This handles double-conversion cases, where there's both
1289 // an l-value promotion and an implicit conversion to int.
1290 APValue LV;
1291 if (!EvaluateLValue(SubExpr, LV, Info))
1292 return false;
1293
1294 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1295 return false;
1296
1297 Result = LV;
1298 return true;
1299 }
1300
Eli Friedman1725f682009-04-22 19:23:09 +00001301 if (SrcType->isAnyComplexType()) {
1302 APValue C;
1303 if (!EvaluateComplex(SubExpr, C, Info))
1304 return false;
1305 if (C.isComplexFloat())
1306 return Success(HandleFloatToIntCast(DestType, SrcType,
1307 C.getComplexFloatReal(), Info.Ctx),
1308 E);
1309 else
1310 return Success(HandleIntToIntCast(DestType, SrcType,
1311 C.getComplexIntReal(), Info.Ctx), E);
1312 }
Eli Friedman2217c872009-02-22 11:46:18 +00001313 // FIXME: Handle vectors
1314
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001315 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001316 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001317
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001318 APFloat F(0.0);
1319 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001320 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001322 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001323}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001324
Eli Friedman722c7172009-02-28 03:59:05 +00001325bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1326 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1327 APValue LV;
1328 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1329 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1330 return Success(LV.getComplexIntReal(), E);
1331 }
1332
1333 return Visit(E->getSubExpr());
1334}
1335
Eli Friedman664a1042009-02-27 04:45:43 +00001336bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001337 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1338 APValue LV;
1339 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1340 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1341 return Success(LV.getComplexIntImag(), E);
1342 }
1343
Eli Friedman664a1042009-02-27 04:45:43 +00001344 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1345 Info.EvalResult.HasSideEffects = true;
1346 return Success(0, E);
1347}
1348
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001349//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001350// Float Evaluation
1351//===----------------------------------------------------------------------===//
1352
1353namespace {
1354class VISIBILITY_HIDDEN FloatExprEvaluator
1355 : public StmtVisitor<FloatExprEvaluator, bool> {
1356 EvalInfo &Info;
1357 APFloat &Result;
1358public:
1359 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1360 : Info(info), Result(result) {}
1361
1362 bool VisitStmt(Stmt *S) {
1363 return false;
1364 }
1365
1366 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001367 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001368
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001369 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001370 bool VisitBinaryOperator(const BinaryOperator *E);
1371 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001372 bool VisitCastExpr(CastExpr *E);
1373 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001374
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001375 bool VisitChooseExpr(const ChooseExpr *E)
1376 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1377 bool VisitUnaryExtension(const UnaryOperator *E)
1378 { return Visit(E->getSubExpr()); }
1379
1380 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1381 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001382 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001383};
1384} // end anonymous namespace
1385
1386static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1387 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1388}
1389
Chris Lattner019f4e82008-10-06 05:28:25 +00001390bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001391 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001392 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001393 case Builtin::BI__builtin_huge_val:
1394 case Builtin::BI__builtin_huge_valf:
1395 case Builtin::BI__builtin_huge_vall:
1396 case Builtin::BI__builtin_inf:
1397 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001398 case Builtin::BI__builtin_infl: {
1399 const llvm::fltSemantics &Sem =
1400 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001401 Result = llvm::APFloat::getInf(Sem);
1402 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001403 }
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Chris Lattner9e621712008-10-06 06:31:58 +00001405 case Builtin::BI__builtin_nan:
1406 case Builtin::BI__builtin_nanf:
1407 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001408 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001409 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001410 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001411 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001412 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001413 const llvm::fltSemantics &Sem =
1414 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001415 llvm::SmallString<16> s;
1416 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1417 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001418 long l;
1419 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001420 l = strtol(&s[0], &endp, 0);
1421 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001422 return false;
1423 unsigned type = (unsigned int)l;;
1424 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001425 return true;
1426 }
1427 }
1428 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001429
1430 case Builtin::BI__builtin_fabs:
1431 case Builtin::BI__builtin_fabsf:
1432 case Builtin::BI__builtin_fabsl:
1433 if (!EvaluateFloat(E->getArg(0), Result, Info))
1434 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001436 if (Result.isNegative())
1437 Result.changeSign();
1438 return true;
1439
Mike Stump1eb44332009-09-09 15:08:12 +00001440 case Builtin::BI__builtin_copysign:
1441 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001442 case Builtin::BI__builtin_copysignl: {
1443 APFloat RHS(0.);
1444 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1445 !EvaluateFloat(E->getArg(1), RHS, Info))
1446 return false;
1447 Result.copySign(RHS);
1448 return true;
1449 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001450 }
1451}
1452
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001453bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001454 if (E->getOpcode() == UnaryOperator::Deref)
1455 return false;
1456
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001457 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1458 return false;
1459
1460 switch (E->getOpcode()) {
1461 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001462 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001463 return true;
1464 case UnaryOperator::Minus:
1465 Result.changeSign();
1466 return true;
1467 }
1468}
Chris Lattner019f4e82008-10-06 05:28:25 +00001469
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001470bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1471 // FIXME: Diagnostics? I really don't understand how the warnings
1472 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001473 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001474 if (!EvaluateFloat(E->getLHS(), Result, Info))
1475 return false;
1476 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1477 return false;
1478
1479 switch (E->getOpcode()) {
1480 default: return false;
1481 case BinaryOperator::Mul:
1482 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1483 return true;
1484 case BinaryOperator::Add:
1485 Result.add(RHS, APFloat::rmNearestTiesToEven);
1486 return true;
1487 case BinaryOperator::Sub:
1488 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1489 return true;
1490 case BinaryOperator::Div:
1491 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1492 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001493 }
1494}
1495
1496bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1497 Result = E->getValue();
1498 return true;
1499}
1500
Eli Friedman4efaa272008-11-12 09:44:48 +00001501bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1502 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Eli Friedman4efaa272008-11-12 09:44:48 +00001504 if (SubExpr->getType()->isIntegralType()) {
1505 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001506 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001507 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001508 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001509 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001510 return true;
1511 }
1512 if (SubExpr->getType()->isRealFloatingType()) {
1513 if (!Visit(SubExpr))
1514 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001515 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1516 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001517 return true;
1518 }
Eli Friedman2217c872009-02-22 11:46:18 +00001519 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001520
1521 return false;
1522}
1523
1524bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1525 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1526 return true;
1527}
1528
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001529//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001530// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001531//===----------------------------------------------------------------------===//
1532
1533namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001534class VISIBILITY_HIDDEN ComplexExprEvaluator
1535 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001536 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001538public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001539 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001541 //===--------------------------------------------------------------------===//
1542 // Visitor Methods
1543 //===--------------------------------------------------------------------===//
1544
1545 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001546 return APValue();
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001549 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1550
1551 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001552 Expr* SubExpr = E->getSubExpr();
1553
1554 if (SubExpr->getType()->isRealFloatingType()) {
1555 APFloat Result(0.0);
1556
1557 if (!EvaluateFloat(SubExpr, Result, Info))
1558 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001559
1560 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001561 Result);
1562 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001563 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001564 "Unexpected imaginary literal.");
1565
1566 llvm::APSInt Result;
1567 if (!EvaluateInteger(SubExpr, Result, Info))
1568 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001570 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1571 Zero = 0;
1572 return APValue(Zero, Result);
1573 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001574 }
1575
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001576 APValue VisitCastExpr(CastExpr *E) {
1577 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001578 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001579 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001580
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001581 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001582 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001583
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001584 if (!EvaluateFloat(SubExpr, Result, Info))
1585 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001586
1587 if (EltType->isRealFloatingType()) {
1588 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001589 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001590 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1591 } else {
1592 llvm::APSInt IResult;
1593 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1594 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1595 Zero = 0;
1596 return APValue(IResult, Zero);
1597 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001598 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001599 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001600
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001601 if (!EvaluateInteger(SubExpr, Result, Info))
1602 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001603
Eli Friedman1725f682009-04-22 19:23:09 +00001604 if (EltType->isRealFloatingType()) {
1605 APFloat FResult =
1606 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001607 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001608 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1609 } else {
1610 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1611 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1612 Zero = 0;
1613 return APValue(Result, Zero);
1614 }
John McCall183700f2009-09-21 23:43:11 +00001615 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001616 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001617
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001618 if (!EvaluateComplex(SubExpr, Src, Info))
1619 return APValue();
1620
1621 QualType SrcType = CT->getElementType();
1622
1623 if (Src.isComplexFloat()) {
1624 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001625 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001626 Src.getComplexFloatReal(),
1627 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001628 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001629 Src.getComplexFloatImag(),
1630 Info.Ctx));
1631 } else {
1632 return APValue(HandleFloatToIntCast(EltType, SrcType,
1633 Src.getComplexFloatReal(),
1634 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001635 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001636 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001637 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001638 }
1639 } else {
1640 assert(Src.isComplexInt() && "Invalid evaluate result.");
1641 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001642 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001643 Src.getComplexIntReal(),
1644 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001645 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001646 Src.getComplexIntImag(),
1647 Info.Ctx));
1648 } else {
1649 return APValue(HandleIntToIntCast(EltType, SrcType,
1650 Src.getComplexIntReal(),
1651 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001652 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001653 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001654 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001655 }
1656 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001657 }
1658
1659 // FIXME: Handle more casts.
1660 return APValue();
1661 }
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001663 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001664 APValue VisitChooseExpr(const ChooseExpr *E)
1665 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1666 APValue VisitUnaryExtension(const UnaryOperator *E)
1667 { return Visit(E->getSubExpr()); }
1668 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001669 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001670};
1671} // end anonymous namespace
1672
Mike Stump1eb44332009-09-09 15:08:12 +00001673static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001674 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1675 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001676 (&Result.getComplexFloatReal().getSemantics() ==
1677 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001678 "Invalid complex evaluation.");
1679 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001680}
1681
Mike Stump1eb44332009-09-09 15:08:12 +00001682APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001683 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001685 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001686 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001688 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001689 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001690
Daniel Dunbar3f279872009-01-29 01:32:56 +00001691 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1692 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001693 switch (E->getOpcode()) {
1694 default: return APValue();
1695 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001696 if (Result.isComplexFloat()) {
1697 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1698 APFloat::rmNearestTiesToEven);
1699 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1700 APFloat::rmNearestTiesToEven);
1701 } else {
1702 Result.getComplexIntReal() += RHS.getComplexIntReal();
1703 Result.getComplexIntImag() += RHS.getComplexIntImag();
1704 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001705 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001706 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001707 if (Result.isComplexFloat()) {
1708 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1709 APFloat::rmNearestTiesToEven);
1710 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1711 APFloat::rmNearestTiesToEven);
1712 } else {
1713 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1714 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1715 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001716 break;
1717 case BinaryOperator::Mul:
1718 if (Result.isComplexFloat()) {
1719 APValue LHS = Result;
1720 APFloat &LHS_r = LHS.getComplexFloatReal();
1721 APFloat &LHS_i = LHS.getComplexFloatImag();
1722 APFloat &RHS_r = RHS.getComplexFloatReal();
1723 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Daniel Dunbar3f279872009-01-29 01:32:56 +00001725 APFloat Tmp = LHS_r;
1726 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1727 Result.getComplexFloatReal() = Tmp;
1728 Tmp = LHS_i;
1729 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1730 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1731
1732 Tmp = LHS_r;
1733 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1734 Result.getComplexFloatImag() = Tmp;
1735 Tmp = LHS_i;
1736 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1737 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1738 } else {
1739 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001740 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001741 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1742 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001743 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001744 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1745 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1746 }
1747 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001748 }
1749
1750 return Result;
1751}
1752
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001753//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001754// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001755//===----------------------------------------------------------------------===//
1756
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001757/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001758/// any crazy technique (that has nothing to do with language standards) that
1759/// we want to. If this function returns true, it returns the folded constant
1760/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001761bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1762 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001763
Nate Begeman59b5da62009-01-18 03:20:47 +00001764 if (getType()->isVectorType()) {
1765 if (!EvaluateVector(this, Result.Val, Info))
1766 return false;
1767 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001768 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001769 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001770 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001771 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001772 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001773 } else if (getType()->isRealFloatingType()) {
1774 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001775 if (!EvaluateFloat(this, f, Info))
1776 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001778 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001779 } else if (getType()->isAnyComplexType()) {
1780 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001781 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001782 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001783 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001784
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001785 return true;
1786}
1787
Anders Carlsson1b782762009-04-10 04:54:13 +00001788bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1789 EvalInfo Info(Ctx, Result);
1790
1791 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1792}
1793
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001794bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1795 EvalInfo Info(Ctx, Result, true);
1796
1797 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1798}
1799
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001800/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001801/// folded, but discard the result.
1802bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001803 EvalResult Result;
1804 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001805}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001806
1807APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001808 EvalResult EvalResult;
1809 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001810 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001811 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001812 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001813
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001814 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001815}