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 | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTDiagnostic.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; |
Anders Carlsson | 6c1a9e2 | 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 | 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), |
Anders Carlsson | 6c1a9e2 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 50 | EvalResult(evalresult), ShortCircuit(0) {} |
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); |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 58 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 59 | |
| 60 | //===----------------------------------------------------------------------===// |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 61 | // Misc utilities |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
| 64 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 65 | if (E->getType()->isIntegralType()) { |
| 66 | APSInt IntResult; |
| 67 | if (!EvaluateInteger(E, IntResult, Info)) |
| 68 | return false; |
| 69 | Result = IntResult != 0; |
| 70 | return true; |
| 71 | } else if (E->getType()->isRealFloatingType()) { |
| 72 | APFloat FloatResult(0.0); |
| 73 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 74 | return false; |
| 75 | Result = !FloatResult.isZero(); |
| 76 | return true; |
| 77 | } else if (E->getType()->isPointerType()) { |
| 78 | APValue PointerResult; |
| 79 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 80 | return false; |
| 81 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 82 | // the check look like? |
| 83 | Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 90 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
| 91 | APFloat &Value, ASTContext &Ctx) { |
| 92 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 93 | // Determine whether we are converting to unsigned or signed. |
| 94 | bool DestSigned = DestType->isSignedIntegerType(); |
| 95 | |
| 96 | // FIXME: Warning for overflow. |
| 97 | uint64_t Space[4]; |
| 98 | bool ignored; |
| 99 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 100 | llvm::APFloat::rmTowardZero, &ignored); |
| 101 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 102 | } |
| 103 | |
| 104 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
| 105 | APFloat &Value, ASTContext &Ctx) { |
| 106 | bool ignored; |
| 107 | APFloat Result = Value; |
| 108 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
| 109 | APFloat::rmNearestTiesToEven, &ignored); |
| 110 | return Result; |
| 111 | } |
| 112 | |
| 113 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
| 114 | APSInt &Value, ASTContext &Ctx) { |
| 115 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 116 | APSInt Result = Value; |
| 117 | // Figure out if this is a truncate, extend or noop cast. |
| 118 | // If the input is signed, do a sign extend, noop, or truncate. |
| 119 | Result.extOrTrunc(DestWidth); |
| 120 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 121 | return Result; |
| 122 | } |
| 123 | |
| 124 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
| 125 | APSInt &Value, ASTContext &Ctx) { |
| 126 | |
| 127 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 128 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 129 | APFloat::rmNearestTiesToEven); |
| 130 | return Result; |
| 131 | } |
| 132 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // LValue Evaluation |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | namespace { |
| 137 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 138 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 139 | EvalInfo &Info; |
| 140 | public: |
| 141 | |
| 142 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 143 | |
| 144 | APValue VisitStmt(Stmt *S) { |
Daniel Dunbar | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 145 | #if 0 |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 146 | // FIXME: Remove this when we support more expressions. |
| 147 | printf("Unhandled pointer statement\n"); |
| 148 | S->dump(); |
Daniel Dunbar | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 149 | #endif |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 150 | return APValue(); |
| 151 | } |
| 152 | |
| 153 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | e284ebe | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 154 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 155 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 156 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 157 | APValue VisitMemberExpr(MemberExpr *E); |
| 158 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 159 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 160 | }; |
| 161 | } // end anonymous namespace |
| 162 | |
| 163 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 164 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 165 | return Result.isLValue(); |
| 166 | } |
| 167 | |
Anders Carlsson | e284ebe | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 168 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) |
| 169 | { |
| 170 | if (!E->hasGlobalStorage()) |
| 171 | return APValue(); |
| 172 | |
| 173 | return APValue(E, 0); |
| 174 | } |
| 175 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 176 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 177 | if (E->isFileScope()) |
| 178 | return APValue(E, 0); |
| 179 | return APValue(); |
| 180 | } |
| 181 | |
| 182 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 183 | APValue result; |
| 184 | QualType Ty; |
| 185 | if (E->isArrow()) { |
| 186 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 187 | return APValue(); |
| 188 | Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType(); |
| 189 | } else { |
| 190 | result = Visit(E->getBase()); |
| 191 | if (result.isUninit()) |
| 192 | return APValue(); |
| 193 | Ty = E->getBase()->getType(); |
| 194 | } |
| 195 | |
| 196 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
| 197 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 198 | |
| 199 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 200 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 201 | return APValue(); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 202 | |
| 203 | // FIXME: This is linear time. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 204 | unsigned i = 0; |
| 205 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 206 | FieldEnd = RD->field_end(); |
| 207 | Field != FieldEnd; (void)++Field, ++i) { |
| 208 | if (*Field == FD) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | |
| 212 | result.setLValue(result.getLValueBase(), |
| 213 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 214 | |
| 215 | return result; |
| 216 | } |
| 217 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 218 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
| 219 | { |
| 220 | APValue Result; |
| 221 | |
| 222 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 223 | return APValue(); |
| 224 | |
| 225 | APSInt Index; |
| 226 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 227 | return APValue(); |
| 228 | |
| 229 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 230 | |
| 231 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
| 232 | Result.setLValue(Result.getLValueBase(), |
| 233 | Result.getLValueOffset() + Offset); |
| 234 | return Result; |
| 235 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 236 | |
| 237 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 238 | // Pointer Evaluation |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 241 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 242 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 243 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 244 | EvalInfo &Info; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 245 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 247 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 248 | |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 249 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 250 | return APValue(); |
| 251 | } |
| 252 | |
| 253 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 254 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 255 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 256 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 257 | APValue VisitUnaryOperator(const UnaryOperator *E); |
| 258 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 259 | { return APValue(E, 0); } |
Eli Friedman | f194113 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 260 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 261 | { return APValue(E, 0); } |
Eli Friedman | 67f4ac5 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 262 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | d55240e | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 263 | APValue VisitBlockExpr(BlockExpr *E) { return APValue(E, 0); } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 264 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 265 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 266 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 268 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Mike Stump | 5b601ff | 2009-02-18 21:44:49 +0000 | [diff] [blame] | 269 | if (!E->getType()->isPointerType() |
| 270 | && !E->getType()->isBlockPointerType()) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 271 | return false; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 272 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 273 | return Result.isLValue(); |
| 274 | } |
| 275 | |
| 276 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 277 | if (E->getOpcode() != BinaryOperator::Add && |
| 278 | E->getOpcode() != BinaryOperator::Sub) |
| 279 | return APValue(); |
| 280 | |
| 281 | const Expr *PExp = E->getLHS(); |
| 282 | const Expr *IExp = E->getRHS(); |
| 283 | if (IExp->getType()->isPointerType()) |
| 284 | std::swap(PExp, IExp); |
| 285 | |
| 286 | APValue ResultLValue; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 287 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 288 | return APValue(); |
| 289 | |
| 290 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 291 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 292 | return APValue(); |
| 293 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 294 | QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType(); |
Anders Carlsson | 4fbb52c | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 295 | uint64_t SizeOfPointee; |
| 296 | |
| 297 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 298 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 299 | SizeOfPointee = 1; |
| 300 | else |
| 301 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 302 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 303 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 304 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 305 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 306 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 307 | else |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 308 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 309 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 310 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 311 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 312 | |
| 313 | APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 314 | if (E->getOpcode() == UnaryOperator::Extension) { |
| 315 | // FIXME: Deal with warnings? |
| 316 | return Visit(E->getSubExpr()); |
| 317 | } |
| 318 | |
| 319 | if (E->getOpcode() == UnaryOperator::AddrOf) { |
| 320 | APValue result; |
| 321 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | return APValue(); |
| 326 | } |
Anders Carlsson | 4ab88da | 2008-12-05 05:24:13 +0000 | [diff] [blame] | 327 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 328 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 329 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 330 | const Expr* SubExpr = E->getSubExpr(); |
| 331 | |
| 332 | // Check for pointer->pointer cast |
| 333 | if (SubExpr->getType()->isPointerType()) { |
| 334 | APValue Result; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 335 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 336 | return Result; |
| 337 | return APValue(); |
| 338 | } |
| 339 | |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 340 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 341 | llvm::APSInt Result(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 342 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 343 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 344 | return APValue(0, Result.getZExtValue()); |
| 345 | } |
| 346 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 347 | |
| 348 | if (SubExpr->getType()->isFunctionType() || |
| 349 | SubExpr->getType()->isArrayType()) { |
| 350 | APValue Result; |
| 351 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 352 | return Result; |
| 353 | return APValue(); |
| 354 | } |
| 355 | |
| 356 | //assert(0 && "Unhandled cast"); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 357 | return APValue(); |
| 358 | } |
| 359 | |
Eli Friedman | 67f4ac5 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 360 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 361 | if (E->isBuiltinCall(Info.Ctx) == |
| 362 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | 67f4ac5 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 363 | return APValue(E, 0); |
| 364 | return APValue(); |
| 365 | } |
| 366 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 367 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 368 | bool BoolResult; |
| 369 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 370 | return APValue(); |
| 371 | |
| 372 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 373 | |
| 374 | APValue Result; |
| 375 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 376 | return Result; |
| 377 | return APValue(); |
| 378 | } |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 379 | |
| 380 | //===----------------------------------------------------------------------===// |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 381 | // Vector Evaluation |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | namespace { |
| 385 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 386 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 387 | EvalInfo &Info; |
| 388 | public: |
| 389 | |
| 390 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
| 391 | |
| 392 | APValue VisitStmt(Stmt *S) { |
| 393 | return APValue(); |
| 394 | } |
| 395 | |
| 396 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 397 | APValue VisitCastExpr(const CastExpr* E); |
| 398 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 399 | APValue VisitInitListExpr(const InitListExpr *E); |
| 400 | }; |
| 401 | } // end anonymous namespace |
| 402 | |
| 403 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 404 | if (!E->getType()->isVectorType()) |
| 405 | return false; |
| 406 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 407 | return !Result.isUninit(); |
| 408 | } |
| 409 | |
| 410 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 411 | const Expr* SE = E->getSubExpr(); |
| 412 | |
| 413 | // Check for vector->vector bitcast. |
| 414 | if (SE->getType()->isVectorType()) |
| 415 | return this->Visit(const_cast<Expr*>(SE)); |
| 416 | |
| 417 | return APValue(); |
| 418 | } |
| 419 | |
| 420 | APValue |
| 421 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 422 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 423 | } |
| 424 | |
| 425 | APValue |
| 426 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 427 | const VectorType *VT = E->getType()->getAsVectorType(); |
| 428 | unsigned NumInits = E->getNumInits(); |
| 429 | |
| 430 | if (!VT || VT->getNumElements() != NumInits) |
| 431 | return APValue(); |
| 432 | |
| 433 | QualType EltTy = VT->getElementType(); |
| 434 | llvm::SmallVector<APValue, 4> Elements; |
| 435 | |
| 436 | for (unsigned i = 0; i < NumInits; i++) { |
| 437 | if (EltTy->isIntegerType()) { |
| 438 | llvm::APSInt sInt(32); |
| 439 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 440 | return APValue(); |
| 441 | Elements.push_back(APValue(sInt)); |
| 442 | } else { |
| 443 | llvm::APFloat f(0.0); |
| 444 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 445 | return APValue(); |
| 446 | Elements.push_back(APValue(f)); |
| 447 | } |
| 448 | } |
| 449 | return APValue(&Elements[0], Elements.size()); |
| 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 453 | // Integer Evaluation |
| 454 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 455 | |
| 456 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 457 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 458 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 459 | EvalInfo &Info; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 460 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 461 | public: |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 462 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 463 | : Info(info), Result(result) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 464 | |
Anders Carlsson | fa76d82 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 465 | bool Extension(SourceLocation L, diag::kind D, const Expr *E) { |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 466 | Info.EvalResult.DiagLoc = L; |
| 467 | Info.EvalResult.Diag = D; |
Anders Carlsson | fa76d82 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 468 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 469 | return true; // still a constant. |
| 470 | } |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 471 | |
| 472 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 473 | Result = I; |
| 474 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | bool Success(uint64_t Value, const Expr *E) { |
| 479 | Result = Info.Ctx.MakeIntValue(Value, E->getType()); |
| 480 | return true; |
| 481 | } |
| 482 | |
Anders Carlsson | fa76d82 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 483 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 484 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 485 | // error. |
Anders Carlsson | 6c1a9e2 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 486 | if (Info.ShortCircuit) { |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 487 | // If error is ignored because the value isn't evaluated, get the real |
| 488 | // type at least to prevent errors downstream. |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 489 | return Success(0, E); |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 490 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 492 | // Take the first error. |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 493 | if (Info.EvalResult.Diag == 0) { |
| 494 | Info.EvalResult.DiagLoc = L; |
| 495 | Info.EvalResult.Diag = D; |
Anders Carlsson | fa76d82 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 496 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 498 | return false; |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 501 | //===--------------------------------------------------------------------===// |
| 502 | // Visitor Methods |
| 503 | //===--------------------------------------------------------------------===// |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 504 | |
| 505 | bool VisitStmt(Stmt *) { |
| 506 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 507 | return false; |
| 508 | } |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 510 | bool VisitExpr(Expr *E) { |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 511 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 514 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 516 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 517 | return Success(E->getValue(), E); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 518 | } |
| 519 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 520 | return Success(E->getValue(), E); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 521 | } |
| 522 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | da8ebd2 | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 523 | // Per gcc docs "this built-in function ignores top level |
| 524 | // qualifiers". We need to use the canonical version to properly |
| 525 | // be able to strip CRV qualifiers from the type. |
| 526 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 527 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 528 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 529 | T1.getUnqualifiedType()), |
| 530 | E); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 531 | } |
| 532 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 533 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 534 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 535 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 536 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 538 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 539 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 540 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 541 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 542 | return Success(E->getValue(), E); |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Anders Carlsson | 774f9c7 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 545 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 546 | return Success(0, E); |
Anders Carlsson | 774f9c7 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 549 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 550 | return Success(0, E); |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 553 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 554 | return Success(E->EvaluateTrait(), E); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 557 | private: |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 558 | unsigned GetAlignOfExpr(const Expr *E); |
| 559 | unsigned GetAlignOfType(QualType T); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 560 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 561 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 563 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 564 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 565 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 567 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 568 | // Enums are integer constant exprs. |
| 569 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 570 | Result = D->getInitVal(); |
Eli Friedman | 8b10a23 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 571 | // FIXME: This is an ugly hack around the fact that enums don't set their |
| 572 | // signedness consistently; see PR3173 |
| 573 | Result.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 574 | return true; |
| 575 | } |
Sebastian Redl | 410dd87 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 576 | |
| 577 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
| 578 | if (Info.Ctx.getLangOptions().CPlusPlus && |
| 579 | E->getType().getCVRQualifiers() == QualType::Const) { |
| 580 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
| 581 | if (const Expr *Init = D->getInit()) |
| 582 | return Visit(const_cast<Expr*>(Init)); |
| 583 | } |
| 584 | } |
| 585 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 586 | // Otherwise, random variable references are not constants. |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 587 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Chris Lattner | 1eee940 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 590 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 591 | /// as GCC. |
| 592 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 593 | // The following enum mimics the values returned by GCC. |
| 594 | enum gcc_type_class { |
| 595 | no_type_class = -1, |
| 596 | void_type_class, integer_type_class, char_type_class, |
| 597 | enumeral_type_class, boolean_type_class, |
| 598 | pointer_type_class, reference_type_class, offset_type_class, |
| 599 | real_type_class, complex_type_class, |
| 600 | function_type_class, method_type_class, |
| 601 | record_type_class, union_type_class, |
| 602 | array_type_class, string_type_class, |
| 603 | lang_type_class |
| 604 | }; |
| 605 | |
| 606 | // If no argument was supplied, default to "no_type_class". This isn't |
| 607 | // ideal, however it is what gcc does. |
| 608 | if (E->getNumArgs() == 0) |
| 609 | return no_type_class; |
| 610 | |
| 611 | QualType ArgTy = E->getArg(0)->getType(); |
| 612 | if (ArgTy->isVoidType()) |
| 613 | return void_type_class; |
| 614 | else if (ArgTy->isEnumeralType()) |
| 615 | return enumeral_type_class; |
| 616 | else if (ArgTy->isBooleanType()) |
| 617 | return boolean_type_class; |
| 618 | else if (ArgTy->isCharType()) |
| 619 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 620 | else if (ArgTy->isIntegerType()) |
| 621 | return integer_type_class; |
| 622 | else if (ArgTy->isPointerType()) |
| 623 | return pointer_type_class; |
| 624 | else if (ArgTy->isReferenceType()) |
| 625 | return reference_type_class; |
| 626 | else if (ArgTy->isRealType()) |
| 627 | return real_type_class; |
| 628 | else if (ArgTy->isComplexType()) |
| 629 | return complex_type_class; |
| 630 | else if (ArgTy->isFunctionType()) |
| 631 | return function_type_class; |
| 632 | else if (ArgTy->isStructureType()) |
| 633 | return record_type_class; |
| 634 | else if (ArgTy->isUnionType()) |
| 635 | return union_type_class; |
| 636 | else if (ArgTy->isArrayType()) |
| 637 | return array_type_class; |
| 638 | else if (ArgTy->isUnionType()) |
| 639 | return union_type_class; |
| 640 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 641 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 642 | return -1; |
| 643 | } |
| 644 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 645 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 646 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 647 | default: |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 648 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 649 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 650 | return Success(EvaluateBuiltinClassifyType(E), E); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 651 | |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 652 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 653 | // __builtin_constant_p always has one operand: it returns true if that |
| 654 | // operand can be folded, false otherwise. |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 655 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 657 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 658 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 659 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 660 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | b1112ad | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 661 | if (!Visit(E->getRHS())) |
| 662 | return false; |
Anders Carlsson | 197f6f7 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 663 | |
| 664 | if (!Info.ShortCircuit) { |
| 665 | // If we can't evaluate the LHS, it must be because it has |
| 666 | // side effects. |
| 667 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 668 | Info.EvalResult.HasSideEffects = true; |
| 669 | |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 670 | return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E); |
Anders Carlsson | 197f6f7 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 671 | } |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 672 | |
Anders Carlsson | b1112ad | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 673 | return true; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (E->isLogicalOp()) { |
| 677 | // These need to be handled specially because the operands aren't |
| 678 | // necessarily integral |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 679 | bool lhsResult, rhsResult; |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 680 | |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 681 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 682 | // We were able to evaluate the LHS, see if we can get away with not |
| 683 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 684 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
| 685 | !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 686 | Result = Info.Ctx.MakeIntValue(lhsResult, E->getType()); |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | 6c1a9e2 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 688 | Info.ShortCircuit++; |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 689 | bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info); |
Anders Carlsson | 6c1a9e2 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 690 | Info.ShortCircuit--; |
| 691 | |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 692 | if (rhsEvaluated) |
| 693 | return true; |
| 694 | |
| 695 | // FIXME: Return an extension warning saying that the RHS could not be |
| 696 | // evaluated. |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 697 | return true; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 698 | } |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 700 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 701 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 702 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 703 | else |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 704 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 705 | } |
| 706 | } else { |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 707 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 708 | // We can't evaluate the LHS; however, sometimes the result |
| 709 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 710 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
| 711 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 712 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | 501da1f | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 713 | // must have had side effects. |
| 714 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 715 | |
| 716 | return Success(rhsResult, E); |
Anders Carlsson | 8bce31a | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 719 | } |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 720 | |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 721 | return false; |
| 722 | } |
| 723 | |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 724 | QualType LHSTy = E->getLHS()->getType(); |
| 725 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | f6060d6 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 726 | |
| 727 | if (LHSTy->isAnyComplexType()) { |
| 728 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 729 | APValue LHS, RHS; |
| 730 | |
| 731 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 732 | return false; |
| 733 | |
| 734 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 735 | return false; |
| 736 | |
| 737 | if (LHS.isComplexFloat()) { |
| 738 | APFloat::cmpResult CR_r = |
| 739 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
| 740 | APFloat::cmpResult CR_i = |
| 741 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 742 | |
Daniel Dunbar | f6060d6 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 743 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 744 | return Success((CR_r == APFloat::cmpEqual && |
| 745 | CR_i == APFloat::cmpEqual), E); |
| 746 | else { |
| 747 | assert(E->getOpcode() == BinaryOperator::NE && |
| 748 | "Invalid complex comparison."); |
| 749 | return Success(((CR_r == APFloat::cmpGreaterThan || |
| 750 | CR_r == APFloat::cmpLessThan) && |
| 751 | (CR_i == APFloat::cmpGreaterThan || |
| 752 | CR_i == APFloat::cmpLessThan)), E); |
| 753 | } |
Daniel Dunbar | f6060d6 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 754 | } else { |
Daniel Dunbar | f6060d6 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 755 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 756 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 757 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 758 | else { |
| 759 | assert(E->getOpcode() == BinaryOperator::NE && |
| 760 | "Invalid compex comparison."); |
| 761 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 762 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 763 | } |
Daniel Dunbar | f6060d6 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 766 | |
| 767 | if (LHSTy->isRealFloatingType() && |
| 768 | RHSTy->isRealFloatingType()) { |
| 769 | APFloat RHS(0.0), LHS(0.0); |
| 770 | |
| 771 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 772 | return false; |
| 773 | |
| 774 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 775 | return false; |
| 776 | |
| 777 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 778 | |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 779 | switch (E->getOpcode()) { |
| 780 | default: |
| 781 | assert(0 && "Invalid binary operator!"); |
| 782 | case BinaryOperator::LT: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 783 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 784 | case BinaryOperator::GT: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 785 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 786 | case BinaryOperator::LE: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 787 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 788 | case BinaryOperator::GE: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 789 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
| 790 | E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 791 | case BinaryOperator::EQ: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 792 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 793 | case BinaryOperator::NE: |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 794 | return Success(CR == APFloat::cmpGreaterThan |
| 795 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 796 | } |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 799 | if (E->getOpcode() == BinaryOperator::Sub) { |
Anders Carlsson | 02bb9c3 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 800 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 801 | APValue LHSValue; |
| 802 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 803 | return false; |
| 804 | |
| 805 | APValue RHSValue; |
| 806 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 807 | return false; |
| 808 | |
| 809 | // FIXME: Is this correct? What if only one of the operands has a base? |
| 810 | if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) |
| 811 | return false; |
| 812 | |
| 813 | const QualType Type = E->getLHS()->getType(); |
| 814 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 815 | |
| 816 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
| 817 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
| 818 | |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 819 | return Success(D, E); |
Anders Carlsson | 027f288 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
Anders Carlsson | ebfa6ed | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 822 | if (!LHSTy->isIntegralType() || |
| 823 | !RHSTy->isIntegralType()) { |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 824 | // We can't continue from here for non-integral types, and they |
| 825 | // could potentially confuse the following operations. |
| 826 | // FIXME: Deal with EQ and friends. |
| 827 | return false; |
| 828 | } |
| 829 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 830 | // The LHS of a constant expr is always evaluated and needed. |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 831 | if (!Visit(E->getLHS())) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 832 | return false; // error in subexpression. |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 833 | } |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 835 | llvm::APSInt RHS; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 836 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 837 | return false; |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 838 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 839 | switch (E->getOpcode()) { |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 840 | default: |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 841 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 842 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 843 | case BinaryOperator::Add: Result += RHS; return true; |
| 844 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 845 | case BinaryOperator::And: Result &= RHS; return true; |
| 846 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 847 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 848 | case BinaryOperator::Div: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 849 | if (RHS == 0) |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 850 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 851 | Result /= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 852 | break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 853 | case BinaryOperator::Rem: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 854 | if (RHS == 0) |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 855 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 856 | Result %= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 857 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 858 | case BinaryOperator::Shl: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 859 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 860 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 861 | break; |
| 862 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 863 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 864 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 866 | case BinaryOperator::LT: return Success(Result < RHS, E); |
| 867 | case BinaryOperator::GT: return Success(Result > RHS, E); |
| 868 | case BinaryOperator::LE: return Success(Result <= RHS, E); |
| 869 | case BinaryOperator::GE: return Success(Result >= RHS, E); |
| 870 | case BinaryOperator::EQ: return Success(Result == RHS, E); |
| 871 | case BinaryOperator::NE: return Success(Result != RHS, E); |
Eli Friedman | b2935ab | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 872 | } |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 873 | |
| 874 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 875 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 878 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 308de75 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 879 | bool Cond; |
| 880 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 881 | return false; |
| 882 | |
Nuno Lopes | 308de75 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 883 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | eb35c0e | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 886 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 887 | const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr(); |
| 888 | |
| 889 | // __alignof__(void) = 1 as a gcc extension. |
| 890 | if (Ty->isVoidType()) |
| 891 | return 1; |
| 892 | |
| 893 | // GCC extension: alignof(function) = 4. |
| 894 | // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the |
| 895 | // attribute(align) directive. |
| 896 | if (Ty->isFunctionType()) |
| 897 | return 4; |
| 898 | |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 899 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) |
| 900 | return GetAlignOfType(QualType(EXTQT->getBaseType(), 0)); |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 901 | |
| 902 | // alignof VLA/incomplete array. |
| 903 | if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty)) |
| 904 | return GetAlignOfType(VAT->getElementType()); |
| 905 | |
| 906 | // sizeof (objc class)? |
| 907 | if (isa<ObjCInterfaceType>(Ty)) |
| 908 | return 1; // FIXME: This probably isn't right. |
| 909 | |
| 910 | // Get information about the alignment. |
| 911 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
| 912 | return Info.Ctx.getTypeAlign(Ty) / CharSize; |
| 913 | } |
| 914 | |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 915 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 916 | E = E->IgnoreParens(); |
| 917 | |
| 918 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 919 | // to 1 in those cases. |
| 920 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | 96d1f1b | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 921 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 922 | |
| 923 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | 96d1f1b | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 924 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 925 | |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 926 | return GetAlignOfType(E->getType()); |
| 927 | } |
| 928 | |
| 929 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 930 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 931 | /// expression's type. |
| 932 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 933 | QualType DstTy = E->getType(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 934 | |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 935 | // Handle alignof separately. |
| 936 | if (!E->isSizeOf()) { |
| 937 | if (E->isArgumentType()) |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 938 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 939 | else |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 940 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 943 | QualType SrcTy = E->getTypeOfArgument(); |
| 944 | |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 945 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 946 | // extension. |
| 947 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 948 | return Success(1, E); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 949 | |
| 950 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 951 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 952 | return false; |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 953 | |
| 954 | if (SrcTy->isObjCInterfaceType()) { |
| 955 | // Slightly unusual case: the size of an ObjC interface type is the |
| 956 | // size of the class. This code intentionally falls through to the normal |
| 957 | // case. |
| 958 | ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl(); |
| 959 | RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI)); |
| 960 | SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD)); |
| 961 | } |
| 962 | |
Chris Lattner | e3f61e1 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 963 | // Get information about the size. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 964 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 965 | return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 968 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 969 | // Special case unary operators that do not need their subexpression |
| 970 | // evaluated. offsetof/sizeof/alignof are all special. |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 971 | if (E->isOffsetOfOp()) |
| 972 | return Success(E->evaluateOffsetOf(Info.Ctx), E); |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 973 | |
| 974 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 975 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 976 | bool bres; |
| 977 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 978 | return false; |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 979 | return Success(!bres, E); |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 982 | // Get the operand value into 'Result'. |
| 983 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 984 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 986 | switch (E->getOpcode()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 987 | default: |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 988 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 989 | // See C99 6.6p3. |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 990 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 991 | case UnaryOperator::Extension: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 992 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 993 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 994 | break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 995 | case UnaryOperator::Plus: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 996 | // The result is always just the subexpr. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 997 | break; |
| 998 | case UnaryOperator::Minus: |
| 999 | Result = -Result; |
| 1000 | break; |
| 1001 | case UnaryOperator::Not: |
| 1002 | Result = ~Result; |
| 1003 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1007 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1010 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1011 | /// result type is integer. |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1012 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | fa76d82 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1013 | Expr *SubExpr = E->getSubExpr(); |
| 1014 | QualType DestType = E->getType(); |
| 1015 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1016 | if (DestType->isBooleanType()) { |
| 1017 | bool BoolResult; |
| 1018 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1019 | return false; |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 1020 | return Success(BoolResult, E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1023 | // Handle simple integer->integer casts. |
Eli Friedman | 14cc754 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1024 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1025 | if (!Visit(SubExpr)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1026 | return false; |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1027 | |
| 1028 | Result = HandleIntToIntCast(DestType, SubExpr->getType(), Result, Info.Ctx); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | // FIXME: Clean this up! |
| 1033 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1034 | APValue LV; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1035 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1036 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1037 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1038 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1039 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | f91896e | 2009-02-19 09:06:44 +0000 | [diff] [blame^] | 1041 | return Success(LV.getLValueOffset(), E); |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1042 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1044 | if (!SubExpr->getType()->isRealFloatingType()) |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1045 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1046 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1047 | APFloat F(0.0); |
| 1048 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | 38bb18c | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1049 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1050 | |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1051 | Result = HandleFloatToIntCast(DestType, SubExpr->getType(), F, Info.Ctx); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1052 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1053 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1055 | //===----------------------------------------------------------------------===// |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1056 | // Float Evaluation |
| 1057 | //===----------------------------------------------------------------------===// |
| 1058 | |
| 1059 | namespace { |
| 1060 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1061 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1062 | EvalInfo &Info; |
| 1063 | APFloat &Result; |
| 1064 | public: |
| 1065 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1066 | : Info(info), Result(result) {} |
| 1067 | |
| 1068 | bool VisitStmt(Stmt *S) { |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
| 1072 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1073 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1074 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1075 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1076 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1077 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1078 | bool VisitCastExpr(CastExpr *E); |
| 1079 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1080 | }; |
| 1081 | } // end anonymous namespace |
| 1082 | |
| 1083 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1084 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1085 | } |
| 1086 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1087 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1088 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1089 | default: return false; |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1090 | case Builtin::BI__builtin_huge_val: |
| 1091 | case Builtin::BI__builtin_huge_valf: |
| 1092 | case Builtin::BI__builtin_huge_vall: |
| 1093 | case Builtin::BI__builtin_inf: |
| 1094 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1095 | case Builtin::BI__builtin_infl: { |
| 1096 | const llvm::fltSemantics &Sem = |
| 1097 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1098 | Result = llvm::APFloat::getInf(Sem); |
| 1099 | return true; |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1100 | } |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1101 | |
| 1102 | case Builtin::BI__builtin_nan: |
| 1103 | case Builtin::BI__builtin_nanf: |
| 1104 | case Builtin::BI__builtin_nanl: |
| 1105 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 1106 | // can't constant fold it. |
| 1107 | if (const StringLiteral *S = |
| 1108 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 1109 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1110 | const llvm::fltSemantics &Sem = |
| 1111 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1112 | Result = llvm::APFloat::getNaN(Sem); |
| 1113 | return true; |
| 1114 | } |
| 1115 | } |
| 1116 | return false; |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1117 | |
| 1118 | case Builtin::BI__builtin_fabs: |
| 1119 | case Builtin::BI__builtin_fabsf: |
| 1120 | case Builtin::BI__builtin_fabsl: |
| 1121 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1122 | return false; |
| 1123 | |
| 1124 | if (Result.isNegative()) |
| 1125 | Result.changeSign(); |
| 1126 | return true; |
| 1127 | |
| 1128 | case Builtin::BI__builtin_copysign: |
| 1129 | case Builtin::BI__builtin_copysignf: |
| 1130 | case Builtin::BI__builtin_copysignl: { |
| 1131 | APFloat RHS(0.); |
| 1132 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1133 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1134 | return false; |
| 1135 | Result.copySign(RHS); |
| 1136 | return true; |
| 1137 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1141 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | 1cea4f4 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1142 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1143 | return false; |
| 1144 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1145 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1146 | return false; |
| 1147 | |
| 1148 | switch (E->getOpcode()) { |
| 1149 | default: return false; |
| 1150 | case UnaryOperator::Plus: |
| 1151 | return true; |
| 1152 | case UnaryOperator::Minus: |
| 1153 | Result.changeSign(); |
| 1154 | return true; |
| 1155 | } |
| 1156 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1157 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1158 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1159 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1160 | // and errors are supposed to work. |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1161 | APFloat RHS(0.0); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1162 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1163 | return false; |
| 1164 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1165 | return false; |
| 1166 | |
| 1167 | switch (E->getOpcode()) { |
| 1168 | default: return false; |
| 1169 | case BinaryOperator::Mul: |
| 1170 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1171 | return true; |
| 1172 | case BinaryOperator::Add: |
| 1173 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1174 | return true; |
| 1175 | case BinaryOperator::Sub: |
| 1176 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1177 | return true; |
| 1178 | case BinaryOperator::Div: |
| 1179 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1180 | return true; |
| 1181 | case BinaryOperator::Rem: |
| 1182 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 1183 | return true; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1188 | Result = E->getValue(); |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1192 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1193 | Expr* SubExpr = E->getSubExpr(); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1194 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1195 | if (SubExpr->getType()->isIntegralType()) { |
| 1196 | APSInt IntResult; |
| 1197 | if (!EvaluateInteger(E, IntResult, Info)) |
| 1198 | return false; |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1199 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
| 1200 | IntResult, Info.Ctx); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1201 | return true; |
| 1202 | } |
| 1203 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1204 | if (!Visit(SubExpr)) |
| 1205 | return false; |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1206 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1207 | Result, Info.Ctx); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1215 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1216 | return true; |
| 1217 | } |
| 1218 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1219 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1220 | // Complex Evaluation (for float and integer) |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1221 | //===----------------------------------------------------------------------===// |
| 1222 | |
| 1223 | namespace { |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1224 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1225 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1226 | EvalInfo &Info; |
| 1227 | |
| 1228 | public: |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1229 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1230 | |
| 1231 | //===--------------------------------------------------------------------===// |
| 1232 | // Visitor Methods |
| 1233 | //===--------------------------------------------------------------------===// |
| 1234 | |
| 1235 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1236 | return APValue(); |
| 1237 | } |
| 1238 | |
| 1239 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1240 | |
| 1241 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1242 | Expr* SubExpr = E->getSubExpr(); |
| 1243 | |
| 1244 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1245 | APFloat Result(0.0); |
| 1246 | |
| 1247 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1248 | return APValue(); |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1249 | |
Daniel Dunbar | 00a9aad | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1250 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1251 | Result); |
| 1252 | } else { |
| 1253 | assert(SubExpr->getType()->isIntegerType() && |
| 1254 | "Unexpected imaginary literal."); |
| 1255 | |
| 1256 | llvm::APSInt Result; |
| 1257 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1258 | return APValue(); |
| 1259 | |
| 1260 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1261 | Zero = 0; |
| 1262 | return APValue(Zero, Result); |
| 1263 | } |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1266 | APValue VisitCastExpr(CastExpr *E) { |
| 1267 | Expr* SubExpr = E->getSubExpr(); |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1268 | QualType EltType = E->getType()->getAsComplexType()->getElementType(); |
| 1269 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1270 | |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1271 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1272 | APFloat Result(0.0); |
| 1273 | |
| 1274 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1275 | return APValue(); |
| 1276 | |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1277 | // Apply float conversion if necessary. |
| 1278 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | f8abb94 | 2009-01-24 19:08:01 +0000 | [diff] [blame] | 1279 | return APValue(Result, |
Daniel Dunbar | 00a9aad | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1280 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1281 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1282 | APSInt Result; |
| 1283 | |
| 1284 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1285 | return APValue(); |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Apply integer conversion if necessary. |
| 1288 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1289 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1290 | Zero = 0; |
| 1291 | return APValue(Result, Zero); |
Daniel Dunbar | affa0e3 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1292 | } else if (const ComplexType *CT = SubType->getAsComplexType()) { |
| 1293 | APValue Src; |
| 1294 | |
| 1295 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1296 | return APValue(); |
| 1297 | |
| 1298 | QualType SrcType = CT->getElementType(); |
| 1299 | |
| 1300 | if (Src.isComplexFloat()) { |
| 1301 | if (EltType->isRealFloatingType()) { |
| 1302 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
| 1303 | Src.getComplexFloatReal(), |
| 1304 | Info.Ctx), |
| 1305 | HandleFloatToFloatCast(EltType, SrcType, |
| 1306 | Src.getComplexFloatImag(), |
| 1307 | Info.Ctx)); |
| 1308 | } else { |
| 1309 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1310 | Src.getComplexFloatReal(), |
| 1311 | Info.Ctx), |
| 1312 | HandleFloatToIntCast(EltType, SrcType, |
| 1313 | Src.getComplexFloatImag(), |
| 1314 | Info.Ctx)); |
| 1315 | } |
| 1316 | } else { |
| 1317 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1318 | if (EltType->isRealFloatingType()) { |
| 1319 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
| 1320 | Src.getComplexIntReal(), |
| 1321 | Info.Ctx), |
| 1322 | HandleIntToFloatCast(EltType, SrcType, |
| 1323 | Src.getComplexIntImag(), |
| 1324 | Info.Ctx)); |
| 1325 | } else { |
| 1326 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1327 | Src.getComplexIntReal(), |
| 1328 | Info.Ctx), |
| 1329 | HandleIntToIntCast(EltType, SrcType, |
| 1330 | Src.getComplexIntImag(), |
| 1331 | Info.Ctx)); |
| 1332 | } |
| 1333 | } |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | // FIXME: Handle more casts. |
| 1337 | return APValue(); |
| 1338 | } |
| 1339 | |
| 1340 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 1341 | |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1342 | }; |
| 1343 | } // end anonymous namespace |
| 1344 | |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1345 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1346 | { |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1347 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1348 | assert((!Result.isComplexFloat() || |
| 1349 | (&Result.getComplexFloatReal().getSemantics() == |
| 1350 | &Result.getComplexFloatImag().getSemantics())) && |
| 1351 | "Invalid complex evaluation."); |
| 1352 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1355 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1356 | { |
| 1357 | APValue Result, RHS; |
| 1358 | |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1359 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1360 | return APValue(); |
| 1361 | |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1362 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1363 | return APValue(); |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1364 | |
Daniel Dunbar | 00a9aad | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1365 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1366 | "Invalid operands to binary operator."); |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1367 | switch (E->getOpcode()) { |
| 1368 | default: return APValue(); |
| 1369 | case BinaryOperator::Add: |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1370 | if (Result.isComplexFloat()) { |
| 1371 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1372 | APFloat::rmNearestTiesToEven); |
| 1373 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1374 | APFloat::rmNearestTiesToEven); |
| 1375 | } else { |
| 1376 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1377 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1378 | } |
Daniel Dunbar | 00a9aad | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1379 | break; |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1380 | case BinaryOperator::Sub: |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1381 | if (Result.isComplexFloat()) { |
| 1382 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1383 | APFloat::rmNearestTiesToEven); |
| 1384 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1385 | APFloat::rmNearestTiesToEven); |
| 1386 | } else { |
| 1387 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1388 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1389 | } |
Daniel Dunbar | 00a9aad | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | case BinaryOperator::Mul: |
| 1392 | if (Result.isComplexFloat()) { |
| 1393 | APValue LHS = Result; |
| 1394 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1395 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1396 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1397 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 1398 | |
| 1399 | APFloat Tmp = LHS_r; |
| 1400 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1401 | Result.getComplexFloatReal() = Tmp; |
| 1402 | Tmp = LHS_i; |
| 1403 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1404 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1405 | |
| 1406 | Tmp = LHS_r; |
| 1407 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1408 | Result.getComplexFloatImag() = Tmp; |
| 1409 | Tmp = LHS_i; |
| 1410 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1411 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1412 | } else { |
| 1413 | APValue LHS = Result; |
| 1414 | Result.getComplexIntReal() = |
| 1415 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1416 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
| 1417 | Result.getComplexIntImag() = |
| 1418 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1419 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1420 | } |
| 1421 | break; |
Anders Carlsson | ad2794c | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | return Result; |
| 1425 | } |
| 1426 | |
Anders Carlsson | f1bb296 | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1427 | //===----------------------------------------------------------------------===// |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1428 | // Top level Expr::Evaluate method. |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1429 | //===----------------------------------------------------------------------===// |
| 1430 | |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1431 | /// 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] | 1432 | /// any crazy technique (that has nothing to do with language standards) that |
| 1433 | /// we want to. If this function returns true, it returns the folded constant |
| 1434 | /// in Result. |
Anders Carlsson | 7f5a96e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1435 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1436 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | dd8d41f | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1437 | |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1438 | if (getType()->isVectorType()) { |
| 1439 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1440 | return false; |
| 1441 | } else if (getType()->isIntegerType()) { |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1442 | llvm::APSInt sInt(32); |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1443 | if (!EvaluateInteger(this, sInt, Info)) |
| 1444 | return false; |
| 1445 | |
Anders Carlsson | 7f5a96e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1446 | Result.Val = APValue(sInt); |
Mike Stump | 5b601ff | 2009-02-18 21:44:49 +0000 | [diff] [blame] | 1447 | } else if (getType()->isPointerType() |
| 1448 | || getType()->isBlockPointerType()) { |
Anders Carlsson | 7f5a96e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1449 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1450 | return false; |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1451 | } else if (getType()->isRealFloatingType()) { |
| 1452 | llvm::APFloat f(0.0); |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1453 | if (!EvaluateFloat(this, f, Info)) |
| 1454 | return false; |
| 1455 | |
Anders Carlsson | 7f5a96e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1456 | Result.Val = APValue(f); |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1457 | } else if (getType()->isAnyComplexType()) { |
| 1458 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1459 | return false; |
Daniel Dunbar | af781bb | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1460 | } else |
Anders Carlsson | cb6a2e8 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1461 | return false; |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 7f5a96e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1463 | return true; |
| 1464 | } |
| 1465 | |
Chris Lattner | ef06966 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1466 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1467 | /// folded, but discard the result. |
| 1468 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 197f6f7 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1469 | EvalResult Result; |
| 1470 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1471 | } |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1472 | |
| 1473 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 8c3de80 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1474 | EvalResult EvalResult; |
| 1475 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | ddebeca | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1476 | Result = Result; |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1477 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 8c3de80 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1478 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1479 | |
Anders Carlsson | 8c3de80 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1480 | return EvalResult.Val.getInt(); |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1481 | } |