Skip to content

Commit 508a717

Browse files
Code clarity/readability
Return early on problems to avoid having to remember the problem until the end of a large block and to reduce nesting Indent TODO comments to keep them together and help IDE's to highlight them correctly Name expressions that are used repeatedly Remove unused parameter names Use count instead of find if the returned value isn't used Clarified the English in comments
1 parent e53c793 commit 508a717

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

jbmc/src/java_bytecode/generic_parameter_specialization_map_keys.cpp

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,42 @@ void generic_parameter_specialization_map_keyst::insert_pairs_for_pointer(
8080
const pointer_typet &pointer_type,
8181
const typet &pointer_subtype_struct)
8282
{
83-
if(is_java_generic_type(pointer_type))
83+
if(!is_java_generic_type(pointer_type))
84+
return;
85+
// The supplied type must be the full type of the pointer's subtype
86+
PRECONDITION(
87+
pointer_type.subtype().get(ID_identifier) ==
88+
pointer_subtype_struct.get(ID_name));
89+
90+
// If the pointer points to:
91+
// - an incomplete class or
92+
// - a class that is neither generic nor implicitly generic (this
93+
// may be due to unsupported class signature)
94+
// then ignore the generic types in the pointer and do not add any pairs.
95+
// TODO TG-1996 should decide how mocking and generics should work
96+
// together. Currently an incomplete class is never marked as generic. If
97+
// this changes in TG-1996 then the condition below should be updated.
98+
if(to_java_class_type(pointer_subtype_struct).get_is_stub())
99+
return;
100+
if(
101+
!is_java_generic_class_type(pointer_subtype_struct) &&
102+
!is_java_implicitly_generic_class_type(pointer_subtype_struct))
84103
{
85-
// The supplied type must be the full type of the pointer's subtype
86-
PRECONDITION(
87-
pointer_type.subtype().get(ID_identifier) ==
88-
pointer_subtype_struct.get(ID_name));
104+
return;
105+
}
89106

90-
// If the pointer points to:
91-
// - an incomplete class or
92-
// - a class that is neither generic nor implicitly generic (this
93-
// may be due to unsupported class signature)
94-
// then ignore the generic types in the pointer and do not add any pairs.
95-
// TODO TG-1996 should decide how mocking and generics should work
96-
// together. Currently an incomplete class is never marked as generic. If
97-
// this changes in TG-1996 then the condition below should be updated.
98-
if(
99-
!to_java_class_type(pointer_subtype_struct).get_is_stub() &&
100-
(is_java_generic_class_type(pointer_subtype_struct) ||
101-
is_java_implicitly_generic_class_type(pointer_subtype_struct)))
102-
{
103-
const java_generic_typet &generic_pointer =
104-
to_java_generic_type(pointer_type);
105-
const std::vector<java_generic_parametert> &generic_parameters =
106-
get_all_generic_parameters(pointer_subtype_struct);
107+
const java_generic_typet &generic_pointer =
108+
to_java_generic_type(pointer_type);
107109

108-
INVARIANT(
109-
generic_pointer.generic_type_arguments().size() ==
110-
generic_parameters.size(),
111-
"All generic parameters of the pointer type need to be specified");
110+
const std::vector<java_generic_parametert> &generic_parameters =
111+
get_all_generic_parameters(pointer_subtype_struct);
112+
const java_generic_typet::generic_type_argumentst &type_args =
113+
generic_pointer.generic_type_arguments();
114+
INVARIANT(
115+
type_args.size() == generic_parameters.size(),
116+
"All generic parameters of the pointer type need to be specified");
112117

113-
insert_pairs(
114-
generic_parameters, generic_pointer.generic_type_arguments());
115-
}
116-
}
118+
insert_pairs(generic_parameters, type_args);
117119
}
118120

119121
/// Add a pair of a parameter and its types for each generic parameter of the
@@ -137,24 +139,29 @@ void generic_parameter_specialization_map_keyst::insert_pairs_for_symbol(
137139
// then ignore the generic types in the struct_tag_type and do not add any
138140
// pairs.
139141
// TODO TG-1996 should decide how mocking and generics should work
140-
// together. Currently an incomplete class is never marked as generic. If
141-
// this changes in TG-1996 then the condition below should be updated.
142+
// together. Currently an incomplete class is never marked as generic. If
143+
// this changes in TG-1996 then the condition below should be updated.
144+
if(!is_java_generic_struct_tag_type(struct_tag_type))
145+
return;
146+
if(to_java_class_type(symbol_struct).get_is_stub())
147+
return;
142148
if(
143-
is_java_generic_struct_tag_type(struct_tag_type) &&
144-
!to_java_class_type(symbol_struct).get_is_stub() &&
145-
(is_java_generic_class_type(symbol_struct) ||
146-
is_java_implicitly_generic_class_type(symbol_struct)))
149+
!is_java_generic_class_type(symbol_struct) &&
150+
!is_java_implicitly_generic_class_type(symbol_struct))
147151
{
148-
const java_generic_struct_tag_typet &generic_symbol =
149-
to_java_generic_struct_tag_type(struct_tag_type);
152+
return;
153+
}
154+
const java_generic_struct_tag_typet &generic_symbol =
155+
to_java_generic_struct_tag_type(struct_tag_type);
150156

151-
const std::vector<java_generic_parametert> &generic_parameters =
152-
get_all_generic_parameters(symbol_struct);
157+
const std::vector<java_generic_parametert> &generic_parameters =
158+
get_all_generic_parameters(symbol_struct);
159+
const java_generic_typet::generic_type_argumentst &type_args =
160+
generic_symbol.generic_types();
153161

154-
INVARIANT(
155-
generic_symbol.generic_types().size() == generic_parameters.size(),
156-
"All generic parameters of the superclass need to be concretized");
162+
INVARIANT(
163+
type_args.size() == generic_parameters.size(),
164+
"All generic parameters of the superclass need to be concretized");
157165

158-
insert_pairs(generic_parameters, generic_symbol.generic_types());
159-
}
166+
insert_pairs(generic_parameters, type_args);
160167
}

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ void java_object_factoryt::gen_nondet_pointer_init(
487487
// If we are changing the pointer, we generate code for creating a pointer
488488
// to the substituted type instead
489489
// TODO if we are comparing array types we need to compare their element
490-
// types. this is for now done by implementing equality function especially
491-
// for java types, technical debt TG-2707
490+
// types. this is for now done by implementing equality function especially
491+
// for java types, technical debt TG-2707
492492
if(!equal_java_types(replacement_pointer_type, pointer_type))
493493
{
494494
// update generic_parameter_specialization_map for the new pointer

jbmc/src/java_bytecode/select_pointer_type.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ pointer_typet select_pointer_typet::convert_pointer_type(
1818
const pointer_typet &pointer_type,
1919
const generic_parameter_specialization_mapt
2020
&generic_parameter_specialization_map,
21-
const namespacet &ns) const
21+
const namespacet &) const
2222
{
23-
(void)ns; // unused parameter
2423
// if we have a map of generic parameters -> types and the pointer is
2524
// a generic parameter, specialize it with concrete types
2625
if(!generic_parameter_specialization_map.empty())
@@ -51,7 +50,7 @@ pointer_typet select_pointer_typet::specialize_generics(
5150

5251
// avoid infinite recursion by looking at each generic argument from
5352
// previous assignments
54-
if(visited_nodes.find(parameter_name) != visited_nodes.end())
53+
if(visited_nodes.count(parameter_name) != 0)
5554
{
5655
const optionalt<pointer_typet> result = get_recursively_instantiated_type(
5756
parameter_name, generic_parameter_specialization_map);
@@ -112,7 +111,7 @@ pointer_typet select_pointer_typet::specialize_generics(
112111
///
113112
/// Example:
114113
/// `class MyGeneric<T,U> { MyGeneric<U,T> gen; T t;}`
115-
/// When instantiating `MyGeneric<Integer,String> my` we need to for example
114+
/// For example, when instantiating `MyGeneric<Integer,String> my` we need to
116115
/// resolve the type of `my.gen.t`. The map would in this context contain
117116
/// - T -> (Integer, U)
118117
/// - U -> (String, T)

0 commit comments

Comments
 (0)