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