blob: e98761781124c6c0aa739d9e3c3277119a369523 [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"
Seo Sanghyeonefddb9c2008-07-08 07:23:12 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattner82437da2008-07-12 00:14:42 +000017#include "clang/Basic/Diagnostic.h"
Anders Carlssonc0328012008-07-08 05:49:43 +000018#include "clang/Basic/TargetInfo.h"
Anders Carlssoncad17b52008-07-08 05:13:58 +000019#include "llvm/Support/Compiler.h"
Anders Carlssonc7436af2008-07-03 04:20:39 +000020using namespace clang;
Chris Lattnera823ccf2008-07-11 18:11:29 +000021using llvm::APSInt;
Eli Friedman2f445492008-08-22 00:06:13 +000022using llvm::APFloat;
Anders Carlssonc7436af2008-07-03 04:20:39 +000023
Chris Lattner422373c2008-07-11 22:52:41 +000024/// EvalInfo - This is a private struct used by the evaluator to capture
25/// information about a subexpression as it is folded. It retains information
26/// about the AST context, but also maintains information about the folded
27/// expression.
28///
29/// If an expression could be evaluated, it is still possible it is not a C
30/// "integer constant expression" or constant expression. If not, this struct
31/// captures information about how and why not.
32///
33/// One bit of information passed *into* the request for constant folding
34/// indicates whether the subexpression is "evaluated" or not according to C
35/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
36/// evaluate the expression regardless of what the RHS is, but C only allows
37/// certain things in certain situations.
38struct EvalInfo {
39 ASTContext &Ctx;
40
41 /// isEvaluated - True if the subexpression is required to be evaluated, false
42 /// if it is short-circuited (according to C rules).
43 bool isEvaluated;
44
Chris Lattner82437da2008-07-12 00:14:42 +000045 /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the
46 /// error diagnostic indicating why it is not foldable and DiagLoc indicates a
47 /// caret position for the error. If it is foldable, but the expression is
48 /// not an integer constant expression, ICEDiag contains the extension
49 /// diagnostic to emit which describes why it isn't an integer constant
50 /// expression. If this expression *is* an integer-constant-expr, then
51 /// ICEDiag is zero.
Chris Lattner422373c2008-07-11 22:52:41 +000052 ///
Chris Lattner82437da2008-07-12 00:14:42 +000053 /// The caller can choose to emit this diagnostic or not, depending on whether
54 /// they require an i-c-e or a constant or not. DiagLoc indicates the caret
55 /// position for the report.
56 ///
57 /// If ICEDiag is zero, then this expression is an i-c-e.
Chris Lattner422373c2008-07-11 22:52:41 +000058 unsigned ICEDiag;
59 SourceLocation DiagLoc;
60
61 EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {}
62};
63
64
65static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
66static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Eli Friedman2f445492008-08-22 00:06:13 +000067static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Chris Lattnera823ccf2008-07-11 18:11:29 +000068
69//===----------------------------------------------------------------------===//
70// Pointer Evaluation
71//===----------------------------------------------------------------------===//
72
Anders Carlssoncad17b52008-07-08 05:13:58 +000073namespace {
Anders Carlsson02a34c32008-07-08 14:30:00 +000074class VISIBILITY_HIDDEN PointerExprEvaluator
75 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner422373c2008-07-11 22:52:41 +000076 EvalInfo &Info;
Anders Carlsson02a34c32008-07-08 14:30:00 +000077public:
Anders Carlsson02a34c32008-07-08 14:30:00 +000078
Chris Lattner422373c2008-07-11 22:52:41 +000079 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +000080
Anders Carlsson02a34c32008-07-08 14:30:00 +000081 APValue VisitStmt(Stmt *S) {
82 // FIXME: Remove this when we support more expressions.
Anders Carlssonc43f44b2008-07-08 15:34:11 +000083 printf("Unhandled pointer statement\n");
Anders Carlsson02a34c32008-07-08 14:30:00 +000084 S->dump();
85 return APValue();
86 }
87
88 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
89
Anders Carlssonc43f44b2008-07-08 15:34:11 +000090 APValue VisitBinaryOperator(const BinaryOperator *E);
91 APValue VisitCastExpr(const CastExpr* E);
Anders Carlssonc43f44b2008-07-08 15:34:11 +000092};
Chris Lattnera823ccf2008-07-11 18:11:29 +000093} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +000094
Chris Lattner422373c2008-07-11 22:52:41 +000095static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnera823ccf2008-07-11 18:11:29 +000096 if (!E->getType()->isPointerType())
97 return false;
Chris Lattner422373c2008-07-11 22:52:41 +000098 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnera823ccf2008-07-11 18:11:29 +000099 return Result.isLValue();
100}
101
102APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
103 if (E->getOpcode() != BinaryOperator::Add &&
104 E->getOpcode() != BinaryOperator::Sub)
105 return APValue();
106
107 const Expr *PExp = E->getLHS();
108 const Expr *IExp = E->getRHS();
109 if (IExp->getType()->isPointerType())
110 std::swap(PExp, IExp);
111
112 APValue ResultLValue;
Chris Lattner422373c2008-07-11 22:52:41 +0000113 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000114 return APValue();
115
116 llvm::APSInt AdditionalOffset(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000117 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000118 return APValue();
119
120 uint64_t Offset = ResultLValue.getLValueOffset();
121 if (E->getOpcode() == BinaryOperator::Add)
122 Offset += AdditionalOffset.getZExtValue();
123 else
124 Offset -= AdditionalOffset.getZExtValue();
125
126 return APValue(ResultLValue.getLValueBase(), Offset);
127}
128
129
Chris Lattnera42f09a2008-07-11 19:10:17 +0000130APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000131 const Expr* SubExpr = E->getSubExpr();
132
133 // Check for pointer->pointer cast
134 if (SubExpr->getType()->isPointerType()) {
135 APValue Result;
Chris Lattner422373c2008-07-11 22:52:41 +0000136 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnera823ccf2008-07-11 18:11:29 +0000137 return Result;
138 return APValue();
139 }
140
Eli Friedman3e64dd72008-07-27 05:46:18 +0000141 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnera823ccf2008-07-11 18:11:29 +0000142 llvm::APSInt Result(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000143 if (EvaluateInteger(SubExpr, Result, Info)) {
144 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnera823ccf2008-07-11 18:11:29 +0000145 return APValue(0, Result.getZExtValue());
146 }
147 }
148
149 assert(0 && "Unhandled cast");
150 return APValue();
151}
152
153
154//===----------------------------------------------------------------------===//
155// Integer Evaluation
156//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000157
158namespace {
Anders Carlssoncad17b52008-07-08 05:13:58 +0000159class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnera42f09a2008-07-11 19:10:17 +0000160 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner422373c2008-07-11 22:52:41 +0000161 EvalInfo &Info;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000162 APSInt &Result;
Anders Carlssoncad17b52008-07-08 05:13:58 +0000163public:
Chris Lattner422373c2008-07-11 22:52:41 +0000164 IntExprEvaluator(EvalInfo &info, APSInt &result)
165 : Info(info), Result(result) {}
Chris Lattnera823ccf2008-07-11 18:11:29 +0000166
Chris Lattner2c99c712008-07-11 19:24:49 +0000167 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner82437da2008-07-12 00:14:42 +0000168 return (unsigned)Info.Ctx.getIntWidth(T);
169 }
170
171 bool Extension(SourceLocation L, diag::kind D) {
172 Info.DiagLoc = L;
173 Info.ICEDiag = D;
174 return true; // still a constant.
175 }
176
177 bool Error(SourceLocation L, diag::kind D) {
178 // If this is in an unevaluated portion of the subexpression, ignore the
179 // error.
180 if (!Info.isEvaluated)
181 return true;
182
183 Info.DiagLoc = L;
184 Info.ICEDiag = D;
185 return false;
Chris Lattner2c99c712008-07-11 19:24:49 +0000186 }
187
Anders Carlssoncad17b52008-07-08 05:13:58 +0000188 //===--------------------------------------------------------------------===//
189 // Visitor Methods
190 //===--------------------------------------------------------------------===//
Chris Lattner2c99c712008-07-11 19:24:49 +0000191
Chris Lattnera42f09a2008-07-11 19:10:17 +0000192 bool VisitStmt(Stmt *S) {
Chris Lattner82437da2008-07-12 00:14:42 +0000193 return Error(S->getLocStart(), diag::err_expr_not_constant);
Anders Carlssoncad17b52008-07-08 05:13:58 +0000194 }
195
Chris Lattnera42f09a2008-07-11 19:10:17 +0000196 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssoncad17b52008-07-08 05:13:58 +0000197
Chris Lattner15e59112008-07-12 00:38:25 +0000198 bool VisitIntegerLiteral(const IntegerLiteral *E) {
199 Result = E->getValue();
200 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
201 return true;
202 }
203 bool VisitCharacterLiteral(const CharacterLiteral *E) {
204 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
205 Result = E->getValue();
206 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
207 return true;
208 }
209 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
210 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
211 Result = Info.Ctx.typesAreCompatible(E->getArgType1(), E->getArgType2());
212 return true;
213 }
214 bool VisitDeclRefExpr(const DeclRefExpr *E);
215 bool VisitCallExpr(const CallExpr *E);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000216 bool VisitBinaryOperator(const BinaryOperator *E);
217 bool VisitUnaryOperator(const UnaryOperator *E);
Anders Carlssonc0328012008-07-08 05:49:43 +0000218
Chris Lattnerff579ff2008-07-12 01:15:53 +0000219 bool VisitCastExpr(CastExpr* E) {
Chris Lattnerff579ff2008-07-12 01:15:53 +0000220 return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType());
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000221 }
Chris Lattner265a0892008-07-11 21:24:13 +0000222 bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
Chris Lattner82437da2008-07-12 00:14:42 +0000223 return EvaluateSizeAlignOf(E->isSizeOf(), E->getArgumentType(),
224 E->getType());
Chris Lattner265a0892008-07-11 21:24:13 +0000225 }
Chris Lattner15e59112008-07-12 00:38:25 +0000226
Chris Lattner265a0892008-07-11 21:24:13 +0000227private:
Chris Lattnerff579ff2008-07-12 01:15:53 +0000228 bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType);
Chris Lattner265a0892008-07-11 21:24:13 +0000229 bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000230};
Chris Lattnera823ccf2008-07-11 18:11:29 +0000231} // end anonymous namespace
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000232
Chris Lattner422373c2008-07-11 22:52:41 +0000233static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
234 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000235}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000236
Chris Lattner15e59112008-07-12 00:38:25 +0000237bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
238 // Enums are integer constant exprs.
239 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
240 Result = D->getInitVal();
241 return true;
242 }
243
244 // Otherwise, random variable references are not constants.
245 return Error(E->getLocStart(), diag::err_expr_not_constant);
246}
247
248
249bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
250 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
251 // __builtin_type_compatible_p is a constant.
252 if (E->isBuiltinClassifyType(Result))
253 return true;
254
255 return Error(E->getLocStart(), diag::err_expr_not_constant);
256}
Anders Carlssonc43f44b2008-07-08 15:34:11 +0000257
Chris Lattnera42f09a2008-07-11 19:10:17 +0000258bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssond1aa5812008-07-08 14:35:21 +0000259 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssond1aa5812008-07-08 14:35:21 +0000260 llvm::APSInt RHS(32);
Chris Lattner82437da2008-07-12 00:14:42 +0000261 if (!Visit(E->getLHS()))
262 return false; // error in subexpression.
263
264 bool OldEval = Info.isEvaluated;
265
266 // The short-circuiting &&/|| operators don't necessarily evaluate their
267 // RHS. Make sure to pass isEvaluated down correctly.
268 if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) ||
269 (E->getOpcode() == BinaryOperator::LOr && Result != 0))
270 Info.isEvaluated = false;
Eli Friedman3e64dd72008-07-27 05:46:18 +0000271
272 // FIXME: Handle pointer subtraction
273
274 // FIXME Maybe we want to succeed even where we can't evaluate the
275 // right side of LAnd/LOr?
276 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner82437da2008-07-12 00:14:42 +0000277 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000278 return false;
Chris Lattner82437da2008-07-12 00:14:42 +0000279 Info.isEvaluated = OldEval;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000280
281 switch (E->getOpcode()) {
Chris Lattner82437da2008-07-12 00:14:42 +0000282 default: return Error(E->getOperatorLoc(), diag::err_expr_not_constant);
283 case BinaryOperator::Mul: Result *= RHS; return true;
284 case BinaryOperator::Add: Result += RHS; return true;
285 case BinaryOperator::Sub: Result -= RHS; return true;
286 case BinaryOperator::And: Result &= RHS; return true;
287 case BinaryOperator::Xor: Result ^= RHS; return true;
288 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000289 case BinaryOperator::Div:
Chris Lattner82437da2008-07-12 00:14:42 +0000290 if (RHS == 0)
291 return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero);
Chris Lattner400d7402008-07-11 22:15:16 +0000292 Result /= RHS;
Chris Lattner82437da2008-07-12 00:14:42 +0000293 return true;
Chris Lattner400d7402008-07-11 22:15:16 +0000294 case BinaryOperator::Rem:
Chris Lattner82437da2008-07-12 00:14:42 +0000295 if (RHS == 0)
296 return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero);
Chris Lattner400d7402008-07-11 22:15:16 +0000297 Result %= RHS;
Chris Lattner82437da2008-07-12 00:14:42 +0000298 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000299 case BinaryOperator::Shl:
Chris Lattner82437da2008-07-12 00:14:42 +0000300 // FIXME: Warn about out of range shift amounts!
Chris Lattnera42f09a2008-07-11 19:10:17 +0000301 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000302 break;
303 case BinaryOperator::Shr:
Chris Lattnera42f09a2008-07-11 19:10:17 +0000304 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000305 break;
Chris Lattnera42f09a2008-07-11 19:10:17 +0000306
Chris Lattner045502c2008-07-11 19:29:32 +0000307 case BinaryOperator::LT:
308 Result = Result < RHS;
309 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
310 break;
311 case BinaryOperator::GT:
312 Result = Result > RHS;
313 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
314 break;
315 case BinaryOperator::LE:
316 Result = Result <= RHS;
317 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
318 break;
319 case BinaryOperator::GE:
320 Result = Result >= RHS;
321 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
322 break;
323 case BinaryOperator::EQ:
324 Result = Result == RHS;
325 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
326 break;
327 case BinaryOperator::NE:
328 Result = Result != RHS;
329 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
330 break;
Chris Lattner82437da2008-07-12 00:14:42 +0000331 case BinaryOperator::LAnd:
332 Result = Result != 0 && RHS != 0;
333 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
334 break;
335 case BinaryOperator::LOr:
336 Result = Result != 0 || RHS != 0;
337 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
338 break;
339
Anders Carlssonc0328012008-07-08 05:49:43 +0000340
Anders Carlssond1aa5812008-07-08 14:35:21 +0000341 case BinaryOperator::Comma:
Chris Lattner82437da2008-07-12 00:14:42 +0000342 // Result of the comma is just the result of the RHS.
343 Result = RHS;
344
Anders Carlssond1aa5812008-07-08 14:35:21 +0000345 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
346 // *except* when they are contained within a subexpression that is not
347 // evaluated". Note that Assignment can never happen due to constraints
348 // on the LHS subexpr, so we don't need to check it here.
Chris Lattner82437da2008-07-12 00:14:42 +0000349 if (!Info.isEvaluated)
350 return true;
351
352 // If the value is evaluated, we can accept it as an extension.
353 return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000354 }
355
356 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000357 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000358}
359
Chris Lattner265a0892008-07-11 21:24:13 +0000360/// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result
361/// as a DstTy type.
362bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy,
363 QualType DstTy) {
364 // Return the result in the right width.
365 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
366 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
367
368 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
369 if (SrcTy->isVoidType())
370 Result = 1;
371
372 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
373 if (!SrcTy->isConstantSizeType()) {
374 // FIXME: Should we attempt to evaluate this?
375 return false;
376 }
377
378 // GCC extension: sizeof(function) = 1.
379 if (SrcTy->isFunctionType()) {
380 // FIXME: AlignOf shouldn't be unconditionally 4!
381 Result = isSizeOf ? 1 : 4;
382 return true;
383 }
384
385 // Get information about the size or align.
Chris Lattner422373c2008-07-11 22:52:41 +0000386 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattner265a0892008-07-11 21:24:13 +0000387 if (isSizeOf)
388 Result = getIntTypeSizeInBits(SrcTy) / CharSize;
389 else
Chris Lattner422373c2008-07-11 22:52:41 +0000390 Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize;
Chris Lattner265a0892008-07-11 21:24:13 +0000391 return true;
392}
393
Chris Lattnera42f09a2008-07-11 19:10:17 +0000394bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner15e59112008-07-12 00:38:25 +0000395 // Special case unary operators that do not need their subexpression
396 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner400d7402008-07-11 22:15:16 +0000397 if (E->isOffsetOfOp()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000398 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner422373c2008-07-11 22:52:41 +0000399 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner400d7402008-07-11 22:15:16 +0000400 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
401 return true;
402 }
403
404 if (E->isSizeOfAlignOfOp())
Chris Lattner265a0892008-07-11 21:24:13 +0000405 return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf,
406 E->getSubExpr()->getType(), E->getType());
Chris Lattner400d7402008-07-11 22:15:16 +0000407
Chris Lattner422373c2008-07-11 22:52:41 +0000408 // Get the operand value into 'Result'.
409 if (!Visit(E->getSubExpr()))
Chris Lattner400d7402008-07-11 22:15:16 +0000410 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000411
Chris Lattner400d7402008-07-11 22:15:16 +0000412 switch (E->getOpcode()) {
Chris Lattner15e59112008-07-12 00:38:25 +0000413 default:
Chris Lattner400d7402008-07-11 22:15:16 +0000414 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
415 // See C99 6.6p3.
Chris Lattner15e59112008-07-12 00:38:25 +0000416 return Error(E->getOperatorLoc(), diag::err_expr_not_constant);
Chris Lattner400d7402008-07-11 22:15:16 +0000417 case UnaryOperator::LNot: {
418 bool Val = Result == 0;
419 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
420 Result = Val;
421 break;
422 }
423 case UnaryOperator::Extension:
Chris Lattner15e59112008-07-12 00:38:25 +0000424 // FIXME: Should extension allow i-c-e extension expressions in its scope?
425 // If so, we could clear the diagnostic ID.
Chris Lattner400d7402008-07-11 22:15:16 +0000426 case UnaryOperator::Plus:
Chris Lattner15e59112008-07-12 00:38:25 +0000427 // The result is always just the subexpr.
Chris Lattner400d7402008-07-11 22:15:16 +0000428 break;
429 case UnaryOperator::Minus:
430 Result = -Result;
431 break;
432 case UnaryOperator::Not:
433 Result = ~Result;
434 break;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000435 }
436
437 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnera42f09a2008-07-11 19:10:17 +0000438 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000439}
440
Chris Lattnerff579ff2008-07-12 01:15:53 +0000441/// HandleCast - This is used to evaluate implicit or explicit casts where the
442/// result type is integer.
443bool IntExprEvaluator::HandleCast(SourceLocation CastLoc,
444 Expr *SubExpr, QualType DestType) {
Chris Lattner2c99c712008-07-11 19:24:49 +0000445 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssond1aa5812008-07-08 14:35:21 +0000446
447 // Handle simple integer->integer casts.
448 if (SubExpr->getType()->isIntegerType()) {
Chris Lattnerff579ff2008-07-12 01:15:53 +0000449 if (!Visit(SubExpr))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000450 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000451
452 // Figure out if this is a truncate, extend or noop cast.
453 // If the input is signed, do a sign extend, noop, or truncate.
454 if (DestType->isBooleanType()) {
455 // Conversion to bool compares against zero.
456 Result = Result != 0;
457 Result.zextOrTrunc(DestWidth);
Chris Lattner2c99c712008-07-11 19:24:49 +0000458 } else
Anders Carlssond1aa5812008-07-08 14:35:21 +0000459 Result.extOrTrunc(DestWidth);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000460 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
461 return true;
462 }
463
464 // FIXME: Clean this up!
465 if (SubExpr->getType()->isPointerType()) {
Anders Carlssond1aa5812008-07-08 14:35:21 +0000466 APValue LV;
Chris Lattner422373c2008-07-11 22:52:41 +0000467 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnera42f09a2008-07-11 19:10:17 +0000468 return false;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000469 if (LV.getLValueBase())
Chris Lattnera42f09a2008-07-11 19:10:17 +0000470 return false;
Anders Carlssonc0328012008-07-08 05:49:43 +0000471
Anders Carlsson8ab15c82008-07-08 16:49:00 +0000472 Result.extOrTrunc(DestWidth);
473 Result = LV.getLValueOffset();
Chris Lattnerff579ff2008-07-12 01:15:53 +0000474 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
475 return true;
Anders Carlsson02a34c32008-07-08 14:30:00 +0000476 }
477
Chris Lattnerff579ff2008-07-12 01:15:53 +0000478 if (!SubExpr->getType()->isRealFloatingType())
479 return Error(CastLoc, diag::err_expr_not_constant);
480
Eli Friedman2f445492008-08-22 00:06:13 +0000481 APFloat F(0.0);
482 if (!EvaluateFloat(SubExpr, F, Info))
Chris Lattnerff579ff2008-07-12 01:15:53 +0000483 return Error(CastLoc, diag::err_expr_not_constant);
Eli Friedman2f445492008-08-22 00:06:13 +0000484
Chris Lattnerff579ff2008-07-12 01:15:53 +0000485 // If the destination is boolean, compare against zero.
486 if (DestType->isBooleanType()) {
Eli Friedman2f445492008-08-22 00:06:13 +0000487 Result = !F.isZero();
Chris Lattnerff579ff2008-07-12 01:15:53 +0000488 Result.zextOrTrunc(DestWidth);
489 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
490 return true;
491 }
492
493 // Determine whether we are converting to unsigned or signed.
494 bool DestSigned = DestType->isSignedIntegerType();
495
496 // FIXME: Warning for overflow.
497 uint64_t Space[4];
Eli Friedman2f445492008-08-22 00:06:13 +0000498 (void)F.convertToInteger(Space, DestWidth, DestSigned,
499 llvm::APFloat::rmTowardZero);
Chris Lattnerff579ff2008-07-12 01:15:53 +0000500 Result = llvm::APInt(DestWidth, 4, Space);
501 Result.setIsUnsigned(!DestSigned);
Chris Lattnera42f09a2008-07-11 19:10:17 +0000502 return true;
Anders Carlssond1aa5812008-07-08 14:35:21 +0000503}
Anders Carlsson02a34c32008-07-08 14:30:00 +0000504
Chris Lattnera823ccf2008-07-11 18:11:29 +0000505//===----------------------------------------------------------------------===//
Eli Friedman2f445492008-08-22 00:06:13 +0000506// Float Evaluation
507//===----------------------------------------------------------------------===//
508
509namespace {
510class VISIBILITY_HIDDEN FloatExprEvaluator
511 : public StmtVisitor<FloatExprEvaluator, bool> {
512 EvalInfo &Info;
513 APFloat &Result;
514public:
515 FloatExprEvaluator(EvalInfo &info, APFloat &result)
516 : Info(info), Result(result) {}
517
518 bool VisitStmt(Stmt *S) {
519 return false;
520 }
521
522 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
523
524 bool VisitBinaryOperator(const BinaryOperator *E);
525 bool VisitFloatingLiteral(const FloatingLiteral *E);
526};
527} // end anonymous namespace
528
529static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
530 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
531}
532
533bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
534 // FIXME: Diagnostics? I really don't understand how the warnings
535 // and errors are supposed to work.
536 APFloat LHS(0.0), RHS(0.0);
537 if (!EvaluateFloat(E->getLHS(), Result, Info))
538 return false;
539 if (!EvaluateFloat(E->getRHS(), RHS, Info))
540 return false;
541
542 switch (E->getOpcode()) {
543 default: return false;
544 case BinaryOperator::Mul:
545 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
546 return true;
547 case BinaryOperator::Add:
548 Result.add(RHS, APFloat::rmNearestTiesToEven);
549 return true;
550 case BinaryOperator::Sub:
551 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
552 return true;
553 case BinaryOperator::Div:
554 Result.divide(RHS, APFloat::rmNearestTiesToEven);
555 return true;
556 case BinaryOperator::Rem:
557 Result.mod(RHS, APFloat::rmNearestTiesToEven);
558 return true;
559 }
560}
561
562bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
563 Result = E->getValue();
564 return true;
565}
566
567//===----------------------------------------------------------------------===//
Chris Lattnera823ccf2008-07-11 18:11:29 +0000568// Top level TryEvaluate.
569//===----------------------------------------------------------------------===//
570
Chris Lattnera42f09a2008-07-11 19:10:17 +0000571bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const {
Chris Lattner422373c2008-07-11 22:52:41 +0000572 EvalInfo Info(Ctx);
Anders Carlssonc0328012008-07-08 05:49:43 +0000573 if (getType()->isIntegerType()) {
Eli Friedman2f445492008-08-22 00:06:13 +0000574 llvm::APSInt sInt(32);
Chris Lattner422373c2008-07-11 22:52:41 +0000575 if (EvaluateInteger(this, sInt, Info)) {
Anders Carlssonc0328012008-07-08 05:49:43 +0000576 Result = APValue(sInt);
577 return true;
578 }
Eli Friedman2f445492008-08-22 00:06:13 +0000579 } else if (getType()->isPointerType()) {
580 if (EvaluatePointer(this, Result, Info)) {
581 return true;
582 }
583 } else if (getType()->isRealFloatingType()) {
584 llvm::APFloat f(0.0);
585 if (EvaluateFloat(this, f, Info)) {
586 Result = APValue(f);
587 return true;
588 }
589 }
Anders Carlsson47968a92008-08-10 17:03:01 +0000590
Anders Carlssonc7436af2008-07-03 04:20:39 +0000591 return false;
592}