14 #include <unordered_set>
39 ansi_c_convert_type.
read(type);
40 ansi_c_convert_type.
write(type);
43 if(type.
id()==ID_already_typechecked)
51 bool packed=type.
get_bool(ID_C_packed);
55 type = already_typechecked.
get_type();
57 c_qualifiers.
write(type);
59 type.
set(ID_C_packed,
true);
63 type.
add(ID_C_typedef, _typedef);
79 if(type.
id()==ID_code)
81 else if(type.
id()==ID_array)
83 else if(type.
id()==ID_pointer)
89 else if(type.
id()==ID_struct ||
92 else if(type.
id()==ID_c_enum)
94 else if(type.
id()==ID_c_enum_tag)
96 else if(type.
id()==ID_c_bit_field)
98 else if(type.
id()==ID_typeof)
100 else if(type.
id() == ID_typedef_type)
102 else if(type.
id() == ID_struct_tag ||
103 type.
id() == ID_union_tag)
107 else if(type.
id()==ID_vector)
111 else if(type.
id() == ID_frontend_vector)
115 else if(type.
id()==ID_custom_unsignedbv ||
116 type.
id()==ID_custom_signedbv ||
117 type.
id()==ID_custom_floatbv ||
118 type.
id()==ID_custom_fixedbv)
120 else if(type.
id()==ID_gcc_attribute_mode)
133 if(underlying_type.
id()==ID_c_enum_tag)
138 assert(underlying_type.
id()==ID_signedbv ||
139 underlying_type.
id()==ID_unsignedbv);
142 if(underlying_type.
id()==ID_signedbv ||
143 underlying_type.
id()==ID_unsignedbv)
149 if(gcc_attr_mode ==
"__QI__")
156 else if(gcc_attr_mode ==
"__byte__")
163 else if(gcc_attr_mode ==
"__HI__")
170 else if(gcc_attr_mode ==
"__SI__")
177 else if(gcc_attr_mode ==
"__word__")
184 else if(gcc_attr_mode ==
"__pointer__")
191 else if(gcc_attr_mode ==
"__DI__")
210 else if(gcc_attr_mode ==
"__TI__")
217 else if(gcc_attr_mode ==
"__V2SI__")
228 else if(gcc_attr_mode ==
"__V4SI__")
254 else if(underlying_type.
id()==ID_floatbv)
258 if(gcc_attr_mode ==
"__SF__")
260 else if(gcc_attr_mode ==
"__DF__")
262 else if(gcc_attr_mode ==
"__TF__")
264 else if(gcc_attr_mode ==
"__V2SF__")
266 else if(gcc_attr_mode ==
"__V2DF__")
268 else if(gcc_attr_mode ==
"__V4SF__")
270 else if(gcc_attr_mode ==
"__V4DF__")
280 else if(underlying_type.
id()==ID_complex)
285 if(gcc_attr_mode ==
"__SC__")
287 else if(gcc_attr_mode ==
"__DC__")
289 else if(gcc_attr_mode ==
"__TC__")
302 error() <<
"attribute mode '" << gcc_attr_mode
303 <<
"' applied to inappropriate type '" <<
to_string(type) <<
"'"
311 if(type.
get_bool(ID_C_restricted) &&
312 type.
id()!=ID_pointer &&
316 error() <<
"only a pointer can be 'restrict'" <<
eom;
325 static_cast<const exprt &
>(type.
find(ID_size));
335 error() <<
"failed to convert bit vector width to constant" <<
eom;
342 error() <<
"bit vector width invalid" <<
eom;
351 if(type.
id()==ID_custom_unsignedbv)
352 type.
id(ID_unsignedbv);
353 else if(type.
id()==ID_custom_signedbv)
354 type.
id(ID_signedbv);
355 else if(type.
id()==ID_custom_fixedbv)
360 static_cast<const exprt &
>(type.
find(ID_f));
373 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
377 if(f_int<0 || f_int>size_int)
380 error() <<
"fixedbv fraction width invalid" <<
eom;
387 else if(type.
id()==ID_custom_floatbv)
392 static_cast<const exprt &
>(type.
find(ID_f));
405 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
409 if(f_int<1 || f_int+1>=size_int)
412 error() <<
"floatbv fraction width invalid" <<
eom;
432 if(parameters.empty())
439 if(type.
parameters().back().id()==ID_ellipsis)
450 if(param.id()==ID_declaration)
460 std::list<codet> tmp_clean_code;
470 if(identifier.
empty())
485 param.
swap(parameter);
491 if(parameters.size() == 1 && parameters[0].type().id() == ID_empty)
506 if(decl_return_type.
id() == ID_array)
509 error() <<
"function must not return array" <<
eom;
513 if(decl_return_type.
id() == ID_code)
516 error() <<
"function must not return function type" <<
eom;
541 (followed_subtype.
id() == ID_struct || followed_subtype.
id() == ID_union) &&
546 error() <<
"array has incomplete element type" <<
eom;
555 error() <<
"array of function element type" <<
eom;
579 error() <<
"failed to convert constant: "
587 error() <<
"array size must not be negative, "
588 "but got " << s <<
eom;
594 else if(tmp_size.
id()==ID_infinity)
598 else if(tmp_size.
id()==ID_symbol &&
619 <<
"' is not constant" <<
eom;
627 size_source_location,
630 new_symbol.
type.
set(ID_C_constant,
true);
640 assignment.
lhs()=symbol_expr;
641 assignment.
rhs() = new_symbol.
value;
670 if(subtype.
id()!=ID_signedbv &&
671 subtype.
id()!=ID_unsignedbv &&
672 subtype.
id()!=ID_floatbv &&
673 subtype.
id()!=ID_fixedbv)
676 error() <<
"cannot make a vector of subtype "
687 error() <<
"failed to convert constant: "
695 error() <<
"vector size must be positive, "
696 "but got " << s <<
eom;
703 if(!sub_size_expr_opt.has_value())
706 error() <<
"failed to determine size of vector base type '"
711 simplify(sub_size_expr_opt.value(), *
this);
713 const auto sub_size = numeric_cast<mp_integer>(sub_size_expr_opt.value());
715 if(!sub_size.has_value())
718 error() <<
"failed to determine size of vector base type '"
731 if(s % *sub_size != 0)
734 error() <<
"vector size (" << s
735 <<
") expected to be multiple of base type size (" << *sub_size
764 remove_qualifiers.
write(type);
766 bool is_packed = type.
get_bool(ID_C_packed);
777 compound_symbol.
type=type;
782 std::string typestr =
type2name(compound_symbol.
type, *
this);
783 compound_symbol.
base_name =
"#anon#" + typestr;
784 compound_symbol.
name=
"tag-#anon#"+typestr;
785 identifier=compound_symbol.
name;
798 identifier=type.
find(ID_tag).
get(ID_identifier);
801 symbol_tablet::symbolst::const_iterator s_it=
809 type.
set(ID_tag, base_name);
813 compound_symbol.
name=identifier;
815 compound_symbol.
type=type;
838 s_it->second.type.id() == type.
id() &&
846 type.
set(ID_tag, base_name);
852 else if(s_it->second.type.id() != type.
id())
855 error() <<
"redefinition of '" << s_it->second.pretty_name <<
"'"
856 <<
" as different kind of tag" <<
eom;
862 error() <<
"redefinition of body of '" << s_it->second.pretty_name
871 if(type.
id() == ID_union)
873 else if(type.
id() == ID_struct)
881 original_qualifiers.
write(type);
884 type.
set(ID_C_packed,
true);
895 old_components.swap(components);
898 for(
auto &decl : old_components)
901 assert(decl.id()==ID_declaration);
909 new_component.
id(ID_static_assert);
912 assert(new_component.
operands().size()==2);
913 components.push_back(new_component);
921 for(
const auto &declarator : declaration.
declarators())
924 declarator.get_base_name(), declaration.
full_type(declarator));
930 : declarator.source_location();
938 (new_component.
type().
id()!=ID_array ||
942 error() <<
"incomplete type not permitted here" <<
eom;
946 if(new_component.
type().
id() == ID_empty)
949 error() <<
"void-typed member not permitted" <<
eom;
953 components.push_back(new_component);
958 unsigned anon_member_counter=0;
961 for(
auto &member : components)
963 if(!member.get_name().empty())
967 member.set_anonymous(
true);
973 std::unordered_set<irep_idt> members;
975 for(
const auto &c : components)
977 if(!members.insert(c.get_name()).second)
980 error() <<
"duplicate member '" << c.get_name() <<
'\'' <<
eom;
989 if(type.
id()==ID_struct ||
992 for(struct_union_typet::componentst::iterator
993 it=components.begin();
994 it!=components.end();
997 typet &c_type=it->type();
999 if(c_type.
id()==ID_array &&
1003 if(type.
id()==ID_struct && it!=--components.
end())
1006 error() <<
"flexible struct member must be last member" <<
eom;
1011 c_type.
id(ID_array);
1021 if(type.
id()==ID_struct)
1023 else if(type.
id()==ID_union)
1028 for(struct_typet::componentst::iterator
1029 it=components.begin();
1030 it!=components.end();
1033 if(it->type().id()==ID_c_bit_field &&
1035 it=components.erase(it);
1041 for(struct_union_typet::componentst::iterator
1042 it=components.begin();
1043 it!=components.end();
1046 if(it->id()==ID_static_assert)
1051 error() <<
"static_assert not supported in compound body" <<
eom;
1064 error() <<
"failed _Static_assert" <<
eom;
1072 it=components.erase(it);
1114 bool is_packed)
const
1186 const bool have_underlying_type =
1189 if(have_underlying_type)
1193 const typet &underlying_type =
1194 static_cast<const typet &
>(type.
find(ID_enum_underlying_type));
1198 std::ostringstream msg;
1199 msg << source_location <<
": non-integral type '"
1200 << underlying_type.
get(ID_C_c_type)
1201 <<
"' is an invalid underlying type";
1208 mp_integer value=0, min_value=0, max_value=0;
1210 std::list<c_enum_typet::c_enum_membert> enum_members;
1234 tmp_v.
id() == ID_constant &&
1241 error() <<
"enum is not a constant" <<
eom;
1251 typet constant_type;
1253 if(have_underlying_type)
1255 constant_type = type.
find_type(ID_enum_underlying_type);
1258 if(value < tmp.smallest() || value > tmp.largest())
1260 std::ostringstream msg;
1263 <<
": enumerator value is not representable in the underlying type '"
1264 << constant_type.
get(ID_C_c_type) <<
"'";
1275 declaration.
type()=constant_type;
1291 enum_members.push_back(member);
1301 bool is_packed=type.
get_bool(ID_C_packed);
1306 if(have_underlying_type)
1317 std::size_t width = underlying_type.
get_width();
1318 for(
auto &member : enum_members)
1330 std::string anon_identifier=
"#anon_enum";
1332 for(
const auto &member : enum_members)
1334 anon_identifier+=
'$';
1335 anon_identifier+=
id2string(member.get_base_name());
1336 anon_identifier+=
'=';
1337 anon_identifier+=
id2string(member.get_value());
1341 anon_identifier+=
"#packed";
1343 type.
add(ID_tag).
set(ID_identifier, anon_identifier);
1354 enum_tag_symbol.
type=type;
1355 enum_tag_symbol.
location=source_location;
1358 enum_tag_symbol.
name=identifier;
1363 for(
const auto &member : enum_members)
1364 body.push_back(member);
1369 symbol_tablet::symbolst::const_iterator s_it=
1375 const symbolt &symbol=s_it->second;
1377 if(symbol.
type.
id() != ID_c_enum)
1380 error() <<
"use of tag that does not match previous declaration" <<
eom;
1395 if(!base_name.
empty())
1398 error() <<
"redeclaration of enum tag" <<
eom;
1410 type.
id(ID_c_enum_tag);
1412 type.
set(ID_identifier, identifier);
1422 error() <<
"anonymous enum tag without members" <<
eom;
1429 warning() <<
"ignoring specification of underlying type for enum" <<
eom;
1439 symbol_tablet::symbolst::const_iterator s_it=
1445 const symbolt &symbol=s_it->second;
1447 if(symbol.
type.
id() != ID_c_enum)
1450 error() <<
"use of tag that does not match previous declaration" <<
eom;
1458 new_type.
add(ID_tag)=tag;
1464 enum_tag_symbol.
type=new_type;
1465 enum_tag_symbol.
location=source_location;
1468 enum_tag_symbol.
name=identifier;
1494 error() <<
"failed to convert bit field width" <<
eom;
1501 error() <<
"bit field width is negative" <<
eom;
1505 type.
set_width(numeric_cast_v<std::size_t>(i));
1511 std::size_t sub_width=0;
1513 if(subtype.
id()==ID_bool)
1518 else if(subtype.
id()==ID_signedbv ||
1519 subtype.
id()==ID_unsignedbv ||
1520 subtype.
id()==ID_c_bool)
1524 else if(subtype.
id()==ID_c_enum_tag)
1529 const auto &c_enum_type =
1532 if(c_enum_type.is_incomplete())
1535 error() <<
"bit field has incomplete enum type" <<
eom;
1544 error() <<
"bit field with non-integer type: "
1552 error() <<
"bit field (" << i
1553 <<
" bits) larger than type (" << sub_width <<
" bits)"
1566 c_qualifiers.
read(type);
1568 const auto &as_expr = (
const exprt &)type;
1570 if(!as_expr.has_operands())
1582 if(expr.
id()==ID_address_of &&
1592 c_qualifiers.
write(type);
1599 symbol_tablet::symbolst::const_iterator s_it =
1605 error() <<
"typedef symbol '" << identifier <<
"' not found" <<
eom;
1609 const symbolt &symbol = s_it->second;
1614 error() <<
"expected type symbol for typedef" <<
eom;
1621 bool is_packed = type.
get_bool(ID_C_packed);
1626 c_qualifiers.
write(type);
1629 type.
set(ID_C_packed,
true);
1646 if(type.
id()==ID_array)
1652 else if(type.
id()==ID_code)
1660 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()
bitvector_typet index_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()
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 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)
virtual void read(const typet &type)
virtual void write(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
const exprt & size() const
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' (...
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
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)
symbol_tablet & symbol_table
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)
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.
irept & add(const irep_namet &name)
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
void set(const irep_namet &name, const irep_idt &value)
bool get_bool(const irep_namet &name) const
const irept & find(const irep_namet &name) const
const irep_idt & id() const
void remove(const irep_namet &name)
const irep_idt & get(const irep_namet &name) const
source_locationt source_location
mstreamt & warning() const
mstreamt & result() const
message_handlert & get_message_handler()
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)
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.
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.
irep_idt pretty_name
Language-specific display name.
exprt value
Initial value of symbol.
void set_identifier(const irep_idt &identifier)
const irep_idt & get_identifier() 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.
const source_locationt & source_location() const
const typet & subtype() const
const typet & find_type(const irep_namet &name) const
typet & add_type(const irep_namet &name)
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 address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
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 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.
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 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?