blob: 28440f40f51ebd6bba25ed77bf4b34f18040e7f5 [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);
156 FieldDecl *FD = E->getMemberDecl();
157
158 // FIXME: This is linear time.
159 unsigned i = 0, e = 0;
160 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
161 if (RD->getMember(i) == FD)
162 break;
163 }
164
165 result.setLValue(result.getLValueBase(),
166 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
167
168 return result;
169}
170
Anders Carlsson027f2882008-11-16 19:01:22 +0000171APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
172{
173 APValue Result;
174
175 if (!EvaluatePointer(E->getBase(), Result, Info))
176 return APValue();
177
178 APSInt Index;
179 if (!EvaluateInteger(E->getIdx(), Index, Info))
180 return APValue();
181
182 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
183
184 uint64_t Offset = Index.getSExtValue() * ElementSize;
185 Result.setLValue(Result.getLValueBase(),
186 Result.getLValueOffset() + Offset);
187 return Result;
188}
Eli Friedman7888b932008-11-12 09:44:48 +0000189
190//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000191// Pointer Evaluation
192//===----------------------------------------------------------------------===//
193
Anders Carlssoncad17b52008-07-08 05:13:58 +0000194namespace {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000195class VISIBILITY_HIDDEN PointerExprEvaluator
196 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner422373c2008-07-11 22:52:41 +0000197 EvalInfo &Info;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000198public:
Anders Carlsson02a34c32008-07-08 14:30:00 +0000199
Chris Lattner422373c2008-07-11 22:52:41 +0000200 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000201
Anders Carlsson02a34c32008-07-08 14:30:00 +0000202 APValue VisitStmt(Stmt *S) {
Anders Carlsson02a34c32008-07-08 14:30:00 +0000203 return APValue();
204 }
205
206 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
207
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000208 APValue VisitBinaryOperator(const BinaryOperator *E);
209 APValue VisitCastExpr(const CastExpr* E);
Anders Carlsson8f92cdd2008-12-05 05:18:05 +0000210 APValue VisitCallExpr(CallExpr *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000211 APValue VisitUnaryOperator(const UnaryOperator *E);
212 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
213 { return APValue(E, 0); }
214 APValue VisitConditionalOperator(ConditionalOperator *E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000215};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000216} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000217
Chris Lattner422373c2008-07-11 22:52:41 +0000218static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000219 if (!E->getType()->isPointerType())
220 return false;
Chris Lattner422373c2008-07-11 22:52:41 +0000221 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000222 return Result.isLValue();
223}
224
225APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
226 if (E->getOpcode() != BinaryOperator::Add &&
227 E->getOpcode() != BinaryOperator::Sub)
228 return APValue();
229
230 const Expr *PExp = E->getLHS();
231 const Expr *IExp = E->getRHS();
232 if (IExp->getType()->isPointerType())
233 std::swap(PExp, IExp);
234
235 APValue ResultLValue;
Chris Lattner422373c2008-07-11 22:52:41 +0000236 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000237 return APValue();
238
239 llvm::APSInt AdditionalOffset(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000240 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000241 return APValue();
242
Eli Friedman7888b932008-11-12 09:44:48 +0000243 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
244 uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
245
Chris Lattnera823ccf2008-07-11 18:11:29 +0000246 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman7888b932008-11-12 09:44:48 +0000247
Chris Lattnera823ccf2008-07-11 18:11:29 +0000248 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman7888b932008-11-12 09:44:48 +0000249 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnera823ccf2008-07-11 18:11:29 +0000250 else
Eli Friedman7888b932008-11-12 09:44:48 +0000251 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
252
Chris Lattnera823ccf2008-07-11 18:11:29 +0000253 return APValue(ResultLValue.getLValueBase(), Offset);
254}
Eli Friedman7888b932008-11-12 09:44:48 +0000255
256APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
257 if (E->getOpcode() == UnaryOperator::Extension) {
258 // FIXME: Deal with warnings?
259 return Visit(E->getSubExpr());
260 }
261
262 if (E->getOpcode() == UnaryOperator::AddrOf) {
263 APValue result;
264 if (EvaluateLValue(E->getSubExpr(), result, Info))
265 return result;
266 }
267
268 return APValue();
269}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000270
Chris Lattnera42f09a2008-07-11 19:10:17 +0000271APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000272 const Expr* SubExpr = E->getSubExpr();
273
274 // Check for pointer->pointer cast
275 if (SubExpr->getType()->isPointerType()) {
276 APValue Result;
Chris Lattner422373c2008-07-11 22:52:41 +0000277 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000278 return Result;
279 return APValue();
280 }
281
Eli Friedman3e64dd72008-07-27 05:46:18 +0000282 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000283 llvm::APSInt Result(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000284 if (EvaluateInteger(SubExpr, Result, Info)) {
285 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000286 return APValue(0, Result.getZExtValue());
287 }
288 }
Eli Friedman7888b932008-11-12 09:44:48 +0000289
290 if (SubExpr->getType()->isFunctionType() ||
291 SubExpr->getType()->isArrayType()) {
292 APValue Result;
293 if (EvaluateLValue(SubExpr, Result, Info))
294 return Result;
295 return APValue();
296 }
297
298 //assert(0 && "Unhandled cast");
Chris Lattnera823ccf2008-07-11 18:11:29 +0000299 return APValue();
300}
301
Anders Carlsson8f92cdd2008-12-05 05:18:05 +0000302APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E)
303{
304 switch (E->isBuiltinCall()) {
305 default: return APValue();
306 case Builtin::BI__builtin___CFStringMakeConstantString:
307 return APValue(E, 0);
308 }
309}
310
Eli Friedman7888b932008-11-12 09:44:48 +0000311APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
312 bool BoolResult;
313 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
314 return APValue();
315
316 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
317
318 APValue Result;
319 if (EvaluatePointer(EvalExpr, Result, Info))
320 return Result;
321 return APValue();
322}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000323
324//===----------------------------------------------------------------------===//
325// Integer Evaluation
326//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000327
328namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000329class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000330 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000331 EvalInfo &Info;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000332 APSInt &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000333public:
Chris Lattner422373c2008-07-11 22:52:41 +0000334 IntExprEvaluator(EvalInfo &info, APSInt &result)
335 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000336
Chris Lattner2c99c712008-07-11 19:24:49 +0000337 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner82437da2008-07-12 00:14:42 +0000338 return (unsigned)Info.Ctx.getIntWidth(T);
339 }
340
Anders Carlssonfa76d822008-11-30 18:14:57 +0000341 bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000342 Info.EvalResult.DiagLoc = L;
343 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000344 Info.EvalResult.DiagExpr = E;
Chris Lattner82437da2008-07-12 00:14:42 +0000345 return true; // still a constant.
346 }
347
Anders Carlssonfa76d822008-11-30 18:14:57 +0000348 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner82437da2008-07-12 00:14:42 +0000349 // If this is in an unevaluated portion of the subexpression, ignore the
350 // error.
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000351 if (Info.ShortCircuit) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000352 // If error is ignored because the value isn't evaluated, get the real
353 // type at least to prevent errors downstream.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000354 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
355 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattner82437da2008-07-12 00:14:42 +0000356 return true;
Chris Lattner438f3b12008-11-12 07:43:42 +0000357 }
Chris Lattner82437da2008-07-12 00:14:42 +0000358
Chris Lattner438f3b12008-11-12 07:43:42 +0000359 // Take the first error.
Anders Carlssondd8d41f2008-11-30 16:38:33 +0000360 if (Info.EvalResult.Diag == 0) {
361 Info.EvalResult.DiagLoc = L;
362 Info.EvalResult.Diag = D;
Anders Carlssonfa76d822008-11-30 18:14:57 +0000363 Info.EvalResult.DiagExpr = E;
Chris Lattner438f3b12008-11-12 07:43:42 +0000364 }
Chris Lattner82437da2008-07-12 00:14:42 +0000365 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000366 }
367
Anders Carlssoncad17b52008-07-08 05:13:58 +0000368 //===--------------------------------------------------------------------===//
369 // Visitor Methods
370 //===--------------------------------------------------------------------===//
Chris Lattner438f3b12008-11-12 07:43:42 +0000371
372 bool VisitStmt(Stmt *) {
373 assert(0 && "This should be called on integers, stmts are not integers");
374 return false;
375 }
Chris Lattner2c99c712008-07-11 19:24:49 +0000376
Chris Lattner438f3b12008-11-12 07:43:42 +0000377 bool VisitExpr(Expr *E) {
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000378 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000379 }
380
Chris Lattnera42f09a2008-07-11 19:10:17 +0000381 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000382
Chris Lattner15e59112008-07-12 00:38:25 +0000383 bool VisitIntegerLiteral(const IntegerLiteral *E) {
384 Result = E->getValue();
385 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
386 return true;
387 }
388 bool VisitCharacterLiteral(const CharacterLiteral *E) {
389 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
390 Result = E->getValue();
391 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
392 return true;
393 }
394 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
395 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000396 // Per gcc docs "this built-in function ignores top level
397 // qualifiers". We need to use the canonical version to properly
398 // be able to strip CRV qualifiers from the type.
399 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
400 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
401 Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
402 T1.getUnqualifiedType());
Chris Lattner15e59112008-07-12 00:38:25 +0000403 return true;
404 }
405 bool VisitDeclRefExpr(const DeclRefExpr *E);
406 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000407 bool VisitBinaryOperator(const BinaryOperator *E);
408 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000409 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000410
Chris Lattnerff579ff2008-07-12 01:15:53 +0000411 bool VisitCastExpr(CastExpr* E) {
Anders Carlssonfa76d822008-11-30 18:14:57 +0000412 return HandleCast(E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000413 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000414 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
415
Anders Carlsson027f2882008-11-16 19:01:22 +0000416 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000417 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson027f2882008-11-16 19:01:22 +0000418 Result = E->getValue();
419 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
420 return true;
421 }
422
423 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
424 Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
425 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
426 return true;
427 }
428
Chris Lattner265a0892008-07-11 21:24:13 +0000429private:
Anders Carlssonfa76d822008-11-30 18:14:57 +0000430 bool HandleCast(CastExpr* E);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000431};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000432} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000433
Chris Lattner422373c2008-07-11 22:52:41 +0000434static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
435 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000436}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000437
Chris Lattner15e59112008-07-12 00:38:25 +0000438bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
439 // Enums are integer constant exprs.
440 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
441 Result = D->getInitVal();
442 return true;
443 }
444
445 // Otherwise, random variable references are not constants.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000446 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner15e59112008-07-12 00:38:25 +0000447}
448
Chris Lattner1eee9402008-10-06 06:40:35 +0000449/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
450/// as GCC.
451static int EvaluateBuiltinClassifyType(const CallExpr *E) {
452 // The following enum mimics the values returned by GCC.
453 enum gcc_type_class {
454 no_type_class = -1,
455 void_type_class, integer_type_class, char_type_class,
456 enumeral_type_class, boolean_type_class,
457 pointer_type_class, reference_type_class, offset_type_class,
458 real_type_class, complex_type_class,
459 function_type_class, method_type_class,
460 record_type_class, union_type_class,
461 array_type_class, string_type_class,
462 lang_type_class
463 };
464
465 // If no argument was supplied, default to "no_type_class". This isn't
466 // ideal, however it is what gcc does.
467 if (E->getNumArgs() == 0)
468 return no_type_class;
469
470 QualType ArgTy = E->getArg(0)->getType();
471 if (ArgTy->isVoidType())
472 return void_type_class;
473 else if (ArgTy->isEnumeralType())
474 return enumeral_type_class;
475 else if (ArgTy->isBooleanType())
476 return boolean_type_class;
477 else if (ArgTy->isCharType())
478 return string_type_class; // gcc doesn't appear to use char_type_class
479 else if (ArgTy->isIntegerType())
480 return integer_type_class;
481 else if (ArgTy->isPointerType())
482 return pointer_type_class;
483 else if (ArgTy->isReferenceType())
484 return reference_type_class;
485 else if (ArgTy->isRealType())
486 return real_type_class;
487 else if (ArgTy->isComplexType())
488 return complex_type_class;
489 else if (ArgTy->isFunctionType())
490 return function_type_class;
491 else if (ArgTy->isStructureType())
492 return record_type_class;
493 else if (ArgTy->isUnionType())
494 return union_type_class;
495 else if (ArgTy->isArrayType())
496 return array_type_class;
497 else if (ArgTy->isUnionType())
498 return union_type_class;
499 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
500 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
501 return -1;
502}
503
Chris Lattner15e59112008-07-12 00:38:25 +0000504bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
505 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner15e59112008-07-12 00:38:25 +0000506
Chris Lattner87293782008-10-06 05:28:25 +0000507 switch (E->isBuiltinCall()) {
508 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000509 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner87293782008-10-06 05:28:25 +0000510 case Builtin::BI__builtin_classify_type:
Chris Lattner1eee9402008-10-06 06:40:35 +0000511 Result.setIsSigned(true);
512 Result = EvaluateBuiltinClassifyType(E);
Chris Lattner87293782008-10-06 05:28:25 +0000513 return true;
514
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000515 case Builtin::BI__builtin_constant_p:
Chris Lattner87293782008-10-06 05:28:25 +0000516 // __builtin_constant_p always has one operand: it returns true if that
517 // operand can be folded, false otherwise.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000518 Result = E->getArg(0)->isEvaluatable(Info.Ctx);
Chris Lattner87293782008-10-06 05:28:25 +0000519 return true;
520 }
Chris Lattner15e59112008-07-12 00:38:25 +0000521}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000522
Chris Lattnera42f09a2008-07-11 19:10:17 +0000523bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000524 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000525 if (!Visit(E->getRHS()))
526 return false;
Anders Carlsson197f6f72008-12-01 06:44:05 +0000527
528 if (!Info.ShortCircuit) {
529 // If we can't evaluate the LHS, it must be because it has
530 // side effects.
531 if (!E->getLHS()->isEvaluatable(Info.Ctx))
532 Info.EvalResult.HasSideEffects = true;
533
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000534 return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
Anders Carlsson197f6f72008-12-01 06:44:05 +0000535 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000536
Anders Carlssonb1112ad2008-12-01 02:07:06 +0000537 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000538 }
539
540 if (E->isLogicalOp()) {
541 // These need to be handled specially because the operands aren't
542 // necessarily integral
Anders Carlsson501da1f2008-11-30 16:51:17 +0000543 bool lhsResult, rhsResult;
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000544
Anders Carlsson501da1f2008-11-30 16:51:17 +0000545 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000546 // We were able to evaluate the LHS, see if we can get away with not
547 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Anders Carlsson501da1f2008-11-30 16:51:17 +0000548 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
549 !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000550 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
551 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000552 Result = lhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000553
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000554 Info.ShortCircuit++;
Anders Carlsson501da1f2008-11-30 16:51:17 +0000555 bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
Anders Carlsson6c1a9e22008-11-30 18:26:25 +0000556 Info.ShortCircuit--;
557
Anders Carlsson501da1f2008-11-30 16:51:17 +0000558 if (rhsEvaluated)
559 return true;
560
561 // FIXME: Return an extension warning saying that the RHS could not be
562 // evaluated.
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000563 return true;
Eli Friedman14cc7542008-11-13 06:09:17 +0000564 }
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000565
Anders Carlsson501da1f2008-11-30 16:51:17 +0000566 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000567 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
568 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
569 if (E->getOpcode() == BinaryOperator::LOr)
Anders Carlsson501da1f2008-11-30 16:51:17 +0000570 Result = lhsResult || rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000571 else
Anders Carlsson501da1f2008-11-30 16:51:17 +0000572 Result = lhsResult && rhsResult;
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000573 return true;
574 }
575 } else {
Anders Carlsson501da1f2008-11-30 16:51:17 +0000576 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000577 // We can't evaluate the LHS; however, sometimes the result
578 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlsson501da1f2008-11-30 16:51:17 +0000579 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
580 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000581 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
582 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Anders Carlsson501da1f2008-11-30 16:51:17 +0000583 Result = rhsResult;
584
585 // Since we werent able to evaluate the left hand side, it
586 // must have had side effects.
587 Info.EvalResult.HasSideEffects = true;
588
Anders Carlsson8bce31a2008-11-24 04:21:33 +0000589 return true;
590 }
591 }
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000592 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000593
Eli Friedman14cc7542008-11-13 06:09:17 +0000594 return false;
595 }
596
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000597 QualType LHSTy = E->getLHS()->getType();
598 QualType RHSTy = E->getRHS()->getType();
599
600 if (LHSTy->isRealFloatingType() &&
601 RHSTy->isRealFloatingType()) {
602 APFloat RHS(0.0), LHS(0.0);
603
604 if (!EvaluateFloat(E->getRHS(), RHS, Info))
605 return false;
606
607 if (!EvaluateFloat(E->getLHS(), LHS, Info))
608 return false;
609
610 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000611
612 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
613
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000614 switch (E->getOpcode()) {
615 default:
616 assert(0 && "Invalid binary operator!");
617 case BinaryOperator::LT:
618 Result = CR == APFloat::cmpLessThan;
619 break;
620 case BinaryOperator::GT:
621 Result = CR == APFloat::cmpGreaterThan;
622 break;
623 case BinaryOperator::LE:
624 Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
625 break;
626 case BinaryOperator::GE:
627 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
628 break;
629 case BinaryOperator::EQ:
630 Result = CR == APFloat::cmpEqual;
631 break;
632 case BinaryOperator::NE:
633 Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
634 break;
635 }
636
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000637 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
638 return true;
639 }
640
Anders Carlsson027f2882008-11-16 19:01:22 +0000641 if (E->getOpcode() == BinaryOperator::Sub) {
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000642 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
Anders Carlsson027f2882008-11-16 19:01:22 +0000643 APValue LHSValue;
644 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
645 return false;
646
647 APValue RHSValue;
648 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
649 return false;
650
651 // FIXME: Is this correct? What if only one of the operands has a base?
652 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
653 return false;
654
655 const QualType Type = E->getLHS()->getType();
656 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
657
658 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
659 D /= Info.Ctx.getTypeSize(ElementType) / 8;
660
Anders Carlsson027f2882008-11-16 19:01:22 +0000661 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Anders Carlsson02bb9c32008-11-16 22:46:56 +0000662 Result = D;
Anders Carlsson027f2882008-11-16 19:01:22 +0000663 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
664
665 return true;
666 }
667 }
Anders Carlssonebfa6ed2008-11-16 07:17:21 +0000668 if (!LHSTy->isIntegralType() ||
669 !RHSTy->isIntegralType()) {
Eli Friedman14cc7542008-11-13 06:09:17 +0000670 // We can't continue from here for non-integral types, and they
671 // could potentially confuse the following operations.
672 // FIXME: Deal with EQ and friends.
673 return false;
674 }
675
Anders Carlssond1aa5812008-07-08 14:35:21 +0000676 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssond1aa5812008-07-08 14:35:21 +0000677 llvm::APSInt RHS(32);
Chris Lattner40d2ae82008-11-12 07:04:29 +0000678 if (!Visit(E->getLHS())) {
Chris Lattner82437da2008-07-12 00:14:42 +0000679 return false; // error in subexpression.
Chris Lattner40d2ae82008-11-12 07:04:29 +0000680 }
Eli Friedman3e64dd72008-07-27 05:46:18 +0000681
Eli Friedman3e64dd72008-07-27 05:46:18 +0000682
683 // FIXME Maybe we want to succeed even where we can't evaluate the
684 // right side of LAnd/LOr?
685 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner82437da2008-07-12 00:14:42 +0000686 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000687 return false;
Eli Friedman14cc7542008-11-13 06:09:17 +0000688
Anders Carlssond1aa5812008-07-08 14:35:21 +0000689 switch (E->getOpcode()) {
Chris Lattner438f3b12008-11-12 07:43:42 +0000690 default:
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000691 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner82437da2008-07-12 00:14:42 +0000692 case BinaryOperator::Mul: Result *= RHS; return true;
693 case BinaryOperator::Add: Result += RHS; return true;
694 case BinaryOperator::Sub: Result -= RHS; return true;
695 case BinaryOperator::And: Result &= RHS; return true;
696 case BinaryOperator::Xor: Result ^= RHS; return true;
697 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000698 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000699 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000700 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000701 Result /= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000702 break;
Chris Lattner400d7402008-07-11 22:15:16 +0000703 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000704 if (RHS == 0)
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000705 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000706 Result %= RHS;
Chris Lattner438f3b12008-11-12 07:43:42 +0000707 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000708 case BinaryOperator::Shl:
Chris Lattner82437da2008-07-12 00:14:42 +0000709 // FIXME: Warn about out of range shift amounts!
Chris Lattnera42f09a2008-07-11 19:10:17 +0000710 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000711 break;
712 case BinaryOperator::Shr:
Chris Lattnera42f09a2008-07-11 19:10:17 +0000713 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000714 break;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000715
Chris Lattner045502c2008-07-11 19:29:32 +0000716 case BinaryOperator::LT:
717 Result = Result < RHS;
718 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
719 break;
720 case BinaryOperator::GT:
721 Result = Result > RHS;
722 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
723 break;
724 case BinaryOperator::LE:
725 Result = Result <= RHS;
726 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
727 break;
728 case BinaryOperator::GE:
729 Result = Result >= RHS;
730 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
731 break;
732 case BinaryOperator::EQ:
733 Result = Result == RHS;
734 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
735 break;
736 case BinaryOperator::NE:
737 Result = Result != RHS;
738 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
739 break;
Chris Lattner82437da2008-07-12 00:14:42 +0000740 case BinaryOperator::LAnd:
741 Result = Result != 0 && RHS != 0;
742 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
743 break;
744 case BinaryOperator::LOr:
745 Result = Result != 0 || RHS != 0;
746 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
747 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000748 }
Anders Carlssond1aa5812008-07-08 14:35:21 +0000749
750 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000751 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000752}
753
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000754bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopes308de752008-11-16 22:06:39 +0000755 bool Cond;
756 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000757 return false;
758
Nuno Lopes308de752008-11-16 22:06:39 +0000759 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopeseb35c0e2008-11-16 19:28:31 +0000760}
761
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000762/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
763/// expression's type.
764bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
765 QualType DstTy = E->getType();
Chris Lattner265a0892008-07-11 21:24:13 +0000766 // Return the result in the right width.
767 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
768 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
769
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000770 QualType SrcTy = E->getTypeOfArgument();
771
Chris Lattner265a0892008-07-11 21:24:13 +0000772 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Eli Friedman7888b932008-11-12 09:44:48 +0000773 if (SrcTy->isVoidType()) {
Chris Lattner265a0892008-07-11 21:24:13 +0000774 Result = 1;
Eli Friedman7888b932008-11-12 09:44:48 +0000775 return true;
776 }
Chris Lattner265a0892008-07-11 21:24:13 +0000777
778 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman7888b932008-11-12 09:44:48 +0000779 // FIXME: But alignof(vla) is!
Chris Lattner265a0892008-07-11 21:24:13 +0000780 if (!SrcTy->isConstantSizeType()) {
781 // FIXME: Should we attempt to evaluate this?
782 return false;
783 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000784
785 bool isSizeOf = E->isSizeOf();
Chris Lattner265a0892008-07-11 21:24:13 +0000786
787 // GCC extension: sizeof(function) = 1.
788 if (SrcTy->isFunctionType()) {
789 // FIXME: AlignOf shouldn't be unconditionally 4!
790 Result = isSizeOf ? 1 : 4;
791 return true;
792 }
793
794 // Get information about the size or align.
Chris Lattner422373c2008-07-11 22:52:41 +0000795 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattner265a0892008-07-11 21:24:13 +0000796 if (isSizeOf)
Eli Friedman7888b932008-11-12 09:44:48 +0000797 Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000798 else
Chris Lattner422373c2008-07-11 22:52:41 +0000799 Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000800 return true;
801}
802
Chris Lattnera42f09a2008-07-11 19:10:17 +0000803bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +0000804 // Special case unary operators that do not need their subexpression
805 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner400d7402008-07-11 22:15:16 +0000806 if (E->isOffsetOfOp()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000807 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner422373c2008-07-11 22:52:41 +0000808 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner400d7402008-07-11 22:15:16 +0000809 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
810 return true;
811 }
Eli Friedman14cc7542008-11-13 06:09:17 +0000812
813 if (E->getOpcode() == UnaryOperator::LNot) {
814 // LNot's operand isn't necessarily an integer, so we handle it specially.
815 bool bres;
816 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
817 return false;
818 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
819 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
820 Result = !bres;
821 return true;
822 }
823
Chris Lattner422373c2008-07-11 22:52:41 +0000824 // Get the operand value into 'Result'.
825 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +0000826 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000827
Chris Lattner400d7402008-07-11 22:15:16 +0000828 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000829 default:
Chris Lattner400d7402008-07-11 22:15:16 +0000830 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
831 // See C99 6.6p3.
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000832 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner400d7402008-07-11 22:15:16 +0000833 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +0000834 // FIXME: Should extension allow i-c-e extension expressions in its scope?
835 // If so, we could clear the diagnostic ID.
Chris Lattner400d7402008-07-11 22:15:16 +0000836 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +0000837 // The result is always just the subexpr.
Chris Lattner400d7402008-07-11 22:15:16 +0000838 break;
839 case UnaryOperator::Minus:
840 Result = -Result;
841 break;
842 case UnaryOperator::Not:
843 Result = ~Result;
844 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000845 }
846
847 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000848 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000849}
850
Chris Lattnerff579ff2008-07-12 01:15:53 +0000851/// HandleCast - This is used to evaluate implicit or explicit casts where the
852/// result type is integer.
Anders Carlssonfa76d822008-11-30 18:14:57 +0000853bool IntExprEvaluator::HandleCast(CastExpr *E) {
854 Expr *SubExpr = E->getSubExpr();
855 QualType DestType = E->getType();
856
Chris Lattner2c99c712008-07-11 19:24:49 +0000857 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000858
Eli Friedman7888b932008-11-12 09:44:48 +0000859 if (DestType->isBooleanType()) {
860 bool BoolResult;
861 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
862 return false;
863 Result.zextOrTrunc(DestWidth);
864 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
865 Result = BoolResult;
866 return true;
867 }
868
Anders Carlssond1aa5812008-07-08 14:35:21 +0000869 // Handle simple integer->integer casts.
Eli Friedman14cc7542008-11-13 06:09:17 +0000870 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +0000871 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000872 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000873
874 // Figure out if this is a truncate, extend or noop cast.
875 // If the input is signed, do a sign extend, noop, or truncate.
Eli Friedman7888b932008-11-12 09:44:48 +0000876 Result.extOrTrunc(DestWidth);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000877 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
878 return true;
879 }
880
881 // FIXME: Clean this up!
882 if (SubExpr->getType()->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +0000883 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +0000884 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000885 return false;
Eli Friedman7888b932008-11-12 09:44:48 +0000886
Anders Carlssond1aa5812008-07-08 14:35:21 +0000887 if (LV.getLValueBase())
Chris Lattnera42f09a2008-07-11 19:10:17 +0000888 return false;
Eli Friedman7888b932008-11-12 09:44:48 +0000889
Anders Carlsson8ab15c82008-07-08 16:49:00 +0000890 Result.extOrTrunc(DestWidth);
891 Result = LV.getLValueOffset();
Chris Lattnerff579ff2008-07-12 01:15:53 +0000892 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
893 return true;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000894 }
Eli Friedman7888b932008-11-12 09:44:48 +0000895
Chris Lattnerff579ff2008-07-12 01:15:53 +0000896 if (!SubExpr->getType()->isRealFloatingType())
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000897 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000898
Eli Friedman2f445492008-08-22 00:06:13 +0000899 APFloat F(0.0);
900 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson38bb18c2008-11-30 18:37:00 +0000901 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000902
903 // Determine whether we are converting to unsigned or signed.
904 bool DestSigned = DestType->isSignedIntegerType();
905
906 // FIXME: Warning for overflow.
Dale Johannesen2461f612008-10-09 23:02:32 +0000907 uint64_t Space[4];
908 bool ignored;
Eli Friedman2f445492008-08-22 00:06:13 +0000909 (void)F.convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +0000910 llvm::APFloat::rmTowardZero, &ignored);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000911 Result = llvm::APInt(DestWidth, 4, Space);
912 Result.setIsUnsigned(!DestSigned);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000913 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000914}
Anders Carlsson02a34c32008-07-08 14:30:00 +0000915
Chris Lattnera823ccf2008-07-11 18:11:29 +0000916//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +0000917// Float Evaluation
918//===----------------------------------------------------------------------===//
919
920namespace {
921class VISIBILITY_HIDDEN FloatExprEvaluator
922 : public StmtVisitor<FloatExprEvaluator, bool> {
923 EvalInfo &Info;
924 APFloat &Result;
925public:
926 FloatExprEvaluator(EvalInfo &info, APFloat &result)
927 : Info(info), Result(result) {}
928
929 bool VisitStmt(Stmt *S) {
930 return false;
931 }
932
933 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner87293782008-10-06 05:28:25 +0000934 bool VisitCallExpr(const CallExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +0000935
Daniel Dunbar804ead02008-10-16 03:51:50 +0000936 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedman2f445492008-08-22 00:06:13 +0000937 bool VisitBinaryOperator(const BinaryOperator *E);
938 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman7888b932008-11-12 09:44:48 +0000939 bool VisitCastExpr(CastExpr *E);
940 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2f445492008-08-22 00:06:13 +0000941};
942} // end anonymous namespace
943
944static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
945 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
946}
947
Chris Lattner87293782008-10-06 05:28:25 +0000948bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Chris Lattner87293782008-10-06 05:28:25 +0000949 switch (E->isBuiltinCall()) {
Chris Lattner27cde262008-10-06 05:53:16 +0000950 default: return false;
Chris Lattner87293782008-10-06 05:28:25 +0000951 case Builtin::BI__builtin_huge_val:
952 case Builtin::BI__builtin_huge_valf:
953 case Builtin::BI__builtin_huge_vall:
954 case Builtin::BI__builtin_inf:
955 case Builtin::BI__builtin_inff:
Daniel Dunbar0b3efb42008-10-14 05:41:12 +0000956 case Builtin::BI__builtin_infl: {
957 const llvm::fltSemantics &Sem =
958 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner27cde262008-10-06 05:53:16 +0000959 Result = llvm::APFloat::getInf(Sem);
960 return true;
Daniel Dunbar0b3efb42008-10-14 05:41:12 +0000961 }
Chris Lattner667e1ee2008-10-06 06:31:58 +0000962
963 case Builtin::BI__builtin_nan:
964 case Builtin::BI__builtin_nanf:
965 case Builtin::BI__builtin_nanl:
966 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
967 // can't constant fold it.
968 if (const StringLiteral *S =
969 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
970 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar0b3efb42008-10-14 05:41:12 +0000971 const llvm::fltSemantics &Sem =
972 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner667e1ee2008-10-06 06:31:58 +0000973 Result = llvm::APFloat::getNaN(Sem);
974 return true;
975 }
976 }
977 return false;
Daniel Dunbar804ead02008-10-16 03:51:50 +0000978
979 case Builtin::BI__builtin_fabs:
980 case Builtin::BI__builtin_fabsf:
981 case Builtin::BI__builtin_fabsl:
982 if (!EvaluateFloat(E->getArg(0), Result, Info))
983 return false;
984
985 if (Result.isNegative())
986 Result.changeSign();
987 return true;
988
989 case Builtin::BI__builtin_copysign:
990 case Builtin::BI__builtin_copysignf:
991 case Builtin::BI__builtin_copysignl: {
992 APFloat RHS(0.);
993 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
994 !EvaluateFloat(E->getArg(1), RHS, Info))
995 return false;
996 Result.copySign(RHS);
997 return true;
998 }
Chris Lattner87293782008-10-06 05:28:25 +0000999 }
1000}
1001
Daniel Dunbar804ead02008-10-16 03:51:50 +00001002bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopes1cea4f42008-11-19 17:44:31 +00001003 if (E->getOpcode() == UnaryOperator::Deref)
1004 return false;
1005
Daniel Dunbar804ead02008-10-16 03:51:50 +00001006 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1007 return false;
1008
1009 switch (E->getOpcode()) {
1010 default: return false;
1011 case UnaryOperator::Plus:
1012 return true;
1013 case UnaryOperator::Minus:
1014 Result.changeSign();
1015 return true;
1016 }
1017}
Chris Lattner87293782008-10-06 05:28:25 +00001018
Eli Friedman2f445492008-08-22 00:06:13 +00001019bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1020 // FIXME: Diagnostics? I really don't understand how the warnings
1021 // and errors are supposed to work.
Daniel Dunbar804ead02008-10-16 03:51:50 +00001022 APFloat RHS(0.0);
Eli Friedman2f445492008-08-22 00:06:13 +00001023 if (!EvaluateFloat(E->getLHS(), Result, Info))
1024 return false;
1025 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1026 return false;
1027
1028 switch (E->getOpcode()) {
1029 default: return false;
1030 case BinaryOperator::Mul:
1031 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1032 return true;
1033 case BinaryOperator::Add:
1034 Result.add(RHS, APFloat::rmNearestTiesToEven);
1035 return true;
1036 case BinaryOperator::Sub:
1037 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1038 return true;
1039 case BinaryOperator::Div:
1040 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1041 return true;
1042 case BinaryOperator::Rem:
1043 Result.mod(RHS, APFloat::rmNearestTiesToEven);
1044 return true;
1045 }
1046}
1047
1048bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1049 Result = E->getValue();
1050 return true;
1051}
1052
Eli Friedman7888b932008-11-12 09:44:48 +00001053bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1054 Expr* SubExpr = E->getSubExpr();
1055 const llvm::fltSemantics& destSemantics =
1056 Info.Ctx.getFloatTypeSemantics(E->getType());
1057 if (SubExpr->getType()->isIntegralType()) {
1058 APSInt IntResult;
1059 if (!EvaluateInteger(E, IntResult, Info))
1060 return false;
1061 Result = APFloat(destSemantics, 1);
1062 Result.convertFromAPInt(IntResult, IntResult.isSigned(),
1063 APFloat::rmNearestTiesToEven);
1064 return true;
1065 }
1066 if (SubExpr->getType()->isRealFloatingType()) {
1067 if (!Visit(SubExpr))
1068 return false;
1069 bool ignored;
1070 Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored);
1071 return true;
1072 }
1073
1074 return false;
1075}
1076
1077bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1078 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1079 return true;
1080}
1081
Eli Friedman2f445492008-08-22 00:06:13 +00001082//===----------------------------------------------------------------------===//
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001083// Complex Float Evaluation
1084//===----------------------------------------------------------------------===//
1085
1086namespace {
1087class VISIBILITY_HIDDEN ComplexFloatExprEvaluator
1088 : public StmtVisitor<ComplexFloatExprEvaluator, APValue> {
1089 EvalInfo &Info;
1090
1091public:
1092 ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {}
1093
1094 //===--------------------------------------------------------------------===//
1095 // Visitor Methods
1096 //===--------------------------------------------------------------------===//
1097
1098 APValue VisitStmt(Stmt *S) {
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001099 return APValue();
1100 }
1101
1102 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1103
1104 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
1105 APFloat Result(0.0);
1106 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1107 return APValue();
1108
1109 return APValue(APFloat(0.0), Result);
1110 }
1111
Anders Carlssonad2794c2008-11-16 21:51:21 +00001112 APValue VisitCastExpr(CastExpr *E) {
1113 Expr* SubExpr = E->getSubExpr();
1114
1115 if (SubExpr->getType()->isRealFloatingType()) {
1116 APFloat Result(0.0);
1117
1118 if (!EvaluateFloat(SubExpr, Result, Info))
1119 return APValue();
1120
1121 return APValue(Result, APFloat(0.0));
1122 }
1123
1124 // FIXME: Handle more casts.
1125 return APValue();
1126 }
1127
1128 APValue VisitBinaryOperator(const BinaryOperator *E);
1129
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001130};
1131} // end anonymous namespace
1132
1133static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info)
1134{
1135 Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1136 return Result.isComplexFloat();
1137}
1138
Anders Carlssonad2794c2008-11-16 21:51:21 +00001139APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
1140{
1141 APValue Result, RHS;
1142
1143 if (!EvaluateComplexFloat(E->getLHS(), Result, Info))
1144 return APValue();
1145
1146 if (!EvaluateComplexFloat(E->getRHS(), RHS, Info))
1147 return APValue();
1148
1149 switch (E->getOpcode()) {
1150 default: return APValue();
1151 case BinaryOperator::Add:
1152 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1153 APFloat::rmNearestTiesToEven);
1154 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1155 APFloat::rmNearestTiesToEven);
1156 case BinaryOperator::Sub:
1157 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1158 APFloat::rmNearestTiesToEven);
1159 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1160 APFloat::rmNearestTiesToEven);
1161 }
1162
1163 return Result;
1164}
1165
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001166//===----------------------------------------------------------------------===//
Chris Lattneref069662008-11-16 21:24:15 +00001167// Top level Expr::Evaluate method.
Chris Lattnera823ccf2008-07-11 18:11:29 +00001168//===----------------------------------------------------------------------===//
1169
Chris Lattneref069662008-11-16 21:24:15 +00001170/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner87293782008-10-06 05:28:25 +00001171/// any crazy technique (that has nothing to do with language standards) that
1172/// we want to. If this function returns true, it returns the folded constant
1173/// in Result.
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001174bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1175 EvalInfo Info(Ctx, Result);
Anders Carlssondd8d41f2008-11-30 16:38:33 +00001176
Anders Carlssonc0328012008-07-08 05:49:43 +00001177 if (getType()->isIntegerType()) {
Eli Friedman2f445492008-08-22 00:06:13 +00001178 llvm::APSInt sInt(32);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001179 if (!EvaluateInteger(this, sInt, Info))
1180 return false;
1181
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001182 Result.Val = APValue(sInt);
Eli Friedman2f445492008-08-22 00:06:13 +00001183 } else if (getType()->isPointerType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001184 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001185 return false;
Eli Friedman2f445492008-08-22 00:06:13 +00001186 } else if (getType()->isRealFloatingType()) {
1187 llvm::APFloat f(0.0);
Anders Carlssonb96c2062008-11-22 21:50:49 +00001188 if (!EvaluateFloat(this, f, Info))
1189 return false;
1190
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001191 Result.Val = APValue(f);
Anders Carlssonf1bb2962008-11-16 20:27:53 +00001192 } else if (getType()->isComplexType()) {
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001193 if (!EvaluateComplexFloat(this, Result.Val, Info))
Anders Carlssonb96c2062008-11-22 21:50:49 +00001194 return false;
Anders Carlssoncb6a2e82008-11-22 22:56:32 +00001195 } else
1196 return false;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001197
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001198 return true;
1199}
1200
1201bool Expr::Evaluate(APValue &Result, ASTContext &Ctx, bool *isEvaluated) const {
1202 EvalResult EvalResult;
1203
1204 if (!Evaluate(EvalResult, Ctx))
1205 return false;
1206
1207 Result = EvalResult.Val;
Anders Carlssonb96c2062008-11-22 21:50:49 +00001208 if (isEvaluated)
Anders Carlsson501da1f2008-11-30 16:51:17 +00001209 *isEvaluated = !EvalResult.HasSideEffects;
Anders Carlsson7f5a96e2008-11-30 16:58:53 +00001210
Anders Carlssonb96c2062008-11-22 21:50:49 +00001211 return true;
Anders Carlssonc7436af2008-07-03 04:20:39 +00001212}
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001213
Chris Lattneref069662008-11-16 21:24:15 +00001214/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001215/// folded, but discard the result.
1216bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson197f6f72008-12-01 06:44:05 +00001217 EvalResult Result;
1218 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner2d9a3f62008-10-06 06:49:02 +00001219}
Anders Carlssone8bd9f22008-11-22 21:04:56 +00001220
1221APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
1222 APValue V;
1223 bool Result = Evaluate(V, Ctx);
1224 assert(Result && "Could not evaluate expression");
1225 assert(V.isInt() && "Expression did not evaluate to integer");
1226
1227 return V.getInt();
1228}