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