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