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