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) { |
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 | //===----------------------------------------------------------------------===// |
| 127 | // Transfer function for Casts. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 130 | RValue RValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 131 | switch (getBaseKind()) { |
| 132 | default: assert(false && "Invalid RValue."); break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 133 | case LValueKind: return cast<LValue>(this)->EvalCast(ValMgr, CastExpr); |
| 134 | case NonLValueKind: return cast<NonLValue>(this)->EvalCast(ValMgr, CastExpr); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 135 | case UninitializedKind: case InvalidKind: break; |
| 136 | } |
| 137 | |
| 138 | return *this; |
| 139 | } |
| 140 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Transfer function dispatch for Non-LValues. |
| 144 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 146 | // Binary Operators (except assignments and comma). |
| 147 | |
| 148 | NonLValue NonLValue::EvalBinaryOp(ValueManager& ValMgr, |
| 149 | BinaryOperator::Opcode Op, |
| 150 | const NonLValue& RHS) const { |
| 151 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 152 | if (isa<UnknownVal>(this) || isa<UnknownVal>(RHS)) |
| 153 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 155 | if (isa<UninitializedVal>(this) || isa<UninitializedVal>(RHS)) |
| 156 | return cast<NonLValue>(UninitializedVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 157 | |
| 158 | switch (getSubKind()) { |
| 159 | default: |
| 160 | assert (false && "Binary Operators not implemented for this NonLValue"); |
| 161 | |
| 162 | case nonlval::ConcreteIntKind: |
| 163 | |
| 164 | if (isa<nonlval::ConcreteInt>(RHS)) { |
| 165 | nonlval::ConcreteInt& self = cast<nonlval::ConcreteInt>(*this); |
| 166 | return self.EvalBinaryOp(ValMgr, Op, |
| 167 | cast<nonlval::ConcreteInt>(RHS)); |
| 168 | } |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 169 | else if(isa<UnknownVal>(RHS)) |
| 170 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 171 | else |
| 172 | return RHS.EvalBinaryOp(ValMgr, Op, *this); |
| 173 | |
| 174 | case nonlval::SymbolValKind: { |
| 175 | const nonlval::SymbolVal& self = cast<nonlval::SymbolVal>(*this); |
| 176 | |
| 177 | switch (RHS.getSubKind()) { |
| 178 | default: assert ("Not Implemented." && false); |
| 179 | case nonlval::ConcreteIntKind: { |
| 180 | const SymIntConstraint& C = |
| 181 | ValMgr.getConstraint(self.getSymbol(), Op, |
| 182 | cast<nonlval::ConcreteInt>(RHS).getValue()); |
| 183 | |
| 184 | return nonlval::SymIntConstraintVal(C); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 191 | static const |
| 192 | llvm::APSInt& EvaluateAPSInt(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 193 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 194 | |
| 195 | switch (Op) { |
| 196 | default: |
| 197 | assert (false && "Invalid Opcode."); |
| 198 | |
| 199 | case BinaryOperator::Mul: |
| 200 | return ValMgr.getValue( V1 * V2 ); |
| 201 | |
| 202 | case BinaryOperator::Div: |
| 203 | return ValMgr.getValue( V1 / V2 ); |
| 204 | |
| 205 | case BinaryOperator::Rem: |
| 206 | return ValMgr.getValue( V1 % V2 ); |
| 207 | |
| 208 | case BinaryOperator::Add: |
| 209 | return ValMgr.getValue( V1 + V2 ); |
| 210 | |
| 211 | case BinaryOperator::Sub: |
| 212 | return ValMgr.getValue( V1 - V2 ); |
| 213 | |
| 214 | #if 0 |
| 215 | case BinaryOperator::Shl: |
| 216 | return ValMgr.getValue( V1 << V2 ); |
| 217 | |
| 218 | case BinaryOperator::Shr: |
| 219 | return ValMgr.getValue( V1 >> V2 ); |
| 220 | #endif |
| 221 | |
| 222 | case BinaryOperator::LT: |
| 223 | return ValMgr.getTruthValue( V1 < V2 ); |
| 224 | |
| 225 | case BinaryOperator::GT: |
| 226 | return ValMgr.getTruthValue( V1 > V2 ); |
| 227 | |
| 228 | case BinaryOperator::LE: |
| 229 | return ValMgr.getTruthValue( V1 <= V2 ); |
| 230 | |
| 231 | case BinaryOperator::GE: |
| 232 | return ValMgr.getTruthValue( V1 >= V2 ); |
| 233 | |
| 234 | case BinaryOperator::EQ: |
| 235 | return ValMgr.getTruthValue( V1 == V2 ); |
| 236 | |
| 237 | case BinaryOperator::NE: |
| 238 | return ValMgr.getTruthValue( V1 != V2 ); |
| 239 | |
| 240 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 241 | |
| 242 | case BinaryOperator::And: |
| 243 | return ValMgr.getValue( V1 & V2 ); |
| 244 | |
| 245 | case BinaryOperator::Or: |
| 246 | return ValMgr.getValue( V1 | V2 ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | nonlval::ConcreteInt |
| 251 | nonlval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr, |
| 252 | BinaryOperator::Opcode Op, |
| 253 | const nonlval::ConcreteInt& RHS) const { |
| 254 | |
| 255 | return EvaluateAPSInt(ValMgr, Op, getValue(), RHS.getValue()); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // Bitwise-Complement. |
| 260 | |
| 261 | NonLValue NonLValue::EvalComplement(ValueManager& ValMgr) const { |
| 262 | switch (getSubKind()) { |
| 263 | case nonlval::ConcreteIntKind: |
| 264 | return cast<nonlval::ConcreteInt>(this)->EvalComplement(ValMgr); |
| 265 | default: |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 266 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | nonlval::ConcreteInt |
| 271 | nonlval::ConcreteInt::EvalComplement(ValueManager& ValMgr) const { |
| 272 | return ValMgr.getValue(~getValue()); |
| 273 | } |
| 274 | |
| 275 | // Casts. |
| 276 | |
| 277 | RValue NonLValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 278 | if (!isa<nonlval::ConcreteInt>(this)) |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 279 | return UnknownVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 281 | APSInt V = cast<nonlval::ConcreteInt>(this)->getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 282 | QualType T = CastExpr->getType(); |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 283 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 284 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 285 | |
| 286 | if (CastExpr->getType()->isPointerType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 287 | return lval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 288 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 289 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 290 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 292 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 294 | NonLValue NonLValue::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 295 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 296 | case nonlval::ConcreteIntKind: |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 297 | return cast<nonlval::ConcreteInt>(this)->EvalMinus(ValMgr, U); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 298 | default: |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 299 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 303 | nonlval::ConcreteInt |
| 304 | nonlval::ConcreteInt::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 305 | assert (U->getType() == U->getSubExpr()->getType()); |
| 306 | assert (U->getType()->isIntegerType()); |
| 307 | return ValMgr.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 310 | //===----------------------------------------------------------------------===// |
| 311 | // Transfer function dispatch for LValues. |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 314 | // Binary Operators (except assignments and comma). |
| 315 | |
| 316 | RValue LValue::EvalBinaryOp(ValueManager& ValMgr, |
| 317 | BinaryOperator::Opcode Op, |
| 318 | const LValue& RHS) const { |
| 319 | |
| 320 | switch (Op) { |
| 321 | default: |
| 322 | assert (false && "Not yet implemented."); |
| 323 | |
| 324 | case BinaryOperator::EQ: |
| 325 | return EQ(ValMgr, RHS); |
| 326 | |
| 327 | case BinaryOperator::NE: |
| 328 | return NE(ValMgr, RHS); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | |
| 333 | lval::ConcreteInt |
| 334 | lval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr, |
| 335 | BinaryOperator::Opcode Op, |
| 336 | const lval::ConcreteInt& RHS) const { |
| 337 | |
| 338 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 339 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 340 | |
| 341 | return EvaluateAPSInt(ValMgr, Op, getValue(), RHS.getValue()); |
| 342 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 343 | |
| 344 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 345 | switch (getSubKind()) { |
| 346 | default: |
| 347 | assert(false && "EQ not implemented for this LValue."); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 348 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 350 | case lval::ConcreteIntKind: |
| 351 | if (isa<lval::ConcreteInt>(RHS)) { |
| 352 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 353 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 354 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 355 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 356 | } |
| 357 | else if (isa<lval::SymbolVal>(RHS)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 359 | const SymIntConstraint& C = |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 360 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 361 | BinaryOperator::EQ, |
| 362 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 363 | |
| 364 | return nonlval::SymIntConstraintVal(C); |
| 365 | } |
| 366 | |
| 367 | break; |
| 368 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 369 | case lval::SymbolValKind: { |
| 370 | if (isa<lval::ConcreteInt>(RHS)) { |
| 371 | |
| 372 | const SymIntConstraint& C = |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 373 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 374 | BinaryOperator::EQ, |
| 375 | cast<lval::ConcreteInt>(RHS).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 376 | |
| 377 | return nonlval::SymIntConstraintVal(C); |
| 378 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 380 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement unification."); |
| 381 | |
| 382 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 383 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 385 | case lval::DeclValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 386 | if (isa<lval::DeclVal>(RHS)) { |
| 387 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 388 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 389 | } |
| 390 | |
| 391 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 392 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 393 | |
| 394 | return NonLValue::GetIntTruthValue(ValMgr, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 398 | switch (getSubKind()) { |
| 399 | default: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 400 | assert(false && "NE not implemented for this LValue."); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 401 | return cast<NonLValue>(UnknownVal()); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 402 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 403 | case lval::ConcreteIntKind: |
| 404 | if (isa<lval::ConcreteInt>(RHS)) { |
| 405 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 406 | cast<lval::ConcreteInt>(RHS).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 407 | |
| 408 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 409 | } |
| 410 | else if (isa<lval::SymbolVal>(RHS)) { |
| 411 | |
| 412 | const SymIntConstraint& C = |
| 413 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 414 | BinaryOperator::NE, |
| 415 | cast<lval::ConcreteInt>(this)->getValue()); |
| 416 | |
| 417 | return nonlval::SymIntConstraintVal(C); |
| 418 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 420 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 422 | case lval::SymbolValKind: { |
| 423 | if (isa<lval::ConcreteInt>(RHS)) { |
| 424 | |
| 425 | const SymIntConstraint& C = |
| 426 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 427 | BinaryOperator::NE, |
| 428 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 429 | |
| 430 | return nonlval::SymIntConstraintVal(C); |
| 431 | } |
| 432 | |
| 433 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement sym !=."); |
| 434 | |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | case lval::DeclValKind: |
| 439 | if (isa<lval::DeclVal>(RHS)) { |
| 440 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
| 441 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 442 | } |
| 443 | |
| 444 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 445 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 446 | |
| 447 | return NonLValue::GetIntTruthValue(ValMgr, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 450 | // Casts. |
| 451 | |
| 452 | RValue LValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 453 | if (CastExpr->getType()->isPointerType()) |
| 454 | return *this; |
| 455 | |
| 456 | assert (CastExpr->getType()->isIntegerType()); |
| 457 | |
| 458 | if (!isa<lval::ConcreteInt>(*this)) |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame^] | 459 | return UnknownVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 460 | |
| 461 | APSInt V = cast<lval::ConcreteInt>(this)->getValue(); |
| 462 | QualType T = CastExpr->getType(); |
| 463 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
| 464 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 465 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
| 466 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 467 | |
| 468 | //===----------------------------------------------------------------------===// |
| 469 | // Utility methods for constructing Non-LValues. |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
| 472 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 473 | SourceLocation Loc) { |
| 474 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 475 | return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 479 | return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 480 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 483 | NonLValue NonLValue::GetIntTruthValue(ValueManager& ValMgr, bool b) { |
| 484 | return nonlval::ConcreteInt(ValMgr.getTruthValue(b)); |
| 485 | } |
| 486 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 487 | RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 488 | QualType T = D->getType(); |
| 489 | |
| 490 | if (T->isPointerType() || T->isReferenceType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 491 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 492 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 493 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 496 | void RValue::print() const { |
| 497 | print(*llvm::cerr.stream()); |
| 498 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 500 | //===----------------------------------------------------------------------===// |
| 501 | // Pretty-Printing. |
| 502 | //===----------------------------------------------------------------------===// |
| 503 | |
| 504 | void RValue::print(std::ostream& Out) const { |
| 505 | switch (getBaseKind()) { |
| 506 | case InvalidKind: |
| 507 | Out << "Invalid"; |
| 508 | break; |
| 509 | |
| 510 | case NonLValueKind: |
| 511 | cast<NonLValue>(this)->print(Out); |
| 512 | break; |
| 513 | |
| 514 | case LValueKind: |
| 515 | cast<LValue>(this)->print(Out); |
| 516 | break; |
| 517 | |
| 518 | case UninitializedKind: |
| 519 | Out << "Uninitialized"; |
| 520 | break; |
| 521 | |
| 522 | default: |
| 523 | assert (false && "Invalid RValue."); |
| 524 | } |
| 525 | } |
| 526 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 527 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 528 | switch (Op) { |
| 529 | case BinaryOperator::Add: Out << "+" ; break; |
| 530 | case BinaryOperator::Sub: Out << "-" ; break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 531 | case BinaryOperator::EQ: Out << "=="; break; |
| 532 | case BinaryOperator::NE: Out << "!="; break; |
| 533 | default: assert(false && "Not yet implemented."); |
| 534 | } |
| 535 | } |
| 536 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 537 | void NonLValue::print(std::ostream& Out) const { |
| 538 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 539 | case nonlval::ConcreteIntKind: |
| 540 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 541 | |
| 542 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 543 | Out << 'U'; |
| 544 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 545 | break; |
| 546 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 547 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 548 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 549 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 550 | |
| 551 | case nonlval::SymIntConstraintValKind: { |
| 552 | const nonlval::SymIntConstraintVal& C = |
| 553 | *cast<nonlval::SymIntConstraintVal>(this); |
| 554 | |
| 555 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 556 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 557 | Out << ' ' << C.getConstraint().getInt().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 558 | |
| 559 | if (C.getConstraint().getInt().isUnsigned()) |
| 560 | Out << 'U'; |
| 561 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 564 | |
| 565 | default: |
| 566 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 571 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 572 | void LValue::print(std::ostream& Out) const { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 573 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 574 | case lval::ConcreteIntKind: |
| 575 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 576 | << " (LValue)"; |
| 577 | break; |
| 578 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 579 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 580 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 581 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 582 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 583 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 584 | Out << '&' |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 585 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | default: |
| 589 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 590 | break; |
| 591 | } |
| 592 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 593 | |