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