Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c7436af | 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 | efddb9c | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 20 | using namespace clang; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 21 | using llvm::APSInt; |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 22 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 23 | #define USE_NEW_EVALUATOR 0 |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | a823ccf | 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 | c7436af | 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 | a823ccf | 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 | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 62 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 64 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
| 65 | ASTContext &Ctx; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 66 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 67 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 68 | PointerExprEvaluator(ASTContext &ctx) : Ctx(ctx) {} |
| 69 | |
Anders Carlsson | 02a34c3 | 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 | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 72 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 02a34c3 | 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 | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 79 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 80 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 81 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 82 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 83 | |
Chris Lattner | a823ccf | 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 | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 119 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 146 | |
| 147 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 148 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 149 | : public StmtVisitor<IntExprEvaluator, bool> { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 150 | ASTContext &Ctx; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 151 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 152 | public: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 153 | IntExprEvaluator(ASTContext &ctx, APSInt &result) : Ctx(ctx), Result(result){} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 2c99c71 | 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 | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 159 | //===--------------------------------------------------------------------===// |
| 160 | // Visitor Methods |
| 161 | //===--------------------------------------------------------------------===// |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 162 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 163 | bool VisitStmt(Stmt *S) { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 164 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 165 | printf("unhandled int expression"); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 166 | S->dump(); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 167 | return false; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 170 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 171 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 172 | bool VisitBinaryOperator(const BinaryOperator *E); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 173 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 174 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 175 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 176 | bool VisitCastExpr(const CastExpr* E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 177 | return HandleCast(E->getSubExpr(), E->getType()); |
| 178 | } |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 179 | bool VisitImplicitCastExpr(const ImplicitCastExpr* E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 180 | return HandleCast(E->getSubExpr(), E->getType()); |
| 181 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 182 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 183 | return EvaluateSizeAlignOf(E->isSizeOf(),E->getArgumentType(),E->getType()); |
| 184 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 185 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 186 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 187 | Result = E->getValue(); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 188 | return true; |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 190 | private: |
| 191 | bool HandleCast(const Expr* SubExpr, QualType DestType); |
| 192 | bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 193 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 194 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 195 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 196 | static bool EvaluateInteger(const Expr* E, APSInt &Result, ASTContext &Ctx) { |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 197 | return IntExprEvaluator(Ctx, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 198 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 199 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 200 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 201 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 202 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 203 | llvm::APSInt RHS(32); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 204 | if (!Visit(E->getLHS()) || !EvaluateInteger(E->getRHS(), RHS, Ctx)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 205 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 206 | |
| 207 | switch (E->getOpcode()) { |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 208 | default: return false; |
| 209 | case BinaryOperator::Mul: Result *= RHS; break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 210 | case BinaryOperator::Add: Result += RHS; break; |
| 211 | case BinaryOperator::Sub: Result -= RHS; break; |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 212 | case BinaryOperator::And: Result &= RHS; break; |
| 213 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 214 | case BinaryOperator::Or: Result |= RHS; break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 215 | case BinaryOperator::Div: |
| 216 | if (RHS == 0) return false; |
| 217 | Result /= RHS; |
| 218 | break; |
| 219 | case BinaryOperator::Rem: |
| 220 | if (RHS == 0) return false; |
| 221 | Result %= RHS; |
| 222 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 223 | case BinaryOperator::Shl: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 224 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 225 | break; |
| 226 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 227 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 228 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 230 | case BinaryOperator::LT: |
| 231 | Result = Result < RHS; |
| 232 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 233 | break; |
| 234 | case BinaryOperator::GT: |
| 235 | Result = Result > RHS; |
| 236 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 237 | break; |
| 238 | case BinaryOperator::LE: |
| 239 | Result = Result <= RHS; |
| 240 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 241 | break; |
| 242 | case BinaryOperator::GE: |
| 243 | Result = Result >= RHS; |
| 244 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 245 | break; |
| 246 | case BinaryOperator::EQ: |
| 247 | Result = Result == RHS; |
| 248 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 249 | break; |
| 250 | case BinaryOperator::NE: |
| 251 | Result = Result != RHS; |
| 252 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 253 | break; |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 254 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 255 | case BinaryOperator::Comma: |
| 256 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 257 | // *except* when they are contained within a subexpression that is not |
| 258 | // evaluated". Note that Assignment can never happen due to constraints |
| 259 | // on the LHS subexpr, so we don't need to check it here. |
| 260 | // FIXME: Need to come up with an efficient way to deal with the C99 |
| 261 | // rules on evaluation while still evaluating this. Maybe a |
| 262 | // "evaluated comma" out parameter? |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 263 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 267 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 270 | /// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result |
| 271 | /// as a DstTy type. |
| 272 | bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, |
| 273 | QualType DstTy) { |
| 274 | // Return the result in the right width. |
| 275 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 276 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 277 | |
| 278 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 279 | if (SrcTy->isVoidType()) |
| 280 | Result = 1; |
| 281 | |
| 282 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 283 | if (!SrcTy->isConstantSizeType()) { |
| 284 | // FIXME: Should we attempt to evaluate this? |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | // GCC extension: sizeof(function) = 1. |
| 289 | if (SrcTy->isFunctionType()) { |
| 290 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 291 | Result = isSizeOf ? 1 : 4; |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | // Get information about the size or align. |
| 296 | unsigned CharSize = Ctx.Target.getCharWidth(); |
| 297 | if (isSizeOf) |
| 298 | Result = getIntTypeSizeInBits(SrcTy) / CharSize; |
| 299 | else |
| 300 | Result = Ctx.getTypeAlign(SrcTy) / CharSize; |
| 301 | return true; |
| 302 | } |
| 303 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 304 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 305 | if (E->isOffsetOfOp()) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 306 | Result = E->evaluateOffsetOf(Ctx); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 307 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | if (E->isSizeOfAlignOfOp()) |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 312 | return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf, |
| 313 | E->getSubExpr()->getType(), E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 314 | |
| 315 | // Get the operand value. |
| 316 | if (!EvaluateInteger(E->getSubExpr(), Result, Ctx)) |
| 317 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 319 | switch (E->getOpcode()) { |
| 320 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 321 | // See C99 6.6p3. |
| 322 | default: |
| 323 | return false; |
| 324 | case UnaryOperator::LNot: { |
| 325 | bool Val = Result == 0; |
| 326 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 327 | Result = Val; |
| 328 | break; |
| 329 | } |
| 330 | case UnaryOperator::Extension: |
| 331 | case UnaryOperator::Plus: |
| 332 | // The result is always just the subexpr |
| 333 | break; |
| 334 | case UnaryOperator::Minus: |
| 335 | Result = -Result; |
| 336 | break; |
| 337 | case UnaryOperator::Not: |
| 338 | Result = ~Result; |
| 339 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 343 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 346 | bool IntExprEvaluator::HandleCast(const Expr* SubExpr, QualType DestType) { |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 347 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 348 | |
| 349 | // Handle simple integer->integer casts. |
| 350 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 351 | if (!EvaluateInteger(SubExpr, Result, Ctx)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 352 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 353 | |
| 354 | // Figure out if this is a truncate, extend or noop cast. |
| 355 | // If the input is signed, do a sign extend, noop, or truncate. |
| 356 | if (DestType->isBooleanType()) { |
| 357 | // Conversion to bool compares against zero. |
| 358 | Result = Result != 0; |
| 359 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 360 | } else |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 361 | Result.extOrTrunc(DestWidth); |
| 362 | } else if (SubExpr->getType()->isPointerType()) { |
| 363 | APValue LV; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 364 | if (!EvaluatePointer(SubExpr, LV, Ctx)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 365 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 366 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 367 | return false; |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | 8ab15c8 | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 369 | Result.extOrTrunc(DestWidth); |
| 370 | Result = LV.getLValueOffset(); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 371 | } else { |
| 372 | assert(0 && "Unhandled cast!"); |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 375 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 376 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 377 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 378 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 379 | //===----------------------------------------------------------------------===// |
| 380 | // Top level TryEvaluate. |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 383 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 334b194 | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 384 | llvm::APSInt sInt(32); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 385 | #if USE_NEW_EVALUATOR |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 386 | if (getType()->isIntegerType()) { |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 387 | if (EvaluateInteger(this, sInt, Ctx)) { |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 388 | Result = APValue(sInt); |
| 389 | return true; |
| 390 | } |
| 391 | } else |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 392 | return false; |
| 393 | |
| 394 | #else |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 395 | if (CalcFakeICEVal(this, sInt, Ctx)) { |
| 396 | Result = APValue(sInt); |
| 397 | return true; |
| 398 | } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 399 | #endif |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 400 | |
| 401 | return false; |
| 402 | } |