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