Skip to content

Commit ffa104c

Browse files
author
thk123
committed
Enforce condition that generic references must refer to generic classes
We require that all generic references refer to generic classes Also pulled out the conversion out of the loop and gave the variable a more logical name Added check for generic utils function
1 parent 6e06fbd commit ffa104c

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/java_bytecode/generate_java_generic_type.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@ symbolt generate_java_generic_typet::operator()(
3232

3333
INVARIANT(
3434
pointer_subtype.id()==ID_struct, "Only pointers to classes in java");
35+
INVARIANT(
36+
is_java_generic_class_type(pointer_subtype),
37+
"Generic references type must be a generic class");
38+
39+
const java_generic_class_typet &generic_class_definition =
40+
to_java_generic_class_type(to_java_class_type(pointer_subtype));
3541

36-
const java_class_typet &replacement_type=
37-
to_java_class_type(pointer_subtype);
38-
const irep_idt new_tag=build_generic_tag(
39-
existing_generic_type, replacement_type);
40-
struct_union_typet::componentst replacement_components=
41-
replacement_type.components();
42+
const irep_idt new_tag =
43+
build_generic_tag(existing_generic_type, generic_class_definition);
44+
struct_union_typet::componentst replacement_components =
45+
generic_class_definition.components();
4246

4347
// Small auxiliary function, to perform the inplace
4448
// modification of the generic fields.
4549
auto replace_type_for_generic_field =
4650
[&](struct_union_typet::componentt &component) {
4751

4852
component.type() = substitute_type(
49-
component.type(),
50-
to_java_generic_class_type(replacement_type),
51-
existing_generic_type);
53+
component.type(), generic_class_definition, existing_generic_type);
5254

5355
return component;
5456
};
@@ -61,8 +63,8 @@ symbolt generate_java_generic_typet::operator()(
6163
replacement_components.end(),
6264
replace_type_for_generic_field);
6365

64-
std::size_t after_modification_size=
65-
replacement_type.components().size();
66+
std::size_t after_modification_size =
67+
generic_class_definition.components().size();
6668

6769
INVARIANT(
6870
pre_modification_size==after_modification_size,

unit/testing-utils/generic_utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <util/ui_message.h>
1010
#include <java_bytecode/generate_java_generic_type.h>
11+
#include <util/namespace.h>
1112
#include "generic_utils.h"
1213
#include "catch.hpp"
1314
#include "require_type.h"
@@ -29,6 +30,10 @@ void generic_utils::specialise_generic(
2930
example_type.generic_type_variables().end(),
3031
is_java_generic_inst_parameter));
3132

33+
namespacet ns(new_symbol_table);
34+
const typet &class_type = ns.follow(example_type.subtype());
35+
REQUIRE(is_java_generic_class_type(class_type));
36+
3237
// Generate the specialised version.
3338
ui_message_handlert message_handler;
3439
generate_java_generic_typet instantiate_generic_type(message_handler);

0 commit comments

Comments
 (0)