cprover
symbol_table_base.h
Go to the documentation of this file.
1 
5 
6 #ifndef CPROVER_UTIL_SYMBOL_TABLE_BASE_H
7 #define CPROVER_UTIL_SYMBOL_TABLE_BASE_H
8 
9 #include <map>
10 #include <unordered_map>
11 
12 #include "symbol.h"
13 
14 typedef std::multimap<irep_idt, irep_idt> symbol_base_mapt;
15 typedef std::multimap<irep_idt, irep_idt> symbol_module_mapt;
16 
17 class symbol_tablet;
18 
22 {
23 public:
24  typedef std::unordered_map<irep_idt, symbolt> symbolst;
25 
26 public:
27  const symbolst &symbols;
30 
31 public:
33  const symbolst &symbols,
36  : symbols(symbols),
39  {
40  }
41 
42  symbol_table_baset(const symbol_table_baset &other) = delete;
43  symbol_table_baset &operator=(const symbol_table_baset &other) = delete;
44 
45  virtual ~symbol_table_baset();
46 
53  std::size_t
54  next_unused_suffix(const std::string &prefix, std::size_t start_number) const
55  {
56  while(this->symbols.find(prefix + std::to_string(start_number)) !=
57  symbols.end())
58  ++start_number;
59 
60  return start_number;
61  }
62 
63  virtual std::size_t next_unused_suffix(const std::string &prefix) const
64  {
65  return next_unused_suffix(prefix, 0);
66  }
67 
69  operator const symbol_tablet &() const
70  {
71  return get_symbol_table();
72  }
73  virtual const symbol_tablet &get_symbol_table() const = 0;
74 
78  bool has_symbol(const irep_idt &name) const
79  {
80  return symbols.find(name) != symbols.end();
81  }
82 
86  const symbolt *lookup(const irep_idt &name) const
87  {
88  symbolst::const_iterator it = symbols.find(name);
89  return it != symbols.end() ? &it->second : nullptr;
90  }
91 
96  const symbolt &lookup_ref(const irep_idt &name) const
97  {
98  return symbols.at(name);
99  }
100 
104  virtual symbolt *get_writeable(const irep_idt &name) = 0;
105 
111  {
112  symbolt *symbol = get_writeable(name);
113  if(symbol == nullptr)
114  throw std::out_of_range("name not found in symbol_table");
115  return *symbol;
116  }
117 
118  bool add(const symbolt &symbol);
128  virtual std::pair<symbolt &, bool> insert(symbolt symbol) = 0;
129  virtual bool move(symbolt &symbol, symbolt *&new_symbol) = 0;
130 
131  bool remove(const irep_idt &name);
134  virtual void erase(const symbolst::const_iterator &entry) = 0;
135  virtual void clear() = 0;
136 
137  void show(std::ostream &out) const;
138 
139  class iteratort
140  {
141  private:
142  symbolst::iterator it;
143  std::function<void(const irep_idt &id)> on_get_writeable;
144 
145  public:
146  explicit iteratort(symbolst::iterator it) : it(std::move(it))
147  {
148  }
149 
151  const iteratort &it,
152  std::function<void(const irep_idt &id)> on_get_writeable)
154  {
155  }
156 
157  // The following typedefs are NOLINT as they are needed by the STL
158  typedef symbolst::iterator::difference_type difference_type; // NOLINT
159  typedef symbolst::const_iterator::value_type value_type; // NOLINT
160  typedef symbolst::const_iterator::pointer pointer; // NOLINT
161  typedef symbolst::const_iterator::reference reference; // NOLINT
162  typedef symbolst::iterator::iterator_category iterator_category; // NOLINT
163 
164  bool operator!=(const iteratort &other) const
165  {
166  return it != other.it;
167  }
168 
169  bool operator==(const iteratort &other) const
170  {
171  return it == other.it;
172  }
173 
177  {
178  ++it;
179  return *this;
180  }
181 
185  {
186  iteratort copy(*this);
187  this->operator++();
188  return copy;
189  }
190 
194  {
195  return *it;
196  }
197 
201  {
202  return &**this;
203  }
204 
213  {
214  if(on_get_writeable)
215  on_get_writeable((*this)->first);
216  return it->second;
217  }
218  };
219 
220  virtual iteratort begin() = 0;
221  virtual iteratort end() = 0;
222 };
223 
224 std::ostream &
225 operator<<(std::ostream &out, const symbol_table_baset &symbol_table);
226 
227 #endif // CPROVER_UTIL_SYMBOL_TABLE_BASE_H
virtual void erase(const symbolst::const_iterator &entry)=0
Remove a symbol from the symbol table.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Symbol table entry.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
void show(std::ostream &out) const
Print the contents of the symbol table.
std::ostream & operator<<(std::ostream &out, const symbol_table_baset &symbol_table)
Print the contents of the symbol table.
symbolst::iterator::difference_type difference_type
const symbol_base_mapt & symbol_base_map
std::multimap< irep_idt, irep_idt > symbol_base_mapt
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
virtual symbolt * get_writeable(const irep_idt &name)=0
Find a symbol in the symbol table for read-write access.
bool operator==(const iteratort &other) const
symbolst::const_iterator::pointer pointer
Symbol table entry.
Definition: symbol.h:27
symbolst::iterator::iterator_category iterator_category
virtual bool move(symbolt &symbol, symbolt *&new_symbol)=0
std::multimap< irep_idt, irep_idt > symbol_module_mapt
iteratort operator++(int)
Post-increment operator.
reference operator *() const
Dereference operator.
symbol_table_baset(const symbolst &symbols, const symbol_base_mapt &symbol_base_map, const symbol_module_mapt &symbol_module_map)
std::unordered_map< irep_idt, symbolt > symbolst
The symbol table.
Definition: symbol_table.h:19
const symbol_module_mapt & symbol_module_map
symbolt & get_writeable_symbol()
Whereas the dereference operator gives a constant reference to the current symbol,...
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
virtual iteratort begin()=0
const symbolst & symbols
virtual iteratort end()=0
symbolst::const_iterator::value_type value_type
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
symbolst::const_iterator::reference reference
iteratort(const iteratort &it, std::function< void(const irep_idt &id)> on_get_writeable)
virtual std::size_t next_unused_suffix(const std::string &prefix) const
symbol_table_baset & operator=(const symbol_table_baset &other)=delete
iteratort & operator++()
Preincrement operator Do not call on the end() iterator.
bool remove(const irep_idt &name)
Remove a symbol from the symbol table.
virtual ~symbol_table_baset()
Author: Diffblue Ltd.
The symbol table base class interface.
std::function< void(const irep_idt &id)> on_get_writeable
std::size_t next_unused_suffix(const std::string &prefix, std::size_t start_number) const
Find smallest unused integer i so that prefix + std::to_string(i) does not exist in the list symbols.
pointer operator->() const
Dereference operator (member access)
virtual std::pair< symbolt &, bool > insert(symbolt symbol)=0
Move or copy a new symbol to the symbol table.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
virtual const symbol_tablet & get_symbol_table() const =0
bool operator!=(const iteratort &other) const
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
virtual void clear()=0
iteratort(symbolst::iterator it)