Skip to content

Commit 1c12543

Browse files
authored
Merge pull request #3623 from tautschnig/struct-union-constructor
Add initializer list constructor to struct/union typet and cleanup API use [blocks: #3597]
2 parents fbed7f1 + ac4ae98 commit 1c12543

18 files changed

+77
-94
lines changed

jbmc/src/java_bytecode/java_root_class.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ void java_root_class(symbolt &class_symbol)
2525

2626
{
2727
// the class identifier is used for stuff such as 'instanceof'
28-
struct_typet::componentt component;
29-
component.set_name("@class_identifier");
28+
struct_typet::componentt component("@class_identifier", string_typet());
3029
component.set_pretty_name("@class_identifier");
31-
component.type()=string_typet();
3230

3331
// add at the beginning
3432
components.insert(components.begin(), component);

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,9 @@ java_string_library_preprocesst::get_primitive_value_of_object(
12571257
const auto maybe_symbol =
12581258
symbol_table.lookup(object_type->get_identifier()))
12591259
{
1260-
struct_typet struct_type=to_struct_type(maybe_symbol->type);
1260+
const struct_typet &struct_type = to_struct_type(maybe_symbol->type);
12611261
// Check that the type has a value field
1262-
const struct_union_typet::componentt value_comp=
1262+
const struct_union_typet::componentt &value_comp =
12631263
struct_type.get_component("value");
12641264
if(!value_comp.is_nil())
12651265
{
@@ -1457,17 +1457,16 @@ code_blockt java_string_library_preprocesst::make_string_format_code(
14571457
// The argument can be:
14581458
// a string, an integer, a floating point, a character, a boolean,
14591459
// an object of which we take the hash code, or a date/time
1460-
struct_typet structured_type;
1460+
struct_typet structured_type({{"string_expr", refined_string_type},
1461+
{ID_int, java_int_type()},
1462+
{ID_float, java_float_type()},
1463+
{ID_char, java_char_type()},
1464+
{ID_boolean, java_boolean_type()},
1465+
// TODO: hash_code not implemented for now
1466+
{"hashcode", java_int_type()},
1467+
// TODO: date_time type not implemented for now
1468+
{"date_time", java_int_type()}});
14611469
structured_type.set_tag(CPROVER_PREFIX "string_formatter_variant");
1462-
structured_type.components().emplace_back("string_expr", refined_string_type);
1463-
structured_type.components().emplace_back(ID_int, java_int_type());
1464-
structured_type.components().emplace_back(ID_float, java_float_type());
1465-
structured_type.components().emplace_back(ID_char, java_char_type());
1466-
structured_type.components().emplace_back(ID_boolean, java_boolean_type());
1467-
// TODO: hash_code not implemented for now
1468-
structured_type.components().emplace_back("hashcode", java_int_type());
1469-
// TODO: date_time type not implemented for now
1470-
structured_type.components().emplace_back("date_time", java_int_type());
14711470

14721471
// We will process arguments so that each is converted to a `struct_exprt`
14731472
// containing each possible type used in format specifiers.

jbmc/src/java_bytecode/java_utils.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,8 @@ void java_add_components_to_class(
239239
PRECONDITION(class_symbol.type.id()==ID_struct);
240240
struct_typet &struct_type=to_struct_type(class_symbol.type);
241241
struct_typet::componentst &components=struct_type.components();
242-
243-
for(const struct_union_typet::componentt &component : components_to_add)
244-
{
245-
components.push_back(component);
246-
}
242+
components.insert(
243+
components.end(), components_to_add.begin(), components_to_add.end());
247244
}
248245

249246
/// Declare a function with the given name and type.

jbmc/unit/solvers/refinement/string_refinement/dependency_graph.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ typet length_type()
3030
/// Make a struct with a pointer content and an integer length
3131
struct_exprt make_string_argument(std::string name)
3232
{
33-
struct_typet type;
3433
const array_typet char_array(char_type(), infinity_exprt(length_type()));
35-
type.components().emplace_back("length", length_type());
36-
type.components().emplace_back("content", pointer_type(char_array));
34+
struct_typet type(
35+
{{"length", length_type()}, {"content", pointer_type(char_array)}});
3736

38-
const symbol_exprt length(name + "_length", length_type());
39-
const symbol_exprt content(name + "_content", pointer_type(java_char_type()));
40-
return struct_exprt({length, content}, type);
37+
symbol_exprt length(name + "_length", length_type());
38+
symbol_exprt content(name + "_content", pointer_type(java_char_type()));
39+
return struct_exprt({std::move(length), std::move(content)}, std::move(type));
4140
}
4241

4342
SCENARIO("dependency_graph", "[core][solvers][refinement][string_refinement]")

jbmc/unit/solvers/refinement/string_refinement/string_symbol_resolution.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ SCENARIO("string_identifiers_resolution_from_equations",
3636
constant_exprt e("e", string_typet());
3737
constant_exprt f("f", string_typet());
3838

39-
struct_typet struct_type;
40-
struct_type.components().emplace_back("str1", string_typet());
41-
struct_type.components().emplace_back("str2", string_typet());
39+
struct_typet struct_type(
40+
{{"str1", string_typet()}, {"str2", string_typet()}});
4241
struct_exprt struct_expr({a, f}, struct_type);
4342
symbol_exprt symbol_struct("sym_struct", struct_type);
4443

jbmc/unit/util/has_subtype.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ SCENARIO("has_subtype", "[core][utils][has_subtype]")
3737

3838
GIVEN("a struct with char and int fields")
3939
{
40-
struct_typet struct_type;
41-
struct_type.components().emplace_back("char_field", char_type);
42-
struct_type.components().emplace_back("int_field", int_type);
40+
struct_typet struct_type(
41+
{{"char_field", char_type}, {"int_field", int_type}});
4342
THEN("char and int are subtypes")
4443
{
4544
REQUIRE(has_subtype(struct_type, is_type(char_type), ns));
@@ -67,9 +66,7 @@ SCENARIO("has_subtype", "[core][utils][has_subtype]")
6766
GIVEN("a recursive struct definition")
6867
{
6968
struct_tag_typet struct_tag("A-struct");
70-
struct_typet::componentt comp("ptr", pointer_type(struct_tag));
71-
struct_typet struct_type;
72-
struct_type.components().push_back(comp);
69+
struct_typet struct_type({{"ptr", pointer_type(struct_tag)}});
7370

7471
symbolt s;
7572
s.type = struct_type;

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
15381538
expr.get(ID_component_name);
15391539

15401540
// first try to find directly
1541-
struct_union_typet::componentt component=
1541+
const struct_union_typet::componentt &component =
15421542
struct_union_type.get_component(component_name);
15431543

15441544
// if that fails, search the anonymous members

src/ansi-c/expr2c.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,8 +1571,7 @@ std::string expr2ct::convert_member(
15711571

15721572
if(component_name!="")
15731573
{
1574-
const exprt comp_expr=
1575-
struct_union_type.get_component(component_name);
1574+
const exprt &comp_expr = struct_union_type.get_component(component_name);
15761575

15771576
if(comp_expr.is_nil())
15781577
return convert_norep(src, precedence);
@@ -1590,8 +1589,7 @@ std::string expr2ct::convert_member(
15901589
if(n>=struct_union_type.components().size())
15911590
return convert_norep(src, precedence);
15921591

1593-
const exprt comp_expr=
1594-
struct_union_type.components()[n];
1592+
const exprt &comp_expr = struct_union_type.components()[n];
15951593

15961594
dest+=comp_expr.get_string(ID_pretty_name);
15971595

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,14 +1699,15 @@ void cpp_typecheckt::make_ptr_typecast(
16991699
assert(src_type.id()== ID_pointer);
17001700
assert(dest_type.id()== ID_pointer);
17011701

1702-
struct_typet src_struct =
1703-
to_struct_type(static_cast<const typet&>(follow(src_type.subtype())));
1702+
const struct_typet &src_struct =
1703+
to_struct_type(static_cast<const typet &>(follow(src_type.subtype())));
17041704

1705-
struct_typet dest_struct =
1706-
to_struct_type(static_cast<const typet&>(follow(dest_type.subtype())));
1705+
const struct_typet &dest_struct =
1706+
to_struct_type(static_cast<const typet &>(follow(dest_type.subtype())));
17071707

1708-
assert(subtype_typecast(src_struct, dest_struct) ||
1709-
subtype_typecast(dest_struct, src_struct));
1708+
PRECONDITION(
1709+
subtype_typecast(src_struct, dest_struct) ||
1710+
subtype_typecast(dest_struct, src_struct));
17101711

17111712
expr.make_typecast(dest_type);
17121713
}

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,10 @@ bool cpp_typecheckt::standard_conversion_pointer_to_member(
619619
return true;
620620
}
621621

622-
struct_typet from_struct = to_struct_type(
622+
const struct_typet &from_struct = to_struct_type(
623623
follow(static_cast<const typet &>(expr.type().find(ID_to_member))));
624624

625-
struct_typet to_struct =
625+
const struct_typet &to_struct =
626626
to_struct_type(follow(static_cast<const typet &>(type.find(ID_to_member))));
627627

628628
if(subtype_typecast(to_struct, from_struct))
@@ -1891,8 +1891,8 @@ bool cpp_typecheckt::static_typecast(
18911891
if(!qual_to.is_subset_of(qual_from))
18921892
return false;
18931893

1894-
struct_typet from_struct=to_struct_type(from);
1895-
struct_typet subto_struct=to_struct_type(subto);
1894+
const struct_typet &from_struct = to_struct_type(from);
1895+
const struct_typet &subto_struct = to_struct_type(subto);
18961896

18971897
if(subtype_typecast(subto_struct, from_struct))
18981898
{
@@ -1975,8 +1975,8 @@ bool cpp_typecheckt::static_typecast(
19751975
return false;
19761976
}
19771977

1978-
struct_typet from_struct=to_struct_type(from);
1979-
struct_typet to_struct=to_struct_type(to);
1978+
const struct_typet &from_struct = to_struct_type(from);
1979+
const struct_typet &to_struct = to_struct_type(to);
19801980
if(subtype_typecast(to_struct, from_struct))
19811981
{
19821982
make_ptr_typecast(e, type);
@@ -1994,10 +1994,10 @@ bool cpp_typecheckt::static_typecast(
19941994
if(type.subtype()!=e.type().subtype())
19951995
return false;
19961996

1997-
struct_typet from_struct = to_struct_type(
1997+
const struct_typet &from_struct = to_struct_type(
19981998
follow(static_cast<const typet &>(e.type().find(ID_to_member))));
19991999

2000-
struct_typet to_struct = to_struct_type(
2000+
const struct_typet &to_struct = to_struct_type(
20012001
follow(static_cast<const typet &>(type.find(ID_to_member))));
20022002

20032003
if(subtype_typecast(from_struct, to_struct))
@@ -2014,7 +2014,7 @@ bool cpp_typecheckt::static_typecast(
20142014
if(type.subtype() != e.type().subtype())
20152015
return false;
20162016

2017-
struct_typet from_struct = to_struct_type(
2017+
const struct_typet &from_struct = to_struct_type(
20182018
follow(static_cast<const typet &>(e.type().find(ID_to_member))));
20192019

20202020
new_expr = e;

0 commit comments

Comments
 (0)