CBMC
c_types_util.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: API to expression classes
4 
5 Author: 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 
25 inline bool is_c_char_type(const typet &type)
26 {
27  const irep_idt &c_type = type.get(ID_C_c_type);
28  return is_signed_or_unsigned_bitvector(type) &&
29  (c_type == ID_char || c_type == ID_unsigned_char ||
30  c_type == ID_signed_char);
31 }
32 
35 inline bool is_c_bool_type(const typet &type)
36 {
37  return type.id() == ID_c_bool;
38 }
39 
44 inline bool is_c_integral_type(const typet &type)
45 {
46  const irep_idt &c_type = type.get(ID_C_c_type);
47  return is_signed_or_unsigned_bitvector(type) &&
48  (c_type == ID_signed_int || c_type == ID_unsigned_int ||
49  c_type == ID_signed_short_int || c_type == ID_unsigned_int ||
50  c_type == ID_unsigned_short_int || c_type == ID_signed_long_int ||
51  c_type == ID_signed_long_long_int || c_type == ID_unsigned_long_int ||
52  c_type == ID_unsigned_long_long_int);
53 }
54 
57 inline 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 
66 inline 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 
74 inline 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  {
93  auto maybe_int_value = numeric_cast<mp_integer>(
94  constant_exprt{enum_value.get_value(), typet{ID_bv}});
95  CHECK_RETURN(maybe_int_value.has_value());
96  return from_integer(*maybe_int_value, c_enum);
97  }
98  }
99  INVARIANT(false, "member_name must be a valid value in the c_enum.");
100 }
101 
105 inline bool id2boolean(const std::string &bool_value)
106 {
107  std::string string_value = bool_value;
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;
114  UNREACHABLE;
115 }
116 
122 inline constant_exprt from_c_boolean_value(bool bool_value, const typet &type)
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
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
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...
Definition: c_types_util.h:57
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.
Definition: c_types_util.h:44
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...
Definition: c_types_util.h:66
bool is_c_enum_type(const typet &type)
This function checks, whether the type has been an enum type in the c program.
Definition: c_types_util.h:74
bool id2boolean(const std::string &bool_value)
Convert id to a Boolean value.
Definition: c_types_util.h:105
bool is_c_bool_type(const typet &type)
This function checks, whether the type has been a bool type in the c program.
Definition: c_types_util.h:35
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.
Definition: c_types_util.h:122
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 ...
Definition: c_types_util.h:85
bool is_c_char_type(const typet &type)
This function checks, whether this has been a char type in the c program.
Definition: c_types_util.h:25
The type of C enums.
Definition: c_types.h:239
memberst & members()
Definition: c_types.h:283
A constant literal expression.
Definition: std_expr.h:2990
const irep_idt & get_value() const
Definition: std_expr.h:2998
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.
Definition: pointer_expr.h:93
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.