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