blob: 9129558421421c7792f79311552e9eef7aa8bb6a [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);
Mike Stump64eda9e2009-10-26 18:35:08 +0000876
877 case Builtin::BI__builtin_object_size: {
878 llvm::APSInt Result(32);
879
880 if (!E->getArg(1)->isIntegerConstantExpr(Result, Info.Ctx))
881 assert(0 && "arg2 not ice in __builtin_object_size");
882
883 const Expr *Arg = E->getArg(0)->IgnoreParens();
884 Expr::EvalResult Base;
885 if (Arg->Evaluate(Base, Info.Ctx)
886 && Base.Val.getKind() == APValue::LValue
887 && !Base.HasSideEffects)
888 if (const Expr *LVBase = Base.Val.getLValueBase())
889 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
890 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
891 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
892 Size -= Base.Val.getLValueOffset();
893 return Success(Size, E);
894 }
895 }
896
897 if (Base.HasSideEffects) {
898 if (Result.getSExtValue() < 2)
899 return Success(-1, E);
900 return Success(0, E);
901 }
902
903 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
904 }
905
Chris Lattner019f4e82008-10-06 05:28:25 +0000906 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000907 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000909 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000910 // __builtin_constant_p always has one operand: it returns true if that
911 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000912 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000913
914 case Builtin::BI__builtin_eh_return_data_regno: {
915 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
916 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
917 return Success(Operand, E);
918 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000919 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000920}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000921
Chris Lattnerb542afe2008-07-11 19:10:17 +0000922bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000923 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000924 if (!Visit(E->getRHS()))
925 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000926
Eli Friedman33ef1452009-02-26 10:19:36 +0000927 // If we can't evaluate the LHS, it might have side effects;
928 // conservatively mark it.
929 if (!E->getLHS()->isEvaluatable(Info.Ctx))
930 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000931
Anders Carlsson027f62e2008-12-01 02:07:06 +0000932 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000933 }
934
935 if (E->isLogicalOp()) {
936 // These need to be handled specially because the operands aren't
937 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000938 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000940 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000941 // We were able to evaluate the LHS, see if we can get away with not
942 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000943 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000944 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000945
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000946 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000947 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000948 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000949 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000950 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000951 }
952 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000953 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000954 // We can't evaluate the LHS; however, sometimes the result
955 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000956 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000957 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000958 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000959 // must have had side effects.
960 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000961
962 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000963 }
964 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000965 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000966
Eli Friedmana6afa762008-11-13 06:09:17 +0000967 return false;
968 }
969
Anders Carlsson286f85e2008-11-16 07:17:21 +0000970 QualType LHSTy = E->getLHS()->getType();
971 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000972
973 if (LHSTy->isAnyComplexType()) {
974 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
975 APValue LHS, RHS;
976
977 if (!EvaluateComplex(E->getLHS(), LHS, Info))
978 return false;
979
980 if (!EvaluateComplex(E->getRHS(), RHS, Info))
981 return false;
982
983 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000984 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000985 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000986 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000987 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
988
Daniel Dunbar4087e242009-01-29 06:43:41 +0000989 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000990 return Success((CR_r == APFloat::cmpEqual &&
991 CR_i == APFloat::cmpEqual), E);
992 else {
993 assert(E->getOpcode() == BinaryOperator::NE &&
994 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000995 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000996 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000997 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000998 CR_i == APFloat::cmpLessThan)), E);
999 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001000 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001001 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001002 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1003 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1004 else {
1005 assert(E->getOpcode() == BinaryOperator::NE &&
1006 "Invalid compex comparison.");
1007 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1008 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1009 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001010 }
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Anders Carlsson286f85e2008-11-16 07:17:21 +00001013 if (LHSTy->isRealFloatingType() &&
1014 RHSTy->isRealFloatingType()) {
1015 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Anders Carlsson286f85e2008-11-16 07:17:21 +00001017 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1018 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Anders Carlsson286f85e2008-11-16 07:17:21 +00001020 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1021 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Anders Carlsson286f85e2008-11-16 07:17:21 +00001023 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001024
Anders Carlsson286f85e2008-11-16 07:17:21 +00001025 switch (E->getOpcode()) {
1026 default:
1027 assert(0 && "Invalid binary operator!");
1028 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001029 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001030 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001031 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001032 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001033 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001034 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001035 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001036 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001037 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001038 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001039 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001040 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001041 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001042 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001045 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1046 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001047 APValue LHSValue;
1048 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1049 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001050
Anders Carlsson3068d112008-11-16 19:01:22 +00001051 APValue RHSValue;
1052 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1053 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001054
Eli Friedman5bc86102009-06-14 02:17:33 +00001055 // Reject any bases from the normal codepath; we special-case comparisons
1056 // to null.
1057 if (LHSValue.getLValueBase()) {
1058 if (!E->isEqualityOp())
1059 return false;
1060 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1061 return false;
1062 bool bres;
1063 if (!EvalPointerValueAsBool(LHSValue, bres))
1064 return false;
1065 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1066 } else if (RHSValue.getLValueBase()) {
1067 if (!E->isEqualityOp())
1068 return false;
1069 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1070 return false;
1071 bool bres;
1072 if (!EvalPointerValueAsBool(RHSValue, bres))
1073 return false;
1074 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1075 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001076
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001077 if (E->getOpcode() == BinaryOperator::Sub) {
1078 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001079 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001080
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001081 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001082 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1083 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001084
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001085 return Success(D, E);
1086 }
1087 bool Result;
1088 if (E->getOpcode() == BinaryOperator::EQ) {
1089 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001090 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001091 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1092 }
1093 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001094 }
1095 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001096 if (!LHSTy->isIntegralType() ||
1097 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001098 // We can't continue from here for non-integral types, and they
1099 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001100 return false;
1101 }
1102
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001103 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001104 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001105 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001106
Eli Friedman42edd0d2009-03-24 01:14:50 +00001107 APValue RHSVal;
1108 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001109 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001110
1111 // Handle cases like (unsigned long)&a + 4.
1112 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1113 uint64_t offset = Result.getLValueOffset();
1114 if (E->getOpcode() == BinaryOperator::Add)
1115 offset += RHSVal.getInt().getZExtValue();
1116 else
1117 offset -= RHSVal.getInt().getZExtValue();
1118 Result = APValue(Result.getLValueBase(), offset);
1119 return true;
1120 }
1121
1122 // Handle cases like 4 + (unsigned long)&a
1123 if (E->getOpcode() == BinaryOperator::Add &&
1124 RHSVal.isLValue() && Result.isInt()) {
1125 uint64_t offset = RHSVal.getLValueOffset();
1126 offset += Result.getInt().getZExtValue();
1127 Result = APValue(RHSVal.getLValueBase(), offset);
1128 return true;
1129 }
1130
1131 // All the following cases expect both operands to be an integer
1132 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001133 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001134
Eli Friedman42edd0d2009-03-24 01:14:50 +00001135 APSInt& RHS = RHSVal.getInt();
1136
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001137 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001138 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001139 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001140 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1141 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1142 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1143 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1144 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1145 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001146 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001147 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001148 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001149 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001150 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001151 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001152 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001153 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001154 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001155 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001156 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001157 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1158 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001159 }
1160 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001161 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001162 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1163 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001166 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1167 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1168 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1169 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1170 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1171 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001172 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001173}
1174
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001175bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001176 bool Cond;
1177 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001178 return false;
1179
Nuno Lopesa25bd552008-11-16 22:06:39 +00001180 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001181}
1182
Chris Lattneraf707ab2009-01-24 21:53:27 +00001183unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001184 // Get information about the alignment.
1185 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001186
Eli Friedman2be58612009-05-30 21:09:44 +00001187 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001188 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001189}
1190
Chris Lattneraf707ab2009-01-24 21:53:27 +00001191unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1192 E = E->IgnoreParens();
1193
1194 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001195 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001196 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001197 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001198
Chris Lattneraf707ab2009-01-24 21:53:27 +00001199 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001200 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001201
Chris Lattnere9feb472009-01-24 21:09:06 +00001202 return GetAlignOfType(E->getType());
1203}
1204
1205
Sebastian Redl05189992008-11-11 17:56:53 +00001206/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1207/// expression's type.
1208bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1209 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001210
Chris Lattnere9feb472009-01-24 21:09:06 +00001211 // Handle alignof separately.
1212 if (!E->isSizeOf()) {
1213 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001214 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001215 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001216 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001217 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001218
Sebastian Redl05189992008-11-11 17:56:53 +00001219 QualType SrcTy = E->getTypeOfArgument();
1220
Daniel Dunbar131eb432009-02-19 09:06:44 +00001221 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1222 // extension.
1223 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1224 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001225
Chris Lattnerfcee0012008-07-11 21:24:13 +00001226 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001227 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001228 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001229
Chris Lattnere9feb472009-01-24 21:09:06 +00001230 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001231 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001232 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001233}
1234
Chris Lattnerb542afe2008-07-11 19:10:17 +00001235bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001236 // Special case unary operators that do not need their subexpression
1237 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001238 if (E->isOffsetOfOp()) {
1239 // The AST for offsetof is defined in such a way that we can just
1240 // directly Evaluate it as an l-value.
1241 APValue LV;
1242 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1243 return false;
1244 if (LV.getLValueBase())
1245 return false;
1246 return Success(LV.getLValueOffset(), E);
1247 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001248
1249 if (E->getOpcode() == UnaryOperator::LNot) {
1250 // LNot's operand isn't necessarily an integer, so we handle it specially.
1251 bool bres;
1252 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1253 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001254 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001255 }
1256
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001257 // Only handle integral operations...
1258 if (!E->getSubExpr()->getType()->isIntegralType())
1259 return false;
1260
Chris Lattner87eae5e2008-07-11 22:52:41 +00001261 // Get the operand value into 'Result'.
1262 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001263 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001264
Chris Lattner75a48812008-07-11 22:15:16 +00001265 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001266 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001267 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1268 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001269 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001270 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001271 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1272 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001273 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001274 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001275 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001276 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001277 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001278 if (!Result.isInt()) return false;
1279 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001280 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001281 if (!Result.isInt()) return false;
1282 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001283 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001284}
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Chris Lattner732b2232008-07-12 01:15:53 +00001286/// HandleCast - This is used to evaluate implicit or explicit casts where the
1287/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001288bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001289 Expr *SubExpr = E->getSubExpr();
1290 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001291 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001292
Eli Friedman4efaa272008-11-12 09:44:48 +00001293 if (DestType->isBooleanType()) {
1294 bool BoolResult;
1295 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1296 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001297 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001298 }
1299
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001300 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001301 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001302 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001303 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001304
Eli Friedmanbe265702009-02-20 01:15:07 +00001305 if (!Result.isInt()) {
1306 // Only allow casts of lvalues if they are lossless.
1307 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1308 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001309
Daniel Dunbardd211642009-02-19 22:24:01 +00001310 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001311 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattner732b2232008-07-12 01:15:53 +00001314 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001315 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001316 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001317 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001318 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001319
Daniel Dunbardd211642009-02-19 22:24:01 +00001320 if (LV.getLValueBase()) {
1321 // Only allow based lvalue casts if they are lossless.
1322 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1323 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001324
Daniel Dunbardd211642009-02-19 22:24:01 +00001325 Result = LV;
1326 return true;
1327 }
1328
1329 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1330 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001331 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001332
Eli Friedmanbe265702009-02-20 01:15:07 +00001333 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1334 // This handles double-conversion cases, where there's both
1335 // an l-value promotion and an implicit conversion to int.
1336 APValue LV;
1337 if (!EvaluateLValue(SubExpr, LV, Info))
1338 return false;
1339
1340 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1341 return false;
1342
1343 Result = LV;
1344 return true;
1345 }
1346
Eli Friedman1725f682009-04-22 19:23:09 +00001347 if (SrcType->isAnyComplexType()) {
1348 APValue C;
1349 if (!EvaluateComplex(SubExpr, C, Info))
1350 return false;
1351 if (C.isComplexFloat())
1352 return Success(HandleFloatToIntCast(DestType, SrcType,
1353 C.getComplexFloatReal(), Info.Ctx),
1354 E);
1355 else
1356 return Success(HandleIntToIntCast(DestType, SrcType,
1357 C.getComplexIntReal(), Info.Ctx), E);
1358 }
Eli Friedman2217c872009-02-22 11:46:18 +00001359 // FIXME: Handle vectors
1360
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001361 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001362 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001363
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001364 APFloat F(0.0);
1365 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001366 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001368 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001369}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001370
Eli Friedman722c7172009-02-28 03:59:05 +00001371bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1372 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1373 APValue LV;
1374 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1375 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1376 return Success(LV.getComplexIntReal(), E);
1377 }
1378
1379 return Visit(E->getSubExpr());
1380}
1381
Eli Friedman664a1042009-02-27 04:45:43 +00001382bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001383 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1384 APValue LV;
1385 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1386 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1387 return Success(LV.getComplexIntImag(), E);
1388 }
1389
Eli Friedman664a1042009-02-27 04:45:43 +00001390 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1391 Info.EvalResult.HasSideEffects = true;
1392 return Success(0, E);
1393}
1394
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001395//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001396// Float Evaluation
1397//===----------------------------------------------------------------------===//
1398
1399namespace {
1400class VISIBILITY_HIDDEN FloatExprEvaluator
1401 : public StmtVisitor<FloatExprEvaluator, bool> {
1402 EvalInfo &Info;
1403 APFloat &Result;
1404public:
1405 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1406 : Info(info), Result(result) {}
1407
1408 bool VisitStmt(Stmt *S) {
1409 return false;
1410 }
1411
1412 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001413 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001414
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001415 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001416 bool VisitBinaryOperator(const BinaryOperator *E);
1417 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001418 bool VisitCastExpr(CastExpr *E);
1419 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001420
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001421 bool VisitChooseExpr(const ChooseExpr *E)
1422 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1423 bool VisitUnaryExtension(const UnaryOperator *E)
1424 { return Visit(E->getSubExpr()); }
1425
1426 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1427 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001428 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001429};
1430} // end anonymous namespace
1431
1432static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1433 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1434}
1435
Chris Lattner019f4e82008-10-06 05:28:25 +00001436bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001437 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001438 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001439 case Builtin::BI__builtin_huge_val:
1440 case Builtin::BI__builtin_huge_valf:
1441 case Builtin::BI__builtin_huge_vall:
1442 case Builtin::BI__builtin_inf:
1443 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001444 case Builtin::BI__builtin_infl: {
1445 const llvm::fltSemantics &Sem =
1446 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001447 Result = llvm::APFloat::getInf(Sem);
1448 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Chris Lattner9e621712008-10-06 06:31:58 +00001451 case Builtin::BI__builtin_nan:
1452 case Builtin::BI__builtin_nanf:
1453 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001454 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001455 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001456 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001457 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001458 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001459 const llvm::fltSemantics &Sem =
1460 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001461 llvm::SmallString<16> s;
1462 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1463 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001464 long l;
1465 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001466 l = strtol(&s[0], &endp, 0);
1467 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001468 return false;
1469 unsigned type = (unsigned int)l;;
1470 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001471 return true;
1472 }
1473 }
1474 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001475
1476 case Builtin::BI__builtin_fabs:
1477 case Builtin::BI__builtin_fabsf:
1478 case Builtin::BI__builtin_fabsl:
1479 if (!EvaluateFloat(E->getArg(0), Result, Info))
1480 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001482 if (Result.isNegative())
1483 Result.changeSign();
1484 return true;
1485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 case Builtin::BI__builtin_copysign:
1487 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001488 case Builtin::BI__builtin_copysignl: {
1489 APFloat RHS(0.);
1490 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1491 !EvaluateFloat(E->getArg(1), RHS, Info))
1492 return false;
1493 Result.copySign(RHS);
1494 return true;
1495 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001496 }
1497}
1498
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001499bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001500 if (E->getOpcode() == UnaryOperator::Deref)
1501 return false;
1502
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001503 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1504 return false;
1505
1506 switch (E->getOpcode()) {
1507 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001508 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001509 return true;
1510 case UnaryOperator::Minus:
1511 Result.changeSign();
1512 return true;
1513 }
1514}
Chris Lattner019f4e82008-10-06 05:28:25 +00001515
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001516bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1517 // FIXME: Diagnostics? I really don't understand how the warnings
1518 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001519 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001520 if (!EvaluateFloat(E->getLHS(), Result, Info))
1521 return false;
1522 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1523 return false;
1524
1525 switch (E->getOpcode()) {
1526 default: return false;
1527 case BinaryOperator::Mul:
1528 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1529 return true;
1530 case BinaryOperator::Add:
1531 Result.add(RHS, APFloat::rmNearestTiesToEven);
1532 return true;
1533 case BinaryOperator::Sub:
1534 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1535 return true;
1536 case BinaryOperator::Div:
1537 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1538 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001539 }
1540}
1541
1542bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1543 Result = E->getValue();
1544 return true;
1545}
1546
Eli Friedman4efaa272008-11-12 09:44:48 +00001547bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1548 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Eli Friedman4efaa272008-11-12 09:44:48 +00001550 if (SubExpr->getType()->isIntegralType()) {
1551 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001552 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001553 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001554 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001555 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001556 return true;
1557 }
1558 if (SubExpr->getType()->isRealFloatingType()) {
1559 if (!Visit(SubExpr))
1560 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001561 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1562 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001563 return true;
1564 }
Eli Friedman2217c872009-02-22 11:46:18 +00001565 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001566
1567 return false;
1568}
1569
1570bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1571 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1572 return true;
1573}
1574
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001575//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001576// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001577//===----------------------------------------------------------------------===//
1578
1579namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001580class VISIBILITY_HIDDEN ComplexExprEvaluator
1581 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001582 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001584public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001585 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001587 //===--------------------------------------------------------------------===//
1588 // Visitor Methods
1589 //===--------------------------------------------------------------------===//
1590
1591 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001592 return APValue();
1593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001595 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1596
1597 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001598 Expr* SubExpr = E->getSubExpr();
1599
1600 if (SubExpr->getType()->isRealFloatingType()) {
1601 APFloat Result(0.0);
1602
1603 if (!EvaluateFloat(SubExpr, Result, Info))
1604 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001605
1606 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001607 Result);
1608 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001609 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001610 "Unexpected imaginary literal.");
1611
1612 llvm::APSInt Result;
1613 if (!EvaluateInteger(SubExpr, Result, Info))
1614 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001616 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1617 Zero = 0;
1618 return APValue(Zero, Result);
1619 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001620 }
1621
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001622 APValue VisitCastExpr(CastExpr *E) {
1623 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001624 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001625 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001626
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001627 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001628 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001629
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001630 if (!EvaluateFloat(SubExpr, Result, Info))
1631 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001632
1633 if (EltType->isRealFloatingType()) {
1634 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001635 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001636 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1637 } else {
1638 llvm::APSInt IResult;
1639 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1640 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1641 Zero = 0;
1642 return APValue(IResult, Zero);
1643 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001644 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001645 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001646
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001647 if (!EvaluateInteger(SubExpr, Result, Info))
1648 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001649
Eli Friedman1725f682009-04-22 19:23:09 +00001650 if (EltType->isRealFloatingType()) {
1651 APFloat FResult =
1652 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001653 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001654 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1655 } else {
1656 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1657 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1658 Zero = 0;
1659 return APValue(Result, Zero);
1660 }
John McCall183700f2009-09-21 23:43:11 +00001661 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001662 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001663
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001664 if (!EvaluateComplex(SubExpr, Src, Info))
1665 return APValue();
1666
1667 QualType SrcType = CT->getElementType();
1668
1669 if (Src.isComplexFloat()) {
1670 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001671 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001672 Src.getComplexFloatReal(),
1673 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001674 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001675 Src.getComplexFloatImag(),
1676 Info.Ctx));
1677 } else {
1678 return APValue(HandleFloatToIntCast(EltType, SrcType,
1679 Src.getComplexFloatReal(),
1680 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001681 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001682 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001683 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001684 }
1685 } else {
1686 assert(Src.isComplexInt() && "Invalid evaluate result.");
1687 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001688 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001689 Src.getComplexIntReal(),
1690 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001691 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001692 Src.getComplexIntImag(),
1693 Info.Ctx));
1694 } else {
1695 return APValue(HandleIntToIntCast(EltType, SrcType,
1696 Src.getComplexIntReal(),
1697 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001698 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001699 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001700 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001701 }
1702 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001703 }
1704
1705 // FIXME: Handle more casts.
1706 return APValue();
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001709 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001710 APValue VisitChooseExpr(const ChooseExpr *E)
1711 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1712 APValue VisitUnaryExtension(const UnaryOperator *E)
1713 { return Visit(E->getSubExpr()); }
1714 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001715 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001716};
1717} // end anonymous namespace
1718
Mike Stump1eb44332009-09-09 15:08:12 +00001719static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001720 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1721 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001722 (&Result.getComplexFloatReal().getSemantics() ==
1723 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001724 "Invalid complex evaluation.");
1725 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001726}
1727
Mike Stump1eb44332009-09-09 15:08:12 +00001728APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001729 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001731 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001732 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001734 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001735 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001736
Daniel Dunbar3f279872009-01-29 01:32:56 +00001737 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1738 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001739 switch (E->getOpcode()) {
1740 default: return APValue();
1741 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001742 if (Result.isComplexFloat()) {
1743 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1744 APFloat::rmNearestTiesToEven);
1745 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1746 APFloat::rmNearestTiesToEven);
1747 } else {
1748 Result.getComplexIntReal() += RHS.getComplexIntReal();
1749 Result.getComplexIntImag() += RHS.getComplexIntImag();
1750 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001751 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001752 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001753 if (Result.isComplexFloat()) {
1754 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1755 APFloat::rmNearestTiesToEven);
1756 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1757 APFloat::rmNearestTiesToEven);
1758 } else {
1759 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1760 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1761 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001762 break;
1763 case BinaryOperator::Mul:
1764 if (Result.isComplexFloat()) {
1765 APValue LHS = Result;
1766 APFloat &LHS_r = LHS.getComplexFloatReal();
1767 APFloat &LHS_i = LHS.getComplexFloatImag();
1768 APFloat &RHS_r = RHS.getComplexFloatReal();
1769 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Daniel Dunbar3f279872009-01-29 01:32:56 +00001771 APFloat Tmp = LHS_r;
1772 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1773 Result.getComplexFloatReal() = Tmp;
1774 Tmp = LHS_i;
1775 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1776 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1777
1778 Tmp = LHS_r;
1779 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1780 Result.getComplexFloatImag() = Tmp;
1781 Tmp = LHS_i;
1782 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1783 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1784 } else {
1785 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001786 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001787 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1788 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001789 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001790 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1791 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1792 }
1793 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001794 }
1795
1796 return Result;
1797}
1798
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001799//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001800// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001801//===----------------------------------------------------------------------===//
1802
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001803/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001804/// any crazy technique (that has nothing to do with language standards) that
1805/// we want to. If this function returns true, it returns the folded constant
1806/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001807bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1808 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001809
Nate Begeman59b5da62009-01-18 03:20:47 +00001810 if (getType()->isVectorType()) {
1811 if (!EvaluateVector(this, Result.Val, Info))
1812 return false;
1813 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001814 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001815 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001816 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001817 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001818 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001819 } else if (getType()->isRealFloatingType()) {
1820 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001821 if (!EvaluateFloat(this, f, Info))
1822 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001824 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001825 } else if (getType()->isAnyComplexType()) {
1826 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001827 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001828 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001829 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001830
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001831 return true;
1832}
1833
Anders Carlsson1b782762009-04-10 04:54:13 +00001834bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1835 EvalInfo Info(Ctx, Result);
1836
1837 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1838}
1839
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001840bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1841 EvalInfo Info(Ctx, Result, true);
1842
1843 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1844}
1845
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001846/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001847/// folded, but discard the result.
1848bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001849 EvalResult Result;
1850 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001851}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001852
1853APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001854 EvalResult EvalResult;
1855 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001856 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001857 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001858 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001859
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001860 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001861}