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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTDiagnostic.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 23 | #include <cstring> |
| 24 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 27 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 29 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 30 | /// information about a subexpression as it is folded. It retains information |
| 31 | /// about the AST context, but also maintains information about the folded |
| 32 | /// expression. |
| 33 | /// |
| 34 | /// If an expression could be evaluated, it is still possible it is not a C |
| 35 | /// "integer constant expression" or constant expression. If not, this struct |
| 36 | /// captures information about how and why not. |
| 37 | /// |
| 38 | /// One bit of information passed *into* the request for constant folding |
| 39 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 40 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 41 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 42 | /// certain things in certain situations. |
| 43 | struct EvalInfo { |
| 44 | ASTContext &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 46 | /// EvalResult - Contains information about the evaluation. |
| 47 | Expr::EvalResult &EvalResult; |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 48 | |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 49 | /// AnyLValue - Stack based LValue results are not discarded. |
| 50 | bool AnyLValue; |
| 51 | |
| 52 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult, |
| 53 | bool anylvalue = false) |
| 54 | : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {} |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 58 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 59 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 60 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 61 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| 62 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 63 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 64 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 65 | |
| 66 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 67 | // Misc utilities |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 70 | static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { |
| 71 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 72 | // the check look like? |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 73 | Result = Value.getLValueBase() || !Value.getLValueOffset().isZero(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 77 | static bool HandleConversionToBool(const Expr* E, bool& Result, |
| 78 | EvalInfo &Info) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 79 | if (E->getType()->isIntegralType()) { |
| 80 | APSInt IntResult; |
| 81 | if (!EvaluateInteger(E, IntResult, Info)) |
| 82 | return false; |
| 83 | Result = IntResult != 0; |
| 84 | return true; |
| 85 | } else if (E->getType()->isRealFloatingType()) { |
| 86 | APFloat FloatResult(0.0); |
| 87 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 88 | return false; |
| 89 | Result = !FloatResult.isZero(); |
| 90 | return true; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 91 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 92 | APValue PointerResult; |
| 93 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 94 | return false; |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 95 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 96 | } else if (E->getType()->isAnyComplexType()) { |
| 97 | APValue ComplexResult; |
| 98 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 99 | return false; |
| 100 | if (ComplexResult.isComplexFloat()) { |
| 101 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 102 | !ComplexResult.getComplexFloatImag().isZero(); |
| 103 | } else { |
| 104 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 105 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 106 | } |
| 107 | return true; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 114 | APFloat &Value, ASTContext &Ctx) { |
| 115 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 116 | // Determine whether we are converting to unsigned or signed. |
| 117 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 119 | // FIXME: Warning for overflow. |
| 120 | uint64_t Space[4]; |
| 121 | bool ignored; |
| 122 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 123 | llvm::APFloat::rmTowardZero, &ignored); |
| 124 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 125 | } |
| 126 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 128 | APFloat &Value, ASTContext &Ctx) { |
| 129 | bool ignored; |
| 130 | APFloat Result = Value; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 132 | APFloat::rmNearestTiesToEven, &ignored); |
| 133 | return Result; |
| 134 | } |
| 135 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 137 | APSInt &Value, ASTContext &Ctx) { |
| 138 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 139 | APSInt Result = Value; |
| 140 | // Figure out if this is a truncate, extend or noop cast. |
| 141 | // If the input is signed, do a sign extend, noop, or truncate. |
| 142 | Result.extOrTrunc(DestWidth); |
| 143 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 144 | return Result; |
| 145 | } |
| 146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 148 | APSInt &Value, ASTContext &Ctx) { |
| 149 | |
| 150 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 151 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 152 | APFloat::rmNearestTiesToEven); |
| 153 | return Result; |
| 154 | } |
| 155 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 156 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 157 | class HasSideEffect |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 158 | : public StmtVisitor<HasSideEffect, bool> { |
| 159 | EvalInfo &Info; |
| 160 | public: |
| 161 | |
| 162 | HasSideEffect(EvalInfo &info) : Info(info) {} |
| 163 | |
| 164 | // Unhandled nodes conservatively default to having side effects. |
| 165 | bool VisitStmt(Stmt *S) { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 170 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 171 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 172 | return true; |
| 173 | return false; |
| 174 | } |
| 175 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 176 | // a ton of code. |
| 177 | bool VisitBlockExpr(BlockExpr *E) { return true; } |
| 178 | bool VisitPredefinedExpr(PredefinedExpr *E) { return false; } |
| 179 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 180 | { return Visit(E->getInitializer()); } |
| 181 | bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); } |
| 182 | bool VisitIntegerLiteral(IntegerLiteral *E) { return false; } |
| 183 | bool VisitFloatingLiteral(FloatingLiteral *E) { return false; } |
| 184 | bool VisitStringLiteral(StringLiteral *E) { return false; } |
| 185 | bool VisitCharacterLiteral(CharacterLiteral *E) { return false; } |
| 186 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; } |
| 187 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 188 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 189 | bool VisitChooseExpr(ChooseExpr *E) |
| 190 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 191 | bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); } |
| 192 | bool VisitBinAssign(BinaryOperator *E) { return true; } |
Mike Stump | 3f0147e | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 193 | bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; } |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 194 | bool VisitBinaryOperator(BinaryOperator *E) |
| 195 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 196 | bool VisitUnaryPreInc(UnaryOperator *E) { return true; } |
| 197 | bool VisitUnaryPostInc(UnaryOperator *E) { return true; } |
| 198 | bool VisitUnaryPreDec(UnaryOperator *E) { return true; } |
| 199 | bool VisitUnaryPostDec(UnaryOperator *E) { return true; } |
| 200 | bool VisitUnaryDeref(UnaryOperator *E) { |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 201 | if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 202 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 203 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 204 | } |
| 205 | bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); } |
| 206 | }; |
| 207 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 208 | } // end anonymous namespace |
| 209 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
| 211 | // LValue Evaluation |
| 212 | //===----------------------------------------------------------------------===// |
| 213 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 214 | class LValueExprEvaluator |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 215 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 216 | EvalInfo &Info; |
| 217 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 219 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 220 | |
| 221 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 222 | return APValue(); |
| 223 | } |
| 224 | |
| 225 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 226 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 227 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 228 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 229 | APValue VisitMemberExpr(MemberExpr *E); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 230 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); } |
| 231 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 232 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 233 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 234 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 235 | { return Visit(E->getSubExpr()); } |
| 236 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 237 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 238 | |
| 239 | APValue VisitCastExpr(CastExpr *E) { |
| 240 | switch (E->getCastKind()) { |
| 241 | default: |
| 242 | return APValue(); |
| 243 | |
| 244 | case CastExpr::CK_NoOp: |
| 245 | return Visit(E->getSubExpr()); |
| 246 | } |
| 247 | } |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 248 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 249 | }; |
| 250 | } // end anonymous namespace |
| 251 | |
| 252 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 253 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 254 | return Result.isLValue(); |
| 255 | } |
| 256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 258 | if (isa<FunctionDecl>(E->getDecl())) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 259 | return APValue(E); |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 260 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 261 | if (!Info.AnyLValue && !VD->hasGlobalStorage()) |
Eli Friedman | d933a01 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 262 | return APValue(); |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 263 | if (!VD->getType()->isReferenceType()) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 264 | return APValue(E); |
Eli Friedman | d933a01 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 265 | // FIXME: Check whether VD might be overridden! |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 266 | const VarDecl *Def = 0; |
| 267 | if (const Expr *Init = VD->getDefinition(Def)) |
| 268 | return Visit(const_cast<Expr *>(Init)); |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return APValue(); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 274 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 275 | if (!Info.AnyLValue && !E->isFileScope()) |
| 276 | return APValue(); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 277 | return APValue(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 281 | APValue result; |
| 282 | QualType Ty; |
| 283 | if (E->isArrow()) { |
| 284 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 285 | return APValue(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 286 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 287 | } else { |
| 288 | result = Visit(E->getBase()); |
| 289 | if (result.isUninit()) |
| 290 | return APValue(); |
| 291 | Ty = E->getBase()->getType(); |
| 292 | } |
| 293 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 294 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 295 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 296 | |
| 297 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 298 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 299 | return APValue(); |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 300 | |
| 301 | if (FD->getType()->isReferenceType()) |
| 302 | return APValue(); |
| 303 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 304 | // FIXME: This is linear time. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 305 | unsigned i = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 306 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 307 | FieldEnd = RD->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 308 | Field != FieldEnd; (void)++Field, ++i) { |
| 309 | if (*Field == FD) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | |
| 313 | result.setLValue(result.getLValueBase(), |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 314 | result.getLValueOffset() + |
| 315 | CharUnits::fromQuantity(RL.getFieldOffset(i) / 8)); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 316 | |
| 317 | return result; |
| 318 | } |
| 319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 321 | APValue Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 323 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 324 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 326 | APSInt Index; |
| 327 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 328 | return APValue(); |
| 329 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 330 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 331 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 332 | CharUnits Offset = Index.getSExtValue() * ElementSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | Result.setLValue(Result.getLValueBase(), |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 334 | Result.getLValueOffset() + Offset); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 335 | return Result; |
| 336 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 339 | APValue Result; |
| 340 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 341 | return APValue(); |
| 342 | return Result; |
| 343 | } |
| 344 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 345 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 346 | // Pointer Evaluation |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 349 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 350 | class PointerExprEvaluator |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 351 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 352 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 353 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 355 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 357 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 358 | return APValue(); |
| 359 | } |
| 360 | |
| 361 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 362 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 363 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 364 | APValue VisitCastExpr(CastExpr* E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 365 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 366 | { return Visit(E->getSubExpr()); } |
| 367 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 368 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 369 | { return APValue(E); } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 370 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 371 | { return APValue(E); } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 372 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 373 | APValue VisitBlockExpr(BlockExpr *E) { |
| 374 | if (!E->hasBlockDeclRefExprs()) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 375 | return APValue(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 376 | return APValue(); |
| 377 | } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 378 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 379 | { return APValue((Expr*)0); } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 380 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 381 | APValue VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 382 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 383 | APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 384 | { return APValue((Expr*)0); } |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 385 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 386 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 387 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 389 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 390 | if (!E->getType()->hasPointerRepresentation()) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 391 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 392 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 393 | return Result.isLValue(); |
| 394 | } |
| 395 | |
| 396 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 397 | if (E->getOpcode() != BinaryOperator::Add && |
| 398 | E->getOpcode() != BinaryOperator::Sub) |
| 399 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 401 | const Expr *PExp = E->getLHS(); |
| 402 | const Expr *IExp = E->getRHS(); |
| 403 | if (IExp->getType()->isPointerType()) |
| 404 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 406 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 407 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 408 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 410 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 411 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 412 | return APValue(); |
| 413 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 414 | QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 415 | CharUnits SizeOfPointee; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 417 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 418 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 419 | SizeOfPointee = CharUnits::One(); |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 420 | else |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 421 | SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 422 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 423 | CharUnits Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 424 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 425 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 426 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 427 | else |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 428 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 429 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 430 | return APValue(ResultLValue.getLValueBase(), Offset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 431 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 432 | |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 433 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 434 | APValue result; |
| 435 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 436 | return result; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 437 | return APValue(); |
| 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 440 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 441 | APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) { |
| 442 | Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 443 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 444 | switch (E->getCastKind()) { |
| 445 | default: |
| 446 | break; |
| 447 | |
| 448 | case CastExpr::CK_Unknown: { |
| 449 | // FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary! |
| 450 | |
| 451 | // Check for pointer->pointer cast |
| 452 | if (SubExpr->getType()->isPointerType() || |
| 453 | SubExpr->getType()->isObjCObjectPointerType() || |
| 454 | SubExpr->getType()->isNullPtrType() || |
| 455 | SubExpr->getType()->isBlockPointerType()) |
| 456 | return Visit(SubExpr); |
| 457 | |
| 458 | if (SubExpr->getType()->isIntegralType()) { |
| 459 | APValue Result; |
| 460 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 461 | break; |
| 462 | |
| 463 | if (Result.isInt()) { |
| 464 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 465 | return APValue(0, |
| 466 | CharUnits::fromQuantity(Result.getInt().getZExtValue())); |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // Cast is of an lvalue, no need to change value. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 470 | return Result; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 471 | } |
| 472 | break; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 475 | case CastExpr::CK_NoOp: |
| 476 | case CastExpr::CK_BitCast: |
| 477 | case CastExpr::CK_AnyPointerToObjCPointerCast: |
| 478 | case CastExpr::CK_AnyPointerToBlockPointerCast: |
| 479 | return Visit(SubExpr); |
| 480 | |
| 481 | case CastExpr::CK_IntegralToPointer: { |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 482 | APValue Result; |
| 483 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 484 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 485 | |
| 486 | if (Result.isInt()) { |
| 487 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 488 | return APValue(0, |
| 489 | CharUnits::fromQuantity(Result.getInt().getZExtValue())); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 492 | // Cast is of an lvalue, no need to change value. |
| 493 | return Result; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 494 | } |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 495 | case CastExpr::CK_ArrayToPointerDecay: |
| 496 | case CastExpr::CK_FunctionToPointerDecay: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 497 | APValue Result; |
| 498 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 499 | return Result; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 500 | break; |
| 501 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 504 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 506 | |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 507 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | if (E->isBuiltinCall(Info.Ctx) == |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 509 | Builtin::BI__builtin___CFStringMakeConstantString) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 510 | return APValue(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 511 | return APValue(); |
| 512 | } |
| 513 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 514 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 515 | bool BoolResult; |
| 516 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 517 | return APValue(); |
| 518 | |
| 519 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 520 | |
| 521 | APValue Result; |
| 522 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 523 | return Result; |
| 524 | return APValue(); |
| 525 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 526 | |
| 527 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 528 | // Vector Evaluation |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | |
| 531 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 532 | class VectorExprEvaluator |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 533 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 534 | EvalInfo &Info; |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 535 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 536 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 538 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 540 | APValue VisitStmt(Stmt *S) { |
| 541 | return APValue(); |
| 542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 544 | APValue VisitParenExpr(ParenExpr *E) |
| 545 | { return Visit(E->getSubExpr()); } |
| 546 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 547 | { return Visit(E->getSubExpr()); } |
| 548 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 549 | { return Visit(E->getSubExpr()); } |
| 550 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 551 | { return Visit(E->getSubExpr()); } |
| 552 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 553 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 554 | APValue VisitCastExpr(const CastExpr* E); |
| 555 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 556 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 557 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 558 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 559 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 560 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 561 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 562 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 563 | // shufflevector, ExtVectorElementExpr |
| 564 | // (Note that these require implementing conversions |
| 565 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 566 | }; |
| 567 | } // end anonymous namespace |
| 568 | |
| 569 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 570 | if (!E->getType()->isVectorType()) |
| 571 | return false; |
| 572 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 573 | return !Result.isUninit(); |
| 574 | } |
| 575 | |
| 576 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 577 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 578 | QualType EltTy = VTy->getElementType(); |
| 579 | unsigned NElts = VTy->getNumElements(); |
| 580 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 582 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 583 | QualType SETy = SE->getType(); |
| 584 | APValue Result = APValue(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 585 | |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 586 | // Check for vector->vector bitcast and scalar->vector splat. |
| 587 | if (SETy->isVectorType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 588 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 589 | } else if (SETy->isIntegerType()) { |
| 590 | APSInt IntResult; |
Daniel Dunbar | d906dc7 | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 591 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 592 | return APValue(); |
| 593 | Result = APValue(IntResult); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 594 | } else if (SETy->isRealFloatingType()) { |
| 595 | APFloat F(0.0); |
Daniel Dunbar | d906dc7 | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 596 | if (!EvaluateFloat(SE, F, Info)) |
| 597 | return APValue(); |
| 598 | Result = APValue(F); |
| 599 | } else |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 600 | return APValue(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 601 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 602 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 603 | // and splat it to all elements. |
| 604 | if (E->getType()->isExtVectorType()) { |
| 605 | if (EltTy->isIntegerType() && Result.isInt()) |
| 606 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 607 | Info.Ctx)); |
| 608 | else if (EltTy->isIntegerType()) |
| 609 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 610 | Info.Ctx)); |
| 611 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 612 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 613 | Info.Ctx)); |
| 614 | else if (EltTy->isRealFloatingType()) |
| 615 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 616 | Info.Ctx)); |
| 617 | else |
| 618 | return APValue(); |
| 619 | |
| 620 | // Splat and create vector APValue. |
| 621 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 622 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 623 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 624 | |
| 625 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 626 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 627 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 628 | // element. |
| 629 | APSInt Init; |
| 630 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 632 | llvm::SmallVector<APValue, 4> Elts; |
| 633 | for (unsigned i = 0; i != NElts; ++i) { |
| 634 | APSInt Tmp = Init; |
| 635 | Tmp.extOrTrunc(EltWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 637 | if (EltTy->isIntegerType()) |
| 638 | Elts.push_back(APValue(Tmp)); |
| 639 | else if (EltTy->isRealFloatingType()) |
| 640 | Elts.push_back(APValue(APFloat(Tmp))); |
| 641 | else |
| 642 | return APValue(); |
| 643 | |
| 644 | Init >>= EltWidth; |
| 645 | } |
| 646 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | APValue |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 650 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 651 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 652 | } |
| 653 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | APValue |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 655 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 656 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 657 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 658 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 660 | QualType EltTy = VT->getElementType(); |
| 661 | llvm::SmallVector<APValue, 4> Elements; |
| 662 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 663 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 664 | if (EltTy->isIntegerType()) { |
| 665 | llvm::APSInt sInt(32); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 666 | if (i < NumInits) { |
| 667 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 668 | return APValue(); |
| 669 | } else { |
| 670 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 671 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 672 | Elements.push_back(APValue(sInt)); |
| 673 | } else { |
| 674 | llvm::APFloat f(0.0); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 675 | if (i < NumInits) { |
| 676 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 677 | return APValue(); |
| 678 | } else { |
| 679 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 680 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 681 | Elements.push_back(APValue(f)); |
| 682 | } |
| 683 | } |
| 684 | return APValue(&Elements[0], Elements.size()); |
| 685 | } |
| 686 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | APValue |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 688 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 689 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 690 | QualType EltTy = VT->getElementType(); |
| 691 | APValue ZeroElement; |
| 692 | if (EltTy->isIntegerType()) |
| 693 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 694 | else |
| 695 | ZeroElement = |
| 696 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 697 | |
| 698 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 699 | return APValue(&Elements[0], Elements.size()); |
| 700 | } |
| 701 | |
| 702 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 703 | bool BoolResult; |
| 704 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 705 | return APValue(); |
| 706 | |
| 707 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 708 | |
| 709 | APValue Result; |
| 710 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 711 | return Result; |
| 712 | return APValue(); |
| 713 | } |
| 714 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 715 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 716 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 717 | Info.EvalResult.HasSideEffects = true; |
| 718 | return GetZeroVector(E->getType()); |
| 719 | } |
| 720 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 721 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 722 | // Integer Evaluation |
| 723 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 724 | |
| 725 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 726 | class IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 727 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 728 | EvalInfo &Info; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 729 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 730 | public: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 731 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 732 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 733 | |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 734 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 735 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 736 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 737 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 738 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 739 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 740 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 741 | return true; |
| 742 | } |
| 743 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 744 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 745 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 746 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 747 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 748 | Result = APValue(APSInt(I)); |
| 749 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 750 | return true; |
| 751 | } |
| 752 | |
| 753 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 754 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 755 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 756 | return true; |
| 757 | } |
| 758 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 759 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 760 | // Take the first error. |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 761 | if (Info.EvalResult.Diag == 0) { |
| 762 | Info.EvalResult.DiagLoc = L; |
| 763 | Info.EvalResult.Diag = D; |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 764 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 765 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 766 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 767 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 769 | //===--------------------------------------------------------------------===// |
| 770 | // Visitor Methods |
| 771 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 773 | bool VisitStmt(Stmt *) { |
| 774 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 775 | return false; |
| 776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 778 | bool VisitExpr(Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 779 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 782 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 784 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 785 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 786 | } |
| 787 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 788 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 789 | } |
| 790 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 791 | // Per gcc docs "this built-in function ignores top level |
| 792 | // qualifiers". We need to use the canonical version to properly |
| 793 | // be able to strip CRV qualifiers from the type. |
| 794 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 795 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 797 | T1.getUnqualifiedType()), |
| 798 | E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 799 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 800 | |
| 801 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 802 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 803 | return CheckReferencedDecl(E, E->getDecl()); |
| 804 | } |
| 805 | bool VisitMemberExpr(const MemberExpr *E) { |
| 806 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
| 807 | // Conservatively assume a MemberExpr will have side-effects |
| 808 | Info.EvalResult.HasSideEffects = true; |
| 809 | return true; |
| 810 | } |
| 811 | return false; |
| 812 | } |
| 813 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 814 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 815 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 816 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 817 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 818 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 819 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 820 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 821 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 822 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 823 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 826 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 827 | return Success(0, E); |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 830 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 831 | return Success(0, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 834 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 835 | return Success(0, E); |
| 836 | } |
| 837 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 838 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 839 | return Success(E->EvaluateTrait(Info.Ctx), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 842 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 843 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 844 | } |
| 845 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 846 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 847 | bool VisitUnaryImag(const UnaryOperator *E); |
| 848 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 849 | private: |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 850 | unsigned GetAlignOfExpr(const Expr *E); |
| 851 | unsigned GetAlignOfType(QualType T); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 852 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 853 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 854 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 856 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 857 | if (!E->getType()->isIntegralType()) |
| 858 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 860 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 861 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 863 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 864 | APValue Val; |
| 865 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 866 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 867 | Result = Val.getInt(); |
| 868 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 869 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 870 | |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 871 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 872 | // Enums are integer constant exprs. |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 873 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 874 | return Success(ECD->getInitVal(), E); |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 875 | |
| 876 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | e1646da | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 877 | // In C, they can also be folded, although they are not ICEs. |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 878 | if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers() |
| 879 | == Qualifiers::Const) { |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 880 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 881 | const VarDecl *Def = 0; |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 882 | if (const Expr *Init = VD->getDefinition(Def)) { |
Eli Friedman | c013118 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 883 | if (APValue *V = VD->getEvaluatedValue()) { |
| 884 | if (V->isInt()) |
| 885 | return Success(V->getInt(), E); |
| 886 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 887 | } |
| 888 | |
| 889 | if (VD->isEvaluatingValue()) |
| 890 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 891 | |
| 892 | VD->setEvaluatingValue(); |
| 893 | |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 894 | if (Visit(const_cast<Expr*>(Init))) { |
| 895 | // Cache the evaluated value in the variable declaration. |
Eli Friedman | c013118 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 896 | VD->setEvaluatedValue(Result); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 897 | return true; |
| 898 | } |
| 899 | |
Eli Friedman | c013118 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 900 | VD->setEvaluatedValue(APValue()); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 901 | return false; |
| 902 | } |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 906 | // Otherwise, random variable references are not constants. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 907 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 910 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 911 | /// as GCC. |
| 912 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 913 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 914 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 915 | enum gcc_type_class { |
| 916 | no_type_class = -1, |
| 917 | void_type_class, integer_type_class, char_type_class, |
| 918 | enumeral_type_class, boolean_type_class, |
| 919 | pointer_type_class, reference_type_class, offset_type_class, |
| 920 | real_type_class, complex_type_class, |
| 921 | function_type_class, method_type_class, |
| 922 | record_type_class, union_type_class, |
| 923 | array_type_class, string_type_class, |
| 924 | lang_type_class |
| 925 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
| 927 | // 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] | 928 | // ideal, however it is what gcc does. |
| 929 | if (E->getNumArgs() == 0) |
| 930 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 932 | QualType ArgTy = E->getArg(0)->getType(); |
| 933 | if (ArgTy->isVoidType()) |
| 934 | return void_type_class; |
| 935 | else if (ArgTy->isEnumeralType()) |
| 936 | return enumeral_type_class; |
| 937 | else if (ArgTy->isBooleanType()) |
| 938 | return boolean_type_class; |
| 939 | else if (ArgTy->isCharType()) |
| 940 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 941 | else if (ArgTy->isIntegerType()) |
| 942 | return integer_type_class; |
| 943 | else if (ArgTy->isPointerType()) |
| 944 | return pointer_type_class; |
| 945 | else if (ArgTy->isReferenceType()) |
| 946 | return reference_type_class; |
| 947 | else if (ArgTy->isRealType()) |
| 948 | return real_type_class; |
| 949 | else if (ArgTy->isComplexType()) |
| 950 | return complex_type_class; |
| 951 | else if (ArgTy->isFunctionType()) |
| 952 | return function_type_class; |
| 953 | else if (ArgTy->isStructureType()) |
| 954 | return record_type_class; |
| 955 | else if (ArgTy->isUnionType()) |
| 956 | return union_type_class; |
| 957 | else if (ArgTy->isArrayType()) |
| 958 | return array_type_class; |
| 959 | else if (ArgTy->isUnionType()) |
| 960 | return union_type_class; |
| 961 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 962 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 963 | return -1; |
| 964 | } |
| 965 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 966 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 967 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 968 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 969 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 970 | |
| 971 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 972 | const Expr *Arg = E->getArg(0)->IgnoreParens(); |
| 973 | Expr::EvalResult Base; |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame^] | 974 | |
| 975 | // TODO: Perhaps we should let LLVM lower this? |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 976 | if (Arg->EvaluateAsAny(Base, Info.Ctx) |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 977 | && Base.Val.getKind() == APValue::LValue |
| 978 | && !Base.HasSideEffects) |
| 979 | if (const Expr *LVBase = Base.Val.getLValueBase()) |
| 980 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) { |
| 981 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Mike Stump | 460d138 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 982 | if (!VD->getType()->isIncompleteType() |
| 983 | && VD->getType()->isObjectType() |
| 984 | && !VD->getType()->isVariablyModifiedType() |
| 985 | && !VD->getType()->isDependentType()) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 986 | CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType()); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 987 | CharUnits Offset = Base.Val.getLValueOffset(); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 988 | if (!Offset.isNegative() && Offset <= Size) |
| 989 | Size -= Offset; |
Mike Stump | 460d138 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 990 | else |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 991 | Size = CharUnits::Zero(); |
| 992 | return Success(Size.getQuantity(), E); |
Mike Stump | 460d138 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 993 | } |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame^] | 997 | // If evaluating the argument has side-effects we can't determine |
| 998 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 999 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Benjamin Kramer | 3f27b38 | 2010-01-03 18:18:37 +0000 | [diff] [blame] | 1000 | if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 1001 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1002 | return Success(0, E); |
| 1003 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1004 | |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1005 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1006 | } |
| 1007 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1008 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1009 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1011 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1012 | // __builtin_constant_p always has one operand: it returns true if that |
| 1013 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1014 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1015 | |
| 1016 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 1017 | int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue(); |
| 1018 | Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand); |
| 1019 | return Success(Operand, E); |
| 1020 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1021 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1022 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1024 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1025 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1026 | if (!Visit(E->getRHS())) |
| 1027 | return false; |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1028 | |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1029 | // If we can't evaluate the LHS, it might have side effects; |
| 1030 | // conservatively mark it. |
| 1031 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 1032 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1033 | |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1034 | return true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (E->isLogicalOp()) { |
| 1038 | // These need to be handled specially because the operands aren't |
| 1039 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1040 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1042 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1043 | // We were able to evaluate the LHS, see if we can get away with not |
| 1044 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1045 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1046 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1047 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1048 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1049 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1050 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1051 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1052 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } else { |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1055 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1056 | // We can't evaluate the LHS; however, sometimes the result |
| 1057 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1059 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1060 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1061 | // must have had side effects. |
| 1062 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1063 | |
| 1064 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1067 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1068 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1072 | QualType LHSTy = E->getLHS()->getType(); |
| 1073 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (LHSTy->isAnyComplexType()) { |
| 1076 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 1077 | APValue LHS, RHS; |
| 1078 | |
| 1079 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 1080 | return false; |
| 1081 | |
| 1082 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 1083 | return false; |
| 1084 | |
| 1085 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1087 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1089 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 1090 | |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1091 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1092 | return Success((CR_r == APFloat::cmpEqual && |
| 1093 | CR_i == APFloat::cmpEqual), E); |
| 1094 | else { |
| 1095 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1096 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1098 | CR_r == APFloat::cmpLessThan) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | (CR_i == APFloat::cmpGreaterThan || |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1100 | CR_i == APFloat::cmpLessThan)), E); |
| 1101 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1102 | } else { |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1103 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1104 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 1105 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 1106 | else { |
| 1107 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1108 | "Invalid compex comparison."); |
| 1109 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 1110 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 1111 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1115 | if (LHSTy->isRealFloatingType() && |
| 1116 | RHSTy->isRealFloatingType()) { |
| 1117 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1119 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1120 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1122 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 1123 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1125 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 1126 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1127 | switch (E->getOpcode()) { |
| 1128 | default: |
| 1129 | assert(0 && "Invalid binary operator!"); |
| 1130 | case BinaryOperator::LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1131 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1132 | case BinaryOperator::GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1133 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1134 | case BinaryOperator::LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1135 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1136 | case BinaryOperator::GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1138 | E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1139 | case BinaryOperator::EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1140 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1141 | case BinaryOperator::NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | return Success(CR == APFloat::cmpGreaterThan |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1143 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1144 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1147 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 1148 | if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1149 | APValue LHSValue; |
| 1150 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1151 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1152 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1153 | APValue RHSValue; |
| 1154 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1155 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1156 | |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1157 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1158 | // to null. |
| 1159 | if (LHSValue.getLValueBase()) { |
| 1160 | if (!E->isEqualityOp()) |
| 1161 | return false; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1162 | if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero()) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | bool bres; |
| 1165 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1166 | return false; |
| 1167 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1168 | } else if (RHSValue.getLValueBase()) { |
| 1169 | if (!E->isEqualityOp()) |
| 1170 | return false; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1171 | if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero()) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | bool bres; |
| 1174 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1175 | return false; |
| 1176 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1177 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1178 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1179 | if (E->getOpcode() == BinaryOperator::Sub) { |
| 1180 | const QualType Type = E->getLHS()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1181 | const QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1182 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1183 | CharUnits ElementSize = CharUnits::One(); |
Eli Friedman | ce1bca7 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1184 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1185 | ElementSize = Info.Ctx.getTypeSizeInChars(ElementType); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1186 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1187 | CharUnits Diff = LHSValue.getLValueOffset() - |
| 1188 | RHSValue.getLValueOffset(); |
| 1189 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1190 | } |
| 1191 | bool Result; |
| 1192 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1193 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 267c0ab | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1194 | } else { |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1195 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1196 | } |
| 1197 | return Success(Result, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1200 | if (!LHSTy->isIntegralType() || |
| 1201 | !RHSTy->isIntegralType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1202 | // We can't continue from here for non-integral types, and they |
| 1203 | // could potentially confuse the following operations. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1204 | return false; |
| 1205 | } |
| 1206 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1207 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1208 | if (!Visit(E->getLHS())) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1209 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1210 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1211 | APValue RHSVal; |
| 1212 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1213 | return false; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1214 | |
| 1215 | // Handle cases like (unsigned long)&a + 4. |
| 1216 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1217 | CharUnits Offset = Result.getLValueOffset(); |
| 1218 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 1219 | RHSVal.getInt().getZExtValue()); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1220 | if (E->getOpcode() == BinaryOperator::Add) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1221 | Offset += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1222 | else |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1223 | Offset -= AdditionalOffset; |
| 1224 | Result = APValue(Result.getLValueBase(), Offset); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | // Handle cases like 4 + (unsigned long)&a |
| 1229 | if (E->getOpcode() == BinaryOperator::Add && |
| 1230 | RHSVal.isLValue() && Result.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1231 | CharUnits Offset = RHSVal.getLValueOffset(); |
| 1232 | Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue()); |
| 1233 | Result = APValue(RHSVal.getLValueBase(), Offset); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | // All the following cases expect both operands to be an integer |
| 1238 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1239 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1240 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1241 | APSInt& RHS = RHSVal.getInt(); |
| 1242 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1243 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1244 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1245 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1246 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 1247 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 1248 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 1249 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 1250 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 1251 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1252 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1253 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1254 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1255 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1256 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1257 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1258 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1259 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1260 | case BinaryOperator::Shl: { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1261 | // FIXME: Warn about out of range shift amounts! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | unsigned SA = |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1263 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1264 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1265 | } |
| 1266 | case BinaryOperator::Shr: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | unsigned SA = |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1268 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1269 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1272 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 1273 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 1274 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 1275 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 1276 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 1277 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1278 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1281 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1282 | bool Cond; |
| 1283 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1284 | return false; |
| 1285 | |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1286 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1289 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1290 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1291 | // the result is the size of the referenced type." |
| 1292 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1293 | // result shall be the alignment of the referenced type." |
| 1294 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 1295 | T = Ref->getPointeeType(); |
| 1296 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1297 | // Get information about the alignment. |
| 1298 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1299 | |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1300 | // __alignof is defined to return the preferred alignment. |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1301 | return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize; |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1304 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 1305 | E = E->IgnoreParens(); |
| 1306 | |
| 1307 | // 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] | 1308 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1309 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1310 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1312 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1313 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(), |
| 1314 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1316 | return GetAlignOfType(E->getType()); |
| 1317 | } |
| 1318 | |
| 1319 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1320 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1321 | /// expression's type. |
| 1322 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1323 | // Handle alignof separately. |
| 1324 | if (!E->isSizeOf()) { |
| 1325 | if (E->isArgumentType()) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1326 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1327 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1328 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1329 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1330 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1331 | QualType SrcTy = E->getTypeOfArgument(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1332 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1333 | // the result is the size of the referenced type." |
| 1334 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1335 | // result shall be the alignment of the referenced type." |
| 1336 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 1337 | SrcTy = Ref->getPointeeType(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1338 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1339 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1340 | // extension. |
| 1341 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1342 | return Success(1, E); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1343 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1344 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1345 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1346 | return false; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1348 | // Get information about the size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1349 | return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1352 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1353 | // Special case unary operators that do not need their subexpression |
| 1354 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1355 | if (E->isOffsetOfOp()) { |
| 1356 | // The AST for offsetof is defined in such a way that we can just |
| 1357 | // directly Evaluate it as an l-value. |
| 1358 | APValue LV; |
| 1359 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
| 1360 | return false; |
| 1361 | if (LV.getLValueBase()) |
| 1362 | return false; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1363 | return Success(LV.getLValueOffset().getQuantity(), E); |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1364 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1365 | |
| 1366 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1367 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1368 | bool bres; |
| 1369 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1370 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1371 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1374 | // Only handle integral operations... |
| 1375 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1376 | return false; |
| 1377 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1378 | // Get the operand value into 'Result'. |
| 1379 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1380 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1382 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1383 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1384 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1385 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1386 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1387 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1388 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1389 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1390 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1391 | case UnaryOperator::Plus: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | // The result is always just the subexpr. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1393 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1394 | case UnaryOperator::Minus: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1395 | if (!Result.isInt()) return false; |
| 1396 | return Success(-Result.getInt(), E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1397 | case UnaryOperator::Not: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1398 | if (!Result.isInt()) return false; |
| 1399 | return Success(~Result.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1400 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1403 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1404 | /// result type is integer. |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1405 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1406 | Expr *SubExpr = E->getSubExpr(); |
| 1407 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1408 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1409 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1410 | if (DestType->isBooleanType()) { |
| 1411 | bool BoolResult; |
| 1412 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1413 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1414 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1417 | // Handle simple integer->integer casts. |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1418 | if (SrcType->isIntegralType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1419 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1420 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1421 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1422 | if (!Result.isInt()) { |
| 1423 | // Only allow casts of lvalues if they are lossless. |
| 1424 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1425 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1426 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1427 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1428 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1431 | // FIXME: Clean this up! |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1432 | if (SrcType->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1433 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1434 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1435 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1436 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1437 | if (LV.getLValueBase()) { |
| 1438 | // Only allow based lvalue casts if they are lossless. |
| 1439 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1440 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1441 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1442 | Result = LV; |
| 1443 | return true; |
| 1444 | } |
| 1445 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 1446 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 1447 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1448 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1449 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1450 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1451 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1452 | // This handles double-conversion cases, where there's both |
| 1453 | // an l-value promotion and an implicit conversion to int. |
| 1454 | APValue LV; |
| 1455 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1456 | return false; |
| 1457 | |
| 1458 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1459 | return false; |
| 1460 | |
| 1461 | Result = LV; |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1465 | if (SrcType->isAnyComplexType()) { |
| 1466 | APValue C; |
| 1467 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1468 | return false; |
| 1469 | if (C.isComplexFloat()) |
| 1470 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1471 | C.getComplexFloatReal(), Info.Ctx), |
| 1472 | E); |
| 1473 | else |
| 1474 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1475 | C.getComplexIntReal(), Info.Ctx), E); |
| 1476 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1477 | // FIXME: Handle vectors |
| 1478 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1479 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1480 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1481 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1482 | APFloat F(0.0); |
| 1483 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1484 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1486 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1487 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1488 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1489 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1490 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1491 | APValue LV; |
| 1492 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1493 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1494 | return Success(LV.getComplexIntReal(), E); |
| 1495 | } |
| 1496 | |
| 1497 | return Visit(E->getSubExpr()); |
| 1498 | } |
| 1499 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1500 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1501 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1502 | APValue LV; |
| 1503 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1504 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1505 | return Success(LV.getComplexIntImag(), E); |
| 1506 | } |
| 1507 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1508 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1509 | Info.EvalResult.HasSideEffects = true; |
| 1510 | return Success(0, E); |
| 1511 | } |
| 1512 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1513 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1514 | // Float Evaluation |
| 1515 | //===----------------------------------------------------------------------===// |
| 1516 | |
| 1517 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1518 | class FloatExprEvaluator |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1519 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1520 | EvalInfo &Info; |
| 1521 | APFloat &Result; |
| 1522 | public: |
| 1523 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1524 | : Info(info), Result(result) {} |
| 1525 | |
| 1526 | bool VisitStmt(Stmt *S) { |
| 1527 | return false; |
| 1528 | } |
| 1529 | |
| 1530 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1531 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1532 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1533 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1534 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1535 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1536 | bool VisitCastExpr(CastExpr *E); |
| 1537 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 67f85fc | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1538 | bool VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1539 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1540 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1541 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1542 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1543 | { return Visit(E->getSubExpr()); } |
| 1544 | |
| 1545 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
Eli Friedman | 67f85fc | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1546 | // member of vector, ImplicitValueInitExpr |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1547 | }; |
| 1548 | } // end anonymous namespace |
| 1549 | |
| 1550 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1551 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1552 | } |
| 1553 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1554 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1555 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1556 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1557 | case Builtin::BI__builtin_huge_val: |
| 1558 | case Builtin::BI__builtin_huge_valf: |
| 1559 | case Builtin::BI__builtin_huge_vall: |
| 1560 | case Builtin::BI__builtin_inf: |
| 1561 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1562 | case Builtin::BI__builtin_infl: { |
| 1563 | const llvm::fltSemantics &Sem = |
| 1564 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1565 | Result = llvm::APFloat::getInf(Sem); |
| 1566 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1569 | case Builtin::BI__builtin_nan: |
| 1570 | case Builtin::BI__builtin_nanf: |
| 1571 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1572 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1573 | // can't constant fold it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | if (const StringLiteral *S = |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1575 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1576 | if (!S->isWide()) { |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1577 | const llvm::fltSemantics &Sem = |
| 1578 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Benjamin Kramer | 3303580 | 2009-12-11 13:26:32 +0000 | [diff] [blame] | 1579 | unsigned Type = 0; |
| 1580 | if (!S->getString().empty() && S->getString().getAsInteger(0, Type)) |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1581 | return false; |
Benjamin Kramer | 3303580 | 2009-12-11 13:26:32 +0000 | [diff] [blame] | 1582 | Result = llvm::APFloat::getNaN(Sem, false, Type); |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1583 | return true; |
| 1584 | } |
| 1585 | } |
| 1586 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1587 | |
| 1588 | case Builtin::BI__builtin_fabs: |
| 1589 | case Builtin::BI__builtin_fabsf: |
| 1590 | case Builtin::BI__builtin_fabsl: |
| 1591 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1592 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1594 | if (Result.isNegative()) |
| 1595 | Result.changeSign(); |
| 1596 | return true; |
| 1597 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | case Builtin::BI__builtin_copysign: |
| 1599 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1600 | case Builtin::BI__builtin_copysignl: { |
| 1601 | APFloat RHS(0.); |
| 1602 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1603 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1604 | return false; |
| 1605 | Result.copySign(RHS); |
| 1606 | return true; |
| 1607 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1611 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | a468d34 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1612 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1613 | return false; |
| 1614 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1615 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1616 | return false; |
| 1617 | |
| 1618 | switch (E->getOpcode()) { |
| 1619 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | case UnaryOperator::Plus: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1621 | return true; |
| 1622 | case UnaryOperator::Minus: |
| 1623 | Result.changeSign(); |
| 1624 | return true; |
| 1625 | } |
| 1626 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1627 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1628 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 1629 | if (E->getOpcode() == BinaryOperator::Comma) { |
| 1630 | if (!EvaluateFloat(E->getRHS(), Result, Info)) |
| 1631 | return false; |
| 1632 | |
| 1633 | // If we can't evaluate the LHS, it might have side effects; |
| 1634 | // conservatively mark it. |
| 1635 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 1636 | Info.EvalResult.HasSideEffects = true; |
| 1637 | |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1641 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1642 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1643 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1644 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1645 | return false; |
| 1646 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1647 | return false; |
| 1648 | |
| 1649 | switch (E->getOpcode()) { |
| 1650 | default: return false; |
| 1651 | case BinaryOperator::Mul: |
| 1652 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1653 | return true; |
| 1654 | case BinaryOperator::Add: |
| 1655 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1656 | return true; |
| 1657 | case BinaryOperator::Sub: |
| 1658 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1659 | return true; |
| 1660 | case BinaryOperator::Div: |
| 1661 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1662 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1667 | Result = E->getValue(); |
| 1668 | return true; |
| 1669 | } |
| 1670 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1671 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1672 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1674 | if (SubExpr->getType()->isIntegralType()) { |
| 1675 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1676 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1677 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1679 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1680 | return true; |
| 1681 | } |
| 1682 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1683 | if (!Visit(SubExpr)) |
| 1684 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1685 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1686 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1687 | return true; |
| 1688 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1689 | // FIXME: Handle complex types |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1690 | |
| 1691 | return false; |
| 1692 | } |
| 1693 | |
| 1694 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1695 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1696 | return true; |
| 1697 | } |
| 1698 | |
Eli Friedman | 67f85fc | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 1699 | bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 1700 | bool Cond; |
| 1701 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
| 1702 | return false; |
| 1703 | |
| 1704 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 1705 | } |
| 1706 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1707 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1708 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1709 | //===----------------------------------------------------------------------===// |
| 1710 | |
| 1711 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1712 | class ComplexExprEvaluator |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1713 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1714 | EvalInfo &Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1716 | public: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1717 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1719 | //===--------------------------------------------------------------------===// |
| 1720 | // Visitor Methods |
| 1721 | //===--------------------------------------------------------------------===// |
| 1722 | |
| 1723 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1724 | return APValue(); |
| 1725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1727 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1728 | |
| 1729 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1730 | Expr* SubExpr = E->getSubExpr(); |
| 1731 | |
| 1732 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1733 | APFloat Result(0.0); |
| 1734 | |
| 1735 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1736 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
| 1738 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1739 | Result); |
| 1740 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | assert(SubExpr->getType()->isIntegerType() && |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1742 | "Unexpected imaginary literal."); |
| 1743 | |
| 1744 | llvm::APSInt Result; |
| 1745 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1746 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1748 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1749 | Zero = 0; |
| 1750 | return APValue(Zero, Result); |
| 1751 | } |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1754 | APValue VisitCastExpr(CastExpr *E) { |
| 1755 | Expr* SubExpr = E->getSubExpr(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1756 | QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1757 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1758 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1759 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1760 | APFloat Result(0.0); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1761 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1762 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1763 | return APValue(); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (EltType->isRealFloatingType()) { |
| 1766 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | return APValue(Result, |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1768 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
| 1769 | } else { |
| 1770 | llvm::APSInt IResult; |
| 1771 | IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1772 | llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); |
| 1773 | Zero = 0; |
| 1774 | return APValue(IResult, Zero); |
| 1775 | } |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1776 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1777 | APSInt Result; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1778 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1779 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1780 | return APValue(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1781 | |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1782 | if (EltType->isRealFloatingType()) { |
| 1783 | APFloat FResult = |
| 1784 | HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | return APValue(FResult, |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1786 | APFloat(FResult.getSemantics(), APFloat::fcZero, false)); |
| 1787 | } else { |
| 1788 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1789 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1790 | Zero = 0; |
| 1791 | return APValue(Result, Zero); |
| 1792 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1793 | } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1794 | APValue Src; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1795 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1796 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1797 | return APValue(); |
| 1798 | |
| 1799 | QualType SrcType = CT->getElementType(); |
| 1800 | |
| 1801 | if (Src.isComplexFloat()) { |
| 1802 | if (EltType->isRealFloatingType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1804 | Src.getComplexFloatReal(), |
| 1805 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1807 | Src.getComplexFloatImag(), |
| 1808 | Info.Ctx)); |
| 1809 | } else { |
| 1810 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1811 | Src.getComplexFloatReal(), |
| 1812 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | HandleFloatToIntCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1814 | Src.getComplexFloatImag(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | Info.Ctx)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1816 | } |
| 1817 | } else { |
| 1818 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1819 | if (EltType->isRealFloatingType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1821 | Src.getComplexIntReal(), |
| 1822 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1824 | Src.getComplexIntImag(), |
| 1825 | Info.Ctx)); |
| 1826 | } else { |
| 1827 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1828 | Src.getComplexIntReal(), |
| 1829 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | HandleIntToIntCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1831 | Src.getComplexIntImag(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | Info.Ctx)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | // FIXME: Handle more casts. |
| 1838 | return APValue(); |
| 1839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1841 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1842 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1843 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1844 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1845 | { return Visit(E->getSubExpr()); } |
| 1846 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1847 | // conditional ?:, comma |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1848 | }; |
| 1849 | } // end anonymous namespace |
| 1850 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1852 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1853 | assert((!Result.isComplexFloat() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | (&Result.getComplexFloatReal().getSemantics() == |
| 1855 | &Result.getComplexFloatImag().getSemantics())) && |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1856 | "Invalid complex evaluation."); |
| 1857 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1861 | APValue Result, RHS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1863 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1864 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1866 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1867 | return APValue(); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1868 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1869 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1870 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1871 | switch (E->getOpcode()) { |
| 1872 | default: return APValue(); |
| 1873 | case BinaryOperator::Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1874 | if (Result.isComplexFloat()) { |
| 1875 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1876 | APFloat::rmNearestTiesToEven); |
| 1877 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1878 | APFloat::rmNearestTiesToEven); |
| 1879 | } else { |
| 1880 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1881 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1882 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1883 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1884 | case BinaryOperator::Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1885 | if (Result.isComplexFloat()) { |
| 1886 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1887 | APFloat::rmNearestTiesToEven); |
| 1888 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1889 | APFloat::rmNearestTiesToEven); |
| 1890 | } else { |
| 1891 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1892 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1893 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1894 | break; |
| 1895 | case BinaryOperator::Mul: |
| 1896 | if (Result.isComplexFloat()) { |
| 1897 | APValue LHS = Result; |
| 1898 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1899 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1900 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1901 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1903 | APFloat Tmp = LHS_r; |
| 1904 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1905 | Result.getComplexFloatReal() = Tmp; |
| 1906 | Tmp = LHS_i; |
| 1907 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1908 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1909 | |
| 1910 | Tmp = LHS_r; |
| 1911 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1912 | Result.getComplexFloatImag() = Tmp; |
| 1913 | Tmp = LHS_i; |
| 1914 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1915 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1916 | } else { |
| 1917 | APValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1919 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1920 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1922 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1923 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1924 | } |
| 1925 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | return Result; |
| 1929 | } |
| 1930 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1931 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1932 | // Top level Expr::Evaluate method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1933 | //===----------------------------------------------------------------------===// |
| 1934 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1935 | /// 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] | 1936 | /// any crazy technique (that has nothing to do with language standards) that |
| 1937 | /// we want to. If this function returns true, it returns the folded constant |
| 1938 | /// in Result. |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1939 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1940 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1941 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1942 | if (getType()->isVectorType()) { |
| 1943 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1944 | return false; |
| 1945 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1946 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1947 | return false; |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 1948 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1949 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1950 | return false; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1951 | } else if (getType()->isRealFloatingType()) { |
| 1952 | llvm::APFloat f(0.0); |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1953 | if (!EvaluateFloat(this, f, Info)) |
| 1954 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1956 | Result.Val = APValue(f); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1957 | } else if (getType()->isAnyComplexType()) { |
| 1958 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1959 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1960 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1961 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1963 | return true; |
| 1964 | } |
| 1965 | |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 1966 | bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const { |
| 1967 | EvalInfo Info(Ctx, Result, true); |
| 1968 | |
| 1969 | if (getType()->isVectorType()) { |
| 1970 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1971 | return false; |
| 1972 | } else if (getType()->isIntegerType()) { |
| 1973 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
| 1974 | return false; |
| 1975 | } else if (getType()->hasPointerRepresentation()) { |
| 1976 | if (!EvaluatePointer(this, Result.Val, Info)) |
| 1977 | return false; |
| 1978 | } else if (getType()->isRealFloatingType()) { |
| 1979 | llvm::APFloat f(0.0); |
| 1980 | if (!EvaluateFloat(this, f, Info)) |
| 1981 | return false; |
| 1982 | |
| 1983 | Result.Val = APValue(f); |
| 1984 | } else if (getType()->isAnyComplexType()) { |
| 1985 | if (!EvaluateComplex(this, Result.Val, Info)) |
| 1986 | return false; |
| 1987 | } else |
| 1988 | return false; |
| 1989 | |
| 1990 | return true; |
| 1991 | } |
| 1992 | |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 1993 | bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const { |
| 1994 | EvalResult Scratch; |
| 1995 | EvalInfo Info(Ctx, Scratch); |
| 1996 | |
| 1997 | return HandleConversionToBool(this, Result, Info); |
| 1998 | } |
| 1999 | |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 2000 | bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 2001 | EvalInfo Info(Ctx, Result); |
| 2002 | |
| 2003 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 2004 | } |
| 2005 | |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 2006 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 2007 | EvalInfo Info(Ctx, Result, true); |
| 2008 | |
| 2009 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 2010 | } |
| 2011 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2012 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2013 | /// folded, but discard the result. |
| 2014 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 2015 | EvalResult Result; |
| 2016 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2017 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2018 | |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 2019 | bool Expr::HasSideEffects(ASTContext &Ctx) const { |
| 2020 | Expr::EvalResult Result; |
| 2021 | EvalInfo Info(Ctx, Result); |
| 2022 | return HasSideEffect(Info).Visit(const_cast<Expr*>(this)); |
| 2023 | } |
| 2024 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2025 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2026 | EvalResult EvalResult; |
| 2027 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 2028 | Result = Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2029 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2030 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2031 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2032 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2033 | } |