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