Skip to content

Commit 5431604

Browse files
author
Daniel Kroening
committed
Java object factory: use follow_tag instead of follow
follow_tag(...) is stronger typed than follow(...), which means that some explict upcasts can be dropped.
1 parent a4aaaf5 commit 5431604

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void initialize_nondet_string_fields(
417417
namespacet ns(symbol_table);
418418

419419
const struct_typet &struct_type =
420-
to_struct_type(ns.follow(struct_expr.type()));
420+
ns.follow_tag(to_struct_tag_type(struct_expr.type()));
421421

422422
// In case the type for String was not added to the symbol table,
423423
// (typically when string refinement is not activated), `struct_type`
@@ -538,7 +538,8 @@ void java_object_factoryt::gen_nondet_pointer_init(
538538
generic_parameter_specialization_map_keys(
539539
generic_parameter_specialization_map);
540540
generic_parameter_specialization_map_keys.insert_pairs_for_pointer(
541-
replacement_pointer_type, ns.follow(replacement_pointer_type.subtype()));
541+
replacement_pointer_type,
542+
ns.follow(to_struct_tag_type(replacement_pointer_type.subtype())));
542543

543544
const symbol_exprt real_pointer_symbol = gen_nondet_subtype_pointer_init(
544545
assignments, lifetime, replacement_pointer_type, depth, location);
@@ -568,24 +569,21 @@ void java_object_factoryt::gen_nondet_pointer_init(
568569
// the pointer to NULL instead of recursively initializing the struct to which
569570
// it points.
570571
const typet &subtype = pointer_type.subtype();
571-
const typet &followed_subtype = ns.follow(subtype);
572-
if(followed_subtype.id() == ID_struct)
573-
{
574-
const struct_typet &struct_type = to_struct_type(followed_subtype);
575-
const irep_idt &struct_tag=struct_type.get_tag();
572+
const struct_typet &struct_type = ns.follow_tag(to_struct_tag_type(subtype));
573+
const irep_idt &struct_tag = struct_type.get_tag();
576574

577-
// If this is a recursive type of some kind AND the depth is exceeded, set
578-
// the pointer to null.
579-
if(!recursion_set_entry.insert_entry(struct_tag) &&
580-
depth>=object_factory_parameters.max_nondet_tree_depth)
575+
// If this is a recursive type of some kind AND the depth is exceeded, set
576+
// the pointer to null.
577+
if(
578+
!recursion_set_entry.insert_entry(struct_tag) &&
579+
depth >= object_factory_parameters.max_nondet_tree_depth)
580+
{
581+
if(update_in_place == update_in_placet::NO_UPDATE_IN_PLACE)
581582
{
582-
if(update_in_place==update_in_placet::NO_UPDATE_IN_PLACE)
583-
{
584-
assignments.add(get_null_assignment(expr, pointer_type));
585-
}
586-
// Otherwise leave it as it is.
587-
return;
583+
assignments.add(get_null_assignment(expr, pointer_type));
588584
}
585+
// Otherwise leave it as it is.
586+
return;
589587
}
590588

591589
// If this is a void* we *must* initialise with null:
@@ -609,7 +607,7 @@ void java_object_factoryt::gen_nondet_pointer_init(
609607
// ci_lazy_methodst::initialize_instantiated_classes.
610608
if(
611609
const auto class_type =
612-
type_try_dynamic_cast<java_class_typet>(followed_subtype))
610+
type_try_dynamic_cast<java_class_typet>(struct_type))
613611
{
614612
if(class_type->get_base("java::java.lang.Enum") && !must_be_null)
615613
{
@@ -830,7 +828,7 @@ void java_object_factoryt::gen_nondet_struct_init(
830828
const update_in_placet &update_in_place,
831829
const source_locationt &location)
832830
{
833-
PRECONDITION(ns.follow(expr.type()).id()==ID_struct);
831+
PRECONDITION(expr.type().id() == ID_struct_tag);
834832
PRECONDITION(struct_type.id()==ID_struct);
835833

836834
typedef struct_typet::componentst componentst;
@@ -1043,7 +1041,7 @@ void java_object_factoryt::gen_nondet_init(
10431041
generic_parameter_specialization_map_keys(
10441042
generic_parameter_specialization_map);
10451043
generic_parameter_specialization_map_keys.insert_pairs_for_pointer(
1046-
pointer_type, ns.follow(pointer_type.subtype()));
1044+
pointer_type, ns.follow_tag(to_struct_tag_type(pointer_type.subtype())));
10471045

10481046
gen_nondet_pointer_init(
10491047
assignments,
@@ -1056,21 +1054,19 @@ void java_object_factoryt::gen_nondet_init(
10561054
}
10571055
else if(followed_type.id() == ID_struct)
10581056
{
1059-
const struct_typet struct_type = to_struct_type(followed_type);
1057+
const struct_typet &struct_type = to_struct_type(followed_type);
10601058

10611059
// If we are about to initialize a generic class (as a superclass object
10621060
// for a different object), add its concrete types to the map and delete
10631061
// them on leaving this function scope.
10641062
generic_parameter_specialization_map_keyst
10651063
generic_parameter_specialization_map_keys(
10661064
generic_parameter_specialization_map);
1065+
10671066
if(is_sub)
10681067
{
1069-
const typet &symbol =
1070-
override_type.has_value() ? *override_type : expr.type();
1071-
PRECONDITION(symbol.id() == ID_struct_tag);
10721068
generic_parameter_specialization_map_keys.insert_pairs_for_symbol(
1073-
to_struct_tag_type(symbol), struct_type);
1069+
to_struct_tag_type(type), struct_type);
10741070
}
10751071

10761072
gen_nondet_struct_init(
@@ -1391,8 +1387,9 @@ void java_object_factoryt::gen_nondet_array_init(
13911387
PRECONDITION(expr.type().subtype().id() == ID_struct_tag);
13921388
PRECONDITION(update_in_place != update_in_placet::MAY_UPDATE_IN_PLACE);
13931389

1394-
const typet &type = ns.follow(expr.type().subtype());
1395-
const struct_typet &struct_type = to_struct_type(type);
1390+
const struct_tag_typet &struct_tag_type =
1391+
to_struct_tag_type(expr.type().subtype());
1392+
const struct_typet &struct_type = ns.follow_tag(struct_tag_type);
13961393
const typet &element_type =
13971394
static_cast<const typet &>(expr.type().subtype().find(ID_element_type));
13981395

@@ -1475,7 +1472,8 @@ void java_object_factoryt::gen_nondet_enum_init(
14751472

14761473
// Access members (length and data) of $VALUES array
14771474
dereference_exprt deref_expr(values.symbol_expr());
1478-
const auto &deref_struct_type = to_struct_type(ns.follow(deref_expr.type()));
1475+
const auto &deref_struct_type =
1476+
ns.follow_tag(to_struct_tag_type(deref_expr.type()));
14791477
PRECONDITION(is_valid_java_array(deref_struct_type));
14801478
const auto &comps = deref_struct_type.components();
14811479
const member_exprt length_expr(deref_expr, "length", comps[1].type());

0 commit comments

Comments
 (0)