Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTDiagnostic.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 23 | #include <cstring> |
| 24 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 27 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 28 | |
Chris Lattner | cdf34e7 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 46 | /// EvalResult - Contains information about the evaluation. |
| 47 | Expr::EvalResult &EvalResult; |
Anders Carlsson | 5862001 | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 48 | |
Eli Friedman | 7d45c48 | 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 | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 58 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 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); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 61 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 62 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 63 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 64 | |
| 65 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 66 | // Misc utilities |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 69 | static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { |
| 70 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 71 | // the check look like? |
| 72 | Result = Value.getLValueBase() || Value.getLValueOffset(); |
| 73 | return true; |
| 74 | } |
| 75 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 76 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 77 | if (E->getType()->isIntegralType()) { |
| 78 | APSInt IntResult; |
| 79 | if (!EvaluateInteger(E, IntResult, Info)) |
| 80 | return false; |
| 81 | Result = IntResult != 0; |
| 82 | return true; |
| 83 | } else if (E->getType()->isRealFloatingType()) { |
| 84 | APFloat FloatResult(0.0); |
| 85 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 86 | return false; |
| 87 | Result = !FloatResult.isZero(); |
| 88 | return true; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 89 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 90 | APValue PointerResult; |
| 91 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 92 | return false; |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 93 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 94 | } else if (E->getType()->isAnyComplexType()) { |
| 95 | APValue ComplexResult; |
| 96 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 97 | return false; |
| 98 | if (ComplexResult.isComplexFloat()) { |
| 99 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 100 | !ComplexResult.getComplexFloatImag().isZero(); |
| 101 | } else { |
| 102 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 103 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 104 | } |
| 105 | return true; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 112 | APFloat &Value, ASTContext &Ctx) { |
| 113 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 114 | // Determine whether we are converting to unsigned or signed. |
| 115 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 117 | // FIXME: Warning for overflow. |
| 118 | uint64_t Space[4]; |
| 119 | bool ignored; |
| 120 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 121 | llvm::APFloat::rmTowardZero, &ignored); |
| 122 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 123 | } |
| 124 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 126 | APFloat &Value, ASTContext &Ctx) { |
| 127 | bool ignored; |
| 128 | APFloat Result = Value; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 130 | APFloat::rmNearestTiesToEven, &ignored); |
| 131 | return Result; |
| 132 | } |
| 133 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 135 | APSInt &Value, ASTContext &Ctx) { |
| 136 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 137 | APSInt Result = Value; |
| 138 | // Figure out if this is a truncate, extend or noop cast. |
| 139 | // If the input is signed, do a sign extend, noop, or truncate. |
| 140 | Result.extOrTrunc(DestWidth); |
| 141 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 142 | return Result; |
| 143 | } |
| 144 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 146 | APSInt &Value, ASTContext &Ctx) { |
| 147 | |
| 148 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 149 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 150 | APFloat::rmNearestTiesToEven); |
| 151 | return Result; |
| 152 | } |
| 153 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 154 | //===----------------------------------------------------------------------===// |
| 155 | // LValue Evaluation |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | namespace { |
| 158 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 159 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 160 | EvalInfo &Info; |
| 161 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 163 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 164 | |
| 165 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 166 | return APValue(); |
| 167 | } |
| 168 | |
| 169 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 170 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 171 | APValue VisitBlockExpr(BlockExpr *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 172 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 173 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 174 | APValue VisitMemberExpr(MemberExpr *E); |
| 175 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 176 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 177 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 178 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 179 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 180 | { return Visit(E->getSubExpr()); } |
| 181 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 182 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 183 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 184 | }; |
| 185 | } // end anonymous namespace |
| 186 | |
| 187 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 188 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 189 | return Result.isLValue(); |
| 190 | } |
| 191 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 193 | if (isa<FunctionDecl>(E->getDecl())) { |
| 194 | return APValue(E, 0); |
| 195 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 196 | if (!Info.AnyLValue && !VD->hasGlobalStorage()) |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 197 | return APValue(); |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 198 | if (!VD->getType()->isReferenceType()) |
| 199 | return APValue(E, 0); |
Eli Friedman | 9ab0319 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 200 | // FIXME: Check whether VD might be overridden! |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 201 | if (VD->getInit()) |
| 202 | return Visit(VD->getInit()); |
| 203 | } |
| 204 | |
| 205 | return APValue(); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) { |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 209 | if (E->hasBlockDeclRefExprs()) |
| 210 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 212 | return APValue(E, 0); |
| 213 | } |
| 214 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 215 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 216 | if (!Info.AnyLValue && !E->isFileScope()) |
| 217 | return APValue(); |
| 218 | return APValue(E, 0); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 222 | APValue result; |
| 223 | QualType Ty; |
| 224 | if (E->isArrow()) { |
| 225 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 226 | return APValue(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 227 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 228 | } else { |
| 229 | result = Visit(E->getBase()); |
| 230 | if (result.isUninit()) |
| 231 | return APValue(); |
| 232 | Ty = E->getBase()->getType(); |
| 233 | } |
| 234 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 235 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 236 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 237 | |
| 238 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 239 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 240 | return APValue(); |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 241 | |
| 242 | if (FD->getType()->isReferenceType()) |
| 243 | return APValue(); |
| 244 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 245 | // FIXME: This is linear time. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 246 | unsigned i = 0; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 247 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 248 | FieldEnd = RD->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 249 | Field != FieldEnd; (void)++Field, ++i) { |
| 250 | if (*Field == FD) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | |
| 254 | result.setLValue(result.getLValueBase(), |
| 255 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 256 | |
| 257 | return result; |
| 258 | } |
| 259 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 261 | APValue Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 263 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 264 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 266 | APSInt Index; |
| 267 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 268 | return APValue(); |
| 269 | |
| 270 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 271 | |
| 272 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | Result.setLValue(Result.getLValueBase(), |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 274 | Result.getLValueOffset() + Offset); |
| 275 | return Result; |
| 276 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 277 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 279 | APValue Result; |
| 280 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 281 | return APValue(); |
| 282 | return Result; |
| 283 | } |
| 284 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 285 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 286 | // Pointer Evaluation |
| 287 | //===----------------------------------------------------------------------===// |
| 288 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 289 | namespace { |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 290 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 291 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 292 | EvalInfo &Info; |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 293 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 295 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 296 | |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 297 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 298 | return APValue(); |
| 299 | } |
| 300 | |
| 301 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 302 | |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 303 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 304 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 305 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 306 | { return Visit(E->getSubExpr()); } |
| 307 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 308 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 309 | { return APValue(E, 0); } |
Eli Friedman | 529a99b | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 310 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 311 | { return APValue(E, 0); } |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 312 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 313 | APValue VisitBlockExpr(BlockExpr *E) { |
| 314 | if (!E->hasBlockDeclRefExprs()) |
| 315 | return APValue(E, 0); |
| 316 | return APValue(); |
| 317 | } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 318 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
| 319 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 320 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 321 | APValue VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 322 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 323 | APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
| 324 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 325 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 326 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 327 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 328 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 329 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Daniel Dunbar | 76ba41c | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 330 | if (!E->getType()->hasPointerRepresentation()) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 331 | return false; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 332 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 333 | return Result.isLValue(); |
| 334 | } |
| 335 | |
| 336 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 337 | if (E->getOpcode() != BinaryOperator::Add && |
| 338 | E->getOpcode() != BinaryOperator::Sub) |
| 339 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 341 | const Expr *PExp = E->getLHS(); |
| 342 | const Expr *IExp = E->getRHS(); |
| 343 | if (IExp->getType()->isPointerType()) |
| 344 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 346 | APValue ResultLValue; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 347 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 348 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 350 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 351 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 352 | return APValue(); |
| 353 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 354 | QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 355 | uint64_t SizeOfPointee; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | ef56fba | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 357 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 358 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 359 | SizeOfPointee = 1; |
| 360 | else |
| 361 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 363 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 365 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 366 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 367 | else |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 368 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 369 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 370 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 371 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 372 | |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 373 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 374 | APValue result; |
| 375 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 376 | return result; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 377 | return APValue(); |
| 378 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 380 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 381 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 382 | const Expr* SubExpr = E->getSubExpr(); |
| 383 | |
| 384 | // Check for pointer->pointer cast |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 385 | if (SubExpr->getType()->isPointerType() || |
Anders Carlsson | 04c3bf4 | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 386 | SubExpr->getType()->isObjCObjectPointerType() || |
| 387 | SubExpr->getType()->isNullPtrType()) { |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 388 | APValue Result; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 389 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 390 | return Result; |
| 391 | return APValue(); |
| 392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 394 | if (SubExpr->getType()->isIntegralType()) { |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 395 | APValue Result; |
| 396 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 397 | return APValue(); |
| 398 | |
| 399 | if (Result.isInt()) { |
| 400 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
| 401 | return APValue(0, Result.getInt().getZExtValue()); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 404 | // Cast is of an lvalue, no need to change value. |
| 405 | return Result; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 406 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 407 | |
| 408 | if (SubExpr->getType()->isFunctionType() || |
Steve Naroff | a0c3270 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 409 | SubExpr->getType()->isBlockPointerType() || |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 410 | SubExpr->getType()->isArrayType()) { |
| 411 | APValue Result; |
| 412 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 413 | return Result; |
| 414 | return APValue(); |
| 415 | } |
| 416 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 417 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 419 | |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 420 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | if (E->isBuiltinCall(Info.Ctx) == |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 422 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 423 | return APValue(E, 0); |
| 424 | return APValue(); |
| 425 | } |
| 426 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 427 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 428 | bool BoolResult; |
| 429 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 430 | return APValue(); |
| 431 | |
| 432 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 433 | |
| 434 | APValue Result; |
| 435 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 436 | return Result; |
| 437 | return APValue(); |
| 438 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 439 | |
| 440 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 441 | // Vector Evaluation |
| 442 | //===----------------------------------------------------------------------===// |
| 443 | |
| 444 | namespace { |
| 445 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 446 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 447 | EvalInfo &Info; |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 448 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 449 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 451 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 453 | APValue VisitStmt(Stmt *S) { |
| 454 | return APValue(); |
| 455 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 457 | APValue VisitParenExpr(ParenExpr *E) |
| 458 | { return Visit(E->getSubExpr()); } |
| 459 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 460 | { return Visit(E->getSubExpr()); } |
| 461 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 462 | { return Visit(E->getSubExpr()); } |
| 463 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 464 | { return Visit(E->getSubExpr()); } |
| 465 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 466 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 467 | APValue VisitCastExpr(const CastExpr* E); |
| 468 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 469 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 470 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 471 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 472 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 473 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 474 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 475 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 476 | // shufflevector, ExtVectorElementExpr |
| 477 | // (Note that these require implementing conversions |
| 478 | // between vector types.) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 479 | }; |
| 480 | } // end anonymous namespace |
| 481 | |
| 482 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 483 | if (!E->getType()->isVectorType()) |
| 484 | return false; |
| 485 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 486 | return !Result.isUninit(); |
| 487 | } |
| 488 | |
| 489 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 490 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 491 | QualType EltTy = VTy->getElementType(); |
| 492 | unsigned NElts = VTy->getNumElements(); |
| 493 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 495 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 496 | QualType SETy = SE->getType(); |
| 497 | APValue Result = APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 498 | |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 499 | // Check for vector->vector bitcast and scalar->vector splat. |
| 500 | if (SETy->isVectorType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 501 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 502 | } else if (SETy->isIntegerType()) { |
| 503 | APSInt IntResult; |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 504 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 505 | return APValue(); |
| 506 | Result = APValue(IntResult); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 507 | } else if (SETy->isRealFloatingType()) { |
| 508 | APFloat F(0.0); |
Daniel Dunbar | df4a58e | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 509 | if (!EvaluateFloat(SE, F, Info)) |
| 510 | return APValue(); |
| 511 | Result = APValue(F); |
| 512 | } else |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 513 | return APValue(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 514 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 515 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 516 | // and splat it to all elements. |
| 517 | if (E->getType()->isExtVectorType()) { |
| 518 | if (EltTy->isIntegerType() && Result.isInt()) |
| 519 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 520 | Info.Ctx)); |
| 521 | else if (EltTy->isIntegerType()) |
| 522 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 523 | Info.Ctx)); |
| 524 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 525 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 526 | Info.Ctx)); |
| 527 | else if (EltTy->isRealFloatingType()) |
| 528 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 529 | Info.Ctx)); |
| 530 | else |
| 531 | return APValue(); |
| 532 | |
| 533 | // Splat and create vector APValue. |
| 534 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 535 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 536 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 537 | |
| 538 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 539 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 540 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 541 | // element. |
| 542 | APSInt Init; |
| 543 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 545 | llvm::SmallVector<APValue, 4> Elts; |
| 546 | for (unsigned i = 0; i != NElts; ++i) { |
| 547 | APSInt Tmp = Init; |
| 548 | Tmp.extOrTrunc(EltWidth); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 550 | if (EltTy->isIntegerType()) |
| 551 | Elts.push_back(APValue(Tmp)); |
| 552 | else if (EltTy->isRealFloatingType()) |
| 553 | Elts.push_back(APValue(APFloat(Tmp))); |
| 554 | else |
| 555 | return APValue(); |
| 556 | |
| 557 | Init >>= EltWidth; |
| 558 | } |
| 559 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 563 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 564 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 565 | } |
| 566 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | APValue |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 568 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 569 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 570 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 571 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 573 | QualType EltTy = VT->getElementType(); |
| 574 | llvm::SmallVector<APValue, 4> Elements; |
| 575 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 576 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 577 | if (EltTy->isIntegerType()) { |
| 578 | llvm::APSInt sInt(32); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 579 | if (i < NumInits) { |
| 580 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 581 | return APValue(); |
| 582 | } else { |
| 583 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 584 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 585 | Elements.push_back(APValue(sInt)); |
| 586 | } else { |
| 587 | llvm::APFloat f(0.0); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 588 | if (i < NumInits) { |
| 589 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 590 | return APValue(); |
| 591 | } else { |
| 592 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 593 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 594 | Elements.push_back(APValue(f)); |
| 595 | } |
| 596 | } |
| 597 | return APValue(&Elements[0], Elements.size()); |
| 598 | } |
| 599 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | APValue |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 601 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 602 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 603 | QualType EltTy = VT->getElementType(); |
| 604 | APValue ZeroElement; |
| 605 | if (EltTy->isIntegerType()) |
| 606 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 607 | else |
| 608 | ZeroElement = |
| 609 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 610 | |
| 611 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 612 | return APValue(&Elements[0], Elements.size()); |
| 613 | } |
| 614 | |
| 615 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 616 | bool BoolResult; |
| 617 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 618 | return APValue(); |
| 619 | |
| 620 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 621 | |
| 622 | APValue Result; |
| 623 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 624 | return Result; |
| 625 | return APValue(); |
| 626 | } |
| 627 | |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 628 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 629 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 630 | Info.EvalResult.HasSideEffects = true; |
| 631 | return GetZeroVector(E->getType()); |
| 632 | } |
| 633 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 634 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 635 | // Integer Evaluation |
| 636 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 637 | |
| 638 | namespace { |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 639 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 640 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 641 | EvalInfo &Info; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 642 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 643 | public: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 644 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 645 | : Info(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 646 | |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 647 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 648 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 649 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 650 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 651 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 652 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 653 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 654 | return true; |
| 655 | } |
| 656 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 657 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 658 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 659 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 660 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 661 | Result = APValue(APSInt(I)); |
| 662 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | |
| 666 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 667 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 668 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 669 | return true; |
| 670 | } |
| 671 | |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 672 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 673 | // Take the first error. |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 674 | if (Info.EvalResult.Diag == 0) { |
| 675 | Info.EvalResult.DiagLoc = L; |
| 676 | Info.EvalResult.Diag = D; |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 677 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 678 | } |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 679 | return false; |
Chris Lattner | ae8cc15 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 680 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 682 | //===--------------------------------------------------------------------===// |
| 683 | // Visitor Methods |
| 684 | //===--------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 686 | bool VisitStmt(Stmt *) { |
| 687 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 688 | return false; |
| 689 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 691 | bool VisitExpr(Expr *E) { |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 692 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 695 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 697 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 698 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 699 | } |
| 700 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 701 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 702 | } |
| 703 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | d7be95d | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 704 | // Per gcc docs "this built-in function ignores top level |
| 705 | // qualifiers". We need to use the canonical version to properly |
| 706 | // be able to strip CRV qualifiers from the type. |
| 707 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 708 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 710 | T1.getUnqualifiedType()), |
| 711 | E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 712 | } |
| 713 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 714 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 715 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 716 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 717 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 718 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 719 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 720 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 721 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 722 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 723 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 724 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 726 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 727 | return Success(0, E); |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 728 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 730 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 731 | return Success(0, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 734 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 735 | return Success(0, E); |
| 736 | } |
| 737 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 738 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 739 | return Success(E->EvaluateTrait(Info.Ctx), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 742 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 743 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 744 | } |
| 745 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 746 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 747 | bool VisitUnaryImag(const UnaryOperator *E); |
| 748 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 749 | private: |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 750 | unsigned GetAlignOfExpr(const Expr *E); |
| 751 | unsigned GetAlignOfType(QualType T); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 752 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 753 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 754 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 756 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 757 | if (!E->getType()->isIntegralType()) |
| 758 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 760 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 761 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 762 | |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 763 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 764 | APValue Val; |
| 765 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 766 | return false; |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 767 | Result = Val.getInt(); |
| 768 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 769 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 771 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 772 | // Enums are integer constant exprs. |
| 773 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
Eli Friedman | 14fb858 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 774 | // FIXME: This is an ugly hack around the fact that enums don't set their |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 775 | // signedness consistently; see PR3173. |
| 776 | APSInt SI = D->getInitVal(); |
| 777 | SI.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
| 778 | // FIXME: This is an ugly hack around the fact that enums don't |
| 779 | // set their width (!?!) consistently; see PR3173. |
| 780 | SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 781 | return Success(SI, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 782 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 783 | |
| 784 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | 29f80c3 | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 785 | // In C, they can also be folded, although they are not ICEs. |
| 786 | if (E->getType().getCVRQualifiers() == QualType::Const) { |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 787 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 788 | if (APValue *V = D->getEvaluatedValue()) |
| 789 | return Success(V->getInt(), E); |
| 790 | if (const Expr *Init = D->getInit()) { |
| 791 | if (Visit(const_cast<Expr*>(Init))) { |
| 792 | // Cache the evaluated value in the variable declaration. |
| 793 | D->setEvaluatedValue(Info.Ctx, Result); |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | return false; |
| 798 | } |
Sebastian Redl | c9ab3d4 | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 802 | // Otherwise, random variable references are not constants. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 803 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 806 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 807 | /// as GCC. |
| 808 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 809 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 810 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 811 | enum gcc_type_class { |
| 812 | no_type_class = -1, |
| 813 | void_type_class, integer_type_class, char_type_class, |
| 814 | enumeral_type_class, boolean_type_class, |
| 815 | pointer_type_class, reference_type_class, offset_type_class, |
| 816 | real_type_class, complex_type_class, |
| 817 | function_type_class, method_type_class, |
| 818 | record_type_class, union_type_class, |
| 819 | array_type_class, string_type_class, |
| 820 | lang_type_class |
| 821 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
| 823 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 824 | // ideal, however it is what gcc does. |
| 825 | if (E->getNumArgs() == 0) |
| 826 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 828 | QualType ArgTy = E->getArg(0)->getType(); |
| 829 | if (ArgTy->isVoidType()) |
| 830 | return void_type_class; |
| 831 | else if (ArgTy->isEnumeralType()) |
| 832 | return enumeral_type_class; |
| 833 | else if (ArgTy->isBooleanType()) |
| 834 | return boolean_type_class; |
| 835 | else if (ArgTy->isCharType()) |
| 836 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 837 | else if (ArgTy->isIntegerType()) |
| 838 | return integer_type_class; |
| 839 | else if (ArgTy->isPointerType()) |
| 840 | return pointer_type_class; |
| 841 | else if (ArgTy->isReferenceType()) |
| 842 | return reference_type_class; |
| 843 | else if (ArgTy->isRealType()) |
| 844 | return real_type_class; |
| 845 | else if (ArgTy->isComplexType()) |
| 846 | return complex_type_class; |
| 847 | else if (ArgTy->isFunctionType()) |
| 848 | return function_type_class; |
| 849 | else if (ArgTy->isStructureType()) |
| 850 | return record_type_class; |
| 851 | else if (ArgTy->isUnionType()) |
| 852 | return union_type_class; |
| 853 | else if (ArgTy->isArrayType()) |
| 854 | return array_type_class; |
| 855 | else if (ArgTy->isUnionType()) |
| 856 | return union_type_class; |
| 857 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 858 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 859 | return -1; |
| 860 | } |
| 861 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 862 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 863 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 864 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 865 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 866 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 867 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 869 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 870 | // __builtin_constant_p always has one operand: it returns true if that |
| 871 | // operand can be folded, false otherwise. |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 872 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 873 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 874 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 875 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 876 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 877 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 878 | if (!Visit(E->getRHS())) |
| 879 | return false; |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 880 | |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 881 | // If we can't evaluate the LHS, it might have side effects; |
| 882 | // conservatively mark it. |
| 883 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 884 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 885 | |
Anders Carlsson | 564730a | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 886 | return true; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | if (E->isLogicalOp()) { |
| 890 | // These need to be handled specially because the operands aren't |
| 891 | // necessarily integral |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 892 | bool lhsResult, rhsResult; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 894 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 895 | // We were able to evaluate the LHS, see if we can get away with not |
| 896 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 9cb9ff4 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 897 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 898 | return Success(lhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 899 | |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 900 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 901 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 902 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 903 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 904 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 905 | } |
| 906 | } else { |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 907 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 908 | // We can't evaluate the LHS; however, sometimes the result |
| 909 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 911 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 912 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | f50de0c | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 913 | // must have had side effects. |
| 914 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 915 | |
| 916 | return Success(rhsResult, E); |
Anders Carlsson | 4c76e93 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 919 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 920 | |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
| 923 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 924 | QualType LHSTy = E->getLHS()->getType(); |
| 925 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 926 | |
| 927 | if (LHSTy->isAnyComplexType()) { |
| 928 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 929 | APValue LHS, RHS; |
| 930 | |
| 931 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 932 | return false; |
| 933 | |
| 934 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 935 | return false; |
| 936 | |
| 937 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 939 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 941 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 942 | |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 943 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 944 | return Success((CR_r == APFloat::cmpEqual && |
| 945 | CR_i == APFloat::cmpEqual), E); |
| 946 | else { |
| 947 | assert(E->getOpcode() == BinaryOperator::NE && |
| 948 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 950 | CR_r == APFloat::cmpLessThan) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | (CR_i == APFloat::cmpGreaterThan || |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 952 | CR_i == APFloat::cmpLessThan)), E); |
| 953 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 954 | } else { |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 955 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 956 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 957 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 958 | else { |
| 959 | assert(E->getOpcode() == BinaryOperator::NE && |
| 960 | "Invalid compex comparison."); |
| 961 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 962 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 963 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 967 | if (LHSTy->isRealFloatingType() && |
| 968 | RHSTy->isRealFloatingType()) { |
| 969 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 971 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 972 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 974 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 975 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 977 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 978 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 979 | switch (E->getOpcode()) { |
| 980 | default: |
| 981 | assert(0 && "Invalid binary operator!"); |
| 982 | case BinaryOperator::LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 983 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 984 | case BinaryOperator::GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 985 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 986 | case BinaryOperator::LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 987 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 988 | case BinaryOperator::GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 990 | E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 991 | case BinaryOperator::EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 992 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 993 | case BinaryOperator::NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | return Success(CR == APFloat::cmpGreaterThan |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 995 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 996 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 997 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 999 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 1000 | if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1001 | APValue LHSValue; |
| 1002 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1003 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1004 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1005 | APValue RHSValue; |
| 1006 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1007 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1008 | |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1009 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1010 | // to null. |
| 1011 | if (LHSValue.getLValueBase()) { |
| 1012 | if (!E->isEqualityOp()) |
| 1013 | return false; |
| 1014 | if (RHSValue.getLValueBase() || RHSValue.getLValueOffset()) |
| 1015 | return false; |
| 1016 | bool bres; |
| 1017 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1018 | return false; |
| 1019 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1020 | } else if (RHSValue.getLValueBase()) { |
| 1021 | if (!E->isEqualityOp()) |
| 1022 | return false; |
| 1023 | if (LHSValue.getLValueBase() || LHSValue.getLValueOffset()) |
| 1024 | return false; |
| 1025 | bool bres; |
| 1026 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1027 | return false; |
| 1028 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1029 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1030 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1031 | if (E->getOpcode() == BinaryOperator::Sub) { |
| 1032 | const QualType Type = E->getLHS()->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1033 | const QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1034 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1035 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
Eli Friedman | fa90b15 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1036 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
| 1037 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1038 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1039 | return Success(D, E); |
| 1040 | } |
| 1041 | bool Result; |
| 1042 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1043 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 8b171f6 | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1044 | } else { |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1045 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1046 | } |
| 1047 | return Success(Result, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1050 | if (!LHSTy->isIntegralType() || |
| 1051 | !RHSTy->isIntegralType()) { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1052 | // We can't continue from here for non-integral types, and they |
| 1053 | // could potentially confuse the following operations. |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
| 1056 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1057 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1058 | if (!Visit(E->getLHS())) |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1059 | return false; // error in subexpression. |
Eli Friedman | bd84059 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1060 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1061 | APValue RHSVal; |
| 1062 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1063 | return false; |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Handle cases like (unsigned long)&a + 4. |
| 1066 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
| 1067 | uint64_t offset = Result.getLValueOffset(); |
| 1068 | if (E->getOpcode() == BinaryOperator::Add) |
| 1069 | offset += RHSVal.getInt().getZExtValue(); |
| 1070 | else |
| 1071 | offset -= RHSVal.getInt().getZExtValue(); |
| 1072 | Result = APValue(Result.getLValueBase(), offset); |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
| 1076 | // Handle cases like 4 + (unsigned long)&a |
| 1077 | if (E->getOpcode() == BinaryOperator::Add && |
| 1078 | RHSVal.isLValue() && Result.isInt()) { |
| 1079 | uint64_t offset = RHSVal.getLValueOffset(); |
| 1080 | offset += Result.getInt().getZExtValue(); |
| 1081 | Result = APValue(RHSVal.getLValueBase(), offset); |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | // All the following cases expect both operands to be an integer |
| 1086 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1087 | return false; |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1088 | |
Eli Friedman | 94c25c6 | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1089 | APSInt& RHS = RHSVal.getInt(); |
| 1090 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1091 | switch (E->getOpcode()) { |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1092 | default: |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1093 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1094 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 1095 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 1096 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 1097 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 1098 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 1099 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1100 | case BinaryOperator::Div: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1101 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1102 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1103 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1104 | case BinaryOperator::Rem: |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1105 | if (RHS == 0) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1106 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1107 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1108 | case BinaryOperator::Shl: { |
Chris Lattner | 9941570 | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1109 | // FIXME: Warn about out of range shift amounts! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1111 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1112 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | case BinaryOperator::Shr: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | unsigned SA = |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1116 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1117 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1120 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 1121 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 1122 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 1123 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 1124 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 1125 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | 8553a98 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1126 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1129 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1130 | bool Cond; |
| 1131 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1132 | return false; |
| 1133 | |
Nuno Lopes | 527b5a6 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1134 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | 4204261 | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1137 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1138 | // Get information about the alignment. |
| 1139 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1140 | |
Eli Friedman | f7f9f68 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1141 | // __alignof is defined to return the preferred alignment. |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1142 | return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize; |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1145 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 1146 | E = E->IgnoreParens(); |
| 1147 | |
| 1148 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1150 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | 43a5d9e | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1151 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1153 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | 43a5d9e | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1154 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1156 | return GetAlignOfType(E->getType()); |
| 1157 | } |
| 1158 | |
| 1159 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1160 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1161 | /// expression's type. |
| 1162 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 1163 | QualType DstTy = E->getType(); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1165 | // Handle alignof separately. |
| 1166 | if (!E->isSizeOf()) { |
| 1167 | if (E->isArgumentType()) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1168 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1169 | else |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1170 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1171 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1172 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1173 | QualType SrcTy = E->getTypeOfArgument(); |
| 1174 | |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1175 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1176 | // extension. |
| 1177 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1178 | return Success(1, E); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1179 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1180 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1181 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1182 | return false; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1184 | // Get information about the size. |
Daniel Dunbar | bc5a7a8 | 2009-05-03 10:35:52 +0000 | [diff] [blame] | 1185 | unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy); |
Daniel Dunbar | 93c1914 | 2009-04-21 15:48:54 +0000 | [diff] [blame] | 1186 | return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1189 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1190 | // Special case unary operators that do not need their subexpression |
| 1191 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1192 | if (E->isOffsetOfOp()) { |
| 1193 | // The AST for offsetof is defined in such a way that we can just |
| 1194 | // directly Evaluate it as an l-value. |
| 1195 | APValue LV; |
| 1196 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
| 1197 | return false; |
| 1198 | if (LV.getLValueBase()) |
| 1199 | return false; |
| 1200 | return Success(LV.getLValueOffset(), E); |
| 1201 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1204 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1205 | bool bres; |
| 1206 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1207 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1208 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Daniel Dunbar | 79e042a | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1211 | // Only handle integral operations... |
| 1212 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1213 | return false; |
| 1214 | |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1215 | // Get the operand value into 'Result'. |
| 1216 | if (!Visit(E->getSubExpr())) |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1217 | return false; |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1219 | switch (E->getOpcode()) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1220 | default: |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1221 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1222 | // See C99 6.6p3. |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1223 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1224 | case UnaryOperator::Extension: |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1225 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1226 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1227 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1228 | case UnaryOperator::Plus: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | // The result is always just the subexpr. |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1230 | return true; |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1231 | case UnaryOperator::Minus: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1232 | if (!Result.isInt()) return false; |
| 1233 | return Success(-Result.getInt(), E); |
Chris Lattner | f09ad16 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1234 | case UnaryOperator::Not: |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1235 | if (!Result.isInt()) return false; |
| 1236 | return Success(~Result.getInt(), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1237 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1238 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1240 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1241 | /// result type is integer. |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1242 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1243 | Expr *SubExpr = E->getSubExpr(); |
| 1244 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1245 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1246 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1247 | if (DestType->isBooleanType()) { |
| 1248 | bool BoolResult; |
| 1249 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1250 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1251 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1254 | // Handle simple integer->integer casts. |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1255 | if (SrcType->isIntegralType()) { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1256 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1257 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1258 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1259 | if (!Result.isInt()) { |
| 1260 | // Only allow casts of lvalues if they are lossless. |
| 1261 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1262 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1263 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1264 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1265 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1266 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1268 | // FIXME: Clean this up! |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1269 | if (SrcType->isPointerType()) { |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1270 | APValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1271 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1272 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1273 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1274 | if (LV.getLValueBase()) { |
| 1275 | // Only allow based lvalue casts if they are lossless. |
| 1276 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1277 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1278 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1279 | Result = LV; |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
| 1283 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType); |
| 1284 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1285 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1286 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1287 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1288 | // This handles double-conversion cases, where there's both |
| 1289 | // an l-value promotion and an implicit conversion to int. |
| 1290 | APValue LV; |
| 1291 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1292 | return false; |
| 1293 | |
| 1294 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1295 | return false; |
| 1296 | |
| 1297 | Result = LV; |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1301 | if (SrcType->isAnyComplexType()) { |
| 1302 | APValue C; |
| 1303 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1304 | return false; |
| 1305 | if (C.isComplexFloat()) |
| 1306 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1307 | C.getComplexFloatReal(), Info.Ctx), |
| 1308 | E); |
| 1309 | else |
| 1310 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1311 | C.getComplexIntReal(), Info.Ctx), E); |
| 1312 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1313 | // FIXME: Handle vectors |
| 1314 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1315 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1316 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1317 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1318 | APFloat F(0.0); |
| 1319 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | b33d6c8 | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1320 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1322 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1323 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1324 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1325 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1326 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1327 | APValue LV; |
| 1328 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1329 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1330 | return Success(LV.getComplexIntReal(), E); |
| 1331 | } |
| 1332 | |
| 1333 | return Visit(E->getSubExpr()); |
| 1334 | } |
| 1335 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1336 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1337 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1338 | APValue LV; |
| 1339 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1340 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1341 | return Success(LV.getComplexIntImag(), E); |
| 1342 | } |
| 1343 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1344 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1345 | Info.EvalResult.HasSideEffects = true; |
| 1346 | return Success(0, E); |
| 1347 | } |
| 1348 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1349 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1350 | // Float Evaluation |
| 1351 | //===----------------------------------------------------------------------===// |
| 1352 | |
| 1353 | namespace { |
| 1354 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1355 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1356 | EvalInfo &Info; |
| 1357 | APFloat &Result; |
| 1358 | public: |
| 1359 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1360 | : Info(info), Result(result) {} |
| 1361 | |
| 1362 | bool VisitStmt(Stmt *S) { |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1367 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1368 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1369 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1370 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1371 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1372 | bool VisitCastExpr(CastExpr *E); |
| 1373 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1374 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1375 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1376 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1377 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1378 | { return Visit(E->getSubExpr()); } |
| 1379 | |
| 1380 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
| 1381 | // member of vector, ImplicitValueInitExpr, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1382 | // conditional ?:, comma |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | } // end anonymous namespace |
| 1385 | |
| 1386 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1387 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1388 | } |
| 1389 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1390 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1391 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1392 | default: return false; |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1393 | case Builtin::BI__builtin_huge_val: |
| 1394 | case Builtin::BI__builtin_huge_valf: |
| 1395 | case Builtin::BI__builtin_huge_vall: |
| 1396 | case Builtin::BI__builtin_inf: |
| 1397 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1398 | case Builtin::BI__builtin_infl: { |
| 1399 | const llvm::fltSemantics &Sem = |
| 1400 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1401 | Result = llvm::APFloat::getInf(Sem); |
| 1402 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1405 | case Builtin::BI__builtin_nan: |
| 1406 | case Builtin::BI__builtin_nanf: |
| 1407 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1408 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1409 | // can't constant fold it. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | if (const StringLiteral *S = |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1411 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1412 | if (!S->isWide()) { |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1413 | const llvm::fltSemantics &Sem = |
| 1414 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1415 | llvm::SmallString<16> s; |
| 1416 | s.append(S->getStrData(), S->getStrData() + S->getByteLength()); |
| 1417 | s += '\0'; |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1418 | long l; |
| 1419 | char *endp; |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1420 | l = strtol(&s[0], &endp, 0); |
| 1421 | if (endp != s.end()-1) |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1422 | return false; |
| 1423 | unsigned type = (unsigned int)l;; |
| 1424 | Result = llvm::APFloat::getNaN(Sem, false, type); |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1425 | return true; |
| 1426 | } |
| 1427 | } |
| 1428 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1429 | |
| 1430 | case Builtin::BI__builtin_fabs: |
| 1431 | case Builtin::BI__builtin_fabsf: |
| 1432 | case Builtin::BI__builtin_fabsl: |
| 1433 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1434 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1436 | if (Result.isNegative()) |
| 1437 | Result.changeSign(); |
| 1438 | return true; |
| 1439 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | case Builtin::BI__builtin_copysign: |
| 1441 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1442 | case Builtin::BI__builtin_copysignl: { |
| 1443 | APFloat RHS(0.); |
| 1444 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1445 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1446 | return false; |
| 1447 | Result.copySign(RHS); |
| 1448 | return true; |
| 1449 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1453 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | 0e33c68 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1454 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1455 | return false; |
| 1456 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1457 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1458 | return false; |
| 1459 | |
| 1460 | switch (E->getOpcode()) { |
| 1461 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | case UnaryOperator::Plus: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1463 | return true; |
| 1464 | case UnaryOperator::Minus: |
| 1465 | Result.changeSign(); |
| 1466 | return true; |
| 1467 | } |
| 1468 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1469 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1470 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1471 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1472 | // and errors are supposed to work. |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1473 | APFloat RHS(0.0); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1474 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1475 | return false; |
| 1476 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1477 | return false; |
| 1478 | |
| 1479 | switch (E->getOpcode()) { |
| 1480 | default: return false; |
| 1481 | case BinaryOperator::Mul: |
| 1482 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1483 | return true; |
| 1484 | case BinaryOperator::Add: |
| 1485 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1486 | return true; |
| 1487 | case BinaryOperator::Sub: |
| 1488 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1489 | return true; |
| 1490 | case BinaryOperator::Div: |
| 1491 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1492 | return true; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1497 | Result = E->getValue(); |
| 1498 | return true; |
| 1499 | } |
| 1500 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1501 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1502 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1504 | if (SubExpr->getType()->isIntegralType()) { |
| 1505 | APSInt IntResult; |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1506 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1507 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1509 | IntResult, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1510 | return true; |
| 1511 | } |
| 1512 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1513 | if (!Visit(SubExpr)) |
| 1514 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1515 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1516 | Result, Info.Ctx); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1517 | return true; |
| 1518 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1519 | // FIXME: Handle complex types |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1520 | |
| 1521 | return false; |
| 1522 | } |
| 1523 | |
| 1524 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1525 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1526 | return true; |
| 1527 | } |
| 1528 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1529 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1530 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1531 | //===----------------------------------------------------------------------===// |
| 1532 | |
| 1533 | namespace { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1534 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1535 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1536 | EvalInfo &Info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1538 | public: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1539 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1541 | //===--------------------------------------------------------------------===// |
| 1542 | // Visitor Methods |
| 1543 | //===--------------------------------------------------------------------===// |
| 1544 | |
| 1545 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1546 | return APValue(); |
| 1547 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1549 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1550 | |
| 1551 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1552 | Expr* SubExpr = E->getSubExpr(); |
| 1553 | |
| 1554 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1555 | APFloat Result(0.0); |
| 1556 | |
| 1557 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1558 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
| 1560 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1561 | Result); |
| 1562 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | assert(SubExpr->getType()->isIntegerType() && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1564 | "Unexpected imaginary literal."); |
| 1565 | |
| 1566 | llvm::APSInt Result; |
| 1567 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1568 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1570 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1571 | Zero = 0; |
| 1572 | return APValue(Zero, Result); |
| 1573 | } |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1576 | APValue VisitCastExpr(CastExpr *E) { |
| 1577 | Expr* SubExpr = E->getSubExpr(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 1578 | QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1579 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1580 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1581 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1582 | APFloat Result(0.0); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1583 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1584 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1585 | return APValue(); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (EltType->isRealFloatingType()) { |
| 1588 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | return APValue(Result, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1590 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
| 1591 | } else { |
| 1592 | llvm::APSInt IResult; |
| 1593 | IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1594 | llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); |
| 1595 | Zero = 0; |
| 1596 | return APValue(IResult, Zero); |
| 1597 | } |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1598 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1599 | APSInt Result; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1600 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1601 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1602 | return APValue(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1603 | |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1604 | if (EltType->isRealFloatingType()) { |
| 1605 | APFloat FResult = |
| 1606 | HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | return APValue(FResult, |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1608 | APFloat(FResult.getSemantics(), APFloat::fcZero, false)); |
| 1609 | } else { |
| 1610 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1611 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1612 | Zero = 0; |
| 1613 | return APValue(Result, Zero); |
| 1614 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 1615 | } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1616 | APValue Src; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1617 | |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1618 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1619 | return APValue(); |
| 1620 | |
| 1621 | QualType SrcType = CT->getElementType(); |
| 1622 | |
| 1623 | if (Src.isComplexFloat()) { |
| 1624 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1626 | Src.getComplexFloatReal(), |
| 1627 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1629 | Src.getComplexFloatImag(), |
| 1630 | Info.Ctx)); |
| 1631 | } else { |
| 1632 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1633 | Src.getComplexFloatReal(), |
| 1634 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | HandleFloatToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1636 | Src.getComplexFloatImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } else { |
| 1640 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1641 | if (EltType->isRealFloatingType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1643 | Src.getComplexIntReal(), |
| 1644 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1646 | Src.getComplexIntImag(), |
| 1647 | Info.Ctx)); |
| 1648 | } else { |
| 1649 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1650 | Src.getComplexIntReal(), |
| 1651 | Info.Ctx), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | HandleIntToIntCast(EltType, SrcType, |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1653 | Src.getComplexIntImag(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | Info.Ctx)); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | // FIXME: Handle more casts. |
| 1660 | return APValue(); |
| 1661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1663 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1664 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1665 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1666 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1667 | { return Visit(E->getSubExpr()); } |
| 1668 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1669 | // conditional ?:, comma |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1670 | }; |
| 1671 | } // end anonymous namespace |
| 1672 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1674 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1675 | assert((!Result.isComplexFloat() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | (&Result.getComplexFloatReal().getSemantics() == |
| 1677 | &Result.getComplexFloatImag().getSemantics())) && |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1678 | "Invalid complex evaluation."); |
| 1679 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1683 | APValue Result, RHS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1685 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1686 | return APValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1688 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1689 | return APValue(); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1690 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1691 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1692 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1693 | switch (E->getOpcode()) { |
| 1694 | default: return APValue(); |
| 1695 | case BinaryOperator::Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1696 | if (Result.isComplexFloat()) { |
| 1697 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1698 | APFloat::rmNearestTiesToEven); |
| 1699 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1700 | APFloat::rmNearestTiesToEven); |
| 1701 | } else { |
| 1702 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1703 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1704 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1705 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1706 | case BinaryOperator::Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1707 | if (Result.isComplexFloat()) { |
| 1708 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1709 | APFloat::rmNearestTiesToEven); |
| 1710 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1711 | APFloat::rmNearestTiesToEven); |
| 1712 | } else { |
| 1713 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1714 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1715 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1716 | break; |
| 1717 | case BinaryOperator::Mul: |
| 1718 | if (Result.isComplexFloat()) { |
| 1719 | APValue LHS = Result; |
| 1720 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1721 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1722 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1723 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1725 | APFloat Tmp = LHS_r; |
| 1726 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1727 | Result.getComplexFloatReal() = Tmp; |
| 1728 | Tmp = LHS_i; |
| 1729 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1730 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1731 | |
| 1732 | Tmp = LHS_r; |
| 1733 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1734 | Result.getComplexFloatImag() = Tmp; |
| 1735 | Tmp = LHS_i; |
| 1736 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1737 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1738 | } else { |
| 1739 | APValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1741 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1742 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1744 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1745 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1746 | } |
| 1747 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | return Result; |
| 1751 | } |
| 1752 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1753 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1754 | // Top level Expr::Evaluate method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1755 | //===----------------------------------------------------------------------===// |
| 1756 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1757 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1758 | /// any crazy technique (that has nothing to do with language standards) that |
| 1759 | /// we want to. If this function returns true, it returns the folded constant |
| 1760 | /// in Result. |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1761 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1762 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | bd1df8e | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1763 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1764 | if (getType()->isVectorType()) { |
| 1765 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1766 | return false; |
| 1767 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1768 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1769 | return false; |
Daniel Dunbar | 76ba41c | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 1770 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1771 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1772 | return false; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1773 | } else if (getType()->isRealFloatingType()) { |
| 1774 | llvm::APFloat f(0.0); |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1775 | if (!EvaluateFloat(this, f, Info)) |
| 1776 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1778 | Result.Val = APValue(f); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1779 | } else if (getType()->isAnyComplexType()) { |
| 1780 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1781 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1782 | } else |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1783 | return false; |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1784 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1785 | return true; |
| 1786 | } |
| 1787 | |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 1788 | bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1789 | EvalInfo Info(Ctx, Result); |
| 1790 | |
| 1791 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1792 | } |
| 1793 | |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 1794 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1795 | EvalInfo Info(Ctx, Result, true); |
| 1796 | |
| 1797 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1798 | } |
| 1799 | |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1800 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1801 | /// folded, but discard the result. |
| 1802 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1803 | EvalResult Result; |
| 1804 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1805 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1806 | |
| 1807 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1808 | EvalResult EvalResult; |
| 1809 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | 435bbe0 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1810 | Result = Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1811 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1812 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1814 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1815 | } |