14 #include <unordered_set>
38 ansi_c_convert_type.
read(type);
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)
137 assert(underlying_type.
id()==ID_signedbv ||
138 underlying_type.
id()==ID_unsignedbv);
141 if(underlying_type.
id()==ID_signedbv ||
142 underlying_type.
id()==ID_unsignedbv)
148 if(gcc_attr_mode ==
"__QI__")
155 else if(gcc_attr_mode ==
"__byte__")
162 else if(gcc_attr_mode ==
"__HI__")
169 else if(gcc_attr_mode ==
"__SI__")
176 else if(gcc_attr_mode ==
"__word__")
183 else if(gcc_attr_mode ==
"__pointer__")
190 else if(gcc_attr_mode ==
"__DI__")
209 else if(gcc_attr_mode ==
"__TI__")
216 else if(gcc_attr_mode ==
"__V2SI__")
227 else if(gcc_attr_mode ==
"__V4SI__")
253 else if(underlying_type.
id()==ID_floatbv)
257 if(gcc_attr_mode ==
"__SF__")
259 else if(gcc_attr_mode ==
"__DF__")
261 else if(gcc_attr_mode ==
"__TF__")
263 else if(gcc_attr_mode ==
"__V2SF__")
265 else if(gcc_attr_mode ==
"__V2DF__")
267 else if(gcc_attr_mode ==
"__V4SF__")
269 else if(gcc_attr_mode ==
"__V4DF__")
279 else if(underlying_type.
id()==ID_complex)
284 if(gcc_attr_mode ==
"__SC__")
286 else if(gcc_attr_mode ==
"__DC__")
288 else if(gcc_attr_mode ==
"__TC__")
301 error() <<
"attribute mode '" << gcc_attr_mode
302 <<
"' applied to inappropriate type '" <<
to_string(type) <<
"'"
310 if(type.
get_bool(ID_C_restricted) &&
311 type.
id()!=ID_pointer &&
315 error() <<
"only a pointer can be 'restrict'" <<
eom;
324 static_cast<const exprt &
>(type.
find(ID_size));
334 error() <<
"failed to convert bit vector width to constant" <<
eom;
341 error() <<
"bit vector width invalid" <<
eom;
350 if(type.
id()==ID_custom_unsignedbv)
351 type.
id(ID_unsignedbv);
352 else if(type.
id()==ID_custom_signedbv)
353 type.
id(ID_signedbv);
354 else if(type.
id()==ID_custom_fixedbv)
359 static_cast<const exprt &
>(type.
find(ID_f));
372 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
376 if(f_int<0 || f_int>size_int)
379 error() <<
"fixedbv fraction width invalid" <<
eom;
386 else if(type.
id()==ID_custom_floatbv)
391 static_cast<const exprt &
>(type.
find(ID_f));
404 error() <<
"failed to convert number of fraction bits to constant" <<
eom;
408 if(f_int<1 || f_int+1>=size_int)
411 error() <<
"floatbv fraction width invalid" <<
eom;
431 if(parameters.empty())
438 if(type.
parameters().back().id()==ID_ellipsis)
449 if(param.id()==ID_declaration)
459 std::list<codet> tmp_clean_code;
469 if(identifier.
empty())
484 param.
swap(parameter);
490 if(parameters.size() == 1 && parameters[0].type().id() == ID_empty)
505 if(decl_return_type.
id() == ID_array)
508 error() <<
"function must not return array" <<
eom;
512 if(decl_return_type.
id() == ID_code)
515 error() <<
"function must not return function type" <<
eom;
540 (followed_subtype.
id() == ID_struct || followed_subtype.
id() == ID_union) &&
545 error() <<
"array has incomplete element type" <<
eom;
554 error() <<
"array of function element type" <<
eom;
578 error() <<
"failed to convert constant: "
586 error() <<
"array size must not be negative, "
587 "but got " << s <<
eom;
593 else if(tmp_size.
id()==ID_infinity)
597 else if(tmp_size.
id()==ID_symbol &&
618 <<
"' is not constant" <<
eom;
626 size_source_location,
629 new_symbol.
type.
set(ID_C_constant,
true);
639 assignment.
lhs()=symbol_expr;
640 assignment.
rhs() = new_symbol.
value;
669 if(subtype.
id()!=ID_signedbv &&
670 subtype.
id()!=ID_unsignedbv &&
671 subtype.
id()!=ID_floatbv &&
672 subtype.
id()!=ID_fixedbv)
675 error() <<
"cannot make a vector of subtype "
686 error() <<
"failed to convert constant: "
694 error() <<
"vector size must be positive, "
695 "but got " << s <<
eom;
702 if(!sub_size_expr_opt.has_value())
705 error() <<
"failed to determine size of vector base type '"
710 simplify(sub_size_expr_opt.value(), *
this);
712 const auto sub_size = numeric_cast<mp_integer>(sub_size_expr_opt.value());
714 if(!sub_size.has_value())
717 error() <<
"failed to determine size of vector base type '"
730 if(s % *sub_size != 0)
733 error() <<
"vector size (" << s
734 <<
") expected to be multiple of base type size (" << *sub_size
763 remove_qualifiers.
write(type);
765 bool is_packed = type.
get_bool(ID_C_packed);
776 compound_symbol.
type=type;
781 std::string typestr =
type2name(compound_symbol.
type, *
this);
782 compound_symbol.
base_name =
"#anon#" + typestr;
783 compound_symbol.
name=
"tag-#anon#"+typestr;
784 identifier=compound_symbol.
name;
797 identifier=type.
find(ID_tag).
get(ID_identifier);
800 symbol_tablet::symbolst::const_iterator s_it=
808 type.
set(ID_tag, base_name);
812 compound_symbol.
name=identifier;
814 compound_symbol.
type=type;
837 s_it->second.type.id() == type.
id() &&
845 type.
set(ID_tag, base_name);
851 else if(s_it->second.type.id() != type.
id())
854 error() <<
"redefinition of '" << s_it->second.pretty_name <<
"'"
855 <<
" as different kind of tag" <<
eom;
861 error() <<
"redefinition of body of '" << s_it->second.pretty_name
870 if(type.
id() == ID_union)
872 else if(type.
id() == ID_struct)
880 original_qualifiers.
write(type);
883 type.
set(ID_C_packed,
true);
894 old_components.swap(components);
897 for(
auto &decl : old_components)
900 assert(decl.id()==ID_declaration);
908 new_component.
id(ID_static_assert);
911 assert(new_component.
operands().size()==2);
912 components.push_back(new_component);
920 for(
const auto &declarator : declaration.
declarators())
923 declarator.get_base_name(), declaration.
full_type(declarator));
929 : declarator.source_location();
937 (new_component.
type().
id()!=ID_array ||
941 error() <<
"incomplete type not permitted here" <<
eom;
945 if(new_component.
type().
id() == ID_empty)
948 error() <<
"void-typed member not permitted" <<
eom;
952 components.push_back(new_component);
957 unsigned anon_member_counter=0;
960 for(
auto &member : components)
962 if(!member.get_name().empty())
966 member.set_anonymous(
true);
972 std::unordered_set<irep_idt> members;
974 for(
const auto &c : components)
976 if(!members.insert(c.get_name()).second)
979 error() <<
"duplicate member '" << c.get_name() <<
'\'' <<
eom;
988 if(type.
id()==ID_struct ||
991 for(struct_union_typet::componentst::iterator
992 it=components.begin();
993 it!=components.end();
996 typet &c_type=it->type();
998 if(c_type.
id()==ID_array &&
1002 if(type.
id()==ID_struct && it!=--components.
end())
1005 error() <<
"flexible struct member must be last member" <<
eom;
1010 c_type.
id(ID_array);
1020 if(type.
id()==ID_struct)
1022 else if(type.
id()==ID_union)
1027 for(struct_typet::componentst::iterator
1028 it=components.begin();
1029 it!=components.end();
1032 if(it->type().id()==ID_c_bit_field &&
1034 it=components.erase(it);
1040 for(struct_union_typet::componentst::iterator
1041 it=components.begin();
1042 it!=components.end();
1045 if(it->id()==ID_static_assert)
1050 error() <<
"static_assert not supported in compound body" <<
eom;
1063 error() <<
"failed _Static_assert" <<
eom;
1071 it=components.erase(it);
1113 bool is_packed)
const
1185 const bool have_underlying_type =
1188 if(have_underlying_type)
1192 const typet &underlying_type =
1193 static_cast<const typet &
>(type.
find(ID_enum_underlying_type));
1197 std::ostringstream msg;
1198 msg << source_location <<
": non-integral type '"
1199 << underlying_type.
get(ID_C_c_type)
1200 <<
"' is an invalid underlying type";
1207 mp_integer value=0, min_value=0, max_value=0;
1209 std::list<c_enum_typet::c_enum_membert> enum_members;
1233 tmp_v.
id() == ID_constant &&
1240 error() <<
"enum is not a constant" <<
eom;
1250 typet constant_type;
1252 if(have_underlying_type)
1254 constant_type = type.
find_type(ID_enum_underlying_type);
1257 if(value < tmp.smallest() || value > tmp.largest())
1259 std::ostringstream msg;
1262 <<
": enumerator value is not representable in the underlying type '"
1263 << constant_type.
get(ID_C_c_type) <<
"'";
1274 declaration.
type()=constant_type;
1290 enum_members.push_back(member);
1300 bool is_packed=type.
get_bool(ID_C_packed);
1305 if(have_underlying_type)
1316 std::size_t width = underlying_type.
get_width();
1317 for(
auto &member : enum_members)
1329 std::string anon_identifier=
"#anon_enum";
1331 for(
const auto &member : enum_members)
1333 anon_identifier+=
'$';
1334 anon_identifier+=
id2string(member.get_base_name());
1335 anon_identifier+=
'=';
1336 anon_identifier+=
id2string(member.get_value());
1340 anon_identifier+=
"#packed";
1342 type.
add(ID_tag).
set(ID_identifier, anon_identifier);
1353 enum_tag_symbol.
type=type;
1354 enum_tag_symbol.
location=source_location;
1357 enum_tag_symbol.
name=identifier;
1362 for(
const auto &member : enum_members)
1363 body.push_back(member);
1368 symbol_tablet::symbolst::const_iterator s_it=
1374 const symbolt &symbol=s_it->second;
1376 if(symbol.
type.
id() != ID_c_enum)
1379 error() <<
"use of tag that does not match previous declaration" <<
eom;
1394 if(!base_name.
empty())
1397 error() <<
"redeclaration of enum tag" <<
eom;
1409 type.
id(ID_c_enum_tag);
1411 type.
set(ID_identifier, identifier);
1421 error() <<
"anonymous enum tag without members" <<
eom;
1428 warning() <<
"ignoring specification of underlying type for enum" <<
eom;
1438 symbol_tablet::symbolst::const_iterator s_it=
1444 const symbolt &symbol=s_it->second;
1446 if(symbol.
type.
id() != ID_c_enum)
1449 error() <<
"use of tag that does not match previous declaration" <<
eom;
1457 new_type.
add(ID_tag)=tag;
1463 enum_tag_symbol.
type=new_type;
1464 enum_tag_symbol.
location=source_location;
1467 enum_tag_symbol.
name=identifier;
1493 error() <<
"failed to convert bit field width" <<
eom;
1500 error() <<
"bit field width is negative" <<
eom;
1504 type.
set_width(numeric_cast_v<std::size_t>(i));
1510 std::size_t sub_width=0;
1512 if(subtype.
id()==ID_bool)
1517 else if(subtype.
id()==ID_signedbv ||
1518 subtype.
id()==ID_unsignedbv ||
1519 subtype.
id()==ID_c_bool)
1523 else if(subtype.
id()==ID_c_enum_tag)
1528 const auto &c_enum_type =
1531 if(c_enum_type.is_incomplete())
1534 error() <<
"bit field has incomplete enum type" <<
eom;
1543 error() <<
"bit field with non-integer type: "
1551 error() <<
"bit field (" << i
1552 <<
" bits) larger than type (" << sub_width <<
" bits)"
1565 c_qualifiers.
read(type);
1567 const auto &as_expr = (
const exprt &)type;
1569 if(!as_expr.has_operands())
1581 if(expr.
id()==ID_address_of &&
1591 c_qualifiers.
write(type);
1598 symbol_tablet::symbolst::const_iterator s_it =
1604 error() <<
"typedef symbol '" << identifier <<
"' not found" <<
eom;
1608 const symbolt &symbol = s_it->second;
1613 error() <<
"expected type symbol for typedef" <<
eom;
1620 bool is_packed = type.
get_bool(ID_C_packed);
1625 c_qualifiers.
write(type);
1628 type.
set(ID_C_packed,
true);
1645 if(type.
id()==ID_array)
1651 else if(type.
id()==ID_code)
1659 else if(type.
id()==ID_KnR)
ANSI-C Language Conversion.
ansi_c_declarationt & to_ansi_c_declaration(exprt &expr)
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()
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.
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 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 c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_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.
const integer_bitvector_typet & to_integer_bitvector_type(const typet &type)
Cast a typet to an integer_bitvector_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?