Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Expr.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 18 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 22 | using llvm::APSInt; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 24 | #define USE_NEW_EVALUATOR 0 |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | static bool CalcFakeICEVal(const Expr *Expr, |
| 27 | llvm::APSInt &Result, |
| 28 | ASTContext &Context) { |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 29 | // Calculate the value of an expression that has a calculatable |
| 30 | // value, but isn't an ICE. Currently, this only supports |
| 31 | // a very narrow set of extensions, but it can be expanded if needed. |
| 32 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr)) |
| 33 | return CalcFakeICEVal(PE->getSubExpr(), Result, Context); |
| 34 | |
| 35 | if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) { |
| 36 | QualType CETy = CE->getType(); |
| 37 | if ((CETy->isIntegralType() && !CETy->isBooleanType()) || |
| 38 | CETy->isPointerType()) { |
| 39 | if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) { |
| 40 | Result.extOrTrunc(Context.getTypeSize(CETy)); |
| 41 | // FIXME: This assumes pointers are signed. |
| 42 | Result.setIsSigned(CETy->isSignedIntegerType() || |
| 43 | CETy->isPointerType()); |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (Expr->getType()->isIntegralType()) |
| 50 | return Expr->isIntegerConstantExpr(Result, Context); |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 56 | /// information about a subexpression as it is folded. It retains information |
| 57 | /// about the AST context, but also maintains information about the folded |
| 58 | /// expression. |
| 59 | /// |
| 60 | /// If an expression could be evaluated, it is still possible it is not a C |
| 61 | /// "integer constant expression" or constant expression. If not, this struct |
| 62 | /// captures information about how and why not. |
| 63 | /// |
| 64 | /// One bit of information passed *into* the request for constant folding |
| 65 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 66 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 67 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 68 | /// certain things in certain situations. |
| 69 | struct EvalInfo { |
| 70 | ASTContext &Ctx; |
| 71 | |
| 72 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 73 | /// if it is short-circuited (according to C rules). |
| 74 | bool isEvaluated; |
| 75 | |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 76 | /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the |
| 77 | /// error diagnostic indicating why it is not foldable and DiagLoc indicates a |
| 78 | /// caret position for the error. If it is foldable, but the expression is |
| 79 | /// not an integer constant expression, ICEDiag contains the extension |
| 80 | /// diagnostic to emit which describes why it isn't an integer constant |
| 81 | /// expression. If this expression *is* an integer-constant-expr, then |
| 82 | /// ICEDiag is zero. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 83 | /// |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 84 | /// The caller can choose to emit this diagnostic or not, depending on whether |
| 85 | /// they require an i-c-e or a constant or not. DiagLoc indicates the caret |
| 86 | /// position for the report. |
| 87 | /// |
| 88 | /// If ICEDiag is zero, then this expression is an i-c-e. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 89 | unsigned ICEDiag; |
| 90 | SourceLocation DiagLoc; |
| 91 | |
| 92 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 97 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // Pointer Evaluation |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 104 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 105 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 106 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 107 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 108 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 110 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 111 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 112 | APValue VisitStmt(Stmt *S) { |
| 113 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 114 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 115 | S->dump(); |
| 116 | return APValue(); |
| 117 | } |
| 118 | |
| 119 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 120 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 121 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 122 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 123 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 124 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 126 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 127 | if (!E->getType()->isPointerType()) |
| 128 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 129 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 130 | return Result.isLValue(); |
| 131 | } |
| 132 | |
| 133 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 134 | if (E->getOpcode() != BinaryOperator::Add && |
| 135 | E->getOpcode() != BinaryOperator::Sub) |
| 136 | return APValue(); |
| 137 | |
| 138 | const Expr *PExp = E->getLHS(); |
| 139 | const Expr *IExp = E->getRHS(); |
| 140 | if (IExp->getType()->isPointerType()) |
| 141 | std::swap(PExp, IExp); |
| 142 | |
| 143 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 144 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 145 | return APValue(); |
| 146 | |
| 147 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 148 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 149 | return APValue(); |
| 150 | |
| 151 | uint64_t Offset = ResultLValue.getLValueOffset(); |
| 152 | if (E->getOpcode() == BinaryOperator::Add) |
| 153 | Offset += AdditionalOffset.getZExtValue(); |
| 154 | else |
| 155 | Offset -= AdditionalOffset.getZExtValue(); |
| 156 | |
| 157 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 158 | } |
| 159 | |
| 160 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 161 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 162 | const Expr* SubExpr = E->getSubExpr(); |
| 163 | |
| 164 | // Check for pointer->pointer cast |
| 165 | if (SubExpr->getType()->isPointerType()) { |
| 166 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 167 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 168 | return Result; |
| 169 | return APValue(); |
| 170 | } |
| 171 | |
| 172 | if (SubExpr->getType()->isArithmeticType()) { |
| 173 | llvm::APSInt Result(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 174 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 175 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 176 | return APValue(0, Result.getZExtValue()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | assert(0 && "Unhandled cast"); |
| 181 | return APValue(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // Integer Evaluation |
| 187 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 188 | |
| 189 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 190 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 191 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 192 | EvalInfo &Info; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 193 | APSInt &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 194 | public: |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 195 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 196 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 198 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 199 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 200 | } |
| 201 | |
| 202 | bool Extension(SourceLocation L, diag::kind D) { |
| 203 | Info.DiagLoc = L; |
| 204 | Info.ICEDiag = D; |
| 205 | return true; // still a constant. |
| 206 | } |
| 207 | |
| 208 | bool Error(SourceLocation L, diag::kind D) { |
| 209 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 210 | // error. |
| 211 | if (!Info.isEvaluated) |
| 212 | return true; |
| 213 | |
| 214 | Info.DiagLoc = L; |
| 215 | Info.ICEDiag = D; |
| 216 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 219 | //===--------------------------------------------------------------------===// |
| 220 | // Visitor Methods |
| 221 | //===--------------------------------------------------------------------===// |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 222 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 223 | bool VisitStmt(Stmt *S) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 224 | return Error(S->getLocStart(), diag::err_expr_not_constant); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 227 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 228 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 229 | bool VisitBinaryOperator(const BinaryOperator *E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 230 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 231 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 232 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 233 | bool VisitCastExpr(const CastExpr* E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 234 | return HandleCast(E->getSubExpr(), E->getType()); |
| 235 | } |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 236 | bool VisitImplicitCastExpr(const ImplicitCastExpr* E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 237 | return HandleCast(E->getSubExpr(), E->getType()); |
| 238 | } |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 239 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 240 | return EvaluateSizeAlignOf(E->isSizeOf(), E->getArgumentType(), |
| 241 | E->getType()); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 242 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 243 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 244 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 245 | Result = E->getValue(); |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 246 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 247 | return true; |
| 248 | } |
| 249 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 250 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 251 | Result = E->getValue(); |
| 252 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 253 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 255 | private: |
| 256 | bool HandleCast(const Expr* SubExpr, QualType DestType); |
| 257 | bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 258 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 259 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 261 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 262 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 263 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 264 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 265 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 266 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 267 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 268 | llvm::APSInt RHS(32); |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 269 | if (!Visit(E->getLHS())) |
| 270 | return false; // error in subexpression. |
| 271 | |
| 272 | bool OldEval = Info.isEvaluated; |
| 273 | |
| 274 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 275 | // RHS. Make sure to pass isEvaluated down correctly. |
| 276 | if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) || |
| 277 | (E->getOpcode() == BinaryOperator::LOr && Result != 0)) |
| 278 | Info.isEvaluated = false; |
| 279 | |
| 280 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 281 | return false; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 282 | Info.isEvaluated = OldEval; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 283 | |
| 284 | switch (E->getOpcode()) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 285 | default: return Error(E->getOperatorLoc(), diag::err_expr_not_constant); |
| 286 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 287 | case BinaryOperator::Add: Result += RHS; return true; |
| 288 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 289 | case BinaryOperator::And: Result &= RHS; return true; |
| 290 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 291 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 292 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 293 | if (RHS == 0) |
| 294 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 295 | Result /= RHS; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 296 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 297 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 298 | if (RHS == 0) |
| 299 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 300 | Result %= RHS; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 301 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 302 | case BinaryOperator::Shl: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 303 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 304 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 305 | break; |
| 306 | case BinaryOperator::Shr: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 307 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 308 | break; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 309 | |
Chris Lattner | ac7cb60 | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 310 | case BinaryOperator::LT: |
| 311 | Result = Result < RHS; |
| 312 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 313 | break; |
| 314 | case BinaryOperator::GT: |
| 315 | Result = Result > RHS; |
| 316 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 317 | break; |
| 318 | case BinaryOperator::LE: |
| 319 | Result = Result <= RHS; |
| 320 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 321 | break; |
| 322 | case BinaryOperator::GE: |
| 323 | Result = Result >= RHS; |
| 324 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 325 | break; |
| 326 | case BinaryOperator::EQ: |
| 327 | Result = Result == RHS; |
| 328 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 329 | break; |
| 330 | case BinaryOperator::NE: |
| 331 | Result = Result != RHS; |
| 332 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 333 | break; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 334 | case BinaryOperator::LAnd: |
| 335 | Result = Result != 0 && RHS != 0; |
| 336 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 337 | break; |
| 338 | case BinaryOperator::LOr: |
| 339 | Result = Result != 0 || RHS != 0; |
| 340 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 341 | break; |
| 342 | |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 344 | case BinaryOperator::Comma: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 345 | // Result of the comma is just the result of the RHS. |
| 346 | Result = RHS; |
| 347 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 348 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 349 | // *except* when they are contained within a subexpression that is not |
| 350 | // evaluated". Note that Assignment can never happen due to constraints |
| 351 | // on the LHS subexpr, so we don't need to check it here. |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame^] | 352 | if (!Info.isEvaluated) |
| 353 | return true; |
| 354 | |
| 355 | // If the value is evaluated, we can accept it as an extension. |
| 356 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 360 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 363 | /// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result |
| 364 | /// as a DstTy type. |
| 365 | bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, |
| 366 | QualType DstTy) { |
| 367 | // Return the result in the right width. |
| 368 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 369 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 370 | |
| 371 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 372 | if (SrcTy->isVoidType()) |
| 373 | Result = 1; |
| 374 | |
| 375 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 376 | if (!SrcTy->isConstantSizeType()) { |
| 377 | // FIXME: Should we attempt to evaluate this? |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | // GCC extension: sizeof(function) = 1. |
| 382 | if (SrcTy->isFunctionType()) { |
| 383 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 384 | Result = isSizeOf ? 1 : 4; |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | // Get information about the size or align. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 389 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 390 | if (isSizeOf) |
| 391 | Result = getIntTypeSizeInBits(SrcTy) / CharSize; |
| 392 | else |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 393 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 394 | return true; |
| 395 | } |
| 396 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 397 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 398 | if (E->isOffsetOfOp()) { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 399 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 400 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | if (E->isSizeOfAlignOfOp()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 405 | return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf, |
| 406 | E->getSubExpr()->getType(), E->getType()); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 408 | // Get the operand value into 'Result'. |
| 409 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 410 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 412 | switch (E->getOpcode()) { |
| 413 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 414 | // See C99 6.6p3. |
| 415 | default: |
| 416 | return false; |
| 417 | case UnaryOperator::LNot: { |
| 418 | bool Val = Result == 0; |
| 419 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 420 | Result = Val; |
| 421 | break; |
| 422 | } |
| 423 | case UnaryOperator::Extension: |
| 424 | case UnaryOperator::Plus: |
| 425 | // The result is always just the subexpr |
| 426 | break; |
| 427 | case UnaryOperator::Minus: |
| 428 | Result = -Result; |
| 429 | break; |
| 430 | case UnaryOperator::Not: |
| 431 | Result = ~Result; |
| 432 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 436 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 439 | bool IntExprEvaluator::HandleCast(const Expr* SubExpr, QualType DestType) { |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 440 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 441 | |
| 442 | // Handle simple integer->integer casts. |
| 443 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 444 | if (!EvaluateInteger(SubExpr, Result, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 445 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 446 | |
| 447 | // Figure out if this is a truncate, extend or noop cast. |
| 448 | // If the input is signed, do a sign extend, noop, or truncate. |
| 449 | if (DestType->isBooleanType()) { |
| 450 | // Conversion to bool compares against zero. |
| 451 | Result = Result != 0; |
| 452 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 453 | } else |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 454 | Result.extOrTrunc(DestWidth); |
| 455 | } else if (SubExpr->getType()->isPointerType()) { |
| 456 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 457 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 458 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 459 | if (LV.getLValueBase()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 460 | return false; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 461 | |
Anders Carlsson | 559e56b | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 462 | Result.extOrTrunc(DestWidth); |
| 463 | Result = LV.getLValueOffset(); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 464 | } else { |
| 465 | assert(0 && "Unhandled cast!"); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 468 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 469 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 470 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 471 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 472 | //===----------------------------------------------------------------------===// |
| 473 | // Top level TryEvaluate. |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 476 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | cf0f51d | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 477 | llvm::APSInt sInt(32); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 478 | #if USE_NEW_EVALUATOR |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 479 | EvalInfo Info(Ctx); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 480 | if (getType()->isIntegerType()) { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 481 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 482 | Result = APValue(sInt); |
| 483 | return true; |
| 484 | } |
| 485 | } else |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 486 | return false; |
| 487 | |
| 488 | #else |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 489 | if (CalcFakeICEVal(this, sInt, Ctx)) { |
| 490 | Result = APValue(sInt); |
| 491 | return true; |
| 492 | } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 493 | #endif |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 494 | |
| 495 | return false; |
| 496 | } |