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