Skip to content

Commit a0883eb

Browse files
authored
Merge pull request #3087 from diffblue/type-cleanup3
do not access typet::subtype() without cast
2 parents c3b78c9 + bdfe7af commit a0883eb

18 files changed

+99
-75
lines changed

src/analyses/goto_rw.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,9 @@ void rw_range_sett::get_objects_rec(const typet &type)
616616
// TODO should recurse into various composite types
617617
if(type.id()==ID_array)
618618
{
619-
get_objects_rec(type.subtype());
620-
get_objects_rec(get_modet::READ, to_array_type(type).size());
619+
const auto &array_type = to_array_type(type);
620+
get_objects_rec(array_type.subtype());
621+
get_objects_rec(get_modet::READ, array_type.size());
621622
}
622623
}
623624

src/goto-instrument/thread_instrumentation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,7 @@ void mutex_init_instrumentation(goto_modelt &goto_model)
140140

141141
Forall_goto_functions(f_it, goto_model.goto_functions)
142142
mutex_init_instrumentation(
143-
goto_model.symbol_table, f_it->second.body, lock_type.subtype());
143+
goto_model.symbol_table,
144+
f_it->second.body,
145+
to_pointer_type(lock_type).subtype());
144146
}

src/goto-symex/auto_objects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void goto_symext::initialize_auto_object(
5757
else if(type.id()==ID_pointer)
5858
{
5959
const pointer_typet &pointer_type=to_pointer_type(type);
60-
const typet &subtype=ns.follow(type.subtype());
60+
const typet &subtype = ns.follow(pointer_type.subtype());
6161

6262
// we don't like function pointers and
6363
// we don't like void *
@@ -67,7 +67,7 @@ void goto_symext::initialize_auto_object(
6767
// could be NULL nondeterministically
6868

6969
address_of_exprt address_of_expr(
70-
make_auto_object(type.subtype(), state), pointer_type);
70+
make_auto_object(pointer_type.subtype(), state), pointer_type);
7171

7272
if_exprt rhs(
7373
side_effect_expr_nondett(bool_typet(), expr.source_location()),

src/goto-symex/goto_symex_state.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static bool check_renaming(const typet &type)
9696
return true;
9797
}
9898
else if(type.has_subtype())
99-
return check_renaming(type.subtype());
99+
return check_renaming(to_type_with_subtype(type).subtype());
100100

101101
return false;
102102
}
@@ -409,7 +409,7 @@ void goto_symex_statet::rename(
409409
{
410410
auto &address_of_expr = to_address_of_expr(expr);
411411
rename_address(address_of_expr.object(), ns, level);
412-
expr.type().subtype() = address_of_expr.object().type();
412+
to_pointer_type(expr.type()).subtype() = address_of_expr.object().type();
413413
}
414414
else
415415
{
@@ -645,7 +645,7 @@ void goto_symex_statet::rename_address(
645645

646646
rename_address(index_expr.array(), ns, level);
647647
PRECONDITION(index_expr.array().type().id() == ID_array);
648-
expr.type()=index_expr.array().type().subtype();
648+
expr.type() = to_array_type(index_expr.array().type()).subtype();
649649

650650
// the index is not an address
651651
rename(index_expr.index(), ns, level);
@@ -730,8 +730,9 @@ void goto_symex_statet::rename(
730730

731731
if(type.id()==ID_array)
732732
{
733-
rename(type.subtype(), irep_idt(), ns, level);
734-
rename(to_array_type(type).size(), ns, level);
733+
auto &array_type = to_array_type(type);
734+
rename(array_type.subtype(), irep_idt(), ns, level);
735+
rename(array_type.size(), ns, level);
735736
}
736737
else if(type.id()==ID_struct ||
737738
type.id()==ID_union ||
@@ -752,7 +753,7 @@ void goto_symex_statet::rename(
752753
}
753754
else if(type.id()==ID_pointer)
754755
{
755-
rename(type.subtype(), irep_idt(), ns, level);
756+
rename(to_pointer_type(type).subtype(), irep_idt(), ns, level);
756757
}
757758
else if(type.id() == ID_symbol_type)
758759
{
@@ -796,8 +797,9 @@ void goto_symex_statet::get_original_name(typet &type) const
796797

797798
if(type.id()==ID_array)
798799
{
799-
get_original_name(type.subtype());
800-
get_original_name(to_array_type(type).size());
800+
auto &array_type = to_array_type(type);
801+
get_original_name(array_type.subtype());
802+
get_original_name(array_type.size());
801803
}
802804
else if(type.id()==ID_struct ||
803805
type.id()==ID_union ||
@@ -814,7 +816,7 @@ void goto_symex_statet::get_original_name(typet &type) const
814816
}
815817
else if(type.id()==ID_pointer)
816818
{
817-
get_original_name(type.subtype());
819+
get_original_name(to_pointer_type(type).subtype());
818820
}
819821
}
820822

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ void goto_symext::symex_allocate(
6060
const irep_idt &mode = function_symbol->mode;
6161

6262
// is the type given?
63-
if(code.type().id()==ID_pointer && code.type().subtype().id()!=ID_empty)
63+
if(
64+
code.type().id() == ID_pointer &&
65+
to_pointer_type(code.type()).subtype().id() != ID_empty)
6466
{
65-
object_type=code.type().subtype();
67+
object_type = to_pointer_type(code.type()).subtype();
6668
}
6769
else
6870
{
@@ -190,11 +192,11 @@ void goto_symext::symex_allocate(
190192

191193
if(object_type.id()==ID_array)
192194
{
193-
index_exprt index_expr(value_symbol.type.subtype());
195+
const auto &array_type = to_array_type(object_type);
196+
index_exprt index_expr(array_type.subtype());
194197
index_expr.array()=value_symbol.symbol_expr();
195198
index_expr.index()=from_integer(0, index_type());
196-
rhs=address_of_exprt(
197-
index_expr, pointer_type(value_symbol.type.subtype()));
199+
rhs = address_of_exprt(index_expr, pointer_type(array_type.subtype()));
198200
}
199201
else
200202
{
@@ -391,6 +393,8 @@ void goto_symext::symex_cpp_new(
391393

392394
PRECONDITION(code.type().id() == ID_pointer);
393395

396+
const auto &pointer_type = to_pointer_type(code.type());
397+
394398
do_array =
395399
(code.get(ID_statement) == ID_cpp_new_array ||
396400
code.get(ID_statement) == ID_java_new_array_data);
@@ -418,19 +422,18 @@ void goto_symext::symex_cpp_new(
418422
{
419423
exprt size_arg = static_cast<const exprt &>(code.find(ID_size));
420424
clean_expr(size_arg, state, false);
421-
symbol.type = array_typet(code.type().subtype(), size_arg);
425+
symbol.type = array_typet(pointer_type.subtype(), size_arg);
422426
}
423427
else
424-
symbol.type=code.type().subtype();
428+
symbol.type = pointer_type.subtype();
425429

426430
symbol.type.set(ID_C_dynamic, true);
427431

428432
state.symbol_table.add(symbol);
429433

430434
// make symbol expression
431435

432-
exprt rhs(ID_address_of, code.type());
433-
rhs.type().subtype()=code.type().subtype();
436+
exprt rhs(ID_address_of, pointer_type);
434437

435438
if(do_array)
436439
{

src/goto-symex/symex_dereference.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,11 @@ void goto_symext::dereference_rec(
338338
exprt &object=address_of_expr.object();
339339

340340
const typet &expr_type=ns.follow(expr.type());
341-
expr=address_arithmetic(object, state, guard,
342-
expr_type.subtype().id()==ID_array);
341+
expr = address_arithmetic(
342+
object,
343+
state,
344+
guard,
345+
to_pointer_type(expr_type).subtype().id() == ID_array);
343346
}
344347
else if(expr.id()==ID_typecast)
345348
{

src/util/arith_tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool to_integer(const constant_exprt &expr, mp_integer &int_value)
7272
}
7373
else if(type_id==ID_c_bit_field)
7474
{
75-
const typet &subtype=type.subtype();
75+
const typet &subtype = to_c_bit_field_type(type).subtype();
7676
if(subtype.id()==ID_signedbv)
7777
{
7878
int_value = bv2integer(id2string(value), true);

src/util/expr_initializer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
116116
result = side_effect_expr_nondett(type, source_location);
117117
else
118118
{
119-
exprt sub_zero = expr_initializer_rec(type.subtype(), source_location);
119+
exprt sub_zero =
120+
expr_initializer_rec(to_complex_type(type).subtype(), source_location);
120121
result = complex_exprt(sub_zero, sub_zero, to_complex_type(type));
121122
}
122123

src/util/find_symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void find_symbols(kindt kind, const typet &src, find_symbols_sett &dest)
124124
src.id()!=ID_pointer)
125125
{
126126
if(src.has_subtype())
127-
find_symbols(kind, src.subtype(), dest);
127+
find_symbols(kind, to_type_with_subtype(src).subtype(), dest);
128128

129129
forall_subtypes(it, src)
130130
find_symbols(kind, *it, dest);

src/util/format_type.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ std::ostream &format_rec(std::ostream &os, const typet &type)
6060
const auto &id = type.id();
6161

6262
if(id == ID_pointer)
63-
return os << '*' << format(type.subtype());
63+
return os << '*' << format(to_pointer_type(type).subtype());
6464
else if(id == ID_array)
6565
{
6666
const auto &t = to_array_type(type);
6767
if(t.is_complete())
68-
return os << format(type.subtype()) << '[' << format(t.size()) << ']';
68+
return os << format(t.subtype()) << '[' << format(t.size()) << ']';
6969
else
70-
return os << format(type.subtype()) << "[]";
70+
return os << format(t.subtype()) << "[]";
7171
}
7272
else if(id == ID_struct)
7373
return format_rec(os, to_struct_type(type));

src/util/json_expr.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ json_objectt json(
154154
else if(type.id()==ID_c_enum_tag)
155155
{
156156
// we return the base type
157-
return json(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns, mode);
157+
return json(
158+
to_c_enum_type(ns.follow_tag(to_c_enum_tag_type(type))).subtype(),
159+
ns,
160+
mode);
158161
}
159162
else if(type.id()==ID_fixedbv)
160163
{
@@ -165,7 +168,7 @@ json_objectt json(
165168
else if(type.id()==ID_pointer)
166169
{
167170
result["name"]=json_stringt("pointer");
168-
result["subtype"]=json(type.subtype(), ns, mode);
171+
result["subtype"] = json(to_pointer_type(type).subtype(), ns, mode);
169172
}
170173
else if(type.id()==ID_bool)
171174
{
@@ -174,12 +177,12 @@ json_objectt json(
174177
else if(type.id()==ID_array)
175178
{
176179
result["name"]=json_stringt("array");
177-
result["subtype"]=json(type.subtype(), ns, mode);
180+
result["subtype"] = json(to_array_type(type).subtype(), ns, mode);
178181
}
179182
else if(type.id()==ID_vector)
180183
{
181184
result["name"]=json_stringt("vector");
182-
result["subtype"]=json(type.subtype(), ns, mode);
185+
result["subtype"] = json(to_vector_type(type).subtype(), ns, mode);
183186
result["size"]=json(to_vector_type(type).size(), ns, mode);
184187
}
185188
else if(type.id()==ID_struct)
@@ -242,9 +245,8 @@ json_objectt json(
242245
lang=std::unique_ptr<languaget>(get_default_language());
243246
}
244247

245-
const typet &underlying_type=
246-
type.id()==ID_c_bit_field?type.subtype():
247-
type;
248+
const typet &underlying_type =
249+
type.id() == ID_c_bit_field ? to_c_bit_field_type(type).subtype() : type;
248250

249251
std::string type_string;
250252
bool error=lang->from_type(underlying_type, type_string, ns);
@@ -270,7 +272,8 @@ json_objectt json(
270272
{
271273
result["name"]=json_stringt("integer");
272274
result["binary"] = json_stringt(constant_expr.get_value());
273-
result["width"]=json_numbert(type.subtype().get_string(ID_width));
275+
result["width"] =
276+
json_numbert(to_c_enum_type(type).subtype().get_string(ID_width));
274277
result["type"]=json_stringt("enum");
275278
result["data"]=json_stringt(value_string);
276279
}

src/util/pointer_offset_size.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pointer_offset_bits(const typet &type, const namespacet &ns)
120120
{
121121
if(type.id()==ID_array)
122122
{
123-
auto sub = pointer_offset_bits(type.subtype(), ns);
123+
auto sub = pointer_offset_bits(to_array_type(type).subtype(), ns);
124124
if(!sub.has_value())
125125
return {};
126126

@@ -137,7 +137,7 @@ pointer_offset_bits(const typet &type, const namespacet &ns)
137137
}
138138
else if(type.id()==ID_vector)
139139
{
140-
auto sub = pointer_offset_bits(type.subtype(), ns);
140+
auto sub = pointer_offset_bits(to_vector_type(type).subtype(), ns);
141141
if(!sub.has_value())
142142
return {};
143143

@@ -154,7 +154,7 @@ pointer_offset_bits(const typet &type, const namespacet &ns)
154154
}
155155
else if(type.id()==ID_complex)
156156
{
157-
auto sub = pointer_offset_bits(type.subtype(), ns);
157+
auto sub = pointer_offset_bits(to_complex_type(type).subtype(), ns);
158158

159159
if(sub.has_value())
160160
return (*sub) * 2;
@@ -212,7 +212,7 @@ pointer_offset_bits(const typet &type, const namespacet &ns)
212212
}
213213
else if(type.id()==ID_c_enum)
214214
{
215-
return mp_integer(to_bitvector_type(type.subtype()).get_width());
215+
return mp_integer(to_bitvector_type(to_c_enum_type(type).subtype()).get_width());
216216
}
217217
else if(type.id()==ID_c_enum_tag)
218218
{
@@ -313,12 +313,14 @@ exprt size_of_expr(
313313
{
314314
if(type.id()==ID_array)
315315
{
316-
exprt sub=size_of_expr(type.subtype(), ns);
316+
const auto &array_type = to_array_type(type);
317+
318+
exprt sub = size_of_expr(array_type.subtype(), ns);
317319
if(sub.is_nil())
318320
return nil_exprt();
319321

320322
// get size
321-
exprt size=to_array_type(type).size();
323+
exprt size = array_type.size();
322324

323325
if(size.is_nil())
324326
return nil_exprt();
@@ -333,7 +335,7 @@ exprt size_of_expr(
333335
}
334336
else if(type.id()==ID_vector)
335337
{
336-
exprt sub=size_of_expr(type.subtype(), ns);
338+
exprt sub = size_of_expr(to_vector_type(type).subtype(), ns);
337339
if(sub.is_nil())
338340
return nil_exprt();
339341

@@ -353,7 +355,7 @@ exprt size_of_expr(
353355
}
354356
else if(type.id()==ID_complex)
355357
{
356-
exprt sub=size_of_expr(type.subtype(), ns);
358+
exprt sub = size_of_expr(to_complex_type(type).subtype(), ns);
357359
if(sub.is_nil())
358360
return nil_exprt();
359361

@@ -465,7 +467,8 @@ exprt size_of_expr(
465467
}
466468
else if(type.id()==ID_c_enum)
467469
{
468-
std::size_t width=to_bitvector_type(type.subtype()).get_width();
470+
std::size_t width =
471+
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
469472
std::size_t bytes=width/8;
470473
if(bytes*8!=width)
471474
bytes++;
@@ -529,15 +532,16 @@ compute_pointer_offset(const exprt &expr, const namespacet &ns)
529532
else if(expr.id()==ID_index)
530533
{
531534
const index_exprt &index_expr=to_index_expr(expr);
532-
const typet &array_type=ns.follow(index_expr.array().type());
533535
DATA_INVARIANT(
534-
array_type.id()==ID_array,
535-
"index into array expected, found "+array_type.id_string());
536+
index_expr.array().type().id() == ID_array,
537+
"index into array expected, found " +
538+
index_expr.array().type().id_string());
536539

537540
auto o = compute_pointer_offset(index_expr.array(), ns);
538541

539542
if(o.has_value())
540543
{
544+
const auto &array_type = to_array_type(index_expr.array().type());
541545
auto sub_size = pointer_offset_size(array_type.subtype(), ns);
542546

543547
mp_integer i;

0 commit comments

Comments
 (0)