Skip to content

TG-1318 Mgudemann/feature/support arrays in generic parameters #1567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/java_bytecode/generate_java_generic_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,24 @@ irep_idt generate_java_generic_typet::build_generic_tag(
INVARIANT(
is_java_generic_inst_parameter(param),
"Only create full concretized generic types");
new_tag_buffer << param.subtype().get(ID_identifier);
const irep_idt &id(id2string(param.subtype().get(ID_identifier)));
new_tag_buffer << id2string(id);
if(is_java_array_tag(id))
{
const typet &element_type =
java_array_element_type(to_symbol_type(param.subtype()));

// If this is an array of references then we will specialize its
// identifier using the type of the objects in the array. Else, there can
// be a problem with the same symbols for different instantiations using
// arrays with different types.
if(element_type.id() == ID_pointer)
{
const symbol_typet element_symbol =
to_symbol_type(element_type.subtype());
new_tag_buffer << "of_" << id2string(element_symbol.get_identifier());
}
}
}

new_tag_buffer << ">";
Expand Down
6 changes: 2 additions & 4 deletions src/java_bytecode/java_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ Author: Daniel Kroening, [email protected]

bool java_is_array_type(const typet &type)
{
if(type.id()!=ID_struct)
if(type.id() != ID_struct)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't restyle untouched line

return false;
return has_prefix(id2string(
to_struct_type(type).get_tag()),
"java::array[");
return is_java_array_tag(to_struct_type(type).get_tag());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this method - I wonder if all instances should be replaced with java_types::is_valid_java_array (this is out of scope so not required!)

}

unsigned java_local_variable_slots(const typet &t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ SCENARIO(

// We want to test that the specialized/instantiated class has it's field
// type updated, so find the specialized class, not the generic class.
const irep_idt test_class=
"java::generic_field_array_instantiation$generic<java::array[reference]>";
const irep_idt test_class =
"java::generic_field_array_instantiation$generic<java::array[reference]"
"of_java::java.lang.Float>";

GIVEN("A generic type instantiated with an array type")
{
Expand Down Expand Up @@ -230,5 +231,42 @@ SCENARIO(
require_type::require_pointer(
java_array_element_type(test_field_array),
symbol_typet("java::java.lang.Float"));

// check for other specialized classes, in particular different symbol ids
// for arrays with different element types
GIVEN("A generic type instantiated with different array types")
{
const irep_idt test_class_integer =
"java::generic_field_array_instantiation$generic<java::array[reference]"
"of_"
"java::java.lang.Integer>";

const irep_idt test_class_int =
"java::generic_field_array_instantiation$generic<java::array[int]>";

const irep_idt test_class_float =
"java::generic_field_array_instantiation$generic<java::array[float]>";

const struct_typet::componentt &component_g =
require_type::require_component(
to_struct_type(harness_symbol.type), "g");
instantiate_generic_type(
to_java_generic_type(component_g.type()), new_symbol_table);
REQUIRE(new_symbol_table.has_symbol(test_class_integer));

const struct_typet::componentt &component_h =
require_type::require_component(
to_struct_type(harness_symbol.type), "h");
instantiate_generic_type(
to_java_generic_type(component_h.type()), new_symbol_table);
REQUIRE(new_symbol_table.has_symbol(test_class_int));

const struct_typet::componentt &component_i =
require_type::require_component(
to_struct_type(harness_symbol.type), "i");
instantiate_generic_type(
to_java_generic_type(component_i.type()), new_symbol_table);
REQUIRE(new_symbol_table.has_symbol(test_class_float));
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class generic<T> {
}

generic<Float []> f;
generic<Integer []> g;
generic<int []> h;
generic<float []> i;
Float [] af;
}