Ted Kremenek | a90ccfe | 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 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame^] | 37 | QualType SymbolData::getType() const { |
| 38 | switch (getKind()) { |
| 39 | default: |
| 40 | assert (false && "getType() not implemented for this symbol."); |
| 41 | |
| 42 | case ParmKind: |
| 43 | return static_cast<ParmVarDecl*>(getPtr())->getType(); |
| 44 | } |
| 45 | } |
| 46 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 47 | SymbolManager::SymbolManager() {} |
| 48 | SymbolManager::~SymbolManager() {} |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 51 | // Values and ValueManager. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | |
| 54 | ValueManager::~ValueManager() { |
| 55 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 56 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 57 | // frees an aux. memory allocated to represent very large constants. |
| 58 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 59 | I->getValue().~APSInt(); |
| 60 | } |
| 61 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 62 | const APSInt& ValueManager::getValue(const APSInt& X) { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 63 | llvm::FoldingSetNodeID ID; |
| 64 | void* InsertPos; |
| 65 | typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy; |
| 66 | |
| 67 | X.Profile(ID); |
| 68 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 69 | |
| 70 | if (!P) { |
| 71 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 72 | new (P) FoldNodeTy(X); |
| 73 | APSIntSet.InsertNode(P, InsertPos); |
| 74 | } |
| 75 | |
| 76 | return *P; |
| 77 | } |
| 78 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 79 | const APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, |
| 80 | bool isUnsigned) { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 81 | APSInt V(BitWidth, isUnsigned); |
| 82 | V = X; |
| 83 | return getValue(V); |
| 84 | } |
| 85 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 86 | const APSInt& ValueManager::getValue(uint64_t X, QualType T, |
| 87 | SourceLocation Loc) { |
| 88 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 89 | unsigned bits = Ctx.getTypeSize(T, Loc); |
| 90 | APSInt V(bits, T->isUnsignedIntegerType()); |
| 91 | V = X; |
| 92 | return getValue(V); |
| 93 | } |
| 94 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 95 | const SymIntConstraint& |
| 96 | ValueManager::getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
| 97 | const llvm::APSInt& V) { |
| 98 | |
| 99 | llvm::FoldingSetNodeID ID; |
| 100 | SymIntConstraint::Profile(ID, sym, Op, V); |
| 101 | void* InsertPos; |
| 102 | |
| 103 | SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos); |
| 104 | |
| 105 | if (!C) { |
| 106 | C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>(); |
| 107 | new (C) SymIntConstraint(sym, Op, V); |
| 108 | SymIntCSet.InsertNode(C, InsertPos); |
| 109 | } |
| 110 | |
| 111 | return *C; |
| 112 | } |
| 113 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | // Transfer function for Casts. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 119 | switch (getBaseKind()) { |
| 120 | default: assert(false && "Invalid RValue."); break; |
| 121 | case LValueKind: return cast<LValue>(this)->Cast(ValMgr, CastExpr); |
| 122 | case NonLValueKind: return cast<NonLValue>(this)->Cast(ValMgr, CastExpr); |
| 123 | case UninitializedKind: case InvalidKind: break; |
| 124 | } |
| 125 | |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | RValue LValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 130 | if (CastExpr->getType()->isPointerType()) |
| 131 | return *this; |
| 132 | |
| 133 | assert (CastExpr->getType()->isIntegerType()); |
| 134 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 135 | if (!isa<lval::ConcreteInt>(*this)) |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 136 | return InvalidValue(); |
| 137 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 138 | APSInt V = cast<lval::ConcreteInt>(this)->getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 139 | QualType T = CastExpr->getType(); |
| 140 | V.setIsUnsigned(T->isUnsignedIntegerType()); |
| 141 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 142 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | RValue NonLValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 146 | if (!isa<nonlval::ConcreteInt>(this)) |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 147 | return InvalidValue(); |
| 148 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 149 | APSInt V = cast<nonlval::ConcreteInt>(this)->getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 150 | QualType T = CastExpr->getType(); |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 151 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 152 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 153 | |
| 154 | if (CastExpr->getType()->isPointerType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 155 | return lval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 156 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 157 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 159 | |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | // Transfer function dispatch for Non-LValues. |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 164 | NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 165 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 166 | case nonlval::ConcreteIntKind: |
| 167 | return cast<nonlval::ConcreteInt>(this)->UnaryMinus(ValMgr, U); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 168 | default: |
| 169 | return cast<NonLValue>(InvalidValue()); |
| 170 | } |
| 171 | } |
| 172 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 173 | NonLValue NonLValue::BitwiseComplement(ValueManager& ValMgr) const { |
| 174 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 175 | case nonlval::ConcreteIntKind: |
| 176 | return cast<nonlval::ConcreteInt>(this)->BitwiseComplement(ValMgr); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 177 | default: |
| 178 | return cast<NonLValue>(InvalidValue()); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 183 | #define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\ |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 184 | case (k1##Kind*nonlval::NumKind+k2##Kind):\ |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 185 | return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS)); |
| 186 | |
| 187 | #define NONLVALUE_DISPATCH(Op)\ |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 188 | switch (getSubKind()*nonlval::NumKind+RHS.getSubKind()){\ |
| 189 | NONLVALUE_DISPATCH_CASE(nonlval::ConcreteInt,nonlval::ConcreteInt,Op)\ |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 190 | default:\ |
| 191 | if (getBaseKind() == UninitializedKind ||\ |
| 192 | RHS.getBaseKind() == UninitializedKind)\ |
| 193 | return cast<NonLValue>(UninitializedValue());\ |
| 194 | assert (!isValid() || !RHS.isValid() && "Missing case.");\ |
| 195 | break;\ |
| 196 | }\ |
| 197 | return cast<NonLValue>(InvalidValue()); |
| 198 | |
| 199 | NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 200 | NONLVALUE_DISPATCH(Add) |
| 201 | } |
| 202 | |
| 203 | NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 204 | NONLVALUE_DISPATCH(Sub) |
| 205 | } |
| 206 | |
| 207 | NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 208 | NONLVALUE_DISPATCH(Mul) |
| 209 | } |
| 210 | |
| 211 | NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 212 | NONLVALUE_DISPATCH(Div) |
| 213 | } |
| 214 | |
| 215 | NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 216 | NONLVALUE_DISPATCH(Rem) |
| 217 | } |
| 218 | |
| 219 | NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 220 | NONLVALUE_DISPATCH(EQ) |
| 221 | } |
| 222 | |
| 223 | NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 224 | NONLVALUE_DISPATCH(NE) |
| 225 | } |
| 226 | |
| 227 | #undef NONLVALUE_DISPATCH_CASE |
| 228 | #undef NONLVALUE_DISPATCH |
| 229 | |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | // Transfer function dispatch for LValues. |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
| 234 | |
| 235 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 236 | switch (getSubKind()) { |
| 237 | default: |
| 238 | assert(false && "EQ not implemented for this LValue."); |
| 239 | return cast<NonLValue>(InvalidValue()); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 241 | case lval::ConcreteIntKind: |
| 242 | if (isa<lval::ConcreteInt>(RHS)) { |
| 243 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
| 244 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 245 | |
| 246 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 247 | } |
| 248 | else if (isa<lval::SymbolVal>(RHS)) { |
| 249 | |
| 250 | const SymIntConstraint& C = |
| 251 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 252 | BinaryOperator::EQ, |
| 253 | cast<lval::ConcreteInt>(this)->getValue()); |
| 254 | |
| 255 | return nonlval::SymIntConstraintVal(C); |
| 256 | } |
| 257 | |
| 258 | break; |
| 259 | |
| 260 | case lval::SymbolValKind: { |
| 261 | if (isa<lval::ConcreteInt>(RHS)) { |
| 262 | |
| 263 | const SymIntConstraint& C = |
| 264 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 265 | BinaryOperator::EQ, |
| 266 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 267 | |
| 268 | return nonlval::SymIntConstraintVal(C); |
| 269 | } |
| 270 | |
| 271 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement unification."); |
| 272 | |
| 273 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 274 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 276 | case lval::DeclValKind: |
| 277 | if (isa<lval::DeclVal>(RHS)) { |
| 278 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 279 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 280 | } |
| 281 | |
| 282 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 283 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 284 | |
| 285 | return NonLValue::GetIntTruthValue(ValMgr, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 289 | switch (getSubKind()) { |
| 290 | default: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 291 | assert(false && "NE not implemented for this LValue."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 292 | return cast<NonLValue>(InvalidValue()); |
| 293 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 294 | case lval::ConcreteIntKind: |
| 295 | if (isa<lval::ConcreteInt>(RHS)) { |
| 296 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
| 297 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 298 | |
| 299 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 300 | } |
| 301 | else if (isa<lval::SymbolVal>(RHS)) { |
| 302 | |
| 303 | const SymIntConstraint& C = |
| 304 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 305 | BinaryOperator::NE, |
| 306 | cast<lval::ConcreteInt>(this)->getValue()); |
| 307 | |
| 308 | return nonlval::SymIntConstraintVal(C); |
| 309 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 311 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 313 | case lval::SymbolValKind: { |
| 314 | if (isa<lval::ConcreteInt>(RHS)) { |
| 315 | |
| 316 | const SymIntConstraint& C = |
| 317 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 318 | BinaryOperator::NE, |
| 319 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 320 | |
| 321 | return nonlval::SymIntConstraintVal(C); |
| 322 | } |
| 323 | |
| 324 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement sym !=."); |
| 325 | |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | case lval::DeclValKind: |
| 330 | if (isa<lval::DeclVal>(RHS)) { |
| 331 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 332 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 333 | } |
| 334 | |
| 335 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 336 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 337 | |
| 338 | return NonLValue::GetIntTruthValue(ValMgr, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // Utility methods for constructing Non-LValues. |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
| 346 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 347 | SourceLocation Loc) { |
| 348 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 349 | return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 353 | return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 354 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 358 | QualType T = D->getType(); |
| 359 | |
| 360 | if (T->isPointerType() || T->isReferenceType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 361 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 362 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 363 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | //===----------------------------------------------------------------------===// |
| 367 | // Pretty-Printing. |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
| 370 | void RValue::print(std::ostream& Out) const { |
| 371 | switch (getBaseKind()) { |
| 372 | case InvalidKind: |
| 373 | Out << "Invalid"; |
| 374 | break; |
| 375 | |
| 376 | case NonLValueKind: |
| 377 | cast<NonLValue>(this)->print(Out); |
| 378 | break; |
| 379 | |
| 380 | case LValueKind: |
| 381 | cast<LValue>(this)->print(Out); |
| 382 | break; |
| 383 | |
| 384 | case UninitializedKind: |
| 385 | Out << "Uninitialized"; |
| 386 | break; |
| 387 | |
| 388 | default: |
| 389 | assert (false && "Invalid RValue."); |
| 390 | } |
| 391 | } |
| 392 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 393 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
| 394 | switch (Op) { |
| 395 | case BinaryOperator::EQ: Out << "=="; break; |
| 396 | case BinaryOperator::NE: Out << "!="; break; |
| 397 | default: assert(false && "Not yet implemented."); |
| 398 | } |
| 399 | } |
| 400 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 401 | void NonLValue::print(std::ostream& Out) const { |
| 402 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 403 | case nonlval::ConcreteIntKind: |
| 404 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 407 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 408 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 409 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 410 | |
| 411 | case nonlval::SymIntConstraintValKind: { |
| 412 | const nonlval::SymIntConstraintVal& C = |
| 413 | *cast<nonlval::SymIntConstraintVal>(this); |
| 414 | |
| 415 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 416 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 417 | Out << ' ' << C.getConstraint().getInt().toString(); |
| 418 | break; |
| 419 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 420 | |
| 421 | default: |
| 422 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 428 | void LValue::print(std::ostream& Out) const { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 429 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 430 | case lval::ConcreteIntKind: |
| 431 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 432 | << " (LValue)"; |
| 433 | break; |
| 434 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 435 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 436 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 437 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 439 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 440 | Out << '&' |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 441 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | default: |
| 445 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 446 | break; |
| 447 | } |
| 448 | } |