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