CBMC
rename_symbol.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "rename_symbol.h"
10 
11 #include "expr_iterator.h"
12 #include "std_expr.h"
13 
15 {
16 }
17 
19 {
20 }
21 
23  const symbol_exprt &old_expr,
24  const symbol_exprt &new_expr)
25 {
26  insert_expr(old_expr.get_identifier(), new_expr.get_identifier());
27 }
28 
29 bool rename_symbolt::rename(exprt &dest) const
30 {
31  bool result=true;
32 
33  for(auto it = dest.depth_begin(), end = dest.depth_end(); it != end; ++it)
34  {
35  exprt * modifiable_expr = nullptr;
36 
37  // first look at type
38  if(have_to_rename(it->type()))
39  {
40  modifiable_expr = &it.mutate();
41  result &= rename(modifiable_expr->type());
42  }
43 
44  // now do expression itself
45  if(it->id()==ID_symbol)
46  {
47  expr_mapt::const_iterator entry =
49 
50  if(entry != expr_map.end())
51  {
52  if(!modifiable_expr)
53  modifiable_expr = &it.mutate();
54  to_symbol_expr(*modifiable_expr).set_identifier(entry->second);
55  result = false;
56  }
57  }
58 
59  const typet &c_sizeof_type =
60  static_cast<const typet&>(it->find(ID_C_c_sizeof_type));
61  if(c_sizeof_type.is_not_nil() && have_to_rename(c_sizeof_type))
62  {
63  if(!modifiable_expr)
64  modifiable_expr = &it.mutate();
65  result &=
66  rename(static_cast<typet&>(modifiable_expr->add(ID_C_c_sizeof_type)));
67  }
68 
69  const typet &va_arg_type =
70  static_cast<const typet&>(it->find(ID_C_va_arg_type));
71  if(va_arg_type.is_not_nil() && have_to_rename(va_arg_type))
72  {
73  if(!modifiable_expr)
74  modifiable_expr = &it.mutate();
75  result &=
76  rename(static_cast<typet&>(modifiable_expr->add(ID_C_va_arg_type)));
77  }
78  }
79 
80  return result;
81 }
82 
83 bool rename_symbolt::have_to_rename(const exprt &dest) const
84 {
85  if(expr_map.empty() && type_map.empty())
86  return false;
87 
88  // first look at type
89 
90  if(have_to_rename(dest.type()))
91  return true;
92 
93  // now do expression itself
94 
95  if(dest.id()==ID_symbol)
96  {
97  const irep_idt &identifier = to_symbol_expr(dest).get_identifier();
98  return expr_map.find(identifier) != expr_map.end();
99  }
100 
101  forall_operands(it, dest)
102  if(have_to_rename(*it))
103  return true;
104 
105  const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
106 
107  if(c_sizeof_type.is_not_nil())
108  if(have_to_rename(static_cast<const typet &>(c_sizeof_type)))
109  return true;
110 
111  const irept &va_arg_type=dest.find(ID_C_va_arg_type);
112 
113  if(va_arg_type.is_not_nil())
114  if(have_to_rename(static_cast<const typet &>(va_arg_type)))
115  return true;
116 
117  return false;
118 }
119 
120 bool rename_symbolt::rename(typet &dest) const
121 {
122  if(!have_to_rename(dest))
123  return true;
124 
125  bool result=true;
126 
127  if(dest.has_subtype())
128  if(!rename(to_type_with_subtype(dest).subtype()))
129  result=false;
130 
131  for(typet &subtype : to_type_with_subtypes(dest).subtypes())
132  {
133  if(!rename(subtype))
134  result=false;
135  }
136 
137  if(dest.id()==ID_struct ||
138  dest.id()==ID_union)
139  {
140  struct_union_typet &struct_union_type=to_struct_union_type(dest);
141 
142  for(auto &c : struct_union_type.components())
143  if(!rename(c))
144  result=false;
145  }
146  else if(dest.id()==ID_code)
147  {
148  code_typet &code_type=to_code_type(dest);
149  if(!rename(code_type.return_type()))
150  result = false;
151 
152  for(auto &p : code_type.parameters())
153  {
154  if(!rename(p.type()))
155  result=false;
156 
157  expr_mapt::const_iterator e_it = expr_map.find(p.get_identifier());
158 
159  if(e_it!=expr_map.end())
160  {
161  p.set_identifier(e_it->second);
162  result=false;
163  }
164  }
165 
166  const exprt &spec_assigns =
167  static_cast<const exprt &>(dest.find(ID_C_spec_assigns));
168  if(spec_assigns.is_not_nil() && have_to_rename(spec_assigns))
169  {
170  rename(static_cast<exprt &>(dest.add(ID_C_spec_assigns)));
171  result = false;
172  }
173 
174  const exprt &spec_ensures =
175  static_cast<const exprt &>(dest.find(ID_C_spec_ensures));
176  if(spec_ensures.is_not_nil() && have_to_rename(spec_ensures))
177  {
178  rename(static_cast<exprt &>(dest.add(ID_C_spec_ensures)));
179  result = false;
180  }
181 
182  const exprt &spec_ensures_contract =
183  static_cast<const exprt &>(dest.find(ID_C_spec_ensures_contract));
184  if(
185  spec_ensures_contract.is_not_nil() &&
186  have_to_rename(spec_ensures_contract))
187  {
188  rename(static_cast<exprt &>(dest.add(ID_C_spec_ensures_contract)));
189  result = false;
190  }
191 
192  const exprt &spec_requires =
193  static_cast<const exprt &>(dest.find(ID_C_spec_requires));
194  if(spec_requires.is_not_nil() && have_to_rename(spec_requires))
195  {
196  rename(static_cast<exprt &>(dest.add(ID_C_spec_requires)));
197  result = false;
198  }
199 
200  const exprt &spec_requires_contract =
201  static_cast<const exprt &>(dest.find(ID_C_spec_requires_contract));
202  if(
203  spec_requires_contract.is_not_nil() &&
204  have_to_rename(spec_requires_contract))
205  {
206  rename(static_cast<exprt &>(dest.add(ID_C_spec_requires_contract)));
207  result = false;
208  }
209  }
210  else if(dest.id()==ID_c_enum_tag ||
211  dest.id()==ID_struct_tag ||
212  dest.id()==ID_union_tag)
213  {
214  type_mapt::const_iterator it=
215  type_map.find(to_tag_type(dest).get_identifier());
216 
217  if(it!=type_map.end())
218  {
219  to_tag_type(dest).set_identifier(it->second);
220  result=false;
221  }
222  }
223  else if(dest.id()==ID_array)
224  {
225  array_typet &array_type=to_array_type(dest);
226  if(!rename(array_type.size()))
227  result=false;
228  }
229 
230  return result;
231 }
232 
233 bool rename_symbolt::have_to_rename(const typet &dest) const
234 {
235  if(expr_map.empty() && type_map.empty())
236  return false;
237 
238  if(dest.has_subtype())
239  if(have_to_rename(to_type_with_subtype(dest).subtype()))
240  return true;
241 
242  for(const typet &subtype : to_type_with_subtypes(dest).subtypes())
243  {
244  if(have_to_rename(subtype))
245  return true;
246  }
247 
248  if(dest.id()==ID_struct ||
249  dest.id()==ID_union)
250  {
251  const struct_union_typet &struct_union_type=
252  to_struct_union_type(dest);
253 
254  for(const auto &c : struct_union_type.components())
255  if(have_to_rename(c))
256  return true;
257  }
258  else if(dest.id()==ID_code)
259  {
260  const code_typet &code_type=to_code_type(dest);
261  if(have_to_rename(code_type.return_type()))
262  return true;
263 
264  for(const auto &p : code_type.parameters())
265  {
266  if(have_to_rename(p.type()))
267  return true;
268 
269  if(expr_map.find(p.get_identifier()) != expr_map.end())
270  return true;
271  }
272 
273  const exprt &spec_assigns =
274  static_cast<const exprt &>(dest.find(ID_C_spec_assigns));
275  if(spec_assigns.is_not_nil() && have_to_rename(spec_assigns))
276  return true;
277 
278  const exprt &spec_ensures =
279  static_cast<const exprt &>(dest.find(ID_C_spec_ensures));
280  if(spec_ensures.is_not_nil() && have_to_rename(spec_ensures))
281  return true;
282 
283  const exprt &spec_ensures_contract =
284  static_cast<const exprt &>(dest.find(ID_C_spec_ensures_contract));
285  if(
286  spec_ensures_contract.is_not_nil() &&
287  have_to_rename(spec_ensures_contract))
288  {
289  return true;
290  }
291 
292  const exprt &spec_requires =
293  static_cast<const exprt &>(dest.find(ID_C_spec_requires));
294  if(spec_requires.is_not_nil() && have_to_rename(spec_requires))
295  return true;
296 
297  const exprt &spec_requires_contract =
298  static_cast<const exprt &>(dest.find(ID_C_spec_requires_contract));
299  if(
300  spec_requires_contract.is_not_nil() &&
301  have_to_rename(spec_requires_contract))
302  {
303  return true;
304  }
305  }
306  else if(dest.id()==ID_c_enum_tag ||
307  dest.id()==ID_struct_tag ||
308  dest.id()==ID_union_tag)
309  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
310  else if(dest.id()==ID_array)
311  return have_to_rename(to_array_type(dest).size());
312 
313  return false;
314 }
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:410
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
tag_typet::set_identifier
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:405
exprt::depth_end
depth_iteratort depth_end()
Definition: expr.cpp:267
to_struct_union_type
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
rename_symbolt::have_to_rename
bool have_to_rename(const exprt &dest) const
Definition: rename_symbol.cpp:83
typet
The type of an expression, extends irept.
Definition: type.h:28
irept::find
const irept & find(const irep_idt &name) const
Definition: irep.cpp:106
typet::has_subtype
bool has_subtype() const
Definition: type.h:82
struct_union_typet
Base type for structs and unions.
Definition: std_types.h:61
rename_symbolt::expr_map
expr_mapt expr_map
Definition: rename_symbol.h:63
to_type_with_subtype
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition: type.h:193
to_type_with_subtypes
const type_with_subtypest & to_type_with_subtypes(const typet &type)
Definition: type.h:237
exprt
Base class for all expressions.
Definition: expr.h:55
rename_symbolt::rename_symbolt
rename_symbolt()
Definition: rename_symbol.cpp:14
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
rename_symbol.h
array_typet::size
const exprt & size() const
Definition: std_types.h:800
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:380
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
to_tag_type
const tag_typet & to_tag_type(const typet &type)
Cast a typet to a tag_typet.
Definition: std_types.h:434
get_identifier
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
Definition: smt2_incremental_decision_procedure.cpp:328
rename_symbolt::insert
void insert(const class symbol_exprt &old_expr, const class symbol_exprt &new_expr)
Definition: rename_symbol.cpp:22
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:20
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:142
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
code_typet
Base type of functions.
Definition: std_types.h:538
irept::id
const irep_idt & id() const
Definition: irep.h:396
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:655
rename_symbolt::~rename_symbolt
virtual ~rename_symbolt()
Definition: rename_symbol.cpp:18
irept::add
irept & add(const irep_idt &name)
Definition: irep.cpp:116
array_typet
Arrays with given size.
Definition: std_types.h:762
expr_iterator.h
exprt::depth_begin
depth_iteratort depth_begin()
Definition: expr.cpp:265
rename_symbolt::insert_expr
void insert_expr(const irep_idt &old_id, const irep_idt &new_id)
Definition: rename_symbol.h:31
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:844
rename_symbolt::type_map
type_mapt type_map
Definition: rename_symbol.h:64
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:645
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:359
rename_symbolt::rename
bool rename(exprt &dest) const
Definition: rename_symbol.cpp:29
std_expr.h
symbol_exprt::set_identifier
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:137