10
10
// / Context-insensitive lazy methods container
11
11
12
12
#include " ci_lazy_methods.h"
13
+ #include " java_static_initializers.h"
13
14
14
15
#include < java_bytecode/select_pointer_type.h>
15
16
#include < string>
@@ -25,6 +26,24 @@ void ci_lazy_methods_neededt::add_needed_method(
25
26
callable_methods.insert (method_symbol_name);
26
27
}
27
28
29
+ // / For a given class id, note that its static initializer is needed.
30
+ // / This applies the same logic to the given class that
31
+ // / `java_bytecode_convert_methodt::get_clinit_call` applies e.g. to classes
32
+ // / whose constructor we call in a method body. This duplication is unavoidable
33
+ // / because ci_lazy_methods essentially has to go through the same logic as
34
+ // / __CPROVER_start in its initial setup, and because return values of opaque
35
+ // / methods need to be considered in ci_lazy_methods too.
36
+ // / \param class_id: The given class id
37
+ // / \param symbol_table: Used to look up occurrences of static initializers
38
+ void ci_lazy_methods_neededt::add_clinit_call (
39
+ const irep_idt &class_id,
40
+ const symbol_tablet &symbol_table)
41
+ {
42
+ const irep_idt &clinit_wrapper = clinit_wrapper_name (class_id);
43
+ if (symbol_table.symbols .count (clinit_wrapper))
44
+ add_needed_method (clinit_wrapper);
45
+ }
46
+
28
47
// / Notes class `class_symbol_name` will be instantiated, or a static field
29
48
// / belonging to it will be accessed. Also notes that its static initializer is
30
49
// / therefore reachable.
@@ -42,6 +61,14 @@ bool ci_lazy_methods_neededt::add_needed_class(
42
61
if (symbol_table.symbols .count (cprover_validate))
43
62
add_needed_method (cprover_validate);
44
63
64
+ // Special case for enums. We may want to generalise this, the comment in
65
+ // \ref java_object_factoryt::gen_nondet_pointer_init (TG-4689).
66
+ namespacet ns (symbol_table);
67
+ const auto &class_type =
68
+ to_java_class_type (ns.lookup (class_symbol_name).type );
69
+ if (class_type.get_base (" java::java.lang.Enum" ))
70
+ add_clinit_call (class_symbol_name, symbol_table);
71
+
45
72
return true ;
46
73
}
47
74
0 commit comments