Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c7436af | 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 | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | efddb9c | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 22 | using llvm::APSInt; |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 23 | using llvm::APFloat; |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 422373c | 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 | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame^] | 42 | /// EvalResult - Contains information about the evaluation. |
| 43 | Expr::EvalResult &EvalResult; |
| 44 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 45 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 46 | /// if it is short-circuited (according to C rules). |
| 47 | bool isEvaluated; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame^] | 49 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx), |
| 50 | EvalResult(evalresult), isEvaluated(true) {} |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 54 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 422373c | 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 | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 57 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 58 | static bool EvaluateComplexFloat(const Expr *E, APValue &Result, |
| 59 | EvalInfo &Info); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 60 | |
| 61 | //===----------------------------------------------------------------------===// |
Eli Friedman | 7888b93 | 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 | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 103 | #if 0 |
Eli Friedman | 7888b93 | 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 | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 107 | #endif |
Eli Friedman | 7888b93 | 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 | e284ebe | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 112 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Eli Friedman | 7888b93 | 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 | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 117 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 7888b93 | 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 | e284ebe | 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 | 7888b93 | 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 | 027f288 | 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 | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 189 | |
| 190 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 191 | // Pointer Evaluation |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 194 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 195 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 196 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 197 | EvalInfo &Info; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 198 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 200 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 201 | |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 202 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 02a34c3 | 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 | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 208 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 209 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 7888b93 | 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 | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 214 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 215 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 217 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 218 | if (!E->getType()->isPointerType()) |
| 219 | return false; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 220 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | a823ccf | 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 | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 235 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 236 | return APValue(); |
| 237 | |
| 238 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 239 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 240 | return APValue(); |
| 241 | |
Eli Friedman | 7888b93 | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 245 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 246 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 247 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 248 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 249 | else |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 250 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 251 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 252 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 253 | } |
Eli Friedman | 7888b93 | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 269 | |
| 270 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 271 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 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 | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 277 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 278 | return Result; |
| 279 | return APValue(); |
| 280 | } |
| 281 | |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 282 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 283 | llvm::APSInt Result(32); |
Chris Lattner | 422373c | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 286 | return APValue(0, Result.getZExtValue()); |
| 287 | } |
| 288 | } |
Eli Friedman | 7888b93 | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 299 | return APValue(); |
| 300 | } |
| 301 | |
Eli Friedman | 7888b93 | 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 | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 314 | |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | // Integer Evaluation |
| 317 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 318 | |
| 319 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 320 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 321 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 322 | EvalInfo &Info; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 323 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 324 | public: |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 325 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 326 | : Info(info), Result(result) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 328 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 329 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 330 | } |
| 331 | |
| 332 | bool Extension(SourceLocation L, diag::kind D) { |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame^] | 333 | Info.EvalResult.DiagLoc = L; |
| 334 | Info.EvalResult.Diag = D; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 335 | return true; // still a constant. |
| 336 | } |
| 337 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 338 | bool Error(SourceLocation L, diag::kind D, QualType ExprTy) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 339 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 340 | // error. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 341 | if (!Info.isEvaluated) { |
| 342 | // If error is ignored because the value isn't evaluated, get the real |
| 343 | // type at least to prevent errors downstream. |
| 344 | Result.zextOrTrunc(getIntTypeSizeInBits(ExprTy)); |
| 345 | Result.setIsUnsigned(ExprTy->isUnsignedIntegerType()); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 346 | return true; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 347 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 349 | // Take the first error. |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame^] | 350 | if (Info.EvalResult.Diag == 0) { |
| 351 | Info.EvalResult.DiagLoc = L; |
| 352 | Info.EvalResult.Diag = D; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 353 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 354 | return false; |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 357 | //===--------------------------------------------------------------------===// |
| 358 | // Visitor Methods |
| 359 | //===--------------------------------------------------------------------===// |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 360 | |
| 361 | bool VisitStmt(Stmt *) { |
| 362 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 363 | return false; |
| 364 | } |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 366 | bool VisitExpr(Expr *E) { |
| 367 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 370 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 372 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| 373 | Result = E->getValue(); |
| 374 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 375 | return true; |
| 376 | } |
| 377 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 378 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 379 | Result = E->getValue(); |
| 380 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 381 | return true; |
| 382 | } |
| 383 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 384 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Daniel Dunbar | da8ebd2 | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 385 | // Per gcc docs "this built-in function ignores top level |
| 386 | // qualifiers". We need to use the canonical version to properly |
| 387 | // be able to strip CRV qualifiers from the type. |
| 388 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 389 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
| 390 | Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 391 | T1.getUnqualifiedType()); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 395 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 396 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 397 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 398 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 399 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 400 | bool VisitCastExpr(CastExpr* E) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 401 | return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType()); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 402 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 403 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 404 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 405 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 406 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 407 | Result = E->getValue(); |
| 408 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
| 413 | Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType())); |
| 414 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 415 | return true; |
| 416 | } |
| 417 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 418 | private: |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 419 | bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 420 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 421 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 423 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 424 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 425 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 427 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 428 | // Enums are integer constant exprs. |
| 429 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 430 | Result = D->getInitVal(); |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | // Otherwise, random variable references are not constants. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 435 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Chris Lattner | 1eee940 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 438 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 439 | /// as GCC. |
| 440 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 441 | // The following enum mimics the values returned by GCC. |
| 442 | enum gcc_type_class { |
| 443 | no_type_class = -1, |
| 444 | void_type_class, integer_type_class, char_type_class, |
| 445 | enumeral_type_class, boolean_type_class, |
| 446 | pointer_type_class, reference_type_class, offset_type_class, |
| 447 | real_type_class, complex_type_class, |
| 448 | function_type_class, method_type_class, |
| 449 | record_type_class, union_type_class, |
| 450 | array_type_class, string_type_class, |
| 451 | lang_type_class |
| 452 | }; |
| 453 | |
| 454 | // If no argument was supplied, default to "no_type_class". This isn't |
| 455 | // ideal, however it is what gcc does. |
| 456 | if (E->getNumArgs() == 0) |
| 457 | return no_type_class; |
| 458 | |
| 459 | QualType ArgTy = E->getArg(0)->getType(); |
| 460 | if (ArgTy->isVoidType()) |
| 461 | return void_type_class; |
| 462 | else if (ArgTy->isEnumeralType()) |
| 463 | return enumeral_type_class; |
| 464 | else if (ArgTy->isBooleanType()) |
| 465 | return boolean_type_class; |
| 466 | else if (ArgTy->isCharType()) |
| 467 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 468 | else if (ArgTy->isIntegerType()) |
| 469 | return integer_type_class; |
| 470 | else if (ArgTy->isPointerType()) |
| 471 | return pointer_type_class; |
| 472 | else if (ArgTy->isReferenceType()) |
| 473 | return reference_type_class; |
| 474 | else if (ArgTy->isRealType()) |
| 475 | return real_type_class; |
| 476 | else if (ArgTy->isComplexType()) |
| 477 | return complex_type_class; |
| 478 | else if (ArgTy->isFunctionType()) |
| 479 | return function_type_class; |
| 480 | else if (ArgTy->isStructureType()) |
| 481 | return record_type_class; |
| 482 | else if (ArgTy->isUnionType()) |
| 483 | return union_type_class; |
| 484 | else if (ArgTy->isArrayType()) |
| 485 | return array_type_class; |
| 486 | else if (ArgTy->isUnionType()) |
| 487 | return union_type_class; |
| 488 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 489 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 490 | return -1; |
| 491 | } |
| 492 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 493 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| 494 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 496 | switch (E->isBuiltinCall()) { |
| 497 | default: |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 498 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 499 | case Builtin::BI__builtin_classify_type: |
Chris Lattner | 1eee940 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 500 | Result.setIsSigned(true); |
| 501 | Result = EvaluateBuiltinClassifyType(E); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 502 | return true; |
| 503 | |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 504 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 505 | // __builtin_constant_p always has one operand: it returns true if that |
| 506 | // operand can be folded, false otherwise. |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 507 | Result = E->getArg(0)->isEvaluatable(Info.Ctx); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 508 | return true; |
| 509 | } |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 510 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 511 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 512 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 513 | if (E->getOpcode() == BinaryOperator::Comma) { |
| 514 | // Evaluate the side that actually matters; this needs to be |
| 515 | // handled specially because calling Visit() on the LHS can |
| 516 | // have strange results when it doesn't have an integral type. |
Nuno Lopes | d88cb9c | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 517 | if (Visit(E->getRHS())) |
| 518 | return true; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 519 | |
| 520 | // Check for isEvaluated; the idea is that this might eventually |
| 521 | // be useful for isICE and other similar uses that care about |
| 522 | // whether a comma is evaluated. This isn't really used yet, though, |
| 523 | // and I'm not sure it really works as intended. |
| 524 | if (!Info.isEvaluated) |
Nuno Lopes | d88cb9c | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 525 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 526 | |
Nuno Lopes | d88cb9c | 2008-11-16 20:09:07 +0000 | [diff] [blame] | 527 | return false; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (E->isLogicalOp()) { |
| 531 | // These need to be handled specially because the operands aren't |
| 532 | // necessarily integral |
| 533 | bool bres; |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 534 | |
| 535 | if (HandleConversionToBool(E->getLHS(), bres, Info)) { |
| 536 | // We were able to evaluate the LHS, see if we can get away with not |
| 537 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 538 | if (bres == (E->getOpcode() == BinaryOperator::LOr) || |
| 539 | !bres == (E->getOpcode() == BinaryOperator::LAnd)) { |
| 540 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 541 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 542 | Result = bres; |
| 543 | |
| 544 | return true; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 545 | } |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 546 | |
| 547 | bool bres2; |
| 548 | if (HandleConversionToBool(E->getRHS(), bres2, Info)) { |
| 549 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 550 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 551 | if (E->getOpcode() == BinaryOperator::LOr) |
| 552 | Result = bres || bres2; |
| 553 | else |
| 554 | Result = bres && bres2; |
| 555 | return true; |
| 556 | } |
| 557 | } else { |
| 558 | if (HandleConversionToBool(E->getRHS(), bres, Info)) { |
| 559 | // We can't evaluate the LHS; however, sometimes the result |
| 560 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 561 | if (bres == (E->getOpcode() == BinaryOperator::LOr) || |
| 562 | !bres == (E->getOpcode() == BinaryOperator::LAnd)) { |
| 563 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 564 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 565 | Result = bres; |
| 566 | Info.isEvaluated = false; |
| 567 | |
| 568 | return true; |
| 569 | } |
| 570 | } |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 571 | } |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 572 | |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 576 | QualType LHSTy = E->getLHS()->getType(); |
| 577 | QualType RHSTy = E->getRHS()->getType(); |
| 578 | |
| 579 | if (LHSTy->isRealFloatingType() && |
| 580 | RHSTy->isRealFloatingType()) { |
| 581 | APFloat RHS(0.0), LHS(0.0); |
| 582 | |
| 583 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 584 | return false; |
| 585 | |
| 586 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 587 | return false; |
| 588 | |
| 589 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 590 | |
| 591 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 592 | |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 593 | switch (E->getOpcode()) { |
| 594 | default: |
| 595 | assert(0 && "Invalid binary operator!"); |
| 596 | case BinaryOperator::LT: |
| 597 | Result = CR == APFloat::cmpLessThan; |
| 598 | break; |
| 599 | case BinaryOperator::GT: |
| 600 | Result = CR == APFloat::cmpGreaterThan; |
| 601 | break; |
| 602 | case BinaryOperator::LE: |
| 603 | Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual; |
| 604 | break; |
| 605 | case BinaryOperator::GE: |
| 606 | Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual; |
| 607 | break; |
| 608 | case BinaryOperator::EQ: |
| 609 | Result = CR == APFloat::cmpEqual; |
| 610 | break; |
| 611 | case BinaryOperator::NE: |
| 612 | Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan; |
| 613 | break; |
| 614 | } |
| 615 | |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 616 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 617 | return true; |
| 618 | } |
| 619 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 620 | if (E->getOpcode() == BinaryOperator::Sub) { |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 621 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 622 | APValue LHSValue; |
| 623 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 624 | return false; |
| 625 | |
| 626 | APValue RHSValue; |
| 627 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 628 | return false; |
| 629 | |
| 630 | // FIXME: Is this correct? What if only one of the operands has a base? |
| 631 | if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) |
| 632 | return false; |
| 633 | |
| 634 | const QualType Type = E->getLHS()->getType(); |
| 635 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 636 | |
| 637 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
| 638 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
| 639 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 640 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 641 | Result = D; |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 642 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 643 | |
| 644 | return true; |
| 645 | } |
| 646 | } |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 647 | if (!LHSTy->isIntegralType() || |
| 648 | !RHSTy->isIntegralType()) { |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 649 | // We can't continue from here for non-integral types, and they |
| 650 | // could potentially confuse the following operations. |
| 651 | // FIXME: Deal with EQ and friends. |
| 652 | return false; |
| 653 | } |
| 654 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 655 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 656 | llvm::APSInt RHS(32); |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 657 | if (!Visit(E->getLHS())) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 658 | return false; // error in subexpression. |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 659 | } |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 660 | |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 661 | |
| 662 | // FIXME Maybe we want to succeed even where we can't evaluate the |
| 663 | // right side of LAnd/LOr? |
| 664 | // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525 |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 665 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 666 | return false; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 668 | switch (E->getOpcode()) { |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 669 | default: |
| 670 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant,E->getType()); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 671 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 672 | case BinaryOperator::Add: Result += RHS; return true; |
| 673 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 674 | case BinaryOperator::And: Result &= RHS; return true; |
| 675 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 676 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 677 | case BinaryOperator::Div: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 678 | if (RHS == 0) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 679 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 680 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 681 | Result /= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 682 | break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 683 | case BinaryOperator::Rem: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 684 | if (RHS == 0) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 685 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 686 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 687 | Result %= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 688 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 689 | case BinaryOperator::Shl: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 690 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 691 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 692 | break; |
| 693 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 694 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 695 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 697 | case BinaryOperator::LT: |
| 698 | Result = Result < RHS; |
| 699 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 700 | break; |
| 701 | case BinaryOperator::GT: |
| 702 | Result = Result > RHS; |
| 703 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 704 | break; |
| 705 | case BinaryOperator::LE: |
| 706 | Result = Result <= RHS; |
| 707 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 708 | break; |
| 709 | case BinaryOperator::GE: |
| 710 | Result = Result >= RHS; |
| 711 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 712 | break; |
| 713 | case BinaryOperator::EQ: |
| 714 | Result = Result == RHS; |
| 715 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 716 | break; |
| 717 | case BinaryOperator::NE: |
| 718 | Result = Result != RHS; |
| 719 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 720 | break; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 721 | case BinaryOperator::LAnd: |
| 722 | Result = Result != 0 && RHS != 0; |
| 723 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 724 | break; |
| 725 | case BinaryOperator::LOr: |
| 726 | Result = Result != 0 || RHS != 0; |
| 727 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 728 | break; |
Eli Friedman | b2935ab | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 729 | } |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 730 | |
| 731 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 732 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 735 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 308de75 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 736 | bool Cond; |
| 737 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 738 | return false; |
| 739 | |
Nuno Lopes | 308de75 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 740 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 743 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 744 | /// expression's type. |
| 745 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 746 | QualType DstTy = E->getType(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 747 | // Return the result in the right width. |
| 748 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 749 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 750 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 751 | QualType SrcTy = E->getTypeOfArgument(); |
| 752 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 753 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 754 | if (SrcTy->isVoidType()) { |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 755 | Result = 1; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 756 | return true; |
| 757 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 758 | |
| 759 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 760 | // FIXME: But alignof(vla) is! |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 761 | if (!SrcTy->isConstantSizeType()) { |
| 762 | // FIXME: Should we attempt to evaluate this? |
| 763 | return false; |
| 764 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 765 | |
| 766 | bool isSizeOf = E->isSizeOf(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 767 | |
| 768 | // GCC extension: sizeof(function) = 1. |
| 769 | if (SrcTy->isFunctionType()) { |
| 770 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 771 | Result = isSizeOf ? 1 : 4; |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | // Get information about the size or align. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 776 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 777 | if (isSizeOf) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 778 | Result = Info.Ctx.getTypeSize(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 779 | else |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 780 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 781 | return true; |
| 782 | } |
| 783 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 784 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 785 | // Special case unary operators that do not need their subexpression |
| 786 | // evaluated. offsetof/sizeof/alignof are all special. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 787 | if (E->isOffsetOfOp()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 788 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 789 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 790 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 791 | return true; |
| 792 | } |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 793 | |
| 794 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 795 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 796 | bool bres; |
| 797 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 798 | return false; |
| 799 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 800 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 801 | Result = !bres; |
| 802 | return true; |
| 803 | } |
| 804 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 805 | // Get the operand value into 'Result'. |
| 806 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 807 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 808 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 809 | switch (E->getOpcode()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 810 | default: |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 811 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 812 | // See C99 6.6p3. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 813 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant, |
| 814 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 815 | case UnaryOperator::Extension: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 816 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 817 | // If so, we could clear the diagnostic ID. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 818 | case UnaryOperator::Plus: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 819 | // The result is always just the subexpr. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 820 | break; |
| 821 | case UnaryOperator::Minus: |
| 822 | Result = -Result; |
| 823 | break; |
| 824 | case UnaryOperator::Not: |
| 825 | Result = ~Result; |
| 826 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 830 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 833 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 834 | /// result type is integer. |
| 835 | bool IntExprEvaluator::HandleCast(SourceLocation CastLoc, |
| 836 | Expr *SubExpr, QualType DestType) { |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 837 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 838 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 839 | if (DestType->isBooleanType()) { |
| 840 | bool BoolResult; |
| 841 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 842 | return false; |
| 843 | Result.zextOrTrunc(DestWidth); |
| 844 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 845 | Result = BoolResult; |
| 846 | return true; |
| 847 | } |
| 848 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 849 | // Handle simple integer->integer casts. |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 850 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 851 | if (!Visit(SubExpr)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 852 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 853 | |
| 854 | // Figure out if this is a truncate, extend or noop cast. |
| 855 | // If the input is signed, do a sign extend, noop, or truncate. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 856 | Result.extOrTrunc(DestWidth); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 857 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | // FIXME: Clean this up! |
| 862 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 863 | APValue LV; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 864 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 865 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 866 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 867 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 868 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 869 | |
Anders Carlsson | 8ab15c8 | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 870 | Result.extOrTrunc(DestWidth); |
| 871 | Result = LV.getLValueOffset(); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 872 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 873 | return true; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 874 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 875 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 876 | if (!SubExpr->getType()->isRealFloatingType()) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 877 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 878 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 879 | APFloat F(0.0); |
| 880 | if (!EvaluateFloat(SubExpr, F, Info)) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 881 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 882 | |
| 883 | // Determine whether we are converting to unsigned or signed. |
| 884 | bool DestSigned = DestType->isSignedIntegerType(); |
| 885 | |
| 886 | // FIXME: Warning for overflow. |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 887 | uint64_t Space[4]; |
| 888 | bool ignored; |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 889 | (void)F.convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 890 | llvm::APFloat::rmTowardZero, &ignored); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 891 | Result = llvm::APInt(DestWidth, 4, Space); |
| 892 | Result.setIsUnsigned(!DestSigned); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 893 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 894 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 895 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 896 | //===----------------------------------------------------------------------===// |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 897 | // Float Evaluation |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
| 900 | namespace { |
| 901 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 902 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 903 | EvalInfo &Info; |
| 904 | APFloat &Result; |
| 905 | public: |
| 906 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 907 | : Info(info), Result(result) {} |
| 908 | |
| 909 | bool VisitStmt(Stmt *S) { |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 914 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 915 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 916 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 917 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 918 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 919 | bool VisitCastExpr(CastExpr *E); |
| 920 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 921 | }; |
| 922 | } // end anonymous namespace |
| 923 | |
| 924 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 925 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 926 | } |
| 927 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 928 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 929 | switch (E->isBuiltinCall()) { |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 930 | default: return false; |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 931 | case Builtin::BI__builtin_huge_val: |
| 932 | case Builtin::BI__builtin_huge_valf: |
| 933 | case Builtin::BI__builtin_huge_vall: |
| 934 | case Builtin::BI__builtin_inf: |
| 935 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 936 | case Builtin::BI__builtin_infl: { |
| 937 | const llvm::fltSemantics &Sem = |
| 938 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 939 | Result = llvm::APFloat::getInf(Sem); |
| 940 | return true; |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 941 | } |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 942 | |
| 943 | case Builtin::BI__builtin_nan: |
| 944 | case Builtin::BI__builtin_nanf: |
| 945 | case Builtin::BI__builtin_nanl: |
| 946 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 947 | // can't constant fold it. |
| 948 | if (const StringLiteral *S = |
| 949 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 950 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 951 | const llvm::fltSemantics &Sem = |
| 952 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 953 | Result = llvm::APFloat::getNaN(Sem); |
| 954 | return true; |
| 955 | } |
| 956 | } |
| 957 | return false; |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 958 | |
| 959 | case Builtin::BI__builtin_fabs: |
| 960 | case Builtin::BI__builtin_fabsf: |
| 961 | case Builtin::BI__builtin_fabsl: |
| 962 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 963 | return false; |
| 964 | |
| 965 | if (Result.isNegative()) |
| 966 | Result.changeSign(); |
| 967 | return true; |
| 968 | |
| 969 | case Builtin::BI__builtin_copysign: |
| 970 | case Builtin::BI__builtin_copysignf: |
| 971 | case Builtin::BI__builtin_copysignl: { |
| 972 | APFloat RHS(0.); |
| 973 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 974 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 975 | return false; |
| 976 | Result.copySign(RHS); |
| 977 | return true; |
| 978 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 982 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | 1cea4f4 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 983 | if (E->getOpcode() == UnaryOperator::Deref) |
| 984 | return false; |
| 985 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 986 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 987 | return false; |
| 988 | |
| 989 | switch (E->getOpcode()) { |
| 990 | default: return false; |
| 991 | case UnaryOperator::Plus: |
| 992 | return true; |
| 993 | case UnaryOperator::Minus: |
| 994 | Result.changeSign(); |
| 995 | return true; |
| 996 | } |
| 997 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 998 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 999 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1000 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1001 | // and errors are supposed to work. |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1002 | APFloat RHS(0.0); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1003 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1004 | return false; |
| 1005 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1006 | return false; |
| 1007 | |
| 1008 | switch (E->getOpcode()) { |
| 1009 | default: return false; |
| 1010 | case BinaryOperator::Mul: |
| 1011 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1012 | return true; |
| 1013 | case BinaryOperator::Add: |
| 1014 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1015 | return true; |
| 1016 | case BinaryOperator::Sub: |
| 1017 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1018 | return true; |
| 1019 | case BinaryOperator::Div: |
| 1020 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1021 | return true; |
| 1022 | case BinaryOperator::Rem: |
| 1023 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 1024 | return true; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1029 | Result = E->getValue(); |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1033 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1034 | Expr* SubExpr = E->getSubExpr(); |
| 1035 | const llvm::fltSemantics& destSemantics = |
| 1036 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 1037 | if (SubExpr->getType()->isIntegralType()) { |
| 1038 | APSInt IntResult; |
| 1039 | if (!EvaluateInteger(E, IntResult, Info)) |
| 1040 | return false; |
| 1041 | Result = APFloat(destSemantics, 1); |
| 1042 | Result.convertFromAPInt(IntResult, IntResult.isSigned(), |
| 1043 | APFloat::rmNearestTiesToEven); |
| 1044 | return true; |
| 1045 | } |
| 1046 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1047 | if (!Visit(SubExpr)) |
| 1048 | return false; |
| 1049 | bool ignored; |
| 1050 | Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored); |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
| 1057 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1058 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1062 | //===----------------------------------------------------------------------===// |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1063 | // Complex Float Evaluation |
| 1064 | //===----------------------------------------------------------------------===// |
| 1065 | |
| 1066 | namespace { |
| 1067 | class VISIBILITY_HIDDEN ComplexFloatExprEvaluator |
| 1068 | : public StmtVisitor<ComplexFloatExprEvaluator, APValue> { |
| 1069 | EvalInfo &Info; |
| 1070 | |
| 1071 | public: |
| 1072 | ComplexFloatExprEvaluator(EvalInfo &info) : Info(info) {} |
| 1073 | |
| 1074 | //===--------------------------------------------------------------------===// |
| 1075 | // Visitor Methods |
| 1076 | //===--------------------------------------------------------------------===// |
| 1077 | |
| 1078 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1079 | return APValue(); |
| 1080 | } |
| 1081 | |
| 1082 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1083 | |
| 1084 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 1085 | APFloat Result(0.0); |
| 1086 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1087 | return APValue(); |
| 1088 | |
| 1089 | return APValue(APFloat(0.0), Result); |
| 1090 | } |
| 1091 | |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1092 | APValue VisitCastExpr(CastExpr *E) { |
| 1093 | Expr* SubExpr = E->getSubExpr(); |
| 1094 | |
| 1095 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1096 | APFloat Result(0.0); |
| 1097 | |
| 1098 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1099 | return APValue(); |
| 1100 | |
| 1101 | return APValue(Result, APFloat(0.0)); |
| 1102 | } |
| 1103 | |
| 1104 | // FIXME: Handle more casts. |
| 1105 | return APValue(); |
| 1106 | } |
| 1107 | |
| 1108 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 1109 | |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1110 | }; |
| 1111 | } // end anonymous namespace |
| 1112 | |
| 1113 | static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info) |
| 1114 | { |
| 1115 | Result = ComplexFloatExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1116 | return Result.isComplexFloat(); |
| 1117 | } |
| 1118 | |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1119 | APValue ComplexFloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) |
| 1120 | { |
| 1121 | APValue Result, RHS; |
| 1122 | |
| 1123 | if (!EvaluateComplexFloat(E->getLHS(), Result, Info)) |
| 1124 | return APValue(); |
| 1125 | |
| 1126 | if (!EvaluateComplexFloat(E->getRHS(), RHS, Info)) |
| 1127 | return APValue(); |
| 1128 | |
| 1129 | switch (E->getOpcode()) { |
| 1130 | default: return APValue(); |
| 1131 | case BinaryOperator::Add: |
| 1132 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1133 | APFloat::rmNearestTiesToEven); |
| 1134 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1135 | APFloat::rmNearestTiesToEven); |
| 1136 | case BinaryOperator::Sub: |
| 1137 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1138 | APFloat::rmNearestTiesToEven); |
| 1139 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1140 | APFloat::rmNearestTiesToEven); |
| 1141 | } |
| 1142 | |
| 1143 | return Result; |
| 1144 | } |
| 1145 | |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1146 | //===----------------------------------------------------------------------===// |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1147 | // Top level Expr::Evaluate method. |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1148 | //===----------------------------------------------------------------------===// |
| 1149 | |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1150 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1151 | /// any crazy technique (that has nothing to do with language standards) that |
| 1152 | /// we want to. If this function returns true, it returns the folded constant |
| 1153 | /// in Result. |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1154 | bool Expr::Evaluate(APValue &Result, ASTContext &Ctx, bool *isEvaluated) const { |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame^] | 1155 | Expr::EvalResult EvalResult; |
| 1156 | EvalInfo Info(Ctx, EvalResult); |
| 1157 | |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 1158 | if (getType()->isIntegerType()) { |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1159 | llvm::APSInt sInt(32); |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1160 | if (!EvaluateInteger(this, sInt, Info)) |
| 1161 | return false; |
| 1162 | |
| 1163 | Result = APValue(sInt); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1164 | } else if (getType()->isPointerType()) { |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1165 | if (!EvaluatePointer(this, Result, Info)) |
| 1166 | return false; |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1167 | } else if (getType()->isRealFloatingType()) { |
| 1168 | llvm::APFloat f(0.0); |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1169 | if (!EvaluateFloat(this, f, Info)) |
| 1170 | return false; |
| 1171 | |
| 1172 | Result = APValue(f); |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1173 | } else if (getType()->isComplexType()) { |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1174 | if (!EvaluateComplexFloat(this, Result, Info)) |
| 1175 | return false; |
Anders Carlsson | cb6a2e8 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1176 | } else |
| 1177 | return false; |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1178 | |
| 1179 | if (isEvaluated) |
| 1180 | *isEvaluated = Info.isEvaluated; |
| 1181 | return true; |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 1182 | } |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1184 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1185 | /// folded, but discard the result. |
| 1186 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
| 1187 | APValue V; |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1188 | return Evaluate(V, Ctx); |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1189 | } |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1190 | |
| 1191 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
| 1192 | APValue V; |
| 1193 | bool Result = Evaluate(V, Ctx); |
| 1194 | assert(Result && "Could not evaluate expression"); |
| 1195 | assert(V.isInt() && "Expression did not evaluate to integer"); |
| 1196 | |
| 1197 | return V.getInt(); |
| 1198 | } |