blob: f1a446ef6e748d60d1cb879abbe0f3a3b1f10760 [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;
880 if (Arg->Evaluate(Base, Info.Ctx)
881 && 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;
887 Size -= Base.Val.getLValueOffset();
888 return Success(Size, E);
889 }
890 }
891
892 if (Base.HasSideEffects) {
Mike Stump99f11f72009-10-26 18:57:47 +0000893 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump722cedf2009-10-26 18:35:08 +0000894 return Success(-1, E);
895 return Success(0, E);
896 }
897
898 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
899 }
900
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000901 case Builtin::BI__builtin_classify_type:
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000902 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Anders Carlsson4c76e932008-11-24 04:21:33 +0000904 case Builtin::BI__builtin_constant_p:
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000905 // __builtin_constant_p always has one operand: it returns true if that
906 // operand can be folded, false otherwise.
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000907 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattnerd545ad12009-09-23 06:06:36 +0000908
909 case Builtin::BI__builtin_eh_return_data_regno: {
910 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
911 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
912 return Success(Operand, E);
913 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +0000914 }
Chris Lattner7174bf32008-07-12 00:38:25 +0000915}
Anders Carlsson4a3585b2008-07-08 15:34:11 +0000916
Chris Lattnere13042c2008-07-11 19:10:17 +0000917bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman5a332ea2008-11-13 06:09:17 +0000918 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson564730a2008-12-01 02:07:06 +0000919 if (!Visit(E->getRHS()))
920 return false;
Anders Carlsson5b3638b2008-12-01 06:44:05 +0000921
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000922 // If we can't evaluate the LHS, it might have side effects;
923 // conservatively mark it.
924 if (!E->getLHS()->isEvaluatable(Info.Ctx))
925 Info.EvalResult.HasSideEffects = true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000926
Anders Carlsson564730a2008-12-01 02:07:06 +0000927 return true;
Eli Friedman5a332ea2008-11-13 06:09:17 +0000928 }
929
930 if (E->isLogicalOp()) {
931 // These need to be handled specially because the operands aren't
932 // necessarily integral
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000933 bool lhsResult, rhsResult;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000935 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson59689ed2008-11-22 21:04:56 +0000936 // We were able to evaluate the LHS, see if we can get away with not
937 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman9cb9ff42009-02-26 10:19:36 +0000938 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbare3c92bc2009-02-19 18:37:50 +0000939 return Success(lhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000940
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000941 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +0000942 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000943 return Success(lhsResult || rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000944 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000945 return Success(lhsResult && rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000946 }
947 } else {
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000948 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4c76e932008-11-24 04:21:33 +0000949 // We can't evaluate the LHS; however, sometimes the result
950 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump11289f42009-09-09 15:08:12 +0000951 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000952 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000953 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonf50de0c2008-11-30 16:51:17 +0000954 // must have had side effects.
955 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000956
957 return Success(rhsResult, E);
Anders Carlsson4c76e932008-11-24 04:21:33 +0000958 }
959 }
Anders Carlsson59689ed2008-11-22 21:04:56 +0000960 }
Eli Friedman5a332ea2008-11-13 06:09:17 +0000961
Eli Friedman5a332ea2008-11-13 06:09:17 +0000962 return false;
963 }
964
Anders Carlssonacc79812008-11-16 07:17:21 +0000965 QualType LHSTy = E->getLHS()->getType();
966 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000967
968 if (LHSTy->isAnyComplexType()) {
969 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
970 APValue LHS, RHS;
971
972 if (!EvaluateComplex(E->getLHS(), LHS, Info))
973 return false;
974
975 if (!EvaluateComplex(E->getRHS(), RHS, Info))
976 return false;
977
978 if (LHS.isComplexFloat()) {
Mike Stump11289f42009-09-09 15:08:12 +0000979 APFloat::cmpResult CR_r =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000980 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump11289f42009-09-09 15:08:12 +0000981 APFloat::cmpResult CR_i =
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000982 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
983
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000984 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000985 return Success((CR_r == APFloat::cmpEqual &&
986 CR_i == APFloat::cmpEqual), E);
987 else {
988 assert(E->getOpcode() == BinaryOperator::NE &&
989 "Invalid complex comparison.");
Mike Stump11289f42009-09-09 15:08:12 +0000990 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000991 CR_r == APFloat::cmpLessThan) &&
Mike Stump11289f42009-09-09 15:08:12 +0000992 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000993 CR_i == APFloat::cmpLessThan)), E);
994 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000995 } else {
Daniel Dunbar74f2425b2009-01-29 06:43:41 +0000996 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar8aafc892009-02-19 09:06:44 +0000997 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
998 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
999 else {
1000 assert(E->getOpcode() == BinaryOperator::NE &&
1001 "Invalid compex comparison.");
1002 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1003 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1004 }
Daniel Dunbar74f2425b2009-01-29 06:43:41 +00001005 }
1006 }
Mike Stump11289f42009-09-09 15:08:12 +00001007
Anders Carlssonacc79812008-11-16 07:17:21 +00001008 if (LHSTy->isRealFloatingType() &&
1009 RHSTy->isRealFloatingType()) {
1010 APFloat RHS(0.0), LHS(0.0);
Mike Stump11289f42009-09-09 15:08:12 +00001011
Anders Carlssonacc79812008-11-16 07:17:21 +00001012 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1013 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Anders Carlssonacc79812008-11-16 07:17:21 +00001015 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1016 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001017
Anders Carlssonacc79812008-11-16 07:17:21 +00001018 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson899c7052008-11-16 22:46:56 +00001019
Anders Carlssonacc79812008-11-16 07:17:21 +00001020 switch (E->getOpcode()) {
1021 default:
1022 assert(0 && "Invalid binary operator!");
1023 case BinaryOperator::LT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001024 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001025 case BinaryOperator::GT:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001026 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001027 case BinaryOperator::LE:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001028 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001029 case BinaryOperator::GE:
Mike Stump11289f42009-09-09 15:08:12 +00001030 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001031 E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001032 case BinaryOperator::EQ:
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001033 return Success(CR == APFloat::cmpEqual, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001034 case BinaryOperator::NE:
Mike Stump11289f42009-09-09 15:08:12 +00001035 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001036 || CR == APFloat::cmpLessThan, E);
Anders Carlssonacc79812008-11-16 07:17:21 +00001037 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001038 }
Mike Stump11289f42009-09-09 15:08:12 +00001039
Eli Friedmana38da572009-04-28 19:17:36 +00001040 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1041 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001042 APValue LHSValue;
1043 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1044 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001045
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001046 APValue RHSValue;
1047 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1048 return false;
Eli Friedman64004332009-03-23 04:38:34 +00001049
Eli Friedman334046a2009-06-14 02:17:33 +00001050 // Reject any bases from the normal codepath; we special-case comparisons
1051 // to null.
1052 if (LHSValue.getLValueBase()) {
1053 if (!E->isEqualityOp())
1054 return false;
1055 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1056 return false;
1057 bool bres;
1058 if (!EvalPointerValueAsBool(LHSValue, bres))
1059 return false;
1060 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1061 } else if (RHSValue.getLValueBase()) {
1062 if (!E->isEqualityOp())
1063 return false;
1064 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1065 return false;
1066 bool bres;
1067 if (!EvalPointerValueAsBool(RHSValue, bres))
1068 return false;
1069 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1070 }
Eli Friedman64004332009-03-23 04:38:34 +00001071
Eli Friedmana38da572009-04-28 19:17:36 +00001072 if (E->getOpcode() == BinaryOperator::Sub) {
1073 const QualType Type = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001074 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001075
Eli Friedmana38da572009-04-28 19:17:36 +00001076 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmanfa90b152009-06-04 20:23:20 +00001077 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1078 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedman64004332009-03-23 04:38:34 +00001079
Eli Friedmana38da572009-04-28 19:17:36 +00001080 return Success(D, E);
1081 }
1082 bool Result;
1083 if (E->getOpcode() == BinaryOperator::EQ) {
1084 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman8b171f62009-04-29 20:29:43 +00001085 } else {
Eli Friedmana38da572009-04-28 19:17:36 +00001086 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1087 }
1088 return Success(Result, E);
Anders Carlsson9f9e4242008-11-16 19:01:22 +00001089 }
1090 }
Anders Carlssonacc79812008-11-16 07:17:21 +00001091 if (!LHSTy->isIntegralType() ||
1092 !RHSTy->isIntegralType()) {
Eli Friedman5a332ea2008-11-13 06:09:17 +00001093 // We can't continue from here for non-integral types, and they
1094 // could potentially confuse the following operations.
Eli Friedman5a332ea2008-11-13 06:09:17 +00001095 return false;
1096 }
1097
Anders Carlsson9c181652008-07-08 14:35:21 +00001098 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001099 if (!Visit(E->getLHS()))
Chris Lattner99415702008-07-12 00:14:42 +00001100 return false; // error in subexpression.
Eli Friedmanbd840592008-07-27 05:46:18 +00001101
Eli Friedman94c25c62009-03-24 01:14:50 +00001102 APValue RHSVal;
1103 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001104 return false;
Eli Friedman94c25c62009-03-24 01:14:50 +00001105
1106 // Handle cases like (unsigned long)&a + 4.
1107 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1108 uint64_t offset = Result.getLValueOffset();
1109 if (E->getOpcode() == BinaryOperator::Add)
1110 offset += RHSVal.getInt().getZExtValue();
1111 else
1112 offset -= RHSVal.getInt().getZExtValue();
1113 Result = APValue(Result.getLValueBase(), offset);
1114 return true;
1115 }
1116
1117 // Handle cases like 4 + (unsigned long)&a
1118 if (E->getOpcode() == BinaryOperator::Add &&
1119 RHSVal.isLValue() && Result.isInt()) {
1120 uint64_t offset = RHSVal.getLValueOffset();
1121 offset += Result.getInt().getZExtValue();
1122 Result = APValue(RHSVal.getLValueBase(), offset);
1123 return true;
1124 }
1125
1126 // All the following cases expect both operands to be an integer
1127 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnere13042c2008-07-11 19:10:17 +00001128 return false;
Eli Friedman5a332ea2008-11-13 06:09:17 +00001129
Eli Friedman94c25c62009-03-24 01:14:50 +00001130 APSInt& RHS = RHSVal.getInt();
1131
Anders Carlsson9c181652008-07-08 14:35:21 +00001132 switch (E->getOpcode()) {
Chris Lattnerfac05ae2008-11-12 07:43:42 +00001133 default:
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001134 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001135 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1136 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1137 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1138 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1139 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1140 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001141 case BinaryOperator::Div:
Chris Lattner99415702008-07-12 00:14:42 +00001142 if (RHS == 0)
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001143 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001144 return Success(Result.getInt() / RHS, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001145 case BinaryOperator::Rem:
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);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001149 case BinaryOperator::Shl: {
Chris Lattner99415702008-07-12 00:14:42 +00001150 // FIXME: Warn about out of range shift amounts!
Mike Stump11289f42009-09-09 15:08:12 +00001151 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001152 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1153 return Success(Result.getInt() << SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001154 }
1155 case BinaryOperator::Shr: {
Mike Stump11289f42009-09-09 15:08:12 +00001156 unsigned SA =
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001157 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1158 return Success(Result.getInt() >> SA, E);
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001159 }
Mike Stump11289f42009-09-09 15:08:12 +00001160
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001161 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1162 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1163 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1164 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1165 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1166 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedman8553a982008-11-13 02:13:11 +00001167 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001168}
1169
Nuno Lopes42042612008-11-16 19:28:31 +00001170bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes527b5a62008-11-16 22:06:39 +00001171 bool Cond;
1172 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopes42042612008-11-16 19:28:31 +00001173 return false;
1174
Nuno Lopes527b5a62008-11-16 22:06:39 +00001175 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopes42042612008-11-16 19:28:31 +00001176}
1177
Chris Lattner68061312009-01-24 21:53:27 +00001178unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattner24aeeab2009-01-24 21:09:06 +00001179 // Get information about the alignment.
1180 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregoref462e62009-04-30 17:32:17 +00001181
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001182 // __alignof is defined to return the preferred alignment.
Douglas Gregoref462e62009-04-30 17:32:17 +00001183 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattner24aeeab2009-01-24 21:09:06 +00001184}
1185
Chris Lattner68061312009-01-24 21:53:27 +00001186unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1187 E = E->IgnoreParens();
1188
1189 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump11289f42009-09-09 15:08:12 +00001190 // to 1 in those cases.
Chris Lattner68061312009-01-24 21:53:27 +00001191 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001192 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedman64004332009-03-23 04:38:34 +00001193
Chris Lattner68061312009-01-24 21:53:27 +00001194 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +00001195 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattner68061312009-01-24 21:53:27 +00001196
Chris Lattner24aeeab2009-01-24 21:09:06 +00001197 return GetAlignOfType(E->getType());
1198}
1199
1200
Sebastian Redl6f282892008-11-11 17:56:53 +00001201/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1202/// expression's type.
1203bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1204 QualType DstTy = E->getType();
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001205
Chris Lattner24aeeab2009-01-24 21:09:06 +00001206 // Handle alignof separately.
1207 if (!E->isSizeOf()) {
1208 if (E->isArgumentType())
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001209 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001210 else
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001211 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattner24aeeab2009-01-24 21:09:06 +00001212 }
Eli Friedman64004332009-03-23 04:38:34 +00001213
Sebastian Redl6f282892008-11-11 17:56:53 +00001214 QualType SrcTy = E->getTypeOfArgument();
1215
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001216 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1217 // extension.
1218 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1219 return Success(1, E);
Eli Friedman64004332009-03-23 04:38:34 +00001220
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001221 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner24aeeab2009-01-24 21:09:06 +00001222 if (!SrcTy->isConstantSizeType())
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001223 return false;
Eli Friedman2aa38fe2009-01-24 22:19:05 +00001224
Chris Lattner24aeeab2009-01-24 21:09:06 +00001225 // Get information about the size.
Daniel Dunbarbc5a7a82009-05-03 10:35:52 +00001226 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbar93c19142009-04-21 15:48:54 +00001227 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerf8d7f722008-07-11 21:24:13 +00001228}
1229
Chris Lattnere13042c2008-07-11 19:10:17 +00001230bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001231 // Special case unary operators that do not need their subexpression
1232 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman988a16b2009-02-27 06:44:11 +00001233 if (E->isOffsetOfOp()) {
1234 // The AST for offsetof is defined in such a way that we can just
1235 // directly Evaluate it as an l-value.
1236 APValue LV;
1237 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1238 return false;
1239 if (LV.getLValueBase())
1240 return false;
1241 return Success(LV.getLValueOffset(), E);
1242 }
Eli Friedman5a332ea2008-11-13 06:09:17 +00001243
1244 if (E->getOpcode() == UnaryOperator::LNot) {
1245 // LNot's operand isn't necessarily an integer, so we handle it specially.
1246 bool bres;
1247 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1248 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001249 return Success(!bres, E);
Eli Friedman5a332ea2008-11-13 06:09:17 +00001250 }
1251
Daniel Dunbar79e042a2009-02-21 18:14:20 +00001252 // Only handle integral operations...
1253 if (!E->getSubExpr()->getType()->isIntegralType())
1254 return false;
1255
Chris Lattnercdf34e72008-07-11 22:52:41 +00001256 // Get the operand value into 'Result'.
1257 if (!Visit(E->getSubExpr()))
Chris Lattnerf09ad162008-07-11 22:15:16 +00001258 return false;
Anders Carlsson9c181652008-07-08 14:35:21 +00001259
Chris Lattnerf09ad162008-07-11 22:15:16 +00001260 switch (E->getOpcode()) {
Chris Lattner7174bf32008-07-12 00:38:25 +00001261 default:
Chris Lattnerf09ad162008-07-11 22:15:16 +00001262 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1263 // See C99 6.6p3.
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001264 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001265 case UnaryOperator::Extension:
Chris Lattner7174bf32008-07-12 00:38:25 +00001266 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1267 // If so, we could clear the diagnostic ID.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001268 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001269 case UnaryOperator::Plus:
Mike Stump11289f42009-09-09 15:08:12 +00001270 // The result is always just the subexpr.
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001271 return true;
Chris Lattnerf09ad162008-07-11 22:15:16 +00001272 case UnaryOperator::Minus:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001273 if (!Result.isInt()) return false;
1274 return Success(-Result.getInt(), E);
Chris Lattnerf09ad162008-07-11 22:15:16 +00001275 case UnaryOperator::Not:
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001276 if (!Result.isInt()) return false;
1277 return Success(~Result.getInt(), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001278 }
Anders Carlsson9c181652008-07-08 14:35:21 +00001279}
Mike Stump11289f42009-09-09 15:08:12 +00001280
Chris Lattner477c4be2008-07-12 01:15:53 +00001281/// HandleCast - This is used to evaluate implicit or explicit casts where the
1282/// result type is integer.
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001283bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001284 Expr *SubExpr = E->getSubExpr();
1285 QualType DestType = E->getType();
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001286 QualType SrcType = SubExpr->getType();
Anders Carlsson27b8c5c2008-11-30 18:14:57 +00001287
Eli Friedman9a156e52008-11-12 09:44:48 +00001288 if (DestType->isBooleanType()) {
1289 bool BoolResult;
1290 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1291 return false;
Daniel Dunbar8aafc892009-02-19 09:06:44 +00001292 return Success(BoolResult, E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001293 }
1294
Anders Carlsson9c181652008-07-08 14:35:21 +00001295 // Handle simple integer->integer casts.
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001296 if (SrcType->isIntegralType()) {
Chris Lattner477c4be2008-07-12 01:15:53 +00001297 if (!Visit(SubExpr))
Chris Lattnere13042c2008-07-11 19:10:17 +00001298 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001299
Eli Friedman742421e2009-02-20 01:15:07 +00001300 if (!Result.isInt()) {
1301 // Only allow casts of lvalues if they are lossless.
1302 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1303 }
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001304
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001305 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001306 Result.getInt(), Info.Ctx), E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001307 }
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattner477c4be2008-07-12 01:15:53 +00001309 // FIXME: Clean this up!
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001310 if (SrcType->isPointerType()) {
Anders Carlsson9c181652008-07-08 14:35:21 +00001311 APValue LV;
Chris Lattnercdf34e72008-07-11 22:52:41 +00001312 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnere13042c2008-07-11 19:10:17 +00001313 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001314
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001315 if (LV.getLValueBase()) {
1316 // Only allow based lvalue casts if they are lossless.
1317 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1318 return false;
Eli Friedman9a156e52008-11-12 09:44:48 +00001319
Daniel Dunbar1c8560d2009-02-19 22:24:01 +00001320 Result = LV;
1321 return true;
1322 }
1323
1324 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1325 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001326 }
Eli Friedman9a156e52008-11-12 09:44:48 +00001327
Eli Friedman742421e2009-02-20 01:15:07 +00001328 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1329 // This handles double-conversion cases, where there's both
1330 // an l-value promotion and an implicit conversion to int.
1331 APValue LV;
1332 if (!EvaluateLValue(SubExpr, LV, Info))
1333 return false;
1334
1335 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1336 return false;
1337
1338 Result = LV;
1339 return true;
1340 }
1341
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001342 if (SrcType->isAnyComplexType()) {
1343 APValue C;
1344 if (!EvaluateComplex(SubExpr, C, Info))
1345 return false;
1346 if (C.isComplexFloat())
1347 return Success(HandleFloatToIntCast(DestType, SrcType,
1348 C.getComplexFloatReal(), Info.Ctx),
1349 E);
1350 else
1351 return Success(HandleIntToIntCast(DestType, SrcType,
1352 C.getComplexIntReal(), Info.Ctx), E);
1353 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001354 // FIXME: Handle vectors
1355
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001356 if (!SrcType->isRealFloatingType())
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001357 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner477c4be2008-07-12 01:15:53 +00001358
Eli Friedman24c01542008-08-22 00:06:13 +00001359 APFloat F(0.0);
1360 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlssonb33d6c82008-11-30 18:37:00 +00001361 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump11289f42009-09-09 15:08:12 +00001362
Daniel Dunbarcf04aa12009-02-19 22:16:29 +00001363 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlsson9c181652008-07-08 14:35:21 +00001364}
Anders Carlssonb5ad0212008-07-08 14:30:00 +00001365
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001366bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1367 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1368 APValue LV;
1369 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1370 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1371 return Success(LV.getComplexIntReal(), E);
1372 }
1373
1374 return Visit(E->getSubExpr());
1375}
1376
Eli Friedman4e7a2412009-02-27 04:45:43 +00001377bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedmana1c7b6c2009-02-28 03:59:05 +00001378 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1379 APValue LV;
1380 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1381 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1382 return Success(LV.getComplexIntImag(), E);
1383 }
1384
Eli Friedman4e7a2412009-02-27 04:45:43 +00001385 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1386 Info.EvalResult.HasSideEffects = true;
1387 return Success(0, E);
1388}
1389
Chris Lattner05706e882008-07-11 18:11:29 +00001390//===----------------------------------------------------------------------===//
Eli Friedman24c01542008-08-22 00:06:13 +00001391// Float Evaluation
1392//===----------------------------------------------------------------------===//
1393
1394namespace {
1395class VISIBILITY_HIDDEN FloatExprEvaluator
1396 : public StmtVisitor<FloatExprEvaluator, bool> {
1397 EvalInfo &Info;
1398 APFloat &Result;
1399public:
1400 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1401 : Info(info), Result(result) {}
1402
1403 bool VisitStmt(Stmt *S) {
1404 return false;
1405 }
1406
1407 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001408 bool VisitCallExpr(const CallExpr *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001409
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001410 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman24c01542008-08-22 00:06:13 +00001411 bool VisitBinaryOperator(const BinaryOperator *E);
1412 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman9a156e52008-11-12 09:44:48 +00001413 bool VisitCastExpr(CastExpr *E);
1414 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmanc2b50172009-02-22 11:46:18 +00001415
Eli Friedman449fe542009-03-23 04:56:01 +00001416 bool VisitChooseExpr(const ChooseExpr *E)
1417 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1418 bool VisitUnaryExtension(const UnaryOperator *E)
1419 { return Visit(E->getSubExpr()); }
1420
1421 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1422 // member of vector, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001423 // conditional ?:, comma
Eli Friedman24c01542008-08-22 00:06:13 +00001424};
1425} // end anonymous namespace
1426
1427static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1428 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1429}
1430
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001431bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregore711f702009-02-14 18:57:46 +00001432 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner37346e02008-10-06 05:53:16 +00001433 default: return false;
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001434 case Builtin::BI__builtin_huge_val:
1435 case Builtin::BI__builtin_huge_valf:
1436 case Builtin::BI__builtin_huge_vall:
1437 case Builtin::BI__builtin_inf:
1438 case Builtin::BI__builtin_inff:
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001439 case Builtin::BI__builtin_infl: {
1440 const llvm::fltSemantics &Sem =
1441 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner37346e02008-10-06 05:53:16 +00001442 Result = llvm::APFloat::getInf(Sem);
1443 return true;
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001444 }
Mike Stump11289f42009-09-09 15:08:12 +00001445
Chris Lattner0b7282e2008-10-06 06:31:58 +00001446 case Builtin::BI__builtin_nan:
1447 case Builtin::BI__builtin_nanf:
1448 case Builtin::BI__builtin_nanl:
Mike Stump2346cd22009-05-30 03:56:50 +00001449 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner0b7282e2008-10-06 06:31:58 +00001450 // can't constant fold it.
Mike Stump11289f42009-09-09 15:08:12 +00001451 if (const StringLiteral *S =
Chris Lattner0b7282e2008-10-06 06:31:58 +00001452 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump2346cd22009-05-30 03:56:50 +00001453 if (!S->isWide()) {
Daniel Dunbar1be9f882008-10-14 05:41:12 +00001454 const llvm::fltSemantics &Sem =
1455 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stumpb807c9c2009-05-30 14:43:18 +00001456 llvm::SmallString<16> s;
1457 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1458 s += '\0';
Mike Stump2346cd22009-05-30 03:56:50 +00001459 long l;
1460 char *endp;
Mike Stumpb807c9c2009-05-30 14:43:18 +00001461 l = strtol(&s[0], &endp, 0);
1462 if (endp != s.end()-1)
Mike Stump2346cd22009-05-30 03:56:50 +00001463 return false;
1464 unsigned type = (unsigned int)l;;
1465 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner0b7282e2008-10-06 06:31:58 +00001466 return true;
1467 }
1468 }
1469 return false;
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001470
1471 case Builtin::BI__builtin_fabs:
1472 case Builtin::BI__builtin_fabsf:
1473 case Builtin::BI__builtin_fabsl:
1474 if (!EvaluateFloat(E->getArg(0), Result, Info))
1475 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001476
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001477 if (Result.isNegative())
1478 Result.changeSign();
1479 return true;
1480
Mike Stump11289f42009-09-09 15:08:12 +00001481 case Builtin::BI__builtin_copysign:
1482 case Builtin::BI__builtin_copysignf:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001483 case Builtin::BI__builtin_copysignl: {
1484 APFloat RHS(0.);
1485 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1486 !EvaluateFloat(E->getArg(1), RHS, Info))
1487 return false;
1488 Result.copySign(RHS);
1489 return true;
1490 }
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001491 }
1492}
1493
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001494bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes0e33c682008-11-19 17:44:31 +00001495 if (E->getOpcode() == UnaryOperator::Deref)
1496 return false;
1497
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001498 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1499 return false;
1500
1501 switch (E->getOpcode()) {
1502 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001503 case UnaryOperator::Plus:
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001504 return true;
1505 case UnaryOperator::Minus:
1506 Result.changeSign();
1507 return true;
1508 }
1509}
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001510
Eli Friedman24c01542008-08-22 00:06:13 +00001511bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1512 // FIXME: Diagnostics? I really don't understand how the warnings
1513 // and errors are supposed to work.
Daniel Dunbarc3d79cf2008-10-16 03:51:50 +00001514 APFloat RHS(0.0);
Eli Friedman24c01542008-08-22 00:06:13 +00001515 if (!EvaluateFloat(E->getLHS(), Result, Info))
1516 return false;
1517 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1518 return false;
1519
1520 switch (E->getOpcode()) {
1521 default: return false;
1522 case BinaryOperator::Mul:
1523 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1524 return true;
1525 case BinaryOperator::Add:
1526 Result.add(RHS, APFloat::rmNearestTiesToEven);
1527 return true;
1528 case BinaryOperator::Sub:
1529 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1530 return true;
1531 case BinaryOperator::Div:
1532 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1533 return true;
Eli Friedman24c01542008-08-22 00:06:13 +00001534 }
1535}
1536
1537bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1538 Result = E->getValue();
1539 return true;
1540}
1541
Eli Friedman9a156e52008-11-12 09:44:48 +00001542bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1543 Expr* SubExpr = E->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001544
Eli Friedman9a156e52008-11-12 09:44:48 +00001545 if (SubExpr->getType()->isIntegralType()) {
1546 APSInt IntResult;
Daniel Dunbare3c92bc2009-02-19 18:37:50 +00001547 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman9a156e52008-11-12 09:44:48 +00001548 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001549 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001550 IntResult, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001551 return true;
1552 }
1553 if (SubExpr->getType()->isRealFloatingType()) {
1554 if (!Visit(SubExpr))
1555 return false;
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001556 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1557 Result, Info.Ctx);
Eli Friedman9a156e52008-11-12 09:44:48 +00001558 return true;
1559 }
Eli Friedmanc2b50172009-02-22 11:46:18 +00001560 // FIXME: Handle complex types
Eli Friedman9a156e52008-11-12 09:44:48 +00001561
1562 return false;
1563}
1564
1565bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1566 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1567 return true;
1568}
1569
Eli Friedman24c01542008-08-22 00:06:13 +00001570//===----------------------------------------------------------------------===//
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001571// Complex Evaluation (for float and integer)
Anders Carlsson537969c2008-11-16 20:27:53 +00001572//===----------------------------------------------------------------------===//
1573
1574namespace {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001575class VISIBILITY_HIDDEN ComplexExprEvaluator
1576 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson537969c2008-11-16 20:27:53 +00001577 EvalInfo &Info;
Mike Stump11289f42009-09-09 15:08:12 +00001578
Anders Carlsson537969c2008-11-16 20:27:53 +00001579public:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001580 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump11289f42009-09-09 15:08:12 +00001581
Anders Carlsson537969c2008-11-16 20:27:53 +00001582 //===--------------------------------------------------------------------===//
1583 // Visitor Methods
1584 //===--------------------------------------------------------------------===//
1585
1586 APValue VisitStmt(Stmt *S) {
Anders Carlsson537969c2008-11-16 20:27:53 +00001587 return APValue();
1588 }
Mike Stump11289f42009-09-09 15:08:12 +00001589
Anders Carlsson537969c2008-11-16 20:27:53 +00001590 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1591
1592 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001593 Expr* SubExpr = E->getSubExpr();
1594
1595 if (SubExpr->getType()->isRealFloatingType()) {
1596 APFloat Result(0.0);
1597
1598 if (!EvaluateFloat(SubExpr, Result, Info))
1599 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001600
1601 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001602 Result);
1603 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001604 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001605 "Unexpected imaginary literal.");
1606
1607 llvm::APSInt Result;
1608 if (!EvaluateInteger(SubExpr, Result, Info))
1609 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001610
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001611 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1612 Zero = 0;
1613 return APValue(Zero, Result);
1614 }
Anders Carlsson537969c2008-11-16 20:27:53 +00001615 }
1616
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001617 APValue VisitCastExpr(CastExpr *E) {
1618 Expr* SubExpr = E->getSubExpr();
John McCall9dd450b2009-09-21 23:43:11 +00001619 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001620 QualType SubType = SubExpr->getType();
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001621
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001622 if (SubType->isRealFloatingType()) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001623 APFloat Result(0.0);
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001624
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001625 if (!EvaluateFloat(SubExpr, Result, Info))
1626 return APValue();
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001627
1628 if (EltType->isRealFloatingType()) {
1629 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001630 return APValue(Result,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001631 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1632 } else {
1633 llvm::APSInt IResult;
1634 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1635 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1636 Zero = 0;
1637 return APValue(IResult, Zero);
1638 }
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001639 } else if (SubType->isIntegerType()) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001640 APSInt Result;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001641
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001642 if (!EvaluateInteger(SubExpr, Result, Info))
1643 return APValue();
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001644
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001645 if (EltType->isRealFloatingType()) {
1646 APFloat FResult =
1647 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001648 return APValue(FResult,
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001649 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1650 } else {
1651 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1652 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1653 Zero = 0;
1654 return APValue(Result, Zero);
1655 }
John McCall9dd450b2009-09-21 23:43:11 +00001656 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001657 APValue Src;
Eli Friedmand3a5a9d2009-04-22 19:23:09 +00001658
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001659 if (!EvaluateComplex(SubExpr, Src, Info))
1660 return APValue();
1661
1662 QualType SrcType = CT->getElementType();
1663
1664 if (Src.isComplexFloat()) {
1665 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001666 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001667 Src.getComplexFloatReal(),
1668 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001669 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001670 Src.getComplexFloatImag(),
1671 Info.Ctx));
1672 } else {
1673 return APValue(HandleFloatToIntCast(EltType, SrcType,
1674 Src.getComplexFloatReal(),
1675 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001676 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001677 Src.getComplexFloatImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001678 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001679 }
1680 } else {
1681 assert(Src.isComplexInt() && "Invalid evaluate result.");
1682 if (EltType->isRealFloatingType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001683 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001684 Src.getComplexIntReal(),
1685 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001686 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001687 Src.getComplexIntImag(),
1688 Info.Ctx));
1689 } else {
1690 return APValue(HandleIntToIntCast(EltType, SrcType,
1691 Src.getComplexIntReal(),
1692 Info.Ctx),
Mike Stump11289f42009-09-09 15:08:12 +00001693 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001694 Src.getComplexIntImag(),
Mike Stump11289f42009-09-09 15:08:12 +00001695 Info.Ctx));
Daniel Dunbarb6f953e2009-01-29 06:16:07 +00001696 }
1697 }
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001698 }
1699
1700 // FIXME: Handle more casts.
1701 return APValue();
1702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001704 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedman449fe542009-03-23 04:56:01 +00001705 APValue VisitChooseExpr(const ChooseExpr *E)
1706 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1707 APValue VisitUnaryExtension(const UnaryOperator *E)
1708 { return Visit(E->getSubExpr()); }
1709 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedmanc2b50172009-02-22 11:46:18 +00001710 // conditional ?:, comma
Anders Carlsson537969c2008-11-16 20:27:53 +00001711};
1712} // end anonymous namespace
1713
Mike Stump11289f42009-09-09 15:08:12 +00001714static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001715 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1716 assert((!Result.isComplexFloat() ||
Mike Stump11289f42009-09-09 15:08:12 +00001717 (&Result.getComplexFloatReal().getSemantics() ==
1718 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001719 "Invalid complex evaluation.");
1720 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson537969c2008-11-16 20:27:53 +00001721}
1722
Mike Stump11289f42009-09-09 15:08:12 +00001723APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001724 APValue Result, RHS;
Mike Stump11289f42009-09-09 15:08:12 +00001725
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001726 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001727 return APValue();
Mike Stump11289f42009-09-09 15:08:12 +00001728
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001729 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001730 return APValue();
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001731
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001732 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1733 "Invalid operands to binary operator.");
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001734 switch (E->getOpcode()) {
1735 default: return APValue();
1736 case BinaryOperator::Add:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001737 if (Result.isComplexFloat()) {
1738 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1739 APFloat::rmNearestTiesToEven);
1740 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1741 APFloat::rmNearestTiesToEven);
1742 } else {
1743 Result.getComplexIntReal() += RHS.getComplexIntReal();
1744 Result.getComplexIntImag() += RHS.getComplexIntImag();
1745 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001746 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001747 case BinaryOperator::Sub:
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001748 if (Result.isComplexFloat()) {
1749 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1750 APFloat::rmNearestTiesToEven);
1751 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1752 APFloat::rmNearestTiesToEven);
1753 } else {
1754 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1755 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1756 }
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001757 break;
1758 case BinaryOperator::Mul:
1759 if (Result.isComplexFloat()) {
1760 APValue LHS = Result;
1761 APFloat &LHS_r = LHS.getComplexFloatReal();
1762 APFloat &LHS_i = LHS.getComplexFloatImag();
1763 APFloat &RHS_r = RHS.getComplexFloatReal();
1764 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump11289f42009-09-09 15:08:12 +00001765
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001766 APFloat Tmp = LHS_r;
1767 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1768 Result.getComplexFloatReal() = Tmp;
1769 Tmp = LHS_i;
1770 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1771 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1772
1773 Tmp = LHS_r;
1774 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1775 Result.getComplexFloatImag() = Tmp;
1776 Tmp = LHS_i;
1777 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1778 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1779 } else {
1780 APValue LHS = Result;
Mike Stump11289f42009-09-09 15:08:12 +00001781 Result.getComplexIntReal() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001782 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1783 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump11289f42009-09-09 15:08:12 +00001784 Result.getComplexIntImag() =
Daniel Dunbar0aa26062009-01-29 01:32:56 +00001785 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1786 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1787 }
1788 break;
Anders Carlsson9ddf7be2008-11-16 21:51:21 +00001789 }
1790
1791 return Result;
1792}
1793
Anders Carlsson537969c2008-11-16 20:27:53 +00001794//===----------------------------------------------------------------------===//
Chris Lattner67d7b922008-11-16 21:24:15 +00001795// Top level Expr::Evaluate method.
Chris Lattner05706e882008-07-11 18:11:29 +00001796//===----------------------------------------------------------------------===//
1797
Chris Lattner67d7b922008-11-16 21:24:15 +00001798/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner4deaa4e2008-10-06 05:28:25 +00001799/// any crazy technique (that has nothing to do with language standards) that
1800/// we want to. If this function returns true, it returns the folded constant
1801/// in Result.
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001802bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1803 EvalInfo Info(Ctx, Result);
Anders Carlssonbd1df8e2008-11-30 16:38:33 +00001804
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001805 if (getType()->isVectorType()) {
1806 if (!EvaluateVector(this, Result.Val, Info))
1807 return false;
1808 } else if (getType()->isIntegerType()) {
Daniel Dunbarca097ad2009-02-19 20:17:33 +00001809 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001810 return false;
Daniel Dunbar76ba41c2009-02-26 20:52:22 +00001811 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001812 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001813 return false;
Eli Friedman24c01542008-08-22 00:06:13 +00001814 } else if (getType()->isRealFloatingType()) {
1815 llvm::APFloat f(0.0);
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001816 if (!EvaluateFloat(this, f, Info))
1817 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001818
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001819 Result.Val = APValue(f);
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001820 } else if (getType()->isAnyComplexType()) {
1821 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001822 return false;
Daniel Dunbarf50e60b2009-01-28 22:24:07 +00001823 } else
Anders Carlsson7c282e42008-11-22 22:56:32 +00001824 return false;
Anders Carlsson475f4bc2008-11-22 21:50:49 +00001825
Anders Carlsson7b6f0af2008-11-30 16:58:53 +00001826 return true;
1827}
1828
Anders Carlsson43168122009-04-10 04:54:13 +00001829bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1830 EvalInfo Info(Ctx, Result);
1831
1832 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1833}
1834
Eli Friedman7d45c482009-09-13 10:17:44 +00001835bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1836 EvalInfo Info(Ctx, Result, true);
1837
1838 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1839}
1840
Chris Lattner67d7b922008-11-16 21:24:15 +00001841/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattnercb136912008-10-06 06:49:02 +00001842/// folded, but discard the result.
1843bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson5b3638b2008-12-01 06:44:05 +00001844 EvalResult Result;
1845 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattnercb136912008-10-06 06:49:02 +00001846}
Anders Carlsson59689ed2008-11-22 21:04:56 +00001847
1848APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001849 EvalResult EvalResult;
1850 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbar435bbe02009-01-15 18:32:35 +00001851 Result = Result;
Anders Carlsson59689ed2008-11-22 21:04:56 +00001852 assert(Result && "Could not evaluate expression");
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001853 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson59689ed2008-11-22 21:04:56 +00001854
Anders Carlsson6736d1a22008-12-19 20:58:05 +00001855 return EvalResult.Val.getInt();
Anders Carlsson59689ed2008-11-22 21:04:56 +00001856}