CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
c_types_util.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: API to expression classes
4
5Author: Malte Mues <mail.mues@gmail.com>
6
7\*******************************************************************/
8
9#ifndef CPROVER_UTIL_C_TYPES_UTIL_H
10#define CPROVER_UTIL_C_TYPES_UTIL_H
11
15
16#include "arith_tools.h"
17#include "invariant.h"
18#include "std_types.h"
19#include "type.h"
20
21#include <algorithm>
22#include <string>
23
25inline bool is_c_char_type(const typet &type)
26{
27 const irep_idt &c_type = type.get(ID_C_c_type);
31}
32
35inline bool is_c_bool_type(const typet &type)
36{
37 return type.id() == ID_c_bool;
38}
39
54
57inline bool is_c_char_pointer_type(const typet &type)
58{
59 return type.id() == ID_pointer &&
60 is_c_char_type(to_pointer_type(type).base_type());
61}
62
66inline bool is_c_integral_pointer_type(const typet &type)
67{
68 return type.id() == ID_pointer &&
69 is_c_integral_type(to_pointer_type(type).base_type());
70}
71
74inline bool is_c_enum_type(const typet &type)
75{
76 return type.id() == ID_c_enum;
77}
78
86 const irep_idt &member_name,
87 const c_enum_typet &c_enum)
88{
89 for(const auto &enum_value : c_enum.members())
90 {
91 if(enum_value.get_identifier() == member_name)
92 {
94 constant_exprt{enum_value.get_value(), typet{ID_bv}});
95 CHECK_RETURN(maybe_int_value.has_value());
97 }
98 }
99 INVARIANT(false, "member_name must be a valid value in the c_enum.");
100}
101
105inline bool id2boolean(const std::string &bool_value)
106{
107 std::string string_value = bool_value;
108 std::transform(
109 string_value.begin(), string_value.end(), string_value.begin(), ::tolower);
110 if(string_value == "true")
111 return true;
112 if(string_value == "false")
113 return false;
115}
116
123{
124 return bool_value ? from_integer(mp_integer(1), type)
125 : from_integer(mp_integer(0), type);
126}
127#endif // CPROVER_UTIL_C_TYPES_UTIL_H
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
bool is_signed_or_unsigned_bitvector(const typet &type)
This method tests, if the given typet is a signed or unsigned bitvector.
bool is_c_char_pointer_type(const typet &type)
This function checks, whether type is a pointer and the target type of the pointer has been a char ty...
bool is_c_integral_type(const typet &type)
This function checks, whether the type has been some kind of integer type in the c program.
bool is_c_integral_pointer_type(const typet &type)
This function checks, whether type is a pointer and the target type has been some kind of int type in...
bool is_c_enum_type(const typet &type)
This function checks, whether the type has been an enum type in the c program.
bool id2boolean(const std::string &bool_value)
Convert id to a Boolean value.
bool is_c_bool_type(const typet &type)
This function checks, whether the type has been a bool type in the c program.
constant_exprt from_c_boolean_value(bool bool_value, const typet &type)
This function creates a constant representing either 0 or 1 as value of type type.
constant_exprt convert_member_name_to_enum_value(const irep_idt &member_name, const c_enum_typet &c_enum)
This function creates a constant representing the bitvector encoded integer value of a string in the ...
bool is_c_char_type(const typet &type)
This function checks, whether this has been a char type in the c program.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
The type of C enums.
Definition c_types.h:239
A constant literal expression.
Definition std_expr.h:3117
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
const irep_idt & get(const irep_idt &name) const
Definition irep.cpp:44
const irep_idt & id() const
Definition irep.h:388
The type of an expression, extends irept.
Definition type.h:29
int tolower(int c)
Definition ctype.c:109
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
BigInt mp_integer
Definition smt_terms.h:17
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
Pre-defined types.
Defines typet, type_with_subtypet and type_with_subtypest.