cprover
find_symbols.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 "find_symbols.h"
10 
11 #include "std_types.h"
12 #include "std_expr.h"
13 
15 
17  const exprt &src,
18  find_symbols_sett &dest)
19 {
20  find_symbols(src, dest, true, true);
21 }
22 
24  const exprt &src,
25  find_symbols_sett &dest,
26  bool current,
27  bool next)
28 {
29  if((src.id()==ID_symbol && current) ||
30  (src.id()==ID_next_symbol && next))
31  dest.insert(src.get(ID_identifier));
32  else
33  {
34  forall_operands(it, src)
35  find_symbols(*it, dest, current, next);
36  }
37 }
38 
40  const exprt &src,
41  const find_symbols_sett &symbols,
42  bool current,
43  bool next)
44 {
45  if((src.id()==ID_symbol && current) ||
46  (src.id()==ID_next_symbol && next))
47  return symbols.count(src.get(ID_identifier))!=0;
48  else
49  {
50  forall_operands(it, src)
51  if(has_symbol(*it, symbols, current, next))
52  return true;
53  }
54 
55  return false;
56 }
57 
59  const exprt &src,
60  const find_symbols_sett &symbols)
61 {
62  return has_symbol(src, symbols, true, true);
63 }
64 
66  const exprt &src,
67  std::set<exprt> &dest)
68 {
69  if(src.id()==ID_symbol || src.id()==ID_next_symbol)
70  dest.insert(src);
71  else
72  {
73  forall_operands(it, src)
74  find_symbols(*it, dest);
75  }
76 }
77 
79  const exprt &src,
80  std::set<symbol_exprt> &dest)
81 {
82  if(src.id()==ID_symbol)
83  dest.insert(to_symbol_expr(src));
84  else
85  {
86  forall_operands(it, src)
87  find_symbols(*it, dest);
88  }
89 }
90 
91 void find_symbols(kindt kind, const typet &src, find_symbols_sett &dest);
92 
93 void find_symbols(kindt kind, const exprt &src, find_symbols_sett &dest)
94 {
95  forall_operands(it, src)
96  find_symbols(kind, *it, dest);
97 
98  find_symbols(kind, src.type(), dest);
99 
100  if(kind==kindt::F_BOTH || kind==kindt::F_EXPR)
101  if(src.id()==ID_symbol ||
102  src.id()==ID_next_symbol)
103  dest.insert(src.get(ID_identifier));
104 
105  const irept &c_sizeof_type=src.find(ID_C_c_sizeof_type);
106 
107  if(c_sizeof_type.is_not_nil())
108  find_symbols(kind, static_cast<const typet &>(c_sizeof_type), dest);
109 
110  const irept &va_arg_type=src.find(ID_C_va_arg_type);
111 
112  if(va_arg_type.is_not_nil())
113  find_symbols(kind, static_cast<const typet &>(va_arg_type), dest);
114 }
115 
116 void find_symbols(kindt kind, const typet &src, find_symbols_sett &dest)
117 {
118  if(kind!=kindt::F_TYPE_NON_PTR ||
119  src.id()!=ID_pointer)
120  {
121  if(src.has_subtype())
122  find_symbols(kind, src.subtype(), dest);
123 
124  forall_subtypes(it, src)
125  find_symbols(kind, *it, dest);
126 
127  const irep_idt &typedef_name=src.get(ID_C_typedef);
128  if(!typedef_name.empty())
129  dest.insert(typedef_name);
130  }
131 
132  if(src.id()==ID_struct ||
133  src.id()==ID_union)
134  {
135  const struct_union_typet &struct_union_type=to_struct_union_type(src);
136  const struct_union_typet::componentst &components=
137  struct_union_type.components();
138 
139  for(struct_union_typet::componentst::const_iterator
140  it=components.begin();
141  it!=components.end();
142  it++)
143  find_symbols(kind, *it, dest);
144  }
145  else if(src.id()==ID_code)
146  {
147  const code_typet &code_type=to_code_type(src);
148  find_symbols(kind, code_type.return_type(), dest);
149  const code_typet::parameterst &parameters=code_type.parameters();
150 
151  for(code_typet::parameterst::const_iterator
152  it=parameters.begin();
153  it!=parameters.end();
154  it++)
155  {
156  find_symbols(kind, *it, dest);
157 
158  // irep_idt identifier=it->get_identifier();
159  // if(!identifier.empty() && (kind==F_TYPE || kind==F_BOTH))
160  // dest.insert(identifier);
161  }
162  }
163  else if(src.id()==ID_symbol)
164  dest.insert(src.get(ID_identifier));
165  else if(src.id()==ID_array)
166  {
167  // do the size -- the subtype is already done
168  find_symbols(kind, to_array_type(src).size(), dest);
169  }
170  else if(src.id()==ID_c_enum_tag)
171  {
172  dest.insert(to_c_enum_tag_type(src).get_identifier());
173  }
174  else if(src.id()==ID_struct_tag)
175  {
176  dest.insert(to_struct_tag_type(src).get_identifier());
177  }
178  else if(src.id()==ID_union_tag)
179  {
180  dest.insert(to_union_tag_type(src).get_identifier());
181  }
182 }
183 
185 {
186  find_symbols(kindt::F_TYPE, src, dest);
187 }
188 
190 {
191  find_symbols(kindt::F_TYPE, src, dest);
192 }
193 
195  const exprt &src,
196  find_symbols_sett &dest)
197 {
199 }
200 
202  const typet &src,
203  find_symbols_sett &dest)
204 {
206 }
207 
209 {
210  find_symbols(kindt::F_BOTH, src, dest);
211 }
212 
214 {
215  find_symbols(kindt::F_BOTH, src, dest);
216 }
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
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
typet & type()
Definition: expr.h:60
kindt
std::unordered_set< irep_idt, irep_id_hash > find_symbols_sett
Definition: find_symbols.h:20
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
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 c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a generic typet to a c_enum_tag_typet.
Definition: std_types.h:717
API to expression classes.
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
Base class for tree-like data structures with sharing.
Definition: irep.h:87
#define forall_operands(it, expr)
Definition: expr.h:17
bool has_symbol(const exprt &src, const find_symbols_sett &symbols, bool current, bool next)
void find_non_pointer_type_symbols(const exprt &src, find_symbols_sett &dest)
API to type classes.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a generic typet to a union_tag_typet.
Definition: std_types.h:536
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:159
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a generic typet to a union_tag_typet.
Definition: std_types.h:573
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
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:884
void find_type_and_expr_symbols(const exprt &src, find_symbols_sett &dest)
const typet & subtype() const
Definition: type.h:31
void find_type_symbols(const exprt &src, find_symbols_sett &dest)
bool empty() const
Definition: dstring.h:61
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
void find_symbols(const exprt &src, find_symbols_sett &dest)
const typet & return_type() const
Definition: std_types.h:831