Skip to content

Commit 05bc82b

Browse files
committed
Verilog: consolidate code that computes width of given type
This replaces verilog_typecheck_exprt::bits_rec by an invocation to verilog_typecheck_baset::get_width_opt.
1 parent 73ecda7 commit 05bc82b

File tree

3 files changed

+41
-60
lines changed

3 files changed

+41
-60
lines changed

src/verilog/verilog_typecheck_base.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mp_integer verilog_typecheck_baset::array_offset(const array_typet &type)
157157

158158
/*******************************************************************\
159159
160-
Function: verilog_typecheck_baset::get_width
160+
Function: verilog_typecheck_baset::get_width_opt
161161
162162
Inputs:
163163
@@ -167,7 +167,8 @@ Function: verilog_typecheck_baset::get_width
167167
168168
\*******************************************************************/
169169

170-
mp_integer verilog_typecheck_baset::get_width(const typet &type)
170+
std::optional<mp_integer>
171+
verilog_typecheck_baset::get_width_opt(const typet &type)
171172
{
172173
if(type.id()==ID_bool)
173174
return 1;
@@ -178,16 +179,24 @@ mp_integer verilog_typecheck_baset::get_width(const typet &type)
178179

179180
if(type.id()==ID_array)
180181
{
181-
mp_integer element_width = get_width(to_array_type(type).element_type());
182-
return (array_size(to_array_type(type)) * element_width).to_ulong();
182+
auto element_width = get_width_opt(to_array_type(type).element_type());
183+
if(element_width.has_value())
184+
return array_size(to_array_type(type)) * element_width.value();
185+
else
186+
return {};
183187
}
184188

185189
if(type.id() == ID_struct)
186190
{
187191
// add them up
188192
mp_integer sum = 0;
189193
for(auto &component : to_struct_type(type).components())
190-
sum += get_width(component.type());
194+
{
195+
auto component_width = get_width_opt(component.type());
196+
if(!component_width.has_value())
197+
return {};
198+
sum += *component_width;
199+
}
191200
return sum;
192201
}
193202

@@ -202,8 +211,30 @@ mp_integer verilog_typecheck_baset::get_width(const typet &type)
202211
else if(type.id() == ID_verilog_time)
203212
return 64;
204213

205-
throw errort().with_location(type.source_location())
206-
<< "type `" << type.id() << "' has unknown width";
214+
return {};
215+
}
216+
217+
/*******************************************************************\
218+
219+
Function: verilog_typecheck_baset::get_width
220+
221+
Inputs:
222+
223+
Outputs:
224+
225+
Purpose:
226+
227+
\*******************************************************************/
228+
229+
mp_integer verilog_typecheck_baset::get_width(const typet &type)
230+
{
231+
auto width_opt = get_width_opt(type);
232+
233+
if(width_opt.has_value())
234+
return std::move(width_opt.value());
235+
else
236+
throw errort().with_location(type.source_location())
237+
<< "type `" << type.id() << "' has unknown width";
207238
}
208239

209240
/*******************************************************************\

src/verilog/verilog_typecheck_base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class verilog_typecheck_baset:public typecheckt
4848
{
4949
return get_width(expr.type());
5050
}
51-
mp_integer get_width(const typet &type);
51+
mp_integer get_width(const typet &);
52+
std::optional<mp_integer> get_width_opt(const typet &);
5253
mp_integer array_size(const array_typet &);
5354
mp_integer array_offset(const array_typet &);
5455
typet index_type(const array_typet &);

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Function: verilog_typecheck_exprt::bits
434434

435435
exprt verilog_typecheck_exprt::bits(const exprt &expr)
436436
{
437-
auto width_opt = bits_rec(expr.type());
437+
auto width_opt = get_width_opt(expr.type());
438438

439439
if(!width_opt.has_value())
440440
{
@@ -447,57 +447,6 @@ exprt verilog_typecheck_exprt::bits(const exprt &expr)
447447

448448
/*******************************************************************\
449449
450-
Function: verilog_typecheck_exprt::bits_rec
451-
452-
Inputs:
453-
454-
Outputs:
455-
456-
Purpose:
457-
458-
\*******************************************************************/
459-
460-
std::optional<mp_integer>
461-
verilog_typecheck_exprt::bits_rec(const typet &type) const
462-
{
463-
if(type.id() == ID_bool)
464-
return 1;
465-
else if(type.id() == ID_unsignedbv)
466-
return to_unsignedbv_type(type).get_width();
467-
else if(type.id() == ID_signedbv)
468-
return to_signedbv_type(type).get_width();
469-
else if(type.id() == ID_integer)
470-
return 32;
471-
else if(type.id() == ID_array)
472-
{
473-
auto &array_type = to_array_type(type);
474-
auto size_int =
475-
numeric_cast_v<mp_integer>(to_constant_expr(array_type.size()));
476-
auto element_bits_opt = bits_rec(array_type.element_type());
477-
if(element_bits_opt.has_value())
478-
return element_bits_opt.value() * size_int;
479-
else
480-
return {};
481-
}
482-
else if(type.id() == ID_struct)
483-
{
484-
auto &struct_type = to_struct_type(type);
485-
mp_integer sum = 0;
486-
for(auto &component : struct_type.components())
487-
{
488-
auto component_bits_opt = bits_rec(component.type());
489-
if(!component_bits_opt.has_value())
490-
return component_bits_opt.value();
491-
sum += component_bits_opt.value();
492-
}
493-
return sum;
494-
}
495-
else
496-
return {};
497-
}
498-
499-
/*******************************************************************\
500-
501450
Function: verilog_typecheck_exprt::convert_system_function
502451
503452
Inputs:

0 commit comments

Comments
 (0)