Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 28 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 29 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 30 | |
Chris Lattner | cdf34e7 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 48 | /// EvalResult - Contains information about the evaluation. |
| 49 | Expr::EvalResult &EvalResult; |
Anders Carlsson | 5862001 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 50 | |
Eli Friedman | 7d45c48 | 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 | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 60 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 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 | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 63 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| 64 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 65 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 66 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 67 | |
| 68 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 69 | // Misc utilities |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 72 | static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 73 | const Expr* Base = Value.getLValueBase(); |
| 74 | |
| 75 | Result = Base || !Value.getLValueOffset().isZero(); |
| 76 | |
| 77 | const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base); |
| 78 | if (!DeclRef) |
| 79 | return true; |
| 80 | |
| 81 | const ValueDecl* Decl = DeclRef->getDecl(); |
| 82 | if (Decl->hasAttr<WeakAttr>() || |
| 83 | Decl->hasAttr<WeakRefAttr>() || |
| 84 | Decl->hasAttr<WeakImportAttr>()) |
| 85 | return false; |
| 86 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 90 | static bool HandleConversionToBool(const Expr* E, bool& Result, |
| 91 | EvalInfo &Info) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 92 | if (E->getType()->isIntegralType()) { |
| 93 | APSInt IntResult; |
| 94 | if (!EvaluateInteger(E, IntResult, Info)) |
| 95 | return false; |
| 96 | Result = IntResult != 0; |
| 97 | return true; |
| 98 | } else if (E->getType()->isRealFloatingType()) { |
| 99 | APFloat FloatResult(0.0); |
| 100 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 101 | return false; |
| 102 | Result = !FloatResult.isZero(); |
| 103 | return true; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 104 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 105 | APValue PointerResult; |
| 106 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 107 | return false; |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 108 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 109 | } else if (E->getType()->isAnyComplexType()) { |
| 110 | APValue ComplexResult; |
| 111 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 112 | return false; |
| 113 | if (ComplexResult.isComplexFloat()) { |
| 114 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 115 | !ComplexResult.getComplexFloatImag().isZero(); |
| 116 | } else { |
| 117 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 118 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 119 | } |
| 120 | return true; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 127 | APFloat &Value, ASTContext &Ctx) { |
| 128 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 129 | // Determine whether we are converting to unsigned or signed. |
| 130 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 132 | // FIXME: Warning for overflow. |
| 133 | uint64_t Space[4]; |
| 134 | bool ignored; |
| 135 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 136 | llvm::APFloat::rmTowardZero, &ignored); |
| 137 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 138 | } |
| 139 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 141 | APFloat &Value, ASTContext &Ctx) { |
| 142 | bool ignored; |
| 143 | APFloat Result = Value; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 145 | APFloat::rmNearestTiesToEven, &ignored); |
| 146 | return Result; |
| 147 | } |
| 148 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 150 | APSInt &Value, ASTContext &Ctx) { |
| 151 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 152 | APSInt Result = Value; |
| 153 | // Figure out if this is a truncate, extend or noop cast. |
| 154 | // If the input is signed, do a sign extend, noop, or truncate. |
| 155 | Result.extOrTrunc(DestWidth); |
| 156 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 157 | return Result; |
| 158 | } |
| 159 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 161 | APSInt &Value, ASTContext &Ctx) { |
| 162 | |
| 163 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 164 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 165 | APFloat::rmNearestTiesToEven); |
| 166 | return Result; |
| 167 | } |
| 168 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 169 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 170 | class HasSideEffect |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 171 | : public StmtVisitor<HasSideEffect, bool> { |
| 172 | EvalInfo &Info; |
| 173 | public: |
| 174 | |
| 175 | HasSideEffect(EvalInfo &info) : Info(info) {} |
| 176 | |
| 177 | // Unhandled nodes conservatively default to having side effects. |
| 178 | bool VisitStmt(Stmt *S) { |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 183 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 184 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 185 | return true; |
| 186 | return false; |
| 187 | } |
| 188 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 189 | // a ton of code. |
| 190 | bool VisitBlockExpr(BlockExpr *E) { return true; } |
| 191 | bool VisitPredefinedExpr(PredefinedExpr *E) { return false; } |
| 192 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 193 | { return Visit(E->getInitializer()); } |
| 194 | bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); } |
| 195 | bool VisitIntegerLiteral(IntegerLiteral *E) { return false; } |
| 196 | bool VisitFloatingLiteral(FloatingLiteral *E) { return false; } |
| 197 | bool VisitStringLiteral(StringLiteral *E) { return false; } |
| 198 | bool VisitCharacterLiteral(CharacterLiteral *E) { return false; } |
| 199 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; } |
| 200 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 201 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 202 | bool VisitChooseExpr(ChooseExpr *E) |
| 203 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 204 | bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); } |
| 205 | bool VisitBinAssign(BinaryOperator *E) { return true; } |
Mike Stump | f3eb5ec | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 206 | bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; } |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 207 | bool VisitBinaryOperator(BinaryOperator *E) |
| 208 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 209 | bool VisitUnaryPreInc(UnaryOperator *E) { return true; } |
| 210 | bool VisitUnaryPostInc(UnaryOperator *E) { return true; } |
| 211 | bool VisitUnaryPreDec(UnaryOperator *E) { return true; } |
| 212 | bool VisitUnaryPostDec(UnaryOperator *E) { return true; } |
| 213 | bool VisitUnaryDeref(UnaryOperator *E) { |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 214 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 215 | return true; |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 216 | return Visit(E->getSubExpr()); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 217 | } |
| 218 | bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | a067942 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 219 | |
| 220 | // Has side effects if any element does. |
| 221 | bool VisitInitListExpr(InitListExpr *E) { |
| 222 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 223 | if (Visit(E->getInit(i))) return true; |
| 224 | return false; |
| 225 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 228 | } // end anonymous namespace |
| 229 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
| 231 | // LValue Evaluation |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 234 | class LValueExprEvaluator |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 235 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 236 | EvalInfo &Info; |
| 237 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 239 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 240 | |
| 241 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 242 | return APValue(); |
| 243 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 244 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 245 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 246 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 247 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 248 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 249 | APValue VisitMemberExpr(MemberExpr *E); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 250 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); } |
| 251 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 252 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 253 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 254 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 255 | { return Visit(E->getSubExpr()); } |
| 256 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 257 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 258 | |
| 259 | APValue VisitCastExpr(CastExpr *E) { |
| 260 | switch (E->getCastKind()) { |
| 261 | default: |
| 262 | return APValue(); |
| 263 | |
| 264 | case CastExpr::CK_NoOp: |
| 265 | return Visit(E->getSubExpr()); |
| 266 | } |
| 267 | } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 268 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 269 | }; |
| 270 | } // end anonymous namespace |
| 271 | |
| 272 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 273 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 274 | return Result.isLValue(); |
| 275 | } |
| 276 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 278 | if (isa<FunctionDecl>(E->getDecl())) { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 279 | return APValue(E); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 280 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 281 | if (!Info.AnyLValue && !VD->hasGlobalStorage()) |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 282 | return APValue(); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 283 | if (!VD->getType()->isReferenceType()) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 284 | return APValue(E); |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 285 | // FIXME: Check whether VD might be overridden! |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 286 | if (const Expr *Init = VD->getAnyInitializer()) |
Douglas Gregor | 0840cc0 | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 287 | return Visit(const_cast<Expr *>(Init)); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return APValue(); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 293 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 294 | if (!Info.AnyLValue && !E->isFileScope()) |
| 295 | return APValue(); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 296 | return APValue(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 300 | APValue result; |
| 301 | QualType Ty; |
| 302 | if (E->isArrow()) { |
| 303 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 304 | return APValue(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 305 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 306 | } else { |
| 307 | result = Visit(E->getBase()); |
| 308 | if (result.isUninit()) |
| 309 | return APValue(); |
| 310 | Ty = E->getBase()->getType(); |
| 311 | } |
| 312 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 313 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 314 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 315 | |
| 316 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 317 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 318 | return APValue(); |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 319 | |
| 320 | if (FD->getType()->isReferenceType()) |
| 321 | return APValue(); |
| 322 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 323 | // FIXME: This is linear time. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 324 | unsigned i = 0; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 325 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 326 | FieldEnd = RD->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 327 | Field != FieldEnd; (void)++Field, ++i) { |
| 328 | if (*Field == FD) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | |
| 332 | result.setLValue(result.getLValueBase(), |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 333 | result.getLValueOffset() + |
| 334 | CharUnits::fromQuantity(RL.getFieldOffset(i) / 8)); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 335 | |
| 336 | return result; |
| 337 | } |
| 338 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 340 | APValue Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 342 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 343 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 345 | APSInt Index; |
| 346 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 347 | return APValue(); |
| 348 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 349 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 350 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 351 | CharUnits Offset = Index.getSExtValue() * ElementSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | Result.setLValue(Result.getLValueBase(), |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 353 | Result.getLValueOffset() + Offset); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 354 | return Result; |
| 355 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 356 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 358 | APValue Result; |
| 359 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 360 | return APValue(); |
| 361 | return Result; |
| 362 | } |
| 363 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 365 | // Pointer Evaluation |
| 366 | //===----------------------------------------------------------------------===// |
| 367 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 368 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 369 | class PointerExprEvaluator |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 370 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 371 | EvalInfo &Info; |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 372 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 374 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 375 | |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 376 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 377 | return APValue(); |
| 378 | } |
| 379 | |
| 380 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 381 | |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 382 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 383 | APValue VisitCastExpr(CastExpr* E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 384 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 385 | { return Visit(E->getSubExpr()); } |
| 386 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 387 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 388 | { return APValue(E); } |
Eli Friedman | 529a99b | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 389 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 390 | { return APValue(E); } |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 391 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 392 | APValue VisitBlockExpr(BlockExpr *E) { |
| 393 | if (!E->hasBlockDeclRefExprs()) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 394 | return APValue(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 395 | return APValue(); |
| 396 | } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 397 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 398 | { return APValue((Expr*)0); } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 399 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 400 | APValue VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 401 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 402 | APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 403 | { return APValue((Expr*)0); } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 404 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 405 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 406 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 407 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 408 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 409 | assert(E->getType()->hasPointerRepresentation()); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 410 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 411 | return Result.isLValue(); |
| 412 | } |
| 413 | |
| 414 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 415 | if (E->getOpcode() != BinaryOperator::Add && |
| 416 | E->getOpcode() != BinaryOperator::Sub) |
| 417 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 419 | const Expr *PExp = E->getLHS(); |
| 420 | const Expr *IExp = E->getRHS(); |
| 421 | if (IExp->getType()->isPointerType()) |
| 422 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 424 | APValue ResultLValue; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 425 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 426 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 428 | llvm::APSInt AdditionalOffset; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 429 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 430 | return APValue(); |
| 431 | |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 432 | // Compute the new offset in the appropriate width. |
| 433 | |
| 434 | QualType PointeeType = |
| 435 | PExp->getType()->getAs<PointerType>()->getPointeeType(); |
| 436 | llvm::APSInt SizeOfPointee(AdditionalOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 438 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 439 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 440 | SizeOfPointee = 1; |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 441 | else |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 442 | SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType).getQuantity(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 444 | llvm::APSInt Offset(AdditionalOffset); |
| 445 | Offset = ResultLValue.getLValueOffset().getQuantity(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 446 | if (E->getOpcode() == BinaryOperator::Add) |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 447 | Offset += AdditionalOffset * SizeOfPointee; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 448 | else |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 449 | Offset -= AdditionalOffset * SizeOfPointee; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 450 | |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 451 | // Sign extend prior to converting back to a char unit. |
| 452 | if (Offset.getBitWidth() < 64) |
| 453 | Offset.extend(64); |
| 454 | return APValue(ResultLValue.getLValueBase(), |
| 455 | CharUnits::fromQuantity(Offset.getLimitedValue())); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 456 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 457 | |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 458 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 459 | APValue result; |
| 460 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 461 | return result; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 462 | return APValue(); |
| 463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 465 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 466 | APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) { |
| 467 | Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 468 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 469 | switch (E->getCastKind()) { |
| 470 | default: |
| 471 | break; |
| 472 | |
| 473 | case CastExpr::CK_Unknown: { |
| 474 | // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary! |
| 475 | |
| 476 | // Check for pointer->pointer cast |
| 477 | if (SubExpr->getType()->isPointerType() || |
| 478 | SubExpr->getType()->isObjCObjectPointerType() || |
| 479 | SubExpr->getType()->isNullPtrType() || |
| 480 | SubExpr->getType()->isBlockPointerType()) |
| 481 | return Visit(SubExpr); |
| 482 | |
| 483 | if (SubExpr->getType()->isIntegralType()) { |
| 484 | APValue Result; |
| 485 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 486 | break; |
| 487 | |
| 488 | if (Result.isInt()) { |
| 489 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 490 | return APValue(0, |
| 491 | CharUnits::fromQuantity(Result.getInt().getZExtValue())); |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // Cast is of an lvalue, no need to change value. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 495 | return Result; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 496 | } |
| 497 | break; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 498 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 500 | case CastExpr::CK_NoOp: |
| 501 | case CastExpr::CK_BitCast: |
| 502 | case CastExpr::CK_AnyPointerToObjCPointerCast: |
| 503 | case CastExpr::CK_AnyPointerToBlockPointerCast: |
| 504 | return Visit(SubExpr); |
| 505 | |
| 506 | case CastExpr::CK_IntegralToPointer: { |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 507 | APValue Result; |
| 508 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 509 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 510 | |
| 511 | if (Result.isInt()) { |
| 512 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 513 | return APValue(0, |
| 514 | CharUnits::fromQuantity(Result.getInt().getZExtValue())); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 515 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 517 | // Cast is of an lvalue, no need to change value. |
| 518 | return Result; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 519 | } |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 520 | case CastExpr::CK_ArrayToPointerDecay: |
| 521 | case CastExpr::CK_FunctionToPointerDecay: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 522 | APValue Result; |
| 523 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 524 | return Result; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 529 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 531 | |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 532 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | if (E->isBuiltinCall(Info.Ctx) == |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 534 | Builtin::BI__builtin___CFStringMakeConstantString || |
| 535 | E->isBuiltinCall(Info.Ctx) == |
| 536 | Builtin::BI__builtin___NSStringMakeConstantString) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 537 | return APValue(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 538 | return APValue(); |
| 539 | } |
| 540 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 541 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 542 | bool BoolResult; |
| 543 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 544 | return APValue(); |
| 545 | |
| 546 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 547 | |
| 548 | APValue Result; |
| 549 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 550 | return Result; |
| 551 | return APValue(); |
| 552 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 553 | |
| 554 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 555 | // Vector Evaluation |
| 556 | //===----------------------------------------------------------------------===// |
| 557 | |
| 558 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 559 | class VectorExprEvaluator |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 560 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 561 | EvalInfo &Info; |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 562 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 563 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 565 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 567 | APValue VisitStmt(Stmt *S) { |
| 568 | return APValue(); |
| 569 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 571 | APValue VisitParenExpr(ParenExpr *E) |
| 572 | { return Visit(E->getSubExpr()); } |
| 573 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 574 | { return Visit(E->getSubExpr()); } |
| 575 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 576 | { return Visit(E->getSubExpr()); } |
| 577 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 578 | { return Visit(E->getSubExpr()); } |
| 579 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 580 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 581 | APValue VisitCastExpr(const CastExpr* E); |
| 582 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 583 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 584 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 585 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 586 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 587 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 588 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 589 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 590 | // shufflevector, ExtVectorElementExpr |
| 591 | // (Note that these require implementing conversions |
| 592 | // between vector types.) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 593 | }; |
| 594 | } // end anonymous namespace |
| 595 | |
| 596 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 597 | if (!E->getType()->isVectorType()) |
| 598 | return false; |
| 599 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 600 | return !Result.isUninit(); |
| 601 | } |
| 602 | |
| 603 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 604 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 605 | QualType EltTy = VTy->getElementType(); |
| 606 | unsigned NElts = VTy->getNumElements(); |
| 607 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 609 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 610 | QualType SETy = SE->getType(); |
| 611 | APValue Result = APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 612 | |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 613 | // Check for vector->vector bitcast and scalar->vector splat. |
| 614 | if (SETy->isVectorType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 615 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 616 | } else if (SETy->isIntegerType()) { |
| 617 | APSInt IntResult; |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 618 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 619 | return APValue(); |
| 620 | Result = APValue(IntResult); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 621 | } else if (SETy->isRealFloatingType()) { |
| 622 | APFloat F(0.0); |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 623 | if (!EvaluateFloat(SE, F, Info)) |
| 624 | return APValue(); |
| 625 | Result = APValue(F); |
| 626 | } else |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 627 | return APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 628 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 629 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 630 | // and splat it to all elements. |
| 631 | if (E->getType()->isExtVectorType()) { |
| 632 | if (EltTy->isIntegerType() && Result.isInt()) |
| 633 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 634 | Info.Ctx)); |
| 635 | else if (EltTy->isIntegerType()) |
| 636 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 637 | Info.Ctx)); |
| 638 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 639 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 640 | Info.Ctx)); |
| 641 | else if (EltTy->isRealFloatingType()) |
| 642 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 643 | Info.Ctx)); |
| 644 | else |
| 645 | return APValue(); |
| 646 | |
| 647 | // Splat and create vector APValue. |
| 648 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 649 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 650 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 651 | |
| 652 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 653 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 654 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 655 | // element. |
| 656 | APSInt Init; |
| 657 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 659 | llvm::SmallVector<APValue, 4> Elts; |
| 660 | for (unsigned i = 0; i != NElts; ++i) { |
| 661 | APSInt Tmp = Init; |
| 662 | Tmp.extOrTrunc(EltWidth); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 664 | if (EltTy->isIntegerType()) |
| 665 | Elts.push_back(APValue(Tmp)); |
| 666 | else if (EltTy->isRealFloatingType()) |
| 667 | Elts.push_back(APValue(APFloat(Tmp))); |
| 668 | else |
| 669 | return APValue(); |
| 670 | |
| 671 | Init >>= EltWidth; |
| 672 | } |
| 673 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 677 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 678 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 679 | } |
| 680 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 682 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 683 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 684 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 685 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 687 | QualType EltTy = VT->getElementType(); |
| 688 | llvm::SmallVector<APValue, 4> Elements; |
| 689 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 690 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 691 | if (EltTy->isIntegerType()) { |
| 692 | llvm::APSInt sInt(32); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 693 | if (i < NumInits) { |
| 694 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 695 | return APValue(); |
| 696 | } else { |
| 697 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 698 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 699 | Elements.push_back(APValue(sInt)); |
| 700 | } else { |
| 701 | llvm::APFloat f(0.0); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 702 | if (i < NumInits) { |
| 703 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 704 | return APValue(); |
| 705 | } else { |
| 706 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 707 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 708 | Elements.push_back(APValue(f)); |
| 709 | } |
| 710 | } |
| 711 | return APValue(&Elements[0], Elements.size()); |
| 712 | } |
| 713 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | APValue |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 715 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 716 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 717 | QualType EltTy = VT->getElementType(); |
| 718 | APValue ZeroElement; |
| 719 | if (EltTy->isIntegerType()) |
| 720 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 721 | else |
| 722 | ZeroElement = |
| 723 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 724 | |
| 725 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 726 | return APValue(&Elements[0], Elements.size()); |
| 727 | } |
| 728 | |
| 729 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 730 | bool BoolResult; |
| 731 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 732 | return APValue(); |
| 733 | |
| 734 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 735 | |
| 736 | APValue Result; |
| 737 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 738 | return Result; |
| 739 | return APValue(); |
| 740 | } |
| 741 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 742 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 743 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 744 | Info.EvalResult.HasSideEffects = true; |
| 745 | return GetZeroVector(E->getType()); |
| 746 | } |
| 747 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 748 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 749 | // Integer Evaluation |
| 750 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 751 | |
| 752 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 753 | class IntExprEvaluator |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 754 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 755 | EvalInfo &Info; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 756 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 757 | public: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 758 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 759 | : Info(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 760 | |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 761 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 762 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 763 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 764 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 765 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 766 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 767 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 768 | return true; |
| 769 | } |
| 770 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 771 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 772 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 773 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 774 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 775 | Result = APValue(APSInt(I)); |
| 776 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 777 | return true; |
| 778 | } |
| 779 | |
| 780 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 781 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 782 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 783 | return true; |
| 784 | } |
| 785 | |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 786 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 787 | // Take the first error. |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 788 | if (Info.EvalResult.Diag == 0) { |
| 789 | Info.EvalResult.DiagLoc = L; |
| 790 | Info.EvalResult.Diag = D; |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 791 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 793 | return false; |
Chris Lattner | ae8cc15 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 796 | //===--------------------------------------------------------------------===// |
| 797 | // Visitor Methods |
| 798 | //===--------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 800 | bool VisitStmt(Stmt *) { |
| 801 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 802 | return false; |
| 803 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 805 | bool VisitExpr(Expr *E) { |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 806 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 807 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 809 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 811 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 812 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 813 | } |
| 814 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 815 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 816 | } |
| 817 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | d7be95d | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 818 | // Per gcc docs "this built-in function ignores top level |
| 819 | // qualifiers". We need to use the canonical version to properly |
| 820 | // be able to strip CRV qualifiers from the type. |
| 821 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 822 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 824 | T1.getUnqualifiedType()), |
| 825 | E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 826 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 827 | |
| 828 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 829 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 830 | return CheckReferencedDecl(E, E->getDecl()); |
| 831 | } |
| 832 | bool VisitMemberExpr(const MemberExpr *E) { |
| 833 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
| 834 | // Conservatively assume a MemberExpr will have side-effects |
| 835 | Info.EvalResult.HasSideEffects = true; |
| 836 | return true; |
| 837 | } |
| 838 | return false; |
| 839 | } |
| 840 | |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 841 | bool VisitCallExpr(CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 842 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 843 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 844 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 845 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 846 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 847 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 848 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 849 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 850 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 851 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 854 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 855 | return Success(0, E); |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 856 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 858 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 859 | return Success(0, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 862 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 863 | return Success(0, E); |
| 864 | } |
| 865 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 866 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 867 | return Success(E->EvaluateTrait(Info.Ctx), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 870 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 871 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 872 | } |
| 873 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 874 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 875 | bool VisitUnaryImag(const UnaryOperator *E); |
| 876 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 877 | private: |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 878 | CharUnits GetAlignOfExpr(const Expr *E); |
| 879 | CharUnits GetAlignOfType(QualType T); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 880 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 881 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 882 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 883 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 884 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 885 | assert(E->getType()->isIntegralType()); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 886 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 887 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 888 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 889 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 890 | assert(E->getType()->isIntegralType()); |
| 891 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 892 | APValue Val; |
| 893 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 894 | return false; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 895 | Result = Val.getInt(); |
| 896 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 897 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 898 | |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 899 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 900 | // Enums are integer constant exprs. |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 901 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 902 | return Success(ECD->getInitVal(), E); |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 903 | |
| 904 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | 29f80c3 | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 905 | // In C, they can also be folded, although they are not ICEs. |
Douglas Gregor | 0840cc0 | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 906 | if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers() |
| 907 | == Qualifiers::Const) { |
Anders Carlsson | b0695ef | 2010-02-03 21:58:41 +0000 | [diff] [blame] | 908 | |
| 909 | if (isa<ParmVarDecl>(D)) |
| 910 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 911 | |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 912 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 913 | if (const Expr *Init = VD->getAnyInitializer()) { |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 914 | if (APValue *V = VD->getEvaluatedValue()) { |
| 915 | if (V->isInt()) |
| 916 | return Success(V->getInt(), E); |
| 917 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 918 | } |
| 919 | |
| 920 | if (VD->isEvaluatingValue()) |
| 921 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 922 | |
| 923 | VD->setEvaluatingValue(); |
| 924 | |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 925 | if (Visit(const_cast<Expr*>(Init))) { |
| 926 | // Cache the evaluated value in the variable declaration. |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 927 | VD->setEvaluatedValue(Result); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 928 | return true; |
| 929 | } |
| 930 | |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 931 | VD->setEvaluatedValue(APValue()); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 932 | return false; |
| 933 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 937 | // Otherwise, random variable references are not constants. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 938 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 941 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 942 | /// as GCC. |
| 943 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 944 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 945 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 946 | enum gcc_type_class { |
| 947 | no_type_class = -1, |
| 948 | void_type_class, integer_type_class, char_type_class, |
| 949 | enumeral_type_class, boolean_type_class, |
| 950 | pointer_type_class, reference_type_class, offset_type_class, |
| 951 | real_type_class, complex_type_class, |
| 952 | function_type_class, method_type_class, |
| 953 | record_type_class, union_type_class, |
| 954 | array_type_class, string_type_class, |
| 955 | lang_type_class |
| 956 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
| 958 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 959 | // ideal, however it is what gcc does. |
| 960 | if (E->getNumArgs() == 0) |
| 961 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 963 | QualType ArgTy = E->getArg(0)->getType(); |
| 964 | if (ArgTy->isVoidType()) |
| 965 | return void_type_class; |
| 966 | else if (ArgTy->isEnumeralType()) |
| 967 | return enumeral_type_class; |
| 968 | else if (ArgTy->isBooleanType()) |
| 969 | return boolean_type_class; |
| 970 | else if (ArgTy->isCharType()) |
| 971 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 972 | else if (ArgTy->isIntegerType()) |
| 973 | return integer_type_class; |
| 974 | else if (ArgTy->isPointerType()) |
| 975 | return pointer_type_class; |
| 976 | else if (ArgTy->isReferenceType()) |
| 977 | return reference_type_class; |
| 978 | else if (ArgTy->isRealType()) |
| 979 | return real_type_class; |
| 980 | else if (ArgTy->isComplexType()) |
| 981 | return complex_type_class; |
| 982 | else if (ArgTy->isFunctionType()) |
| 983 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 984 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 985 | return record_type_class; |
| 986 | else if (ArgTy->isUnionType()) |
| 987 | return union_type_class; |
| 988 | else if (ArgTy->isArrayType()) |
| 989 | return array_type_class; |
| 990 | else if (ArgTy->isUnionType()) |
| 991 | return union_type_class; |
| 992 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 993 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 994 | return -1; |
| 995 | } |
| 996 | |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 997 | bool IntExprEvaluator::VisitCallExpr(CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 998 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 999 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1000 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1001 | |
| 1002 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1003 | const Expr *Arg = E->getArg(0)->IgnoreParens(); |
| 1004 | Expr::EvalResult Base; |
Eric Christopher | 9946970 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 1005 | |
| 1006 | // TODO: Perhaps we should let LLVM lower this? |
Mike Stump | 5183a14 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 1007 | if (Arg->EvaluateAsAny(Base, Info.Ctx) |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1008 | && Base.Val.getKind() == APValue::LValue |
| 1009 | && !Base.HasSideEffects) |
| 1010 | if (const Expr *LVBase = Base.Val.getLValueBase()) |
| 1011 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) { |
| 1012 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Mike Stump | 5179f30 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 1013 | if (!VD->getType()->isIncompleteType() |
| 1014 | && VD->getType()->isObjectType() |
| 1015 | && !VD->getType()->isVariablyModifiedType() |
| 1016 | && !VD->getType()->isDependentType()) { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1017 | CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType()); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1018 | CharUnits Offset = Base.Val.getLValueOffset(); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1019 | if (!Offset.isNegative() && Offset <= Size) |
| 1020 | Size -= Offset; |
Mike Stump | 5179f30 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 1021 | else |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1022 | Size = CharUnits::Zero(); |
| 1023 | return Success(Size.getQuantity(), E); |
Mike Stump | 5179f30 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 1024 | } |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
Eric Christopher | 9946970 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 1028 | // If evaluating the argument has side-effects we can't determine |
| 1029 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 1030 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Benjamin Kramer | 0128f66 | 2010-01-03 18:18:37 +0000 | [diff] [blame] | 1031 | if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | 4f10559 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 1032 | return Success(-1ULL, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1033 | return Success(0, E); |
| 1034 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1035 | |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1036 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1037 | } |
| 1038 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1039 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1040 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1042 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1043 | // __builtin_constant_p always has one operand: it returns true if that |
| 1044 | // operand can be folded, false otherwise. |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1045 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1046 | |
| 1047 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 1048 | int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue(); |
| 1049 | Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand); |
| 1050 | return Success(Operand, E); |
| 1051 | } |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 1052 | |
| 1053 | case Builtin::BI__builtin_expect: |
| 1054 | return Visit(E->getArg(0)); |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1056 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1058 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1059 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1060 | if (!Visit(E->getRHS())) |
| 1061 | return false; |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1062 | |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1063 | // If we can't evaluate the LHS, it might have side effects; |
| 1064 | // conservatively mark it. |
| 1065 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 1066 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1067 | |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1068 | return true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | if (E->isLogicalOp()) { |
| 1072 | // These need to be handled specially because the operands aren't |
| 1073 | // necessarily integral |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1074 | bool lhsResult, rhsResult; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1076 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1077 | // We were able to evaluate the LHS, see if we can get away with not |
| 1078 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1079 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1080 | return Success(lhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1081 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1082 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1083 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1084 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1085 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1086 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } else { |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1089 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1090 | // We can't evaluate the LHS; however, sometimes the result |
| 1091 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1093 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1094 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1095 | // must have had side effects. |
| 1096 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1097 | |
| 1098 | return Success(rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1101 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1102 | |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1103 | return false; |
| 1104 | } |
| 1105 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1106 | QualType LHSTy = E->getLHS()->getType(); |
| 1107 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (LHSTy->isAnyComplexType()) { |
| 1110 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 1111 | APValue LHS, RHS; |
| 1112 | |
| 1113 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 1114 | return false; |
| 1115 | |
| 1116 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 1117 | return false; |
| 1118 | |
| 1119 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1121 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1123 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 1124 | |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1125 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1126 | return Success((CR_r == APFloat::cmpEqual && |
| 1127 | CR_i == APFloat::cmpEqual), E); |
| 1128 | else { |
| 1129 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1130 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1132 | CR_r == APFloat::cmpLessThan || |
| 1133 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1135 | CR_i == APFloat::cmpLessThan || |
| 1136 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1137 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1138 | } else { |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1139 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1140 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 1141 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 1142 | else { |
| 1143 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1144 | "Invalid compex comparison."); |
| 1145 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 1146 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 1147 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1148 | } |
| 1149 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1151 | if (LHSTy->isRealFloatingType() && |
| 1152 | RHSTy->isRealFloatingType()) { |
| 1153 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1155 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1156 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1158 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 1159 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1161 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 1162 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1163 | switch (E->getOpcode()) { |
| 1164 | default: |
| 1165 | assert(0 && "Invalid binary operator!"); |
| 1166 | case BinaryOperator::LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1167 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1168 | case BinaryOperator::GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1169 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1170 | case BinaryOperator::LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1171 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1172 | case BinaryOperator::GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1174 | E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1175 | case BinaryOperator::EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1176 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1177 | case BinaryOperator::NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1179 | || CR == APFloat::cmpLessThan |
| 1180 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1181 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1182 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1184 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 1185 | if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1186 | APValue LHSValue; |
| 1187 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1188 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1189 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1190 | APValue RHSValue; |
| 1191 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1192 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1193 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1194 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1195 | // to null. |
| 1196 | if (LHSValue.getLValueBase()) { |
| 1197 | if (!E->isEqualityOp()) |
| 1198 | return false; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1199 | if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero()) |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1200 | return false; |
| 1201 | bool bres; |
| 1202 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1203 | return false; |
| 1204 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1205 | } else if (RHSValue.getLValueBase()) { |
| 1206 | if (!E->isEqualityOp()) |
| 1207 | return false; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1208 | if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero()) |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | bool bres; |
| 1211 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1212 | return false; |
| 1213 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1214 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1215 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1216 | if (E->getOpcode() == BinaryOperator::Sub) { |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 1217 | QualType Type = E->getLHS()->getType(); |
| 1218 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1219 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1220 | CharUnits ElementSize = CharUnits::One(); |
Eli Friedman | fa90b15 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1221 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1222 | ElementSize = Info.Ctx.getTypeSizeInChars(ElementType); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1223 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1224 | CharUnits Diff = LHSValue.getLValueOffset() - |
| 1225 | RHSValue.getLValueOffset(); |
| 1226 | return Success(Diff / ElementSize, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1227 | } |
| 1228 | bool Result; |
| 1229 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1230 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 8b171f6 | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1231 | } else { |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1232 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1233 | } |
| 1234 | return Success(Result, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1237 | if (!LHSTy->isIntegralType() || |
| 1238 | !RHSTy->isIntegralType()) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1239 | // We can't continue from here for non-integral types, and they |
| 1240 | // could potentially confuse the following operations. |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1241 | return false; |
| 1242 | } |
| 1243 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1244 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1245 | if (!Visit(E->getLHS())) |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1246 | return false; // error in subexpression. |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1247 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1248 | APValue RHSVal; |
| 1249 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1250 | return false; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1251 | |
| 1252 | // Handle cases like (unsigned long)&a + 4. |
| 1253 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1254 | CharUnits Offset = Result.getLValueOffset(); |
| 1255 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 1256 | RHSVal.getInt().getZExtValue()); |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1257 | if (E->getOpcode() == BinaryOperator::Add) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1258 | Offset += AdditionalOffset; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1259 | else |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1260 | Offset -= AdditionalOffset; |
| 1261 | Result = APValue(Result.getLValueBase(), Offset); |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | // Handle cases like 4 + (unsigned long)&a |
| 1266 | if (E->getOpcode() == BinaryOperator::Add && |
| 1267 | RHSVal.isLValue() && Result.isInt()) { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1268 | CharUnits Offset = RHSVal.getLValueOffset(); |
| 1269 | Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue()); |
| 1270 | Result = APValue(RHSVal.getLValueBase(), Offset); |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | |
| 1274 | // All the following cases expect both operands to be an integer |
| 1275 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1276 | return false; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1277 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1278 | APSInt& RHS = RHSVal.getInt(); |
| 1279 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1280 | switch (E->getOpcode()) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1281 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1282 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1283 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 1284 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 1285 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 1286 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 1287 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 1288 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1289 | case BinaryOperator::Div: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1290 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1291 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1292 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1293 | case BinaryOperator::Rem: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1294 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1295 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1296 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1297 | case BinaryOperator::Shl: { |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1298 | // FIXME: Warn about out of range shift amounts! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1300 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1301 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1302 | } |
| 1303 | case BinaryOperator::Shr: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1305 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1306 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1307 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1309 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 1310 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 1311 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 1312 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 1313 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 1314 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | 8553a98 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1315 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1318 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1319 | bool Cond; |
| 1320 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1323 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1326 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1327 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1328 | // the result is the size of the referenced type." |
| 1329 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1330 | // result shall be the alignment of the referenced type." |
| 1331 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 1332 | T = Ref->getPointeeType(); |
| 1333 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1334 | // Get information about the alignment. |
| 1335 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1336 | |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1337 | // __alignof is defined to return the preferred alignment. |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1338 | return CharUnits::fromQuantity( |
| 1339 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1342 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1343 | E = E->IgnoreParens(); |
| 1344 | |
| 1345 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1347 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1348 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 1349 | /*RefAsPointee*/true); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1351 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1352 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 1353 | /*RefAsPointee*/true); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1355 | return GetAlignOfType(E->getType()); |
| 1356 | } |
| 1357 | |
| 1358 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1359 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1360 | /// expression's type. |
| 1361 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1362 | // Handle alignof separately. |
| 1363 | if (!E->isSizeOf()) { |
| 1364 | if (E->isArgumentType()) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1365 | return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1366 | else |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1367 | return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1368 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1369 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1370 | QualType SrcTy = E->getTypeOfArgument(); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1371 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1372 | // the result is the size of the referenced type." |
| 1373 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1374 | // result shall be the alignment of the referenced type." |
| 1375 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 1376 | SrcTy = Ref->getPointeeType(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1377 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1378 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1379 | // extension. |
| 1380 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1381 | return Success(1, E); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1383 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1384 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1385 | return false; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1387 | // Get information about the size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1388 | return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1391 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
| 1392 | CharUnits Result; |
| 1393 | unsigned n = E->getNumComponents(); |
| 1394 | OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E); |
| 1395 | if (n == 0) |
| 1396 | return false; |
| 1397 | QualType CurrentType = E->getTypeSourceInfo()->getType(); |
| 1398 | for (unsigned i = 0; i != n; ++i) { |
| 1399 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 1400 | switch (ON.getKind()) { |
| 1401 | case OffsetOfExpr::OffsetOfNode::Array: { |
| 1402 | Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
| 1403 | APSInt IdxResult; |
| 1404 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 1405 | return false; |
| 1406 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 1407 | if (!AT) |
| 1408 | return false; |
| 1409 | CurrentType = AT->getElementType(); |
| 1410 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 1411 | Result += IdxResult.getSExtValue() * ElementSize; |
| 1412 | break; |
| 1413 | } |
| 1414 | |
| 1415 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 1416 | FieldDecl *MemberDecl = ON.getField(); |
| 1417 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 1418 | if (!RT) |
| 1419 | return false; |
| 1420 | RecordDecl *RD = RT->getDecl(); |
| 1421 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 1422 | unsigned i = 0; |
| 1423 | // FIXME: It would be nice if we didn't have to loop here! |
| 1424 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1425 | FieldEnd = RD->field_end(); |
| 1426 | Field != FieldEnd; (void)++Field, ++i) { |
| 1427 | if (*Field == MemberDecl) |
| 1428 | break; |
| 1429 | } |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1430 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
| 1431 | Result += CharUnits::fromQuantity( |
| 1432 | RL.getFieldOffset(i) / Info.Ctx.getCharWidth()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1433 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 1438 | llvm_unreachable("dependent __builtin_offsetof"); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1439 | return false; |
| 1440 | |
| 1441 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 1442 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 1443 | if (BaseSpec->isVirtual()) |
| 1444 | return false; |
| 1445 | |
| 1446 | // Find the layout of the class whose base we are looking into. |
| 1447 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 1448 | if (!RT) |
| 1449 | return false; |
| 1450 | RecordDecl *RD = RT->getDecl(); |
| 1451 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 1452 | |
| 1453 | // Find the base class itself. |
| 1454 | CurrentType = BaseSpec->getType(); |
| 1455 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 1456 | if (!BaseRT) |
| 1457 | return false; |
| 1458 | |
| 1459 | // Add the offset to the base. |
| 1460 | Result += CharUnits::fromQuantity( |
| 1461 | RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())) |
| 1462 | / Info.Ctx.getCharWidth()); |
| 1463 | break; |
| 1464 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | return Success(Result.getQuantity(), E); |
| 1468 | } |
| 1469 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1470 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1471 | // Special case unary operators that do not need their subexpression |
| 1472 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1473 | if (E->isOffsetOfOp()) { |
| 1474 | // The AST for offsetof is defined in such a way that we can just |
| 1475 | // directly Evaluate it as an l-value. |
| 1476 | APValue LV; |
| 1477 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1478 | return false; |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1479 | if (LV.getLValueBase()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1480 | return false; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1481 | return Success(LV.getLValueOffset().getQuantity(), E); |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1482 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1483 | |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1484 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1485 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1486 | bool bres; |
| 1487 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1488 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1489 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1492 | // Only handle integral operations... |
| 1493 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1494 | return false; |
| 1495 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1496 | // Get the operand value into 'Result'. |
| 1497 | if (!Visit(E->getSubExpr())) |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1498 | return false; |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1500 | switch (E->getOpcode()) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1501 | default: |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1502 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1503 | // See C99 6.6p3. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1504 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1505 | case UnaryOperator::Extension: |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1506 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1507 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1508 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1509 | case UnaryOperator::Plus: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | // The result is always just the subexpr. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1511 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1512 | case UnaryOperator::Minus: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1513 | if (!Result.isInt()) return false; |
| 1514 | return Success(-Result.getInt(), E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1515 | case UnaryOperator::Not: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1516 | if (!Result.isInt()) return false; |
| 1517 | return Success(~Result.getInt(), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1518 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1521 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1522 | /// result type is integer. |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1523 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1524 | Expr *SubExpr = E->getSubExpr(); |
| 1525 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1526 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1527 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1528 | if (DestType->isBooleanType()) { |
| 1529 | bool BoolResult; |
| 1530 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1531 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1532 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1535 | // Handle simple integer->integer casts. |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1536 | if (SrcType->isIntegralType()) { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1537 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1538 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1539 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1540 | if (!Result.isInt()) { |
| 1541 | // Only allow casts of lvalues if they are lossless. |
| 1542 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1543 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1544 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1545 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1546 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1547 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1549 | // FIXME: Clean this up! |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1550 | if (SrcType->isPointerType()) { |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1551 | APValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1552 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1553 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1554 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1555 | if (LV.getLValueBase()) { |
| 1556 | // Only allow based lvalue casts if they are lossless. |
| 1557 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1558 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1559 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1560 | Result = LV; |
| 1561 | return true; |
| 1562 | } |
| 1563 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1564 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 1565 | SrcType); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1566 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1567 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1568 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1569 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1570 | // This handles double-conversion cases, where there's both |
| 1571 | // an l-value promotion and an implicit conversion to int. |
| 1572 | APValue LV; |
| 1573 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1574 | return false; |
| 1575 | |
| 1576 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1577 | return false; |
| 1578 | |
| 1579 | Result = LV; |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1583 | if (SrcType->isAnyComplexType()) { |
| 1584 | APValue C; |
| 1585 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1586 | return false; |
| 1587 | if (C.isComplexFloat()) |
| 1588 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1589 | C.getComplexFloatReal(), Info.Ctx), |
| 1590 | E); |
| 1591 | else |
| 1592 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1593 | C.getComplexIntReal(), Info.Ctx), E); |
| 1594 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1595 | // FIXME: Handle vectors |
| 1596 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1597 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1598 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1599 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1600 | APFloat F(0.0); |
| 1601 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1602 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1604 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1605 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1606 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1607 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1608 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1609 | APValue LV; |
| 1610 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1611 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1612 | return Success(LV.getComplexIntReal(), E); |
| 1613 | } |
| 1614 | |
| 1615 | return Visit(E->getSubExpr()); |
| 1616 | } |
| 1617 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1618 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1619 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1620 | APValue LV; |
| 1621 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1622 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1623 | return Success(LV.getComplexIntImag(), E); |
| 1624 | } |
| 1625 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1626 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1627 | Info.EvalResult.HasSideEffects = true; |
| 1628 | return Success(0, E); |
| 1629 | } |
| 1630 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1631 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1632 | // Float Evaluation |
| 1633 | //===----------------------------------------------------------------------===// |
| 1634 | |
| 1635 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1636 | class FloatExprEvaluator |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1637 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1638 | EvalInfo &Info; |
| 1639 | APFloat &Result; |
| 1640 | public: |
| 1641 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1642 | : Info(info), Result(result) {} |
| 1643 | |
| 1644 | bool VisitStmt(Stmt *S) { |
| 1645 | return false; |
| 1646 | } |
| 1647 | |
| 1648 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1649 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1650 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1651 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1652 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1653 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1654 | bool VisitCastExpr(CastExpr *E); |
| 1655 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | f3da334 | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1656 | bool VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1657 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1658 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1659 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1660 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1661 | { return Visit(E->getSubExpr()); } |
| 1662 | |
| 1663 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
Eli Friedman | f3da334 | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1664 | // member of vector, ImplicitValueInitExpr |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1665 | }; |
| 1666 | } // end anonymous namespace |
| 1667 | |
| 1668 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 1669 | assert(E->getType()->isRealFloatingType()); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1670 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1671 | } |
| 1672 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1673 | static bool TryEvaluateBuiltinNaN(ASTContext &Context, |
| 1674 | QualType ResultTy, |
| 1675 | const Expr *Arg, |
| 1676 | bool SNaN, |
| 1677 | llvm::APFloat &Result) { |
| 1678 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 1679 | if (!S) return false; |
| 1680 | |
| 1681 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 1682 | |
| 1683 | llvm::APInt fill; |
| 1684 | |
| 1685 | // Treat empty strings as if they were zero. |
| 1686 | if (S->getString().empty()) |
| 1687 | fill = llvm::APInt(32, 0); |
| 1688 | else if (S->getString().getAsInteger(0, fill)) |
| 1689 | return false; |
| 1690 | |
| 1691 | if (SNaN) |
| 1692 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 1693 | else |
| 1694 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1698 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1699 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1700 | default: return false; |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1701 | case Builtin::BI__builtin_huge_val: |
| 1702 | case Builtin::BI__builtin_huge_valf: |
| 1703 | case Builtin::BI__builtin_huge_vall: |
| 1704 | case Builtin::BI__builtin_inf: |
| 1705 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1706 | case Builtin::BI__builtin_infl: { |
| 1707 | const llvm::fltSemantics &Sem = |
| 1708 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1709 | Result = llvm::APFloat::getInf(Sem); |
| 1710 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1711 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1713 | case Builtin::BI__builtin_nans: |
| 1714 | case Builtin::BI__builtin_nansf: |
| 1715 | case Builtin::BI__builtin_nansl: |
| 1716 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 1717 | true, Result); |
| 1718 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1719 | case Builtin::BI__builtin_nan: |
| 1720 | case Builtin::BI__builtin_nanf: |
| 1721 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1722 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1723 | // can't constant fold it. |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1724 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 1725 | false, Result); |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1726 | |
| 1727 | case Builtin::BI__builtin_fabs: |
| 1728 | case Builtin::BI__builtin_fabsf: |
| 1729 | case Builtin::BI__builtin_fabsl: |
| 1730 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1731 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1733 | if (Result.isNegative()) |
| 1734 | Result.changeSign(); |
| 1735 | return true; |
| 1736 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | case Builtin::BI__builtin_copysign: |
| 1738 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1739 | case Builtin::BI__builtin_copysignl: { |
| 1740 | APFloat RHS(0.); |
| 1741 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1742 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1743 | return false; |
| 1744 | Result.copySign(RHS); |
| 1745 | return true; |
| 1746 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1750 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | 0e33c68 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1751 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1752 | return false; |
| 1753 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1754 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1755 | return false; |
| 1756 | |
| 1757 | switch (E->getOpcode()) { |
| 1758 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | case UnaryOperator::Plus: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1760 | return true; |
| 1761 | case UnaryOperator::Minus: |
| 1762 | Result.changeSign(); |
| 1763 | return true; |
| 1764 | } |
| 1765 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1766 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1767 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 1768 | if (E->getOpcode() == BinaryOperator::Comma) { |
| 1769 | if (!EvaluateFloat(E->getRHS(), Result, Info)) |
| 1770 | return false; |
| 1771 | |
| 1772 | // If we can't evaluate the LHS, it might have side effects; |
| 1773 | // conservatively mark it. |
| 1774 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 1775 | Info.EvalResult.HasSideEffects = true; |
| 1776 | |
| 1777 | return true; |
| 1778 | } |
| 1779 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1780 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1781 | // and errors are supposed to work. |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1782 | APFloat RHS(0.0); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1783 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1784 | return false; |
| 1785 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1786 | return false; |
| 1787 | |
| 1788 | switch (E->getOpcode()) { |
| 1789 | default: return false; |
| 1790 | case BinaryOperator::Mul: |
| 1791 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1792 | return true; |
| 1793 | case BinaryOperator::Add: |
| 1794 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1795 | return true; |
| 1796 | case BinaryOperator::Sub: |
| 1797 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1798 | return true; |
| 1799 | case BinaryOperator::Div: |
| 1800 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1801 | return true; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1806 | Result = E->getValue(); |
| 1807 | return true; |
| 1808 | } |
| 1809 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1810 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1811 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1813 | if (SubExpr->getType()->isIntegralType()) { |
| 1814 | APSInt IntResult; |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1815 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1816 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1818 | IntResult, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | } |
| 1821 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1822 | if (!Visit(SubExpr)) |
| 1823 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1824 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1825 | Result, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1826 | return true; |
| 1827 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1828 | // FIXME: Handle complex types |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1829 | |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1834 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1835 | return true; |
| 1836 | } |
| 1837 | |
Eli Friedman | f3da334 | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1838 | bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 1839 | bool Cond; |
| 1840 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
| 1841 | return false; |
| 1842 | |
| 1843 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 1844 | } |
| 1845 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1846 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1847 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1848 | //===----------------------------------------------------------------------===// |
| 1849 | |
| 1850 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1851 | class ComplexExprEvaluator |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1852 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1853 | EvalInfo &Info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1855 | public: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1856 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1858 | //===--------------------------------------------------------------------===// |
| 1859 | // Visitor Methods |
| 1860 | //===--------------------------------------------------------------------===// |
| 1861 | |
| 1862 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1863 | return APValue(); |
| 1864 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1866 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1867 | |
| 1868 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1869 | Expr* SubExpr = E->getSubExpr(); |
| 1870 | |
| 1871 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1872 | APFloat Result(0.0); |
| 1873 | |
| 1874 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1875 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | |
| 1877 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1878 | Result); |
| 1879 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | assert(SubExpr->getType()->isIntegerType() && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1881 | "Unexpected imaginary literal."); |
| 1882 | |
| 1883 | llvm::APSInt Result; |
| 1884 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1885 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1887 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1888 | Zero = 0; |
| 1889 | return APValue(Zero, Result); |
| 1890 | } |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1893 | APValue VisitCastExpr(CastExpr *E) { |
| 1894 | Expr* SubExpr = E->getSubExpr(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1895 | QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1896 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1897 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1898 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1899 | APFloat Result(0.0); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1900 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1901 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1902 | return APValue(); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1903 | |
| 1904 | if (EltType->isRealFloatingType()) { |
| 1905 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | return APValue(Result, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1907 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
| 1908 | } else { |
| 1909 | llvm::APSInt IResult; |
| 1910 | IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1911 | llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); |
| 1912 | Zero = 0; |
| 1913 | return APValue(IResult, Zero); |
| 1914 | } |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1915 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1916 | APSInt Result; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1917 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1918 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1919 | return APValue(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1920 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1921 | if (EltType->isRealFloatingType()) { |
| 1922 | APFloat FResult = |
| 1923 | HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | return APValue(FResult, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1925 | APFloat(FResult.getSemantics(), APFloat::fcZero, false)); |
| 1926 | } else { |
| 1927 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1928 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1929 | Zero = 0; |
| 1930 | return APValue(Result, Zero); |
| 1931 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1932 | } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1933 | APValue Src; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1934 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1935 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1936 | return APValue(); |
| 1937 | |
| 1938 | QualType SrcType = CT->getElementType(); |
| 1939 | |
| 1940 | if (Src.isComplexFloat()) { |
| 1941 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1943 | Src.getComplexFloatReal(), |
| 1944 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1946 | Src.getComplexFloatImag(), |
| 1947 | Info.Ctx)); |
| 1948 | } else { |
| 1949 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1950 | Src.getComplexFloatReal(), |
| 1951 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | HandleFloatToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1953 | Src.getComplexFloatImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } else { |
| 1957 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1958 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1960 | Src.getComplexIntReal(), |
| 1961 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1963 | Src.getComplexIntImag(), |
| 1964 | Info.Ctx)); |
| 1965 | } else { |
| 1966 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1967 | Src.getComplexIntReal(), |
| 1968 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | HandleIntToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1970 | Src.getComplexIntImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | // FIXME: Handle more casts. |
| 1977 | return APValue(); |
| 1978 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1980 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1981 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1982 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1983 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1984 | { return Visit(E->getSubExpr()); } |
| 1985 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1986 | // conditional ?:, comma |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1987 | }; |
| 1988 | } // end anonymous namespace |
| 1989 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 1991 | assert(E->getType()->isAnyComplexType()); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1992 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1993 | assert((!Result.isComplexFloat() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | (&Result.getComplexFloatReal().getSemantics() == |
| 1995 | &Result.getComplexFloatImag().getSemantics())) && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1996 | "Invalid complex evaluation."); |
| 1997 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2001 | APValue Result, RHS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2003 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2004 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2006 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2007 | return APValue(); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2008 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2009 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 2010 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2011 | switch (E->getOpcode()) { |
| 2012 | default: return APValue(); |
| 2013 | case BinaryOperator::Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2014 | if (Result.isComplexFloat()) { |
| 2015 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 2016 | APFloat::rmNearestTiesToEven); |
| 2017 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 2018 | APFloat::rmNearestTiesToEven); |
| 2019 | } else { |
| 2020 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 2021 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 2022 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2023 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2024 | case BinaryOperator::Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2025 | if (Result.isComplexFloat()) { |
| 2026 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 2027 | APFloat::rmNearestTiesToEven); |
| 2028 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 2029 | APFloat::rmNearestTiesToEven); |
| 2030 | } else { |
| 2031 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 2032 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 2033 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | case BinaryOperator::Mul: |
| 2036 | if (Result.isComplexFloat()) { |
| 2037 | APValue LHS = Result; |
| 2038 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 2039 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 2040 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 2041 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2043 | APFloat Tmp = LHS_r; |
| 2044 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2045 | Result.getComplexFloatReal() = Tmp; |
| 2046 | Tmp = LHS_i; |
| 2047 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2048 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 2049 | |
| 2050 | Tmp = LHS_r; |
| 2051 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2052 | Result.getComplexFloatImag() = Tmp; |
| 2053 | Tmp = LHS_i; |
| 2054 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2055 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 2056 | } else { |
| 2057 | APValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2059 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 2060 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2062 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 2063 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 2064 | } |
| 2065 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | return Result; |
| 2069 | } |
| 2070 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2071 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2072 | // Top level Expr::Evaluate method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2073 | //===----------------------------------------------------------------------===// |
| 2074 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2075 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2076 | /// any crazy technique (that has nothing to do with language standards) that |
| 2077 | /// we want to. If this function returns true, it returns the folded constant |
| 2078 | /// in Result. |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 2079 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 2080 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 2081 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2082 | if (getType()->isVectorType()) { |
| 2083 | if (!EvaluateVector(this, Result.Val, Info)) |
| 2084 | return false; |
| 2085 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2086 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2087 | return false; |
Daniel Dunbar | 76ba41c | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 2088 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 2089 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2090 | return false; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2091 | } else if (getType()->isRealFloatingType()) { |
| 2092 | llvm::APFloat f(0.0); |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2093 | if (!EvaluateFloat(this, f, Info)) |
| 2094 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 2096 | Result.Val = APValue(f); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2097 | } else if (getType()->isAnyComplexType()) { |
| 2098 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2099 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2100 | } else |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 2101 | return false; |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2102 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 2103 | return true; |
| 2104 | } |
| 2105 | |
Mike Stump | 5183a14 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 2106 | bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const { |
| 2107 | EvalInfo Info(Ctx, Result, true); |
| 2108 | |
| 2109 | if (getType()->isVectorType()) { |
| 2110 | if (!EvaluateVector(this, Result.Val, Info)) |
| 2111 | return false; |
| 2112 | } else if (getType()->isIntegerType()) { |
| 2113 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
| 2114 | return false; |
| 2115 | } else if (getType()->hasPointerRepresentation()) { |
| 2116 | if (!EvaluatePointer(this, Result.Val, Info)) |
| 2117 | return false; |
| 2118 | } else if (getType()->isRealFloatingType()) { |
| 2119 | llvm::APFloat f(0.0); |
| 2120 | if (!EvaluateFloat(this, f, Info)) |
| 2121 | return false; |
| 2122 | |
| 2123 | Result.Val = APValue(f); |
| 2124 | } else if (getType()->isAnyComplexType()) { |
| 2125 | if (!EvaluateComplex(this, Result.Val, Info)) |
| 2126 | return false; |
| 2127 | } else |
| 2128 | return false; |
| 2129 | |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 2133 | bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const { |
| 2134 | EvalResult Scratch; |
| 2135 | EvalInfo Info(Ctx, Scratch); |
| 2136 | |
| 2137 | return HandleConversionToBool(this, Result, Info); |
| 2138 | } |
| 2139 | |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 2140 | bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 2141 | EvalInfo Info(Ctx, Result); |
| 2142 | |
| 2143 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 2144 | } |
| 2145 | |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 2146 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 2147 | EvalInfo Info(Ctx, Result, true); |
| 2148 | |
| 2149 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 2150 | } |
| 2151 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2152 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2153 | /// folded, but discard the result. |
| 2154 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 2155 | EvalResult Result; |
| 2156 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2157 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2158 | |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 2159 | bool Expr::HasSideEffects(ASTContext &Ctx) const { |
| 2160 | Expr::EvalResult Result; |
| 2161 | EvalInfo Info(Ctx, Result); |
| 2162 | return HasSideEffect(Info).Visit(const_cast<Expr*>(this)); |
| 2163 | } |
| 2164 | |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2165 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2166 | EvalResult EvalResult; |
| 2167 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | 435bbe0 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 2168 | Result = Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2169 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2170 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2171 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2172 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2173 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2174 | |
| 2175 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 2176 | /// an integer constant expression. |
| 2177 | |
| 2178 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 2179 | /// comma, etc |
| 2180 | /// |
| 2181 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 2182 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 2183 | /// cast+dereference. |
| 2184 | |
| 2185 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 2186 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 2187 | // Note that to reduce code duplication, this helper does no evaluation |
| 2188 | // itself; the caller checks whether the expression is evaluatable, and |
| 2189 | // in the rare cases where CheckICE actually cares about the evaluated |
| 2190 | // value, it calls into Evalute. |
| 2191 | // |
| 2192 | // Meanings of Val: |
| 2193 | // 0: This expression is an ICE if it can be evaluated by Evaluate. |
| 2194 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 2195 | // a legal subexpression for an ICE. This return value is used to handle |
| 2196 | // the comma operator in C99 mode. |
| 2197 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 2198 | |
| 2199 | struct ICEDiag { |
| 2200 | unsigned Val; |
| 2201 | SourceLocation Loc; |
| 2202 | |
| 2203 | public: |
| 2204 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 2205 | ICEDiag() : Val(0) {} |
| 2206 | }; |
| 2207 | |
| 2208 | ICEDiag NoDiag() { return ICEDiag(); } |
| 2209 | |
| 2210 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 2211 | Expr::EvalResult EVResult; |
| 2212 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 2213 | !EVResult.Val.isInt()) { |
| 2214 | return ICEDiag(2, E->getLocStart()); |
| 2215 | } |
| 2216 | return NoDiag(); |
| 2217 | } |
| 2218 | |
| 2219 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 2220 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
| 2221 | if (!E->getType()->isIntegralType()) { |
| 2222 | return ICEDiag(2, E->getLocStart()); |
| 2223 | } |
| 2224 | |
| 2225 | switch (E->getStmtClass()) { |
| 2226 | #define STMT(Node, Base) case Expr::Node##Class: |
| 2227 | #define EXPR(Node, Base) |
| 2228 | #include "clang/AST/StmtNodes.inc" |
| 2229 | case Expr::PredefinedExprClass: |
| 2230 | case Expr::FloatingLiteralClass: |
| 2231 | case Expr::ImaginaryLiteralClass: |
| 2232 | case Expr::StringLiteralClass: |
| 2233 | case Expr::ArraySubscriptExprClass: |
| 2234 | case Expr::MemberExprClass: |
| 2235 | case Expr::CompoundAssignOperatorClass: |
| 2236 | case Expr::CompoundLiteralExprClass: |
| 2237 | case Expr::ExtVectorElementExprClass: |
| 2238 | case Expr::InitListExprClass: |
| 2239 | case Expr::DesignatedInitExprClass: |
| 2240 | case Expr::ImplicitValueInitExprClass: |
| 2241 | case Expr::ParenListExprClass: |
| 2242 | case Expr::VAArgExprClass: |
| 2243 | case Expr::AddrLabelExprClass: |
| 2244 | case Expr::StmtExprClass: |
| 2245 | case Expr::CXXMemberCallExprClass: |
| 2246 | case Expr::CXXDynamicCastExprClass: |
| 2247 | case Expr::CXXTypeidExprClass: |
| 2248 | case Expr::CXXNullPtrLiteralExprClass: |
| 2249 | case Expr::CXXThisExprClass: |
| 2250 | case Expr::CXXThrowExprClass: |
| 2251 | case Expr::CXXNewExprClass: |
| 2252 | case Expr::CXXDeleteExprClass: |
| 2253 | case Expr::CXXPseudoDestructorExprClass: |
| 2254 | case Expr::UnresolvedLookupExprClass: |
| 2255 | case Expr::DependentScopeDeclRefExprClass: |
| 2256 | case Expr::CXXConstructExprClass: |
| 2257 | case Expr::CXXBindTemporaryExprClass: |
| 2258 | case Expr::CXXBindReferenceExprClass: |
| 2259 | case Expr::CXXExprWithTemporariesClass: |
| 2260 | case Expr::CXXTemporaryObjectExprClass: |
| 2261 | case Expr::CXXUnresolvedConstructExprClass: |
| 2262 | case Expr::CXXDependentScopeMemberExprClass: |
| 2263 | case Expr::UnresolvedMemberExprClass: |
| 2264 | case Expr::ObjCStringLiteralClass: |
| 2265 | case Expr::ObjCEncodeExprClass: |
| 2266 | case Expr::ObjCMessageExprClass: |
| 2267 | case Expr::ObjCSelectorExprClass: |
| 2268 | case Expr::ObjCProtocolExprClass: |
| 2269 | case Expr::ObjCIvarRefExprClass: |
| 2270 | case Expr::ObjCPropertyRefExprClass: |
| 2271 | case Expr::ObjCImplicitSetterGetterRefExprClass: |
| 2272 | case Expr::ObjCSuperExprClass: |
| 2273 | case Expr::ObjCIsaExprClass: |
| 2274 | case Expr::ShuffleVectorExprClass: |
| 2275 | case Expr::BlockExprClass: |
| 2276 | case Expr::BlockDeclRefExprClass: |
| 2277 | case Expr::NoStmtClass: |
| 2278 | return ICEDiag(2, E->getLocStart()); |
| 2279 | |
| 2280 | case Expr::GNUNullExprClass: |
| 2281 | // GCC considers the GNU __null value to be an integral constant expression. |
| 2282 | return NoDiag(); |
| 2283 | |
| 2284 | case Expr::ParenExprClass: |
| 2285 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
| 2286 | case Expr::IntegerLiteralClass: |
| 2287 | case Expr::CharacterLiteralClass: |
| 2288 | case Expr::CXXBoolLiteralExprClass: |
| 2289 | case Expr::CXXZeroInitValueExprClass: |
| 2290 | case Expr::TypesCompatibleExprClass: |
| 2291 | case Expr::UnaryTypeTraitExprClass: |
| 2292 | return NoDiag(); |
| 2293 | case Expr::CallExprClass: |
| 2294 | case Expr::CXXOperatorCallExprClass: { |
| 2295 | const CallExpr *CE = cast<CallExpr>(E); |
| 2296 | if (CE->isBuiltinCall(Ctx)) |
| 2297 | return CheckEvalInICE(E, Ctx); |
| 2298 | return ICEDiag(2, E->getLocStart()); |
| 2299 | } |
| 2300 | case Expr::DeclRefExprClass: |
| 2301 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 2302 | return NoDiag(); |
| 2303 | if (Ctx.getLangOptions().CPlusPlus && |
| 2304 | E->getType().getCVRQualifiers() == Qualifiers::Const) { |
| 2305 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 2306 | |
| 2307 | // Parameter variables are never constants. Without this check, |
| 2308 | // getAnyInitializer() can find a default argument, which leads |
| 2309 | // to chaos. |
| 2310 | if (isa<ParmVarDecl>(D)) |
| 2311 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2312 | |
| 2313 | // C++ 7.1.5.1p2 |
| 2314 | // A variable of non-volatile const-qualified integral or enumeration |
| 2315 | // type initialized by an ICE can be used in ICEs. |
| 2316 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
| 2317 | Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers(); |
| 2318 | if (Quals.hasVolatile() || !Quals.hasConst()) |
| 2319 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2320 | |
| 2321 | // Look for a declaration of this variable that has an initializer. |
| 2322 | const VarDecl *ID = 0; |
| 2323 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 2324 | if (Init) { |
| 2325 | if (ID->isInitKnownICE()) { |
| 2326 | // We have already checked whether this subexpression is an |
| 2327 | // integral constant expression. |
| 2328 | if (ID->isInitICE()) |
| 2329 | return NoDiag(); |
| 2330 | else |
| 2331 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2332 | } |
| 2333 | |
| 2334 | // It's an ICE whether or not the definition we found is |
| 2335 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2336 | // 6206 for details. |
| 2337 | |
| 2338 | if (Dcl->isCheckingICE()) { |
| 2339 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2340 | } |
| 2341 | |
| 2342 | Dcl->setCheckingICE(); |
| 2343 | ICEDiag Result = CheckICE(Init, Ctx); |
| 2344 | // Cache the result of the ICE test. |
| 2345 | Dcl->setInitKnownICE(Result.Val == 0); |
| 2346 | return Result; |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | return ICEDiag(2, E->getLocStart()); |
| 2351 | case Expr::UnaryOperatorClass: { |
| 2352 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 2353 | switch (Exp->getOpcode()) { |
| 2354 | case UnaryOperator::PostInc: |
| 2355 | case UnaryOperator::PostDec: |
| 2356 | case UnaryOperator::PreInc: |
| 2357 | case UnaryOperator::PreDec: |
| 2358 | case UnaryOperator::AddrOf: |
| 2359 | case UnaryOperator::Deref: |
| 2360 | return ICEDiag(2, E->getLocStart()); |
| 2361 | case UnaryOperator::Extension: |
| 2362 | case UnaryOperator::LNot: |
| 2363 | case UnaryOperator::Plus: |
| 2364 | case UnaryOperator::Minus: |
| 2365 | case UnaryOperator::Not: |
| 2366 | case UnaryOperator::Real: |
| 2367 | case UnaryOperator::Imag: |
| 2368 | return CheckICE(Exp->getSubExpr(), Ctx); |
| 2369 | case UnaryOperator::OffsetOf: |
| 2370 | break; |
| 2371 | } |
| 2372 | |
| 2373 | // OffsetOf falls through here. |
| 2374 | } |
| 2375 | case Expr::OffsetOfExprClass: { |
| 2376 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 2377 | // Evaluate matches the proposed gcc behavior for cases like |
| 2378 | // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect |
| 2379 | // compliance: we should warn earlier for offsetof expressions with |
| 2380 | // array subscripts that aren't ICEs, and if the array subscripts |
| 2381 | // are ICEs, the value of the offsetof must be an integer constant. |
| 2382 | return CheckEvalInICE(E, Ctx); |
| 2383 | } |
| 2384 | case Expr::SizeOfAlignOfExprClass: { |
| 2385 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E); |
| 2386 | if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType()) |
| 2387 | return ICEDiag(2, E->getLocStart()); |
| 2388 | return NoDiag(); |
| 2389 | } |
| 2390 | case Expr::BinaryOperatorClass: { |
| 2391 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 2392 | switch (Exp->getOpcode()) { |
| 2393 | case BinaryOperator::PtrMemD: |
| 2394 | case BinaryOperator::PtrMemI: |
| 2395 | case BinaryOperator::Assign: |
| 2396 | case BinaryOperator::MulAssign: |
| 2397 | case BinaryOperator::DivAssign: |
| 2398 | case BinaryOperator::RemAssign: |
| 2399 | case BinaryOperator::AddAssign: |
| 2400 | case BinaryOperator::SubAssign: |
| 2401 | case BinaryOperator::ShlAssign: |
| 2402 | case BinaryOperator::ShrAssign: |
| 2403 | case BinaryOperator::AndAssign: |
| 2404 | case BinaryOperator::XorAssign: |
| 2405 | case BinaryOperator::OrAssign: |
| 2406 | return ICEDiag(2, E->getLocStart()); |
| 2407 | |
| 2408 | case BinaryOperator::Mul: |
| 2409 | case BinaryOperator::Div: |
| 2410 | case BinaryOperator::Rem: |
| 2411 | case BinaryOperator::Add: |
| 2412 | case BinaryOperator::Sub: |
| 2413 | case BinaryOperator::Shl: |
| 2414 | case BinaryOperator::Shr: |
| 2415 | case BinaryOperator::LT: |
| 2416 | case BinaryOperator::GT: |
| 2417 | case BinaryOperator::LE: |
| 2418 | case BinaryOperator::GE: |
| 2419 | case BinaryOperator::EQ: |
| 2420 | case BinaryOperator::NE: |
| 2421 | case BinaryOperator::And: |
| 2422 | case BinaryOperator::Xor: |
| 2423 | case BinaryOperator::Or: |
| 2424 | case BinaryOperator::Comma: { |
| 2425 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 2426 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 2427 | if (Exp->getOpcode() == BinaryOperator::Div || |
| 2428 | Exp->getOpcode() == BinaryOperator::Rem) { |
| 2429 | // Evaluate gives an error for undefined Div/Rem, so make sure |
| 2430 | // we don't evaluate one. |
| 2431 | if (LHSResult.Val != 2 && RHSResult.Val != 2) { |
| 2432 | llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); |
| 2433 | if (REval == 0) |
| 2434 | return ICEDiag(1, E->getLocStart()); |
| 2435 | if (REval.isSigned() && REval.isAllOnesValue()) { |
| 2436 | llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); |
| 2437 | if (LEval.isMinSignedValue()) |
| 2438 | return ICEDiag(1, E->getLocStart()); |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | if (Exp->getOpcode() == BinaryOperator::Comma) { |
| 2443 | if (Ctx.getLangOptions().C99) { |
| 2444 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 2445 | // if it isn't evaluated. |
| 2446 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 2447 | return ICEDiag(1, E->getLocStart()); |
| 2448 | } else { |
| 2449 | // In both C89 and C++, commas in ICEs are illegal. |
| 2450 | return ICEDiag(2, E->getLocStart()); |
| 2451 | } |
| 2452 | } |
| 2453 | if (LHSResult.Val >= RHSResult.Val) |
| 2454 | return LHSResult; |
| 2455 | return RHSResult; |
| 2456 | } |
| 2457 | case BinaryOperator::LAnd: |
| 2458 | case BinaryOperator::LOr: { |
| 2459 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 2460 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 2461 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 2462 | // Rare case where the RHS has a comma "side-effect"; we need |
| 2463 | // to actually check the condition to see whether the side |
| 2464 | // with the comma is evaluated. |
| 2465 | if ((Exp->getOpcode() == BinaryOperator::LAnd) != |
| 2466 | (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) |
| 2467 | return RHSResult; |
| 2468 | return NoDiag(); |
| 2469 | } |
| 2470 | |
| 2471 | if (LHSResult.Val >= RHSResult.Val) |
| 2472 | return LHSResult; |
| 2473 | return RHSResult; |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | case Expr::ImplicitCastExprClass: |
| 2478 | case Expr::CStyleCastExprClass: |
| 2479 | case Expr::CXXFunctionalCastExprClass: |
| 2480 | case Expr::CXXStaticCastExprClass: |
| 2481 | case Expr::CXXReinterpretCastExprClass: |
| 2482 | case Expr::CXXConstCastExprClass: { |
| 2483 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 2484 | if (SubExpr->getType()->isIntegralType()) |
| 2485 | return CheckICE(SubExpr, Ctx); |
| 2486 | if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) |
| 2487 | return NoDiag(); |
| 2488 | return ICEDiag(2, E->getLocStart()); |
| 2489 | } |
| 2490 | case Expr::ConditionalOperatorClass: { |
| 2491 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 2492 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 2493 | // then only the true side is actually considered in an integer constant |
| 2494 | // expression, and it is fully evaluated. This is an important GNU |
| 2495 | // extension. See GCC PR38377 for discussion. |
| 2496 | if (const CallExpr *CallCE |
| 2497 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
| 2498 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
| 2499 | Expr::EvalResult EVResult; |
| 2500 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 2501 | !EVResult.Val.isInt()) { |
| 2502 | return ICEDiag(2, E->getLocStart()); |
| 2503 | } |
| 2504 | return NoDiag(); |
| 2505 | } |
| 2506 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
| 2507 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 2508 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 2509 | if (CondResult.Val == 2) |
| 2510 | return CondResult; |
| 2511 | if (TrueResult.Val == 2) |
| 2512 | return TrueResult; |
| 2513 | if (FalseResult.Val == 2) |
| 2514 | return FalseResult; |
| 2515 | if (CondResult.Val == 1) |
| 2516 | return CondResult; |
| 2517 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 2518 | return NoDiag(); |
| 2519 | // Rare case where the diagnostics depend on which side is evaluated |
| 2520 | // Note that if we get here, CondResult is 0, and at least one of |
| 2521 | // TrueResult and FalseResult is non-zero. |
| 2522 | if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { |
| 2523 | return FalseResult; |
| 2524 | } |
| 2525 | return TrueResult; |
| 2526 | } |
| 2527 | case Expr::CXXDefaultArgExprClass: |
| 2528 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 2529 | case Expr::ChooseExprClass: { |
| 2530 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | // Silence a GCC warning |
| 2535 | return ICEDiag(2, E->getLocStart()); |
| 2536 | } |
| 2537 | |
| 2538 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 2539 | SourceLocation *Loc, bool isEvaluated) const { |
| 2540 | ICEDiag d = CheckICE(this, Ctx); |
| 2541 | if (d.Val != 0) { |
| 2542 | if (Loc) *Loc = d.Loc; |
| 2543 | return false; |
| 2544 | } |
| 2545 | EvalResult EvalResult; |
| 2546 | if (!Evaluate(EvalResult, Ctx)) |
| 2547 | llvm_unreachable("ICE cannot be evaluated!"); |
| 2548 | assert(!EvalResult.HasSideEffects && "ICE with side effects!"); |
| 2549 | assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); |
| 2550 | Result = EvalResult.Val.getInt(); |
| 2551 | return true; |
| 2552 | } |