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