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