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" |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.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; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 23 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 25 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 26 | /// information about a subexpression as it is folded. It retains information |
| 27 | /// about the AST context, but also maintains information about the folded |
| 28 | /// expression. |
| 29 | /// |
| 30 | /// If an expression could be evaluated, it is still possible it is not a C |
| 31 | /// "integer constant expression" or constant expression. If not, this struct |
| 32 | /// captures information about how and why not. |
| 33 | /// |
| 34 | /// One bit of information passed *into* the request for constant folding |
| 35 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 36 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 37 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 38 | /// certain things in certain situations. |
| 39 | struct EvalInfo { |
| 40 | ASTContext &Ctx; |
| 41 | |
| 42 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 43 | /// if it is short-circuited (according to C rules). |
| 44 | bool isEvaluated; |
| 45 | |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 46 | /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the |
| 47 | /// error diagnostic indicating why it is not foldable and DiagLoc indicates a |
| 48 | /// caret position for the error. If it is foldable, but the expression is |
| 49 | /// not an integer constant expression, ICEDiag contains the extension |
| 50 | /// diagnostic to emit which describes why it isn't an integer constant |
| 51 | /// expression. If this expression *is* an integer-constant-expr, then |
| 52 | /// ICEDiag is zero. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 53 | /// |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 54 | /// The caller can choose to emit this diagnostic or not, depending on whether |
| 55 | /// they require an i-c-e or a constant or not. DiagLoc indicates the caret |
| 56 | /// position for the report. |
| 57 | /// |
| 58 | /// If ICEDiag is zero, then this expression is an i-c-e. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 59 | unsigned ICEDiag; |
| 60 | SourceLocation DiagLoc; |
| 61 | |
| 62 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 63 | }; |
| 64 | |
| 65 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 66 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 67 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 68 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 69 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 70 | static bool EvaluateComplexFloat(const Expr *E, APValue &Result, |
| 71 | EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 72 | |
| 73 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 74 | // Misc utilities |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
| 77 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 78 | if (E->getType()->isIntegralType()) { |
| 79 | APSInt IntResult; |
| 80 | if (!EvaluateInteger(E, IntResult, Info)) |
| 81 | return false; |
| 82 | Result = IntResult != 0; |
| 83 | return true; |
| 84 | } else if (E->getType()->isRealFloatingType()) { |
| 85 | APFloat FloatResult(0.0); |
| 86 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 87 | return false; |
| 88 | Result = !FloatResult.isZero(); |
| 89 | return true; |
| 90 | } else if (E->getType()->isPointerType()) { |
| 91 | APValue PointerResult; |
| 92 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 93 | return false; |
| 94 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 95 | // the check look like? |
| 96 | Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset(); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | // LValue Evaluation |
| 105 | //===----------------------------------------------------------------------===// |
| 106 | namespace { |
| 107 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 108 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 109 | EvalInfo &Info; |
| 110 | public: |
| 111 | |
| 112 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 113 | |
| 114 | APValue VisitStmt(Stmt *S) { |
Daniel Dunbar | 8a7b7c6 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 115 | #if 0 |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 116 | // FIXME: Remove this when we support more expressions. |
| 117 | printf("Unhandled pointer statement\n"); |
| 118 | S->dump(); |
Daniel Dunbar | 8a7b7c6 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 119 | #endif |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 120 | return APValue(); |
| 121 | } |
| 122 | |
| 123 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 124 | APValue VisitDeclRefExpr(DeclRefExpr *E) { return APValue(E, 0); } |
| 125 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 126 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 127 | APValue VisitMemberExpr(MemberExpr *E); |
| 128 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 129 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 130 | }; |
| 131 | } // end anonymous namespace |
| 132 | |
| 133 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 134 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 135 | return Result.isLValue(); |
| 136 | } |
| 137 | |
| 138 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 139 | if (E->isFileScope()) |
| 140 | return APValue(E, 0); |
| 141 | return APValue(); |
| 142 | } |
| 143 | |
| 144 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 145 | APValue result; |
| 146 | QualType Ty; |
| 147 | if (E->isArrow()) { |
| 148 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 149 | return APValue(); |
| 150 | Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType(); |
| 151 | } else { |
| 152 | result = Visit(E->getBase()); |
| 153 | if (result.isUninit()) |
| 154 | return APValue(); |
| 155 | Ty = E->getBase()->getType(); |
| 156 | } |
| 157 | |
| 158 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
| 159 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 160 | FieldDecl *FD = E->getMemberDecl(); |
| 161 | |
| 162 | // FIXME: This is linear time. |
| 163 | unsigned i = 0, e = 0; |
| 164 | for (i = 0, e = RD->getNumMembers(); i != e; i++) { |
| 165 | if (RD->getMember(i) == FD) |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | result.setLValue(result.getLValueBase(), |
| 170 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 175 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
| 176 | { |
| 177 | APValue Result; |
| 178 | |
| 179 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 180 | return APValue(); |
| 181 | |
| 182 | APSInt Index; |
| 183 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 184 | return APValue(); |
| 185 | |
| 186 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 187 | |
| 188 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
| 189 | Result.setLValue(Result.getLValueBase(), |
| 190 | Result.getLValueOffset() + Offset); |
| 191 | return Result; |
| 192 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 193 | |
| 194 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 195 | // Pointer Evaluation |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 198 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 199 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 200 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 201 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 202 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 204 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 205 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 206 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 207 | return APValue(); |
| 208 | } |
| 209 | |
| 210 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 211 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 212 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 213 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 214 | APValue VisitUnaryOperator(const UnaryOperator *E); |
| 215 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 216 | { return APValue(E, 0); } |
| 217 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 218 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 219 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 221 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 222 | if (!E->getType()->isPointerType()) |
| 223 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 224 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 225 | return Result.isLValue(); |
| 226 | } |
| 227 | |
| 228 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 229 | if (E->getOpcode() != BinaryOperator::Add && |
| 230 | E->getOpcode() != BinaryOperator::Sub) |
| 231 | return APValue(); |
| 232 | |
| 233 | const Expr *PExp = E->getLHS(); |
| 234 | const Expr *IExp = E->getRHS(); |
| 235 | if (IExp->getType()->isPointerType()) |
| 236 | std::swap(PExp, IExp); |
| 237 | |
| 238 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 239 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 240 | return APValue(); |
| 241 | |
| 242 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 243 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 244 | return APValue(); |
| 245 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 246 | QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType(); |
| 247 | uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
| 248 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 249 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 250 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 251 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 252 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 253 | else |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 254 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 255 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 256 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 257 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 258 | |
| 259 | APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 260 | if (E->getOpcode() == UnaryOperator::Extension) { |
| 261 | // FIXME: Deal with warnings? |
| 262 | return Visit(E->getSubExpr()); |
| 263 | } |
| 264 | |
| 265 | if (E->getOpcode() == UnaryOperator::AddrOf) { |
| 266 | APValue result; |
| 267 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 268 | return result; |
| 269 | } |
| 270 | |
| 271 | return APValue(); |
| 272 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 273 | |
| 274 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 275 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 276 | const Expr* SubExpr = E->getSubExpr(); |
| 277 | |
| 278 | // Check for pointer->pointer cast |
| 279 | if (SubExpr->getType()->isPointerType()) { |
| 280 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 281 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 282 | return Result; |
| 283 | return APValue(); |
| 284 | } |
| 285 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 286 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 287 | llvm::APSInt Result(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 288 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 289 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 290 | return APValue(0, Result.getZExtValue()); |
| 291 | } |
| 292 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 293 | |
| 294 | if (SubExpr->getType()->isFunctionType() || |
| 295 | SubExpr->getType()->isArrayType()) { |
| 296 | APValue Result; |
| 297 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 298 | return Result; |
| 299 | return APValue(); |
| 300 | } |
| 301 | |
| 302 | //assert(0 && "Unhandled cast"); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 303 | return APValue(); |
| 304 | } |
| 305 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 306 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 307 | bool BoolResult; |
| 308 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 309 | return APValue(); |
| 310 | |
| 311 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 312 | |
| 313 | APValue Result; |
| 314 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 315 | return Result; |
| 316 | return APValue(); |
| 317 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 318 | |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | // Integer Evaluation |
| 321 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 322 | |
| 323 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 324 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 325 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 326 | EvalInfo &Info; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 327 | APSInt &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 328 | public: |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 329 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 330 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 332 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 333 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 334 | } |
| 335 | |
| 336 | bool Extension(SourceLocation L, diag::kind D) { |
| 337 | Info.DiagLoc = L; |
| 338 | Info.ICEDiag = D; |
| 339 | return true; // still a constant. |
| 340 | } |
| 341 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 342 | bool Error(SourceLocation L, diag::kind D, QualType ExprTy) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 343 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 344 | // error. |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 345 | if (!Info.isEvaluated) { |
| 346 | // If error is ignored because the value isn't evaluated, get the real |
| 347 | // type at least to prevent errors downstream. |
| 348 | Result.zextOrTrunc(getIntTypeSizeInBits(ExprTy)); |
| 349 | Result.setIsUnsigned(ExprTy->isUnsignedIntegerType()); |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 350 | return true; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 353 | // Take the first error. |
| 354 | if (Info.ICEDiag == 0) { |
| 355 | Info.DiagLoc = L; |
| 356 | Info.ICEDiag = D; |
| 357 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 358 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 361 | //===--------------------------------------------------------------------===// |
| 362 | // Visitor Methods |
| 363 | //===--------------------------------------------------------------------===// |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 364 | |
| 365 | bool VisitStmt(Stmt *) { |
| 366 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 367 | return false; |
| 368 | } |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 370 | bool VisitExpr(Expr *E) { |
| 371 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 374 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 376 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| 377 | Result = E->getValue(); |
| 378 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 379 | return true; |
| 380 | } |
| 381 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 382 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 383 | Result = E->getValue(); |
| 384 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 385 | return true; |
| 386 | } |
| 387 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 388 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 389 | // Per gcc docs "this built-in function ignores top level |
| 390 | // qualifiers". We need to use the canonical version to properly |
| 391 | // be able to strip CRV qualifiers from the type. |
| 392 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 393 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
| 394 | Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 395 | T1.getUnqualifiedType()); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 396 | return true; |
| 397 | } |
| 398 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 399 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 400 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 401 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 402 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 404 | bool VisitCastExpr(CastExpr* E) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 405 | return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType()); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 406 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 407 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 408 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 409 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame^] | 410 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 411 | Result = E->getValue(); |
| 412 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
| 417 | Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType())); |
| 418 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 419 | return true; |
| 420 | } |
| 421 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 422 | private: |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 423 | bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 424 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 425 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 427 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 428 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 429 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 431 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 432 | // Enums are integer constant exprs. |
| 433 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 434 | Result = D->getInitVal(); |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | // Otherwise, random variable references are not constants. |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 439 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 442 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 443 | /// as GCC. |
| 444 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 445 | // The following enum mimics the values returned by GCC. |
| 446 | enum gcc_type_class { |
| 447 | no_type_class = -1, |
| 448 | void_type_class, integer_type_class, char_type_class, |
| 449 | enumeral_type_class, boolean_type_class, |
| 450 | pointer_type_class, reference_type_class, offset_type_class, |
| 451 | real_type_class, complex_type_class, |
| 452 | function_type_class, method_type_class, |
| 453 | record_type_class, union_type_class, |
| 454 | array_type_class, string_type_class, |
| 455 | lang_type_class |
| 456 | }; |
| 457 | |
| 458 | // If no argument was supplied, default to "no_type_class". This isn't |
| 459 | // ideal, however it is what gcc does. |
| 460 | if (E->getNumArgs() == 0) |
| 461 | return no_type_class; |
| 462 | |
| 463 | QualType ArgTy = E->getArg(0)->getType(); |
| 464 | if (ArgTy->isVoidType()) |
| 465 | return void_type_class; |
| 466 | else if (ArgTy->isEnumeralType()) |
| 467 | return enumeral_type_class; |
| 468 | else if (ArgTy->isBooleanType()) |
| 469 | return boolean_type_class; |
| 470 | else if (ArgTy->isCharType()) |
| 471 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 472 | else if (ArgTy->isIntegerType()) |
| 473 | return integer_type_class; |
| 474 | else if (ArgTy->isPointerType()) |
| 475 | return pointer_type_class; |
| 476 | else if (ArgTy->isReferenceType()) |
| 477 | return reference_type_class; |
| 478 | else if (ArgTy->isRealType()) |
| 479 | return real_type_class; |
| 480 | else if (ArgTy->isComplexType()) |
| 481 | return complex_type_class; |
| 482 | else if (ArgTy->isFunctionType()) |
| 483 | return function_type_class; |
| 484 | else if (ArgTy->isStructureType()) |
| 485 | return record_type_class; |
| 486 | else if (ArgTy->isUnionType()) |
| 487 | return union_type_class; |
| 488 | else if (ArgTy->isArrayType()) |
| 489 | return array_type_class; |
| 490 | else if (ArgTy->isUnionType()) |
| 491 | return union_type_class; |
| 492 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 493 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 494 | return -1; |
| 495 | } |
| 496 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 497 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| 498 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 500 | switch (E->isBuiltinCall()) { |
| 501 | default: |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 502 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 503 | case Builtin::BI__builtin_classify_type: |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 504 | Result.setIsSigned(true); |
| 505 | Result = EvaluateBuiltinClassifyType(E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 506 | return true; |
| 507 | |
| 508 | case Builtin::BI__builtin_constant_p: { |
| 509 | // __builtin_constant_p always has one operand: it returns true if that |
| 510 | // operand can be folded, false otherwise. |
| 511 | APValue Res; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 512 | Result = E->getArg(0)->Evaluate(Res, Info.Ctx); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 513 | return true; |
| 514 | } |
| 515 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 516 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 517 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 518 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 519 | if (E->getOpcode() == BinaryOperator::Comma) { |
| 520 | // Evaluate the side that actually matters; this needs to be |
| 521 | // handled specially because calling Visit() on the LHS can |
| 522 | // have strange results when it doesn't have an integral type. |
Nuno Lopes | f9ef0c6 | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 523 | if (Visit(E->getRHS())) |
| 524 | return true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 525 | |
| 526 | // Check for isEvaluated; the idea is that this might eventually |
| 527 | // be useful for isICE and other similar uses that care about |
| 528 | // whether a comma is evaluated. This isn't really used yet, though, |
| 529 | // and I'm not sure it really works as intended. |
| 530 | if (!Info.isEvaluated) |
Nuno Lopes | f9ef0c6 | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 531 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 532 | |
Nuno Lopes | f9ef0c6 | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 533 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | if (E->isLogicalOp()) { |
| 537 | // These need to be handled specially because the operands aren't |
| 538 | // necessarily integral |
| 539 | bool bres; |
| 540 | if (!HandleConversionToBool(E->getLHS(), bres, Info)) { |
| 541 | // We can't evaluate the LHS; however, sometimes the result |
| 542 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 543 | if (HandleConversionToBool(E->getRHS(), bres, Info) && |
| 544 | bres == (E->getOpcode() == BinaryOperator::LOr)) { |
| 545 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 546 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 547 | Result = bres; |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | // Really can't evaluate |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | bool bres2; |
| 556 | if (HandleConversionToBool(E->getRHS(), bres2, Info)) { |
| 557 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 558 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 559 | if (E->getOpcode() == BinaryOperator::LOr) |
| 560 | Result = bres || bres2; |
| 561 | else |
| 562 | Result = bres && bres2; |
| 563 | return true; |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 568 | QualType LHSTy = E->getLHS()->getType(); |
| 569 | QualType RHSTy = E->getRHS()->getType(); |
| 570 | |
| 571 | if (LHSTy->isRealFloatingType() && |
| 572 | RHSTy->isRealFloatingType()) { |
| 573 | APFloat RHS(0.0), LHS(0.0); |
| 574 | |
| 575 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 576 | return false; |
| 577 | |
| 578 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 579 | return false; |
| 580 | |
| 581 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame^] | 582 | |
| 583 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 584 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 585 | switch (E->getOpcode()) { |
| 586 | default: |
| 587 | assert(0 && "Invalid binary operator!"); |
| 588 | case BinaryOperator::LT: |
| 589 | Result = CR == APFloat::cmpLessThan; |
| 590 | break; |
| 591 | case BinaryOperator::GT: |
| 592 | Result = CR == APFloat::cmpGreaterThan; |
| 593 | break; |
| 594 | case BinaryOperator::LE: |
| 595 | Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual; |
| 596 | break; |
| 597 | case BinaryOperator::GE: |
| 598 | Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual; |
| 599 | break; |
| 600 | case BinaryOperator::EQ: |
| 601 | Result = CR == APFloat::cmpEqual; |
| 602 | break; |
| 603 | case BinaryOperator::NE: |
| 604 | Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan; |
| 605 | break; |
| 606 | } |
| 607 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 608 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 609 | return true; |
| 610 | } |
| 611 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 612 | if (E->getOpcode() == BinaryOperator::Sub) { |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame^] | 613 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 614 | APValue LHSValue; |
| 615 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 616 | return false; |
| 617 | |
| 618 | APValue RHSValue; |
| 619 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 620 | return false; |
| 621 | |
| 622 | // FIXME: Is this correct? What if only one of the operands has a base? |
| 623 | if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) |
| 624 | return false; |
| 625 | |
| 626 | const QualType Type = E->getLHS()->getType(); |
| 627 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 628 | |
| 629 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
| 630 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
| 631 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 632 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame^] | 633 | Result = D; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 634 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 635 | |
| 636 | return true; |
| 637 | } |
| 638 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 639 | if (!LHSTy->isIntegralType() || |
| 640 | !RHSTy->isIntegralType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 641 | // We can't continue from here for non-integral types, and they |
| 642 | // could potentially confuse the following operations. |
| 643 | // FIXME: Deal with EQ and friends. |
| 644 | return false; |
| 645 | } |
| 646 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 647 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 648 | llvm::APSInt RHS(32); |
Chris Lattner | c8cc9cc | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 649 | if (!Visit(E->getLHS())) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 650 | return false; // error in subexpression. |
Chris Lattner | c8cc9cc | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 651 | } |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 652 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 653 | |
| 654 | // FIXME Maybe we want to succeed even where we can't evaluate the |
| 655 | // right side of LAnd/LOr? |
| 656 | // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525 |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 657 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 658 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 659 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 660 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 661 | default: |
| 662 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant,E->getType()); |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 663 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 664 | case BinaryOperator::Add: Result += RHS; return true; |
| 665 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 666 | case BinaryOperator::And: Result &= RHS; return true; |
| 667 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 668 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 669 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 670 | if (RHS == 0) |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 671 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 672 | E->getType()); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 673 | Result /= RHS; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 674 | break; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 675 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 676 | if (RHS == 0) |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 677 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 678 | E->getType()); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 679 | Result %= RHS; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 680 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 681 | case BinaryOperator::Shl: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 682 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 683 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 684 | break; |
| 685 | case BinaryOperator::Shr: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 686 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 687 | break; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 688 | |
Chris Lattner | ac7cb60 | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 689 | case BinaryOperator::LT: |
| 690 | Result = Result < RHS; |
| 691 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 692 | break; |
| 693 | case BinaryOperator::GT: |
| 694 | Result = Result > RHS; |
| 695 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 696 | break; |
| 697 | case BinaryOperator::LE: |
| 698 | Result = Result <= RHS; |
| 699 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 700 | break; |
| 701 | case BinaryOperator::GE: |
| 702 | Result = Result >= RHS; |
| 703 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 704 | break; |
| 705 | case BinaryOperator::EQ: |
| 706 | Result = Result == RHS; |
| 707 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 708 | break; |
| 709 | case BinaryOperator::NE: |
| 710 | Result = Result != RHS; |
| 711 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 712 | break; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 713 | case BinaryOperator::LAnd: |
| 714 | Result = Result != 0 && RHS != 0; |
| 715 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 716 | break; |
| 717 | case BinaryOperator::LOr: |
| 718 | Result = Result != 0 || RHS != 0; |
| 719 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 720 | break; |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 721 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 722 | |
| 723 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 724 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 727 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 728 | bool Cond; |
| 729 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 730 | return false; |
| 731 | |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 732 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 735 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 736 | /// expression's type. |
| 737 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 738 | QualType DstTy = E->getType(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 739 | // Return the result in the right width. |
| 740 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 741 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 742 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 743 | QualType SrcTy = E->getTypeOfArgument(); |
| 744 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 745 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 746 | if (SrcTy->isVoidType()) { |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 747 | Result = 1; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 750 | |
| 751 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 752 | // FIXME: But alignof(vla) is! |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 753 | if (!SrcTy->isConstantSizeType()) { |
| 754 | // FIXME: Should we attempt to evaluate this? |
| 755 | return false; |
| 756 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 757 | |
| 758 | bool isSizeOf = E->isSizeOf(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 759 | |
| 760 | // GCC extension: sizeof(function) = 1. |
| 761 | if (SrcTy->isFunctionType()) { |
| 762 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 763 | Result = isSizeOf ? 1 : 4; |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | // Get information about the size or align. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 768 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 769 | if (isSizeOf) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 770 | Result = Info.Ctx.getTypeSize(SrcTy) / CharSize; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 771 | else |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 772 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 776 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 777 | // Special case unary operators that do not need their subexpression |
| 778 | // evaluated. offsetof/sizeof/alignof are all special. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 779 | if (E->isOffsetOfOp()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 780 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 781 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 782 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 783 | return true; |
| 784 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 785 | |
| 786 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 787 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 788 | bool bres; |
| 789 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 790 | return false; |
| 791 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 792 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 793 | Result = !bres; |
| 794 | return true; |
| 795 | } |
| 796 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 797 | // Get the operand value into 'Result'. |
| 798 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 799 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 801 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 802 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 803 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 804 | // See C99 6.6p3. |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 805 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant, |
| 806 | E->getType()); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 807 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 808 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 809 | // If so, we could clear the diagnostic ID. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 810 | case UnaryOperator::Plus: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 811 | // The result is always just the subexpr. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 812 | break; |
| 813 | case UnaryOperator::Minus: |
| 814 | Result = -Result; |
| 815 | break; |
| 816 | case UnaryOperator::Not: |
| 817 | Result = ~Result; |
| 818 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 822 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 825 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 826 | /// result type is integer. |
| 827 | bool IntExprEvaluator::HandleCast(SourceLocation CastLoc, |
| 828 | Expr *SubExpr, QualType DestType) { |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 829 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 830 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 831 | if (DestType->isBooleanType()) { |
| 832 | bool BoolResult; |
| 833 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 834 | return false; |
| 835 | Result.zextOrTrunc(DestWidth); |
| 836 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 837 | Result = BoolResult; |
| 838 | return true; |
| 839 | } |
| 840 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 841 | // Handle simple integer->integer casts. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 842 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 843 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 844 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 845 | |
| 846 | // Figure out if this is a truncate, extend or noop cast. |
| 847 | // If the input is signed, do a sign extend, noop, or truncate. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 848 | Result.extOrTrunc(DestWidth); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 849 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | // FIXME: Clean this up! |
| 854 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 855 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 856 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 857 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 858 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 859 | if (LV.getLValueBase()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 860 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 861 | |
Anders Carlsson | 559e56b | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 862 | Result.extOrTrunc(DestWidth); |
| 863 | Result = LV.getLValueOffset(); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 864 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 865 | return true; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 866 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 867 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 868 | if (!SubExpr->getType()->isRealFloatingType()) |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 869 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 870 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 871 | APFloat F(0.0); |
| 872 | if (!EvaluateFloat(SubExpr, F, Info)) |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 873 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 874 | |
| 875 | // Determine whether we are converting to unsigned or signed. |
| 876 | bool DestSigned = DestType->isSignedIntegerType(); |
| 877 | |
| 878 | // FIXME: Warning for overflow. |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 879 | uint64_t Space[4]; |
| 880 | bool ignored; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 881 | (void)F.convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 882 | llvm::APFloat::rmTowardZero, &ignored); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 883 | Result = llvm::APInt(DestWidth, 4, Space); |
| 884 | Result.setIsUnsigned(!DestSigned); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 885 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 886 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 887 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 888 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 889 | // Float Evaluation |
| 890 | //===----------------------------------------------------------------------===// |
| 891 | |
| 892 | namespace { |
| 893 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 894 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 895 | EvalInfo &Info; |
| 896 | APFloat &Result; |
| 897 | public: |
| 898 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 899 | : Info(info), Result(result) {} |
| 900 | |
| 901 | bool VisitStmt(Stmt *S) { |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 906 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 907 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 908 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 909 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 910 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 911 | bool VisitCastExpr(CastExpr *E); |
| 912 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 913 | }; |
| 914 | } // end anonymous namespace |
| 915 | |
| 916 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 917 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 918 | } |
| 919 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 920 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 921 | switch (E->isBuiltinCall()) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 922 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 923 | case Builtin::BI__builtin_huge_val: |
| 924 | case Builtin::BI__builtin_huge_valf: |
| 925 | case Builtin::BI__builtin_huge_vall: |
| 926 | case Builtin::BI__builtin_inf: |
| 927 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 928 | case Builtin::BI__builtin_infl: { |
| 929 | const llvm::fltSemantics &Sem = |
| 930 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 931 | Result = llvm::APFloat::getInf(Sem); |
| 932 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 934 | |
| 935 | case Builtin::BI__builtin_nan: |
| 936 | case Builtin::BI__builtin_nanf: |
| 937 | case Builtin::BI__builtin_nanl: |
| 938 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 939 | // can't constant fold it. |
| 940 | if (const StringLiteral *S = |
| 941 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 942 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 943 | const llvm::fltSemantics &Sem = |
| 944 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 945 | Result = llvm::APFloat::getNaN(Sem); |
| 946 | return true; |
| 947 | } |
| 948 | } |
| 949 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 950 | |
| 951 | case Builtin::BI__builtin_fabs: |
| 952 | case Builtin::BI__builtin_fabsf: |
| 953 | case Builtin::BI__builtin_fabsl: |
| 954 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 955 | return false; |
| 956 | |
| 957 | if (Result.isNegative()) |
| 958 | Result.changeSign(); |
| 959 | return true; |
| 960 | |
| 961 | case Builtin::BI__builtin_copysign: |
| 962 | case Builtin::BI__builtin_copysignf: |
| 963 | case Builtin::BI__builtin_copysignl: { |
| 964 | APFloat RHS(0.); |
| 965 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 966 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 967 | return false; |
| 968 | Result.copySign(RHS); |
| 969 | return true; |
| 970 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 974 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 975 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 976 | return false; |
| 977 | |
| 978 | switch (E->getOpcode()) { |
| 979 | default: return false; |
| 980 | case UnaryOperator::Plus: |
| 981 | return true; |
| 982 | case UnaryOperator::Minus: |
| 983 | Result.changeSign(); |
| 984 | return true; |
| 985 | } |
| 986 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 987 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 988 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 989 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 990 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 991 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 992 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 993 | return false; |
| 994 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 995 | return false; |
| 996 | |
| 997 | switch (E->getOpcode()) { |
| 998 | default: return false; |
| 999 | case BinaryOperator::Mul: |
| 1000 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1001 | return true; |
| 1002 | case BinaryOperator::Add: |
| 1003 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1004 | return true; |
| 1005 | case BinaryOperator::Sub: |
| 1006 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1007 | return true; |
| 1008 | case BinaryOperator::Div: |
| 1009 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1010 | return true; |
| 1011 | case BinaryOperator::Rem: |
| 1012 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 1013 | return true; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1018 | Result = E->getValue(); |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1022 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1023 | Expr* SubExpr = E->getSubExpr(); |
| 1024 | const llvm::fltSemantics& destSemantics = |
| 1025 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 1026 | if (SubExpr->getType()->isIntegralType()) { |
| 1027 | APSInt IntResult; |
| 1028 | if (!EvaluateInteger(E, IntResult, Info)) |
| 1029 | return false; |
| 1030 | Result = APFloat(destSemantics, 1); |
| 1031 | Result.convertFromAPInt(IntResult, IntResult.isSigned(), |
| 1032 | APFloat::rmNearestTiesToEven); |
| 1033 | return true; |
| 1034 | } |
| 1035 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1036 | if (!Visit(SubExpr)) |
| 1037 | return false; |
| 1038 | bool ignored; |
| 1039 | Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored); |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1047 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1051 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1052 | // Complex Float Evaluation |
| 1053 | //===----------------------------------------------------------------------===// |
| 1054 | |
| 1055 | namespace { |
| 1056 | class VISIBILITY_HIDDEN ComplexFloatExprEvaluator |
| 1057 | : public StmtVisitor<ComplexFloatExprEvaluator, APValue> { |
| 1058 | EvalInfo &Info; |
| 1059 | |
| 1060 | public: |
| 1061 | ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {} |
| 1062 | |
| 1063 | //===--------------------------------------------------------------------===// |
| 1064 | // Visitor Methods |
| 1065 | //===--------------------------------------------------------------------===// |
| 1066 | |
| 1067 | APValue VisitStmt(Stmt *S) { |
| 1068 | assert(0 && "This should be called on complex floats"); |
| 1069 | return APValue(); |
| 1070 | } |
| 1071 | |
| 1072 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1073 | |
| 1074 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 1075 | APFloat Result(0.0); |
| 1076 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1077 | return APValue(); |
| 1078 | |
| 1079 | return APValue(APFloat(0.0), Result); |
| 1080 | } |
| 1081 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1082 | APValue VisitCastExpr(CastExpr *E) { |
| 1083 | Expr* SubExpr = E->getSubExpr(); |
| 1084 | |
| 1085 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1086 | APFloat Result(0.0); |
| 1087 | |
| 1088 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1089 | return APValue(); |
| 1090 | |
| 1091 | return APValue(Result, APFloat(0.0)); |
| 1092 | } |
| 1093 | |
| 1094 | // FIXME: Handle more casts. |
| 1095 | return APValue(); |
| 1096 | } |
| 1097 | |
| 1098 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 1099 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1100 | }; |
| 1101 | } // end anonymous namespace |
| 1102 | |
| 1103 | static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info) |
| 1104 | { |
| 1105 | Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1106 | return Result.isComplexFloat(); |
| 1107 | } |
| 1108 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1109 | APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) |
| 1110 | { |
| 1111 | APValue Result, RHS; |
| 1112 | |
| 1113 | if (!EvaluateComplexFloat(E->getLHS(), Result, Info)) |
| 1114 | return APValue(); |
| 1115 | |
| 1116 | if (!EvaluateComplexFloat(E->getRHS(), RHS, Info)) |
| 1117 | return APValue(); |
| 1118 | |
| 1119 | switch (E->getOpcode()) { |
| 1120 | default: return APValue(); |
| 1121 | case BinaryOperator::Add: |
| 1122 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1123 | APFloat::rmNearestTiesToEven); |
| 1124 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1125 | APFloat::rmNearestTiesToEven); |
| 1126 | case BinaryOperator::Sub: |
| 1127 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1128 | APFloat::rmNearestTiesToEven); |
| 1129 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1130 | APFloat::rmNearestTiesToEven); |
| 1131 | } |
| 1132 | |
| 1133 | return Result; |
| 1134 | } |
| 1135 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1136 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1137 | // Top level Expr::Evaluate method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1138 | //===----------------------------------------------------------------------===// |
| 1139 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1140 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1141 | /// any crazy technique (that has nothing to do with language standards) that |
| 1142 | /// we want to. If this function returns true, it returns the folded constant |
| 1143 | /// in Result. |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1144 | bool Expr::Evaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1145 | EvalInfo Info(Ctx); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 1146 | if (getType()->isIntegerType()) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1147 | llvm::APSInt sInt(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1148 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 1149 | Result = APValue(sInt); |
| 1150 | return true; |
| 1151 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1152 | } else if (getType()->isPointerType()) { |
| 1153 | if (EvaluatePointer(this, Result, Info)) { |
| 1154 | return true; |
| 1155 | } |
| 1156 | } else if (getType()->isRealFloatingType()) { |
| 1157 | llvm::APFloat f(0.0); |
| 1158 | if (EvaluateFloat(this, f, Info)) { |
| 1159 | Result = APValue(f); |
| 1160 | return true; |
| 1161 | } |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1162 | } else if (getType()->isComplexType()) { |
| 1163 | if (EvaluateComplexFloat(this, Result, Info)) |
| 1164 | return true; |
| 1165 | } |
Anders Carlsson | 165a70f | 2008-08-10 17:03:01 +0000 | [diff] [blame] | 1166 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1170 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1171 | /// folded, but discard the result. |
| 1172 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
| 1173 | APValue V; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1174 | return Evaluate(V, Ctx); |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1175 | } |