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