blob: b0f0496e0436b0f96f3dd8d55d0ad24df3c24a24 [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//== ValueState.h - Path-Sens. "State" for tracking valuues -----*- 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 Kremenek9153f732008-02-05 07:17:49 +000010// This files defines SymbolID, VarBindKey, and ValueState.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_VALUESTATE_H
15#define LLVM_CLANG_ANALYSIS_VALUESTATE_H
16
17// FIXME: Reduce the number of includes.
18
19#include "RValues.h"
20
21#include "clang/Analysis/PathSensitive/GREngine.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/ASTContext.h"
25#include "clang/Analysis/Analyses/LiveVariables.h"
26
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/DataTypes.h"
29#include "llvm/ADT/APSInt.h"
30#include "llvm/ADT/FoldingSet.h"
31#include "llvm/ADT/ImmutableMap.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/SmallPtrSet.h"
34#include "llvm/Support/Allocator.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Streams.h"
37
38#include <functional>
39
40namespace clang {
41
Ted Kremenek9153f732008-02-05 07:17:49 +000042/// VarBindKey - A variant smart pointer that wraps either a ValueDecl* or a
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000043/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
Ted Kremenek9153f732008-02-05 07:17:49 +000044class VarBindKey {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000045 uintptr_t Raw;
Ted Kremenek9153f732008-02-05 07:17:49 +000046 void operator=(const VarBindKey& RHS); // Do not implement.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000047
48public:
49 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
50 IsSymbol=0x3, // Symbol Bindings.
51 Mask=0x3 };
52
53 inline Kind getKind() const {
54 return (Kind) (Raw & Mask);
55 }
56
57 inline void* getPtr() const {
58 assert (getKind() != IsSymbol);
59 return reinterpret_cast<void*>(Raw & ~Mask);
60 }
61
62 inline SymbolID getSymbolID() const {
63 assert (getKind() == IsSymbol);
64 return Raw >> 2;
65 }
66
Ted Kremenek9153f732008-02-05 07:17:49 +000067 VarBindKey(const ValueDecl* VD)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000068 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
69 assert(VD && "ValueDecl cannot be NULL.");
70 }
71
Ted Kremenek9153f732008-02-05 07:17:49 +000072 VarBindKey(Stmt* S, bool isBlkExpr = false)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000073 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
74 assert(S && "Tracked statement cannot be NULL.");
75 }
76
Ted Kremenek9153f732008-02-05 07:17:49 +000077 VarBindKey(SymbolID V)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000078 : Raw((V << 2) | IsSymbol) {}
79
80 bool isSymbol() const { return getKind() == IsSymbol; }
81 bool isSubExpr() const { return getKind() == IsSubExpr; }
82 bool isBlkExpr() const { return getKind() == IsBlkExpr; }
83 bool isDecl() const { return getKind() == IsDecl; }
84 bool isStmt() const { return getKind() <= IsBlkExpr; }
85
86 inline void Profile(llvm::FoldingSetNodeID& ID) const {
87 ID.AddInteger(isSymbol() ? 1 : 0);
88
89 if (isSymbol())
90 ID.AddInteger(getSymbolID());
91 else
92 ID.AddPointer(getPtr());
93 }
94
Ted Kremenek9153f732008-02-05 07:17:49 +000095 inline bool operator==(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000096 return isSymbol() ? getSymbolID() == X.getSymbolID()
97 : getPtr() == X.getPtr();
98 }
99
Ted Kremenek9153f732008-02-05 07:17:49 +0000100 inline bool operator!=(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000101 return !operator==(X);
102 }
103
Ted Kremenek9153f732008-02-05 07:17:49 +0000104 inline bool operator<(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000105 if (isSymbol())
106 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
107
108 return getPtr() < X.getPtr();
109 }
110};
111
112//===----------------------------------------------------------------------===//
113// ValueState - An ImmutableMap type Stmt*/Decl*/Symbols to RValues.
114//===----------------------------------------------------------------------===//
115
Ted Kremenek9153f732008-02-05 07:17:49 +0000116namespace vstate {
117 typedef llvm::ImmutableMap<VarBindKey,RValue> VariableBindingsTy;
118}
119
120struct ValueStateImpl : public llvm::FoldingSetNode {
121 vstate::VariableBindingsTy VariableBindings;
122
123 ValueStateImpl(vstate::VariableBindingsTy VB)
124 : VariableBindings(VB) {}
125
126 ValueStateImpl(const ValueStateImpl& RHS)
127 : llvm::FoldingSetNode(), VariableBindings(RHS.VariableBindings) {}
128
129
130 static void Profile(llvm::FoldingSetNodeID& ID, const ValueStateImpl& V) {
131 V.VariableBindings.Profile(ID);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000132 }
Ted Kremenek9153f732008-02-05 07:17:49 +0000133
134 void Profile(llvm::FoldingSetNodeID& ID) const {
135 Profile(ID, *this);
136 }
137
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000138};
139
Ted Kremenek9153f732008-02-05 07:17:49 +0000140class ValueState : public llvm::FoldingSetNode {
141 ValueStateImpl* Data;
142public:
143 typedef vstate::VariableBindingsTy VariableBindingsTy;
144 typedef VariableBindingsTy::iterator iterator;
145
146
147
148 iterator begin() { return Data->VariableBindings.begin(); }
149 iterator end() { return Data->VariableBindings.end(); }
150
151 bool operator==(const ValueState& RHS) const {
152 return Data == RHS.Data;
153 }
154
155 static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) {
156 ID.AddPointer(V.getImpl());
157 }
158
159 void Profile(llvm::FoldingSetNodeID& ID) const {
160 Profile(ID, *this);
161 }
162
163 ValueState(ValueStateImpl* D) : Data(D) {}
164 ValueState() : Data(0) {}
165
166 void operator=(ValueStateImpl* D) {
167 Data = D;
168 }
169
170 ValueStateImpl* getImpl() const { return Data; }
171};
172
173template<> struct GRTrait<ValueState> {
174 static inline void* toPtr(ValueState St) {
175 return reinterpret_cast<void*>(St.getImpl());
176 }
177 static inline ValueState toState(void* P) {
178 return ValueState(static_cast<ValueStateImpl*>(P));
179 }
180};
181
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000182
183class ValueStateManager {
184public:
185 typedef ValueState StateTy;
186
187private:
Ted Kremenek9153f732008-02-05 07:17:49 +0000188 ValueState::VariableBindingsTy::Factory VBFactory;
189 llvm::FoldingSet<ValueStateImpl> StateSet;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000190
191 /// ValueMgr - Object that manages the data for all created RValues.
192 ValueManager ValMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000193
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194 /// SymMgr - Object that manages the symbol information.
195 SymbolManager SymMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000196
197 /// Alloc - A BumpPtrAllocator to allocate states.
198 llvm::BumpPtrAllocator& Alloc;
199
200 StateTy getPersistentState(const ValueState& St);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000201
202public:
Ted Kremenek9153f732008-02-05 07:17:49 +0000203 ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc)
204 : ValMgr(Ctx, alloc), Alloc(alloc) {}
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000205
Ted Kremenek9153f732008-02-05 07:17:49 +0000206 StateTy getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000207
208 ValueManager& getValueManager() { return ValMgr; }
209 SymbolManager& getSymbolManager() { return SymMgr; }
210
211 StateTy SetValue(StateTy St, Stmt* S, bool isBlkExpr, const RValue& V);
212 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
213
Ted Kremenekf233d482008-02-05 00:26:40 +0000214 RValue GetValue(const StateTy& St, Stmt* S, bool* hasVal = NULL);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000215 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekf233d482008-02-05 00:26:40 +0000216
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000217 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenek9153f732008-02-05 07:17:49 +0000218
219 StateTy Add(StateTy St, VarBindKey K, const RValue& V);
220 StateTy Remove(StateTy St, VarBindKey K);
221 StateTy getPersistentState(const ValueStateImpl& Impl);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000222};
223
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000224} // end clang namespace
225
226//==------------------------------------------------------------------------==//
Ted Kremenek9153f732008-02-05 07:17:49 +0000227// Casting machinery to get cast<> and dyn_cast<> working with VarBindKey.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000228//==------------------------------------------------------------------------==//
229
230namespace llvm {
231
232 template<> inline bool
Ted Kremenek9153f732008-02-05 07:17:49 +0000233 isa<clang::ValueDecl,clang::VarBindKey>(const clang::VarBindKey& V) {
234 return V.getKind() == clang::VarBindKey::IsDecl;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000235 }
236
237 template<> inline bool
Ted Kremenek9153f732008-02-05 07:17:49 +0000238 isa<clang::Stmt,clang::VarBindKey>(const clang::VarBindKey& V) {
239 return ((unsigned) V.getKind()) < clang::VarBindKey::IsDecl;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000240 }
241
Ted Kremenek9153f732008-02-05 07:17:49 +0000242 template<> struct cast_retty_impl<clang::ValueDecl,clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000243 typedef const clang::ValueDecl* ret_type;
244 };
245
Ted Kremenek9153f732008-02-05 07:17:49 +0000246 template<> struct cast_retty_impl<clang::Stmt,clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000247 typedef const clang::Stmt* ret_type;
248 };
249
Ted Kremenek9153f732008-02-05 07:17:49 +0000250 template<> struct simplify_type<clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000251 typedef void* SimpleType;
Ted Kremenek9153f732008-02-05 07:17:49 +0000252 static inline SimpleType getSimplifiedValue(const clang::VarBindKey &V) {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000253 return V.getPtr();
254 }
255 };
256} // end llvm namespace
257
258#endif