Skip to content

Commit 4b05bde

Browse files
author
svorenova
committed
Move functions from type to std_types
1 parent f3dd86d commit 4b05bde

File tree

5 files changed

+92
-100
lines changed

5 files changed

+92
-100
lines changed

jbmc/src/java_bytecode/ci_lazy_methods_needed.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ Author: Chris Smowton, [email protected]
1212
#ifndef CPROVER_JAVA_BYTECODE_CI_LAZY_METHODS_H
1313
#define CPROVER_JAVA_BYTECODE_CI_LAZY_METHODS_H
1414

15-
#include <vector>
1615
#include <set>
1716
#include <unordered_set>
17+
#include <util/namespace.h>
1818
#include <util/symbol_table.h>
19+
#include <vector>
1920

2021
class select_pointer_typet;
2122
class pointer_typet;

src/util/std_types.cpp

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include "std_types.h"
1414

15-
#include "string2int.h"
1615
#include "arith_tools.h"
16+
#include "namespace.h"
1717
#include "std_expr.h"
18+
#include "string2int.h"
1819

1920
std::size_t fixedbv_typet::get_integer_bits() const
2021
{
@@ -195,3 +196,84 @@ constant_exprt unsignedbv_typet::largest_expr() const
195196
{
196197
return from_integer(largest(), *this);
197198
}
199+
200+
/// Returns true if the type is a rational, real, integer, natural, complex,
201+
/// unsignedbv, signedbv, floatbv or fixedbv.
202+
bool is_number(const typet &type)
203+
{
204+
const irep_idt &id = type.id();
205+
return id == ID_rational || id == ID_real || id == ID_integer ||
206+
id == ID_natural || id == ID_complex || id == ID_unsignedbv ||
207+
id == ID_signedbv || id == ID_floatbv || id == ID_fixedbv;
208+
}
209+
210+
/// Identify whether a given type is constant itself or contains constant
211+
/// components.
212+
/// Examples include:
213+
/// - const int a;
214+
/// - struct contains_constant_pointer { int x; int * const p; };
215+
/// - const int b[3];
216+
/// \param type The type we want to query constness of.
217+
/// \param ns The namespace, needed for resolution of symbols.
218+
/// \return Whether passed in type is const or not.
219+
bool is_constant_or_has_constant_components(
220+
const typet &type,
221+
const namespacet &ns)
222+
{
223+
// Helper function to avoid the code duplication in the branches
224+
// below.
225+
const auto has_constant_components = [&ns](const typet &subtype) -> bool {
226+
if(subtype.id() == ID_struct || subtype.id() == ID_union)
227+
{
228+
const auto &struct_union_type = to_struct_union_type(subtype);
229+
for(const auto &component : struct_union_type.components())
230+
{
231+
if(is_constant_or_has_constant_components(component.type(), ns))
232+
return true;
233+
}
234+
}
235+
return false;
236+
};
237+
238+
// There are 4 possibilities the code below is handling.
239+
// The possibilities are enumerated as comments, to show
240+
// what each code is supposed to be handling. For more
241+
// comprehensive test case for this, take a look at
242+
// regression/cbmc/no_nondet_static/main.c
243+
244+
// const int a;
245+
if(type.get_bool(ID_C_constant))
246+
return true;
247+
248+
// This is a termination condition to break the recursion
249+
// for recursive types such as the following:
250+
// struct list { const int datum; struct list * next; };
251+
// NOTE: the difference between this condition and the previous
252+
// one is that this one always returns.
253+
if(type.id() == ID_pointer)
254+
return type.get_bool(ID_C_constant);
255+
256+
// When we have a case like the following, we don't immediately
257+
// see the struct t. Instead, we only get to see symbol t1, which
258+
// we have to use the namespace to resolve to its definition:
259+
// struct t { const int a; };
260+
// struct t t1;
261+
if(type.id() == ID_symbol_type)
262+
{
263+
const auto &resolved_type = ns.follow(type);
264+
return has_constant_components(resolved_type);
265+
}
266+
267+
// In a case like this, where we see an array (b[3] here), we know that
268+
// the array contains subtypes. We get the first one, and
269+
// then resolve it to its definition through the usage of the namespace.
270+
// struct contains_constant_pointer { int x; int * const p; };
271+
// struct contains_constant_pointer b[3] = { {23, &y}, {23, &y}, {23, &y} };
272+
if(type.has_subtype())
273+
{
274+
const auto &subtype = type.subtype();
275+
return is_constant_or_has_constant_components(subtype, ns);
276+
}
277+
278+
return false;
279+
}

src/util/std_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
2121
#include <unordered_map>
2222

2323
class constant_exprt;
24+
class namespacet;
2425

2526
/// The Boolean type
2627
class bool_typet:public typet
@@ -1708,4 +1709,10 @@ inline mathematical_function_typet &
17081709
return static_cast<mathematical_function_typet &>(type);
17091710
}
17101711

1712+
bool is_number(const typet &type);
1713+
1714+
bool is_constant_or_has_constant_components(
1715+
const typet &type,
1716+
const namespacet &ns);
1717+
17111718
#endif // CPROVER_UTIL_STD_TYPES_H

src/util/type.cpp

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Author: Daniel Kroening, [email protected]
1111
/// Implementations of some functions of typet
1212

1313
#include "type.h"
14-
#include "std_types.h"
15-
#include "namespace.h"
1614

1715
/// Copy the provided type to the subtypes of this type.
1816
/// \param type The type to add to subtypes
@@ -30,91 +28,3 @@ void typet::move_to_subtypes(typet &type)
3028
sub.push_back(static_cast<const typet &>(get_nil_irep()));
3129
sub.back().swap(type);
3230
}
33-
34-
/// Returns true if the type is a rational, real, integer, natural, complex,
35-
/// unsignedbv, signedbv, floatbv or fixedbv.
36-
bool is_number(const typet &type)
37-
{
38-
const irep_idt &id=type.id();
39-
return id==ID_rational ||
40-
id==ID_real ||
41-
id==ID_integer ||
42-
id==ID_natural ||
43-
id==ID_complex ||
44-
id==ID_unsignedbv ||
45-
id==ID_signedbv ||
46-
id==ID_floatbv ||
47-
id==ID_fixedbv;
48-
}
49-
50-
/// Identify whether a given type is constant itself or contains constant
51-
/// components.
52-
/// Examples include:
53-
/// - const int a;
54-
/// - struct contains_constant_pointer { int x; int * const p; };
55-
/// - const int b[3];
56-
/// \param type The type we want to query constness of.
57-
/// \param ns The namespace, needed for resolution of symbols.
58-
/// \return Whether passed in type is const or not.
59-
bool is_constant_or_has_constant_components(
60-
const typet &type, const namespacet &ns)
61-
{
62-
// Helper function to avoid the code duplication in the branches
63-
// below.
64-
const auto has_constant_components =
65-
[&ns](const typet &subtype) -> bool
66-
{
67-
if(subtype.id() == ID_struct || subtype.id() == ID_union)
68-
{
69-
const auto &struct_union_type = to_struct_union_type(subtype);
70-
for(const auto &component : struct_union_type.components())
71-
{
72-
if(is_constant_or_has_constant_components(component.type(), ns))
73-
return true;
74-
}
75-
}
76-
return false;
77-
};
78-
79-
// There are 4 possibilities the code below is handling.
80-
// The possibilities are enumerated as comments, to show
81-
// what each code is supposed to be handling. For more
82-
// comprehensive test case for this, take a look at
83-
// regression/cbmc/no_nondet_static/main.c
84-
85-
// const int a;
86-
if(type.get_bool(ID_C_constant))
87-
return true;
88-
89-
// This is a termination condition to break the recursion
90-
// for recursive types such as the following:
91-
// struct list { const int datum; struct list * next; };
92-
// NOTE: the difference between this condition and the previous
93-
// one is that this one always returns.
94-
if(type.id()==ID_pointer)
95-
return type.get_bool(ID_C_constant);
96-
97-
// When we have a case like the following, we don't immediately
98-
// see the struct t. Instead, we only get to see symbol t1, which
99-
// we have to use the namespace to resolve to its definition:
100-
// struct t { const int a; };
101-
// struct t t1;
102-
if(type.id() == ID_symbol_type)
103-
{
104-
const auto &resolved_type = ns.follow(type);
105-
return has_constant_components(resolved_type);
106-
}
107-
108-
// In a case like this, where we see an array (b[3] here), we know that
109-
// the array contains subtypes. We get the first one, and
110-
// then resolve it to its definition through the usage of the namespace.
111-
// struct contains_constant_pointer { int x; int * const p; };
112-
// struct contains_constant_pointer b[3] = { {23, &y}, {23, &y}, {23, &y} };
113-
if(type.has_subtype())
114-
{
115-
const auto &subtype = type.subtype();
116-
return is_constant_or_has_constant_components(subtype, ns);
117-
}
118-
119-
return false;
120-
}

src/util/type.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Author: Daniel Kroening, [email protected]
1818
#define SUBTYPE_IN_GETSUB
1919
#define SUBTYPES_IN_GETSUB
2020

21-
class namespacet;
22-
2321
/// The type of an expression, extends irept. Types may have subtypes. This is
2422
/// modeled with two subs named “subtype” (a single type) and “subtypes”
2523
/// (a vector of types). The class typet only adds specialized methods
@@ -177,10 +175,4 @@ class type_with_subtypest:public typet
177175
for(typet::subtypest::iterator it=(type).subtypes().begin(); \
178176
it!=(type).subtypes().end(); ++it)
179177

180-
bool is_number(const typet &type);
181-
182-
bool is_constant_or_has_constant_components(
183-
const typet &type,
184-
const namespacet &ns);
185-
186178
#endif // CPROVER_UTIL_TYPE_H

0 commit comments

Comments
 (0)