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