cprover
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_types.h"
13 #include "std_expr.h"
14 
16 {
17 }
18 
20 {
21 }
22 
24  const symbol_exprt &old_expr,
25  const symbol_exprt &new_expr)
26 {
27  insert_expr(old_expr.get_identifier(), new_expr.get_identifier());
28 }
29 
30 bool rename_symbolt::rename(exprt &dest) const
31 {
32  bool result=true;
33 
34  for(auto it = dest.depth_begin(), end = dest.depth_end(); it != end; ++it)
35  {
36  exprt * modifiable_expr = nullptr;
37 
38  // first look at type
39  if(have_to_rename(it->type()))
40  {
41  modifiable_expr = &it.mutate();
42  result &= rename(modifiable_expr->type());
43  }
44 
45  // now do expression itself
46  if(it->id()==ID_symbol)
47  {
48  expr_mapt::const_iterator entry =
50 
51  if(entry != expr_map.end())
52  {
53  if(!modifiable_expr)
54  modifiable_expr = &it.mutate();
55  to_symbol_expr(*modifiable_expr).set_identifier(entry->second);
56  result = false;
57  }
58  }
59 
60  const typet &c_sizeof_type =
61  static_cast<const typet&>(it->find(ID_C_c_sizeof_type));
62  if(c_sizeof_type.is_not_nil() && have_to_rename(c_sizeof_type))
63  {
64  if(!modifiable_expr)
65  modifiable_expr = &it.mutate();
66  result &=
67  rename(static_cast<typet&>(modifiable_expr->add(ID_C_c_sizeof_type)));
68  }
69 
70  const typet &va_arg_type =
71  static_cast<const typet&>(it->find(ID_C_va_arg_type));
72  if(va_arg_type.is_not_nil() && have_to_rename(va_arg_type))
73  {
74  if(!modifiable_expr)
75  modifiable_expr = &it.mutate();
76  result &=
77  rename(static_cast<typet&>(modifiable_expr->add(ID_C_va_arg_type)));
78  }
79  }
80 
81  return result;
82 }
83 
84 bool rename_symbolt::have_to_rename(const exprt &dest) const
85 {
86  if(expr_map.empty() && type_map.empty())
87  return false;
88 
89  // first look at type
90 
91  if(have_to_rename(dest.type()))
92  return true;
93 
94  // now do expression itself
95 
96  if(dest.id()==ID_symbol)
97  {
98  const irep_idt &identifier = to_symbol_expr(dest).get_identifier();
99  return expr_map.find(identifier) != expr_map.end();
100  }
101 
102  forall_operands(it, dest)
103  if(have_to_rename(*it))
104  return true;
105 
106  const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
107 
108  if(c_sizeof_type.is_not_nil())
109  if(have_to_rename(static_cast<const typet &>(c_sizeof_type)))
110  return true;
111 
112  const irept &va_arg_type=dest.find(ID_C_va_arg_type);
113 
114  if(va_arg_type.is_not_nil())
115  if(have_to_rename(static_cast<const typet &>(va_arg_type)))
116  return true;
117 
118  return false;
119 }
120 
121 bool rename_symbolt::rename(typet &dest) const
122 {
123  if(!have_to_rename(dest))
124  return true;
125 
126  bool result=true;
127 
128  if(dest.has_subtype())
129  if(!rename(dest.subtype()))
130  result=false;
131 
132  for(typet &subtype : to_type_with_subtypes(dest).subtypes())
133  {
134  if(!rename(subtype))
135  result=false;
136  }
137 
138  if(dest.id()==ID_struct ||
139  dest.id()==ID_union)
140  {
141  struct_union_typet &struct_union_type=to_struct_union_type(dest);
142 
143  for(auto &c : struct_union_type.components())
144  if(!rename(c))
145  result=false;
146  }
147  else if(dest.id()==ID_code)
148  {
149  code_typet &code_type=to_code_type(dest);
150  rename(code_type.return_type());
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  else if(dest.id()==ID_c_enum_tag ||
167  dest.id()==ID_struct_tag ||
168  dest.id()==ID_union_tag)
169  {
170  type_mapt::const_iterator it=
171  type_map.find(to_tag_type(dest).get_identifier());
172 
173  if(it!=type_map.end())
174  {
175  to_tag_type(dest).set_identifier(it->second);
176  result=false;
177  }
178  }
179  else if(dest.id()==ID_array)
180  {
181  array_typet &array_type=to_array_type(dest);
182  if(!rename(array_type.size()))
183  result=false;
184  }
185 
186  return result;
187 }
188 
189 bool rename_symbolt::have_to_rename(const typet &dest) const
190 {
191  if(expr_map.empty() && type_map.empty())
192  return false;
193 
194  if(dest.has_subtype())
195  if(have_to_rename(dest.subtype()))
196  return true;
197 
198  for(const typet &subtype : to_type_with_subtypes(dest).subtypes())
199  {
200  if(have_to_rename(subtype))
201  return true;
202  }
203 
204  if(dest.id()==ID_struct ||
205  dest.id()==ID_union)
206  {
207  const struct_union_typet &struct_union_type=
208  to_struct_union_type(dest);
209 
210  for(const auto &c : struct_union_type.components())
211  if(have_to_rename(c))
212  return true;
213  }
214  else if(dest.id()==ID_code)
215  {
216  const code_typet &code_type=to_code_type(dest);
217  if(have_to_rename(code_type.return_type()))
218  return true;
219 
220  for(const auto &p : code_type.parameters())
221  {
222  if(have_to_rename(p.type()))
223  return true;
224 
225  if(expr_map.find(p.get_identifier()) != expr_map.end())
226  return true;
227  }
228  }
229  else if(dest.id()==ID_c_enum_tag ||
230  dest.id()==ID_struct_tag ||
231  dest.id()==ID_union_tag)
232  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
233  else if(dest.id()==ID_array)
234  return have_to_rename(to_array_type(dest).size());
235 
236  return false;
237 }
Arrays with given size.
Definition: std_types.h:968
const exprt & size() const
Definition: std_types.h:976
Base type of functions.
Definition: std_types.h:744
const typet & return_type() const
Definition: std_types.h:850
const parameterst & parameters() const
Definition: std_types.h:860
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
depth_iteratort depth_end()
Definition: expr.cpp:281
depth_iteratort depth_begin()
Definition: expr.cpp:279
typet & type()
Return the type of the expression.
Definition: expr.h:82
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:383
irept & add(const irep_namet &name)
Definition: irep.cpp:113
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
bool is_not_nil() const
Definition: irep.h:391
const irep_idt & id() const
Definition: irep.h:407
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:57
const componentst & components() const
Definition: std_types.h:142
Expression to hold a symbol (variable)
Definition: std_expr.h:81
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:105
const irep_idt & get_identifier() const
Definition: std_expr.h:110
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:454
The type of an expression, extends irept.
Definition: type.h:28
const typet & subtype() const
Definition: type.h:47
bool has_subtype() const
Definition: type.h:65
#define forall_operands(it, expr)
Definition: expr.h:18
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:190
Pre-defined types.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:949
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:1018
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:209
const tag_typet & to_tag_type(const typet &type)
Cast a typet to a tag_typet.
Definition: std_types.h:483
const type_with_subtypest & to_type_with_subtypes(const typet &type)
Definition: type.h:198