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