Skip to content

Commit 08276eb

Browse files
committed
Replace use of deprecated nil_typet in java_type_from_string
Use optionalt<typet> as recommended in the deprecation note.
1 parent 054ac09 commit 08276eb

File tree

10 files changed

+98
-113
lines changed

10 files changed

+98
-113
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_class.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,8 @@ void java_bytecode_convert_classt::convert(
637637
typet field_type;
638638
if(f.signature.has_value())
639639
{
640-
field_type=java_type_from_string_with_exception(
641-
f.descriptor,
642-
f.signature,
643-
id2string(class_symbol.name));
640+
field_type = *java_type_from_string_with_exception(
641+
f.descriptor, f.signature, id2string(class_symbol.name));
644642

645643
/// this is for a free type variable, e.g., a field of the form `T f;`
646644
if(is_java_generic_parameter(field_type))
@@ -670,7 +668,7 @@ void java_bytecode_convert_classt::convert(
670668
}
671669
}
672670
else
673-
field_type=java_type_from_string(f.descriptor);
671+
field_type = *java_type_from_string(f.descriptor);
674672

675673
// is this a static field?
676674
if(f.is_static)

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,26 @@ java_method_typet member_type_lazy(
259259

260260
messaget message(message_handler);
261261

262-
typet member_type_from_descriptor=java_type_from_string(descriptor);
263-
INVARIANT(member_type_from_descriptor.id()==ID_code, "Must be code type");
262+
auto member_type_from_descriptor = java_type_from_string(descriptor);
263+
INVARIANT(
264+
member_type_from_descriptor.has_value() &&
265+
member_type_from_descriptor->id() == ID_code,
266+
"Must be code type");
264267
if(signature.has_value())
265268
{
266269
try
267270
{
268-
typet member_type_from_signature=java_type_from_string(
269-
signature.value(),
270-
class_name);
271-
INVARIANT(member_type_from_signature.id()==ID_code, "Must be code type");
271+
auto member_type_from_signature =
272+
java_type_from_string(signature.value(), class_name);
273+
INVARIANT(
274+
member_type_from_signature.has_value() &&
275+
member_type_from_signature->id() == ID_code,
276+
"Must be code type");
272277
if(
273-
to_java_method_type(member_type_from_signature).parameters().size() ==
274-
to_java_method_type(member_type_from_descriptor).parameters().size())
278+
to_java_method_type(*member_type_from_signature).parameters().size() ==
279+
to_java_method_type(*member_type_from_descriptor).parameters().size())
275280
{
276-
return to_java_method_type(member_type_from_signature);
281+
return to_java_method_type(*member_type_from_signature);
277282
}
278283
else
279284
{
@@ -294,7 +299,7 @@ java_method_typet member_type_lazy(
294299
<< message.eom;
295300
}
296301
}
297-
return to_java_method_type(member_type_from_descriptor);
302+
return to_java_method_type(*member_type_from_descriptor);
298303
}
299304

300305
/// Retrieves the symbol of the lambda method associated with the given
@@ -460,24 +465,10 @@ void java_bytecode_convert_methodt::convert(
460465
if(!is_parameter(v))
461466
continue;
462467

463-
// Construct a fully qualified name for the parameter v,
464-
// e.g. my.package.ClassName.myMethodName:(II)I::anIntParam, and then a
465-
// symbol_exprt with the parameter and its type
466-
typet t;
467-
if(v.signature.has_value())
468-
{
469-
t=java_type_from_string_with_exception(
470-
v.descriptor,
471-
v.signature,
472-
id2string(class_symbol.name));
473-
}
474-
else
475-
t=java_type_from_string(v.descriptor);
476-
477468
std::ostringstream id_oss;
478469
id_oss << method_id << "::" << v.name;
479470
irep_idt identifier(id_oss.str());
480-
symbol_exprt result(identifier, t);
471+
symbol_exprt result = symbol_exprt::typeless(identifier);
481472
result.set(ID_C_base_name, v.name);
482473

483474
// Create a new variablet in the variables vector; in fact this entry will

jbmc/src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class java_bytecode_parsert:public parsert
100100

101101
const typet type_entry(u2 index)
102102
{
103-
return java_type_from_string(id2string(pool_entry(index).s));
103+
return *java_type_from_string(id2string(pool_entry(index).s));
104104
}
105105

106106
void populate_bytecode_mnemonics_table()
@@ -547,10 +547,8 @@ void java_bytecode_parsert::get_class_refs()
547547
break;
548548

549549
case CONSTANT_NameAndType:
550-
{
551-
typet t=java_type_from_string(id2string(pool_entry(c.ref2).s));
552-
get_class_refs_rec(t);
553-
}
550+
get_class_refs_rec(
551+
*java_type_from_string(id2string(pool_entry(c.ref2).s)));
554552
break;
555553

556554
default: {}
@@ -562,59 +560,57 @@ void java_bytecode_parsert::get_class_refs()
562560
for(const auto &field : parse_tree.parsed_class.fields)
563561
{
564562
get_annotation_class_refs(field.annotations);
565-
typet field_type;
563+
566564
if(field.signature.has_value())
567565
{
568-
field_type=java_type_from_string_with_exception(
566+
typet field_type = *java_type_from_string_with_exception(
569567
field.descriptor,
570568
field.signature,
571-
"java::"+id2string(parse_tree.parsed_class.name));
569+
"java::" + id2string(parse_tree.parsed_class.name));
572570

573571
// add generic type args to class refs as dependencies, same below for
574572
// method types and entries from the local variable type table
575573
get_dependencies_from_generic_parameters(
576574
field_type, parse_tree.class_refs);
575+
get_class_refs_rec(field_type);
577576
}
578-
else
579-
field_type=java_type_from_string(field.descriptor);
580-
581-
get_class_refs_rec(field_type);
577+
else if(const auto field_type = java_type_from_string(field.descriptor))
578+
get_class_refs_rec(*field_type);
582579
}
583580

584581
for(const auto &method : parse_tree.parsed_class.methods)
585582
{
586583
get_annotation_class_refs(method.annotations);
587584
for(const auto &parameter_annotations : method.parameter_annotations)
588585
get_annotation_class_refs(parameter_annotations);
589-
typet method_type;
586+
590587
if(method.signature.has_value())
591588
{
592-
method_type=java_type_from_string_with_exception(
589+
typet method_type = *java_type_from_string_with_exception(
593590
method.descriptor,
594591
method.signature,
595-
"java::"+id2string(parse_tree.parsed_class.name));
592+
"java::" + id2string(parse_tree.parsed_class.name));
596593
get_dependencies_from_generic_parameters(
597594
method_type, parse_tree.class_refs);
595+
get_class_refs_rec(method_type);
598596
}
599-
else
600-
method_type=java_type_from_string(method.descriptor);
597+
else if(const auto method_type = java_type_from_string(method.descriptor))
598+
get_class_refs_rec(*method_type);
601599

602-
get_class_refs_rec(method_type);
603600
for(const auto &var : method.local_variable_table)
604601
{
605-
typet var_type;
606602
if(var.signature.has_value())
607603
{
608-
var_type=java_type_from_string_with_exception(
604+
typet var_type = *java_type_from_string_with_exception(
609605
var.descriptor,
610606
var.signature,
611-
"java::"+id2string(parse_tree.parsed_class.name));
607+
"java::" + id2string(parse_tree.parsed_class.name));
612608
get_dependencies_from_generic_parameters(
613609
var_type, parse_tree.class_refs);
610+
get_class_refs_rec(var_type);
614611
}
615-
else
616-
var_type=java_type_from_string(var.descriptor);
617-
get_class_refs_rec(var_type);
612+
else if(const auto var_type = java_type_from_string(var.descriptor))
613+
get_class_refs_rec(*var_type);
618614
}
619615
}
620616
}
@@ -672,8 +668,9 @@ void java_bytecode_parsert::get_annotation_value_class_refs(const exprt &value)
672668
if(const auto &symbol_expr = expr_try_dynamic_cast<symbol_exprt>(value))
673669
{
674670
const irep_idt &value_id = symbol_expr->get_identifier();
675-
const typet value_type = java_type_from_string(id2string(value_id));
676-
get_class_refs_rec(value_type);
671+
const auto value_type = java_type_from_string(id2string(value_id));
672+
if(value_type.has_value())
673+
get_class_refs_rec(*value_type);
677674
}
678675
else if(const auto &array_expr = expr_try_dynamic_cast<array_exprt>(value))
679676
{

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ bool is_java_main(const symbolt &function)
267267
{
268268
bool named_main = has_suffix(id2string(function.name), JAVA_MAIN_METHOD);
269269
const java_method_typet &function_type = to_java_method_type(function.type);
270-
const typet &string_array_type = java_type_from_string("[Ljava/lang/String;");
270+
const auto string_array_type = java_type_from_string("[Ljava/lang/String;");
271271
// checks whether the function is static and has a single String[] parameter
272272
bool is_static = !function_type.has_this();
273273
// this should be implied by the signature
274274
const java_method_typet::parameterst &parameters = function_type.parameters();
275275
bool has_correct_type = function_type.return_type().id() == ID_empty &&
276276
parameters.size() == 1 &&
277-
parameters[0].type().full_eq(string_array_type);
277+
parameters[0].type().full_eq(*string_array_type);
278278
bool public_access = function_type.get(ID_access) == ID_public;
279279
return named_main && is_static && has_correct_type && public_access;
280280
}

jbmc/src/java_bytecode/java_local_variable_table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,9 @@ void java_bytecode_convert_methodt::setup_local_variables(
794794
const std::string class_name = method_name.substr(0, class_name_end);
795795

796796
const typet t = v.var.signature.has_value()
797-
? java_type_from_string_with_exception(
797+
? *java_type_from_string_with_exception(
798798
v.var.descriptor, v.var.signature, class_name)
799-
: java_type_from_string(v.var.descriptor);
799+
: *java_type_from_string(v.var.descriptor);
800800

801801
std::ostringstream id_oss;
802802
id_oss << method_id << "::" << v.var.start_pc << "::" << v.var.name;

jbmc/src/java_bytecode/java_types.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ std::vector<typet> parse_list_types(
351351
for(const std::string &raw_type :
352352
parse_raw_list_types(src, opening_bracket, closing_bracket))
353353
{
354-
const typet new_type = java_type_from_string(raw_type, class_name_prefix);
355-
INVARIANT(new_type != nil_typet(), "Failed to parse type");
356-
type_list.push_back(new_type);
354+
auto new_type = java_type_from_string(raw_type, class_name_prefix);
355+
INVARIANT(new_type.has_value(), "Failed to parse type");
356+
type_list.push_back(std::move(*new_type));
357357
}
358358
return type_list;
359359
}
@@ -501,12 +501,12 @@ size_t find_closing_semi_colon_for_reference_type(
501501
/// \param class_name_prefix: name of class to append to generic type
502502
/// variables/parameters
503503
/// \return internal type representation for GOTO programs
504-
typet java_type_from_string(
504+
optionalt<typet> java_type_from_string(
505505
const std::string &src,
506506
const std::string &class_name_prefix)
507507
{
508508
if(src.empty())
509-
return nil_typet();
509+
return {};
510510

511511
// a java type is encoded in different ways
512512
// - a method type is encoded as '(...)R' where the parenthesis include the
@@ -550,15 +550,15 @@ typet java_type_from_string(
550550
"Cannot currently parse bounds on generic types");
551551
}
552552

553-
const typet &method_type=java_type_from_string(
554-
src.substr(closing_generic+1, std::string::npos), class_name_prefix);
553+
auto method_type = java_type_from_string(
554+
src.substr(closing_generic + 1, std::string::npos), class_name_prefix);
555555

556556
// This invariant being violated means that tkiley has not understood
557557
// part of the signature spec.
558558
// Only class and method signatures can start with a '<' and classes are
559559
// handled separately.
560560
INVARIANT(
561-
method_type.id()==ID_code,
561+
method_type.has_value() && method_type->id() == ID_code,
562562
"This should correspond to method signatures only");
563563

564564
return method_type;
@@ -567,9 +567,9 @@ typet java_type_from_string(
567567
{
568568
std::size_t e_pos=src.rfind(')');
569569
if(e_pos==std::string::npos)
570-
return nil_typet();
570+
return {};
571571

572-
typet return_type = java_type_from_string(
572+
auto return_type = java_type_from_string(
573573
std::string(src, e_pos + 1, std::string::npos), class_name_prefix);
574574

575575
std::vector<typet> param_types =
@@ -583,26 +583,25 @@ typet java_type_from_string(
583583
std::back_inserter(parameters),
584584
[](const typet &type) { return java_method_typet::parametert(type); });
585585

586-
return java_method_typet(std::move(parameters), std::move(return_type));
586+
return java_method_typet(std::move(parameters), std::move(*return_type));
587587
}
588588

589589
case '[': // array type
590590
{
591591
// If this is a reference array, we generate a plain array[reference]
592592
// with void* members, but note the real type in ID_element_type.
593593
if(src.size()<=1)
594-
return nil_typet();
594+
return {};
595595
char subtype_letter=src[1];
596-
const typet subtype=
597-
java_type_from_string(src.substr(1, std::string::npos),
598-
class_name_prefix);
596+
auto subtype = java_type_from_string(
597+
src.substr(1, std::string::npos), class_name_prefix);
599598
if(subtype_letter=='L' || // [L denotes a reference array of some sort.
600599
subtype_letter=='[' || // Array-of-arrays
601600
subtype_letter=='T') // Array of generic types
602601
subtype_letter='A';
603602
typet tmp=java_array_type(std::tolower(subtype_letter));
604-
tmp.subtype().set(ID_element_type, subtype);
605-
return tmp;
603+
tmp.subtype().set(ID_element_type, std::move(*subtype));
604+
return std::move(tmp);
606605
}
607606

608607
case 'B': return java_byte_type();
@@ -623,7 +622,7 @@ typet java_type_from_string(
623622
return java_generic_parametert(
624623
type_var_name,
625624
to_struct_tag_type(
626-
java_type_from_string("Ljava/lang/Object;").subtype()));
625+
java_type_from_string("Ljava/lang/Object;")->subtype()));
627626
}
628627
case 'L':
629628
{
@@ -639,7 +638,7 @@ typet java_type_from_string(
639638
throw unsupported_java_class_signature_exceptiont("wild card generic");
640639
}
641640
default:
642-
return nil_typet();
641+
return {};
643642
}
644643
}
645644

@@ -724,7 +723,7 @@ std::vector<typet> java_generic_type_from_string(
724723
java_generic_parametert type_var_type(
725724
type_var_name,
726725
to_struct_tag_type(
727-
java_type_from_string(bound_type, class_name).subtype()));
726+
java_type_from_string(bound_type, class_name)->subtype()));
728727

729728
types.push_back(type_var_type);
730729
signature=signature.substr(var_sep+1, std::string::npos);
@@ -746,7 +745,7 @@ static std::string slash_to_dot(const std::string &src)
746745
struct_tag_typet java_classname(const std::string &id)
747746
{
748747
if(!id.empty() && id[0]=='[')
749-
return to_struct_tag_type(java_type_from_string(id).subtype());
748+
return to_struct_tag_type(java_type_from_string(id)->subtype());
750749

751750
std::string class_name=id;
752751

@@ -900,9 +899,9 @@ void get_dependencies_from_generic_parameters(
900899
// class signature without bounds and without wildcards
901900
else if(signature.find('*') == std::string::npos)
902901
{
903-
get_dependencies_from_generic_parameters_rec(
904-
java_type_from_string(signature, erase_type_arguments(signature)),
905-
refs);
902+
auto type_from_string =
903+
java_type_from_string(signature, erase_type_arguments(signature));
904+
get_dependencies_from_generic_parameters_rec(*type_from_string, refs);
906905
}
907906
}
908907
catch(unsupported_java_class_signature_exceptiont &)
@@ -940,9 +939,9 @@ java_generic_struct_tag_typet::java_generic_struct_tag_typet(
940939
: struct_tag_typet(type)
941940
{
942941
set(ID_C_java_generic_symbol, true);
943-
const typet &base_type = java_type_from_string(base_ref, class_name_prefix);
944-
PRECONDITION(is_java_generic_type(base_type));
945-
const java_generic_typet &gen_base_type = to_java_generic_type(base_type);
942+
const auto base_type = java_type_from_string(base_ref, class_name_prefix);
943+
PRECONDITION(is_java_generic_type(*base_type));
944+
const java_generic_typet &gen_base_type = to_java_generic_type(*base_type);
946945
INVARIANT(
947946
type.get_identifier() ==
948947
to_struct_tag_type(gen_base_type.subtype()).get_identifier(),

0 commit comments

Comments
 (0)