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