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) &&
156 if(full_type.
id()==ID_array &&
160 const auto array_size =
162 if(!array_size.has_value())
165 error() <<
"array size needs to be constant, got "
173 error() <<
"array size must not be negative" <<
eom;
180 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size));
191 if(!zero.has_value())
194 error() <<
"cannot zero-initialize array with subtype '"
199 tmp2.
operands().resize(numeric_cast_v<std::size_t>(*array_size), *zero);
206 if(full_type.
id()==ID_array &&
211 <<
"' cannot be initialized with '" <<
to_string(value) <<
"'"
216 if(value.
id()==ID_designated_initializer)
220 <<
"' cannot be initialized with designated initializer" <<
eom;
244 symbol.
type.
id() == ID_array &&
267 symbol.
type.
id() == ID_array &&
282 if(full_type.
id()==ID_struct)
293 if(c.type().id() != ID_code && !c.get_is_padding())
302 else if(full_type.
id()==ID_union)
319 else if(full_type.
id()==ID_array)
330 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
331 if(!array_size.has_value())
334 error() <<
"array has non-constant size '"
339 entry.
size = numeric_cast_v<std::size_t>(*array_size);
343 else if(full_type.
id()==ID_vector)
347 const auto vector_size = numeric_cast<mp_integer>(vector_type.
size());
349 if(!vector_size.has_value())
352 error() <<
"vector has non-constant size '"
357 entry.
size = numeric_cast_v<std::size_t>(*vector_size);
369 const exprt &initializer_list,
370 exprt::operandst::const_iterator init_it,
374 exprt value=*init_it;
376 assert(!designator.
empty());
378 if(value.
id()==ID_designated_initializer)
385 static_cast<const exprt &
>(value.
find(ID_designator)));
387 assert(!designator.
empty());
391 result, designator, value, value.
operands().begin(), force_constant);
399 for(
size_t i=0; i<designator.
size(); i++)
401 size_t index=designator[i].index;
402 const typet &type=designator[i].type;
405 if(full_type.
id()==ID_array ||
406 full_type.
id()==ID_vector)
411 if(dest->
id() == ID_array_of)
414 const auto array_size = numeric_cast<mp_integer>(array_type.
size());
415 if(!array_size.has_value())
418 error() <<
"cannot zero-initialize array with element type '"
425 dest->
operands().resize(numeric_cast_v<std::size_t>(*array_size), zero);
430 if(full_type.
id()==ID_array &&
439 if(!zero.has_value())
442 error() <<
"cannot zero-initialize array with element type '"
448 numeric_cast_v<std::size_t>(index) + 1, *zero);
455 error() <<
"array index designator " << index
456 <<
" out of bounds (" << dest->
operands().size()
462 dest = &(dest->
operands()[numeric_cast_v<std::size_t>(index)]);
464 else if(full_type.
id()==ID_struct)
472 error() <<
"structure member designator " << index
473 <<
" out of bounds (" << dest->
operands().size()
479 "member designator is bounded by components size");
481 !components[index].get_is_padding(),
482 "member designator points at data member");
486 else if(full_type.
id()==ID_union)
493 if(components.empty())
496 error() <<
"union member designator found for empty union" <<
eom;
499 else if(init_it != initializer_list.
operands().begin())
504 error() <<
"too many initializers" <<
eom;
510 warning() <<
"excess elements in union initializer" <<
eom;
515 else if(index >= components.size())
518 error() <<
"union member designator " << index <<
" out of bounds ("
519 << components.size() <<
")" <<
eom;
526 dest->
id() == ID_union &&
533 else if(dest->
id() == ID_union)
543 if(!zero.has_value())
546 error() <<
"cannot zero-initialize union component of type '"
556 *dest = std::move(byte_update);
563 *dest = std::move(union_expr);
568 dest->
id() == ID_byte_update_big_endian ||
569 dest->
id() == ID_byte_update_little_endian)
574 dest = &byte_update.
op2();
592 if(full_type.
id()!=ID_struct &&
593 full_type.
id()!=ID_union &&
594 full_type.
id()!=ID_array &&
595 full_type.
id()!=ID_vector)
600 if(value.
id()==ID_initializer_list &&
616 if(full_type.
id()==ID_union)
623 if(!components.empty())
630 if(!zero.has_value())
633 error() <<
"cannot zero-initialize union component of type '"
644 if(value.
id()==ID_initializer_list)
649 else if(value.
id()==ID_string_constant)
655 full_type.
id() == ID_array &&
656 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
657 to_array_type(full_type).element_type().
id() == ID_unsignedbv))
668 if(full_type.
id()==ID_struct ||
669 full_type.
id()==ID_union ||
670 full_type.
id()==ID_vector)
677 assert(full_type.
id()==ID_struct ||
678 full_type.
id()==ID_union ||
679 full_type.
id()==ID_array ||
680 full_type.
id()==ID_vector);
684 const typet dest_type=full_type;
693 warning() <<
"initialisation of " << dest_type.
id()
694 <<
" requires initializer list, found " << value.
id()
695 <<
" instead" <<
eom;
700 dest_type.
id()==ID_array &&
704 value.
id(ID_initializer_list);
706 for( ; init_it!=initializer_list.
operands().end(); ++init_it)
731 assert(!designator.
empty());
740 if(full_type.
id()==ID_array &&
744 if(full_type.
id()==ID_struct &&
752 assert(components.size()==entry.
size);
757 (components[entry.
index].get_is_padding() ||
758 (components[entry.
index].get_anonymous() &&
759 components[entry.
index].type().id() != ID_struct_tag &&
760 components[entry.
index].type().id() != ID_union_tag) ||
761 components[entry.
index].type().id() == ID_code))
773 if(designator.
size()==1)
779 assert(!designator.
empty());
784 const typet &src_type,
794 const exprt &d_op=*it;
798 if(full_type.
id()==ID_array)
800 if(d_op.
id()!=ID_index)
803 error() <<
"expected array index designator" <<
eom;
815 error() <<
"expected constant array index designator" <<
eom;
822 const auto size_opt =
828 error() <<
"expected constant array size" <<
eom;
832 entry.
index = numeric_cast_v<std::size_t>(index);
833 entry.
size = numeric_cast_v<std::size_t>(size);
836 else if(full_type.
id()==ID_struct ||
837 full_type.
id()==ID_union)
842 if(d_op.
id()!=ID_member)
845 error() <<
"expected member designator" <<
eom;
849 const irep_idt &component_name=d_op.
get(ID_component_name);
863 bool found=
false, repeat;
869 std::size_t number = 0;
873 for(
const auto &c : components)
875 if(c.get_name() == component_name)
879 entry.
size=components.size();
885 (c.type().id() == ID_struct_tag ||
886 c.type().id() == ID_union_tag) &&
890 entry.
size=components.size();
907 error() <<
"failed to find struct component '" << component_name
908 <<
"' in initialization of '" <<
to_string(struct_union_type)
917 error() <<
"designated initializers cannot initialize '"
926 assert(!designator.
empty());
936 assert(value.
id()==ID_initializer_list);
943 full_type.
id() == ID_array && value.
operands().size() >= 1 &&
945 (
to_array_type(full_type).element_type().
id() == ID_signedbv ||
946 to_array_type(full_type).element_type().
id() == ID_unsignedbv) &&
953 warning() <<
"ignoring excess initializers" <<
eom;
961 if(full_type.
id()==ID_struct ||
962 full_type.
id()==ID_union ||
963 full_type.
id()==ID_vector)
967 if(!zero.has_value())
970 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
976 else if(full_type.
id()==ID_array)
988 if(!zero.has_value())
991 error() <<
"cannot zero-initialize '" <<
to_string(full_type) <<
"'"
1009 <<
"' with an initializer list" <<
eom;
1018 for(exprt::operandst::const_iterator it=operands.begin();
1019 it!=operands.end(); )
1022 result, current_designator, value, it, force_constant);
1029 if(full_type.
id()==ID_struct)
1031 assert(
result.operands().size()==
1035 if(full_type.
id()==ID_array &&
1039 size_t size=
result.operands().size();
1040 result.type().id(ID_array);
bool has_component_rec(const typet &type, const irep_idt &component_name, const namespacet &ns)
C Language Type Checking.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
Expression classes for byte-level operators.
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
ANSI-C Language Type Checking.
bitvector_typet char_type()
bitvector_typet c_index_type()
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Array constructor from list of elements.
const typet & element_type() const
The type of the elements of the array.
const exprt & size() const
std::size_t get_width() const
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
virtual exprt do_initializer_list(const exprt &value, const typet &type, bool force_constant)
virtual void implicit_typecast(exprt &expr, const typet &type)
virtual exprt do_initializer_rec(const exprt &value, const typet &type, bool force_constant)
initialize something of type ‘type’ with given value ‘value’
virtual void typecheck_expr(exprt &expr)
virtual void do_initializer(exprt &initializer, const typet &type, bool force_constant)
virtual void make_constant(exprt &expr)
virtual std::string to_string(const exprt &expr)
void increment_designator(designatort &designator)
virtual exprt::operandst::const_iterator do_designated_initializer(exprt &result, designatort &designator, const exprt &initializer_list, exprt::operandst::const_iterator init_it, bool force_constant)
virtual void make_constant_index(exprt &expr)
designatort make_designator(const typet &type, const exprt &src)
void designator_enter(const typet &type, designatort &designator)
struct configt::ansi_ct ansi_c
const entryt & front() const
void push_entry(const entryt &entry)
const entryt & back() const
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Base class for all expressions.
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
std::vector< exprt > operandst
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
source_locationt & add_source_location()
const source_locationt & source_location() const
typet & type()
Return the type of the expression.
bool get_bool(const irep_idt &name) const
const irept & find(const irep_idt &name) const
const irep_idt & get(const irep_idt &name) const
const irep_idt & id() const
source_locationt source_location
mstreamt & warning() const
mstreamt & result() const
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
array_exprt to_array_expr() const
convert string into array constant
Structure type, corresponds to C style structs.
Base type for structs and unions.
bool has_component(const irep_idt &component_name) const
const componentst & components() const
std::size_t component_number(const irep_idt &component_name) const
Return the sequence number of the component with given name.
std::vector< componentt > componentst
typet type
Type of symbol.
irep_idt name
The unique identifier.
exprt value
Initial value of symbol.
The type of an expression, extends irept.
const typet & subtype() const
Union constructor from single element.
irep_idt get_component_name() const
const constant_exprt & size() const
const typet & element_type() const
The type of the elements of the vector.
bool has_prefix(const std::string &s, const std::string &prefix)
#define forall_operands(it, expr)
#define Forall_operands(it, expr)
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Expression Initialization.
const std::string & id2string(const irep_idt &d)
#define UNREACHABLE
This should be used to mark dead code.
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
const union_exprt & to_union_expr(const exprt &expr)
Cast an exprt to a union_exprt.
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
const array_of_exprt & to_array_of_expr(const exprt &expr)
Cast an exprt to an array_of_exprt.
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
const string_constantt & to_string_constant(const exprt &expr)
const type_with_subtypet & to_type_with_subtype(const typet &type)
byte_update_exprt make_byte_update(const exprt &_op, const exprt &_offset, const exprt &_value)
Construct a byte_update_exprt with endianness and byte width matching the current configuration.