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