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