cprover
namespace.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Namespace
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "namespace.h"
13 
14 #include <algorithm>
15 
16 #include "prefix.h"
17 #include "std_expr.h"
18 #include "std_types.h"
19 #include "string2int.h"
20 #include "symbol_table.h"
21 
23 {
24 }
25 
32 const symbolt &namespace_baset::lookup(const symbol_exprt &expr) const
33 {
34  return lookup(expr.get_identifier());
35 }
36 
43 const symbolt &namespace_baset::lookup(const tag_typet &type) const
44 {
45  return lookup(type.get_identifier());
46 }
47 
51 const typet &namespace_baset::follow(const typet &src) const
52 {
53  if(src.id() == ID_union_tag)
54  return follow_tag(to_union_tag_type(src));
55 
56  if(src.id() == ID_struct_tag)
57  return follow_tag(to_struct_tag_type(src));
58 
59  return src;
60 }
61 
66 {
67  const symbolt &symbol=lookup(src.get_identifier());
68  CHECK_RETURN(symbol.is_type);
69  CHECK_RETURN(symbol.type.id() == ID_union);
70  return to_union_type(symbol.type);
71 }
72 
76 const struct_typet &
78 {
79  const symbolt &symbol=lookup(src.get_identifier());
80  CHECK_RETURN(symbol.is_type);
81  CHECK_RETURN(symbol.type.id() == ID_struct);
82  return to_struct_type(symbol.type);
83 }
84 
88 const c_enum_typet &
90 {
91  const symbolt &symbol=lookup(src.get_identifier());
92  CHECK_RETURN(symbol.is_type);
93  CHECK_RETURN(symbol.type.id() == ID_c_enum);
94  return to_c_enum_type(symbol.type);
95 }
96 
100 {
101  if(expr.id()==ID_symbol)
102  {
103  const symbolt &symbol = lookup(to_symbol_expr(expr));
104 
105  if(symbol.is_macro && !symbol.value.is_nil())
106  {
107  expr=symbol.value;
108  follow_macros(expr);
109  }
110 
111  return;
112  }
113 
114  Forall_operands(it, expr)
115  follow_macros(*it);
116 }
117 
122 std::size_t namespacet::smallest_unused_suffix(const std::string &prefix) const
123 {
124  std::size_t m = 0;
125 
126  if(symbol_table1!=nullptr)
127  m = std::max(m, symbol_table1->next_unused_suffix(prefix));
128 
129  if(symbol_table2!=nullptr)
130  m = std::max(m, symbol_table2->next_unused_suffix(prefix));
131 
132  return m;
133 }
134 
141  const irep_idt &name,
142  const symbolt *&symbol) const
143 {
144  symbol_tablet::symbolst::const_iterator it;
145 
146  if(symbol_table1!=nullptr)
147  {
148  it=symbol_table1->symbols.find(name);
149 
150  if(it!=symbol_table1->symbols.end())
151  {
152  symbol=&(it->second);
153  return false;
154  }
155  }
156 
157  if(symbol_table2!=nullptr)
158  {
159  it=symbol_table2->symbols.find(name);
160 
161  if(it!=symbol_table2->symbols.end())
162  {
163  symbol=&(it->second);
164  return false;
165  }
166  }
167 
168  return true;
169 }
170 
174 std::size_t
175 multi_namespacet::smallest_unused_suffix(const std::string &prefix) const
176 {
177  std::size_t m = 0;
178 
179  for(const auto &st : symbol_table_list)
180  m = std::max(m, st->next_unused_suffix(prefix));
181 
182  return m;
183 }
184 
192  const irep_idt &name,
193  const symbolt *&symbol) const
194 {
195  symbol_tablet::symbolst::const_iterator s_it;
196 
197  for(symbol_table_listt::const_iterator
198  c_it=symbol_table_list.begin();
199  c_it!=symbol_table_list.end();
200  c_it++)
201  {
202  s_it=(*c_it)->symbols.find(name);
203 
204  if(s_it!=(*c_it)->symbols.end())
205  {
206  symbol=&(s_it->second);
207  return false;
208  }
209  }
210 
211  return true;
212 }
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:451
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
to_union_tag_type
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: std_types.h:555
namespacet::symbol_table2
const symbol_table_baset * symbol_table2
Definition: namespace.h:130
c_enum_typet
The type of C enums.
Definition: std_types.h:619
Forall_operands
#define Forall_operands(it, expr)
Definition: expr.h:24
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
to_c_enum_type
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition: std_types.h:681
typet
The type of an expression, extends irept.
Definition: type.h:28
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
prefix.h
exprt
Base class for all expressions.
Definition: expr.h:52
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:489
namespace_baset::follow_tag
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:65
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:81
namespace.h
union_tag_typet
A union tag type, i.e., union_typet with an identifier.
Definition: std_types.h:529
multi_namespacet::symbol_table_list
symbol_table_listt symbol_table_list
Definition: namespace.h:170
namespace_baset::follow_macros
void follow_macros(exprt &) const
Follow macros to their values in a given expression.
Definition: namespace.cpp:99
string2int.h
multi_namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:191
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
namespace_baset::lookup
const symbolt & lookup(const irep_idt &name) const
Lookup a symbol in the namespace.
Definition: namespace.h:44
namespacet::smallest_unused_suffix
std::size_t smallest_unused_suffix(const std::string &prefix) const override
See documentation for namespace_baset::smallest_unused_suffix().
Definition: namespace.cpp:122
namespace_baset::~namespace_baset
virtual ~namespace_baset()
Definition: namespace.cpp:22
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:111
std_types.h
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
namespacet::symbol_table1
const symbol_table_baset * symbol_table1
Definition: namespace.h:130
irept::is_nil
bool is_nil() const
Definition: irep.h:398
irept::id
const irep_idt & id() const
Definition: irep.h:418
to_struct_tag_type
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:515
union_typet
The union type.
Definition: std_types.h:392
tag_typet
A tag-based type, i.e., typet with an identifier.
Definition: std_types.h:436
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:225
c_enum_tag_typet
C enum tag type, i.e., c_enum_typet with an identifier.
Definition: std_types.h:695
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
symbolt
Symbol table entry.
Definition: symbol.h:27
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
symbolt::is_type
bool is_type
Definition: symbol.h:61
to_union_type
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: std_types.h:422
symbol_table.h
Author: Diffblue Ltd.
symbol_table_baset::next_unused_suffix
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.
Definition: symbol_table_base.h:63
std_expr.h
multi_namespacet::smallest_unused_suffix
std::size_t smallest_unused_suffix(const std::string &prefix) const override
See documentation for namespace_baset::smallest_unused_suffix().
Definition: namespace.cpp:175