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