@@ -144,6 +144,8 @@ class smv_typecheckt:public typecheckt
144144 return id;
145145 }
146146
147+ mp_integer require_integer_constant (const irept &) const ;
148+
147149 void lower_node (exprt &) const ;
148150
149151 void lower (typet &) const ;
@@ -363,6 +365,36 @@ void smv_typecheckt::instantiate(
363365
364366/* ******************************************************************\
365367
368+ Function: smv_typecheckt::require_integer_constant
369+
370+ Inputs:
371+
372+ Outputs:
373+
374+ Purpose:
375+
376+ \*******************************************************************/
377+
378+ mp_integer smv_typecheckt::require_integer_constant (const irept &irep) const
379+ {
380+ auto &as_expr = static_cast <const exprt &>(irep);
381+ if (as_expr.id () != ID_constant)
382+ {
383+ throw errort ().with_location (as_expr.source_location ())
384+ << " expected constant expression" ;
385+ }
386+
387+ if (as_expr.type ().id () != ID_integer)
388+ {
389+ throw errort ().with_location (as_expr.source_location ())
390+ << " expected integer expression" ;
391+ }
392+
393+ return numeric_cast_v<mp_integer>(to_constant_expr (as_expr));
394+ }
395+
396+ /* ******************************************************************\
397+
366398Function: smv_typecheckt::check_type
367399
368400 Inputs:
@@ -377,10 +409,8 @@ void smv_typecheckt::check_type(typet &type)
377409{
378410 if (type.id () == ID_smv_array)
379411 {
380- auto from = numeric_cast_v<mp_integer>(
381- to_constant_expr (static_cast <const exprt &>(type.find (ID_from))));
382- auto to = numeric_cast_v<mp_integer>(
383- to_constant_expr (static_cast <const exprt &>(type.find (ID_to))));
412+ auto from = require_integer_constant (type.find (ID_from));
413+ auto to = require_integer_constant (type.find (ID_to));
384414
385415 if (to < from)
386416 throw errort ().with_location (type.source_location ())
@@ -402,10 +432,8 @@ void smv_typecheckt::check_type(typet &type)
402432 }
403433 else if (type.id () == ID_smv_range)
404434 {
405- auto from = numeric_cast_v<mp_integer>(
406- to_constant_expr (static_cast <const exprt &>(type.find (ID_from))));
407- auto to = numeric_cast_v<mp_integer>(
408- to_constant_expr (static_cast <const exprt &>(type.find (ID_to))));
435+ auto from = require_integer_constant (type.find (ID_from));
436+ auto to = require_integer_constant (type.find (ID_to));
409437
410438 if (from > to)
411439 throw errort ().with_location (type.source_location ()) << " range is empty" ;
@@ -416,8 +444,7 @@ void smv_typecheckt::check_type(typet &type)
416444 }
417445 else if (type.id () == ID_smv_signed_word)
418446 {
419- auto width = numeric_cast_v<mp_integer>(
420- to_constant_expr (static_cast <const exprt &>(type.find (ID_width))));
447+ auto width = require_integer_constant (type.find (ID_width));
421448
422449 if (width < 1 )
423450 throw errort ().with_location (type.source_location ())
@@ -428,8 +455,7 @@ void smv_typecheckt::check_type(typet &type)
428455 }
429456 else if (type.id () == ID_smv_word || type.id () == ID_smv_unsigned_word)
430457 {
431- auto width = numeric_cast_v<mp_integer>(
432- to_constant_expr (static_cast <const exprt &>(type.find (ID_width))));
458+ auto width = require_integer_constant (type.find (ID_width));
433459
434460 if (width < 1 )
435461 throw errort ().with_location (type.source_location ())
0 commit comments