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