Skip to content

Commit 152dfcc

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 ab81ffc commit 152dfcc

File tree

10 files changed

+98
-111
lines changed

10 files changed

+98
-111
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 & 24 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()
@@ -548,8 +548,8 @@ void java_bytecode_parsert::get_class_refs()
548548

549549
case CONSTANT_NameAndType:
550550
{
551-
typet t=java_type_from_string(id2string(pool_entry(c.ref2).s));
552-
get_class_refs_rec(t);
551+
const auto t = java_type_from_string(id2string(pool_entry(c.ref2).s));
552+
get_class_refs_rec(*t);
553553
}
554554
break;
555555

@@ -562,59 +562,57 @@ void java_bytecode_parsert::get_class_refs()
562562
for(const auto &field : parse_tree.parsed_class.fields)
563563
{
564564
get_annotation_class_refs(field.annotations);
565-
typet field_type;
565+
566566
if(field.signature.has_value())
567567
{
568-
field_type=java_type_from_string_with_exception(
568+
typet field_type = *java_type_from_string_with_exception(
569569
field.descriptor,
570570
field.signature,
571-
"java::"+id2string(parse_tree.parsed_class.name));
571+
"java::" + id2string(parse_tree.parsed_class.name));
572572

573573
// add generic type args to class refs as dependencies, same below for
574574
// method types and entries from the local variable type table
575575
get_dependencies_from_generic_parameters(
576576
field_type, parse_tree.class_refs);
577+
get_class_refs_rec(field_type);
577578
}
578-
else
579-
field_type=java_type_from_string(field.descriptor);
580-
581-
get_class_refs_rec(field_type);
579+
else if(const auto field_type = java_type_from_string(field.descriptor))
580+
get_class_refs_rec(*field_type);
582581
}
583582

584583
for(const auto &method : parse_tree.parsed_class.methods)
585584
{
586585
get_annotation_class_refs(method.annotations);
587586
for(const auto &parameter_annotations : method.parameter_annotations)
588587
get_annotation_class_refs(parameter_annotations);
589-
typet method_type;
588+
590589
if(method.signature.has_value())
591590
{
592-
method_type=java_type_from_string_with_exception(
591+
typet method_type = *java_type_from_string_with_exception(
593592
method.descriptor,
594593
method.signature,
595-
"java::"+id2string(parse_tree.parsed_class.name));
594+
"java::" + id2string(parse_tree.parsed_class.name));
596595
get_dependencies_from_generic_parameters(
597596
method_type, parse_tree.class_refs);
597+
get_class_refs_rec(method_type);
598598
}
599-
else
600-
method_type=java_type_from_string(method.descriptor);
599+
else if(const auto method_type = java_type_from_string(method.descriptor))
600+
get_class_refs_rec(*method_type);
601601

602-
get_class_refs_rec(method_type);
603602
for(const auto &var : method.local_variable_table)
604603
{
605-
typet var_type;
606604
if(var.signature.has_value())
607605
{
608-
var_type=java_type_from_string_with_exception(
606+
typet var_type = *java_type_from_string_with_exception(
609607
var.descriptor,
610608
var.signature,
611-
"java::"+id2string(parse_tree.parsed_class.name));
609+
"java::" + id2string(parse_tree.parsed_class.name));
612610
get_dependencies_from_generic_parameters(
613611
var_type, parse_tree.class_refs);
612+
get_class_refs_rec(var_type);
614613
}
615-
else
616-
var_type=java_type_from_string(var.descriptor);
617-
get_class_refs_rec(var_type);
614+
else if(const auto var_type = java_type_from_string(var.descriptor))
615+
get_class_refs_rec(*var_type);
618616
}
619617
}
620618
}
@@ -672,8 +670,9 @@ void java_bytecode_parsert::get_annotation_value_class_refs(const exprt &value)
672670
if(const auto &symbol_expr = expr_try_dynamic_cast<symbol_exprt>(value))
673671
{
674672
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);
673+
const auto value_type = java_type_from_string(id2string(value_id));
674+
if(value_type.has_value())
675+
get_class_refs_rec(*value_type);
677676
}
678677
else if(const auto &array_expr = expr_try_dynamic_cast<array_exprt>(value))
679678
{

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
@@ -350,9 +350,9 @@ std::vector<typet> parse_list_types(
350350
for(const std::string &raw_type :
351351
parse_raw_list_types(src, opening_bracket, closing_bracket))
352352
{
353-
const typet new_type = java_type_from_string(raw_type, class_name_prefix);
354-
INVARIANT(new_type != nil_typet(), "Failed to parse type");
355-
type_list.push_back(new_type);
353+
auto new_type = java_type_from_string(raw_type, class_name_prefix);
354+
INVARIANT(new_type.has_value(), "Failed to parse type");
355+
type_list.push_back(std::move(*new_type));
356356
}
357357
return type_list;
358358
}
@@ -500,12 +500,12 @@ size_t find_closing_semi_colon_for_reference_type(
500500
/// \param class_name_prefix: name of class to append to generic type
501501
/// variables/parameters
502502
/// \return internal type representation for GOTO programs
503-
typet java_type_from_string(
503+
optionalt<typet> java_type_from_string(
504504
const std::string &src,
505505
const std::string &class_name_prefix)
506506
{
507507
if(src.empty())
508-
return nil_typet();
508+
return {};
509509

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

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

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

563563
return method_type;
@@ -566,9 +566,9 @@ typet java_type_from_string(
566566
{
567567
std::size_t e_pos=src.rfind(')');
568568
if(e_pos==std::string::npos)
569-
return nil_typet();
569+
return {};
570570

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

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

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

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

607606
case 'B': return java_byte_type();
@@ -622,7 +621,7 @@ typet java_type_from_string(
622621
return java_generic_parametert(
623622
type_var_name,
624623
to_struct_tag_type(
625-
java_type_from_string("Ljava/lang/Object;").subtype()));
624+
java_type_from_string("Ljava/lang/Object;")->subtype()));
626625
}
627626
case 'L':
628627
{
@@ -638,7 +637,7 @@ typet java_type_from_string(
638637
throw unsupported_java_class_signature_exceptiont("wild card generic");
639638
}
640639
default:
641-
return nil_typet();
640+
return {};
642641
}
643642
}
644643

@@ -723,7 +722,7 @@ std::vector<typet> java_generic_type_from_string(
723722
java_generic_parametert type_var_type(
724723
type_var_name,
725724
to_struct_tag_type(
726-
java_type_from_string(bound_type, class_name).subtype()));
725+
java_type_from_string(bound_type, class_name)->subtype()));
727726

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

750749
std::string class_name=id;
751750

@@ -899,9 +898,9 @@ void get_dependencies_from_generic_parameters(
899898
// class signature without bounds and without wildcards
900899
else if(signature.find('*') == std::string::npos)
901900
{
902-
get_dependencies_from_generic_parameters_rec(
903-
java_type_from_string(signature, erase_type_arguments(signature)),
904-
refs);
901+
auto type_from_string =
902+
java_type_from_string(signature, erase_type_arguments(signature));
903+
get_dependencies_from_generic_parameters_rec(*type_from_string, refs);
905904
}
906905
}
907906
catch(unsupported_java_class_signature_exceptiont &)
@@ -939,9 +938,9 @@ java_generic_struct_tag_typet::java_generic_struct_tag_typet(
939938
: struct_tag_typet(type)
940939
{
941940
set(ID_C_java_generic_symbol, true);
942-
const typet &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);
941+
const auto base_type = java_type_from_string(base_ref, class_name_prefix);
942+
PRECONDITION(is_java_generic_type(*base_type));
943+
const java_generic_typet &gen_base_type = to_java_generic_type(*base_type);
945944
INVARIANT(
946945
type.get_identifier() ==
947946
to_struct_tag_type(gen_base_type.subtype()).get_identifier(),

0 commit comments

Comments
 (0)