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