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