blob: f41051ed57c86c88b37ef61bce856fe82031532c [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//= 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
17using namespace clang;
18using llvm::dyn_cast;
19using llvm::cast;
20using llvm::APSInt;
21
22//===----------------------------------------------------------------------===//
23// SymbolManager.
24//===----------------------------------------------------------------------===//
25
26SymbolID SymbolManager::getSymbol(ParmVarDecl* D) {
27 SymbolID& X = DataToSymbol[D];
28
29 if (!X.isInitialized()) {
30 X = SymbolToData.size();
31 SymbolToData.push_back(D);
32 }
33
34 return X;
35}
36
37SymbolManager::SymbolManager() {}
38SymbolManager::~SymbolManager() {}
39
40//===----------------------------------------------------------------------===//
41// ValueManager.
42//===----------------------------------------------------------------------===//
43
44ValueManager::~ValueManager() {
45 // Note that the dstor for the contents of APSIntSet will never be called,
46 // so we iterate over the set and invoke the dstor for each APSInt. This
47 // frees an aux. memory allocated to represent very large constants.
48 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
49 I->getValue().~APSInt();
50}
51
52APSInt& ValueManager::getValue(const APSInt& X) {
53 llvm::FoldingSetNodeID ID;
54 void* InsertPos;
55 typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
56
57 X.Profile(ID);
58 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
59
60 if (!P) {
61 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
62 new (P) FoldNodeTy(X);
63 APSIntSet.InsertNode(P, InsertPos);
64 }
65
66 return *P;
67}
68
69APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) {
70 APSInt V(BitWidth, isUnsigned);
71 V = X;
72 return getValue(V);
73}
74
75APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) {
76 unsigned bits = Ctx.getTypeSize(T, Loc);
77 APSInt V(bits, T->isUnsignedIntegerType());
78 V = X;
79 return getValue(V);
80}
81
Ted Kremeneka6e4d212008-02-01 06:36:40 +000082//===----------------------------------------------------------------------===//
83// Transfer function for Casts.
84//===----------------------------------------------------------------------===//
85
86RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
87 switch (getBaseKind()) {
88 default: assert(false && "Invalid RValue."); break;
89 case LValueKind: return cast<LValue>(this)->Cast(ValMgr, CastExpr);
90 case NonLValueKind: return cast<NonLValue>(this)->Cast(ValMgr, CastExpr);
91 case UninitializedKind: case InvalidKind: break;
92 }
93
94 return *this;
95}
96
97RValue LValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
98 if (CastExpr->getType()->isPointerType())
99 return *this;
100
101 assert (CastExpr->getType()->isIntegerType());
102
103 if (!isa<ConcreteIntLValue>(*this))
104 return InvalidValue();
105
106 APSInt V = cast<ConcreteIntLValue>(this)->getValue();
107 QualType T = CastExpr->getType();
108 V.setIsUnsigned(T->isUnsignedIntegerType());
109 V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
110 return ConcreteInt(ValMgr.getValue(V));
111}
112
113RValue NonLValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
114 if (!isa<ConcreteInt>(this))
115 return InvalidValue();
116
117 APSInt V = cast<ConcreteInt>(this)->getValue();
118 QualType T = CastExpr->getType();
119 V.setIsUnsigned(T->isUnsignedIntegerType());
120 V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
121
122 if (CastExpr->getType()->isPointerType())
123 return ConcreteIntLValue(ValMgr.getValue(V));
124 else
125 return ConcreteInt(ValMgr.getValue(V));
126}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000127
128//===----------------------------------------------------------------------===//
129// Transfer function dispatch for Non-LValues.
130//===----------------------------------------------------------------------===//
131
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000132NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
133 switch (getSubKind()) {
134 case ConcreteIntKind:
135 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
136 default:
137 return cast<NonLValue>(InvalidValue());
138 }
139}
140
141#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
142case (k1##Kind*NumNonLValueKind+k2##Kind):\
143return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
144
145#define NONLVALUE_DISPATCH(Op)\
146switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
147NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
148default:\
149if (getBaseKind() == UninitializedKind ||\
150RHS.getBaseKind() == UninitializedKind)\
151return cast<NonLValue>(UninitializedValue());\
152assert (!isValid() || !RHS.isValid() && "Missing case.");\
153break;\
154}\
155return cast<NonLValue>(InvalidValue());
156
157NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
158 NONLVALUE_DISPATCH(Add)
159}
160
161NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
162 NONLVALUE_DISPATCH(Sub)
163}
164
165NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
166 NONLVALUE_DISPATCH(Mul)
167}
168
169NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
170 NONLVALUE_DISPATCH(Div)
171}
172
173NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
174 NONLVALUE_DISPATCH(Rem)
175}
176
177NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
178 NONLVALUE_DISPATCH(EQ)
179}
180
181NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
182 NONLVALUE_DISPATCH(NE)
183}
184
185#undef NONLVALUE_DISPATCH_CASE
186#undef NONLVALUE_DISPATCH
187
188//===----------------------------------------------------------------------===//
189// Transfer function dispatch for LValues.
190//===----------------------------------------------------------------------===//
191
192
193NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
194 if (getSubKind() != RHS.getSubKind())
195 return NonLValue::GetIntTruthValue(ValMgr, false);
196
197 switch (getSubKind()) {
198 default:
199 assert(false && "EQ not implemented for this LValue.");
200 return cast<NonLValue>(InvalidValue());
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000201
202 case ConcreteIntLValueKind: {
203 bool b = cast<ConcreteIntLValue>(this)->getValue() ==
204 cast<ConcreteIntLValue>(RHS).getValue();
205
206 return NonLValue::GetIntTruthValue(ValMgr, b);
207 }
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000208
209 case LValueDeclKind: {
210 bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS);
211 return NonLValue::GetIntTruthValue(ValMgr, b);
212 }
213 }
214}
215
216NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
217 if (getSubKind() != RHS.getSubKind())
218 return NonLValue::GetIntTruthValue(ValMgr, true);
219
220 switch (getSubKind()) {
221 default:
222 assert(false && "EQ not implemented for this LValue.");
223 return cast<NonLValue>(InvalidValue());
224
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000225 case ConcreteIntLValueKind: {
226 bool b = cast<ConcreteIntLValue>(this)->getValue() !=
227 cast<ConcreteIntLValue>(RHS).getValue();
228
229 return NonLValue::GetIntTruthValue(ValMgr, b);
230 }
231
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000232 case LValueDeclKind: {
233 bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
234 return NonLValue::GetIntTruthValue(ValMgr, b);
235 }
236 }
237}
238
239
240//===----------------------------------------------------------------------===//
241// Utility methods for constructing Non-LValues.
242//===----------------------------------------------------------------------===//
243
244NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
245 SourceLocation Loc) {
246
247 return ConcreteInt(ValMgr.getValue(X, T, Loc));
248}
249
250NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
251 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
252 I->getType()->isUnsignedIntegerType())));
253}
254
255RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
256 QualType T = D->getType();
257
258 if (T->isPointerType() || T->isReferenceType())
259 return SymbolicLValue(SymMgr.getSymbol(D));
260 else
261 return SymbolicNonLValue(SymMgr.getSymbol(D));
262}
263
264//===----------------------------------------------------------------------===//
265// Pretty-Printing.
266//===----------------------------------------------------------------------===//
267
268void RValue::print(std::ostream& Out) const {
269 switch (getBaseKind()) {
270 case InvalidKind:
271 Out << "Invalid";
272 break;
273
274 case NonLValueKind:
275 cast<NonLValue>(this)->print(Out);
276 break;
277
278 case LValueKind:
279 cast<LValue>(this)->print(Out);
280 break;
281
282 case UninitializedKind:
283 Out << "Uninitialized";
284 break;
285
286 default:
287 assert (false && "Invalid RValue.");
288 }
289}
290
291void NonLValue::print(std::ostream& Out) const {
292 switch (getSubKind()) {
293 case ConcreteIntKind:
294 Out << cast<ConcreteInt>(this)->getValue().toString();
295 break;
296
297 case SymbolicNonLValueKind:
298 Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID();
299 break;
300
301 default:
302 assert (false && "Pretty-printed not implemented for this NonLValue.");
303 break;
304 }
305}
306
307void LValue::print(std::ostream& Out) const {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000308 switch (getSubKind()) {
309 case ConcreteIntLValueKind:
310 Out << cast<ConcreteIntLValue>(this)->getValue().toString()
311 << " (LValue)";
312 break;
313
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000314 case SymbolicLValueKind:
315 Out << '$' << cast<SymbolicLValue>(this)->getSymbolID();
316 break;
317
318 case LValueDeclKind:
319 Out << '&'
320 << cast<LValueDecl>(this)->getDecl()->getIdentifier()->getName();
321 break;
322
323 default:
324 assert (false && "Pretty-printed not implemented for this LValue.");
325 break;
326 }
327}