blob: 90446a2cc56264bfd1faafce7ef35f5bac58079c [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//== RValues.h - 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#ifndef LLVM_CLANG_ANALYSIS_RVALUE_H
16#define LLVM_CLANG_ANALYSIS_RVALUE_H
17
18// FIXME: reduce the number of includes.
19
20#include "clang/Analysis/PathSensitive/GREngine.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/ASTContext.h"
24#include "clang/Analysis/Analyses/LiveVariables.h"
25
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/DataTypes.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/Support/Allocator.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/Streams.h"
36
37#include <functional>
38
39//==------------------------------------------------------------------------==//
40// RValue "management" data structures.
41//==------------------------------------------------------------------------==//
42
43namespace clang {
44
45class SymbolID {
46 unsigned Data;
47public:
48 SymbolID() : Data(~0) {}
49 SymbolID(unsigned x) : Data(x) {}
50
51 bool isInitialized() const { return Data != (unsigned) ~0; }
52 operator unsigned() const { assert (isInitialized()); return Data; }
53};
54
55class SymbolData {
56 uintptr_t Data;
57public:
58 enum Kind { ParmKind = 0x0, Mask = 0x3 };
59
60 SymbolData(ParmVarDecl* D)
61 : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
62
63 inline Kind getKind() const { return (Kind) (Data & Mask); }
64 inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
65 inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
66};
67
68class SymbolManager {
69 std::vector<SymbolData> SymbolToData;
70
71 typedef llvm::DenseMap<void*,SymbolID> MapTy;
72 MapTy DataToSymbol;
73
74public:
75 SymbolManager();
76 ~SymbolManager();
77
78 SymbolData getSymbolData(SymbolID id) const {
79 assert (id < SymbolToData.size());
80 return SymbolToData[id];
81 }
82
83 SymbolID getSymbol(ParmVarDecl* D);
84};
85
86class ValueManager {
87 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
88 APSIntSetTy;
89
90 ASTContext& Ctx;
91 APSIntSetTy APSIntSet;
92 llvm::BumpPtrAllocator BPAlloc;
93
94public:
95 ValueManager(ASTContext& ctx) : Ctx(ctx) {}
96 ~ValueManager();
97
98 ASTContext& getContext() const { return Ctx; }
99 llvm::APSInt& getValue(const llvm::APSInt& X);
100 llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
101 llvm::APSInt& getValue(uint64_t X, QualType T,
102 SourceLocation Loc = SourceLocation());
103};
104
105//==------------------------------------------------------------------------==//
106// Base RValue types.
107//==------------------------------------------------------------------------==//
108
109class RValue {
110public:
111 enum BaseKind { LValueKind=0x0,
112 NonLValueKind=0x1,
113 UninitializedKind=0x2,
114 InvalidKind=0x3 };
115
116 enum { BaseBits = 2,
117 BaseMask = 0x3 };
118
119private:
120 void* Data;
121 unsigned Kind;
122
123protected:
124 RValue(const void* d, bool isLValue, unsigned ValKind)
125 : Data(const_cast<void*>(d)),
126 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {}
127
128 explicit RValue(BaseKind k)
129 : Data(0), Kind(k) {}
130
131 void* getRawPtr() const {
132 return reinterpret_cast<void*>(Data);
133 }
134
135public:
136 ~RValue() {};
137
138 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
139
140 unsigned getRawKind() const { return Kind; }
141 BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
142 unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
143
144 void Profile(llvm::FoldingSetNodeID& ID) const {
145 ID.AddInteger((unsigned) getRawKind());
146 ID.AddPointer(reinterpret_cast<void*>(Data));
147 }
148
149 bool operator==(const RValue& RHS) const {
150 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
151 }
152
153 static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
154
155 inline bool isValid() const { return getRawKind() != InvalidKind; }
156 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
157
158 void print(std::ostream& OS) const;
159 void print() const { print(*llvm::cerr.stream()); }
160
161 // Implement isa<T> support.
162 static inline bool classof(const RValue*) { return true; }
163};
164
165class InvalidValue : public RValue {
166public:
167 InvalidValue() : RValue(InvalidKind) {}
168
169 static inline bool classof(const RValue* V) {
170 return V->getBaseKind() == InvalidKind;
171 }
172};
173
174class UninitializedValue : public RValue {
175public:
176 UninitializedValue() : RValue(UninitializedKind) {}
177
178 static inline bool classof(const RValue* V) {
179 return V->getBaseKind() == UninitializedKind;
180 }
181};
182
183class NonLValue : public RValue {
184protected:
185 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
186
187public:
188 void print(std::ostream& Out) const;
189
190 // Arithmetic operators.
191 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
192 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
193 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
194 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
195 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
196 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
197
198 // Equality operators.
199 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
200 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
201
202 // Utility methods to create NonLValues.
203 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
204 SourceLocation Loc = SourceLocation());
205
206 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
207
208 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
209 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
210 }
211
212 // Implement isa<T> support.
213 static inline bool classof(const RValue* V) {
214 return V->getBaseKind() >= NonLValueKind;
215 }
216};
217
218class LValue : public RValue {
219protected:
220 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
221
222public:
223 void print(std::ostream& Out) const;
224
225 // Equality operators.
226 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
227 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
228
229 // Implement isa<T> support.
230 static inline bool classof(const RValue* V) {
231 return V->getBaseKind() == LValueKind;
232 }
233};
234
235//==------------------------------------------------------------------------==//
236// Subclasses of LValue.
237//==------------------------------------------------------------------------==//
238
239enum LValueKind { SymbolicLValueKind, LValueDeclKind, NumLValueKind };
240
241class SymbolicLValue : public LValue {
242public:
243 SymbolicLValue(unsigned SymID)
244 : LValue(SymbolicLValueKind, reinterpret_cast<void*>((uintptr_t) SymID)) {}
245
246 SymbolID getSymbolID() const {
247 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
248 }
249
250 static inline bool classof(const RValue* V) {
251 return V->getSubKind() == SymbolicLValueKind;
252 }
253};
254
255class LValueDecl : public LValue {
256public:
257 LValueDecl(const ValueDecl* vd)
258 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
259
260 ValueDecl* getDecl() const {
261 return static_cast<ValueDecl*>(getRawPtr());
262 }
263
264 inline bool operator==(const LValueDecl& R) const {
265 return getDecl() == R.getDecl();
266 }
267
268 inline bool operator!=(const LValueDecl& R) const {
269 return getDecl() != R.getDecl();
270 }
271
272 // Implement isa<T> support.
273 static inline bool classof(const RValue* V) {
274 return V->getSubKind() == LValueDeclKind;
275 }
276};
277
278//==------------------------------------------------------------------------==//
279// Subclasses of NonLValue.
280//==------------------------------------------------------------------------==//
281
282enum NonLValueKind { SymbolicNonLValueKind, ConcreteIntKind,
283NumNonLValueKind };
284
285class SymbolicNonLValue : public NonLValue {
286public:
287 SymbolicNonLValue(unsigned SymID)
288 : NonLValue(SymbolicNonLValueKind,
289 reinterpret_cast<void*>((uintptr_t) SymID)) {}
290
291 SymbolID getSymbolID() const {
292 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
293 }
294
295 static inline bool classof(const RValue* V) {
296 return V->getSubKind() == SymbolicNonLValueKind;
297 }
298};
299
300class ConcreteInt : public NonLValue {
301public:
302 ConcreteInt(const llvm::APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
303
304 const llvm::APSInt& getValue() const {
305 return *static_cast<llvm::APSInt*>(getRawPtr());
306 }
307
308 // Arithmetic operators.
309
310 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
311 return ValMgr.getValue(getValue() + V.getValue());
312 }
313
314 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
315 return ValMgr.getValue(getValue() - V.getValue());
316 }
317
318 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
319 return ValMgr.getValue(getValue() * V.getValue());
320 }
321
322 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
323 return ValMgr.getValue(getValue() / V.getValue());
324 }
325
326 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
327 return ValMgr.getValue(getValue() % V.getValue());
328 }
329
330 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
331 assert (U->getType() == U->getSubExpr()->getType());
332 assert (U->getType()->isIntegerType());
333 return ValMgr.getValue(-getValue());
334 }
335
336 // Casting.
337
338 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
339 assert (CastExpr->getType()->isIntegerType());
340
341 llvm::APSInt X(getValue());
342 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
343 CastExpr->getLocStart()));
344 return ValMgr.getValue(X);
345 }
346
347 // Equality operators.
348
349 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
350 const llvm::APSInt& Val = getValue();
351 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
352 Val.getBitWidth(), Val.isUnsigned());
353 }
354
355 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
356 const llvm::APSInt& Val = getValue();
357 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
358 Val.getBitWidth(), Val.isUnsigned());
359 }
360
361 // Implement isa<T> support.
362 static inline bool classof(const RValue* V) {
363 return V->getSubKind() == ConcreteIntKind;
364 }
365};
366
367} // end clang namespace
368
369//==------------------------------------------------------------------------==//
370// Casting machinery to get cast<> and dyn_cast<> working with SymbolData.
371//==------------------------------------------------------------------------==//
372
373namespace llvm {
374
375template<> inline bool
376isa<clang::ParmVarDecl,clang::SymbolData>(const clang::SymbolData& V) {
377 return V.getKind() == clang::SymbolData::ParmKind;
378}
379
380template<> struct cast_retty_impl<clang::ParmVarDecl,clang::SymbolData> {
381 typedef const clang::ParmVarDecl* ret_type;
382};
383
384template<> struct simplify_type<clang::SymbolData> {
385 typedef void* SimpleType;
386 static inline SimpleType getSimplifiedValue(const clang::SymbolData &V) {
387 return V.getPtr();
388 }
389};
390
391} // end llvm namespace
392
393#endif