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