Skip to content

Commit a6d1084

Browse files
Move generate_class_stub out of java_bytecode_convert_classt and to java_utils.cpp and use it to generate stubs for exceptions
1 parent 961c0a3 commit a6d1084

7 files changed

+75
-64
lines changed

src/java_bytecode/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ SRC = bytecode_info.cpp \
55
jar_file.cpp \
66
java_bytecode_convert_class.cpp \
77
java_bytecode_convert_method.cpp \
8+
java_bytecode_instrument.cpp \
89
java_bytecode_internal_additions.cpp \
910
java_bytecode_language.cpp \
1011
java_bytecode_parse_tree.cpp \
@@ -16,7 +17,6 @@ SRC = bytecode_info.cpp \
1617
java_class_loader.cpp \
1718
java_class_loader_limit.cpp \
1819
java_entry_point.cpp \
19-
java_bytecode_instrument.cpp \
2020
java_local_variable_table.cpp \
2121
java_object_factory.cpp \
2222
java_pointer_casts.cpp \

src/java_bytecode/java_bytecode_convert_class.cpp

+5-36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "java_types.h"
1919
#include "java_bytecode_convert_method.h"
2020
#include "java_bytecode_language.h"
21+
#include "java_utils.h"
2122

2223
#include <util/arith_tools.h>
2324
#include <util/namespace.h>
@@ -56,7 +57,10 @@ class java_bytecode_convert_classt:public messaget
5657
string_preprocess.add_string_type(
5758
parse_tree.parsed_class.name, symbol_table);
5859
else if(!loading_success)
59-
generate_class_stub(parse_tree.parsed_class.name);
60+
generate_class_stub(
61+
parse_tree.parsed_class.name,
62+
symbol_table,
63+
get_message_handler());
6064
}
6165

6266
typedef java_bytecode_parse_treet::classt classt;
@@ -73,7 +77,6 @@ class java_bytecode_convert_classt:public messaget
7377
void convert(const classt &c);
7478
void convert(symbolt &class_symbol, const fieldt &f);
7579

76-
void generate_class_stub(const irep_idt &class_name);
7780
void add_array_types();
7881
};
7982

@@ -171,40 +174,6 @@ void java_bytecode_convert_classt::convert(const classt &c)
171174
java_root_class(*class_symbol);
172175
}
173176

174-
void java_bytecode_convert_classt::generate_class_stub(
175-
const irep_idt &class_name)
176-
{
177-
class_typet class_type;
178-
179-
class_type.set_tag(class_name);
180-
class_type.set(ID_base_name, class_name);
181-
182-
class_type.set(ID_incomplete_class, true);
183-
184-
// produce class symbol
185-
symbolt new_symbol;
186-
new_symbol.base_name=class_name;
187-
new_symbol.pretty_name=class_name;
188-
new_symbol.name="java::"+id2string(class_name);
189-
class_type.set(ID_name, new_symbol.name);
190-
new_symbol.type=class_type;
191-
new_symbol.mode=ID_java;
192-
new_symbol.is_type=true;
193-
194-
symbolt *class_symbol;
195-
196-
if(symbol_table.move(new_symbol, class_symbol))
197-
{
198-
warning() << "stub class symbol " << new_symbol.name
199-
<< " already exists" << eom;
200-
}
201-
else
202-
{
203-
// create the class identifier etc
204-
java_root_class(*class_symbol);
205-
}
206-
}
207-
208177
void java_bytecode_convert_classt::convert(
209178
symbolt &class_symbol,
210179
const fieldt &f)

src/java_bytecode/java_bytecode_instrument.cpp

+18-24
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@ Date: June 2017
1515

1616
#include <goto-programs/goto_functions.h>
1717

18+
#include "java_bytecode_convert_class.h"
1819
#include "java_bytecode_instrument.h"
1920
#include "java_entry_point.h"
2021
#include "java_object_factory.h"
2122
#include "java_root_class.h"
2223
#include "java_types.h"
24+
#include "java_utils.h"
2325

24-
class java_bytecode_instrumentt
26+
class java_bytecode_instrumentt:public messaget
2527
{
2628
public:
2729
java_bytecode_instrumentt(
2830
symbol_tablet &_symbol_table,
2931
const bool _throw_runtime_exceptions,
32+
message_handlert &_message_handler,
3033
const size_t _max_array_length):
3134
symbol_table(_symbol_table),
3235
throw_runtime_exceptions(_throw_runtime_exceptions),
36+
message_handler(_message_handler),
3337
max_array_length(_max_array_length)
3438
{
3539
}
@@ -38,8 +42,9 @@ class java_bytecode_instrumentt
3842

3943
protected:
4044
symbol_tablet &symbol_table;
41-
bool throw_runtime_exceptions;
42-
size_t max_array_length;
45+
const bool throw_runtime_exceptions;
46+
message_handlert &message_handler;
47+
const size_t max_array_length;
4348

4449
codet throw_exception(
4550
const exprt &cond,
@@ -92,25 +97,12 @@ codet java_bytecode_instrumentt::throw_exception(
9297

9398
if(!symbol_table.has_symbol(exc_obj_name))
9499
{
95-
// for now, create a class stub
96-
// TODO: model exceptions and use that model
97-
class_typet class_type;
98-
class_type.set_tag(exc_name);
99-
class_type.set(ID_base_name, exc_name);
100-
class_type.set(ID_incomplete_class, true);
101-
102-
// produce class symbol
103-
symbolt exc_symbol;
104-
exc_symbol.base_name=exc_name;
105-
exc_symbol.pretty_name=exc_name;
106-
exc_symbol.name="java::"+id2string(exc_name);
107-
class_type.set(ID_name, exc_symbol.name);
108-
exc_symbol.type=class_type;
109-
exc_symbol.mode=ID_java;
110-
exc_symbol.is_type=true;
111-
symbol_table.add(exc_symbol);
112-
// create the class identifier
113-
java_root_class(exc_symbol);
100+
generate_class_stub(
101+
exc_name,
102+
symbol_table,
103+
get_message_handler());
104+
const symbolt &exc_symbol=symbol_table.lookup(
105+
"java::"+id2string(exc_name));
114106

115107
// create the exception object
116108
exc=object_factory(
@@ -512,12 +504,14 @@ void java_bytecode_instrumentt::operator()(exprt &expr)
512504
/// in order to be able to call the object factory to create exceptions.
513505
void java_bytecode_instrument(
514506
symbol_tablet &symbol_table,
515-
bool throw_runtime_exceptions,
516-
size_t max_array_length)
507+
const bool throw_runtime_exceptions,
508+
message_handlert &message_handler,
509+
const size_t max_array_length)
517510
{
518511
java_bytecode_instrumentt instrument(
519512
symbol_table,
520513
throw_runtime_exceptions,
514+
message_handler,
521515
max_array_length);
522516

523517
Forall_symbols(s_it, symbol_table.symbols)

src/java_bytecode/java_bytecode_instrument.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Date: June 2017
1414
void java_bytecode_instrument(
1515
symbol_tablet &symbol_table,
1616
const bool throw_runtime_exceptions,
17+
message_handlert &_message_handler,
1718
const size_t max_array_length);
18-
1919
#endif

src/java_bytecode/java_bytecode_language.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ bool java_bytecode_languaget::typecheck(
444444
java_bytecode_instrument(
445445
symbol_table,
446446
throw_runtime_exceptions,
447+
get_message_handler(),
447448
max_nondet_array_length);
448449

449450
// now typecheck all

src/java_bytecode/java_utils.cpp

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
9+
#include <util/invariant.h>
10+
#include <util/message.h>
1011
#include <util/prefix.h>
1112
#include <util/std_types.h>
12-
#include <util/invariant.h>
1313
#include <util/string_utils.h>
1414

15+
#include "java_root_class.h"
1516
#include "java_utils.h"
1617
#include "java_types.h"
1718

@@ -54,7 +55,47 @@ unsigned java_method_parameter_slots(const code_typet &t)
5455
return slots;
5556
}
5657

58+
5759
const std::string java_class_to_package(const std::string &canonical_classname)
5860
{
5961
return trim_from_last_delimiter(canonical_classname, '.');
6062
}
63+
64+
void generate_class_stub(
65+
const irep_idt &class_name,
66+
symbol_tablet &symbol_table,
67+
message_handlert &message_handler)
68+
{
69+
class_typet class_type;
70+
71+
class_type.set_tag(class_name);
72+
class_type.set(ID_base_name, class_name);
73+
74+
class_type.set(ID_incomplete_class, true);
75+
76+
// produce class symbol
77+
symbolt new_symbol;
78+
new_symbol.base_name=class_name;
79+
new_symbol.pretty_name=class_name;
80+
new_symbol.name="java::"+id2string(class_name);
81+
class_type.set(ID_name, new_symbol.name);
82+
new_symbol.type=class_type;
83+
new_symbol.mode=ID_java;
84+
new_symbol.is_type=true;
85+
86+
symbolt *class_symbol;
87+
88+
if(symbol_table.move(new_symbol, class_symbol))
89+
{
90+
messaget message(message_handler);
91+
message.warning() <<
92+
"stub class symbol " <<
93+
new_symbol.name <<
94+
" already exists" << messaget::eom;
95+
}
96+
else
97+
{
98+
// create the class identifier etc
99+
java_root_class(*class_symbol);
100+
}
101+
}

src/java_bytecode/java_utils.h

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99

1010
#include <util/type.h>
11+
#include <util/symbol_table.h>
1112

1213
#include "java_bytecode_parse_tree.h"
1314

@@ -16,6 +17,11 @@ Author: Daniel Kroening, [email protected]
1617

1718
bool java_is_array_type(const typet &type);
1819

20+
void generate_class_stub(
21+
const irep_idt &class_name,
22+
symbol_tablet &symbol_table,
23+
message_handlert &message_handler);
24+
1925
/// Returns the number of JVM local variables (slots) taken by a local variable
2026
/// that, when translated to goto, has type \p t.
2127
unsigned java_local_variable_slots(const typet &t);

0 commit comments

Comments
 (0)