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