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 | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.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 | |
| 42 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 43 | /// if it is short-circuited (according to C rules). |
| 44 | bool isEvaluated; |
| 45 | |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 46 | /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the |
| 47 | /// error diagnostic indicating why it is not foldable and DiagLoc indicates a |
| 48 | /// caret position for the error. If it is foldable, but the expression is |
| 49 | /// not an integer constant expression, ICEDiag contains the extension |
| 50 | /// diagnostic to emit which describes why it isn't an integer constant |
| 51 | /// expression. If this expression *is* an integer-constant-expr, then |
| 52 | /// ICEDiag is zero. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 53 | /// |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 54 | /// The caller can choose to emit this diagnostic or not, depending on whether |
| 55 | /// they require an i-c-e or a constant or not. DiagLoc indicates the caret |
| 56 | /// position for the report. |
| 57 | /// |
| 58 | /// If ICEDiag is zero, then this expression is an i-c-e. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 59 | unsigned ICEDiag; |
| 60 | SourceLocation DiagLoc; |
| 61 | |
| 62 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 63 | }; |
| 64 | |
| 65 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 66 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 67 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 68 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 69 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 70 | |
| 71 | //===----------------------------------------------------------------------===// |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 72 | // Misc utilities |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 76 | if (E->getType()->isIntegralType()) { |
| 77 | APSInt IntResult; |
| 78 | if (!EvaluateInteger(E, IntResult, Info)) |
| 79 | return false; |
| 80 | Result = IntResult != 0; |
| 81 | return true; |
| 82 | } else if (E->getType()->isRealFloatingType()) { |
| 83 | APFloat FloatResult(0.0); |
| 84 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 85 | return false; |
| 86 | Result = !FloatResult.isZero(); |
| 87 | return true; |
| 88 | } else if (E->getType()->isPointerType()) { |
| 89 | APValue PointerResult; |
| 90 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 91 | return false; |
| 92 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 93 | // the check look like? |
| 94 | Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset(); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // LValue Evaluation |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | namespace { |
| 105 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 106 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 107 | EvalInfo &Info; |
| 108 | public: |
| 109 | |
| 110 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 111 | |
| 112 | APValue VisitStmt(Stmt *S) { |
Daniel Dunbar | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 113 | #if 0 |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 114 | // FIXME: Remove this when we support more expressions. |
| 115 | printf("Unhandled pointer statement\n"); |
| 116 | S->dump(); |
Daniel Dunbar | ff59ed8 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 117 | #endif |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 118 | return APValue(); |
| 119 | } |
| 120 | |
| 121 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 122 | APValue VisitDeclRefExpr(DeclRefExpr *E) { return APValue(E, 0); } |
| 123 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 124 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 125 | APValue VisitMemberExpr(MemberExpr *E); |
| 126 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
| 127 | }; |
| 128 | } // end anonymous namespace |
| 129 | |
| 130 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 131 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 132 | return Result.isLValue(); |
| 133 | } |
| 134 | |
| 135 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 136 | if (E->isFileScope()) |
| 137 | return APValue(E, 0); |
| 138 | return APValue(); |
| 139 | } |
| 140 | |
| 141 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 142 | APValue result; |
| 143 | QualType Ty; |
| 144 | if (E->isArrow()) { |
| 145 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 146 | return APValue(); |
| 147 | Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType(); |
| 148 | } else { |
| 149 | result = Visit(E->getBase()); |
| 150 | if (result.isUninit()) |
| 151 | return APValue(); |
| 152 | Ty = E->getBase()->getType(); |
| 153 | } |
| 154 | |
| 155 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
| 156 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 157 | FieldDecl *FD = E->getMemberDecl(); |
| 158 | |
| 159 | // FIXME: This is linear time. |
| 160 | unsigned i = 0, e = 0; |
| 161 | for (i = 0, e = RD->getNumMembers(); i != e; i++) { |
| 162 | if (RD->getMember(i) == FD) |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | result.setLValue(result.getLValueBase(), |
| 167 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 174 | // Pointer Evaluation |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 177 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 178 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 179 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 180 | EvalInfo &Info; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 181 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 183 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 184 | |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 185 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 186 | return APValue(); |
| 187 | } |
| 188 | |
| 189 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 190 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 191 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 192 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 193 | APValue VisitUnaryOperator(const UnaryOperator *E); |
| 194 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 195 | { return APValue(E, 0); } |
| 196 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 197 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 198 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 200 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 201 | if (!E->getType()->isPointerType()) |
| 202 | return false; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 203 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 204 | return Result.isLValue(); |
| 205 | } |
| 206 | |
| 207 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 208 | if (E->getOpcode() != BinaryOperator::Add && |
| 209 | E->getOpcode() != BinaryOperator::Sub) |
| 210 | return APValue(); |
| 211 | |
| 212 | const Expr *PExp = E->getLHS(); |
| 213 | const Expr *IExp = E->getRHS(); |
| 214 | if (IExp->getType()->isPointerType()) |
| 215 | std::swap(PExp, IExp); |
| 216 | |
| 217 | APValue ResultLValue; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 218 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 219 | return APValue(); |
| 220 | |
| 221 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 222 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 223 | return APValue(); |
| 224 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 225 | QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType(); |
| 226 | uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
| 227 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 228 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 229 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 230 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 231 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 232 | else |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 233 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 234 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 235 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 236 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 237 | |
| 238 | APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 239 | if (E->getOpcode() == UnaryOperator::Extension) { |
| 240 | // FIXME: Deal with warnings? |
| 241 | return Visit(E->getSubExpr()); |
| 242 | } |
| 243 | |
| 244 | if (E->getOpcode() == UnaryOperator::AddrOf) { |
| 245 | APValue result; |
| 246 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | return APValue(); |
| 251 | } |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 252 | |
| 253 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 254 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 255 | const Expr* SubExpr = E->getSubExpr(); |
| 256 | |
| 257 | // Check for pointer->pointer cast |
| 258 | if (SubExpr->getType()->isPointerType()) { |
| 259 | APValue Result; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 260 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 261 | return Result; |
| 262 | return APValue(); |
| 263 | } |
| 264 | |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 265 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 266 | llvm::APSInt Result(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 267 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 268 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 269 | return APValue(0, Result.getZExtValue()); |
| 270 | } |
| 271 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 272 | |
| 273 | if (SubExpr->getType()->isFunctionType() || |
| 274 | SubExpr->getType()->isArrayType()) { |
| 275 | APValue Result; |
| 276 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 277 | return Result; |
| 278 | return APValue(); |
| 279 | } |
| 280 | |
| 281 | //assert(0 && "Unhandled cast"); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 282 | return APValue(); |
| 283 | } |
| 284 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 285 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 286 | bool BoolResult; |
| 287 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 288 | return APValue(); |
| 289 | |
| 290 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 291 | |
| 292 | APValue Result; |
| 293 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 294 | return Result; |
| 295 | return APValue(); |
| 296 | } |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 297 | |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | // Integer Evaluation |
| 300 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 301 | |
| 302 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 303 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 304 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 305 | EvalInfo &Info; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 306 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 307 | public: |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 308 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 309 | : Info(info), Result(result) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 311 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 312 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 313 | } |
| 314 | |
| 315 | bool Extension(SourceLocation L, diag::kind D) { |
| 316 | Info.DiagLoc = L; |
| 317 | Info.ICEDiag = D; |
| 318 | return true; // still a constant. |
| 319 | } |
| 320 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 321 | bool Error(SourceLocation L, diag::kind D, QualType ExprTy) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 322 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 323 | // error. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 324 | if (!Info.isEvaluated) { |
| 325 | // If error is ignored because the value isn't evaluated, get the real |
| 326 | // type at least to prevent errors downstream. |
| 327 | Result.zextOrTrunc(getIntTypeSizeInBits(ExprTy)); |
| 328 | Result.setIsUnsigned(ExprTy->isUnsignedIntegerType()); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 329 | return true; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 332 | // Take the first error. |
| 333 | if (Info.ICEDiag == 0) { |
| 334 | Info.DiagLoc = L; |
| 335 | Info.ICEDiag = D; |
| 336 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 337 | return false; |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 340 | //===--------------------------------------------------------------------===// |
| 341 | // Visitor Methods |
| 342 | //===--------------------------------------------------------------------===// |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 343 | |
| 344 | bool VisitStmt(Stmt *) { |
| 345 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 346 | return false; |
| 347 | } |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 349 | bool VisitExpr(Expr *E) { |
| 350 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 353 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 355 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| 356 | Result = E->getValue(); |
| 357 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 358 | return true; |
| 359 | } |
| 360 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 361 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 362 | Result = E->getValue(); |
| 363 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 364 | return true; |
| 365 | } |
| 366 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 367 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Daniel Dunbar | da8ebd2 | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 368 | // Per gcc docs "this built-in function ignores top level |
| 369 | // qualifiers". We need to use the canonical version to properly |
| 370 | // be able to strip CRV qualifiers from the type. |
| 371 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 372 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
| 373 | Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 374 | T1.getUnqualifiedType()); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 378 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 379 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 380 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 381 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 382 | bool VisitCastExpr(CastExpr* E) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 383 | return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType()); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 384 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 385 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 386 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 387 | private: |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 388 | bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 389 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 390 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 392 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 393 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 394 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 396 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 397 | // Enums are integer constant exprs. |
| 398 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 399 | Result = D->getInitVal(); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | // Otherwise, random variable references are not constants. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 404 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Chris Lattner | 1eee940 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 407 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 408 | /// as GCC. |
| 409 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 410 | // The following enum mimics the values returned by GCC. |
| 411 | enum gcc_type_class { |
| 412 | no_type_class = -1, |
| 413 | void_type_class, integer_type_class, char_type_class, |
| 414 | enumeral_type_class, boolean_type_class, |
| 415 | pointer_type_class, reference_type_class, offset_type_class, |
| 416 | real_type_class, complex_type_class, |
| 417 | function_type_class, method_type_class, |
| 418 | record_type_class, union_type_class, |
| 419 | array_type_class, string_type_class, |
| 420 | lang_type_class |
| 421 | }; |
| 422 | |
| 423 | // If no argument was supplied, default to "no_type_class". This isn't |
| 424 | // ideal, however it is what gcc does. |
| 425 | if (E->getNumArgs() == 0) |
| 426 | return no_type_class; |
| 427 | |
| 428 | QualType ArgTy = E->getArg(0)->getType(); |
| 429 | if (ArgTy->isVoidType()) |
| 430 | return void_type_class; |
| 431 | else if (ArgTy->isEnumeralType()) |
| 432 | return enumeral_type_class; |
| 433 | else if (ArgTy->isBooleanType()) |
| 434 | return boolean_type_class; |
| 435 | else if (ArgTy->isCharType()) |
| 436 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 437 | else if (ArgTy->isIntegerType()) |
| 438 | return integer_type_class; |
| 439 | else if (ArgTy->isPointerType()) |
| 440 | return pointer_type_class; |
| 441 | else if (ArgTy->isReferenceType()) |
| 442 | return reference_type_class; |
| 443 | else if (ArgTy->isRealType()) |
| 444 | return real_type_class; |
| 445 | else if (ArgTy->isComplexType()) |
| 446 | return complex_type_class; |
| 447 | else if (ArgTy->isFunctionType()) |
| 448 | return function_type_class; |
| 449 | else if (ArgTy->isStructureType()) |
| 450 | return record_type_class; |
| 451 | else if (ArgTy->isUnionType()) |
| 452 | return union_type_class; |
| 453 | else if (ArgTy->isArrayType()) |
| 454 | return array_type_class; |
| 455 | else if (ArgTy->isUnionType()) |
| 456 | return union_type_class; |
| 457 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 458 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 459 | return -1; |
| 460 | } |
| 461 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 462 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| 463 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 465 | switch (E->isBuiltinCall()) { |
| 466 | default: |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 467 | return Error(E->getLocStart(), diag::err_expr_not_constant, E->getType()); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 468 | case Builtin::BI__builtin_classify_type: |
Chris Lattner | 1eee940 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 469 | Result.setIsSigned(true); |
| 470 | Result = EvaluateBuiltinClassifyType(E); |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 471 | return true; |
| 472 | |
| 473 | case Builtin::BI__builtin_constant_p: { |
| 474 | // __builtin_constant_p always has one operand: it returns true if that |
| 475 | // operand can be folded, false otherwise. |
| 476 | APValue Res; |
| 477 | Result = E->getArg(0)->tryEvaluate(Res, Info.Ctx); |
| 478 | return true; |
| 479 | } |
| 480 | } |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 481 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 482 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 483 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 484 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 485 | llvm::APSInt RHS(32); |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 486 | if (!Visit(E->getLHS())) { |
| 487 | // If the LHS is unfoldable, we generally can't fold this. However, if this |
| 488 | // is a logical operator like &&/||, and if we know that the RHS determines |
| 489 | // the outcome of the result (e.g. X && 0), return the outcome. |
| 490 | if (!E->isLogicalOp()) |
| 491 | return false; |
| 492 | |
| 493 | // If this is a logical op, see if the RHS determines the outcome. |
| 494 | EvalInfo Info2(Info.Ctx); |
| 495 | if (!EvaluateInteger(E->getRHS(), RHS, Info2)) |
| 496 | return false; |
| 497 | |
| 498 | // X && 0 -> 0, X || 1 -> 1. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 499 | if ((E->getOpcode() == BinaryOperator::LAnd && RHS == 0) || |
| 500 | (E->getOpcode() == BinaryOperator::LOr && RHS != 0)) { |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 501 | Result = RHS != 0; |
| 502 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 503 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 507 | return false; // error in subexpression. |
Chris Lattner | 40d2ae8 | 2008-11-12 07:04:29 +0000 | [diff] [blame] | 508 | } |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 509 | |
| 510 | bool OldEval = Info.isEvaluated; |
| 511 | |
| 512 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 513 | // RHS. Make sure to pass isEvaluated down correctly. |
| 514 | if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) || |
| 515 | (E->getOpcode() == BinaryOperator::LOr && Result != 0)) |
| 516 | Info.isEvaluated = false; |
Eli Friedman | 3e64dd7 | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 517 | |
| 518 | // FIXME: Handle pointer subtraction |
| 519 | |
| 520 | // FIXME Maybe we want to succeed even where we can't evaluate the |
| 521 | // right side of LAnd/LOr? |
| 522 | // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525 |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 523 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 524 | return false; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 525 | Info.isEvaluated = OldEval; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 526 | |
| 527 | switch (E->getOpcode()) { |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 528 | default: |
| 529 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant,E->getType()); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 530 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 531 | case BinaryOperator::Add: Result += RHS; return true; |
| 532 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 533 | case BinaryOperator::And: Result &= RHS; return true; |
| 534 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 535 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 536 | case BinaryOperator::Div: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 537 | if (RHS == 0) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 538 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 539 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 540 | Result /= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 541 | break; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 542 | case BinaryOperator::Rem: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 543 | if (RHS == 0) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 544 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero, |
| 545 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 546 | Result %= RHS; |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 547 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 548 | case BinaryOperator::Shl: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 549 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 550 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 551 | break; |
| 552 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 553 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 554 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 556 | case BinaryOperator::LT: |
| 557 | Result = Result < RHS; |
| 558 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 559 | break; |
| 560 | case BinaryOperator::GT: |
| 561 | Result = Result > RHS; |
| 562 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 563 | break; |
| 564 | case BinaryOperator::LE: |
| 565 | Result = Result <= RHS; |
| 566 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 567 | break; |
| 568 | case BinaryOperator::GE: |
| 569 | Result = Result >= RHS; |
| 570 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 571 | break; |
| 572 | case BinaryOperator::EQ: |
| 573 | Result = Result == RHS; |
| 574 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 575 | break; |
| 576 | case BinaryOperator::NE: |
| 577 | Result = Result != RHS; |
| 578 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 579 | break; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 580 | case BinaryOperator::LAnd: |
| 581 | Result = Result != 0 && RHS != 0; |
| 582 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 583 | break; |
| 584 | case BinaryOperator::LOr: |
| 585 | Result = Result != 0 || RHS != 0; |
| 586 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 587 | break; |
Eli Friedman | b2935ab | 2008-11-13 02:13:11 +0000 | [diff] [blame^] | 588 | |
| 589 | |
| 590 | case BinaryOperator::Comma: |
| 591 | // Result of the comma is just the result of the RHS. |
| 592 | Result = RHS; |
| 593 | |
| 594 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 595 | // *except* when they are contained within a subexpression that is not |
| 596 | // evaluated". Note that Assignment can never happen due to constraints |
| 597 | // on the LHS subexpr, so we don't need to check it here. |
| 598 | if (!Info.isEvaluated) |
| 599 | return true; |
| 600 | |
| 601 | // If the value is evaluated, we can accept it as an extension. |
| 602 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
| 603 | } |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 604 | |
| 605 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 606 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 609 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 610 | /// expression's type. |
| 611 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 612 | QualType DstTy = E->getType(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 613 | // Return the result in the right width. |
| 614 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 615 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 616 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 617 | QualType SrcTy = E->getTypeOfArgument(); |
| 618 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 619 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 620 | if (SrcTy->isVoidType()) { |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 621 | Result = 1; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 622 | return true; |
| 623 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 624 | |
| 625 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 626 | // FIXME: But alignof(vla) is! |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 627 | if (!SrcTy->isConstantSizeType()) { |
| 628 | // FIXME: Should we attempt to evaluate this? |
| 629 | return false; |
| 630 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 631 | |
| 632 | bool isSizeOf = E->isSizeOf(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 633 | |
| 634 | // GCC extension: sizeof(function) = 1. |
| 635 | if (SrcTy->isFunctionType()) { |
| 636 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 637 | Result = isSizeOf ? 1 : 4; |
| 638 | return true; |
| 639 | } |
| 640 | |
| 641 | // Get information about the size or align. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 642 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 643 | if (isSizeOf) |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 644 | Result = Info.Ctx.getTypeSize(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 645 | else |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 646 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 647 | return true; |
| 648 | } |
| 649 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 650 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 651 | // Special case unary operators that do not need their subexpression |
| 652 | // evaluated. offsetof/sizeof/alignof are all special. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 653 | if (E->isOffsetOfOp()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 654 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 655 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 656 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 657 | return true; |
| 658 | } |
| 659 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 660 | // Get the operand value into 'Result'. |
| 661 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 662 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 664 | switch (E->getOpcode()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 665 | default: |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 666 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 667 | // See C99 6.6p3. |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 668 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant, |
| 669 | E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 670 | case UnaryOperator::LNot: { |
| 671 | bool Val = Result == 0; |
| 672 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 673 | Result = Val; |
| 674 | break; |
| 675 | } |
| 676 | case UnaryOperator::Extension: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 677 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 678 | // If so, we could clear the diagnostic ID. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 679 | case UnaryOperator::Plus: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 680 | // The result is always just the subexpr. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 681 | break; |
| 682 | case UnaryOperator::Minus: |
| 683 | Result = -Result; |
| 684 | break; |
| 685 | case UnaryOperator::Not: |
| 686 | Result = ~Result; |
| 687 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 691 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 694 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 695 | /// result type is integer. |
| 696 | bool IntExprEvaluator::HandleCast(SourceLocation CastLoc, |
| 697 | Expr *SubExpr, QualType DestType) { |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 698 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 699 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 700 | if (DestType->isBooleanType()) { |
| 701 | bool BoolResult; |
| 702 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 703 | return false; |
| 704 | Result.zextOrTrunc(DestWidth); |
| 705 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 706 | Result = BoolResult; |
| 707 | return true; |
| 708 | } |
| 709 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 710 | // Handle simple integer->integer casts. |
| 711 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 712 | if (!Visit(SubExpr)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 713 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 714 | |
| 715 | // Figure out if this is a truncate, extend or noop cast. |
| 716 | // If the input is signed, do a sign extend, noop, or truncate. |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 717 | Result.extOrTrunc(DestWidth); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 718 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | // FIXME: Clean this up! |
| 723 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 724 | APValue LV; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 725 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 726 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 727 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 728 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 729 | return false; |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 8ab15c8 | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 731 | Result.extOrTrunc(DestWidth); |
| 732 | Result = LV.getLValueOffset(); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 733 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 734 | return true; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 735 | } |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 736 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 737 | if (!SubExpr->getType()->isRealFloatingType()) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 738 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 739 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 740 | APFloat F(0.0); |
| 741 | if (!EvaluateFloat(SubExpr, F, Info)) |
Chris Lattner | 438f3b1 | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 742 | return Error(CastLoc, diag::err_expr_not_constant, DestType); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 743 | |
| 744 | // Determine whether we are converting to unsigned or signed. |
| 745 | bool DestSigned = DestType->isSignedIntegerType(); |
| 746 | |
| 747 | // FIXME: Warning for overflow. |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 748 | uint64_t Space[4]; |
| 749 | bool ignored; |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 750 | (void)F.convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 751 | llvm::APFloat::rmTowardZero, &ignored); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 752 | Result = llvm::APInt(DestWidth, 4, Space); |
| 753 | Result.setIsUnsigned(!DestSigned); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 754 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 755 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 756 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 757 | //===----------------------------------------------------------------------===// |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 758 | // Float Evaluation |
| 759 | //===----------------------------------------------------------------------===// |
| 760 | |
| 761 | namespace { |
| 762 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 763 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 764 | EvalInfo &Info; |
| 765 | APFloat &Result; |
| 766 | public: |
| 767 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 768 | : Info(info), Result(result) {} |
| 769 | |
| 770 | bool VisitStmt(Stmt *S) { |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 775 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 776 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 777 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 778 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 779 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 780 | bool VisitCastExpr(CastExpr *E); |
| 781 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 782 | }; |
| 783 | } // end anonymous namespace |
| 784 | |
| 785 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 786 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 787 | } |
| 788 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 789 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 790 | switch (E->isBuiltinCall()) { |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 791 | default: return false; |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 792 | case Builtin::BI__builtin_huge_val: |
| 793 | case Builtin::BI__builtin_huge_valf: |
| 794 | case Builtin::BI__builtin_huge_vall: |
| 795 | case Builtin::BI__builtin_inf: |
| 796 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 797 | case Builtin::BI__builtin_infl: { |
| 798 | const llvm::fltSemantics &Sem = |
| 799 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 27cde26 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 800 | Result = llvm::APFloat::getInf(Sem); |
| 801 | return true; |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 802 | } |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 803 | |
| 804 | case Builtin::BI__builtin_nan: |
| 805 | case Builtin::BI__builtin_nanf: |
| 806 | case Builtin::BI__builtin_nanl: |
| 807 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 808 | // can't constant fold it. |
| 809 | if (const StringLiteral *S = |
| 810 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 811 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 0b3efb4 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 812 | const llvm::fltSemantics &Sem = |
| 813 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 667e1ee | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 814 | Result = llvm::APFloat::getNaN(Sem); |
| 815 | return true; |
| 816 | } |
| 817 | } |
| 818 | return false; |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 819 | |
| 820 | case Builtin::BI__builtin_fabs: |
| 821 | case Builtin::BI__builtin_fabsf: |
| 822 | case Builtin::BI__builtin_fabsl: |
| 823 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 824 | return false; |
| 825 | |
| 826 | if (Result.isNegative()) |
| 827 | Result.changeSign(); |
| 828 | return true; |
| 829 | |
| 830 | case Builtin::BI__builtin_copysign: |
| 831 | case Builtin::BI__builtin_copysignf: |
| 832 | case Builtin::BI__builtin_copysignl: { |
| 833 | APFloat RHS(0.); |
| 834 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 835 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 836 | return false; |
| 837 | Result.copySign(RHS); |
| 838 | return true; |
| 839 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 843 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 844 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 845 | return false; |
| 846 | |
| 847 | switch (E->getOpcode()) { |
| 848 | default: return false; |
| 849 | case UnaryOperator::Plus: |
| 850 | return true; |
| 851 | case UnaryOperator::Minus: |
| 852 | Result.changeSign(); |
| 853 | return true; |
| 854 | } |
| 855 | } |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 856 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 857 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 858 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 859 | // and errors are supposed to work. |
Daniel Dunbar | 804ead0 | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 860 | APFloat RHS(0.0); |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 861 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 862 | return false; |
| 863 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 864 | return false; |
| 865 | |
| 866 | switch (E->getOpcode()) { |
| 867 | default: return false; |
| 868 | case BinaryOperator::Mul: |
| 869 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 870 | return true; |
| 871 | case BinaryOperator::Add: |
| 872 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 873 | return true; |
| 874 | case BinaryOperator::Sub: |
| 875 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 876 | return true; |
| 877 | case BinaryOperator::Div: |
| 878 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 879 | return true; |
| 880 | case BinaryOperator::Rem: |
| 881 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 882 | return true; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 887 | Result = E->getValue(); |
| 888 | return true; |
| 889 | } |
| 890 | |
Eli Friedman | 7888b93 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 891 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 892 | Expr* SubExpr = E->getSubExpr(); |
| 893 | const llvm::fltSemantics& destSemantics = |
| 894 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 895 | if (SubExpr->getType()->isIntegralType()) { |
| 896 | APSInt IntResult; |
| 897 | if (!EvaluateInteger(E, IntResult, Info)) |
| 898 | return false; |
| 899 | Result = APFloat(destSemantics, 1); |
| 900 | Result.convertFromAPInt(IntResult, IntResult.isSigned(), |
| 901 | APFloat::rmNearestTiesToEven); |
| 902 | return true; |
| 903 | } |
| 904 | if (SubExpr->getType()->isRealFloatingType()) { |
| 905 | if (!Visit(SubExpr)) |
| 906 | return false; |
| 907 | bool ignored; |
| 908 | Result.convert(destSemantics, APFloat::rmNearestTiesToEven, &ignored); |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 916 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 917 | return true; |
| 918 | } |
| 919 | |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 920 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 921 | // Top level TryEvaluate. |
| 922 | //===----------------------------------------------------------------------===// |
| 923 | |
Chris Lattner | 8729378 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 924 | /// tryEvaluate - Return true if this is a constant which we can fold using |
| 925 | /// any crazy technique (that has nothing to do with language standards) that |
| 926 | /// we want to. If this function returns true, it returns the folded constant |
| 927 | /// in Result. |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 928 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 929 | EvalInfo Info(Ctx); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 930 | if (getType()->isIntegerType()) { |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 931 | llvm::APSInt sInt(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 932 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 933 | Result = APValue(sInt); |
| 934 | return true; |
| 935 | } |
Eli Friedman | 2f44549 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 936 | } else if (getType()->isPointerType()) { |
| 937 | if (EvaluatePointer(this, Result, Info)) { |
| 938 | return true; |
| 939 | } |
| 940 | } else if (getType()->isRealFloatingType()) { |
| 941 | llvm::APFloat f(0.0); |
| 942 | if (EvaluateFloat(this, f, Info)) { |
| 943 | Result = APValue(f); |
| 944 | return true; |
| 945 | } |
| 946 | } |
Anders Carlsson | 47968a9 | 2008-08-10 17:03:01 +0000 | [diff] [blame] | 947 | |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 948 | return false; |
| 949 | } |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 950 | |
| 951 | /// isEvaluatable - Call tryEvaluate to see if this expression can be constant |
| 952 | /// folded, but discard the result. |
| 953 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
| 954 | APValue V; |
| 955 | return tryEvaluate(V, Ctx); |
| 956 | } |