|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: GOTO Program Utilities |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | + |
| 11 | +#include "resolve_inherited_component.h" |
| 12 | + |
| 13 | +/// See the operator() method comment. |
| 14 | +/// \param symbol_table: The symbol table to resolve the component against |
| 15 | +resolve_inherited_componentt::resolve_inherited_componentt( |
| 16 | + const symbol_tablet &symbol_table): |
| 17 | + symbol_table(symbol_table) |
| 18 | +{ |
| 19 | + class_hierarchy(symbol_table); |
| 20 | +} |
| 21 | + |
| 22 | +/// See the operator() method comment |
| 23 | +/// \param symbol_table: The symbol table to resolve the component against |
| 24 | +/// \param class_hierarchy: A prebuilt class_hierachy based on the symbol_table |
| 25 | +/// |
| 26 | +resolve_inherited_componentt::resolve_inherited_componentt( |
| 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. |
| 33 | + PRECONDITION(!class_hierarchy.class_map.empty()); |
| 34 | +} |
| 35 | + |
| 36 | +/// Given a class and a component, identify the concrete field or method it is |
| 37 | +/// resolved to. For example, a reference Child.abc refers to Child's method or |
| 38 | +/// field if it exists, or else Parent.abc, and so on regarding Parent's |
| 39 | +/// ancestors. If none are found, an empty string will be returned. |
| 40 | +/// \param class_id: The name of the class the function is being called on |
| 41 | +/// \param component_name: The base name of the component (i.e. without the |
| 42 | +/// class specifier) |
| 43 | +/// \return The concrete component that has been resolved |
| 44 | +resolve_inherited_componentt::inherited_componentt |
| 45 | + resolve_inherited_componentt::operator()( |
| 46 | + const irep_idt &class_id, const irep_idt &component_name) |
| 47 | +{ |
| 48 | + PRECONDITION(!class_id.empty()); |
| 49 | + PRECONDITION(!component_name.empty()); |
| 50 | + |
| 51 | + irep_idt current_class=class_id; |
| 52 | + while(!current_class.empty()) |
| 53 | + { |
| 54 | + const irep_idt &full_component_identifier= |
| 55 | + build_full_component_identifier(current_class, component_name); |
| 56 | + |
| 57 | + if(symbol_table.has_symbol(full_component_identifier)) |
| 58 | + { |
| 59 | + return inherited_componentt(current_class, component_name); |
| 60 | + } |
| 61 | + |
| 62 | + const class_hierarchyt::idst &parents= |
| 63 | + class_hierarchy.class_map[current_class].parents; |
| 64 | + |
| 65 | + if(parents.empty()) |
| 66 | + break; |
| 67 | + current_class=parents.front(); |
| 68 | + } |
| 69 | + |
| 70 | + return inherited_componentt(); |
| 71 | +} |
| 72 | + |
| 73 | +/// Build a component name as found in a GOTO symbol table equivalent to the |
| 74 | +/// name of a concrete component component_name on class class_name |
| 75 | +/// \param component_name: The name of the component |
| 76 | +/// \param class_name: The class the implementation would be found on. |
| 77 | +/// \return A name for looking up in the symbol table for classes `class_name`'s |
| 78 | +/// component `component_name` |
| 79 | +irep_idt resolve_inherited_componentt::build_full_component_identifier( |
| 80 | + const irep_idt &class_name, const irep_idt &component_name) |
| 81 | +{ |
| 82 | + // Verify the parameters are called in the correct order. |
| 83 | + PRECONDITION(id2string(class_name).find("::")!=std::string::npos); |
| 84 | + PRECONDITION(id2string(component_name).find("::")==std::string::npos); |
| 85 | + return id2string(class_name)+'.'+id2string(component_name); |
| 86 | +} |
| 87 | + |
| 88 | +/// Get the full name of this function |
| 89 | +/// \return The symbol name for this function call |
| 90 | +irep_idt resolve_inherited_componentt::inherited_componentt:: |
| 91 | + get_full_component_identifier() const |
| 92 | +{ |
| 93 | + return resolve_inherited_componentt::build_full_component_identifier( |
| 94 | + class_identifier, component_identifier); |
| 95 | +} |
| 96 | + |
| 97 | +/// Use to check if this inherited_componentt has been fully constructed. |
| 98 | +/// \return True if this represents a real concrete component |
| 99 | +bool resolve_inherited_componentt::inherited_componentt::is_valid() const |
| 100 | +{ |
| 101 | + return !class_identifier.empty(); |
| 102 | +} |
0 commit comments