blob: f45bd787b569b6b41733b0335fd766f150afaff7 [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);
Douglas Gregor86f19402008-12-20 23:49:58 +0000156
157 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
158 if (!FD) // FIXME: deal with other kinds of member expressions
159 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000160
161 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000162 unsigned i = 0;
163 for (RecordDecl::field_iterator Field = RD->field_begin(),
164 FieldEnd = RD->field_end();
165 Field != FieldEnd; (void)++Field, ++i) {
166 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000167 break;
168 }
169
170 result.setLValue(result.getLValueBase(),
171 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
172
173 return result;
174}
175
Anders Carlsson3068d112008-11-16 19:01:22 +0000176APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
177{
178 APValue Result;
179
180 if (!EvaluatePointer(E->getBase(), Result, Info))
181 return APValue();
182
183 APSInt Index;
184 if (!EvaluateInteger(E->getIdx(), Index, Info))
185 return APValue();
186
187 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
188
189 uint64_t Offset = Index.getSExtValue() * ElementSize;
190 Result.setLValue(Result.getLValueBase(),
191 Result.getLValueOffset() + Offset);
192 return Result;
193}
Eli Friedman4efaa272008-11-12 09:44:48 +0000194
195//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000196// Pointer Evaluation
197//===----------------------------------------------------------------------===//
198
Anders Carlssonc754aa62008-07-08 05:13:58 +0000199namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000200class VISIBILITY_HIDDEN PointerExprEvaluator
201 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000202 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000203public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000204
Chris Lattner87eae5e2008-07-11 22:52:41 +0000205 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000206
Anders Carlsson2bad1682008-07-08 14:30:00 +0000207 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000208 return APValue();
209 }
210
211 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
212
Anders Carlsson650c92f2008-07-08 15:34:11 +0000213 APValue VisitBinaryOperator(const BinaryOperator *E);
214 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 APValue VisitUnaryOperator(const UnaryOperator *E);
216 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
217 { return APValue(E, 0); }
218 APValue VisitConditionalOperator(ConditionalOperator *E);
Anders Carlsson650c92f2008-07-08 15:34:11 +0000219};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000220} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000221
Chris Lattner87eae5e2008-07-11 22:52:41 +0000222static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000223 if (!E->getType()->isPointerType())
224 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000225 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000226 return Result.isLValue();
227}
228
229APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
230 if (E->getOpcode() != BinaryOperator::Add &&
231 E->getOpcode() != BinaryOperator::Sub)
232 return APValue();
233
234 const Expr *PExp = E->getLHS();
235 const Expr *IExp = E->getRHS();
236 if (IExp->getType()->isPointerType())
237 std::swap(PExp, IExp);
238
239 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000240 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000241 return APValue();
242
243 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000244 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000245 return APValue();
246
Eli Friedman4efaa272008-11-12 09:44:48 +0000247 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
248 uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
249
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000250 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000251
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000252 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000253 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000254 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000255 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
256
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000257 return APValue(ResultLValue.getLValueBase(), Offset);
258}
Eli Friedman4efaa272008-11-12 09:44:48 +0000259
260APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
261 if (E->getOpcode() == UnaryOperator::Extension) {
262 // FIXME: Deal with warnings?
263 return Visit(E->getSubExpr());
264 }
265
266 if (E->getOpcode() == UnaryOperator::AddrOf) {
267 APValue result;
268 if (EvaluateLValue(E->getSubExpr(), result, Info))
269 return result;
270 }
271
272 return APValue();
273}
Anders Carlssond407a762008-12-05 05:24:13 +0000274
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000275
Chris Lattnerb542afe2008-07-11 19:10:17 +0000276APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000277 const Expr* SubExpr = E->getSubExpr();
278
279 // Check for pointer->pointer cast
280 if (SubExpr->getType()->isPointerType()) {
281 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000282 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000283 return Result;
284 return APValue();
285 }
286
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000287 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000288 llvm::APSInt Result(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000289 if (EvaluateInteger(SubExpr, Result, Info)) {
290 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000291 return APValue(0, Result.getZExtValue());
292 }
293 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000294
295 if (SubExpr->getType()->isFunctionType() ||
296 SubExpr->getType()->isArrayType()) {
297 APValue Result;
298 if (EvaluateLValue(SubExpr, Result, Info))
299 return Result;
300 return APValue();
301 }
302
303 //assert(0 && "Unhandled cast");
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000304 return APValue();
305}
306
Eli Friedman4efaa272008-11-12 09:44:48 +0000307APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
308 bool BoolResult;
309 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
310 return APValue();
311
312 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
313
314 APValue Result;
315 if (EvaluatePointer(EvalExpr, Result, Info))
316 return Result;
317 return APValue();
318}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000319
320//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000321// Vector Evaluation
322//===----------------------------------------------------------------------===//
323
324namespace {
325 class VISIBILITY_HIDDEN VectorExprEvaluator
326 : public StmtVisitor<VectorExprEvaluator, APValue> {
327 EvalInfo &Info;
328 public:
329
330 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
331
332 APValue VisitStmt(Stmt *S) {
333 return APValue();
334 }
335
336 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
337 APValue VisitCastExpr(const CastExpr* E);
338 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
339 APValue VisitInitListExpr(const InitListExpr *E);
340 };
341} // end anonymous namespace
342
343static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
344 if (!E->getType()->isVectorType())
345 return false;
346 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
347 return !Result.isUninit();
348}
349
350APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
351 const Expr* SE = E->getSubExpr();
352
353 // Check for vector->vector bitcast.
354 if (SE->getType()->isVectorType())
355 return this->Visit(const_cast<Expr*>(SE));
356
357 return APValue();
358}
359
360APValue
361VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
362 return this->Visit(const_cast<Expr*>(E->getInitializer()));
363}
364
365APValue
366VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
367 const VectorType *VT = E->getType()->getAsVectorType();
368 unsigned NumInits = E->getNumInits();
369
370 if (!VT || VT->getNumElements() != NumInits)
371 return APValue();
372
373 QualType EltTy = VT->getElementType();
374 llvm::SmallVector<APValue, 4> Elements;
375
376 for (unsigned i = 0; i < NumInits; i++) {
377 if (EltTy->isIntegerType()) {
378 llvm::APSInt sInt(32);
379 if (!EvaluateInteger(E->getInit(i), sInt, Info))
380 return APValue();
381 Elements.push_back(APValue(sInt));
382 } else {
383 llvm::APFloat f(0.0);
384 if (!EvaluateFloat(E->getInit(i), f, Info))
385 return APValue();
386 Elements.push_back(APValue(f));
387 }
388 }
389 return APValue(&Elements[0], Elements.size());
390}
391
392//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393// Integer Evaluation
394//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000395
396namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000397class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000398 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000399 EvalInfo &Info;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000400 APSInt &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000401public:
Chris Lattner87eae5e2008-07-11 22:52:41 +0000402 IntExprEvaluator(EvalInfo &info, APSInt &result)
403 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000404
Chris Lattner7a767782008-07-11 19:24:49 +0000405 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner54176fd2008-07-12 00:14:42 +0000406 return (unsigned)Info.Ctx.getIntWidth(T);
407 }
408
Anders Carlsson82206e22008-11-30 18:14:57 +0000409 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlsson54da0492008-11-30 16:38:33 +0000410 Info.EvalResult.DiagLoc = L;
411 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000412 Info.EvalResult.DiagExpr = E;
Chris Lattner54176fd2008-07-12 00:14:42 +0000413 return true; // still a constant.
414 }
415
Anders Carlsson82206e22008-11-30 18:14:57 +0000416 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000417 // If this is in an unevaluated portion of the subexpression, ignore the
418 // error.
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000419 if (Info.ShortCircuit) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000420 // If error is ignored because the value isn't evaluated, get the real
421 // type at least to prevent errors downstream.
Anders Carlsson82206e22008-11-30 18:14:57 +0000422 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
423 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner54176fd2008-07-12 00:14:42 +0000424 return true;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000425 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000426
Chris Lattner32fea9d2008-11-12 07:43:42 +0000427 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000428 if (Info.EvalResult.Diag == 0) {
429 Info.EvalResult.DiagLoc = L;
430 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000431 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000432 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000433 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000434 }
435
Anders Carlssonc754aa62008-07-08 05:13:58 +0000436 //===--------------------------------------------------------------------===//
437 // Visitor Methods
438 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000439
440 bool VisitStmt(Stmt *) {
441 assert(0 && "This should be called on integers, stmts are not integers");
442 return false;
443 }
Chris Lattner7a767782008-07-11 19:24:49 +0000444
Chris Lattner32fea9d2008-11-12 07:43:42 +0000445 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000446 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000447 }
448
Chris Lattnerb542afe2008-07-11 19:10:17 +0000449 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000450
Chris Lattner4c4867e2008-07-12 00:38:25 +0000451 bool VisitIntegerLiteral(const IntegerLiteral *E) {
452 Result = E->getValue();
453 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
454 return true;
455 }
456 bool VisitCharacterLiteral(const CharacterLiteral *E) {
457 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
458 Result = E->getValue();
459 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
460 return true;
461 }
462 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
463 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarac620de2008-10-24 08:07:57 +0000464 // Per gcc docs "this built-in function ignores top level
465 // qualifiers". We need to use the canonical version to properly
466 // be able to strip CRV qualifiers from the type.
467 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
468 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
469 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
470 T1.getUnqualifiedType());
Chris Lattner4c4867e2008-07-12 00:38:25 +0000471 return true;
472 }
473 bool VisitDeclRefExpr(const DeclRefExpr *E);
474 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000475 bool VisitBinaryOperator(const BinaryOperator *E);
476 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000477 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000478
Chris Lattner732b2232008-07-12 01:15:53 +0000479 bool VisitCastExpr(CastExpr* E) {
Anders Carlsson82206e22008-11-30 18:14:57 +0000480 return HandleCast(E);
Anders Carlsson650c92f2008-07-08 15:34:11 +0000481 }
Sebastian Redl05189992008-11-11 17:56:53 +0000482 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
483
Anders Carlsson3068d112008-11-16 19:01:22 +0000484 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000485 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson3068d112008-11-16 19:01:22 +0000486 Result = E->getValue();
487 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
488 return true;
489 }
490
Anders Carlsson3f704562008-12-21 22:39:40 +0000491 bool VisitGNUNullExpr(const GNUNullExpr *E) {
492 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
493 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
494 return true;
495 }
496
Anders Carlsson3068d112008-11-16 19:01:22 +0000497 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
498 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
499 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
500 return true;
501 }
502
Sebastian Redl64b45f72009-01-05 20:52:13 +0000503 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
504 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
505 Result = E->Evaluate();
506 return true;
507 }
508
Chris Lattnerfcee0012008-07-11 21:24:13 +0000509private:
Anders Carlsson82206e22008-11-30 18:14:57 +0000510 bool HandleCast(CastExpr* E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000511};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000512} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000513
Chris Lattner87eae5e2008-07-11 22:52:41 +0000514static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
515 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson650c92f2008-07-08 15:34:11 +0000516}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000517
Chris Lattner4c4867e2008-07-12 00:38:25 +0000518bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
519 // Enums are integer constant exprs.
520 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
521 Result = D->getInitVal();
Eli Friedmane9a0f432008-12-08 02:21:03 +0000522 // FIXME: This is an ugly hack around the fact that enums don't set their
523 // signedness consistently; see PR3173
524 Result.setIsUnsigned(!E->getType()->isSignedIntegerType());
Chris Lattner4c4867e2008-07-12 00:38:25 +0000525 return true;
526 }
527
528 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000529 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000530}
531
Chris Lattnera4d55d82008-10-06 06:40:35 +0000532/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
533/// as GCC.
534static int EvaluateBuiltinClassifyType(const CallExpr *E) {
535 // The following enum mimics the values returned by GCC.
536 enum gcc_type_class {
537 no_type_class = -1,
538 void_type_class, integer_type_class, char_type_class,
539 enumeral_type_class, boolean_type_class,
540 pointer_type_class, reference_type_class, offset_type_class,
541 real_type_class, complex_type_class,
542 function_type_class, method_type_class,
543 record_type_class, union_type_class,
544 array_type_class, string_type_class,
545 lang_type_class
546 };
547
548 // If no argument was supplied, default to "no_type_class". This isn't
549 // ideal, however it is what gcc does.
550 if (E->getNumArgs() == 0)
551 return no_type_class;
552
553 QualType ArgTy = E->getArg(0)->getType();
554 if (ArgTy->isVoidType())
555 return void_type_class;
556 else if (ArgTy->isEnumeralType())
557 return enumeral_type_class;
558 else if (ArgTy->isBooleanType())
559 return boolean_type_class;
560 else if (ArgTy->isCharType())
561 return string_type_class; // gcc doesn't appear to use char_type_class
562 else if (ArgTy->isIntegerType())
563 return integer_type_class;
564 else if (ArgTy->isPointerType())
565 return pointer_type_class;
566 else if (ArgTy->isReferenceType())
567 return reference_type_class;
568 else if (ArgTy->isRealType())
569 return real_type_class;
570 else if (ArgTy->isComplexType())
571 return complex_type_class;
572 else if (ArgTy->isFunctionType())
573 return function_type_class;
574 else if (ArgTy->isStructureType())
575 return record_type_class;
576 else if (ArgTy->isUnionType())
577 return union_type_class;
578 else if (ArgTy->isArrayType())
579 return array_type_class;
580 else if (ArgTy->isUnionType())
581 return union_type_class;
582 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
583 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
584 return -1;
585}
586
Chris Lattner4c4867e2008-07-12 00:38:25 +0000587bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
588 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner4c4867e2008-07-12 00:38:25 +0000589
Chris Lattner019f4e82008-10-06 05:28:25 +0000590 switch (E->isBuiltinCall()) {
591 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000592 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000593 case Builtin::BI__builtin_classify_type:
Chris Lattnera4d55d82008-10-06 06:40:35 +0000594 Result.setIsSigned(true);
595 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000596 return true;
597
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000598 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000599 // __builtin_constant_p always has one operand: it returns true if that
600 // operand can be folded, false otherwise.
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000601 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner019f4e82008-10-06 05:28:25 +0000602 return true;
603 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000604}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000605
Chris Lattnerb542afe2008-07-11 19:10:17 +0000606bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000607 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000608 if (!Visit(E->getRHS()))
609 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000610
611 if (!Info.ShortCircuit) {
612 // If we can't evaluate the LHS, it must be because it has
613 // side effects.
614 if (!E->getLHS()->isEvaluatable(Info.Ctx))
615 Info.EvalResult.HasSideEffects = true;
616
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000617 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000618 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000619
Anders Carlsson027f62e2008-12-01 02:07:06 +0000620 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000621 }
622
623 if (E->isLogicalOp()) {
624 // These need to be handled specially because the operands aren't
625 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000626 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000627
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000628 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000629 // We were able to evaluate the LHS, see if we can get away with not
630 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000631 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
632 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000633 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
634 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000635 Result = lhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000636
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000637 Info.ShortCircuit++;
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000638 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +0000639 Info.ShortCircuit--;
640
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000641 if (rhsEvaluated)
642 return true;
643
644 // FIXME: Return an extension warning saying that the RHS could not be
645 // evaluated.
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000646 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000647 }
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000648
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000649 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000650 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
651 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
652 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000653 Result = lhsResult || rhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000654 else
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000655 Result = lhsResult && rhsResult;
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000656 return true;
657 }
658 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000659 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000660 // We can't evaluate the LHS; however, sometimes the result
661 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000662 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
663 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000664 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
665 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000666 Result = rhsResult;
667
668 // Since we werent able to evaluate the left hand side, it
669 // must have had side effects.
670 Info.EvalResult.HasSideEffects = true;
671
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000672 return true;
673 }
674 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000675 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000676
Eli Friedmana6afa762008-11-13 06:09:17 +0000677 return false;
678 }
679
Anders Carlsson286f85e2008-11-16 07:17:21 +0000680 QualType LHSTy = E->getLHS()->getType();
681 QualType RHSTy = E->getRHS()->getType();
682
683 if (LHSTy->isRealFloatingType() &&
684 RHSTy->isRealFloatingType()) {
685 APFloat RHS(0.0), LHS(0.0);
686
687 if (!EvaluateFloat(E->getRHS(), RHS, Info))
688 return false;
689
690 if (!EvaluateFloat(E->getLHS(), LHS, Info))
691 return false;
692
693 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000694
695 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
696
Anders Carlsson286f85e2008-11-16 07:17:21 +0000697 switch (E->getOpcode()) {
698 default:
699 assert(0 && "Invalid binary operator!");
700 case BinaryOperator::LT:
701 Result = CR == APFloat::cmpLessThan;
702 break;
703 case BinaryOperator::GT:
704 Result = CR == APFloat::cmpGreaterThan;
705 break;
706 case BinaryOperator::LE:
707 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
708 break;
709 case BinaryOperator::GE:
710 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
711 break;
712 case BinaryOperator::EQ:
713 Result = CR == APFloat::cmpEqual;
714 break;
715 case BinaryOperator::NE:
716 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
717 break;
718 }
719
Anders Carlsson286f85e2008-11-16 07:17:21 +0000720 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
721 return true;
722 }
723
Anders Carlsson3068d112008-11-16 19:01:22 +0000724 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson529569e2008-11-16 22:46:56 +0000725 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000726 APValue LHSValue;
727 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
728 return false;
729
730 APValue RHSValue;
731 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
732 return false;
733
734 // FIXME: Is this correct? What if only one of the operands has a base?
735 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
736 return false;
737
738 const QualType Type = E->getLHS()->getType();
739 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
740
741 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
742 D /= Info.Ctx.getTypeSize(ElementType) / 8;
743
Anders Carlsson3068d112008-11-16 19:01:22 +0000744 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson529569e2008-11-16 22:46:56 +0000745 Result = D;
Anders Carlsson3068d112008-11-16 19:01:22 +0000746 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
747
748 return true;
749 }
750 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000751 if (!LHSTy->isIntegralType() ||
752 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000753 // We can't continue from here for non-integral types, and they
754 // could potentially confuse the following operations.
755 // FIXME: Deal with EQ and friends.
756 return false;
757 }
758
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000759 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000760 llvm::APSInt RHS(32);
Chris Lattnerc8cc9cc2008-11-12 07:04:29 +0000761 if (!Visit(E->getLHS())) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000762 return false; // error in subexpression.
Chris Lattnerc8cc9cc2008-11-12 07:04:29 +0000763 }
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000764
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000765
766 // FIXME Maybe we want to succeed even where we can't evaluate the
767 // right side of LAnd/LOr?
768 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner54176fd2008-07-12 00:14:42 +0000769 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000770 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000771
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000772 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000773 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000774 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner54176fd2008-07-12 00:14:42 +0000775 case BinaryOperator::Mul: Result *= RHS; return true;
776 case BinaryOperator::Add: Result += RHS; return true;
777 case BinaryOperator::Sub: Result -= RHS; return true;
778 case BinaryOperator::And: Result &= RHS; return true;
779 case BinaryOperator::Xor: Result ^= RHS; return true;
780 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner75a48812008-07-11 22:15:16 +0000781 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000782 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000783 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000784 Result /= RHS;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000785 break;
Chris Lattner75a48812008-07-11 22:15:16 +0000786 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000787 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000788 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000789 Result %= RHS;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000790 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000791 case BinaryOperator::Shl:
Chris Lattner54176fd2008-07-12 00:14:42 +0000792 // FIXME: Warn about out of range shift amounts!
Chris Lattnerb542afe2008-07-11 19:10:17 +0000793 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000794 break;
795 case BinaryOperator::Shr:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000796 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000797 break;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000798
Chris Lattnerac7cb602008-07-11 19:29:32 +0000799 case BinaryOperator::LT:
800 Result = Result < RHS;
801 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
802 break;
803 case BinaryOperator::GT:
804 Result = Result > RHS;
805 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
806 break;
807 case BinaryOperator::LE:
808 Result = Result <= RHS;
809 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
810 break;
811 case BinaryOperator::GE:
812 Result = Result >= RHS;
813 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
814 break;
815 case BinaryOperator::EQ:
816 Result = Result == RHS;
817 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
818 break;
819 case BinaryOperator::NE:
820 Result = Result != RHS;
821 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
822 break;
Chris Lattner54176fd2008-07-12 00:14:42 +0000823 case BinaryOperator::LAnd:
824 Result = Result != 0 && RHS != 0;
825 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
826 break;
827 case BinaryOperator::LOr:
828 Result = Result != 0 || RHS != 0;
829 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
830 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +0000831 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000832
833 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000834 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000835}
836
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000837bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +0000838 bool Cond;
839 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000840 return false;
841
Nuno Lopesa25bd552008-11-16 22:06:39 +0000842 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000843}
844
Sebastian Redl05189992008-11-11 17:56:53 +0000845/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
846/// expression's type.
847bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
848 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000849 // Return the result in the right width.
850 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
851 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
852
Sebastian Redl05189992008-11-11 17:56:53 +0000853 QualType SrcTy = E->getTypeOfArgument();
854
Chris Lattnerfcee0012008-07-11 21:24:13 +0000855 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman4efaa272008-11-12 09:44:48 +0000856 if (SrcTy->isVoidType()) {
Chris Lattnerfcee0012008-07-11 21:24:13 +0000857 Result = 1;
Eli Friedman4efaa272008-11-12 09:44:48 +0000858 return true;
859 }
Chris Lattnerfcee0012008-07-11 21:24:13 +0000860
861 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman4efaa272008-11-12 09:44:48 +0000862 // FIXME: But alignof(vla) is!
Chris Lattnerfcee0012008-07-11 21:24:13 +0000863 if (!SrcTy->isConstantSizeType()) {
864 // FIXME: Should we attempt to evaluate this?
865 return false;
866 }
Sebastian Redl05189992008-11-11 17:56:53 +0000867
Fariborz Jahanian67303c12009-01-16 01:42:12 +0000868 // sizeof (objc class) ?
869 if (SrcTy->isObjCInterfaceType())
870 return false;
871
Sebastian Redl05189992008-11-11 17:56:53 +0000872 bool isSizeOf = E->isSizeOf();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000873
874 // GCC extension: sizeof(function) = 1.
875 if (SrcTy->isFunctionType()) {
876 // FIXME: AlignOf shouldn't be unconditionally 4!
877 Result = isSizeOf ? 1 : 4;
878 return true;
879 }
880
881 // Get information about the size or align.
Chris Lattner87eae5e2008-07-11 22:52:41 +0000882 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000883 if (isSizeOf)
Eli Friedman4efaa272008-11-12 09:44:48 +0000884 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattnerfcee0012008-07-11 21:24:13 +0000885 else
Chris Lattner87eae5e2008-07-11 22:52:41 +0000886 Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize;
Chris Lattnerfcee0012008-07-11 21:24:13 +0000887 return true;
888}
889
Chris Lattnerb542afe2008-07-11 19:10:17 +0000890bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000891 // Special case unary operators that do not need their subexpression
892 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner75a48812008-07-11 22:15:16 +0000893 if (E->isOffsetOfOp()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000894 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner87eae5e2008-07-11 22:52:41 +0000895 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner75a48812008-07-11 22:15:16 +0000896 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
897 return true;
898 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000899
900 if (E->getOpcode() == UnaryOperator::LNot) {
901 // LNot's operand isn't necessarily an integer, so we handle it specially.
902 bool bres;
903 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
904 return false;
905 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
906 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
907 Result = !bres;
908 return true;
909 }
910
Chris Lattner87eae5e2008-07-11 22:52:41 +0000911 // Get the operand value into 'Result'.
912 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +0000913 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000914
Chris Lattner75a48812008-07-11 22:15:16 +0000915 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000916 default:
Chris Lattner75a48812008-07-11 22:15:16 +0000917 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
918 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000919 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000920 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000921 // FIXME: Should extension allow i-c-e extension expressions in its scope?
922 // If so, we could clear the diagnostic ID.
Chris Lattner75a48812008-07-11 22:15:16 +0000923 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000924 // The result is always just the subexpr.
Chris Lattner75a48812008-07-11 22:15:16 +0000925 break;
926 case UnaryOperator::Minus:
927 Result = -Result;
928 break;
929 case UnaryOperator::Not:
930 Result = ~Result;
931 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000932 }
933
934 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000935 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000936}
937
Chris Lattner732b2232008-07-12 01:15:53 +0000938/// HandleCast - This is used to evaluate implicit or explicit casts where the
939/// result type is integer.
Anders Carlsson82206e22008-11-30 18:14:57 +0000940bool IntExprEvaluator::HandleCast(CastExpr *E) {
941 Expr *SubExpr = E->getSubExpr();
942 QualType DestType = E->getType();
943
Chris Lattner7a767782008-07-11 19:24:49 +0000944 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000945
Eli Friedman4efaa272008-11-12 09:44:48 +0000946 if (DestType->isBooleanType()) {
947 bool BoolResult;
948 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
949 return false;
950 Result.zextOrTrunc(DestWidth);
951 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
952 Result = BoolResult;
953 return true;
954 }
955
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000956 // Handle simple integer->integer casts.
Eli Friedmana6afa762008-11-13 06:09:17 +0000957 if (SubExpr->getType()->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +0000958 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000959 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000960
961 // Figure out if this is a truncate, extend or noop cast.
962 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman4efaa272008-11-12 09:44:48 +0000963 Result.extOrTrunc(DestWidth);
Chris Lattner732b2232008-07-12 01:15:53 +0000964 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
965 return true;
966 }
967
968 // FIXME: Clean this up!
969 if (SubExpr->getType()->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000970 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000971 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000972 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000973
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000974 if (LV.getLValueBase())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000975 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000976
Anders Carlsson559e56b2008-07-08 16:49:00 +0000977 Result.extOrTrunc(DestWidth);
978 Result = LV.getLValueOffset();
Chris Lattner732b2232008-07-12 01:15:53 +0000979 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
980 return true;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000981 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000982
Chris Lattner732b2232008-07-12 01:15:53 +0000983 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000984 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +0000985
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000986 APFloat F(0.0);
987 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000988 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +0000989
990 // Determine whether we are converting to unsigned or signed.
991 bool DestSigned = DestType->isSignedIntegerType();
992
993 // FIXME: Warning for overflow.
Dale Johannesenee5a7002008-10-09 23:02:32 +0000994 uint64_t Space[4];
995 bool ignored;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000996 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +0000997 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattner732b2232008-07-12 01:15:53 +0000998 Result = llvm::APInt(DestWidth, 4, Space);
999 Result.setIsUnsigned(!DestSigned);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001000 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001001}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001002
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001003//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001004// Float Evaluation
1005//===----------------------------------------------------------------------===//
1006
1007namespace {
1008class VISIBILITY_HIDDEN FloatExprEvaluator
1009 : public StmtVisitor<FloatExprEvaluator, bool> {
1010 EvalInfo &Info;
1011 APFloat &Result;
1012public:
1013 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1014 : Info(info), Result(result) {}
1015
1016 bool VisitStmt(Stmt *S) {
1017 return false;
1018 }
1019
1020 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001021 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001022
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001023 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001024 bool VisitBinaryOperator(const BinaryOperator *E);
1025 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001026 bool VisitCastExpr(CastExpr *E);
1027 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001028};
1029} // end anonymous namespace
1030
1031static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1032 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1033}
1034
Chris Lattner019f4e82008-10-06 05:28:25 +00001035bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001036 switch (E->isBuiltinCall()) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001037 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001038 case Builtin::BI__builtin_huge_val:
1039 case Builtin::BI__builtin_huge_valf:
1040 case Builtin::BI__builtin_huge_vall:
1041 case Builtin::BI__builtin_inf:
1042 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001043 case Builtin::BI__builtin_infl: {
1044 const llvm::fltSemantics &Sem =
1045 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001046 Result = llvm::APFloat::getInf(Sem);
1047 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001048 }
Chris Lattner9e621712008-10-06 06:31:58 +00001049
1050 case Builtin::BI__builtin_nan:
1051 case Builtin::BI__builtin_nanf:
1052 case Builtin::BI__builtin_nanl:
1053 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1054 // can't constant fold it.
1055 if (const StringLiteral *S =
1056 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1057 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001058 const llvm::fltSemantics &Sem =
1059 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001060 Result = llvm::APFloat::getNaN(Sem);
1061 return true;
1062 }
1063 }
1064 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001065
1066 case Builtin::BI__builtin_fabs:
1067 case Builtin::BI__builtin_fabsf:
1068 case Builtin::BI__builtin_fabsl:
1069 if (!EvaluateFloat(E->getArg(0), Result, Info))
1070 return false;
1071
1072 if (Result.isNegative())
1073 Result.changeSign();
1074 return true;
1075
1076 case Builtin::BI__builtin_copysign:
1077 case Builtin::BI__builtin_copysignf:
1078 case Builtin::BI__builtin_copysignl: {
1079 APFloat RHS(0.);
1080 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1081 !EvaluateFloat(E->getArg(1), RHS, Info))
1082 return false;
1083 Result.copySign(RHS);
1084 return true;
1085 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001086 }
1087}
1088
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001089bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001090 if (E->getOpcode() == UnaryOperator::Deref)
1091 return false;
1092
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001093 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1094 return false;
1095
1096 switch (E->getOpcode()) {
1097 default: return false;
1098 case UnaryOperator::Plus:
1099 return true;
1100 case UnaryOperator::Minus:
1101 Result.changeSign();
1102 return true;
1103 }
1104}
Chris Lattner019f4e82008-10-06 05:28:25 +00001105
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001106bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1107 // FIXME: Diagnostics? I really don't understand how the warnings
1108 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001109 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001110 if (!EvaluateFloat(E->getLHS(), Result, Info))
1111 return false;
1112 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1113 return false;
1114
1115 switch (E->getOpcode()) {
1116 default: return false;
1117 case BinaryOperator::Mul:
1118 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1119 return true;
1120 case BinaryOperator::Add:
1121 Result.add(RHS, APFloat::rmNearestTiesToEven);
1122 return true;
1123 case BinaryOperator::Sub:
1124 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1125 return true;
1126 case BinaryOperator::Div:
1127 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1128 return true;
1129 case BinaryOperator::Rem:
1130 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1131 return true;
1132 }
1133}
1134
1135bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1136 Result = E->getValue();
1137 return true;
1138}
1139
Eli Friedman4efaa272008-11-12 09:44:48 +00001140bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1141 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001142
Eli Friedman4efaa272008-11-12 09:44:48 +00001143 const llvm::fltSemantics& destSemantics =
1144 Info.Ctx.getFloatTypeSemantics(E->getType());
1145 if (SubExpr->getType()->isIntegralType()) {
1146 APSInt IntResult;
1147 if (!EvaluateInteger(E, IntResult, Info))
1148 return false;
1149 Result = APFloat(destSemantics, 1);
1150 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1151 APFloat::rmNearestTiesToEven);
1152 return true;
1153 }
1154 if (SubExpr->getType()->isRealFloatingType()) {
1155 if (!Visit(SubExpr))
1156 return false;
1157 bool ignored;
1158 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1159 return true;
1160 }
1161
1162 return false;
1163}
1164
1165bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1166 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1167 return true;
1168}
1169
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001170//===----------------------------------------------------------------------===//
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001171// Complex Float Evaluation
1172//===----------------------------------------------------------------------===//
1173
1174namespace {
1175class VISIBILITY_HIDDEN ComplexFloatExprEvaluator
1176 : public StmtVisitor<ComplexFloatExprEvaluator, APValue> {
1177 EvalInfo &Info;
1178
1179public:
1180 ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {}
1181
1182 //===--------------------------------------------------------------------===//
1183 // Visitor Methods
1184 //===--------------------------------------------------------------------===//
1185
1186 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001187 return APValue();
1188 }
1189
1190 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1191
1192 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
1193 APFloat Result(0.0);
1194 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1195 return APValue();
1196
1197 return APValue(APFloat(0.0), Result);
1198 }
1199
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001200 APValue VisitCastExpr(CastExpr *E) {
1201 Expr* SubExpr = E->getSubExpr();
1202
1203 if (SubExpr->getType()->isRealFloatingType()) {
1204 APFloat Result(0.0);
1205
1206 if (!EvaluateFloat(SubExpr, Result, Info))
1207 return APValue();
1208
1209 return APValue(Result, APFloat(0.0));
1210 }
1211
1212 // FIXME: Handle more casts.
1213 return APValue();
1214 }
1215
1216 APValue VisitBinaryOperator(const BinaryOperator *E);
1217
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001218};
1219} // end anonymous namespace
1220
1221static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info)
1222{
1223 Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1224 return Result.isComplexFloat();
1225}
1226
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001227APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
1228{
1229 APValue Result, RHS;
1230
1231 if (!EvaluateComplexFloat(E->getLHS(), Result, Info))
1232 return APValue();
1233
1234 if (!EvaluateComplexFloat(E->getRHS(), RHS, Info))
1235 return APValue();
1236
1237 switch (E->getOpcode()) {
1238 default: return APValue();
1239 case BinaryOperator::Add:
1240 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1241 APFloat::rmNearestTiesToEven);
1242 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1243 APFloat::rmNearestTiesToEven);
1244 case BinaryOperator::Sub:
1245 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1246 APFloat::rmNearestTiesToEven);
1247 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1248 APFloat::rmNearestTiesToEven);
1249 }
1250
1251 return Result;
1252}
1253
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001254//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001255// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001256//===----------------------------------------------------------------------===//
1257
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001258/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001259/// any crazy technique (that has nothing to do with language standards) that
1260/// we want to. If this function returns true, it returns the folded constant
1261/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001262bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1263 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001264
Nate Begeman59b5da62009-01-18 03:20:47 +00001265 if (getType()->isVectorType()) {
1266 if (!EvaluateVector(this, Result.Val, Info))
1267 return false;
1268 } else if (getType()->isIntegerType()) {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001269 llvm::APSInt sInt(32);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001270 if (!EvaluateInteger(this, sInt, Info))
1271 return false;
1272
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001273 Result.Val = APValue(sInt);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001274 } else if (getType()->isPointerType()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001275 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001276 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001277 } else if (getType()->isRealFloatingType()) {
1278 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001279 if (!EvaluateFloat(this, f, Info))
1280 return false;
1281
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001282 Result.Val = APValue(f);
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001283 } else if (getType()->isComplexType()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001284 if (!EvaluateComplexFloat(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001285 return false;
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001286 } else
1287 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001288
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001289 return true;
1290}
1291
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001292/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001293/// folded, but discard the result.
1294bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001295 EvalResult Result;
1296 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001297}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001298
1299APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001300 EvalResult EvalResult;
1301 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001302 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001303 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001304 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001305
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001306 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001307}