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 | // |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 10 | // This files 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 { |
| 28 | if (isa<lval::SymbolVal>(this)) |
| 29 | return (symbol_iterator) (&Data); |
| 30 | else if (isa<nonlval::SymbolVal>(this)) |
| 31 | return (symbol_iterator) (&Data); |
| 32 | else if (isa<nonlval::SymIntConstraintVal>(this)) { |
| 33 | const SymIntConstraint& C = |
| 34 | cast<nonlval::SymIntConstraintVal>(this)->getConstraint(); |
| 35 | |
| 36 | return (symbol_iterator) &C.getSymbol(); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return NULL; |
| 40 | } |
| 41 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 42 | RVal::symbol_iterator RVal::symbol_end() const { |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 43 | symbol_iterator X = symbol_begin(); |
| 44 | return X ? X+1 : NULL; |
| 45 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 48 | // Transfer function dispatch for Non-LVals. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 51 | nonlval::ConcreteInt |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 52 | nonlval::ConcreteInt::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 53 | const nonlval::ConcreteInt& R) const { |
| 54 | |
| 55 | return ValMgr.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | // Bitwise-Complement. |
| 60 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 61 | nonlval::ConcreteInt |
| 62 | nonlval::ConcreteInt::EvalComplement(ValueManager& ValMgr) const { |
| 63 | return ValMgr.getValue(~getValue()); |
| 64 | } |
| 65 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 66 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 68 | nonlval::ConcreteInt |
| 69 | nonlval::ConcreteInt::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 70 | assert (U->getType() == U->getSubExpr()->getType()); |
| 71 | assert (U->getType()->isIntegerType()); |
| 72 | return ValMgr.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 76 | // Transfer function dispatch for LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 77 | //===----------------------------------------------------------------------===// |
| 78 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 79 | lval::ConcreteInt |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 80 | lval::ConcreteInt::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 81 | const lval::ConcreteInt& R) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 82 | |
| 83 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 84 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 85 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 86 | return ValMgr.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 89 | NonLVal LVal::EQ(ValueManager& ValMgr, const LVal& R) const { |
| 90 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 91 | switch (getSubKind()) { |
| 92 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 93 | assert(false && "EQ not implemented for this LVal."); |
| 94 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 96 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 97 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 98 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 99 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 101 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 102 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 103 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 105 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 106 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
| 107 | BinaryOperator::EQ, |
| 108 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 109 | |
| 110 | return nonlval::SymIntConstraintVal(C); |
| 111 | } |
| 112 | |
| 113 | break; |
| 114 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 115 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 116 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 117 | |
| 118 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 119 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 120 | BinaryOperator::EQ, |
| 121 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 122 | |
| 123 | return nonlval::SymIntConstraintVal(C); |
| 124 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 126 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement unification."); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 127 | |
| 128 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 129 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 131 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 132 | if (isa<lval::DeclVal>(R)) { |
| 133 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
| 134 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 138 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 140 | return NonLVal::MakeIntTruthVal(ValMgr, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 143 | NonLVal LVal::NE(ValueManager& ValMgr, const LVal& R) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 144 | switch (getSubKind()) { |
| 145 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 146 | assert(false && "NE not implemented for this LVal."); |
| 147 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 149 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 150 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 151 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 152 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 154 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 155 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 156 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 157 | |
| 158 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 159 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 160 | BinaryOperator::NE, |
| 161 | cast<lval::ConcreteInt>(this)->getValue()); |
| 162 | |
| 163 | return nonlval::SymIntConstraintVal(C); |
| 164 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 166 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 168 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 169 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 170 | |
| 171 | const SymIntConstraint& C = |
| 172 | ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
| 173 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 174 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 175 | |
| 176 | return nonlval::SymIntConstraintVal(C); |
| 177 | } |
| 178 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 179 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement sym !=."); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 180 | |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 185 | if (isa<lval::DeclVal>(R)) { |
| 186 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
| 187 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
| 188 | } |
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 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 191 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 193 | return NonLVal::MakeIntTruthVal(ValMgr, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 197 | // Utility methods for constructing Non-LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 198 | //===----------------------------------------------------------------------===// |
| 199 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 200 | NonLVal NonLVal::MakeVal(ValueManager& ValMgr, uint64_t X, QualType T, |
| 201 | SourceLocation Loc) { |
| 202 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 203 | return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 206 | NonLVal NonLVal::MakeVal(ValueManager& ValMgr, IntegerLiteral* I) { |
| 207 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 208 | return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 209 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 212 | NonLVal NonLVal::MakeIntTruthVal(ValueManager& ValMgr, bool b) { |
| 213 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 214 | return nonlval::ConcreteInt(ValMgr.getTruthValue(b)); |
| 215 | } |
| 216 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 217 | RVal RVal::GetSymbolValue(SymbolManager& SymMgr, VarDecl* D) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 219 | QualType T = D->getType(); |
| 220 | |
| 221 | if (T->isPointerType() || T->isReferenceType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 222 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 223 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 224 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 227 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 228 | // Utility methods for constructing LVals. |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 231 | LVal LVal::MakeVal(AddrLabelExpr* E) { return lval::GotoLabel(E->getLabel()); } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | // Pretty-Printing. |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 237 | void RVal::printStdErr() const { print(*llvm::cerr.stream()); } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 239 | void RVal::print(std::ostream& Out) const { |
| 240 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 241 | switch (getBaseKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 243 | case UnknownKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 244 | Out << "Invalid"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 246 | case NonLValKind: |
| 247 | cast<NonLVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 249 | case LValKind: |
| 250 | cast<LVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame^] | 252 | case UndefinedKind: |
| 253 | Out << "Undefined"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 254 | |
| 255 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 256 | assert (false && "Invalid RVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 260 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 261 | |
| 262 | switch (Op) { |
| 263 | case BinaryOperator::Mul: Out << '*' ; break; |
| 264 | case BinaryOperator::Div: Out << '/' ; break; |
| 265 | case BinaryOperator::Rem: Out << '%' ; break; |
| 266 | case BinaryOperator::Add: Out << '+' ; break; |
| 267 | case BinaryOperator::Sub: Out << '-' ; break; |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 268 | case BinaryOperator::Shl: Out << "<<" ; break; |
| 269 | case BinaryOperator::Shr: Out << ">>" ; break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 270 | case BinaryOperator::LT: Out << "<" ; break; |
| 271 | case BinaryOperator::GT: Out << '>' ; break; |
| 272 | case BinaryOperator::LE: Out << "<=" ; break; |
| 273 | case BinaryOperator::GE: Out << ">=" ; break; |
| 274 | case BinaryOperator::EQ: Out << "==" ; break; |
| 275 | case BinaryOperator::NE: Out << "!=" ; break; |
| 276 | case BinaryOperator::And: Out << '&' ; break; |
| 277 | case BinaryOperator::Xor: Out << '^' ; break; |
| 278 | case BinaryOperator::Or: Out << '|' ; break; |
| 279 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 280 | default: assert(false && "Not yet implemented."); |
| 281 | } |
| 282 | } |
| 283 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 284 | void NonLVal::print(std::ostream& Out) const { |
| 285 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 286 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 288 | case nonlval::ConcreteIntKind: |
| 289 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 290 | |
| 291 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 292 | Out << 'U'; |
| 293 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 294 | break; |
| 295 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 296 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 297 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 298 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 299 | |
| 300 | case nonlval::SymIntConstraintValKind: { |
| 301 | const nonlval::SymIntConstraintVal& C = |
| 302 | *cast<nonlval::SymIntConstraintVal>(this); |
| 303 | |
| 304 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 305 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 306 | Out << ' ' << C.getConstraint().getInt().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 307 | |
| 308 | if (C.getConstraint().getInt().isUnsigned()) |
| 309 | Out << 'U'; |
| 310 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 311 | break; |
| 312 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 313 | |
| 314 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 315 | assert (false && "Pretty-printed not implemented for this NonLVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 320 | void LVal::print(std::ostream& Out) const { |
| 321 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 322 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 324 | case lval::ConcreteIntKind: |
| 325 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 326 | << " (LVal)"; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 327 | break; |
| 328 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 329 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 330 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 331 | break; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 332 | |
| 333 | case lval::GotoLabelKind: |
| 334 | Out << "&&" |
| 335 | << cast<lval::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 336 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 338 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 339 | Out << '&' |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 340 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
| 341 | break; |
| 342 | |
| 343 | case lval::FuncValKind: |
| 344 | Out << "function " |
| 345 | << cast<lval::FuncVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 349 | assert (false && "Pretty-printing not implemented for this LVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } |