32 #include <unordered_set>
39 ansi_c_convert_type.
write(type);
42 if(type.
id()==ID_already_typechecked)
50 bool packed=type.
get_bool(ID_C_packed);
54 type = already_typechecked.
get_type();
56 c_qualifiers.
write(type);
58 type.
set(ID_C_packed,
true);
62 type.
add(ID_C_typedef, _typedef);
78 if(type.
id()==ID_code)
80 else if(type.
id()==ID_array)
82 else if(type.
id()==ID_pointer)
88 else if(type.
id()==ID_struct ||
91 else if(type.
id()==ID_c_enum)
93 else if(type.
id()==ID_c_enum_tag)
95 else if(type.
id()==ID_c_bit_field)
97 else if(type.
id()==ID_typeof)
99 else if(type.
id() == ID_typedef_type)
101 else if(type.
id() == ID_struct_tag ||
102 type.
id() == ID_union_tag)
106 else if(type.
id()==ID_vector)
110 else if(type.
id() == ID_frontend_vector)
114 else if(type.
id()==ID_custom_unsignedbv ||
115 type.
id()==ID_custom_signedbv ||
116 type.
id()==ID_custom_floatbv ||
117 type.
id()==ID_custom_fixedbv)
119 else if(type.
id()==ID_gcc_attribute_mode)
132 if(underlying_type.
id()==ID_c_enum_tag)
138 underlying_type.
id() == ID_signedbv ||
139 underlying_type.
id() == ID_unsignedbv,
140 "underlying type must be bitvector");
143 if(underlying_type.
id()==ID_signedbv ||
144 underlying_type.
id()==ID_unsignedbv)
150 if(gcc_attr_mode ==
"__QI__")
157 else if(gcc_attr_mode ==
"__byte__")
164 else if(gcc_attr_mode ==
"__HI__")
171 else if(gcc_attr_mode ==
"__SI__")
178 else if(gcc_attr_mode ==
"__word__")
185 else if(gcc_attr_mode ==
"__pointer__")
192 else if(gcc_attr_mode ==
"__DI__")
211 else if(gcc_attr_mode ==
"__TI__")
218 else if(gcc_attr_mode ==
"__V2SI__")
231 else if(gcc_attr_mode ==
"__V4SI__")
261 else if(underlying_type.
id()==ID_floatbv)
265 if(gcc_attr_mode ==
"__SF__")
267 else if(gcc_attr_mode ==
"__DF__")
269 else if(gcc_attr_mode ==
"__TF__")
271 else if(gcc_attr_mode ==
"__V2SF__")
274 else if(gcc_attr_mode ==
"__V2DF__")
277 else if(gcc_attr_mode ==
"__V4SF__")
280 else if(gcc_attr_mode ==
"__V4DF__")
287 type =
result.with_source_location(type);
289 else if(underlying_type.
id()==ID_complex)
294 if(gcc_attr_mode ==
"__SC__")
296 else if(gcc_attr_mode ==
"__DC__")
298 else if(gcc_attr_mode ==
"__TC__")
309 <<
"attribute mode '" << gcc_attr_mode
310 <<
"' applied to inappropriate type '" <<
to_string(type) <<
"'";
316 if(type.
get_bool(ID_C_restricted) &&
317 type.
id()!=ID_pointer &&
321 <<
"only a pointer can be 'restrict'";
329 static_cast<const exprt &
>(type.
find(ID_size));
339 <<
"failed to convert bit vector width to constant";
352 if(type.
id()==ID_custom_unsignedbv)
353 type.
id(ID_unsignedbv);
354 else if(type.
id()==ID_custom_signedbv)
355 type.
id(ID_signedbv);
356 else if(type.
id()==ID_custom_fixedbv)
361 static_cast<const exprt &
>(type.
find(ID_f));
374 <<
"failed to convert number of fraction bits to constant";
377 if(f_int<0 || f_int>size_int)
380 <<
"fixedbv fraction width invalid";
386 else if(type.
id()==ID_custom_floatbv)
391 static_cast<const exprt &
>(type.
find(ID_f));
404 <<
"failed to convert number of fraction bits to constant";
407 if(f_int<1 || f_int+1>=size_int)
410 <<
"floatbv fraction width invalid";
429 if(parameters.empty())
436 if(type.
parameters().back().id()==ID_ellipsis)
447 if(param.id()==ID_declaration)
457 std::list<codet> tmp_clean_code;
467 if(identifier.
empty())
482 param.
swap(parameter);
488 if(parameters.size() == 1 && parameters[0].type().id() == ID_empty)
503 if(decl_return_type.
id() == ID_array)
506 <<
"function must not return array";
509 if(decl_return_type.
id() == ID_code)
512 <<
"function must not return function type";
536 <<
"array has incomplete element type";
544 <<
"array of function element type";
571 <<
"failed to convert constant: " << tmp_size.
pretty();
577 <<
"array size must not be negative, "
584 else if(tmp_size.
id()==ID_infinity)
588 else if(tmp_size.
id()==ID_symbol &&
609 <<
"' is not constant";
616 size_source_location,
619 new_symbol.
type.
set(ID_C_constant,
true);
629 assignment.
lhs()=symbol_expr;
630 assignment.
rhs() = new_symbol.
value;
659 if(subtype.
id()!=ID_signedbv &&
660 subtype.
id()!=ID_unsignedbv &&
661 subtype.
id()!=ID_floatbv &&
662 subtype.
id()!=ID_fixedbv)
665 <<
"cannot make a vector of subtype " <<
to_string(subtype);
674 <<
"failed to convert constant: " << size.
pretty();
680 <<
"vector size must be positive, "
688 if(!sub_size_expr_opt.has_value())
691 <<
"failed to determine size of vector base type '" <<
to_string(subtype)
695 simplify(sub_size_expr_opt.value(), *
this);
697 const auto sub_size = numeric_cast<mp_integer>(sub_size_expr_opt.value());
699 if(!sub_size.has_value())
702 <<
"failed to determine size of vector base type '" <<
to_string(subtype)
709 <<
"type had size 0: '" <<
to_string(subtype) <<
"'";
713 if(s % *sub_size != 0)
716 <<
"vector size (" << s <<
") expected to be multiple of base type size ("
744 remove_qualifiers.
write(type);
746 bool is_packed = type.
get_bool(ID_C_packed);
760 std::string typestr =
type2name(compound_symbol.type, *
this);
761 compound_symbol.base_name =
"#anon#" + typestr;
762 compound_symbol.name=
"tag-#anon#"+typestr;
763 identifier=compound_symbol.name;
776 identifier=type.
find(ID_tag).
get(ID_identifier);
779 symbol_table_baset::symbolst::const_iterator s_it =
787 type.
set(ID_tag, base_name);
790 compound_symbol.base_name=base_name;
794 typet new_type=compound_symbol.type;
813 s_it->second.type.id() == type.
id() &&
821 type.
set(ID_tag, base_name);
827 else if(s_it->second.type.id() != type.
id())
830 <<
"redefinition of '" << s_it->second.pretty_name <<
"'"
831 <<
" as different kind of tag";
836 <<
"redefinition of body of '" << s_it->second.pretty_name <<
"'";
843 if(type.
id() == ID_union)
845 else if(type.
id() == ID_struct)
853 original_qualifiers.
write(type);
856 type.
set(ID_C_packed,
true);
867 old_components.swap(components);
870 for(
auto &decl : old_components)
881 new_component.
id(ID_static_assert);
885 components.push_back(new_component);
893 for(
const auto &declarator : declaration.
declarators())
896 declarator.get_base_name(), declaration.
full_type(declarator));
902 : declarator.source_location();
913 new_component.
type().
id() != ID_c_bit_field)
920 new_component.
type().
id() != ID_struct_tag &&
921 new_component.
type().
id() != ID_union_tag)
924 <<
"no members defined";
936 new_component.
type().
id() == ID_struct_tag &&
944 new_component.
type().
id() == ID_union_tag &&
959 (new_component.
type().
id()!=ID_array ||
963 <<
"incomplete type not permitted here";
966 if(new_component.
type().
id() == ID_empty)
969 <<
"void-typed member not permitted";
973 new_component.
type().
id() == ID_c_bit_field &&
978 <<
"zero-width bit-field with declarator not permitted";
981 components.push_back(new_component);
986 unsigned anon_member_counter=0;
989 for(
auto &member : components)
991 if(!member.get_name().empty())
995 member.set_anonymous(
true);
1001 std::unordered_set<irep_idt> members;
1003 for(
const auto &c : components)
1005 if(!members.insert(c.get_name()).second)
1008 <<
"duplicate member '" << c.get_name() <<
'\'';
1016 if(type.
id()==ID_struct ||
1017 type.
id()==ID_union)
1019 for(struct_union_typet::componentst::iterator
1020 it=components.begin();
1021 it!=components.end();
1024 typet &c_type=it->type();
1026 if(c_type.
id()==ID_array &&
1030 if(type.
id()==ID_struct && it!=--components.
end())
1033 <<
"flexible struct member must be last member";
1038 c_type.
set(ID_C_flexible_array_member,
true);
1047 if(type.
id()==ID_struct)
1049 else if(type.
id()==ID_union)
1053 for(struct_union_typet::componentst::iterator
1054 it=components.begin();
1055 it!=components.end();
1058 if(it->id()==ID_static_assert)
1063 <<
"static_assert not supported in compound body";
1075 <<
"failed _Static_assert";
1082 it=components.erase(it);
1091 components.empty() &&
1095 <<
"C requires that a struct or union has at least one member";
1134 bool is_packed)
const
1204 const bool have_underlying_type =
1207 if(have_underlying_type)
1211 const typet &underlying_type =
1212 static_cast<const typet &
>(type.
find(ID_enum_underlying_type));
1217 <<
"non-integral type '" << underlying_type.
get(ID_C_c_type)
1218 <<
"' is an invalid underlying type";
1224 mp_integer value=0, min_value=0, max_value=0;
1226 std::vector<c_enum_typet::c_enum_membert> enum_members;
1227 enum_members.reserve(as_expr.
operands().size());
1257 <<
"enum is not a constant";
1266 typet constant_type;
1268 if(have_underlying_type)
1270 constant_type = type.
find_type(ID_enum_underlying_type);
1273 if(value < tmp.smallest() || value > tmp.largest())
1276 <<
"enumerator value is not representable in the underlying type '"
1277 << constant_type.
get(ID_C_c_type) <<
"'";
1287 declaration.
type()=constant_type;
1303 enum_members.push_back(member);
1313 bool is_packed=type.
get_bool(ID_C_packed);
1318 if(have_underlying_type)
1329 std::size_t width = underlying_type.
get_width();
1330 for(
auto &member : enum_members)
1342 std::string anon_identifier=
"#anon_enum";
1344 for(
const auto &member : enum_members)
1346 anon_identifier+=
'$';
1347 anon_identifier+=
id2string(member.get_base_name());
1348 anon_identifier+=
'=';
1349 anon_identifier+=
id2string(member.get_value());
1353 anon_identifier+=
"#packed";
1355 type.
add(ID_tag).
set(ID_identifier, anon_identifier);
1364 enum_tag_symbol.location=source_location;
1365 enum_tag_symbol.is_file_local=
true;
1366 enum_tag_symbol.base_name=base_name;
1368 enum_tag_symbol.type.add_subtype() = underlying_type;
1379 const symbolt &symbol = *existing_symbol;
1381 if(symbol.
type.
id() != ID_c_enum)
1384 <<
"use of tag that does not match previous declaration";
1391 existing_symbol->
type = enum_tag_symbol.type;
1398 if(!base_name.
empty())
1401 <<
"redeclaration of enum tag";
1412 type.
id(ID_c_enum_tag);
1414 type.
set(ID_identifier, identifier);
1424 <<
"anonymous enum tag without members";
1430 warning() <<
"ignoring specification of underlying type for enum" <<
eom;
1440 symbol_table_baset::symbolst::const_iterator s_it =
1446 const symbolt &symbol=s_it->second;
1448 if(symbol.
type.
id() != ID_c_enum)
1451 <<
"use of tag that does not match previous declaration";
1458 new_type.
add(ID_tag)=tag;
1462 enum_tag_symbol.location=source_location;
1463 enum_tag_symbol.is_file_local=
true;
1464 enum_tag_symbol.base_name=base_name;
1490 <<
"failed to convert bit field width";
1496 <<
"bit field width is negative";
1505 std::size_t sub_width=0;
1507 if(underlying_type.
id() == ID_bool)
1513 underlying_type.
id() == ID_signedbv ||
1514 underlying_type.
id() == ID_unsignedbv || underlying_type.
id() == ID_c_bool)
1518 else if(underlying_type.
id() == ID_c_enum_tag)
1523 const auto &c_enum_type =
1526 if(c_enum_type.is_incomplete())
1529 <<
"bit field has incomplete enum type";
1537 <<
"bit field with non-integer type: " <<
to_string(underlying_type);
1543 <<
"bit field (" << i <<
" bits) larger than type (" << sub_width
1555 c_qualifiers.
read(type);
1557 const auto &as_expr = (
const exprt &)type;
1559 if(!as_expr.has_operands())
1571 if(expr.
id()==ID_address_of &&
1581 c_qualifiers.
write(type);
1588 symbol_table_baset::symbolst::const_iterator s_it =
1594 <<
"typedef symbol '" << identifier <<
"' not found";
1597 const symbolt &symbol = s_it->second;
1602 <<
"expected type symbol for typedef";
1608 bool is_packed = type.
get_bool(ID_C_packed);
1613 c_qualifiers.
write(type);
1616 type.
set(ID_C_packed,
true);
1633 if(type.
id()==ID_array)
1639 else if(type.
id()==ID_code)
1647 else if(type.
id()==ID_KnR)
ANSI-C Language Conversion.
ANSI-CC Language Type Checking.
ansi_c_declarationt & to_ansi_c_declaration(exprt &expr)
bool is_signed_or_unsigned_bitvector(const typet &type)
This method tests, if the given typet is a signed or unsigned bitvector.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
const integer_bitvector_typet & to_integer_bitvector_type(const typet &type)
Cast a typet to an integer_bitvector_typet.
ANSI-C Language Type Checking.
already_typechecked_typet & to_already_typechecked_type(typet &type)
floatbv_typet float_type()
signedbv_typet signed_long_int_type()
signedbv_typet signed_char_type()
unsignedbv_typet unsigned_int_type()
unsignedbv_typet unsigned_long_long_int_type()
unsignedbv_typet unsigned_long_int_type()
signedbv_typet signed_int_type()
pointer_typet pointer_type(const typet &subtype)
unsignedbv_typet unsigned_char_type()
signedbv_typet signed_size_type()
signedbv_typet signed_long_long_int_type()
bitvector_typet c_index_type()
floatbv_typet double_type()
signedbv_typet signed_short_int_type()
unsignedbv_typet unsigned_short_int_type()
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
static void make_already_typechecked(typet &type)
typet full_type(const ansi_c_declaratort &) const
const ansi_c_declaratort & declarator() const
const declaratorst & declarators() const
bool get_is_static_assert() const
irep_idt get_base_name() const
irep_idt get_name() const
bool is_incomplete() const
typet & index_type_nonconst()
The type of the index expressions into any instance of this type.
const typet & element_type() const
The type of the elements of the array.
const exprt & size() const
Base class of fixed-width bit-vector types.
std::size_t get_width() const
std::size_t width() const
Type for C bit fields These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (...
const typet & underlying_type() const
C enum tag type, i.e., c_enum_typet with an identifier.
void set_identifier(const irep_idt &identifier)
void set_value(const irep_idt &value)
void set_base_name(const irep_idt &base_name)
void make_incomplete()
enum types may be incomplete
bool is_incomplete() const
enum types may be incomplete
virtual void write(typet &src) const
virtual void read(const typet &src)
bool is_transparent_union
symbol_table_baset & symbol_table
virtual void typecheck_compound_body(struct_union_typet &type)
virtual void make_index_type(exprt &expr)
virtual void typecheck_code_type(code_typet &type)
virtual void typecheck_expr(exprt &expr)
void move_symbol(symbolt &symbol, symbolt *&new_symbol)
virtual void typecheck_vector_type(typet &type)
virtual void typecheck_c_enum_type(typet &type)
virtual void make_constant(exprt &expr)
virtual void typecheck_c_bit_field_type(c_bit_field_typet &type)
static void add_rounding_mode(exprt &)
std::list< codet > clean_code
virtual std::string to_string(const exprt &expr)
void typecheck_declaration(ansi_c_declarationt &)
virtual void typecheck_c_enum_tag_type(c_enum_tag_typet &type)
virtual void adjust_function_parameter(typet &type) const
typet enum_constant_type(const mp_integer &min, const mp_integer &max) const
virtual void typecheck_custom_type(typet &type)
virtual void make_constant_index(exprt &expr)
bitvector_typet enum_underlying_type(const mp_integer &min, const mp_integer &max, bool is_packed) const
virtual void typecheck_compound_type(struct_union_typet &type)
virtual bool is_complete_type(const typet &type) const
id_type_mapt parameter_map
virtual void typecheck_typedef_type(typet &type)
virtual void typecheck_array_type(array_typet &type)
virtual void typecheck_typeof_type(typet &type)
virtual void typecheck_type(typet &type)
A codet representing an assignment in the program.
A codet representing the declaration of a local variable.
void set_base_name(const irep_idt &name)
std::vector< parametert > parameterst
const typet & return_type() const
const parameterst & parameters() const
Complex numbers made of pair of given subtype.
struct configt::ansi_ct ansi_c
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
std::string::const_iterator end() const
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).
bool is_true() const
Return whether the expression is a constant representing true.
source_locationt & add_source_location()
const source_locationt & source_location() const
bool is_false() const
Return whether the expression is a constant representing false.
typet & type()
Return the type of the expression.
bool is_constant() const
Return whether the expression is a constant.
Unbounded, signed integers (mathematical integers, not bitvectors)
There are a large number of kinds of tree structured or tree-like data in CPROVER.
bool get_bool(const irep_idt &name) const
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
const irept & find(const irep_idt &name) const
const irep_idt & get(const irep_idt &name) const
void remove(const irep_idt &name)
void set(const irep_idt &name, const irep_idt &value)
const irep_idt & id() const
irept & add(const irep_idt &name)
source_locationt source_location
mstreamt & warning() const
mstreamt & result() const
message_handlert & get_message_handler()
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Unbounded, signed rational numbers.
A struct tag type, i.e., struct_typet with an identifier.
const irep_idt & get_name() const
void set_pretty_name(const irep_idt &name)
Base type for structs and unions.
bool is_incomplete() const
A struct/union may be incomplete.
const componentst & components() const
void make_incomplete()
A struct/union may be incomplete.
std::vector< componentt > componentst
Expression to hold a symbol (variable)
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
virtual symbolt * get_writeable(const irep_idt &name)=0
Find a symbol in the symbol table for read-write access.
irep_idt base_name
Base (non-scoped) name.
source_locationt location
Source code location of definition of symbol.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
typet type
Type of symbol.
irep_idt name
The unique identifier.
exprt value
Initial value of symbol.
void set_identifier(const irep_idt &identifier)
const irep_idt & get_identifier() const
Symbol table entry describing a data typeThis is a symbol generated as part of type checking.
const typet & subtype() const
Semantic type conversion.
static exprt conditional_cast(const exprt &expr, const typet &type)
errort with_location(source_locationt _location) &&
const irep_idt & get_identifier() const
The type of an expression, extends irept.
const source_locationt & source_location() const
typet & add_type(const irep_idt &name)
const typet & find_type(const irep_idt &name) const
typet && with_source_location(source_locationt location) &&
This is a 'fluent style' method for creating a new type with an added-on source location.
source_locationt & add_source_location()
A union tag type, i.e., union_typet with an identifier.
const constant_exprt & size() const
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
Fresh auxiliary symbol creation.
signedbv_typet gcc_signed_int128_type()
unsignedbv_typet gcc_unsigned_int128_type()
floatbv_typet gcc_float128_type()
const std::string & id2string(const irep_idt &d)
const mp_integer string2integer(const std::string &n, unsigned base)
const std::string integer2string(const mp_integer &n, unsigned base)
mp_integer alignment(const typet &type, const namespacet &ns)
void add_padding(struct_typet &type, const namespacet &ns)
ANSI-C Language Type Checking.
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
std::optional< exprt > size_of_expr(const typet &type, const namespacet &ns)
bool simplify(exprt &expr, const namespacet &ns)
#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,...
#define PRECONDITION(CONDITION)
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_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 struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
std::size_t long_long_int_width
std::size_t long_int_width
std::size_t short_int_width
static std::string type2name(const typet &type, const namespacet &ns, symbol_numbert &symbol_number)
const type_with_subtypet & to_type_with_subtype(const typet &type)
const typedef_typet & to_typedef_type(const typet &type)
Cast a generic typet to a typedef_typet.
bool is_signed(const typet &t)
Convenience function – is the type signed?