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 "std_types.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  // first look at type
34 
35  if(have_to_rename(dest.type()))
36  if(!rename(dest.type()))
37  result=false;
38 
39  // now do expression itself
40 
41  if(!have_to_rename(dest))
42  return result;
43 
44  if(dest.id()==ID_symbol)
45  {
46  expr_mapt::const_iterator it=
47  expr_map.find(to_symbol_expr(dest).get_identifier());
48 
49  if(it!=expr_map.end())
50  {
51  to_symbol_expr(dest).set_identifier(it->second);
52  return false;
53  }
54  }
55 
56  Forall_operands(it, dest)
57  if(!rename(*it))
58  result=false;
59 
60  const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
61 
62  if(c_sizeof_type.is_not_nil() &&
63  !rename(static_cast<typet&>(dest.add(ID_C_c_sizeof_type))))
64  result=false;
65 
66  const irept &va_arg_type=dest.find(ID_C_va_arg_type);
67 
68  if(va_arg_type.is_not_nil() &&
69  !rename(static_cast<typet&>(dest.add(ID_C_va_arg_type))))
70  result=false;
71 
72  return result;
73 }
74 
75 bool rename_symbolt::have_to_rename(const exprt &dest) const
76 {
77  if(expr_map.empty() && type_map.empty())
78  return false;
79 
80  // first look at type
81 
82  if(have_to_rename(dest.type()))
83  return true;
84 
85  // now do expression itself
86 
87  if(dest.id()==ID_symbol)
88  return expr_map.find(dest.get(ID_identifier))!=expr_map.end();
89 
90  forall_operands(it, dest)
91  if(have_to_rename(*it))
92  return true;
93 
94  const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
95 
96  if(c_sizeof_type.is_not_nil())
97  if(have_to_rename(static_cast<const typet &>(c_sizeof_type)))
98  return true;
99 
100  const irept &va_arg_type=dest.find(ID_C_va_arg_type);
101 
102  if(va_arg_type.is_not_nil())
103  if(have_to_rename(static_cast<const typet &>(va_arg_type)))
104  return true;
105 
106  return false;
107 }
108 
109 bool rename_symbolt::rename(typet &dest) const
110 {
111  if(!have_to_rename(dest))
112  return true;
113 
114  bool result=true;
115 
116  if(dest.has_subtype())
117  if(!rename(dest.subtype()))
118  result=false;
119 
120  Forall_subtypes(it, dest)
121  if(!rename(*it))
122  result=false;
123 
124  if(dest.id()==ID_struct ||
125  dest.id()==ID_union)
126  {
127  struct_union_typet &struct_union_type=to_struct_union_type(dest);
129  struct_union_type.components();
130 
131  for(struct_union_typet::componentst::iterator
132  it=components.begin();
133  it!=components.end();
134  it++)
135  if(!rename(*it))
136  result=false;
137  }
138  else if(dest.id()==ID_code)
139  {
140  code_typet &code_type=to_code_type(dest);
141  rename(code_type.return_type());
142  code_typet::parameterst &parameters=code_type.parameters();
143 
144  for(code_typet::parameterst::iterator it = parameters.begin();
145  it!=parameters.end();
146  it++)
147  {
148  if(!rename(it->type()))
149  result=false;
150 
151  expr_mapt::const_iterator e_it=
152  expr_map.find(it->get_identifier());
153 
154  if(e_it!=expr_map.end())
155  {
156  it->set_identifier(e_it->second);
157  result=false;
158  }
159  }
160  }
161  else if(dest.id()==ID_symbol)
162  {
163  type_mapt::const_iterator it=
164  type_map.find(to_symbol_type(dest).get_identifier());
165 
166  if(it!=type_map.end())
167  {
168  to_symbol_type(dest).set_identifier(it->second);
169  result=false;
170  }
171  }
172  else if(dest.id()==ID_c_enum_tag ||
173  dest.id()==ID_struct_tag ||
174  dest.id()==ID_union_tag)
175  {
176  type_mapt::const_iterator it=
177  type_map.find(to_tag_type(dest).get_identifier());
178 
179  if(it!=type_map.end())
180  {
181  to_tag_type(dest).set_identifier(it->second);
182  result=false;
183  }
184  }
185  else if(dest.id()==ID_array)
186  {
187  array_typet &array_type=to_array_type(dest);
188  if(!rename(array_type.size()))
189  result=false;
190  }
191 
192  return result;
193 }
194 
195 bool rename_symbolt::have_to_rename(const typet &dest) const
196 {
197  if(expr_map.empty() && type_map.empty())
198  return false;
199 
200  if(dest.has_subtype())
201  if(have_to_rename(dest.subtype()))
202  return true;
203 
204  forall_subtypes(it, dest)
205  if(have_to_rename(*it))
206  return true;
207 
208  if(dest.id()==ID_struct ||
209  dest.id()==ID_union)
210  {
211  const struct_union_typet &struct_union_type=
212  to_struct_union_type(dest);
213 
214  const struct_union_typet::componentst &components=
215  struct_union_type.components();
216 
217  for(struct_union_typet::componentst::const_iterator
218  it=components.begin();
219  it!=components.end();
220  it++)
221  if(have_to_rename(*it))
222  return true;
223  }
224  else if(dest.id()==ID_code)
225  {
226  const code_typet &code_type=to_code_type(dest);
227  if(have_to_rename(code_type.return_type()))
228  return true;
229 
230  const code_typet::parameterst &parameters=code_type.parameters();
231 
232  for(code_typet::parameterst::const_iterator
233  it=parameters.begin();
234  it!=parameters.end();
235  it++)
236  {
237  if(have_to_rename(it->type()))
238  return true;
239 
240  if(expr_map.find(it->get_identifier())!=expr_map.end())
241  return true;
242  }
243  }
244  else if(dest.id()==ID_symbol)
245  return type_map.find(dest.get(ID_identifier))!=type_map.end();
246  else if(dest.id()==ID_c_enum_tag ||
247  dest.id()==ID_struct_tag ||
248  dest.id()==ID_union_tag)
249  return type_map.find(to_tag_type(dest).get_identifier())!=type_map.end();
250  else if(dest.id()==ID_array)
251  return have_to_rename(to_array_type(dest).size());
252 
253  return false;
254 }
type_mapt type_map
Definition: rename_symbol.h:60
The type of an expression.
Definition: type.h:20
#define forall_subtypes(it, type)
Definition: type.h:159
Base type of functions.
Definition: std_types.h:734
bool is_not_nil() const
Definition: irep.h:104
bool has_subtype() const
Definition: type.h:77
const tag_typet & to_tag_type(const typet &type)
Cast a generic typet to a tag_typet.
Definition: std_types.h:495
const irep_idt & get_identifier() const
Definition: std_expr.h:120
std::vector< componentt > componentst
Definition: std_types.h:240
std::vector< parametert > parameterst
Definition: std_types.h:829
const componentst & components() const
Definition: std_types.h:242
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:121
typet & type()
Definition: expr.h:60
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:474
expr_mapt expr_map
Definition: rename_symbol.h:59
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a generic typet to a struct_union_typet.
Definition: std_types.h:277
bool have_to_rename(const exprt &dest) const
const irep_idt & id() const
Definition: irep.h:189
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:946
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
Definition: std_types.h:142
API to expression classes.
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
bool rename(exprt &dest) const
const exprt & size() const
Definition: std_types.h:915
Base class for tree-like data structures with sharing.
Definition: irep.h:87
#define forall_operands(it, expr)
Definition: expr.h:17
API to type classes.
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:159
void insert_expr(const irep_idt &old_id, const irep_idt &new_id)
Definition: rename_symbol.h:31
Base class for all expressions.
Definition: expr.h:46
const parameterst & parameters() const
Definition: std_types.h:841
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:202
irept & add(const irep_namet &name)
Definition: irep.cpp:306
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:884
#define Forall_subtypes(it, type)
Definition: type.h:165
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:115
#define Forall_operands(it, expr)
Definition: expr.h:23
arrays with given size
Definition: std_types.h:901
Expression to hold a symbol (variable)
Definition: std_expr.h:82
const typet & subtype() const
Definition: type.h:31
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:831
virtual ~rename_symbolt()