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