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 
18  const symbol_tablet &symbol_table,
19  const class_hierarchyt &class_hierarchy)
20  : class_hierarchy(class_hierarchy), symbol_table(symbol_table)
21 {
22  // We require the class_hierarchy to be already populated if we are being
23  // supplied it.
25 }
26 
39  const irep_idt &class_id,
40  const irep_idt &component_name,
41  bool include_interfaces)
42 {
43  PRECONDITION(!class_id.empty());
44  PRECONDITION(!component_name.empty());
45 
46  std::vector<irep_idt> classes_to_visit;
47  classes_to_visit.push_back(class_id);
48  while(!classes_to_visit.empty())
49  {
50  irep_idt current_class = classes_to_visit.back();
51  classes_to_visit.pop_back();
52 
53  const irep_idt &full_component_identifier=
54  build_full_component_identifier(current_class, component_name);
55 
56  if(symbol_table.has_symbol(full_component_identifier))
57  {
58  return inherited_componentt(current_class, component_name);
59  }
60 
61  const auto current_class_id = class_hierarchy.class_map.find(current_class);
62  if(current_class_id != class_hierarchy.class_map.end())
63  {
64  const class_hierarchyt::idst &parents = current_class_id->second.parents;
65 
66  if(include_interfaces)
67  {
68  classes_to_visit.insert(
69  classes_to_visit.end(), parents.begin(), parents.end());
70  }
71  else
72  {
73  if(!parents.empty())
74  classes_to_visit.push_back(parents.front());
75  }
76  }
77  }
78 
79  return inherited_componentt();
80 }
81 
89  const irep_idt &class_name, const irep_idt &component_name)
90 {
91  // Verify the parameters are called in the correct order.
92  PRECONDITION(id2string(class_name).find("::")!=std::string::npos);
93  PRECONDITION(id2string(component_name).find("::")==std::string::npos);
94  return id2string(class_name)+'.'+id2string(component_name);
95 }
96 
101 {
104 }
105 
109 {
110  return !class_identifier.empty();
111 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
const class_hierarchyt & class_hierarchy
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...
resolve_inherited_componentt(const symbol_tablet &symbol_table, const class_hierarchyt &class_hierarchy)
See the operator() method comment.
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:438
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:35
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:75
irep_idt get_full_component_identifier() const
Get the full name of this function.