Ted Kremenek | 7219790 | 2008-01-31 19:34:24 +0000 | [diff] [blame^] | 1 | //= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- C++ -*-==// |
| 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 files defines RValue, LValue, and NonLValue, classes that represent |
| 11 | // abstract r-values for use with path-sensitive value tracking. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "RValues.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using llvm::dyn_cast; |
| 19 | using llvm::cast; |
| 20 | using llvm::APSInt; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // SymbolManager. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | SymbolID SymbolManager::getSymbol(ParmVarDecl* D) { |
| 27 | SymbolID& X = DataToSymbol[D]; |
| 28 | |
| 29 | if (!X.isInitialized()) { |
| 30 | X = SymbolToData.size(); |
| 31 | SymbolToData.push_back(D); |
| 32 | } |
| 33 | |
| 34 | return X; |
| 35 | } |
| 36 | |
| 37 | SymbolManager::SymbolManager() {} |
| 38 | SymbolManager::~SymbolManager() {} |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // ValueManager. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | ValueManager::~ValueManager() { |
| 45 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 46 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 47 | // frees an aux. memory allocated to represent very large constants. |
| 48 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 49 | I->getValue().~APSInt(); |
| 50 | } |
| 51 | |
| 52 | APSInt& ValueManager::getValue(const APSInt& X) { |
| 53 | llvm::FoldingSetNodeID ID; |
| 54 | void* InsertPos; |
| 55 | typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy; |
| 56 | |
| 57 | X.Profile(ID); |
| 58 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 59 | |
| 60 | if (!P) { |
| 61 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 62 | new (P) FoldNodeTy(X); |
| 63 | APSIntSet.InsertNode(P, InsertPos); |
| 64 | } |
| 65 | |
| 66 | return *P; |
| 67 | } |
| 68 | |
| 69 | APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) { |
| 70 | APSInt V(BitWidth, isUnsigned); |
| 71 | V = X; |
| 72 | return getValue(V); |
| 73 | } |
| 74 | |
| 75 | APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) { |
| 76 | unsigned bits = Ctx.getTypeSize(T, Loc); |
| 77 | APSInt V(bits, T->isUnsignedIntegerType()); |
| 78 | V = X; |
| 79 | return getValue(V); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | // Transfer function dispatch for Non-LValues. |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
| 87 | RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 88 | switch (getSubKind()) { |
| 89 | case ConcreteIntKind: |
| 90 | return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr); |
| 91 | default: |
| 92 | return InvalidValue(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 97 | switch (getSubKind()) { |
| 98 | case ConcreteIntKind: |
| 99 | return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U); |
| 100 | default: |
| 101 | return cast<NonLValue>(InvalidValue()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\ |
| 106 | case (k1##Kind*NumNonLValueKind+k2##Kind):\ |
| 107 | return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS)); |
| 108 | |
| 109 | #define NONLVALUE_DISPATCH(Op)\ |
| 110 | switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\ |
| 111 | NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\ |
| 112 | default:\ |
| 113 | if (getBaseKind() == UninitializedKind ||\ |
| 114 | RHS.getBaseKind() == UninitializedKind)\ |
| 115 | return cast<NonLValue>(UninitializedValue());\ |
| 116 | assert (!isValid() || !RHS.isValid() && "Missing case.");\ |
| 117 | break;\ |
| 118 | }\ |
| 119 | return cast<NonLValue>(InvalidValue()); |
| 120 | |
| 121 | NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 122 | NONLVALUE_DISPATCH(Add) |
| 123 | } |
| 124 | |
| 125 | NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 126 | NONLVALUE_DISPATCH(Sub) |
| 127 | } |
| 128 | |
| 129 | NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 130 | NONLVALUE_DISPATCH(Mul) |
| 131 | } |
| 132 | |
| 133 | NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 134 | NONLVALUE_DISPATCH(Div) |
| 135 | } |
| 136 | |
| 137 | NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 138 | NONLVALUE_DISPATCH(Rem) |
| 139 | } |
| 140 | |
| 141 | NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 142 | NONLVALUE_DISPATCH(EQ) |
| 143 | } |
| 144 | |
| 145 | NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 146 | NONLVALUE_DISPATCH(NE) |
| 147 | } |
| 148 | |
| 149 | #undef NONLVALUE_DISPATCH_CASE |
| 150 | #undef NONLVALUE_DISPATCH |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // Transfer function dispatch for LValues. |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | |
| 156 | |
| 157 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
| 158 | if (getSubKind() != RHS.getSubKind()) |
| 159 | return NonLValue::GetIntTruthValue(ValMgr, false); |
| 160 | |
| 161 | switch (getSubKind()) { |
| 162 | default: |
| 163 | assert(false && "EQ not implemented for this LValue."); |
| 164 | return cast<NonLValue>(InvalidValue()); |
| 165 | |
| 166 | case LValueDeclKind: { |
| 167 | bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS); |
| 168 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
| 174 | if (getSubKind() != RHS.getSubKind()) |
| 175 | return NonLValue::GetIntTruthValue(ValMgr, true); |
| 176 | |
| 177 | switch (getSubKind()) { |
| 178 | default: |
| 179 | assert(false && "EQ not implemented for this LValue."); |
| 180 | return cast<NonLValue>(InvalidValue()); |
| 181 | |
| 182 | case LValueDeclKind: { |
| 183 | bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS); |
| 184 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | // Utility methods for constructing Non-LValues. |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | |
| 194 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 195 | SourceLocation Loc) { |
| 196 | |
| 197 | return ConcreteInt(ValMgr.getValue(X, T, Loc)); |
| 198 | } |
| 199 | |
| 200 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
| 201 | return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 202 | I->getType()->isUnsignedIntegerType()))); |
| 203 | } |
| 204 | |
| 205 | RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 206 | QualType T = D->getType(); |
| 207 | |
| 208 | if (T->isPointerType() || T->isReferenceType()) |
| 209 | return SymbolicLValue(SymMgr.getSymbol(D)); |
| 210 | else |
| 211 | return SymbolicNonLValue(SymMgr.getSymbol(D)); |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // Pretty-Printing. |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | void RValue::print(std::ostream& Out) const { |
| 219 | switch (getBaseKind()) { |
| 220 | case InvalidKind: |
| 221 | Out << "Invalid"; |
| 222 | break; |
| 223 | |
| 224 | case NonLValueKind: |
| 225 | cast<NonLValue>(this)->print(Out); |
| 226 | break; |
| 227 | |
| 228 | case LValueKind: |
| 229 | cast<LValue>(this)->print(Out); |
| 230 | break; |
| 231 | |
| 232 | case UninitializedKind: |
| 233 | Out << "Uninitialized"; |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | assert (false && "Invalid RValue."); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void NonLValue::print(std::ostream& Out) const { |
| 242 | switch (getSubKind()) { |
| 243 | case ConcreteIntKind: |
| 244 | Out << cast<ConcreteInt>(this)->getValue().toString(); |
| 245 | break; |
| 246 | |
| 247 | case SymbolicNonLValueKind: |
| 248 | Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID(); |
| 249 | break; |
| 250 | |
| 251 | default: |
| 252 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void LValue::print(std::ostream& Out) const { |
| 258 | switch (getSubKind()) { |
| 259 | case SymbolicLValueKind: |
| 260 | Out << '$' << cast<SymbolicLValue>(this)->getSymbolID(); |
| 261 | break; |
| 262 | |
| 263 | case LValueDeclKind: |
| 264 | Out << '&' |
| 265 | << cast<LValueDecl>(this)->getDecl()->getIdentifier()->getName(); |
| 266 | break; |
| 267 | |
| 268 | default: |
| 269 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 270 | break; |
| 271 | } |
| 272 | } |