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