blob: 62e1441484b0cb1aa71aaf99b4dfc90dd7139cfb [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-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"
16#include "clang/AST/Expr.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner54176fd2008-07-12 00:14:42 +000018#include "clang/Basic/Diagnostic.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000022using llvm::APSInt;
Anders Carlssonc44eec62008-07-03 04:20:39 +000023
Chris Lattner87eae5e2008-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 Lattner54176fd2008-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 Lattner87eae5e2008-07-11 22:52:41 +000052 ///
Chris Lattner54176fd2008-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 Lattner87eae5e2008-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);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000067
68
69//===----------------------------------------------------------------------===//
70// Pointer Evaluation
71//===----------------------------------------------------------------------===//
72
Anders Carlssonc754aa62008-07-08 05:13:58 +000073namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +000074class VISIBILITY_HIDDEN PointerExprEvaluator
75 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +000076 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +000077public:
Anders Carlsson2bad1682008-07-08 14:30:00 +000078
Chris Lattner87eae5e2008-07-11 22:52:41 +000079 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +000080
Anders Carlsson2bad1682008-07-08 14:30:00 +000081 APValue VisitStmt(Stmt *S) {
82 // FIXME: Remove this when we support more expressions.
Anders Carlsson650c92f2008-07-08 15:34:11 +000083 printf("Unhandled pointer statement\n");
Anders Carlsson2bad1682008-07-08 14:30:00 +000084 S->dump();
85 return APValue();
86 }
87
88 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
89
Anders Carlsson650c92f2008-07-08 15:34:11 +000090 APValue VisitBinaryOperator(const BinaryOperator *E);
91 APValue VisitCastExpr(const CastExpr* E);
Anders Carlsson650c92f2008-07-08 15:34:11 +000092};
Chris Lattnerf5eeb052008-07-11 18:11:29 +000093} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +000094
Chris Lattner87eae5e2008-07-11 22:52:41 +000095static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +000096 if (!E->getType()->isPointerType())
97 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +000098 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-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 Lattner87eae5e2008-07-11 22:52:41 +0000113 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000114 return APValue();
115
116 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000117 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-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 Lattnerb542afe2008-07-11 19:10:17 +0000130APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-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 Lattner87eae5e2008-07-11 22:52:41 +0000136 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000137 return Result;
138 return APValue();
139 }
140
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000141 if (SubExpr->getType()->isIntegralType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000142 llvm::APSInt Result(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000143 if (EvaluateInteger(SubExpr, Result, Info)) {
144 Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
Chris Lattnerf5eeb052008-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 Lattnerf5eeb052008-07-11 18:11:29 +0000157
158namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000159class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000160 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000161 EvalInfo &Info;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000162 APSInt &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000163public:
Chris Lattner87eae5e2008-07-11 22:52:41 +0000164 IntExprEvaluator(EvalInfo &info, APSInt &result)
165 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000166
Chris Lattner7a767782008-07-11 19:24:49 +0000167 unsigned getIntTypeSizeInBits(QualType T) const {
Chris Lattner54176fd2008-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 Lattner7a767782008-07-11 19:24:49 +0000186 }
187
Anders Carlssonc754aa62008-07-08 05:13:58 +0000188 //===--------------------------------------------------------------------===//
189 // Visitor Methods
190 //===--------------------------------------------------------------------===//
Chris Lattner7a767782008-07-11 19:24:49 +0000191
Chris Lattnerb542afe2008-07-11 19:10:17 +0000192 bool VisitStmt(Stmt *S) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000193 return Error(S->getLocStart(), diag::err_expr_not_constant);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000194 }
195
Chris Lattnerb542afe2008-07-11 19:10:17 +0000196 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000197
Chris Lattner4c4867e2008-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 Lattnerb542afe2008-07-11 19:10:17 +0000216 bool VisitBinaryOperator(const BinaryOperator *E);
217 bool VisitUnaryOperator(const UnaryOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000218
Chris Lattner732b2232008-07-12 01:15:53 +0000219 bool VisitCastExpr(CastExpr* E) {
220 return HandleCast(E->getLParenLoc(), E->getSubExpr(), E->getType());
Anders Carlsson650c92f2008-07-08 15:34:11 +0000221 }
Chris Lattner732b2232008-07-12 01:15:53 +0000222 bool VisitImplicitCastExpr(ImplicitCastExpr* E) {
223 return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType());
Anders Carlsson650c92f2008-07-08 15:34:11 +0000224 }
Chris Lattnerfcee0012008-07-11 21:24:13 +0000225 bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000226 return EvaluateSizeAlignOf(E->isSizeOf(), E->getArgumentType(),
227 E->getType());
Chris Lattnerfcee0012008-07-11 21:24:13 +0000228 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000229
Chris Lattnerfcee0012008-07-11 21:24:13 +0000230private:
Chris Lattner732b2232008-07-12 01:15:53 +0000231 bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType);
Chris Lattnerfcee0012008-07-11 21:24:13 +0000232 bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000233};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000234} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000235
Chris Lattner87eae5e2008-07-11 22:52:41 +0000236static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
237 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
Anders Carlsson650c92f2008-07-08 15:34:11 +0000238}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000239
Chris Lattner4c4867e2008-07-12 00:38:25 +0000240bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
241 // Enums are integer constant exprs.
242 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
243 Result = D->getInitVal();
244 return true;
245 }
246
247 // Otherwise, random variable references are not constants.
248 return Error(E->getLocStart(), diag::err_expr_not_constant);
249}
250
251
252bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
253 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
254 // __builtin_type_compatible_p is a constant.
255 if (E->isBuiltinClassifyType(Result))
256 return true;
257
258 return Error(E->getLocStart(), diag::err_expr_not_constant);
259}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000260
Chris Lattnerb542afe2008-07-11 19:10:17 +0000261bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000262 // The LHS of a constant expr is always evaluated and needed.
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000263 llvm::APSInt RHS(32);
Chris Lattner54176fd2008-07-12 00:14:42 +0000264 if (!Visit(E->getLHS()))
265 return false; // error in subexpression.
266
267 bool OldEval = Info.isEvaluated;
268
269 // The short-circuiting &&/|| operators don't necessarily evaluate their
270 // RHS. Make sure to pass isEvaluated down correctly.
271 if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) ||
272 (E->getOpcode() == BinaryOperator::LOr && Result != 0))
273 Info.isEvaluated = false;
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000274
275 // FIXME: Handle pointer subtraction
276
277 // FIXME Maybe we want to succeed even where we can't evaluate the
278 // right side of LAnd/LOr?
279 // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
Chris Lattner54176fd2008-07-12 00:14:42 +0000280 if (!EvaluateInteger(E->getRHS(), RHS, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000281 return false;
Chris Lattner54176fd2008-07-12 00:14:42 +0000282 Info.isEvaluated = OldEval;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000283
284 switch (E->getOpcode()) {
Chris Lattner54176fd2008-07-12 00:14:42 +0000285 default: return Error(E->getOperatorLoc(), diag::err_expr_not_constant);
286 case BinaryOperator::Mul: Result *= RHS; return true;
287 case BinaryOperator::Add: Result += RHS; return true;
288 case BinaryOperator::Sub: Result -= RHS; return true;
289 case BinaryOperator::And: Result &= RHS; return true;
290 case BinaryOperator::Xor: Result ^= RHS; return true;
291 case BinaryOperator::Or: Result |= RHS; return true;
Chris Lattner75a48812008-07-11 22:15:16 +0000292 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000293 if (RHS == 0)
294 return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero);
Chris Lattner75a48812008-07-11 22:15:16 +0000295 Result /= RHS;
Chris Lattner54176fd2008-07-12 00:14:42 +0000296 return true;
Chris Lattner75a48812008-07-11 22:15:16 +0000297 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000298 if (RHS == 0)
299 return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero);
Chris Lattner75a48812008-07-11 22:15:16 +0000300 Result %= RHS;
Chris Lattner54176fd2008-07-12 00:14:42 +0000301 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000302 case BinaryOperator::Shl:
Chris Lattner54176fd2008-07-12 00:14:42 +0000303 // FIXME: Warn about out of range shift amounts!
Chris Lattnerb542afe2008-07-11 19:10:17 +0000304 Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000305 break;
306 case BinaryOperator::Shr:
Chris Lattnerb542afe2008-07-11 19:10:17 +0000307 Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000308 break;
Chris Lattnerb542afe2008-07-11 19:10:17 +0000309
Chris Lattnerac7cb602008-07-11 19:29:32 +0000310 case BinaryOperator::LT:
311 Result = Result < RHS;
312 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
313 break;
314 case BinaryOperator::GT:
315 Result = Result > RHS;
316 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
317 break;
318 case BinaryOperator::LE:
319 Result = Result <= RHS;
320 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
321 break;
322 case BinaryOperator::GE:
323 Result = Result >= RHS;
324 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
325 break;
326 case BinaryOperator::EQ:
327 Result = Result == RHS;
328 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
329 break;
330 case BinaryOperator::NE:
331 Result = Result != RHS;
332 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
333 break;
Chris Lattner54176fd2008-07-12 00:14:42 +0000334 case BinaryOperator::LAnd:
335 Result = Result != 0 && RHS != 0;
336 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
337 break;
338 case BinaryOperator::LOr:
339 Result = Result != 0 || RHS != 0;
340 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
341 break;
342
Anders Carlsson06a36752008-07-08 05:49:43 +0000343
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000344 case BinaryOperator::Comma:
Chris Lattner54176fd2008-07-12 00:14:42 +0000345 // Result of the comma is just the result of the RHS.
346 Result = RHS;
347
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000348 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
349 // *except* when they are contained within a subexpression that is not
350 // evaluated". Note that Assignment can never happen due to constraints
351 // on the LHS subexpr, so we don't need to check it here.
Chris Lattner54176fd2008-07-12 00:14:42 +0000352 if (!Info.isEvaluated)
353 return true;
354
355 // If the value is evaluated, we can accept it as an extension.
356 return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000357 }
358
359 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000360 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000361}
362
Chris Lattnerfcee0012008-07-11 21:24:13 +0000363/// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result
364/// as a DstTy type.
365bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy,
366 QualType DstTy) {
367 // Return the result in the right width.
368 Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
369 Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
370
371 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
372 if (SrcTy->isVoidType())
373 Result = 1;
374
375 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
376 if (!SrcTy->isConstantSizeType()) {
377 // FIXME: Should we attempt to evaluate this?
378 return false;
379 }
380
381 // GCC extension: sizeof(function) = 1.
382 if (SrcTy->isFunctionType()) {
383 // FIXME: AlignOf shouldn't be unconditionally 4!
384 Result = isSizeOf ? 1 : 4;
385 return true;
386 }
387
388 // Get information about the size or align.
Chris Lattner87eae5e2008-07-11 22:52:41 +0000389 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Chris Lattnerfcee0012008-07-11 21:24:13 +0000390 if (isSizeOf)
391 Result = getIntTypeSizeInBits(SrcTy) / CharSize;
392 else
Chris Lattner87eae5e2008-07-11 22:52:41 +0000393 Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize;
Chris Lattnerfcee0012008-07-11 21:24:13 +0000394 return true;
395}
396
Chris Lattnerb542afe2008-07-11 19:10:17 +0000397bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000398 // Special case unary operators that do not need their subexpression
399 // evaluated. offsetof/sizeof/alignof are all special.
Chris Lattner75a48812008-07-11 22:15:16 +0000400 if (E->isOffsetOfOp()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000401 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
Chris Lattner87eae5e2008-07-11 22:52:41 +0000402 Result = E->evaluateOffsetOf(Info.Ctx);
Chris Lattner75a48812008-07-11 22:15:16 +0000403 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
404 return true;
405 }
406
407 if (E->isSizeOfAlignOfOp())
Chris Lattnerfcee0012008-07-11 21:24:13 +0000408 return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf,
409 E->getSubExpr()->getType(), E->getType());
Chris Lattner75a48812008-07-11 22:15:16 +0000410
Chris Lattner87eae5e2008-07-11 22:52:41 +0000411 // Get the operand value into 'Result'.
412 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +0000413 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000414
Chris Lattner75a48812008-07-11 22:15:16 +0000415 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +0000416 default:
Chris Lattner75a48812008-07-11 22:15:16 +0000417 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
418 // See C99 6.6p3.
Chris Lattner4c4867e2008-07-12 00:38:25 +0000419 return Error(E->getOperatorLoc(), diag::err_expr_not_constant);
Chris Lattner75a48812008-07-11 22:15:16 +0000420 case UnaryOperator::LNot: {
421 bool Val = Result == 0;
422 Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
423 Result = Val;
424 break;
425 }
426 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000427 // FIXME: Should extension allow i-c-e extension expressions in its scope?
428 // If so, we could clear the diagnostic ID.
Chris Lattner75a48812008-07-11 22:15:16 +0000429 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +0000430 // The result is always just the subexpr.
Chris Lattner75a48812008-07-11 22:15:16 +0000431 break;
432 case UnaryOperator::Minus:
433 Result = -Result;
434 break;
435 case UnaryOperator::Not:
436 Result = ~Result;
437 break;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000438 }
439
440 Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
Chris Lattnerb542afe2008-07-11 19:10:17 +0000441 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000442}
443
Chris Lattner732b2232008-07-12 01:15:53 +0000444/// HandleCast - This is used to evaluate implicit or explicit casts where the
445/// result type is integer.
446bool IntExprEvaluator::HandleCast(SourceLocation CastLoc,
447 Expr *SubExpr, QualType DestType) {
Chris Lattner7a767782008-07-11 19:24:49 +0000448 unsigned DestWidth = getIntTypeSizeInBits(DestType);
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000449
450 // Handle simple integer->integer casts.
451 if (SubExpr->getType()->isIntegerType()) {
Chris Lattner732b2232008-07-12 01:15:53 +0000452 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000453 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000454
455 // Figure out if this is a truncate, extend or noop cast.
456 // If the input is signed, do a sign extend, noop, or truncate.
457 if (DestType->isBooleanType()) {
458 // Conversion to bool compares against zero.
459 Result = Result != 0;
460 Result.zextOrTrunc(DestWidth);
Chris Lattner7a767782008-07-11 19:24:49 +0000461 } else
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000462 Result.extOrTrunc(DestWidth);
Chris Lattner732b2232008-07-12 01:15:53 +0000463 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
464 return true;
465 }
466
467 // FIXME: Clean this up!
468 if (SubExpr->getType()->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000469 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000470 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +0000471 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000472 if (LV.getLValueBase())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000473 return false;
Anders Carlsson06a36752008-07-08 05:49:43 +0000474
Anders Carlsson559e56b2008-07-08 16:49:00 +0000475 Result.extOrTrunc(DestWidth);
476 Result = LV.getLValueOffset();
Chris Lattner732b2232008-07-12 01:15:53 +0000477 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
478 return true;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000479 }
480
Chris Lattner732b2232008-07-12 01:15:53 +0000481 if (!SubExpr->getType()->isRealFloatingType())
482 return Error(CastLoc, diag::err_expr_not_constant);
483
484 // FIXME: Generalize floating point constant folding! For now we just permit
485 // which is allowed by integer constant expressions.
486
487 // Allow floating constants that are the immediate operands of casts or that
488 // are parenthesized.
489 const Expr *Operand = SubExpr;
490 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
491 Operand = PE->getSubExpr();
492
493 // If this isn't a floating literal, we can't handle it.
494 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
495 if (!FL)
496 return Error(CastLoc, diag::err_expr_not_constant);
497
498 // If the destination is boolean, compare against zero.
499 if (DestType->isBooleanType()) {
500 Result = !FL->getValue().isZero();
501 Result.zextOrTrunc(DestWidth);
502 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
503 return true;
504 }
505
506 // Determine whether we are converting to unsigned or signed.
507 bool DestSigned = DestType->isSignedIntegerType();
508
509 // FIXME: Warning for overflow.
510 uint64_t Space[4];
511 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
512 llvm::APFloat::rmTowardZero);
513 Result = llvm::APInt(DestWidth, 4, Space);
514 Result.setIsUnsigned(!DestSigned);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000515 return true;
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000516}
Anders Carlsson2bad1682008-07-08 14:30:00 +0000517
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000518//===----------------------------------------------------------------------===//
519// Top level TryEvaluate.
520//===----------------------------------------------------------------------===//
521
Chris Lattnerb542afe2008-07-11 19:10:17 +0000522bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const {
Chris Lattnercf0f51d2008-07-11 19:19:21 +0000523 llvm::APSInt sInt(32);
Anders Carlsson165a70f2008-08-10 17:03:01 +0000524
Chris Lattner87eae5e2008-07-11 22:52:41 +0000525 EvalInfo Info(Ctx);
Anders Carlsson06a36752008-07-08 05:49:43 +0000526 if (getType()->isIntegerType()) {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000527 if (EvaluateInteger(this, sInt, Info)) {
Anders Carlsson06a36752008-07-08 05:49:43 +0000528 Result = APValue(sInt);
529 return true;
530 }
531 } else
Anders Carlssonc754aa62008-07-08 05:13:58 +0000532 return false;
Anders Carlsson165a70f2008-08-10 17:03:01 +0000533
Anders Carlssonc44eec62008-07-03 04:20:39 +0000534 return false;
535}