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