32 if(type.
id()==ID_array)
37 "any array must have a size");
40 if(
result.id() != ID_array &&
result.id() != ID_array_of)
61 (full_type.
id() == ID_struct || full_type.
id() == ID_union) &&
66 <<
"' is still incomplete -- cannot initialize" <<
eom;
70 if(value.
id()==ID_initializer_list)
74 value.
id() == ID_array && value.
get_bool(ID_C_string_constant) &&
75 full_type.
id() == ID_array &&
76 (full_type.
subtype().
id() == ID_signedbv ||
77 full_type.
subtype().
id() == ID_unsignedbv) &&
89 if(full_type.
id()==ID_array &&
93 const auto array_size =
95 if(!array_size.has_value())
98 error() <<
"array size needs to be constant, got "
106 error() <<
"array size must not be negative" <<
eom;
113 tmp.
operands().resize(numeric_cast_v<std::size_t>(*array_size));
122 if(!zero.has_value())
125 error() <<
"cannot zero-initialize array with subtype '"
129 tmp.
operands().resize(numeric_cast_v<std::size_t>(*array_size), *zero);
137 value.
id() == ID_string_constant && full_type.
id() == ID_array &&
138 (full_type.
subtype().
id() == ID_signedbv ||
139 full_type.
subtype().
id() == ID_unsignedbv) &&
151 if(full_type.
id()==ID_array &&
155 const auto array_size =
157 if(!array_size.has_value())
160 error() <<
"array size needs to be constant, got "
168 error() <<
"array size must not be negative" <<
eom;
175 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size));
184 if(!zero.has_value())
187 error() <<
"cannot zero-initialize array with subtype '"
191 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size), *zero);
198 if(full_type.
id()==ID_array &&
203 <<
"' cannot be initialized with '" <<
to_string(value) <<
"'"
208 if(value.
id()==ID_designated_initializer)
212 <<
"' cannot be initialized with designated initializer" <<
eom;
236 symbol.
type.
id() == ID_array &&
259 symbol.
type.
id() == ID_array &&
274 if(full_type.
id()==ID_struct)
285 if(c.type().id() != ID_code && !c.get_is_padding())
294 else if(full_type.
id()==ID_union)
311 else if(full_type.
id()==ID_array)
322 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
323 if(!array_size.has_value())
326 error() <<
"array has non-constant size '"
331 entry.
size = numeric_cast_v<std::size_t>(*array_size);
335 else if(full_type.
id()==ID_vector)
339 const auto vector_size = numeric_cast<mp_integer>(vector_type.
size());
341 if(!vector_size.has_value())
344 error() <<
"vector has non-constant size '"
349 entry.
size = numeric_cast_v<std::size_t>(*vector_size);
361 const exprt &initializer_list,
362 exprt::operandst::const_iterator init_it,
366 exprt value=*init_it;
368 assert(!designator.
empty());
370 if(value.
id()==ID_designated_initializer)
377 static_cast<const exprt &
>(value.
find(ID_designator)));
379 assert(!designator.
empty());
383 result, designator, value, value.
operands().begin(), force_constant);
391 for(
size_t i=0; i<designator.
size(); i++)
393 size_t index=designator[i].index;
394 const typet &type=designator[i].type;
397 if(full_type.
id()==ID_array ||
398 full_type.
id()==ID_vector)
403 if(dest->
id() == ID_array_of)
406 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
407 if(!array_size.has_value())
410 error() <<
"cannot zero-initialize array with subtype '"
416 dest->
operands().resize(numeric_cast_v<std::size_t>(*array_size), zero);
421 if(full_type.
id()==ID_array &&
428 if(!zero.has_value())
431 error() <<
"cannot zero-initialize array with subtype '"
436 numeric_cast_v<std::size_t>(index) + 1, *zero);
443 error() <<
"array index designator " << index
444 <<
" out of bounds (" << dest->
operands().size()
450 dest = &(dest->
operands()[numeric_cast_v<std::size_t>(index)]);
452 else if(full_type.
id()==ID_struct)
460 error() <<
"structure member designator " << index
461 <<
" out of bounds (" << dest->
operands().size()
467 "member designator is bounded by components size");
469 !components[index].get_is_padding(),
470 "member designator points at data member");
474 else if(full_type.
id()==ID_union)
482 "member designator is bounded by components size");
486 if(dest->
id()==ID_union &&
498 if(!zero.has_value())
501 error() <<
"cannot zero-initialize union component of type '"
527 if(full_type.
id()!=ID_struct &&
528 full_type.
id()!=ID_union &&
529 full_type.
id()!=ID_array &&
530 full_type.
id()!=ID_vector)
535 if(value.
id()==ID_initializer_list &&
551 if(full_type.
id()==ID_union)
558 if(!components.empty())
565 if(!zero.has_value())
568 error() <<
"cannot zero-initialize union component of type '"
579 if(value.
id()==ID_initializer_list)
584 else if(value.
id()==ID_string_constant)
590 full_type.
id() == ID_array &&
591 (full_type.
subtype().
id() == ID_signedbv ||
592 full_type.
subtype().
id() == ID_unsignedbv))
603 if(full_type.
id()==ID_struct ||
604 full_type.
id()==ID_union ||
605 full_type.
id()==ID_vector)
612 assert(full_type.
id()==ID_struct ||
613 full_type.
id()==ID_union ||
614 full_type.
id()==ID_array ||
615 full_type.
id()==ID_vector);
619 const typet dest_type=full_type;
628 warning() <<
"initialisation of " << dest_type.
id()
629 <<
" requires initializer list, found " << value.
id()
630 <<
" instead" <<
eom;
635 dest_type.
id()==ID_array &&
639 value.
id(ID_initializer_list);
641 for( ; init_it!=initializer_list.
operands().end(); ++init_it)
666 assert(!designator.
empty());
675 if(full_type.
id()==ID_array &&
679 if(full_type.
id()==ID_struct &&
687 assert(components.size()==entry.
size);
692 (components[entry.
index].get_is_padding() ||
693 (components[entry.
index].get_anonymous() &&
694 components[entry.
index].type().id() != ID_struct_tag &&
695 components[entry.
index].type().id() != ID_union_tag) ||
696 components[entry.
index].type().id() == ID_code))
708 if(designator.
size()==1)
714 assert(!designator.
empty());
719 const typet &src_type,
729 const exprt &d_op=*it;
733 if(full_type.
id()==ID_array)
735 if(d_op.
id()!=ID_index)
738 error() <<
"expected array index designator" <<
eom;
750 error() <<
"expected constant array index designator" <<
eom;
757 const auto size_opt =
763 error() <<
"expected constant array size" <<
eom;
767 entry.
index = numeric_cast_v<std::size_t>(index);
768 entry.
size = numeric_cast_v<std::size_t>(size);
771 else if(full_type.
id()==ID_struct ||
772 full_type.
id()==ID_union)
777 if(d_op.
id()!=ID_member)
780 error() <<
"expected member designator" <<
eom;
784 const irep_idt &component_name=d_op.
get(ID_component_name);
798 bool found=
false, repeat;
804 std::size_t number = 0;
808 for(
const auto &c : components)
810 if(c.get_name() == component_name)
814 entry.
size=components.size();
820 (c.type().id() == ID_struct_tag ||
821 c.type().id() == ID_union_tag) &&
825 entry.
size=components.size();
842 error() <<
"failed to find struct component '" << component_name
843 <<
"' in initialization of '" <<
to_string(struct_union_type)
852 error() <<
"designated initializers cannot initialize '"
861 assert(!designator.
empty());
871 assert(value.
id()==ID_initializer_list);
876 if(full_type.
id()==ID_struct ||
877 full_type.
id()==ID_union ||
878 full_type.
id()==ID_vector)
882 if(!zero.has_value())
885 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
891 else if(full_type.
id()==ID_array)
903 if(!zero.has_value())
906 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
918 (full_type.
subtype().
id() == ID_signedbv ||
919 full_type.
subtype().
id() == ID_unsignedbv) &&
926 warning() <<
"ignoring excess initializers" <<
eom;
944 <<
"' with an initializer list" <<
eom;
953 for(exprt::operandst::const_iterator it=operands.begin();
954 it!=operands.end(); )
957 result, current_designator, value, it, force_constant);
964 if(full_type.
id()==ID_struct)
966 assert(
result.operands().size()==
970 if(full_type.
id()==ID_array &&
974 size_t size=
result.operands().size();
975 result.type().id(ID_array);