@@ -739,6 +739,15 @@ void c_typecastt::do_typecast(exprt &expr, const typet &dest_type)
739739
740740 if (src_type.id ()==ID_array)
741741 {
742+ // This is the promotion from an array
743+ // to a pointer to the first element.
744+ auto error_opt = check_address_can_be_taken (expr.type ());
745+ if (error_opt.has_value ())
746+ {
747+ errors.push_back (error_opt.value ());
748+ return ;
749+ }
750+
742751 index_exprt index (expr, from_integer (0 , c_index_type ()));
743752 expr = typecast_exprt::conditional_cast (address_of_exprt (index), dest_type);
744753 return ;
@@ -765,3 +774,31 @@ void c_typecastt::do_typecast(exprt &expr, const typet &dest_type)
765774 }
766775 }
767776}
777+
778+ optionalt<std::string>
779+ c_typecastt::check_address_can_be_taken (const typet &type)
780+ {
781+ if (type.id () == ID_c_bit_field)
782+ return std::string (" cannot take address of a bit field" );
783+
784+ if (type.id () == ID_bool)
785+ return std::string (" cannot take address of a proper Boolean" );
786+
787+ if (can_cast_type<bitvector_typet>(type))
788+ {
789+ // The width of the bitvector must be a multiple of CHAR_BIT.
790+ auto width = to_bitvector_type (type).get_width ();
791+ if (width % config.ansi_c .char_width != 0 )
792+ {
793+ return std::string (
794+ " bitvector must have width that is a multiple of CHAR_BIT" );
795+ }
796+ else
797+ return {};
798+ }
799+
800+ if (type.id () == ID_array)
801+ return check_address_can_be_taken (to_array_type (type).element_type ());
802+
803+ return {}; // ok
804+ }
0 commit comments