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