cprover
class_hierarchy.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Class Hierarchy
4 
5 Author: Daniel Kroening
6 
7 Date: April 2016
8 
9 \*******************************************************************/
10 
13 
14 #include "class_hierarchy.h"
15 
16 #include <ostream>
17 
18 #include <util/std_types.h>
19 #include <util/symbol_table.h>
20 
26 {
27  forall_symbols(it, symbol_table.symbols)
28  {
29  if(it->second.is_type && it->second.type.id()==ID_struct)
30  {
31  const struct_typet &struct_type=
32  to_struct_type(it->second.type);
33 
34  const irept::subt &bases=
35  struct_type.find(ID_bases).get_sub();
36 
37  for(const auto &base : bases)
38  {
39  irep_idt parent=base.find(ID_type).get(ID_identifier);
40  if(parent.empty())
41  continue;
42 
43  class_map[parent].children.push_back(it->first);
44  class_map[it->first].parents.push_back(parent);
45  }
46  }
47  }
48 }
49 
51  const irep_idt &c,
52  idst &dest) const
53 {
54  class_mapt::const_iterator it=class_map.find(c);
55  if(it==class_map.end())
56  return;
57  const entryt &entry=it->second;
58 
59  for(const auto &child : entry.children)
60  dest.push_back(child);
61 
62  // recursive calls
63  for(const auto &child : entry.children)
64  get_children_trans_rec(child, dest);
65 }
66 
68  const irep_idt &c,
69  idst &dest) const
70 {
71  class_mapt::const_iterator it=class_map.find(c);
72  if(it==class_map.end())
73  return;
74  const entryt &entry=it->second;
75 
76  for(const auto &child : entry.parents)
77  dest.push_back(child);
78 
79  // recursive calls
80  for(const auto &child : entry.parents)
81  get_parents_trans_rec(child, dest);
82 }
83 
84 void class_hierarchyt::output(std::ostream &out) const
85 {
86  for(const auto &c : class_map)
87  {
88  for(const auto &pa : c.second.parents)
89  out << "Parent of " << c.first << ": "
90  << pa << '\n';
91 
92  for(const auto &ch : c.second.children)
93  out << "Child of " << c.first << ": "
94  << ch << '\n';
95  }
96 }
#define forall_symbols(it, expr)
Definition: symbol_table.h:28
void get_children_trans_rec(const irep_idt &, idst &) const
void operator()(const symbol_tablet &)
Looks for all the struct types in the symbol table and construct a map from class names to a data str...
std::vector< irept > subt
Definition: irep.h:91
void get_parents_trans_rec(const irep_idt &, idst &) const
Structure type.
Definition: std_types.h:296
subt & get_sub()
Definition: irep.h:245
Class Hierarchy.
symbolst symbols
Definition: symbol_table.h:57
The symbol table.
Definition: symbol_table.h:52
class_mapt class_map
Symbol table.
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:317
std::vector< irep_idt > idst
API to type classes.
void output(std::ostream &) const
bool empty() const
Definition: dstring.h:61
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285