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)
402 if(full_type.
id()==ID_array &&
409 if(!zero.has_value())
412 error() <<
"cannot zero-initialize array with subtype '"
417 numeric_cast_v<std::size_t>(index) + 1, *zero);
424 error() <<
"array index designator " << index
425 <<
" out of bounds (" << dest->
operands().size()
431 dest = &(dest->
operands()[numeric_cast_v<std::size_t>(index)]);
433 else if(full_type.
id()==ID_struct)
441 error() <<
"structure member designator " << index
442 <<
" out of bounds (" << dest->
operands().size()
448 "member designator is bounded by components size");
450 !components[index].get_is_padding(),
451 "member designator points at data member");
455 else if(full_type.
id()==ID_union)
463 "member designator is bounded by components size");
467 if(dest->
id()==ID_union &&
479 if(!zero.has_value())
482 error() <<
"cannot zero-initialize union component of type '"
508 if(full_type.
id()!=ID_struct &&
509 full_type.
id()!=ID_union &&
510 full_type.
id()!=ID_array &&
511 full_type.
id()!=ID_vector)
516 if(value.
id()==ID_initializer_list &&
532 if(full_type.
id()==ID_union)
539 if(!components.empty())
546 if(!zero.has_value())
549 error() <<
"cannot zero-initialize union component of type '"
560 if(value.
id()==ID_initializer_list)
565 else if(value.
id()==ID_string_constant)
571 full_type.
id() == ID_array &&
572 (full_type.
subtype().
id() == ID_signedbv ||
573 full_type.
subtype().
id() == ID_unsignedbv))
584 if(full_type.
id()==ID_struct ||
585 full_type.
id()==ID_union ||
586 full_type.
id()==ID_vector)
593 assert(full_type.
id()==ID_struct ||
594 full_type.
id()==ID_union ||
595 full_type.
id()==ID_array ||
596 full_type.
id()==ID_vector);
600 const typet dest_type=full_type;
609 warning() <<
"initialisation of " << dest_type.
id()
610 <<
" requires initializer list, found " << value.
id()
611 <<
" instead" <<
eom;
616 dest_type.
id()==ID_array &&
620 value.
id(ID_initializer_list);
622 for( ; init_it!=initializer_list.
operands().end(); ++init_it)
647 assert(!designator.
empty());
656 if(full_type.
id()==ID_array &&
660 if(full_type.
id()==ID_struct &&
668 assert(components.size()==entry.
size);
673 (components[entry.
index].get_is_padding() ||
674 (components[entry.
index].get_anonymous() &&
675 components[entry.
index].type().id() != ID_struct_tag &&
676 components[entry.
index].type().id() != ID_union_tag) ||
677 components[entry.
index].type().id() == ID_code))
689 if(designator.
size()==1)
695 assert(!designator.
empty());
700 const typet &src_type,
710 const exprt &d_op=*it;
714 if(full_type.
id()==ID_array)
716 if(d_op.
id()!=ID_index)
719 error() <<
"expected array index designator" <<
eom;
731 error() <<
"expected constant array index designator" <<
eom;
738 const auto size_opt =
744 error() <<
"expected constant array size" <<
eom;
748 entry.
index = numeric_cast_v<std::size_t>(index);
749 entry.
size = numeric_cast_v<std::size_t>(size);
752 else if(full_type.
id()==ID_struct ||
753 full_type.
id()==ID_union)
758 if(d_op.
id()!=ID_member)
761 error() <<
"expected member designator" <<
eom;
765 const irep_idt &component_name=d_op.
get(ID_component_name);
779 bool found=
false, repeat;
785 std::size_t number = 0;
789 for(
const auto &c : components)
791 if(c.get_name() == component_name)
795 entry.
size=components.size();
801 (c.type().id() == ID_struct_tag ||
802 c.type().id() == ID_union_tag) &&
806 entry.
size=components.size();
823 error() <<
"failed to find struct component '" << component_name
824 <<
"' in initialization of '" <<
to_string(struct_union_type)
833 error() <<
"designated initializers cannot initialize '"
842 assert(!designator.
empty());
852 assert(value.
id()==ID_initializer_list);
857 if(full_type.
id()==ID_struct ||
858 full_type.
id()==ID_union ||
859 full_type.
id()==ID_vector)
863 if(!zero.has_value())
866 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
872 else if(full_type.
id()==ID_array)
884 if(!zero.has_value())
887 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
899 (full_type.
subtype().
id() == ID_signedbv ||
900 full_type.
subtype().
id() == ID_unsignedbv) &&
907 warning() <<
"ignoring excess initializers" <<
eom;
925 <<
"' with an initializer list" <<
eom;
934 for(exprt::operandst::const_iterator it=operands.begin();
935 it!=operands.end(); )
938 result, current_designator, value, it, force_constant);
945 if(full_type.
id()==ID_struct)
947 assert(
result.operands().size()==
951 if(full_type.
id()==ID_array &&
955 size_t size=
result.operands().size();
956 result.type().id(ID_array);