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