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