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