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