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  Forall_subtypes(it, dest)
133  if(!rename(*it))
134  result=false;
135 
136  if(dest.id()==ID_struct ||
137  dest.id()==ID_union)
138  {
139  struct_union_typet &struct_union_type=to_struct_union_type(dest);
141  struct_union_type.components();
142 
143  for(struct_union_typet::componentst::iterator
144  it=components.begin();
145  it!=components.end();
146  it++)
147  if(!rename(*it))
148  result=false;
149  }
150  else if(dest.id()==ID_code)
151  {
152  code_typet &code_type=to_code_type(dest);
153  rename(code_type.return_type());
154  code_typet::parameterst &parameters=code_type.parameters();
155 
156  for(code_typet::parameterst::iterator it = parameters.begin();
157  it!=parameters.end();
158  it++)
159  {
160  if(!rename(it->type()))
161  result=false;
162 
163  expr_mapt::const_iterator e_it=
164  expr_map.find(it->get_identifier());
165 
166  if(e_it!=expr_map.end())
167  {
168  it->set_identifier(e_it->second);
169  result=false;
170  }
171  }
172  }
173  else if(dest.id()==ID_symbol)
174  {
175  type_mapt::const_iterator it=
176  type_map.find(to_symbol_type(dest).get_identifier());
177 
178  if(it!=type_map.end())
179  {
180  to_symbol_type(dest).set_identifier(it->second);
181  result=false;
182  }
183  }
184  else if(dest.id()==ID_c_enum_tag ||
185  dest.id()==ID_struct_tag ||
186  dest.id()==ID_union_tag)
187  {
188  type_mapt::const_iterator it=
189  type_map.find(to_tag_type(dest).get_identifier());
190 
191  if(it!=type_map.end())
192  {
193  to_tag_type(dest).set_identifier(it->second);
194  result=false;
195  }
196  }
197  else if(dest.id()==ID_array)
198  {
199  array_typet &array_type=to_array_type(dest);
200  if(!rename(array_type.size()))
201  result=false;
202  }
203 
204  return result;
205 }
206 
207 bool rename_symbolt::have_to_rename(const typet &dest) const
208 {
209  if(expr_map.empty() && type_map.empty())
210  return false;
211 
212  if(dest.has_subtype())
213  if(have_to_rename(dest.subtype()))
214  return true;
215 
216  forall_subtypes(it, dest)
217  if(have_to_rename(*it))
218  return true;
219 
220  if(dest.id()==ID_struct ||
221  dest.id()==ID_union)
222  {
223  const struct_union_typet &struct_union_type=
224  to_struct_union_type(dest);
225 
226  const struct_union_typet::componentst &components=
227  struct_union_type.components();
228 
229  for(struct_union_typet::componentst::const_iterator
230  it=components.begin();
231  it!=components.end();
232  it++)
233  if(have_to_rename(*it))
234  return true;
235  }
236  else if(dest.id()==ID_code)
237  {
238  const code_typet &code_type=to_code_type(dest);
239  if(have_to_rename(code_type.return_type()))
240  return true;
241 
242  const code_typet::parameterst &parameters=code_type.parameters();
243 
244  for(code_typet::parameterst::const_iterator
245  it=parameters.begin();
246  it!=parameters.end();
247  it++)
248  {
249  if(have_to_rename(it->type()))
250  return true;
251 
252  if(expr_map.find(it->get_identifier())!=expr_map.end())
253  return true;
254  }
255  }
256  else if(dest.id()==ID_symbol)
257  {
258  const irep_idt &identifier = to_symbol_type(dest).get_identifier();
259  return type_map.find(identifier) != type_map.end();
260  }
261  else if(dest.id()==ID_c_enum_tag ||
262  dest.id()==ID_struct_tag ||
263  dest.id()==ID_union_tag)
264  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
265  else if(dest.id()==ID_array)
266  return have_to_rename(to_array_type(dest).size());
267 
268  return false;
269 }
type_mapt type_map
Definition: rename_symbol.h:60
The type of an expression.
Definition: type.h:22
#define forall_subtypes(it, type)
Definition: type.h:161
Base type of functions.
Definition: std_types.h:764
bool is_not_nil() const
Definition: irep.h:103
bool has_subtype() const
Definition: type.h:79
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:987
const irep_idt & get_identifier() const
Definition: std_expr.h:128
const tag_typet & to_tag_type(const typet &type)
Cast a generic typet to a tag_typet.
Definition: std_types.h:525
std::vector< componentt > componentst
Definition: std_types.h:243
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
Definition: std_types.h:139
std::vector< parametert > parameterst
Definition: std_types.h:767
const componentst & components() const
Definition: std_types.h:245
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:118
typet & type()
Definition: expr.h:56
depth_iteratort depth_begin()
Definition: expr.cpp:299
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:504
expr_mapt expr_map
Definition: rename_symbol.h:59
bool have_to_rename(const exprt &dest) const
const irep_idt & id() const
Definition: irep.h:189
API to expression classes.
bool rename(exprt &dest) const
const exprt & size() const
Definition: std_types.h:1014
Base class for tree-like data structures with sharing.
Definition: irep.h:86
#define forall_operands(it, expr)
Definition: expr.h:17
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
API to type classes.
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:162
void insert_expr(const irep_idt &old_id, const irep_idt &new_id)
Definition: rename_symbol.h:31
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:1045
Base class for all expressions.
Definition: expr.h:42
const parameterst & parameters() const
Definition: std_types.h:905
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a generic typet to a struct_union_typet.
Definition: std_types.h:280
irept & add(const irep_namet &name)
Definition: irep.cpp:306
#define Forall_subtypes(it, type)
Definition: type.h:167
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:123
arrays with given size
Definition: std_types.h:1004
Expression to hold a symbol (variable)
Definition: std_expr.h:90
depth_iteratort depth_end()
Definition: expr.cpp:301
const typet & subtype() const
Definition: type.h:33
void insert(const class symbol_exprt &old_expr, const class symbol_exprt &new_expr)
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
const typet & return_type() const
Definition: std_types.h:895
const irep_idt & get_identifier() const
Definition: std_types.h:123
virtual ~rename_symbolt()