|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Generate Java Generic Type - Instantiate a generic class with |
| 4 | + concrete type information. |
| 5 | +
|
| 6 | + Author: DiffBlue Limited. All rights reserved. |
| 7 | +
|
| 8 | +\*******************************************************************/ |
| 9 | +#include "generate_java_generic_type.h" |
| 10 | +#include <util/namespace.h> |
| 11 | +#include <java_bytecode/java_types.h> |
| 12 | +#include <java_bytecode/java_utils.h> |
| 13 | + |
| 14 | + |
| 15 | +generate_java_generic_typet::generate_java_generic_typet( |
| 16 | + message_handlert &message_handler): |
| 17 | + message_handler(message_handler) |
| 18 | +{} |
| 19 | + |
| 20 | +/// Generate a concrete instantiation of a generic type. |
| 21 | +/// \param existing_generic_type The type to be concretised |
| 22 | +/// \param symbol_table The symbol table that the concrete type will be |
| 23 | +/// inserted into. |
| 24 | +/// \return The symbol as it was retrieved from the symbol table after |
| 25 | +/// it has been inserted into. |
| 26 | +symbolt generate_java_generic_typet::operator()( |
| 27 | + const java_generic_typet &existing_generic_type, |
| 28 | + symbol_tablet &symbol_table) const |
| 29 | +{ |
| 30 | + namespacet ns(symbol_table); |
| 31 | + |
| 32 | + const typet &pointer_subtype=ns.follow(existing_generic_type.subtype()); |
| 33 | + |
| 34 | + INVARIANT( |
| 35 | + pointer_subtype.id()==ID_struct, "Only pointers to classes in java"); |
| 36 | + |
| 37 | + const java_class_typet &replacement_type= |
| 38 | + to_java_class_type(pointer_subtype); |
| 39 | + const irep_idt new_tag=build_generic_tag( |
| 40 | + existing_generic_type, replacement_type); |
| 41 | + struct_union_typet::componentst replacement_components= |
| 42 | + replacement_type.components(); |
| 43 | + |
| 44 | + // Small auxiliary function, to perform the inplace |
| 45 | + // modification of the generic fields. |
| 46 | + auto replace_type_for_generic_field= |
| 47 | + [&](struct_union_typet::componentt &component) |
| 48 | + { |
| 49 | + if(is_java_generic_parameter(component.type())) |
| 50 | + { |
| 51 | + auto replacement_type_param= |
| 52 | + to_java_generics_class_type(replacement_type); |
| 53 | + |
| 54 | + auto component_identifier= |
| 55 | + to_java_generic_parameter(component.type()).type_variable() |
| 56 | + .get_identifier(); |
| 57 | + |
| 58 | + optionalt<size_t> results=java_generics_get_index_for_subtype( |
| 59 | + replacement_type_param, component_identifier); |
| 60 | + |
| 61 | + INVARIANT( |
| 62 | + results.has_value(), |
| 63 | + "generic component type not found"); |
| 64 | + |
| 65 | + if(results) |
| 66 | + { |
| 67 | + component.type()= |
| 68 | + existing_generic_type.generic_type_variables()[*results]; |
| 69 | + } |
| 70 | + } |
| 71 | + return component; |
| 72 | + }; |
| 73 | + |
| 74 | + std::size_t pre_modification_size=to_java_class_type( |
| 75 | + ns.follow(existing_generic_type.subtype())).components().size(); |
| 76 | + |
| 77 | + std::for_each( |
| 78 | + replacement_components.begin(), |
| 79 | + replacement_components.end(), |
| 80 | + replace_type_for_generic_field); |
| 81 | + |
| 82 | + std::size_t after_modification_size= |
| 83 | + replacement_type.components().size(); |
| 84 | + |
| 85 | + INVARIANT( |
| 86 | + pre_modification_size==after_modification_size, |
| 87 | + "All components in the original class should be in the new class"); |
| 88 | + |
| 89 | + const auto expected_symbol="java::"+id2string(new_tag); |
| 90 | + |
| 91 | + generate_class_stub( |
| 92 | + new_tag, |
| 93 | + symbol_table, |
| 94 | + message_handler, |
| 95 | + replacement_components); |
| 96 | + auto symbol=symbol_table.lookup(expected_symbol); |
| 97 | + INVARIANT(symbol, "New class not created"); |
| 98 | + return *symbol; |
| 99 | +} |
| 100 | + |
| 101 | +/// Build a unique tag for the generic to be instantiated. |
| 102 | +/// \param existing_generic_type The type we want to concretise |
| 103 | +/// \param original_class |
| 104 | +/// \return A tag for the new generic we want a unique tag for. |
| 105 | +irep_idt generate_java_generic_typet::build_generic_tag( |
| 106 | + const java_generic_typet &existing_generic_type, |
| 107 | + const java_class_typet &original_class) const |
| 108 | +{ |
| 109 | + std::ostringstream new_tag_buffer; |
| 110 | + new_tag_buffer << original_class.get_tag(); |
| 111 | + new_tag_buffer << "<"; |
| 112 | + bool first=true; |
| 113 | + for(const typet ¶m : existing_generic_type.generic_type_variables()) |
| 114 | + { |
| 115 | + if(!first) |
| 116 | + new_tag_buffer << ","; |
| 117 | + first=false; |
| 118 | + |
| 119 | + INVARIANT( |
| 120 | + is_java_generic_inst_parameter(param), |
| 121 | + "Only create full concretized generic types"); |
| 122 | + new_tag_buffer << param.subtype().get(ID_identifier); |
| 123 | + } |
| 124 | + |
| 125 | + new_tag_buffer << ">"; |
| 126 | + |
| 127 | + return new_tag_buffer.str(); |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +/// Activate the generic instantiation code. |
| 132 | +/// \param message_handler |
| 133 | +/// \param symbol_table The symbol table so far. |
| 134 | +void |
| 135 | +instantiate_generics( |
| 136 | + message_handlert &message_handler, |
| 137 | + symbol_tablet &symbol_table) |
| 138 | +{ |
| 139 | + generate_java_generic_typet instantiate_generic_type(message_handler); |
| 140 | + // check out the symbols in the symbol table at this point to see if we |
| 141 | + // have a a generic type in. |
| 142 | + for(const auto &symbol : symbol_table.symbols) |
| 143 | + { |
| 144 | + if(symbol.second.type.id()==ID_struct) |
| 145 | + { |
| 146 | + auto symbol_struct=to_struct_type(symbol.second.type); |
| 147 | + auto &components=symbol_struct.components(); |
| 148 | + |
| 149 | + for(const auto &component : components) |
| 150 | + { |
| 151 | + if(is_java_generic_type(component.type())) |
| 152 | + { |
| 153 | + const auto &type_vars=to_java_generic_type(component.type()). |
| 154 | + generic_type_variables(); |
| 155 | + |
| 156 | + // Before we can instantiate a generic component, we need |
| 157 | + // its type variables to be instantiated parameters |
| 158 | + if(all_of(type_vars.cbegin(), type_vars.cend(), |
| 159 | + [](const typet &type) |
| 160 | + { |
| 161 | + return is_java_generic_inst_parameter(type); |
| 162 | + })) |
| 163 | + { |
| 164 | + instantiate_generic_type( |
| 165 | + to_java_generic_type(component.type()), symbol_table); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments