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