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