cprover
symbol_table.cpp
Go to the documentation of this file.
1 
3 #include "symbol_table.h"
4 
5 #include <util/invariant.h>
6 
15 std::pair<symbolt &, bool> symbol_tablet::insert(symbolt symbol)
16 {
17  // Add the symbol to the table or retrieve existing symbol with the same name
18  std::pair<symbolst::iterator, bool> result=
19  internal_symbols.emplace(symbol.name, std::move(symbol));
20  symbolt &new_symbol=result.first->second;
21  if(result.second)
22  {
23  try
24  {
25  symbol_base_mapt::iterator base_result=
26  internal_symbol_base_map.emplace(new_symbol.base_name, new_symbol.name);
27  try
28  {
29  internal_symbol_module_map.emplace(new_symbol.module, new_symbol.name);
30  }
31  catch(...)
32  {
33  internal_symbol_base_map.erase(base_result);
34  throw;
35  }
36  }
37  catch(...)
38  {
39  internal_symbols.erase(result.first);
40  throw;
41  }
42  }
43  return std::make_pair(std::ref(new_symbol), result.second);
44 }
45 
61 bool symbol_tablet::move(symbolt &symbol, symbolt *&new_symbol)
62 {
63  // Add an empty symbol to the table or retrieve existing symbol with same name
64  symbolt temp_symbol;
65  // This is not copying the symbol, this is passing the three required
66  // parameters to insert (just in the symbol)
67  temp_symbol.name=symbol.name;
68  temp_symbol.base_name=symbol.base_name;
69  temp_symbol.module=symbol.module;
70  std::pair<symbolt &, bool> result=insert(std::move(temp_symbol));
71  if(result.second)
72  {
73  // Move the provided symbol into the symbol table, this can't be done
74  // earlier
75  result.first.swap(symbol);
76  }
77  // Return the address of the symbol in the table
78  new_symbol=&result.first;
79  return !result.second;
80 }
81 
84 void symbol_tablet::erase(const symbolst::const_iterator &entry)
85 {
86  const symbolt &symbol=entry->second;
87 
88  symbol_base_mapt::const_iterator
89  base_it=symbol_base_map.lower_bound(entry->second.base_name);
90  symbol_base_mapt::const_iterator
91  base_it_end=symbol_base_map.upper_bound(entry->second.base_name);
92  while(base_it!=base_it_end && base_it->second!=symbol.name)
93  ++base_it;
94  INVARIANT(
95  base_it!=base_it_end,
96  "symbolt::base_name should not be changed "
97  "after it is added to the symbol_table "
98  "(name: "+id2string(symbol.name)+", "
99  "current base_name: "+id2string(symbol.base_name)+")");
100  internal_symbol_base_map.erase(base_it);
101 
102  symbol_module_mapt::const_iterator
103  module_it=symbol_module_map.lower_bound(entry->second.module),
104  module_it_end=symbol_module_map.upper_bound(entry->second.module);
105  while(module_it!=module_it_end && module_it->second!=symbol.name)
106  ++module_it;
107  INVARIANT(
108  module_it!=module_it_end,
109  "symbolt::module should not be changed "
110  "after it is added to the symbol_table "
111  "(name: "+id2string(symbol.name)+", "
112  "current module: "+id2string(symbol.module)+")");
113  internal_symbol_module_map.erase(module_it);
114 
115  internal_symbols.erase(entry);
116 }
symbolst internal_symbols
Definition: symbol_table.h:22
irep_idt name
The unique identifier.
Definition: symbol.h:43
const std::string & id2string(const irep_idt &d)
Definition: irep.h:43
const symbol_base_mapt & symbol_base_map
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:46
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
#define INVARIANT(CONDITION, REASON)
Definition: invariant.h:205
symbol_module_mapt internal_symbol_module_map
Definition: symbol_table.h:24
virtual bool move(symbolt &symbol, symbolt *&new_symbol) override
Move a symbol into the symbol table.
const symbol_module_mapt & symbol_module_map
Author: Diffblue Ltd.
virtual void erase(const symbolst::const_iterator &entry) override
Remove a symbol from the symbol table.
symbol_base_mapt internal_symbol_base_map
Definition: symbol_table.h:23
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:49
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.