Skip to content

Commit 9d9e50d

Browse files
authored
Merge pull request diffblue#1507 from smowton/smowton/1420-fragments/factor_java_object_init
Java frontend: factor out root class initialisation
2 parents b2104b0 + 7537302 commit 9d9e50d

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/java_bytecode/java_bytecode_typecheck_expr.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
2121
#include "java_pointer_casts.h"
2222
#include "java_types.h"
2323
#include "java_utils.h"
24+
#include "java_root_class.h"
2425

2526
void java_bytecode_typecheckt::typecheck_expr(exprt &expr)
2627
{
@@ -132,15 +133,7 @@ void java_bytecode_typecheckt::typecheck_expr_java_string_literal(exprt &expr)
132133
const auto &jlo_struct=to_struct_type(ns.follow(jlo_symbol));
133134
struct_exprt jlo_init(jlo_symbol);
134135
const auto &jls_struct=to_struct_type(ns.follow(string_type));
135-
136-
jlo_init.copy_to_operands(
137-
constant_exprt(
138-
"java::java.lang.String",
139-
jlo_struct.components()[0].type()));
140-
jlo_init.copy_to_operands(
141-
from_integer(
142-
0,
143-
jlo_struct.components()[1].type()));
136+
java_root_class_init(jlo_init, jlo_struct, false, "java::java.lang.String");
144137

145138
// If string refinement *is* around, populate the actual
146139
// contents as well:

src/java_bytecode/java_root_class.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Author: Daniel Kroening, [email protected]
88

99
#include "java_root_class.h"
1010

11+
#include <util/arith_tools.h>
1112
#include <util/symbol.h>
12-
#include <util/std_types.h>
1313

1414
#include "java_types.h"
1515

@@ -52,3 +52,26 @@ void java_root_class(symbolt &class_symbol)
5252
components.insert(components.begin(), component);
5353
}
5454
}
55+
56+
/// Adds members for an object of the root class (usually java.lang.Object).
57+
/// \param jlo [out] : object to initialize
58+
/// \param root_type: type of the root class
59+
/// \param lock: lock field
60+
/// \param class_identifier: class identifier field
61+
void java_root_class_init(
62+
struct_exprt &jlo,
63+
const struct_typet &root_type,
64+
const bool lock,
65+
const irep_idt &class_identifier)
66+
{
67+
jlo.operands().resize(root_type.components().size());
68+
69+
const std::size_t clsid_nb=root_type.component_number("@class_identifier");
70+
const typet &clsid_type=root_type.components()[clsid_nb].type();
71+
constant_exprt clsid(class_identifier, clsid_type);
72+
jlo.operands()[clsid_nb]=clsid;
73+
74+
const std::size_t lock_nb=root_type.component_number("@lock");
75+
const typet &lock_type=root_type.components()[lock_nb].type();
76+
jlo.operands()[lock_nb]=from_integer(lock, lock_type);
77+
}

src/java_bytecode/java_root_class.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ Author: Daniel Kroening, [email protected]
1818
void java_root_class(
1919
class symbolt &class_symbol);
2020

21+
void java_root_class_init(
22+
struct_exprt &jlo,
23+
const struct_typet &root_type,
24+
bool lock,
25+
const irep_idt &class_identifier);
26+
2127
#endif // CPROVER_JAVA_BYTECODE_JAVA_ROOT_CLASS_H

0 commit comments

Comments
 (0)