blob: 8435e03417308dde245c1874a6b3ac2b88b2f5c0 [file] [log] [blame]
Chris Lattnere13042c2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlsson7a241ba2008-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 Carlsson15b73de2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon1904f442008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson374b93d2008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stumpb807c9c2009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Anders Carlsson0a1707c2008-07-08 05:13:58 +000022#include "llvm/Support/Compiler.h"
Mike Stump2346cd22009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlsson7a241ba2008-07-03 04:20:39 +000025using namespace clang;
Chris Lattner05706e882008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedman24c01542008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlsson7a241ba2008-07-03 04:20:39 +000028
Chris Lattnercdf34e72008-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 Stump11289f42009-09-09 15:08:12 +000045
Anders Carlssonbd1df8e2008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlsson58620012008-11-30 18:26:25 +000048
Eli Friedman7d45c482009-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 Lattnercdf34e72008-07-11 22:52:41 +000055};
56
57
Eli Friedman9a156e52008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnercdf34e72008-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 Dunbarce399542009-02-20 18:22:23 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedman24c01542008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner05706e882008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman9a156e52008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman334046a2009-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 Friedman9a156e52008-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 Friedman64004332009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman9a156e52008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman334046a2009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedman64004332009-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 Friedman9a156e52008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-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 Stump11289f42009-09-09 15:08:12 +0000116
Daniel Dunbarb6f953e2009-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 Stump11289f42009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump11289f42009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump11289f42009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-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 Stump11289f42009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Eli Friedman9a156e52008-11-12 09:44:48 +0000154//===----------------------------------------------------------------------===//
155// LValue Evaluation
156//===----------------------------------------------------------------------===//
157namespace {
158class VISIBILITY_HIDDEN LValueExprEvaluator
159 : public StmtVisitor<LValueExprEvaluator, APValue> {
160 EvalInfo &Info;
161public:
Mike Stump11289f42009-09-09 15:08:12 +0000162
Eli Friedman9a156e52008-11-12 09:44:48 +0000163 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
164
165 APValue VisitStmt(Stmt *S) {
Eli Friedman9a156e52008-11-12 09:44:48 +0000166 return APValue();
167 }
168
169 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssona42ee442008-11-24 04:41:22 +0000170 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroffa0c32702009-04-16 19:02:57 +0000171 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000172 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
173 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
174 APValue VisitMemberExpr(MemberExpr *E);
175 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000176 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000177 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman0b8337c2009-02-20 01:57:15 +0000178 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000179 APValue VisitUnaryExtension(const UnaryOperator *E)
180 { return Visit(E->getSubExpr()); }
181 APValue VisitChooseExpr(const ChooseExpr *E)
182 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlssonde55f642009-10-03 16:30:22 +0000183
184 APValue VisitCastExpr(CastExpr *E) {
185 switch (E->getCastKind()) {
186 default:
187 return APValue();
188
189 case CastExpr::CK_NoOp:
190 return Visit(E->getSubExpr());
191 }
192 }
Eli Friedman449fe542009-03-23 04:56:01 +0000193 // FIXME: Missing: __real__, __imag__
Eli Friedman9a156e52008-11-12 09:44:48 +0000194};
195} // end anonymous namespace
196
197static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
198 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
199 return Result.isLValue();
200}
201
Mike Stump11289f42009-09-09 15:08:12 +0000202APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman751aa72b72009-05-27 06:04:58 +0000203 if (isa<FunctionDecl>(E->getDecl())) {
204 return APValue(E, 0);
205 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000206 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedman9ab03192009-08-29 19:09:59 +0000207 return APValue();
Eli Friedman751aa72b72009-05-27 06:04:58 +0000208 if (!VD->getType()->isReferenceType())
209 return APValue(E, 0);
Eli Friedman9ab03192009-08-29 19:09:59 +0000210 // FIXME: Check whether VD might be overridden!
Eli Friedman751aa72b72009-05-27 06:04:58 +0000211 if (VD->getInit())
212 return Visit(VD->getInit());
213 }
214
215 return APValue();
Anders Carlssona42ee442008-11-24 04:41:22 +0000216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroffa0c32702009-04-16 19:02:57 +0000219 if (E->hasBlockDeclRefExprs())
220 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000221
Steve Naroffa0c32702009-04-16 19:02:57 +0000222 return APValue(E, 0);
223}
224
Eli Friedman9a156e52008-11-12 09:44:48 +0000225APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedman7d45c482009-09-13 10:17:44 +0000226 if (!Info.AnyLValue && !E->isFileScope())
227 return APValue();
228 return APValue(E, 0);
Eli Friedman9a156e52008-11-12 09:44:48 +0000229}
230
231APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
232 APValue result;
233 QualType Ty;
234 if (E->isArrow()) {
235 if (!EvaluatePointer(E->getBase(), result, Info))
236 return APValue();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000237 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman9a156e52008-11-12 09:44:48 +0000238 } else {
239 result = Visit(E->getBase());
240 if (result.isUninit())
241 return APValue();
242 Ty = E->getBase()->getType();
243 }
244
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000245 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman9a156e52008-11-12 09:44:48 +0000246 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000247
248 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
249 if (!FD) // FIXME: deal with other kinds of member expressions
250 return APValue();
Eli Friedmanf7f9f682009-05-30 21:09:44 +0000251
252 if (FD->getType()->isReferenceType())
253 return APValue();
254
Eli Friedman9a156e52008-11-12 09:44:48 +0000255 // FIXME: This is linear time.
Douglas Gregor91f84212008-12-11 16:49:14 +0000256 unsigned i = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000257 for (RecordDecl::field_iterator Field = RD->field_begin(),
258 FieldEnd = RD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000259 Field != FieldEnd; (void)++Field, ++i) {
260 if (*Field == FD)
Eli Friedman9a156e52008-11-12 09:44:48 +0000261 break;
262 }
263
264 result.setLValue(result.getLValueBase(),
265 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
266
267 return result;
268}
269
Mike Stump11289f42009-09-09 15:08:12 +0000270APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000271 APValue Result;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000273 if (!EvaluatePointer(E->getBase(), Result, Info))
274 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000275
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000276 APSInt Index;
277 if (!EvaluateInteger(E->getIdx(), Index, Info))
278 return APValue();
279
280 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
281
282 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump11289f42009-09-09 15:08:12 +0000283 Result.setLValue(Result.getLValueBase(),
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000284 Result.getLValueOffset() + Offset);
285 return Result;
286}
Eli Friedman9a156e52008-11-12 09:44:48 +0000287
Mike Stump11289f42009-09-09 15:08:12 +0000288APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedman0b8337c2009-02-20 01:57:15 +0000289 APValue Result;
290 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
291 return APValue();
292 return Result;
293}
294
Eli Friedman9a156e52008-11-12 09:44:48 +0000295//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000296// Pointer Evaluation
297//===----------------------------------------------------------------------===//
298
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000299namespace {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000300class VISIBILITY_HIDDEN PointerExprEvaluator
301 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000302 EvalInfo &Info;
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000303public:
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattnercdf34e72008-07-11 22:52:41 +0000305 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000306
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000307 APValue VisitStmt(Stmt *S) {
Anders Carlssonb5ad0212008-07-08 14:30:00 +0000308 return APValue();
309 }
310
311 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
312
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000313 APValue VisitBinaryOperator(const BinaryOperator *E);
314 APValue VisitCastExpr(const CastExpr* E);
Eli Friedmanc2b50172009-02-22 11:46:18 +0000315 APValue VisitUnaryExtension(const UnaryOperator *E)
316 { return Visit(E->getSubExpr()); }
317 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman9a156e52008-11-12 09:44:48 +0000318 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
319 { return APValue(E, 0); }
Eli Friedman529a99b2009-01-25 01:21:06 +0000320 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
321 { return APValue(E, 0); }
Eli Friedmanc69d4542009-01-25 01:54:01 +0000322 APValue VisitCallExpr(CallExpr *E);
Mike Stumpa6703322009-02-19 22:01:56 +0000323 APValue VisitBlockExpr(BlockExpr *E) {
324 if (!E->hasBlockDeclRefExprs())
325 return APValue(E, 0);
326 return APValue();
327 }
Eli Friedman3ae59112009-02-23 04:23:56 +0000328 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
329 { return APValue((Expr*)0, 0); }
Eli Friedman9a156e52008-11-12 09:44:48 +0000330 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000331 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl576fd422009-05-10 18:38:11 +0000332 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
333 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
334 { return APValue((Expr*)0, 0); }
Eli Friedman449fe542009-03-23 04:56:01 +0000335 // FIXME: Missing: @protocol, @selector
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000336};
Chris Lattner05706e882008-07-11 18:11:29 +0000337} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000338
Chris Lattnercdf34e72008-07-11 22:52:41 +0000339static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar76ba41c2009-02-26 20:52:22 +0000340 if (!E->getType()->hasPointerRepresentation())
Chris Lattner05706e882008-07-11 18:11:29 +0000341 return false;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000342 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattner05706e882008-07-11 18:11:29 +0000343 return Result.isLValue();
344}
345
346APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
347 if (E->getOpcode() != BinaryOperator::Add &&
348 E->getOpcode() != BinaryOperator::Sub)
349 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner05706e882008-07-11 18:11:29 +0000351 const Expr *PExp = E->getLHS();
352 const Expr *IExp = E->getRHS();
353 if (IExp->getType()->isPointerType())
354 std::swap(PExp, IExp);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattner05706e882008-07-11 18:11:29 +0000356 APValue ResultLValue;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000357 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000358 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattner05706e882008-07-11 18:11:29 +0000360 llvm::APSInt AdditionalOffset(32);
Chris Lattnercdf34e72008-07-11 22:52:41 +0000361 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000362 return APValue();
363
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000364 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlssonef56fba2009-02-19 04:55:58 +0000365 uint64_t SizeOfPointee;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Anders Carlssonef56fba2009-02-19 04:55:58 +0000367 // Explicitly handle GNU void* and function pointer arithmetic extensions.
368 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
369 SizeOfPointee = 1;
370 else
371 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman9a156e52008-11-12 09:44:48 +0000372
Chris Lattner05706e882008-07-11 18:11:29 +0000373 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman9a156e52008-11-12 09:44:48 +0000374
Chris Lattner05706e882008-07-11 18:11:29 +0000375 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman9a156e52008-11-12 09:44:48 +0000376 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattner05706e882008-07-11 18:11:29 +0000377 else
Eli Friedman9a156e52008-11-12 09:44:48 +0000378 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
379
Chris Lattner05706e882008-07-11 18:11:29 +0000380 return APValue(ResultLValue.getLValueBase(), Offset);
381}
Eli Friedman9a156e52008-11-12 09:44:48 +0000382
Eli Friedmanc2b50172009-02-22 11:46:18 +0000383APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
384 APValue result;
385 if (EvaluateLValue(E->getSubExpr(), result, Info))
386 return result;
Eli Friedman9a156e52008-11-12 09:44:48 +0000387 return APValue();
388}
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattner05706e882008-07-11 18:11:29 +0000390
Chris Lattnere13042c2008-07-11 19:10:17 +0000391APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattner05706e882008-07-11 18:11:29 +0000392 const Expr* SubExpr = E->getSubExpr();
393
394 // Check for pointer->pointer cast
Steve Naroff7cae42b2009-07-10 23:34:53 +0000395 if (SubExpr->getType()->isPointerType() ||
Anders Carlsson04c3bf42009-09-15 04:39:46 +0000396 SubExpr->getType()->isObjCObjectPointerType() ||
397 SubExpr->getType()->isNullPtrType()) {
Chris Lattner05706e882008-07-11 18:11:29 +0000398 APValue Result;
Chris Lattnercdf34e72008-07-11 22:52:41 +0000399 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattner05706e882008-07-11 18:11:29 +0000400 return Result;
401 return APValue();
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Eli Friedmanbd840592008-07-27 05:46:18 +0000404 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbarce399542009-02-20 18:22:23 +0000405 APValue Result;
406 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
407 return APValue();
408
409 if (Result.isInt()) {
410 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
411 return APValue(0, Result.getInt().getZExtValue());
Chris Lattner05706e882008-07-11 18:11:29 +0000412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Daniel Dunbarce399542009-02-20 18:22:23 +0000414 // Cast is of an lvalue, no need to change value.
415 return Result;
Chris Lattner05706e882008-07-11 18:11:29 +0000416 }
Eli Friedman9a156e52008-11-12 09:44:48 +0000417
418 if (SubExpr->getType()->isFunctionType() ||
Steve Naroffa0c32702009-04-16 19:02:57 +0000419 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman9a156e52008-11-12 09:44:48 +0000420 SubExpr->getType()->isArrayType()) {
421 APValue Result;
422 if (EvaluateLValue(SubExpr, Result, Info))
423 return Result;
424 return APValue();
425 }
426
Chris Lattner05706e882008-07-11 18:11:29 +0000427 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +0000428}
Chris Lattner05706e882008-07-11 18:11:29 +0000429
Eli Friedmanc69d4542009-01-25 01:54:01 +0000430APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +0000431 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregore711f702009-02-14 18:57:46 +0000432 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedmanc69d4542009-01-25 01:54:01 +0000433 return APValue(E, 0);
434 return APValue();
435}
436
Eli Friedman9a156e52008-11-12 09:44:48 +0000437APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
438 bool BoolResult;
439 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
440 return APValue();
441
442 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
443
444 APValue Result;
445 if (EvaluatePointer(EvalExpr, Result, Info))
446 return Result;
447 return APValue();
448}
Chris Lattner05706e882008-07-11 18:11:29 +0000449
450//===----------------------------------------------------------------------===//
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000451// Vector Evaluation
452//===----------------------------------------------------------------------===//
453
454namespace {
455 class VISIBILITY_HIDDEN VectorExprEvaluator
456 : public StmtVisitor<VectorExprEvaluator, APValue> {
457 EvalInfo &Info;
Eli Friedman3ae59112009-02-23 04:23:56 +0000458 APValue GetZeroVector(QualType VecType);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000459 public:
Mike Stump11289f42009-09-09 15:08:12 +0000460
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000461 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +0000462
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000463 APValue VisitStmt(Stmt *S) {
464 return APValue();
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Eli Friedman3ae59112009-02-23 04:23:56 +0000467 APValue VisitParenExpr(ParenExpr *E)
468 { return Visit(E->getSubExpr()); }
469 APValue VisitUnaryExtension(const UnaryOperator *E)
470 { return Visit(E->getSubExpr()); }
471 APValue VisitUnaryPlus(const UnaryOperator *E)
472 { return Visit(E->getSubExpr()); }
473 APValue VisitUnaryReal(const UnaryOperator *E)
474 { return Visit(E->getSubExpr()); }
475 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
476 { return GetZeroVector(E->getType()); }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000477 APValue VisitCastExpr(const CastExpr* E);
478 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
479 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman3ae59112009-02-23 04:23:56 +0000480 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +0000481 APValue VisitChooseExpr(const ChooseExpr *E)
482 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman3ae59112009-02-23 04:23:56 +0000483 APValue VisitUnaryImag(const UnaryOperator *E);
484 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedmanc2b50172009-02-22 11:46:18 +0000485 // binary comparisons, binary and/or/xor,
Eli Friedman3ae59112009-02-23 04:23:56 +0000486 // shufflevector, ExtVectorElementExpr
487 // (Note that these require implementing conversions
488 // between vector types.)
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000489 };
490} // end anonymous namespace
491
492static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
493 if (!E->getType()->isVectorType())
494 return false;
495 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
496 return !Result.isUninit();
497}
498
499APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall9dd450b2009-09-21 23:43:11 +0000500 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000501 QualType EltTy = VTy->getElementType();
502 unsigned NElts = VTy->getNumElements();
503 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump11289f42009-09-09 15:08:12 +0000504
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000505 const Expr* SE = E->getSubExpr();
Nate Begeman2ffd3842009-06-26 18:22:18 +0000506 QualType SETy = SE->getType();
507 APValue Result = APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000508
Nate Begeman2ffd3842009-06-26 18:22:18 +0000509 // Check for vector->vector bitcast and scalar->vector splat.
510 if (SETy->isVectorType()) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000511 return this->Visit(const_cast<Expr*>(SE));
Nate Begeman2ffd3842009-06-26 18:22:18 +0000512 } else if (SETy->isIntegerType()) {
513 APSInt IntResult;
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000514 if (!EvaluateInteger(SE, IntResult, Info))
515 return APValue();
516 Result = APValue(IntResult);
Nate Begeman2ffd3842009-06-26 18:22:18 +0000517 } else if (SETy->isRealFloatingType()) {
518 APFloat F(0.0);
Daniel Dunbardf4a58e2009-07-01 20:37:45 +0000519 if (!EvaluateFloat(SE, F, Info))
520 return APValue();
521 Result = APValue(F);
522 } else
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000523 return APValue();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000524
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000525 // For casts of a scalar to ExtVector, convert the scalar to the element type
526 // and splat it to all elements.
527 if (E->getType()->isExtVectorType()) {
528 if (EltTy->isIntegerType() && Result.isInt())
529 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
530 Info.Ctx));
531 else if (EltTy->isIntegerType())
532 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
533 Info.Ctx));
534 else if (EltTy->isRealFloatingType() && Result.isInt())
535 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
536 Info.Ctx));
537 else if (EltTy->isRealFloatingType())
538 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
539 Info.Ctx));
540 else
541 return APValue();
542
543 // Splat and create vector APValue.
544 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
545 return APValue(&Elts[0], Elts.size());
Nate Begeman2ffd3842009-06-26 18:22:18 +0000546 }
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000547
548 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
549 // to the vector. To construct the APValue vector initializer, bitcast the
550 // initializing value to an APInt, and shift out the bits pertaining to each
551 // element.
552 APSInt Init;
553 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump11289f42009-09-09 15:08:12 +0000554
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000555 llvm::SmallVector<APValue, 4> Elts;
556 for (unsigned i = 0; i != NElts; ++i) {
557 APSInt Tmp = Init;
558 Tmp.extOrTrunc(EltWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Nate Begemanef1a7fa2009-07-01 07:50:47 +0000560 if (EltTy->isIntegerType())
561 Elts.push_back(APValue(Tmp));
562 else if (EltTy->isRealFloatingType())
563 Elts.push_back(APValue(APFloat(Tmp)));
564 else
565 return APValue();
566
567 Init >>= EltWidth;
568 }
569 return APValue(&Elts[0], Elts.size());
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000570}
571
Mike Stump11289f42009-09-09 15:08:12 +0000572APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000573VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
574 return this->Visit(const_cast<Expr*>(E->getInitializer()));
575}
576
Mike Stump11289f42009-09-09 15:08:12 +0000577APValue
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000578VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall9dd450b2009-09-21 23:43:11 +0000579 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000580 unsigned NumInits = E->getNumInits();
Eli Friedman3ae59112009-02-23 04:23:56 +0000581 unsigned NumElements = VT->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000582
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000583 QualType EltTy = VT->getElementType();
584 llvm::SmallVector<APValue, 4> Elements;
585
Eli Friedman3ae59112009-02-23 04:23:56 +0000586 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000587 if (EltTy->isIntegerType()) {
588 llvm::APSInt sInt(32);
Eli Friedman3ae59112009-02-23 04:23:56 +0000589 if (i < NumInits) {
590 if (!EvaluateInteger(E->getInit(i), sInt, Info))
591 return APValue();
592 } else {
593 sInt = Info.Ctx.MakeIntValue(0, EltTy);
594 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000595 Elements.push_back(APValue(sInt));
596 } else {
597 llvm::APFloat f(0.0);
Eli Friedman3ae59112009-02-23 04:23:56 +0000598 if (i < NumInits) {
599 if (!EvaluateFloat(E->getInit(i), f, Info))
600 return APValue();
601 } else {
602 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
603 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000604 Elements.push_back(APValue(f));
605 }
606 }
607 return APValue(&Elements[0], Elements.size());
608}
609
Mike Stump11289f42009-09-09 15:08:12 +0000610APValue
Eli Friedman3ae59112009-02-23 04:23:56 +0000611VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +0000612 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman3ae59112009-02-23 04:23:56 +0000613 QualType EltTy = VT->getElementType();
614 APValue ZeroElement;
615 if (EltTy->isIntegerType())
616 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
617 else
618 ZeroElement =
619 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
620
621 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
622 return APValue(&Elements[0], Elements.size());
623}
624
625APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
626 bool BoolResult;
627 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
628 return APValue();
629
630 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
631
632 APValue Result;
633 if (EvaluateVector(EvalExpr, Result, Info))
634 return Result;
635 return APValue();
636}
637
Eli Friedman3ae59112009-02-23 04:23:56 +0000638APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
639 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
640 Info.EvalResult.HasSideEffects = true;
641 return GetZeroVector(E->getType());
642}
643
Nate Begeman2f2bdeb2009-01-18 03:20:47 +0000644//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000645// Integer Evaluation
646//===----------------------------------------------------------------------===//
Chris Lattner05706e882008-07-11 18:11:29 +0000647
648namespace {
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000649class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnere13042c2008-07-11 19:10:17 +0000650 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattnercdf34e72008-07-11 22:52:41 +0000651 EvalInfo &Info;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000652 APValue &Result;
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000653public:
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000654 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattnercdf34e72008-07-11 22:52:41 +0000655 : Info(info), Result(result) {}
Chris Lattner05706e882008-07-11 18:11:29 +0000656
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000657 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000658 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000659 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000660 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000661 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000662 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000663 Result = APValue(SI);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000664 return true;
665 }
666
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000667 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000668 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000669 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000670 "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000671 Result = APValue(APSInt(I));
672 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000673 return true;
674 }
675
676 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar79e042a2009-02-21 18:14:20 +0000677 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000678 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000679 return true;
680 }
681
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000682 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000683 // Take the first error.
Anders Carlssonbd1df8e2008-11-30 16:38:33 +0000684 if (Info.EvalResult.Diag == 0) {
685 Info.EvalResult.DiagLoc = L;
686 Info.EvalResult.Diag = D;
Anders Carlsson27b8c5c2008-11-30 18:14:57 +0000687 Info.EvalResult.DiagExpr = E;
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000688 }
Chris Lattner99415702008-07-12 00:14:42 +0000689 return false;
Chris Lattnerae8cc152008-07-11 19:24:49 +0000690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000692 //===--------------------------------------------------------------------===//
693 // Visitor Methods
694 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000696 bool VisitStmt(Stmt *) {
697 assert(0 && "This should be called on integers, stmts are not integers");
698 return false;
699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattnerfac05ae2008-11-12 07:43:42 +0000701 bool VisitExpr(Expr *E) {
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000702 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattnere13042c2008-07-11 19:10:17 +0000705 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson0a1707c2008-07-08 05:13:58 +0000706
Chris Lattner7174bf32008-07-12 00:38:25 +0000707 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000708 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000709 }
710 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000711 return Success(E->getValue(), E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000712 }
713 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000714 // Per gcc docs "this built-in function ignores top level
715 // qualifiers". We need to use the canonical version to properly
716 // be able to strip CRV qualifiers from the type.
717 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
718 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump11289f42009-09-09 15:08:12 +0000719 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000720 T1.getUnqualifiedType()),
721 E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000722 }
723 bool VisitDeclRefExpr(const DeclRefExpr *E);
724 bool VisitCallExpr(const CallExpr *E);
Chris Lattnere13042c2008-07-11 19:10:17 +0000725 bool VisitBinaryOperator(const BinaryOperator *E);
726 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopes42042612008-11-16 19:28:31 +0000727 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson374b93d2008-07-08 05:49:43 +0000728
Daniel Dunbarb6f953e2009-01-29 06:16:07 +0000729 bool VisitCastExpr(CastExpr* E);
Sebastian Redl6f282892008-11-11 17:56:53 +0000730 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
731
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000732 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000733 return Success(E->getValue(), E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000734 }
Mike Stump11289f42009-09-09 15:08:12 +0000735
Anders Carlsson39def3a2008-12-21 22:39:40 +0000736 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000737 return Success(0, E);
Anders Carlsson39def3a2008-12-21 22:39:40 +0000738 }
Mike Stump11289f42009-09-09 15:08:12 +0000739
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000740 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000741 return Success(0, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +0000742 }
743
Eli Friedman4e7a2412009-02-27 04:45:43 +0000744 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
745 return Success(0, E);
746 }
747
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000748 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000749 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000750 }
751
Eli Friedman449fe542009-03-23 04:56:01 +0000752 bool VisitChooseExpr(const ChooseExpr *E) {
753 return Visit(E->getChosenSubExpr(Info.Ctx));
754 }
755
Eli Friedmana1c7b6c2009-02-28 03:59:05 +0000756 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000757 bool VisitUnaryImag(const UnaryOperator *E);
758
Chris Lattnerf8d7f722008-07-11 21:24:13 +0000759private:
Chris Lattner68061312009-01-24 21:53:27 +0000760 unsigned GetAlignOfExpr(const Expr *E);
761 unsigned GetAlignOfType(QualType T);
Eli Friedman4e7a2412009-02-27 04:45:43 +0000762 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlsson9c181652008-07-08 14:35:21 +0000763};
Chris Lattner05706e882008-07-11 18:11:29 +0000764} // end anonymous namespace
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000765
Daniel Dunbarce399542009-02-20 18:22:23 +0000766static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000767 if (!E->getType()->isIntegralType())
768 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000769
Daniel Dunbarce399542009-02-20 18:22:23 +0000770 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
771}
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000772
Daniel Dunbarce399542009-02-20 18:22:23 +0000773static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
774 APValue Val;
775 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
776 return false;
Daniel Dunbarca097ad2009-02-19 20:17:33 +0000777 Result = Val.getInt();
778 return true;
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000779}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000780
Chris Lattner7174bf32008-07-12 00:38:25 +0000781bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
782 // Enums are integer constant exprs.
783 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedman14fb8582008-12-08 02:21:03 +0000784 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000785 // signedness consistently; see PR3173.
786 APSInt SI = D->getInitVal();
787 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
788 // FIXME: This is an ugly hack around the fact that enums don't
789 // set their width (!?!) consistently; see PR3173.
790 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
791 return Success(SI, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000792 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000793
794 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedman29f80c32009-03-30 23:39:01 +0000795 // In C, they can also be folded, although they are not ICEs.
John McCall8ccfcb52009-09-24 19:53:00 +0000796 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000797 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000798 if (APValue *V = D->getEvaluatedValue())
799 return Success(V->getInt(), E);
800 if (const Expr *Init = D->getInit()) {
801 if (Visit(const_cast<Expr*>(Init))) {
802 // Cache the evaluated value in the variable declaration.
803 D->setEvaluatedValue(Info.Ctx, Result);
804 return true;
805 }
806
807 return false;
808 }
Sebastian Redlc9ab3d42009-02-08 15:51:17 +0000809 }
810 }
811
Chris Lattner7174bf32008-07-12 00:38:25 +0000812 // Otherwise, random variable references are not constants.
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000813 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner7174bf32008-07-12 00:38:25 +0000814}
815
Chris Lattner86ee2862008-10-06 06:40:35 +0000816/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
817/// as GCC.
818static int EvaluateBuiltinClassifyType(const CallExpr *E) {
819 // The following enum mimics the values returned by GCC.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000820 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattner86ee2862008-10-06 06:40:35 +0000821 enum gcc_type_class {
822 no_type_class = -1,
823 void_type_class, integer_type_class, char_type_class,
824 enumeral_type_class, boolean_type_class,
825 pointer_type_class, reference_type_class, offset_type_class,
826 real_type_class, complex_type_class,
827 function_type_class, method_type_class,
828 record_type_class, union_type_class,
829 array_type_class, string_type_class,
830 lang_type_class
831 };
Mike Stump11289f42009-09-09 15:08:12 +0000832
833 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattner86ee2862008-10-06 06:40:35 +0000834 // ideal, however it is what gcc does.
835 if (E->getNumArgs() == 0)
836 return no_type_class;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner86ee2862008-10-06 06:40:35 +0000838 QualType ArgTy = E->getArg(0)->getType();
839 if (ArgTy->isVoidType())
840 return void_type_class;
841 else if (ArgTy->isEnumeralType())
842 return enumeral_type_class;
843 else if (ArgTy->isBooleanType())
844 return boolean_type_class;
845 else if (ArgTy->isCharType())
846 return string_type_class; // gcc doesn't appear to use char_type_class
847 else if (ArgTy->isIntegerType())
848 return integer_type_class;
849 else if (ArgTy->isPointerType())
850 return pointer_type_class;
851 else if (ArgTy->isReferenceType())
852 return reference_type_class;
853 else if (ArgTy->isRealType())
854 return real_type_class;
855 else if (ArgTy->isComplexType())
856 return complex_type_class;
857 else if (ArgTy->isFunctionType())
858 return function_type_class;
859 else if (ArgTy->isStructureType())
860 return record_type_class;
861 else if (ArgTy->isUnionType())
862 return union_type_class;
863 else if (ArgTy->isArrayType())
864 return array_type_class;
865 else if (ArgTy->isUnionType())
866 return union_type_class;
867 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
868 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
869 return -1;
870}
871
Chris Lattner7174bf32008-07-12 00:38:25 +0000872bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +0000873 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000874 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +0000875 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump722cedf2009-10-26 18:35:08 +0000876
877 case Builtin::BI__builtin_object_size: {
Mike Stump722cedf2009-10-26 18:35:08 +0000878 const Expr *Arg = E->getArg(0)->IgnoreParens();
879 Expr::EvalResult Base;
Mike Stump5183a142009-10-26 23:05:19 +0000880 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump722cedf2009-10-26 18:35:08 +0000881 && Base.Val.getKind() == APValue::LValue
882 && !Base.HasSideEffects)
883 if (const Expr *LVBase = Base.Val.getLValueBase())
884 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
885 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
886 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
Mike Stump10bd7e12009-10-26 21:38:39 +0000887 uint64_t Offset = Base.Val.getLValueOffset();
888 if (Offset <= Size)
889 Size -= Base.Val.getLValueOffset();
890 else
891 Size = 0;
Mike Stump722cedf2009-10-26 18:35:08 +0000892 return Success(Size, E);
893 }
894 }
895
896 if (Base.HasSideEffects) {
Mike Stump99f11f72009-10-26 18:57:47 +0000897 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump722cedf2009-10-26 18:35:08 +0000898 return Success(-1, E);
899 return Success(0, E);
900 }
901
902 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
903 }
904
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000905 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000906 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000907
Anders Carlsson4c76e932008-11-24 04:21:33 +0000908 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000909 // __builtin_constant_p always has one operand: it returns true if that
910 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000911 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000912
913 case Builtin::BI__builtin_eh_return_data_regno: {
914 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
915 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
916 return Success(Operand, E);
917 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000918 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000919}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000920
Chris Lattnere13042c2008-07-11 19:10:17 +0000921bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000922 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +0000923 if (!Visit(E->getRHS()))
924 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +0000925
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000926 // If we can't evaluate the LHS, it might have side effects;
927 // conservatively mark it.
928 if (!E->getLHS()->isEvaluatable(Info.Ctx))
929 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000930
Anders Carlsson564730a2008-12-01 02:07:06 +0000931 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000932 }
933
934 if (E->isLogicalOp()) {
935 // These need to be handled specially because the operands aren't
936 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000937 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +0000938
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000939 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +0000940 // We were able to evaluate the LHS, see if we can get away with not
941 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000942 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000943 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000944
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000945 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +0000946 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000947 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000948 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000949 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000950 }
951 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000952 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +0000953 // We can't evaluate the LHS; however, sometimes the result
954 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +0000955 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000956 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000957 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000958 // must have had side effects.
959 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000960
961 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000962 }
963 }
Anders Carlsson59689ed2008-11-22 21:04:56 +0000964 }
Eli Friedman5a332ea2008-11-13 06:09:17 +0000965
Eli Friedman5a332ea2008-11-13 06:09:17 +0000966 return false;
967 }
968
Anders Carlssonacc79812008-11-16 07:17:21 +0000969 QualType LHSTy = E->getLHS()->getType();
970 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000971
972 if (LHSTy->isAnyComplexType()) {
973 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
974 APValue LHS, RHS;
975
976 if (!EvaluateComplex(E->getLHS(), LHS, Info))
977 return false;
978
979 if (!EvaluateComplex(E->getRHS(), RHS, Info))
980 return false;
981
982 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +0000983 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000984 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +0000985 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000986 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
987
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000988 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000989 return Success((CR_r == APFloat::cmpEqual &&
990 CR_i == APFloat::cmpEqual), E);
991 else {
992 assert(E->getOpcode() == BinaryOperator::NE &&
993 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +0000994 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000995 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +0000996 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000997 CR_i == APFloat::cmpLessThan)), E);
998 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000999 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001000 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001001 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1002 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1003 else {
1004 assert(E->getOpcode() == BinaryOperator::NE &&
1005 "Invalid compex comparison.");
1006 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1007 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1008 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001009 }
1010 }
Mike Stump11289f42009-09-09 15:08:12 +00001011
Anders Carlssonacc79812008-11-16 07:17:21 +00001012 if (LHSTy->isRealFloatingType() &&
1013 RHSTy->isRealFloatingType()) {
1014 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001015
Anders Carlssonacc79812008-11-16 07:17:21 +00001016 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1017 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001018
Anders Carlssonacc79812008-11-16 07:17:21 +00001019 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1020 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001021
Anders Carlssonacc79812008-11-16 07:17:21 +00001022 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001023
Anders Carlssonacc79812008-11-16 07:17:21 +00001024 switch (E->getOpcode()) {
1025 default:
1026 assert(0 && "Invalid binary operator!");
1027 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001028 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001029 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001030 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001031 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001032 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001033 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001034 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001035 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001036 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001037 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001038 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001039 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001040 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001041 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001042 }
Mike Stump11289f42009-09-09 15:08:12 +00001043
Eli Friedmana38da572009-04-28 19:17:36 +00001044 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1045 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001046 APValue LHSValue;
1047 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1048 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001049
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001050 APValue RHSValue;
1051 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1052 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001053
Eli Friedman334046a2009-06-14 02:17:33 +00001054 // Reject any bases from the normal codepath; we special-case comparisons
1055 // to null.
1056 if (LHSValue.getLValueBase()) {
1057 if (!E->isEqualityOp())
1058 return false;
1059 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1060 return false;
1061 bool bres;
1062 if (!EvalPointerValueAsBool(LHSValue, bres))
1063 return false;
1064 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1065 } else if (RHSValue.getLValueBase()) {
1066 if (!E->isEqualityOp())
1067 return false;
1068 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1069 return false;
1070 bool bres;
1071 if (!EvalPointerValueAsBool(RHSValue, bres))
1072 return false;
1073 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1074 }
Eli Friedman64004332009-03-23 04:38:34 +00001075
Eli Friedmana38da572009-04-28 19:17:36 +00001076 if (E->getOpcode() == BinaryOperator::Sub) {
1077 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001078 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001079
Eli Friedmana38da572009-04-28 19:17:36 +00001080 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001081 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1082 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001083
Eli Friedmana38da572009-04-28 19:17:36 +00001084 return Success(D, E);
1085 }
1086 bool Result;
1087 if (E->getOpcode() == BinaryOperator::EQ) {
1088 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001089 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001090 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1091 }
1092 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001093 }
1094 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001095 if (!LHSTy->isIntegralType() ||
1096 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001097 // We can't continue from here for non-integral types, and they
1098 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001099 return false;
1100 }
1101
Anders Carlsson9c181652008-07-08 14:35:21 +00001102 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001103 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001104 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001105
Eli Friedman94c25c62009-03-24 01:14:50 +00001106 APValue RHSVal;
1107 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001108 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001109
1110 // Handle cases like (unsigned long)&a + 4.
1111 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1112 uint64_t offset = Result.getLValueOffset();
1113 if (E->getOpcode() == BinaryOperator::Add)
1114 offset += RHSVal.getInt().getZExtValue();
1115 else
1116 offset -= RHSVal.getInt().getZExtValue();
1117 Result = APValue(Result.getLValueBase(), offset);
1118 return true;
1119 }
1120
1121 // Handle cases like 4 + (unsigned long)&a
1122 if (E->getOpcode() == BinaryOperator::Add &&
1123 RHSVal.isLValue() && Result.isInt()) {
1124 uint64_t offset = RHSVal.getLValueOffset();
1125 offset += Result.getInt().getZExtValue();
1126 Result = APValue(RHSVal.getLValueBase(), offset);
1127 return true;
1128 }
1129
1130 // All the following cases expect both operands to be an integer
1131 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001132 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001133
Eli Friedman94c25c62009-03-24 01:14:50 +00001134 APSInt& RHS = RHSVal.getInt();
1135
Anders Carlsson9c181652008-07-08 14:35:21 +00001136 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001137 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001138 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001139 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1140 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1141 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1142 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1143 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1144 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001145 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001146 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001147 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001148 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001149 case BinaryOperator::Rem:
Chris Lattner99415702008-07-12 00:14:42 +00001150 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001151 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001152 return Success(Result.getInt() % RHS, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001153 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001154 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001155 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001156 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1157 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001158 }
1159 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001160 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001161 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1162 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001165 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1166 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1167 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1168 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1169 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1170 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001171 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001172}
1173
Nuno Lopes42042612008-11-16 19:28:31 +00001174bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001175 bool Cond;
1176 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001177 return false;
1178
Nuno Lopes527b5a62008-11-16 22:06:39 +00001179 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001180}
1181
Chris Lattner68061312009-01-24 21:53:27 +00001182unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001183 // Get information about the alignment.
1184 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001185
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001186 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001187 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001188}
1189
Chris Lattner68061312009-01-24 21:53:27 +00001190unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1191 E = E->IgnoreParens();
1192
1193 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001194 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001195 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001196 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedman64004332009-03-23 04:38:34 +00001197
Chris Lattner68061312009-01-24 21:53:27 +00001198 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001199 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattner68061312009-01-24 21:53:27 +00001200
Chris Lattner24aeeab2009-01-24 21:09:06 +00001201 return GetAlignOfType(E->getType());
1202}
1203
1204
Sebastian Redl6f282892008-11-11 17:56:53 +00001205/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1206/// expression's type.
1207bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1208 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001209
Chris Lattner24aeeab2009-01-24 21:09:06 +00001210 // Handle alignof separately.
1211 if (!E->isSizeOf()) {
1212 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001213 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001214 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001215 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001216 }
Eli Friedman64004332009-03-23 04:38:34 +00001217
Sebastian Redl6f282892008-11-11 17:56:53 +00001218 QualType SrcTy = E->getTypeOfArgument();
1219
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001220 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1221 // extension.
1222 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1223 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001224
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001225 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001226 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001227 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001228
Chris Lattner24aeeab2009-01-24 21:09:06 +00001229 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001230 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001231 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001232}
1233
Chris Lattnere13042c2008-07-11 19:10:17 +00001234bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001235 // Special case unary operators that do not need their subexpression
1236 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001237 if (E->isOffsetOfOp()) {
1238 // The AST for offsetof is defined in such a way that we can just
1239 // directly Evaluate it as an l-value.
1240 APValue LV;
1241 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1242 return false;
1243 if (LV.getLValueBase())
1244 return false;
1245 return Success(LV.getLValueOffset(), E);
1246 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001247
1248 if (E->getOpcode() == UnaryOperator::LNot) {
1249 // LNot's operand isn't necessarily an integer, so we handle it specially.
1250 bool bres;
1251 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1252 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001253 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001254 }
1255
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001256 // Only handle integral operations...
1257 if (!E->getSubExpr()->getType()->isIntegralType())
1258 return false;
1259
Chris Lattnercdf34e72008-07-11 22:52:41 +00001260 // Get the operand value into 'Result'.
1261 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001262 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001263
Chris Lattnerf09ad162008-07-11 22:15:16 +00001264 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001265 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001266 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1267 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001268 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001269 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001270 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1271 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001272 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001273 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001274 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001275 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001276 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001277 if (!Result.isInt()) return false;
1278 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001279 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001280 if (!Result.isInt()) return false;
1281 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001282 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001283}
Mike Stump11289f42009-09-09 15:08:12 +00001284
Chris Lattner477c4be2008-07-12 01:15:53 +00001285/// HandleCast - This is used to evaluate implicit or explicit casts where the
1286/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001287bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001288 Expr *SubExpr = E->getSubExpr();
1289 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001290 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001291
Eli Friedman9a156e52008-11-12 09:44:48 +00001292 if (DestType->isBooleanType()) {
1293 bool BoolResult;
1294 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1295 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001296 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001297 }
1298
Anders Carlsson9c181652008-07-08 14:35:21 +00001299 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001300 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001301 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001302 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001303
Eli Friedman742421e2009-02-20 01:15:07 +00001304 if (!Result.isInt()) {
1305 // Only allow casts of lvalues if they are lossless.
1306 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1307 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001308
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001309 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001310 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001311 }
Mike Stump11289f42009-09-09 15:08:12 +00001312
Chris Lattner477c4be2008-07-12 01:15:53 +00001313 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001314 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001315 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001316 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001317 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001318
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001319 if (LV.getLValueBase()) {
1320 // Only allow based lvalue casts if they are lossless.
1321 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1322 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001323
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001324 Result = LV;
1325 return true;
1326 }
1327
1328 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1329 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001330 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001331
Eli Friedman742421e2009-02-20 01:15:07 +00001332 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1333 // This handles double-conversion cases, where there's both
1334 // an l-value promotion and an implicit conversion to int.
1335 APValue LV;
1336 if (!EvaluateLValue(SubExpr, LV, Info))
1337 return false;
1338
1339 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1340 return false;
1341
1342 Result = LV;
1343 return true;
1344 }
1345
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001346 if (SrcType->isAnyComplexType()) {
1347 APValue C;
1348 if (!EvaluateComplex(SubExpr, C, Info))
1349 return false;
1350 if (C.isComplexFloat())
1351 return Success(HandleFloatToIntCast(DestType, SrcType,
1352 C.getComplexFloatReal(), Info.Ctx),
1353 E);
1354 else
1355 return Success(HandleIntToIntCast(DestType, SrcType,
1356 C.getComplexIntReal(), Info.Ctx), E);
1357 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001358 // FIXME: Handle vectors
1359
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001360 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001361 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001362
Eli Friedman24c01542008-08-22 00:06:13 +00001363 APFloat F(0.0);
1364 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001365 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001366
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001367 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001368}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001369
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001370bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1371 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1372 APValue LV;
1373 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1374 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1375 return Success(LV.getComplexIntReal(), E);
1376 }
1377
1378 return Visit(E->getSubExpr());
1379}
1380
Eli Friedman4e7a2412009-02-27 04:45:43 +00001381bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001382 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1383 APValue LV;
1384 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1385 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1386 return Success(LV.getComplexIntImag(), E);
1387 }
1388
Eli Friedman4e7a2412009-02-27 04:45:43 +00001389 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1390 Info.EvalResult.HasSideEffects = true;
1391 return Success(0, E);
1392}
1393
Chris Lattner05706e882008-07-11 18:11:29 +00001394//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001395// Float Evaluation
1396//===----------------------------------------------------------------------===//
1397
1398namespace {
1399class VISIBILITY_HIDDEN FloatExprEvaluator
1400 : public StmtVisitor<FloatExprEvaluator, bool> {
1401 EvalInfo &Info;
1402 APFloat &Result;
1403public:
1404 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1405 : Info(info), Result(result) {}
1406
1407 bool VisitStmt(Stmt *S) {
1408 return false;
1409 }
1410
1411 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001412 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001413
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001414 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001415 bool VisitBinaryOperator(const BinaryOperator *E);
1416 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001417 bool VisitCastExpr(CastExpr *E);
1418 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001419
Eli Friedman449fe542009-03-23 04:56:01 +00001420 bool VisitChooseExpr(const ChooseExpr *E)
1421 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1422 bool VisitUnaryExtension(const UnaryOperator *E)
1423 { return Visit(E->getSubExpr()); }
1424
1425 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1426 // member of vector, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001427 // conditional ?:, comma
Eli Friedman24c01542008-08-22 00:06:13 +00001428};
1429} // end anonymous namespace
1430
1431static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1432 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1433}
1434
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001435bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001436 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001437 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001438 case Builtin::BI__builtin_huge_val:
1439 case Builtin::BI__builtin_huge_valf:
1440 case Builtin::BI__builtin_huge_vall:
1441 case Builtin::BI__builtin_inf:
1442 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001443 case Builtin::BI__builtin_infl: {
1444 const llvm::fltSemantics &Sem =
1445 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001446 Result = llvm::APFloat::getInf(Sem);
1447 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001448 }
Mike Stump11289f42009-09-09 15:08:12 +00001449
Chris Lattner0b7282e2008-10-06 06:31:58 +00001450 case Builtin::BI__builtin_nan:
1451 case Builtin::BI__builtin_nanf:
1452 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001453 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001454 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001455 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001456 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001457 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001458 const llvm::fltSemantics &Sem =
1459 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001460 llvm::SmallString<16> s;
1461 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1462 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001463 long l;
1464 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001465 l = strtol(&s[0], &endp, 0);
1466 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001467 return false;
1468 unsigned type = (unsigned int)l;;
1469 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001470 return true;
1471 }
1472 }
1473 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001474
1475 case Builtin::BI__builtin_fabs:
1476 case Builtin::BI__builtin_fabsf:
1477 case Builtin::BI__builtin_fabsl:
1478 if (!EvaluateFloat(E->getArg(0), Result, Info))
1479 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001480
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001481 if (Result.isNegative())
1482 Result.changeSign();
1483 return true;
1484
Mike Stump11289f42009-09-09 15:08:12 +00001485 case Builtin::BI__builtin_copysign:
1486 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001487 case Builtin::BI__builtin_copysignl: {
1488 APFloat RHS(0.);
1489 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1490 !EvaluateFloat(E->getArg(1), RHS, Info))
1491 return false;
1492 Result.copySign(RHS);
1493 return true;
1494 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001495 }
1496}
1497
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001498bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001499 if (E->getOpcode() == UnaryOperator::Deref)
1500 return false;
1501
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001502 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1503 return false;
1504
1505 switch (E->getOpcode()) {
1506 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001507 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001508 return true;
1509 case UnaryOperator::Minus:
1510 Result.changeSign();
1511 return true;
1512 }
1513}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001514
Eli Friedman24c01542008-08-22 00:06:13 +00001515bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1516 // FIXME: Diagnostics? I really don't understand how the warnings
1517 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001518 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001519 if (!EvaluateFloat(E->getLHS(), Result, Info))
1520 return false;
1521 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1522 return false;
1523
1524 switch (E->getOpcode()) {
1525 default: return false;
1526 case BinaryOperator::Mul:
1527 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1528 return true;
1529 case BinaryOperator::Add:
1530 Result.add(RHS, APFloat::rmNearestTiesToEven);
1531 return true;
1532 case BinaryOperator::Sub:
1533 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1534 return true;
1535 case BinaryOperator::Div:
1536 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1537 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001538 }
1539}
1540
1541bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1542 Result = E->getValue();
1543 return true;
1544}
1545
Eli Friedman9a156e52008-11-12 09:44:48 +00001546bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1547 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001548
Eli Friedman9a156e52008-11-12 09:44:48 +00001549 if (SubExpr->getType()->isIntegralType()) {
1550 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001551 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001552 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001553 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001554 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001555 return true;
1556 }
1557 if (SubExpr->getType()->isRealFloatingType()) {
1558 if (!Visit(SubExpr))
1559 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001560 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1561 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001562 return true;
1563 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001564 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001565
1566 return false;
1567}
1568
1569bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1570 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1571 return true;
1572}
1573
Eli Friedman24c01542008-08-22 00:06:13 +00001574//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001575// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001576//===----------------------------------------------------------------------===//
1577
1578namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001579class VISIBILITY_HIDDEN ComplexExprEvaluator
1580 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001581 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001582
Anders Carlsson537969c2008-11-16 20:27:53 +00001583public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001584 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001585
Anders Carlsson537969c2008-11-16 20:27:53 +00001586 //===--------------------------------------------------------------------===//
1587 // Visitor Methods
1588 //===--------------------------------------------------------------------===//
1589
1590 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001591 return APValue();
1592 }
Mike Stump11289f42009-09-09 15:08:12 +00001593
Anders Carlsson537969c2008-11-16 20:27:53 +00001594 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1595
1596 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001597 Expr* SubExpr = E->getSubExpr();
1598
1599 if (SubExpr->getType()->isRealFloatingType()) {
1600 APFloat Result(0.0);
1601
1602 if (!EvaluateFloat(SubExpr, Result, Info))
1603 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001604
1605 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001606 Result);
1607 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001608 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001609 "Unexpected imaginary literal.");
1610
1611 llvm::APSInt Result;
1612 if (!EvaluateInteger(SubExpr, Result, Info))
1613 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001614
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001615 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1616 Zero = 0;
1617 return APValue(Zero, Result);
1618 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001619 }
1620
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001621 APValue VisitCastExpr(CastExpr *E) {
1622 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001623 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001624 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001625
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001626 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001627 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001628
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001629 if (!EvaluateFloat(SubExpr, Result, Info))
1630 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001631
1632 if (EltType->isRealFloatingType()) {
1633 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001634 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001635 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1636 } else {
1637 llvm::APSInt IResult;
1638 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1639 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1640 Zero = 0;
1641 return APValue(IResult, Zero);
1642 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001643 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001644 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001645
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001646 if (!EvaluateInteger(SubExpr, Result, Info))
1647 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001648
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001649 if (EltType->isRealFloatingType()) {
1650 APFloat FResult =
1651 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001652 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001653 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1654 } else {
1655 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1656 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1657 Zero = 0;
1658 return APValue(Result, Zero);
1659 }
John McCall9dd450b2009-09-21 23:43:11 +00001660 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001661 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001662
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001663 if (!EvaluateComplex(SubExpr, Src, Info))
1664 return APValue();
1665
1666 QualType SrcType = CT->getElementType();
1667
1668 if (Src.isComplexFloat()) {
1669 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001670 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001671 Src.getComplexFloatReal(),
1672 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001673 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001674 Src.getComplexFloatImag(),
1675 Info.Ctx));
1676 } else {
1677 return APValue(HandleFloatToIntCast(EltType, SrcType,
1678 Src.getComplexFloatReal(),
1679 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001680 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001681 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001682 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001683 }
1684 } else {
1685 assert(Src.isComplexInt() && "Invalid evaluate result.");
1686 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001687 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001688 Src.getComplexIntReal(),
1689 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001690 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001691 Src.getComplexIntImag(),
1692 Info.Ctx));
1693 } else {
1694 return APValue(HandleIntToIntCast(EltType, SrcType,
1695 Src.getComplexIntReal(),
1696 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001697 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001698 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001699 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001700 }
1701 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001702 }
1703
1704 // FIXME: Handle more casts.
1705 return APValue();
1706 }
Mike Stump11289f42009-09-09 15:08:12 +00001707
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001708 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001709 APValue VisitChooseExpr(const ChooseExpr *E)
1710 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1711 APValue VisitUnaryExtension(const UnaryOperator *E)
1712 { return Visit(E->getSubExpr()); }
1713 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001714 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001715};
1716} // end anonymous namespace
1717
Mike Stump11289f42009-09-09 15:08:12 +00001718static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001719 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1720 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001721 (&Result.getComplexFloatReal().getSemantics() ==
1722 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001723 "Invalid complex evaluation.");
1724 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001725}
1726
Mike Stump11289f42009-09-09 15:08:12 +00001727APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001728 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001729
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001730 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001731 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001732
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001733 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001734 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001735
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001736 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1737 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001738 switch (E->getOpcode()) {
1739 default: return APValue();
1740 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001741 if (Result.isComplexFloat()) {
1742 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1743 APFloat::rmNearestTiesToEven);
1744 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1745 APFloat::rmNearestTiesToEven);
1746 } else {
1747 Result.getComplexIntReal() += RHS.getComplexIntReal();
1748 Result.getComplexIntImag() += RHS.getComplexIntImag();
1749 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001750 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001751 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001752 if (Result.isComplexFloat()) {
1753 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1754 APFloat::rmNearestTiesToEven);
1755 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1756 APFloat::rmNearestTiesToEven);
1757 } else {
1758 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1759 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1760 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001761 break;
1762 case BinaryOperator::Mul:
1763 if (Result.isComplexFloat()) {
1764 APValue LHS = Result;
1765 APFloat &LHS_r = LHS.getComplexFloatReal();
1766 APFloat &LHS_i = LHS.getComplexFloatImag();
1767 APFloat &RHS_r = RHS.getComplexFloatReal();
1768 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001769
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001770 APFloat Tmp = LHS_r;
1771 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1772 Result.getComplexFloatReal() = Tmp;
1773 Tmp = LHS_i;
1774 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1775 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1776
1777 Tmp = LHS_r;
1778 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1779 Result.getComplexFloatImag() = Tmp;
1780 Tmp = LHS_i;
1781 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1782 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1783 } else {
1784 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001785 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001786 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1787 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001788 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001789 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1790 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1791 }
1792 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001793 }
1794
1795 return Result;
1796}
1797
Anders Carlsson537969c2008-11-16 20:27:53 +00001798//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001799// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001800//===----------------------------------------------------------------------===//
1801
Chris Lattner67d7b922008-11-16 21:24:15 +00001802/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001803/// any crazy technique (that has nothing to do with language standards) that
1804/// we want to. If this function returns true, it returns the folded constant
1805/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001806bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1807 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001808
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001809 if (getType()->isVectorType()) {
1810 if (!EvaluateVector(this, Result.Val, Info))
1811 return false;
1812 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001813 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001814 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001815 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001816 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001817 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001818 } else if (getType()->isRealFloatingType()) {
1819 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001820 if (!EvaluateFloat(this, f, Info))
1821 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001822
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001823 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001824 } else if (getType()->isAnyComplexType()) {
1825 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001826 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001827 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001828 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001829
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001830 return true;
1831}
1832
Mike Stump5183a142009-10-26 23:05:19 +00001833bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1834 EvalInfo Info(Ctx, Result, true);
1835
1836 if (getType()->isVectorType()) {
1837 if (!EvaluateVector(this, Result.Val, Info))
1838 return false;
1839 } else if (getType()->isIntegerType()) {
1840 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1841 return false;
1842 } else if (getType()->hasPointerRepresentation()) {
1843 if (!EvaluatePointer(this, Result.Val, Info))
1844 return false;
1845 } else if (getType()->isRealFloatingType()) {
1846 llvm::APFloat f(0.0);
1847 if (!EvaluateFloat(this, f, Info))
1848 return false;
1849
1850 Result.Val = APValue(f);
1851 } else if (getType()->isAnyComplexType()) {
1852 if (!EvaluateComplex(this, Result.Val, Info))
1853 return false;
1854 } else
1855 return false;
1856
1857 return true;
1858}
1859
Anders Carlsson43168122009-04-10 04:54:13 +00001860bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1861 EvalInfo Info(Ctx, Result);
1862
1863 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1864}
1865
Eli Friedman7d45c482009-09-13 10:17:44 +00001866bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1867 EvalInfo Info(Ctx, Result, true);
1868
1869 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1870}
1871
Chris Lattner67d7b922008-11-16 21:24:15 +00001872/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001873/// folded, but discard the result.
1874bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001875 EvalResult Result;
1876 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001877}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001878
1879APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001880 EvalResult EvalResult;
1881 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001882 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001883 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001884 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001885
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001886 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001887}