blob: 66261045a5b6aa3b089dfdabfa1f52683aa583a2 [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 Lattner52a425b2009-01-27 18:30:58 +000018#include "clang/Basic/DiagnosticAST.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 Friedman67f4ac52009-01-25 01:54:01 +0000220 APValue VisitCallExpr(CallExpr *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000221 APValue VisitConditionalOperator(ConditionalOperator *E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000222};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000223} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000224
Chris Lattner422373c2008-07-11 22:52:41 +0000225static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000226 if (!E->getType()->isPointerType())
227 return false;
Chris Lattner422373c2008-07-11 22:52:41 +0000228 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000229 return Result.isLValue();
230}
231
232APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
233 if (E->getOpcode() != BinaryOperator::Add &&
234 E->getOpcode() != BinaryOperator::Sub)
235 return APValue();
236
237 const Expr *PExp = E->getLHS();
238 const Expr *IExp = E->getRHS();
239 if (IExp->getType()->isPointerType())
240 std::swap(PExp, IExp);
241
242 APValue ResultLValue;
Chris Lattner422373c2008-07-11 22:52:41 +0000243 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000244 return APValue();
245
246 llvm::APSInt AdditionalOffset(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000247 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000248 return APValue();
249
Eli Friedman7888b932008-11-12 09:44:48 +0000250 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
251 uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
252
Chris Lattnera823ccf2008-07-11 18:11:29 +0000253 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman7888b932008-11-12 09:44:48 +0000254
Chris Lattnera823ccf2008-07-11 18:11:29 +0000255 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman7888b932008-11-12 09:44:48 +0000256 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnera823ccf2008-07-11 18:11:29 +0000257 else
Eli Friedman7888b932008-11-12 09:44:48 +0000258 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
259
Chris Lattnera823ccf2008-07-11 18:11:29 +0000260 return APValue(ResultLValue.getLValueBase(), Offset);
261}
Eli Friedman7888b932008-11-12 09:44:48 +0000262
263APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
264 if (E->getOpcode() == UnaryOperator::Extension) {
265 // FIXME: Deal with warnings?
266 return Visit(E->getSubExpr());
267 }
268
269 if (E->getOpcode() == UnaryOperator::AddrOf) {
270 APValue result;
271 if (EvaluateLValue(E->getSubExpr(), result, Info))
272 return result;
273 }
274
275 return APValue();
276}
Anders Carlsson4ab88da2008-12-05 05:24:13 +0000277
Chris Lattnera823ccf2008-07-11 18:11:29 +0000278
Chris Lattnera42f09a2008-07-11 19:10:17 +0000279APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000280 const Expr* SubExpr = E->getSubExpr();
281
282 // Check for pointer->pointer cast
283 if (SubExpr->getType()->isPointerType()) {
284 APValue Result;
Chris Lattner422373c2008-07-11 22:52:41 +0000285 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000286 return Result;
287 return APValue();
288 }
289
Eli Friedman3e64dd72008-07-27 05:46:18 +0000290 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000291 llvm::APSInt Result(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000292 if (EvaluateInteger(SubExpr, Result, Info)) {
293 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000294 return APValue(0, Result.getZExtValue());
295 }
296 }
Eli Friedman7888b932008-11-12 09:44:48 +0000297
298 if (SubExpr->getType()->isFunctionType() ||
299 SubExpr->getType()->isArrayType()) {
300 APValue Result;
301 if (EvaluateLValue(SubExpr, Result, Info))
302 return Result;
303 return APValue();
304 }
305
306 //assert(0 && "Unhandled cast");
Chris Lattnera823ccf2008-07-11 18:11:29 +0000307 return APValue();
308}
309
Eli Friedman67f4ac52009-01-25 01:54:01 +0000310APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
311 if (E->isBuiltinCall() == Builtin::BI__builtin___CFStringMakeConstantString)
312 return APValue(E, 0);
313 return APValue();
314}
315
Eli Friedman7888b932008-11-12 09:44:48 +0000316APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
317 bool BoolResult;
318 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
319 return APValue();
320
321 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
322
323 APValue Result;
324 if (EvaluatePointer(EvalExpr, Result, Info))
325 return Result;
326 return APValue();
327}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000328
329//===----------------------------------------------------------------------===//
Nate Begemand6d2f772009-01-18 03:20:47 +0000330// Vector Evaluation
331//===----------------------------------------------------------------------===//
332
333namespace {
334 class VISIBILITY_HIDDEN VectorExprEvaluator
335 : public StmtVisitor<VectorExprEvaluator, APValue> {
336 EvalInfo &Info;
337 public:
338
339 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
340
341 APValue VisitStmt(Stmt *S) {
342 return APValue();
343 }
344
345 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
346 APValue VisitCastExpr(const CastExpr* E);
347 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
348 APValue VisitInitListExpr(const InitListExpr *E);
349 };
350} // end anonymous namespace
351
352static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
353 if (!E->getType()->isVectorType())
354 return false;
355 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
356 return !Result.isUninit();
357}
358
359APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
360 const Expr* SE = E->getSubExpr();
361
362 // Check for vector->vector bitcast.
363 if (SE->getType()->isVectorType())
364 return this->Visit(const_cast<Expr*>(SE));
365
366 return APValue();
367}
368
369APValue
370VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
371 return this->Visit(const_cast<Expr*>(E->getInitializer()));
372}
373
374APValue
375VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
376 const VectorType *VT = E->getType()->getAsVectorType();
377 unsigned NumInits = E->getNumInits();
378
379 if (!VT || VT->getNumElements() != NumInits)
380 return APValue();
381
382 QualType EltTy = VT->getElementType();
383 llvm::SmallVector<APValue, 4> Elements;
384
385 for (unsigned i = 0; i < NumInits; i++) {
386 if (EltTy->isIntegerType()) {
387 llvm::APSInt sInt(32);
388 if (!EvaluateInteger(E->getInit(i), sInt, Info))
389 return APValue();
390 Elements.push_back(APValue(sInt));
391 } else {
392 llvm::APFloat f(0.0);
393 if (!EvaluateFloat(E->getInit(i), f, Info))
394 return APValue();
395 Elements.push_back(APValue(f));
396 }
397 }
398 return APValue(&Elements[0], Elements.size());
399}
400
401//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000402// Integer Evaluation
403//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000404
405namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000406class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000407 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000408 EvalInfo &Info;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000409 APSInt &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000410public:
Chris Lattner422373c2008-07-11 22:52:41 +0000411 IntExprEvaluator(EvalInfo &info, APSInt &result)
412 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000413
Chris Lattner2c99c712008-07-11 19:24:49 +0000414 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner82437da2008-07-12 00:14:42 +0000415 return (unsigned)Info.Ctx.getIntWidth(T);
416 }
417
Anders Carlssonfa76d822008-11-30 18:14:57 +0000418 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000419 Info.EvalResult.DiagLoc = L;
420 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000421 Info.EvalResult.DiagExpr = E;
Chris Lattner82437da2008-07-12 00:14:42 +0000422 return true; // still a constant.
423 }
424
Anders Carlssonfa76d822008-11-30 18:14:57 +0000425 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner82437da2008-07-12 00:14:42 +0000426 // If this is in an unevaluated portion of the subexpression, ignore the
427 // error.
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000428 if (Info.ShortCircuit) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000429 // If error is ignored because the value isn't evaluated, get the real
430 // type at least to prevent errors downstream.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000431 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
432 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner82437da2008-07-12 00:14:42 +0000433 return true;
Chris Lattner438f3b12008-11-12 07:43:42 +0000434 }
Chris Lattner82437da2008-07-12 00:14:42 +0000435
Chris Lattner438f3b12008-11-12 07:43:42 +0000436 // Take the first error.
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000437 if (Info.EvalResult.Diag == 0) {
438 Info.EvalResult.DiagLoc = L;
439 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000440 Info.EvalResult.DiagExpr = E;
Chris Lattner438f3b12008-11-12 07:43:42 +0000441 }
Chris Lattner82437da2008-07-12 00:14:42 +0000442 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000443 }
444
Anders Carlssoncad17b52008-07-08 05:13:58 +0000445 //===--------------------------------------------------------------------===//
446 // Visitor Methods
447 //===--------------------------------------------------------------------===//
Chris Lattner438f3b12008-11-12 07:43:42 +0000448
449 bool VisitStmt(Stmt *) {
450 assert(0 && "This should be called on integers, stmts are not integers");
451 return false;
452 }
Chris Lattner2c99c712008-07-11 19:24:49 +0000453
Chris Lattner438f3b12008-11-12 07:43:42 +0000454 bool VisitExpr(Expr *E) {
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000455 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000456 }
457
Chris Lattnera42f09a2008-07-11 19:10:17 +0000458 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000459
Chris Lattner15e59112008-07-12 00:38:25 +0000460 bool VisitIntegerLiteral(const IntegerLiteral *E) {
461 Result = E->getValue();
462 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
463 return true;
464 }
465 bool VisitCharacterLiteral(const CharacterLiteral *E) {
466 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
467 Result = E->getValue();
468 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
469 return true;
470 }
471 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
472 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000473 // Per gcc docs "this built-in function ignores top level
474 // qualifiers". We need to use the canonical version to properly
475 // be able to strip CRV qualifiers from the type.
476 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
477 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
478 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
479 T1.getUnqualifiedType());
Chris Lattner15e59112008-07-12 00:38:25 +0000480 return true;
481 }
482 bool VisitDeclRefExpr(const DeclRefExpr *E);
483 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000484 bool VisitBinaryOperator(const BinaryOperator *E);
485 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000486 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000487
Chris Lattnerff579ff2008-07-12 01:15:53 +0000488 bool VisitCastExpr(CastExpr* E) {
Anders Carlssonfa76d822008-11-30 18:14:57 +0000489 return HandleCast(E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000490 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000491 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
492
Anders Carlsson027f2882008-11-16 19:01:22 +0000493 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000494 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson027f2882008-11-16 19:01:22 +0000495 Result = E->getValue();
496 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
497 return true;
498 }
499
Anders Carlsson774f9c72008-12-21 22:39:40 +0000500 bool VisitGNUNullExpr(const GNUNullExpr *E) {
501 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
502 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
503 return true;
504 }
505
Anders Carlsson027f2882008-11-16 19:01:22 +0000506 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
507 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
508 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
509 return true;
510 }
511
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000512 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
513 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
514 Result = E->Evaluate();
515 return true;
516 }
517
Chris Lattner265a0892008-07-11 21:24:13 +0000518private:
Anders Carlssonfa76d822008-11-30 18:14:57 +0000519 bool HandleCast(CastExpr* E);
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000520 unsigned GetAlignOfExpr(const Expr *E);
521 unsigned GetAlignOfType(QualType T);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000522};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000523} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000524
Chris Lattner422373c2008-07-11 22:52:41 +0000525static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
526 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000527}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000528
Chris Lattner15e59112008-07-12 00:38:25 +0000529bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
530 // Enums are integer constant exprs.
531 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
532 Result = D->getInitVal();
Eli Friedman8b10a232008-12-08 02:21:03 +0000533 // FIXME: This is an ugly hack around the fact that enums don't set their
534 // signedness consistently; see PR3173
535 Result.setIsUnsigned(!E->getType()->isSignedIntegerType());
Chris Lattner15e59112008-07-12 00:38:25 +0000536 return true;
537 }
538
539 // Otherwise, random variable references are not constants.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000540 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000541}
542
Chris Lattner1eee9402008-10-06 06:40:35 +0000543/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
544/// as GCC.
545static int EvaluateBuiltinClassifyType(const CallExpr *E) {
546 // The following enum mimics the values returned by GCC.
547 enum gcc_type_class {
548 no_type_class = -1,
549 void_type_class, integer_type_class, char_type_class,
550 enumeral_type_class, boolean_type_class,
551 pointer_type_class, reference_type_class, offset_type_class,
552 real_type_class, complex_type_class,
553 function_type_class, method_type_class,
554 record_type_class, union_type_class,
555 array_type_class, string_type_class,
556 lang_type_class
557 };
558
559 // If no argument was supplied, default to "no_type_class". This isn't
560 // ideal, however it is what gcc does.
561 if (E->getNumArgs() == 0)
562 return no_type_class;
563
564 QualType ArgTy = E->getArg(0)->getType();
565 if (ArgTy->isVoidType())
566 return void_type_class;
567 else if (ArgTy->isEnumeralType())
568 return enumeral_type_class;
569 else if (ArgTy->isBooleanType())
570 return boolean_type_class;
571 else if (ArgTy->isCharType())
572 return string_type_class; // gcc doesn't appear to use char_type_class
573 else if (ArgTy->isIntegerType())
574 return integer_type_class;
575 else if (ArgTy->isPointerType())
576 return pointer_type_class;
577 else if (ArgTy->isReferenceType())
578 return reference_type_class;
579 else if (ArgTy->isRealType())
580 return real_type_class;
581 else if (ArgTy->isComplexType())
582 return complex_type_class;
583 else if (ArgTy->isFunctionType())
584 return function_type_class;
585 else if (ArgTy->isStructureType())
586 return record_type_class;
587 else if (ArgTy->isUnionType())
588 return union_type_class;
589 else if (ArgTy->isArrayType())
590 return array_type_class;
591 else if (ArgTy->isUnionType())
592 return union_type_class;
593 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
594 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
595 return -1;
596}
597
Chris Lattner15e59112008-07-12 00:38:25 +0000598bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
599 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner15e59112008-07-12 00:38:25 +0000600
Chris Lattner87293782008-10-06 05:28:25 +0000601 switch (E->isBuiltinCall()) {
602 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000603 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner87293782008-10-06 05:28:25 +0000604 case Builtin::BI__builtin_classify_type:
Chris Lattner1eee9402008-10-06 06:40:35 +0000605 Result.setIsSigned(true);
606 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner87293782008-10-06 05:28:25 +0000607 return true;
608
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000609 case Builtin::BI__builtin_constant_p:
Chris Lattner87293782008-10-06 05:28:25 +0000610 // __builtin_constant_p always has one operand: it returns true if that
611 // operand can be folded, false otherwise.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000612 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner87293782008-10-06 05:28:25 +0000613 return true;
614 }
Chris Lattner15e59112008-07-12 00:38:25 +0000615}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000616
Chris Lattnera42f09a2008-07-11 19:10:17 +0000617bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000618 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000619 if (!Visit(E->getRHS()))
620 return false;
Anders Carlsson197f6f72008-12-01 06:44:05 +0000621
622 if (!Info.ShortCircuit) {
623 // If we can't evaluate the LHS, it must be because it has
624 // side effects.
625 if (!E->getLHS()->isEvaluatable(Info.Ctx))
626 Info.EvalResult.HasSideEffects = true;
627
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000628 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson197f6f72008-12-01 06:44:05 +0000629 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000630
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000631 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000632 }
633
634 if (E->isLogicalOp()) {
635 // These need to be handled specially because the operands aren't
636 // necessarily integral
Anders Carlsson501da1f2008-11-30 16:51:17 +0000637 bool lhsResult, rhsResult;
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000638
Anders Carlsson501da1f2008-11-30 16:51:17 +0000639 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000640 // We were able to evaluate the LHS, see if we can get away with not
641 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlsson501da1f2008-11-30 16:51:17 +0000642 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
643 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000644 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
645 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000646 Result = lhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000647
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000648 Info.ShortCircuit++;
Anders Carlsson501da1f2008-11-30 16:51:17 +0000649 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000650 Info.ShortCircuit--;
651
Anders Carlsson501da1f2008-11-30 16:51:17 +0000652 if (rhsEvaluated)
653 return true;
654
655 // FIXME: Return an extension warning saying that the RHS could not be
656 // evaluated.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000657 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000658 }
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000659
Anders Carlsson501da1f2008-11-30 16:51:17 +0000660 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000661 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
662 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
663 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlsson501da1f2008-11-30 16:51:17 +0000664 Result = lhsResult || rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000665 else
Anders Carlsson501da1f2008-11-30 16:51:17 +0000666 Result = lhsResult && rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000667 return true;
668 }
669 } else {
Anders Carlsson501da1f2008-11-30 16:51:17 +0000670 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000671 // We can't evaluate the LHS; however, sometimes the result
672 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlsson501da1f2008-11-30 16:51:17 +0000673 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
674 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000675 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
676 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000677 Result = rhsResult;
678
679 // Since we werent able to evaluate the left hand side, it
680 // must have had side effects.
681 Info.EvalResult.HasSideEffects = true;
682
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000683 return true;
684 }
685 }
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000686 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000687
Eli Friedman14cc7542008-11-13 06:09:17 +0000688 return false;
689 }
690
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000691 QualType LHSTy = E->getLHS()->getType();
692 QualType RHSTy = E->getRHS()->getType();
693
694 if (LHSTy->isRealFloatingType() &&
695 RHSTy->isRealFloatingType()) {
696 APFloat RHS(0.0), LHS(0.0);
697
698 if (!EvaluateFloat(E->getRHS(), RHS, Info))
699 return false;
700
701 if (!EvaluateFloat(E->getLHS(), LHS, Info))
702 return false;
703
704 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000705
706 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
707
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000708 switch (E->getOpcode()) {
709 default:
710 assert(0 && "Invalid binary operator!");
711 case BinaryOperator::LT:
712 Result = CR == APFloat::cmpLessThan;
713 break;
714 case BinaryOperator::GT:
715 Result = CR == APFloat::cmpGreaterThan;
716 break;
717 case BinaryOperator::LE:
718 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
719 break;
720 case BinaryOperator::GE:
721 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
722 break;
723 case BinaryOperator::EQ:
724 Result = CR == APFloat::cmpEqual;
725 break;
726 case BinaryOperator::NE:
727 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
728 break;
729 }
730
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000731 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
732 return true;
733 }
734
Anders Carlsson027f2882008-11-16 19:01:22 +0000735 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000736 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson027f2882008-11-16 19:01:22 +0000737 APValue LHSValue;
738 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
739 return false;
740
741 APValue RHSValue;
742 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
743 return false;
744
745 // FIXME: Is this correct? What if only one of the operands has a base?
746 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
747 return false;
748
749 const QualType Type = E->getLHS()->getType();
750 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
751
752 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
753 D /= Info.Ctx.getTypeSize(ElementType) / 8;
754
Anders Carlsson027f2882008-11-16 19:01:22 +0000755 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000756 Result = D;
Anders Carlsson027f2882008-11-16 19:01:22 +0000757 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
758
759 return true;
760 }
761 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000762 if (!LHSTy->isIntegralType() ||
763 !RHSTy->isIntegralType()) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000764 // We can't continue from here for non-integral types, and they
765 // could potentially confuse the following operations.
766 // FIXME: Deal with EQ and friends.
767 return false;
768 }
769
Anders Carlssond1aa5812008-07-08 14:35:21 +0000770 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssond1aa5812008-07-08 14:35:21 +0000771 llvm::APSInt RHS(32);
Chris Lattner40d2ae82008-11-12 07:04:29 +0000772 if (!Visit(E->getLHS())) {
Chris Lattner82437da2008-07-12 00:14:42 +0000773 return false; // error in subexpression.
Chris Lattner40d2ae82008-11-12 07:04:29 +0000774 }
Eli Friedman3e64dd72008-07-27 05:46:18 +0000775
Eli Friedman3e64dd72008-07-27 05:46:18 +0000776
777 // FIXME Maybe we want to succeed even where we can't evaluate the
778 // right side of LAnd/LOr?
779 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner82437da2008-07-12 00:14:42 +0000780 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000781 return false;
Eli Friedman14cc7542008-11-13 06:09:17 +0000782
Anders Carlssond1aa5812008-07-08 14:35:21 +0000783 switch (E->getOpcode()) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000784 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000785 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner82437da2008-07-12 00:14:42 +0000786 case BinaryOperator::Mul: Result *= RHS; return true;
787 case BinaryOperator::Add: Result += RHS; return true;
788 case BinaryOperator::Sub: Result -= RHS; return true;
789 case BinaryOperator::And: Result &= RHS; return true;
790 case BinaryOperator::Xor: Result ^= RHS; return true;
791 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000792 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000793 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000794 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000795 Result /= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000796 break;
Chris Lattner400d7402008-07-11 22:15:16 +0000797 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000798 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000799 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000800 Result %= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000801 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000802 case BinaryOperator::Shl:
Chris Lattner82437da2008-07-12 00:14:42 +0000803 // FIXME: Warn about out of range shift amounts!
Chris Lattnera42f09a2008-07-11 19:10:17 +0000804 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000805 break;
806 case BinaryOperator::Shr:
Chris Lattnera42f09a2008-07-11 19:10:17 +0000807 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000808 break;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000809
Chris Lattner045502c2008-07-11 19:29:32 +0000810 case BinaryOperator::LT:
811 Result = Result < RHS;
812 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
813 break;
814 case BinaryOperator::GT:
815 Result = Result > RHS;
816 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
817 break;
818 case BinaryOperator::LE:
819 Result = Result <= RHS;
820 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
821 break;
822 case BinaryOperator::GE:
823 Result = Result >= RHS;
824 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
825 break;
826 case BinaryOperator::EQ:
827 Result = Result == RHS;
828 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
829 break;
830 case BinaryOperator::NE:
831 Result = Result != RHS;
832 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
833 break;
Chris Lattner82437da2008-07-12 00:14:42 +0000834 case BinaryOperator::LAnd:
835 Result = Result != 0 && RHS != 0;
836 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
837 break;
838 case BinaryOperator::LOr:
839 Result = Result != 0 || RHS != 0;
840 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
841 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000842 }
Anders Carlssond1aa5812008-07-08 14:35:21 +0000843
844 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000845 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000846}
847
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000848bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes308de752008-11-16 22:06:39 +0000849 bool Cond;
850 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000851 return false;
852
Nuno Lopes308de752008-11-16 22:06:39 +0000853 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000854}
855
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000856unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000857 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
858
859 // __alignof__(void) = 1 as a gcc extension.
860 if (Ty->isVoidType())
861 return 1;
862
863 // GCC extension: alignof(function) = 4.
864 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
865 // attribute(align) directive.
866 if (Ty->isFunctionType())
867 return 4;
868
869 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty))
870 return GetAlignOfType(QualType(ASQT->getBaseType(), 0));
871
872 // alignof VLA/incomplete array.
873 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
874 return GetAlignOfType(VAT->getElementType());
875
876 // sizeof (objc class)?
877 if (isa<ObjCInterfaceType>(Ty))
878 return 1; // FIXME: This probably isn't right.
879
880 // Get information about the alignment.
881 unsigned CharSize = Info.Ctx.Target.getCharWidth();
882 return Info.Ctx.getTypeAlign(Ty) / CharSize;
883}
884
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000885unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
886 E = E->IgnoreParens();
887
888 // alignof decl is always accepted, even if it doesn't make sense: we default
889 // to 1 in those cases.
890 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
891 return Info.Ctx.getDeclAlign(DRE->getDecl());
892
893 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
894 return Info.Ctx.getDeclAlign(ME->getMemberDecl());
895
Chris Lattnere3f61e12009-01-24 21:09:06 +0000896 return GetAlignOfType(E->getType());
897}
898
899
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000900/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
901/// expression's type.
902bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
903 QualType DstTy = E->getType();
Chris Lattner265a0892008-07-11 21:24:13 +0000904 // Return the result in the right width.
905 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
906 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
907
Chris Lattnere3f61e12009-01-24 21:09:06 +0000908 // Handle alignof separately.
909 if (!E->isSizeOf()) {
910 if (E->isArgumentType())
911 Result = GetAlignOfType(E->getArgumentType());
912 else
913 Result = GetAlignOfExpr(E->getArgumentExpr());
914 return true;
915 }
916
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000917 QualType SrcTy = E->getTypeOfArgument();
918
Chris Lattner265a0892008-07-11 21:24:13 +0000919 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman7888b932008-11-12 09:44:48 +0000920 if (SrcTy->isVoidType()) {
Chris Lattner265a0892008-07-11 21:24:13 +0000921 Result = 1;
Eli Friedman7888b932008-11-12 09:44:48 +0000922 return true;
923 }
Chris Lattner265a0892008-07-11 21:24:13 +0000924
925 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere3f61e12009-01-24 21:09:06 +0000926 if (!SrcTy->isConstantSizeType())
Chris Lattner265a0892008-07-11 21:24:13 +0000927 return false;
Fariborz Jahanian2a032ec2009-01-16 01:42:12 +0000928
Chris Lattner265a0892008-07-11 21:24:13 +0000929 // GCC extension: sizeof(function) = 1.
930 if (SrcTy->isFunctionType()) {
Chris Lattnere3f61e12009-01-24 21:09:06 +0000931 Result = 1;
Chris Lattner265a0892008-07-11 21:24:13 +0000932 return true;
933 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000934
935 if (SrcTy->isObjCInterfaceType()) {
936 // Slightly unusual case: the size of an ObjC interface type is the
937 // size of the class. This code intentionally falls through to the normal
938 // case.
939 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
940 RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
941 SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
942 }
943
Chris Lattnere3f61e12009-01-24 21:09:06 +0000944 // Get information about the size.
Chris Lattner422373c2008-07-11 22:52:41 +0000945 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnere3f61e12009-01-24 21:09:06 +0000946 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000947 return true;
948}
949
Chris Lattnera42f09a2008-07-11 19:10:17 +0000950bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +0000951 // Special case unary operators that do not need their subexpression
952 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner400d7402008-07-11 22:15:16 +0000953 if (E->isOffsetOfOp()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000954 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner422373c2008-07-11 22:52:41 +0000955 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner400d7402008-07-11 22:15:16 +0000956 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
957 return true;
958 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000959
960 if (E->getOpcode() == UnaryOperator::LNot) {
961 // LNot's operand isn't necessarily an integer, so we handle it specially.
962 bool bres;
963 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
964 return false;
965 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
966 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
967 Result = !bres;
968 return true;
969 }
970
Chris Lattner422373c2008-07-11 22:52:41 +0000971 // Get the operand value into 'Result'.
972 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +0000973 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000974
Chris Lattner400d7402008-07-11 22:15:16 +0000975 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000976 default:
Chris Lattner400d7402008-07-11 22:15:16 +0000977 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
978 // See C99 6.6p3.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000979 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000980 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +0000981 // FIXME: Should extension allow i-c-e extension expressions in its scope?
982 // If so, we could clear the diagnostic ID.
Chris Lattner400d7402008-07-11 22:15:16 +0000983 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +0000984 // The result is always just the subexpr.
Chris Lattner400d7402008-07-11 22:15:16 +0000985 break;
986 case UnaryOperator::Minus:
987 Result = -Result;
988 break;
989 case UnaryOperator::Not:
990 Result = ~Result;
991 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000992 }
993
994 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000995 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000996}
997
Chris Lattnerff579ff2008-07-12 01:15:53 +0000998/// HandleCast - This is used to evaluate implicit or explicit casts where the
999/// result type is integer.
Anders Carlssonfa76d822008-11-30 18:14:57 +00001000bool IntExprEvaluator::HandleCast(CastExpr *E) {
1001 Expr *SubExpr = E->getSubExpr();
1002 QualType DestType = E->getType();
1003
Chris Lattner2c99c712008-07-11 19:24:49 +00001004 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssond1aa5812008-07-08 14:35:21 +00001005
Eli Friedman7888b932008-11-12 09:44:48 +00001006 if (DestType->isBooleanType()) {
1007 bool BoolResult;
1008 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1009 return false;
1010 Result.zextOrTrunc(DestWidth);
1011 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1012 Result = BoolResult;
1013 return true;
1014 }
1015
Anders Carlssond1aa5812008-07-08 14:35:21 +00001016 // Handle simple integer->integer casts.
Eli Friedman14cc7542008-11-13 06:09:17 +00001017 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +00001018 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001019 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001020
1021 // Figure out if this is a truncate, extend or noop cast.
1022 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman7888b932008-11-12 09:44:48 +00001023 Result.extOrTrunc(DestWidth);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001024 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1025 return true;
1026 }
1027
1028 // FIXME: Clean this up!
1029 if (SubExpr->getType()->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +00001030 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +00001031 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +00001032 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001033
Anders Carlssond1aa5812008-07-08 14:35:21 +00001034 if (LV.getLValueBase())
Chris Lattnera42f09a2008-07-11 19:10:17 +00001035 return false;
Eli Friedman7888b932008-11-12 09:44:48 +00001036
Anders Carlsson8ab15c82008-07-08 16:49:00 +00001037 Result.extOrTrunc(DestWidth);
1038 Result = LV.getLValueOffset();
Chris Lattnerff579ff2008-07-12 01:15:53 +00001039 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
1040 return true;
Anders Carlsson02a34c32008-07-08 14:30:00 +00001041 }
Eli Friedman7888b932008-11-12 09:44:48 +00001042
Chris Lattnerff579ff2008-07-12 01:15:53 +00001043 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001044 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001045
Eli Friedman2f445492008-08-22 00:06:13 +00001046 APFloat F(0.0);
1047 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson38bb18c2008-11-30 18:37:00 +00001048 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001049
1050 // Determine whether we are converting to unsigned or signed.
1051 bool DestSigned = DestType->isSignedIntegerType();
1052
1053 // FIXME: Warning for overflow.
Dale Johannesen2461f612008-10-09 23:02:32 +00001054 uint64_t Space[4];
1055 bool ignored;
Eli Friedman2f445492008-08-22 00:06:13 +00001056 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001057 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattnerff579ff2008-07-12 01:15:53 +00001058 Result = llvm::APInt(DestWidth, 4, Space);
1059 Result.setIsUnsigned(!DestSigned);
Chris Lattnera42f09a2008-07-11 19:10:17 +00001060 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +00001061}
Anders Carlsson02a34c32008-07-08 14:30:00 +00001062
Chris Lattnera823ccf2008-07-11 18:11:29 +00001063//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +00001064// Float Evaluation
1065//===----------------------------------------------------------------------===//
1066
1067namespace {
1068class VISIBILITY_HIDDEN FloatExprEvaluator
1069 : public StmtVisitor<FloatExprEvaluator, bool> {
1070 EvalInfo &Info;
1071 APFloat &Result;
1072public:
1073 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1074 : Info(info), Result(result) {}
1075
1076 bool VisitStmt(Stmt *S) {
1077 return false;
1078 }
1079
1080 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner87293782008-10-06 05:28:25 +00001081 bool VisitCallExpr(const CallExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001082
Daniel Dunbar804ead02008-10-16 03:51:50 +00001083 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001084 bool VisitBinaryOperator(const BinaryOperator *E);
1085 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman7888b932008-11-12 09:44:48 +00001086 bool VisitCastExpr(CastExpr *E);
1087 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +00001088};
1089} // end anonymous namespace
1090
1091static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1092 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1093}
1094
Chris Lattner87293782008-10-06 05:28:25 +00001095bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner87293782008-10-06 05:28:25 +00001096 switch (E->isBuiltinCall()) {
Chris Lattner27cde262008-10-06 05:53:16 +00001097 default: return false;
Chris Lattner87293782008-10-06 05:28:25 +00001098 case Builtin::BI__builtin_huge_val:
1099 case Builtin::BI__builtin_huge_valf:
1100 case Builtin::BI__builtin_huge_vall:
1101 case Builtin::BI__builtin_inf:
1102 case Builtin::BI__builtin_inff:
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001103 case Builtin::BI__builtin_infl: {
1104 const llvm::fltSemantics &Sem =
1105 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner27cde262008-10-06 05:53:16 +00001106 Result = llvm::APFloat::getInf(Sem);
1107 return true;
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001108 }
Chris Lattner667e1ee2008-10-06 06:31:58 +00001109
1110 case Builtin::BI__builtin_nan:
1111 case Builtin::BI__builtin_nanf:
1112 case Builtin::BI__builtin_nanl:
1113 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1114 // can't constant fold it.
1115 if (const StringLiteral *S =
1116 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1117 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar0b3efb42008-10-14 05:41:12 +00001118 const llvm::fltSemantics &Sem =
1119 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner667e1ee2008-10-06 06:31:58 +00001120 Result = llvm::APFloat::getNaN(Sem);
1121 return true;
1122 }
1123 }
1124 return false;
Daniel Dunbar804ead02008-10-16 03:51:50 +00001125
1126 case Builtin::BI__builtin_fabs:
1127 case Builtin::BI__builtin_fabsf:
1128 case Builtin::BI__builtin_fabsl:
1129 if (!EvaluateFloat(E->getArg(0), Result, Info))
1130 return false;
1131
1132 if (Result.isNegative())
1133 Result.changeSign();
1134 return true;
1135
1136 case Builtin::BI__builtin_copysign:
1137 case Builtin::BI__builtin_copysignf:
1138 case Builtin::BI__builtin_copysignl: {
1139 APFloat RHS(0.);
1140 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1141 !EvaluateFloat(E->getArg(1), RHS, Info))
1142 return false;
1143 Result.copySign(RHS);
1144 return true;
1145 }
Chris Lattner87293782008-10-06 05:28:25 +00001146 }
1147}
1148
Daniel Dunbar804ead02008-10-16 03:51:50 +00001149bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes1cea4f42008-11-19 17:44:31 +00001150 if (E->getOpcode() == UnaryOperator::Deref)
1151 return false;
1152
Daniel Dunbar804ead02008-10-16 03:51:50 +00001153 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1154 return false;
1155
1156 switch (E->getOpcode()) {
1157 default: return false;
1158 case UnaryOperator::Plus:
1159 return true;
1160 case UnaryOperator::Minus:
1161 Result.changeSign();
1162 return true;
1163 }
1164}
Chris Lattner87293782008-10-06 05:28:25 +00001165
Eli Friedman2f445492008-08-22 00:06:13 +00001166bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1167 // FIXME: Diagnostics? I really don't understand how the warnings
1168 // and errors are supposed to work.
Daniel Dunbar804ead02008-10-16 03:51:50 +00001169 APFloat RHS(0.0);
Eli Friedman2f445492008-08-22 00:06:13 +00001170 if (!EvaluateFloat(E->getLHS(), Result, Info))
1171 return false;
1172 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1173 return false;
1174
1175 switch (E->getOpcode()) {
1176 default: return false;
1177 case BinaryOperator::Mul:
1178 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1179 return true;
1180 case BinaryOperator::Add:
1181 Result.add(RHS, APFloat::rmNearestTiesToEven);
1182 return true;
1183 case BinaryOperator::Sub:
1184 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1185 return true;
1186 case BinaryOperator::Div:
1187 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1188 return true;
1189 case BinaryOperator::Rem:
1190 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1191 return true;
1192 }
1193}
1194
1195bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1196 Result = E->getValue();
1197 return true;
1198}
1199
Eli Friedman7888b932008-11-12 09:44:48 +00001200bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1201 Expr* SubExpr = E->getSubExpr();
Nate Begemand6d2f772009-01-18 03:20:47 +00001202
Eli Friedman7888b932008-11-12 09:44:48 +00001203 const llvm::fltSemantics& destSemantics =
1204 Info.Ctx.getFloatTypeSemantics(E->getType());
1205 if (SubExpr->getType()->isIntegralType()) {
1206 APSInt IntResult;
1207 if (!EvaluateInteger(E, IntResult, Info))
1208 return false;
1209 Result = APFloat(destSemantics, 1);
1210 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1211 APFloat::rmNearestTiesToEven);
1212 return true;
1213 }
1214 if (SubExpr->getType()->isRealFloatingType()) {
1215 if (!Visit(SubExpr))
1216 return false;
1217 bool ignored;
1218 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1219 return true;
1220 }
1221
1222 return false;
1223}
1224
1225bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1226 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1227 return true;
1228}
1229
Eli Friedman2f445492008-08-22 00:06:13 +00001230//===----------------------------------------------------------------------===//
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001231// Complex Float Evaluation
1232//===----------------------------------------------------------------------===//
1233
1234namespace {
1235class VISIBILITY_HIDDEN ComplexFloatExprEvaluator
1236 : public StmtVisitor<ComplexFloatExprEvaluator, APValue> {
1237 EvalInfo &Info;
1238
1239public:
1240 ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {}
1241
1242 //===--------------------------------------------------------------------===//
1243 // Visitor Methods
1244 //===--------------------------------------------------------------------===//
1245
1246 APValue VisitStmt(Stmt *S) {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001247 return APValue();
1248 }
1249
1250 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1251
1252 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
1253 APFloat Result(0.0);
1254 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1255 return APValue();
1256
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001257 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero),
1258 Result);
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001259 }
1260
Anders Carlssonad2794c2008-11-16 21:51:21 +00001261 APValue VisitCastExpr(CastExpr *E) {
1262 Expr* SubExpr = E->getSubExpr();
1263
1264 if (SubExpr->getType()->isRealFloatingType()) {
1265 APFloat Result(0.0);
1266
1267 if (!EvaluateFloat(SubExpr, Result, Info))
1268 return APValue();
1269
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001270 return APValue(Result,
1271 APFloat(Result.getSemantics(), APFloat::fcZero));
Anders Carlssonad2794c2008-11-16 21:51:21 +00001272 }
1273
1274 // FIXME: Handle more casts.
1275 return APValue();
1276 }
1277
1278 APValue VisitBinaryOperator(const BinaryOperator *E);
1279
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001280};
1281} // end anonymous namespace
1282
1283static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info)
1284{
1285 Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Daniel Dunbarf8abb942009-01-24 19:08:01 +00001286 if (Result.isComplexFloat())
1287 assert(&Result.getComplexFloatReal().getSemantics() ==
1288 &Result.getComplexFloatImag().getSemantics() &&
1289 "Invalid complex evaluation.");
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001290 return Result.isComplexFloat();
1291}
1292
Anders Carlssonad2794c2008-11-16 21:51:21 +00001293APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
1294{
1295 APValue Result, RHS;
1296
1297 if (!EvaluateComplexFloat(E->getLHS(), Result, Info))
1298 return APValue();
1299
1300 if (!EvaluateComplexFloat(E->getRHS(), RHS, Info))
1301 return APValue();
1302
1303 switch (E->getOpcode()) {
1304 default: return APValue();
1305 case BinaryOperator::Add:
1306 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1307 APFloat::rmNearestTiesToEven);
1308 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1309 APFloat::rmNearestTiesToEven);
1310 case BinaryOperator::Sub:
1311 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1312 APFloat::rmNearestTiesToEven);
1313 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1314 APFloat::rmNearestTiesToEven);
1315 }
1316
1317 return Result;
1318}
1319
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001320//===----------------------------------------------------------------------===//
Chris Lattneref069662008-11-16 21:24:15 +00001321// Top level Expr::Evaluate method.
Chris Lattnera823ccf2008-07-11 18:11:29 +00001322//===----------------------------------------------------------------------===//
1323
Chris Lattneref069662008-11-16 21:24:15 +00001324/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner87293782008-10-06 05:28:25 +00001325/// any crazy technique (that has nothing to do with language standards) that
1326/// we want to. If this function returns true, it returns the folded constant
1327/// in Result.
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001328bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1329 EvalInfo Info(Ctx, Result);
Anders Carlssondd8d41f2008-11-30 16:38:33 +00001330
Nate Begemand6d2f772009-01-18 03:20:47 +00001331 if (getType()->isVectorType()) {
1332 if (!EvaluateVector(this, Result.Val, Info))
1333 return false;
1334 } else if (getType()->isIntegerType()) {
Eli Friedman2f445492008-08-22 00:06:13 +00001335 llvm::APSInt sInt(32);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001336 if (!EvaluateInteger(this, sInt, Info))
1337 return false;
1338
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001339 Result.Val = APValue(sInt);
Eli Friedman2f445492008-08-22 00:06:13 +00001340 } else if (getType()->isPointerType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001341 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001342 return false;
Eli Friedman2f445492008-08-22 00:06:13 +00001343 } else if (getType()->isRealFloatingType()) {
1344 llvm::APFloat f(0.0);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001345 if (!EvaluateFloat(this, f, Info))
1346 return false;
1347
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001348 Result.Val = APValue(f);
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001349 } else if (getType()->isComplexType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001350 if (!EvaluateComplexFloat(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001351 return false;
Anders Carlssoncb6a2e82008-11-22 22:56:32 +00001352 } else
1353 return false;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001354
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001355 return true;
1356}
1357
Chris Lattneref069662008-11-16 21:24:15 +00001358/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001359/// folded, but discard the result.
1360bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson197f6f72008-12-01 06:44:05 +00001361 EvalResult Result;
1362 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001363}
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001364
1365APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson8c3de802008-12-19 20:58:05 +00001366 EvalResult EvalResult;
1367 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarddebeca2009-01-15 18:32:35 +00001368 Result = Result;
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001369 assert(Result && "Could not evaluate expression");
Anders Carlsson8c3de802008-12-19 20:58:05 +00001370 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001371
Anders Carlsson8c3de802008-12-19 20:58:05 +00001372 return EvalResult.Val.getInt();
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001373}