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