33 if(type.
id()==ID_array)
38 "any array must have a size");
43 result.id() != ID_compound_literal)
64 (full_type.
id() == ID_struct || full_type.
id() == ID_union) &&
69 <<
"' is still incomplete -- cannot initialize" <<
eom;
73 if(value.
id()==ID_initializer_list)
77 value.
id() == ID_array && value.
get_bool(ID_C_string_constant) &&
78 full_type.
id() == ID_array &&
79 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
80 to_array_type(full_type).element_type().
id() == ID_unsignedbv) &&
93 if(full_type.
id()==ID_array &&
99 const auto array_size = numeric_cast<mp_integer>(array_type.size());
100 if(!array_size.has_value())
103 error() <<
"array size needs to be constant, got "
111 error() <<
"array size must not be negative" <<
eom;
118 tmp.
operands().resize(numeric_cast_v<std::size_t>(*array_size));
127 if(!zero.has_value())
130 error() <<
"cannot zero-initialize array with subtype '"
134 tmp.
operands().resize(numeric_cast_v<std::size_t>(*array_size), *zero);
142 value.
id() == ID_string_constant && full_type.
id() == ID_array &&
143 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
144 to_array_type(full_type).element_type().
id() == ID_unsignedbv) &&
157 if(full_type.
id()==ID_array &&
161 const auto array_size =
163 if(!array_size.has_value())
166 error() <<
"array size needs to be constant, got "
174 error() <<
"array size must not be negative" <<
eom;
181 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size));
192 if(!zero.has_value())
195 error() <<
"cannot zero-initialize array with subtype '"
200 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size), *zero);
207 if(full_type.
id()==ID_array &&
212 <<
"' cannot be initialized with '" <<
to_string(value) <<
"'"
217 if(value.
id()==ID_designated_initializer)
221 <<
"' cannot be initialized with designated initializer" <<
eom;
264 if(full_type.
id()==ID_struct)
275 if(c.type().id() != ID_code && !c.get_is_padding())
284 else if(full_type.
id()==ID_union)
301 else if(full_type.
id()==ID_array)
312 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
313 if(!array_size.has_value())
316 error() <<
"array has non-constant size '"
321 entry.
size = numeric_cast_v<std::size_t>(*array_size);
325 else if(full_type.
id()==ID_vector)
329 const auto vector_size = numeric_cast<mp_integer>(vector_type.
size());
331 if(!vector_size.has_value())
334 error() <<
"vector has non-constant size '"
339 entry.
size = numeric_cast_v<std::size_t>(*vector_size);
351 const exprt &initializer_list,
352 exprt::operandst::const_iterator init_it,
356 exprt value=*init_it;
358 assert(!designator.
empty());
360 if(value.
id()==ID_designated_initializer)
367 static_cast<const exprt &
>(value.
find(ID_designator)));
369 assert(!designator.
empty());
373 result, designator, value, value.
operands().begin(), force_constant);
381 for(
size_t i=0; i<designator.
size(); i++)
383 size_t index=designator[i].index;
384 const typet &type=designator[i].type;
387 if(full_type.
id()==ID_array ||
388 full_type.
id()==ID_vector)
393 if(dest->
id() == ID_array_of)
396 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
397 if(!array_size.has_value())
400 error() <<
"cannot zero-initialize array with element type '"
407 dest->
operands().resize(numeric_cast_v<std::size_t>(*array_size), zero);
412 if(full_type.
id()==ID_array &&
422 if(!zero.has_value())
425 error() <<
"cannot zero-initialize array with element type '"
430 numeric_cast_v<std::size_t>(index) + 1, *zero);
435 error() <<
"array index designator " << index
436 <<
" out of bounds (" << dest->
operands().size()
442 dest = &(dest->
operands()[numeric_cast_v<std::size_t>(index)]);
444 else if(full_type.
id()==ID_struct)
452 error() <<
"structure member designator " << index
453 <<
" out of bounds (" << dest->
operands().size()
459 "member designator is bounded by components size");
461 !components[index].get_is_padding(),
462 "member designator points at data member");
466 else if(full_type.
id()==ID_union)
473 if(components.empty())
476 error() <<
"union member designator found for empty union" <<
eom;
479 else if(init_it != initializer_list.
operands().begin())
484 error() <<
"too many initializers" <<
eom;
490 warning() <<
"excess elements in union initializer" <<
eom;
495 else if(index >= components.size())
498 error() <<
"union member designator " << index <<
" out of bounds ("
499 << components.size() <<
")" <<
eom;
506 dest->
id() == ID_union &&
513 else if(dest->
id() == ID_union)
523 if(!zero.has_value())
526 error() <<
"cannot zero-initialize union component of type '"
536 *dest = std::move(byte_update);
543 *dest = std::move(union_expr);
548 dest->
id() == ID_byte_update_big_endian ||
549 dest->
id() == ID_byte_update_little_endian)
554 dest = &byte_update.
op2();
572 if(full_type.
id()!=ID_struct &&
573 full_type.
id()!=ID_union &&
574 full_type.
id()!=ID_array &&
575 full_type.
id()!=ID_vector)
580 if(value.
id()==ID_initializer_list &&
596 if(full_type.
id()==ID_union)
603 if(!components.empty())
610 if(!zero.has_value())
613 error() <<
"cannot zero-initialize union component of type '"
624 if(value.
id()==ID_initializer_list)
629 else if(value.
id()==ID_string_constant)
635 full_type.
id() == ID_array &&
636 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
637 to_array_type(full_type).element_type().
id() == ID_unsignedbv))
648 if(full_type.
id()==ID_struct ||
649 full_type.
id()==ID_union ||
650 full_type.
id()==ID_vector)
657 assert(full_type.
id()==ID_struct ||
658 full_type.
id()==ID_union ||
659 full_type.
id()==ID_array ||
660 full_type.
id()==ID_vector);
664 const typet dest_type=full_type;
673 warning() <<
"initialisation of " << dest_type.
id()
674 <<
" requires initializer list, found " << value.
id()
675 <<
" instead" <<
eom;
680 dest_type.
id()==ID_array &&
684 value.
id(ID_initializer_list);
686 for( ; init_it!=initializer_list.
operands().end(); ++init_it)
711 assert(!designator.
empty());
720 if(full_type.
id()==ID_array &&
724 if(full_type.
id()==ID_struct &&
732 assert(components.size()==entry.
size);
737 (components[entry.
index].get_is_padding() ||
738 (components[entry.
index].get_anonymous() &&
739 components[entry.
index].type().id() != ID_struct_tag &&
740 components[entry.
index].type().id() != ID_union_tag) ||
741 components[entry.
index].type().id() == ID_code))
753 if(designator.
size()==1)
759 assert(!designator.
empty());
764 const typet &src_type,
774 const exprt &d_op=*it;
778 if(full_type.
id()==ID_array)
780 if(d_op.
id()!=ID_index)
783 error() <<
"expected array index designator" <<
eom;
795 error() <<
"expected constant array index designator" <<
eom;
802 const auto size_opt =
808 error() <<
"expected constant array size" <<
eom;
812 entry.
index = numeric_cast_v<std::size_t>(index);
813 entry.
size = numeric_cast_v<std::size_t>(size);
816 else if(full_type.
id()==ID_struct ||
817 full_type.
id()==ID_union)
822 if(d_op.
id()!=ID_member)
825 error() <<
"expected member designator" <<
eom;
829 const irep_idt &component_name=d_op.
get(ID_component_name);
843 bool found=
false, repeat;
849 std::size_t number = 0;
853 for(
const auto &c : components)
855 if(c.get_name() == component_name)
859 entry.
size=components.size();
865 (c.type().id() == ID_struct_tag ||
866 c.type().id() == ID_union_tag) &&
870 entry.
size=components.size();
887 error() <<
"failed to find struct component '" << component_name
888 <<
"' in initialization of '" <<
to_string(struct_union_type)
897 error() <<
"designated initializers cannot initialize '"
906 assert(!designator.
empty());
916 assert(value.
id()==ID_initializer_list);
923 full_type.
id() == ID_array && value.
operands().size() >= 1 &&
925 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
926 to_array_type(full_type).element_type().
id() == ID_unsignedbv) &&
933 warning() <<
"ignoring excess initializers" <<
eom;
941 if(full_type.
id()==ID_struct ||
942 full_type.
id()==ID_union ||
943 full_type.
id()==ID_vector)
947 if(!zero.has_value())
950 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
956 else if(full_type.
id()==ID_array)
968 if(!zero.has_value())
971 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
989 <<
"' with an initializer list" <<
eom;
998 for(exprt::operandst::const_iterator it=operands.begin();
999 it!=operands.end(); )
1002 result, current_designator, value, it, force_constant);
1008 if(full_type.
id()==ID_struct)
1016 !components.empty() &&
1017 components.back().type().get_bool(ID_C_flexible_array_member))
1019 const auto array_size = numeric_cast<mp_integer>(
1023 !array_size.has_value() ||
1024 (*array_size <= 1 && init_array.
operands().size() != *array_size))
1031 actual_array_type.
set(ID_C_flexible_array_member,
true);
1032 init_array.
type() = actual_array_type;
1035 actual_struct_type.
remove(ID_tag);
1037 compound_symbol.mode =
mode;
1039 std::string typestr =
type2name(compound_symbol.type, *
this);
1040 compound_symbol.base_name =
"#anon#" + typestr;
1041 compound_symbol.name =
"tag-#anon#" + typestr;
1042 irep_idt tag_identifier = compound_symbol.name;
1053 if(full_type.
id()==ID_array &&