@@ -91,29 +91,29 @@ exprt c_typecheck_baset::do_initializer_rec(
9191 to_array_type (full_type).is_complete ())
9292 {
9393 // check size
94- mp_integer array_size;
95- if (to_integer ( to_array_type (full_type). size (), array_size ))
94+ const auto array_size = numeric_cast<mp_integer>( to_array_type (full_type). size ()) ;
95+ if (!array_size. has_value ( ))
9696 {
9797 error ().source_location = value.source_location ();
9898 error () << " array size needs to be constant, got "
9999 << to_string (to_array_type (full_type).size ()) << eom;
100100 throw 0 ;
101101 }
102102
103- if (array_size<0 )
103+ if (* array_size<0 )
104104 {
105105 error ().source_location = value.source_location ();
106106 error () << " array size must not be negative" << eom;
107107 throw 0 ;
108108 }
109109
110- if (mp_integer (tmp.operands ().size ())>array_size)
110+ if (mp_integer (tmp.operands ().size ())>* array_size)
111111 {
112112 // cut off long strings. gcc does a warning for this
113- tmp.operands ().resize (numeric_cast_v<std::size_t >(array_size));
113+ tmp.operands ().resize (numeric_cast_v<std::size_t >(* array_size));
114114 tmp.type ()=type;
115115 }
116- else if (mp_integer (tmp.operands ().size ())<array_size)
116+ else if (mp_integer (tmp.operands ().size ())<* array_size)
117117 {
118118 // fill up
119119 tmp.type ()=type;
@@ -126,7 +126,7 @@ exprt c_typecheck_baset::do_initializer_rec(
126126 << to_string (full_type.subtype ()) << " '" << eom;
127127 throw 0 ;
128128 }
129- tmp.operands ().resize (numeric_cast_v<std::size_t >(array_size), *zero);
129+ tmp.operands ().resize (numeric_cast_v<std::size_t >(* array_size), *zero);
130130 }
131131 }
132132
@@ -152,29 +152,29 @@ exprt c_typecheck_baset::do_initializer_rec(
152152 to_array_type (full_type).is_complete ())
153153 {
154154 // check size
155- mp_integer array_size;
156- if (to_integer ( to_array_type (full_type). size (), array_size ))
155+ const auto array_size = numeric_cast<mp_integer>( to_array_type (full_type). size ()) ;
156+ if (!array_size. has_value ( ))
157157 {
158158 error ().source_location = value.source_location ();
159159 error () << " array size needs to be constant, got "
160160 << to_string (to_array_type (full_type).size ()) << eom;
161161 throw 0 ;
162162 }
163163
164- if (array_size<0 )
164+ if (* array_size<0 )
165165 {
166166 error ().source_location = value.source_location ();
167167 error () << " array size must not be negative" << eom;
168168 throw 0 ;
169169 }
170170
171- if (mp_integer (tmp2.operands ().size ())>array_size)
171+ if (mp_integer (tmp2.operands ().size ())>* array_size)
172172 {
173173 // cut off long strings. gcc does a warning for this
174- tmp2.operands ().resize (numeric_cast_v<std::size_t >(array_size));
174+ tmp2.operands ().resize (numeric_cast_v<std::size_t >(* array_size));
175175 tmp2.type ()=type;
176176 }
177- else if (mp_integer (tmp2.operands ().size ())<array_size)
177+ else if (mp_integer (tmp2.operands ().size ())<* array_size)
178178 {
179179 // fill up
180180 tmp2.type ()=type;
@@ -187,7 +187,7 @@ exprt c_typecheck_baset::do_initializer_rec(
187187 << to_string (full_type.subtype ()) << " '" << eom;
188188 throw 0 ;
189189 }
190- tmp2.operands ().resize (numeric_cast_v<std::size_t >(array_size), *zero);
190+ tmp2.operands ().resize (numeric_cast_v<std::size_t >(* array_size), *zero);
191191 }
192192 }
193193
@@ -319,35 +319,34 @@ void c_typecheck_baset::designator_enter(
319319 }
320320 else
321321 {
322- mp_integer array_size;
323-
324- if (to_integer (array_type.size (), array_size))
322+ const auto array_size = numeric_cast<mp_integer>(array_type.size ());
323+ if (!array_size.has_value ())
325324 {
326325 error ().source_location = array_type.size ().source_location ();
327326 error () << " array has non-constant size `"
328327 << to_string (array_type.size ()) << " '" << eom;
329328 throw 0 ;
330329 }
331330
332- entry.size = numeric_cast_v<std::size_t >(array_size);
331+ entry.size = numeric_cast_v<std::size_t >(* array_size);
333332 entry.subtype =array_type.subtype ();
334333 }
335334 }
336335 else if (full_type.id ()==ID_vector)
337336 {
338337 const vector_typet &vector_type=to_vector_type (full_type);
339338
340- mp_integer vector_size;
339+ const auto vector_size = numeric_cast<mp_integer>(vector_type. size ()) ;
341340
342- if (to_integer (vector_type. size (), vector_size ))
341+ if (!vector_size. has_value ( ))
343342 {
344343 error ().source_location = vector_type.size ().source_location ();
345344 error () << " vector has non-constant size `"
346345 << to_string (vector_type.size ()) << " '" << eom;
347346 throw 0 ;
348347 }
349348
350- entry.size = numeric_cast_v<std::size_t >(vector_size);
349+ entry.size = numeric_cast_v<std::size_t >(* vector_size);
351350 entry.subtype =vector_type.subtype ();
352351 }
353352 else
@@ -735,7 +734,9 @@ designatort c_typecheck_baset::make_designator(
735734
736735 if (to_array_type (full_type).size ().is_nil ())
737736 size=0 ;
738- else if (to_integer (to_array_type (full_type).size (), size))
737+ else if (const auto size_opt = numeric_cast<mp_integer>(to_array_type (full_type).size ()))
738+ size = *size_opt;
739+ else
739740 {
740741 error ().source_location = d_op.op0 ().source_location ();
741742 error () << " expected constant array size" << eom;
0 commit comments