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