Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 28 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 29 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 30 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 31 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 32 | /// information about a subexpression as it is folded. It retains information |
| 33 | /// about the AST context, but also maintains information about the folded |
| 34 | /// expression. |
| 35 | /// |
| 36 | /// If an expression could be evaluated, it is still possible it is not a C |
| 37 | /// "integer constant expression" or constant expression. If not, this struct |
| 38 | /// captures information about how and why not. |
| 39 | /// |
| 40 | /// One bit of information passed *into* the request for constant folding |
| 41 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 42 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 43 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 44 | /// certain things in certain situations. |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 45 | namespace { |
Benjamin Kramer | 024e619 | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 46 | struct EvalInfo { |
| 47 | const ASTContext &Ctx; |
| 48 | |
| 49 | /// EvalResult - Contains information about the evaluation. |
| 50 | Expr::EvalResult &EvalResult; |
| 51 | |
| 52 | typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy; |
| 53 | MapTy OpaqueValues; |
| 54 | const APValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 55 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 56 | if (i == OpaqueValues.end()) return 0; |
| 57 | return &i->second; |
| 58 | } |
| 59 | |
| 60 | EvalInfo(const ASTContext &ctx, Expr::EvalResult &evalresult) |
| 61 | : Ctx(ctx), EvalResult(evalresult) {} |
| 62 | }; |
| 63 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 64 | struct ComplexValue { |
| 65 | private: |
| 66 | bool IsInt; |
| 67 | |
| 68 | public: |
| 69 | APSInt IntReal, IntImag; |
| 70 | APFloat FloatReal, FloatImag; |
| 71 | |
| 72 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 73 | |
| 74 | void makeComplexFloat() { IsInt = false; } |
| 75 | bool isComplexFloat() const { return !IsInt; } |
| 76 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 77 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 78 | |
| 79 | void makeComplexInt() { IsInt = true; } |
| 80 | bool isComplexInt() const { return IsInt; } |
| 81 | APSInt &getComplexIntReal() { return IntReal; } |
| 82 | APSInt &getComplexIntImag() { return IntImag; } |
| 83 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 84 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 85 | if (isComplexFloat()) |
| 86 | v = APValue(FloatReal, FloatImag); |
| 87 | else |
| 88 | v = APValue(IntReal, IntImag); |
| 89 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 90 | void setFrom(const APValue &v) { |
| 91 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 92 | if (v.isComplexFloat()) { |
| 93 | makeComplexFloat(); |
| 94 | FloatReal = v.getComplexFloatReal(); |
| 95 | FloatImag = v.getComplexFloatImag(); |
| 96 | } else { |
| 97 | makeComplexInt(); |
| 98 | IntReal = v.getComplexIntReal(); |
| 99 | IntImag = v.getComplexIntImag(); |
| 100 | } |
| 101 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 102 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 103 | |
| 104 | struct LValue { |
| 105 | Expr *Base; |
| 106 | CharUnits Offset; |
| 107 | |
| 108 | Expr *getLValueBase() { return Base; } |
| 109 | CharUnits getLValueOffset() { return Offset; } |
| 110 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 111 | void moveInto(APValue &v) const { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 112 | v = APValue(Base, Offset); |
| 113 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 114 | void setFrom(const APValue &v) { |
| 115 | assert(v.isLValue()); |
| 116 | Base = v.getLValueBase(); |
| 117 | Offset = v.getLValueOffset(); |
| 118 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 119 | }; |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 121 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 122 | static bool Evaluate(EvalInfo &info, const Expr *E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 123 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 124 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 125 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 126 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| 127 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 128 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 129 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 130 | |
| 131 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 132 | // Misc utilities |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 135 | static bool IsGlobalLValue(const Expr* E) { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 136 | if (!E) return true; |
| 137 | |
| 138 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 139 | if (isa<FunctionDecl>(DRE->getDecl())) |
| 140 | return true; |
| 141 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 142 | return VD->hasGlobalStorage(); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E)) |
| 147 | return CLE->isFileScope(); |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 152 | static bool EvalPointerValueAsBool(LValue& Value, bool& Result) { |
| 153 | const Expr* Base = Value.Base; |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 154 | |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 155 | // A null base expression indicates a null pointer. These are always |
| 156 | // evaluatable, and they are false unless the offset is zero. |
| 157 | if (!Base) { |
| 158 | Result = !Value.Offset.isZero(); |
| 159 | return true; |
| 160 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 161 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 162 | // Require the base expression to be a global l-value. |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 163 | if (!IsGlobalLValue(Base)) return false; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 164 | |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 165 | // We have a non-null base expression. These are generally known to |
| 166 | // be true, but if it'a decl-ref to a weak symbol it can be null at |
| 167 | // runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 168 | Result = true; |
| 169 | |
| 170 | const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base); |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 171 | if (!DeclRef) |
| 172 | return true; |
| 173 | |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 174 | // If it's a weak symbol, it isn't constant-evaluable. |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 175 | const ValueDecl* Decl = DeclRef->getDecl(); |
| 176 | if (Decl->hasAttr<WeakAttr>() || |
| 177 | Decl->hasAttr<WeakRefAttr>() || |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 178 | Decl->isWeakImported()) |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 179 | return false; |
| 180 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 184 | static bool HandleConversionToBool(const Expr* E, bool& Result, |
| 185 | EvalInfo &Info) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 186 | if (E->getType()->isIntegralOrEnumerationType()) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 187 | APSInt IntResult; |
| 188 | if (!EvaluateInteger(E, IntResult, Info)) |
| 189 | return false; |
| 190 | Result = IntResult != 0; |
| 191 | return true; |
| 192 | } else if (E->getType()->isRealFloatingType()) { |
| 193 | APFloat FloatResult(0.0); |
| 194 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 195 | return false; |
| 196 | Result = !FloatResult.isZero(); |
| 197 | return true; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 198 | } else if (E->getType()->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 199 | LValue PointerResult; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 200 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 201 | return false; |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 202 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 203 | } else if (E->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 204 | ComplexValue ComplexResult; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 205 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 206 | return false; |
| 207 | if (ComplexResult.isComplexFloat()) { |
| 208 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 209 | !ComplexResult.getComplexFloatImag().isZero(); |
| 210 | } else { |
| 211 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 212 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 213 | } |
| 214 | return true; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 221 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 222 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 223 | // Determine whether we are converting to unsigned or signed. |
| 224 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 226 | // FIXME: Warning for overflow. |
| 227 | uint64_t Space[4]; |
| 228 | bool ignored; |
| 229 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 230 | llvm::APFloat::rmTowardZero, &ignored); |
| 231 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 232 | } |
| 233 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 235 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 236 | bool ignored; |
| 237 | APFloat Result = Value; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 239 | APFloat::rmNearestTiesToEven, &ignored); |
| 240 | return Result; |
| 241 | } |
| 242 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 244 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 245 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 246 | APSInt Result = Value; |
| 247 | // Figure out if this is a truncate, extend or noop cast. |
| 248 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 249 | Result = Result.extOrTrunc(DestWidth); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 250 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 251 | return Result; |
| 252 | } |
| 253 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 255 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 256 | |
| 257 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 258 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 259 | APFloat::rmNearestTiesToEven); |
| 260 | return Result; |
| 261 | } |
| 262 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 263 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 264 | class HasSideEffect |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 265 | : public StmtVisitor<HasSideEffect, bool> { |
| 266 | EvalInfo &Info; |
| 267 | public: |
| 268 | |
| 269 | HasSideEffect(EvalInfo &info) : Info(info) {} |
| 270 | |
| 271 | // Unhandled nodes conservatively default to having side effects. |
| 272 | bool VisitStmt(Stmt *S) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 277 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 278 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 279 | return true; |
| 280 | return false; |
| 281 | } |
| 282 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 283 | // a ton of code. |
| 284 | bool VisitBlockExpr(BlockExpr *E) { return true; } |
| 285 | bool VisitPredefinedExpr(PredefinedExpr *E) { return false; } |
| 286 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 287 | { return Visit(E->getInitializer()); } |
| 288 | bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); } |
| 289 | bool VisitIntegerLiteral(IntegerLiteral *E) { return false; } |
| 290 | bool VisitFloatingLiteral(FloatingLiteral *E) { return false; } |
| 291 | bool VisitStringLiteral(StringLiteral *E) { return false; } |
| 292 | bool VisitCharacterLiteral(CharacterLiteral *E) { return false; } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 293 | bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) |
| 294 | { return false; } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 295 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 296 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 297 | bool VisitChooseExpr(ChooseExpr *E) |
| 298 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 299 | bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); } |
| 300 | bool VisitBinAssign(BinaryOperator *E) { return true; } |
Mike Stump | f3eb5ec | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 301 | bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; } |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 302 | bool VisitBinaryOperator(BinaryOperator *E) |
| 303 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 304 | bool VisitUnaryPreInc(UnaryOperator *E) { return true; } |
| 305 | bool VisitUnaryPostInc(UnaryOperator *E) { return true; } |
| 306 | bool VisitUnaryPreDec(UnaryOperator *E) { return true; } |
| 307 | bool VisitUnaryPostDec(UnaryOperator *E) { return true; } |
| 308 | bool VisitUnaryDeref(UnaryOperator *E) { |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 309 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 310 | return true; |
Mike Stump | fa50290 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 311 | return Visit(E->getSubExpr()); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 312 | } |
| 313 | bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | a067942 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 314 | |
| 315 | // Has side effects if any element does. |
| 316 | bool VisitInitListExpr(InitListExpr *E) { |
| 317 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 318 | if (Visit(E->getInit(i))) return true; |
| 319 | return false; |
| 320 | } |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 321 | |
| 322 | bool VisitSizeOfPackExpr(SizeOfPackExpr *) { return false; } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 325 | class OpaqueValueEvaluation { |
| 326 | EvalInfo &info; |
| 327 | OpaqueValueExpr *opaqueValue; |
| 328 | |
| 329 | public: |
| 330 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 331 | Expr *value) |
| 332 | : info(info), opaqueValue(opaqueValue) { |
| 333 | |
| 334 | // If evaluation fails, fail immediately. |
| 335 | if (!Evaluate(info, value)) { |
| 336 | this->opaqueValue = 0; |
| 337 | return; |
| 338 | } |
| 339 | info.OpaqueValues[opaqueValue] = info.EvalResult.Val; |
| 340 | } |
| 341 | |
| 342 | bool hasError() const { return opaqueValue == 0; } |
| 343 | |
| 344 | ~OpaqueValueEvaluation() { |
| 345 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 346 | } |
| 347 | }; |
| 348 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 349 | } // end anonymous namespace |
| 350 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 351 | //===----------------------------------------------------------------------===// |
| 352 | // LValue Evaluation |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 355 | class LValueExprEvaluator |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 356 | : public StmtVisitor<LValueExprEvaluator, bool> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 357 | EvalInfo &Info; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 358 | LValue &Result; |
| 359 | |
| 360 | bool Success(Expr *E) { |
| 361 | Result.Base = E; |
| 362 | Result.Offset = CharUnits::Zero(); |
| 363 | return true; |
| 364 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 365 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 367 | LValueExprEvaluator(EvalInfo &info, LValue &Result) : |
| 368 | Info(info), Result(Result) {} |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 369 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 370 | bool VisitStmt(Stmt *S) { |
| 371 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 372 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 373 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 374 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 375 | bool VisitDeclRefExpr(DeclRefExpr *E); |
| 376 | bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); } |
| 377 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 378 | bool VisitMemberExpr(MemberExpr *E); |
| 379 | bool VisitStringLiteral(StringLiteral *E) { return Success(E); } |
| 380 | bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); } |
| 381 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 382 | bool VisitUnaryDeref(UnaryOperator *E); |
| 383 | bool VisitUnaryExtension(const UnaryOperator *E) |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 384 | { return Visit(E->getSubExpr()); } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 385 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 386 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 387 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 388 | bool VisitCastExpr(CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 389 | switch (E->getCastKind()) { |
| 390 | default: |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 391 | return false; |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 392 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 393 | case CK_NoOp: |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 394 | return Visit(E->getSubExpr()); |
| 395 | } |
| 396 | } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 397 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 398 | }; |
| 399 | } // end anonymous namespace |
| 400 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 401 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
| 402 | return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 403 | } |
| 404 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 405 | bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 406 | if (isa<FunctionDecl>(E->getDecl())) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 407 | return Success(E); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 408 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
| 409 | if (!VD->getType()->isReferenceType()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 410 | return Success(E); |
Chandler Carruth | e299ba6 | 2010-05-16 09:32:51 +0000 | [diff] [blame] | 411 | // Reference parameters can refer to anything even if they have an |
| 412 | // "initializer" in the form of a default argument. |
| 413 | if (isa<ParmVarDecl>(VD)) |
| 414 | return false; |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 415 | // FIXME: Check whether VD might be overridden! |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 416 | if (const Expr *Init = VD->getAnyInitializer()) |
Douglas Gregor | 0840cc0 | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 417 | return Visit(const_cast<Expr *>(Init)); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 418 | } |
| 419 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 420 | return false; |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 421 | } |
| 422 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 423 | bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 424 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 425 | } |
| 426 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 427 | bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 428 | QualType Ty; |
| 429 | if (E->isArrow()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 430 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 431 | return false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 432 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 433 | } else { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 434 | if (!Visit(E->getBase())) |
| 435 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 436 | Ty = E->getBase()->getType(); |
| 437 | } |
| 438 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 439 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 440 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 441 | |
| 442 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 443 | if (!FD) // FIXME: deal with other kinds of member expressions |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 444 | return false; |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 445 | |
| 446 | if (FD->getType()->isReferenceType()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 447 | return false; |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 448 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 449 | // FIXME: This is linear time. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 450 | unsigned i = 0; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 451 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 452 | FieldEnd = RD->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 453 | Field != FieldEnd; (void)++Field, ++i) { |
| 454 | if (*Field == FD) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 458 | Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 459 | return true; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 460 | } |
| 461 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 462 | bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 463 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 464 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 466 | APSInt Index; |
| 467 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 468 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 469 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 470 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 471 | Result.Offset += Index.getSExtValue() * ElementSize; |
| 472 | return true; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 473 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 474 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 475 | bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
| 476 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 479 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 480 | // Pointer Evaluation |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 483 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 484 | class PointerExprEvaluator |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 485 | : public StmtVisitor<PointerExprEvaluator, bool> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 486 | EvalInfo &Info; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 487 | LValue &Result; |
| 488 | |
| 489 | bool Success(Expr *E) { |
| 490 | Result.Base = E; |
| 491 | Result.Offset = CharUnits::Zero(); |
| 492 | return true; |
| 493 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 494 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 496 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
| 497 | : Info(info), Result(Result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 498 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 499 | bool VisitStmt(Stmt *S) { |
| 500 | return false; |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 501 | } |
| 502 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 503 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 504 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 505 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 506 | bool VisitCastExpr(CastExpr* E); |
| 507 | bool VisitUnaryExtension(const UnaryOperator *E) |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 508 | { return Visit(E->getSubExpr()); } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 509 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 510 | bool VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 511 | { return Success(E); } |
| 512 | bool VisitAddrLabelExpr(AddrLabelExpr *E) |
| 513 | { return Success(E); } |
| 514 | bool VisitCallExpr(CallExpr *E); |
| 515 | bool VisitBlockExpr(BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 516 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 517 | return Success(E); |
| 518 | return false; |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 519 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 520 | bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
| 521 | { return Success((Expr*)0); } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 522 | bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 523 | bool VisitConditionalOperator(ConditionalOperator *E); |
| 524 | bool VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 525 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 526 | bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
| 527 | { return Success((Expr*)0); } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 528 | |
| 529 | bool VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 530 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 531 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 532 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 533 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 534 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 535 | assert(E->getType()->hasPointerRepresentation()); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 536 | return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 537 | } |
| 538 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 539 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 540 | if (E->getOpcode() != BO_Add && |
| 541 | E->getOpcode() != BO_Sub) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 542 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 544 | const Expr *PExp = E->getLHS(); |
| 545 | const Expr *IExp = E->getRHS(); |
| 546 | if (IExp->getType()->isPointerType()) |
| 547 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 549 | if (!EvaluatePointer(PExp, Result, Info)) |
| 550 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 552 | llvm::APSInt Offset; |
| 553 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 554 | return false; |
| 555 | int64_t AdditionalOffset |
| 556 | = Offset.isSigned() ? Offset.getSExtValue() |
| 557 | : static_cast<int64_t>(Offset.getZExtValue()); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 4c43e31 | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 559 | // Compute the new offset in the appropriate width. |
| 560 | |
| 561 | QualType PointeeType = |
| 562 | PExp->getType()->getAs<PointerType>()->getPointeeType(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 563 | CharUnits SizeOfPointee; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 565 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 566 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 567 | SizeOfPointee = CharUnits::One(); |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 568 | else |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 569 | SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 570 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 571 | if (E->getOpcode() == BO_Add) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 572 | Result.Offset += AdditionalOffset * SizeOfPointee; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 573 | else |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 574 | Result.Offset -= AdditionalOffset * SizeOfPointee; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 575 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 576 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 577 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 578 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 579 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 580 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 583 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 584 | bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) { |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 585 | Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 586 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 587 | switch (E->getCastKind()) { |
| 588 | default: |
| 589 | break; |
| 590 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 591 | case CK_NoOp: |
| 592 | case CK_BitCast: |
| 593 | case CK_LValueBitCast: |
| 594 | case CK_AnyPointerToObjCPointerCast: |
| 595 | case CK_AnyPointerToBlockPointerCast: |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 596 | return Visit(SubExpr); |
| 597 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 598 | case CK_DerivedToBase: |
| 599 | case CK_UncheckedDerivedToBase: { |
| 600 | LValue BaseLV; |
| 601 | if (!EvaluatePointer(E->getSubExpr(), BaseLV, Info)) |
| 602 | return false; |
| 603 | |
| 604 | // Now figure out the necessary offset to add to the baseLV to get from |
| 605 | // the derived class to the base class. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 606 | CharUnits Offset = CharUnits::Zero(); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 607 | |
| 608 | QualType Ty = E->getSubExpr()->getType(); |
| 609 | const CXXRecordDecl *DerivedDecl = |
| 610 | Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl(); |
| 611 | |
| 612 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 613 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 614 | const CXXBaseSpecifier *Base = *PathI; |
| 615 | |
| 616 | // FIXME: If the base is virtual, we'd need to determine the type of the |
| 617 | // most derived class and we don't support that right now. |
| 618 | if (Base->isVirtual()) |
| 619 | return false; |
| 620 | |
| 621 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 622 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 623 | |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 624 | Offset += Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 625 | DerivedDecl = BaseDecl; |
| 626 | } |
| 627 | |
| 628 | Result.Base = BaseLV.getLValueBase(); |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 629 | Result.Offset = BaseLV.getLValueOffset() + Offset; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 630 | return true; |
| 631 | } |
| 632 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 633 | case CK_NullToPointer: { |
| 634 | Result.Base = 0; |
| 635 | Result.Offset = CharUnits::Zero(); |
| 636 | return true; |
| 637 | } |
| 638 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 639 | case CK_IntegralToPointer: { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 640 | APValue Value; |
| 641 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 642 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 643 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 644 | if (Value.isInt()) { |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 645 | Value.getInt() = Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 646 | Result.Base = 0; |
| 647 | Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue()); |
| 648 | return true; |
| 649 | } else { |
| 650 | // Cast is of an lvalue, no need to change value. |
| 651 | Result.Base = Value.getLValueBase(); |
| 652 | Result.Offset = Value.getLValueOffset(); |
| 653 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 656 | case CK_ArrayToPointerDecay: |
| 657 | case CK_FunctionToPointerDecay: |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 658 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 659 | } |
| 660 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 661 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 663 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 664 | bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | if (E->isBuiltinCall(Info.Ctx) == |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 666 | Builtin::BI__builtin___CFStringMakeConstantString || |
| 667 | E->isBuiltinCall(Info.Ctx) == |
| 668 | Builtin::BI__builtin___NSStringMakeConstantString) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 669 | return Success(E); |
| 670 | return false; |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 671 | } |
| 672 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 673 | bool PointerExprEvaluator::VisitOpaqueValueExpr(OpaqueValueExpr *e) { |
| 674 | const APValue *value = Info.getOpaqueValue(e); |
| 675 | if (!value) |
| 676 | return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false); |
| 677 | Result.setFrom(*value); |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | bool PointerExprEvaluator:: |
| 682 | VisitBinaryConditionalOperator(BinaryConditionalOperator *e) { |
| 683 | OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon()); |
| 684 | if (opaque.hasError()) return false; |
| 685 | |
| 686 | bool cond; |
| 687 | if (!HandleConversionToBool(e->getCond(), cond, Info)) |
| 688 | return false; |
| 689 | |
| 690 | return Visit(cond ? e->getTrueExpr() : e->getFalseExpr()); |
| 691 | } |
| 692 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 693 | bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 694 | bool BoolResult; |
| 695 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 696 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 697 | |
| 698 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 699 | return Visit(EvalExpr); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 700 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 701 | |
| 702 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 703 | // Vector Evaluation |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
| 706 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 707 | class VectorExprEvaluator |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 708 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 709 | EvalInfo &Info; |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 710 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 711 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 713 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 715 | APValue VisitStmt(Stmt *S) { |
| 716 | return APValue(); |
| 717 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 719 | APValue VisitParenExpr(ParenExpr *E) |
| 720 | { return Visit(E->getSubExpr()); } |
| 721 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 722 | { return Visit(E->getSubExpr()); } |
| 723 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 724 | { return Visit(E->getSubExpr()); } |
| 725 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 726 | { return Visit(E->getSubExpr()); } |
| 727 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 728 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 729 | APValue VisitCastExpr(const CastExpr* E); |
| 730 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 731 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 732 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 733 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 734 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 735 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 736 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 737 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 738 | // shufflevector, ExtVectorElementExpr |
| 739 | // (Note that these require implementing conversions |
| 740 | // between vector types.) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 741 | }; |
| 742 | } // end anonymous namespace |
| 743 | |
| 744 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 745 | if (!E->getType()->isVectorType()) |
| 746 | return false; |
| 747 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 748 | return !Result.isUninit(); |
| 749 | } |
| 750 | |
| 751 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 752 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 753 | QualType EltTy = VTy->getElementType(); |
| 754 | unsigned NElts = VTy->getNumElements(); |
| 755 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 757 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 758 | QualType SETy = SE->getType(); |
| 759 | APValue Result = APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 760 | |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 761 | // Check for vector->vector bitcast and scalar->vector splat. |
| 762 | if (SETy->isVectorType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 763 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 764 | } else if (SETy->isIntegerType()) { |
| 765 | APSInt IntResult; |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 766 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 767 | return APValue(); |
| 768 | Result = APValue(IntResult); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 769 | } else if (SETy->isRealFloatingType()) { |
| 770 | APFloat F(0.0); |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 771 | if (!EvaluateFloat(SE, F, Info)) |
| 772 | return APValue(); |
| 773 | Result = APValue(F); |
| 774 | } else |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 775 | return APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 776 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 777 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 778 | // and splat it to all elements. |
| 779 | if (E->getType()->isExtVectorType()) { |
| 780 | if (EltTy->isIntegerType() && Result.isInt()) |
| 781 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 782 | Info.Ctx)); |
| 783 | else if (EltTy->isIntegerType()) |
| 784 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 785 | Info.Ctx)); |
| 786 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 787 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 788 | Info.Ctx)); |
| 789 | else if (EltTy->isRealFloatingType()) |
| 790 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 791 | Info.Ctx)); |
| 792 | else |
| 793 | return APValue(); |
| 794 | |
| 795 | // Splat and create vector APValue. |
| 796 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 797 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 798 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 799 | |
| 800 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 801 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 802 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 803 | // element. |
| 804 | APSInt Init; |
| 805 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 807 | llvm::SmallVector<APValue, 4> Elts; |
| 808 | for (unsigned i = 0; i != NElts; ++i) { |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 809 | APSInt Tmp = Init.extOrTrunc(EltWidth); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 811 | if (EltTy->isIntegerType()) |
| 812 | Elts.push_back(APValue(Tmp)); |
| 813 | else if (EltTy->isRealFloatingType()) |
| 814 | Elts.push_back(APValue(APFloat(Tmp))); |
| 815 | else |
| 816 | return APValue(); |
| 817 | |
| 818 | Init >>= EltWidth; |
| 819 | } |
| 820 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 824 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 825 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 826 | } |
| 827 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 829 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 830 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 831 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 832 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 834 | QualType EltTy = VT->getElementType(); |
| 835 | llvm::SmallVector<APValue, 4> Elements; |
| 836 | |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 837 | // If a vector is initialized with a single element, that value |
| 838 | // becomes every element of the vector, not just the first. |
| 839 | // This is the behavior described in the IBM AltiVec documentation. |
| 840 | if (NumInits == 1) { |
| 841 | APValue InitValue; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 842 | if (EltTy->isIntegerType()) { |
| 843 | llvm::APSInt sInt(32); |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 844 | if (!EvaluateInteger(E->getInit(0), sInt, Info)) |
| 845 | return APValue(); |
| 846 | InitValue = APValue(sInt); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 847 | } else { |
| 848 | llvm::APFloat f(0.0); |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 849 | if (!EvaluateFloat(E->getInit(0), f, Info)) |
| 850 | return APValue(); |
| 851 | InitValue = APValue(f); |
| 852 | } |
| 853 | for (unsigned i = 0; i < NumElements; i++) { |
| 854 | Elements.push_back(InitValue); |
| 855 | } |
| 856 | } else { |
| 857 | for (unsigned i = 0; i < NumElements; i++) { |
| 858 | if (EltTy->isIntegerType()) { |
| 859 | llvm::APSInt sInt(32); |
| 860 | if (i < NumInits) { |
| 861 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 862 | return APValue(); |
| 863 | } else { |
| 864 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 865 | } |
| 866 | Elements.push_back(APValue(sInt)); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 867 | } else { |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 868 | llvm::APFloat f(0.0); |
| 869 | if (i < NumInits) { |
| 870 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 871 | return APValue(); |
| 872 | } else { |
| 873 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 874 | } |
| 875 | Elements.push_back(APValue(f)); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 876 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | return APValue(&Elements[0], Elements.size()); |
| 880 | } |
| 881 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | APValue |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 883 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 884 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 885 | QualType EltTy = VT->getElementType(); |
| 886 | APValue ZeroElement; |
| 887 | if (EltTy->isIntegerType()) |
| 888 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 889 | else |
| 890 | ZeroElement = |
| 891 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 892 | |
| 893 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 894 | return APValue(&Elements[0], Elements.size()); |
| 895 | } |
| 896 | |
| 897 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 898 | bool BoolResult; |
| 899 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 900 | return APValue(); |
| 901 | |
| 902 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 903 | |
| 904 | APValue Result; |
| 905 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 906 | return Result; |
| 907 | return APValue(); |
| 908 | } |
| 909 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 910 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 911 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 912 | Info.EvalResult.HasSideEffects = true; |
| 913 | return GetZeroVector(E->getType()); |
| 914 | } |
| 915 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 916 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 917 | // Integer Evaluation |
| 918 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 919 | |
| 920 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 921 | class IntExprEvaluator |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 922 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 923 | EvalInfo &Info; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 924 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 925 | public: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 926 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 927 | : Info(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 928 | |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 929 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 930 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 931 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 932 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 933 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 934 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 935 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 936 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 937 | return true; |
| 938 | } |
| 939 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 940 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 941 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 942 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 943 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 944 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 945 | Result = APValue(APSInt(I)); |
| 946 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 947 | return true; |
| 948 | } |
| 949 | |
| 950 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 951 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 952 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 953 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 954 | return true; |
| 955 | } |
| 956 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 957 | bool Success(CharUnits Size, const Expr *E) { |
| 958 | return Success(Size.getQuantity(), E); |
| 959 | } |
| 960 | |
| 961 | |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 962 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 963 | // Take the first error. |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 964 | if (Info.EvalResult.Diag == 0) { |
| 965 | Info.EvalResult.DiagLoc = L; |
| 966 | Info.EvalResult.Diag = D; |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 967 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 969 | return false; |
Chris Lattner | ae8cc15 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 970 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 972 | //===--------------------------------------------------------------------===// |
| 973 | // Visitor Methods |
| 974 | //===--------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 976 | bool VisitStmt(Stmt *) { |
| 977 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 978 | return false; |
| 979 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 981 | bool VisitExpr(Expr *E) { |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 982 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 983 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 985 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 987 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 988 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 989 | } |
| 990 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 991 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 992 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 993 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 994 | bool VisitOpaqueValueExpr(OpaqueValueExpr *e) { |
| 995 | const APValue *value = Info.getOpaqueValue(e); |
| 996 | if (!value) { |
| 997 | if (e->getSourceExpr()) return Visit(e->getSourceExpr()); |
| 998 | return Error(e->getExprLoc(), diag::note_invalid_subexpr_in_ice, e); |
| 999 | } |
| 1000 | return Success(value->getInt(), e); |
| 1001 | } |
| 1002 | |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1003 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 1004 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 1005 | return CheckReferencedDecl(E, E->getDecl()); |
| 1006 | } |
| 1007 | bool VisitMemberExpr(const MemberExpr *E) { |
| 1008 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
| 1009 | // Conservatively assume a MemberExpr will have side-effects |
| 1010 | Info.EvalResult.HasSideEffects = true; |
| 1011 | return true; |
| 1012 | } |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 1016 | bool VisitCallExpr(CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1017 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1018 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1019 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1020 | bool VisitConditionalOperator(const ConditionalOperator *E); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1021 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1023 | bool VisitCastExpr(CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1024 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1025 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1026 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1027 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 1030 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1031 | return Success(0, E); |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 1032 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 1034 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1035 | return Success(0, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1038 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 1039 | return Success(0, E); |
| 1040 | } |
| 1041 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1042 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 1043 | return Success(E->getValue(), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1046 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 1047 | return Success(E->getValue(), E); |
| 1048 | } |
| 1049 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1050 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 1051 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 1052 | } |
| 1053 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1054 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1055 | bool VisitUnaryImag(const UnaryOperator *E); |
| 1056 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1057 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1058 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 1059 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1060 | private: |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1061 | CharUnits GetAlignOfExpr(const Expr *E); |
| 1062 | CharUnits GetAlignOfType(QualType T); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1063 | static QualType GetObjectType(const Expr *E); |
| 1064 | bool TryEvaluateBuiltinObjectSize(CallExpr *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1065 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1066 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1067 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1068 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1069 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1070 | assert(E->getType()->isIntegralOrEnumerationType()); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1071 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1072 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1073 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1074 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1075 | assert(E->getType()->isIntegralOrEnumerationType()); |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 1076 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1077 | APValue Val; |
| 1078 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 1079 | return false; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1080 | Result = Val.getInt(); |
| 1081 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1082 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1083 | |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1084 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1085 | // Enums are integer constant exprs. |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 1086 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 1087 | return Success(ECD->getInitVal(), E); |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 1088 | |
| 1089 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | 29f80c3 | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 1090 | // In C, they can also be folded, although they are not ICEs. |
Douglas Gregor | 0840cc0 | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 1091 | if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers() |
| 1092 | == Qualifiers::Const) { |
Anders Carlsson | b0695ef | 2010-02-03 21:58:41 +0000 | [diff] [blame] | 1093 | |
| 1094 | if (isa<ParmVarDecl>(D)) |
| 1095 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1096 | |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1097 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1098 | if (const Expr *Init = VD->getAnyInitializer()) { |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 1099 | if (APValue *V = VD->getEvaluatedValue()) { |
| 1100 | if (V->isInt()) |
| 1101 | return Success(V->getInt(), E); |
| 1102 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1103 | } |
| 1104 | |
| 1105 | if (VD->isEvaluatingValue()) |
| 1106 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1107 | |
| 1108 | VD->setEvaluatingValue(); |
| 1109 | |
Eli Friedman | 0b1fbd1 | 2010-09-06 00:10:32 +0000 | [diff] [blame] | 1110 | Expr::EvalResult EResult; |
| 1111 | if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects && |
| 1112 | EResult.Val.isInt()) { |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1113 | // Cache the evaluated value in the variable declaration. |
Eli Friedman | 0b1fbd1 | 2010-09-06 00:10:32 +0000 | [diff] [blame] | 1114 | Result = EResult.Val; |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 1115 | VD->setEvaluatedValue(Result); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1116 | return true; |
| 1117 | } |
| 1118 | |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 1119 | VD->setEvaluatedValue(APValue()); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1120 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1124 | // Otherwise, random variable references are not constants. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1125 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1128 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 1129 | /// as GCC. |
| 1130 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 1131 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1132 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1133 | enum gcc_type_class { |
| 1134 | no_type_class = -1, |
| 1135 | void_type_class, integer_type_class, char_type_class, |
| 1136 | enumeral_type_class, boolean_type_class, |
| 1137 | pointer_type_class, reference_type_class, offset_type_class, |
| 1138 | real_type_class, complex_type_class, |
| 1139 | function_type_class, method_type_class, |
| 1140 | record_type_class, union_type_class, |
| 1141 | array_type_class, string_type_class, |
| 1142 | lang_type_class |
| 1143 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
| 1145 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1146 | // ideal, however it is what gcc does. |
| 1147 | if (E->getNumArgs() == 0) |
| 1148 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1150 | QualType ArgTy = E->getArg(0)->getType(); |
| 1151 | if (ArgTy->isVoidType()) |
| 1152 | return void_type_class; |
| 1153 | else if (ArgTy->isEnumeralType()) |
| 1154 | return enumeral_type_class; |
| 1155 | else if (ArgTy->isBooleanType()) |
| 1156 | return boolean_type_class; |
| 1157 | else if (ArgTy->isCharType()) |
| 1158 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 1159 | else if (ArgTy->isIntegerType()) |
| 1160 | return integer_type_class; |
| 1161 | else if (ArgTy->isPointerType()) |
| 1162 | return pointer_type_class; |
| 1163 | else if (ArgTy->isReferenceType()) |
| 1164 | return reference_type_class; |
| 1165 | else if (ArgTy->isRealType()) |
| 1166 | return real_type_class; |
| 1167 | else if (ArgTy->isComplexType()) |
| 1168 | return complex_type_class; |
| 1169 | else if (ArgTy->isFunctionType()) |
| 1170 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1171 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1172 | return record_type_class; |
| 1173 | else if (ArgTy->isUnionType()) |
| 1174 | return union_type_class; |
| 1175 | else if (ArgTy->isArrayType()) |
| 1176 | return array_type_class; |
| 1177 | else if (ArgTy->isUnionType()) |
| 1178 | return union_type_class; |
| 1179 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 1180 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1184 | /// Retrieves the "underlying object type" of the given expression, |
| 1185 | /// as used by __builtin_object_size. |
| 1186 | QualType IntExprEvaluator::GetObjectType(const Expr *E) { |
| 1187 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 1188 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1189 | return VD->getType(); |
| 1190 | } else if (isa<CompoundLiteralExpr>(E)) { |
| 1191 | return E->getType(); |
| 1192 | } |
| 1193 | |
| 1194 | return QualType(); |
| 1195 | } |
| 1196 | |
| 1197 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) { |
| 1198 | // TODO: Perhaps we should let LLVM lower this? |
| 1199 | LValue Base; |
| 1200 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 1201 | return false; |
| 1202 | |
| 1203 | // If we can prove the base is null, lower to zero now. |
| 1204 | const Expr *LVBase = Base.getLValueBase(); |
| 1205 | if (!LVBase) return Success(0, E); |
| 1206 | |
| 1207 | QualType T = GetObjectType(LVBase); |
| 1208 | if (T.isNull() || |
| 1209 | T->isIncompleteType() || |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1210 | T->isFunctionType() || |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1211 | T->isVariablyModifiedType() || |
| 1212 | T->isDependentType()) |
| 1213 | return false; |
| 1214 | |
| 1215 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 1216 | CharUnits Offset = Base.getLValueOffset(); |
| 1217 | |
| 1218 | if (!Offset.isNegative() && Offset <= Size) |
| 1219 | Size -= Offset; |
| 1220 | else |
| 1221 | Size = CharUnits::Zero(); |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1222 | return Success(Size, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 1225 | bool IntExprEvaluator::VisitCallExpr(CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1226 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1227 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1228 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1229 | |
| 1230 | case Builtin::BI__builtin_object_size: { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1231 | if (TryEvaluateBuiltinObjectSize(E)) |
| 1232 | return true; |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1233 | |
Eric Christopher | 9946970 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 1234 | // If evaluating the argument has side-effects we can't determine |
| 1235 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 1236 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Benjamin Kramer | 0128f66 | 2010-01-03 18:18:37 +0000 | [diff] [blame] | 1237 | if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | 4f10559 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 1238 | return Success(-1ULL, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1239 | return Success(0, E); |
| 1240 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1241 | |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1242 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1243 | } |
| 1244 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1245 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1246 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1248 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1249 | // __builtin_constant_p always has one operand: it returns true if that |
| 1250 | // operand can be folded, false otherwise. |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1251 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1252 | |
| 1253 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 1254 | int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue(); |
| 1255 | Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand); |
| 1256 | return Success(Operand, E); |
| 1257 | } |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 1258 | |
| 1259 | case Builtin::BI__builtin_expect: |
| 1260 | return Visit(E->getArg(0)); |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 1261 | |
| 1262 | case Builtin::BIstrlen: |
| 1263 | case Builtin::BI__builtin_strlen: |
| 1264 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 1265 | // expressions when the argument is a string literal. |
| 1266 | if (StringLiteral *S |
| 1267 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 1268 | // The string literal may have embedded null characters. Find the first |
| 1269 | // one and truncate there. |
| 1270 | llvm::StringRef Str = S->getString(); |
| 1271 | llvm::StringRef::size_type Pos = Str.find(0); |
| 1272 | if (Pos != llvm::StringRef::npos) |
| 1273 | Str = Str.substr(0, Pos); |
| 1274 | |
| 1275 | return Success(Str.size(), E); |
| 1276 | } |
| 1277 | |
| 1278 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1280 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1282 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1283 | if (E->getOpcode() == BO_Comma) { |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1284 | if (!Visit(E->getRHS())) |
| 1285 | return false; |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1286 | |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1287 | // If we can't evaluate the LHS, it might have side effects; |
| 1288 | // conservatively mark it. |
| 1289 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 1290 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1291 | |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1292 | return true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (E->isLogicalOp()) { |
| 1296 | // These need to be handled specially because the operands aren't |
| 1297 | // necessarily integral |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1298 | bool lhsResult, rhsResult; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1300 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1301 | // We were able to evaluate the LHS, see if we can get away with not |
| 1302 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1303 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1304 | return Success(lhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1305 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1306 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1307 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1308 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1309 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1310 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } else { |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1313 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1314 | // We can't evaluate the LHS; however, sometimes the result |
| 1315 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1316 | if (rhsResult == (E->getOpcode() == BO_LOr) || |
| 1317 | !rhsResult == (E->getOpcode() == BO_LAnd)) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1318 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1319 | // must have had side effects. |
| 1320 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1321 | |
| 1322 | return Success(rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1325 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1326 | |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1327 | return false; |
| 1328 | } |
| 1329 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1330 | QualType LHSTy = E->getLHS()->getType(); |
| 1331 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (LHSTy->isAnyComplexType()) { |
| 1334 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1335 | ComplexValue LHS, RHS; |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1336 | |
| 1337 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 1338 | return false; |
| 1339 | |
| 1340 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 1341 | return false; |
| 1342 | |
| 1343 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1345 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1347 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 1348 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1349 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1350 | return Success((CR_r == APFloat::cmpEqual && |
| 1351 | CR_i == APFloat::cmpEqual), E); |
| 1352 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1353 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1354 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1356 | CR_r == APFloat::cmpLessThan || |
| 1357 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1359 | CR_i == APFloat::cmpLessThan || |
| 1360 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1361 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1362 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1363 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1364 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 1365 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 1366 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1367 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1368 | "Invalid compex comparison."); |
| 1369 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 1370 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 1371 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1375 | if (LHSTy->isRealFloatingType() && |
| 1376 | RHSTy->isRealFloatingType()) { |
| 1377 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1379 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1380 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1382 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 1383 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1385 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 1386 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1387 | switch (E->getOpcode()) { |
| 1388 | default: |
| 1389 | assert(0 && "Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1390 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1391 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1392 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1393 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1394 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1395 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1396 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1398 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1399 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1400 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1401 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 1403 | || CR == APFloat::cmpLessThan |
| 1404 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1405 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1406 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1408 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1409 | if (E->getOpcode() == BO_Sub || E->isEqualityOp()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1410 | LValue LHSValue; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1411 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1412 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1413 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1414 | LValue RHSValue; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1415 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1416 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1417 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1418 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1419 | // to null. |
| 1420 | if (LHSValue.getLValueBase()) { |
| 1421 | if (!E->isEqualityOp()) |
| 1422 | return false; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1423 | if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero()) |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | bool bres; |
| 1426 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1427 | return false; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1428 | return Success(bres ^ (E->getOpcode() == BO_EQ), E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1429 | } else if (RHSValue.getLValueBase()) { |
| 1430 | if (!E->isEqualityOp()) |
| 1431 | return false; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1432 | if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero()) |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1433 | return false; |
| 1434 | bool bres; |
| 1435 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1436 | return false; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1437 | return Success(bres ^ (E->getOpcode() == BO_EQ), E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1438 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1439 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1440 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 1441 | QualType Type = E->getLHS()->getType(); |
| 1442 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1443 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1444 | CharUnits ElementSize = CharUnits::One(); |
Eli Friedman | fa90b15 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1445 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1446 | ElementSize = Info.Ctx.getTypeSizeInChars(ElementType); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1447 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1448 | CharUnits Diff = LHSValue.getLValueOffset() - |
| 1449 | RHSValue.getLValueOffset(); |
| 1450 | return Success(Diff / ElementSize, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1451 | } |
| 1452 | bool Result; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1453 | if (E->getOpcode() == BO_EQ) { |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1454 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 8b171f6 | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1455 | } else { |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1456 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1457 | } |
| 1458 | return Success(Result, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1461 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 1462 | !RHSTy->isIntegralOrEnumerationType()) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1463 | // We can't continue from here for non-integral types, and they |
| 1464 | // could potentially confuse the following operations. |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1465 | return false; |
| 1466 | } |
| 1467 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1468 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1469 | if (!Visit(E->getLHS())) |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1470 | return false; // error in subexpression. |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1471 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1472 | APValue RHSVal; |
| 1473 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1474 | return false; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Handle cases like (unsigned long)&a + 4. |
| 1477 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1478 | CharUnits Offset = Result.getLValueOffset(); |
| 1479 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 1480 | RHSVal.getInt().getZExtValue()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1481 | if (E->getOpcode() == BO_Add) |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1482 | Offset += AdditionalOffset; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1483 | else |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1484 | Offset -= AdditionalOffset; |
| 1485 | Result = APValue(Result.getLValueBase(), Offset); |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | // Handle cases like 4 + (unsigned long)&a |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1490 | if (E->getOpcode() == BO_Add && |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1491 | RHSVal.isLValue() && Result.isInt()) { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1492 | CharUnits Offset = RHSVal.getLValueOffset(); |
| 1493 | Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue()); |
| 1494 | Result = APValue(RHSVal.getLValueBase(), Offset); |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1495 | return true; |
| 1496 | } |
| 1497 | |
| 1498 | // All the following cases expect both operands to be an integer |
| 1499 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1500 | return false; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1501 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1502 | APSInt& RHS = RHSVal.getInt(); |
| 1503 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1504 | switch (E->getOpcode()) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1505 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1506 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1507 | case BO_Mul: return Success(Result.getInt() * RHS, E); |
| 1508 | case BO_Add: return Success(Result.getInt() + RHS, E); |
| 1509 | case BO_Sub: return Success(Result.getInt() - RHS, E); |
| 1510 | case BO_And: return Success(Result.getInt() & RHS, E); |
| 1511 | case BO_Xor: return Success(Result.getInt() ^ RHS, E); |
| 1512 | case BO_Or: return Success(Result.getInt() | RHS, E); |
| 1513 | case BO_Div: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1514 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1515 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1516 | return Success(Result.getInt() / RHS, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1517 | case BO_Rem: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1518 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1519 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1520 | return Success(Result.getInt() % RHS, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1521 | case BO_Shl: { |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 1522 | // During constant-folding, a negative shift is an opposite shift. |
| 1523 | if (RHS.isSigned() && RHS.isNegative()) { |
| 1524 | RHS = -RHS; |
| 1525 | goto shift_right; |
| 1526 | } |
| 1527 | |
| 1528 | shift_left: |
| 1529 | unsigned SA |
| 1530 | = (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1531 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1532 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1533 | case BO_Shr: { |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 1534 | // During constant-folding, a negative shift is an opposite shift. |
| 1535 | if (RHS.isSigned() && RHS.isNegative()) { |
| 1536 | RHS = -RHS; |
| 1537 | goto shift_left; |
| 1538 | } |
| 1539 | |
| 1540 | shift_right: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1542 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1543 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1546 | case BO_LT: return Success(Result.getInt() < RHS, E); |
| 1547 | case BO_GT: return Success(Result.getInt() > RHS, E); |
| 1548 | case BO_LE: return Success(Result.getInt() <= RHS, E); |
| 1549 | case BO_GE: return Success(Result.getInt() >= RHS, E); |
| 1550 | case BO_EQ: return Success(Result.getInt() == RHS, E); |
| 1551 | case BO_NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | 8553a98 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1552 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1555 | bool IntExprEvaluator:: |
| 1556 | VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) { |
| 1557 | OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon()); |
| 1558 | if (opaque.hasError()) return false; |
| 1559 | |
| 1560 | bool cond; |
| 1561 | if (!HandleConversionToBool(e->getCond(), cond, Info)) |
| 1562 | return false; |
| 1563 | |
| 1564 | return Visit(cond ? e->getTrueExpr() : e->getFalseExpr()); |
| 1565 | } |
| 1566 | |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1567 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1568 | bool Cond; |
| 1569 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1570 | return false; |
| 1571 | |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1572 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1575 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1576 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1577 | // the result is the size of the referenced type." |
| 1578 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1579 | // result shall be the alignment of the referenced type." |
| 1580 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 1581 | T = Ref->getPointeeType(); |
| 1582 | |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1583 | // __alignof is defined to return the preferred alignment. |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 1584 | return Info.Ctx.toCharUnitsFromBits( |
| 1585 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1588 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1589 | E = E->IgnoreParens(); |
| 1590 | |
| 1591 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1593 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1594 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 1595 | /*RefAsPointee*/true); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1597 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1598 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 1599 | /*RefAsPointee*/true); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1601 | return GetAlignOfType(E->getType()); |
| 1602 | } |
| 1603 | |
| 1604 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1605 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 1606 | /// a result as the expression's type. |
| 1607 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 1608 | const UnaryExprOrTypeTraitExpr *E) { |
| 1609 | switch(E->getKind()) { |
| 1610 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1611 | if (E->isArgumentType()) |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1612 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1613 | else |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1614 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1615 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1616 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1617 | case UETT_VecStep: { |
| 1618 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1619 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1620 | if (Ty->isVectorType()) { |
| 1621 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1622 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1623 | // The vec_step built-in functions that take a 3-component |
| 1624 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 1625 | if (n == 3) |
| 1626 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1627 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1628 | return Success(n, E); |
| 1629 | } else |
| 1630 | return Success(1, E); |
| 1631 | } |
| 1632 | |
| 1633 | case UETT_SizeOf: { |
| 1634 | QualType SrcTy = E->getTypeOfArgument(); |
| 1635 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1636 | // the result is the size of the referenced type." |
| 1637 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1638 | // result shall be the alignment of the referenced type." |
| 1639 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 1640 | SrcTy = Ref->getPointeeType(); |
| 1641 | |
| 1642 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1643 | // extension. |
| 1644 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1645 | return Success(1, E); |
| 1646 | |
| 1647 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 1648 | if (!SrcTy->isConstantSizeType()) |
| 1649 | return false; |
| 1650 | |
| 1651 | // Get information about the size. |
| 1652 | return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E); |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | llvm_unreachable("unknown expr/type trait"); |
| 1657 | return false; |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1660 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
| 1661 | CharUnits Result; |
| 1662 | unsigned n = E->getNumComponents(); |
| 1663 | OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E); |
| 1664 | if (n == 0) |
| 1665 | return false; |
| 1666 | QualType CurrentType = E->getTypeSourceInfo()->getType(); |
| 1667 | for (unsigned i = 0; i != n; ++i) { |
| 1668 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 1669 | switch (ON.getKind()) { |
| 1670 | case OffsetOfExpr::OffsetOfNode::Array: { |
| 1671 | Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
| 1672 | APSInt IdxResult; |
| 1673 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 1674 | return false; |
| 1675 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 1676 | if (!AT) |
| 1677 | return false; |
| 1678 | CurrentType = AT->getElementType(); |
| 1679 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 1680 | Result += IdxResult.getSExtValue() * ElementSize; |
| 1681 | break; |
| 1682 | } |
| 1683 | |
| 1684 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 1685 | FieldDecl *MemberDecl = ON.getField(); |
| 1686 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 1687 | if (!RT) |
| 1688 | return false; |
| 1689 | RecordDecl *RD = RT->getDecl(); |
| 1690 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1691 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1692 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 1693 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1694 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 1695 | break; |
| 1696 | } |
| 1697 | |
| 1698 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 1699 | llvm_unreachable("dependent __builtin_offsetof"); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1700 | return false; |
| 1701 | |
| 1702 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 1703 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 1704 | if (BaseSpec->isVirtual()) |
| 1705 | return false; |
| 1706 | |
| 1707 | // Find the layout of the class whose base we are looking into. |
| 1708 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 1709 | if (!RT) |
| 1710 | return false; |
| 1711 | RecordDecl *RD = RT->getDecl(); |
| 1712 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 1713 | |
| 1714 | // Find the base class itself. |
| 1715 | CurrentType = BaseSpec->getType(); |
| 1716 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 1717 | if (!BaseRT) |
| 1718 | return false; |
| 1719 | |
| 1720 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 1721 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1722 | break; |
| 1723 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1724 | } |
| 1725 | } |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1726 | return Success(Result, E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1729 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1730 | if (E->getOpcode() == UO_LNot) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1731 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1732 | bool bres; |
| 1733 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1734 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1735 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1738 | // Only handle integral operations... |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1739 | if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType()) |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1740 | return false; |
| 1741 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1742 | // Get the operand value into 'Result'. |
| 1743 | if (!Visit(E->getSubExpr())) |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1744 | return false; |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1746 | switch (E->getOpcode()) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1747 | default: |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1748 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1749 | // See C99 6.6p3. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1750 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1751 | case UO_Extension: |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1752 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1753 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1754 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1755 | case UO_Plus: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | // The result is always just the subexpr. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1757 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1758 | case UO_Minus: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1759 | if (!Result.isInt()) return false; |
| 1760 | return Success(-Result.getInt(), E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1761 | case UO_Not: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1762 | if (!Result.isInt()) return false; |
| 1763 | return Success(~Result.getInt(), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1764 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1765 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1767 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1768 | /// result type is integer. |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1769 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1770 | Expr *SubExpr = E->getSubExpr(); |
| 1771 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1772 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1773 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1774 | if (DestType->isBooleanType()) { |
| 1775 | bool BoolResult; |
| 1776 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1777 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1778 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1781 | // Handle simple integer->integer casts. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1782 | if (SrcType->isIntegralOrEnumerationType()) { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1783 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1784 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1785 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1786 | if (!Result.isInt()) { |
| 1787 | // Only allow casts of lvalues if they are lossless. |
| 1788 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1789 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1790 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1791 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1792 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1795 | // FIXME: Clean this up! |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1796 | if (SrcType->isPointerType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1797 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1798 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1799 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1800 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1801 | if (LV.getLValueBase()) { |
| 1802 | // Only allow based lvalue casts if they are lossless. |
| 1803 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1804 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1805 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1806 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1807 | return true; |
| 1808 | } |
| 1809 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1810 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 1811 | SrcType); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1812 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1813 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1814 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1815 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1816 | // This handles double-conversion cases, where there's both |
| 1817 | // an l-value promotion and an implicit conversion to int. |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1818 | LValue LV; |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1819 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1820 | return false; |
| 1821 | |
| 1822 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1823 | return false; |
| 1824 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1825 | LV.moveInto(Result); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1826 | return true; |
| 1827 | } |
| 1828 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1829 | if (SrcType->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1830 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1831 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1832 | return false; |
| 1833 | if (C.isComplexFloat()) |
| 1834 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1835 | C.getComplexFloatReal(), Info.Ctx), |
| 1836 | E); |
| 1837 | else |
| 1838 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1839 | C.getComplexIntReal(), Info.Ctx), E); |
| 1840 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1841 | // FIXME: Handle vectors |
| 1842 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1843 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1844 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1845 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1846 | APFloat F(0.0); |
| 1847 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1848 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1850 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1851 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1852 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1853 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1854 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1855 | ComplexValue LV; |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1856 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1857 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1858 | return Success(LV.getComplexIntReal(), E); |
| 1859 | } |
| 1860 | |
| 1861 | return Visit(E->getSubExpr()); |
| 1862 | } |
| 1863 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1864 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1865 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1866 | ComplexValue LV; |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1867 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1868 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1869 | return Success(LV.getComplexIntImag(), E); |
| 1870 | } |
| 1871 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1872 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1873 | Info.EvalResult.HasSideEffects = true; |
| 1874 | return Success(0, E); |
| 1875 | } |
| 1876 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1877 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 1878 | return Success(E->getPackLength(), E); |
| 1879 | } |
| 1880 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1881 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 1882 | return Success(E->getValue(), E); |
| 1883 | } |
| 1884 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1885 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1886 | // Float Evaluation |
| 1887 | //===----------------------------------------------------------------------===// |
| 1888 | |
| 1889 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1890 | class FloatExprEvaluator |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1891 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1892 | EvalInfo &Info; |
| 1893 | APFloat &Result; |
| 1894 | public: |
| 1895 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1896 | : Info(info), Result(result) {} |
| 1897 | |
| 1898 | bool VisitStmt(Stmt *S) { |
| 1899 | return false; |
| 1900 | } |
| 1901 | |
| 1902 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1903 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1904 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1905 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1906 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1907 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1908 | bool VisitCastExpr(CastExpr *E); |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 1909 | bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Eli Friedman | f3da334 | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1910 | bool VisitConditionalOperator(ConditionalOperator *E); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1911 | bool VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1912 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1913 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1914 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1915 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1916 | { return Visit(E->getSubExpr()); } |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 1917 | bool VisitUnaryReal(const UnaryOperator *E); |
| 1918 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1919 | |
John McCall | a2fabff | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 1920 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 1921 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1922 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) { |
| 1923 | const APValue *value = Info.getOpaqueValue(e); |
| 1924 | if (!value) |
| 1925 | return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false); |
| 1926 | Result = value->getFloat(); |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 1930 | // FIXME: Missing: array subscript of vector, member of vector, |
| 1931 | // ImplicitValueInitExpr |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1932 | }; |
| 1933 | } // end anonymous namespace |
| 1934 | |
| 1935 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 1936 | assert(E->getType()->isRealFloatingType()); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1937 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1938 | } |
| 1939 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1940 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1941 | QualType ResultTy, |
| 1942 | const Expr *Arg, |
| 1943 | bool SNaN, |
| 1944 | llvm::APFloat &Result) { |
| 1945 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 1946 | if (!S) return false; |
| 1947 | |
| 1948 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 1949 | |
| 1950 | llvm::APInt fill; |
| 1951 | |
| 1952 | // Treat empty strings as if they were zero. |
| 1953 | if (S->getString().empty()) |
| 1954 | fill = llvm::APInt(32, 0); |
| 1955 | else if (S->getString().getAsInteger(0, fill)) |
| 1956 | return false; |
| 1957 | |
| 1958 | if (SNaN) |
| 1959 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 1960 | else |
| 1961 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1965 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1966 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1967 | default: return false; |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1968 | case Builtin::BI__builtin_huge_val: |
| 1969 | case Builtin::BI__builtin_huge_valf: |
| 1970 | case Builtin::BI__builtin_huge_vall: |
| 1971 | case Builtin::BI__builtin_inf: |
| 1972 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1973 | case Builtin::BI__builtin_infl: { |
| 1974 | const llvm::fltSemantics &Sem = |
| 1975 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1976 | Result = llvm::APFloat::getInf(Sem); |
| 1977 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1978 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1980 | case Builtin::BI__builtin_nans: |
| 1981 | case Builtin::BI__builtin_nansf: |
| 1982 | case Builtin::BI__builtin_nansl: |
| 1983 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 1984 | true, Result); |
| 1985 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1986 | case Builtin::BI__builtin_nan: |
| 1987 | case Builtin::BI__builtin_nanf: |
| 1988 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1989 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1990 | // can't constant fold it. |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 1991 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 1992 | false, Result); |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1993 | |
| 1994 | case Builtin::BI__builtin_fabs: |
| 1995 | case Builtin::BI__builtin_fabsf: |
| 1996 | case Builtin::BI__builtin_fabsl: |
| 1997 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1998 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2000 | if (Result.isNegative()) |
| 2001 | Result.changeSign(); |
| 2002 | return true; |
| 2003 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | case Builtin::BI__builtin_copysign: |
| 2005 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2006 | case Builtin::BI__builtin_copysignl: { |
| 2007 | APFloat RHS(0.); |
| 2008 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 2009 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 2010 | return false; |
| 2011 | Result.copySign(RHS); |
| 2012 | return true; |
| 2013 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
John McCall | a2fabff | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 2017 | bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 2018 | const Decl *D = E->getDecl(); |
| 2019 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false; |
| 2020 | const VarDecl *VD = cast<VarDecl>(D); |
| 2021 | |
| 2022 | // Require the qualifiers to be const and not volatile. |
| 2023 | CanQualType T = Info.Ctx.getCanonicalType(E->getType()); |
| 2024 | if (!T.isConstQualified() || T.isVolatileQualified()) |
| 2025 | return false; |
| 2026 | |
| 2027 | const Expr *Init = VD->getAnyInitializer(); |
| 2028 | if (!Init) return false; |
| 2029 | |
| 2030 | if (APValue *V = VD->getEvaluatedValue()) { |
| 2031 | if (V->isFloat()) { |
| 2032 | Result = V->getFloat(); |
| 2033 | return true; |
| 2034 | } |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
| 2038 | if (VD->isEvaluatingValue()) |
| 2039 | return false; |
| 2040 | |
| 2041 | VD->setEvaluatingValue(); |
| 2042 | |
| 2043 | Expr::EvalResult InitResult; |
| 2044 | if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects && |
| 2045 | InitResult.Val.isFloat()) { |
| 2046 | // Cache the evaluated value in the variable declaration. |
| 2047 | Result = InitResult.Val.getFloat(); |
| 2048 | VD->setEvaluatedValue(InitResult.Val); |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | VD->setEvaluatedValue(APValue()); |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2056 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 2057 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 2058 | ComplexValue CV; |
| 2059 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 2060 | return false; |
| 2061 | Result = CV.FloatReal; |
| 2062 | return true; |
| 2063 | } |
| 2064 | |
| 2065 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 2069 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 2070 | ComplexValue CV; |
| 2071 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 2072 | return false; |
| 2073 | Result = CV.FloatImag; |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
| 2077 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 2078 | Info.EvalResult.HasSideEffects = true; |
| 2079 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 2080 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2081 | return true; |
| 2082 | } |
| 2083 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2084 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2085 | if (E->getOpcode() == UO_Deref) |
Nuno Lopes | 0e33c68 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 2086 | return false; |
| 2087 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2088 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 2089 | return false; |
| 2090 | |
| 2091 | switch (E->getOpcode()) { |
| 2092 | default: return false; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2093 | case UO_Plus: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2094 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2095 | case UO_Minus: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2096 | Result.changeSign(); |
| 2097 | return true; |
| 2098 | } |
| 2099 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2100 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2101 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2102 | if (E->getOpcode() == BO_Comma) { |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 2103 | if (!EvaluateFloat(E->getRHS(), Result, Info)) |
| 2104 | return false; |
| 2105 | |
| 2106 | // If we can't evaluate the LHS, it might have side effects; |
| 2107 | // conservatively mark it. |
| 2108 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 2109 | Info.EvalResult.HasSideEffects = true; |
| 2110 | |
| 2111 | return true; |
| 2112 | } |
| 2113 | |
Anders Carlsson | a5df61a | 2010-10-31 01:21:47 +0000 | [diff] [blame] | 2114 | // We can't evaluate pointer-to-member operations. |
| 2115 | if (E->isPtrMemOp()) |
| 2116 | return false; |
| 2117 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2118 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 2119 | // and errors are supposed to work. |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2120 | APFloat RHS(0.0); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2121 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 2122 | return false; |
| 2123 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 2124 | return false; |
| 2125 | |
| 2126 | switch (E->getOpcode()) { |
| 2127 | default: return false; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2128 | case BO_Mul: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2129 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2130 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2131 | case BO_Add: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2132 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 2133 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2134 | case BO_Sub: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2135 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2136 | return true; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2137 | case BO_Div: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2138 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2139 | return true; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 2144 | Result = E->getValue(); |
| 2145 | return true; |
| 2146 | } |
| 2147 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2148 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 2149 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2151 | if (SubExpr->getType()->isIntegralOrEnumerationType()) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2152 | APSInt IntResult; |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2153 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2154 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2156 | IntResult, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2157 | return true; |
| 2158 | } |
| 2159 | if (SubExpr->getType()->isRealFloatingType()) { |
| 2160 | if (!Visit(SubExpr)) |
| 2161 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2162 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 2163 | Result, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2164 | return true; |
| 2165 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2166 | |
| 2167 | if (E->getCastKind() == CK_FloatingComplexToReal) { |
| 2168 | ComplexValue V; |
| 2169 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 2170 | return false; |
| 2171 | Result = V.getComplexFloatReal(); |
| 2172 | return true; |
| 2173 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2174 | |
| 2175 | return false; |
| 2176 | } |
| 2177 | |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2178 | bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2179 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 2180 | return true; |
| 2181 | } |
| 2182 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2183 | bool FloatExprEvaluator:: |
| 2184 | VisitBinaryConditionalOperator(BinaryConditionalOperator *e) { |
| 2185 | OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon()); |
| 2186 | if (opaque.hasError()) return false; |
| 2187 | |
| 2188 | bool cond; |
| 2189 | if (!HandleConversionToBool(e->getCond(), cond, Info)) |
| 2190 | return false; |
| 2191 | |
| 2192 | return Visit(cond ? e->getTrueExpr() : e->getFalseExpr()); |
| 2193 | } |
| 2194 | |
Eli Friedman | f3da334 | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 2195 | bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 2196 | bool Cond; |
| 2197 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
| 2198 | return false; |
| 2199 | |
| 2200 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 2201 | } |
| 2202 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2203 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2204 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2205 | //===----------------------------------------------------------------------===// |
| 2206 | |
| 2207 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2208 | class ComplexExprEvaluator |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2209 | : public StmtVisitor<ComplexExprEvaluator, bool> { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2210 | EvalInfo &Info; |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2211 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2213 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2214 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
| 2215 | : Info(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2217 | //===--------------------------------------------------------------------===// |
| 2218 | // Visitor Methods |
| 2219 | //===--------------------------------------------------------------------===// |
| 2220 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2221 | bool VisitStmt(Stmt *S) { |
| 2222 | return false; |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2225 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2226 | |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2227 | bool VisitImaginaryLiteral(ImaginaryLiteral *E); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2228 | |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2229 | bool VisitCastExpr(CastExpr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2231 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2232 | bool VisitUnaryOperator(const UnaryOperator *E); |
| 2233 | bool VisitConditionalOperator(const ConditionalOperator *E); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2234 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2235 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2236 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2237 | bool VisitUnaryExtension(const UnaryOperator *E) |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2238 | { return Visit(E->getSubExpr()); } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2239 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *e) { |
| 2240 | const APValue *value = Info.getOpaqueValue(e); |
| 2241 | if (!value) |
| 2242 | return (e->getSourceExpr() ? Visit(e->getSourceExpr()) : false); |
| 2243 | Result.setFrom(*value); |
| 2244 | return true; |
| 2245 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2246 | // FIXME Missing: ImplicitValueInitExpr |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2247 | }; |
| 2248 | } // end anonymous namespace |
| 2249 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2250 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 2251 | EvalInfo &Info) { |
John McCall | f0c4f35 | 2010-05-07 05:46:35 +0000 | [diff] [blame] | 2252 | assert(E->getType()->isAnyComplexType()); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2253 | return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2256 | bool ComplexExprEvaluator::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 2257 | Expr* SubExpr = E->getSubExpr(); |
| 2258 | |
| 2259 | if (SubExpr->getType()->isRealFloatingType()) { |
| 2260 | Result.makeComplexFloat(); |
| 2261 | APFloat &Imag = Result.FloatImag; |
| 2262 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 2263 | return false; |
| 2264 | |
| 2265 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 2266 | return true; |
| 2267 | } else { |
| 2268 | assert(SubExpr->getType()->isIntegerType() && |
| 2269 | "Unexpected imaginary literal."); |
| 2270 | |
| 2271 | Result.makeComplexInt(); |
| 2272 | APSInt &Imag = Result.IntImag; |
| 2273 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 2274 | return false; |
| 2275 | |
| 2276 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 2277 | return true; |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2282 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 2283 | switch (E->getCastKind()) { |
| 2284 | case CK_BitCast: |
| 2285 | case CK_LValueBitCast: |
| 2286 | case CK_BaseToDerived: |
| 2287 | case CK_DerivedToBase: |
| 2288 | case CK_UncheckedDerivedToBase: |
| 2289 | case CK_Dynamic: |
| 2290 | case CK_ToUnion: |
| 2291 | case CK_ArrayToPointerDecay: |
| 2292 | case CK_FunctionToPointerDecay: |
| 2293 | case CK_NullToPointer: |
| 2294 | case CK_NullToMemberPointer: |
| 2295 | case CK_BaseToDerivedMemberPointer: |
| 2296 | case CK_DerivedToBaseMemberPointer: |
| 2297 | case CK_MemberPointerToBoolean: |
| 2298 | case CK_ConstructorConversion: |
| 2299 | case CK_IntegralToPointer: |
| 2300 | case CK_PointerToIntegral: |
| 2301 | case CK_PointerToBoolean: |
| 2302 | case CK_ToVoid: |
| 2303 | case CK_VectorSplat: |
| 2304 | case CK_IntegralCast: |
| 2305 | case CK_IntegralToBoolean: |
| 2306 | case CK_IntegralToFloating: |
| 2307 | case CK_FloatingToIntegral: |
| 2308 | case CK_FloatingToBoolean: |
| 2309 | case CK_FloatingCast: |
| 2310 | case CK_AnyPointerToObjCPointerCast: |
| 2311 | case CK_AnyPointerToBlockPointerCast: |
| 2312 | case CK_ObjCObjectLValueCast: |
| 2313 | case CK_FloatingComplexToReal: |
| 2314 | case CK_FloatingComplexToBoolean: |
| 2315 | case CK_IntegralComplexToReal: |
| 2316 | case CK_IntegralComplexToBoolean: |
| 2317 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 2318 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 2319 | case CK_LValueToRValue: |
| 2320 | case CK_NoOp: |
| 2321 | return Visit(E->getSubExpr()); |
| 2322 | |
| 2323 | case CK_Dependent: |
| 2324 | case CK_GetObjCProperty: |
| 2325 | case CK_UserDefinedConversion: |
| 2326 | return false; |
| 2327 | |
| 2328 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2329 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 2330 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2331 | return false; |
| 2332 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 2333 | Result.makeComplexFloat(); |
| 2334 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 2335 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 2338 | case CK_FloatingComplexCast: { |
| 2339 | if (!Visit(E->getSubExpr())) |
| 2340 | return false; |
| 2341 | |
| 2342 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 2343 | QualType From |
| 2344 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 2345 | |
| 2346 | Result.FloatReal |
| 2347 | = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx); |
| 2348 | Result.FloatImag |
| 2349 | = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx); |
| 2350 | return true; |
| 2351 | } |
| 2352 | |
| 2353 | case CK_FloatingComplexToIntegralComplex: { |
| 2354 | if (!Visit(E->getSubExpr())) |
| 2355 | return false; |
| 2356 | |
| 2357 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 2358 | QualType From |
| 2359 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 2360 | Result.makeComplexInt(); |
| 2361 | Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx); |
| 2362 | Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx); |
| 2363 | return true; |
| 2364 | } |
| 2365 | |
| 2366 | case CK_IntegralRealToComplex: { |
| 2367 | APSInt &Real = Result.IntReal; |
| 2368 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 2369 | return false; |
| 2370 | |
| 2371 | Result.makeComplexInt(); |
| 2372 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
| 2376 | case CK_IntegralComplexCast: { |
| 2377 | if (!Visit(E->getSubExpr())) |
| 2378 | return false; |
| 2379 | |
| 2380 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 2381 | QualType From |
| 2382 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 2383 | |
| 2384 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 2385 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 2386 | return true; |
| 2387 | } |
| 2388 | |
| 2389 | case CK_IntegralComplexToFloatingComplex: { |
| 2390 | if (!Visit(E->getSubExpr())) |
| 2391 | return false; |
| 2392 | |
| 2393 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 2394 | QualType From |
| 2395 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 2396 | Result.makeComplexFloat(); |
| 2397 | Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx); |
| 2398 | Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx); |
| 2399 | return true; |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 2404 | return false; |
| 2405 | } |
| 2406 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2407 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2408 | if (E->getOpcode() == BO_Comma) { |
| 2409 | if (!Visit(E->getRHS())) |
| 2410 | return false; |
| 2411 | |
| 2412 | // If we can't evaluate the LHS, it might have side effects; |
| 2413 | // conservatively mark it. |
| 2414 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 2415 | Info.EvalResult.HasSideEffects = true; |
| 2416 | |
| 2417 | return true; |
| 2418 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2419 | if (!Visit(E->getLHS())) |
| 2420 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2422 | ComplexValue RHS; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2423 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2424 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2425 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2426 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 2427 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2428 | switch (E->getOpcode()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2429 | default: return false; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2430 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2431 | if (Result.isComplexFloat()) { |
| 2432 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 2433 | APFloat::rmNearestTiesToEven); |
| 2434 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 2435 | APFloat::rmNearestTiesToEven); |
| 2436 | } else { |
| 2437 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 2438 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 2439 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2440 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2441 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2442 | if (Result.isComplexFloat()) { |
| 2443 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 2444 | APFloat::rmNearestTiesToEven); |
| 2445 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 2446 | APFloat::rmNearestTiesToEven); |
| 2447 | } else { |
| 2448 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 2449 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 2450 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2451 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2452 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2453 | if (Result.isComplexFloat()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2454 | ComplexValue LHS = Result; |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2455 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 2456 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 2457 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 2458 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2460 | APFloat Tmp = LHS_r; |
| 2461 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2462 | Result.getComplexFloatReal() = Tmp; |
| 2463 | Tmp = LHS_i; |
| 2464 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2465 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 2466 | |
| 2467 | Tmp = LHS_r; |
| 2468 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2469 | Result.getComplexFloatImag() = Tmp; |
| 2470 | Tmp = LHS_i; |
| 2471 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2472 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 2473 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2474 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2476 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 2477 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 2479 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 2480 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 2481 | } |
| 2482 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2483 | case BO_Div: |
| 2484 | if (Result.isComplexFloat()) { |
| 2485 | ComplexValue LHS = Result; |
| 2486 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 2487 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 2488 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 2489 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 2490 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 2491 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 2492 | |
| 2493 | APFloat Den = RHS_r; |
| 2494 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2495 | APFloat Tmp = RHS_i; |
| 2496 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2497 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 2498 | |
| 2499 | Res_r = LHS_r; |
| 2500 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2501 | Tmp = LHS_i; |
| 2502 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2503 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 2504 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 2505 | |
| 2506 | Res_i = LHS_i; |
| 2507 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 2508 | Tmp = LHS_r; |
| 2509 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 2510 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 2511 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 2512 | } else { |
| 2513 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) { |
| 2514 | // FIXME: what about diagnostics? |
| 2515 | return false; |
| 2516 | } |
| 2517 | ComplexValue LHS = Result; |
| 2518 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 2519 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 2520 | Result.getComplexIntReal() = |
| 2521 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 2522 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 2523 | Result.getComplexIntImag() = |
| 2524 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 2525 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 2526 | } |
| 2527 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2530 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2533 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 2534 | // Get the operand value into 'Result'. |
| 2535 | if (!Visit(E->getSubExpr())) |
| 2536 | return false; |
| 2537 | |
| 2538 | switch (E->getOpcode()) { |
| 2539 | default: |
| 2540 | // FIXME: what about diagnostics? |
| 2541 | return false; |
| 2542 | case UO_Extension: |
| 2543 | return true; |
| 2544 | case UO_Plus: |
| 2545 | // The result is always just the subexpr. |
| 2546 | return true; |
| 2547 | case UO_Minus: |
| 2548 | if (Result.isComplexFloat()) { |
| 2549 | Result.getComplexFloatReal().changeSign(); |
| 2550 | Result.getComplexFloatImag().changeSign(); |
| 2551 | } |
| 2552 | else { |
| 2553 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 2554 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 2555 | } |
| 2556 | return true; |
| 2557 | case UO_Not: |
| 2558 | if (Result.isComplexFloat()) |
| 2559 | Result.getComplexFloatImag().changeSign(); |
| 2560 | else |
| 2561 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 2562 | return true; |
| 2563 | } |
| 2564 | } |
| 2565 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2566 | bool ComplexExprEvaluator:: |
| 2567 | VisitBinaryConditionalOperator(const BinaryConditionalOperator *e) { |
| 2568 | OpaqueValueEvaluation opaque(Info, e->getOpaqueValue(), e->getCommon()); |
| 2569 | if (opaque.hasError()) return false; |
| 2570 | |
| 2571 | bool cond; |
| 2572 | if (!HandleConversionToBool(e->getCond(), cond, Info)) |
| 2573 | return false; |
| 2574 | |
| 2575 | return Visit(cond ? e->getTrueExpr() : e->getFalseExpr()); |
| 2576 | } |
| 2577 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2578 | bool ComplexExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 2579 | bool Cond; |
| 2580 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
| 2581 | return false; |
| 2582 | |
| 2583 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 2584 | } |
| 2585 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2586 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2587 | // Top level Expr::Evaluate method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2588 | //===----------------------------------------------------------------------===// |
| 2589 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2590 | static bool Evaluate(EvalInfo &Info, const Expr *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2591 | if (E->getType()->isVectorType()) { |
| 2592 | if (!EvaluateVector(E, Info.EvalResult.Val, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2593 | return false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2594 | } else if (E->getType()->isIntegerType()) { |
| 2595 | if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E))) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2596 | return false; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2597 | if (Info.EvalResult.Val.isLValue() && |
| 2598 | !IsGlobalLValue(Info.EvalResult.Val.getLValueBase())) |
John McCall | 11086fc | 2010-07-07 05:08:32 +0000 | [diff] [blame] | 2599 | return false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2600 | } else if (E->getType()->hasPointerRepresentation()) { |
| 2601 | LValue LV; |
| 2602 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2603 | return false; |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 2604 | if (!IsGlobalLValue(LV.Base)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2605 | return false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2606 | LV.moveInto(Info.EvalResult.Val); |
| 2607 | } else if (E->getType()->isRealFloatingType()) { |
| 2608 | llvm::APFloat F(0.0); |
| 2609 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2610 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2612 | Info.EvalResult.Val = APValue(F); |
| 2613 | } else if (E->getType()->isAnyComplexType()) { |
| 2614 | ComplexValue C; |
| 2615 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2616 | return false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2617 | C.moveInto(Info.EvalResult.Val); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2618 | } else |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 2619 | return false; |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 2620 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 2621 | return true; |
| 2622 | } |
| 2623 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2624 | /// Evaluate - Return true if this is a constant which we can fold using |
| 2625 | /// any crazy technique (that has nothing to do with language standards) that |
| 2626 | /// we want to. If this function returns true, it returns the folded constant |
| 2627 | /// in Result. |
| 2628 | bool Expr::Evaluate(EvalResult &Result, const ASTContext &Ctx) const { |
| 2629 | EvalInfo Info(Ctx, Result); |
| 2630 | return ::Evaluate(Info, this); |
| 2631 | } |
| 2632 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2633 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 2634 | const ASTContext &Ctx) const { |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 2635 | EvalResult Scratch; |
| 2636 | EvalInfo Info(Ctx, Scratch); |
| 2637 | |
| 2638 | return HandleConversionToBool(this, Result, Info); |
| 2639 | } |
| 2640 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2641 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 2642 | EvalInfo Info(Ctx, Result); |
| 2643 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2644 | LValue LV; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2645 | if (EvaluateLValue(this, LV, Info) && |
| 2646 | !Result.HasSideEffects && |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 2647 | IsGlobalLValue(LV.Base)) { |
| 2648 | LV.moveInto(Result.Val); |
| 2649 | return true; |
| 2650 | } |
| 2651 | return false; |
| 2652 | } |
| 2653 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2654 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, |
| 2655 | const ASTContext &Ctx) const { |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 2656 | EvalInfo Info(Ctx, Result); |
| 2657 | |
| 2658 | LValue LV; |
| 2659 | if (EvaluateLValue(this, LV, Info)) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2660 | LV.moveInto(Result.Val); |
| 2661 | return true; |
| 2662 | } |
| 2663 | return false; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2666 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2667 | /// folded, but discard the result. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2668 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 2669 | EvalResult Result; |
| 2670 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2671 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2672 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2673 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 2674 | Expr::EvalResult Result; |
| 2675 | EvalInfo Info(Ctx, Result); |
| 2676 | return HasSideEffect(Info).Visit(const_cast<Expr*>(this)); |
| 2677 | } |
| 2678 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2679 | APSInt Expr::EvaluateAsInt(const ASTContext &Ctx) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2680 | EvalResult EvalResult; |
| 2681 | bool Result = Evaluate(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2682 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2683 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2684 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2685 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2686 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2687 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2688 | |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 2689 | bool Expr::EvalResult::isGlobalLValue() const { |
| 2690 | assert(Val.isLValue()); |
| 2691 | return IsGlobalLValue(Val.getLValueBase()); |
| 2692 | } |
| 2693 | |
| 2694 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2695 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 2696 | /// an integer constant expression. |
| 2697 | |
| 2698 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 2699 | /// comma, etc |
| 2700 | /// |
| 2701 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 2702 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 2703 | /// cast+dereference. |
| 2704 | |
| 2705 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 2706 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 2707 | // Note that to reduce code duplication, this helper does no evaluation |
| 2708 | // itself; the caller checks whether the expression is evaluatable, and |
| 2709 | // in the rare cases where CheckICE actually cares about the evaluated |
| 2710 | // value, it calls into Evalute. |
| 2711 | // |
| 2712 | // Meanings of Val: |
| 2713 | // 0: This expression is an ICE if it can be evaluated by Evaluate. |
| 2714 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 2715 | // a legal subexpression for an ICE. This return value is used to handle |
| 2716 | // the comma operator in C99 mode. |
| 2717 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 2718 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2719 | namespace { |
| 2720 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2721 | struct ICEDiag { |
| 2722 | unsigned Val; |
| 2723 | SourceLocation Loc; |
| 2724 | |
| 2725 | public: |
| 2726 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 2727 | ICEDiag() : Val(0) {} |
| 2728 | }; |
| 2729 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2733 | |
| 2734 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 2735 | Expr::EvalResult EVResult; |
| 2736 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 2737 | !EVResult.Val.isInt()) { |
| 2738 | return ICEDiag(2, E->getLocStart()); |
| 2739 | } |
| 2740 | return NoDiag(); |
| 2741 | } |
| 2742 | |
| 2743 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 2744 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2745 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2746 | return ICEDiag(2, E->getLocStart()); |
| 2747 | } |
| 2748 | |
| 2749 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2750 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2751 | #define STMT(Node, Base) case Expr::Node##Class: |
| 2752 | #define EXPR(Node, Base) |
| 2753 | #include "clang/AST/StmtNodes.inc" |
| 2754 | case Expr::PredefinedExprClass: |
| 2755 | case Expr::FloatingLiteralClass: |
| 2756 | case Expr::ImaginaryLiteralClass: |
| 2757 | case Expr::StringLiteralClass: |
| 2758 | case Expr::ArraySubscriptExprClass: |
| 2759 | case Expr::MemberExprClass: |
| 2760 | case Expr::CompoundAssignOperatorClass: |
| 2761 | case Expr::CompoundLiteralExprClass: |
| 2762 | case Expr::ExtVectorElementExprClass: |
| 2763 | case Expr::InitListExprClass: |
| 2764 | case Expr::DesignatedInitExprClass: |
| 2765 | case Expr::ImplicitValueInitExprClass: |
| 2766 | case Expr::ParenListExprClass: |
| 2767 | case Expr::VAArgExprClass: |
| 2768 | case Expr::AddrLabelExprClass: |
| 2769 | case Expr::StmtExprClass: |
| 2770 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 2771 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2772 | case Expr::CXXDynamicCastExprClass: |
| 2773 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 2774 | case Expr::CXXUuidofExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2775 | case Expr::CXXNullPtrLiteralExprClass: |
| 2776 | case Expr::CXXThisExprClass: |
| 2777 | case Expr::CXXThrowExprClass: |
| 2778 | case Expr::CXXNewExprClass: |
| 2779 | case Expr::CXXDeleteExprClass: |
| 2780 | case Expr::CXXPseudoDestructorExprClass: |
| 2781 | case Expr::UnresolvedLookupExprClass: |
| 2782 | case Expr::DependentScopeDeclRefExprClass: |
| 2783 | case Expr::CXXConstructExprClass: |
| 2784 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2785 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2786 | case Expr::CXXTemporaryObjectExprClass: |
| 2787 | case Expr::CXXUnresolvedConstructExprClass: |
| 2788 | case Expr::CXXDependentScopeMemberExprClass: |
| 2789 | case Expr::UnresolvedMemberExprClass: |
| 2790 | case Expr::ObjCStringLiteralClass: |
| 2791 | case Expr::ObjCEncodeExprClass: |
| 2792 | case Expr::ObjCMessageExprClass: |
| 2793 | case Expr::ObjCSelectorExprClass: |
| 2794 | case Expr::ObjCProtocolExprClass: |
| 2795 | case Expr::ObjCIvarRefExprClass: |
| 2796 | case Expr::ObjCPropertyRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2797 | case Expr::ObjCIsaExprClass: |
| 2798 | case Expr::ShuffleVectorExprClass: |
| 2799 | case Expr::BlockExprClass: |
| 2800 | case Expr::BlockDeclRefExprClass: |
| 2801 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 2802 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 2803 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2804 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2805 | return ICEDiag(2, E->getLocStart()); |
| 2806 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2807 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2808 | case Expr::GNUNullExprClass: |
| 2809 | // GCC considers the GNU __null value to be an integral constant expression. |
| 2810 | return NoDiag(); |
| 2811 | |
| 2812 | case Expr::ParenExprClass: |
| 2813 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
| 2814 | case Expr::IntegerLiteralClass: |
| 2815 | case Expr::CharacterLiteralClass: |
| 2816 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2817 | case Expr::CXXScalarValueInitExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2818 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2819 | case Expr::BinaryTypeTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 2820 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2821 | return NoDiag(); |
| 2822 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 2823 | case Expr::CXXOperatorCallExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2824 | const CallExpr *CE = cast<CallExpr>(E); |
| 2825 | if (CE->isBuiltinCall(Ctx)) |
| 2826 | return CheckEvalInICE(E, Ctx); |
| 2827 | return ICEDiag(2, E->getLocStart()); |
| 2828 | } |
| 2829 | case Expr::DeclRefExprClass: |
| 2830 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 2831 | return NoDiag(); |
| 2832 | if (Ctx.getLangOptions().CPlusPlus && |
| 2833 | E->getType().getCVRQualifiers() == Qualifiers::Const) { |
| 2834 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 2835 | |
| 2836 | // Parameter variables are never constants. Without this check, |
| 2837 | // getAnyInitializer() can find a default argument, which leads |
| 2838 | // to chaos. |
| 2839 | if (isa<ParmVarDecl>(D)) |
| 2840 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2841 | |
| 2842 | // C++ 7.1.5.1p2 |
| 2843 | // A variable of non-volatile const-qualified integral or enumeration |
| 2844 | // type initialized by an ICE can be used in ICEs. |
| 2845 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
| 2846 | Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers(); |
| 2847 | if (Quals.hasVolatile() || !Quals.hasConst()) |
| 2848 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2849 | |
| 2850 | // Look for a declaration of this variable that has an initializer. |
| 2851 | const VarDecl *ID = 0; |
| 2852 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 2853 | if (Init) { |
| 2854 | if (ID->isInitKnownICE()) { |
| 2855 | // We have already checked whether this subexpression is an |
| 2856 | // integral constant expression. |
| 2857 | if (ID->isInitICE()) |
| 2858 | return NoDiag(); |
| 2859 | else |
| 2860 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2861 | } |
| 2862 | |
| 2863 | // It's an ICE whether or not the definition we found is |
| 2864 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2865 | // 6206 for details. |
| 2866 | |
| 2867 | if (Dcl->isCheckingICE()) { |
| 2868 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 2869 | } |
| 2870 | |
| 2871 | Dcl->setCheckingICE(); |
| 2872 | ICEDiag Result = CheckICE(Init, Ctx); |
| 2873 | // Cache the result of the ICE test. |
| 2874 | Dcl->setInitKnownICE(Result.Val == 0); |
| 2875 | return Result; |
| 2876 | } |
| 2877 | } |
| 2878 | } |
| 2879 | return ICEDiag(2, E->getLocStart()); |
| 2880 | case Expr::UnaryOperatorClass: { |
| 2881 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 2882 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2883 | case UO_PostInc: |
| 2884 | case UO_PostDec: |
| 2885 | case UO_PreInc: |
| 2886 | case UO_PreDec: |
| 2887 | case UO_AddrOf: |
| 2888 | case UO_Deref: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2889 | return ICEDiag(2, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2890 | case UO_Extension: |
| 2891 | case UO_LNot: |
| 2892 | case UO_Plus: |
| 2893 | case UO_Minus: |
| 2894 | case UO_Not: |
| 2895 | case UO_Real: |
| 2896 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2897 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | // OffsetOf falls through here. |
| 2901 | } |
| 2902 | case Expr::OffsetOfExprClass: { |
| 2903 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 2904 | // Evaluate matches the proposed gcc behavior for cases like |
| 2905 | // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect |
| 2906 | // compliance: we should warn earlier for offsetof expressions with |
| 2907 | // array subscripts that aren't ICEs, and if the array subscripts |
| 2908 | // are ICEs, the value of the offsetof must be an integer constant. |
| 2909 | return CheckEvalInICE(E, Ctx); |
| 2910 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2911 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 2912 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 2913 | if ((Exp->getKind() == UETT_SizeOf) && |
| 2914 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2915 | return ICEDiag(2, E->getLocStart()); |
| 2916 | return NoDiag(); |
| 2917 | } |
| 2918 | case Expr::BinaryOperatorClass: { |
| 2919 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 2920 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2921 | case BO_PtrMemD: |
| 2922 | case BO_PtrMemI: |
| 2923 | case BO_Assign: |
| 2924 | case BO_MulAssign: |
| 2925 | case BO_DivAssign: |
| 2926 | case BO_RemAssign: |
| 2927 | case BO_AddAssign: |
| 2928 | case BO_SubAssign: |
| 2929 | case BO_ShlAssign: |
| 2930 | case BO_ShrAssign: |
| 2931 | case BO_AndAssign: |
| 2932 | case BO_XorAssign: |
| 2933 | case BO_OrAssign: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2934 | return ICEDiag(2, E->getLocStart()); |
| 2935 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2936 | case BO_Mul: |
| 2937 | case BO_Div: |
| 2938 | case BO_Rem: |
| 2939 | case BO_Add: |
| 2940 | case BO_Sub: |
| 2941 | case BO_Shl: |
| 2942 | case BO_Shr: |
| 2943 | case BO_LT: |
| 2944 | case BO_GT: |
| 2945 | case BO_LE: |
| 2946 | case BO_GE: |
| 2947 | case BO_EQ: |
| 2948 | case BO_NE: |
| 2949 | case BO_And: |
| 2950 | case BO_Xor: |
| 2951 | case BO_Or: |
| 2952 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2953 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 2954 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2955 | if (Exp->getOpcode() == BO_Div || |
| 2956 | Exp->getOpcode() == BO_Rem) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2957 | // Evaluate gives an error for undefined Div/Rem, so make sure |
| 2958 | // we don't evaluate one. |
John McCall | 4b13633 | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 2959 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2960 | llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); |
| 2961 | if (REval == 0) |
| 2962 | return ICEDiag(1, E->getLocStart()); |
| 2963 | if (REval.isSigned() && REval.isAllOnesValue()) { |
| 2964 | llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); |
| 2965 | if (LEval.isMinSignedValue()) |
| 2966 | return ICEDiag(1, E->getLocStart()); |
| 2967 | } |
| 2968 | } |
| 2969 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2970 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2971 | if (Ctx.getLangOptions().C99) { |
| 2972 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 2973 | // if it isn't evaluated. |
| 2974 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 2975 | return ICEDiag(1, E->getLocStart()); |
| 2976 | } else { |
| 2977 | // In both C89 and C++, commas in ICEs are illegal. |
| 2978 | return ICEDiag(2, E->getLocStart()); |
| 2979 | } |
| 2980 | } |
| 2981 | if (LHSResult.Val >= RHSResult.Val) |
| 2982 | return LHSResult; |
| 2983 | return RHSResult; |
| 2984 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2985 | case BO_LAnd: |
| 2986 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2987 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 2988 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 2989 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 2990 | // Rare case where the RHS has a comma "side-effect"; we need |
| 2991 | // to actually check the condition to see whether the side |
| 2992 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2993 | if ((Exp->getOpcode() == BO_LAnd) != |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 2994 | (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) |
| 2995 | return RHSResult; |
| 2996 | return NoDiag(); |
| 2997 | } |
| 2998 | |
| 2999 | if (LHSResult.Val >= RHSResult.Val) |
| 3000 | return LHSResult; |
| 3001 | return RHSResult; |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | case Expr::ImplicitCastExprClass: |
| 3006 | case Expr::CStyleCastExprClass: |
| 3007 | case Expr::CXXFunctionalCastExprClass: |
| 3008 | case Expr::CXXStaticCastExprClass: |
| 3009 | case Expr::CXXReinterpretCastExprClass: |
| 3010 | case Expr::CXXConstCastExprClass: { |
| 3011 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3012 | if (SubExpr->getType()->isIntegralOrEnumerationType()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3013 | return CheckICE(SubExpr, Ctx); |
| 3014 | if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) |
| 3015 | return NoDiag(); |
| 3016 | return ICEDiag(2, E->getLocStart()); |
| 3017 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3018 | case Expr::BinaryConditionalOperatorClass: { |
| 3019 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 3020 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 3021 | if (CommonResult.Val == 2) return CommonResult; |
| 3022 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 3023 | if (FalseResult.Val == 2) return FalseResult; |
| 3024 | if (CommonResult.Val == 1) return CommonResult; |
| 3025 | if (FalseResult.Val == 1 && |
| 3026 | Exp->getCommon()->EvaluateAsInt(Ctx) == 0) return NoDiag(); |
| 3027 | return FalseResult; |
| 3028 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3029 | case Expr::ConditionalOperatorClass: { |
| 3030 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 3031 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 3032 | // then only the true side is actually considered in an integer constant |
| 3033 | // expression, and it is fully evaluated. This is an important GNU |
| 3034 | // extension. See GCC PR38377 for discussion. |
| 3035 | if (const CallExpr *CallCE |
| 3036 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
| 3037 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
| 3038 | Expr::EvalResult EVResult; |
| 3039 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 3040 | !EVResult.Val.isInt()) { |
| 3041 | return ICEDiag(2, E->getLocStart()); |
| 3042 | } |
| 3043 | return NoDiag(); |
| 3044 | } |
| 3045 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
| 3046 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 3047 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 3048 | if (CondResult.Val == 2) |
| 3049 | return CondResult; |
| 3050 | if (TrueResult.Val == 2) |
| 3051 | return TrueResult; |
| 3052 | if (FalseResult.Val == 2) |
| 3053 | return FalseResult; |
| 3054 | if (CondResult.Val == 1) |
| 3055 | return CondResult; |
| 3056 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 3057 | return NoDiag(); |
| 3058 | // Rare case where the diagnostics depend on which side is evaluated |
| 3059 | // Note that if we get here, CondResult is 0, and at least one of |
| 3060 | // TrueResult and FalseResult is non-zero. |
| 3061 | if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { |
| 3062 | return FalseResult; |
| 3063 | } |
| 3064 | return TrueResult; |
| 3065 | } |
| 3066 | case Expr::CXXDefaultArgExprClass: |
| 3067 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 3068 | case Expr::ChooseExprClass: { |
| 3069 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | // Silence a GCC warning |
| 3074 | return ICEDiag(2, E->getLocStart()); |
| 3075 | } |
| 3076 | |
| 3077 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 3078 | SourceLocation *Loc, bool isEvaluated) const { |
| 3079 | ICEDiag d = CheckICE(this, Ctx); |
| 3080 | if (d.Val != 0) { |
| 3081 | if (Loc) *Loc = d.Loc; |
| 3082 | return false; |
| 3083 | } |
| 3084 | EvalResult EvalResult; |
| 3085 | if (!Evaluate(EvalResult, Ctx)) |
| 3086 | llvm_unreachable("ICE cannot be evaluated!"); |
| 3087 | assert(!EvalResult.HasSideEffects && "ICE with side effects!"); |
| 3088 | assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); |
| 3089 | Result = EvalResult.Val.getInt(); |
| 3090 | return true; |
| 3091 | } |