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