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