@@ -82,6 +82,7 @@ class smv_typecheckt:public typecheckt
8282 void convert (smv_parse_treet::modulet::itemt &);
8383 void typecheck (smv_parse_treet::modulet::itemt &);
8484 void typecheck_expr_rec (exprt &, const typet &, modet);
85+ void convert_expr_to (exprt &, const typet &dest);
8586
8687 smv_parse_treet::modulet *modulep;
8788
@@ -581,7 +582,7 @@ Function: smv_typecheckt::typecheck_expr_rec
581582
582583void smv_typecheckt::typecheck_expr_rec (
583584 exprt &expr,
584- const typet &type ,
585+ const typet &dest_type ,
585586 modet mode)
586587{
587588 const auto static nil_type = static_cast <const typet &>(get_nil_irep ());
@@ -631,12 +632,12 @@ void smv_typecheckt::typecheck_expr_rec(
631632 }
632633 else if (expr.id ()==ID_nondet_symbol)
633634 {
634- if (!type .is_nil ())
635- expr.type ()=type ;
635+ if (!dest_type .is_nil ())
636+ expr.type () = dest_type ;
636637 }
637638 else if (expr.id ()==ID_constraint_select_one)
638639 {
639- typecheck_op (expr, type , mode);
640+ typecheck_op (expr, dest_type , mode);
640641
641642 typet op_type;
642643 op_type.make_nil ();
@@ -702,23 +703,23 @@ void smv_typecheckt::typecheck_expr_rec(
702703 auto &true_case = if_expr.true_case ();
703704 auto &false_case = if_expr.false_case ();
704705 typecheck_expr_rec (if_expr.cond (), bool_typet{}, mode);
705- typecheck_expr_rec (true_case, type , mode);
706- typecheck_expr_rec (false_case, type , mode);
707- expr.type () = type ;
706+ typecheck_expr_rec (true_case, dest_type , mode);
707+ typecheck_expr_rec (false_case, dest_type , mode);
708+ expr.type () = dest_type ;
708709 }
709710 else if (expr.id ()==ID_plus || expr.id ()==ID_minus ||
710711 expr.id ()==ID_mult || expr.id ()==ID_div ||
711712 expr.id ()==ID_mod)
712713 {
713- typecheck_op (expr, type , mode);
714-
714+ typecheck_op (expr, dest_type , mode);
715+
715716 if (expr.operands ().size ()!=2 )
716717 {
717718 throw errort ().with_location (expr.find_source_location ())
718719 << " Expected two operands for " << expr.id ();
719720 }
720-
721- if (type .is_nil ())
721+
722+ if (dest_type .is_nil ())
722723 {
723724 if (expr.type ().id ()==ID_range ||
724725 expr.type ().id ()==ID_bool)
@@ -745,7 +746,7 @@ void smv_typecheckt::typecheck_expr_rec(
745746 new_range.to_type (expr.type ());
746747 }
747748 }
748- else if (type .id ()!= ID_range)
749+ else if (dest_type .id () != ID_range)
749750 {
750751 throw errort ().with_location (expr.find_source_location ())
751752 << " Expected number type for " << to_string (expr);
@@ -758,17 +759,17 @@ void smv_typecheckt::typecheck_expr_rec(
758759 const std::string &value=expr.get_string (ID_value);
759760 mp_integer int_value=string2integer (value);
760761
761- if (type .is_nil ())
762+ if (dest_type .is_nil ())
762763 {
763764 expr.type ()=typet (ID_range);
764765 expr.type ().set (ID_from, integer2string (int_value));
765766 expr.type ().set (ID_to, integer2string (int_value));
766767 }
767768 else
768769 {
769- expr.type ()=type ;
770+ expr.type () = dest_type ;
770771
771- if (type .id ()== ID_bool)
772+ if (dest_type .id () == ID_bool)
772773 {
773774 if (int_value==0 )
774775 expr=false_exprt ();
@@ -780,9 +781,9 @@ void smv_typecheckt::typecheck_expr_rec(
780781 << " expected 0 or 1 here, but got " << value;
781782 }
782783 }
783- else if (type .id ()== ID_range)
784+ else if (dest_type .id () == ID_range)
784785 {
785- smv_ranget smv_range= convert_type (type );
786+ smv_ranget smv_range = convert_type (dest_type );
786787
787788 if (int_value<smv_range.from || int_value>smv_range.to )
788789 {
@@ -800,13 +801,13 @@ void smv_typecheckt::typecheck_expr_rec(
800801 }
801802 else if (expr.type ().id ()==ID_enumeration)
802803 {
803- if (type .id ()== ID_enumeration)
804+ if (dest_type .id () == ID_enumeration)
804805 {
805806 if (to_enumeration_type (expr.type ()).elements ().empty ())
806- expr.type ()=type ;
807+ expr.type () = dest_type ;
807808 }
808809 }
809- else if (type .is_not_nil () && type!= expr.type ())
810+ else if (dest_type .is_not_nil () && dest_type != expr.type ())
810811 {
811812 // already done, but maybe need to change the type
812813 mp_integer int_value;
@@ -825,21 +826,21 @@ void smv_typecheckt::typecheck_expr_rec(
825826
826827 if (have_int_value)
827828 {
828- if (type .id ()== ID_bool)
829+ if (dest_type .id () == ID_bool)
829830 {
830831 if (int_value==0 )
831832 expr=false_exprt ();
832833 else if (int_value==1 )
833834 expr=true_exprt ();
834835 }
835- else if (type .id ()== ID_range)
836+ else if (dest_type .id () == ID_range)
836837 {
837- mp_integer from= string2integer (type .get_string (ID_from)),
838- to= string2integer (type .get_string (ID_to));
838+ mp_integer from = string2integer (dest_type .get_string (ID_from)),
839+ to = string2integer (dest_type .get_string (ID_to));
839840
840841 if (int_value>=from && int_value<=to)
841842 {
842- expr= exprt (ID_constant, type );
843+ expr = exprt (ID_constant, dest_type );
843844 expr.set (ID_value, integer2string (int_value));
844845 }
845846 }
@@ -848,7 +849,7 @@ void smv_typecheckt::typecheck_expr_rec(
848849 }
849850 else if (expr.id ()==ID_cond)
850851 {
851- if (type .is_nil ())
852+ if (dest_type .is_nil ())
852853 {
853854 bool condition=true ;
854855
@@ -870,7 +871,7 @@ void smv_typecheckt::typecheck_expr_rec(
870871 }
871872 else
872873 {
873- expr.type ()=type ;
874+ expr.type () = dest_type ;
874875
875876 bool condition=true ;
876877
@@ -942,7 +943,27 @@ void smv_typecheckt::typecheck_expr_rec(
942943 << " No type checking for " << expr.id ();
943944 }
944945
945- if (!type.is_nil () && expr.type ()!=type)
946+ if (!dest_type.is_nil ())
947+ convert_expr_to (expr, dest_type);
948+ }
949+
950+ /* ******************************************************************\
951+
952+ Function: smv_typecheckt::convert_expr_to
953+
954+ Inputs:
955+
956+ Outputs:
957+
958+ Purpose:
959+
960+ \*******************************************************************/
961+
962+ void smv_typecheckt::convert_expr_to (exprt &expr, const typet &type)
963+ {
964+ PRECONDITION (type.is_not_nil ());
965+
966+ if (expr.type () != type)
946967 {
947968 smv_ranget e=convert_type (expr.type ());
948969 smv_ranget t=convert_type (type);
0 commit comments