Skip to content

Commit ada4475

Browse files
Replace map to pair with a more specialized type
Requested in code review
1 parent 0ac4d28 commit ada4475

5 files changed

+70
-24
lines changed

src/java_bytecode/ci_lazy_methods.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,16 @@ bool ci_lazy_methodst::operator()(
140140
{
141141
if(!methods_already_populated.insert(mname).second)
142142
continue;
143-
auto findit = method_bytecode.find(mname);
144-
if(findit == method_bytecode.end())
143+
method_bytecodet::opt_reft cmb = method_bytecode.get(mname);
144+
if(!cmb)
145145
{
146146
debug() << "Skip " << mname << eom;
147147
continue;
148148
}
149149
debug() << "CI lazy methods: elaborate " << mname << eom;
150-
const auto &parsed_method=findit->second;
151150
method_converter(
152-
symbol_table.lookup_ref(parsed_method.first),
153-
*parsed_method.second,
151+
symbol_table.lookup_ref(cmb->get().class_id),
152+
cmb->get().method,
154153
// Note this wraps *references* to method_worklist2 & needed_classes
155154
ci_lazy_methods_neededt(
156155
method_worklist2, needed_classes, symbol_table));
@@ -190,7 +189,7 @@ bool ci_lazy_methodst::operator()(
190189
if(sym.second.is_static_lifetime)
191190
continue;
192191
if(
193-
method_bytecode.count(sym.first) &&
192+
method_bytecode.contains_method(sym.first) &&
194193
!methods_already_populated.count(sym.first))
195194
{
196195
continue;

src/java_bytecode/ci_lazy_methods.h

+57-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,63 @@
2626

2727
class java_string_library_preprocesst;
2828

29-
// Pair of class id and methodt
30-
typedef std::pair<irep_idt, const java_bytecode_parse_treet::methodt *>
31-
class_and_bytecodet;
32-
// Map from method id to class_and_bytecodet
33-
typedef std::map<irep_idt, class_and_bytecodet> method_bytecodet;
29+
// Map from method id to class_method_and_bytecodet
30+
class method_bytecodet
31+
{
32+
public:
33+
/// Pair of class id and methodt
34+
struct class_method_and_bytecodet
35+
{
36+
irep_idt class_id;
37+
irep_idt method_id;
38+
const java_bytecode_parse_treet::methodt &method;
39+
};
40+
41+
typedef optionalt<std::reference_wrapper<const class_method_and_bytecodet>>
42+
opt_reft;
43+
44+
private:
45+
typedef std::map<irep_idt, class_method_and_bytecodet> mapt;
46+
mapt map;
47+
48+
public:
49+
bool contains_method(const irep_idt &method_id) const
50+
{
51+
return map.count(method_id) != 0;
52+
}
53+
54+
void add(const class_method_and_bytecodet &method_class_and_bytecode)
55+
{
56+
map.emplace(
57+
std::make_pair(
58+
method_class_and_bytecode.method_id, method_class_and_bytecode));
59+
}
60+
61+
void add(
62+
const irep_idt &class_id,
63+
const irep_idt &method_id,
64+
const java_bytecode_parse_treet::methodt &method)
65+
{
66+
add(class_method_and_bytecodet{class_id, method_id, method});
67+
}
68+
69+
mapt::const_iterator begin() const
70+
{
71+
return map.begin();
72+
}
73+
mapt::const_iterator end() const
74+
{
75+
return map.end();
76+
}
77+
78+
opt_reft get(const irep_idt &method_id)
79+
{
80+
const auto it = map.find(method_id);
81+
if(it == map.end())
82+
return opt_reft();
83+
return std::cref(it->second);
84+
}
85+
};
3486

3587
typedef std::function<void(
3688
const symbolt &,

src/java_bytecode/java_bytecode_convert_class.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ void java_bytecode_convert_classt::convert(const classt &c)
205205
method,
206206
symbol_table,
207207
get_message_handler());
208-
method_bytecode[method_identifier] =
209-
std::make_pair(qualified_classname, &method);
208+
method_bytecode.add(qualified_classname, method_identifier, method);
210209
}
211210

212211
// is this a root class?

src/java_bytecode/java_bytecode_language.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ bool java_bytecode_languaget::typecheck(
251251
for(const auto &method_sig : method_bytecode)
252252
{
253253
java_bytecode_convert_method(
254-
symbol_table.lookup_ref(method_sig.second.first),
255-
*method_sig.second.second,
254+
symbol_table.lookup_ref(method_sig.second.class_id),
255+
method_sig.second.method,
256256
symbol_table,
257257
get_message_handler(),
258258
max_user_array_length,
@@ -368,10 +368,11 @@ void java_bytecode_languaget::convert_lazy_method(
368368
const irep_idt &function_id,
369369
symbol_tablet &symbol_table)
370370
{
371-
const auto &lazy_method_entry = method_bytecode.at(function_id);
371+
const method_bytecodet::class_method_and_bytecodet &cmb =
372+
*method_bytecode.get(function_id);
372373
java_bytecode_convert_method(
373-
symbol_table.lookup_ref(lazy_method_entry.first),
374-
*lazy_method_entry.second,
374+
symbol_table.lookup_ref(cmb.class_id),
375+
cmb.method,
375376
symbol_table,
376377
get_message_handler(),
377378
max_user_array_length,

src/java_bytecode/java_bytecode_language.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/cmdline.h>
1717
#include <util/make_unique.h>
1818

19+
#include "ci_lazy_methods.h"
1920
#include "java_class_loader.h"
2021
#include "java_string_library_preprocess.h"
2122

@@ -81,12 +82,6 @@ struct object_factory_parameterst final
8182
bool string_printable = false;
8283
};
8384

84-
// Pair of class id and methodt
85-
typedef std::pair<irep_idt, const java_bytecode_parse_treet::methodt *>
86-
class_and_bytecodet;
87-
// Map from method id to class_and_bytecodet
88-
typedef std::map<irep_idt, class_and_bytecodet> method_bytecodet;
89-
9085
class java_bytecode_languaget:public languaget
9186
{
9287
public:

0 commit comments

Comments
 (0)