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__")
291 else if(underlying_type.
id()==ID_complex)
296 if(gcc_attr_mode ==
"__SC__")
298 else if(gcc_attr_mode ==
"__DC__")
300 else if(gcc_attr_mode ==
"__TC__")
313 error() <<
"attribute mode '" << gcc_attr_mode
314 <<
"' applied to inappropriate type '" <<
to_string(type) <<
"'"
322 if(type.
get_bool(ID_C_restricted) &&
323 type.
id()!=ID_pointer &&
327 error() <<
"only a pointer can be 'restrict'" <<
eom;
336 static_cast<const exprt &
>(type.
find(ID_size));
346 error() <<
"failed to convert bit vector width to constant" <<
eom;
353 error() <<
"bit vector width invalid" <<
eom;
362 if(type.
id()==ID_custom_unsignedbv)
363 type.
id(ID_unsignedbv);
364 else if(type.
id()==ID_custom_signedbv)
365 type.
id(ID_signedbv);
366 else if(type.
id()==ID_custom_fixedbv)
371 static_cast<const exprt &
>(type.
find(ID_f));
384 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
388 if(f_int<0 || f_int>size_int)
391 error() <<
"fixedbv fraction width invalid" <<
eom;
398 else if(type.
id()==ID_custom_floatbv)
403 static_cast<const exprt &
>(type.
find(ID_f));
416 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
420 if(f_int<1 || f_int+1>=size_int)
423 error() <<
"floatbv fraction width invalid" <<
eom;
443 if(parameters.empty())
450 if(type.
parameters().back().id()==ID_ellipsis)
461 if(param.id()==ID_declaration)
471 std::list<codet> tmp_clean_code;
481 if(identifier.
empty())
496 param.
swap(parameter);
502 if(parameters.size() == 1 && parameters[0].type().id() == ID_empty)
517 if(decl_return_type.
id() == ID_array)
520 error() <<
"function must not return array" <<
eom;
524 if(decl_return_type.
id() == ID_code)
527 error() <<
"function must not return function type" <<
eom;
549 (followed_subtype.
id() == ID_struct || followed_subtype.
id() == ID_union) &&
554 error() <<
"array has incomplete element type" <<
eom;
563 error() <<
"array of function element type" <<
eom;
591 error() <<
"failed to convert constant: "
599 error() <<
"array size must not be negative, "
600 "but got " << s <<
eom;
606 else if(tmp_size.
id()==ID_infinity)
610 else if(tmp_size.
id()==ID_symbol &&
631 <<
"' is not constant" <<
eom;
639 size_source_location,
642 new_symbol.
type.
set(ID_C_constant,
true);
652 assignment.
lhs()=symbol_expr;
653 assignment.
rhs() = new_symbol.
value;
682 if(subtype.
id()!=ID_signedbv &&
683 subtype.
id()!=ID_unsignedbv &&
684 subtype.
id()!=ID_floatbv &&
685 subtype.
id()!=ID_fixedbv)
688 error() <<
"cannot make a vector of subtype "
699 error() <<
"failed to convert constant: "
707 error() <<
"vector size must be positive, "
708 "but got " << s <<
eom;
715 if(!sub_size_expr_opt.has_value())
718 error() <<
"failed to determine size of vector base type '"
723 simplify(sub_size_expr_opt.value(), *
this);
727 if(!sub_size.has_value())
730 error() <<
"failed to determine size of vector base type '"
743 if(s % *sub_size != 0)
746 error() <<
"vector size (" << s
747 <<
") expected to be multiple of base type size (" << *sub_size
777 remove_qualifiers.
write(type);
779 bool is_packed = type.
get_bool(ID_C_packed);
793 std::string typestr =
type2name(compound_symbol.type, *
this);
794 compound_symbol.base_name =
"#anon#" + typestr;
795 compound_symbol.name=
"tag-#anon#"+typestr;
796 identifier=compound_symbol.name;
809 identifier=type.
find(ID_tag).
get(ID_identifier);
812 symbol_table_baset::symbolst::const_iterator s_it =
820 type.
set(ID_tag, base_name);
823 compound_symbol.base_name=base_name;
827 typet new_type=compound_symbol.type;
846 s_it->second.type.id() == type.
id() &&
854 type.
set(ID_tag, base_name);
860 else if(s_it->second.type.id() != type.
id())
863 error() <<
"redefinition of '" << s_it->second.pretty_name <<
"'"
864 <<
" as different kind of tag" <<
eom;
870 error() <<
"redefinition of body of '" << s_it->second.pretty_name
879 if(type.
id() == ID_union)
881 else if(type.
id() == ID_struct)
889 original_qualifiers.
write(type);
892 type.
set(ID_C_packed,
true);
903 old_components.swap(components);
906 for(
auto &decl : old_components)
917 new_component.
id(ID_static_assert);
921 components.push_back(new_component);
929 for(
const auto &declarator : declaration.
declarators())
932 declarator.get_base_name(), declaration.
full_type(declarator));
938 : declarator.source_location();
949 new_component.
type().
id() != ID_c_bit_field)
956 new_component.
type().
id() != ID_struct_tag &&
957 new_component.
type().
id() != ID_union_tag)
960 "no members defined", source_location};
972 new_component.
type().
id() == ID_struct_tag &&
980 new_component.
type().
id() == ID_union_tag &&
995 (new_component.
type().
id()!=ID_array ||
999 error() <<
"incomplete type not permitted here" <<
eom;
1003 if(new_component.
type().
id() == ID_empty)
1006 error() <<
"void-typed member not permitted" <<
eom;
1011 new_component.
type().
id() == ID_c_bit_field &&
1016 "zero-width bit-field with declarator not permitted",
1020 components.push_back(new_component);
1025 unsigned anon_member_counter=0;
1028 for(
auto &member : components)
1030 if(!member.get_name().empty())
1033 member.set_name(
"$anon"+std::to_string(anon_member_counter++));
1034 member.set_anonymous(
true);
1040 std::unordered_set<irep_idt> members;
1042 for(
const auto &c : components)
1044 if(!members.insert(c.get_name()).second)
1047 error() <<
"duplicate member '" << c.get_name() <<
'\'' <<
eom;
1056 if(type.
id()==ID_struct ||
1057 type.
id()==ID_union)
1059 for(struct_union_typet::componentst::iterator
1060 it=components.begin();
1061 it!=components.end();
1064 typet &c_type=it->type();
1066 if(c_type.
id()==ID_array &&
1070 if(type.
id()==ID_struct && it!=--components.end())
1073 error() <<
"flexible struct member must be last member" <<
eom;
1079 c_type.
set(ID_C_flexible_array_member,
true);
1088 if(type.
id()==ID_struct)
1090 else if(type.
id()==ID_union)
1094 for(struct_union_typet::componentst::iterator
1095 it=components.begin();
1096 it!=components.end();
1099 if(it->id()==ID_static_assert)
1104 error() <<
"static_assert not supported in compound body" <<
eom;
1117 error() <<
"failed _Static_assert" <<
eom;
1125 it=components.erase(it);
1134 components.empty() &&
1138 "C requires that a struct or union has at least one member",
1178 bool is_packed)
const
1250 const bool have_underlying_type =
1253 if(have_underlying_type)
1257 const typet &underlying_type =
1258 static_cast<const typet &
>(type.
find(ID_enum_underlying_type));
1262 std::ostringstream msg;
1263 msg <<
"non-integral type '" << underlying_type.
get(ID_C_c_type)
1264 <<
"' is an invalid underlying type";
1271 mp_integer value=0, min_value=0, max_value=0;
1273 std::vector<c_enum_typet::c_enum_membert> enum_members;
1274 enum_members.reserve(as_expr.
operands().size());
1304 error() <<
"enum is not a constant" <<
eom;
1314 typet constant_type;
1316 if(have_underlying_type)
1318 constant_type = type.
find_type(ID_enum_underlying_type);
1321 if(value < tmp.smallest() || value > tmp.largest())
1323 std::ostringstream msg;
1324 msg <<
"enumerator value is not representable in the underlying type '"
1325 << constant_type.
get(ID_C_c_type) <<
"'";
1336 declaration.
type()=constant_type;
1352 enum_members.push_back(member);
1362 bool is_packed=type.
get_bool(ID_C_packed);
1367 if(have_underlying_type)
1378 std::size_t width = underlying_type.
get_width();
1379 for(
auto &member : enum_members)
1391 std::string anon_identifier=
"#anon_enum";
1393 for(
const auto &member : enum_members)
1395 anon_identifier+=
'$';
1396 anon_identifier+=
id2string(member.get_base_name());
1397 anon_identifier+=
'=';
1398 anon_identifier+=
id2string(member.get_value());
1402 anon_identifier+=
"#packed";
1404 type.
add(ID_tag).
set(ID_identifier, anon_identifier);
1413 enum_tag_symbol.location=source_location;
1414 enum_tag_symbol.is_file_local=
true;
1415 enum_tag_symbol.base_name=base_name;
1417 enum_tag_symbol.type.add_subtype() = underlying_type;
1428 const symbolt &symbol = *existing_symbol;
1430 if(symbol.
type.
id() != ID_c_enum)
1433 error() <<
"use of tag that does not match previous declaration" <<
eom;
1441 existing_symbol->
type = enum_tag_symbol.type;
1448 if(!base_name.
empty())
1451 error() <<
"redeclaration of enum tag" <<
eom;
1463 type.
id(ID_c_enum_tag);
1465 type.
set(ID_identifier, identifier);
1475 error() <<
"anonymous enum tag without members" <<
eom;
1482 warning() <<
"ignoring specification of underlying type for enum" <<
eom;
1492 symbol_table_baset::symbolst::const_iterator s_it =
1498 const symbolt &symbol=s_it->second;
1500 if(symbol.
type.
id() != ID_c_enum)
1503 error() <<
"use of tag that does not match previous declaration" <<
eom;
1511 new_type.
add(ID_tag)=tag;
1515 enum_tag_symbol.location=source_location;
1516 enum_tag_symbol.is_file_local=
true;
1517 enum_tag_symbol.base_name=base_name;
1543 error() <<
"failed to convert bit field width" <<
eom;
1550 error() <<
"bit field width is negative" <<
eom;
1560 std::size_t sub_width=0;
1562 if(underlying_type.
id() == ID_bool)
1568 underlying_type.
id() == ID_signedbv ||
1569 underlying_type.
id() == ID_unsignedbv || underlying_type.
id() == ID_c_bool)
1573 else if(underlying_type.
id() == ID_c_enum_tag)
1578 const auto &c_enum_type =
1581 if(c_enum_type.is_incomplete())
1584 error() <<
"bit field has incomplete enum type" <<
eom;
1593 error() <<
"bit field with non-integer type: " <<
to_string(underlying_type)
1601 error() <<
"bit field (" << i
1602 <<
" bits) larger than type (" << sub_width <<
" bits)"
1615 c_qualifiers.
read(type);
1617 const auto &as_expr = (
const exprt &)type;
1619 if(!as_expr.has_operands())
1631 if(expr.
id()==ID_address_of &&
1641 c_qualifiers.
write(type);
1648 symbol_table_baset::symbolst::const_iterator s_it =
1654 error() <<
"typedef symbol '" << identifier <<
"' not found" <<
eom;
1658 const symbolt &symbol = s_it->second;
1663 error() <<
"expected type symbol for typedef" <<
eom;
1670 bool is_packed = type.
get_bool(ID_C_packed);
1675 c_qualifiers.
write(type);
1678 type.
set(ID_C_packed,
true);
1695 if(type.
id()==ID_array)
1701 else if(type.
id()==ID_code)
1709 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()
unsignedbv_typet size_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_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
static void make_already_typechecked(typet &type)
const ansi_c_declaratort & declarator() const
const declaratorst & declarators() const
typet full_type(const ansi_c_declaratort &) const
bool get_is_static_assert() const
irep_idt get_base_name() const
irep_idt get_name() const
bool is_incomplete() const
const exprt & size() const
const typet & element_type() const
The type of the elements of the array.
typet & index_type_nonconst()
The type of the index expressions into any instance of this type.
Base class of fixed-width bit-vector types.
void set_width(std::size_t width)
std::size_t get_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 override
bool is_transparent_union
virtual void read(const typet &src) override
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 parameterst & parameters() const
const typet & return_type() 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.
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.
bool is_false() const
Return whether the expression is a constant representing false.
bool is_constant() const
Return whether the expression is a constant.
typet & type()
Return the type of the expression.
const source_locationt & source_location() const
source_locationt & add_source_location()
Unbounded, signed integers (mathematical integers, not bitvectors)
Thrown when we can't handle something in an input source file.
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
message_handlert & get_message_handler()
mstreamt & warning() const
mstreamt & result() const
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
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.
void set_pretty_name(const irep_idt &name)
const irep_idt & get_name() const
Base type for structs and unions.
const componentst & components() const
bool is_incomplete() const
A struct/union may be incomplete.
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.
virtual symbolt * get_writeable(const irep_idt &name)=0
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.
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.
const irep_idt & get_identifier() const
void set_identifier(const irep_idt &identifier)
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)
const irep_idt & get_identifier() const
The type of an expression, extends irept.
typet & add_type(const irep_idt &name)
const typet & find_type(const irep_idt &name) const
source_locationt & add_source_location()
const source_locationt & source_location() const
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 address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
optionalt< 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)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
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 constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_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.
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?