blob: 76bdaa2c8f8389cb2551818c07fdb91a9e5af882 [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 Lattner82437da2008-07-12 00:14:42 +000018#include "clang/Basic/Diagnostic.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);
Anders Carlssonf1bb2962008-11-16 20:27:53 +000058static bool EvaluateComplexFloat(const Expr *E, APValue &Result,
59 EvalInfo &Info);
Chris Lattnera823ccf2008-07-11 18:11:29 +000060
61//===----------------------------------------------------------------------===//
Eli Friedman7888b932008-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 Dunbarff59ed82008-11-12 21:52:46 +0000103#if 0
Eli Friedman7888b932008-11-12 09:44:48 +0000104 // FIXME: Remove this when we support more expressions.
105 printf("Unhandled pointer statement\n");
106 S->dump();
Daniel Dunbarff59ed82008-11-12 21:52:46 +0000107#endif
Eli Friedman7888b932008-11-12 09:44:48 +0000108 return APValue();
109 }
110
111 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssone284ebe2008-11-24 04:41:22 +0000112 APValue VisitDeclRefExpr(DeclRefExpr *E);
Eli Friedman7888b932008-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 Carlsson027f2882008-11-16 19:01:22 +0000117 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedman7888b932008-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 Carlssone284ebe2008-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 Friedman7888b932008-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 Gregor82d44772008-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 Friedman7888b932008-11-12 09:44:48 +0000160
161 // FIXME: This is linear time.
Douglas Gregor8acb7272008-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 Friedman7888b932008-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 Carlsson027f2882008-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 Friedman7888b932008-11-12 09:44:48 +0000194
195//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000196// Pointer Evaluation
197//===----------------------------------------------------------------------===//
198
Anders Carlssoncad17b52008-07-08 05:13:58 +0000199namespace {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000200class VISIBILITY_HIDDEN PointerExprEvaluator
201 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner422373c2008-07-11 22:52:41 +0000202 EvalInfo &Info;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000203public:
Anders Carlsson02a34c32008-07-08 14:30:00 +0000204
Chris Lattner422373c2008-07-11 22:52:41 +0000205 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000206
Anders Carlsson02a34c32008-07-08 14:30:00 +0000207 APValue VisitStmt(Stmt *S) {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000208 return APValue();
209 }
210
211 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
212
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000213 APValue VisitBinaryOperator(const BinaryOperator *E);
214 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman7888b932008-11-12 09:44:48 +0000215 APValue VisitUnaryOperator(const UnaryOperator *E);
216 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
217 { return APValue(E, 0); }
Eli Friedmanf1941132009-01-25 01:21:06 +0000218 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
219 { return APValue(E, 0); }
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 Friedman7888b932008-11-12 09:44:48 +0000309APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
310 bool BoolResult;
311 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
312 return APValue();
313
314 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
315
316 APValue Result;
317 if (EvaluatePointer(EvalExpr, Result, Info))
318 return Result;
319 return APValue();
320}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000321
322//===----------------------------------------------------------------------===//
Nate Begemand6d2f772009-01-18 03:20:47 +0000323// Vector Evaluation
324//===----------------------------------------------------------------------===//
325
326namespace {
327 class VISIBILITY_HIDDEN VectorExprEvaluator
328 : public StmtVisitor<VectorExprEvaluator, APValue> {
329 EvalInfo &Info;
330 public:
331
332 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
333
334 APValue VisitStmt(Stmt *S) {
335 return APValue();
336 }
337
338 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
339 APValue VisitCastExpr(const CastExpr* E);
340 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
341 APValue VisitInitListExpr(const InitListExpr *E);
342 };
343} // end anonymous namespace
344
345static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
346 if (!E->getType()->isVectorType())
347 return false;
348 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
349 return !Result.isUninit();
350}
351
352APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
353 const Expr* SE = E->getSubExpr();
354
355 // Check for vector->vector bitcast.
356 if (SE->getType()->isVectorType())
357 return this->Visit(const_cast<Expr*>(SE));
358
359 return APValue();
360}
361
362APValue
363VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
364 return this->Visit(const_cast<Expr*>(E->getInitializer()));
365}
366
367APValue
368VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
369 const VectorType *VT = E->getType()->getAsVectorType();
370 unsigned NumInits = E->getNumInits();
371
372 if (!VT || VT->getNumElements() != NumInits)
373 return APValue();
374
375 QualType EltTy = VT->getElementType();
376 llvm::SmallVector<APValue, 4> Elements;
377
378 for (unsigned i = 0; i < NumInits; i++) {
379 if (EltTy->isIntegerType()) {
380 llvm::APSInt sInt(32);
381 if (!EvaluateInteger(E->getInit(i), sInt, Info))
382 return APValue();
383 Elements.push_back(APValue(sInt));
384 } else {
385 llvm::APFloat f(0.0);
386 if (!EvaluateFloat(E->getInit(i), f, Info))
387 return APValue();
388 Elements.push_back(APValue(f));
389 }
390 }
391 return APValue(&Elements[0], Elements.size());
392}
393
394//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000395// Integer Evaluation
396//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000397
398namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000399class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000400 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000401 EvalInfo &Info;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000402 APSInt &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000403public:
Chris Lattner422373c2008-07-11 22:52:41 +0000404 IntExprEvaluator(EvalInfo &info, APSInt &result)
405 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000406
Chris Lattner2c99c712008-07-11 19:24:49 +0000407 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner82437da2008-07-12 00:14:42 +0000408 return (unsigned)Info.Ctx.getIntWidth(T);
409 }
410
Anders Carlssonfa76d822008-11-30 18:14:57 +0000411 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000412 Info.EvalResult.DiagLoc = L;
413 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000414 Info.EvalResult.DiagExpr = E;
Chris Lattner82437da2008-07-12 00:14:42 +0000415 return true; // still a constant.
416 }
417
Anders Carlssonfa76d822008-11-30 18:14:57 +0000418 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner82437da2008-07-12 00:14:42 +0000419 // If this is in an unevaluated portion of the subexpression, ignore the
420 // error.
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000421 if (Info.ShortCircuit) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000422 // If error is ignored because the value isn't evaluated, get the real
423 // type at least to prevent errors downstream.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000424 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
425 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner82437da2008-07-12 00:14:42 +0000426 return true;
Chris Lattner438f3b12008-11-12 07:43:42 +0000427 }
Chris Lattner82437da2008-07-12 00:14:42 +0000428
Chris Lattner438f3b12008-11-12 07:43:42 +0000429 // Take the first error.
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000430 if (Info.EvalResult.Diag == 0) {
431 Info.EvalResult.DiagLoc = L;
432 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000433 Info.EvalResult.DiagExpr = E;
Chris Lattner438f3b12008-11-12 07:43:42 +0000434 }
Chris Lattner82437da2008-07-12 00:14:42 +0000435 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000436 }
437
Anders Carlssoncad17b52008-07-08 05:13:58 +0000438 //===--------------------------------------------------------------------===//
439 // Visitor Methods
440 //===--------------------------------------------------------------------===//
Chris Lattner438f3b12008-11-12 07:43:42 +0000441
442 bool VisitStmt(Stmt *) {
443 assert(0 && "This should be called on integers, stmts are not integers");
444 return false;
445 }
Chris Lattner2c99c712008-07-11 19:24:49 +0000446
Chris Lattner438f3b12008-11-12 07:43:42 +0000447 bool VisitExpr(Expr *E) {
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000448 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000449 }
450
Chris Lattnera42f09a2008-07-11 19:10:17 +0000451 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000452
Chris Lattner15e59112008-07-12 00:38:25 +0000453 bool VisitIntegerLiteral(const IntegerLiteral *E) {
454 Result = E->getValue();
455 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
456 return true;
457 }
458 bool VisitCharacterLiteral(const CharacterLiteral *E) {
459 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
460 Result = E->getValue();
461 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
462 return true;
463 }
464 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
465 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000466 // Per gcc docs "this built-in function ignores top level
467 // qualifiers". We need to use the canonical version to properly
468 // be able to strip CRV qualifiers from the type.
469 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
470 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
471 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
472 T1.getUnqualifiedType());
Chris Lattner15e59112008-07-12 00:38:25 +0000473 return true;
474 }
475 bool VisitDeclRefExpr(const DeclRefExpr *E);
476 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000477 bool VisitBinaryOperator(const BinaryOperator *E);
478 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000479 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000480
Chris Lattnerff579ff2008-07-12 01:15:53 +0000481 bool VisitCastExpr(CastExpr* E) {
Anders Carlssonfa76d822008-11-30 18:14:57 +0000482 return HandleCast(E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000483 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000484 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
485
Anders Carlsson027f2882008-11-16 19:01:22 +0000486 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000487 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson027f2882008-11-16 19:01:22 +0000488 Result = E->getValue();
489 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
490 return true;
491 }
492
Anders Carlsson774f9c72008-12-21 22:39:40 +0000493 bool VisitGNUNullExpr(const GNUNullExpr *E) {
494 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
495 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
496 return true;
497 }
498
Anders Carlsson027f2882008-11-16 19:01:22 +0000499 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
500 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
501 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
502 return true;
503 }
504
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000505 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
506 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
507 Result = E->Evaluate();
508 return true;
509 }
510
Chris Lattner265a0892008-07-11 21:24:13 +0000511private:
Anders Carlssonfa76d822008-11-30 18:14:57 +0000512 bool HandleCast(CastExpr* E);
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000513 unsigned GetAlignOfExpr(const Expr *E);
514 unsigned GetAlignOfType(QualType T);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000515};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000516} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000517
Chris Lattner422373c2008-07-11 22:52:41 +0000518static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
519 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000520}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000521
Chris Lattner15e59112008-07-12 00:38:25 +0000522bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
523 // Enums are integer constant exprs.
524 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
525 Result = D->getInitVal();
Eli Friedman8b10a232008-12-08 02:21:03 +0000526 // FIXME: This is an ugly hack around the fact that enums don't set their
527 // signedness consistently; see PR3173
528 Result.setIsUnsigned(!E->getType()->isSignedIntegerType());
Chris Lattner15e59112008-07-12 00:38:25 +0000529 return true;
530 }
531
532 // Otherwise, random variable references are not constants.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000533 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000534}
535
Chris Lattner1eee9402008-10-06 06:40:35 +0000536/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
537/// as GCC.
538static int EvaluateBuiltinClassifyType(const CallExpr *E) {
539 // The following enum mimics the values returned by GCC.
540 enum gcc_type_class {
541 no_type_class = -1,
542 void_type_class, integer_type_class, char_type_class,
543 enumeral_type_class, boolean_type_class,
544 pointer_type_class, reference_type_class, offset_type_class,
545 real_type_class, complex_type_class,
546 function_type_class, method_type_class,
547 record_type_class, union_type_class,
548 array_type_class, string_type_class,
549 lang_type_class
550 };
551
552 // If no argument was supplied, default to "no_type_class". This isn't
553 // ideal, however it is what gcc does.
554 if (E->getNumArgs() == 0)
555 return no_type_class;
556
557 QualType ArgTy = E->getArg(0)->getType();
558 if (ArgTy->isVoidType())
559 return void_type_class;
560 else if (ArgTy->isEnumeralType())
561 return enumeral_type_class;
562 else if (ArgTy->isBooleanType())
563 return boolean_type_class;
564 else if (ArgTy->isCharType())
565 return string_type_class; // gcc doesn't appear to use char_type_class
566 else if (ArgTy->isIntegerType())
567 return integer_type_class;
568 else if (ArgTy->isPointerType())
569 return pointer_type_class;
570 else if (ArgTy->isReferenceType())
571 return reference_type_class;
572 else if (ArgTy->isRealType())
573 return real_type_class;
574 else if (ArgTy->isComplexType())
575 return complex_type_class;
576 else if (ArgTy->isFunctionType())
577 return function_type_class;
578 else if (ArgTy->isStructureType())
579 return record_type_class;
580 else if (ArgTy->isUnionType())
581 return union_type_class;
582 else if (ArgTy->isArrayType())
583 return array_type_class;
584 else if (ArgTy->isUnionType())
585 return union_type_class;
586 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
587 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
588 return -1;
589}
590
Chris Lattner15e59112008-07-12 00:38:25 +0000591bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
592 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner15e59112008-07-12 00:38:25 +0000593
Chris Lattner87293782008-10-06 05:28:25 +0000594 switch (E->isBuiltinCall()) {
595 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000596 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner87293782008-10-06 05:28:25 +0000597 case Builtin::BI__builtin_classify_type:
Chris Lattner1eee9402008-10-06 06:40:35 +0000598 Result.setIsSigned(true);
599 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner87293782008-10-06 05:28:25 +0000600 return true;
601
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000602 case Builtin::BI__builtin_constant_p:
Chris Lattner87293782008-10-06 05:28:25 +0000603 // __builtin_constant_p always has one operand: it returns true if that
604 // operand can be folded, false otherwise.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000605 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner87293782008-10-06 05:28:25 +0000606 return true;
607 }
Chris Lattner15e59112008-07-12 00:38:25 +0000608}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000609
Chris Lattnera42f09a2008-07-11 19:10:17 +0000610bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000611 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000612 if (!Visit(E->getRHS()))
613 return false;
Anders Carlsson197f6f72008-12-01 06:44:05 +0000614
615 if (!Info.ShortCircuit) {
616 // If we can't evaluate the LHS, it must be because it has
617 // side effects.
618 if (!E->getLHS()->isEvaluatable(Info.Ctx))
619 Info.EvalResult.HasSideEffects = true;
620
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000621 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson197f6f72008-12-01 06:44:05 +0000622 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000623
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000624 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000625 }
626
627 if (E->isLogicalOp()) {
628 // These need to be handled specially because the operands aren't
629 // necessarily integral
Anders Carlsson501da1f2008-11-30 16:51:17 +0000630 bool lhsResult, rhsResult;
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000631
Anders Carlsson501da1f2008-11-30 16:51:17 +0000632 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000633 // We were able to evaluate the LHS, see if we can get away with not
634 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlsson501da1f2008-11-30 16:51:17 +0000635 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
636 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000637 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
638 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000639 Result = lhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000640
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000641 Info.ShortCircuit++;
Anders Carlsson501da1f2008-11-30 16:51:17 +0000642 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000643 Info.ShortCircuit--;
644
Anders Carlsson501da1f2008-11-30 16:51:17 +0000645 if (rhsEvaluated)
646 return true;
647
648 // FIXME: Return an extension warning saying that the RHS could not be
649 // evaluated.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000650 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000651 }
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000652
Anders Carlsson501da1f2008-11-30 16:51:17 +0000653 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000654 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
655 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
656 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlsson501da1f2008-11-30 16:51:17 +0000657 Result = lhsResult || rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000658 else
Anders Carlsson501da1f2008-11-30 16:51:17 +0000659 Result = lhsResult && rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000660 return true;
661 }
662 } else {
Anders Carlsson501da1f2008-11-30 16:51:17 +0000663 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000664 // We can't evaluate the LHS; however, sometimes the result
665 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlsson501da1f2008-11-30 16:51:17 +0000666 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
667 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000668 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
669 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000670 Result = rhsResult;
671
672 // Since we werent able to evaluate the left hand side, it
673 // must have had side effects.
674 Info.EvalResult.HasSideEffects = true;
675
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000676 return true;
677 }
678 }
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000679 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000680
Eli Friedman14cc7542008-11-13 06:09:17 +0000681 return false;
682 }
683
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000684 QualType LHSTy = E->getLHS()->getType();
685 QualType RHSTy = E->getRHS()->getType();
686
687 if (LHSTy->isRealFloatingType() &&
688 RHSTy->isRealFloatingType()) {
689 APFloat RHS(0.0), LHS(0.0);
690
691 if (!EvaluateFloat(E->getRHS(), RHS, Info))
692 return false;
693
694 if (!EvaluateFloat(E->getLHS(), LHS, Info))
695 return false;
696
697 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000698
699 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
700
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000701 switch (E->getOpcode()) {
702 default:
703 assert(0 && "Invalid binary operator!");
704 case BinaryOperator::LT:
705 Result = CR == APFloat::cmpLessThan;
706 break;
707 case BinaryOperator::GT:
708 Result = CR == APFloat::cmpGreaterThan;
709 break;
710 case BinaryOperator::LE:
711 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
712 break;
713 case BinaryOperator::GE:
714 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
715 break;
716 case BinaryOperator::EQ:
717 Result = CR == APFloat::cmpEqual;
718 break;
719 case BinaryOperator::NE:
720 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
721 break;
722 }
723
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000724 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
725 return true;
726 }
727
Anders Carlsson027f2882008-11-16 19:01:22 +0000728 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000729 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson027f2882008-11-16 19:01:22 +0000730 APValue LHSValue;
731 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
732 return false;
733
734 APValue RHSValue;
735 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
736 return false;
737
738 // FIXME: Is this correct? What if only one of the operands has a base?
739 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
740 return false;
741
742 const QualType Type = E->getLHS()->getType();
743 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
744
745 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
746 D /= Info.Ctx.getTypeSize(ElementType) / 8;
747
Anders Carlsson027f2882008-11-16 19:01:22 +0000748 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000749 Result = D;
Anders Carlsson027f2882008-11-16 19:01:22 +0000750 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
751
752 return true;
753 }
754 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000755 if (!LHSTy->isIntegralType() ||
756 !RHSTy->isIntegralType()) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000757 // We can't continue from here for non-integral types, and they
758 // could potentially confuse the following operations.
759 // FIXME: Deal with EQ and friends.
760 return false;
761 }
762
Anders Carlssond1aa5812008-07-08 14:35:21 +0000763 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssond1aa5812008-07-08 14:35:21 +0000764 llvm::APSInt RHS(32);
Chris Lattner40d2ae82008-11-12 07:04:29 +0000765 if (!Visit(E->getLHS())) {
Chris Lattner82437da2008-07-12 00:14:42 +0000766 return false; // error in subexpression.
Chris Lattner40d2ae82008-11-12 07:04:29 +0000767 }
Eli Friedman3e64dd72008-07-27 05:46:18 +0000768
Eli Friedman3e64dd72008-07-27 05:46:18 +0000769
770 // FIXME Maybe we want to succeed even where we can't evaluate the
771 // right side of LAnd/LOr?
772 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner82437da2008-07-12 00:14:42 +0000773 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000774 return false;
Eli Friedman14cc7542008-11-13 06:09:17 +0000775
Anders Carlssond1aa5812008-07-08 14:35:21 +0000776 switch (E->getOpcode()) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000777 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000778 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner82437da2008-07-12 00:14:42 +0000779 case BinaryOperator::Mul: Result *= RHS; return true;
780 case BinaryOperator::Add: Result += RHS; return true;
781 case BinaryOperator::Sub: Result -= RHS; return true;
782 case BinaryOperator::And: Result &= RHS; return true;
783 case BinaryOperator::Xor: Result ^= RHS; return true;
784 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000785 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000786 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000787 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000788 Result /= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000789 break;
Chris Lattner400d7402008-07-11 22:15:16 +0000790 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000791 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000792 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000793 Result %= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000794 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000795 case BinaryOperator::Shl:
Chris Lattner82437da2008-07-12 00:14:42 +0000796 // FIXME: Warn about out of range shift amounts!
Chris Lattnera42f09a2008-07-11 19:10:17 +0000797 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000798 break;
799 case BinaryOperator::Shr:
Chris Lattnera42f09a2008-07-11 19:10:17 +0000800 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000801 break;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000802
Chris Lattner045502c2008-07-11 19:29:32 +0000803 case BinaryOperator::LT:
804 Result = Result < RHS;
805 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
806 break;
807 case BinaryOperator::GT:
808 Result = Result > RHS;
809 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
810 break;
811 case BinaryOperator::LE:
812 Result = Result <= RHS;
813 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
814 break;
815 case BinaryOperator::GE:
816 Result = Result >= RHS;
817 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
818 break;
819 case BinaryOperator::EQ:
820 Result = Result == RHS;
821 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
822 break;
823 case BinaryOperator::NE:
824 Result = Result != RHS;
825 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
826 break;
Chris Lattner82437da2008-07-12 00:14:42 +0000827 case BinaryOperator::LAnd:
828 Result = Result != 0 && RHS != 0;
829 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
830 break;
831 case BinaryOperator::LOr:
832 Result = Result != 0 || RHS != 0;
833 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
834 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000835 }
Anders Carlssond1aa5812008-07-08 14:35:21 +0000836
837 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000838 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000839}
840
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000841bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes308de752008-11-16 22:06:39 +0000842 bool Cond;
843 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000844 return false;
845
Nuno Lopes308de752008-11-16 22:06:39 +0000846 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000847}
848
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000849unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000850 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
851
852 // __alignof__(void) = 1 as a gcc extension.
853 if (Ty->isVoidType())
854 return 1;
855
856 // GCC extension: alignof(function) = 4.
857 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
858 // attribute(align) directive.
859 if (Ty->isFunctionType())
860 return 4;
861
862 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty))
863 return GetAlignOfType(QualType(ASQT->getBaseType(), 0));
864
865 // alignof VLA/incomplete array.
866 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
867 return GetAlignOfType(VAT->getElementType());
868
869 // sizeof (objc class)?
870 if (isa<ObjCInterfaceType>(Ty))
871 return 1; // FIXME: This probably isn't right.
872
873 // Get information about the alignment.
874 unsigned CharSize = Info.Ctx.Target.getCharWidth();
875 return Info.Ctx.getTypeAlign(Ty) / CharSize;
876}
877
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000878unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
879 E = E->IgnoreParens();
880
881 // alignof decl is always accepted, even if it doesn't make sense: we default
882 // to 1 in those cases.
883 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
884 return Info.Ctx.getDeclAlign(DRE->getDecl());
885
886 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
887 return Info.Ctx.getDeclAlign(ME->getMemberDecl());
888
Chris Lattnere3f61e12009-01-24 21:09:06 +0000889 return GetAlignOfType(E->getType());
890}
891
892
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000893/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
894/// expression's type.
895bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
896 QualType DstTy = E->getType();
Chris Lattner265a0892008-07-11 21:24:13 +0000897 // Return the result in the right width.
898 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
899 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
900
Chris Lattnere3f61e12009-01-24 21:09:06 +0000901 // Handle alignof separately.
902 if (!E->isSizeOf()) {
903 if (E->isArgumentType())
904 Result = GetAlignOfType(E->getArgumentType());
905 else
906 Result = GetAlignOfExpr(E->getArgumentExpr());
907 return true;
908 }
909
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000910 QualType SrcTy = E->getTypeOfArgument();
911
Chris Lattner265a0892008-07-11 21:24:13 +0000912 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman7888b932008-11-12 09:44:48 +0000913 if (SrcTy->isVoidType()) {
Chris Lattner265a0892008-07-11 21:24:13 +0000914 Result = 1;
Eli Friedman7888b932008-11-12 09:44:48 +0000915 return true;
916 }
Chris Lattner265a0892008-07-11 21:24:13 +0000917
918 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere3f61e12009-01-24 21:09:06 +0000919 if (!SrcTy->isConstantSizeType())
Chris Lattner265a0892008-07-11 21:24:13 +0000920 return false;
Fariborz Jahanian2a032ec2009-01-16 01:42:12 +0000921
Chris Lattner265a0892008-07-11 21:24:13 +0000922 // GCC extension: sizeof(function) = 1.
923 if (SrcTy->isFunctionType()) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000924 Result = 1;
Chris Lattner265a0892008-07-11 21:24:13 +0000925 return true;
926 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000927
928 if (SrcTy->isObjCInterfaceType()) {
929 // Slightly unusual case: the size of an ObjC interface type is the
930 // size of the class. This code intentionally falls through to the normal
931 // case.
932 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
933 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
934 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
935 }
936
Chris Lattnere3f61e12009-01-24 21:09:06 +0000937 // Get information about the size.
Chris Lattner422373c2008-07-11 22:52:41 +0000938 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnere3f61e12009-01-24 21:09:06 +0000939 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000940 return true;
941}
942
Chris Lattnera42f09a2008-07-11 19:10:17 +0000943bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +0000944 // Special case unary operators that do not need their subexpression
945 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner400d7402008-07-11 22:15:16 +0000946 if (E->isOffsetOfOp()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000947 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner422373c2008-07-11 22:52:41 +0000948 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner400d7402008-07-11 22:15:16 +0000949 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
950 return true;
951 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000952
953 if (E->getOpcode() == UnaryOperator::LNot) {
954 // LNot's operand isn't necessarily an integer, so we handle it specially.
955 bool bres;
956 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
957 return false;
958 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
959 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
960 Result = !bres;
961 return true;
962 }
963
Chris Lattner422373c2008-07-11 22:52:41 +0000964 // Get the operand value into 'Result'.
965 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +0000966 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000967
Chris Lattner400d7402008-07-11 22:15:16 +0000968 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000969 default:
Chris Lattner400d7402008-07-11 22:15:16 +0000970 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
971 // See C99 6.6p3.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000972 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000973 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +0000974 // FIXME: Should extension allow i-c-e extension expressions in its scope?
975 // If so, we could clear the diagnostic ID.
Chris Lattner400d7402008-07-11 22:15:16 +0000976 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +0000977 // The result is always just the subexpr.
Chris Lattner400d7402008-07-11 22:15:16 +0000978 break;
979 case UnaryOperator::Minus:
980 Result = -Result;
981 break;
982 case UnaryOperator::Not:
983 Result = ~Result;
984 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000985 }
986
987 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000988 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000989}
990
Chris Lattnerff579ff2008-07-12 01:15:53 +0000991/// HandleCast - This is used to evaluate implicit or explicit casts where the
992/// result type is integer.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000993bool IntExprEvaluator::HandleCast(CastExpr *E) {
994 Expr *SubExpr = E->getSubExpr();
995 QualType DestType = E->getType();
996
Chris Lattner2c99c712008-07-11 19:24:49 +0000997 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000998
Eli Friedman7888b932008-11-12 09:44:48 +0000999 if (DestType->isBooleanType()) {
1000 bool BoolResult;
1001 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1002 return false;
1003 Result.zextOrTrunc(DestWidth);
1004 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1005 Result = BoolResult;
1006 return true;
1007 }
1008
Anders Carlssond1aa5812008-07-08 14:35:21 +00001009 // Handle simple integer->integer casts.
Eli Friedman14cc7542008-11-13 06:09:17 +00001010 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +00001011 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001012 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001013
1014 // Figure out if this is a truncate, extend or noop cast.
1015 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman7888b932008-11-12 09:44:48 +00001016 Result.extOrTrunc(DestWidth);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001017 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1018 return true;
1019 }
1020
1021 // FIXME: Clean this up!
1022 if (SubExpr->getType()->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +00001023 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +00001024 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001025 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001026
Anders Carlssond1aa5812008-07-08 14:35:21 +00001027 if (LV.getLValueBase())
Chris Lattnera42f09a2008-07-11 19:10:17 +00001028 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001029
Anders Carlsson8ab15c82008-07-08 16:49:00 +00001030 Result.extOrTrunc(DestWidth);
1031 Result = LV.getLValueOffset();
Chris Lattnerff579ff2008-07-12 01:15:53 +00001032 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1033 return true;
Anders Carlsson02a34c32008-07-08 14:30:00 +00001034 }
Eli Friedman7888b932008-11-12 09:44:48 +00001035
Chris Lattnerff579ff2008-07-12 01:15:53 +00001036 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001037 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001038
Eli Friedman2f445492008-08-22 00:06:13 +00001039 APFloat F(0.0);
1040 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001041 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001042
1043 // Determine whether we are converting to unsigned or signed.
1044 bool DestSigned = DestType->isSignedIntegerType();
1045
1046 // FIXME: Warning for overflow.
Dale Johannesen2461f612008-10-09 23:02:32 +00001047 uint64_t Space[4];
1048 bool ignored;
Eli Friedman2f445492008-08-22 00:06:13 +00001049 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001050 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001051 Result = llvm::APInt(DestWidth, 4, Space);
1052 Result.setIsUnsigned(!DestSigned);
Chris Lattnera42f09a2008-07-11 19:10:17 +00001053 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001054}
Anders Carlsson02a34c32008-07-08 14:30:00 +00001055
Chris Lattnera823ccf2008-07-11 18:11:29 +00001056//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +00001057// Float Evaluation
1058//===----------------------------------------------------------------------===//
1059
1060namespace {
1061class VISIBILITY_HIDDEN FloatExprEvaluator
1062 : public StmtVisitor<FloatExprEvaluator, bool> {
1063 EvalInfo &Info;
1064 APFloat &Result;
1065public:
1066 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1067 : Info(info), Result(result) {}
1068
1069 bool VisitStmt(Stmt *S) {
1070 return false;
1071 }
1072
1073 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner87293782008-10-06 05:28:25 +00001074 bool VisitCallExpr(const CallExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001075
Daniel Dunbar804ead02008-10-16 03:51:50 +00001076 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001077 bool VisitBinaryOperator(const BinaryOperator *E);
1078 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman7888b932008-11-12 09:44:48 +00001079 bool VisitCastExpr(CastExpr *E);
1080 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001081};
1082} // end anonymous namespace
1083
1084static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1085 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1086}
1087
Chris Lattner87293782008-10-06 05:28:25 +00001088bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner87293782008-10-06 05:28:25 +00001089 switch (E->isBuiltinCall()) {
Chris Lattner27cde262008-10-06 05:53:16 +00001090 default: return false;
Chris Lattner87293782008-10-06 05:28:25 +00001091 case Builtin::BI__builtin_huge_val:
1092 case Builtin::BI__builtin_huge_valf:
1093 case Builtin::BI__builtin_huge_vall:
1094 case Builtin::BI__builtin_inf:
1095 case Builtin::BI__builtin_inff:
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001096 case Builtin::BI__builtin_infl: {
1097 const llvm::fltSemantics &Sem =
1098 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner27cde262008-10-06 05:53:16 +00001099 Result = llvm::APFloat::getInf(Sem);
1100 return true;
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001101 }
Chris Lattner667e1ee2008-10-06 06:31:58 +00001102
1103 case Builtin::BI__builtin_nan:
1104 case Builtin::BI__builtin_nanf:
1105 case Builtin::BI__builtin_nanl:
1106 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1107 // can't constant fold it.
1108 if (const StringLiteral *S =
1109 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1110 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001111 const llvm::fltSemantics &Sem =
1112 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner667e1ee2008-10-06 06:31:58 +00001113 Result = llvm::APFloat::getNaN(Sem);
1114 return true;
1115 }
1116 }
1117 return false;
Daniel Dunbar804ead02008-10-16 03:51:50 +00001118
1119 case Builtin::BI__builtin_fabs:
1120 case Builtin::BI__builtin_fabsf:
1121 case Builtin::BI__builtin_fabsl:
1122 if (!EvaluateFloat(E->getArg(0), Result, Info))
1123 return false;
1124
1125 if (Result.isNegative())
1126 Result.changeSign();
1127 return true;
1128
1129 case Builtin::BI__builtin_copysign:
1130 case Builtin::BI__builtin_copysignf:
1131 case Builtin::BI__builtin_copysignl: {
1132 APFloat RHS(0.);
1133 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1134 !EvaluateFloat(E->getArg(1), RHS, Info))
1135 return false;
1136 Result.copySign(RHS);
1137 return true;
1138 }
Chris Lattner87293782008-10-06 05:28:25 +00001139 }
1140}
1141
Daniel Dunbar804ead02008-10-16 03:51:50 +00001142bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes1cea4f42008-11-19 17:44:31 +00001143 if (E->getOpcode() == UnaryOperator::Deref)
1144 return false;
1145
Daniel Dunbar804ead02008-10-16 03:51:50 +00001146 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1147 return false;
1148
1149 switch (E->getOpcode()) {
1150 default: return false;
1151 case UnaryOperator::Plus:
1152 return true;
1153 case UnaryOperator::Minus:
1154 Result.changeSign();
1155 return true;
1156 }
1157}
Chris Lattner87293782008-10-06 05:28:25 +00001158
Eli Friedman2f445492008-08-22 00:06:13 +00001159bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1160 // FIXME: Diagnostics? I really don't understand how the warnings
1161 // and errors are supposed to work.
Daniel Dunbar804ead02008-10-16 03:51:50 +00001162 APFloat RHS(0.0);
Eli Friedman2f445492008-08-22 00:06:13 +00001163 if (!EvaluateFloat(E->getLHS(), Result, Info))
1164 return false;
1165 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1166 return false;
1167
1168 switch (E->getOpcode()) {
1169 default: return false;
1170 case BinaryOperator::Mul:
1171 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1172 return true;
1173 case BinaryOperator::Add:
1174 Result.add(RHS, APFloat::rmNearestTiesToEven);
1175 return true;
1176 case BinaryOperator::Sub:
1177 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1178 return true;
1179 case BinaryOperator::Div:
1180 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1181 return true;
1182 case BinaryOperator::Rem:
1183 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1184 return true;
1185 }
1186}
1187
1188bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1189 Result = E->getValue();
1190 return true;
1191}
1192
Eli Friedman7888b932008-11-12 09:44:48 +00001193bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1194 Expr* SubExpr = E->getSubExpr();
Nate Begemand6d2f772009-01-18 03:20:47 +00001195
Eli Friedman7888b932008-11-12 09:44:48 +00001196 const llvm::fltSemantics& destSemantics =
1197 Info.Ctx.getFloatTypeSemantics(E->getType());
1198 if (SubExpr->getType()->isIntegralType()) {
1199 APSInt IntResult;
1200 if (!EvaluateInteger(E, IntResult, Info))
1201 return false;
1202 Result = APFloat(destSemantics, 1);
1203 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1204 APFloat::rmNearestTiesToEven);
1205 return true;
1206 }
1207 if (SubExpr->getType()->isRealFloatingType()) {
1208 if (!Visit(SubExpr))
1209 return false;
1210 bool ignored;
1211 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1212 return true;
1213 }
1214
1215 return false;
1216}
1217
1218bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1219 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1220 return true;
1221}
1222
Eli Friedman2f445492008-08-22 00:06:13 +00001223//===----------------------------------------------------------------------===//
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001224// Complex Float Evaluation
1225//===----------------------------------------------------------------------===//
1226
1227namespace {
1228class VISIBILITY_HIDDEN ComplexFloatExprEvaluator
1229 : public StmtVisitor<ComplexFloatExprEvaluator, APValue> {
1230 EvalInfo &Info;
1231
1232public:
1233 ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {}
1234
1235 //===--------------------------------------------------------------------===//
1236 // Visitor Methods
1237 //===--------------------------------------------------------------------===//
1238
1239 APValue VisitStmt(Stmt *S) {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001240 return APValue();
1241 }
1242
1243 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1244
1245 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
1246 APFloat Result(0.0);
1247 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1248 return APValue();
1249
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001250 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero),
1251 Result);
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001252 }
1253
Anders Carlssonad2794c2008-11-16 21:51:21 +00001254 APValue VisitCastExpr(CastExpr *E) {
1255 Expr* SubExpr = E->getSubExpr();
1256
1257 if (SubExpr->getType()->isRealFloatingType()) {
1258 APFloat Result(0.0);
1259
1260 if (!EvaluateFloat(SubExpr, Result, Info))
1261 return APValue();
1262
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001263 return APValue(Result,
1264 APFloat(Result.getSemantics(), APFloat::fcZero));
Anders Carlssonad2794c2008-11-16 21:51:21 +00001265 }
1266
1267 // FIXME: Handle more casts.
1268 return APValue();
1269 }
1270
1271 APValue VisitBinaryOperator(const BinaryOperator *E);
1272
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001273};
1274} // end anonymous namespace
1275
1276static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info)
1277{
1278 Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001279 if (Result.isComplexFloat())
1280 assert(&Result.getComplexFloatReal().getSemantics() ==
1281 &Result.getComplexFloatImag().getSemantics() &&
1282 "Invalid complex evaluation.");
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001283 return Result.isComplexFloat();
1284}
1285
Anders Carlssonad2794c2008-11-16 21:51:21 +00001286APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
1287{
1288 APValue Result, RHS;
1289
1290 if (!EvaluateComplexFloat(E->getLHS(), Result, Info))
1291 return APValue();
1292
1293 if (!EvaluateComplexFloat(E->getRHS(), RHS, Info))
1294 return APValue();
1295
1296 switch (E->getOpcode()) {
1297 default: return APValue();
1298 case BinaryOperator::Add:
1299 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1300 APFloat::rmNearestTiesToEven);
1301 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1302 APFloat::rmNearestTiesToEven);
1303 case BinaryOperator::Sub:
1304 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1305 APFloat::rmNearestTiesToEven);
1306 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1307 APFloat::rmNearestTiesToEven);
1308 }
1309
1310 return Result;
1311}
1312
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001313//===----------------------------------------------------------------------===//
Chris Lattneref069662008-11-16 21:24:15 +00001314// Top level Expr::Evaluate method.
Chris Lattnera823ccf2008-07-11 18:11:29 +00001315//===----------------------------------------------------------------------===//
1316
Chris Lattneref069662008-11-16 21:24:15 +00001317/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner87293782008-10-06 05:28:25 +00001318/// any crazy technique (that has nothing to do with language standards) that
1319/// we want to. If this function returns true, it returns the folded constant
1320/// in Result.
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001321bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1322 EvalInfo Info(Ctx, Result);
Anders Carlssondd8d41f2008-11-30 16:38:33 +00001323
Nate Begemand6d2f772009-01-18 03:20:47 +00001324 if (getType()->isVectorType()) {
1325 if (!EvaluateVector(this, Result.Val, Info))
1326 return false;
1327 } else if (getType()->isIntegerType()) {
Eli Friedman2f445492008-08-22 00:06:13 +00001328 llvm::APSInt sInt(32);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001329 if (!EvaluateInteger(this, sInt, Info))
1330 return false;
1331
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001332 Result.Val = APValue(sInt);
Eli Friedman2f445492008-08-22 00:06:13 +00001333 } else if (getType()->isPointerType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001334 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001335 return false;
Eli Friedman2f445492008-08-22 00:06:13 +00001336 } else if (getType()->isRealFloatingType()) {
1337 llvm::APFloat f(0.0);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001338 if (!EvaluateFloat(this, f, Info))
1339 return false;
1340
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001341 Result.Val = APValue(f);
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001342 } else if (getType()->isComplexType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001343 if (!EvaluateComplexFloat(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001344 return false;
Anders Carlssoncb6a2e82008-11-22 22:56:32 +00001345 } else
1346 return false;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001347
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001348 return true;
1349}
1350
Chris Lattneref069662008-11-16 21:24:15 +00001351/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001352/// folded, but discard the result.
1353bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson197f6f72008-12-01 06:44:05 +00001354 EvalResult Result;
1355 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001356}
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001357
1358APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson8c3de802008-12-19 20:58:05 +00001359 EvalResult EvalResult;
1360 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarddebeca2009-01-15 18:32:35 +00001361 Result = Result;
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001362 assert(Result && "Could not evaluate expression");
Anders Carlsson8c3de802008-12-19 20:58:05 +00001363 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001364
Anders Carlsson8c3de802008-12-19 20:58:05 +00001365 return EvalResult.Val.getInt();
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001366}