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