blob: 94d22998ebbed043b983d164f7badcdfce9e5edb [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)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000183
184 APValue VisitCastExpr(CastExpr *E) {
185 switch (E->getCastKind()) {
186 default:
187 return APValue();
188
189 case CastExpr::CK_NoOp:
190 return Visit(E->getSubExpr());
191 }
192 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000193 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000194};
195} // end anonymous namespace
196
197static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
198 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
199 return Result.isLValue();
200}
201
Mike Stump1eb44332009-09-09 15:08:12 +0000202APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000203 if (isa<FunctionDecl>(E->getDecl())) {
204 return APValue(E, 0);
205 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000206 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000207 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000208 if (!VD->getType()->isReferenceType())
209 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000210 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000211 if (VD->getInit())
212 return Visit(VD->getInit());
213 }
214
215 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000216}
217
Mike Stump1eb44332009-09-09 15:08:12 +0000218APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000219 if (E->hasBlockDeclRefExprs())
220 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroff3aaa4822009-04-16 19:02:57 +0000222 return APValue(E, 0);
223}
224
Eli Friedman4efaa272008-11-12 09:44:48 +0000225APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000226 if (!Info.AnyLValue && !E->isFileScope())
227 return APValue();
228 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000229}
230
231APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
232 APValue result;
233 QualType Ty;
234 if (E->isArrow()) {
235 if (!EvaluatePointer(E->getBase(), result, Info))
236 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000237 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000238 } else {
239 result = Visit(E->getBase());
240 if (result.isUninit())
241 return APValue();
242 Ty = E->getBase()->getType();
243 }
244
Ted Kremenek6217b802009-07-29 21:53:49 +0000245 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000246 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000247
248 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
249 if (!FD) // FIXME: deal with other kinds of member expressions
250 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000251
252 if (FD->getType()->isReferenceType())
253 return APValue();
254
Eli Friedman4efaa272008-11-12 09:44:48 +0000255 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000256 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000257 for (RecordDecl::field_iterator Field = RD->field_begin(),
258 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000259 Field != FieldEnd; (void)++Field, ++i) {
260 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000261 break;
262 }
263
264 result.setLValue(result.getLValueBase(),
265 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
266
267 return result;
268}
269
Mike Stump1eb44332009-09-09 15:08:12 +0000270APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000271 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson3068d112008-11-16 19:01:22 +0000273 if (!EvaluatePointer(E->getBase(), Result, Info))
274 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Anders Carlsson3068d112008-11-16 19:01:22 +0000276 APSInt Index;
277 if (!EvaluateInteger(E->getIdx(), Index, Info))
278 return APValue();
279
280 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
281
282 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000283 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000284 Result.getLValueOffset() + Offset);
285 return Result;
286}
Eli Friedman4efaa272008-11-12 09:44:48 +0000287
Mike Stump1eb44332009-09-09 15:08:12 +0000288APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000289 APValue Result;
290 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
291 return APValue();
292 return Result;
293}
294
Eli Friedman4efaa272008-11-12 09:44:48 +0000295//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000296// Pointer Evaluation
297//===----------------------------------------------------------------------===//
298
Anders Carlssonc754aa62008-07-08 05:13:58 +0000299namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000300class VISIBILITY_HIDDEN PointerExprEvaluator
301 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000302 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000303public:
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattner87eae5e2008-07-11 22:52:41 +0000305 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000306
Anders Carlsson2bad1682008-07-08 14:30:00 +0000307 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000308 return APValue();
309 }
310
311 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
312
Anders Carlsson650c92f2008-07-08 15:34:11 +0000313 APValue VisitBinaryOperator(const BinaryOperator *E);
314 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000315 APValue VisitUnaryExtension(const UnaryOperator *E)
316 { return Visit(E->getSubExpr()); }
317 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000318 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
319 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000320 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
321 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000322 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000323 APValue VisitBlockExpr(BlockExpr *E) {
324 if (!E->hasBlockDeclRefExprs())
325 return APValue(E, 0);
326 return APValue();
327 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000328 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
329 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000330 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000331 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000332 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
333 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
334 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000335 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000336};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000337} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000338
Chris Lattner87eae5e2008-07-11 22:52:41 +0000339static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000340 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000342 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000343 return Result.isLValue();
344}
345
346APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
347 if (E->getOpcode() != BinaryOperator::Add &&
348 E->getOpcode() != BinaryOperator::Sub)
349 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000351 const Expr *PExp = E->getLHS();
352 const Expr *IExp = E->getRHS();
353 if (IExp->getType()->isPointerType())
354 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000356 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000357 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000358 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000360 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000361 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000362 return APValue();
363
Ted Kremenek6217b802009-07-29 21:53:49 +0000364 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000365 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000367 // Explicitly handle GNU void* and function pointer arithmetic extensions.
368 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
369 SizeOfPointee = 1;
370 else
371 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000372
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000373 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000374
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000375 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000376 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000377 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000378 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
379
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000380 return APValue(ResultLValue.getLValueBase(), Offset);
381}
Eli Friedman4efaa272008-11-12 09:44:48 +0000382
Eli Friedman2217c872009-02-22 11:46:18 +0000383APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
384 APValue result;
385 if (EvaluateLValue(E->getSubExpr(), result, Info))
386 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000387 return APValue();
388}
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000390
Chris Lattnerb542afe2008-07-11 19:10:17 +0000391APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000392 const Expr* SubExpr = E->getSubExpr();
393
394 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000395 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000396 SubExpr->getType()->isObjCObjectPointerType() ||
397 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000398 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000399 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000400 return Result;
401 return APValue();
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000404 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000405 APValue Result;
406 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
407 return APValue();
408
409 if (Result.isInt()) {
410 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
411 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000414 // Cast is of an lvalue, no need to change value.
415 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000417
418 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000419 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000420 SubExpr->getType()->isArrayType()) {
421 APValue Result;
422 if (EvaluateLValue(SubExpr, Result, Info))
423 return Result;
424 return APValue();
425 }
426
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000428}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000429
Eli Friedman3941b182009-01-25 01:54:01 +0000430APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000431 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000432 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000433 return APValue(E, 0);
434 return APValue();
435}
436
Eli Friedman4efaa272008-11-12 09:44:48 +0000437APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
438 bool BoolResult;
439 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
440 return APValue();
441
442 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
443
444 APValue Result;
445 if (EvaluatePointer(EvalExpr, Result, Info))
446 return Result;
447 return APValue();
448}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000449
450//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000451// Vector Evaluation
452//===----------------------------------------------------------------------===//
453
454namespace {
455 class VISIBILITY_HIDDEN VectorExprEvaluator
456 : public StmtVisitor<VectorExprEvaluator, APValue> {
457 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000458 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000459 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Nate Begeman59b5da62009-01-18 03:20:47 +0000461 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Nate Begeman59b5da62009-01-18 03:20:47 +0000463 APValue VisitStmt(Stmt *S) {
464 return APValue();
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Eli Friedman91110ee2009-02-23 04:23:56 +0000467 APValue VisitParenExpr(ParenExpr *E)
468 { return Visit(E->getSubExpr()); }
469 APValue VisitUnaryExtension(const UnaryOperator *E)
470 { return Visit(E->getSubExpr()); }
471 APValue VisitUnaryPlus(const UnaryOperator *E)
472 { return Visit(E->getSubExpr()); }
473 APValue VisitUnaryReal(const UnaryOperator *E)
474 { return Visit(E->getSubExpr()); }
475 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
476 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000477 APValue VisitCastExpr(const CastExpr* E);
478 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
479 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000480 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000481 APValue VisitChooseExpr(const ChooseExpr *E)
482 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000483 APValue VisitUnaryImag(const UnaryOperator *E);
484 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000485 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000486 // shufflevector, ExtVectorElementExpr
487 // (Note that these require implementing conversions
488 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000489 };
490} // end anonymous namespace
491
492static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
493 if (!E->getType()->isVectorType())
494 return false;
495 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
496 return !Result.isUninit();
497}
498
499APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000500 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000501 QualType EltTy = VTy->getElementType();
502 unsigned NElts = VTy->getNumElements();
503 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Nate Begeman59b5da62009-01-18 03:20:47 +0000505 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000506 QualType SETy = SE->getType();
507 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000508
Nate Begemane8c9e922009-06-26 18:22:18 +0000509 // Check for vector->vector bitcast and scalar->vector splat.
510 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000511 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000512 } else if (SETy->isIntegerType()) {
513 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000514 if (!EvaluateInteger(SE, IntResult, Info))
515 return APValue();
516 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000517 } else if (SETy->isRealFloatingType()) {
518 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000519 if (!EvaluateFloat(SE, F, Info))
520 return APValue();
521 Result = APValue(F);
522 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000523 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000524
Nate Begemanc0b8b192009-07-01 07:50:47 +0000525 // For casts of a scalar to ExtVector, convert the scalar to the element type
526 // and splat it to all elements.
527 if (E->getType()->isExtVectorType()) {
528 if (EltTy->isIntegerType() && Result.isInt())
529 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
530 Info.Ctx));
531 else if (EltTy->isIntegerType())
532 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
533 Info.Ctx));
534 else if (EltTy->isRealFloatingType() && Result.isInt())
535 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
536 Info.Ctx));
537 else if (EltTy->isRealFloatingType())
538 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
539 Info.Ctx));
540 else
541 return APValue();
542
543 // Splat and create vector APValue.
544 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
545 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000546 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000547
548 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
549 // to the vector. To construct the APValue vector initializer, bitcast the
550 // initializing value to an APInt, and shift out the bits pertaining to each
551 // element.
552 APSInt Init;
553 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Nate Begemanc0b8b192009-07-01 07:50:47 +0000555 llvm::SmallVector<APValue, 4> Elts;
556 for (unsigned i = 0; i != NElts; ++i) {
557 APSInt Tmp = Init;
558 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Nate Begemanc0b8b192009-07-01 07:50:47 +0000560 if (EltTy->isIntegerType())
561 Elts.push_back(APValue(Tmp));
562 else if (EltTy->isRealFloatingType())
563 Elts.push_back(APValue(APFloat(Tmp)));
564 else
565 return APValue();
566
567 Init >>= EltWidth;
568 }
569 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000570}
571
Mike Stump1eb44332009-09-09 15:08:12 +0000572APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000573VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
574 return this->Visit(const_cast<Expr*>(E->getInitializer()));
575}
576
Mike Stump1eb44332009-09-09 15:08:12 +0000577APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000578VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000579 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000580 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000581 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Nate Begeman59b5da62009-01-18 03:20:47 +0000583 QualType EltTy = VT->getElementType();
584 llvm::SmallVector<APValue, 4> Elements;
585
Eli Friedman91110ee2009-02-23 04:23:56 +0000586 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000587 if (EltTy->isIntegerType()) {
588 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000589 if (i < NumInits) {
590 if (!EvaluateInteger(E->getInit(i), sInt, Info))
591 return APValue();
592 } else {
593 sInt = Info.Ctx.MakeIntValue(0, EltTy);
594 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000595 Elements.push_back(APValue(sInt));
596 } else {
597 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000598 if (i < NumInits) {
599 if (!EvaluateFloat(E->getInit(i), f, Info))
600 return APValue();
601 } else {
602 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
603 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000604 Elements.push_back(APValue(f));
605 }
606 }
607 return APValue(&Elements[0], Elements.size());
608}
609
Mike Stump1eb44332009-09-09 15:08:12 +0000610APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000611VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000612 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000613 QualType EltTy = VT->getElementType();
614 APValue ZeroElement;
615 if (EltTy->isIntegerType())
616 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
617 else
618 ZeroElement =
619 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
620
621 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
622 return APValue(&Elements[0], Elements.size());
623}
624
625APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
626 bool BoolResult;
627 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
628 return APValue();
629
630 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
631
632 APValue Result;
633 if (EvaluateVector(EvalExpr, Result, Info))
634 return Result;
635 return APValue();
636}
637
Eli Friedman91110ee2009-02-23 04:23:56 +0000638APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
639 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
640 Info.EvalResult.HasSideEffects = true;
641 return GetZeroVector(E->getType());
642}
643
Nate Begeman59b5da62009-01-18 03:20:47 +0000644//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000645// Integer Evaluation
646//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000647
648namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000649class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000650 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000651 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000652 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000653public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000654 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000655 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000656
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000657 bool Success(const llvm::APSInt &SI, 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(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000660 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000661 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000662 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000663 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000664 return true;
665 }
666
Daniel Dunbar131eb432009-02-19 09:06:44 +0000667 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000668 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000669 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000670 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000671 Result = APValue(APSInt(I));
672 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000673 return true;
674 }
675
676 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000677 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000678 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000679 return true;
680 }
681
Anders Carlsson82206e22008-11-30 18:14:57 +0000682 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000683 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000684 if (Info.EvalResult.Diag == 0) {
685 Info.EvalResult.DiagLoc = L;
686 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000687 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000688 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000689 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Anders Carlssonc754aa62008-07-08 05:13:58 +0000692 //===--------------------------------------------------------------------===//
693 // Visitor Methods
694 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattner32fea9d2008-11-12 07:43:42 +0000696 bool VisitStmt(Stmt *) {
697 assert(0 && "This should be called on integers, stmts are not integers");
698 return false;
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattner32fea9d2008-11-12 07:43:42 +0000701 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000702 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattnerb542afe2008-07-11 19:10:17 +0000705 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000706
Chris Lattner4c4867e2008-07-12 00:38:25 +0000707 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000708 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000709 }
710 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000711 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000712 }
713 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000714 // Per gcc docs "this built-in function ignores top level
715 // qualifiers". We need to use the canonical version to properly
716 // be able to strip CRV qualifiers from the type.
717 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
718 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000719 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000720 T1.getUnqualifiedType()),
721 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000722 }
723 bool VisitDeclRefExpr(const DeclRefExpr *E);
724 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000725 bool VisitBinaryOperator(const BinaryOperator *E);
726 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000727 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000728
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000729 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000730 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
731
Anders Carlsson3068d112008-11-16 19:01:22 +0000732 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000733 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Anders Carlsson3f704562008-12-21 22:39:40 +0000736 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000737 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anders Carlsson3068d112008-11-16 19:01:22 +0000740 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000741 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000742 }
743
Eli Friedman664a1042009-02-27 04:45:43 +0000744 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
745 return Success(0, E);
746 }
747
Sebastian Redl64b45f72009-01-05 20:52:13 +0000748 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000749 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000750 }
751
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000752 bool VisitChooseExpr(const ChooseExpr *E) {
753 return Visit(E->getChosenSubExpr(Info.Ctx));
754 }
755
Eli Friedman722c7172009-02-28 03:59:05 +0000756 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000757 bool VisitUnaryImag(const UnaryOperator *E);
758
Chris Lattnerfcee0012008-07-11 21:24:13 +0000759private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000760 unsigned GetAlignOfExpr(const Expr *E);
761 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000762 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000763};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000764} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000765
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000766static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000767 if (!E->getType()->isIntegralType())
768 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000770 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
771}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000772
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000773static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
774 APValue Val;
775 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
776 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000777 Result = Val.getInt();
778 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000779}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000780
Chris Lattner4c4867e2008-07-12 00:38:25 +0000781bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
782 // Enums are integer constant exprs.
783 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000784 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000785 // signedness consistently; see PR3173.
786 APSInt SI = D->getInitVal();
787 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
788 // FIXME: This is an ugly hack around the fact that enums don't
789 // set their width (!?!) consistently; see PR3173.
790 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
791 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000792 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000793
794 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000795 // In C, they can also be folded, although they are not ICEs.
John McCall0953e762009-09-24 19:53:00 +0000796 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000797 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000798 if (APValue *V = D->getEvaluatedValue())
799 return Success(V->getInt(), E);
800 if (const Expr *Init = D->getInit()) {
801 if (Visit(const_cast<Expr*>(Init))) {
802 // Cache the evaluated value in the variable declaration.
803 D->setEvaluatedValue(Info.Ctx, Result);
804 return true;
805 }
806
807 return false;
808 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000809 }
810 }
811
Chris Lattner4c4867e2008-07-12 00:38:25 +0000812 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000813 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000814}
815
Chris Lattnera4d55d82008-10-06 06:40:35 +0000816/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
817/// as GCC.
818static int EvaluateBuiltinClassifyType(const CallExpr *E) {
819 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000820 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000821 enum gcc_type_class {
822 no_type_class = -1,
823 void_type_class, integer_type_class, char_type_class,
824 enumeral_type_class, boolean_type_class,
825 pointer_type_class, reference_type_class, offset_type_class,
826 real_type_class, complex_type_class,
827 function_type_class, method_type_class,
828 record_type_class, union_type_class,
829 array_type_class, string_type_class,
830 lang_type_class
831 };
Mike Stump1eb44332009-09-09 15:08:12 +0000832
833 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000834 // ideal, however it is what gcc does.
835 if (E->getNumArgs() == 0)
836 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattnera4d55d82008-10-06 06:40:35 +0000838 QualType ArgTy = E->getArg(0)->getType();
839 if (ArgTy->isVoidType())
840 return void_type_class;
841 else if (ArgTy->isEnumeralType())
842 return enumeral_type_class;
843 else if (ArgTy->isBooleanType())
844 return boolean_type_class;
845 else if (ArgTy->isCharType())
846 return string_type_class; // gcc doesn't appear to use char_type_class
847 else if (ArgTy->isIntegerType())
848 return integer_type_class;
849 else if (ArgTy->isPointerType())
850 return pointer_type_class;
851 else if (ArgTy->isReferenceType())
852 return reference_type_class;
853 else if (ArgTy->isRealType())
854 return real_type_class;
855 else if (ArgTy->isComplexType())
856 return complex_type_class;
857 else if (ArgTy->isFunctionType())
858 return function_type_class;
859 else if (ArgTy->isStructureType())
860 return record_type_class;
861 else if (ArgTy->isUnionType())
862 return union_type_class;
863 else if (ArgTy->isArrayType())
864 return array_type_class;
865 else if (ArgTy->isUnionType())
866 return union_type_class;
867 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
868 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
869 return -1;
870}
871
Chris Lattner4c4867e2008-07-12 00:38:25 +0000872bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000873 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000874 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000875 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000876 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000877 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000879 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000880 // __builtin_constant_p always has one operand: it returns true if that
881 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000882 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000883
884 case Builtin::BI__builtin_eh_return_data_regno: {
885 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
886 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
887 return Success(Operand, E);
888 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000889 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000890}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000891
Chris Lattnerb542afe2008-07-11 19:10:17 +0000892bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000893 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000894 if (!Visit(E->getRHS()))
895 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000896
Eli Friedman33ef1452009-02-26 10:19:36 +0000897 // If we can't evaluate the LHS, it might have side effects;
898 // conservatively mark it.
899 if (!E->getLHS()->isEvaluatable(Info.Ctx))
900 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000901
Anders Carlsson027f62e2008-12-01 02:07:06 +0000902 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000903 }
904
905 if (E->isLogicalOp()) {
906 // These need to be handled specially because the operands aren't
907 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000908 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000910 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000911 // We were able to evaluate the LHS, see if we can get away with not
912 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000913 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000914 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000915
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000916 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000917 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000918 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000919 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000920 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000921 }
922 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000923 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000924 // We can't evaluate the LHS; however, sometimes the result
925 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000926 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000927 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000928 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000929 // must have had side effects.
930 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000931
932 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000933 }
934 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000935 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000936
Eli Friedmana6afa762008-11-13 06:09:17 +0000937 return false;
938 }
939
Anders Carlsson286f85e2008-11-16 07:17:21 +0000940 QualType LHSTy = E->getLHS()->getType();
941 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000942
943 if (LHSTy->isAnyComplexType()) {
944 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
945 APValue LHS, RHS;
946
947 if (!EvaluateComplex(E->getLHS(), LHS, Info))
948 return false;
949
950 if (!EvaluateComplex(E->getRHS(), RHS, Info))
951 return false;
952
953 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000954 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000955 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000956 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000957 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
958
Daniel Dunbar4087e242009-01-29 06:43:41 +0000959 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000960 return Success((CR_r == APFloat::cmpEqual &&
961 CR_i == APFloat::cmpEqual), E);
962 else {
963 assert(E->getOpcode() == BinaryOperator::NE &&
964 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000965 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000966 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000967 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000968 CR_i == APFloat::cmpLessThan)), E);
969 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000970 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000971 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000972 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
973 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
974 else {
975 assert(E->getOpcode() == BinaryOperator::NE &&
976 "Invalid compex comparison.");
977 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
978 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
979 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000980 }
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Anders Carlsson286f85e2008-11-16 07:17:21 +0000983 if (LHSTy->isRealFloatingType() &&
984 RHSTy->isRealFloatingType()) {
985 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Anders Carlsson286f85e2008-11-16 07:17:21 +0000987 if (!EvaluateFloat(E->getRHS(), RHS, Info))
988 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Anders Carlsson286f85e2008-11-16 07:17:21 +0000990 if (!EvaluateFloat(E->getLHS(), LHS, Info))
991 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Anders Carlsson286f85e2008-11-16 07:17:21 +0000993 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000994
Anders Carlsson286f85e2008-11-16 07:17:21 +0000995 switch (E->getOpcode()) {
996 default:
997 assert(0 && "Invalid binary operator!");
998 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000999 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001000 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001001 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001002 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001003 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001004 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001005 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001006 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001007 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001008 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001009 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001010 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001011 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001012 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001015 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1016 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001017 APValue LHSValue;
1018 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1019 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001020
Anders Carlsson3068d112008-11-16 19:01:22 +00001021 APValue RHSValue;
1022 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1023 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001024
Eli Friedman5bc86102009-06-14 02:17:33 +00001025 // Reject any bases from the normal codepath; we special-case comparisons
1026 // to null.
1027 if (LHSValue.getLValueBase()) {
1028 if (!E->isEqualityOp())
1029 return false;
1030 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1031 return false;
1032 bool bres;
1033 if (!EvalPointerValueAsBool(LHSValue, bres))
1034 return false;
1035 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1036 } else if (RHSValue.getLValueBase()) {
1037 if (!E->isEqualityOp())
1038 return false;
1039 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1040 return false;
1041 bool bres;
1042 if (!EvalPointerValueAsBool(RHSValue, bres))
1043 return false;
1044 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1045 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001046
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001047 if (E->getOpcode() == BinaryOperator::Sub) {
1048 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001049 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001050
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001051 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001052 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1053 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001054
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001055 return Success(D, E);
1056 }
1057 bool Result;
1058 if (E->getOpcode() == BinaryOperator::EQ) {
1059 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001060 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001061 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1062 }
1063 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001064 }
1065 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001066 if (!LHSTy->isIntegralType() ||
1067 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001068 // We can't continue from here for non-integral types, and they
1069 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001070 return false;
1071 }
1072
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001073 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001074 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001075 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001076
Eli Friedman42edd0d2009-03-24 01:14:50 +00001077 APValue RHSVal;
1078 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001079 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001080
1081 // Handle cases like (unsigned long)&a + 4.
1082 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1083 uint64_t offset = Result.getLValueOffset();
1084 if (E->getOpcode() == BinaryOperator::Add)
1085 offset += RHSVal.getInt().getZExtValue();
1086 else
1087 offset -= RHSVal.getInt().getZExtValue();
1088 Result = APValue(Result.getLValueBase(), offset);
1089 return true;
1090 }
1091
1092 // Handle cases like 4 + (unsigned long)&a
1093 if (E->getOpcode() == BinaryOperator::Add &&
1094 RHSVal.isLValue() && Result.isInt()) {
1095 uint64_t offset = RHSVal.getLValueOffset();
1096 offset += Result.getInt().getZExtValue();
1097 Result = APValue(RHSVal.getLValueBase(), offset);
1098 return true;
1099 }
1100
1101 // All the following cases expect both operands to be an integer
1102 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001103 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001104
Eli Friedman42edd0d2009-03-24 01:14:50 +00001105 APSInt& RHS = RHSVal.getInt();
1106
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001107 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001108 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001109 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001110 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1111 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1112 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1113 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1114 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1115 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001116 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001117 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001118 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001119 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001120 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001121 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001122 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001123 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001124 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001125 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001126 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001127 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1128 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001129 }
1130 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001131 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001132 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1133 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001136 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1137 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1138 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1139 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1140 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1141 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001142 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001143}
1144
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001145bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001146 bool Cond;
1147 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001148 return false;
1149
Nuno Lopesa25bd552008-11-16 22:06:39 +00001150 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001151}
1152
Chris Lattneraf707ab2009-01-24 21:53:27 +00001153unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001154 // Get information about the alignment.
1155 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001156
Eli Friedman2be58612009-05-30 21:09:44 +00001157 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001158 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001159}
1160
Chris Lattneraf707ab2009-01-24 21:53:27 +00001161unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1162 E = E->IgnoreParens();
1163
1164 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001165 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001166 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001167 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001168
Chris Lattneraf707ab2009-01-24 21:53:27 +00001169 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001170 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001171
Chris Lattnere9feb472009-01-24 21:09:06 +00001172 return GetAlignOfType(E->getType());
1173}
1174
1175
Sebastian Redl05189992008-11-11 17:56:53 +00001176/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1177/// expression's type.
1178bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1179 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001180
Chris Lattnere9feb472009-01-24 21:09:06 +00001181 // Handle alignof separately.
1182 if (!E->isSizeOf()) {
1183 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001184 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001185 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001186 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001187 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001188
Sebastian Redl05189992008-11-11 17:56:53 +00001189 QualType SrcTy = E->getTypeOfArgument();
1190
Daniel Dunbar131eb432009-02-19 09:06:44 +00001191 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1192 // extension.
1193 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1194 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001195
Chris Lattnerfcee0012008-07-11 21:24:13 +00001196 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001197 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001198 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001199
Chris Lattnere9feb472009-01-24 21:09:06 +00001200 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001201 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001202 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001203}
1204
Chris Lattnerb542afe2008-07-11 19:10:17 +00001205bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001206 // Special case unary operators that do not need their subexpression
1207 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001208 if (E->isOffsetOfOp()) {
1209 // The AST for offsetof is defined in such a way that we can just
1210 // directly Evaluate it as an l-value.
1211 APValue LV;
1212 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1213 return false;
1214 if (LV.getLValueBase())
1215 return false;
1216 return Success(LV.getLValueOffset(), E);
1217 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001218
1219 if (E->getOpcode() == UnaryOperator::LNot) {
1220 // LNot's operand isn't necessarily an integer, so we handle it specially.
1221 bool bres;
1222 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1223 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001224 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001225 }
1226
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001227 // Only handle integral operations...
1228 if (!E->getSubExpr()->getType()->isIntegralType())
1229 return false;
1230
Chris Lattner87eae5e2008-07-11 22:52:41 +00001231 // Get the operand value into 'Result'.
1232 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001233 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001234
Chris Lattner75a48812008-07-11 22:15:16 +00001235 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001236 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001237 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1238 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001239 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001240 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001241 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1242 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001243 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001244 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001245 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001246 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001247 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001248 if (!Result.isInt()) return false;
1249 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001250 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001251 if (!Result.isInt()) return false;
1252 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001253 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001254}
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattner732b2232008-07-12 01:15:53 +00001256/// HandleCast - This is used to evaluate implicit or explicit casts where the
1257/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001258bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001259 Expr *SubExpr = E->getSubExpr();
1260 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001261 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001262
Eli Friedman4efaa272008-11-12 09:44:48 +00001263 if (DestType->isBooleanType()) {
1264 bool BoolResult;
1265 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1266 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001267 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001268 }
1269
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001270 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001271 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001272 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001273 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001274
Eli Friedmanbe265702009-02-20 01:15:07 +00001275 if (!Result.isInt()) {
1276 // Only allow casts of lvalues if they are lossless.
1277 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1278 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001279
Daniel Dunbardd211642009-02-19 22:24:01 +00001280 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001281 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Chris Lattner732b2232008-07-12 01:15:53 +00001284 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001285 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001286 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001287 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001288 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001289
Daniel Dunbardd211642009-02-19 22:24:01 +00001290 if (LV.getLValueBase()) {
1291 // Only allow based lvalue casts if they are lossless.
1292 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1293 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001294
Daniel Dunbardd211642009-02-19 22:24:01 +00001295 Result = LV;
1296 return true;
1297 }
1298
1299 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1300 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001301 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001302
Eli Friedmanbe265702009-02-20 01:15:07 +00001303 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1304 // This handles double-conversion cases, where there's both
1305 // an l-value promotion and an implicit conversion to int.
1306 APValue LV;
1307 if (!EvaluateLValue(SubExpr, LV, Info))
1308 return false;
1309
1310 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1311 return false;
1312
1313 Result = LV;
1314 return true;
1315 }
1316
Eli Friedman1725f682009-04-22 19:23:09 +00001317 if (SrcType->isAnyComplexType()) {
1318 APValue C;
1319 if (!EvaluateComplex(SubExpr, C, Info))
1320 return false;
1321 if (C.isComplexFloat())
1322 return Success(HandleFloatToIntCast(DestType, SrcType,
1323 C.getComplexFloatReal(), Info.Ctx),
1324 E);
1325 else
1326 return Success(HandleIntToIntCast(DestType, SrcType,
1327 C.getComplexIntReal(), Info.Ctx), E);
1328 }
Eli Friedman2217c872009-02-22 11:46:18 +00001329 // FIXME: Handle vectors
1330
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001331 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001332 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001333
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001334 APFloat F(0.0);
1335 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001336 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001338 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001339}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001340
Eli Friedman722c7172009-02-28 03:59:05 +00001341bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1342 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1343 APValue LV;
1344 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1345 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1346 return Success(LV.getComplexIntReal(), E);
1347 }
1348
1349 return Visit(E->getSubExpr());
1350}
1351
Eli Friedman664a1042009-02-27 04:45:43 +00001352bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001353 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1354 APValue LV;
1355 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1356 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1357 return Success(LV.getComplexIntImag(), E);
1358 }
1359
Eli Friedman664a1042009-02-27 04:45:43 +00001360 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1361 Info.EvalResult.HasSideEffects = true;
1362 return Success(0, E);
1363}
1364
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001365//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001366// Float Evaluation
1367//===----------------------------------------------------------------------===//
1368
1369namespace {
1370class VISIBILITY_HIDDEN FloatExprEvaluator
1371 : public StmtVisitor<FloatExprEvaluator, bool> {
1372 EvalInfo &Info;
1373 APFloat &Result;
1374public:
1375 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1376 : Info(info), Result(result) {}
1377
1378 bool VisitStmt(Stmt *S) {
1379 return false;
1380 }
1381
1382 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001383 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001384
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001385 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001386 bool VisitBinaryOperator(const BinaryOperator *E);
1387 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001388 bool VisitCastExpr(CastExpr *E);
1389 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001390
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001391 bool VisitChooseExpr(const ChooseExpr *E)
1392 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1393 bool VisitUnaryExtension(const UnaryOperator *E)
1394 { return Visit(E->getSubExpr()); }
1395
1396 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1397 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001398 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001399};
1400} // end anonymous namespace
1401
1402static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1403 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1404}
1405
Chris Lattner019f4e82008-10-06 05:28:25 +00001406bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001407 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001408 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001409 case Builtin::BI__builtin_huge_val:
1410 case Builtin::BI__builtin_huge_valf:
1411 case Builtin::BI__builtin_huge_vall:
1412 case Builtin::BI__builtin_inf:
1413 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001414 case Builtin::BI__builtin_infl: {
1415 const llvm::fltSemantics &Sem =
1416 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001417 Result = llvm::APFloat::getInf(Sem);
1418 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001419 }
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Chris Lattner9e621712008-10-06 06:31:58 +00001421 case Builtin::BI__builtin_nan:
1422 case Builtin::BI__builtin_nanf:
1423 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001424 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001425 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001426 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001427 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001428 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001429 const llvm::fltSemantics &Sem =
1430 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001431 llvm::SmallString<16> s;
1432 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1433 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001434 long l;
1435 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001436 l = strtol(&s[0], &endp, 0);
1437 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001438 return false;
1439 unsigned type = (unsigned int)l;;
1440 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001441 return true;
1442 }
1443 }
1444 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001445
1446 case Builtin::BI__builtin_fabs:
1447 case Builtin::BI__builtin_fabsf:
1448 case Builtin::BI__builtin_fabsl:
1449 if (!EvaluateFloat(E->getArg(0), Result, Info))
1450 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001452 if (Result.isNegative())
1453 Result.changeSign();
1454 return true;
1455
Mike Stump1eb44332009-09-09 15:08:12 +00001456 case Builtin::BI__builtin_copysign:
1457 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001458 case Builtin::BI__builtin_copysignl: {
1459 APFloat RHS(0.);
1460 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1461 !EvaluateFloat(E->getArg(1), RHS, Info))
1462 return false;
1463 Result.copySign(RHS);
1464 return true;
1465 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001466 }
1467}
1468
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001469bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001470 if (E->getOpcode() == UnaryOperator::Deref)
1471 return false;
1472
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001473 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1474 return false;
1475
1476 switch (E->getOpcode()) {
1477 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001478 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001479 return true;
1480 case UnaryOperator::Minus:
1481 Result.changeSign();
1482 return true;
1483 }
1484}
Chris Lattner019f4e82008-10-06 05:28:25 +00001485
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001486bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1487 // FIXME: Diagnostics? I really don't understand how the warnings
1488 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001489 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001490 if (!EvaluateFloat(E->getLHS(), Result, Info))
1491 return false;
1492 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1493 return false;
1494
1495 switch (E->getOpcode()) {
1496 default: return false;
1497 case BinaryOperator::Mul:
1498 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1499 return true;
1500 case BinaryOperator::Add:
1501 Result.add(RHS, APFloat::rmNearestTiesToEven);
1502 return true;
1503 case BinaryOperator::Sub:
1504 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1505 return true;
1506 case BinaryOperator::Div:
1507 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1508 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001509 }
1510}
1511
1512bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1513 Result = E->getValue();
1514 return true;
1515}
1516
Eli Friedman4efaa272008-11-12 09:44:48 +00001517bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1518 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Eli Friedman4efaa272008-11-12 09:44:48 +00001520 if (SubExpr->getType()->isIntegralType()) {
1521 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001522 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001523 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001525 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001526 return true;
1527 }
1528 if (SubExpr->getType()->isRealFloatingType()) {
1529 if (!Visit(SubExpr))
1530 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001531 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1532 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001533 return true;
1534 }
Eli Friedman2217c872009-02-22 11:46:18 +00001535 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001536
1537 return false;
1538}
1539
1540bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1541 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1542 return true;
1543}
1544
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001545//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001546// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001547//===----------------------------------------------------------------------===//
1548
1549namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001550class VISIBILITY_HIDDEN ComplexExprEvaluator
1551 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001552 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001554public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001555 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001557 //===--------------------------------------------------------------------===//
1558 // Visitor Methods
1559 //===--------------------------------------------------------------------===//
1560
1561 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001562 return APValue();
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001565 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1566
1567 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001568 Expr* SubExpr = E->getSubExpr();
1569
1570 if (SubExpr->getType()->isRealFloatingType()) {
1571 APFloat Result(0.0);
1572
1573 if (!EvaluateFloat(SubExpr, Result, Info))
1574 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001575
1576 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001577 Result);
1578 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001579 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001580 "Unexpected imaginary literal.");
1581
1582 llvm::APSInt Result;
1583 if (!EvaluateInteger(SubExpr, Result, Info))
1584 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001586 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1587 Zero = 0;
1588 return APValue(Zero, Result);
1589 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001590 }
1591
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001592 APValue VisitCastExpr(CastExpr *E) {
1593 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001594 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001595 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001596
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001597 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001598 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001599
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001600 if (!EvaluateFloat(SubExpr, Result, Info))
1601 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001602
1603 if (EltType->isRealFloatingType()) {
1604 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001605 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001606 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1607 } else {
1608 llvm::APSInt IResult;
1609 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1610 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1611 Zero = 0;
1612 return APValue(IResult, Zero);
1613 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001614 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001615 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001616
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001617 if (!EvaluateInteger(SubExpr, Result, Info))
1618 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001619
Eli Friedman1725f682009-04-22 19:23:09 +00001620 if (EltType->isRealFloatingType()) {
1621 APFloat FResult =
1622 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001623 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001624 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1625 } else {
1626 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1627 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1628 Zero = 0;
1629 return APValue(Result, Zero);
1630 }
John McCall183700f2009-09-21 23:43:11 +00001631 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001632 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001633
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001634 if (!EvaluateComplex(SubExpr, Src, Info))
1635 return APValue();
1636
1637 QualType SrcType = CT->getElementType();
1638
1639 if (Src.isComplexFloat()) {
1640 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001641 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001642 Src.getComplexFloatReal(),
1643 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001644 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001645 Src.getComplexFloatImag(),
1646 Info.Ctx));
1647 } else {
1648 return APValue(HandleFloatToIntCast(EltType, SrcType,
1649 Src.getComplexFloatReal(),
1650 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001651 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001652 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001653 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001654 }
1655 } else {
1656 assert(Src.isComplexInt() && "Invalid evaluate result.");
1657 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001658 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001659 Src.getComplexIntReal(),
1660 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001661 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001662 Src.getComplexIntImag(),
1663 Info.Ctx));
1664 } else {
1665 return APValue(HandleIntToIntCast(EltType, SrcType,
1666 Src.getComplexIntReal(),
1667 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001668 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001669 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001670 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001671 }
1672 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001673 }
1674
1675 // FIXME: Handle more casts.
1676 return APValue();
1677 }
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001679 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001680 APValue VisitChooseExpr(const ChooseExpr *E)
1681 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1682 APValue VisitUnaryExtension(const UnaryOperator *E)
1683 { return Visit(E->getSubExpr()); }
1684 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001685 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001686};
1687} // end anonymous namespace
1688
Mike Stump1eb44332009-09-09 15:08:12 +00001689static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001690 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1691 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001692 (&Result.getComplexFloatReal().getSemantics() ==
1693 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001694 "Invalid complex evaluation.");
1695 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001696}
1697
Mike Stump1eb44332009-09-09 15:08:12 +00001698APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001699 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001701 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001702 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001704 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001705 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001706
Daniel Dunbar3f279872009-01-29 01:32:56 +00001707 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1708 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001709 switch (E->getOpcode()) {
1710 default: return APValue();
1711 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001712 if (Result.isComplexFloat()) {
1713 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1714 APFloat::rmNearestTiesToEven);
1715 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1716 APFloat::rmNearestTiesToEven);
1717 } else {
1718 Result.getComplexIntReal() += RHS.getComplexIntReal();
1719 Result.getComplexIntImag() += RHS.getComplexIntImag();
1720 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001721 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001722 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001723 if (Result.isComplexFloat()) {
1724 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1725 APFloat::rmNearestTiesToEven);
1726 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1727 APFloat::rmNearestTiesToEven);
1728 } else {
1729 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1730 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1731 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001732 break;
1733 case BinaryOperator::Mul:
1734 if (Result.isComplexFloat()) {
1735 APValue LHS = Result;
1736 APFloat &LHS_r = LHS.getComplexFloatReal();
1737 APFloat &LHS_i = LHS.getComplexFloatImag();
1738 APFloat &RHS_r = RHS.getComplexFloatReal();
1739 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Daniel Dunbar3f279872009-01-29 01:32:56 +00001741 APFloat Tmp = LHS_r;
1742 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1743 Result.getComplexFloatReal() = Tmp;
1744 Tmp = LHS_i;
1745 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1746 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1747
1748 Tmp = LHS_r;
1749 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1750 Result.getComplexFloatImag() = Tmp;
1751 Tmp = LHS_i;
1752 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1753 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1754 } else {
1755 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001756 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001757 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1758 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001759 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001760 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1761 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1762 }
1763 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001764 }
1765
1766 return Result;
1767}
1768
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001769//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001770// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001771//===----------------------------------------------------------------------===//
1772
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001773/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001774/// any crazy technique (that has nothing to do with language standards) that
1775/// we want to. If this function returns true, it returns the folded constant
1776/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001777bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1778 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001779
Nate Begeman59b5da62009-01-18 03:20:47 +00001780 if (getType()->isVectorType()) {
1781 if (!EvaluateVector(this, Result.Val, Info))
1782 return false;
1783 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001784 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001785 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001786 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001787 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001788 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001789 } else if (getType()->isRealFloatingType()) {
1790 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001791 if (!EvaluateFloat(this, f, Info))
1792 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001794 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001795 } else if (getType()->isAnyComplexType()) {
1796 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001797 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001798 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001799 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001800
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001801 return true;
1802}
1803
Anders Carlsson1b782762009-04-10 04:54:13 +00001804bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1805 EvalInfo Info(Ctx, Result);
1806
1807 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1808}
1809
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001810bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1811 EvalInfo Info(Ctx, Result, true);
1812
1813 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1814}
1815
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001816/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001817/// folded, but discard the result.
1818bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001819 EvalResult Result;
1820 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001821}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001822
1823APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001824 EvalResult EvalResult;
1825 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001826 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001827 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001828 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001829
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001830 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001831}