cprover
resolve_inherited_component.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3  Module: GOTO Program Utilities
4 
5  Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
9 #include <algorithm>
10 
12 
16  const symbol_tablet &symbol_table):
17  symbol_table(symbol_table)
18 {
20 }
21 
27  const symbol_tablet &symbol_table, const class_hierarchyt &class_hierarchy):
28  class_hierarchy(class_hierarchy),
29  symbol_table(symbol_table)
30 {
31  // We require the class_hierarchy to be already populated if we are being
32  // supplied it.
34 }
35 
48  const irep_idt &class_id,
49  const irep_idt &component_name,
50  bool include_interfaces)
51 {
52  PRECONDITION(!class_id.empty());
53  PRECONDITION(!component_name.empty());
54 
55  std::vector<irep_idt> classes_to_visit;
56  classes_to_visit.push_back(class_id);
57  while(!classes_to_visit.empty())
58  {
59  irep_idt current_class = classes_to_visit.back();
60  classes_to_visit.pop_back();
61 
62  const irep_idt &full_component_identifier=
63  build_full_component_identifier(current_class, component_name);
64 
65  if(symbol_table.has_symbol(full_component_identifier))
66  {
67  return inherited_componentt(current_class, component_name);
68  }
69 
70  const class_hierarchyt::idst &parents=
71  class_hierarchy.class_map[current_class].parents;
72 
73  if(include_interfaces)
74  {
75  classes_to_visit.insert(
76  classes_to_visit.end(), parents.begin(), parents.end());
77  }
78  else
79  {
80  if(!parents.empty())
81  classes_to_visit.push_back(parents.front());
82  }
83  }
84 
85  return inherited_componentt();
86 }
87 
95  const irep_idt &class_name, const irep_idt &component_name)
96 {
97  // Verify the parameters are called in the correct order.
98  PRECONDITION(id2string(class_name).find("::")!=std::string::npos);
99  PRECONDITION(id2string(component_name).find("::")==std::string::npos);
100  return id2string(class_name)+'.'+id2string(component_name);
101 }
102 
107 {
110 }
111 
115 {
116  return !class_identifier.empty();
117 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
Non-graph-based representation of the class hierarchy.
bool is_valid() const
Use to check if this inherited_componentt has been fully constructed.
inherited_componentt operator()(const irep_idt &class_id, const irep_idt &component_name, bool include_interfaces)
Given a class and a component, identify the concrete field or method it is resolved to...
static irep_idt build_full_component_identifier(const irep_idt &class_name, const irep_idt &component_name)
Build a component name as found in a GOTO symbol table equivalent to the name of a concrete component...
The symbol table.
Definition: symbol_table.h:19
#define PRECONDITION(CONDITION)
Definition: invariant.h:242
Given a class and a component (either field or method), find the closest parent that defines that com...
class_mapt class_map
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
resolve_inherited_componentt(const symbol_tablet &symbol_table)
See the operator() method comment.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
std::vector< irep_idt > idst
bool empty() const
Definition: dstring.h:73
irep_idt get_full_component_identifier() const
Get the full name of this function.