Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 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 Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 20 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 21 | using llvm::APSInt; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 22 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 23 | #define USE_NEW_EVALUATOR 0 |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 25 | static bool CalcFakeICEVal(const Expr *Expr, |
| 26 | llvm::APSInt &Result, |
| 27 | ASTContext &Context) { |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 28 | // Calculate the value of an expression that has a calculatable |
| 29 | // value, but isn't an ICE. Currently, this only supports |
| 30 | // a very narrow set of extensions, but it can be expanded if needed. |
| 31 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr)) |
| 32 | return CalcFakeICEVal(PE->getSubExpr(), Result, Context); |
| 33 | |
| 34 | if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) { |
| 35 | QualType CETy = CE->getType(); |
| 36 | if ((CETy->isIntegralType() && !CETy->isBooleanType()) || |
| 37 | CETy->isPointerType()) { |
| 38 | if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) { |
| 39 | Result.extOrTrunc(Context.getTypeSize(CETy)); |
| 40 | // FIXME: This assumes pointers are signed. |
| 41 | Result.setIsSigned(CETy->isSignedIntegerType() || |
| 42 | CETy->isPointerType()); |
| 43 | return true; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (Expr->getType()->isIntegralType()) |
| 49 | return Expr->isIntegerConstantExpr(Result, Context); |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 54 | static bool EvaluatePointer(const Expr *E, APValue &Result, ASTContext &Ctx); |
| 55 | static bool EvaluateInteger(const Expr *E, APSInt &Result, ASTContext &Ctx); |
| 56 | |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // Pointer Evaluation |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 62 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 64 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
| 65 | ASTContext &Ctx; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 66 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 67 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 68 | PointerExprEvaluator(ASTContext &ctx) : Ctx(ctx) {} |
| 69 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 70 | APValue VisitStmt(Stmt *S) { |
| 71 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 72 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 73 | S->dump(); |
| 74 | return APValue(); |
| 75 | } |
| 76 | |
| 77 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 78 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 79 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 80 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 81 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 82 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 83 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 84 | static bool EvaluatePointer(const Expr* E, APValue& Result, ASTContext &Ctx) { |
| 85 | if (!E->getType()->isPointerType()) |
| 86 | return false; |
| 87 | Result = PointerExprEvaluator(Ctx).Visit(const_cast<Expr*>(E)); |
| 88 | return Result.isLValue(); |
| 89 | } |
| 90 | |
| 91 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 92 | if (E->getOpcode() != BinaryOperator::Add && |
| 93 | E->getOpcode() != BinaryOperator::Sub) |
| 94 | return APValue(); |
| 95 | |
| 96 | const Expr *PExp = E->getLHS(); |
| 97 | const Expr *IExp = E->getRHS(); |
| 98 | if (IExp->getType()->isPointerType()) |
| 99 | std::swap(PExp, IExp); |
| 100 | |
| 101 | APValue ResultLValue; |
| 102 | if (!EvaluatePointer(PExp, ResultLValue, Ctx)) |
| 103 | return APValue(); |
| 104 | |
| 105 | llvm::APSInt AdditionalOffset(32); |
| 106 | if (!EvaluateInteger(IExp, AdditionalOffset, Ctx)) |
| 107 | return APValue(); |
| 108 | |
| 109 | uint64_t Offset = ResultLValue.getLValueOffset(); |
| 110 | if (E->getOpcode() == BinaryOperator::Add) |
| 111 | Offset += AdditionalOffset.getZExtValue(); |
| 112 | else |
| 113 | Offset -= AdditionalOffset.getZExtValue(); |
| 114 | |
| 115 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 116 | } |
| 117 | |
| 118 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 119 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 120 | const Expr* SubExpr = E->getSubExpr(); |
| 121 | |
| 122 | // Check for pointer->pointer cast |
| 123 | if (SubExpr->getType()->isPointerType()) { |
| 124 | APValue Result; |
| 125 | if (EvaluatePointer(SubExpr, Result, Ctx)) |
| 126 | return Result; |
| 127 | return APValue(); |
| 128 | } |
| 129 | |
| 130 | if (SubExpr->getType()->isArithmeticType()) { |
| 131 | llvm::APSInt Result(32); |
| 132 | if (EvaluateInteger(SubExpr, Result, Ctx)) { |
| 133 | Result.extOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(E->getType()))); |
| 134 | return APValue(0, Result.getZExtValue()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | assert(0 && "Unhandled cast"); |
| 139 | return APValue(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | // Integer Evaluation |
| 145 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 146 | |
| 147 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 148 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 149 | : public StmtVisitor<IntExprEvaluator, bool> { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 150 | ASTContext &Ctx; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 151 | APSInt &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 152 | public: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 153 | IntExprEvaluator(ASTContext &ctx, APSInt &result) : Ctx(ctx), Result(result){} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 155 | unsigned getIntTypeSizeInBits(QualType T) const { |
| 156 | return (unsigned)Ctx.getTypeSize(T); |
| 157 | } |
| 158 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 159 | //===--------------------------------------------------------------------===// |
| 160 | // Visitor Methods |
| 161 | //===--------------------------------------------------------------------===// |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 162 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 163 | bool VisitStmt(Stmt *S) { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 164 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 165 | printf("unhandled int expression"); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 166 | S->dump(); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 167 | return false; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 170 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 171 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 172 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 173 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 174 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 175 | bool HandleCast(const Expr* SubExpr, QualType DestType); |
| 176 | bool VisitCastExpr(const CastExpr* E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 177 | return HandleCast(E->getSubExpr(), E->getType()); |
| 178 | } |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 179 | bool VisitImplicitCastExpr(const ImplicitCastExpr* E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 180 | return HandleCast(E->getSubExpr(), E->getType()); |
| 181 | } |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 182 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 183 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 184 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 185 | Result = E->getValue(); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 186 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 187 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 188 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 189 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 190 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 191 | static bool EvaluateInteger(const Expr* E, APSInt &Result, ASTContext &Ctx) { |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 192 | return IntExprEvaluator(Ctx, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 193 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 194 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 195 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 196 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 197 | // The LHS of a constant expr is always evaluated and needed. |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 198 | if (!Visit(E->getLHS())) |
| 199 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 200 | |
| 201 | llvm::APSInt RHS(32); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 202 | if (!EvaluateInteger(E->getRHS(), RHS, Ctx)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 203 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 204 | |
| 205 | switch (E->getOpcode()) { |
| 206 | default: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 207 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 208 | case BinaryOperator::Mul: |
| 209 | Result *= RHS; |
| 210 | break; |
| 211 | case BinaryOperator::Div: |
| 212 | if (RHS == 0) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 213 | return false; |
| 214 | Result /= RHS; |
| 215 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 216 | case BinaryOperator::Rem: |
| 217 | if (RHS == 0) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 218 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 219 | Result %= RHS; |
| 220 | break; |
| 221 | case BinaryOperator::Add: Result += RHS; break; |
| 222 | case BinaryOperator::Sub: Result -= RHS; break; |
| 223 | case BinaryOperator::Shl: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 224 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 225 | break; |
| 226 | case BinaryOperator::Shr: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 227 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 228 | break; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 229 | |
| 230 | // FIXME: Need to set the result width? |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 231 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 232 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 233 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 234 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 235 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 236 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 237 | case BinaryOperator::And: Result &= RHS; break; |
| 238 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 239 | case BinaryOperator::Or: Result |= RHS; break; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 241 | case BinaryOperator::Comma: |
| 242 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 243 | // *except* when they are contained within a subexpression that is not |
| 244 | // evaluated". Note that Assignment can never happen due to constraints |
| 245 | // on the LHS subexpr, so we don't need to check it here. |
| 246 | // FIXME: Need to come up with an efficient way to deal with the C99 |
| 247 | // rules on evaluation while still evaluating this. Maybe a |
| 248 | // "evaluated comma" out parameter? |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 249 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 253 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 256 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 257 | if (E->isOffsetOfOp()) |
| 258 | Result = E->evaluateOffsetOf(Ctx); |
| 259 | else if (E->isSizeOfAlignOfOp()) { |
| 260 | // Return the result in the right width. |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 261 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 262 | |
| 263 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 264 | if (E->getSubExpr()->getType()->isVoidType()) |
| 265 | Result = 1; |
| 266 | |
| 267 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 268 | if (!E->getSubExpr()->getType()->isConstantSizeType()) { |
| 269 | // FIXME: Should we attempt to evaluate this? |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 270 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Get information about the size or align. |
| 274 | if (E->getSubExpr()->getType()->isFunctionType()) { |
| 275 | // GCC extension: sizeof(function) = 1. |
| 276 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 277 | Result = E->getOpcode() == UnaryOperator::AlignOf ? 4 : 1; |
| 278 | } else { |
| 279 | unsigned CharSize = Ctx.Target.getCharWidth(); |
| 280 | if (E->getOpcode() == UnaryOperator::AlignOf) |
| 281 | Result = Ctx.getTypeAlign(E->getSubExpr()->getType()) / CharSize; |
| 282 | else |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 283 | Result = getIntTypeSizeInBits(E->getSubExpr()->getType()) / CharSize; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 284 | } |
| 285 | } else { |
| 286 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 287 | // operand. This affects C99 6.6p3. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 288 | if (!EvaluateInteger(E->getSubExpr(), Result, Ctx)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 289 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 290 | |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 291 | switch (E->getOpcode()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 292 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 293 | // See C99 6.6p3. |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 294 | default: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 295 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 296 | case UnaryOperator::Extension: |
| 297 | assert(0 && "Handle UnaryOperator::Extension"); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 298 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 299 | case UnaryOperator::LNot: { |
| 300 | bool Val = Result == 0; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 301 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 302 | Result = Val; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 303 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 304 | } |
| 305 | case UnaryOperator::Plus: |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 306 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 307 | case UnaryOperator::Minus: |
| 308 | Result = -Result; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 309 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 310 | case UnaryOperator::Not: |
| 311 | Result = ~Result; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 312 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 317 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 320 | bool IntExprEvaluator::HandleCast(const Expr* SubExpr, QualType DestType) { |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 321 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 322 | |
| 323 | // Handle simple integer->integer casts. |
| 324 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 325 | if (!EvaluateInteger(SubExpr, Result, Ctx)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 326 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 327 | |
| 328 | // Figure out if this is a truncate, extend or noop cast. |
| 329 | // If the input is signed, do a sign extend, noop, or truncate. |
| 330 | if (DestType->isBooleanType()) { |
| 331 | // Conversion to bool compares against zero. |
| 332 | Result = Result != 0; |
| 333 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 334 | } else |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 335 | Result.extOrTrunc(DestWidth); |
| 336 | } else if (SubExpr->getType()->isPointerType()) { |
| 337 | APValue LV; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 338 | if (!EvaluatePointer(SubExpr, LV, Ctx)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 339 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 340 | if (LV.getLValueBase()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 341 | return false; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 342 | |
Anders Carlsson | 559e56b | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 343 | Result.extOrTrunc(DestWidth); |
| 344 | Result = LV.getLValueOffset(); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 345 | } else { |
| 346 | assert(0 && "Unhandled cast!"); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 349 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 350 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 351 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 352 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 353 | bool IntExprEvaluator:: |
| 354 | VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 355 | // Return the result in the right width. |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 356 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 357 | |
| 358 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 359 | if (E->getArgumentType()->isVoidType()) { |
| 360 | Result = 1; |
| 361 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 362 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // alignof always evaluates to a constant, sizeof does if arg is not VLA. |
| 366 | if (E->isSizeOf() && !E->getArgumentType()->isConstantSizeType()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 367 | return false; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 368 | |
| 369 | // Get information about the size or align. |
| 370 | if (E->getArgumentType()->isFunctionType()) { |
| 371 | // GCC extension: sizeof(function) = 1. |
| 372 | Result = E->isSizeOf() ? 1 : 4; |
| 373 | } else { |
| 374 | unsigned CharSize = Ctx.Target.getCharWidth(); |
| 375 | if (E->isSizeOf()) |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame^] | 376 | Result = getIntTypeSizeInBits(E->getArgumentType()) / CharSize; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 377 | else |
| 378 | Result = Ctx.getTypeAlign(E->getArgumentType()) / CharSize; |
| 379 | } |
| 380 | |
| 381 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 382 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 385 | //===----------------------------------------------------------------------===// |
| 386 | // Top level TryEvaluate. |
| 387 | //===----------------------------------------------------------------------===// |
| 388 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 389 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | cf0f51d | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 390 | llvm::APSInt sInt(32); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 391 | #if USE_NEW_EVALUATOR |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 392 | if (getType()->isIntegerType()) { |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 393 | if (EvaluateInteger(this, sInt, Ctx)) { |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 394 | Result = APValue(sInt); |
| 395 | return true; |
| 396 | } |
| 397 | } else |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 398 | return false; |
| 399 | |
| 400 | #else |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 401 | if (CalcFakeICEVal(this, sInt, Ctx)) { |
| 402 | Result = APValue(sInt); |
| 403 | return true; |
| 404 | } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 405 | #endif |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 406 | |
| 407 | return false; |
| 408 | } |