1
- // Copyright 2018 Author: Malte Mues <[email protected] >
1
+ /* ******************************************************************\
2
+
3
+ Module: API to expression classes
4
+
5
+ Author: Malte Mues <[email protected] >
6
+
7
+ \*******************************************************************/
2
8
3
- // / \file
4
- // / This file contains functions, that should support test for underlying
5
- // / c types, in cases where this is required for anlysis purpose.
6
9
#ifndef CPROVER_UTIL_C_TYPES_UTIL_H
7
10
#define CPROVER_UTIL_C_TYPES_UTIL_H
8
11
12
+ // / \file
13
+ // / This file contains functions, that should support test for underlying
14
+ // / c types, in cases where this is required for analysis purpose.
15
+
9
16
#include " arith_tools.h"
10
17
#include " invariant.h"
11
18
#include " std_types.h"
15
22
#include < string>
16
23
17
24
// / This function checks, whether this has been a char type in the c program.
18
- inline bool is_c_char (const typet &type)
25
+ inline bool is_c_char_type (const typet &type)
19
26
{
20
27
const irep_idt &c_type = type.get (ID_C_c_type);
21
28
return is_signed_or_unsigned_bitvector (type) &&
@@ -25,16 +32,16 @@ inline bool is_c_char(const typet &type)
25
32
26
33
// / This function checks, whether the type
27
34
// / has been a bool type in the c program.
28
- inline bool is_c_bool (const typet &type)
35
+ inline bool is_c_bool_type (const typet &type)
29
36
{
30
37
return type.id () == ID_c_bool;
31
38
}
32
39
33
- // / This function checks, whether the type is has been some kind of integer
40
+ // / This function checks, whether the type has been some kind of integer
34
41
// / type in the c program.
35
42
// / It considers the signed and unsigned verison of
36
43
// / int, short, long and long long as integer types in c.
37
- inline bool is_c_int_derivate (const typet &type)
44
+ inline bool is_c_integral_type (const typet &type)
38
45
{
39
46
const irep_idt &c_type = type.get (ID_C_c_type);
40
47
return is_signed_or_unsigned_bitvector (type) &&
@@ -47,63 +54,72 @@ inline bool is_c_int_derivate(const typet &type)
47
54
48
55
// / This function checks, whether type is a pointer and the target type
49
56
// / of the pointer has been a char type in the c program.
50
- inline bool is_c_char_pointer (const typet &type)
57
+ inline bool is_c_char_pointer_type (const typet &type)
51
58
{
52
- return type.id () == ID_pointer && is_c_char (type.subtype ());
59
+ return type.id () == ID_pointer && is_c_char_type (type.subtype ());
53
60
}
54
61
55
62
// / This function checks, whether type is a pointer and the target type
56
63
// / has been some kind of int type in the c program.
57
64
// / is_c_int_derivate answers is used for checking the int type.
58
- inline bool is_c_int_derivate_pointer (const typet &type)
65
+ inline bool is_c_integral_pointer_type (const typet &type)
59
66
{
60
- return type.id () == ID_pointer && is_c_int_derivate (type.subtype ());
67
+ return type.id () == ID_pointer && is_c_integral_type (type.subtype ());
61
68
}
62
69
63
70
// / This function checks, whether the type
64
71
// / has been an enum type in the c program.
65
- inline bool is_c_enum (const typet &type)
72
+ inline bool is_c_enum_type (const typet &type)
66
73
{
67
74
return type.id () == ID_c_enum;
68
75
}
69
76
70
77
// / This function creates a constant representing the
71
78
// / bitvector encoded integer value of a string in the enum.
72
79
// / \param member_name is a string that should be in the enum.
73
- // / \param c_enum the enum type memeber_name is supposed to be part of.
74
- // / \return value a constant, that could be assigned as value for an expression
75
- // / with type c_enum.
80
+ // / \param c_enum the enum type \p member_name is supposed to be part of.
81
+ // / \return constant, that could be assigned as the value of an expression with
82
+ // / type c_enum.
76
83
constant_exprt convert_member_name_to_enum_value (
77
- const std::string &member_name,
84
+ const irep_idt &member_name,
78
85
const c_enum_typet &c_enum)
79
86
{
80
87
for (const auto &enum_value : c_enum.members ())
81
88
{
82
- if (id2string ( enum_value.get_identifier () ) == member_name)
89
+ if (enum_value.get_identifier () == member_name)
83
90
{
84
- mp_integer int_value = string2integer (id2string (enum_value.get_value ()));
85
- return from_integer (int_value, c_enum);
91
+ auto maybe_int_value = numeric_cast<mp_integer>(
92
+ constant_exprt{enum_value.get_value (), typet{ID_bv}});
93
+ CHECK_RETURN (maybe_int_value.has_value ());
94
+ return from_integer (*maybe_int_value, c_enum);
86
95
}
87
96
}
88
97
INVARIANT (false , " member_name must be a valid value in the c_enum." );
89
98
}
90
99
100
+ // / Convert id to a Boolean value
101
+ // / \param bool_value: A string that is compared to "true" ignoring case.
102
+ // / \return a constant of type Boolean
103
+ bool id2boolean (const irep_idt &bool_value)
104
+ {
105
+ std::string string_value = id2string (bool_value);
106
+ std::transform (
107
+ string_value.begin (), string_value.end (), string_value.begin (), ::tolower);
108
+ if (string_value == " true" )
109
+ return true ;
110
+ if (string_value == " false" )
111
+ return false ;
112
+ UNREACHABLE;
113
+ }
114
+
91
115
// / This function creates a constant representing either 0 or 1 as value of
92
116
// / type type.
93
- // / \param bool_value: A string that is compared to "true" ignoring case .
117
+ // / \param bool_value: A Boolean value .
94
118
// / \param type: The type, the resulting constant is supposed to have.
95
119
// / \return a constant of type \param type with either 0 or 1 as value.
96
- constant_exprt from_c_boolean_value (std::string bool_value, const typet &type)
120
+ constant_exprt from_c_boolean_value (bool bool_value, const typet &type)
97
121
{
98
- std::transform (
99
- bool_value.begin (), bool_value.end (), bool_value.begin (), ::tolower);
100
- if (bool_value == " true" )
101
- {
102
- return from_integer (mp_integer (1 ), type);
103
- }
104
- else
105
- {
106
- return from_integer (mp_integer (0 ), type);
107
- }
122
+ return bool_value ? from_integer (mp_integer (1 ), type)
123
+ : from_integer (mp_integer (0 ), type);
108
124
}
109
125
#endif // CPROVER_UTIL_C_TYPES_UTIL_H
0 commit comments