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