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