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