Skip to content

Commit 4aad8d1

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 4eb7d04 commit 4aad8d1

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
@@ -339,9 +339,9 @@ std::vector<typet> parse_list_types(
339339
for(const std::string &raw_type :
340340
parse_raw_list_types(src, opening_bracket, closing_bracket))
341341
{
342-
const typet new_type = java_type_from_string(raw_type, class_name_prefix);
343-
INVARIANT(new_type != nil_typet(), "Failed to parse type");
344-
type_list.push_back(new_type);
342+
auto new_type = java_type_from_string(raw_type, class_name_prefix);
343+
INVARIANT(new_type.has_value(), "Failed to parse type");
344+
type_list.push_back(std::move(*new_type));
345345
}
346346
return type_list;
347347
}
@@ -489,12 +489,12 @@ size_t find_closing_semi_colon_for_reference_type(
489489
/// \param class_name_prefix: name of class to append to generic type
490490
/// variables/parameters
491491
/// \return internal type representation for GOTO programs
492-
typet java_type_from_string(
492+
optionalt<typet> java_type_from_string(
493493
const std::string &src,
494494
const std::string &class_name_prefix)
495495
{
496496
if(src.empty())
497-
return nil_typet();
497+
return {};
498498

499499
// a java type is encoded in different ways
500500
// - a method type is encoded as '(...)R' where the parenthesis include the
@@ -538,15 +538,15 @@ typet java_type_from_string(
538538
"Cannot currently parse bounds on generic types");
539539
}
540540

541-
const typet &method_type=java_type_from_string(
542-
src.substr(closing_generic+1, std::string::npos), class_name_prefix);
541+
auto method_type = java_type_from_string(
542+
src.substr(closing_generic + 1, std::string::npos), class_name_prefix);
543543

544544
// This invariant being violated means that tkiley has not understood
545545
// part of the signature spec.
546546
// Only class and method signatures can start with a '<' and classes are
547547
// handled separately.
548548
INVARIANT(
549-
method_type.id()==ID_code,
549+
method_type.has_value() && method_type->id() == ID_code,
550550
"This should correspond to method signatures only");
551551

552552
return method_type;
@@ -555,9 +555,9 @@ typet java_type_from_string(
555555
{
556556
std::size_t e_pos=src.rfind(')');
557557
if(e_pos==std::string::npos)
558-
return nil_typet();
558+
return {};
559559

560-
typet return_type = java_type_from_string(
560+
auto return_type = java_type_from_string(
561561
std::string(src, e_pos + 1, std::string::npos), class_name_prefix);
562562

563563
std::vector<typet> param_types =
@@ -571,26 +571,25 @@ typet java_type_from_string(
571571
std::back_inserter(parameters),
572572
[](const typet &type) { return java_method_typet::parametert(type); });
573573

574-
return java_method_typet(std::move(parameters), std::move(return_type));
574+
return java_method_typet(std::move(parameters), std::move(*return_type));
575575
}
576576

577577
case '[': // array type
578578
{
579579
// If this is a reference array, we generate a plain array[reference]
580580
// with void* members, but note the real type in ID_element_type.
581581
if(src.size()<=1)
582-
return nil_typet();
582+
return {};
583583
char subtype_letter=src[1];
584-
const typet subtype=
585-
java_type_from_string(src.substr(1, std::string::npos),
586-
class_name_prefix);
584+
auto subtype = java_type_from_string(
585+
src.substr(1, std::string::npos), class_name_prefix);
587586
if(subtype_letter=='L' || // [L denotes a reference array of some sort.
588587
subtype_letter=='[' || // Array-of-arrays
589588
subtype_letter=='T') // Array of generic types
590589
subtype_letter='A';
591590
typet tmp=java_array_type(std::tolower(subtype_letter));
592-
tmp.subtype().set(ID_element_type, subtype);
593-
return tmp;
591+
tmp.subtype().set(ID_element_type, std::move(*subtype));
592+
return std::move(tmp);
594593
}
595594

596595
case 'B': return java_byte_type();
@@ -611,7 +610,7 @@ typet java_type_from_string(
611610
return java_generic_parametert(
612611
type_var_name,
613612
to_struct_tag_type(
614-
java_type_from_string("Ljava/lang/Object;").subtype()));
613+
java_type_from_string("Ljava/lang/Object;")->subtype()));
615614
}
616615
case 'L':
617616
{
@@ -627,7 +626,7 @@ typet java_type_from_string(
627626
throw unsupported_java_class_signature_exceptiont("wild card generic");
628627
}
629628
default:
630-
return nil_typet();
629+
return {};
631630
}
632631
}
633632

@@ -712,7 +711,7 @@ std::vector<typet> java_generic_type_from_string(
712711
java_generic_parametert type_var_type(
713712
type_var_name,
714713
to_struct_tag_type(
715-
java_type_from_string(bound_type, class_name).subtype()));
714+
java_type_from_string(bound_type, class_name)->subtype()));
716715

717716
types.push_back(type_var_type);
718717
signature=signature.substr(var_sep+1, std::string::npos);
@@ -734,7 +733,7 @@ static std::string slash_to_dot(const std::string &src)
734733
struct_tag_typet java_classname(const std::string &id)
735734
{
736735
if(!id.empty() && id[0]=='[')
737-
return to_struct_tag_type(java_type_from_string(id).subtype());
736+
return to_struct_tag_type(java_type_from_string(id)->subtype());
738737

739738
std::string class_name=id;
740739

@@ -888,9 +887,9 @@ void get_dependencies_from_generic_parameters(
888887
// class signature without bounds and without wildcards
889888
else if(signature.find('*') == std::string::npos)
890889
{
891-
get_dependencies_from_generic_parameters_rec(
892-
java_type_from_string(signature, erase_type_arguments(signature)),
893-
refs);
890+
auto type_from_string =
891+
java_type_from_string(signature, erase_type_arguments(signature));
892+
get_dependencies_from_generic_parameters_rec(*type_from_string, refs);
894893
}
895894
}
896895
catch(unsupported_java_class_signature_exceptiont &)
@@ -928,9 +927,9 @@ java_generic_struct_tag_typet::java_generic_struct_tag_typet(
928927
: struct_tag_typet(type)
929928
{
930929
set(ID_C_java_generic_symbol, true);
931-
const typet &base_type = java_type_from_string(base_ref, class_name_prefix);
932-
PRECONDITION(is_java_generic_type(base_type));
933-
const java_generic_typet &gen_base_type = to_java_generic_type(base_type);
930+
const auto base_type = java_type_from_string(base_ref, class_name_prefix);
931+
PRECONDITION(is_java_generic_type(*base_type));
932+
const java_generic_typet &gen_base_type = to_java_generic_type(*base_type);
934933
INVARIANT(
935934
type.get_identifier() ==
936935
to_struct_tag_type(gen_base_type.subtype()).get_identifier(),

0 commit comments

Comments
 (0)