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