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