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);
140 
141  for(auto &c : struct_union_type.components())
142  if(!rename(c))
143  result=false;
144  }
145  else if(dest.id()==ID_code)
146  {
147  code_typet &code_type=to_code_type(dest);
148  rename(code_type.return_type());
149 
150  for(auto &p : code_type.parameters())
151  {
152  if(!rename(p.type()))
153  result=false;
154 
155  expr_mapt::const_iterator e_it = expr_map.find(p.get_identifier());
156 
157  if(e_it!=expr_map.end())
158  {
159  p.set_identifier(e_it->second);
160  result=false;
161  }
162  }
163  }
164  else if(dest.id() == ID_symbol_type)
165  {
166  type_mapt::const_iterator it=
167  type_map.find(to_symbol_type(dest).get_identifier());
168 
169  if(it!=type_map.end())
170  {
171  to_symbol_type(dest).set_identifier(it->second);
172  result=false;
173  }
174  }
175  else if(dest.id()==ID_c_enum_tag ||
176  dest.id()==ID_struct_tag ||
177  dest.id()==ID_union_tag)
178  {
179  type_mapt::const_iterator it=
180  type_map.find(to_tag_type(dest).get_identifier());
181 
182  if(it!=type_map.end())
183  {
184  to_tag_type(dest).set_identifier(it->second);
185  result=false;
186  }
187  }
188  else if(dest.id()==ID_array)
189  {
190  array_typet &array_type=to_array_type(dest);
191  if(!rename(array_type.size()))
192  result=false;
193  }
194 
195  return result;
196 }
197 
198 bool rename_symbolt::have_to_rename(const typet &dest) const
199 {
200  if(expr_map.empty() && type_map.empty())
201  return false;
202 
203  if(dest.has_subtype())
204  if(have_to_rename(dest.subtype()))
205  return true;
206 
207  forall_subtypes(it, dest)
208  if(have_to_rename(*it))
209  return true;
210 
211  if(dest.id()==ID_struct ||
212  dest.id()==ID_union)
213  {
214  const struct_union_typet &struct_union_type=
215  to_struct_union_type(dest);
216 
217  for(const auto &c : struct_union_type.components())
218  if(have_to_rename(c))
219  return true;
220  }
221  else if(dest.id()==ID_code)
222  {
223  const code_typet &code_type=to_code_type(dest);
224  if(have_to_rename(code_type.return_type()))
225  return true;
226 
227  for(const auto &p : code_type.parameters())
228  {
229  if(have_to_rename(p.type()))
230  return true;
231 
232  if(expr_map.find(p.get_identifier()) != expr_map.end())
233  return true;
234  }
235  }
236  else if(dest.id() == ID_symbol_type)
237  {
238  const irep_idt &identifier = to_symbol_type(dest).get_identifier();
239  return type_map.find(identifier) != type_map.end();
240  }
241  else if(dest.id()==ID_c_enum_tag ||
242  dest.id()==ID_struct_tag ||
243  dest.id()==ID_union_tag)
244  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
245  else if(dest.id()==ID_array)
246  return have_to_rename(to_array_type(dest).size());
247 
248  return false;
249 }
type_mapt type_map
Definition: rename_symbol.h:60
The type of an expression, extends irept.
Definition: type.h:27
#define forall_subtypes(it, type)
Definition: type.h:216
Base type of functions.
Definition: std_types.h:751
bool is_not_nil() const
Definition: irep.h:173
bool has_subtype() const
Definition: type.h:56
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:982
const irep_idt & get_identifier() const
Definition: std_expr.h:176
const tag_typet & to_tag_type(const typet &type)
Cast a typet to a tag_typet.
Definition: std_types.h:503
const symbol_typet & to_symbol_type(const typet &type)
Cast a typet to a symbol_typet.
Definition: std_types.h:98
const componentst & components() const
Definition: std_types.h:205
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:70
typet & type()
Return the type of the expression.
Definition: expr.h:68
depth_iteratort depth_begin()
Definition: expr.cpp:282
Forward depth-first search iterators These iterators&#39; copy operations are expensive, so use auto&, and avoid std::next(), std::prev() and post-increment iterator.
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:474
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:259
API to expression classes.
bool rename(exprt &dest) const
const exprt & size() const
Definition: std_types.h:1010
Base class for tree-like data structures with sharing.
Definition: irep.h:156
#define forall_operands(it, expr)
Definition: expr.h:20
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:251
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:35
Pre-defined types.
Base type for structs and unions.
Definition: std_types.h:114
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 typet to an array_typet.
Definition: std_types.h:1048
Base class for all expressions.
Definition: expr.h:54
const parameterst & parameters() const
Definition: std_types.h:893
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:260
irept & add(const irep_namet &name)
Definition: irep.cpp:305
#define Forall_subtypes(it, type)
Definition: type.h:222
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:171
Arrays with given size.
Definition: std_types.h:1000
Expression to hold a symbol (variable)
Definition: std_expr.h:143
depth_iteratort depth_end()
Definition: expr.cpp:284
const typet & subtype() const
Definition: type.h:38
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:284
const typet & return_type() const
Definition: std_types.h:883
const irep_idt & get_identifier() const
Definition: std_types.h:75
virtual ~rename_symbolt()