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