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 | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
| 127 | // Symbol Iteration. |
| 128 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 130 | RValue::symbol_iterator RValue::symbol_begin() const { |
| 131 | if (isa<LValue>(this)) { |
| 132 | if (isa<lval::SymbolVal>(this)) |
| 133 | return (symbol_iterator) (&Data); |
| 134 | } |
| 135 | else { |
| 136 | if (isa<nonlval::SymbolVal>(this)) |
| 137 | return (symbol_iterator) (&Data); |
| 138 | else if (isa<nonlval::SymIntConstraintVal>(this)) { |
| 139 | const SymIntConstraint& C = |
| 140 | cast<nonlval::SymIntConstraintVal>(this)->getConstraint(); |
| 141 | return (symbol_iterator) &C.getSymbol(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | RValue::symbol_iterator RValue::symbol_end() const { |
| 149 | symbol_iterator X = symbol_begin(); |
| 150 | return X ? X+1 : NULL; |
| 151 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // Transfer function dispatch for Non-LValues. |
| 155 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 157 | static const |
| 158 | llvm::APSInt& EvaluateAPSInt(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 159 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 160 | |
| 161 | switch (Op) { |
| 162 | default: |
| 163 | assert (false && "Invalid Opcode."); |
| 164 | |
| 165 | case BinaryOperator::Mul: |
| 166 | return ValMgr.getValue( V1 * V2 ); |
| 167 | |
| 168 | case BinaryOperator::Div: |
| 169 | return ValMgr.getValue( V1 / V2 ); |
| 170 | |
| 171 | case BinaryOperator::Rem: |
| 172 | return ValMgr.getValue( V1 % V2 ); |
| 173 | |
| 174 | case BinaryOperator::Add: |
| 175 | return ValMgr.getValue( V1 + V2 ); |
| 176 | |
| 177 | case BinaryOperator::Sub: |
| 178 | return ValMgr.getValue( V1 - V2 ); |
| 179 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 180 | case BinaryOperator::Shl: |
Ted Kremenek | 59c2d26 | 2008-02-08 07:14:58 +0000 | [diff] [blame] | 181 | return ValMgr.getValue( V1.operator<<( (unsigned) V2.getZExtValue() )); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 182 | |
| 183 | case BinaryOperator::Shr: |
Ted Kremenek | 59c2d26 | 2008-02-08 07:14:58 +0000 | [diff] [blame] | 184 | return ValMgr.getValue( V1.operator>>( (unsigned) V2.getZExtValue() )); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 185 | |
| 186 | case BinaryOperator::LT: |
| 187 | return ValMgr.getTruthValue( V1 < V2 ); |
| 188 | |
| 189 | case BinaryOperator::GT: |
| 190 | return ValMgr.getTruthValue( V1 > V2 ); |
| 191 | |
| 192 | case BinaryOperator::LE: |
| 193 | return ValMgr.getTruthValue( V1 <= V2 ); |
| 194 | |
| 195 | case BinaryOperator::GE: |
| 196 | return ValMgr.getTruthValue( V1 >= V2 ); |
| 197 | |
| 198 | case BinaryOperator::EQ: |
| 199 | return ValMgr.getTruthValue( V1 == V2 ); |
| 200 | |
| 201 | case BinaryOperator::NE: |
| 202 | return ValMgr.getTruthValue( V1 != V2 ); |
| 203 | |
| 204 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 205 | |
| 206 | case BinaryOperator::And: |
| 207 | return ValMgr.getValue( V1 & V2 ); |
| 208 | |
| 209 | case BinaryOperator::Or: |
| 210 | return ValMgr.getValue( V1 | V2 ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | nonlval::ConcreteInt |
| 215 | nonlval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr, |
| 216 | BinaryOperator::Opcode Op, |
| 217 | const nonlval::ConcreteInt& RHS) const { |
| 218 | |
| 219 | return EvaluateAPSInt(ValMgr, Op, getValue(), RHS.getValue()); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | // Bitwise-Complement. |
| 224 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 225 | |
| 226 | nonlval::ConcreteInt |
| 227 | nonlval::ConcreteInt::EvalComplement(ValueManager& ValMgr) const { |
| 228 | return ValMgr.getValue(~getValue()); |
| 229 | } |
| 230 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 231 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 233 | nonlval::ConcreteInt |
| 234 | nonlval::ConcreteInt::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 235 | assert (U->getType() == U->getSubExpr()->getType()); |
| 236 | assert (U->getType()->isIntegerType()); |
| 237 | return ValMgr.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 240 | //===----------------------------------------------------------------------===// |
| 241 | // Transfer function dispatch for LValues. |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 244 | lval::ConcreteInt |
| 245 | lval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr, |
| 246 | BinaryOperator::Opcode Op, |
| 247 | const lval::ConcreteInt& RHS) const { |
| 248 | |
| 249 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 250 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 251 | |
| 252 | return EvaluateAPSInt(ValMgr, Op, getValue(), RHS.getValue()); |
| 253 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 254 | |
| 255 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 256 | switch (getSubKind()) { |
| 257 | default: |
| 258 | assert(false && "EQ not implemented for this LValue."); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 259 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 261 | case lval::ConcreteIntKind: |
| 262 | if (isa<lval::ConcreteInt>(RHS)) { |
| 263 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 264 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 265 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 266 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 267 | } |
| 268 | else if (isa<lval::SymbolVal>(RHS)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 270 | const SymIntConstraint& C = |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 271 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 272 | BinaryOperator::EQ, |
| 273 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 274 | |
| 275 | return nonlval::SymIntConstraintVal(C); |
| 276 | } |
| 277 | |
| 278 | break; |
| 279 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 280 | case lval::SymbolValKind: { |
| 281 | if (isa<lval::ConcreteInt>(RHS)) { |
| 282 | |
| 283 | const SymIntConstraint& C = |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 284 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 285 | BinaryOperator::EQ, |
| 286 | cast<lval::ConcreteInt>(RHS).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 287 | |
| 288 | return nonlval::SymIntConstraintVal(C); |
| 289 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 291 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement unification."); |
| 292 | |
| 293 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 294 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 296 | case lval::DeclValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 297 | if (isa<lval::DeclVal>(RHS)) { |
| 298 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 299 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 300 | } |
| 301 | |
| 302 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 303 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 304 | |
| 305 | return NonLValue::GetIntTruthValue(ValMgr, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 309 | switch (getSubKind()) { |
| 310 | default: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 311 | assert(false && "NE not implemented for this LValue."); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 312 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 314 | case lval::ConcreteIntKind: |
| 315 | if (isa<lval::ConcreteInt>(RHS)) { |
| 316 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 317 | cast<lval::ConcreteInt>(RHS).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 318 | |
| 319 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 320 | } |
| 321 | else if (isa<lval::SymbolVal>(RHS)) { |
| 322 | |
| 323 | const SymIntConstraint& C = |
| 324 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 325 | BinaryOperator::NE, |
| 326 | cast<lval::ConcreteInt>(this)->getValue()); |
| 327 | |
| 328 | return nonlval::SymIntConstraintVal(C); |
| 329 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 331 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 333 | case lval::SymbolValKind: { |
| 334 | if (isa<lval::ConcreteInt>(RHS)) { |
| 335 | |
| 336 | const SymIntConstraint& C = |
| 337 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 338 | BinaryOperator::NE, |
| 339 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 340 | |
| 341 | return nonlval::SymIntConstraintVal(C); |
| 342 | } |
| 343 | |
| 344 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement sym !=."); |
| 345 | |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | case lval::DeclValKind: |
| 350 | if (isa<lval::DeclVal>(RHS)) { |
| 351 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 352 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 353 | } |
| 354 | |
| 355 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 356 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 357 | |
| 358 | return NonLValue::GetIntTruthValue(ValMgr, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Utility methods for constructing Non-LValues. |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 368 | SourceLocation Loc) { |
| 369 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 370 | return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 374 | return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 375 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 378 | NonLValue NonLValue::GetIntTruthValue(ValueManager& ValMgr, bool b) { |
| 379 | return nonlval::ConcreteInt(ValMgr.getTruthValue(b)); |
| 380 | } |
| 381 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 382 | RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 383 | QualType T = D->getType(); |
| 384 | |
| 385 | if (T->isPointerType() || T->isReferenceType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 386 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 387 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 388 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 391 | //===----------------------------------------------------------------------===// |
| 392 | // Utility methods for constructing LValues. |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | |
| 395 | LValue LValue::GetValue(AddrLabelExpr* E) { |
| 396 | return lval::GotoLabel(E->getLabel()); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 397 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // Pretty-Printing. |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 403 | void RValue::print() const { |
| 404 | print(*llvm::cerr.stream()); |
| 405 | } |
| 406 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 407 | void RValue::print(std::ostream& Out) const { |
| 408 | switch (getBaseKind()) { |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 409 | case UnknownKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 410 | Out << "Invalid"; |
| 411 | break; |
| 412 | |
| 413 | case NonLValueKind: |
| 414 | cast<NonLValue>(this)->print(Out); |
| 415 | break; |
| 416 | |
| 417 | case LValueKind: |
| 418 | cast<LValue>(this)->print(Out); |
| 419 | break; |
| 420 | |
| 421 | case UninitializedKind: |
| 422 | Out << "Uninitialized"; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | assert (false && "Invalid RValue."); |
| 427 | } |
| 428 | } |
| 429 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 430 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 431 | switch (Op) { |
| 432 | case BinaryOperator::Add: Out << "+" ; break; |
| 433 | case BinaryOperator::Sub: Out << "-" ; break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 434 | case BinaryOperator::EQ: Out << "=="; break; |
| 435 | case BinaryOperator::NE: Out << "!="; break; |
| 436 | default: assert(false && "Not yet implemented."); |
| 437 | } |
| 438 | } |
| 439 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 440 | void NonLValue::print(std::ostream& Out) const { |
| 441 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 442 | case nonlval::ConcreteIntKind: |
| 443 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 444 | |
| 445 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 446 | Out << 'U'; |
| 447 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 448 | break; |
| 449 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 450 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 451 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 452 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 453 | |
| 454 | case nonlval::SymIntConstraintValKind: { |
| 455 | const nonlval::SymIntConstraintVal& C = |
| 456 | *cast<nonlval::SymIntConstraintVal>(this); |
| 457 | |
| 458 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 459 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 460 | Out << ' ' << C.getConstraint().getInt().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 461 | |
| 462 | if (C.getConstraint().getInt().isUnsigned()) |
| 463 | Out << 'U'; |
| 464 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 467 | |
| 468 | default: |
| 469 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 474 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 475 | void LValue::print(std::ostream& Out) const { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 476 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 477 | case lval::ConcreteIntKind: |
| 478 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 479 | << " (LValue)"; |
| 480 | break; |
| 481 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 482 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 483 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 484 | break; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 485 | |
| 486 | case lval::GotoLabelKind: |
| 487 | Out << "&&" |
| 488 | << cast<lval::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 489 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 491 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 492 | Out << '&' |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 493 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 494 | break; |
| 495 | |
| 496 | default: |
| 497 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 498 | break; |
| 499 | } |
| 500 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 501 | |