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