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 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines RVal, LVal, and NonLVal, classes that represent |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 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 | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | using llvm::dyn_cast; |
| 20 | using llvm::cast; |
| 21 | using llvm::APSInt; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 24 | // Symbol Iteration. |
| 25 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 27 | RVal::symbol_iterator RVal::symbol_begin() const { |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 28 | |
| 29 | // FIXME: This is a rat's nest. Cleanup. |
| 30 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 31 | if (isa<lval::SymbolVal>(this)) |
| 32 | return (symbol_iterator) (&Data); |
| 33 | else if (isa<nonlval::SymbolVal>(this)) |
| 34 | return (symbol_iterator) (&Data); |
| 35 | else if (isa<nonlval::SymIntConstraintVal>(this)) { |
| 36 | const SymIntConstraint& C = |
| 37 | cast<nonlval::SymIntConstraintVal>(this)->getConstraint(); |
| 38 | |
| 39 | return (symbol_iterator) &C.getSymbol(); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 40 | } |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 41 | else if (isa<nonlval::LValAsInteger>(this)) { |
| 42 | const nonlval::LValAsInteger& V = cast<nonlval::LValAsInteger>(*this); |
| 43 | return V.getPersistentLVal().symbol_begin(); |
| 44 | } |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 45 | else if (isa<lval::FieldOffset>(this)) { |
| 46 | const lval::FieldOffset& V = cast<lval::FieldOffset>(*this); |
| 47 | return V.getPersistentBase().symbol_begin(); |
| 48 | } |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 52 | RVal::symbol_iterator RVal::symbol_end() const { |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 53 | symbol_iterator X = symbol_begin(); |
| 54 | return X ? X+1 : NULL; |
| 55 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 58 | // Useful predicates. |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | bool RVal::isZeroConstant() const { |
| 62 | if (isa<lval::ConcreteInt>(*this)) |
| 63 | return cast<lval::ConcreteInt>(*this).getValue() == 0; |
| 64 | else if (isa<nonlval::ConcreteInt>(*this)) |
| 65 | return cast<nonlval::ConcreteInt>(*this).getValue() == 0; |
| 66 | else |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 72 | // Transfer function dispatch for Non-LVals. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 73 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 75b0a1c | 2008-07-18 15:59:33 +0000 | [diff] [blame] | 75 | RVal nonlval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, |
| 76 | BinaryOperator::Opcode Op, |
| 77 | const nonlval::ConcreteInt& R) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 75b0a1c | 2008-07-18 15:59:33 +0000 | [diff] [blame] | 79 | const llvm::APSInt* X = |
| 80 | BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 81 | |
| 82 | if (X) |
| 83 | return nonlval::ConcreteInt(*X); |
| 84 | else |
| 85 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 88 | // Bitwise-Complement. |
| 89 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 90 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 91 | nonlval::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const { |
| 92 | return BasicVals.getValue(~getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 95 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 97 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 98 | nonlval::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 99 | assert (U->getType() == U->getSubExpr()->getType()); |
| 100 | assert (U->getType()->isIntegerType()); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 101 | return BasicVals.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 104 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 105 | // Transfer function dispatch for LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 108 | RVal |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 109 | lval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 110 | const lval::ConcreteInt& R) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 111 | |
| 112 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 113 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 114 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 115 | const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 116 | |
| 117 | if (X) |
| 118 | return lval::ConcreteInt(*X); |
| 119 | else |
| 120 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 121 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 123 | NonLVal LVal::EQ(BasicValueFactory& BasicVals, const LVal& R) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 125 | switch (getSubKind()) { |
| 126 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 127 | assert(false && "EQ not implemented for this LVal."); |
| 128 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 130 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 131 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 132 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 133 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 135 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 136 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 137 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 139 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 140 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 141 | BinaryOperator::EQ, |
| 142 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 143 | |
| 144 | return nonlval::SymIntConstraintVal(C); |
| 145 | } |
| 146 | |
| 147 | break; |
| 148 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 149 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 150 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 151 | |
| 152 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 153 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 154 | BinaryOperator::EQ, |
| 155 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 156 | |
| 157 | return nonlval::SymIntConstraintVal(C); |
| 158 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 160 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement unification."); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 161 | |
| 162 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 163 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 165 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 166 | if (isa<lval::DeclVal>(R)) { |
| 167 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 168 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 172 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 174 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 177 | NonLVal LVal::NE(BasicValueFactory& BasicVals, const LVal& R) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 178 | switch (getSubKind()) { |
| 179 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 180 | assert(false && "NE not implemented for this LVal."); |
| 181 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 183 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 184 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 185 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 186 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 188 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 190 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 191 | |
| 192 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 193 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 194 | BinaryOperator::NE, |
| 195 | cast<lval::ConcreteInt>(this)->getValue()); |
| 196 | |
| 197 | return nonlval::SymIntConstraintVal(C); |
| 198 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 200 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 202 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 203 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 204 | |
| 205 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 206 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 207 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 208 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 209 | |
| 210 | return nonlval::SymIntConstraintVal(C); |
| 211 | } |
| 212 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 213 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement sym !=."); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 214 | |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 219 | if (isa<lval::DeclVal>(R)) { |
| 220 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 221 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 222 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 224 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 225 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 227 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 231 | // Utility methods for constructing Non-LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 232 | //===----------------------------------------------------------------------===// |
| 233 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 234 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) { |
| 235 | return nonlval::ConcreteInt(BasicVals.getValue(X, T)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 238 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 240 | return nonlval::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 241 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 244 | NonLVal NonLVal::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) { |
| 245 | return nonlval::ConcreteInt(BasicVals.getTruthValue(b)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 248 | RVal RVal::GetSymbolValue(SymbolManager& SymMgr, VarDecl* D) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 250 | QualType T = D->getType(); |
| 251 | |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 252 | if (T->isPointerLikeType() || T->isObjCQualifiedIdType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 253 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame] | 255 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 258 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 259 | // Utility methods for constructing LVals. |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
| 261 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 262 | LVal LVal::MakeVal(AddrLabelExpr* E) { return lval::GotoLabel(E->getLabel()); } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 264 | LVal LVal::MakeVal(StringLiteral* S) { |
| 265 | return lval::StringLiteralVal(S); |
| 266 | } |
| 267 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 269 | // Utility methods for constructing RVals (both NonLVals and LVals). |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | RVal RVal::MakeVal(BasicValueFactory& BasicVals, DeclRefExpr* E) { |
| 273 | |
| 274 | ValueDecl* D = cast<DeclRefExpr>(E)->getDecl(); |
| 275 | |
| 276 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 277 | return lval::DeclVal(VD); |
| 278 | } |
| 279 | else if (EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) { |
| 280 | |
| 281 | // FIXME: Do we need to cache a copy of this enum, since it |
| 282 | // already has persistent storage? We do this because we |
| 283 | // are comparing states using pointer equality. Perhaps there is |
| 284 | // a better way, since APInts are fairly lightweight. |
| 285 | |
| 286 | return nonlval::ConcreteInt(BasicVals.getValue(ED->getInitVal())); |
| 287 | } |
| 288 | else if (FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) { |
| 289 | return lval::FuncVal(FD); |
| 290 | } |
| 291 | |
| 292 | assert (false && |
| 293 | "ValueDecl support for this ValueDecl not implemented."); |
| 294 | |
| 295 | return UnknownVal(); |
| 296 | } |
| 297 | |
| 298 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 299 | // Pretty-Printing. |
| 300 | //===----------------------------------------------------------------------===// |
| 301 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 302 | void RVal::printStdErr() const { print(*llvm::cerr.stream()); } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 304 | void RVal::print(std::ostream& Out) const { |
| 305 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 306 | switch (getBaseKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 308 | case UnknownKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 309 | Out << "Invalid"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 311 | case NonLValKind: |
| 312 | cast<NonLVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 314 | case LValKind: |
| 315 | cast<LVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 317 | case UndefinedKind: |
| 318 | Out << "Undefined"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 319 | |
| 320 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 321 | assert (false && "Invalid RVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 325 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 326 | |
| 327 | switch (Op) { |
| 328 | case BinaryOperator::Mul: Out << '*' ; break; |
| 329 | case BinaryOperator::Div: Out << '/' ; break; |
| 330 | case BinaryOperator::Rem: Out << '%' ; break; |
| 331 | case BinaryOperator::Add: Out << '+' ; break; |
| 332 | case BinaryOperator::Sub: Out << '-' ; break; |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 333 | case BinaryOperator::Shl: Out << "<<" ; break; |
| 334 | case BinaryOperator::Shr: Out << ">>" ; break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 335 | case BinaryOperator::LT: Out << "<" ; break; |
| 336 | case BinaryOperator::GT: Out << '>' ; break; |
| 337 | case BinaryOperator::LE: Out << "<=" ; break; |
| 338 | case BinaryOperator::GE: Out << ">=" ; break; |
| 339 | case BinaryOperator::EQ: Out << "==" ; break; |
| 340 | case BinaryOperator::NE: Out << "!=" ; break; |
| 341 | case BinaryOperator::And: Out << '&' ; break; |
| 342 | case BinaryOperator::Xor: Out << '^' ; break; |
| 343 | case BinaryOperator::Or: Out << '|' ; break; |
| 344 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 345 | default: assert(false && "Not yet implemented."); |
| 346 | } |
| 347 | } |
| 348 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 349 | void NonLVal::print(std::ostream& Out) const { |
| 350 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 351 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 353 | case nonlval::ConcreteIntKind: |
| 354 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 355 | |
| 356 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 357 | Out << 'U'; |
| 358 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 359 | break; |
| 360 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 361 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 362 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 363 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 364 | |
| 365 | case nonlval::SymIntConstraintValKind: { |
| 366 | const nonlval::SymIntConstraintVal& C = |
| 367 | *cast<nonlval::SymIntConstraintVal>(this); |
| 368 | |
| 369 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 370 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 371 | Out << ' ' << C.getConstraint().getInt().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 372 | |
| 373 | if (C.getConstraint().getInt().isUnsigned()) |
| 374 | Out << 'U'; |
| 375 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 376 | break; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | case nonlval::LValAsIntegerKind: { |
| 380 | const nonlval::LValAsInteger& C = *cast<nonlval::LValAsInteger>(this); |
| 381 | C.getLVal().print(Out); |
| 382 | Out << " [as " << C.getNumBits() << " bit integer]"; |
| 383 | break; |
| 384 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 385 | |
| 386 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 387 | assert (false && "Pretty-printed not implemented for this NonLVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 392 | void LVal::print(std::ostream& Out) const { |
| 393 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 394 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 396 | case lval::ConcreteIntKind: |
| 397 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 398 | << " (LVal)"; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 399 | break; |
| 400 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 401 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 402 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 403 | break; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 404 | |
| 405 | case lval::GotoLabelKind: |
| 406 | Out << "&&" |
| 407 | << cast<lval::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 408 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 410 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 411 | Out << '&' |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 412 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
| 413 | break; |
| 414 | |
| 415 | case lval::FuncValKind: |
| 416 | Out << "function " |
| 417 | << cast<lval::FuncVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 418 | break; |
| 419 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 420 | case lval::StringLiteralValKind: |
| 421 | Out << "literal \"" |
| 422 | << cast<lval::StringLiteralVal>(this)->getLiteral()->getStrData() |
| 423 | << "\""; |
| 424 | break; |
| 425 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 426 | case lval::FieldOffsetKind: { |
| 427 | const lval::FieldOffset& C = *cast<lval::FieldOffset>(this); |
| 428 | C.getBase().print(Out); |
| 429 | Out << "." << C.getFieldDecl()->getName() << " (field LVal)"; |
| 430 | break; |
| 431 | } |
| 432 | |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 433 | case lval::ArrayOffsetKind: { |
| 434 | const lval::ArrayOffset& C = *cast<lval::ArrayOffset>(this); |
| 435 | C.getBase().print(Out); |
| 436 | Out << "["; |
| 437 | C.getOffset().print(Out); |
| 438 | Out << "] (lval array entry)"; |
| 439 | break; |
| 440 | } |
| 441 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 442 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 443 | assert (false && "Pretty-printing not implemented for this LVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | } |