Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 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" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTDiagnostic.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 23 | #include <cstring> |
| 24 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 27 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 28 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 29 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 30 | /// information about a subexpression as it is folded. It retains information |
| 31 | /// about the AST context, but also maintains information about the folded |
| 32 | /// expression. |
| 33 | /// |
| 34 | /// If an expression could be evaluated, it is still possible it is not a C |
| 35 | /// "integer constant expression" or constant expression. If not, this struct |
| 36 | /// captures information about how and why not. |
| 37 | /// |
| 38 | /// One bit of information passed *into* the request for constant folding |
| 39 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 40 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 41 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 42 | /// certain things in certain situations. |
| 43 | struct EvalInfo { |
| 44 | ASTContext &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 46 | /// EvalResult - Contains information about the evaluation. |
| 47 | Expr::EvalResult &EvalResult; |
Anders Carlsson | 5862001 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 48 | |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 49 | /// AnyLValue - Stack based LValue results are not discarded. |
| 50 | bool AnyLValue; |
| 51 | |
| 52 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult, |
| 53 | bool anylvalue = false) |
| 54 | : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {} |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 58 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 59 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 60 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 61 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| 62 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 63 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 64 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 65 | |
| 66 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 67 | // Misc utilities |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 70 | static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { |
| 71 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 72 | // the check look like? |
| 73 | Result = Value.getLValueBase() || Value.getLValueOffset(); |
| 74 | return true; |
| 75 | } |
| 76 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 77 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 78 | if (E->getType()->isIntegralType()) { |
| 79 | APSInt IntResult; |
| 80 | if (!EvaluateInteger(E, IntResult, Info)) |
| 81 | return false; |
| 82 | Result = IntResult != 0; |
| 83 | return true; |
| 84 | } else if (E->getType()->isRealFloatingType()) { |
| 85 | APFloat FloatResult(0.0); |
| 86 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 87 | return false; |
| 88 | Result = !FloatResult.isZero(); |
| 89 | return true; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 90 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 91 | APValue PointerResult; |
| 92 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 93 | return false; |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 94 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 95 | } else if (E->getType()->isAnyComplexType()) { |
| 96 | APValue ComplexResult; |
| 97 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 98 | return false; |
| 99 | if (ComplexResult.isComplexFloat()) { |
| 100 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 101 | !ComplexResult.getComplexFloatImag().isZero(); |
| 102 | } else { |
| 103 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 104 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 105 | } |
| 106 | return true; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 113 | APFloat &Value, ASTContext &Ctx) { |
| 114 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 115 | // Determine whether we are converting to unsigned or signed. |
| 116 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 118 | // FIXME: Warning for overflow. |
| 119 | uint64_t Space[4]; |
| 120 | bool ignored; |
| 121 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 122 | llvm::APFloat::rmTowardZero, &ignored); |
| 123 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 124 | } |
| 125 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 127 | APFloat &Value, ASTContext &Ctx) { |
| 128 | bool ignored; |
| 129 | APFloat Result = Value; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 131 | APFloat::rmNearestTiesToEven, &ignored); |
| 132 | return Result; |
| 133 | } |
| 134 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 136 | APSInt &Value, ASTContext &Ctx) { |
| 137 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 138 | APSInt Result = Value; |
| 139 | // Figure out if this is a truncate, extend or noop cast. |
| 140 | // If the input is signed, do a sign extend, noop, or truncate. |
| 141 | Result.extOrTrunc(DestWidth); |
| 142 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 143 | return Result; |
| 144 | } |
| 145 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 147 | APSInt &Value, ASTContext &Ctx) { |
| 148 | |
| 149 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 150 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 151 | APFloat::rmNearestTiesToEven); |
| 152 | return Result; |
| 153 | } |
| 154 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 155 | namespace { |
| 156 | class VISIBILITY_HIDDEN HasSideEffect |
| 157 | : public StmtVisitor<HasSideEffect, bool> { |
| 158 | EvalInfo &Info; |
| 159 | public: |
| 160 | |
| 161 | HasSideEffect(EvalInfo &info) : Info(info) {} |
| 162 | |
| 163 | // Unhandled nodes conservatively default to having side effects. |
| 164 | bool VisitStmt(Stmt *S) { |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 169 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 170 | if (E->getType().isVolatileQualified()) |
| 171 | return true; |
| 172 | return false; |
| 173 | } |
| 174 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 175 | // a ton of code. |
| 176 | bool VisitBlockExpr(BlockExpr *E) { return true; } |
| 177 | bool VisitPredefinedExpr(PredefinedExpr *E) { return false; } |
| 178 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 179 | { return Visit(E->getInitializer()); } |
| 180 | bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); } |
| 181 | bool VisitIntegerLiteral(IntegerLiteral *E) { return false; } |
| 182 | bool VisitFloatingLiteral(FloatingLiteral *E) { return false; } |
| 183 | bool VisitStringLiteral(StringLiteral *E) { return false; } |
| 184 | bool VisitCharacterLiteral(CharacterLiteral *E) { return false; } |
| 185 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; } |
| 186 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 187 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 188 | bool VisitChooseExpr(ChooseExpr *E) |
| 189 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 190 | bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); } |
| 191 | bool VisitBinAssign(BinaryOperator *E) { return true; } |
Mike Stump | f3eb5ec | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 192 | bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; } |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 193 | bool VisitBinaryOperator(BinaryOperator *E) |
| 194 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 195 | bool VisitUnaryPreInc(UnaryOperator *E) { return true; } |
| 196 | bool VisitUnaryPostInc(UnaryOperator *E) { return true; } |
| 197 | bool VisitUnaryPreDec(UnaryOperator *E) { return true; } |
| 198 | bool VisitUnaryPostDec(UnaryOperator *E) { return true; } |
| 199 | bool VisitUnaryDeref(UnaryOperator *E) { |
| 200 | if (E->getType().isVolatileQualified()) |
| 201 | return true; |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 202 | return Visit(E->getSubExpr()); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 203 | } |
| 204 | bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); } |
| 205 | }; |
| 206 | |
| 207 | bool HasSideEffects(const Expr* E, ASTContext &Ctx) { |
| 208 | Expr::EvalResult Result; |
| 209 | EvalInfo Info(Ctx, Result); |
| 210 | |
| 211 | return HasSideEffect(Info).Visit(const_cast<Expr*>(E)); |
| 212 | } |
| 213 | |
| 214 | } // end anonymous namespace |
| 215 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 216 | //===----------------------------------------------------------------------===// |
| 217 | // LValue Evaluation |
| 218 | //===----------------------------------------------------------------------===// |
| 219 | namespace { |
| 220 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 221 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 222 | EvalInfo &Info; |
| 223 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 225 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 226 | |
| 227 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 228 | return APValue(); |
| 229 | } |
| 230 | |
| 231 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 232 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 233 | APValue VisitBlockExpr(BlockExpr *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 234 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 235 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 236 | APValue VisitMemberExpr(MemberExpr *E); |
| 237 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 238 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 239 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 240 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 241 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 242 | { return Visit(E->getSubExpr()); } |
| 243 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 244 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 245 | |
| 246 | APValue VisitCastExpr(CastExpr *E) { |
| 247 | switch (E->getCastKind()) { |
| 248 | default: |
| 249 | return APValue(); |
| 250 | |
| 251 | case CastExpr::CK_NoOp: |
| 252 | return Visit(E->getSubExpr()); |
| 253 | } |
| 254 | } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 255 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 256 | }; |
| 257 | } // end anonymous namespace |
| 258 | |
| 259 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 260 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 261 | return Result.isLValue(); |
| 262 | } |
| 263 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 265 | if (isa<FunctionDecl>(E->getDecl())) { |
| 266 | return APValue(E, 0); |
| 267 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 268 | if (!Info.AnyLValue && !VD->hasGlobalStorage()) |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 269 | return APValue(); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 270 | if (!VD->getType()->isReferenceType()) |
| 271 | return APValue(E, 0); |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 272 | // FIXME: Check whether VD might be overridden! |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 273 | if (VD->getInit()) |
| 274 | return Visit(VD->getInit()); |
| 275 | } |
| 276 | |
| 277 | return APValue(); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) { |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 281 | if (E->hasBlockDeclRefExprs()) |
| 282 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 284 | return APValue(E, 0); |
| 285 | } |
| 286 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 287 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 288 | if (!Info.AnyLValue && !E->isFileScope()) |
| 289 | return APValue(); |
| 290 | return APValue(E, 0); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 294 | APValue result; |
| 295 | QualType Ty; |
| 296 | if (E->isArrow()) { |
| 297 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 298 | return APValue(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 299 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 300 | } else { |
| 301 | result = Visit(E->getBase()); |
| 302 | if (result.isUninit()) |
| 303 | return APValue(); |
| 304 | Ty = E->getBase()->getType(); |
| 305 | } |
| 306 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 307 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 308 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 309 | |
| 310 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 311 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 312 | return APValue(); |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 313 | |
| 314 | if (FD->getType()->isReferenceType()) |
| 315 | return APValue(); |
| 316 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 317 | // FIXME: This is linear time. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 318 | unsigned i = 0; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 319 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 320 | FieldEnd = RD->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 321 | Field != FieldEnd; (void)++Field, ++i) { |
| 322 | if (*Field == FD) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | |
| 326 | result.setLValue(result.getLValueBase(), |
| 327 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 328 | |
| 329 | return result; |
| 330 | } |
| 331 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 333 | APValue Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 335 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 336 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 338 | APSInt Index; |
| 339 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 340 | return APValue(); |
| 341 | |
| 342 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 343 | |
| 344 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | Result.setLValue(Result.getLValueBase(), |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 346 | Result.getLValueOffset() + Offset); |
| 347 | return Result; |
| 348 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 349 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 351 | APValue Result; |
| 352 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 353 | return APValue(); |
| 354 | return Result; |
| 355 | } |
| 356 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 358 | // Pointer Evaluation |
| 359 | //===----------------------------------------------------------------------===// |
| 360 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 361 | namespace { |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 362 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 363 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 364 | EvalInfo &Info; |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 365 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 367 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 369 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 370 | return APValue(); |
| 371 | } |
| 372 | |
| 373 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 374 | |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 375 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 376 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 377 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 378 | { return Visit(E->getSubExpr()); } |
| 379 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 380 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 381 | { return APValue(E, 0); } |
Eli Friedman | 529a99b | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 382 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 383 | { return APValue(E, 0); } |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 384 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 385 | APValue VisitBlockExpr(BlockExpr *E) { |
| 386 | if (!E->hasBlockDeclRefExprs()) |
| 387 | return APValue(E, 0); |
| 388 | return APValue(); |
| 389 | } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 390 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
| 391 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 392 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 393 | APValue VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 394 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 395 | APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
| 396 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 397 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 398 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 399 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 400 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 401 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Daniel Dunbar | 76ba41c | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 402 | if (!E->getType()->hasPointerRepresentation()) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 403 | return false; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 404 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 405 | return Result.isLValue(); |
| 406 | } |
| 407 | |
| 408 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 409 | if (E->getOpcode() != BinaryOperator::Add && |
| 410 | E->getOpcode() != BinaryOperator::Sub) |
| 411 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 413 | const Expr *PExp = E->getLHS(); |
| 414 | const Expr *IExp = E->getRHS(); |
| 415 | if (IExp->getType()->isPointerType()) |
| 416 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 418 | APValue ResultLValue; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 419 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 420 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 422 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 423 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 424 | return APValue(); |
| 425 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 426 | QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 427 | uint64_t SizeOfPointee; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 429 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 430 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 431 | SizeOfPointee = 1; |
| 432 | else |
| 433 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 435 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 437 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 438 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 439 | else |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 440 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 441 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 442 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 443 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 444 | |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 445 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 446 | APValue result; |
| 447 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 448 | return result; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 449 | return APValue(); |
| 450 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 452 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 453 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 454 | const Expr* SubExpr = E->getSubExpr(); |
| 455 | |
| 456 | // Check for pointer->pointer cast |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 457 | if (SubExpr->getType()->isPointerType() || |
Anders Carlsson | 04c3bf4 | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 458 | SubExpr->getType()->isObjCObjectPointerType() || |
| 459 | SubExpr->getType()->isNullPtrType()) { |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 460 | APValue Result; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 461 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 462 | return Result; |
| 463 | return APValue(); |
| 464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 466 | if (SubExpr->getType()->isIntegralType()) { |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 467 | APValue Result; |
| 468 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 469 | return APValue(); |
| 470 | |
| 471 | if (Result.isInt()) { |
| 472 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
| 473 | return APValue(0, Result.getInt().getZExtValue()); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 474 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 476 | // Cast is of an lvalue, no need to change value. |
| 477 | return Result; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 478 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 479 | |
| 480 | if (SubExpr->getType()->isFunctionType() || |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 481 | SubExpr->getType()->isBlockPointerType() || |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 482 | SubExpr->getType()->isArrayType()) { |
| 483 | APValue Result; |
| 484 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 485 | return Result; |
| 486 | return APValue(); |
| 487 | } |
| 488 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 489 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 491 | |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 492 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | if (E->isBuiltinCall(Info.Ctx) == |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 494 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 495 | return APValue(E, 0); |
| 496 | return APValue(); |
| 497 | } |
| 498 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 499 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 500 | bool BoolResult; |
| 501 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 502 | return APValue(); |
| 503 | |
| 504 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 505 | |
| 506 | APValue Result; |
| 507 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 508 | return Result; |
| 509 | return APValue(); |
| 510 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 511 | |
| 512 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 513 | // Vector Evaluation |
| 514 | //===----------------------------------------------------------------------===// |
| 515 | |
| 516 | namespace { |
| 517 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 518 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 519 | EvalInfo &Info; |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 520 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 521 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 523 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 525 | APValue VisitStmt(Stmt *S) { |
| 526 | return APValue(); |
| 527 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 529 | APValue VisitParenExpr(ParenExpr *E) |
| 530 | { return Visit(E->getSubExpr()); } |
| 531 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 532 | { return Visit(E->getSubExpr()); } |
| 533 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 534 | { return Visit(E->getSubExpr()); } |
| 535 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 536 | { return Visit(E->getSubExpr()); } |
| 537 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 538 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 539 | APValue VisitCastExpr(const CastExpr* E); |
| 540 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 541 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 542 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 543 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 544 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 545 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 546 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 547 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 548 | // shufflevector, ExtVectorElementExpr |
| 549 | // (Note that these require implementing conversions |
| 550 | // between vector types.) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 551 | }; |
| 552 | } // end anonymous namespace |
| 553 | |
| 554 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 555 | if (!E->getType()->isVectorType()) |
| 556 | return false; |
| 557 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 558 | return !Result.isUninit(); |
| 559 | } |
| 560 | |
| 561 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 562 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 563 | QualType EltTy = VTy->getElementType(); |
| 564 | unsigned NElts = VTy->getNumElements(); |
| 565 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 567 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 568 | QualType SETy = SE->getType(); |
| 569 | APValue Result = APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 570 | |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 571 | // Check for vector->vector bitcast and scalar->vector splat. |
| 572 | if (SETy->isVectorType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 573 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 574 | } else if (SETy->isIntegerType()) { |
| 575 | APSInt IntResult; |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 576 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 577 | return APValue(); |
| 578 | Result = APValue(IntResult); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 579 | } else if (SETy->isRealFloatingType()) { |
| 580 | APFloat F(0.0); |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 581 | if (!EvaluateFloat(SE, F, Info)) |
| 582 | return APValue(); |
| 583 | Result = APValue(F); |
| 584 | } else |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 585 | return APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 586 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 587 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 588 | // and splat it to all elements. |
| 589 | if (E->getType()->isExtVectorType()) { |
| 590 | if (EltTy->isIntegerType() && Result.isInt()) |
| 591 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 592 | Info.Ctx)); |
| 593 | else if (EltTy->isIntegerType()) |
| 594 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 595 | Info.Ctx)); |
| 596 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 597 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 598 | Info.Ctx)); |
| 599 | else if (EltTy->isRealFloatingType()) |
| 600 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 601 | Info.Ctx)); |
| 602 | else |
| 603 | return APValue(); |
| 604 | |
| 605 | // Splat and create vector APValue. |
| 606 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 607 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 608 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 609 | |
| 610 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 611 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 612 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 613 | // element. |
| 614 | APSInt Init; |
| 615 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 617 | llvm::SmallVector<APValue, 4> Elts; |
| 618 | for (unsigned i = 0; i != NElts; ++i) { |
| 619 | APSInt Tmp = Init; |
| 620 | Tmp.extOrTrunc(EltWidth); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 622 | if (EltTy->isIntegerType()) |
| 623 | Elts.push_back(APValue(Tmp)); |
| 624 | else if (EltTy->isRealFloatingType()) |
| 625 | Elts.push_back(APValue(APFloat(Tmp))); |
| 626 | else |
| 627 | return APValue(); |
| 628 | |
| 629 | Init >>= EltWidth; |
| 630 | } |
| 631 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 635 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 636 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 637 | } |
| 638 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 640 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 641 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 642 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 643 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 645 | QualType EltTy = VT->getElementType(); |
| 646 | llvm::SmallVector<APValue, 4> Elements; |
| 647 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 648 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 649 | if (EltTy->isIntegerType()) { |
| 650 | llvm::APSInt sInt(32); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 651 | if (i < NumInits) { |
| 652 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 653 | return APValue(); |
| 654 | } else { |
| 655 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 656 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 657 | Elements.push_back(APValue(sInt)); |
| 658 | } else { |
| 659 | llvm::APFloat f(0.0); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 660 | if (i < NumInits) { |
| 661 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 662 | return APValue(); |
| 663 | } else { |
| 664 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 665 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 666 | Elements.push_back(APValue(f)); |
| 667 | } |
| 668 | } |
| 669 | return APValue(&Elements[0], Elements.size()); |
| 670 | } |
| 671 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | APValue |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 673 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 674 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 675 | QualType EltTy = VT->getElementType(); |
| 676 | APValue ZeroElement; |
| 677 | if (EltTy->isIntegerType()) |
| 678 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 679 | else |
| 680 | ZeroElement = |
| 681 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 682 | |
| 683 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 684 | return APValue(&Elements[0], Elements.size()); |
| 685 | } |
| 686 | |
| 687 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 688 | bool BoolResult; |
| 689 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 690 | return APValue(); |
| 691 | |
| 692 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 693 | |
| 694 | APValue Result; |
| 695 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 696 | return Result; |
| 697 | return APValue(); |
| 698 | } |
| 699 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 700 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 701 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 702 | Info.EvalResult.HasSideEffects = true; |
| 703 | return GetZeroVector(E->getType()); |
| 704 | } |
| 705 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 706 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 707 | // Integer Evaluation |
| 708 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 709 | |
| 710 | namespace { |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 711 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 712 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 713 | EvalInfo &Info; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 714 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 715 | public: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 716 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 717 | : Info(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 718 | |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 719 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 720 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 721 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 722 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 723 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 724 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 725 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 729 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 730 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 731 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 732 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 733 | Result = APValue(APSInt(I)); |
| 734 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | |
| 738 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 739 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 740 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 741 | return true; |
| 742 | } |
| 743 | |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 744 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 745 | // Take the first error. |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 746 | if (Info.EvalResult.Diag == 0) { |
| 747 | Info.EvalResult.DiagLoc = L; |
| 748 | Info.EvalResult.Diag = D; |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 749 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 750 | } |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 751 | return false; |
Chris Lattner | ae8cc15 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 754 | //===--------------------------------------------------------------------===// |
| 755 | // Visitor Methods |
| 756 | //===--------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 758 | bool VisitStmt(Stmt *) { |
| 759 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 760 | return false; |
| 761 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 763 | bool VisitExpr(Expr *E) { |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 764 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 765 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 767 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 768 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 769 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 770 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 771 | } |
| 772 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 773 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 774 | } |
| 775 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | d7be95d | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 776 | // Per gcc docs "this built-in function ignores top level |
| 777 | // qualifiers". We need to use the canonical version to properly |
| 778 | // be able to strip CRV qualifiers from the type. |
| 779 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 780 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 782 | T1.getUnqualifiedType()), |
| 783 | E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 784 | } |
| 785 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 786 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 787 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 788 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 789 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 790 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 791 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 792 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 793 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 794 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 795 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 798 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 799 | return Success(0, E); |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 800 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 802 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 803 | return Success(0, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 806 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 807 | return Success(0, E); |
| 808 | } |
| 809 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 810 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 811 | return Success(E->EvaluateTrait(Info.Ctx), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 814 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 815 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 816 | } |
| 817 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 818 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 819 | bool VisitUnaryImag(const UnaryOperator *E); |
| 820 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 821 | private: |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 822 | unsigned GetAlignOfExpr(const Expr *E); |
| 823 | unsigned GetAlignOfType(QualType T); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 824 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 825 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 826 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 827 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 828 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 829 | if (!E->getType()->isIntegralType()) |
| 830 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 832 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 833 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 835 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 836 | APValue Val; |
| 837 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 838 | return false; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 839 | Result = Val.getInt(); |
| 840 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 841 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 843 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 844 | // Enums are integer constant exprs. |
| 845 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
Eli Friedman | 14fb858 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 846 | // FIXME: This is an ugly hack around the fact that enums don't set their |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 847 | // signedness consistently; see PR3173. |
| 848 | APSInt SI = D->getInitVal(); |
| 849 | SI.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
| 850 | // FIXME: This is an ugly hack around the fact that enums don't |
| 851 | // set their width (!?!) consistently; see PR3173. |
| 852 | SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 853 | return Success(SI, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 854 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 855 | |
| 856 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | 29f80c3 | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 857 | // In C, they can also be folded, although they are not ICEs. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 858 | if (E->getType().getCVRQualifiers() == Qualifiers::Const) { |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 859 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 860 | if (APValue *V = D->getEvaluatedValue()) |
| 861 | return Success(V->getInt(), E); |
| 862 | if (const Expr *Init = D->getInit()) { |
| 863 | if (Visit(const_cast<Expr*>(Init))) { |
| 864 | // Cache the evaluated value in the variable declaration. |
| 865 | D->setEvaluatedValue(Info.Ctx, Result); |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | return false; |
| 870 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 874 | // Otherwise, random variable references are not constants. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 875 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 878 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 879 | /// as GCC. |
| 880 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 881 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 882 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 883 | enum gcc_type_class { |
| 884 | no_type_class = -1, |
| 885 | void_type_class, integer_type_class, char_type_class, |
| 886 | enumeral_type_class, boolean_type_class, |
| 887 | pointer_type_class, reference_type_class, offset_type_class, |
| 888 | real_type_class, complex_type_class, |
| 889 | function_type_class, method_type_class, |
| 890 | record_type_class, union_type_class, |
| 891 | array_type_class, string_type_class, |
| 892 | lang_type_class |
| 893 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
| 895 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 896 | // ideal, however it is what gcc does. |
| 897 | if (E->getNumArgs() == 0) |
| 898 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 900 | QualType ArgTy = E->getArg(0)->getType(); |
| 901 | if (ArgTy->isVoidType()) |
| 902 | return void_type_class; |
| 903 | else if (ArgTy->isEnumeralType()) |
| 904 | return enumeral_type_class; |
| 905 | else if (ArgTy->isBooleanType()) |
| 906 | return boolean_type_class; |
| 907 | else if (ArgTy->isCharType()) |
| 908 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 909 | else if (ArgTy->isIntegerType()) |
| 910 | return integer_type_class; |
| 911 | else if (ArgTy->isPointerType()) |
| 912 | return pointer_type_class; |
| 913 | else if (ArgTy->isReferenceType()) |
| 914 | return reference_type_class; |
| 915 | else if (ArgTy->isRealType()) |
| 916 | return real_type_class; |
| 917 | else if (ArgTy->isComplexType()) |
| 918 | return complex_type_class; |
| 919 | else if (ArgTy->isFunctionType()) |
| 920 | return function_type_class; |
| 921 | else if (ArgTy->isStructureType()) |
| 922 | return record_type_class; |
| 923 | else if (ArgTy->isUnionType()) |
| 924 | return union_type_class; |
| 925 | else if (ArgTy->isArrayType()) |
| 926 | return array_type_class; |
| 927 | else if (ArgTy->isUnionType()) |
| 928 | return union_type_class; |
| 929 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 930 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 931 | return -1; |
| 932 | } |
| 933 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 934 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 935 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 936 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 937 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 938 | |
| 939 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 940 | const Expr *Arg = E->getArg(0)->IgnoreParens(); |
| 941 | Expr::EvalResult Base; |
Mike Stump | 5183a14 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 942 | if (Arg->EvaluateAsAny(Base, Info.Ctx) |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 943 | && Base.Val.getKind() == APValue::LValue |
| 944 | && !Base.HasSideEffects) |
| 945 | if (const Expr *LVBase = Base.Val.getLValueBase()) |
| 946 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) { |
| 947 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Mike Stump | 5179f30 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 948 | if (!VD->getType()->isIncompleteType() |
| 949 | && VD->getType()->isObjectType() |
| 950 | && !VD->getType()->isVariablyModifiedType() |
| 951 | && !VD->getType()->isDependentType()) { |
| 952 | uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8; |
| 953 | uint64_t Offset = Base.Val.getLValueOffset(); |
| 954 | if (Offset <= Size) |
| 955 | Size -= Base.Val.getLValueOffset(); |
| 956 | else |
| 957 | Size = 0; |
| 958 | return Success(Size, E); |
| 959 | } |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 963 | if (HasSideEffects(E->getArg(0), Info.Ctx)) { |
Mike Stump | 99f11f7 | 2009-10-26 18:57:47 +0000 | [diff] [blame] | 964 | if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2) |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 965 | return Success(-1, E); |
| 966 | return Success(0, E); |
| 967 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 968 | |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 969 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 970 | } |
| 971 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 972 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 973 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 975 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 976 | // __builtin_constant_p always has one operand: it returns true if that |
| 977 | // operand can be folded, false otherwise. |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 978 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 979 | |
| 980 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 981 | int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue(); |
| 982 | Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand); |
| 983 | return Success(Operand, E); |
| 984 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 985 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 986 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 987 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 988 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 989 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 990 | if (!Visit(E->getRHS())) |
| 991 | return false; |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 992 | |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 993 | // If we can't evaluate the LHS, it might have side effects; |
| 994 | // conservatively mark it. |
| 995 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 996 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 997 | |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 998 | return true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | if (E->isLogicalOp()) { |
| 1002 | // These need to be handled specially because the operands aren't |
| 1003 | // necessarily integral |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1004 | bool lhsResult, rhsResult; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1006 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1007 | // We were able to evaluate the LHS, see if we can get away with not |
| 1008 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1009 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1010 | return Success(lhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1011 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1012 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1013 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1014 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1015 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1016 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } else { |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1019 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1020 | // We can't evaluate the LHS; however, sometimes the result |
| 1021 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1023 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1024 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1025 | // must have had side effects. |
| 1026 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1027 | |
| 1028 | return Success(rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1031 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1032 | |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
| 1035 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1036 | QualType LHSTy = E->getLHS()->getType(); |
| 1037 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (LHSTy->isAnyComplexType()) { |
| 1040 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 1041 | APValue LHS, RHS; |
| 1042 | |
| 1043 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 1044 | return false; |
| 1045 | |
| 1046 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 1047 | return false; |
| 1048 | |
| 1049 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1051 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1053 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 1054 | |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1055 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1056 | return Success((CR_r == APFloat::cmpEqual && |
| 1057 | CR_i == APFloat::cmpEqual), E); |
| 1058 | else { |
| 1059 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1060 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1062 | CR_r == APFloat::cmpLessThan) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | (CR_i == APFloat::cmpGreaterThan || |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1064 | CR_i == APFloat::cmpLessThan)), E); |
| 1065 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1066 | } else { |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1067 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1068 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 1069 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 1070 | else { |
| 1071 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1072 | "Invalid compex comparison."); |
| 1073 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 1074 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 1075 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1076 | } |
| 1077 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1079 | if (LHSTy->isRealFloatingType() && |
| 1080 | RHSTy->isRealFloatingType()) { |
| 1081 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1083 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1084 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1086 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 1087 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1089 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 1090 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1091 | switch (E->getOpcode()) { |
| 1092 | default: |
| 1093 | assert(0 && "Invalid binary operator!"); |
| 1094 | case BinaryOperator::LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1095 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1096 | case BinaryOperator::GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1097 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1098 | case BinaryOperator::LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1099 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1100 | case BinaryOperator::GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1102 | E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1103 | case BinaryOperator::EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1104 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1105 | case BinaryOperator::NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | return Success(CR == APFloat::cmpGreaterThan |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1107 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1108 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1111 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 1112 | if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1113 | APValue LHSValue; |
| 1114 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1115 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1116 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1117 | APValue RHSValue; |
| 1118 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1119 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1120 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1121 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1122 | // to null. |
| 1123 | if (LHSValue.getLValueBase()) { |
| 1124 | if (!E->isEqualityOp()) |
| 1125 | return false; |
| 1126 | if (RHSValue.getLValueBase() || RHSValue.getLValueOffset()) |
| 1127 | return false; |
| 1128 | bool bres; |
| 1129 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1130 | return false; |
| 1131 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1132 | } else if (RHSValue.getLValueBase()) { |
| 1133 | if (!E->isEqualityOp()) |
| 1134 | return false; |
| 1135 | if (LHSValue.getLValueBase() || LHSValue.getLValueOffset()) |
| 1136 | return false; |
| 1137 | bool bres; |
| 1138 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1139 | return false; |
| 1140 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1141 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1142 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1143 | if (E->getOpcode() == BinaryOperator::Sub) { |
| 1144 | const QualType Type = E->getLHS()->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1145 | const QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1146 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1147 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
Eli Friedman | fa90b15 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1148 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
| 1149 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1150 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1151 | return Success(D, E); |
| 1152 | } |
| 1153 | bool Result; |
| 1154 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1155 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 8b171f6 | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1156 | } else { |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1157 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1158 | } |
| 1159 | return Success(Result, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1162 | if (!LHSTy->isIntegralType() || |
| 1163 | !RHSTy->isIntegralType()) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1164 | // We can't continue from here for non-integral types, and they |
| 1165 | // could potentially confuse the following operations. |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | } |
| 1168 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1169 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1170 | if (!Visit(E->getLHS())) |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1171 | return false; // error in subexpression. |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1172 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1173 | APValue RHSVal; |
| 1174 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1175 | return false; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1176 | |
| 1177 | // Handle cases like (unsigned long)&a + 4. |
| 1178 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
| 1179 | uint64_t offset = Result.getLValueOffset(); |
| 1180 | if (E->getOpcode() == BinaryOperator::Add) |
| 1181 | offset += RHSVal.getInt().getZExtValue(); |
| 1182 | else |
| 1183 | offset -= RHSVal.getInt().getZExtValue(); |
| 1184 | Result = APValue(Result.getLValueBase(), offset); |
| 1185 | return true; |
| 1186 | } |
| 1187 | |
| 1188 | // Handle cases like 4 + (unsigned long)&a |
| 1189 | if (E->getOpcode() == BinaryOperator::Add && |
| 1190 | RHSVal.isLValue() && Result.isInt()) { |
| 1191 | uint64_t offset = RHSVal.getLValueOffset(); |
| 1192 | offset += Result.getInt().getZExtValue(); |
| 1193 | Result = APValue(RHSVal.getLValueBase(), offset); |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | // All the following cases expect both operands to be an integer |
| 1198 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1199 | return false; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1200 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1201 | APSInt& RHS = RHSVal.getInt(); |
| 1202 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1203 | switch (E->getOpcode()) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1204 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1205 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1206 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 1207 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 1208 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 1209 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 1210 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 1211 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1212 | case BinaryOperator::Div: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1213 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1214 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1215 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1216 | case BinaryOperator::Rem: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1217 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1218 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1219 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1220 | case BinaryOperator::Shl: { |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1221 | // FIXME: Warn about out of range shift amounts! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1223 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1224 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1225 | } |
| 1226 | case BinaryOperator::Shr: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1228 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1229 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1232 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 1233 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 1234 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 1235 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 1236 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 1237 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | 8553a98 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1238 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1241 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1242 | bool Cond; |
| 1243 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1246 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1249 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1250 | // Get information about the alignment. |
| 1251 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1252 | |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1253 | // __alignof is defined to return the preferred alignment. |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1254 | return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize; |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1257 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 1258 | E = E->IgnoreParens(); |
| 1259 | |
| 1260 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1262 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | 43a5d9e | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1263 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1265 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | 43a5d9e | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1266 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1268 | return GetAlignOfType(E->getType()); |
| 1269 | } |
| 1270 | |
| 1271 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1272 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1273 | /// expression's type. |
| 1274 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 1275 | QualType DstTy = E->getType(); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1277 | // Handle alignof separately. |
| 1278 | if (!E->isSizeOf()) { |
| 1279 | if (E->isArgumentType()) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1280 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1281 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1282 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1283 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1284 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1285 | QualType SrcTy = E->getTypeOfArgument(); |
| 1286 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1287 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1288 | // extension. |
| 1289 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1290 | return Success(1, E); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1292 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1293 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1294 | return false; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1296 | // Get information about the size. |
Daniel Dunbar | bc5a7a8 | 2009-05-03 10:35:52 +0000 | [diff] [blame] | 1297 | unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy); |
Daniel Dunbar | 93c1914 | 2009-04-21 15:48:54 +0000 | [diff] [blame] | 1298 | return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1301 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1302 | // Special case unary operators that do not need their subexpression |
| 1303 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1304 | if (E->isOffsetOfOp()) { |
| 1305 | // The AST for offsetof is defined in such a way that we can just |
| 1306 | // directly Evaluate it as an l-value. |
| 1307 | APValue LV; |
| 1308 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
| 1309 | return false; |
| 1310 | if (LV.getLValueBase()) |
| 1311 | return false; |
| 1312 | return Success(LV.getLValueOffset(), E); |
| 1313 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1314 | |
| 1315 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1316 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1317 | bool bres; |
| 1318 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1319 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1320 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1323 | // Only handle integral operations... |
| 1324 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1325 | return false; |
| 1326 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1327 | // Get the operand value into 'Result'. |
| 1328 | if (!Visit(E->getSubExpr())) |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1329 | return false; |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1331 | switch (E->getOpcode()) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1332 | default: |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1333 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1334 | // See C99 6.6p3. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1335 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1336 | case UnaryOperator::Extension: |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1337 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1338 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1339 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1340 | case UnaryOperator::Plus: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | // The result is always just the subexpr. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1342 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1343 | case UnaryOperator::Minus: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1344 | if (!Result.isInt()) return false; |
| 1345 | return Success(-Result.getInt(), E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1346 | case UnaryOperator::Not: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1347 | if (!Result.isInt()) return false; |
| 1348 | return Success(~Result.getInt(), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1349 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1350 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1352 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1353 | /// result type is integer. |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1354 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1355 | Expr *SubExpr = E->getSubExpr(); |
| 1356 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1357 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1358 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1359 | if (DestType->isBooleanType()) { |
| 1360 | bool BoolResult; |
| 1361 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1362 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1363 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1366 | // Handle simple integer->integer casts. |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1367 | if (SrcType->isIntegralType()) { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1368 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1369 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1370 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1371 | if (!Result.isInt()) { |
| 1372 | // Only allow casts of lvalues if they are lossless. |
| 1373 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1374 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1375 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1376 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1377 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1378 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1380 | // FIXME: Clean this up! |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1381 | if (SrcType->isPointerType()) { |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1382 | APValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1383 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1384 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1385 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1386 | if (LV.getLValueBase()) { |
| 1387 | // Only allow based lvalue casts if they are lossless. |
| 1388 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1389 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1390 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1391 | Result = LV; |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType); |
| 1396 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1397 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1398 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1399 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1400 | // This handles double-conversion cases, where there's both |
| 1401 | // an l-value promotion and an implicit conversion to int. |
| 1402 | APValue LV; |
| 1403 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1404 | return false; |
| 1405 | |
| 1406 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1407 | return false; |
| 1408 | |
| 1409 | Result = LV; |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1413 | if (SrcType->isAnyComplexType()) { |
| 1414 | APValue C; |
| 1415 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1416 | return false; |
| 1417 | if (C.isComplexFloat()) |
| 1418 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1419 | C.getComplexFloatReal(), Info.Ctx), |
| 1420 | E); |
| 1421 | else |
| 1422 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1423 | C.getComplexIntReal(), Info.Ctx), E); |
| 1424 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1425 | // FIXME: Handle vectors |
| 1426 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1427 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1428 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1429 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1430 | APFloat F(0.0); |
| 1431 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1432 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1434 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1435 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1436 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1437 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1438 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1439 | APValue LV; |
| 1440 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1441 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1442 | return Success(LV.getComplexIntReal(), E); |
| 1443 | } |
| 1444 | |
| 1445 | return Visit(E->getSubExpr()); |
| 1446 | } |
| 1447 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1448 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1449 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1450 | APValue LV; |
| 1451 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1452 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1453 | return Success(LV.getComplexIntImag(), E); |
| 1454 | } |
| 1455 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1456 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1457 | Info.EvalResult.HasSideEffects = true; |
| 1458 | return Success(0, E); |
| 1459 | } |
| 1460 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1461 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1462 | // Float Evaluation |
| 1463 | //===----------------------------------------------------------------------===// |
| 1464 | |
| 1465 | namespace { |
| 1466 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1467 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1468 | EvalInfo &Info; |
| 1469 | APFloat &Result; |
| 1470 | public: |
| 1471 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1472 | : Info(info), Result(result) {} |
| 1473 | |
| 1474 | bool VisitStmt(Stmt *S) { |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
| 1478 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1479 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1480 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1481 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1482 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1483 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1484 | bool VisitCastExpr(CastExpr *E); |
| 1485 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1486 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1487 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1488 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1489 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1490 | { return Visit(E->getSubExpr()); } |
| 1491 | |
| 1492 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
| 1493 | // member of vector, ImplicitValueInitExpr, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1494 | // conditional ?:, comma |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1495 | }; |
| 1496 | } // end anonymous namespace |
| 1497 | |
| 1498 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1499 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1500 | } |
| 1501 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1502 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1503 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1504 | default: return false; |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1505 | case Builtin::BI__builtin_huge_val: |
| 1506 | case Builtin::BI__builtin_huge_valf: |
| 1507 | case Builtin::BI__builtin_huge_vall: |
| 1508 | case Builtin::BI__builtin_inf: |
| 1509 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1510 | case Builtin::BI__builtin_infl: { |
| 1511 | const llvm::fltSemantics &Sem = |
| 1512 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1513 | Result = llvm::APFloat::getInf(Sem); |
| 1514 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1515 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1517 | case Builtin::BI__builtin_nan: |
| 1518 | case Builtin::BI__builtin_nanf: |
| 1519 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1520 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1521 | // can't constant fold it. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | if (const StringLiteral *S = |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1523 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1524 | if (!S->isWide()) { |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1525 | const llvm::fltSemantics &Sem = |
| 1526 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1527 | llvm::SmallString<16> s; |
| 1528 | s.append(S->getStrData(), S->getStrData() + S->getByteLength()); |
| 1529 | s += '\0'; |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1530 | long l; |
| 1531 | char *endp; |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1532 | l = strtol(&s[0], &endp, 0); |
| 1533 | if (endp != s.end()-1) |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1534 | return false; |
| 1535 | unsigned type = (unsigned int)l;; |
| 1536 | Result = llvm::APFloat::getNaN(Sem, false, type); |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1537 | return true; |
| 1538 | } |
| 1539 | } |
| 1540 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1541 | |
| 1542 | case Builtin::BI__builtin_fabs: |
| 1543 | case Builtin::BI__builtin_fabsf: |
| 1544 | case Builtin::BI__builtin_fabsl: |
| 1545 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1546 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1548 | if (Result.isNegative()) |
| 1549 | Result.changeSign(); |
| 1550 | return true; |
| 1551 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | case Builtin::BI__builtin_copysign: |
| 1553 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1554 | case Builtin::BI__builtin_copysignl: { |
| 1555 | APFloat RHS(0.); |
| 1556 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1557 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1558 | return false; |
| 1559 | Result.copySign(RHS); |
| 1560 | return true; |
| 1561 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1565 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | 0e33c68 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1566 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1567 | return false; |
| 1568 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1569 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1570 | return false; |
| 1571 | |
| 1572 | switch (E->getOpcode()) { |
| 1573 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | case UnaryOperator::Plus: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1575 | return true; |
| 1576 | case UnaryOperator::Minus: |
| 1577 | Result.changeSign(); |
| 1578 | return true; |
| 1579 | } |
| 1580 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1581 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1582 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1583 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1584 | // and errors are supposed to work. |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1585 | APFloat RHS(0.0); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1586 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1587 | return false; |
| 1588 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1589 | return false; |
| 1590 | |
| 1591 | switch (E->getOpcode()) { |
| 1592 | default: return false; |
| 1593 | case BinaryOperator::Mul: |
| 1594 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1595 | return true; |
| 1596 | case BinaryOperator::Add: |
| 1597 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1598 | return true; |
| 1599 | case BinaryOperator::Sub: |
| 1600 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1601 | return true; |
| 1602 | case BinaryOperator::Div: |
| 1603 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1604 | return true; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1609 | Result = E->getValue(); |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1613 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1614 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1616 | if (SubExpr->getType()->isIntegralType()) { |
| 1617 | APSInt IntResult; |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1618 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1619 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1621 | IntResult, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1622 | return true; |
| 1623 | } |
| 1624 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1625 | if (!Visit(SubExpr)) |
| 1626 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1627 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1628 | Result, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1629 | return true; |
| 1630 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1631 | // FIXME: Handle complex types |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1632 | |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
| 1636 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1637 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1641 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1642 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1643 | //===----------------------------------------------------------------------===// |
| 1644 | |
| 1645 | namespace { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1646 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1647 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1648 | EvalInfo &Info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1650 | public: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1651 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1653 | //===--------------------------------------------------------------------===// |
| 1654 | // Visitor Methods |
| 1655 | //===--------------------------------------------------------------------===// |
| 1656 | |
| 1657 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1658 | return APValue(); |
| 1659 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1661 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1662 | |
| 1663 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1664 | Expr* SubExpr = E->getSubExpr(); |
| 1665 | |
| 1666 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1667 | APFloat Result(0.0); |
| 1668 | |
| 1669 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1670 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
| 1672 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1673 | Result); |
| 1674 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | assert(SubExpr->getType()->isIntegerType() && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1676 | "Unexpected imaginary literal."); |
| 1677 | |
| 1678 | llvm::APSInt Result; |
| 1679 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1680 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1682 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1683 | Zero = 0; |
| 1684 | return APValue(Zero, Result); |
| 1685 | } |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1688 | APValue VisitCastExpr(CastExpr *E) { |
| 1689 | Expr* SubExpr = E->getSubExpr(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1690 | QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1691 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1692 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1693 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1694 | APFloat Result(0.0); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1695 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1696 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1697 | return APValue(); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1698 | |
| 1699 | if (EltType->isRealFloatingType()) { |
| 1700 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | return APValue(Result, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1702 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
| 1703 | } else { |
| 1704 | llvm::APSInt IResult; |
| 1705 | IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1706 | llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); |
| 1707 | Zero = 0; |
| 1708 | return APValue(IResult, Zero); |
| 1709 | } |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1710 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1711 | APSInt Result; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1712 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1713 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1714 | return APValue(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1715 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1716 | if (EltType->isRealFloatingType()) { |
| 1717 | APFloat FResult = |
| 1718 | HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | return APValue(FResult, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1720 | APFloat(FResult.getSemantics(), APFloat::fcZero, false)); |
| 1721 | } else { |
| 1722 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1723 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1724 | Zero = 0; |
| 1725 | return APValue(Result, Zero); |
| 1726 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1727 | } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1728 | APValue Src; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1729 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1730 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1731 | return APValue(); |
| 1732 | |
| 1733 | QualType SrcType = CT->getElementType(); |
| 1734 | |
| 1735 | if (Src.isComplexFloat()) { |
| 1736 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1738 | Src.getComplexFloatReal(), |
| 1739 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1741 | Src.getComplexFloatImag(), |
| 1742 | Info.Ctx)); |
| 1743 | } else { |
| 1744 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1745 | Src.getComplexFloatReal(), |
| 1746 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | HandleFloatToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1748 | Src.getComplexFloatImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } else { |
| 1752 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1753 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1755 | Src.getComplexIntReal(), |
| 1756 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1758 | Src.getComplexIntImag(), |
| 1759 | Info.Ctx)); |
| 1760 | } else { |
| 1761 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1762 | Src.getComplexIntReal(), |
| 1763 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | HandleIntToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1765 | Src.getComplexIntImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1767 | } |
| 1768 | } |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | // FIXME: Handle more casts. |
| 1772 | return APValue(); |
| 1773 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1775 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1776 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1777 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1778 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1779 | { return Visit(E->getSubExpr()); } |
| 1780 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1781 | // conditional ?:, comma |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1782 | }; |
| 1783 | } // end anonymous namespace |
| 1784 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1786 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1787 | assert((!Result.isComplexFloat() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | (&Result.getComplexFloatReal().getSemantics() == |
| 1789 | &Result.getComplexFloatImag().getSemantics())) && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1790 | "Invalid complex evaluation."); |
| 1791 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1795 | APValue Result, RHS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1797 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1798 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1800 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1801 | return APValue(); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1802 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1803 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1804 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1805 | switch (E->getOpcode()) { |
| 1806 | default: return APValue(); |
| 1807 | case BinaryOperator::Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1808 | if (Result.isComplexFloat()) { |
| 1809 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1810 | APFloat::rmNearestTiesToEven); |
| 1811 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1812 | APFloat::rmNearestTiesToEven); |
| 1813 | } else { |
| 1814 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1815 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1816 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1817 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1818 | case BinaryOperator::Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1819 | if (Result.isComplexFloat()) { |
| 1820 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1821 | APFloat::rmNearestTiesToEven); |
| 1822 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1823 | APFloat::rmNearestTiesToEven); |
| 1824 | } else { |
| 1825 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1826 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1827 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1828 | break; |
| 1829 | case BinaryOperator::Mul: |
| 1830 | if (Result.isComplexFloat()) { |
| 1831 | APValue LHS = Result; |
| 1832 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1833 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1834 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1835 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1837 | APFloat Tmp = LHS_r; |
| 1838 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1839 | Result.getComplexFloatReal() = Tmp; |
| 1840 | Tmp = LHS_i; |
| 1841 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1842 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1843 | |
| 1844 | Tmp = LHS_r; |
| 1845 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1846 | Result.getComplexFloatImag() = Tmp; |
| 1847 | Tmp = LHS_i; |
| 1848 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1849 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1850 | } else { |
| 1851 | APValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1853 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1854 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1856 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1857 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1858 | } |
| 1859 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | return Result; |
| 1863 | } |
| 1864 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1865 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1866 | // Top level Expr::Evaluate method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1867 | //===----------------------------------------------------------------------===// |
| 1868 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1869 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1870 | /// any crazy technique (that has nothing to do with language standards) that |
| 1871 | /// we want to. If this function returns true, it returns the folded constant |
| 1872 | /// in Result. |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1873 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1874 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1875 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1876 | if (getType()->isVectorType()) { |
| 1877 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1878 | return false; |
| 1879 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1880 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1881 | return false; |
Daniel Dunbar | 76ba41c | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 1882 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1883 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1884 | return false; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1885 | } else if (getType()->isRealFloatingType()) { |
| 1886 | llvm::APFloat f(0.0); |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1887 | if (!EvaluateFloat(this, f, Info)) |
| 1888 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1890 | Result.Val = APValue(f); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1891 | } else if (getType()->isAnyComplexType()) { |
| 1892 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1893 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1894 | } else |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1895 | return false; |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1896 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1897 | return true; |
| 1898 | } |
| 1899 | |
Mike Stump | 5183a14 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 1900 | bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const { |
| 1901 | EvalInfo Info(Ctx, Result, true); |
| 1902 | |
| 1903 | if (getType()->isVectorType()) { |
| 1904 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1905 | return false; |
| 1906 | } else if (getType()->isIntegerType()) { |
| 1907 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
| 1908 | return false; |
| 1909 | } else if (getType()->hasPointerRepresentation()) { |
| 1910 | if (!EvaluatePointer(this, Result.Val, Info)) |
| 1911 | return false; |
| 1912 | } else if (getType()->isRealFloatingType()) { |
| 1913 | llvm::APFloat f(0.0); |
| 1914 | if (!EvaluateFloat(this, f, Info)) |
| 1915 | return false; |
| 1916 | |
| 1917 | Result.Val = APValue(f); |
| 1918 | } else if (getType()->isAnyComplexType()) { |
| 1919 | if (!EvaluateComplex(this, Result.Val, Info)) |
| 1920 | return false; |
| 1921 | } else |
| 1922 | return false; |
| 1923 | |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 1927 | bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1928 | EvalInfo Info(Ctx, Result); |
| 1929 | |
| 1930 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1931 | } |
| 1932 | |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 1933 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1934 | EvalInfo Info(Ctx, Result, true); |
| 1935 | |
| 1936 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1937 | } |
| 1938 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1939 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1940 | /// folded, but discard the result. |
| 1941 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1942 | EvalResult Result; |
| 1943 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1944 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1945 | |
| 1946 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1947 | EvalResult EvalResult; |
| 1948 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | 435bbe0 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1949 | Result = Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1950 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1951 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1952 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1953 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1954 | } |