blob: dc53289165aae5416177fd7a835cf7fc63dad2d9 [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
Mike Stumpc4c90452009-10-27 22:09:17 +0000154namespace {
155class VISIBILITY_HIDDEN HasSideEffect
156 : public StmtVisitor<HasSideEffect, bool> {
157 EvalInfo &Info;
158public:
159
160 HasSideEffect(EvalInfo &info) : Info(info) {}
161
162 // Unhandled nodes conservatively default to having side effects.
163 bool VisitStmt(Stmt *S) {
164 return true;
165 }
166
167 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
168 bool VisitDeclRefExpr(DeclRefExpr *E) {
169 if (E->getType().isVolatileQualified())
170 return true;
171 return false;
172 }
173 // We don't want to evaluate BlockExprs multiple times, as they generate
174 // a ton of code.
175 bool VisitBlockExpr(BlockExpr *E) { return true; }
176 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
177 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
178 { return Visit(E->getInitializer()); }
179 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
180 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
181 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
182 bool VisitStringLiteral(StringLiteral *E) { return false; }
183 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
184 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
185 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
186 { return Visit(E->getLHS()) && Visit(E->getRHS()); }
187 bool VisitChooseExpr(ChooseExpr *E)
188 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
189 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
190 bool VisitBinAssign(BinaryOperator *E) { return true; }
191 bool VisitCompoundAssign(BinaryOperator *E) { return true; }
192 bool VisitBinaryOperator(BinaryOperator *E) { return false; }
193 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
194 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
195 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
197 bool VisitUnaryDeref(UnaryOperator *E) {
198 if (E->getType().isVolatileQualified())
199 return true;
200 return false;
201 }
202 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
203};
204
205bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
206 Expr::EvalResult Result;
207 EvalInfo Info(Ctx, Result);
208
209 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
210}
211
212} // end anonymous namespace
213
Eli Friedman4efaa272008-11-12 09:44:48 +0000214//===----------------------------------------------------------------------===//
215// LValue Evaluation
216//===----------------------------------------------------------------------===//
217namespace {
218class VISIBILITY_HIDDEN LValueExprEvaluator
219 : public StmtVisitor<LValueExprEvaluator, APValue> {
220 EvalInfo &Info;
221public:
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Eli Friedman4efaa272008-11-12 09:44:48 +0000223 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
224
225 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000226 return APValue();
227 }
228
229 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000230 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000231 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000232 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
233 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
234 APValue VisitMemberExpr(MemberExpr *E);
235 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000236 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000237 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000238 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000239 APValue VisitUnaryExtension(const UnaryOperator *E)
240 { return Visit(E->getSubExpr()); }
241 APValue VisitChooseExpr(const ChooseExpr *E)
242 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000243
244 APValue VisitCastExpr(CastExpr *E) {
245 switch (E->getCastKind()) {
246 default:
247 return APValue();
248
249 case CastExpr::CK_NoOp:
250 return Visit(E->getSubExpr());
251 }
252 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000253 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000254};
255} // end anonymous namespace
256
257static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
258 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
259 return Result.isLValue();
260}
261
Mike Stump1eb44332009-09-09 15:08:12 +0000262APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000263 if (isa<FunctionDecl>(E->getDecl())) {
264 return APValue(E, 0);
265 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000266 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000267 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000268 if (!VD->getType()->isReferenceType())
269 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000270 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000271 if (VD->getInit())
272 return Visit(VD->getInit());
273 }
274
275 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000276}
277
Mike Stump1eb44332009-09-09 15:08:12 +0000278APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000279 if (E->hasBlockDeclRefExprs())
280 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Steve Naroff3aaa4822009-04-16 19:02:57 +0000282 return APValue(E, 0);
283}
284
Eli Friedman4efaa272008-11-12 09:44:48 +0000285APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000286 if (!Info.AnyLValue && !E->isFileScope())
287 return APValue();
288 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000289}
290
291APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
292 APValue result;
293 QualType Ty;
294 if (E->isArrow()) {
295 if (!EvaluatePointer(E->getBase(), result, Info))
296 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000297 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000298 } else {
299 result = Visit(E->getBase());
300 if (result.isUninit())
301 return APValue();
302 Ty = E->getBase()->getType();
303 }
304
Ted Kremenek6217b802009-07-29 21:53:49 +0000305 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000306 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000307
308 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
309 if (!FD) // FIXME: deal with other kinds of member expressions
310 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000311
312 if (FD->getType()->isReferenceType())
313 return APValue();
314
Eli Friedman4efaa272008-11-12 09:44:48 +0000315 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000316 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000317 for (RecordDecl::field_iterator Field = RD->field_begin(),
318 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000319 Field != FieldEnd; (void)++Field, ++i) {
320 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000321 break;
322 }
323
324 result.setLValue(result.getLValueBase(),
325 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
326
327 return result;
328}
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000331 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Anders Carlsson3068d112008-11-16 19:01:22 +0000333 if (!EvaluatePointer(E->getBase(), Result, Info))
334 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlsson3068d112008-11-16 19:01:22 +0000336 APSInt Index;
337 if (!EvaluateInteger(E->getIdx(), Index, Info))
338 return APValue();
339
340 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
341
342 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000343 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000344 Result.getLValueOffset() + Offset);
345 return Result;
346}
Eli Friedman4efaa272008-11-12 09:44:48 +0000347
Mike Stump1eb44332009-09-09 15:08:12 +0000348APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000349 APValue Result;
350 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
351 return APValue();
352 return Result;
353}
354
Eli Friedman4efaa272008-11-12 09:44:48 +0000355//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000356// Pointer Evaluation
357//===----------------------------------------------------------------------===//
358
Anders Carlssonc754aa62008-07-08 05:13:58 +0000359namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000360class VISIBILITY_HIDDEN PointerExprEvaluator
361 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000362 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000363public:
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner87eae5e2008-07-11 22:52:41 +0000365 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000366
Anders Carlsson2bad1682008-07-08 14:30:00 +0000367 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000368 return APValue();
369 }
370
371 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
372
Anders Carlsson650c92f2008-07-08 15:34:11 +0000373 APValue VisitBinaryOperator(const BinaryOperator *E);
374 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000375 APValue VisitUnaryExtension(const UnaryOperator *E)
376 { return Visit(E->getSubExpr()); }
377 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000378 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
379 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000380 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
381 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000382 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000383 APValue VisitBlockExpr(BlockExpr *E) {
384 if (!E->hasBlockDeclRefExprs())
385 return APValue(E, 0);
386 return APValue();
387 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000388 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
389 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000390 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000391 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000392 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
393 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
394 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000395 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000396};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000397} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000398
Chris Lattner87eae5e2008-07-11 22:52:41 +0000399static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000400 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000401 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000402 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000403 return Result.isLValue();
404}
405
406APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
407 if (E->getOpcode() != BinaryOperator::Add &&
408 E->getOpcode() != BinaryOperator::Sub)
409 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000411 const Expr *PExp = E->getLHS();
412 const Expr *IExp = E->getRHS();
413 if (IExp->getType()->isPointerType())
414 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000417 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000418 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000420 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000421 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000422 return APValue();
423
Ted Kremenek6217b802009-07-29 21:53:49 +0000424 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000425 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000427 // Explicitly handle GNU void* and function pointer arithmetic extensions.
428 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
429 SizeOfPointee = 1;
430 else
431 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000432
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000433 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000434
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000435 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000436 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000437 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000438 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
439
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000440 return APValue(ResultLValue.getLValueBase(), Offset);
441}
Eli Friedman4efaa272008-11-12 09:44:48 +0000442
Eli Friedman2217c872009-02-22 11:46:18 +0000443APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
444 APValue result;
445 if (EvaluateLValue(E->getSubExpr(), result, Info))
446 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000447 return APValue();
448}
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000450
Chris Lattnerb542afe2008-07-11 19:10:17 +0000451APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000452 const Expr* SubExpr = E->getSubExpr();
453
454 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000455 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000456 SubExpr->getType()->isObjCObjectPointerType() ||
457 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000458 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000459 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000460 return Result;
461 return APValue();
462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000464 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000465 APValue Result;
466 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
467 return APValue();
468
469 if (Result.isInt()) {
470 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
471 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000474 // Cast is of an lvalue, no need to change value.
475 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000476 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000477
478 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000479 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000480 SubExpr->getType()->isArrayType()) {
481 APValue Result;
482 if (EvaluateLValue(SubExpr, Result, Info))
483 return Result;
484 return APValue();
485 }
486
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000487 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000488}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000489
Eli Friedman3941b182009-01-25 01:54:01 +0000490APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000491 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000492 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000493 return APValue(E, 0);
494 return APValue();
495}
496
Eli Friedman4efaa272008-11-12 09:44:48 +0000497APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
498 bool BoolResult;
499 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
500 return APValue();
501
502 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
503
504 APValue Result;
505 if (EvaluatePointer(EvalExpr, Result, Info))
506 return Result;
507 return APValue();
508}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000509
510//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000511// Vector Evaluation
512//===----------------------------------------------------------------------===//
513
514namespace {
515 class VISIBILITY_HIDDEN VectorExprEvaluator
516 : public StmtVisitor<VectorExprEvaluator, APValue> {
517 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000518 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000519 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Nate Begeman59b5da62009-01-18 03:20:47 +0000521 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Nate Begeman59b5da62009-01-18 03:20:47 +0000523 APValue VisitStmt(Stmt *S) {
524 return APValue();
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Eli Friedman91110ee2009-02-23 04:23:56 +0000527 APValue VisitParenExpr(ParenExpr *E)
528 { return Visit(E->getSubExpr()); }
529 APValue VisitUnaryExtension(const UnaryOperator *E)
530 { return Visit(E->getSubExpr()); }
531 APValue VisitUnaryPlus(const UnaryOperator *E)
532 { return Visit(E->getSubExpr()); }
533 APValue VisitUnaryReal(const UnaryOperator *E)
534 { return Visit(E->getSubExpr()); }
535 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
536 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000537 APValue VisitCastExpr(const CastExpr* E);
538 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
539 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000540 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000541 APValue VisitChooseExpr(const ChooseExpr *E)
542 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000543 APValue VisitUnaryImag(const UnaryOperator *E);
544 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000545 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000546 // shufflevector, ExtVectorElementExpr
547 // (Note that these require implementing conversions
548 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000549 };
550} // end anonymous namespace
551
552static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
553 if (!E->getType()->isVectorType())
554 return false;
555 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
556 return !Result.isUninit();
557}
558
559APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000560 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000561 QualType EltTy = VTy->getElementType();
562 unsigned NElts = VTy->getNumElements();
563 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Nate Begeman59b5da62009-01-18 03:20:47 +0000565 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000566 QualType SETy = SE->getType();
567 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000568
Nate Begemane8c9e922009-06-26 18:22:18 +0000569 // Check for vector->vector bitcast and scalar->vector splat.
570 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000571 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000572 } else if (SETy->isIntegerType()) {
573 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000574 if (!EvaluateInteger(SE, IntResult, Info))
575 return APValue();
576 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000577 } else if (SETy->isRealFloatingType()) {
578 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000579 if (!EvaluateFloat(SE, F, Info))
580 return APValue();
581 Result = APValue(F);
582 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000583 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000584
Nate Begemanc0b8b192009-07-01 07:50:47 +0000585 // For casts of a scalar to ExtVector, convert the scalar to the element type
586 // and splat it to all elements.
587 if (E->getType()->isExtVectorType()) {
588 if (EltTy->isIntegerType() && Result.isInt())
589 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
590 Info.Ctx));
591 else if (EltTy->isIntegerType())
592 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
593 Info.Ctx));
594 else if (EltTy->isRealFloatingType() && Result.isInt())
595 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
596 Info.Ctx));
597 else if (EltTy->isRealFloatingType())
598 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
599 Info.Ctx));
600 else
601 return APValue();
602
603 // Splat and create vector APValue.
604 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
605 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000606 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000607
608 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
609 // to the vector. To construct the APValue vector initializer, bitcast the
610 // initializing value to an APInt, and shift out the bits pertaining to each
611 // element.
612 APSInt Init;
613 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Nate Begemanc0b8b192009-07-01 07:50:47 +0000615 llvm::SmallVector<APValue, 4> Elts;
616 for (unsigned i = 0; i != NElts; ++i) {
617 APSInt Tmp = Init;
618 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Nate Begemanc0b8b192009-07-01 07:50:47 +0000620 if (EltTy->isIntegerType())
621 Elts.push_back(APValue(Tmp));
622 else if (EltTy->isRealFloatingType())
623 Elts.push_back(APValue(APFloat(Tmp)));
624 else
625 return APValue();
626
627 Init >>= EltWidth;
628 }
629 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000630}
631
Mike Stump1eb44332009-09-09 15:08:12 +0000632APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000633VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
634 return this->Visit(const_cast<Expr*>(E->getInitializer()));
635}
636
Mike Stump1eb44332009-09-09 15:08:12 +0000637APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000638VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000639 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000640 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000641 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Nate Begeman59b5da62009-01-18 03:20:47 +0000643 QualType EltTy = VT->getElementType();
644 llvm::SmallVector<APValue, 4> Elements;
645
Eli Friedman91110ee2009-02-23 04:23:56 +0000646 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000647 if (EltTy->isIntegerType()) {
648 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000649 if (i < NumInits) {
650 if (!EvaluateInteger(E->getInit(i), sInt, Info))
651 return APValue();
652 } else {
653 sInt = Info.Ctx.MakeIntValue(0, EltTy);
654 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000655 Elements.push_back(APValue(sInt));
656 } else {
657 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000658 if (i < NumInits) {
659 if (!EvaluateFloat(E->getInit(i), f, Info))
660 return APValue();
661 } else {
662 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
663 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000664 Elements.push_back(APValue(f));
665 }
666 }
667 return APValue(&Elements[0], Elements.size());
668}
669
Mike Stump1eb44332009-09-09 15:08:12 +0000670APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000671VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000672 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000673 QualType EltTy = VT->getElementType();
674 APValue ZeroElement;
675 if (EltTy->isIntegerType())
676 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
677 else
678 ZeroElement =
679 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
680
681 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
682 return APValue(&Elements[0], Elements.size());
683}
684
685APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
686 bool BoolResult;
687 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
688 return APValue();
689
690 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
691
692 APValue Result;
693 if (EvaluateVector(EvalExpr, Result, Info))
694 return Result;
695 return APValue();
696}
697
Eli Friedman91110ee2009-02-23 04:23:56 +0000698APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
699 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
700 Info.EvalResult.HasSideEffects = true;
701 return GetZeroVector(E->getType());
702}
703
Nate Begeman59b5da62009-01-18 03:20:47 +0000704//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000705// Integer Evaluation
706//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000707
708namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000709class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000710 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000711 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000712 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000713public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000714 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000715 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000716
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000717 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000718 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000719 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000720 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000721 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000722 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000723 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000724 return true;
725 }
726
Daniel Dunbar131eb432009-02-19 09:06:44 +0000727 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000728 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000729 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000730 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000731 Result = APValue(APSInt(I));
732 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000733 return true;
734 }
735
736 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000737 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000738 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000739 return true;
740 }
741
Anders Carlsson82206e22008-11-30 18:14:57 +0000742 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000743 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000744 if (Info.EvalResult.Diag == 0) {
745 Info.EvalResult.DiagLoc = L;
746 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000747 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000748 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000749 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Anders Carlssonc754aa62008-07-08 05:13:58 +0000752 //===--------------------------------------------------------------------===//
753 // Visitor Methods
754 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner32fea9d2008-11-12 07:43:42 +0000756 bool VisitStmt(Stmt *) {
757 assert(0 && "This should be called on integers, stmts are not integers");
758 return false;
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattner32fea9d2008-11-12 07:43:42 +0000761 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000762 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattnerb542afe2008-07-11 19:10:17 +0000765 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000766
Chris Lattner4c4867e2008-07-12 00:38:25 +0000767 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000768 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000769 }
770 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000771 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000772 }
773 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000774 // Per gcc docs "this built-in function ignores top level
775 // qualifiers". We need to use the canonical version to properly
776 // be able to strip CRV qualifiers from the type.
777 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
778 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000779 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000780 T1.getUnqualifiedType()),
781 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000782 }
783 bool VisitDeclRefExpr(const DeclRefExpr *E);
784 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000785 bool VisitBinaryOperator(const BinaryOperator *E);
786 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000787 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000788
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000789 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000790 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
791
Anders Carlsson3068d112008-11-16 19:01:22 +0000792 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000793 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Anders Carlsson3f704562008-12-21 22:39:40 +0000796 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000797 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Anders Carlsson3068d112008-11-16 19:01:22 +0000800 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000801 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000802 }
803
Eli Friedman664a1042009-02-27 04:45:43 +0000804 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
805 return Success(0, E);
806 }
807
Sebastian Redl64b45f72009-01-05 20:52:13 +0000808 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000809 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000810 }
811
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000812 bool VisitChooseExpr(const ChooseExpr *E) {
813 return Visit(E->getChosenSubExpr(Info.Ctx));
814 }
815
Eli Friedman722c7172009-02-28 03:59:05 +0000816 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000817 bool VisitUnaryImag(const UnaryOperator *E);
818
Chris Lattnerfcee0012008-07-11 21:24:13 +0000819private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000820 unsigned GetAlignOfExpr(const Expr *E);
821 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000822 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000823};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000824} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000825
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000826static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000827 if (!E->getType()->isIntegralType())
828 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000830 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
831}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000832
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000833static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
834 APValue Val;
835 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
836 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000837 Result = Val.getInt();
838 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000839}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000840
Chris Lattner4c4867e2008-07-12 00:38:25 +0000841bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
842 // Enums are integer constant exprs.
843 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000844 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000845 // signedness consistently; see PR3173.
846 APSInt SI = D->getInitVal();
847 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
848 // FIXME: This is an ugly hack around the fact that enums don't
849 // set their width (!?!) consistently; see PR3173.
850 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
851 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000852 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000853
854 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000855 // In C, they can also be folded, although they are not ICEs.
John McCall0953e762009-09-24 19:53:00 +0000856 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000857 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000858 if (APValue *V = D->getEvaluatedValue())
859 return Success(V->getInt(), E);
860 if (const Expr *Init = D->getInit()) {
861 if (Visit(const_cast<Expr*>(Init))) {
862 // Cache the evaluated value in the variable declaration.
863 D->setEvaluatedValue(Info.Ctx, Result);
864 return true;
865 }
866
867 return false;
868 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000869 }
870 }
871
Chris Lattner4c4867e2008-07-12 00:38:25 +0000872 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000873 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000874}
875
Chris Lattnera4d55d82008-10-06 06:40:35 +0000876/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
877/// as GCC.
878static int EvaluateBuiltinClassifyType(const CallExpr *E) {
879 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000880 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000881 enum gcc_type_class {
882 no_type_class = -1,
883 void_type_class, integer_type_class, char_type_class,
884 enumeral_type_class, boolean_type_class,
885 pointer_type_class, reference_type_class, offset_type_class,
886 real_type_class, complex_type_class,
887 function_type_class, method_type_class,
888 record_type_class, union_type_class,
889 array_type_class, string_type_class,
890 lang_type_class
891 };
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000894 // ideal, however it is what gcc does.
895 if (E->getNumArgs() == 0)
896 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattnera4d55d82008-10-06 06:40:35 +0000898 QualType ArgTy = E->getArg(0)->getType();
899 if (ArgTy->isVoidType())
900 return void_type_class;
901 else if (ArgTy->isEnumeralType())
902 return enumeral_type_class;
903 else if (ArgTy->isBooleanType())
904 return boolean_type_class;
905 else if (ArgTy->isCharType())
906 return string_type_class; // gcc doesn't appear to use char_type_class
907 else if (ArgTy->isIntegerType())
908 return integer_type_class;
909 else if (ArgTy->isPointerType())
910 return pointer_type_class;
911 else if (ArgTy->isReferenceType())
912 return reference_type_class;
913 else if (ArgTy->isRealType())
914 return real_type_class;
915 else if (ArgTy->isComplexType())
916 return complex_type_class;
917 else if (ArgTy->isFunctionType())
918 return function_type_class;
919 else if (ArgTy->isStructureType())
920 return record_type_class;
921 else if (ArgTy->isUnionType())
922 return union_type_class;
923 else if (ArgTy->isArrayType())
924 return array_type_class;
925 else if (ArgTy->isUnionType())
926 return union_type_class;
927 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
928 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
929 return -1;
930}
931
Chris Lattner4c4867e2008-07-12 00:38:25 +0000932bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000933 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000934 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000935 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000936
937 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000938 const Expr *Arg = E->getArg(0)->IgnoreParens();
939 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000940 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000941 && Base.Val.getKind() == APValue::LValue
942 && !Base.HasSideEffects)
943 if (const Expr *LVBase = Base.Val.getLValueBase())
944 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
945 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
946 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
Mike Stump06bc9bc2009-10-26 21:38:39 +0000947 uint64_t Offset = Base.Val.getLValueOffset();
948 if (Offset <= Size)
949 Size -= Base.Val.getLValueOffset();
950 else
951 Size = 0;
Mike Stump64eda9e2009-10-26 18:35:08 +0000952 return Success(Size, E);
953 }
954 }
955
Mike Stumpc4c90452009-10-27 22:09:17 +0000956 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump4fc87582009-10-26 18:57:47 +0000957 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump64eda9e2009-10-26 18:35:08 +0000958 return Success(-1, E);
959 return Success(0, E);
960 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000961
Mike Stump64eda9e2009-10-26 18:35:08 +0000962 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
963 }
964
Chris Lattner019f4e82008-10-06 05:28:25 +0000965 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000966 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000968 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000969 // __builtin_constant_p always has one operand: it returns true if that
970 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000971 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000972
973 case Builtin::BI__builtin_eh_return_data_regno: {
974 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
975 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
976 return Success(Operand, E);
977 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000978 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000979}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000980
Chris Lattnerb542afe2008-07-11 19:10:17 +0000981bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000982 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000983 if (!Visit(E->getRHS()))
984 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000985
Eli Friedman33ef1452009-02-26 10:19:36 +0000986 // If we can't evaluate the LHS, it might have side effects;
987 // conservatively mark it.
988 if (!E->getLHS()->isEvaluatable(Info.Ctx))
989 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000990
Anders Carlsson027f62e2008-12-01 02:07:06 +0000991 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000992 }
993
994 if (E->isLogicalOp()) {
995 // These need to be handled specially because the operands aren't
996 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000997 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000999 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001000 // We were able to evaluate the LHS, see if we can get away with not
1001 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001002 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001003 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001004
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001005 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001006 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001007 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001008 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001009 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001010 }
1011 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001012 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001013 // We can't evaluate the LHS; however, sometimes the result
1014 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001015 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001016 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001017 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001018 // must have had side effects.
1019 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001020
1021 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001022 }
1023 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001024 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001025
Eli Friedmana6afa762008-11-13 06:09:17 +00001026 return false;
1027 }
1028
Anders Carlsson286f85e2008-11-16 07:17:21 +00001029 QualType LHSTy = E->getLHS()->getType();
1030 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001031
1032 if (LHSTy->isAnyComplexType()) {
1033 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1034 APValue LHS, RHS;
1035
1036 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1037 return false;
1038
1039 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1040 return false;
1041
1042 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001043 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001044 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001045 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001046 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1047
Daniel Dunbar4087e242009-01-29 06:43:41 +00001048 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001049 return Success((CR_r == APFloat::cmpEqual &&
1050 CR_i == APFloat::cmpEqual), E);
1051 else {
1052 assert(E->getOpcode() == BinaryOperator::NE &&
1053 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001054 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001055 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001056 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001057 CR_i == APFloat::cmpLessThan)), E);
1058 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001059 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001060 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001061 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1062 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1063 else {
1064 assert(E->getOpcode() == BinaryOperator::NE &&
1065 "Invalid compex comparison.");
1066 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1067 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1068 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001069 }
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Anders Carlsson286f85e2008-11-16 07:17:21 +00001072 if (LHSTy->isRealFloatingType() &&
1073 RHSTy->isRealFloatingType()) {
1074 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Anders Carlsson286f85e2008-11-16 07:17:21 +00001076 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1077 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Anders Carlsson286f85e2008-11-16 07:17:21 +00001079 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1080 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Anders Carlsson286f85e2008-11-16 07:17:21 +00001082 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001083
Anders Carlsson286f85e2008-11-16 07:17:21 +00001084 switch (E->getOpcode()) {
1085 default:
1086 assert(0 && "Invalid binary operator!");
1087 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001088 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001089 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001090 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001091 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001092 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001093 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001094 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001095 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001096 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001097 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001098 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001099 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001100 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001101 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001104 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1105 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001106 APValue LHSValue;
1107 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1108 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001109
Anders Carlsson3068d112008-11-16 19:01:22 +00001110 APValue RHSValue;
1111 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1112 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001113
Eli Friedman5bc86102009-06-14 02:17:33 +00001114 // Reject any bases from the normal codepath; we special-case comparisons
1115 // to null.
1116 if (LHSValue.getLValueBase()) {
1117 if (!E->isEqualityOp())
1118 return false;
1119 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1120 return false;
1121 bool bres;
1122 if (!EvalPointerValueAsBool(LHSValue, bres))
1123 return false;
1124 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1125 } else if (RHSValue.getLValueBase()) {
1126 if (!E->isEqualityOp())
1127 return false;
1128 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1129 return false;
1130 bool bres;
1131 if (!EvalPointerValueAsBool(RHSValue, bres))
1132 return false;
1133 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1134 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001135
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001136 if (E->getOpcode() == BinaryOperator::Sub) {
1137 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001138 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001139
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001140 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001141 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1142 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001143
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001144 return Success(D, E);
1145 }
1146 bool Result;
1147 if (E->getOpcode() == BinaryOperator::EQ) {
1148 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001149 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001150 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1151 }
1152 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001153 }
1154 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001155 if (!LHSTy->isIntegralType() ||
1156 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001157 // We can't continue from here for non-integral types, and they
1158 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001159 return false;
1160 }
1161
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001162 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001163 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001164 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001165
Eli Friedman42edd0d2009-03-24 01:14:50 +00001166 APValue RHSVal;
1167 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001168 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001169
1170 // Handle cases like (unsigned long)&a + 4.
1171 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1172 uint64_t offset = Result.getLValueOffset();
1173 if (E->getOpcode() == BinaryOperator::Add)
1174 offset += RHSVal.getInt().getZExtValue();
1175 else
1176 offset -= RHSVal.getInt().getZExtValue();
1177 Result = APValue(Result.getLValueBase(), offset);
1178 return true;
1179 }
1180
1181 // Handle cases like 4 + (unsigned long)&a
1182 if (E->getOpcode() == BinaryOperator::Add &&
1183 RHSVal.isLValue() && Result.isInt()) {
1184 uint64_t offset = RHSVal.getLValueOffset();
1185 offset += Result.getInt().getZExtValue();
1186 Result = APValue(RHSVal.getLValueBase(), offset);
1187 return true;
1188 }
1189
1190 // All the following cases expect both operands to be an integer
1191 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001192 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001193
Eli Friedman42edd0d2009-03-24 01:14:50 +00001194 APSInt& RHS = RHSVal.getInt();
1195
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001196 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001197 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001198 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001199 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1200 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1201 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1202 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1203 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1204 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001205 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001206 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001207 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001208 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001209 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001210 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001211 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001212 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001213 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001214 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001215 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001216 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1217 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001218 }
1219 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001220 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001221 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1222 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001223 }
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001225 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1226 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1227 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1228 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1229 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1230 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001231 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001232}
1233
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001234bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001235 bool Cond;
1236 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001237 return false;
1238
Nuno Lopesa25bd552008-11-16 22:06:39 +00001239 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001240}
1241
Chris Lattneraf707ab2009-01-24 21:53:27 +00001242unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001243 // Get information about the alignment.
1244 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001245
Eli Friedman2be58612009-05-30 21:09:44 +00001246 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001247 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001248}
1249
Chris Lattneraf707ab2009-01-24 21:53:27 +00001250unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1251 E = E->IgnoreParens();
1252
1253 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001254 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001255 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001256 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001257
Chris Lattneraf707ab2009-01-24 21:53:27 +00001258 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001259 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001260
Chris Lattnere9feb472009-01-24 21:09:06 +00001261 return GetAlignOfType(E->getType());
1262}
1263
1264
Sebastian Redl05189992008-11-11 17:56:53 +00001265/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1266/// expression's type.
1267bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1268 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001269
Chris Lattnere9feb472009-01-24 21:09:06 +00001270 // Handle alignof separately.
1271 if (!E->isSizeOf()) {
1272 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001273 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001274 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001275 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001276 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001277
Sebastian Redl05189992008-11-11 17:56:53 +00001278 QualType SrcTy = E->getTypeOfArgument();
1279
Daniel Dunbar131eb432009-02-19 09:06:44 +00001280 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1281 // extension.
1282 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1283 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001284
Chris Lattnerfcee0012008-07-11 21:24:13 +00001285 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001286 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001287 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001288
Chris Lattnere9feb472009-01-24 21:09:06 +00001289 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001290 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001291 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001292}
1293
Chris Lattnerb542afe2008-07-11 19:10:17 +00001294bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001295 // Special case unary operators that do not need their subexpression
1296 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001297 if (E->isOffsetOfOp()) {
1298 // The AST for offsetof is defined in such a way that we can just
1299 // directly Evaluate it as an l-value.
1300 APValue LV;
1301 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1302 return false;
1303 if (LV.getLValueBase())
1304 return false;
1305 return Success(LV.getLValueOffset(), E);
1306 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001307
1308 if (E->getOpcode() == UnaryOperator::LNot) {
1309 // LNot's operand isn't necessarily an integer, so we handle it specially.
1310 bool bres;
1311 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1312 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001313 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001314 }
1315
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001316 // Only handle integral operations...
1317 if (!E->getSubExpr()->getType()->isIntegralType())
1318 return false;
1319
Chris Lattner87eae5e2008-07-11 22:52:41 +00001320 // Get the operand value into 'Result'.
1321 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001322 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001323
Chris Lattner75a48812008-07-11 22:15:16 +00001324 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001325 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001326 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1327 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001328 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001329 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001330 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1331 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001332 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001333 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001334 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001335 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001336 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001337 if (!Result.isInt()) return false;
1338 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001339 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001340 if (!Result.isInt()) return false;
1341 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001342 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001343}
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Chris Lattner732b2232008-07-12 01:15:53 +00001345/// HandleCast - This is used to evaluate implicit or explicit casts where the
1346/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001347bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001348 Expr *SubExpr = E->getSubExpr();
1349 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001350 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001351
Eli Friedman4efaa272008-11-12 09:44:48 +00001352 if (DestType->isBooleanType()) {
1353 bool BoolResult;
1354 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1355 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001356 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001357 }
1358
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001359 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001360 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001361 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001362 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001363
Eli Friedmanbe265702009-02-20 01:15:07 +00001364 if (!Result.isInt()) {
1365 // Only allow casts of lvalues if they are lossless.
1366 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1367 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001368
Daniel Dunbardd211642009-02-19 22:24:01 +00001369 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001370 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001371 }
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Chris Lattner732b2232008-07-12 01:15:53 +00001373 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001374 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001375 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001376 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001377 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001378
Daniel Dunbardd211642009-02-19 22:24:01 +00001379 if (LV.getLValueBase()) {
1380 // Only allow based lvalue casts if they are lossless.
1381 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1382 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001383
Daniel Dunbardd211642009-02-19 22:24:01 +00001384 Result = LV;
1385 return true;
1386 }
1387
1388 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1389 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001390 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001391
Eli Friedmanbe265702009-02-20 01:15:07 +00001392 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1393 // This handles double-conversion cases, where there's both
1394 // an l-value promotion and an implicit conversion to int.
1395 APValue LV;
1396 if (!EvaluateLValue(SubExpr, LV, Info))
1397 return false;
1398
1399 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1400 return false;
1401
1402 Result = LV;
1403 return true;
1404 }
1405
Eli Friedman1725f682009-04-22 19:23:09 +00001406 if (SrcType->isAnyComplexType()) {
1407 APValue C;
1408 if (!EvaluateComplex(SubExpr, C, Info))
1409 return false;
1410 if (C.isComplexFloat())
1411 return Success(HandleFloatToIntCast(DestType, SrcType,
1412 C.getComplexFloatReal(), Info.Ctx),
1413 E);
1414 else
1415 return Success(HandleIntToIntCast(DestType, SrcType,
1416 C.getComplexIntReal(), Info.Ctx), E);
1417 }
Eli Friedman2217c872009-02-22 11:46:18 +00001418 // FIXME: Handle vectors
1419
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001420 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001421 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001422
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001423 APFloat F(0.0);
1424 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001425 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001427 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001428}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001429
Eli Friedman722c7172009-02-28 03:59:05 +00001430bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1431 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1432 APValue LV;
1433 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1434 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1435 return Success(LV.getComplexIntReal(), E);
1436 }
1437
1438 return Visit(E->getSubExpr());
1439}
1440
Eli Friedman664a1042009-02-27 04:45:43 +00001441bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001442 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1443 APValue LV;
1444 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1445 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1446 return Success(LV.getComplexIntImag(), E);
1447 }
1448
Eli Friedman664a1042009-02-27 04:45:43 +00001449 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1450 Info.EvalResult.HasSideEffects = true;
1451 return Success(0, E);
1452}
1453
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001454//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001455// Float Evaluation
1456//===----------------------------------------------------------------------===//
1457
1458namespace {
1459class VISIBILITY_HIDDEN FloatExprEvaluator
1460 : public StmtVisitor<FloatExprEvaluator, bool> {
1461 EvalInfo &Info;
1462 APFloat &Result;
1463public:
1464 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1465 : Info(info), Result(result) {}
1466
1467 bool VisitStmt(Stmt *S) {
1468 return false;
1469 }
1470
1471 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001472 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001473
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001474 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001475 bool VisitBinaryOperator(const BinaryOperator *E);
1476 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001477 bool VisitCastExpr(CastExpr *E);
1478 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001479
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001480 bool VisitChooseExpr(const ChooseExpr *E)
1481 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1482 bool VisitUnaryExtension(const UnaryOperator *E)
1483 { return Visit(E->getSubExpr()); }
1484
1485 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1486 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001487 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001488};
1489} // end anonymous namespace
1490
1491static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1492 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1493}
1494
Chris Lattner019f4e82008-10-06 05:28:25 +00001495bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001496 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001497 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001498 case Builtin::BI__builtin_huge_val:
1499 case Builtin::BI__builtin_huge_valf:
1500 case Builtin::BI__builtin_huge_vall:
1501 case Builtin::BI__builtin_inf:
1502 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001503 case Builtin::BI__builtin_infl: {
1504 const llvm::fltSemantics &Sem =
1505 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001506 Result = llvm::APFloat::getInf(Sem);
1507 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Chris Lattner9e621712008-10-06 06:31:58 +00001510 case Builtin::BI__builtin_nan:
1511 case Builtin::BI__builtin_nanf:
1512 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001513 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001514 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001515 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001516 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001517 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001518 const llvm::fltSemantics &Sem =
1519 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001520 llvm::SmallString<16> s;
1521 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1522 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001523 long l;
1524 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001525 l = strtol(&s[0], &endp, 0);
1526 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001527 return false;
1528 unsigned type = (unsigned int)l;;
1529 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001530 return true;
1531 }
1532 }
1533 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001534
1535 case Builtin::BI__builtin_fabs:
1536 case Builtin::BI__builtin_fabsf:
1537 case Builtin::BI__builtin_fabsl:
1538 if (!EvaluateFloat(E->getArg(0), Result, Info))
1539 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001541 if (Result.isNegative())
1542 Result.changeSign();
1543 return true;
1544
Mike Stump1eb44332009-09-09 15:08:12 +00001545 case Builtin::BI__builtin_copysign:
1546 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001547 case Builtin::BI__builtin_copysignl: {
1548 APFloat RHS(0.);
1549 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1550 !EvaluateFloat(E->getArg(1), RHS, Info))
1551 return false;
1552 Result.copySign(RHS);
1553 return true;
1554 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001555 }
1556}
1557
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001558bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001559 if (E->getOpcode() == UnaryOperator::Deref)
1560 return false;
1561
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001562 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1563 return false;
1564
1565 switch (E->getOpcode()) {
1566 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001567 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001568 return true;
1569 case UnaryOperator::Minus:
1570 Result.changeSign();
1571 return true;
1572 }
1573}
Chris Lattner019f4e82008-10-06 05:28:25 +00001574
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001575bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1576 // FIXME: Diagnostics? I really don't understand how the warnings
1577 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001578 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001579 if (!EvaluateFloat(E->getLHS(), Result, Info))
1580 return false;
1581 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1582 return false;
1583
1584 switch (E->getOpcode()) {
1585 default: return false;
1586 case BinaryOperator::Mul:
1587 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1588 return true;
1589 case BinaryOperator::Add:
1590 Result.add(RHS, APFloat::rmNearestTiesToEven);
1591 return true;
1592 case BinaryOperator::Sub:
1593 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1594 return true;
1595 case BinaryOperator::Div:
1596 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1597 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001598 }
1599}
1600
1601bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1602 Result = E->getValue();
1603 return true;
1604}
1605
Eli Friedman4efaa272008-11-12 09:44:48 +00001606bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1607 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Eli Friedman4efaa272008-11-12 09:44:48 +00001609 if (SubExpr->getType()->isIntegralType()) {
1610 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001611 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001612 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001613 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001614 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001615 return true;
1616 }
1617 if (SubExpr->getType()->isRealFloatingType()) {
1618 if (!Visit(SubExpr))
1619 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001620 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1621 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001622 return true;
1623 }
Eli Friedman2217c872009-02-22 11:46:18 +00001624 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001625
1626 return false;
1627}
1628
1629bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1630 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1631 return true;
1632}
1633
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001634//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001635// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001636//===----------------------------------------------------------------------===//
1637
1638namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001639class VISIBILITY_HIDDEN ComplexExprEvaluator
1640 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001641 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001643public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001644 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001646 //===--------------------------------------------------------------------===//
1647 // Visitor Methods
1648 //===--------------------------------------------------------------------===//
1649
1650 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001651 return APValue();
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001654 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1655
1656 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001657 Expr* SubExpr = E->getSubExpr();
1658
1659 if (SubExpr->getType()->isRealFloatingType()) {
1660 APFloat Result(0.0);
1661
1662 if (!EvaluateFloat(SubExpr, Result, Info))
1663 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001664
1665 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001666 Result);
1667 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001668 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001669 "Unexpected imaginary literal.");
1670
1671 llvm::APSInt Result;
1672 if (!EvaluateInteger(SubExpr, Result, Info))
1673 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001675 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1676 Zero = 0;
1677 return APValue(Zero, Result);
1678 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001679 }
1680
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001681 APValue VisitCastExpr(CastExpr *E) {
1682 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001683 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001684 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001685
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001686 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001687 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001688
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001689 if (!EvaluateFloat(SubExpr, Result, Info))
1690 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001691
1692 if (EltType->isRealFloatingType()) {
1693 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001694 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001695 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1696 } else {
1697 llvm::APSInt IResult;
1698 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1699 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1700 Zero = 0;
1701 return APValue(IResult, Zero);
1702 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001703 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001704 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001705
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001706 if (!EvaluateInteger(SubExpr, Result, Info))
1707 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001708
Eli Friedman1725f682009-04-22 19:23:09 +00001709 if (EltType->isRealFloatingType()) {
1710 APFloat FResult =
1711 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001712 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001713 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1714 } else {
1715 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1716 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1717 Zero = 0;
1718 return APValue(Result, Zero);
1719 }
John McCall183700f2009-09-21 23:43:11 +00001720 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001721 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001722
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001723 if (!EvaluateComplex(SubExpr, Src, Info))
1724 return APValue();
1725
1726 QualType SrcType = CT->getElementType();
1727
1728 if (Src.isComplexFloat()) {
1729 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001730 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001731 Src.getComplexFloatReal(),
1732 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001733 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001734 Src.getComplexFloatImag(),
1735 Info.Ctx));
1736 } else {
1737 return APValue(HandleFloatToIntCast(EltType, SrcType,
1738 Src.getComplexFloatReal(),
1739 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001740 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001741 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001742 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001743 }
1744 } else {
1745 assert(Src.isComplexInt() && "Invalid evaluate result.");
1746 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001747 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001748 Src.getComplexIntReal(),
1749 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001750 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001751 Src.getComplexIntImag(),
1752 Info.Ctx));
1753 } else {
1754 return APValue(HandleIntToIntCast(EltType, SrcType,
1755 Src.getComplexIntReal(),
1756 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001757 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001758 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001759 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001760 }
1761 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001762 }
1763
1764 // FIXME: Handle more casts.
1765 return APValue();
1766 }
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001768 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001769 APValue VisitChooseExpr(const ChooseExpr *E)
1770 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1771 APValue VisitUnaryExtension(const UnaryOperator *E)
1772 { return Visit(E->getSubExpr()); }
1773 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001774 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001775};
1776} // end anonymous namespace
1777
Mike Stump1eb44332009-09-09 15:08:12 +00001778static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001779 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1780 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001781 (&Result.getComplexFloatReal().getSemantics() ==
1782 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001783 "Invalid complex evaluation.");
1784 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001785}
1786
Mike Stump1eb44332009-09-09 15:08:12 +00001787APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001788 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001790 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001791 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001793 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001794 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001795
Daniel Dunbar3f279872009-01-29 01:32:56 +00001796 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1797 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001798 switch (E->getOpcode()) {
1799 default: return APValue();
1800 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001801 if (Result.isComplexFloat()) {
1802 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1803 APFloat::rmNearestTiesToEven);
1804 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1805 APFloat::rmNearestTiesToEven);
1806 } else {
1807 Result.getComplexIntReal() += RHS.getComplexIntReal();
1808 Result.getComplexIntImag() += RHS.getComplexIntImag();
1809 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001810 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001811 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001812 if (Result.isComplexFloat()) {
1813 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1814 APFloat::rmNearestTiesToEven);
1815 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1816 APFloat::rmNearestTiesToEven);
1817 } else {
1818 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1819 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1820 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001821 break;
1822 case BinaryOperator::Mul:
1823 if (Result.isComplexFloat()) {
1824 APValue LHS = Result;
1825 APFloat &LHS_r = LHS.getComplexFloatReal();
1826 APFloat &LHS_i = LHS.getComplexFloatImag();
1827 APFloat &RHS_r = RHS.getComplexFloatReal();
1828 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Daniel Dunbar3f279872009-01-29 01:32:56 +00001830 APFloat Tmp = LHS_r;
1831 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1832 Result.getComplexFloatReal() = Tmp;
1833 Tmp = LHS_i;
1834 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1835 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1836
1837 Tmp = LHS_r;
1838 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1839 Result.getComplexFloatImag() = Tmp;
1840 Tmp = LHS_i;
1841 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1842 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1843 } else {
1844 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001845 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001846 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1847 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001848 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001849 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1850 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1851 }
1852 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001853 }
1854
1855 return Result;
1856}
1857
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001858//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001859// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001860//===----------------------------------------------------------------------===//
1861
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001862/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001863/// any crazy technique (that has nothing to do with language standards) that
1864/// we want to. If this function returns true, it returns the folded constant
1865/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001866bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1867 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001868
Nate Begeman59b5da62009-01-18 03:20:47 +00001869 if (getType()->isVectorType()) {
1870 if (!EvaluateVector(this, Result.Val, Info))
1871 return false;
1872 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001873 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001874 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001875 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001876 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001877 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001878 } else if (getType()->isRealFloatingType()) {
1879 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001880 if (!EvaluateFloat(this, f, Info))
1881 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001883 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001884 } else if (getType()->isAnyComplexType()) {
1885 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001886 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001887 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001888 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001889
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001890 return true;
1891}
1892
Mike Stump660e6f72009-10-26 23:05:19 +00001893bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1894 EvalInfo Info(Ctx, Result, true);
1895
1896 if (getType()->isVectorType()) {
1897 if (!EvaluateVector(this, Result.Val, Info))
1898 return false;
1899 } else if (getType()->isIntegerType()) {
1900 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1901 return false;
1902 } else if (getType()->hasPointerRepresentation()) {
1903 if (!EvaluatePointer(this, Result.Val, Info))
1904 return false;
1905 } else if (getType()->isRealFloatingType()) {
1906 llvm::APFloat f(0.0);
1907 if (!EvaluateFloat(this, f, Info))
1908 return false;
1909
1910 Result.Val = APValue(f);
1911 } else if (getType()->isAnyComplexType()) {
1912 if (!EvaluateComplex(this, Result.Val, Info))
1913 return false;
1914 } else
1915 return false;
1916
1917 return true;
1918}
1919
Anders Carlsson1b782762009-04-10 04:54:13 +00001920bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1921 EvalInfo Info(Ctx, Result);
1922
1923 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1924}
1925
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001926bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1927 EvalInfo Info(Ctx, Result, true);
1928
1929 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1930}
1931
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001932/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001933/// folded, but discard the result.
1934bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001935 EvalResult Result;
1936 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001937}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001938
1939APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001940 EvalResult EvalResult;
1941 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001942 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001943 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001944 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001945
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001946 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001947}