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