blob: 9eeaa868a59e297a27f86e4610e360d996007898 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr constant evaluator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "clang/AST/ASTContext.h"
Eli Friedman4efaa272008-11-12 09:44:48 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner54176fd2008-07-12 00:14:42 +000018#include "clang/Basic/Diagnostic.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000022using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000023using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000024
Chris Lattner87eae5e2008-07-11 22:52:41 +000025/// EvalInfo - This is a private struct used by the evaluator to capture
26/// information about a subexpression as it is folded. It retains information
27/// about the AST context, but also maintains information about the folded
28/// expression.
29///
30/// If an expression could be evaluated, it is still possible it is not a C
31/// "integer constant expression" or constant expression. If not, this struct
32/// captures information about how and why not.
33///
34/// One bit of information passed *into* the request for constant folding
35/// indicates whether the subexpression is "evaluated" or not according to C
36/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
37/// evaluate the expression regardless of what the RHS is, but C only allows
38/// certain things in certain situations.
39struct EvalInfo {
40 ASTContext &Ctx;
41
Anders Carlsson54da0492008-11-30 16:38:33 +000042 /// EvalResult - Contains information about the evaluation.
43 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000044
45 /// ShortCircuit - will be greater than zero if the current subexpression has
46 /// will not be evaluated because it's short-circuited (according to C rules).
47 unsigned ShortCircuit;
Chris Lattner87eae5e2008-07-11 22:52:41 +000048
Anders Carlsson54da0492008-11-30 16:38:33 +000049 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000050 EvalResult(evalresult), ShortCircuit(0) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000051};
52
53
Eli Friedman4efaa272008-11-12 09:44:48 +000054static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000055static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
56static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000057static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Anders Carlsson9ad16ae2008-11-16 20:27:53 +000058static bool EvaluateComplexFloat(const Expr *E, APValue &Result,
59 EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000060
61//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000062// Misc utilities
63//===----------------------------------------------------------------------===//
64
65static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
66 if (E->getType()->isIntegralType()) {
67 APSInt IntResult;
68 if (!EvaluateInteger(E, IntResult, Info))
69 return false;
70 Result = IntResult != 0;
71 return true;
72 } else if (E->getType()->isRealFloatingType()) {
73 APFloat FloatResult(0.0);
74 if (!EvaluateFloat(E, FloatResult, Info))
75 return false;
76 Result = !FloatResult.isZero();
77 return true;
78 } else if (E->getType()->isPointerType()) {
79 APValue PointerResult;
80 if (!EvaluatePointer(E, PointerResult, Info))
81 return false;
82 // FIXME: Is this accurate for all kinds of bases? If not, what would
83 // the check look like?
84 Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
85 return true;
86 }
87
88 return false;
89}
90
91//===----------------------------------------------------------------------===//
92// LValue Evaluation
93//===----------------------------------------------------------------------===//
94namespace {
95class VISIBILITY_HIDDEN LValueExprEvaluator
96 : public StmtVisitor<LValueExprEvaluator, APValue> {
97 EvalInfo &Info;
98public:
99
100 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
101
102 APValue VisitStmt(Stmt *S) {
Daniel Dunbar8a7b7c62008-11-12 21:52:46 +0000103#if 0
Eli Friedman4efaa272008-11-12 09:44:48 +0000104 // FIXME: Remove this when we support more expressions.
105 printf("Unhandled pointer statement\n");
106 S->dump();
Daniel Dunbar8a7b7c62008-11-12 21:52:46 +0000107#endif
Eli Friedman4efaa272008-11-12 09:44:48 +0000108 return APValue();
109 }
110
111 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000112 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000113 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
114 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
115 APValue VisitMemberExpr(MemberExpr *E);
116 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000117 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000118};
119} // end anonymous namespace
120
121static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
122 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
123 return Result.isLValue();
124}
125
Anders Carlsson35873c42008-11-24 04:41:22 +0000126APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
127{
128 if (!E->hasGlobalStorage())
129 return APValue();
130
131 return APValue(E, 0);
132}
133
Eli Friedman4efaa272008-11-12 09:44:48 +0000134APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
135 if (E->isFileScope())
136 return APValue(E, 0);
137 return APValue();
138}
139
140APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
141 APValue result;
142 QualType Ty;
143 if (E->isArrow()) {
144 if (!EvaluatePointer(E->getBase(), result, Info))
145 return APValue();
146 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
147 } else {
148 result = Visit(E->getBase());
149 if (result.isUninit())
150 return APValue();
151 Ty = E->getBase()->getType();
152 }
153
154 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
155 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
156 FieldDecl *FD = E->getMemberDecl();
157
158 // FIXME: This is linear time.
159 unsigned i = 0, e = 0;
160 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
161 if (RD->getMember(i) == FD)
162 break;
163 }
164
165 result.setLValue(result.getLValueBase(),
166 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
167
168 return result;
169}
170
Anders Carlsson3068d112008-11-16 19:01:22 +0000171APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
172{
173 APValue Result;
174
175 if (!EvaluatePointer(E->getBase(), Result, Info))
176 return APValue();
177
178 APSInt Index;
179 if (!EvaluateInteger(E->getIdx(), Index, Info))
180 return APValue();
181
182 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
183
184 uint64_t Offset = Index.getSExtValue() * ElementSize;
185 Result.setLValue(Result.getLValueBase(),
186 Result.getLValueOffset() + Offset);
187 return Result;
188}
Eli Friedman4efaa272008-11-12 09:44:48 +0000189
190//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000191// Pointer Evaluation
192//===----------------------------------------------------------------------===//
193
Anders Carlssonc754aa62008-07-08 05:13:58 +0000194namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000195class VISIBILITY_HIDDEN PointerExprEvaluator
196 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000197 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000198public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000199
Chris Lattner87eae5e2008-07-11 22:52:41 +0000200 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000201
Anders Carlsson2bad1682008-07-08 14:30:00 +0000202 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000203 return APValue();
204 }
205
206 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
207
Anders Carlsson650c92f2008-07-08 15:34:11 +0000208 APValue VisitBinaryOperator(const BinaryOperator *E);
209 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000210 APValue VisitUnaryOperator(const UnaryOperator *E);
211 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
212 { return APValue(E, 0); }
213 APValue VisitConditionalOperator(ConditionalOperator *E);
Anders Carlsson650c92f2008-07-08 15:34:11 +0000214};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000215} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000216
Chris Lattner87eae5e2008-07-11 22:52:41 +0000217static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000218 if (!E->getType()->isPointerType())
219 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000220 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000221 return Result.isLValue();
222}
223
224APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
225 if (E->getOpcode() != BinaryOperator::Add &&
226 E->getOpcode() != BinaryOperator::Sub)
227 return APValue();
228
229 const Expr *PExp = E->getLHS();
230 const Expr *IExp = E->getRHS();
231 if (IExp->getType()->isPointerType())
232 std::swap(PExp, IExp);
233
234 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000235 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000236 return APValue();
237
238 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000239 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000240 return APValue();
241
Eli Friedman4efaa272008-11-12 09:44:48 +0000242 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
243 uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
244
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000245 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000246
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000247 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000248 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000249 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000250 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
251
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000252 return APValue(ResultLValue.getLValueBase(), Offset);
253}
Eli Friedman4efaa272008-11-12 09:44:48 +0000254
255APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
256 if (E->getOpcode() == UnaryOperator::Extension) {
257 // FIXME: Deal with warnings?
258 return Visit(E->getSubExpr());
259 }
260
261 if (E->getOpcode() == UnaryOperator::AddrOf) {
262 APValue result;
263 if (EvaluateLValue(E->getSubExpr(), result, Info))
264 return result;
265 }
266
267 return APValue();
268}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000269
270
Chris Lattnerb542afe2008-07-11 19:10:17 +0000271APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000272 const Expr* SubExpr = E->getSubExpr();
273
274 // Check for pointer->pointer cast
275 if (SubExpr->getType()->isPointerType()) {
276 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000277 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000278 return Result;
279 return APValue();
280 }
281
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000282 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000283 llvm::APSInt Result(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000284 if (EvaluateInteger(SubExpr, Result, Info)) {
285 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000286 return APValue(0, Result.getZExtValue());
287 }
288 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000289
290 if (SubExpr->getType()->isFunctionType() ||
291 SubExpr->getType()->isArrayType()) {
292 APValue Result;
293 if (EvaluateLValue(SubExpr, Result, Info))
294 return Result;
295 return APValue();
296 }
297
298 //assert(0 && "Unhandled cast");
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000299 return APValue();
300}
301
Eli Friedman4efaa272008-11-12 09:44:48 +0000302APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
303 bool BoolResult;
304 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
305 return APValue();
306
307 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
308
309 APValue Result;
310 if (EvaluatePointer(EvalExpr, Result, Info))
311 return Result;
312 return APValue();
313}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000314
315//===----------------------------------------------------------------------===//
316// Integer Evaluation
317//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000318
319namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000320class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000321 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000322 EvalInfo &Info;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000323 APSInt &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000324public:
Chris Lattner87eae5e2008-07-11 22:52:41 +0000325 IntExprEvaluator(EvalInfo &info, APSInt &result)
326 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000327
Chris Lattner7a767782008-07-11 19:24:49 +0000328 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner54176fd2008-07-12 00:14:42 +0000329 return (unsigned)Info.Ctx.getIntWidth(T);
330 }
331
Anders Carlsson82206e22008-11-30 18:14:57 +0000332 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlsson54da0492008-11-30 16:38:33 +0000333 Info.EvalResult.DiagLoc = L;
334 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000335 Info.EvalResult.DiagExpr = E;
Chris Lattner54176fd2008-07-12 00:14:42 +0000336 return true; // still a constant.
337 }
338
Anders Carlsson82206e22008-11-30 18:14:57 +0000339 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000340 // If this is in an unevaluated portion of the subexpression, ignore the
341 // error.
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000342 if (Info.ShortCircuit) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000343 // If error is ignored because the value isn't evaluated, get the real
344 // type at least to prevent errors downstream.
Anders Carlsson82206e22008-11-30 18:14:57 +0000345 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
346 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner54176fd2008-07-12 00:14:42 +0000347 return true;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000348 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000349
Chris Lattner32fea9d2008-11-12 07:43:42 +0000350 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000351 if (Info.EvalResult.Diag == 0) {
352 Info.EvalResult.DiagLoc = L;
353 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000354 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000355 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000356 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000357 }
358
Anders Carlssonc754aa62008-07-08 05:13:58 +0000359 //===--------------------------------------------------------------------===//
360 // Visitor Methods
361 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000362
363 bool VisitStmt(Stmt *) {
364 assert(0 && "This should be called on integers, stmts are not integers");
365 return false;
366 }
Chris Lattner7a767782008-07-11 19:24:49 +0000367
Chris Lattner32fea9d2008-11-12 07:43:42 +0000368 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000369 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000370 }
371
Chris Lattnerb542afe2008-07-11 19:10:17 +0000372 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000373
Chris Lattner4c4867e2008-07-12 00:38:25 +0000374 bool VisitIntegerLiteral(const IntegerLiteral *E) {
375 Result = E->getValue();
376 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
377 return true;
378 }
379 bool VisitCharacterLiteral(const CharacterLiteral *E) {
380 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
381 Result = E->getValue();
382 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
383 return true;
384 }
385 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
386 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarac620de2008-10-24 08:07:57 +0000387 // Per gcc docs "this built-in function ignores top level
388 // qualifiers". We need to use the canonical version to properly
389 // be able to strip CRV qualifiers from the type.
390 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
391 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
392 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
393 T1.getUnqualifiedType());
Chris Lattner4c4867e2008-07-12 00:38:25 +0000394 return true;
395 }
396 bool VisitDeclRefExpr(const DeclRefExpr *E);
397 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000398 bool VisitBinaryOperator(const BinaryOperator *E);
399 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000400 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000401
Chris Lattner732b2232008-07-12 01:15:53 +0000402 bool VisitCastExpr(CastExpr* E) {
Anders Carlsson82206e22008-11-30 18:14:57 +0000403 return HandleCast(E);
Anders Carlsson650c92f2008-07-08 15:34:11 +0000404 }
Sebastian Redl05189992008-11-11 17:56:53 +0000405 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
406
Anders Carlsson3068d112008-11-16 19:01:22 +0000407 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000408 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson3068d112008-11-16 19:01:22 +0000409 Result = E->getValue();
410 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
411 return true;
412 }
413
414 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
415 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
416 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
417 return true;
418 }
419
Chris Lattnerfcee0012008-07-11 21:24:13 +0000420private:
Anders Carlsson82206e22008-11-30 18:14:57 +0000421 bool HandleCast(CastExpr* E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000422};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000423} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000424
Chris Lattner87eae5e2008-07-11 22:52:41 +0000425static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
426 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson650c92f2008-07-08 15:34:11 +0000427}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000428
Chris Lattner4c4867e2008-07-12 00:38:25 +0000429bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
430 // Enums are integer constant exprs.
431 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
432 Result = D->getInitVal();
433 return true;
434 }
435
436 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000437 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000438}
439
Chris Lattnera4d55d82008-10-06 06:40:35 +0000440/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
441/// as GCC.
442static int EvaluateBuiltinClassifyType(const CallExpr *E) {
443 // The following enum mimics the values returned by GCC.
444 enum gcc_type_class {
445 no_type_class = -1,
446 void_type_class, integer_type_class, char_type_class,
447 enumeral_type_class, boolean_type_class,
448 pointer_type_class, reference_type_class, offset_type_class,
449 real_type_class, complex_type_class,
450 function_type_class, method_type_class,
451 record_type_class, union_type_class,
452 array_type_class, string_type_class,
453 lang_type_class
454 };
455
456 // If no argument was supplied, default to "no_type_class". This isn't
457 // ideal, however it is what gcc does.
458 if (E->getNumArgs() == 0)
459 return no_type_class;
460
461 QualType ArgTy = E->getArg(0)->getType();
462 if (ArgTy->isVoidType())
463 return void_type_class;
464 else if (ArgTy->isEnumeralType())
465 return enumeral_type_class;
466 else if (ArgTy->isBooleanType())
467 return boolean_type_class;
468 else if (ArgTy->isCharType())
469 return string_type_class; // gcc doesn't appear to use char_type_class
470 else if (ArgTy->isIntegerType())
471 return integer_type_class;
472 else if (ArgTy->isPointerType())
473 return pointer_type_class;
474 else if (ArgTy->isReferenceType())
475 return reference_type_class;
476 else if (ArgTy->isRealType())
477 return real_type_class;
478 else if (ArgTy->isComplexType())
479 return complex_type_class;
480 else if (ArgTy->isFunctionType())
481 return function_type_class;
482 else if (ArgTy->isStructureType())
483 return record_type_class;
484 else if (ArgTy->isUnionType())
485 return union_type_class;
486 else if (ArgTy->isArrayType())
487 return array_type_class;
488 else if (ArgTy->isUnionType())
489 return union_type_class;
490 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
491 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
492 return -1;
493}
494
Chris Lattner4c4867e2008-07-12 00:38:25 +0000495bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
496 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner4c4867e2008-07-12 00:38:25 +0000497
Chris Lattner019f4e82008-10-06 05:28:25 +0000498 switch (E->isBuiltinCall()) {
499 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000500 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000501 case Builtin::BI__builtin_classify_type:
Chris Lattnera4d55d82008-10-06 06:40:35 +0000502 Result.setIsSigned(true);
503 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000504 return true;
505
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000506 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000507 // __builtin_constant_p always has one operand: it returns true if that
508 // operand can be folded, false otherwise.
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000509 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner019f4e82008-10-06 05:28:25 +0000510 return true;
511 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000512}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000513
Chris Lattnerb542afe2008-07-11 19:10:17 +0000514bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000515 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000516 if (!Visit(E->getRHS()))
517 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000518
519 if (!Info.ShortCircuit) {
520 // If we can't evaluate the LHS, it must be because it has
521 // side effects.
522 if (!E->getLHS()->isEvaluatable(Info.Ctx))
523 Info.EvalResult.HasSideEffects = true;
524
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000525 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000526 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000527
Anders Carlsson027f62e2008-12-01 02:07:06 +0000528 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000529 }
530
531 if (E->isLogicalOp()) {
532 // These need to be handled specially because the operands aren't
533 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000534 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000535
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000536 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000537 // We were able to evaluate the LHS, see if we can get away with not
538 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000539 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
540 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000541 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
542 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000543 Result = lhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000544
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000545 Info.ShortCircuit++;
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000546 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000547 Info.ShortCircuit--;
548
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000549 if (rhsEvaluated)
550 return true;
551
552 // FIXME: Return an extension warning saying that the RHS could not be
553 // evaluated.
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000554 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000555 }
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000556
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000557 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000558 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
559 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
560 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000561 Result = lhsResult || rhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000562 else
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000563 Result = lhsResult && rhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000564 return true;
565 }
566 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000567 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000568 // We can't evaluate the LHS; however, sometimes the result
569 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000570 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
571 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000572 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
573 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000574 Result = rhsResult;
575
576 // Since we werent able to evaluate the left hand side, it
577 // must have had side effects.
578 Info.EvalResult.HasSideEffects = true;
579
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000580 return true;
581 }
582 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000583 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000584
Eli Friedmana6afa762008-11-13 06:09:17 +0000585 return false;
586 }
587
Anders Carlsson286f85e2008-11-16 07:17:21 +0000588 QualType LHSTy = E->getLHS()->getType();
589 QualType RHSTy = E->getRHS()->getType();
590
591 if (LHSTy->isRealFloatingType() &&
592 RHSTy->isRealFloatingType()) {
593 APFloat RHS(0.0), LHS(0.0);
594
595 if (!EvaluateFloat(E->getRHS(), RHS, Info))
596 return false;
597
598 if (!EvaluateFloat(E->getLHS(), LHS, Info))
599 return false;
600
601 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000602
603 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
604
Anders Carlsson286f85e2008-11-16 07:17:21 +0000605 switch (E->getOpcode()) {
606 default:
607 assert(0 && "Invalid binary operator!");
608 case BinaryOperator::LT:
609 Result = CR == APFloat::cmpLessThan;
610 break;
611 case BinaryOperator::GT:
612 Result = CR == APFloat::cmpGreaterThan;
613 break;
614 case BinaryOperator::LE:
615 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
616 break;
617 case BinaryOperator::GE:
618 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
619 break;
620 case BinaryOperator::EQ:
621 Result = CR == APFloat::cmpEqual;
622 break;
623 case BinaryOperator::NE:
624 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
625 break;
626 }
627
Anders Carlsson286f85e2008-11-16 07:17:21 +0000628 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
629 return true;
630 }
631
Anders Carlsson3068d112008-11-16 19:01:22 +0000632 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000633 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000634 APValue LHSValue;
635 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
636 return false;
637
638 APValue RHSValue;
639 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
640 return false;
641
642 // FIXME: Is this correct? What if only one of the operands has a base?
643 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
644 return false;
645
646 const QualType Type = E->getLHS()->getType();
647 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
648
649 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
650 D /= Info.Ctx.getTypeSize(ElementType) / 8;
651
Anders Carlsson3068d112008-11-16 19:01:22 +0000652 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson529569e2008-11-16 22:46:56 +0000653 Result = D;
Anders Carlsson3068d112008-11-16 19:01:22 +0000654 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
655
656 return true;
657 }
658 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000659 if (!LHSTy->isIntegralType() ||
660 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000661 // We can't continue from here for non-integral types, and they
662 // could potentially confuse the following operations.
663 // FIXME: Deal with EQ and friends.
664 return false;
665 }
666
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000667 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000668 llvm::APSInt RHS(32);
Chris Lattnerc8cc9cc2008-11-12 07:04:29 +0000669 if (!Visit(E->getLHS())) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000670 return false; // error in subexpression.
Chris Lattnerc8cc9cc2008-11-12 07:04:29 +0000671 }
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000672
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000673
674 // FIXME Maybe we want to succeed even where we can't evaluate the
675 // right side of LAnd/LOr?
676 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner54176fd2008-07-12 00:14:42 +0000677 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000678 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000679
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000680 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000681 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000682 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner54176fd2008-07-12 00:14:42 +0000683 case BinaryOperator::Mul: Result *= RHS; return true;
684 case BinaryOperator::Add: Result += RHS; return true;
685 case BinaryOperator::Sub: Result -= RHS; return true;
686 case BinaryOperator::And: Result &= RHS; return true;
687 case BinaryOperator::Xor: Result ^= RHS; return true;
688 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner75a48812008-07-11 22:15:16 +0000689 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000690 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000691 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000692 Result /= RHS;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000693 break;
Chris Lattner75a48812008-07-11 22:15:16 +0000694 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000695 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000696 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000697 Result %= RHS;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000698 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000699 case BinaryOperator::Shl:
Chris Lattner54176fd2008-07-12 00:14:42 +0000700 // FIXME: Warn about out of range shift amounts!
Chris Lattnerb542afe2008-07-11 19:10:17 +0000701 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000702 break;
703 case BinaryOperator::Shr:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000704 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000705 break;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000706
Chris Lattnerac7cb602008-07-11 19:29:32 +0000707 case BinaryOperator::LT:
708 Result = Result < RHS;
709 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
710 break;
711 case BinaryOperator::GT:
712 Result = Result > RHS;
713 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
714 break;
715 case BinaryOperator::LE:
716 Result = Result <= RHS;
717 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
718 break;
719 case BinaryOperator::GE:
720 Result = Result >= RHS;
721 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
722 break;
723 case BinaryOperator::EQ:
724 Result = Result == RHS;
725 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
726 break;
727 case BinaryOperator::NE:
728 Result = Result != RHS;
729 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
730 break;
Chris Lattner54176fd2008-07-12 00:14:42 +0000731 case BinaryOperator::LAnd:
732 Result = Result != 0 && RHS != 0;
733 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
734 break;
735 case BinaryOperator::LOr:
736 Result = Result != 0 || RHS != 0;
737 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
738 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +0000739 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000740
741 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000742 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000743}
744
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000745bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +0000746 bool Cond;
747 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000748 return false;
749
Nuno Lopesa25bd552008-11-16 22:06:39 +0000750 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000751}
752
Sebastian Redl05189992008-11-11 17:56:53 +0000753/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
754/// expression's type.
755bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
756 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000757 // Return the result in the right width.
758 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
759 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
760
Sebastian Redl05189992008-11-11 17:56:53 +0000761 QualType SrcTy = E->getTypeOfArgument();
762
Chris Lattnerfcee0012008-07-11 21:24:13 +0000763 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman4efaa272008-11-12 09:44:48 +0000764 if (SrcTy->isVoidType()) {
Chris Lattnerfcee0012008-07-11 21:24:13 +0000765 Result = 1;
Eli Friedman4efaa272008-11-12 09:44:48 +0000766 return true;
767 }
Chris Lattnerfcee0012008-07-11 21:24:13 +0000768
769 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman4efaa272008-11-12 09:44:48 +0000770 // FIXME: But alignof(vla) is!
Chris Lattnerfcee0012008-07-11 21:24:13 +0000771 if (!SrcTy->isConstantSizeType()) {
772 // FIXME: Should we attempt to evaluate this?
773 return false;
774 }
Sebastian Redl05189992008-11-11 17:56:53 +0000775
776 bool isSizeOf = E->isSizeOf();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000777
778 // GCC extension: sizeof(function) = 1.
779 if (SrcTy->isFunctionType()) {
780 // FIXME: AlignOf shouldn't be unconditionally 4!
781 Result = isSizeOf ? 1 : 4;
782 return true;
783 }
784
785 // Get information about the size or align.
Chris Lattner87eae5e2008-07-11 22:52:41 +0000786 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000787 if (isSizeOf)
Eli Friedman4efaa272008-11-12 09:44:48 +0000788 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattnerfcee0012008-07-11 21:24:13 +0000789 else
Chris Lattner87eae5e2008-07-11 22:52:41 +0000790 Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize;
Chris Lattnerfcee0012008-07-11 21:24:13 +0000791 return true;
792}
793
Chris Lattnerb542afe2008-07-11 19:10:17 +0000794bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000795 // Special case unary operators that do not need their subexpression
796 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner75a48812008-07-11 22:15:16 +0000797 if (E->isOffsetOfOp()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000798 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner87eae5e2008-07-11 22:52:41 +0000799 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner75a48812008-07-11 22:15:16 +0000800 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
801 return true;
802 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000803
804 if (E->getOpcode() == UnaryOperator::LNot) {
805 // LNot's operand isn't necessarily an integer, so we handle it specially.
806 bool bres;
807 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
808 return false;
809 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
810 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
811 Result = !bres;
812 return true;
813 }
814
Chris Lattner87eae5e2008-07-11 22:52:41 +0000815 // Get the operand value into 'Result'.
816 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +0000817 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000818
Chris Lattner75a48812008-07-11 22:15:16 +0000819 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000820 default:
Chris Lattner75a48812008-07-11 22:15:16 +0000821 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
822 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000823 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000824 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000825 // FIXME: Should extension allow i-c-e extension expressions in its scope?
826 // If so, we could clear the diagnostic ID.
Chris Lattner75a48812008-07-11 22:15:16 +0000827 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000828 // The result is always just the subexpr.
Chris Lattner75a48812008-07-11 22:15:16 +0000829 break;
830 case UnaryOperator::Minus:
831 Result = -Result;
832 break;
833 case UnaryOperator::Not:
834 Result = ~Result;
835 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000836 }
837
838 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000839 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000840}
841
Chris Lattner732b2232008-07-12 01:15:53 +0000842/// HandleCast - This is used to evaluate implicit or explicit casts where the
843/// result type is integer.
Anders Carlsson82206e22008-11-30 18:14:57 +0000844bool IntExprEvaluator::HandleCast(CastExpr *E) {
845 Expr *SubExpr = E->getSubExpr();
846 QualType DestType = E->getType();
847
Chris Lattner7a767782008-07-11 19:24:49 +0000848 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000849
Eli Friedman4efaa272008-11-12 09:44:48 +0000850 if (DestType->isBooleanType()) {
851 bool BoolResult;
852 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
853 return false;
854 Result.zextOrTrunc(DestWidth);
855 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
856 Result = BoolResult;
857 return true;
858 }
859
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000860 // Handle simple integer->integer casts.
Eli Friedmana6afa762008-11-13 06:09:17 +0000861 if (SubExpr->getType()->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +0000862 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000863 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000864
865 // Figure out if this is a truncate, extend or noop cast.
866 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman4efaa272008-11-12 09:44:48 +0000867 Result.extOrTrunc(DestWidth);
Chris Lattner732b2232008-07-12 01:15:53 +0000868 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
869 return true;
870 }
871
872 // FIXME: Clean this up!
873 if (SubExpr->getType()->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000874 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000875 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000876 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000877
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000878 if (LV.getLValueBase())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000879 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000880
Anders Carlsson559e56b2008-07-08 16:49:00 +0000881 Result.extOrTrunc(DestWidth);
882 Result = LV.getLValueOffset();
Chris Lattner732b2232008-07-12 01:15:53 +0000883 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
884 return true;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000885 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000886
Chris Lattner732b2232008-07-12 01:15:53 +0000887 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000888 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +0000889
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000890 APFloat F(0.0);
891 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000892 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +0000893
894 // Determine whether we are converting to unsigned or signed.
895 bool DestSigned = DestType->isSignedIntegerType();
896
897 // FIXME: Warning for overflow.
Dale Johannesenee5a7002008-10-09 23:02:32 +0000898 uint64_t Space[4];
899 bool ignored;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000900 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +0000901 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattner732b2232008-07-12 01:15:53 +0000902 Result = llvm::APInt(DestWidth, 4, Space);
903 Result.setIsUnsigned(!DestSigned);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000904 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000905}
Anders Carlsson2bad1682008-07-08 14:30:00 +0000906
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000907//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000908// Float Evaluation
909//===----------------------------------------------------------------------===//
910
911namespace {
912class VISIBILITY_HIDDEN FloatExprEvaluator
913 : public StmtVisitor<FloatExprEvaluator, bool> {
914 EvalInfo &Info;
915 APFloat &Result;
916public:
917 FloatExprEvaluator(EvalInfo &info, APFloat &result)
918 : Info(info), Result(result) {}
919
920 bool VisitStmt(Stmt *S) {
921 return false;
922 }
923
924 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +0000925 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000926
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +0000927 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000928 bool VisitBinaryOperator(const BinaryOperator *E);
929 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000930 bool VisitCastExpr(CastExpr *E);
931 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000932};
933} // end anonymous namespace
934
935static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
936 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
937}
938
Chris Lattner019f4e82008-10-06 05:28:25 +0000939bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000940 switch (E->isBuiltinCall()) {
Chris Lattner34a74ab2008-10-06 05:53:16 +0000941 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +0000942 case Builtin::BI__builtin_huge_val:
943 case Builtin::BI__builtin_huge_valf:
944 case Builtin::BI__builtin_huge_vall:
945 case Builtin::BI__builtin_inf:
946 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +0000947 case Builtin::BI__builtin_infl: {
948 const llvm::fltSemantics &Sem =
949 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +0000950 Result = llvm::APFloat::getInf(Sem);
951 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +0000952 }
Chris Lattner9e621712008-10-06 06:31:58 +0000953
954 case Builtin::BI__builtin_nan:
955 case Builtin::BI__builtin_nanf:
956 case Builtin::BI__builtin_nanl:
957 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
958 // can't constant fold it.
959 if (const StringLiteral *S =
960 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
961 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +0000962 const llvm::fltSemantics &Sem =
963 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +0000964 Result = llvm::APFloat::getNaN(Sem);
965 return true;
966 }
967 }
968 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +0000969
970 case Builtin::BI__builtin_fabs:
971 case Builtin::BI__builtin_fabsf:
972 case Builtin::BI__builtin_fabsl:
973 if (!EvaluateFloat(E->getArg(0), Result, Info))
974 return false;
975
976 if (Result.isNegative())
977 Result.changeSign();
978 return true;
979
980 case Builtin::BI__builtin_copysign:
981 case Builtin::BI__builtin_copysignf:
982 case Builtin::BI__builtin_copysignl: {
983 APFloat RHS(0.);
984 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
985 !EvaluateFloat(E->getArg(1), RHS, Info))
986 return false;
987 Result.copySign(RHS);
988 return true;
989 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000990 }
991}
992
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +0000993bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +0000994 if (E->getOpcode() == UnaryOperator::Deref)
995 return false;
996
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +0000997 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
998 return false;
999
1000 switch (E->getOpcode()) {
1001 default: return false;
1002 case UnaryOperator::Plus:
1003 return true;
1004 case UnaryOperator::Minus:
1005 Result.changeSign();
1006 return true;
1007 }
1008}
Chris Lattner019f4e82008-10-06 05:28:25 +00001009
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001010bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1011 // FIXME: Diagnostics? I really don't understand how the warnings
1012 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001013 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001014 if (!EvaluateFloat(E->getLHS(), Result, Info))
1015 return false;
1016 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1017 return false;
1018
1019 switch (E->getOpcode()) {
1020 default: return false;
1021 case BinaryOperator::Mul:
1022 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1023 return true;
1024 case BinaryOperator::Add:
1025 Result.add(RHS, APFloat::rmNearestTiesToEven);
1026 return true;
1027 case BinaryOperator::Sub:
1028 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1029 return true;
1030 case BinaryOperator::Div:
1031 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1032 return true;
1033 case BinaryOperator::Rem:
1034 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1035 return true;
1036 }
1037}
1038
1039bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1040 Result = E->getValue();
1041 return true;
1042}
1043
Eli Friedman4efaa272008-11-12 09:44:48 +00001044bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1045 Expr* SubExpr = E->getSubExpr();
1046 const llvm::fltSemantics& destSemantics =
1047 Info.Ctx.getFloatTypeSemantics(E->getType());
1048 if (SubExpr->getType()->isIntegralType()) {
1049 APSInt IntResult;
1050 if (!EvaluateInteger(E, IntResult, Info))
1051 return false;
1052 Result = APFloat(destSemantics, 1);
1053 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1054 APFloat::rmNearestTiesToEven);
1055 return true;
1056 }
1057 if (SubExpr->getType()->isRealFloatingType()) {
1058 if (!Visit(SubExpr))
1059 return false;
1060 bool ignored;
1061 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1062 return true;
1063 }
1064
1065 return false;
1066}
1067
1068bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1069 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1070 return true;
1071}
1072
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001073//===----------------------------------------------------------------------===//
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001074// Complex Float Evaluation
1075//===----------------------------------------------------------------------===//
1076
1077namespace {
1078class VISIBILITY_HIDDEN ComplexFloatExprEvaluator
1079 : public StmtVisitor<ComplexFloatExprEvaluator, APValue> {
1080 EvalInfo &Info;
1081
1082public:
1083 ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {}
1084
1085 //===--------------------------------------------------------------------===//
1086 // Visitor Methods
1087 //===--------------------------------------------------------------------===//
1088
1089 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001090 return APValue();
1091 }
1092
1093 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1094
1095 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
1096 APFloat Result(0.0);
1097 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1098 return APValue();
1099
1100 return APValue(APFloat(0.0), Result);
1101 }
1102
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001103 APValue VisitCastExpr(CastExpr *E) {
1104 Expr* SubExpr = E->getSubExpr();
1105
1106 if (SubExpr->getType()->isRealFloatingType()) {
1107 APFloat Result(0.0);
1108
1109 if (!EvaluateFloat(SubExpr, Result, Info))
1110 return APValue();
1111
1112 return APValue(Result, APFloat(0.0));
1113 }
1114
1115 // FIXME: Handle more casts.
1116 return APValue();
1117 }
1118
1119 APValue VisitBinaryOperator(const BinaryOperator *E);
1120
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001121};
1122} // end anonymous namespace
1123
1124static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info)
1125{
1126 Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1127 return Result.isComplexFloat();
1128}
1129
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001130APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
1131{
1132 APValue Result, RHS;
1133
1134 if (!EvaluateComplexFloat(E->getLHS(), Result, Info))
1135 return APValue();
1136
1137 if (!EvaluateComplexFloat(E->getRHS(), RHS, Info))
1138 return APValue();
1139
1140 switch (E->getOpcode()) {
1141 default: return APValue();
1142 case BinaryOperator::Add:
1143 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1144 APFloat::rmNearestTiesToEven);
1145 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1146 APFloat::rmNearestTiesToEven);
1147 case BinaryOperator::Sub:
1148 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1149 APFloat::rmNearestTiesToEven);
1150 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1151 APFloat::rmNearestTiesToEven);
1152 }
1153
1154 return Result;
1155}
1156
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001157//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001158// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001159//===----------------------------------------------------------------------===//
1160
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001161/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001162/// any crazy technique (that has nothing to do with language standards) that
1163/// we want to. If this function returns true, it returns the folded constant
1164/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001165bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1166 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001167
Anders Carlsson06a36752008-07-08 05:49:43 +00001168 if (getType()->isIntegerType()) {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001169 llvm::APSInt sInt(32);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001170 if (!EvaluateInteger(this, sInt, Info))
1171 return false;
1172
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001173 Result.Val = APValue(sInt);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001174 } else if (getType()->isPointerType()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001175 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001176 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001177 } else if (getType()->isRealFloatingType()) {
1178 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001179 if (!EvaluateFloat(this, f, Info))
1180 return false;
1181
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001182 Result.Val = APValue(f);
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001183 } else if (getType()->isComplexType()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001184 if (!EvaluateComplexFloat(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001185 return false;
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001186 } else
1187 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001188
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001189 return true;
1190}
1191
1192bool Expr::Evaluate(APValue &Result, ASTContext &Ctx, bool *isEvaluated) const {
1193 EvalResult EvalResult;
1194
1195 if (!Evaluate(EvalResult, Ctx))
1196 return false;
1197
1198 Result = EvalResult.Val;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001199 if (isEvaluated)
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001200 *isEvaluated = !EvalResult.HasSideEffects;
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001201
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001202 return true;
Anders Carlssonc44eec62008-07-03 04:20:39 +00001203}
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001204
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001205/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001206/// folded, but discard the result.
1207bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001208 EvalResult Result;
1209 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001210}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001211
1212APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
1213 APValue V;
1214 bool Result = Evaluate(V, Ctx);
1215 assert(Result && "Could not evaluate expression");
1216 assert(V.isInt() && "Expression did not evaluate to integer");
1217
1218 return V.getInt();
1219}