Environment.h revision 241677a13cc46647a8f5098b3e3239bd9480dca2
1//== Environment.h - Map from Stmt* to Locations/Values ---------*- 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 file defined the Environment and EnvironmentManager classes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
15#define LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
16
17// For using typedefs in StoreManager. Should find a better place for these
18// typedefs.
19#include "clang/Analysis/PathSensitive/Store.h"
20
21#include "llvm/ADT/ImmutableMap.h"
22#include "llvm/ADT/SmallVector.h"
23#include "clang/Analysis/PathSensitive/SVals.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/ADT/FoldingSet.h"
26
27namespace clang {
28
29class EnvironmentManager;
30class BasicValueFactory;
31class LiveVariables;
32
33class Environment : public llvm::FoldingSetNode {
34private:
35
36  friend class EnvironmentManager;
37
38  // Type definitions.
39  typedef llvm::ImmutableMap<Stmt*,SVal> BindingsTy;
40
41  // Data.
42  BindingsTy SubExprBindings;
43  BindingsTy BlkExprBindings;
44
45  Environment(BindingsTy seb, BindingsTy beb)
46    : SubExprBindings(seb), BlkExprBindings(beb) {}
47
48public:
49
50  typedef BindingsTy::iterator seb_iterator;
51  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
52  seb_iterator seb_end() const { return SubExprBindings.end(); }
53
54  typedef BindingsTy::iterator beb_iterator;
55  beb_iterator beb_begin() const { return BlkExprBindings.begin(); }
56  beb_iterator beb_end() const { return BlkExprBindings.end(); }
57
58  SVal LookupSubExpr(Stmt* E) const {
59    const SVal* X = SubExprBindings.lookup(cast<Expr>(E));
60    return X ? *X : UnknownVal();
61  }
62
63  SVal LookupBlkExpr(Stmt* E) const {
64    const SVal* X = BlkExprBindings.lookup(E);
65    return X ? *X : UnknownVal();
66  }
67
68  SVal LookupExpr(Stmt* E) const {
69    const SVal* X = SubExprBindings.lookup(E);
70    if (X) return *X;
71    X = BlkExprBindings.lookup(E);
72    return X ? *X : UnknownVal();
73  }
74
75  SVal GetSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
76  SVal GetBlkExprSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
77
78  /// Profile - Profile the contents of an Environment object for use
79  ///  in a FoldingSet.
80  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
81    E->SubExprBindings.Profile(ID);
82    E->BlkExprBindings.Profile(ID);
83  }
84
85  /// Profile - Used to profile the contents of this object for inclusion
86  ///  in a FoldingSet.
87  void Profile(llvm::FoldingSetNodeID& ID) const {
88    Profile(ID, this);
89  }
90
91  bool operator==(const Environment& RHS) const {
92    return SubExprBindings == RHS.SubExprBindings &&
93           BlkExprBindings == RHS.BlkExprBindings;
94  }
95};
96
97class EnvironmentManager {
98private:
99  typedef Environment::BindingsTy::Factory FactoryTy;
100  FactoryTy F;
101
102public:
103
104  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
105  ~EnvironmentManager() {}
106
107  /// RemoveBlkExpr - Return a new environment object with the same bindings as
108  ///  the provided environment except with any bindings for the provided Stmt*
109  ///  removed.  This method only removes bindings for block-level expressions.
110  ///  Using this method on a non-block level expression will return the
111  ///  same environment object.
112  Environment RemoveBlkExpr(const Environment& Env, Stmt* E) {
113    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
114  }
115
116  Environment RemoveSubExpr(const Environment& Env, Stmt* E) {
117    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
118  }
119
120  Environment AddBlkExpr(const Environment& Env, Stmt* E, SVal V) {
121    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));
122  }
123
124  Environment AddSubExpr(const Environment& Env, Stmt* E, SVal V) {
125    return Environment(F.Add(Env.SubExprBindings, E, V), Env.BlkExprBindings);
126  }
127
128  /// RemoveSubExprBindings - Return a new environment object with
129  ///  the same bindings as the provided environment except with all the
130  ///  subexpression bindings removed.
131  Environment RemoveSubExprBindings(const Environment& Env) {
132    return Environment(F.GetEmptyMap(), Env.BlkExprBindings);
133  }
134
135  Environment getInitialEnvironment() {
136    return Environment(F.GetEmptyMap(), F.GetEmptyMap());
137  }
138
139  Environment BindExpr(const Environment& Env, Stmt* E, SVal V,
140                       bool isBlkExpr, bool Invalidate);
141
142  Environment
143  RemoveDeadBindings(Environment Env, Stmt* Loc, SymbolReaper& SymReaper,
144                     llvm::SmallVectorImpl<const MemRegion*>& DRoots);
145
146};
147
148} // end clang namespace
149
150#endif
151