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  for(const auto &op : dest.operands())
102  {
103  if(have_to_rename(op))
104  return true;
105  }
106 
107  const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
108 
109  if(c_sizeof_type.is_not_nil())
110  if(have_to_rename(static_cast<const typet &>(c_sizeof_type)))
111  return true;
112 
113  const irept &va_arg_type=dest.find(ID_C_va_arg_type);
114 
115  if(va_arg_type.is_not_nil())
116  if(have_to_rename(static_cast<const typet &>(va_arg_type)))
117  return true;
118 
119  return false;
120 }
121 
122 bool rename_symbolt::rename(typet &dest) const
123 {
124  if(!have_to_rename(dest))
125  return true;
126 
127  bool result=true;
128 
129  if(dest.has_subtype())
130  if(!rename(to_type_with_subtype(dest).subtype()))
131  result=false;
132 
133  for(typet &subtype : to_type_with_subtypes(dest).subtypes())
134  {
135  if(!rename(subtype))
136  result=false;
137  }
138 
139  if(dest.id()==ID_struct ||
140  dest.id()==ID_union)
141  {
142  struct_union_typet &struct_union_type=to_struct_union_type(dest);
143 
144  for(auto &c : struct_union_type.components())
145  if(!rename(c))
146  result=false;
147  }
148  else if(dest.id()==ID_code)
149  {
150  code_typet &code_type=to_code_type(dest);
151  if(!rename(code_type.return_type()))
152  result = false;
153 
154  for(auto &p : code_type.parameters())
155  {
156  if(!rename(p.type()))
157  result=false;
158 
159  expr_mapt::const_iterator e_it = expr_map.find(p.get_identifier());
160 
161  if(e_it!=expr_map.end())
162  {
163  p.set_identifier(e_it->second);
164  result=false;
165  }
166  }
167 
168  const exprt &spec_assigns =
169  static_cast<const exprt &>(dest.find(ID_C_spec_assigns));
170  if(spec_assigns.is_not_nil() && have_to_rename(spec_assigns))
171  {
172  rename(static_cast<exprt &>(dest.add(ID_C_spec_assigns)));
173  result = false;
174  }
175 
176  const exprt &spec_frees =
177  static_cast<const exprt &>(dest.find(ID_C_spec_frees));
178  if(spec_frees.is_not_nil() && have_to_rename(spec_frees))
179  {
180  rename(static_cast<exprt &>(dest.add(ID_C_spec_frees)));
181  result = false;
182  }
183 
184  const exprt &spec_ensures =
185  static_cast<const exprt &>(dest.find(ID_C_spec_ensures));
186  if(spec_ensures.is_not_nil() && have_to_rename(spec_ensures))
187  {
188  rename(static_cast<exprt &>(dest.add(ID_C_spec_ensures)));
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  else if(dest.id()==ID_c_enum_tag ||
201  dest.id()==ID_struct_tag ||
202  dest.id()==ID_union_tag)
203  {
204  type_mapt::const_iterator it=
205  type_map.find(to_tag_type(dest).get_identifier());
206 
207  if(it!=type_map.end())
208  {
209  to_tag_type(dest).set_identifier(it->second);
210  result=false;
211  }
212  }
213  else if(dest.id()==ID_array)
214  {
215  array_typet &array_type=to_array_type(dest);
216  if(!rename(array_type.size()))
217  result=false;
218  }
219 
220  return result;
221 }
222 
223 bool rename_symbolt::have_to_rename(const typet &dest) const
224 {
225  if(expr_map.empty() && type_map.empty())
226  return false;
227 
228  if(dest.has_subtype())
229  if(have_to_rename(to_type_with_subtype(dest).subtype()))
230  return true;
231 
232  for(const typet &subtype : to_type_with_subtypes(dest).subtypes())
233  {
234  if(have_to_rename(subtype))
235  return true;
236  }
237 
238  if(dest.id()==ID_struct ||
239  dest.id()==ID_union)
240  {
241  const struct_union_typet &struct_union_type=
242  to_struct_union_type(dest);
243 
244  for(const auto &c : struct_union_type.components())
245  if(have_to_rename(c))
246  return true;
247  }
248  else if(dest.id()==ID_code)
249  {
250  const code_typet &code_type=to_code_type(dest);
251  if(have_to_rename(code_type.return_type()))
252  return true;
253 
254  for(const auto &p : code_type.parameters())
255  {
256  if(have_to_rename(p.type()))
257  return true;
258 
259  if(expr_map.find(p.get_identifier()) != expr_map.end())
260  return true;
261  }
262 
263  const exprt &spec_assigns =
264  static_cast<const exprt &>(dest.find(ID_C_spec_assigns));
265  if(spec_assigns.is_not_nil() && have_to_rename(spec_assigns))
266  return true;
267 
268  const exprt &spec_ensures =
269  static_cast<const exprt &>(dest.find(ID_C_spec_ensures));
270  if(spec_ensures.is_not_nil() && have_to_rename(spec_ensures))
271  return true;
272 
273  const exprt &spec_requires =
274  static_cast<const exprt &>(dest.find(ID_C_spec_requires));
275  if(spec_requires.is_not_nil() && have_to_rename(spec_requires))
276  return true;
277  }
278  else if(dest.id()==ID_c_enum_tag ||
279  dest.id()==ID_struct_tag ||
280  dest.id()==ID_union_tag)
281  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
282  else if(dest.id()==ID_array)
283  return have_to_rename(to_array_type(dest).size());
284 
285  return false;
286 }
Arrays with given size.
Definition: std_types.h:807
const exprt & size() const
Definition: std_types.h:840
Base type of functions.
Definition: std_types.h:583
const typet & return_type() const
Definition: std_types.h:689
const parameterst & parameters() const
Definition: std_types.h:699
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
depth_iteratort depth_end()
Definition: expr.cpp:249
depth_iteratort depth_begin()
Definition: expr.cpp:247
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:360
const irept & find(const irep_idt &name) const
Definition: irep.cpp:93
bool is_not_nil() const
Definition: irep.h:368
const irep_idt & id() const
Definition: irep.h:384
irept & add(const irep_idt &name)
Definition: irep.cpp:103
void insert(const class symbol_exprt &old_expr, const class symbol_exprt &new_expr)
virtual ~rename_symbolt()
bool have_to_rename(const exprt &dest) const
expr_mapt expr_map
Definition: rename_symbol.h:63
bool rename(exprt &dest) const
type_mapt type_map
Definition: rename_symbol.h:64
void insert_expr(const irep_idt &old_id, const irep_idt &new_id)
Definition: rename_symbol.h:31
Base type for structs and unions.
Definition: std_types.h:62
const componentst & components() const
Definition: std_types.h:147
Expression to hold a symbol (variable)
Definition: std_expr.h:131
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:155
const irep_idt & get_identifier() const
Definition: std_expr.h:160
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:405
const irep_idt & get_identifier() const
Definition: std_types.h:410
The type of an expression, extends irept.
Definition: type.h:29
bool has_subtype() const
Definition: type.h:64
Forward depth-first search iterators These iterators' copy operations are expensive,...
API to expression classes.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:888
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
const tag_typet & to_tag_type(const typet &type)
Cast a typet to a tag_typet.
Definition: std_types.h:434
const type_with_subtypest & to_type_with_subtypes(const typet &type)
Definition: type.h:252
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition: type.h:208