Skip to content

Commit 5bc48f8

Browse files
author
thk123
committed
Use optional to indicate if an inherited component is not found
Provides more natural syntax.
1 parent c3e4cbd commit 5bc48f8

8 files changed

+50
-80
lines changed

jbmc/src/java_bytecode/ci_lazy_methods.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,10 @@ irep_idt ci_lazy_methodst::get_virtual_method_target(
548548
return irep_idt();
549549

550550
resolve_inherited_componentt call_resolver(symbol_table, class_hierarchy);
551-
const resolve_inherited_componentt::inherited_componentt resolved_call =
552-
call_resolver(classname, call_basename, false);
551+
const auto resolved_call = call_resolver(classname, call_basename, false);
553552

554-
if(resolved_call.is_valid())
555-
return resolved_call.get_full_component_identifier();
553+
if(resolved_call)
554+
return resolved_call->get_full_component_identifier();
556555
else
557556
return irep_idt();
558557
}

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,14 +3147,9 @@ bool java_bytecode_convert_methodt::is_method_inherited(
31473147
const irep_idt &classname,
31483148
const irep_idt &methodid) const
31493149
{
3150-
resolve_inherited_componentt::inherited_componentt inherited_method =
3151-
get_inherited_component(
3152-
classname,
3153-
methodid,
3154-
symbol_table,
3155-
class_hierarchy,
3156-
false);
3157-
return inherited_method.is_valid();
3150+
const auto inherited_method = get_inherited_component(
3151+
classname, methodid, symbol_table, class_hierarchy, false);
3152+
return inherited_method.has_value();
31583153
}
31593154

31603155
/// Get static field identifier referred to by `class_identifier.component_name`
@@ -3166,18 +3161,13 @@ irep_idt java_bytecode_convert_methodt::get_static_field(
31663161
const irep_idt &class_identifier,
31673162
const irep_idt &component_name) const
31683163
{
3169-
resolve_inherited_componentt::inherited_componentt inherited_method =
3170-
get_inherited_component(
3171-
class_identifier,
3172-
component_name,
3173-
symbol_table,
3174-
class_hierarchy,
3175-
true);
3164+
const auto inherited_method = get_inherited_component(
3165+
class_identifier, component_name, symbol_table, class_hierarchy, true);
31763166

31773167
INVARIANT(
3178-
inherited_method.is_valid(), "static field should be in symbol table");
3168+
inherited_method.has_value(), "static field should be in symbol table");
31793169

3180-
return inherited_method.get_full_component_identifier();
3170+
return inherited_method->get_full_component_identifier();
31813171
}
31823172

31833173
/// Create temporary variables if a write instruction can have undesired side-

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,9 @@ static void create_stub_global_symbols(
591591

592592
// The final 'true' parameter here includes interfaces, as they can
593593
// define static fields.
594-
resolve_inherited_componentt::inherited_componentt referred_component =
595-
get_inherited_component(
596-
class_id,
597-
component,
598-
symbol_table,
599-
class_hierarchy,
600-
true);
601-
if(!referred_component.is_valid())
594+
const auto referred_component = get_inherited_component(
595+
class_id, component, symbol_table, class_hierarchy, true);
596+
if(!referred_component)
602597
{
603598
// Create a new stub global on an arbitrary incomplete ancestor of the
604599
// class that was referred to. This is just a guess, but we have no

jbmc/src/java_bytecode/java_utils.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ std::string pretty_print_java_type(const std::string &fqn_java_type)
330330
/// ancestors including interfaces, rather than just parents.
331331
/// \return the concrete component referred to if any is found, or an invalid
332332
/// resolve_inherited_componentt::inherited_componentt otherwise.
333-
resolve_inherited_componentt::inherited_componentt get_inherited_component(
333+
optionalt<resolve_inherited_componentt::inherited_componentt>
334+
get_inherited_component(
334335
const irep_idt &component_class_id,
335336
const irep_idt &component_name,
336337
const symbol_tablet &symbol_table,
@@ -339,22 +340,22 @@ resolve_inherited_componentt::inherited_componentt get_inherited_component(
339340
{
340341
resolve_inherited_componentt component_resolver(
341342
symbol_table, class_hierarchy);
342-
const resolve_inherited_componentt::inherited_componentt resolved_component =
343+
const auto resolved_component =
343344
component_resolver(component_class_id, component_name, include_interfaces);
344345

345346
// resolved_component is a pair (class-name, component-name) found by walking
346347
// the chain of class inheritance (not interfaces!) and stopping on the first
347348
// class that contains a component of equal name and type to `component_name`
348349

349-
if(resolved_component.is_valid())
350+
if(resolved_component)
350351
{
351352
// Directly defined on the class referred to?
352-
if(component_class_id == resolved_component.get_class_identifier())
353-
return resolved_component;
353+
if(component_class_id == resolved_component->get_class_identifier())
354+
return *resolved_component;
354355

355356
// No, may be inherited from some parent class; check it is visible:
356-
const symbolt &component_symbol=
357-
*symbol_table.lookup(resolved_component.get_full_component_identifier());
357+
const symbolt &component_symbol =
358+
*symbol_table.lookup(resolved_component->get_full_component_identifier());
358359

359360
irep_idt access = component_symbol.type.get(ID_access);
360361
if(access.empty())
@@ -363,7 +364,7 @@ resolve_inherited_componentt::inherited_componentt get_inherited_component(
363364
if(access==ID_public || access==ID_protected)
364365
{
365366
// since the component is public, it is inherited
366-
return resolved_component;
367+
return *resolved_component;
367368
}
368369

369370
// components with the default access modifier are only
@@ -372,14 +373,12 @@ resolve_inherited_componentt::inherited_componentt get_inherited_component(
372373
{
373374
const std::string &class_package=
374375
java_class_to_package(id2string(component_class_id));
375-
const std::string &component_package=
376-
java_class_to_package(
377-
id2string(
378-
resolved_component.get_class_identifier()));
376+
const std::string &component_package = java_class_to_package(
377+
id2string(resolved_component->get_class_identifier()));
379378
if(component_package == class_package)
380-
return resolved_component;
379+
return *resolved_component;
381380
else
382-
return resolve_inherited_componentt::inherited_componentt();
381+
return {};
383382
}
384383

385384
if(access==ID_private)
@@ -391,14 +390,14 @@ resolve_inherited_componentt::inherited_componentt get_inherited_component(
391390
// to `classname`, a component can only become "more accessible". So, if
392391
// the last occurrence is private, all others before must be private as
393392
// well, and none is inherited in `classname`.
394-
return resolve_inherited_componentt::inherited_componentt();
393+
return {};
395394
}
396395

397396
UNREACHABLE; // Unexpected access modifier
398397
}
399398
else
400399
{
401-
return resolve_inherited_componentt::inherited_componentt();
400+
return {};
402401
}
403402
}
404403

jbmc/src/java_bytecode/java_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ irep_idt strip_java_namespace_prefix(const irep_idt &to_strip);
9797

9898
std::string pretty_print_java_type(const std::string &fqn_java_type);
9999

100-
resolve_inherited_componentt::inherited_componentt get_inherited_component(
100+
optionalt<resolve_inherited_componentt::inherited_componentt>
101+
get_inherited_component(
101102
const irep_idt &component_class_id,
102103
const irep_idt &component_name,
103104
const symbol_tablet &symbol_table,

src/goto-programs/remove_virtual_functions.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class remove_virtual_functionst
4848
goto_programt &goto_program,
4949
goto_programt::targett target);
5050
typedef std::function<
51-
resolve_inherited_componentt::inherited_componentt(
51+
optionalt<resolve_inherited_componentt::inherited_componentt>(
5252
const irep_idt &,
5353
const irep_idt &)>
5454
function_call_resolvert;
@@ -399,18 +399,16 @@ void remove_virtual_functionst::get_child_functions_rec(
399399
}
400400
if(!function.symbol_expr.has_value())
401401
{
402-
const resolve_inherited_componentt::inherited_componentt
403-
&resolved_call = resolve_function_call(child, component_name);
404-
if(resolved_call.is_valid())
402+
const auto resolved_call = resolve_function_call(child, component_name);
403+
if(resolved_call)
405404
{
406-
function.class_id = resolved_call.get_class_identifier();
407-
const symbolt &called_symbol =
408-
symbol_table.lookup_ref(
409-
resolved_call.get_full_component_identifier());
405+
function.class_id = resolved_call->get_class_identifier();
406+
const symbolt &called_symbol = symbol_table.lookup_ref(
407+
resolved_call->get_full_component_identifier());
410408

411409
function.symbol_expr = called_symbol.symbol_expr();
412410
function.symbol_expr->set(
413-
ID_C_class, resolved_call.get_class_identifier());
411+
ID_C_class, resolved_call->get_class_identifier());
414412
}
415413
}
416414
functions.push_back(function);
@@ -449,22 +447,22 @@ void remove_virtual_functionst::get_functions(
449447
return get_virtual_call_target(class_id, function_name, false);
450448
};
451449

452-
const resolve_inherited_componentt::inherited_componentt
453-
&resolved_call = get_virtual_call_target(class_id, function_name, false);
450+
const auto resolved_call =
451+
get_virtual_call_target(class_id, function_name, false);
454452

455453
// might be an abstract function
456454
dispatch_table_entryt root_function(class_id);
457455

458-
if(resolved_call.is_valid())
456+
if(resolved_call)
459457
{
460-
root_function.class_id=resolved_call.get_class_identifier();
458+
root_function.class_id = resolved_call->get_class_identifier();
461459

462460
const symbolt &called_symbol =
463-
symbol_table.lookup_ref(resolved_call.get_full_component_identifier());
461+
symbol_table.lookup_ref(resolved_call->get_full_component_identifier());
464462

465463
root_function.symbol_expr=called_symbol.symbol_expr();
466464
root_function.symbol_expr->set(
467-
ID_C_class, resolved_call.get_class_identifier());
465+
ID_C_class, resolved_call->get_class_identifier());
468466
}
469467

470468
// iterate over all children, transitively

src/goto-programs/resolve_inherited_component.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ resolve_inherited_componentt::resolve_inherited_componentt(
3434
/// \param include_interfaces: If true, consider inheritance from interfaces
3535
/// (parent types other than the first listed)
3636
/// \return The concrete component that has been resolved
37-
resolve_inherited_componentt::inherited_componentt
38-
resolve_inherited_componentt::operator()(
39-
const irep_idt &class_id,
40-
const irep_idt &component_name,
41-
bool include_interfaces)
37+
optionalt<resolve_inherited_componentt::inherited_componentt>
38+
resolve_inherited_componentt::operator()(
39+
const irep_idt &class_id,
40+
const irep_idt &component_name,
41+
bool include_interfaces)
4242
{
4343
PRECONDITION(!class_id.empty());
4444
PRECONDITION(!component_name.empty());
@@ -76,7 +76,7 @@ resolve_inherited_componentt::inherited_componentt
7676
}
7777
}
7878

79-
return inherited_componentt();
79+
return {};
8080
}
8181

8282
/// Build a component name as found in a GOTO symbol table equivalent to the
@@ -102,10 +102,3 @@ irep_idt resolve_inherited_componentt::inherited_componentt::
102102
return resolve_inherited_componentt::build_full_component_identifier(
103103
class_identifier, component_identifier);
104104
}
105-
106-
/// Use to check if this inherited_componentt has been fully constructed.
107-
/// \return True if this represents a real concrete component
108-
bool resolve_inherited_componentt::inherited_componentt::is_valid() const
109-
{
110-
return !class_identifier.empty();
111-
}

src/goto-programs/resolve_inherited_component.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class resolve_inherited_componentt
2727
class inherited_componentt
2828
{
2929
public:
30-
inherited_componentt()
31-
{}
32-
3330
inherited_componentt(
3431
const irep_idt &class_id, const irep_idt &component_id):
3532
class_identifier(class_id),
@@ -43,14 +40,12 @@ class resolve_inherited_componentt
4340
return class_identifier;
4441
}
4542

46-
bool is_valid() const;
47-
4843
private:
4944
irep_idt class_identifier;
5045
irep_idt component_identifier;
5146
};
5247

53-
inherited_componentt operator()(
48+
optionalt<inherited_componentt> operator()(
5449
const irep_idt &class_id,
5550
const irep_idt &component_name,
5651
bool include_interfaces);

0 commit comments

Comments
 (0)