Skip to content

Commit ffd089f

Browse files
author
thk123
committed
Constructed class to mimic the original class in all but name of symbol
When the tag was wrong it causes issues in calling methods and meant the real time type information would not be handled correctly. As the components on the new class have been moved, accessing the components by index no longer works. Instead access the components by name (and made use of other helper functions)
1 parent 7f53f02 commit ffd089f

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

src/java_bytecode/generate_java_generic_type.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,20 @@ symbolt generate_java_generic_typet::operator()(
7070
pre_modification_size==after_modification_size,
7171
"All components in the original class should be in the new class");
7272

73-
const auto expected_symbol="java::"+id2string(new_tag);
73+
const java_class_typet &new_java_class = construct_specialised_generic_type(
74+
generic_class_definition, new_tag, replacement_components);
75+
const type_symbolt &class_symbol =
76+
build_symbol_from_specialised_class(new_java_class);
77+
78+
std::pair<symbolt &, bool> res = symbol_table.insert(std::move(class_symbol));
79+
if(!res.second)
80+
{
81+
messaget message(message_handler);
82+
message.warning() << "stub class symbol " << class_symbol.name
83+
<< " already exists" << messaget::eom;
84+
}
7485

75-
generate_class_stub(
76-
new_tag,
77-
symbol_table,
78-
message_handler,
79-
replacement_components);
86+
const auto expected_symbol="java::"+id2string(new_tag);
8087
auto symbol=symbol_table.lookup(expected_symbol);
8188
INVARIANT(symbol, "New class not created");
8289
return *symbol;
@@ -216,3 +223,38 @@ irep_idt generate_java_generic_typet::build_generic_tag(
216223

217224
return new_tag_buffer.str();
218225
}
226+
227+
/// Build the specalised version of the specific class, with the specified
228+
/// parameters and name.
229+
/// \param generic_class_definition: The generic class we are specialising
230+
/// \param new_tag: The new name for the class (like Generic<java::Float>)
231+
/// \param new_components: The specialised components
232+
/// \return The newly constructed class.
233+
java_class_typet
234+
generate_java_generic_typet::construct_specialised_generic_type(
235+
const java_generic_class_typet &generic_class_definition,
236+
const irep_idt &new_tag,
237+
const struct_typet::componentst &new_components) const
238+
{
239+
java_class_typet specialised_class = generic_class_definition;
240+
// We are specialising the logic - so we don't want to be marked as generic
241+
specialised_class.set(ID_C_java_generics_class_type, false);
242+
specialised_class.set(ID_name, "java::" + id2string(new_tag));
243+
specialised_class.set(ID_base_name, new_tag);
244+
specialised_class.components() = new_components;
245+
return specialised_class;
246+
}
247+
248+
/// Construct the symbol to be moved into the symbol table
249+
/// \param specialised_class: The newly constructed specialised class
250+
/// \return The symbol to add to the symbol table
251+
type_symbolt generate_java_generic_typet::build_symbol_from_specialised_class(
252+
const java_class_typet &specialised_class) const
253+
{
254+
type_symbolt new_symbol(specialised_class);
255+
new_symbol.base_name = specialised_class.get(ID_base_name);
256+
new_symbol.pretty_name = specialised_class.get(ID_base_name);
257+
new_symbol.name = specialised_class.get(ID_name);
258+
new_symbol.mode = ID_java;
259+
return new_symbol;
260+
}

src/java_bytecode/generate_java_generic_type.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class generate_java_generic_typet
3333
const java_generic_class_typet &replacement_type,
3434
const java_generic_typet &generic_reference) const;
3535

36+
java_class_typet construct_specialised_generic_type(
37+
const java_generic_class_typet &generic_class_definition,
38+
const irep_idt &new_tag,
39+
const struct_typet::componentst &new_components) const;
40+
41+
type_symbolt build_symbol_from_specialised_class(
42+
const java_class_typet &specialised_class) const;
43+
3644
message_handlert &message_handler;
3745
};
3846

unit/java_bytecode/generate_concrete_generic_type/generate_java_generic_type.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ SCENARIO(
153153
REQUIRE(new_symbol_table.has_symbol(first_expected_symbol));
154154
auto first_symbol=new_symbol_table.lookup(first_expected_symbol);
155155
REQUIRE(first_symbol->type.id()==ID_struct);
156-
auto first_symbol_type=
157-
to_struct_type(first_symbol->type).components()[3].type();
158-
REQUIRE(first_symbol_type.id()==ID_pointer);
159-
REQUIRE(first_symbol_type.subtype().id()==ID_symbol);
160-
REQUIRE(to_symbol_type(first_symbol_type.subtype()).get_identifier()==
161-
"java::java.lang.Boolean");
156+
const struct_union_typet::componentt &component =
157+
require_type::require_component(
158+
to_struct_type(first_symbol->type), "elem");
159+
auto first_symbol_type=component.type();
160+
require_type::require_pointer(
161+
first_symbol_type, symbol_typet("java::java.lang.Boolean"));
162162

163163
REQUIRE(new_symbol_table.has_symbol(second_expected_symbol));
164164
auto second_symbol=new_symbol_table.lookup(second_expected_symbol);
165165
REQUIRE(second_symbol->type.id()==ID_struct);
166-
auto second_symbol_type=
167-
to_struct_type(second_symbol->type).components()[3].type();
168-
REQUIRE(second_symbol_type.id()==ID_pointer);
169-
REQUIRE(second_symbol_type.subtype().id()==ID_symbol);
170-
REQUIRE(to_symbol_type(second_symbol_type.subtype()).get_identifier()==
171-
"java::java.lang.Integer");
166+
const struct_union_typet::componentt &second_component =
167+
require_type::require_component(
168+
to_struct_type(second_symbol->type), "elem");
169+
auto second_symbol_type=second_component.type();
170+
require_type::require_pointer(
171+
second_symbol_type, symbol_typet("java::java.lang.Integer"));
172172
}
173173
}
174174

0 commit comments

Comments
 (0)