blob: f07df0a187ebc4d37e293b6d0c9279ada9d71641 [file] [log] [blame]
Chris Lattnera42f09a2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc7436af2008-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 Friedman7888b932008-11-12 09:44:48 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeonefddb9c2008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Anders Carlssonc0328012008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssoncad17b52008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc7436af2008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnera823ccf2008-07-11 18:11:29 +000022using llvm::APSInt;
Eli Friedman2f445492008-08-22 00:06:13 +000023using llvm::APFloat;
Anders Carlssonc7436af2008-07-03 04:20:39 +000024
Chris Lattner422373c2008-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 Carlssondd8d41f2008-11-30 16:38:33 +000042 /// EvalResult - Contains information about the evaluation.
43 Expr::EvalResult &EvalResult;
Anders Carlsson6c1a9e22008-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 Lattner422373c2008-07-11 22:52:41 +000048
Anders Carlssondd8d41f2008-11-30 16:38:33 +000049 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Anders Carlsson6c1a9e22008-11-30 18:26:25 +000050 EvalResult(evalresult), ShortCircuit(0) {}
Chris Lattner422373c2008-07-11 22:52:41 +000051};
52
53
Eli Friedman7888b932008-11-12 09:44:48 +000054static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner422373c2008-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 Friedman2f445492008-08-22 00:06:13 +000057static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbaraf781bb2009-01-28 22:24:07 +000058static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnera823ccf2008-07-11 18:11:29 +000059
60//===----------------------------------------------------------------------===//
Eli Friedman7888b932008-11-12 09:44:48 +000061// Misc utilities
62//===----------------------------------------------------------------------===//
63
64static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
65 if (E->getType()->isIntegralType()) {
66 APSInt IntResult;
67 if (!EvaluateInteger(E, IntResult, Info))
68 return false;
69 Result = IntResult != 0;
70 return true;
71 } else if (E->getType()->isRealFloatingType()) {
72 APFloat FloatResult(0.0);
73 if (!EvaluateFloat(E, FloatResult, Info))
74 return false;
75 Result = !FloatResult.isZero();
76 return true;
77 } else if (E->getType()->isPointerType()) {
78 APValue PointerResult;
79 if (!EvaluatePointer(E, PointerResult, Info))
80 return false;
81 // FIXME: Is this accurate for all kinds of bases? If not, what would
82 // the check look like?
83 Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
84 return true;
85 }
86
87 return false;
88}
89
90//===----------------------------------------------------------------------===//
91// LValue Evaluation
92//===----------------------------------------------------------------------===//
93namespace {
94class VISIBILITY_HIDDEN LValueExprEvaluator
95 : public StmtVisitor<LValueExprEvaluator, APValue> {
96 EvalInfo &Info;
97public:
98
99 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
100
101 APValue VisitStmt(Stmt *S) {
Daniel Dunbarff59ed82008-11-12 21:52:46 +0000102#if 0
Eli Friedman7888b932008-11-12 09:44:48 +0000103 // FIXME: Remove this when we support more expressions.
104 printf("Unhandled pointer statement\n");
105 S->dump();
Daniel Dunbarff59ed82008-11-12 21:52:46 +0000106#endif
Eli Friedman7888b932008-11-12 09:44:48 +0000107 return APValue();
108 }
109
110 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssone284ebe2008-11-24 04:41:22 +0000111 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000112 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
113 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
114 APValue VisitMemberExpr(MemberExpr *E);
115 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Anders Carlsson027f2882008-11-16 19:01:22 +0000116 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000117};
118} // end anonymous namespace
119
120static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
121 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
122 return Result.isLValue();
123}
124
Anders Carlssone284ebe2008-11-24 04:41:22 +0000125APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
126{
127 if (!E->hasGlobalStorage())
128 return APValue();
129
130 return APValue(E, 0);
131}
132
Eli Friedman7888b932008-11-12 09:44:48 +0000133APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
134 if (E->isFileScope())
135 return APValue(E, 0);
136 return APValue();
137}
138
139APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
140 APValue result;
141 QualType Ty;
142 if (E->isArrow()) {
143 if (!EvaluatePointer(E->getBase(), result, Info))
144 return APValue();
145 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
146 } else {
147 result = Visit(E->getBase());
148 if (result.isUninit())
149 return APValue();
150 Ty = E->getBase()->getType();
151 }
152
153 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
154 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +0000155
156 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
157 if (!FD) // FIXME: deal with other kinds of member expressions
158 return APValue();
Eli Friedman7888b932008-11-12 09:44:48 +0000159
160 // FIXME: This is linear time.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000161 unsigned i = 0;
162 for (RecordDecl::field_iterator Field = RD->field_begin(),
163 FieldEnd = RD->field_end();
164 Field != FieldEnd; (void)++Field, ++i) {
165 if (*Field == FD)
Eli Friedman7888b932008-11-12 09:44:48 +0000166 break;
167 }
168
169 result.setLValue(result.getLValueBase(),
170 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
171
172 return result;
173}
174
Anders Carlsson027f2882008-11-16 19:01:22 +0000175APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
176{
177 APValue Result;
178
179 if (!EvaluatePointer(E->getBase(), Result, Info))
180 return APValue();
181
182 APSInt Index;
183 if (!EvaluateInteger(E->getIdx(), Index, Info))
184 return APValue();
185
186 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
187
188 uint64_t Offset = Index.getSExtValue() * ElementSize;
189 Result.setLValue(Result.getLValueBase(),
190 Result.getLValueOffset() + Offset);
191 return Result;
192}
Eli Friedman7888b932008-11-12 09:44:48 +0000193
194//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000195// Pointer Evaluation
196//===----------------------------------------------------------------------===//
197
Anders Carlssoncad17b52008-07-08 05:13:58 +0000198namespace {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000199class VISIBILITY_HIDDEN PointerExprEvaluator
200 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner422373c2008-07-11 22:52:41 +0000201 EvalInfo &Info;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000202public:
Anders Carlsson02a34c32008-07-08 14:30:00 +0000203
Chris Lattner422373c2008-07-11 22:52:41 +0000204 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000205
Anders Carlsson02a34c32008-07-08 14:30:00 +0000206 APValue VisitStmt(Stmt *S) {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000207 return APValue();
208 }
209
210 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
211
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000212 APValue VisitBinaryOperator(const BinaryOperator *E);
213 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman7888b932008-11-12 09:44:48 +0000214 APValue VisitUnaryOperator(const UnaryOperator *E);
215 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
216 { return APValue(E, 0); }
Eli Friedmanf1941132009-01-25 01:21:06 +0000217 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
218 { return APValue(E, 0); }
Eli Friedman67f4ac52009-01-25 01:54:01 +0000219 APValue VisitCallExpr(CallExpr *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000220 APValue VisitConditionalOperator(ConditionalOperator *E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000221};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000222} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000223
Chris Lattner422373c2008-07-11 22:52:41 +0000224static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000225 if (!E->getType()->isPointerType())
226 return false;
Chris Lattner422373c2008-07-11 22:52:41 +0000227 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000228 return Result.isLValue();
229}
230
231APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
232 if (E->getOpcode() != BinaryOperator::Add &&
233 E->getOpcode() != BinaryOperator::Sub)
234 return APValue();
235
236 const Expr *PExp = E->getLHS();
237 const Expr *IExp = E->getRHS();
238 if (IExp->getType()->isPointerType())
239 std::swap(PExp, IExp);
240
241 APValue ResultLValue;
Chris Lattner422373c2008-07-11 22:52:41 +0000242 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000243 return APValue();
244
245 llvm::APSInt AdditionalOffset(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000246 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000247 return APValue();
248
Eli Friedman7888b932008-11-12 09:44:48 +0000249 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
250 uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
251
Chris Lattnera823ccf2008-07-11 18:11:29 +0000252 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman7888b932008-11-12 09:44:48 +0000253
Chris Lattnera823ccf2008-07-11 18:11:29 +0000254 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman7888b932008-11-12 09:44:48 +0000255 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnera823ccf2008-07-11 18:11:29 +0000256 else
Eli Friedman7888b932008-11-12 09:44:48 +0000257 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
258
Chris Lattnera823ccf2008-07-11 18:11:29 +0000259 return APValue(ResultLValue.getLValueBase(), Offset);
260}
Eli Friedman7888b932008-11-12 09:44:48 +0000261
262APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
263 if (E->getOpcode() == UnaryOperator::Extension) {
264 // FIXME: Deal with warnings?
265 return Visit(E->getSubExpr());
266 }
267
268 if (E->getOpcode() == UnaryOperator::AddrOf) {
269 APValue result;
270 if (EvaluateLValue(E->getSubExpr(), result, Info))
271 return result;
272 }
273
274 return APValue();
275}
Anders Carlsson4ab88da2008-12-05 05:24:13 +0000276
Chris Lattnera823ccf2008-07-11 18:11:29 +0000277
Chris Lattnera42f09a2008-07-11 19:10:17 +0000278APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000279 const Expr* SubExpr = E->getSubExpr();
280
281 // Check for pointer->pointer cast
282 if (SubExpr->getType()->isPointerType()) {
283 APValue Result;
Chris Lattner422373c2008-07-11 22:52:41 +0000284 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000285 return Result;
286 return APValue();
287 }
288
Eli Friedman3e64dd72008-07-27 05:46:18 +0000289 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000290 llvm::APSInt Result(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000291 if (EvaluateInteger(SubExpr, Result, Info)) {
292 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000293 return APValue(0, Result.getZExtValue());
294 }
295 }
Eli Friedman7888b932008-11-12 09:44:48 +0000296
297 if (SubExpr->getType()->isFunctionType() ||
298 SubExpr->getType()->isArrayType()) {
299 APValue Result;
300 if (EvaluateLValue(SubExpr, Result, Info))
301 return Result;
302 return APValue();
303 }
304
305 //assert(0 && "Unhandled cast");
Chris Lattnera823ccf2008-07-11 18:11:29 +0000306 return APValue();
307}
308
Eli Friedman67f4ac52009-01-25 01:54:01 +0000309APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
310 if (E->isBuiltinCall() == Builtin::BI__builtin___CFStringMakeConstantString)
311 return APValue(E, 0);
312 return APValue();
313}
314
Eli Friedman7888b932008-11-12 09:44:48 +0000315APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
316 bool BoolResult;
317 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
318 return APValue();
319
320 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
321
322 APValue Result;
323 if (EvaluatePointer(EvalExpr, Result, Info))
324 return Result;
325 return APValue();
326}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000327
328//===----------------------------------------------------------------------===//
Nate Begemand6d2f772009-01-18 03:20:47 +0000329// Vector Evaluation
330//===----------------------------------------------------------------------===//
331
332namespace {
333 class VISIBILITY_HIDDEN VectorExprEvaluator
334 : public StmtVisitor<VectorExprEvaluator, APValue> {
335 EvalInfo &Info;
336 public:
337
338 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
339
340 APValue VisitStmt(Stmt *S) {
341 return APValue();
342 }
343
344 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
345 APValue VisitCastExpr(const CastExpr* E);
346 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
347 APValue VisitInitListExpr(const InitListExpr *E);
348 };
349} // end anonymous namespace
350
351static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
352 if (!E->getType()->isVectorType())
353 return false;
354 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
355 return !Result.isUninit();
356}
357
358APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
359 const Expr* SE = E->getSubExpr();
360
361 // Check for vector->vector bitcast.
362 if (SE->getType()->isVectorType())
363 return this->Visit(const_cast<Expr*>(SE));
364
365 return APValue();
366}
367
368APValue
369VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
370 return this->Visit(const_cast<Expr*>(E->getInitializer()));
371}
372
373APValue
374VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
375 const VectorType *VT = E->getType()->getAsVectorType();
376 unsigned NumInits = E->getNumInits();
377
378 if (!VT || VT->getNumElements() != NumInits)
379 return APValue();
380
381 QualType EltTy = VT->getElementType();
382 llvm::SmallVector<APValue, 4> Elements;
383
384 for (unsigned i = 0; i < NumInits; i++) {
385 if (EltTy->isIntegerType()) {
386 llvm::APSInt sInt(32);
387 if (!EvaluateInteger(E->getInit(i), sInt, Info))
388 return APValue();
389 Elements.push_back(APValue(sInt));
390 } else {
391 llvm::APFloat f(0.0);
392 if (!EvaluateFloat(E->getInit(i), f, Info))
393 return APValue();
394 Elements.push_back(APValue(f));
395 }
396 }
397 return APValue(&Elements[0], Elements.size());
398}
399
400//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000401// Integer Evaluation
402//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000403
404namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000405class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000406 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000407 EvalInfo &Info;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000408 APSInt &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000409public:
Chris Lattner422373c2008-07-11 22:52:41 +0000410 IntExprEvaluator(EvalInfo &info, APSInt &result)
411 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000412
Chris Lattner2c99c712008-07-11 19:24:49 +0000413 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner82437da2008-07-12 00:14:42 +0000414 return (unsigned)Info.Ctx.getIntWidth(T);
415 }
416
Anders Carlssonfa76d822008-11-30 18:14:57 +0000417 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000418 Info.EvalResult.DiagLoc = L;
419 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000420 Info.EvalResult.DiagExpr = E;
Chris Lattner82437da2008-07-12 00:14:42 +0000421 return true; // still a constant.
422 }
423
Anders Carlssonfa76d822008-11-30 18:14:57 +0000424 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner82437da2008-07-12 00:14:42 +0000425 // If this is in an unevaluated portion of the subexpression, ignore the
426 // error.
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000427 if (Info.ShortCircuit) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000428 // If error is ignored because the value isn't evaluated, get the real
429 // type at least to prevent errors downstream.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000430 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
431 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner82437da2008-07-12 00:14:42 +0000432 return true;
Chris Lattner438f3b12008-11-12 07:43:42 +0000433 }
Chris Lattner82437da2008-07-12 00:14:42 +0000434
Chris Lattner438f3b12008-11-12 07:43:42 +0000435 // Take the first error.
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000436 if (Info.EvalResult.Diag == 0) {
437 Info.EvalResult.DiagLoc = L;
438 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000439 Info.EvalResult.DiagExpr = E;
Chris Lattner438f3b12008-11-12 07:43:42 +0000440 }
Chris Lattner82437da2008-07-12 00:14:42 +0000441 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000442 }
443
Anders Carlssoncad17b52008-07-08 05:13:58 +0000444 //===--------------------------------------------------------------------===//
445 // Visitor Methods
446 //===--------------------------------------------------------------------===//
Chris Lattner438f3b12008-11-12 07:43:42 +0000447
448 bool VisitStmt(Stmt *) {
449 assert(0 && "This should be called on integers, stmts are not integers");
450 return false;
451 }
Chris Lattner2c99c712008-07-11 19:24:49 +0000452
Chris Lattner438f3b12008-11-12 07:43:42 +0000453 bool VisitExpr(Expr *E) {
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000454 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000455 }
456
Chris Lattnera42f09a2008-07-11 19:10:17 +0000457 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000458
Chris Lattner15e59112008-07-12 00:38:25 +0000459 bool VisitIntegerLiteral(const IntegerLiteral *E) {
460 Result = E->getValue();
461 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
462 return true;
463 }
464 bool VisitCharacterLiteral(const CharacterLiteral *E) {
465 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
466 Result = E->getValue();
467 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
468 return true;
469 }
470 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
471 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000472 // Per gcc docs "this built-in function ignores top level
473 // qualifiers". We need to use the canonical version to properly
474 // be able to strip CRV qualifiers from the type.
475 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
476 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
477 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
478 T1.getUnqualifiedType());
Chris Lattner15e59112008-07-12 00:38:25 +0000479 return true;
480 }
481 bool VisitDeclRefExpr(const DeclRefExpr *E);
482 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000483 bool VisitBinaryOperator(const BinaryOperator *E);
484 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000485 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000486
Chris Lattnerff579ff2008-07-12 01:15:53 +0000487 bool VisitCastExpr(CastExpr* E) {
Anders Carlssonfa76d822008-11-30 18:14:57 +0000488 return HandleCast(E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000489 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000490 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
491
Anders Carlsson027f2882008-11-16 19:01:22 +0000492 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000493 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson027f2882008-11-16 19:01:22 +0000494 Result = E->getValue();
495 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
496 return true;
497 }
498
Anders Carlsson774f9c72008-12-21 22:39:40 +0000499 bool VisitGNUNullExpr(const GNUNullExpr *E) {
500 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
501 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
502 return true;
503 }
504
Anders Carlsson027f2882008-11-16 19:01:22 +0000505 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
506 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
507 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
508 return true;
509 }
510
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000511 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
512 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
513 Result = E->Evaluate();
514 return true;
515 }
516
Chris Lattner265a0892008-07-11 21:24:13 +0000517private:
Anders Carlssonfa76d822008-11-30 18:14:57 +0000518 bool HandleCast(CastExpr* E);
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000519 unsigned GetAlignOfExpr(const Expr *E);
520 unsigned GetAlignOfType(QualType T);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000521};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000522} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000523
Chris Lattner422373c2008-07-11 22:52:41 +0000524static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
525 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000526}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000527
Chris Lattner15e59112008-07-12 00:38:25 +0000528bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
529 // Enums are integer constant exprs.
530 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
531 Result = D->getInitVal();
Eli Friedman8b10a232008-12-08 02:21:03 +0000532 // FIXME: This is an ugly hack around the fact that enums don't set their
533 // signedness consistently; see PR3173
534 Result.setIsUnsigned(!E->getType()->isSignedIntegerType());
Chris Lattner15e59112008-07-12 00:38:25 +0000535 return true;
536 }
537
538 // Otherwise, random variable references are not constants.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000539 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000540}
541
Chris Lattner1eee9402008-10-06 06:40:35 +0000542/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
543/// as GCC.
544static int EvaluateBuiltinClassifyType(const CallExpr *E) {
545 // The following enum mimics the values returned by GCC.
546 enum gcc_type_class {
547 no_type_class = -1,
548 void_type_class, integer_type_class, char_type_class,
549 enumeral_type_class, boolean_type_class,
550 pointer_type_class, reference_type_class, offset_type_class,
551 real_type_class, complex_type_class,
552 function_type_class, method_type_class,
553 record_type_class, union_type_class,
554 array_type_class, string_type_class,
555 lang_type_class
556 };
557
558 // If no argument was supplied, default to "no_type_class". This isn't
559 // ideal, however it is what gcc does.
560 if (E->getNumArgs() == 0)
561 return no_type_class;
562
563 QualType ArgTy = E->getArg(0)->getType();
564 if (ArgTy->isVoidType())
565 return void_type_class;
566 else if (ArgTy->isEnumeralType())
567 return enumeral_type_class;
568 else if (ArgTy->isBooleanType())
569 return boolean_type_class;
570 else if (ArgTy->isCharType())
571 return string_type_class; // gcc doesn't appear to use char_type_class
572 else if (ArgTy->isIntegerType())
573 return integer_type_class;
574 else if (ArgTy->isPointerType())
575 return pointer_type_class;
576 else if (ArgTy->isReferenceType())
577 return reference_type_class;
578 else if (ArgTy->isRealType())
579 return real_type_class;
580 else if (ArgTy->isComplexType())
581 return complex_type_class;
582 else if (ArgTy->isFunctionType())
583 return function_type_class;
584 else if (ArgTy->isStructureType())
585 return record_type_class;
586 else if (ArgTy->isUnionType())
587 return union_type_class;
588 else if (ArgTy->isArrayType())
589 return array_type_class;
590 else if (ArgTy->isUnionType())
591 return union_type_class;
592 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
593 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
594 return -1;
595}
596
Chris Lattner15e59112008-07-12 00:38:25 +0000597bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
598 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner15e59112008-07-12 00:38:25 +0000599
Chris Lattner87293782008-10-06 05:28:25 +0000600 switch (E->isBuiltinCall()) {
601 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000602 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner87293782008-10-06 05:28:25 +0000603 case Builtin::BI__builtin_classify_type:
Chris Lattner1eee9402008-10-06 06:40:35 +0000604 Result.setIsSigned(true);
605 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner87293782008-10-06 05:28:25 +0000606 return true;
607
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000608 case Builtin::BI__builtin_constant_p:
Chris Lattner87293782008-10-06 05:28:25 +0000609 // __builtin_constant_p always has one operand: it returns true if that
610 // operand can be folded, false otherwise.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000611 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner87293782008-10-06 05:28:25 +0000612 return true;
613 }
Chris Lattner15e59112008-07-12 00:38:25 +0000614}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000615
Chris Lattnera42f09a2008-07-11 19:10:17 +0000616bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000617 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000618 if (!Visit(E->getRHS()))
619 return false;
Anders Carlsson197f6f72008-12-01 06:44:05 +0000620
621 if (!Info.ShortCircuit) {
622 // If we can't evaluate the LHS, it must be because it has
623 // side effects.
624 if (!E->getLHS()->isEvaluatable(Info.Ctx))
625 Info.EvalResult.HasSideEffects = true;
626
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000627 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson197f6f72008-12-01 06:44:05 +0000628 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000629
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000630 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000631 }
632
633 if (E->isLogicalOp()) {
634 // These need to be handled specially because the operands aren't
635 // necessarily integral
Anders Carlsson501da1f2008-11-30 16:51:17 +0000636 bool lhsResult, rhsResult;
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000637
Anders Carlsson501da1f2008-11-30 16:51:17 +0000638 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000639 // We were able to evaluate the LHS, see if we can get away with not
640 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlsson501da1f2008-11-30 16:51:17 +0000641 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
642 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000643 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
644 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000645 Result = lhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000646
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000647 Info.ShortCircuit++;
Anders Carlsson501da1f2008-11-30 16:51:17 +0000648 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000649 Info.ShortCircuit--;
650
Anders Carlsson501da1f2008-11-30 16:51:17 +0000651 if (rhsEvaluated)
652 return true;
653
654 // FIXME: Return an extension warning saying that the RHS could not be
655 // evaluated.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000656 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000657 }
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000658
Anders Carlsson501da1f2008-11-30 16:51:17 +0000659 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000660 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
661 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
662 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlsson501da1f2008-11-30 16:51:17 +0000663 Result = lhsResult || rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000664 else
Anders Carlsson501da1f2008-11-30 16:51:17 +0000665 Result = lhsResult && rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000666 return true;
667 }
668 } else {
Anders Carlsson501da1f2008-11-30 16:51:17 +0000669 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000670 // We can't evaluate the LHS; however, sometimes the result
671 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlsson501da1f2008-11-30 16:51:17 +0000672 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
673 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000674 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
675 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000676 Result = rhsResult;
677
678 // Since we werent able to evaluate the left hand side, it
679 // must have had side effects.
680 Info.EvalResult.HasSideEffects = true;
681
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000682 return true;
683 }
684 }
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000685 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000686
Eli Friedman14cc7542008-11-13 06:09:17 +0000687 return false;
688 }
689
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000690 QualType LHSTy = E->getLHS()->getType();
691 QualType RHSTy = E->getRHS()->getType();
692
693 if (LHSTy->isRealFloatingType() &&
694 RHSTy->isRealFloatingType()) {
695 APFloat RHS(0.0), LHS(0.0);
696
697 if (!EvaluateFloat(E->getRHS(), RHS, Info))
698 return false;
699
700 if (!EvaluateFloat(E->getLHS(), LHS, Info))
701 return false;
702
703 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000704
705 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
706
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000707 switch (E->getOpcode()) {
708 default:
709 assert(0 && "Invalid binary operator!");
710 case BinaryOperator::LT:
711 Result = CR == APFloat::cmpLessThan;
712 break;
713 case BinaryOperator::GT:
714 Result = CR == APFloat::cmpGreaterThan;
715 break;
716 case BinaryOperator::LE:
717 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
718 break;
719 case BinaryOperator::GE:
720 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
721 break;
722 case BinaryOperator::EQ:
723 Result = CR == APFloat::cmpEqual;
724 break;
725 case BinaryOperator::NE:
726 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
727 break;
728 }
729
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000730 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
731 return true;
732 }
733
Anders Carlsson027f2882008-11-16 19:01:22 +0000734 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000735 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson027f2882008-11-16 19:01:22 +0000736 APValue LHSValue;
737 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
738 return false;
739
740 APValue RHSValue;
741 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
742 return false;
743
744 // FIXME: Is this correct? What if only one of the operands has a base?
745 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
746 return false;
747
748 const QualType Type = E->getLHS()->getType();
749 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
750
751 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
752 D /= Info.Ctx.getTypeSize(ElementType) / 8;
753
Anders Carlsson027f2882008-11-16 19:01:22 +0000754 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000755 Result = D;
Anders Carlsson027f2882008-11-16 19:01:22 +0000756 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
757
758 return true;
759 }
760 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000761 if (!LHSTy->isIntegralType() ||
762 !RHSTy->isIntegralType()) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000763 // We can't continue from here for non-integral types, and they
764 // could potentially confuse the following operations.
765 // FIXME: Deal with EQ and friends.
766 return false;
767 }
768
Anders Carlssond1aa5812008-07-08 14:35:21 +0000769 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssond1aa5812008-07-08 14:35:21 +0000770 llvm::APSInt RHS(32);
Chris Lattner40d2ae82008-11-12 07:04:29 +0000771 if (!Visit(E->getLHS())) {
Chris Lattner82437da2008-07-12 00:14:42 +0000772 return false; // error in subexpression.
Chris Lattner40d2ae82008-11-12 07:04:29 +0000773 }
Eli Friedman3e64dd72008-07-27 05:46:18 +0000774
Eli Friedman3e64dd72008-07-27 05:46:18 +0000775
776 // FIXME Maybe we want to succeed even where we can't evaluate the
777 // right side of LAnd/LOr?
778 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner82437da2008-07-12 00:14:42 +0000779 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000780 return false;
Eli Friedman14cc7542008-11-13 06:09:17 +0000781
Anders Carlssond1aa5812008-07-08 14:35:21 +0000782 switch (E->getOpcode()) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000783 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000784 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner82437da2008-07-12 00:14:42 +0000785 case BinaryOperator::Mul: Result *= RHS; return true;
786 case BinaryOperator::Add: Result += RHS; return true;
787 case BinaryOperator::Sub: Result -= RHS; return true;
788 case BinaryOperator::And: Result &= RHS; return true;
789 case BinaryOperator::Xor: Result ^= RHS; return true;
790 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000791 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000792 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000793 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000794 Result /= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000795 break;
Chris Lattner400d7402008-07-11 22:15:16 +0000796 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000797 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000798 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000799 Result %= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000800 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000801 case BinaryOperator::Shl:
Chris Lattner82437da2008-07-12 00:14:42 +0000802 // FIXME: Warn about out of range shift amounts!
Chris Lattnera42f09a2008-07-11 19:10:17 +0000803 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000804 break;
805 case BinaryOperator::Shr:
Chris Lattnera42f09a2008-07-11 19:10:17 +0000806 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000807 break;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000808
Chris Lattner045502c2008-07-11 19:29:32 +0000809 case BinaryOperator::LT:
810 Result = Result < RHS;
811 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
812 break;
813 case BinaryOperator::GT:
814 Result = Result > RHS;
815 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
816 break;
817 case BinaryOperator::LE:
818 Result = Result <= RHS;
819 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
820 break;
821 case BinaryOperator::GE:
822 Result = Result >= RHS;
823 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
824 break;
825 case BinaryOperator::EQ:
826 Result = Result == RHS;
827 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
828 break;
829 case BinaryOperator::NE:
830 Result = Result != RHS;
831 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
832 break;
Chris Lattner82437da2008-07-12 00:14:42 +0000833 case BinaryOperator::LAnd:
834 Result = Result != 0 && RHS != 0;
835 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
836 break;
837 case BinaryOperator::LOr:
838 Result = Result != 0 || RHS != 0;
839 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
840 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000841 }
Anders Carlssond1aa5812008-07-08 14:35:21 +0000842
843 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000844 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000845}
846
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000847bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes308de752008-11-16 22:06:39 +0000848 bool Cond;
849 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000850 return false;
851
Nuno Lopes308de752008-11-16 22:06:39 +0000852 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000853}
854
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000855unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000856 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
857
858 // __alignof__(void) = 1 as a gcc extension.
859 if (Ty->isVoidType())
860 return 1;
861
862 // GCC extension: alignof(function) = 4.
863 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
864 // attribute(align) directive.
865 if (Ty->isFunctionType())
866 return 4;
867
868 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty))
869 return GetAlignOfType(QualType(ASQT->getBaseType(), 0));
870
871 // alignof VLA/incomplete array.
872 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
873 return GetAlignOfType(VAT->getElementType());
874
875 // sizeof (objc class)?
876 if (isa<ObjCInterfaceType>(Ty))
877 return 1; // FIXME: This probably isn't right.
878
879 // Get information about the alignment.
880 unsigned CharSize = Info.Ctx.Target.getCharWidth();
881 return Info.Ctx.getTypeAlign(Ty) / CharSize;
882}
883
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000884unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
885 E = E->IgnoreParens();
886
887 // alignof decl is always accepted, even if it doesn't make sense: we default
888 // to 1 in those cases.
889 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
890 return Info.Ctx.getDeclAlign(DRE->getDecl());
891
892 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
893 return Info.Ctx.getDeclAlign(ME->getMemberDecl());
894
Chris Lattnere3f61e12009-01-24 21:09:06 +0000895 return GetAlignOfType(E->getType());
896}
897
898
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000899/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
900/// expression's type.
901bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
902 QualType DstTy = E->getType();
Chris Lattner265a0892008-07-11 21:24:13 +0000903 // Return the result in the right width.
904 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
905 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
906
Chris Lattnere3f61e12009-01-24 21:09:06 +0000907 // Handle alignof separately.
908 if (!E->isSizeOf()) {
909 if (E->isArgumentType())
910 Result = GetAlignOfType(E->getArgumentType());
911 else
912 Result = GetAlignOfExpr(E->getArgumentExpr());
913 return true;
914 }
915
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000916 QualType SrcTy = E->getTypeOfArgument();
917
Chris Lattner265a0892008-07-11 21:24:13 +0000918 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman7888b932008-11-12 09:44:48 +0000919 if (SrcTy->isVoidType()) {
Chris Lattner265a0892008-07-11 21:24:13 +0000920 Result = 1;
Eli Friedman7888b932008-11-12 09:44:48 +0000921 return true;
922 }
Chris Lattner265a0892008-07-11 21:24:13 +0000923
924 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere3f61e12009-01-24 21:09:06 +0000925 if (!SrcTy->isConstantSizeType())
Chris Lattner265a0892008-07-11 21:24:13 +0000926 return false;
Fariborz Jahanian2a032ec2009-01-16 01:42:12 +0000927
Chris Lattner265a0892008-07-11 21:24:13 +0000928 // GCC extension: sizeof(function) = 1.
929 if (SrcTy->isFunctionType()) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000930 Result = 1;
Chris Lattner265a0892008-07-11 21:24:13 +0000931 return true;
932 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000933
934 if (SrcTy->isObjCInterfaceType()) {
935 // Slightly unusual case: the size of an ObjC interface type is the
936 // size of the class. This code intentionally falls through to the normal
937 // case.
938 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
939 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
940 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
941 }
942
Chris Lattnere3f61e12009-01-24 21:09:06 +0000943 // Get information about the size.
Chris Lattner422373c2008-07-11 22:52:41 +0000944 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnere3f61e12009-01-24 21:09:06 +0000945 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000946 return true;
947}
948
Chris Lattnera42f09a2008-07-11 19:10:17 +0000949bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +0000950 // Special case unary operators that do not need their subexpression
951 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner400d7402008-07-11 22:15:16 +0000952 if (E->isOffsetOfOp()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000953 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner422373c2008-07-11 22:52:41 +0000954 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner400d7402008-07-11 22:15:16 +0000955 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
956 return true;
957 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000958
959 if (E->getOpcode() == UnaryOperator::LNot) {
960 // LNot's operand isn't necessarily an integer, so we handle it specially.
961 bool bres;
962 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
963 return false;
964 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
965 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
966 Result = !bres;
967 return true;
968 }
969
Chris Lattner422373c2008-07-11 22:52:41 +0000970 // Get the operand value into 'Result'.
971 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +0000972 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000973
Chris Lattner400d7402008-07-11 22:15:16 +0000974 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000975 default:
Chris Lattner400d7402008-07-11 22:15:16 +0000976 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
977 // See C99 6.6p3.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000978 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000979 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +0000980 // FIXME: Should extension allow i-c-e extension expressions in its scope?
981 // If so, we could clear the diagnostic ID.
Chris Lattner400d7402008-07-11 22:15:16 +0000982 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +0000983 // The result is always just the subexpr.
Chris Lattner400d7402008-07-11 22:15:16 +0000984 break;
985 case UnaryOperator::Minus:
986 Result = -Result;
987 break;
988 case UnaryOperator::Not:
989 Result = ~Result;
990 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000991 }
992
993 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000994 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000995}
996
Chris Lattnerff579ff2008-07-12 01:15:53 +0000997/// HandleCast - This is used to evaluate implicit or explicit casts where the
998/// result type is integer.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000999bool IntExprEvaluator::HandleCast(CastExpr *E) {
1000 Expr *SubExpr = E->getSubExpr();
1001 QualType DestType = E->getType();
1002
Chris Lattner2c99c712008-07-11 19:24:49 +00001003 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssond1aa5812008-07-08 14:35:21 +00001004
Eli Friedman7888b932008-11-12 09:44:48 +00001005 if (DestType->isBooleanType()) {
1006 bool BoolResult;
1007 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1008 return false;
1009 Result.zextOrTrunc(DestWidth);
1010 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1011 Result = BoolResult;
1012 return true;
1013 }
1014
Anders Carlssond1aa5812008-07-08 14:35:21 +00001015 // Handle simple integer->integer casts.
Eli Friedman14cc7542008-11-13 06:09:17 +00001016 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +00001017 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001018 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001019
1020 // Figure out if this is a truncate, extend or noop cast.
1021 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman7888b932008-11-12 09:44:48 +00001022 Result.extOrTrunc(DestWidth);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001023 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1024 return true;
1025 }
1026
1027 // FIXME: Clean this up!
1028 if (SubExpr->getType()->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +00001029 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +00001030 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001031 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001032
Anders Carlssond1aa5812008-07-08 14:35:21 +00001033 if (LV.getLValueBase())
Chris Lattnera42f09a2008-07-11 19:10:17 +00001034 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001035
Anders Carlsson8ab15c82008-07-08 16:49:00 +00001036 Result.extOrTrunc(DestWidth);
1037 Result = LV.getLValueOffset();
Chris Lattnerff579ff2008-07-12 01:15:53 +00001038 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1039 return true;
Anders Carlsson02a34c32008-07-08 14:30:00 +00001040 }
Eli Friedman7888b932008-11-12 09:44:48 +00001041
Chris Lattnerff579ff2008-07-12 01:15:53 +00001042 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001043 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001044
Eli Friedman2f445492008-08-22 00:06:13 +00001045 APFloat F(0.0);
1046 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001047 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001048
1049 // Determine whether we are converting to unsigned or signed.
1050 bool DestSigned = DestType->isSignedIntegerType();
1051
1052 // FIXME: Warning for overflow.
Dale Johannesen2461f612008-10-09 23:02:32 +00001053 uint64_t Space[4];
1054 bool ignored;
Eli Friedman2f445492008-08-22 00:06:13 +00001055 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001056 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001057 Result = llvm::APInt(DestWidth, 4, Space);
1058 Result.setIsUnsigned(!DestSigned);
Chris Lattnera42f09a2008-07-11 19:10:17 +00001059 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001060}
Anders Carlsson02a34c32008-07-08 14:30:00 +00001061
Chris Lattnera823ccf2008-07-11 18:11:29 +00001062//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +00001063// Float Evaluation
1064//===----------------------------------------------------------------------===//
1065
1066namespace {
1067class VISIBILITY_HIDDEN FloatExprEvaluator
1068 : public StmtVisitor<FloatExprEvaluator, bool> {
1069 EvalInfo &Info;
1070 APFloat &Result;
1071public:
1072 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1073 : Info(info), Result(result) {}
1074
1075 bool VisitStmt(Stmt *S) {
1076 return false;
1077 }
1078
1079 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner87293782008-10-06 05:28:25 +00001080 bool VisitCallExpr(const CallExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001081
Daniel Dunbar804ead02008-10-16 03:51:50 +00001082 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001083 bool VisitBinaryOperator(const BinaryOperator *E);
1084 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman7888b932008-11-12 09:44:48 +00001085 bool VisitCastExpr(CastExpr *E);
1086 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001087};
1088} // end anonymous namespace
1089
1090static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1091 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1092}
1093
Chris Lattner87293782008-10-06 05:28:25 +00001094bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner87293782008-10-06 05:28:25 +00001095 switch (E->isBuiltinCall()) {
Chris Lattner27cde262008-10-06 05:53:16 +00001096 default: return false;
Chris Lattner87293782008-10-06 05:28:25 +00001097 case Builtin::BI__builtin_huge_val:
1098 case Builtin::BI__builtin_huge_valf:
1099 case Builtin::BI__builtin_huge_vall:
1100 case Builtin::BI__builtin_inf:
1101 case Builtin::BI__builtin_inff:
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001102 case Builtin::BI__builtin_infl: {
1103 const llvm::fltSemantics &Sem =
1104 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner27cde262008-10-06 05:53:16 +00001105 Result = llvm::APFloat::getInf(Sem);
1106 return true;
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001107 }
Chris Lattner667e1ee2008-10-06 06:31:58 +00001108
1109 case Builtin::BI__builtin_nan:
1110 case Builtin::BI__builtin_nanf:
1111 case Builtin::BI__builtin_nanl:
1112 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1113 // can't constant fold it.
1114 if (const StringLiteral *S =
1115 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1116 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001117 const llvm::fltSemantics &Sem =
1118 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner667e1ee2008-10-06 06:31:58 +00001119 Result = llvm::APFloat::getNaN(Sem);
1120 return true;
1121 }
1122 }
1123 return false;
Daniel Dunbar804ead02008-10-16 03:51:50 +00001124
1125 case Builtin::BI__builtin_fabs:
1126 case Builtin::BI__builtin_fabsf:
1127 case Builtin::BI__builtin_fabsl:
1128 if (!EvaluateFloat(E->getArg(0), Result, Info))
1129 return false;
1130
1131 if (Result.isNegative())
1132 Result.changeSign();
1133 return true;
1134
1135 case Builtin::BI__builtin_copysign:
1136 case Builtin::BI__builtin_copysignf:
1137 case Builtin::BI__builtin_copysignl: {
1138 APFloat RHS(0.);
1139 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1140 !EvaluateFloat(E->getArg(1), RHS, Info))
1141 return false;
1142 Result.copySign(RHS);
1143 return true;
1144 }
Chris Lattner87293782008-10-06 05:28:25 +00001145 }
1146}
1147
Daniel Dunbar804ead02008-10-16 03:51:50 +00001148bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes1cea4f42008-11-19 17:44:31 +00001149 if (E->getOpcode() == UnaryOperator::Deref)
1150 return false;
1151
Daniel Dunbar804ead02008-10-16 03:51:50 +00001152 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1153 return false;
1154
1155 switch (E->getOpcode()) {
1156 default: return false;
1157 case UnaryOperator::Plus:
1158 return true;
1159 case UnaryOperator::Minus:
1160 Result.changeSign();
1161 return true;
1162 }
1163}
Chris Lattner87293782008-10-06 05:28:25 +00001164
Eli Friedman2f445492008-08-22 00:06:13 +00001165bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1166 // FIXME: Diagnostics? I really don't understand how the warnings
1167 // and errors are supposed to work.
Daniel Dunbar804ead02008-10-16 03:51:50 +00001168 APFloat RHS(0.0);
Eli Friedman2f445492008-08-22 00:06:13 +00001169 if (!EvaluateFloat(E->getLHS(), Result, Info))
1170 return false;
1171 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1172 return false;
1173
1174 switch (E->getOpcode()) {
1175 default: return false;
1176 case BinaryOperator::Mul:
1177 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1178 return true;
1179 case BinaryOperator::Add:
1180 Result.add(RHS, APFloat::rmNearestTiesToEven);
1181 return true;
1182 case BinaryOperator::Sub:
1183 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1184 return true;
1185 case BinaryOperator::Div:
1186 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1187 return true;
1188 case BinaryOperator::Rem:
1189 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1190 return true;
1191 }
1192}
1193
1194bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1195 Result = E->getValue();
1196 return true;
1197}
1198
Eli Friedman7888b932008-11-12 09:44:48 +00001199bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1200 Expr* SubExpr = E->getSubExpr();
Nate Begemand6d2f772009-01-18 03:20:47 +00001201
Eli Friedman7888b932008-11-12 09:44:48 +00001202 const llvm::fltSemantics& destSemantics =
1203 Info.Ctx.getFloatTypeSemantics(E->getType());
1204 if (SubExpr->getType()->isIntegralType()) {
1205 APSInt IntResult;
1206 if (!EvaluateInteger(E, IntResult, Info))
1207 return false;
1208 Result = APFloat(destSemantics, 1);
1209 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1210 APFloat::rmNearestTiesToEven);
1211 return true;
1212 }
1213 if (SubExpr->getType()->isRealFloatingType()) {
1214 if (!Visit(SubExpr))
1215 return false;
1216 bool ignored;
1217 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1218 return true;
1219 }
1220
1221 return false;
1222}
1223
1224bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1225 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1226 return true;
1227}
1228
Eli Friedman2f445492008-08-22 00:06:13 +00001229//===----------------------------------------------------------------------===//
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001230// Complex Evaluation (for float and integer)
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001231//===----------------------------------------------------------------------===//
1232
1233namespace {
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001234class VISIBILITY_HIDDEN ComplexExprEvaluator
1235 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001236 EvalInfo &Info;
1237
1238public:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001239 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001240
1241 //===--------------------------------------------------------------------===//
1242 // Visitor Methods
1243 //===--------------------------------------------------------------------===//
1244
1245 APValue VisitStmt(Stmt *S) {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001246 return APValue();
1247 }
1248
1249 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1250
1251 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001252 Expr* SubExpr = E->getSubExpr();
1253
1254 if (SubExpr->getType()->isRealFloatingType()) {
1255 APFloat Result(0.0);
1256
1257 if (!EvaluateFloat(SubExpr, Result, Info))
1258 return APValue();
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001259
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001260 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001261 Result);
1262 } else {
1263 assert(SubExpr->getType()->isIntegerType() &&
1264 "Unexpected imaginary literal.");
1265
1266 llvm::APSInt Result;
1267 if (!EvaluateInteger(SubExpr, Result, Info))
1268 return APValue();
1269
1270 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1271 Zero = 0;
1272 return APValue(Zero, Result);
1273 }
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001274 }
1275
Anders Carlssonad2794c2008-11-16 21:51:21 +00001276 APValue VisitCastExpr(CastExpr *E) {
1277 Expr* SubExpr = E->getSubExpr();
1278
1279 if (SubExpr->getType()->isRealFloatingType()) {
1280 APFloat Result(0.0);
1281
1282 if (!EvaluateFloat(SubExpr, Result, Info))
1283 return APValue();
1284
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001285 return APValue(Result,
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001286 APFloat(Result.getSemantics(), APFloat::fcZero, false));
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001287 } else if (SubExpr->getType()->isIntegerType()) {
1288 APSInt Result;
1289
1290 if (!EvaluateInteger(SubExpr, Result, Info))
1291 return APValue();
1292
1293 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1294 Zero = 0;
1295 return APValue(Result, Zero);
Anders Carlssonad2794c2008-11-16 21:51:21 +00001296 }
1297
1298 // FIXME: Handle more casts.
1299 return APValue();
1300 }
1301
1302 APValue VisitBinaryOperator(const BinaryOperator *E);
1303
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001304};
1305} // end anonymous namespace
1306
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001307static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001308{
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001309 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1310 assert((!Result.isComplexFloat() ||
1311 (&Result.getComplexFloatReal().getSemantics() ==
1312 &Result.getComplexFloatImag().getSemantics())) &&
1313 "Invalid complex evaluation.");
1314 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001315}
1316
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001317APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonad2794c2008-11-16 21:51:21 +00001318{
1319 APValue Result, RHS;
1320
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001321 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonad2794c2008-11-16 21:51:21 +00001322 return APValue();
1323
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001324 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonad2794c2008-11-16 21:51:21 +00001325 return APValue();
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001326
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001327 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1328 "Invalid operands to binary operator.");
Anders Carlssonad2794c2008-11-16 21:51:21 +00001329 switch (E->getOpcode()) {
1330 default: return APValue();
1331 case BinaryOperator::Add:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001332 if (Result.isComplexFloat()) {
1333 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1334 APFloat::rmNearestTiesToEven);
1335 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1336 APFloat::rmNearestTiesToEven);
1337 } else {
1338 Result.getComplexIntReal() += RHS.getComplexIntReal();
1339 Result.getComplexIntImag() += RHS.getComplexIntImag();
1340 }
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001341 break;
Anders Carlssonad2794c2008-11-16 21:51:21 +00001342 case BinaryOperator::Sub:
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001343 if (Result.isComplexFloat()) {
1344 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1345 APFloat::rmNearestTiesToEven);
1346 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1347 APFloat::rmNearestTiesToEven);
1348 } else {
1349 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1350 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1351 }
Daniel Dunbar00a9aad2009-01-29 01:32:56 +00001352 break;
1353 case BinaryOperator::Mul:
1354 if (Result.isComplexFloat()) {
1355 APValue LHS = Result;
1356 APFloat &LHS_r = LHS.getComplexFloatReal();
1357 APFloat &LHS_i = LHS.getComplexFloatImag();
1358 APFloat &RHS_r = RHS.getComplexFloatReal();
1359 APFloat &RHS_i = RHS.getComplexFloatImag();
1360
1361 APFloat Tmp = LHS_r;
1362 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1363 Result.getComplexFloatReal() = Tmp;
1364 Tmp = LHS_i;
1365 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1366 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1367
1368 Tmp = LHS_r;
1369 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1370 Result.getComplexFloatImag() = Tmp;
1371 Tmp = LHS_i;
1372 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1373 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1374 } else {
1375 APValue LHS = Result;
1376 Result.getComplexIntReal() =
1377 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1378 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1379 Result.getComplexIntImag() =
1380 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1381 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1382 }
1383 break;
Anders Carlssonad2794c2008-11-16 21:51:21 +00001384 }
1385
1386 return Result;
1387}
1388
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001389//===----------------------------------------------------------------------===//
Chris Lattneref069662008-11-16 21:24:15 +00001390// Top level Expr::Evaluate method.
Chris Lattnera823ccf2008-07-11 18:11:29 +00001391//===----------------------------------------------------------------------===//
1392
Chris Lattneref069662008-11-16 21:24:15 +00001393/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner87293782008-10-06 05:28:25 +00001394/// any crazy technique (that has nothing to do with language standards) that
1395/// we want to. If this function returns true, it returns the folded constant
1396/// in Result.
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001397bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1398 EvalInfo Info(Ctx, Result);
Anders Carlssondd8d41f2008-11-30 16:38:33 +00001399
Nate Begemand6d2f772009-01-18 03:20:47 +00001400 if (getType()->isVectorType()) {
1401 if (!EvaluateVector(this, Result.Val, Info))
1402 return false;
1403 } else if (getType()->isIntegerType()) {
Eli Friedman2f445492008-08-22 00:06:13 +00001404 llvm::APSInt sInt(32);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001405 if (!EvaluateInteger(this, sInt, Info))
1406 return false;
1407
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001408 Result.Val = APValue(sInt);
Eli Friedman2f445492008-08-22 00:06:13 +00001409 } else if (getType()->isPointerType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001410 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001411 return false;
Eli Friedman2f445492008-08-22 00:06:13 +00001412 } else if (getType()->isRealFloatingType()) {
1413 llvm::APFloat f(0.0);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001414 if (!EvaluateFloat(this, f, Info))
1415 return false;
1416
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001417 Result.Val = APValue(f);
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001418 } else if (getType()->isAnyComplexType()) {
1419 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001420 return false;
Daniel Dunbaraf781bb2009-01-28 22:24:07 +00001421 } else
Anders Carlssoncb6a2e82008-11-22 22:56:32 +00001422 return false;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001423
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001424 return true;
1425}
1426
Chris Lattneref069662008-11-16 21:24:15 +00001427/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001428/// folded, but discard the result.
1429bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson197f6f72008-12-01 06:44:05 +00001430 EvalResult Result;
1431 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001432}
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001433
1434APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson8c3de802008-12-19 20:58:05 +00001435 EvalResult EvalResult;
1436 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarddebeca2009-01-15 18:32:35 +00001437 Result = Result;
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001438 assert(Result && "Could not evaluate expression");
Anders Carlsson8c3de802008-12-19 20:58:05 +00001439 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001440
Anders Carlsson8c3de802008-12-19 20:58:05 +00001441 return EvalResult.Val.getInt();
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001442}