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 | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 54 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 55 | /// information about a subexpression as it is folded. It retains information |
| 56 | /// about the AST context, but also maintains information about the folded |
| 57 | /// expression. |
| 58 | /// |
| 59 | /// If an expression could be evaluated, it is still possible it is not a C |
| 60 | /// "integer constant expression" or constant expression. If not, this struct |
| 61 | /// captures information about how and why not. |
| 62 | /// |
| 63 | /// One bit of information passed *into* the request for constant folding |
| 64 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 65 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 66 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 67 | /// certain things in certain situations. |
| 68 | struct EvalInfo { |
| 69 | ASTContext &Ctx; |
| 70 | |
| 71 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 72 | /// if it is short-circuited (according to C rules). |
| 73 | bool isEvaluated; |
| 74 | |
| 75 | /// ICEDiag - If the expression is foldable, but the expression is not an |
| 76 | /// integer constant expression, this contains the extension diagnostic to |
| 77 | /// emit which describes why it isn't an integer constant expression. The |
| 78 | /// caller can choose to emit this or not, depending on whether they require |
| 79 | /// an i-c-e or not. DiagLoc indicates the caret position for the report. |
| 80 | /// |
| 81 | /// If ICEDiag is zero, then this expression is an i-c-e. |
| 82 | unsigned ICEDiag; |
| 83 | SourceLocation DiagLoc; |
| 84 | |
| 85 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 90 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // Pointer Evaluation |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 97 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 98 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 99 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 100 | EvalInfo &Info; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 101 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 103 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 104 | |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 105 | APValue VisitStmt(Stmt *S) { |
| 106 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 107 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 108 | S->dump(); |
| 109 | return APValue(); |
| 110 | } |
| 111 | |
| 112 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 113 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 114 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 115 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 116 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 117 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 119 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 120 | if (!E->getType()->isPointerType()) |
| 121 | return false; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 122 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 123 | return Result.isLValue(); |
| 124 | } |
| 125 | |
| 126 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 127 | if (E->getOpcode() != BinaryOperator::Add && |
| 128 | E->getOpcode() != BinaryOperator::Sub) |
| 129 | return APValue(); |
| 130 | |
| 131 | const Expr *PExp = E->getLHS(); |
| 132 | const Expr *IExp = E->getRHS(); |
| 133 | if (IExp->getType()->isPointerType()) |
| 134 | std::swap(PExp, IExp); |
| 135 | |
| 136 | APValue ResultLValue; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 137 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 138 | return APValue(); |
| 139 | |
| 140 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 141 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 142 | return APValue(); |
| 143 | |
| 144 | uint64_t Offset = ResultLValue.getLValueOffset(); |
| 145 | if (E->getOpcode() == BinaryOperator::Add) |
| 146 | Offset += AdditionalOffset.getZExtValue(); |
| 147 | else |
| 148 | Offset -= AdditionalOffset.getZExtValue(); |
| 149 | |
| 150 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 151 | } |
| 152 | |
| 153 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 154 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 155 | const Expr* SubExpr = E->getSubExpr(); |
| 156 | |
| 157 | // Check for pointer->pointer cast |
| 158 | if (SubExpr->getType()->isPointerType()) { |
| 159 | APValue Result; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 160 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 161 | return Result; |
| 162 | return APValue(); |
| 163 | } |
| 164 | |
| 165 | if (SubExpr->getType()->isArithmeticType()) { |
| 166 | llvm::APSInt Result(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 167 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 168 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 169 | return APValue(0, Result.getZExtValue()); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | assert(0 && "Unhandled cast"); |
| 174 | return APValue(); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | // Integer Evaluation |
| 180 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 181 | |
| 182 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 183 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 184 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 185 | EvalInfo &Info; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 186 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 187 | public: |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 188 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 189 | : Info(info), Result(result) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 191 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 192 | return (unsigned)Info.Ctx.getTypeSize(T); |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 195 | //===--------------------------------------------------------------------===// |
| 196 | // Visitor Methods |
| 197 | //===--------------------------------------------------------------------===// |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 198 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 199 | bool VisitStmt(Stmt *S) { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 200 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 201 | printf("unhandled int expression"); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 202 | S->dump(); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 203 | return false; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 206 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 207 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 208 | bool VisitBinaryOperator(const BinaryOperator *E); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 209 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 210 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 211 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 212 | bool VisitCastExpr(const CastExpr* E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 213 | return HandleCast(E->getSubExpr(), E->getType()); |
| 214 | } |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 215 | bool VisitImplicitCastExpr(const ImplicitCastExpr* E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 216 | return HandleCast(E->getSubExpr(), E->getType()); |
| 217 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 218 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 219 | return EvaluateSizeAlignOf(E->isSizeOf(),E->getArgumentType(),E->getType()); |
| 220 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 221 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 222 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 223 | Result = E->getValue(); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 224 | return true; |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 226 | private: |
| 227 | bool HandleCast(const Expr* SubExpr, QualType DestType); |
| 228 | bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 229 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 230 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 232 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 233 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 234 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 235 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 236 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 237 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 238 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 239 | llvm::APSInt RHS(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 240 | if (!Visit(E->getLHS()) || !EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 241 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 242 | |
| 243 | switch (E->getOpcode()) { |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 244 | default: return false; |
| 245 | case BinaryOperator::Mul: Result *= RHS; break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 246 | case BinaryOperator::Add: Result += RHS; break; |
| 247 | case BinaryOperator::Sub: Result -= RHS; break; |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 248 | case BinaryOperator::And: Result &= RHS; break; |
| 249 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 250 | case BinaryOperator::Or: Result |= RHS; break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 251 | case BinaryOperator::Div: |
| 252 | if (RHS == 0) return false; |
| 253 | Result /= RHS; |
| 254 | break; |
| 255 | case BinaryOperator::Rem: |
| 256 | if (RHS == 0) return false; |
| 257 | Result %= RHS; |
| 258 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 259 | case BinaryOperator::Shl: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 260 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 261 | break; |
| 262 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 263 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 264 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 266 | case BinaryOperator::LT: |
| 267 | Result = Result < RHS; |
| 268 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 269 | break; |
| 270 | case BinaryOperator::GT: |
| 271 | Result = Result > RHS; |
| 272 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 273 | break; |
| 274 | case BinaryOperator::LE: |
| 275 | Result = Result <= RHS; |
| 276 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 277 | break; |
| 278 | case BinaryOperator::GE: |
| 279 | Result = Result >= RHS; |
| 280 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 281 | break; |
| 282 | case BinaryOperator::EQ: |
| 283 | Result = Result == RHS; |
| 284 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 285 | break; |
| 286 | case BinaryOperator::NE: |
| 287 | Result = Result != RHS; |
| 288 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 289 | break; |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 290 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 291 | case BinaryOperator::Comma: |
| 292 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 293 | // *except* when they are contained within a subexpression that is not |
| 294 | // evaluated". Note that Assignment can never happen due to constraints |
| 295 | // on the LHS subexpr, so we don't need to check it here. |
| 296 | // FIXME: Need to come up with an efficient way to deal with the C99 |
| 297 | // rules on evaluation while still evaluating this. Maybe a |
| 298 | // "evaluated comma" out parameter? |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 299 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 303 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 306 | /// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result |
| 307 | /// as a DstTy type. |
| 308 | bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, |
| 309 | QualType DstTy) { |
| 310 | // Return the result in the right width. |
| 311 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 312 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 313 | |
| 314 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 315 | if (SrcTy->isVoidType()) |
| 316 | Result = 1; |
| 317 | |
| 318 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 319 | if (!SrcTy->isConstantSizeType()) { |
| 320 | // FIXME: Should we attempt to evaluate this? |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // GCC extension: sizeof(function) = 1. |
| 325 | if (SrcTy->isFunctionType()) { |
| 326 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 327 | Result = isSizeOf ? 1 : 4; |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | // Get information about the size or align. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 332 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 333 | if (isSizeOf) |
| 334 | Result = getIntTypeSizeInBits(SrcTy) / CharSize; |
| 335 | else |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 336 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 340 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 341 | if (E->isOffsetOfOp()) { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 342 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 343 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | if (E->isSizeOfAlignOfOp()) |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 348 | return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf, |
| 349 | E->getSubExpr()->getType(), E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 351 | // Get the operand value into 'Result'. |
| 352 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 353 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 355 | switch (E->getOpcode()) { |
| 356 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 357 | // See C99 6.6p3. |
| 358 | default: |
| 359 | return false; |
| 360 | case UnaryOperator::LNot: { |
| 361 | bool Val = Result == 0; |
| 362 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 363 | Result = Val; |
| 364 | break; |
| 365 | } |
| 366 | case UnaryOperator::Extension: |
| 367 | case UnaryOperator::Plus: |
| 368 | // The result is always just the subexpr |
| 369 | break; |
| 370 | case UnaryOperator::Minus: |
| 371 | Result = -Result; |
| 372 | break; |
| 373 | case UnaryOperator::Not: |
| 374 | Result = ~Result; |
| 375 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 379 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 382 | bool IntExprEvaluator::HandleCast(const Expr* SubExpr, QualType DestType) { |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 383 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 384 | |
| 385 | // Handle simple integer->integer casts. |
| 386 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 387 | if (!EvaluateInteger(SubExpr, Result, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 388 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 389 | |
| 390 | // Figure out if this is a truncate, extend or noop cast. |
| 391 | // If the input is signed, do a sign extend, noop, or truncate. |
| 392 | if (DestType->isBooleanType()) { |
| 393 | // Conversion to bool compares against zero. |
| 394 | Result = Result != 0; |
| 395 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 396 | } else |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 397 | Result.extOrTrunc(DestWidth); |
| 398 | } else if (SubExpr->getType()->isPointerType()) { |
| 399 | APValue LV; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 400 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 401 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 402 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 403 | return false; |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 404 | |
Anders Carlsson | 8ab15c8 | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 405 | Result.extOrTrunc(DestWidth); |
| 406 | Result = LV.getLValueOffset(); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 407 | } else { |
| 408 | assert(0 && "Unhandled cast!"); |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 411 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 412 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 413 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 414 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
| 416 | // Top level TryEvaluate. |
| 417 | //===----------------------------------------------------------------------===// |
| 418 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 419 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 334b194 | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 420 | llvm::APSInt sInt(32); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 421 | #if USE_NEW_EVALUATOR |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 422 | EvalInfo Info(Ctx); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 423 | if (getType()->isIntegerType()) { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame^] | 424 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 425 | Result = APValue(sInt); |
| 426 | return true; |
| 427 | } |
| 428 | } else |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 429 | return false; |
| 430 | |
| 431 | #else |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 432 | if (CalcFakeICEVal(this, sInt, Ctx)) { |
| 433 | Result = APValue(sInt); |
| 434 | return true; |
| 435 | } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 436 | #endif |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 437 | |
| 438 | return false; |
| 439 | } |